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