Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SurfaceJsonConverterTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file SurfaceJsonConverterTests.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 
28 
29 #include <fstream>
30 #include <memory>
31 #include <string>
32 
33 #include <nlohmann/json.hpp>
34 
35 using namespace Acts;
36 
37 std::ofstream out;
38 
40 
41 BOOST_AUTO_TEST_SUITE(SurfaceJsonConversion)
42 
43 BOOST_AUTO_TEST_CASE(ConeSurfaceRoundTripTests) {
44  Transform3 trf(Transform3::Identity() * Translation3(0., 0., -7.));
45  auto cone = std::make_shared<ConeBounds>(0.123, 10., 100.);
46  auto coneRef = Surface::makeShared<ConeSurface>(trf, cone);
47  coneRef->assignGeometryId(GeometryIdentifier(13u));
48 
49  // Test a cone
50  nlohmann::json coneOut = SurfaceJsonConverter::toJson(gctx, *coneRef);
51  out.open("ConeSurface.json");
52  out << coneOut.dump(2);
53  out.close();
54 
55  auto in = std::ifstream("ConeSurface.json",
56  std::ifstream::in | std::ifstream::binary);
57  BOOST_CHECK(in.good());
58  nlohmann::json coneIn;
59  in >> coneIn;
60  in.close();
61 
62  auto coneTest = SurfaceJsonConverter::fromJson(coneIn);
63 
64  BOOST_CHECK(coneTest->transform(gctx).isApprox(coneRef->transform(gctx)));
65  BOOST_CHECK(coneTest->geometryId() == coneRef->geometryId());
66  BOOST_CHECK(coneTest->bounds() == coneRef->bounds());
67 }
68 
69 BOOST_AUTO_TEST_CASE(DiscSurfaceRoundTripTests) {
70  Transform3 trf(Transform3::Identity() * Translation3(0., 0., -7.));
71  auto ring = std::make_shared<RadialBounds>(0., 4.);
72  auto ringDiscRef = Surface::makeShared<DiscSurface>(trf, ring);
73  ringDiscRef->assignGeometryId(GeometryIdentifier(10u));
74 
75  // Test a disc
76  nlohmann::json discOut = SurfaceJsonConverter::toJson(gctx, *ringDiscRef);
77  out.open("DiscSurface.json");
78  out << discOut.dump(2);
79  out.close();
80 
81  auto in = std::ifstream("DiscSurface.json",
82  std::ifstream::in | std::ifstream::binary);
83  BOOST_CHECK(in.good());
84  nlohmann::json discIn;
85  in >> discIn;
86  in.close();
87 
88  auto ringDiscTest = SurfaceJsonConverter::fromJson(discIn);
89 
90  BOOST_CHECK(
91  ringDiscTest->transform(gctx).isApprox(ringDiscRef->transform(gctx)));
92  BOOST_CHECK(ringDiscTest->geometryId() == ringDiscRef->geometryId());
93  BOOST_CHECK(ringDiscTest->bounds() == ringDiscRef->bounds());
94 }
95 
96 BOOST_AUTO_TEST_CASE(CylinderSurfaceRoundTripTests) {
97  Transform3 trf(Transform3::Identity() * Translation3(0., 0., -7.));
98  auto tube = std::make_shared<CylinderBounds>(5., 20.);
99  auto cylinderRef = Surface::makeShared<CylinderSurface>(trf, tube);
100  cylinderRef->assignGeometryId(GeometryIdentifier(11u));
101 
102  // Test a cyoinder
103  nlohmann::json cylinderOut = SurfaceJsonConverter::toJson(gctx, *cylinderRef);
104  out.open("CylinderSurface.json");
105  out << cylinderOut.dump(2);
106  out.close();
107 
108  auto in = std::ifstream("CylinderSurface.json",
109  std::ifstream::in | std::ifstream::binary);
110  BOOST_CHECK(in.good());
111  nlohmann::json cylinderIn;
112  in >> cylinderIn;
113  in.close();
114 
115  auto cylinderTest = SurfaceJsonConverter::fromJson(cylinderIn);
116 
117  BOOST_CHECK(
118  cylinderTest->transform(gctx).isApprox(cylinderRef->transform(gctx)));
119  BOOST_CHECK(cylinderTest->geometryId() == cylinderRef->geometryId());
120  BOOST_CHECK(cylinderTest->bounds() == cylinderRef->bounds());
121 }
122 
123 BOOST_AUTO_TEST_CASE(PlaneSurfaceRoundTripTests) {
124  Transform3 trf(Transform3::Identity() * Translation3(0., 0., -7.));
125  auto trapezoid = std::make_shared<TrapezoidBounds>(2., 3., 4.);
126  auto trapezoidPlaneRef = Surface::makeShared<PlaneSurface>(trf, trapezoid);
127  trapezoidPlaneRef->assignGeometryId(GeometryIdentifier(9u));
128 
129  // Test a plane
130  nlohmann::json planeOut =
131  SurfaceJsonConverter::toJson(gctx, *trapezoidPlaneRef);
132  to_json(planeOut, *trapezoidPlaneRef);
133  out.open("PlaneSurface.json");
134  out << planeOut.dump(2);
135  out.close();
136 
137  auto in = std::ifstream("PlaneSurface.json",
138  std::ifstream::in | std::ifstream::binary);
139  BOOST_CHECK(in.good());
140  nlohmann::json planeIn;
141  in >> planeIn;
142  in.close();
143 
144  auto trapezoidPlaneTest = SurfaceJsonConverter::fromJson(planeIn);
145 
146  BOOST_CHECK(trapezoidPlaneTest->transform(gctx).isApprox(
147  trapezoidPlaneRef->transform(gctx)));
148  BOOST_CHECK(trapezoidPlaneTest->geometryId() ==
149  trapezoidPlaneRef->geometryId());
150  BOOST_CHECK(trapezoidPlaneTest->bounds() == trapezoidPlaneRef->bounds());
151 }
152 
153 BOOST_AUTO_TEST_CASE(StrawSurfaceRoundTripTests) {
154  Transform3 trf(Transform3::Identity() * Translation3(0., 0., -7.));
155  auto straw = std::make_shared<LineBounds>(1., 100.);
156  auto strawRef = Surface::makeShared<StrawSurface>(trf, straw);
157  strawRef->assignGeometryId(GeometryIdentifier(12u));
158 
159  // Test a straw
160  nlohmann::json strawOut = SurfaceJsonConverter::toJson(gctx, *strawRef);
161  out.open("StrawSurface.json");
162  out << strawOut.dump(2);
163  out.close();
164 
165  auto in = std::ifstream("StrawSurface.json",
166  std::ifstream::in | std::ifstream::binary);
167  BOOST_CHECK(in.good());
168  nlohmann::json strawIn;
169  in >> strawIn;
170  in.close();
171 
172  auto strawTest = SurfaceJsonConverter::fromJson(strawIn);
173 
174  BOOST_CHECK(strawTest->transform(gctx).isApprox(strawRef->transform(gctx)));
175  BOOST_CHECK(strawTest->geometryId() == strawRef->geometryId());
176  BOOST_CHECK(strawTest->bounds() == strawRef->bounds());
177 }
178 
179 BOOST_AUTO_TEST_CASE(PerigeeRoundTripTests) {
180  Transform3 trf(Transform3::Identity() * Translation3(-1., -2., -7.));
181  auto perigeeRef = Surface::makeShared<PerigeeSurface>(trf);
182  perigeeRef->assignGeometryId(GeometryIdentifier(99u));
183 
184  // Test a perigee
185  nlohmann::json perigeeOut = SurfaceJsonConverter::toJson(gctx, *perigeeRef);
186  out.open("PerigeeSurface.json");
187  out << perigeeOut.dump(2);
188  out.close();
189 
190  auto in = std::ifstream("PerigeeSurface.json",
191  std::ifstream::in | std::ifstream::binary);
192  BOOST_CHECK(in.good());
193  nlohmann::json perigeeIn;
194  in >> perigeeIn;
195  in.close();
196 
197  auto perigeeTest = SurfaceJsonConverter::fromJson(perigeeIn);
198 
199  BOOST_CHECK(
200  perigeeTest->transform(gctx).isApprox(perigeeRef->transform(gctx)));
201  BOOST_CHECK(perigeeTest->geometryId() == perigeeRef->geometryId());
202 }
203 
204 BOOST_AUTO_TEST_CASE(SurfacesDetrayTests) {
205  Transform3 trf(Transform3::Identity() * Translation3(0., 0., -7.));
206  auto trapezoid = std::make_shared<TrapezoidBounds>(2., 3., 4.);
207  auto trapezoidPlaneRef = Surface::makeShared<PlaneSurface>(trf, trapezoid);
208  trapezoidPlaneRef->assignGeometryId(GeometryIdentifier(9u));
209 
210  // Test a rectangle
211  nlohmann::json trapOut =
212  SurfaceJsonConverter::toJsonDetray(gctx, *trapezoidPlaneRef);
213  out.open("Surfaces-detray.json");
214  out << trapOut.dump(2);
215  out.close();
216 }
217 
218 BOOST_AUTO_TEST_SUITE_END()