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  bigPixelsx_ = getDouble("BigPixels_X", *fv);
97  bigPixelsy_ = getDouble("BigPixels_Y", *fv);
98  bigPixelsPitchx_ = getDouble("BigPixels_Pitch_X", *fv);
99  bigPixelsPitchy_ = getDouble("BigPixels_Pitch_Y", *fv);
100  isFirstSensor_ = (getString("TrackerFirstDetectors", *fv) == strue);
101  isSecondSensor_ = (getString("TrackerSecondDetectors", *fv) == strue);
102  stereo_ = (getString("TrackerStereoDetectors", *fv) == strue);
103  isLowerSensor_ = (getString("TrackerLowerDetectors", *fv) == strue);
104  isUpperSensor_ = (getString("TrackerUpperDetectors", *fv) == strue);
105  siliconAPVNum_ = getDouble("SiliconAPVNumber", *fv);
106  }
107 }
108 
109 /*
110  Constructor from DD4hep Filtered view.
111 */
113  : ddname_(dd4hep::dd::noNamespace(fv->name())),
114  type_(type),
115  ddd_(fv->navPos()), // To remove after DetExtra is removed (not used)
116  trans_((fv->translation()) / dd4hep::mm),
117  rho_(trans_.Rho()),
118  phi_(trans_.Phi()),
119  rot_(fv->rotation()),
120  shape_(fv->shape()),
121  params_(computeLegacyShapeParameters(shape_, fv->solid())),
122  isFromDD4hep_(true) {
123  using namespace angle_units::operators;
124  if (almostEqual(phi_, -1._pi, 10)) {
125  phi_ = 1._pi;
126  // Standardize phi values of |pi| to be always +pi instead of sometimes -pi.
127  }
128 
129  // Only look for sensor-related info on sensor volumes!
130  if (type_ == DetUnit) {
131  // IT sensors only (NB: hence could add a branch here, but not a critical part on perf)
132  pixROCRows_ = fv->get<double>("PixelROCRows");
133  pixROCCols_ = fv->get<double>("PixelROCCols");
134  pixROCx_ = fv->get<double>("PixelROC_X");
135  pixROCy_ = fv->get<double>("PixelROC_Y");
136  bigPixelsx_ = fv->get<double>("BigPixels_X");
137  bigPixelsy_ = fv->get<double>("BigPixels_Y");
138  bigPixelsPitchx_ = fv->get<double>("BigPixels_Pitch_X");
139  bigPixelsPitchy_ = fv->get<double>("BigPixels_Pitch_Y");
140 
141  // Phase 2 IT 3D sensors only
142  isFirstSensor_ = (fv->get<std::string_view>("TrackerFirstDetectors") == strue);
143  isSecondSensor_ = (fv->get<std::string_view>("TrackerSecondDetectors") == strue);
144 
145  // Phase 1 OT sensors only (NB: hence could add a branch here, but not a critical part on perf)
146  stereo_ = (fv->get<std::string_view>("TrackerStereoDetectors") == strue);
147  siliconAPVNum_ = fv->get<double>("SiliconAPVNumber");
148 
149  // Phase 2 OT sensors only (NB: hence could add a branch here, but not a critical part on perf)
150  isLowerSensor_ = (fv->get<std::string_view>("TrackerLowerDetectors") == strue);
151  isUpperSensor_ = (fv->get<std::string_view>("TrackerUpperDetectors") == strue);
152 
153  // All sensors: IT or OT, Phase 1 or Phase 2 (NB: critical part on perf)
154  fv->findSpecPar("TrackerRadLength", "TrackerXi");
155  radLength_ = fv->getNextValue("TrackerRadLength");
156  xi_ = fv->getNextValue("TrackerXi");
157  }
158 }
159 
160 /*
161  Constructor from persistent version (DB).
162 */
164  : ddname_(onePGD._name),
165  type_(type),
166  ddd_(),
167  geographicalID_(onePGD._geographicalID),
168  trans_(onePGD._x, onePGD._y, onePGD._z),
169  rho_(onePGD._rho),
170  phi_(onePGD._phi),
171  rot_(onePGD._a11,
172  onePGD._a12,
173  onePGD._a13,
174  onePGD._a21,
175  onePGD._a22,
176  onePGD._a23,
177  onePGD._a31,
178  onePGD._a32,
179  onePGD._a33),
180  shape_(cms::dd::name_from_value(cms::LegacySolidShapeMap, static_cast<LegacySolidShape>(onePGD._shape))),
181  params_(),
182  radLength_(onePGD._radLength),
183  xi_(onePGD._xi),
184  pixROCRows_(onePGD._pixROCRows),
185  pixROCCols_(onePGD._pixROCCols),
186  pixROCx_(onePGD._pixROCx),
187  pixROCy_(onePGD._pixROCy),
188  stereo_(onePGD._stereo),
189  siliconAPVNum_(onePGD._siliconAPVNum)
190 // NB: what about new data members isLowerSensor_, isUpperSensor_, isFromDD4hep_?
191 // They are presently not added to PGeometricDet (no change in info stored into DB).
192 {
193  // Solid shape parameters: only for box (1) and trapezoid (3)
194  if (onePGD._shape == 1 || onePGD._shape == 3) {
195  params_.reserve(11);
196  params_.emplace_back(onePGD._params0);
197  params_.emplace_back(onePGD._params1);
198  params_.emplace_back(onePGD._params2);
199  params_.emplace_back(onePGD._params3);
200  params_.emplace_back(onePGD._params4);
201  params_.emplace_back(onePGD._params5);
202  params_.emplace_back(onePGD._params6);
203  params_.emplace_back(onePGD._params7);
204  params_.emplace_back(onePGD._params8);
205  params_.emplace_back(onePGD._params9);
206  params_.emplace_back(onePGD._params10);
207  }
208 
209  ddd_.reserve(onePGD._numnt);
210  ddd_.emplace_back(onePGD._nt0);
211  ddd_.emplace_back(onePGD._nt1);
212  ddd_.emplace_back(onePGD._nt2);
213  ddd_.emplace_back(onePGD._nt3);
214  if (onePGD._numnt > 4) {
215  ddd_.emplace_back(onePGD._nt4);
216  if (onePGD._numnt > 5) {
217  ddd_.emplace_back(onePGD._nt5);
218  if (onePGD._numnt > 6) {
219  ddd_.emplace_back(onePGD._nt6);
220  if (onePGD._numnt > 7) {
221  ddd_.emplace_back(onePGD._nt7);
222  if (onePGD._numnt > 8) {
223  ddd_.emplace_back(onePGD._nt8);
224  if (onePGD._numnt > 9) {
225  ddd_.emplace_back(onePGD._nt9);
226  if (onePGD._numnt > 10) {
227  ddd_.emplace_back(onePGD._nt10);
228  }
229  }
230  }
231  }
232  }
233  }
234  }
235 }
236 
237 std::unique_ptr<Bounds> GeometricDet::bounds() const {
238  TrackerShapeToBounds shapeToBounds;
239  return std::unique_ptr<Bounds>(shapeToBounds.buildBounds(shape_, params_));
240 }
241 
243  Position pos_(static_cast<float>(geant_units::operators::convertMmToCm(trans_.x())),
244  static_cast<float>(geant_units::operators::convertMmToCm(trans_.y())),
245  static_cast<float>(geant_units::operators::convertMmToCm(trans_.z())));
246  return pos_;
247 }
248 
250  Translation x, y, z;
251  rot_.GetComponents(x, y, z);
252  Rotation rotation_(float(x.X()),
253  float(x.Y()),
254  float(x.Z()),
255  float(y.X()),
256  float(y.Y()),
257  float(y.Z()),
258  float(z.X()),
259  float(z.Y()),
260  float(z.Z()));
261  return rotation_;
262 }
263 
265  // iterate on all the DESCENDANTS!!
267  deepComponents(temp_);
268  return temp_;
269 }
270 
272  if (isLeaf())
273  cont.emplace_back(this);
274  else
275  std::for_each(container_.begin(), container_.end(), [&](const GeometricDet* iDet) { iDet->deepComponents(cont); });
276 }
277 
279  container_.reserve(container_.size() + cont.size());
280  std::copy(cont.begin(), cont.end(), back_inserter(container_));
281 }
282 
284  container_.reserve(container_.size() + cont.size());
285  std::copy(cont.begin(), cont.end(), back_inserter(container_));
286 }
287 
288 void GeometricDet::addComponent(GeometricDet* det) { container_.emplace_back(det); }
289 
290 namespace {
291  struct Deleter {
292  void operator()(GeometricDet const* det) const { delete const_cast<GeometricDet*>(det); }
293  };
294 } // namespace
295 
297  std::for_each(container_.begin(), container_.end(), Deleter());
298  container_.clear();
299 }
300 
301 /*
302  * PRIVATE
303  */
304 
305 /*
306  * DD4hep.
307  * Keep order and units of parameters same as old DD, to avoid numerous regressions.
308  * Shape parameters of interest, and those to be stored in DB, are only from boxes, trapezoids, and tubs.
309  * Hence, they are the only shapes treated here.
310  * params() will complain, if the parameters of any other shape are accessed.
311  */
312 std::vector<double> GeometricDet::computeLegacyShapeParameters(const cms::DDSolidShape& mySolidShape,
313  const dd4hep::Solid& mySolid) const {
314  std::vector<double> myOldDDShapeParameters;
315 
316  // Box
317  if (mySolidShape == cms::DDSolidShape::ddbox) {
318  const dd4hep::Box& myBox = dd4hep::Box(mySolid);
319  myOldDDShapeParameters = {(myBox.x()) / dd4hep::mm, (myBox.y()) / dd4hep::mm, (myBox.z()) / dd4hep::mm};
320  }
321 
322  // Trapezoid
323  else if (mySolidShape == cms::DDSolidShape::ddtrap) {
324  const dd4hep::Trap& myTrap = dd4hep::Trap(mySolid);
325  myOldDDShapeParameters = {(myTrap->GetDZ()) / dd4hep::mm,
326  static_cast<double>(angle_units::operators::convertDegToRad(myTrap->GetTheta())),
327  static_cast<double>(angle_units::operators::convertDegToRad(myTrap->GetPhi())),
328  (myTrap->GetH1()) / dd4hep::mm,
329  (myTrap->GetBl1()) / dd4hep::mm,
330  (myTrap->GetTl1()) / dd4hep::mm,
331  static_cast<double>(angle_units::operators::convertDegToRad(myTrap->GetAlpha1())),
332  (myTrap->GetH2()) / dd4hep::mm,
333  (myTrap->GetBl2()) / dd4hep::mm,
334  (myTrap->GetTl2()) / dd4hep::mm,
335  static_cast<double>(angle_units::operators::convertDegToRad(myTrap->GetAlpha2()))};
336  }
337 
338  // Tub
339  else if (mySolidShape == cms::DDSolidShape::ddtubs) {
340  const dd4hep::Tube& myTube = dd4hep::Tube(mySolid);
341  double phi1 = static_cast<double>(angle_units::operators::convertDegToRad(myTube->GetPhi1()));
342  phi1 = cms_rounding::roundIfNear0(phi1);
343  if (phi1 != 0.0) {
344  using namespace angle0to2pi;
345  phi1 = make0To2pi(phi1) - 2._pi;
346  // Turn positive angle to negative
347  }
348  myOldDDShapeParameters = {
349  (myTube->GetDz()) / dd4hep::mm,
350  (myTube->GetRmin()) / dd4hep::mm,
351  (myTube->GetRmax()) / dd4hep::mm,
352  phi1,
353  static_cast<double>(angle_units::operators::convertDegToRad(myTube->GetPhi2() - myTube->GetPhi1()))};
354  }
355 
356  return myOldDDShapeParameters;
357 }
358 
359 std::string GeometricDet::printNavType(int const* n, size_t sz) {
360  std::ostringstream oss;
361  oss << '(';
362  for (int const* it = n; it != n + sz; ++it) {
363  oss << *it << ',';
364  }
365  oss << ')';
366  return oss.str();
367 }
std::vector< GeometricDet * > GeometricDetContainer
Definition: GeometricDet.h:35
float bigPixelsPitchy_
Definition: GeometricDet.h:205
cms::DDSolidShape shape_
Definition: GeometricDet.h:193
bool isLowerSensor_
Definition: GeometricDet.h:209
constexpr double convertDegToRad(NumType degrees)
Definition: angle_units.h:27
double pixROCCols_
Definition: GeometricDet.h:199
void addComponent(GeometricDet *)
std::vector< double > params_
Definition: GeometricDet.h:194
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:186
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:210
nav_type ddd_
Definition: GeometricDet.h:185
ConstGeometricDetContainer container_
Definition: GeometricDet.h:215
double pixROCx_
Definition: GeometricDet.h:200
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:183
double radLength_
Definition: GeometricDet.h:196
Rotation rotationBounds() const
GeometricDet(DDFilteredView *fv, GeometricEnumType dd)
Definition: GeometricDet.cc:73
bool isFirstSensor_
Definition: GeometricDet.h:206
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:201
Namespace of DDCMS conversion namespace.
std::vector< double > computeLegacyShapeParameters(const cms::DDSolidShape &mySolidShape, const dd4hep::Solid &mySolid) const
bool isLeaf() const
Definition: GeometricDet.h:155
double pixROCRows_
Definition: GeometricDet.h:198
float bigPixelsPitchx_
Definition: GeometricDet.h:204
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
bool isSecondSensor_
Definition: GeometricDet.h:207
Translation trans_
Definition: GeometricDet.h:188
DDExpandedView::nav_type nav_type
void addComponents(GeometricDetContainer const &cont)
std::string ddname_
Definition: GeometricDet.h:182
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:191
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< double > > Translation
Definition: GeometricDet.h:37
cont
load Luminosity info ##
Definition: generateEDF.py:620
double siliconAPVNum_
Definition: GeometricDet.h:211
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