Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DD4hepCylinderLayerStructureTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file DD4hepCylinderLayerStructureTests.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 
17 
18 #include <fstream>
19 #include <string>
20 
21 #include <DD4hep/DetElement.h>
22 #include <DD4hep/DetFactoryHelper.h>
23 #include <DD4hep/Detector.h>
24 #include <XML/Utilities.h>
25 #include <XMLFragments.hpp>
26 
27 #include "DD4hepTestsHelper.hpp"
28 
32 
33 const char* beampipe_head_xml =
34  R""""( <detectors> <detector id="0" name="BeamPipe" type="BarrelDetector"> <type_flags type="DetType_TRACKER + DetType_BEAMPIPE"/> <layers> <layer name="BP" id="0"> )"""";
35 
37  R""""( <detectors> <detector id="1" name="BarrelLayer" type="BarrelDetector" readout="PixelReadout"> <type_flags type="DetType_TRACKER + DetType_BARREL"/> <layers> <layer name="B0" id="0"> )"""";
38 
39 const char* tail_xml =
40  R""""( </layer> </layers> </detector> </detectors> )"""";
41 
42 const char* indent_12_xml = " ";
43 
44 BOOST_AUTO_TEST_SUITE(DD4hepPlugin)
45 
46 // This tests creates a beampipe as a passive cylinder surface
47 BOOST_AUTO_TEST_CASE(DD4hepPluginBeampipeStructure) {
48  // Create an XML from it
49  std::ofstream cxml;
50 
51  std::string fNameBase = "BeamPipe";
52 
53  cxml.open(fNameBase + ".xml");
54  cxml << head_xml;
55  cxml << beampipe_head_xml;
56  cxml << indent_12_xml << " <acts_passive_surface>" << '\n';
57  cxml << indent_12_xml
58  << " <tubs rmin=\"25*mm\" rmax=\"25.8*mm\" dz=\"1800*mm\" "
59  "material=\"Air\"/>"
60  << '\n';
61  cxml << indent_12_xml << " </acts_passive_surface>" << '\n';
62 
63  cxml << tail_xml;
64  cxml << end_xml;
65  cxml.close();
66 
67  auto lcdd = &(dd4hep::Detector::getInstance());
68  lcdd->fromCompact(fNameBase + ".xml");
69  lcdd->volumeManager();
70  lcdd->apply("DD4hepVolumeManager", 0, nullptr);
71 
72  auto world = lcdd->world();
73 
74  // Now the test starts ...
75  auto sFactory = std::make_shared<Acts::DD4hepDetectorSurfaceFactory>(
76  Acts::getDefaultLogger("DD4hepDetectorSurfaceFactory",
78 
80  std::move(sFactory), Acts::getDefaultLogger("DD4hepBeamPipeStructure",
82 
84 
86  lsOptions.name = "BeamPipe";
87  lsOptions.logLevel = Acts::Logging::VERBOSE;
88 
89  auto beamPipeInternalsBuilder =
90  beamPipeStructure.builder(dd4hepStore, world, lsOptions);
91 
92  // Build the internal volume structure
93  auto [surfaces, volumes, surfacesUpdator, volumeUpdator] =
94  beamPipeInternalsBuilder->construct(tContext);
95 
96  // All surfaces are filled
97  BOOST_CHECK(surfaces.size() == 1u);
98  // No volumes are added
99  BOOST_CHECK(volumes.empty());
100  // The surface updator is connected
101  BOOST_CHECK(surfacesUpdator.connected());
102  // The volume updator is connected
103  BOOST_CHECK(volumeUpdator.connected());
104 
105  // Kill that instance before going into the next test
106  lcdd->destroyInstance();
107 }
108 
109 // This test creates DD4hep xml compact files for cylindrical
110 // layer structures and then converts them into IndexedSurfacesUpdator
111 // and other additional components needed for Internal DetectorVolume
112 // structure.
113 //
114 // It tests without explicit binning first
115 //
116 // It tests also with Bin expansion, i.e. the filling of additional
117 // bins with the found surface object.
118 //
119 BOOST_AUTO_TEST_CASE(DD4hepPluginCylinderLayerStructure) {
120  // First create some test surfaces
122  auto cSurfaces = cGeometry.surfacesCylinder(dStore, 8.4, 36., 0.15, 0.145,
123  116., 3., 2., {52, 14});
124 
125  // Running three tests with
126  // - no binning / no support
127  // - 14 x 52 bins and expansion / support
128  // - 28 x 104 bins without expansion / support with proto material
129  std::vector<std::array<unsigned int, 4u> > zphiBinning = {
130  {1u, 1u, 0u, 0u}, {14u, 52u, 1u, 1u}, {28u, 104u, 0u, 0u}};
131 
132  size_t itest = 0;
133  for (auto [nz, nphi, ez, ephi] : zphiBinning) {
134  // Create an XML from it
135  std::ofstream cxml;
136 
137  std::string fNameBase = "CylinderLayer_nz";
138  fNameBase += std::to_string(nz);
139  fNameBase += "_nphi";
140  fNameBase += std::to_string(nphi);
141 
142  cxml.open(fNameBase + ".xml");
143  cxml << head_xml;
144  cxml << segmentation_xml;
145  cxml << cylinder_layer_head_xml;
146 
147  if (nz * nphi > 1u) {
148  cxml << indent_12_xml << "<acts_surface_binning";
149  cxml << " ztype=\"equidistant\"";
150  cxml << " phitype=\"equidistant\"";
151  cxml << " nz=\"" << nz << "\" zmin=\"-500*mm\" zmax=\"500*mm\"";
152  cxml << " zexpansion= \"" << ez << "\"";
153  cxml << " nphi=\"" << nphi << "\" phimin=\"-3.1415\" phimax=\"3.1415\"";
154  cxml << " phiexpansion= \"" << ephi << "\"/>";
155  }
156  cxml << "<modules>" << '\n';
157 
158  for (const auto& s : cSurfaces) {
159  cxml << indent_12_xml
160  << DD4hepTestsHelper::surfaceToXML(tContext, *s,
161  Acts::Transform3::Identity())
162  << "\n";
163  }
164 
165  cxml << "</modules>" << '\n';
166 
167  // test the support structure definition
168  unsigned int passiveAddon = 0u;
169  if (itest == 1u) {
170  cxml << indent_12_xml << " <acts_passive_surface>" << '\n';
171  cxml << indent_12_xml
172  << " <tubs rmin=\"122*mm\" rmax=\"124*mm\" dz=\"500*mm\" "
173  "material=\"Air\"/>"
174  << '\n';
175  cxml << indent_12_xml << " </acts_passive_surface>" << '\n';
176  passiveAddon = 1u;
177  } else if (itest == 2u) {
178  cxml << indent_12_xml << " <acts_passive_surface>" << '\n';
179  cxml << indent_12_xml
180  << " <tubs rmin=\"122*mm\" rmax=\"124*mm\" dz=\"500*mm\" "
181  "material=\"Air\"/>"
182  << '\n';
183  cxml << " <acts_proto_material/>" << '\n';
184  cxml << indent_12_xml << " </acts_passive_surface>" << '\n';
185  passiveAddon = 1u;
186  }
187  ++itest;
188 
189  cxml << tail_xml;
190  cxml << end_xml;
191  cxml.close();
192 
193  auto lcdd = &(dd4hep::Detector::getInstance());
194  lcdd->fromCompact(fNameBase + ".xml");
195  lcdd->volumeManager();
196  lcdd->apply("DD4hepVolumeManager", 0, nullptr);
197 
198  auto world = lcdd->world();
199 
200  // Now the test starts ...
201  auto sFactory = std::make_shared<Acts::DD4hepDetectorSurfaceFactory>(
202  Acts::getDefaultLogger("DD4hepDetectorSurfaceFactory",
204 
206  std::move(sFactory),
207  Acts::getDefaultLogger("DD4hepLayerStructure", Acts::Logging::VERBOSE));
208 
210 
212  lsOptions.name = "BarrelLayer";
213  lsOptions.logLevel = Acts::Logging::VERBOSE;
214 
215  auto barrelInternalsBuilder =
216  barrelStructure.builder(dd4hepStore, world, lsOptions);
217 
218  // Build the internal volume structure
219  auto [surfaces, volumes, surfacesUpdator, volumeUpdator] =
220  barrelInternalsBuilder->construct(tContext);
221 
222  // All surfaces are filled
223  BOOST_CHECK(surfaces.size() == 14u * 52u + passiveAddon);
224  // No volumes are added
225  BOOST_CHECK(volumes.empty());
226  // The surface updator is connected
227  BOOST_CHECK(surfacesUpdator.connected());
228  // The volume updator is connected
229  BOOST_CHECK(volumeUpdator.connected());
230 
231  // Kill that instance before going into the next test
232  lcdd->destroyInstance();
233  }
234 }
235 
236 BOOST_AUTO_TEST_SUITE_END()
237