CMS 3D CMS Logo

DDDefinitions2Objects.cc
Go to the documentation of this file.
1 #include "DD4hep/DetFactoryHelper.h"
2 #include "DD4hep/DetectorHelper.h"
3 #include "DD4hep/DD4hepUnits.h"
4 #include "DD4hep/GeoHandler.h"
5 #include "DD4hep/Printout.h"
6 #include "DD4hep/Plugins.h"
7 #include "DD4hep/detail/SegmentationsInterna.h"
8 #include "DD4hep/detail/DetectorInterna.h"
9 #include "DD4hep/detail/ObjectsInterna.h"
10 
11 #include "XML/Utilities.h"
17 
18 #include "TGeoManager.h"
19 #include "TGeoMaterial.h"
20 
21 #include <climits>
22 #include <iostream>
23 #include <iomanip>
24 #include <set>
25 #include <map>
26 #include <utility>
27 
28 using namespace std;
29 using namespace dd4hep;
30 using namespace cms;
31 
32 namespace dd4hep {
33 
34  namespace {
35 
36  UInt_t unique_mat_id = 0xAFFEFEED;
37 
38  class disabled_algo;
39  class include_constants;
40  class include_load;
41  class include_unload;
42  class print_xml_doc;
43 
44  class ConstantsSection;
45  class DDLConstant;
46  class DDRegistry {
47  public:
48  std::vector<xml::Document> includes;
49  std::map<std::string, std::string> unresolvedConst, allConst, originalConst;
50  };
51 
52  class MaterialSection;
55 
56  class RotationSection;
57  class DDLRotation;
58  class DDLRotationSequence;
59  class DDLRotationByAxis;
60  class DDLTransform3D;
61 
62  class PosPartSection;
63  class DDLPosPart;
64 
65  class LogicalPartSection;
66  class DDLLogicalPart;
67 
68  class SolidSection;
69  class DDLExtrudedPolygon;
70  class DDLShapeless;
71  class DDLTrapezoid;
72  class DDLEllipticalTube;
73  class DDLPseudoTrap;
74  class DDLPolyhedra;
75  class DDLPolycone;
76  class DDLTorus;
77  class DDLTrd1;
78  class DDLTruncTubs;
79  class DDLCutTubs;
80  class DDLTubs;
81  class DDLBox;
82  class DDLCone;
83  class DDLSphere;
84  class DDLUnionSolid;
85  class DDLIntersectionSolid;
86  class DDLSubtractionSolid;
87 
88  class DDLAlgorithm;
89  class DDLVector;
90 
91  class vissection;
92  class vis;
93  class debug;
94  }
95 
97  template <> void Converter<debug>::operator()(xml_h element) const;
98  template <> void Converter<print_xml_doc>::operator()(xml_h element) const;
99  template <> void Converter<disabled_algo>::operator()(xml_h element) const;
100 
102  template <> void Converter<ConstantsSection>::operator()(xml_h element) const;
103  template <> void Converter<DDLConstant>::operator()(xml_h element) const;
104  template <> void Converter<DDRegistry>::operator()(xml_h element) const;
105 
107  template <> void Converter<vissection>::operator()(xml_h element) const;
109  template <> void Converter<vis>::operator()(xml_h element) const;
110 
112  template <> void Converter<MaterialSection>::operator()(xml_h element) const;
113  template <> void Converter<DDLElementaryMaterial>::operator()(xml_h element) const;
114  template <> void Converter<DDLCompositeMaterial>::operator()(xml_h element) const;
115 
117  template <> void Converter<RotationSection>::operator()(xml_h element) const;
119  template <> void Converter<DDLRotation>::operator()(xml_h element) const;
121  template <> void Converter<DDLRotationSequence>::operator()(xml_h element) const;
123  template <> void Converter<DDLRotationByAxis>::operator()(xml_h element) const;
124  template <> void Converter<DDLTransform3D>::operator()(xml_h element) const;
125 
127  template <> void Converter<LogicalPartSection>::operator()(xml_h element) const;
128  template <> void Converter<DDLLogicalPart>::operator()(xml_h element) const;
129 
130  template <> void Converter<PosPartSection>::operator()(xml_h element) const;
132  template <> void Converter<DDLPosPart>::operator()(xml_h element) const;
133 
135  template <> void Converter<SolidSection>::operator()(xml_h element) const;
137  template <> void Converter<DDLUnionSolid>::operator()(xml_h element) const;
139  template <> void Converter<DDLSubtractionSolid>::operator()(xml_h element) const;
141  template <> void Converter<DDLIntersectionSolid>::operator()(xml_h element) const;
143  template <> void Converter<DDLPseudoTrap>::operator()(xml_h element) const;
145  template <> void Converter<DDLExtrudedPolygon>::operator()(xml_h element) const;
147  template <> void Converter<DDLShapeless>::operator()(xml_h element) const;
149  template <> void Converter<DDLTrapezoid>::operator()(xml_h element) const;
151  template <> void Converter<DDLPolycone>::operator()(xml_h element) const;
153  template <> void Converter<DDLPolyhedra>::operator()(xml_h element) const;
155  template <> void Converter<DDLEllipticalTube>::operator()(xml_h element) const;
157  template <> void Converter<DDLTorus>::operator()(xml_h element) const;
159  template <> void Converter<DDLTubs>::operator()(xml_h element) const;
161  template <> void Converter<DDLCutTubs>::operator()(xml_h element) const;
163  template <> void Converter<DDLTruncTubs>::operator()(xml_h element) const;
165  template <> void Converter<DDLSphere>::operator()(xml_h element) const;
167  template <> void Converter<DDLTrd1>::operator()(xml_h element) const;
169  template <> void Converter<DDLCone>::operator()(xml_h element) const;
171  template <> void Converter<DDLBox>::operator()(xml_h element) const;
173  template <> void Converter<DDLAlgorithm>::operator()(xml_h element) const;
175  template <> void Converter<DDLVector>::operator()(xml_h element) const;
176 
178  template <> void Converter<include_load>::operator()(xml_h element) const;
180  template <> void Converter<include_unload>::operator()(xml_h element) const;
182  template <> void Converter<include_constants>::operator()(xml_h element) const;
183 }
184 
186 template <> void Converter<ConstantsSection>::operator()(xml_h element) const {
187  cms::DDNamespace _ns(_param<cms::DDParsingContext>(), element);
188  xml_coll_t(element, _CMU(Constant)).for_each(Converter<DDLConstant>(description,_ns.context,optional));
189  xml_coll_t(element, _CMU(Vector)).for_each(Converter<DDLVector>(description,_ns.context,optional));
190 }
191 
193 template <> void Converter<vissection>::operator()(xml_h element) const {
194  cms::DDNamespace _ns(_param<cms::DDParsingContext>(), element);
195  xml_coll_t(element, _CMU(vis)).for_each(Converter<vis>(description,_ns.context,optional));
196 }
197 
199 template <> void Converter<MaterialSection>::operator()(xml_h element) const {
200  cms::DDNamespace _ns(_param<cms::DDParsingContext>(), element);
201  xml_coll_t(element, _CMU(ElementaryMaterial)).for_each(Converter<DDLElementaryMaterial>(description,_ns.context,optional));
202  xml_coll_t(element, _CMU(CompositeMaterial)).for_each(Converter<DDLCompositeMaterial>(description,_ns.context,optional));
203 }
204 
205 template <> void Converter<RotationSection>::operator()(xml_h element) const {
206  cms::DDNamespace _ns(_param<cms::DDParsingContext>(), element);
207  xml_coll_t(element, _CMU(Rotation)).for_each(Converter<DDLRotation>(description,_ns.context,optional));
208  xml_coll_t(element, _CMU(RotationSequence)).for_each(Converter<DDLRotationSequence>(description,_ns.context,optional));
209  xml_coll_t(element, _CMU(RotationByAxis)).for_each(Converter<DDLRotationByAxis>(description,_ns.context,optional));
210 }
211 
212 template <> void Converter<PosPartSection>::operator()(xml_h element) const {
213  cms::DDNamespace _ns(_param<cms::DDParsingContext>(), element);
214  xml_coll_t(element, _CMU(PosPart)).for_each(Converter<DDLPosPart>(description,_ns.context,optional));
215 }
216 
218 template <> void Converter<LogicalPartSection>::operator()(xml_h element) const {
219  cms::DDNamespace _ns(_param<cms::DDParsingContext>(), element);
220  xml_coll_t(element, _CMU(LogicalPart)).for_each(Converter<DDLLogicalPart>(description,_ns.context,optional));
221 }
222 
223 template <> void Converter<disabled_algo>::operator()(xml_h element) const {
224  cms::DDParsingContext* c = _param<cms::DDParsingContext>();
225  c->disabledAlgs.insert(element.attr<string>(_U(name)));
226 }
227 
229 template <> void Converter<SolidSection>::operator()(xml_h element) const {
230  cms::DDNamespace _ns(_param<cms::DDParsingContext>(), element);
231  for(xml_coll_t solid(element, _U(star)); solid; ++solid) {
232  string tag = solid.tag();
233  using cms::hash;
234  switch( hash( solid.tag()))
235  {
236  case hash("Box"):
237  Converter<DDLBox>(description,_ns.context,optional)(solid);
238  break;
239  case hash("Polycone"):
240  Converter<DDLPolycone>(description,_ns.context,optional)(solid);
241  break;
242  case hash("Polyhedra"):
243  Converter<DDLPolyhedra>(description,_ns.context,optional)(solid);
244  break;
245  case hash("Tubs"):
246  Converter<DDLTubs>(description,_ns.context,optional)(solid);
247  break;
248  case hash("CutTubs"):
249  Converter<DDLCutTubs>(description,_ns.context,optional)(solid);
250  break;
251  case hash("TruncTubs"):
252  Converter<DDLTruncTubs>(description,_ns.context,optional)(solid);
253  break;
254  case hash("Tube"):
255  Converter<DDLTubs>(description,_ns.context,optional)(solid);
256  break;
257  case hash("Trd1"):
258  Converter<DDLTrd1>(description,_ns.context,optional)(solid);
259  break;
260  case hash("Cone"):
261  Converter<DDLCone>(description,_ns.context,optional)(solid);
262  break;
263  case hash("Sphere"):
264  Converter<DDLSphere>(description,_ns.context,optional)(solid);
265  break;
266  case hash("EllipticalTube"):
267  Converter<DDLEllipticalTube>(description,_ns.context,optional)(solid);
268  break;
269  case hash("Torus"):
270  Converter<DDLTorus>(description,_ns.context,optional)(solid);
271  break;
272  case hash("PseudoTrap"):
273  Converter<DDLPseudoTrap>(description,_ns.context,optional)(solid);
274  break;
275  case hash("ExtrudedPolygon"):
276  Converter<DDLExtrudedPolygon>(description,_ns.context,optional)(solid);
277  break;
278  case hash("Trapezoid"):
279  Converter<DDLTrapezoid>(description,_ns.context,optional)(solid);
280  break;
281  case hash("UnionSolid"):
282  Converter<DDLUnionSolid>(description,_ns.context,optional)(solid);
283  break;
284  case hash("SubtractionSolid"):
285  Converter<DDLSubtractionSolid>(description,_ns.context,optional)(solid);
286  break;
287  case hash("IntersectionSolid"):
288  Converter<DDLIntersectionSolid>(description,_ns.context,optional)(solid);
289  break;
290  case hash("ShapelessSolid"):
291  Converter<DDLShapeless>(description,_ns.context,optional)(solid);
292  break;
293  default:
294  throw std::runtime_error( "Request to process unknown shape '" + xml_dim_t(solid).nameStr() + "' [" + tag + "]");
295  break;
296  }
297  }
298 }
299 
301 template <> void Converter<DDLConstant>::operator()(xml_h element) const {
302  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
303  DDRegistry* res = _option<DDRegistry>();
304  xml_dim_t constant = element;
305  xml_dim_t par = constant.parent();
306  bool eval = par.hasAttr(_U(eval)) ? par.attr<bool>(_U(eval)) : false;
307  string val = constant.valueStr();
308  string nam = constant.nameStr();
309  string real = _ns.prepend(nam);
310  string typ = eval ? "number" : "string";
311  size_t idx = val.find('[');
312 
313  if ( constant.hasAttr(_U(type)) )
314  typ = constant.typeStr();
315 
316  if ( idx == string::npos || typ == "string" ) {
317  try {
318  _ns.addConstant(nam, val, typ);
319  res->allConst[real] = val;
320  res->originalConst[real] = val;
321  }
322  catch(const exception& e) {
323  printout(INFO,"MyDDCMS","++ Unresolved constant: %s = %s [%s]. Try to resolve later. [%s]",
324  real.c_str(), val.c_str(), typ.c_str(), e.what());
325  }
326  return;
327  }
328  // Setup the resolution mechanism in Converter<resolve>
329  while ( idx != string::npos ) {
330  ++idx;
331  size_t idp = val.find(':',idx);
332  size_t idq = val.find(']',idx);
333  if ( idp == string::npos || idp > idq )
334  val.insert(idx,_ns.name());
335  else if ( idp != string::npos && idp < idq )
336  val[idp] = '_';
337  idx = val.find('[',idx);
338  }
339  while ( (idx=val.find(':')) != string::npos ) val[idx]='_';
340  printout(_ns.context->debug_constants ? ALWAYS : DEBUG,
341  "Constant","Unresolved: %s -> %s",real.c_str(),val.c_str());
342  res->allConst[real] = val;
343  res->originalConst[real] = val;
344  res->unresolvedConst[real] = val;
345 }
346 
355 template <> void Converter<vis>::operator()(xml_h e) const {
356  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
357  VisAttr attr(e.attr<string>(_U(name)));
358  float red = e.hasAttr(_U(r)) ? e.attr<float>(_U(r)) : 1.0f;
359  float green = e.hasAttr(_U(g)) ? e.attr<float>(_U(g)) : 1.0f;
360  float blue = e.hasAttr(_U(b)) ? e.attr<float>(_U(b)) : 1.0f;
361 
362  printout(_ns.context->debug_visattr ? ALWAYS : DEBUG, "Compact",
363  "++ Converting VisAttr structure: %-16s. R=%.3f G=%.3f B=%.3f",
364  attr.name(), red, green, blue);
365  attr.setColor(red, green, blue);
366  if (e.hasAttr(_U(alpha)))
367  attr.setAlpha(e.attr<float>(_U(alpha)));
368  if (e.hasAttr(_U(visible)))
369  attr.setVisible(e.attr<bool>(_U(visible)));
370  if (e.hasAttr(_U(lineStyle))) {
371  string ls = e.attr<string>(_U(lineStyle));
372  if (ls == "unbroken")
373  attr.setLineStyle(VisAttr::SOLID);
374  else if (ls == "broken")
375  attr.setLineStyle(VisAttr::DASHED);
376  }
377  else {
378  attr.setLineStyle(VisAttr::SOLID);
379  }
380  if (e.hasAttr(_U(drawingStyle))) {
381  string ds = e.attr<string>(_U(drawingStyle));
382  if (ds == "wireframe")
383  attr.setDrawingStyle(VisAttr::WIREFRAME);
384  else if (ds == "solid")
385  attr.setDrawingStyle(VisAttr::SOLID);
386  }
387  else {
388  attr.setDrawingStyle(VisAttr::SOLID);
389  }
390  if (e.hasAttr(_U(showDaughters)))
391  attr.setShowDaughters(e.attr<bool>(_U(showDaughters)));
392  else
393  attr.setShowDaughters(true);
394  description.addVisAttribute(attr);
395 }
396 
398 template <> void Converter<DDLElementaryMaterial>::operator()(xml_h element) const {
399  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
400  xml_dim_t xmat(element);
401  string nam = _ns.prepend(xmat.nameStr());
402  TGeoManager& mgr = description.manager();
403  TGeoMaterial* mat = mgr.GetMaterial(nam.c_str());
404  if ( nullptr == mat ) {
405  const char* matname = nam.c_str();
406  double density = xmat.density();
407  //double atomicWeight = xmat.attr<double>(_CMU(atomicWeight));
408  double atomicNumber = xmat.attr<double>(_CMU(atomicNumber));
409  TGeoElementTable* tab = mgr.GetElementTable();
410  TGeoMixture* mix = new TGeoMixture(nam.c_str(), 1, density);
411  TGeoElement* elt = tab->FindElement(xmat.nameStr().c_str());
412 
413  printout(_ns.context->debug_materials ? ALWAYS : DEBUG, "MyDDCMS",
414  "+++ Converting material %-48s Density: %.3f.",
415  ('"'+nam+'"').c_str(), density);
416 
417  if ( !elt ) {
418  printout(WARNING,"MyDDCMS",
419  "+++ Converter<ElementaryMaterial> No element present with name:%s [FAKE IT]",
420  matname);
421  int n = int(atomicNumber/2e0);
422  if ( n < 2 ) n = 2;
423  elt = new TGeoElement(xmat.nameStr().c_str(),"CMS element",n,atomicNumber);
424  //return;
425  }
426  if ( elt->Z() == 0 ) {
427  int n = int(atomicNumber/2e0);
428  if ( n < 2 ) n = 2;
429  elt = new TGeoElement((xmat.nameStr()+"-CMS").c_str(),"CMS element",n,atomicNumber);
430  }
431  mix->AddElement(elt, 1.0);
432  mix->SetRadLen(0e0);
434  TGeoMedium* medium = mgr.GetMedium(matname);
435  if (nullptr == medium) {
436  --unique_mat_id;
437  medium = new TGeoMedium(matname, unique_mat_id, mix);
438  medium->SetTitle("material");
439  medium->SetUniqueID(unique_mat_id);
440  }
441  }
442 }
443 
445 template <> void Converter<DDLCompositeMaterial>::operator()(xml_h element) const {
446  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
447  xml_dim_t xmat(element);
448  string nam = _ns.prepend(xmat.nameStr());
449  TGeoManager& mgr = description.manager();
450  TGeoMaterial* mat = mgr.GetMaterial(nam.c_str());
451  if ( nullptr == mat ) {
452  const char* matname = nam.c_str();
453  double density = xmat.density();
454  xml_coll_t composites(xmat,_CMU(MaterialFraction));
455  TGeoMixture* mix = new TGeoMixture(nam.c_str(), composites.size(), density);
456 
457  printout(_ns.context->debug_materials ? ALWAYS : DEBUG, "MyDDCMS",
458  "++ Converting material %-48s Density: %.3f.",
459  ('"'+nam+'"').c_str(), density);
460 
461  for (composites.reset(); composites; ++composites) {
462  xml_dim_t xfrac(composites);
463  xml_dim_t xfrac_mat(xfrac.child(_CMU(rMaterial)));
464  double fraction = xfrac.fraction();
465  string fracname = _ns.realName(xfrac_mat.nameStr());
466 
467  TGeoMaterial* frac_mat = mgr.GetMaterial(fracname.c_str());
468  if ( frac_mat ) {
469  mix->AddElement(frac_mat, fraction);
470  continue;
471  }
472  printout(WARNING,"MyDDCMS","+++ Composite material \"%s\" not present!",
473  fracname.c_str());
474  }
475  mix->SetRadLen(0e0);
477  TGeoMedium* medium = mgr.GetMedium(matname);
478  if (nullptr == medium) {
479  --unique_mat_id;
480  medium = new TGeoMedium(matname, unique_mat_id, mix);
481  medium->SetTitle("material");
482  medium->SetUniqueID(unique_mat_id);
483  }
484  }
485 }
486 
488 template <> void Converter<DDLRotation>::operator()(xml_h element) const {
489  cms::DDParsingContext* context = _param<cms::DDParsingContext>();
490  cms::DDNamespace _ns(context);
491  xml_dim_t xrot(element);
492  string nam = xrot.nameStr();
493  double thetaX = xrot.hasAttr(_CMU(thetaX)) ? _ns.attr<double>(xrot,_CMU(thetaX)) : 0e0;
494  double phiX = xrot.hasAttr(_CMU(phiX)) ? _ns.attr<double>(xrot,_CMU(phiX)) : 0e0;
495  double thetaY = xrot.hasAttr(_CMU(thetaY)) ? _ns.attr<double>(xrot,_CMU(thetaY)) : 0e0;
496  double phiY = xrot.hasAttr(_CMU(phiY)) ? _ns.attr<double>(xrot,_CMU(phiY)) : 0e0;
497  double thetaZ = xrot.hasAttr(_CMU(thetaZ)) ? _ns.attr<double>(xrot,_CMU(thetaZ)) : 0e0;
498  double phiZ = xrot.hasAttr(_CMU(phiZ)) ? _ns.attr<double>(xrot,_CMU(phiZ)) : 0e0;
499  Rotation3D rot = makeRotation3D(thetaX, phiX, thetaY, phiY, thetaZ, phiZ);
500  printout(context->debug_rotations ? ALWAYS : DEBUG,
501  "MyDDCMS","+++ Adding rotation: %-32s: (theta/phi)[rad] X: %6.3f %6.3f Y: %6.3f %6.3f Z: %6.3f %6.3f",
502  _ns.prepend(nam).c_str(),thetaX,phiX,thetaY,phiY,thetaZ,phiZ);
503  _ns.addRotation(nam, rot);
504 }
505 
507 template <> void Converter<DDLRotationSequence>::operator()(xml_h element) const {
508  cms::DDParsingContext* context = _param<cms::DDParsingContext>();
509  cms::DDNamespace _ns(context);
510  xml_dim_t xrot(element);
511  string nam = xrot.nameStr();
512  Rotation3D rot;
513  xml_coll_t rotations(xrot,_CMU(RotationByAxis));
514  for( rotations.reset(); rotations; ++rotations ) {
515  string axis = _ns.attr<string>(rotations,_CMU(axis));
516  double angle = _ns.attr<double>(rotations,_U(angle));
517  rot = makeRotation3D( rot, axis, angle );
518  printout(context->debug_rotations ? ALWAYS : DEBUG,
519  "MyDDCMS","+ Adding rotation to: %-29s: (axis/angle)[rad] Axis: %s Angle: %6.3f",
520  nam.c_str(), axis.c_str(), angle);
521  }
522  double xx, xy, xz;
523  double yx, yy, yz;
524  double zx, zy, zz;
525  rot.GetComponents(xx,xy,xz,yx,yy,yz,zx,zy,zz);
526  printout(context->debug_rotations ? ALWAYS : DEBUG,
527  "MyDDCMS","+++ Adding rotation sequence: %-23s: %6.3f %6.3f %6.3f, %6.3f %6.3f %6.3f, %6.3f %6.3f %6.3f",
528  _ns.prepend(nam).c_str(), xx, xy, xz, yx, yy, yz, zx, zy, zz);
529  _ns.addRotation(nam, rot);
530 }
531 
533 template <> void Converter<DDLRotationByAxis>::operator()(xml_h element) const {
534  cms::DDParsingContext* context = _param<cms::DDParsingContext>();
535  cms::DDNamespace _ns(context);
536  xml_dim_t xrot(element);
537  xml_dim_t par(xrot.parent());
538  if( xrot.hasAttr(_U(name))) {
539  string nam = xrot.nameStr();// + string("Rotation"); // xrot.hasAttr(_U(name)) ? xrot.nameStr() : par.nameStr();
540  string axis = _ns.attr<string>(xrot,_CMU(axis));
541  double angle = _ns.attr<double>(xrot,_U(angle));
542  Rotation3D rot;
543  rot = makeRotation3D( rot, axis, angle );
544  printout(context->debug_rotations ? ALWAYS : DEBUG,
545  "MyDDCMS","+++ Adding rotation: %-32s: (axis/angle)[rad] Axis: %s Angle: %6.3f",
546  _ns.prepend(nam).c_str(), axis.c_str(), angle);
547  _ns.addRotation(nam, rot);
548  }
549 }
550 
552 template <> void Converter<DDLLogicalPart>::operator()(xml_h element) const {
553  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
554  xml_dim_t e(element);
555  string sol = e.child(_CMU(rSolid)).attr<string>(_U(name));
556  string mat = e.child(_CMU(rMaterial)).attr<string>(_U(name));
557  _ns.addVolume(Volume(e.nameStr(), _ns.solid(sol), _ns.material(mat)));
558 }
559 
561 template <> void Converter<DDLTransform3D>::operator()(xml_h element) const {
562  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
563  Transform3D* tr = _option<Transform3D>();
564  xml_dim_t e(element);
565  xml_dim_t translation = e.child(_CMU(Translation),false);
566  xml_dim_t rotation = e.child(_CMU(Rotation),false);
567  xml_dim_t refRotation = e.child(_CMU(rRotation),false);
568  Position pos;
569  Rotation3D rot;
570 
571  if ( translation.ptr() ) {
572  double x = _ns.attr<double>(translation,_U(x));
573  double y = _ns.attr<double>(translation,_U(y));
574  double z = _ns.attr<double>(translation,_U(z));
575  pos = Position(x,y,z);
576  }
577  if ( rotation.ptr() ) {
578  double x = _ns.attr<double>(rotation,_U(x));
579  double y = _ns.attr<double>(rotation,_U(y));
580  double z = _ns.attr<double>(rotation,_U(z));
581  rot = RotationZYX(z,y,x);
582  }
583  else if ( refRotation.ptr() ) {
584  rot = _ns.rotation(refRotation.nameStr());
585  }
586  *tr = Transform3D(rot,pos);
587 }
588 
590 template <> void Converter<DDLPosPart>::operator()(xml_h element) const {
591  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
592  xml_dim_t e(element);
593  int copy = e.attr<int>(_CMU(copyNumber));
594  string parent_nam = _ns.attr<string>(e.child(_CMU(rParent)),_U(name));
595  string child_nam = _ns.attr<string>(e.child(_CMU(rChild)),_U(name));
596  Volume parent = _ns.volume(parent_nam);
597  Volume child = _ns.volume(child_nam, false);
598 
599  printout(_ns.context->debug_placements ? ALWAYS : DEBUG, "MyDDCMS",
600  "+++ %s Parent: %-24s [%s] Child: %-32s [%s] copy:%d",
601  e.tag().c_str(),
602  parent_nam.c_str(), parent.isValid() ? "VALID" : "INVALID",
603  child_nam.c_str(), child.isValid() ? "VALID" : "INVALID",
604  copy);
605  PlacedVolume pv;
606  if ( child.isValid() ) {
607  Transform3D trafo;
608  Converter<DDLTransform3D>(description,param,&trafo)(element);
609  pv = parent.placeVolume(child,copy,trafo);
610  }
611  if ( !pv.isValid() ) {
612  printout(ERROR,"MyDDCMS","+++ Placement FAILED! Parent:%s Child:%s Valid:%s",
613  parent.name(), child_nam.c_str(), yes_no(child.isValid()));
614  }
615 }
616 
617 template <typename TYPE>
618 static void convert_boolean(cms::DDParsingContext* context, xml_h element) {
619  cms::DDNamespace _ns(context);
620  xml_dim_t e(element);
621  string nam = e.nameStr();
622  Solid solids[2];
623  Solid boolean;
624  int cnt = 0;
625 
626  if ( e.hasChild(_CMU(rSolid)) ) { // Old version
627  for(xml_coll_t c(element, _CMU(rSolid)); cnt<2 && c; ++c, ++cnt)
628  solids[cnt] = _ns.solid(c.attr<string>(_U(name)));
629  }
630  else {
631  if ( (solids[0] = _ns.solid(e.attr<string>(_CMU(firstSolid)))).isValid() ) ++cnt;
632  if ( (solids[1] = _ns.solid(e.attr<string>(_CMU(secondSolid)))).isValid() ) ++cnt;
633  }
634  if ( cnt != 2 ) {
635  except("MyDDCMS","+++ Failed to create boolean solid %s. Found only %d parts.",nam.c_str(), cnt);
636  }
637  printout(_ns.context->debug_placements ? ALWAYS : DEBUG, "MyDDCMS",
638  "+++ BooleanSolid: %s Left: %-32s Right: %-32s",
639  nam.c_str(), solids[0]->GetName(), solids[1]->GetName());
640 
641  if ( solids[0].isValid() && solids[1].isValid() ) {
642  Transform3D trafo;
643  Converter<DDLTransform3D>(*context->description,context,&trafo)(element);
644  boolean = TYPE(solids[0],solids[1],trafo);
645  }
646  if ( !boolean.isValid() )
647  except("MyDDCMS","+++ FAILED to construct subtraction solid: %s",nam.c_str());
648  _ns.addSolid(nam,boolean);
649 }
650 
652 template <> void Converter<DDLUnionSolid>::operator()(xml_h element) const {
653  convert_boolean<UnionSolid>(_param<cms::DDParsingContext>(),element);
654 }
655 
657 template <> void Converter<DDLSubtractionSolid>::operator()(xml_h element) const {
658  convert_boolean<SubtractionSolid>(_param<cms::DDParsingContext>(),element);
659 }
660 
662 template <> void Converter<DDLIntersectionSolid>::operator()(xml_h element) const {
663  convert_boolean<IntersectionSolid>(_param<cms::DDParsingContext>(),element);
664 }
665 
667 template <> void Converter<DDLPolycone>::operator()(xml_h element) const {
668  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
669  xml_dim_t e(element);
670  string nam = e.nameStr();
671  double startPhi = _ns.attr<double>(e,_CMU(startPhi));
672  double deltaPhi = _ns.attr<double>(e,_CMU(deltaPhi));
673  vector<double> z, rmin, rmax, r;
674 
675  for(xml_coll_t rzpoint(element, _CMU(RZPoint)); rzpoint; ++rzpoint) {
676  z.emplace_back(_ns.attr<double>(rzpoint,_U(z)));
677  r.emplace_back(_ns.attr<double>(rzpoint,_U(r)));
678  }
679  if( z.empty()) {
680  for(xml_coll_t zplane(element, _CMU(ZSection)); zplane; ++zplane) {
681  rmin.emplace_back(_ns.attr<double>(zplane,_CMU(rMin)));
682  rmax.emplace_back(_ns.attr<double>(zplane,_CMU(rMax)));
683  z.emplace_back(_ns.attr<double>(zplane,_U(z)));
684  }
685  printout(_ns.context->debug_shapes ? ALWAYS : DEBUG, "MyDDCMS",
686  "+ Polycone: startPhi=%10.3f [rad] deltaPhi=%10.3f [rad] %4ld z-planes",
687  startPhi, deltaPhi, z.size());
688  _ns.addSolid(nam, Polycone(startPhi,deltaPhi,rmin,rmax,z));
689  }
690  else {
691  printout(_ns.context->debug_shapes ? ALWAYS : DEBUG, "MyDDCMS",
692  "+ Polycone: startPhi=%10.3f [rad] deltaPhi=%10.3f [rad] %4ld z-planes and %4ld radii",
693  startPhi, deltaPhi, z.size(), r.size());
694  _ns.addSolid(nam, Polycone(startPhi,deltaPhi,r,z));
695  }
696 }
697 
699 template <> void Converter<DDLExtrudedPolygon>::operator()(xml_h element) const {
700  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
701  xml_dim_t e(element);
702  string nam = e.nameStr();
703  vector<double> pt_x, pt_y, sec_x, sec_y, sec_z, sec_scale;
704 
705  for(xml_coll_t sec(element, _CMU(ZXYSection)); sec; ++sec) {
706  sec_z.emplace_back(_ns.attr<double>(sec,_U(z)));
707  sec_x.emplace_back(_ns.attr<double>(sec,_U(x)));
708  sec_y.emplace_back(_ns.attr<double>(sec,_U(y)));
709  sec_scale.emplace_back(_ns.attr<double>(sec,_CMU(scale),1.0));
710  }
711  for(xml_coll_t pt(element, _CMU(XYPoint)); pt; ++pt) {
712  pt_x.emplace_back(_ns.attr<double>(pt,_U(x)));
713  pt_y.emplace_back(_ns.attr<double>(pt,_U(y)));
714  }
715  printout(_ns.context->debug_shapes ? ALWAYS : DEBUG, "MyDDCMS",
716  "+ ExtrudedPolygon: %4ld points %4ld zxy sections",
717  pt_x.size(), sec_z.size());
718  _ns.addSolid(nam,ExtrudedPolygon(pt_x,pt_y,sec_z,sec_x,sec_y,sec_scale));
719 }
720 
722 template <> void Converter<DDLPolyhedra>::operator()(xml_h element) const {
723  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
724  xml_dim_t e(element);
725  string nam = e.nameStr();
726  double numSide = _ns.attr<int>(e,_CMU(numSide));
727  double startPhi = _ns.attr<double>(e,_CMU(startPhi));
728  double deltaPhi = _ns.attr<double>(e,_CMU(deltaPhi));
729  vector<double> z, rmin, rmax;
730 
731  for(xml_coll_t zplane(element, _CMU(RZPoint)); zplane; ++zplane) {
732  rmin.emplace_back(0.0);
733  rmax.emplace_back(_ns.attr<double>(zplane,_U(r)));
734  z.emplace_back(_ns.attr<double>(zplane,_U(z)));
735  }
736  for(xml_coll_t zplane(element, _CMU(ZSection)); zplane; ++zplane) {
737  rmin.emplace_back(_ns.attr<double>(zplane,_CMU(rMin)));
738  rmax.emplace_back(_ns.attr<double>(zplane,_CMU(rMax)));
739  z.emplace_back(_ns.attr<double>(zplane,_U(z)));
740  }
741  printout(_ns.context->debug_shapes ? ALWAYS : DEBUG, "MyDDCMS",
742  "+ Polyhedra:startPhi=%8.3f [rad] deltaPhi=%8.3f [rad] %4d sides %4ld z-planes",
743  startPhi, deltaPhi, numSide, z.size());
744  _ns.addSolid(nam, Polyhedra(numSide,startPhi,deltaPhi,z,rmin,rmax));
745 }
746 
748 template <> void Converter<DDLSphere>::operator()(xml_h element) const {
749  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
750  xml_dim_t e(element);
751  string nam = e.nameStr();
752  double rinner = _ns.attr<double>(e,_CMU(innerRadius));
753  double router = _ns.attr<double>(e,_CMU(outerRadius));
754  double startPhi = _ns.attr<double>(e,_CMU(startPhi));
755  double deltaPhi = _ns.attr<double>(e,_CMU(deltaPhi));
756  double startTheta = _ns.attr<double>(e,_CMU(startTheta));
757  double deltaTheta = _ns.attr<double>(e,_CMU(deltaTheta));
758  printout(_ns.context->debug_shapes ? ALWAYS : DEBUG, "MyDDCMS",
759  "+ Sphere: r_inner=%8.3f [cm] r_outer=%8.3f [cm]"
760  " startPhi=%8.3f [rad] deltaPhi=%8.3f startTheta=%8.3f delteTheta=%8.3f [rad]",
761  rinner, router, startPhi, deltaPhi, startTheta, deltaTheta);
762  _ns.addSolid(nam, Sphere(rinner, router, startTheta, deltaTheta, startPhi, deltaPhi));
763 }
764 
766 template <> void Converter<DDLTorus>::operator()(xml_h element) const {
767  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
768  xml_dim_t e(element);
769  string nam = e.nameStr();
770  double r = _ns.attr<double>(e,_CMU(torusRadius));
771  double rinner = _ns.attr<double>(e,_CMU(innerRadius));
772  double router = _ns.attr<double>(e,_CMU(outerRadius));
773  double startPhi = _ns.attr<double>(e,_CMU(startPhi));
774  double deltaPhi = _ns.attr<double>(e,_CMU(deltaPhi));
775  printout(_ns.context->debug_shapes ? ALWAYS : DEBUG, "MyDDCMS",
776  "+ Torus: r=%10.3f [cm] r_inner=%10.3f [cm] r_outer=%10.3f [cm]"
777  " startPhi=%10.3f [rad] deltaPhi=%10.3f [rad]",
778  r, rinner, router, startPhi, deltaPhi);
779  _ns.addSolid(nam, Torus(r, rinner, router, startPhi, deltaPhi));
780 }
781 
783 template <> void Converter<DDLPseudoTrap>::operator()(xml_h element) const {
784  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
785  xml_dim_t e(element);
786  string nam = e.nameStr();
787  double dx1 = _ns.attr<double>(e,_CMU(dx1));
788  double dy1 = _ns.attr<double>(e,_CMU(dy1));
789  double dx2 = _ns.attr<double>(e,_CMU(dx2));
790  double dy2 = _ns.attr<double>(e,_CMU(dy2));
791  double dz = _ns.attr<double>(e,_U(dz));
792  double r = _ns.attr<double>(e,_U(radius));
793  bool atMinusZ = _ns.attr<bool> (e,_CMU(atMinusZ));
794  printout(_ns.context->debug_shapes ? ALWAYS : DEBUG, "MyDDCMS",
795  "+ Pseudotrap: dz=%8.3f [cm] dx1:%.3f dy1:%.3f dx2=%.3f dy2=%.3f radius:%.3f atMinusZ:%s",
796  dz, dx1, dy1, dx2, dy2, r, yes_no(atMinusZ));
797  _ns.addSolid(nam, PseudoTrap(dx1, dx2, dy1, dy2, dz, r, atMinusZ));
798 }
799 
801 template <> void Converter<DDLTrapezoid>::operator()(xml_h element) const {
802  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
803  xml_dim_t e(element);
804  string nam = e.nameStr();
805  double dz = _ns.attr<double>(e,_U(dz));
806  double alp1 = _ns.attr<double>(e,_CMU(alp1));
807  double bl1 = _ns.attr<double>(e,_CMU(bl1));
808  double tl1 = _ns.attr<double>(e,_CMU(tl1));
809  double h1 = _ns.attr<double>(e,_CMU(h1));
810  double alp2 = _ns.attr<double>(e,_CMU(alp2));
811  double bl2 = _ns.attr<double>(e,_CMU(bl2));
812  double tl2 = _ns.attr<double>(e,_CMU(tl2));
813  double h2 = _ns.attr<double>(e,_CMU(h2));
814  double phi = _ns.attr<double>(e,_U(phi),0.0);
815  double theta = _ns.attr<double>(e,_U(theta),0.0);
816 
817  printout(_ns.context->debug_shapes ? ALWAYS : DEBUG, "MyDDCMS",
818  "+ Trapezoid: dz=%10.3f [cm] alp1:%.3f bl1=%.3f tl1=%.3f alp2=%.3f bl2=%.3f tl2=%.3f h2=%.3f phi=%.3f theta=%.3f",
819  dz, alp1, bl1, tl1, h1, alp2, bl2, tl2, h2, phi, theta);
820  _ns.addSolid( nam, Trap( dz, theta, phi, h1, bl1, tl1, alp1, h2, bl2, tl2, alp2 ));
821 }
822 
824 template <> void Converter<DDLTrd1>::operator()(xml_h element) const {
825  cms::DDNamespace _ns( _param<cms::DDParsingContext>());
826  xml_dim_t e( element );
827  string nam = e.nameStr();
828  double dx1 = _ns.attr<double>( e, _CMU( dx1 ));
829  double dy1 = _ns.attr<double>( e, _CMU( dy1 ));
830  double dx2 = _ns.attr<double>( e, _CMU( dx2 ), 0.0 );
831  double dy2 = _ns.attr<double>( e, _CMU( dy2 ), dy1 );
832  double dz = _ns.attr<double>( e, _CMU( dz ));
833  printout(_ns.context->debug_shapes ? ALWAYS : DEBUG, "MyDDCMS",
834  "+ Trd1: dz=%8.3f [cm] dx1:%.3f dy1:%.3f dx2:%.3f dy2:%.3f",
835  dz, dx1, dy1, dx2, dy2);
836  _ns.addSolid( nam, Trapezoid( dx1, dx2, dy1, dy2, dz ));
837 }
838 
840 template <> void Converter<DDLTubs>::operator()(xml_h element) const {
841  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
842  xml_dim_t e(element);
843  string nam = e.nameStr();
844  double dz = _ns.attr<double>(e,_CMU(dz));
845  double rmin = _ns.attr<double>(e,_CMU(rMin));
846  double rmax = _ns.attr<double>(e,_CMU(rMax));
847  double startPhi = _ns.attr<double>(e,_CMU(startPhi),0.0);
848  double deltaPhi = _ns.attr<double>(e,_CMU(deltaPhi),2*M_PI);
849  printout(_ns.context->debug_shapes ? ALWAYS : DEBUG, "MyDDCMS",
850  "+ Tubs: dz=%8.3f [cm] rmin=%8.3f [cm] rmax=%8.3f [cm]"
851  " startPhi=%8.3f [rad] deltaPhi=%8.3f [rad]", dz, rmin, rmax, startPhi, deltaPhi);
852  _ns.addSolid(nam, Tube(rmin,rmax,dz,startPhi,deltaPhi));
853 }
854 
856 template <> void Converter<DDLCutTubs>::operator()(xml_h element) const {
857  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
858  xml_dim_t e(element);
859  string nam = e.nameStr();
860  double dz = _ns.attr<double>(e,_CMU(dz));
861  double rmin = _ns.attr<double>(e,_CMU(rMin));
862  double rmax = _ns.attr<double>(e,_CMU(rMax));
863  double startPhi = _ns.attr<double>(e,_CMU(startPhi));
864  double deltaPhi = _ns.attr<double>(e,_CMU(deltaPhi));
865  double lx = _ns.attr<double>(e,_CMU(lx));
866  double ly = _ns.attr<double>(e,_CMU(ly));
867  double lz = _ns.attr<double>(e,_CMU(lz));
868  double tx = _ns.attr<double>(e,_CMU(tx));
869  double ty = _ns.attr<double>(e,_CMU(ty));
870  double tz = _ns.attr<double>(e,_CMU(tz));
871  printout(_ns.context->debug_shapes ? ALWAYS : DEBUG, "MyDDCMS",
872  "+ CutTube: dz=%8.3f [cm] rmin=%8.3f [cm] rmax=%8.3f [cm]"
873  " startPhi=%8.3f [rad] deltaPhi=%8.3f [rad]...",
874  dz, rmin, rmax, startPhi, deltaPhi);
875  _ns.addSolid(nam, CutTube(rmin,rmax,dz,startPhi,deltaPhi,lx,ly,lz,tx,ty,tz));
876 }
877 
879 template <> void Converter<DDLTruncTubs>::operator()(xml_h element) const {
880  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
881  xml_dim_t e(element);
882  string nam = e.nameStr();
883  double zhalf = _ns.attr<double>(e,_CMU(zHalf));
884  double rmin = _ns.attr<double>(e,_CMU(rMin));
885  double rmax = _ns.attr<double>(e,_CMU(rMax));
886  double startPhi = _ns.attr<double>(e,_CMU(startPhi));
887  double deltaPhi = _ns.attr<double>(e,_CMU(deltaPhi));
888  double cutAtStart = _ns.attr<double>(e,_CMU(cutAtStart));
889  double cutAtDelta = _ns.attr<double>(e,_CMU(cutAtDelta));
890  bool cutInside = _ns.attr<bool>(e,_CMU(cutInside));
891  printout(_ns.context->debug_shapes ? ALWAYS : DEBUG, "MyDDCMS",
892  "+ TruncTube:zHalf=%8.3f [cm] rmin=%8.3f [cm] rmax=%8.3f [cm]"
893  " startPhi=%8.3f [rad] deltaPhi=%8.3f [rad] atStart=%8.3f [cm] atDelta=%8.3f [cm] inside:%s",
894  zhalf, rmin, rmax, startPhi, deltaPhi, cutAtStart, cutAtDelta, yes_no(cutInside));
895  _ns.addSolid(nam, TruncatedTube(zhalf,rmin,rmax,startPhi,deltaPhi,cutAtStart,cutAtDelta,cutInside));
896 }
897 
899 template <> void Converter<DDLEllipticalTube>::operator()(xml_h element) const {
900  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
901  xml_dim_t e(element);
902  string nam = e.nameStr();
903  double dx = _ns.attr<double>(e,_CMU(xSemiAxis));
904  double dy = _ns.attr<double>(e,_CMU(ySemiAxis));
905  double dz = _ns.attr<double>(e,_CMU(zHeight));
906  printout(_ns.context->debug_shapes ? ALWAYS : DEBUG, "MyDDCMS",
907  "+ EllipticalTube xSemiAxis=%8.3f [cm] ySemiAxis=%8.3f [cm] zHeight=%8.3f [cm]",dx,dy,dz);
908  _ns.addSolid(nam, EllipticalTube(dx,dy,dz));
909 }
910 
912 template <> void Converter<DDLCone>::operator()(xml_h element) const {
913  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
914  xml_dim_t e(element);
915  string nam = e.nameStr();
916  double dz = _ns.attr<double>(e,_CMU(dz));
917  double rmin1 = _ns.attr<double>(e,_CMU(rMin1));
918  double rmin2 = _ns.attr<double>(e,_CMU(rMin2));
919  double rmax1 = _ns.attr<double>(e,_CMU(rMax1));
920  double rmax2 = _ns.attr<double>(e,_CMU(rMax2));
921  double startPhi = _ns.attr<double>(e,_CMU(startPhi));
922  double deltaPhi = _ns.attr<double>(e,_CMU(deltaPhi));
923  double phi2 = startPhi + deltaPhi;
924  printout(_ns.context->debug_shapes ? ALWAYS : DEBUG, "MyDDCMS",
925  "+ Cone: dz=%8.3f [cm]"
926  " rmin1=%8.3f [cm] rmax1=%8.3f [cm]"
927  " rmin2=%8.3f [cm] rmax2=%8.3f [cm]"
928  " startPhi=%8.3f [rad] deltaPhi=%8.3f [rad]",
929  dz, rmin1, rmax1, rmin2, rmax2, startPhi, deltaPhi);
930  _ns.addSolid(nam, ConeSegment(dz,rmin1,rmax1,rmin2,rmax2,startPhi,phi2));
931 }
932 
934 template <> void Converter<DDLShapeless>::operator()(xml_h element) const {
935  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
936  xml_dim_t e(element);
937  string nam = e.nameStr();
938  printout(_ns.context->debug_shapes ? ALWAYS : DEBUG, "MyDDCMS",
939  "+ Shapeless: THIS ONE CAN ONLY BE USED AT THE VOLUME LEVEL -> Assembly%s", nam.c_str());
940  _ns.addSolid(nam, Box(1,1,1));
941 }
942 
944 template <> void Converter<DDLBox>::operator()(xml_h element) const {
945  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
946  xml_dim_t e(element);
947  string nam = e.nameStr();
948  double dx = _ns.attr<double>(e,_CMU(dx));
949  double dy = _ns.attr<double>(e,_CMU(dy));
950  double dz = _ns.attr<double>(e,_CMU(dz));
951  printout(_ns.context->debug_shapes ? ALWAYS : DEBUG, "MyDDCMS",
952  "+ Box: dx=%10.3f [cm] dy=%10.3f [cm] dz=%10.3f [cm]", dx, dy, dz);
953  _ns.addSolid(nam, Box(dx,dy,dz));
954 }
955 
957 template <> void Converter<include_load>::operator()(xml_h element) const {
958  string fname = element.attr<string>(_U(ref));
959  edm::FileInPath fp( fname );
960  xml::Document doc;
961  doc = xml::DocumentHandler().load( fp.fullPath());
962  printout(_param<cms::DDParsingContext>()->debug_includes ? ALWAYS : DEBUG,
963  "MyDDCMS","+++ Processing the CMS detector description %s", fname.c_str());
964  _option<DDRegistry>()->includes.emplace_back( doc );
965 }
966 
968 template <> void Converter<include_unload>::operator()(xml_h element) const {
969  string fname = xml::DocumentHandler::system_path(element);
970  xml::DocumentHolder(xml_elt_t(element).document()).assign(nullptr);
971  printout(_param<cms::DDParsingContext>()->debug_includes ? ALWAYS : DEBUG,
972  "MyDDCMS","+++ Finished processing %s",fname.c_str());
973 }
974 
976 template <> void Converter<include_constants>::operator()(xml_h element) const {
977  xml_coll_t(element, _CMU(ConstantsSection)).for_each(Converter<ConstantsSection>(description,param,optional));
978 }
979 
981 template <> void Converter<DDLAlgorithm>::operator()(xml_h element) const {
982  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
983  xml_dim_t e(element);
984  string name = e.nameStr();
985  if ( _ns.context->disabledAlgs.find(name) != _ns.context->disabledAlgs.end() ) {
986  printout(INFO,"MyDDCMS","+++ Skip disabled algorithms: %s",name.c_str());
987  return;
988  }
989  try {
991  string type = "DDCMS_"+_ns.realName(name);
992 
993  // SensitiveDetector and Segmentation currently are undefined. Let's keep it like this
994  // until we found something better.....
995  printout(_ns.context->debug_algorithms ? ALWAYS : DEBUG,
996  "MyDDCMS","+++ Start executing algorithm %s....",type.c_str());
997 
998  long ret = PluginService::Create<long>(type, &description, _ns.context, &element, &sd);
999  if ( ret == 1 ) {
1000  printout(_ns.context->debug_algorithms ? ALWAYS : DEBUG,
1001  "MyDDCMS", "+++ Executed algorithm: %08lX = %s", ret, name.c_str());
1002  return;
1003  }
1004 #if 0
1005  Segmentation seg;
1006  DetElement det(PluginService::Create<NamedObject*>(type, &description, _ns.context, &element, &sd));
1007  if (det.isValid()) {
1008  // setChildTitles(make_pair(name, det));
1009  if ( sd.isValid() ) {
1010  det->flag |= DetElement::Object::HAVE_SENSITIVE_DETECTOR;
1011  }
1012  if ( seg.isValid() ) {
1013  seg->sensitive = sd;
1014  seg->detector = det;
1015  }
1016  }
1017  if (!det.isValid()) {
1018  PluginDebug dbg;
1019  PluginService::Create<NamedObject*>(type, &description, _ns.context, &element, &sd);
1020  except("MyDDCMS","Failed to execute subdetector creation plugin. " + dbg.missingFactory(type));
1021  }
1022  description.addDetector(det);
1023 #endif
1024  printout(ERROR, "MyDDCMS", "++ FAILED NOT ADDING SUBDETECTOR %08lX = %s",ret, name.c_str());
1026  return;
1027  }
1028  catch (const exception& exc) {
1029  printout(ERROR, "MyDDCMS", "++ FAILED to convert subdetector: %s: %s", name.c_str(), exc.what());
1030  terminate();
1031  }
1032  catch (...) {
1033  printout(ERROR, "MyDDCMS", "++ FAILED to convert subdetector: %s: %s", name.c_str(), "UNKNONW Exception");
1034  terminate();
1035  }
1036 }
1037 
1038 template <class InputIt, class ForwardIt, class BinOp>
1039 void for_each_token( InputIt first, InputIt last,
1040  ForwardIt s_first, ForwardIt s_last,
1041  BinOp binary_op)
1042 {
1043  while( first != last ) {
1044  const auto pos = std::find_first_of( first, last, s_first, s_last );
1045  binary_op( first, pos );
1046  if( pos == last ) break;
1047  first = std::next( pos );
1048  }
1049 }
1050 
1051 vector<double>
1052 splitNumeric( const string& str, const string& delims = "," )
1053 {
1054  vector<double> output;
1055 
1056  for_each_token( cbegin( str ), cend( str ),
1057  cbegin( delims ), cend( delims ),
1058  [&output] ( auto first, auto second ) {
1059  if( first != second ) {
1060  output.emplace_back(stod(string( first, second )));
1061  }
1062  });
1063  return output;
1064 }
1065 
1066 vector<string>
1067 splitString( const string& str, const string& delims = "," )
1068 {
1069  vector<string> output;
1070 
1071  for_each_token( cbegin( str ), cend( str ),
1072  cbegin( delims ), cend( delims ),
1073  [&output] ( auto first, auto second ) {
1074  if( first != second ) {
1075  output.emplace_back( first, second );
1076  }
1077  });
1078  return output;
1079 }
1080 
1083 template <> void Converter<DDLVector>::operator()( xml_h element ) const {
1085 
1086  cms::DDNamespace _ns( _param<cms::DDParsingContext>());
1087  xml_dim_t e( element );
1088  string name = e.nameStr();
1089  string type = _ns.attr<string>(e,_U(type));
1090  string nEntries = _ns.attr<string>(e,_CMU(nEntries));
1091  string val = e.text();
1092  val.erase( remove_if( val.begin(), val.end(), [](unsigned char x){return isspace(x);}), val.end());
1093 
1094  printout( _ns.context->debug_constants ? ALWAYS : DEBUG,
1095  "MyDDCMS","+++ Vector<%s>: %s[%s]: %s", type.c_str(), name.c_str(),
1096  nEntries.c_str(), val.c_str());
1097 
1098  vector<double> results = splitNumeric( val );
1099  registry->insert( { name, results } );
1100 
1101  for( auto it : results )
1102  cout << it << " ";
1103  cout << "\n";
1104 }
1105 
1106 template <> void Converter<debug>::operator()(xml_h dbg) const {
1107  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
1108  if ( dbg.hasChild(_CMU(debug_visattr)) ) _ns.context->debug_visattr = true;
1109  if ( dbg.hasChild(_CMU(debug_constants)) ) _ns.context->debug_constants = true;
1110  if ( dbg.hasChild(_CMU(debug_materials)) ) _ns.context->debug_materials = true;
1111  if ( dbg.hasChild(_CMU(debug_rotations)) ) _ns.context->debug_rotations = true;
1112  if ( dbg.hasChild(_CMU(debug_shapes)) ) _ns.context->debug_shapes = true;
1113  if ( dbg.hasChild(_CMU(debug_volumes)) ) _ns.context->debug_volumes = true;
1114  if ( dbg.hasChild(_CMU(debug_placements)) ) _ns.context->debug_placements = true;
1115  if ( dbg.hasChild(_CMU(debug_namespaces)) ) _ns.context->debug_namespaces = true;
1116  if ( dbg.hasChild(_CMU(debug_includes)) ) _ns.context->debug_includes = true;
1117  if ( dbg.hasChild(_CMU(debug_algorithms)) ) _ns.context->debug_algorithms = true;
1118 }
1119 
1120 template <> void Converter<DDRegistry>::operator()(xml_h /* element */) const {
1121  cms::DDParsingContext* context = _param<cms::DDParsingContext>();
1122  DDRegistry* res = _option<DDRegistry>();
1123  cms::DDNamespace _ns(context);
1124  int count = 0;
1125 
1126  printout(context->debug_constants ? ALWAYS : DEBUG,
1127  "MyDDCMS","+++ RESOLVING %ld unknown constants.....",res->unresolvedConst.size());
1128  while ( !res->unresolvedConst.empty() ) {
1129  for(auto i=res->unresolvedConst.begin(); i!=res->unresolvedConst.end(); ++i ) {
1130  const string& n = (*i).first;
1131  string rep;
1132  string& v = (*i).second;
1133  size_t idx, idq;
1134  for(idx=v.find('[',0); idx != string::npos; idx = v.find('[',idx+1) ) {
1135  idq = v.find(']',idx+1);
1136  rep = v.substr(idx+1,idq-idx-1);
1137  auto r = res->allConst.find(rep);
1138  if ( r != res->allConst.end() ) {
1139  rep = "("+(*r).second+")";
1140  v.replace(idx,idq-idx+1,rep);
1141  }
1142  }
1143  if ( v.find(']') == string::npos ) {
1144  if ( v.find("-+") != string::npos || v.find("+-") != string::npos ) {
1145  while ( (idx=v.find("-+")) != string::npos )
1146  v.replace(idx,2,"-");
1147  while ( (idx=v.find("+-")) != string::npos )
1148  v.replace(idx,2,"-");
1149  }
1150  printout(context->debug_constants ? ALWAYS : DEBUG,
1151  "MyDDCMS","+++ [%06ld] ---------- %-40s = %s",
1152  res->unresolvedConst.size()-1,n.c_str(),res->originalConst[n].c_str());
1153  _ns.addConstantNS(n, v, "number");
1154  res->unresolvedConst.erase(i);
1155  break;
1156  }
1157  }
1158  if ( ++count > 1000 ) break;
1159  }
1160  if ( !res->unresolvedConst.empty() ) {
1161  for(const auto& e : res->unresolvedConst )
1162  printout(ERROR,"MyDDCMS","+++ Unresolved constant: %-40s = %s.",e.first.c_str(), e.second.c_str());
1163  except("MyDDCMS","++ FAILED to resolve %ld constant entries:",res->unresolvedConst.size());
1164  }
1165  res->unresolvedConst.clear();
1166  res->originalConst.clear();
1167  res->allConst.clear();
1168 }
1169 
1170 template <> void Converter<print_xml_doc>::operator()(xml_h element) const {
1171  string fname = xml::DocumentHandler::system_path(element);
1172  printout(_param<cms::DDParsingContext>()->debug_includes ? ALWAYS : DEBUG,
1173  "MyDDCMS","+++ Processing data from: %s",fname.c_str());
1174 }
1175 
1177 static long load_dddefinition(Detector& det, xml_h element) {
1178  static cms::DDParsingContext context(&det);
1179  cms::DDNamespace _ns(context);
1180  xml_elt_t dddef(element);
1181  string fname = xml::DocumentHandler::system_path(element);
1182  bool open_geometry = dddef.hasChild(_CMU(open_geometry));
1183  bool close_geometry = dddef.hasChild(_CMU(close_geometry));
1184 
1185  xml_coll_t(dddef, _U(debug)).for_each(Converter<debug>(det,&context));
1186 
1187  // Here we define the order how XML elements are processed.
1188  // Be aware of dependencies. This can only defined once.
1189  // At the end it is a limitation of DOM....
1190  printout(INFO,"MyDDCMS","+++ Processing the CMS detector description %s",fname.c_str());
1191 
1192  xml::Document doc;
1193  Converter<print_xml_doc> print_doc(det,&context);
1194  try {
1195  DDRegistry res;
1196  print_doc((doc=dddef.document()).root());
1197  xml_coll_t(dddef, _CMU(DisabledAlgo)).for_each(Converter<disabled_algo>(det,&context,&res));
1198  xml_coll_t(dddef, _CMU(ConstantsSection)).for_each(Converter<ConstantsSection>(det,&context,&res));
1199  xml_coll_t(dddef, _CMU(VisSection)).for_each(Converter<vissection>(det,&context));
1200  xml_coll_t(dddef, _CMU(RotationSection)).for_each(Converter<RotationSection>(det,&context));
1201  xml_coll_t(dddef, _CMU(MaterialSection)).for_each(Converter<MaterialSection>(det,&context));
1202 
1203  xml_coll_t(dddef, _CMU(IncludeSection)).for_each(_CMU(Include), Converter<include_load>(det,&context,&res));
1204 
1205  for(xml::Document d : res.includes ) {
1206  print_doc((doc=d).root());
1207  Converter<include_constants>(det,&context,&res)((doc=d).root());
1208  }
1209  // Before we continue, we have to resolve all constants NOW!
1210  Converter<DDRegistry>(det,&context,&res)(dddef);
1211  // Now we can process the include files one by one.....
1212  for(xml::Document d : res.includes ) {
1213  print_doc((doc=d).root());
1214  xml_coll_t(d.root(),_CMU(MaterialSection)).for_each(Converter<MaterialSection>(det,&context));
1215  }
1216  if ( open_geometry ) {
1217  context.geo_inited = true;
1218  det.init();
1219  _ns.addVolume(det.worldVolume());
1220  }
1221  for(xml::Document d : res.includes ) {
1222  print_doc((doc=d).root());
1223  xml_coll_t(d.root(),_CMU(RotationSection)).for_each(Converter<RotationSection>(det,&context));
1224  }
1225  for(xml::Document d : res.includes ) {
1226  print_doc((doc=d).root());
1227  xml_coll_t(d.root(), _CMU(SolidSection)).for_each(Converter<SolidSection>(det,&context));
1228  }
1229  for(xml::Document d : res.includes ) {
1230  print_doc((doc=d).root());
1231  xml_coll_t(d.root(), _CMU(LogicalPartSection)).for_each(Converter<LogicalPartSection>(det,&context));
1232  }
1233  for(xml::Document d : res.includes ) {
1234  print_doc((doc=d).root());
1235  xml_coll_t(d.root(), _CMU(Algorithm)).for_each(Converter<DDLAlgorithm>(det,&context));
1236  }
1237  for(xml::Document d : res.includes ) {
1238  print_doc((doc=d).root());
1239  xml_coll_t(d.root(), _CMU(PosPartSection)).for_each(Converter<PosPartSection>(det,&context));
1240  }
1241 
1243  for(xml::Document d : res.includes ) Converter<include_unload>(det,&context,&res)(d.root());
1244 
1245  print_doc((doc=dddef.document()).root());
1246  // Now process the actual geometry items
1247  xml_coll_t(dddef, _CMU(SolidSection)).for_each(Converter<SolidSection>(det,&context));
1248  xml_coll_t(dddef, _CMU(LogicalPartSection)).for_each(Converter<LogicalPartSection>(det,&context));
1249  xml_coll_t(dddef, _CMU(Algorithm)).for_each(Converter<DDLAlgorithm>(det,&context));
1250  xml_coll_t(dddef, _CMU(PosPartSection)).for_each(Converter<PosPartSection>(det,&context));
1251  }
1252  catch(const exception& e) {
1253  printout(ERROR,"MyDDCMS","Exception while processing xml source:%s",doc.uri().c_str());
1254  printout(ERROR,"MyDDCMS","----> %s", e.what());
1255  throw;
1256  }
1257 
1259  if ( close_geometry ) {
1260  det.endDocument();
1261  }
1262  printout(INFO,"DDDefinition","+++ Finished processing %s",fname.c_str());
1263  return 1;
1264 }
1265 
1266 // Now declare the factory entry for the plugin mechanism
1267 DECLARE_XML_DOC_READER(DDDefinition,load_dddefinition)
DDLPosPart handles PosPart elements.
Definition: DDLPosPart.h:27
type
Definition: HCALResponse.h:21
#define DEBUG
float alpha
Definition: AMPTWrapper.h:95
DDLSphere processes all Sphere elements.
Definition: DDLSphere.h:24
#define _CMU(a)
Definition: DDXMLTags.h:173
std::map< std::string, std::string > allConst
def copy(args, dbName)
ROOT::Math::Plane3D::Vector Vector
Definition: EcalHitMaker.cc:29
DDLVector handles Rotation and ReflectionRotation elements.
Definition: DDLVector.h:29
dd4hep::Detector * description
Geom::Theta< T > theta() const
static const int WARNING
DDLEllipticalTube processes all EllipticalTube elements.
dd4hep::Volume addVolume(dd4hep::Volume vol) const
Add rotation matrix to current namespace.
Definition: DDNamespace.cc:219
dd4hep::Solid solid(const std::string &name) const
Definition: DDNamespace.cc:270
DDLAlgorithm processes Algorithm elements.
Definition: DDLAlgorithm.h:25
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e g
Definition: Activities.doc:4
Definition: Electron.h:6
dd4hep::Rotation3D makeRotation3D(double thetaX, double phiX, double thetaY, double phiY, double thetaZ, double phiZ)
U second(std::pair< T, U > const &p)
DDLBox processes Box elements.
Definition: DDLBox.h:27
DDLElementaryMaterial processes ElementaryMaterial elements.
vector< string > splitString(const string &str, const string &delims=",")
std::string realName(const std::string &) const
Definition: DDNamespace.cc:93
T attr(xml_elt_t element, const xml_tag_t &name) const
Definition: DDNamespace.h:32
DDLCone processes all Cone elements.
Definition: DDLCone.h:27
vector< double > splitNumeric(const string &str, const string &delims=",")
def pv(vc)
Definition: MetAnalyzer.py:6
double f[11][100]
constexpr unsigned int hash(const char *str, int h=0)
static void convert_boolean(cms::DDParsingContext *context, xml_h element)
rep
Definition: cuy.py:1189
#define M_PI
std::set< std::string > disabledAlgs
Namespace of DDCMS conversion namespace.
DDLTubs processes Tubs elements.
Definition: DDLTubs.h:21
Polynomial< 0 > Constant
Definition: Constant.h:6
def green(string)
std::map< std::string, std::string > originalConst
def ls(path, rec=False)
Definition: eostools.py:348
#define debug
Definition: HDRShower.cc:19
dd4hep::Solid addSolid(const std::string &name, dd4hep::Solid solid) const
Definition: DDNamespace.cc:264
DDLLogicalPart processes LogicalPart elements.
DDParsingContext * context
Definition: DDNamespace.h:63
double sd
double b
Definition: hdecay.h:120
DDLRotationByAxis handles RotationByAxis elements.
DDLCompositeMaterial processes all CompositeMaterial elements.
std::map< std::string, std::string > unresolvedConst
string fname
main script
std::vector< xml::Document > includes
static long load_dddefinition(Detector &det, xml_h element)
Converter for <DDDefinition> tags.
static Interceptor::Registry registry("Interceptor")
#define str(s)
DDLRotationSequence handles a set of Rotations.
void for_each_token(InputIt first, InputIt last, ForwardIt s_first, ForwardIt s_last, BinOp binary_op)
static const int ERROR
T angle(T x1, T y1, T z1, T x2, T y2, T z2)
Definition: angle.h:11