Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UtilitiesJsonConverterTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file UtilitiesJsonConverterTests.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2021 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #include <boost/test/unit_test.hpp>
10 
17 
18 #include <cmath>
19 #include <fstream>
20 #include <initializer_list>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 #include <nlohmann/json.hpp>
26 
27 #include "EqualityHelpers.hpp"
28 
29 using namespace Acts;
30 
31 BOOST_AUTO_TEST_SUITE(UtilitiesJsonConverter)
32 
33 BOOST_AUTO_TEST_CASE(BinUtilityRoundTripTests) {
34  BinUtility reference(2, 0., 4., open, binR);
35 
36  std::ofstream out;
37 
38  // Test in one dimension
39  nlohmann::json joneDimOut;
40  to_json(joneDimOut, reference);
41  out.open("BinUtility_1D.json");
42  out << joneDimOut.dump(2);
43  out.close();
44 
45  auto in = std::ifstream("BinUtility_1D.json",
46  std::ifstream::in | std::ifstream::binary);
47  BOOST_CHECK(in.good());
48  nlohmann::json joneDimIn;
49  in >> joneDimIn;
50  in.close();
51 
53  from_json(joneDimIn, test);
54 
55  BOOST_CHECK(isEqual(reference, test, 0.0001));
56 
57  // Increase to two dimensions
58  reference += BinUtility(10., -M_PI, M_PI, closed, binPhi);
59  nlohmann::json jtwoDimOut;
60  to_json(jtwoDimOut, reference);
61  out.open("BinUtility_2D.json");
62  out << jtwoDimOut.dump(2);
63  out.close();
64 
65  in = std::ifstream("BinUtility_2D.json",
66  std::ifstream::in | std::ifstream::binary);
67  BOOST_CHECK(in.good());
68  nlohmann::json jtwoDimIn;
69  in >> jtwoDimIn;
70  in.close();
71 
72  test = BinUtility();
73  from_json(jtwoDimIn, test);
74 
75  BOOST_CHECK(isEqual(reference, test, 0.0001));
76 
77  // Increase to three dimensions
78  std::vector<float> boundaries = {-4., -1.5, 0., 10.};
79  reference += BinUtility(boundaries, open, binZ);
80  nlohmann::json jthreeDimOut;
81  to_json(jthreeDimOut, reference);
82  out.open("BinUtility_3D.json");
83  out << jthreeDimOut.dump(2);
84  out.close();
85 
86  in = std::ifstream("BinUtility_3D.json",
87  std::ifstream::in | std::ifstream::binary);
88  BOOST_CHECK(in.good());
89  nlohmann::json jthreeDimIn;
90  in >> jthreeDimIn;
91  in.close();
92 
93  test = BinUtility();
94  from_json(jthreeDimIn, test);
95 
96  BOOST_CHECK(isEqual(reference, test, 0.0001));
97 
98  // One with transform
99  Transform3 t;
100  t = Eigen::AngleAxis(0.12334, Vector3(1., 2., 3).normalized());
101  t.pretranslate(Vector3(1., 2., 3.));
102 
103  auto bData = reference.binningData()[0];
104 
105  reference = BinUtility(bData, t);
106 
107  nlohmann::json jtransformOut;
108  to_json(jtransformOut, reference);
109  out.open("BinUtility_Transform.json");
110  out << jtransformOut.dump(2);
111  out.close();
112 
113  in = std::ifstream("BinUtility_Transform.json",
114  std::ifstream::in | std::ifstream::binary);
115  BOOST_CHECK(in.good());
116  nlohmann::json jtransformIn;
117  in >> jtransformIn;
118  in.close();
119 
120  test = BinUtility();
121  from_json(jtransformIn, test);
122 
123  BOOST_CHECK(isEqual(reference, test, 0.0001));
124 }
125 
126 BOOST_AUTO_TEST_CASE(Range1DRoundTrip) {
127  Range1D<ActsScalar> r(-10., 100.);
128 
129  nlohmann::json jrange;
130  jrange["range"] = r;
131 
132  Range1D<ActsScalar> rIn = jrange["range"];
133 
134  CHECK_CLOSE_ABS(rIn.min(), -10., 10e-5);
135  CHECK_CLOSE_ABS(rIn.max(), 100., 10e-5);
136 }
137 
138 BOOST_AUTO_TEST_SUITE_END()