CMS 3D CMS Logo

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