CMS 3D CMS Logo

GeometricDet.cc
Go to the documentation of this file.
1 #include <DD4hep/DD4hepUnits.h>
2 
11 
12 #include <cfloat>
13 #include <string>
14 
15 namespace {
16 
17  const std::string strue("true");
18 
19  template <typename DDView>
20  double getDouble(const char* s, DDView const& ev) {
21  DDValue val(s);
22  std::vector<const DDsvalues_type*> result;
23  ev.specificsV(result);
24  std::vector<const DDsvalues_type*>::iterator it = result.begin();
25  bool foundIt = false;
26  for (; it != result.end(); ++it) {
27  foundIt = DDfetch(*it, val);
28  if (foundIt)
29  break;
30  }
31  if (foundIt) {
32  const std::vector<std::string>& temp = val.strings();
33  if (temp.size() != 1) {
34  throw cms::Exception("Configuration") << "I need 1 " << s << " tags";
35  }
36  return double(::atof(temp[0].c_str()));
37  }
38  return 0;
39  }
40 
41  template <typename DDView>
42  std::string getString(const char* s, DDView const& ev) {
43  DDValue val(s);
44  std::vector<const DDsvalues_type*> result;
45  ev.specificsV(result);
46  std::vector<const DDsvalues_type*>::iterator it = result.begin();
47  bool foundIt = false;
48  for (; it != result.end(); ++it) {
49  foundIt = DDfetch(*it, val);
50  if (foundIt)
51  break;
52  }
53  if (foundIt) {
54  const std::vector<std::string>& temp = val.strings();
55  if (temp.size() != 1) {
56  throw cms::Exception("Configuration") << "I need 1 " << s << " tags";
57  }
58  return temp[0];
59  }
60  return "NotFound";
61  }
62 } // namespace
63 
69 
70 /*
71  Constructor from old DD Filtered view.
72 */
74  : ddname_(fv->name()),
75  type_(type),
76  ddd_(), // To remove after DetExtra is removed (not used)
77  trans_(fv->translation()),
78  rho_(trans_.Rho()),
79  phi_(trans_.Phi()),
80  rot_(fv->rotation()),
82  params_(fv->parameters()),
83  isFromDD4hep_(false) {
84  // workaround instead of this at initialization
85  const DDFilteredView::nav_type& nt = fv->navPos();
86  ddd_ = nav_type(nt.begin(), nt.end());
87 
88  // Only look for sensor-related info on sensor volumes!
89  if (type_ == DetUnit) {
90  radLength_ = getDouble("TrackerRadLength", *fv);
91  xi_ = getDouble("TrackerXi", *fv);
92  pixROCRows_ = getDouble("PixelROCRows", *fv);
93  pixROCCols_ = getDouble("PixelROCCols", *fv);
94  pixROCx_ = getDouble("PixelROC_X", *fv);
95  pixROCy_ = getDouble("PixelROC_Y", *fv);
96  stereo_ = (getString("TrackerStereoDetectors", *fv) == strue);
97  isLowerSensor_ = (getString("TrackerLowerDetectors", *fv) == strue);
98  isUpperSensor_ = (getString("TrackerUpperDetectors", *fv) == strue);
99  siliconAPVNum_ = getDouble("SiliconAPVNumber", *fv);
100  }
101 }
102 
103 /*
104  Constructor from DD4hep Filtered view.
105 */
107  : ddname_(dd4hep::dd::noNamespace(fv->name())),
108  type_(type),
109  ddd_(fv->navPos()), // To remove after DetExtra is removed (not used)
110  trans_((fv->translation()) / dd4hep::mm),
111  rho_(trans_.Rho()),
112  phi_(trans_.Phi()),
113  rot_(fv->rotation()),
114  shape_(fv->shape()),
115  params_(computeLegacyShapeParameters(shape_, fv->solid())),
116  isFromDD4hep_(true) {
117  using namespace angle_units::operators;
118  if (almostEqual(phi_, -1._pi, 10)) {
119  phi_ = 1._pi;
120  // Standardize phi values of |pi| to be always +pi instead of sometimes -pi.
121  }
122 
123  // Only look for sensor-related info on sensor volumes!
124  if (type_ == DetUnit) {
125  // IT sensors only (NB: hence could add a branch here, but not a critical part on perf)
126  pixROCRows_ = fv->get<double>("PixelROCRows");
127  pixROCCols_ = fv->get<double>("PixelROCCols");
128  pixROCx_ = fv->get<double>("PixelROC_X");
129  pixROCy_ = fv->get<double>("PixelROC_Y");
130 
131  // Phase 1 OT sensors only (NB: hence could add a branch here, but not a critical part on perf)
132  stereo_ = (fv->get<std::string_view>("TrackerStereoDetectors") == strue);
133  siliconAPVNum_ = fv->get<double>("SiliconAPVNumber");
134 
135  // Phase 2 OT sensors only (NB: hence could add a branch here, but not a critical part on perf)
136  isLowerSensor_ = (fv->get<std::string_view>("TrackerLowerDetectors") == strue);
137  isUpperSensor_ = (fv->get<std::string_view>("TrackerUpperDetectors") == strue);
138 
139  // All sensors: IT or OT, Phase 1 or Phase 2 (NB: critical part on perf)
140  fv->findSpecPar("TrackerRadLength", "TrackerXi");
141  radLength_ = fv->getNextValue("TrackerRadLength");
142  xi_ = fv->getNextValue("TrackerXi");
143  }
144 }
145 
146 /*
147  Constructor from persistent version (DB).
148 */
150  : ddname_(onePGD._name),
151  type_(type),
152  ddd_(),
153  geographicalID_(onePGD._geographicalID),
154  trans_(onePGD._x, onePGD._y, onePGD._z),
155  rho_(onePGD._rho),
156  phi_(onePGD._phi),
157  rot_(onePGD._a11,
158  onePGD._a12,
159  onePGD._a13,
160  onePGD._a21,
161  onePGD._a22,
162  onePGD._a23,
163  onePGD._a31,
164  onePGD._a32,
165  onePGD._a33),
166  shape_(cms::dd::name_from_value(cms::LegacySolidShapeMap, static_cast<LegacySolidShape>(onePGD._shape))),
167  params_(),
168  radLength_(onePGD._radLength),
169  xi_(onePGD._xi),
170  pixROCRows_(onePGD._pixROCRows),
171  pixROCCols_(onePGD._pixROCCols),
172  pixROCx_(onePGD._pixROCx),
173  pixROCy_(onePGD._pixROCy),
174  stereo_(onePGD._stereo),
175  siliconAPVNum_(onePGD._siliconAPVNum)
176 // NB: what about new data members isLowerSensor_, isUpperSensor_, isFromDD4hep_?
177 // They are presently not added to PGeometricDet (no change in info stored into DB).
178 {
179  // Solid shape parameters: only for box (1) and trapezoid (3)
180  if (onePGD._shape == 1 || onePGD._shape == 3) {
181  params_.reserve(11);
182  params_.emplace_back(onePGD._params0);
183  params_.emplace_back(onePGD._params1);
184  params_.emplace_back(onePGD._params2);
185  params_.emplace_back(onePGD._params3);
186  params_.emplace_back(onePGD._params4);
187  params_.emplace_back(onePGD._params5);
188  params_.emplace_back(onePGD._params6);
189  params_.emplace_back(onePGD._params7);
190  params_.emplace_back(onePGD._params8);
191  params_.emplace_back(onePGD._params9);
192  params_.emplace_back(onePGD._params10);
193  }
194 
195  ddd_.reserve(onePGD._numnt);
196  ddd_.emplace_back(onePGD._nt0);
197  ddd_.emplace_back(onePGD._nt1);
198  ddd_.emplace_back(onePGD._nt2);
199  ddd_.emplace_back(onePGD._nt3);
200  if (onePGD._numnt > 4) {
201  ddd_.emplace_back(onePGD._nt4);
202  if (onePGD._numnt > 5) {
203  ddd_.emplace_back(onePGD._nt5);
204  if (onePGD._numnt > 6) {
205  ddd_.emplace_back(onePGD._nt6);
206  if (onePGD._numnt > 7) {
207  ddd_.emplace_back(onePGD._nt7);
208  if (onePGD._numnt > 8) {
209  ddd_.emplace_back(onePGD._nt8);
210  if (onePGD._numnt > 9) {
211  ddd_.emplace_back(onePGD._nt9);
212  if (onePGD._numnt > 10) {
213  ddd_.emplace_back(onePGD._nt10);
214  }
215  }
216  }
217  }
218  }
219  }
220  }
221 }
222 
223 std::unique_ptr<Bounds> GeometricDet::bounds() const {
224  TrackerShapeToBounds shapeToBounds;
225  return std::unique_ptr<Bounds>(shapeToBounds.buildBounds(shape_, params_));
226 }
227 
229  Position pos_(static_cast<float>(geant_units::operators::convertMmToCm(trans_.x())),
230  static_cast<float>(geant_units::operators::convertMmToCm(trans_.y())),
231  static_cast<float>(geant_units::operators::convertMmToCm(trans_.z())));
232  return pos_;
233 }
234 
236  Translation x, y, z;
237  rot_.GetComponents(x, y, z);
238  Rotation rotation_(float(x.X()),
239  float(x.Y()),
240  float(x.Z()),
241  float(y.X()),
242  float(y.Y()),
243  float(y.Z()),
244  float(z.X()),
245  float(z.Y()),
246  float(z.Z()));
247  return rotation_;
248 }
249 
251  // iterate on all the DESCENDANTS!!
253  deepComponents(temp_);
254  return temp_;
255 }
256 
258  if (isLeaf())
259  cont.emplace_back(this);
260  else
261  std::for_each(container_.begin(), container_.end(), [&](const GeometricDet* iDet) { iDet->deepComponents(cont); });
262 }
263 
265  container_.reserve(container_.size() + cont.size());
266  std::copy(cont.begin(), cont.end(), back_inserter(container_));
267 }
268 
270  container_.reserve(container_.size() + cont.size());
271  std::copy(cont.begin(), cont.end(), back_inserter(container_));
272 }
273 
274 void GeometricDet::addComponent(GeometricDet* det) { container_.emplace_back(det); }
275 
276 namespace {
277  struct Deleter {
278  void operator()(GeometricDet const* det) const { delete const_cast<GeometricDet*>(det); }
279  };
280 } // namespace
281 
283  std::for_each(container_.begin(), container_.end(), Deleter());
284  container_.clear();
285 }
286 
287 /*
288  * PRIVATE
289  */
290 
291 /*
292  * DD4hep.
293  * Keep order and units of parameters same as old DD, to avoid numerous regressions.
294  * Shape parameters of interest, and those to be stored in DB, are only from boxes, trapezoids, and tubs.
295  * Hence, they are the only shapes treated here.
296  * params() will complain, if the parameters of any other shape are accessed.
297  */
298 std::vector<double> GeometricDet::computeLegacyShapeParameters(const cms::DDSolidShape& mySolidShape,
299  const dd4hep::Solid& mySolid) const {
300  std::vector<double> myOldDDShapeParameters;
301 
302  // Box
303  if (mySolidShape == cms::DDSolidShape::ddbox) {
304  const dd4hep::Box& myBox = dd4hep::Box(mySolid);
305  myOldDDShapeParameters = {(myBox.x()) / dd4hep::mm, (myBox.y()) / dd4hep::mm, (myBox.z()) / dd4hep::mm};
306  }
307 
308  // Trapezoid
309  else if (mySolidShape == cms::DDSolidShape::ddtrap) {
310  const dd4hep::Trap& myTrap = dd4hep::Trap(mySolid);
311  myOldDDShapeParameters = {(myTrap->GetDZ()) / dd4hep::mm,
312  static_cast<double>(angle_units::operators::convertDegToRad(myTrap->GetTheta())),
313  static_cast<double>(angle_units::operators::convertDegToRad(myTrap->GetPhi())),
314  (myTrap->GetH1()) / dd4hep::mm,
315  (myTrap->GetBl1()) / dd4hep::mm,
316  (myTrap->GetTl1()) / dd4hep::mm,
317  static_cast<double>(angle_units::operators::convertDegToRad(myTrap->GetAlpha1())),
318  (myTrap->GetH2()) / dd4hep::mm,
319  (myTrap->GetBl2()) / dd4hep::mm,
320  (myTrap->GetTl2()) / dd4hep::mm,
321  static_cast<double>(angle_units::operators::convertDegToRad(myTrap->GetAlpha2()))};
322  }
323 
324  // Tub
325  else if (mySolidShape == cms::DDSolidShape::ddtubs) {
326  const dd4hep::Tube& myTube = dd4hep::Tube(mySolid);
327  double phi1 = static_cast<double>(angle_units::operators::convertDegToRad(myTube->GetPhi1()));
328  phi1 = cms_rounding::roundIfNear0(phi1);
329  if (phi1 != 0.0) {
330  using namespace angle0to2pi;
331  phi1 = make0To2pi(phi1) - 2._pi;
332  // Turn positive angle to negative
333  }
334  myOldDDShapeParameters = {
335  (myTube->GetDz()) / dd4hep::mm,
336  (myTube->GetRmin()) / dd4hep::mm,
337  (myTube->GetRmax()) / dd4hep::mm,
338  phi1,
339  static_cast<double>(angle_units::operators::convertDegToRad(myTube->GetPhi2() - myTube->GetPhi1()))};
340  }
341 
342  return myOldDDShapeParameters;
343 }
344 
345 std::string GeometricDet::printNavType(int const* n, size_t sz) {
346  std::ostringstream oss;
347  oss << '(';
348  for (int const* it = n; it != n + sz; ++it) {
349  oss << *it << ',';
350  }
351  oss << ')';
352  return oss.str();
353 }
std::vector< GeometricDet * > GeometricDetContainer
Definition: GeometricDet.h:35
cms::DDSolidShape shape_
Definition: GeometricDet.h:186
bool isLowerSensor_
Definition: GeometricDet.h:196
constexpr double convertDegToRad(NumType degrees)
Definition: angle_units.h:27
double pixROCCols_
Definition: GeometricDet.h:192
void addComponent(GeometricDet *)
std::vector< double > params_
Definition: GeometricDet.h:187
Bounds * buildBounds(const cms::DDSolidShape &, const std::vector< double > &) const
std::enable_if<!std::numeric_limits< NumType >::is_integer, bool >::type almostEqual(NumType x, NumType y, int ulp)
Definition: angle_units.h:33
Position positionBounds() const
DetId geographicalID_
Definition: GeometricDet.h:179
bool DDfetch(const DDsvalues_type *, DDValue &)
helper for retrieving DDValues from DDsvalues_type *.
Definition: DDsvalues.cc:79
std::vector< int > nav_type
Definition: GeometricDet.h:42
enum GeometricDet::GDEnumType GeometricEnumType
string dd
Definition: createTree.py:154
bool isUpperSensor_
Definition: GeometricDet.h:197
nav_type ddd_
Definition: GeometricDet.h:178
ConstGeometricDetContainer container_
Definition: GeometricDet.h:202
double pixROCx_
Definition: GeometricDet.h:193
static std::string printNavType(int const *n, size_t sz)
double getNextValue(const std::string &) const
extract another value from the same SpecPar
GeometricEnumType type_
Definition: GeometricDet.h:176
double radLength_
Definition: GeometricDet.h:189
Rotation rotationBounds() const
GeometricDet(DDFilteredView *fv, GeometricEnumType dd)
Definition: GeometricDet.cc:73
std::vector< GeometricDet const * > ConstGeometricDetContainer
Definition: GeometricDet.h:34
const std::array< const cms::dd::ValuePair< LegacySolidShape, cms::DDSolidShape >, 21 > LegacySolidShapeMap
constexpr valType roundIfNear0(valType value, double tolerance=1.e-7)
Definition: Rounding.h:11
void deleteComponents()
int nt
Definition: AMPTWrapper.h:42
double pixROCy_
Definition: GeometricDet.h:194
Namespace of DDCMS conversion namespace.
std::vector< double > computeLegacyShapeParameters(const cms::DDSolidShape &mySolidShape, const dd4hep::Solid &mySolid) const
bool isLeaf() const
Definition: GeometricDet.h:148
double pixROCRows_
Definition: GeometricDet.h:191
T get(const std::string &)
extract attribute value
constexpr NumType convertMmToCm(NumType millimeters)
Definition: angle_units.h:44
DDSolidShape
Definition: DDSolidShapes.h:6
constexpr valType make0To2pi(valType angle)
Definition: deltaPhi.h:67
Translation trans_
Definition: GeometricDet.h:181
DDExpandedView::nav_type nav_type
void addComponents(GeometricDetContainer const &cont)
std::string ddname_
Definition: GeometricDet.h:175
nav_type navPos() const
return the stack of sibling numbers
ConstGeometricDetContainer deepComponents() const
Mapping::value_type::name_type name_from_value(Mapping a, V value)
Definition: DDSolidShapes.h:62
RotationMatrix rot_
Definition: GeometricDet.h:184
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< double > > Translation
Definition: GeometricDet.h:37
cont
load Luminosity info ##
Definition: generateEDF.py:628
double siliconAPVNum_
Definition: GeometricDet.h:198
std::unique_ptr< Bounds > bounds() const
void findSpecPar(T const &first, Ts const &... rest)
find a current Node SpecPar that has at least
DDSolidShape
Definition: DDSolidShapes.h:73