Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DD4hepCylinderVolumeStructureTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file DD4hepCylinderVolumeStructureTests.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 
15 
16 #include <fstream>
17 #include <string>
18 
19 #include <DD4hep/DetElement.h>
20 #include <DD4hep/DetFactoryHelper.h>
21 #include <DD4hep/Detector.h>
22 #include <XML/Utilities.h>
23 #include <XMLFragments.hpp>
24 
25 #include "DD4hepTestsHelper.hpp"
26 
28 
29 const char* beampipe_head_xml =
30  R""""( <detectors> <detector id="0" name="BeamPipe" type="BarrelDetector"> <type_flags type="DetType_TRACKER + DetType_BEAMPIPE"/> )"""";
31 
32 const char* tail_xml =
33  R""""( </detector> </detectors> )"""";
34 
35 const char* indent_12_xml = " ";
36 
37 BOOST_AUTO_TEST_SUITE(DD4hepPlugin)
38 
39 // This tests creates a cylindrical structure
40 BOOST_AUTO_TEST_CASE(DD4hepPluginCylindricalVolumeStructure) {
41  // Create an XML from it
42  std::ofstream cxml;
43 
44  std::string fNameBase = "CylindricalVolume";
45 
46  cxml.open(fNameBase + ".xml");
47  cxml << head_xml;
48  cxml << beampipe_head_xml;
49  // DD4hep description
50  cxml << indent_12_xml
51  << "<envelope rmin=\"0*mm\" rmax=\"30*mm\" dz=\"2000*mm\" "
52  "cz=\"0*mm\" "
53  "material=\"Air\"/>"
54  << '\n';
55  // External description
56  cxml << indent_12_xml << "<layers>" << '\n';
57  cxml << indent_12_xml << "<layer name=\"CylinderLayer\" id=\"0\">" << '\n';
58  cxml << indent_12_xml
59  << "<acts_volume name=\"CylinderLayerVolume\" sequence=\"1\">" << '\n';
60  cxml << indent_12_xml
61  << " <tubs rmin=\"0*mm\" rmax=\"30.0*mm\" dz=\"2000*mm\" />" << '\n';
62  cxml << indent_12_xml << "</acts_volume>" << '\n';
63 
64  // Internal description
65  cxml << indent_12_xml << "<passive_surface>" << '\n';
66  cxml << indent_12_xml
67  << " <tubs rmin=\"25*mm\" rmax=\"25.8*mm\" dz=\"1800*mm\" "
68  "material=\"Air\"/>"
69  << '\n';
70  cxml << indent_12_xml << "</passive_surface>" << '\n';
71  cxml << indent_12_xml << "</layer>" << '\n';
72  cxml << indent_12_xml << "</layers>" << '\n';
73 
74  cxml << tail_xml;
75  cxml << end_xml;
76  cxml.close();
77 
78  auto lcdd = &(dd4hep::Detector::getInstance());
79  lcdd->fromCompact(fNameBase + ".xml");
80  lcdd->volumeManager();
81  lcdd->apply("DD4hepVolumeManager", 0, nullptr);
82 
83  auto world = lcdd->world();
84 
85  // Create the structure & the builder
87  Acts::getDefaultLogger("DD4hepVolumeStructure", Acts::Logging::VERBOSE));
88 
91  vOptions.name = "CylinderLayerVolume";
92 
93  auto volumeBuilder = volumeStructure.builder(world, vOptions);
94 
96  volumeBuilder->construct(tContext);
97 
98  BOOST_CHECK(transform.isApprox(Acts::Transform3::Identity()));
99  BOOST_CHECK(bounds != nullptr);
100  BOOST_CHECK(bounds->type() == Acts::VolumeBounds::BoundsType::eCylinder);
101  BOOST_CHECK(bounds->values().size() == 7u);
102  BOOST_CHECK(bounds->values()[0u] == 0.);
103  BOOST_CHECK(bounds->values()[1u] == 30.);
104  BOOST_CHECK(bounds->values()[2u] == 1000.);
105 
106  // Kill that instance before going into the next test
107  lcdd->destroyInstance();
108 }
109 
110 // This tests creates a cylindrical structure
111 BOOST_AUTO_TEST_CASE(DD4hepPluginShiftedCylindricalVolumeStructure) {
112  // Create an XML from it
113  std::ofstream cxml;
114 
115  std::string fNameBase = "ShiftedCylindricalVolume";
116 
117  cxml.open(fNameBase + ".xml");
118  cxml << head_xml;
119  cxml << beampipe_head_xml;
120  // DD4hep description
121  cxml << indent_12_xml
122  << "<envelope rmin=\"0*mm\" rmax=\"30*mm\" dz=\"2000*mm\" "
123  "cz=\"0*mm\" "
124  "material=\"Air\"/>"
125  << '\n';
126  // External description
127  cxml << indent_12_xml << "<layers>" << '\n';
128  cxml << indent_12_xml << "<layer name=\"ShiftedCylinderLayer\" id=\"0\">"
129  << '\n';
130  cxml << indent_12_xml
131  << "<acts_volume name=\"ShiftedCylinderLayerVolume\" sequence=\"1\">"
132  << '\n';
133  cxml << indent_12_xml
134  << " <tubs rmin=\"0*mm\" rmax=\"30.0*mm\" dz=\"2000*mm\" "
135  "x=\"10.*mm\" y=\"1.5*cm\" z=\"-1300*mm\"/>"
136  << '\n';
137  cxml << indent_12_xml << "</acts_volume>" << '\n';
138 
139  // Internal description
140  cxml << indent_12_xml << "<passive_surface>" << '\n';
141  cxml << indent_12_xml
142  << " <tubs rmin=\"25*mm\" rmax=\"25.8*mm\" dz=\"1800*mm\" "
143  "material=\"Air\"/>"
144  << '\n';
145  cxml << indent_12_xml << "</passive_surface>" << '\n';
146  cxml << indent_12_xml << "</layer>" << '\n';
147  cxml << indent_12_xml << "</layers>" << '\n';
148 
149  cxml << tail_xml;
150  cxml << end_xml;
151  cxml.close();
152 
153  auto lcdd = &(dd4hep::Detector::getInstance());
154  lcdd->fromCompact(fNameBase + ".xml");
155  lcdd->volumeManager();
156  lcdd->apply("DD4hepVolumeManager", 0, nullptr);
157 
158  auto world = lcdd->world();
159 
160  // Create the structure & the builder
162  Acts::getDefaultLogger("DD4hepVolumeStructure", Acts::Logging::VERBOSE));
163 
165  vOptions.logLevel = Acts::Logging::VERBOSE;
166  vOptions.name = "ShiftedCylinderLayerVolume";
167 
168  auto volumeBuilder = volumeStructure.builder(world, vOptions);
169 
170  auto [transform, bounds, portalGenerator] =
171  volumeBuilder->construct(tContext);
172 
173  BOOST_CHECK(bounds != nullptr);
174  BOOST_CHECK(bounds->type() == Acts::VolumeBounds::BoundsType::eCylinder);
175  BOOST_CHECK(bounds->values().size() == 7u);
176  BOOST_CHECK(bounds->values()[0u] == 0.);
177  BOOST_CHECK(bounds->values()[1u] == 30.);
178  BOOST_CHECK(bounds->values()[2u] == 1000.);
179 
180  // It is shifted - so let us check this
181  auto shift = transform.translation();
182  CHECK_CLOSE_ABS(shift.x(), 10., 1e-10);
183  CHECK_CLOSE_ABS(shift.y(), 15., 1e-10);
184  CHECK_CLOSE_ABS(shift.z(), -1300., 1e-10);
185 
186  // Kill that instance before going into the next test
187  lcdd->destroyInstance();
188 }
189 
190 BOOST_AUTO_TEST_SUITE_END()
191