Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PortalJsonConverterTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PortalJsonConverterTests.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2023 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 
13 #include "Acts/Detector/Portal.hpp"
20 
21 #include <fstream>
22 
23 #include <nlohmann/json.hpp>
24 
25 namespace Acts {
26 namespace Experimental {
27 class DetectorVolume {};
28 } // namespace Experimental
29 } // namespace Acts
30 
32 
33 BOOST_AUTO_TEST_SUITE(PortalJsonConverter)
34 
35 BOOST_AUTO_TEST_CASE(PortalUnconnected) {
36  std::ofstream out;
37 
38  auto surface = Acts::Surface::makeShared<Acts::PlaneSurface>(
39  Acts::Vector3(0., 0., 0.), Acts::Vector3(0., 1., 0.));
40 
42 
43  BOOST_CHECK_NE(portal, nullptr);
44 
45  auto jPortal = Acts::PortalJsonConverter::toJson(tContext, *portal, {});
46 
47  out.open("portal.json");
48  out << jPortal.dump(4);
49  out.close();
50 
51  // Now read it back in
52  auto in =
53  std::ifstream("portal.json", std::ifstream::in | std::ifstream::binary);
54  BOOST_CHECK(in.good());
55  nlohmann::json jPortalIn;
56  in >> jPortalIn;
57  in.close();
58 
59  auto portalIn = Acts::PortalJsonConverter::fromJson(tContext, jPortalIn, {});
60 
61  BOOST_CHECK_NE(portalIn, nullptr);
62 }
63 
64 BOOST_AUTO_TEST_CASE(PortalSingleConnected) {
65  std::ofstream out;
66 
67  auto forwardVolume = std::make_shared<Acts::Experimental::DetectorVolume>();
68  auto backwardVolume = std::make_shared<Acts::Experimental::DetectorVolume>();
69 
70  auto surface = Acts::Surface::makeShared<Acts::PlaneSurface>(
71  Acts::Vector3(0., 0., 0.), Acts::Vector3(0., 1., 0.));
72 
73  auto portal = Acts::Experimental::Portal::makeShared(std::move(surface));
74  BOOST_CHECK_NE(portal, nullptr);
75  // Attaching the portals
77  *portal, forwardVolume, Acts::Direction::Forward);
79  *portal, backwardVolume, Acts::Direction::Backward);
80 
81  std::vector<const Acts::Experimental::DetectorVolume*> detectorVolumes = {
82  forwardVolume.get(), backwardVolume.get()};
83  // No volumes provided, must bail
84  BOOST_CHECK_THROW(Acts::PortalJsonConverter::toJson(tContext, *portal, {}),
85  std::runtime_error);
86  auto jPortal =
87  Acts::PortalJsonConverter::toJson(tContext, *portal, detectorVolumes);
88 
89  out.open("portal-single-connected.json");
90  out << jPortal.dump(4);
91  out.close();
92 
93  // Now read it back in
94  auto in = std::ifstream("portal-single-connected.json",
95  std::ifstream::in | std::ifstream::binary);
96  BOOST_CHECK(in.good());
97  nlohmann::json jPortalIn;
98  in >> jPortalIn;
99  in.close();
100 
101  auto portalIn = Acts::PortalJsonConverter::fromJson(
102  tContext, jPortalIn, {forwardVolume, backwardVolume});
103  BOOST_CHECK_NE(portalIn, nullptr);
104 }
105 
106 BOOST_AUTO_TEST_CASE(PortalMultiConnected) {
107  std::ofstream out;
108 
109  auto forwardVolumeA = std::make_shared<Acts::Experimental::DetectorVolume>();
110  auto forwardVolumeB = std::make_shared<Acts::Experimental::DetectorVolume>();
111  auto forwardVolumeC = std::make_shared<Acts::Experimental::DetectorVolume>();
112 
113  auto backwardVolume = std::make_shared<Acts::Experimental::DetectorVolume>();
114 
115  auto surface = Acts::Surface::makeShared<Acts::PlaneSurface>(
116  Acts::Vector3(0., 0., 0.), Acts::Vector3(0., 1., 0.));
117 
118  auto portal = Acts::Experimental::Portal::makeShared(std::move(surface));
119  BOOST_CHECK_NE(portal, nullptr);
120 
121  // Attaching the portals
123  *portal, backwardVolume, Acts::Direction::Backward);
124 
126  tContext, *portal, {forwardVolumeA, forwardVolumeB, forwardVolumeC},
127  Acts::Direction::Forward, {-100, 10, 20, 200}, Acts::binX);
128 
129  std::vector<const Acts::Experimental::DetectorVolume*> detectorVolumes = {
130  forwardVolumeA.get(), forwardVolumeB.get(), forwardVolumeC.get(),
131  backwardVolume.get()};
132 
133  auto jPortal =
134  Acts::PortalJsonConverter::toJson(tContext, *portal, detectorVolumes);
135 
136  out.open("portal-multi-connected.json");
137  out << jPortal.dump(4);
138  out.close();
139 }
140 
141 BOOST_AUTO_TEST_SUITE_END()