Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DD4hepDetectorElementTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file DD4hepDetectorElementTests.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 
16 
17 #include <fstream>
18 #include <iostream>
19 
20 #include <DD4hep/DetElement.h>
21 #include <DD4hep/Detector.h>
22 
23 #include "DD4hep/DetFactoryHelper.h"
24 #include "XML/Utilities.h"
25 #include "XMLFragments.hpp"
26 
28 
29 const char* cylinder_xml =
30  R""""( <detectors> <detector id="1" name="Cylinder" type="Cylinder"> <type_flags type="DetType_TRACKER"/> <tubs name="Cylinder" rmin="10*cm" rmax="11*cm" dz="100*cm" material="Vacuum" sensitive="true"/> </detector> </detectors> )"""";
31 
32 const char* sectoral_cylinder_xml =
33  R""""( <detectors> <detector id="1" name="SectoralCylinder" type="Cylinder"> <type_flags type="DetType_TRACKER"/> <tubs name="Cylinder" rmin="10*cm" rmax="11*cm" dz="100*cm" phimin="0.1*rad" phimax="0.8*rad" material="Vacuum" sensitive="true"/> </detector> </detectors> )"""";
34 
35 const char* disc_xml =
36  R""""( <detectors> <detector id="1" name="Disc" type="Disc"> <type_flags type="DetType_TRACKER"/> <tubs name="Disc" rmin="10*cm" rmax="90*cm" dz="1*cm" material="Vacuum" sensitive="true"/> </detector> </detectors> )"""";
37 
38 const char* sectoral_disc_xml =
39  R""""( <detectors> <detector id="1" name="SectoralDisc" type="Disc"> <type_flags type="DetType_TRACKER"/> <tubs name="Disc" rmin="10*cm" rmax="90*cm" dz="1*cm" phimin="0.*rad" phimax="1.5*rad" material="Vacuum" sensitive="true"/> </detector> </detectors> )"""";
40 
41 const char* rectangle_xml =
42  R""""( <detectors> <detector id="1" name="Rectangle" type="Rectangle"> <type_flags type="DetType_TRACKER"/> <box name="Rectangle" dx="10*cm" dy="90*cm" dz="0.1*cm" cx="1.*cm" cy="2.*cm" cz="3.*cm" material="Vacuum" sensitive="true"/> </detector> </detectors> )"""";
43 
44 const char* trapezoid_xml =
45  R""""( <detectors> <detector id="1" name="Trapezoid" type="Trapezoid"> <type_flags type="DetType_TRACKER"/> <trap name="Trapezoid" x1="10*cm" x2="20*cm" dy="30*cm" dz="0.1*cm" cx="2.*cm" cy="3.*cm" cz="4.*cm" material="Vacuum" sensitive="true"/> </detector> </detectors> )"""";
46 
47 BOOST_AUTO_TEST_SUITE(DD4hepPlugin)
48 
49 BOOST_AUTO_TEST_CASE(DD4hepPluginDetectorElementCylinder) {
50  std::ofstream cxml;
51  cxml.open("Cylinder.xml");
52  cxml << head_xml;
53  cxml << cylinder_xml;
54  cxml << end_xml;
55  cxml.close();
56 
57  auto lcdd = &(dd4hep::Detector::getInstance());
58  lcdd->fromCompact("Cylinder.xml");
59  lcdd->volumeManager();
60  lcdd->apply("DD4hepVolumeManager", 0, nullptr);
61 
62  auto world = lcdd->world();
63 
64  std::shared_ptr<Acts::DD4hepDetectorElement> cylindricalElement = nullptr;
65  for (auto [chn, child] : world.children()) {
66  cylindricalElement =
67  std::make_shared<Acts::DD4hepDetectorElement>(child, "XYZ", 10.);
68  }
69 
70  BOOST_CHECK(cylindricalElement != nullptr);
71 
72  const auto& surface = cylindricalElement->surface();
73  BOOST_CHECK(surface.type() == Acts::Surface::SurfaceType::Cylinder);
74  BOOST_CHECK(
75  surface.transform(tContext).isApprox(Acts::Transform3::Identity()));
76  auto boundValues = surface.bounds().values();
77  CHECK_CLOSE_ABS(boundValues[0u], 105., 1e-10);
78  CHECK_CLOSE_ABS(boundValues[1u], 1000., 1e-10);
79  lcdd->destroyInstance();
80 }
81 
82 BOOST_AUTO_TEST_CASE(DD4hepPluginDetectorElementSectoralCylinder) {
83  std::ofstream cxml;
84  cxml.open("SectoralCylinder.xml");
85  cxml << head_xml;
86  cxml << sectoral_cylinder_xml;
87  cxml << end_xml;
88  cxml.close();
89 
90  auto lcdd = &(dd4hep::Detector::getInstance());
91  lcdd->fromCompact("SectoralCylinder.xml");
92  lcdd->volumeManager();
93  lcdd->apply("DD4hepVolumeManager", 0, nullptr);
94 
95  auto world = lcdd->world();
96 
97  std::shared_ptr<Acts::DD4hepDetectorElement> cylindricalElement = nullptr;
98  for (auto [chn, child] : world.children()) {
99  cylindricalElement =
100  std::make_shared<Acts::DD4hepDetectorElement>(child, "XYZ", 10.);
101  }
102 
103  BOOST_CHECK(cylindricalElement != nullptr);
104 
105  const auto& surface = cylindricalElement->surface();
106  BOOST_CHECK(surface.type() == Acts::Surface::SurfaceType::Cylinder);
107  BOOST_CHECK(
108  surface.transform(tContext).isApprox(Acts::Transform3::Identity()));
109  auto boundValues = surface.bounds().values();
110  CHECK_CLOSE_ABS(boundValues[0u], 105., 1e-10);
111  CHECK_CLOSE_ABS(boundValues[1u], 1000., 1e-10);
112  CHECK_CLOSE_ABS(boundValues[2u], 0.35, 1e-10);
113  CHECK_CLOSE_ABS(boundValues[3u], 0.45, 1e-10);
114  lcdd->destroyInstance();
115 }
116 
117 BOOST_AUTO_TEST_CASE(DD4hepPluginDetectorElementDisc) {
118  std::ofstream cxml;
119  cxml.open("Disc.xml");
120  cxml << head_xml;
121  cxml << disc_xml;
122  cxml << end_xml;
123  cxml.close();
124 
125  auto lcdd = &(dd4hep::Detector::getInstance());
126  lcdd->fromCompact("Disc.xml");
127  lcdd->volumeManager();
128  lcdd->apply("DD4hepVolumeManager", 0, nullptr);
129 
130  auto world = lcdd->world();
131 
132  std::shared_ptr<Acts::DD4hepDetectorElement> discElement = nullptr;
133  for (auto [chn, child] : world.children()) {
134  discElement =
135  std::make_shared<Acts::DD4hepDetectorElement>(child, "XYZ", 10., true);
136  }
137 
138  BOOST_CHECK(discElement != nullptr);
139 
140  const auto& surface = discElement->surface();
141  BOOST_CHECK(surface.type() == Acts::Surface::SurfaceType::Disc);
142  BOOST_CHECK(
143  surface.transform(tContext).isApprox(Acts::Transform3::Identity()));
144  auto boundValues = surface.bounds().values();
145  CHECK_CLOSE_ABS(boundValues[0u], 100., 1e-10);
146  CHECK_CLOSE_ABS(boundValues[1u], 900., 1e-10);
147  lcdd->destroyInstance();
148 }
149 
150 BOOST_AUTO_TEST_CASE(DD4hepPluginDetectorElementSectoralDisc) {
151  std::ofstream cxml;
152  cxml.open("SectoralDisc.xml");
153  cxml << head_xml;
154  cxml << sectoral_disc_xml;
155  cxml << end_xml;
156  cxml.close();
157 
158  auto lcdd = &(dd4hep::Detector::getInstance());
159  lcdd->fromCompact("SectoralDisc.xml");
160  lcdd->volumeManager();
161  lcdd->apply("DD4hepVolumeManager", 0, nullptr);
162 
163  auto world = lcdd->world();
164 
165  std::shared_ptr<Acts::DD4hepDetectorElement> discElement = nullptr;
166  for (auto [chn, child] : world.children()) {
167  discElement =
168  std::make_shared<Acts::DD4hepDetectorElement>(child, "XYZ", 10., true);
169  }
170 
171  BOOST_CHECK(discElement != nullptr);
172 
173  const auto& surface = discElement->surface();
174  BOOST_CHECK(surface.type() == Acts::Surface::SurfaceType::Disc);
175  BOOST_CHECK(
176  surface.transform(tContext).isApprox(Acts::Transform3::Identity()));
177  auto boundValues = surface.bounds().values();
178 
179  CHECK_CLOSE_ABS(boundValues[0u], 100., 1e-10);
180  CHECK_CLOSE_ABS(boundValues[1u], 900., 1e-10);
181  CHECK_CLOSE_ABS(boundValues[2u], 0.75, 1e-10);
182  CHECK_CLOSE_ABS(boundValues[3u], 0.75, 1e-10);
183  lcdd->destroyInstance();
184 }
185 
186 BOOST_AUTO_TEST_CASE(DD4hepPluginDetectorElementRectangle) {
187  std::ofstream cxml;
188  cxml.open("Rectangle.xml");
189  cxml << head_xml;
190  cxml << rectangle_xml;
191  cxml << end_xml;
192  cxml.close();
193 
194  auto lcdd = &(dd4hep::Detector::getInstance());
195  lcdd->fromCompact("Rectangle.xml");
196  lcdd->volumeManager();
197  lcdd->apply("DD4hepVolumeManager", 0, nullptr);
198 
199  auto world = lcdd->world();
200 
201  std::shared_ptr<Acts::DD4hepDetectorElement> rectangleElement = nullptr;
202  for (auto [chn, child] : world.children()) {
203  rectangleElement =
204  std::make_shared<Acts::DD4hepDetectorElement>(child, "XYZ", 10., true);
205  }
206 
207  BOOST_CHECK(rectangleElement != nullptr);
208 
209  const auto& surface = rectangleElement->surface();
210  BOOST_CHECK(surface.type() == Acts::Surface::SurfaceType::Plane);
211 
212  auto sTransform = surface.transform(tContext);
213  BOOST_CHECK(sTransform.translation().isApprox(Acts::Vector3(10., 20., 30.)));
214 
215  const auto& sBounds = surface.bounds();
216  BOOST_CHECK(sBounds.type() == Acts::SurfaceBounds::BoundsType::eRectangle);
217 
218  auto boundValues = sBounds.values();
219 
220  CHECK_CLOSE_ABS(boundValues[0u], -50., 1e-10);
221  CHECK_CLOSE_ABS(boundValues[1u], -450., 1e-10);
222  CHECK_CLOSE_ABS(boundValues[2u], 50, 1e-10);
223  CHECK_CLOSE_ABS(boundValues[3u], 450, 1e-10);
224 
225  lcdd->destroyInstance();
226 }
227 
228 BOOST_AUTO_TEST_CASE(DD4hepPluginDetectorElementTrapezoid) {
229  std::ofstream cxml;
230  cxml.open("Trapezoid.xml");
231  cxml << head_xml;
232  cxml << trapezoid_xml;
233  cxml << end_xml;
234  cxml.close();
235 
236  auto lcdd = &(dd4hep::Detector::getInstance());
237  lcdd->fromCompact("Trapezoid.xml");
238  lcdd->volumeManager();
239  lcdd->apply("DD4hepVolumeManager", 0, nullptr);
240 
241  auto world = lcdd->world();
242 
243  std::shared_ptr<Acts::DD4hepDetectorElement> trapezoidElement = nullptr;
244  for (auto [chn, child] : world.children()) {
245  trapezoidElement =
246  std::make_shared<Acts::DD4hepDetectorElement>(child, "xZ", 10., true);
247  }
248 
249  BOOST_CHECK(trapezoidElement != nullptr);
250 
251  const auto& surface = trapezoidElement->surface();
252  BOOST_CHECK(surface.type() == Acts::Surface::SurfaceType::Plane);
253 
254  auto sTransform = surface.transform(tContext);
255  BOOST_CHECK(sTransform.translation().isApprox(Acts::Vector3(20., 30., 40.)));
256 
257  const auto& sBounds = surface.bounds();
258  BOOST_CHECK(sBounds.type() == Acts::SurfaceBounds::BoundsType::eTrapezoid);
259 
260  auto boundValues = sBounds.values();
261  CHECK_CLOSE_ABS(boundValues[0u], 100., 1e-10);
262  CHECK_CLOSE_ABS(boundValues[1u], 200., 1e-10);
263  CHECK_CLOSE_ABS(boundValues[2u], 150, 1e-10);
264 
265  lcdd->destroyInstance();
266 }
267 
268 BOOST_AUTO_TEST_SUITE_END()
269