Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
VolumeBoundsJsonConverterTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file VolumeBoundsJsonConverterTests.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2022 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 
20 
21 #include <algorithm>
22 #include <array>
23 #include <cmath>
24 #include <fstream>
25 #include <memory>
26 #include <string>
27 #include <vector>
28 
29 #include <nlohmann/json.hpp>
30 
31 using namespace Acts;
32 
33 BOOST_AUTO_TEST_SUITE(VolumeBoundsJsonConversion)
34 
36  std::ofstream out("CuboidVolumeBounds.json");
37 
38  auto cuboidRef = std::make_shared<const CuboidVolumeBounds>(2., 4., 6.);
39  nlohmann::json cuboidOut = VolumeBoundsJsonConverter::toJson(*cuboidRef);
40  out << cuboidOut.dump(2);
41  out.close();
42 
43  // Read in json file
44  auto in = std::ifstream("CuboidVolumeBounds.json",
45  std::ifstream::in | std::ifstream::binary);
46  BOOST_CHECK(in.good());
47  nlohmann::json cuboidIn;
48  in >> cuboidIn;
49  in.close();
50 
51  auto cuboidTest =
52  VolumeBoundsJsonConverter::fromJson<CuboidVolumeBounds>(cuboidIn);
53  BOOST_CHECK(cuboidRef->values() == cuboidTest->values());
54 }
55 
57  std::ofstream out("CylinderVolumeBounds.json");
58 
59  auto cylinderRef =
60  std::make_shared<const CylinderVolumeBounds>(10., 20., 30., M_PI / 4, 0);
61  nlohmann::json cylinderOut = VolumeBoundsJsonConverter::toJson(*cylinderRef);
62  out << cylinderOut.dump(2);
63  out.close();
64 
65  // Read in json file
66  auto in = std::ifstream("CylinderVolumeBounds.json",
67  std::ifstream::in | std::ifstream::binary);
68  BOOST_CHECK(in.good());
69  nlohmann::json cylinderIn;
70  in >> cylinderIn;
71  in.close();
72 
73  auto cylinderTest =
74  VolumeBoundsJsonConverter::fromJson<CylinderVolumeBounds>(cylinderIn);
75  BOOST_CHECK(cylinderRef->values() == cylinderTest->values());
76 }
77 
79  std::ofstream out("ConeVolumeBounds.json");
80 
81  auto coneRef = std::make_shared<const ConeVolumeBounds>(0., 0., 0.45, 0.050,
82  0.050, 0., M_PI);
83  nlohmann::json coneOut = VolumeBoundsJsonConverter::toJson(*coneRef);
84  out << coneOut.dump(2);
85  out.close();
86 
87  // Read in json file
88  auto in = std::ifstream("ConeVolumeBounds.json",
89  std::ifstream::in | std::ifstream::binary);
90  BOOST_CHECK(in.good());
91  nlohmann::json coneIn;
92  in >> coneIn;
93  in.close();
94 
95  auto coneTest = VolumeBoundsJsonConverter::fromJson<ConeVolumeBounds>(coneIn);
96  BOOST_CHECK(coneRef->values() == coneTest->values());
97 }
98 
99 BOOST_AUTO_TEST_CASE(CutoutCylinder) {
100  std::ofstream out("CutoutCylinderVolumeBounds.json");
101 
102  auto cutoutCylinderRef =
103  std::make_shared<const CutoutCylinderVolumeBounds>(5, 10, 15, 30, 25);
104  nlohmann::json cutoutCylinderOut =
105  VolumeBoundsJsonConverter::toJson(*cutoutCylinderRef);
106  out << cutoutCylinderOut.dump(2);
107  out.close();
108 
109  // Read in json file
110  auto in = std::ifstream("CutoutCylinderVolumeBounds.json",
111  std::ifstream::in | std::ifstream::binary);
112  BOOST_CHECK(in.good());
113  nlohmann::json cutoutCylinderIn;
114  in >> cutoutCylinderIn;
115  in.close();
116 
117  auto cutoutCylinderTest =
118  VolumeBoundsJsonConverter::fromJson<CutoutCylinderVolumeBounds>(
119  cutoutCylinderIn);
120  BOOST_CHECK(cutoutCylinderRef->values() == cutoutCylinderTest->values());
121 }
122 
123 BOOST_AUTO_TEST_CASE(GenericCuboid) {
124  std::ofstream out("GenericCuboidVolumeBounds.json");
125  std::array<Vector3, 8> vertices;
126  vertices = {{{0, 0, 0},
127  {2, 0, 0},
128  {2, 1, 0},
129  {0, 1, 0},
130  {0, 0, 1},
131  {2, 0, 1},
132  {2, 1, 1},
133  {0, 1, 1}}};
134 
135  auto genericCuboidRef =
136  std::make_shared<const GenericCuboidVolumeBounds>(vertices);
137  nlohmann::json genericCuboidOut =
138  VolumeBoundsJsonConverter::toJson(*genericCuboidRef);
139  out << genericCuboidOut.dump(2);
140  out.close();
141 
142  // Read in json file
143  auto in = std::ifstream("GenericCuboidVolumeBounds.json",
144  std::ifstream::in | std::ifstream::binary);
145  BOOST_CHECK(in.good());
146  nlohmann::json genericCuboidIn;
147  in >> genericCuboidIn;
148  in.close();
149 
150  auto genericCuboidTest = VolumeBoundsJsonConverter::fromJson(genericCuboidIn);
151  BOOST_CHECK(genericCuboidRef->values() == genericCuboidTest->values());
152 }
153 
155  std::ofstream out("TrapezoidVolumeBounds.json");
156 
157  auto trapezoidRef =
158  std::make_shared<const TrapezoidVolumeBounds>(2., 4., 6., 8.);
159  nlohmann::json trapezoidOut =
160  VolumeBoundsJsonConverter::toJson(*trapezoidRef);
161  out << trapezoidOut.dump(2);
162  out.close();
163 
164  // Read in json file
165  auto in = std::ifstream("TrapezoidVolumeBounds.json",
166  std::ifstream::in | std::ifstream::binary);
167  BOOST_CHECK(in.good());
168  nlohmann::json trapezoidIn;
169  in >> trapezoidIn;
170  in.close();
171 
172  auto trapezoidTest =
173  VolumeBoundsJsonConverter::fromJson<TrapezoidVolumeBounds>(trapezoidIn);
174  BOOST_CHECK(trapezoidRef->values() == trapezoidTest->values());
175 }
176 BOOST_AUTO_TEST_SUITE_END()