Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DetUtils.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file DetUtils.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2017 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 "DetUtils.h"
10 
16 
17 #include <cstddef>
18 
19 #include <DD4hep/CartesianGridXZ.h>
20 #include <DD4hep/Segmentations.h>
21 #include <XML/XMLTags.h>
22 
23 namespace det {
24 namespace utils {
25 
26 std::shared_ptr<const Acts::DigitizationModule> rectangleDigiModuleXZ(
27  double halflengthX, double halflengthZ, double thickness,
28  const dd4hep::Segmentation& segmentation) {
29  // convert to ACTS units
30  double scalor = Acts::UnitConstants::cm;
31  halflengthX *= scalor;
32  halflengthZ *= scalor;
33  thickness *= scalor;
34  auto bounds =
35  std::make_shared<const Acts::RectangleBounds>(halflengthX, halflengthZ);
36  dd4hep::CartesianGridXZ cartesianGrid = segmentation;
37  if (cartesianGrid.isValid()) {
38  // the Acts segmentation of the DigitizationModule
39  double gridSizeX = cartesianGrid.gridSizeX() * scalor;
40  double gridSizeZ = cartesianGrid.gridSizeZ() * scalor;
41  size_t bins0 = (cartesianGrid.gridSizeX() != 0)
42  ? static_cast<size_t>((2 * halflengthX) / gridSizeX)
43  : 0;
44  size_t bins1 = (cartesianGrid.gridSizeZ() != 0)
45  ? static_cast<size_t>((2 * halflengthZ) / gridSizeZ)
46  : 0;
47 
48  std::shared_ptr<const Acts::CartesianSegmentation> actsSegmentation =
49  std::make_shared<const Acts::CartesianSegmentation>(bounds, bins0,
50  bins1);
51  // finally create the digitization module
52  // @todo set lorentz angle
53  return (std::make_shared<const Acts::DigitizationModule>(actsSegmentation,
54  thickness, 1, 0));
55  }
56  return nullptr;
57 }
58 
59 std::shared_ptr<const Acts::DigitizationModule> rectangleDigiModuleXZ(
60  double halflengthX, double halflengthZ, double thickness, double gridSizeX,
61  double gridSizeZ) {
62  // convert to ACTS units
63  double scalor = Acts::UnitConstants::cm;
64  halflengthX *= scalor;
65  halflengthZ *= scalor;
66  thickness *= scalor;
67  auto bounds =
68  std::make_shared<const Acts::RectangleBounds>(halflengthX, halflengthZ);
69  // the Acts segmentation of the DigitizationModule
70  size_t bins0 =
71  (gridSizeX != 0)
72  ? static_cast<size_t>((2 * halflengthX) / (gridSizeX * scalor))
73  : 0;
74  size_t bins1 =
75  (gridSizeZ != 0)
76  ? static_cast<size_t>((2 * halflengthZ) / (gridSizeZ * scalor))
77  : 0;
78 
79  std::shared_ptr<const Acts::CartesianSegmentation> actsSegmentation =
80  std::make_shared<const Acts::CartesianSegmentation>(bounds, bins0, bins1);
81 
82  // finally create the digitization module
83  // @todo set lorentz angle
84  return (std::make_shared<const Acts::DigitizationModule>(actsSegmentation,
85  thickness, 1, 0));
86 }
87 
88 std::shared_ptr<const Acts::DigitizationModule> trapezoidalDigiModuleXZ(
89  double minHalflengthX, double maxHalflengthX, double halflengthZ,
90  double thickness, const dd4hep::Segmentation& segmentation) {
91  // convert to ACTS units
92  double scalor = Acts::UnitConstants::cm;
93  minHalflengthX *= scalor;
94  maxHalflengthX *= scalor;
95  halflengthZ *= scalor;
96  thickness *= scalor;
97 
98  auto bounds = std::make_shared<const Acts::TrapezoidBounds>(
99  minHalflengthX, maxHalflengthX, halflengthZ);
100 
101  dd4hep::CartesianGridXZ cartesianGrid = segmentation;
102  if (cartesianGrid.isValid()) {
103  // the Acts segmentation of the DigitizationModule
104  double gridSizeX = cartesianGrid.gridSizeX() * scalor;
105  double gridSizeZ = cartesianGrid.gridSizeZ() * scalor;
106  size_t bins0 = (cartesianGrid.gridSizeX() != 0)
107  ? static_cast<size_t>((2 * maxHalflengthX) / gridSizeX)
108  : 0;
109  size_t bins1 = (cartesianGrid.gridSizeZ() != 0)
110  ? static_cast<size_t>((2 * halflengthZ) / gridSizeZ)
111  : 0;
112 
113  std::shared_ptr<const Acts::CartesianSegmentation> actsSegmentation =
114  std::make_shared<const Acts::CartesianSegmentation>(bounds, bins0,
115  bins1);
116  // finally create the digitization module
117  // @todo set lorentz angle
118  return (std::make_shared<const Acts::DigitizationModule>(actsSegmentation,
119  thickness, 1, 0));
120  }
121  return nullptr;
122 }
123 
124 std::shared_ptr<const Acts::DigitizationModule> trapezoidalDigiModuleXZ(
125  double minHalflengthX, double maxHalflengthX, double halflengthZ,
126  double thickness, double gridSizeX, double gridSizeZ) {
127  // convert to ACTS units
128  double scalor = Acts::UnitConstants::cm;
129  minHalflengthX *= scalor;
130  maxHalflengthX *= scalor;
131  halflengthZ *= scalor;
132  thickness *= scalor;
133 
134  auto bounds = std::make_shared<const Acts::TrapezoidBounds>(
135  minHalflengthX, maxHalflengthX, halflengthZ);
136 
137  // the Acts segmentation of the DigitizationModule
138  size_t bins0 =
139  (gridSizeX != 0)
140  ? static_cast<size_t>((2 * maxHalflengthX) / (gridSizeX * scalor))
141  : 0;
142  size_t bins1 =
143  (gridSizeZ != 0)
144  ? static_cast<size_t>((2 * halflengthZ) / (gridSizeZ * scalor))
145  : 0;
146 
147  std::shared_ptr<const Acts::CartesianSegmentation> actsSegmentation =
148  std::make_shared<const Acts::CartesianSegmentation>(bounds, bins0, bins1);
149  // finally create the digitization module
150  // @todo set lorentz angle
151  return (std::make_shared<const Acts::DigitizationModule>(actsSegmentation,
152  thickness, 1, 0));
153 }
154 
155 dd4hep::xml::Component getNodeByStrAttr(const dd4hep::xml::Handle_t& mother,
156  const std::string& nodeName,
157  const std::string& attrName,
158  const std::string& attrValue) {
159  for (dd4hep::xml::Collection_t xCompColl(mother, nodeName.c_str());
160  nullptr != xCompColl; ++xCompColl) {
161  if (xCompColl.attr<std::string>(attrName.c_str()) == attrValue) {
162  return static_cast<dd4hep::xml::Component>(xCompColl);
163  }
164  }
165  // in case there was no xml daughter with matching name
166  return dd4hep::xml::Component(nullptr);
167 }
168 
169 double getAttrValueWithFallback(const dd4hep::xml::Component& node,
170  const std::string& attrName,
171  const double& defaultValue) {
172  if (node.hasAttr(_Unicode(attrName.c_str()))) {
173  return node.attr<double>(attrName.c_str());
174  } else {
175  return defaultValue;
176  }
177 }
178 } // namespace utils
179 } // namespace det