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] = NAMESPACE_SEP;
337  idx = val.find('[',idx);
338  }
339  printout(_ns.context->debug_constants ? ALWAYS : DEBUG,
340  "Constant","Unresolved: %s -> %s",real.c_str(),val.c_str());
341  res->allConst[real] = val;
342  res->originalConst[real] = val;
343  res->unresolvedConst[real] = val;
344 }
345 
354 template <> void Converter<vis>::operator()(xml_h e) const {
355  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
356  VisAttr attr(e.attr<string>(_U(name)));
357  float red = e.hasAttr(_U(r)) ? e.attr<float>(_U(r)) : 1.0f;
358  float green = e.hasAttr(_U(g)) ? e.attr<float>(_U(g)) : 1.0f;
359  float blue = e.hasAttr(_U(b)) ? e.attr<float>(_U(b)) : 1.0f;
360 
361  printout(_ns.context->debug_visattr ? ALWAYS : DEBUG, "Compact",
362  "++ Converting VisAttr structure: %-16s. R=%.3f G=%.3f B=%.3f",
363  attr.name(), red, green, blue);
364  attr.setColor(red, green, blue);
365  if (e.hasAttr(_U(alpha)))
366  attr.setAlpha(e.attr<float>(_U(alpha)));
367  if (e.hasAttr(_U(visible)))
368  attr.setVisible(e.attr<bool>(_U(visible)));
369  if (e.hasAttr(_U(lineStyle))) {
370  string ls = e.attr<string>(_U(lineStyle));
371  if (ls == "unbroken")
372  attr.setLineStyle(VisAttr::SOLID);
373  else if (ls == "broken")
374  attr.setLineStyle(VisAttr::DASHED);
375  }
376  else {
377  attr.setLineStyle(VisAttr::SOLID);
378  }
379  if (e.hasAttr(_U(drawingStyle))) {
380  string ds = e.attr<string>(_U(drawingStyle));
381  if (ds == "wireframe")
382  attr.setDrawingStyle(VisAttr::WIREFRAME);
383  else if (ds == "solid")
384  attr.setDrawingStyle(VisAttr::SOLID);
385  }
386  else {
387  attr.setDrawingStyle(VisAttr::SOLID);
388  }
389  if (e.hasAttr(_U(showDaughters)))
390  attr.setShowDaughters(e.attr<bool>(_U(showDaughters)));
391  else
392  attr.setShowDaughters(true);
393  description.addVisAttribute(attr);
394 }
395 
397 template <> void Converter<DDLElementaryMaterial>::operator()(xml_h element) const {
398  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
399  xml_dim_t xmat(element);
400  string nam = _ns.prepend(xmat.nameStr());
401  TGeoManager& mgr = description.manager();
402  TGeoMaterial* mat = mgr.GetMaterial(nam.c_str());
403  if ( nullptr == mat ) {
404  const char* matname = nam.c_str();
405  double density = xmat.density();
406  //double atomicWeight = xmat.attr<double>(_CMU(atomicWeight));
407  double atomicNumber = xmat.attr<double>(_CMU(atomicNumber));
408  TGeoElementTable* tab = mgr.GetElementTable();
409  TGeoMixture* mix = new TGeoMixture(nam.c_str(), 1, density);
410  TGeoElement* elt = tab->FindElement(xmat.nameStr().c_str());
411 
412  printout(_ns.context->debug_materials ? ALWAYS : DEBUG, "MyDDCMS",
413  "+++ Converting material %-48s Density: %.3f.",
414  ('"'+nam+'"').c_str(), density);
415 
416  if ( !elt ) {
417  printout(WARNING,"MyDDCMS",
418  "+++ Converter<ElementaryMaterial> No element present with name:%s [FAKE IT]",
419  matname);
420  int n = int(atomicNumber/2e0);
421  if ( n < 2 ) n = 2;
422  elt = new TGeoElement(xmat.nameStr().c_str(),"CMS element",n,atomicNumber);
423  //return;
424  }
425  if ( elt->Z() == 0 ) {
426  int n = int(atomicNumber/2e0);
427  if ( n < 2 ) n = 2;
428  elt = new TGeoElement((xmat.nameStr()+"-CMS").c_str(),"CMS element",n,atomicNumber);
429  }
430  mix->AddElement(elt, 1.0);
431  mix->SetRadLen(0e0);
433  TGeoMedium* medium = mgr.GetMedium(matname);
434  if (nullptr == medium) {
435  --unique_mat_id;
436  medium = new TGeoMedium(matname, unique_mat_id, mix);
437  medium->SetTitle("material");
438  medium->SetUniqueID(unique_mat_id);
439  }
440  }
441 }
442 
444 template <> void Converter<DDLCompositeMaterial>::operator()(xml_h element) const {
445  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
446  xml_dim_t xmat(element);
447  string nam = _ns.prepend(xmat.nameStr());
448  TGeoManager& mgr = description.manager();
449  TGeoMaterial* mat = mgr.GetMaterial(nam.c_str());
450  if ( nullptr == mat ) {
451  const char* matname = nam.c_str();
452  double density = xmat.density();
453  xml_coll_t composites(xmat,_CMU(MaterialFraction));
454  TGeoMixture* mix = new TGeoMixture(nam.c_str(), composites.size(), density);
455 
456  printout(_ns.context->debug_materials ? ALWAYS : DEBUG, "MyDDCMS",
457  "++ Converting material %-48s Density: %.3f.",
458  ('"'+nam+'"').c_str(), density);
459 
460  for (composites.reset(); composites; ++composites) {
461  xml_dim_t xfrac(composites);
462  xml_dim_t xfrac_mat(xfrac.child(_CMU(rMaterial)));
463  double fraction = xfrac.fraction();
464  string fracname = _ns.realName(xfrac_mat.nameStr());
465 
466  TGeoMaterial* frac_mat = mgr.GetMaterial(fracname.c_str());
467  if ( frac_mat ) {
468  mix->AddElement(frac_mat, fraction);
469  continue;
470  }
471  printout(WARNING,"MyDDCMS","+++ Composite material \"%s\" not present!",
472  fracname.c_str());
473  }
474  mix->SetRadLen(0e0);
476  TGeoMedium* medium = mgr.GetMedium(matname);
477  if (nullptr == medium) {
478  --unique_mat_id;
479  medium = new TGeoMedium(matname, unique_mat_id, mix);
480  medium->SetTitle("material");
481  medium->SetUniqueID(unique_mat_id);
482  }
483  }
484 }
485 
487 template <> void Converter<DDLRotation>::operator()(xml_h element) const {
488  cms::DDParsingContext* context = _param<cms::DDParsingContext>();
489  cms::DDNamespace _ns(context);
490  xml_dim_t xrot(element);
491  string nam = xrot.nameStr();
492  double thetaX = xrot.hasAttr(_CMU(thetaX)) ? _ns.attr<double>(xrot,_CMU(thetaX)) : 0e0;
493  double phiX = xrot.hasAttr(_CMU(phiX)) ? _ns.attr<double>(xrot,_CMU(phiX)) : 0e0;
494  double thetaY = xrot.hasAttr(_CMU(thetaY)) ? _ns.attr<double>(xrot,_CMU(thetaY)) : 0e0;
495  double phiY = xrot.hasAttr(_CMU(phiY)) ? _ns.attr<double>(xrot,_CMU(phiY)) : 0e0;
496  double thetaZ = xrot.hasAttr(_CMU(thetaZ)) ? _ns.attr<double>(xrot,_CMU(thetaZ)) : 0e0;
497  double phiZ = xrot.hasAttr(_CMU(phiZ)) ? _ns.attr<double>(xrot,_CMU(phiZ)) : 0e0;
498  Rotation3D rot = makeRotation3D(thetaX, phiX, thetaY, phiY, thetaZ, phiZ);
499  printout(context->debug_rotations ? ALWAYS : DEBUG,
500  "MyDDCMS","+++ Adding rotation: %-32s: (theta/phi)[rad] X: %6.3f %6.3f Y: %6.3f %6.3f Z: %6.3f %6.3f",
501  _ns.prepend(nam).c_str(),thetaX,phiX,thetaY,phiY,thetaZ,phiZ);
502  _ns.addRotation(nam, rot);
503 }
504 
506 template <> void Converter<DDLRotationSequence>::operator()(xml_h element) const {
507  cms::DDParsingContext* context = _param<cms::DDParsingContext>();
508  cms::DDNamespace _ns(context);
509  xml_dim_t xrot(element);
510  string nam = xrot.nameStr();
511  Rotation3D rot;
512  xml_coll_t rotations(xrot,_CMU(RotationByAxis));
513  for( rotations.reset(); rotations; ++rotations ) {
514  string axis = _ns.attr<string>(rotations,_CMU(axis));
515  double angle = _ns.attr<double>(rotations,_U(angle));
516  rot = makeRotation3D( rot, axis, angle );
517  printout(context->debug_rotations ? ALWAYS : DEBUG,
518  "MyDDCMS","+ Adding rotation to: %-29s: (axis/angle)[rad] Axis: %s Angle: %6.3f",
519  nam.c_str(), axis.c_str(), angle);
520  }
521  double xx, xy, xz;
522  double yx, yy, yz;
523  double zx, zy, zz;
524  rot.GetComponents(xx,xy,xz,yx,yy,yz,zx,zy,zz);
525  printout(context->debug_rotations ? ALWAYS : DEBUG,
526  "MyDDCMS","+++ Adding rotation sequence: %-23s: %6.3f %6.3f %6.3f, %6.3f %6.3f %6.3f, %6.3f %6.3f %6.3f",
527  _ns.prepend(nam).c_str(), xx, xy, xz, yx, yy, yz, zx, zy, zz);
528  _ns.addRotation(nam, rot);
529 }
530 
532 template <> void Converter<DDLRotationByAxis>::operator()(xml_h element) const {
533  cms::DDParsingContext* context = _param<cms::DDParsingContext>();
534  cms::DDNamespace _ns(context);
535  xml_dim_t xrot(element);
536  xml_dim_t par(xrot.parent());
537  if( xrot.hasAttr(_U(name))) {
538  string nam = xrot.nameStr();// + string("Rotation"); // xrot.hasAttr(_U(name)) ? xrot.nameStr() : par.nameStr();
539  string axis = _ns.attr<string>(xrot,_CMU(axis));
540  double angle = _ns.attr<double>(xrot,_U(angle));
541  Rotation3D rot;
542  rot = makeRotation3D( rot, axis, angle );
543  printout(context->debug_rotations ? ALWAYS : DEBUG,
544  "MyDDCMS","+++ Adding rotation: %-32s: (axis/angle)[rad] Axis: %s Angle: %6.3f",
545  _ns.prepend(nam).c_str(), axis.c_str(), angle);
546  _ns.addRotation(nam, rot);
547  }
548 }
549 
551 template <> void Converter<DDLLogicalPart>::operator()(xml_h element) const {
552  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
553  xml_dim_t e(element);
554  string sol = e.child(_CMU(rSolid)).attr<string>(_U(name));
555  string mat = e.child(_CMU(rMaterial)).attr<string>(_U(name));
556  _ns.addVolume(Volume(e.nameStr(), _ns.solid(sol), _ns.material(mat)));
557 }
558 
560 template <> void Converter<DDLTransform3D>::operator()(xml_h element) const {
561  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
562  Transform3D* tr = _option<Transform3D>();
563  xml_dim_t e(element);
564  xml_dim_t translation = e.child(_CMU(Translation),false);
565  xml_dim_t rotation = e.child(_CMU(Rotation),false);
566  xml_dim_t refRotation = e.child(_CMU(rRotation),false);
567  Position pos;
568  Rotation3D rot;
569 
570  if ( translation.ptr() ) {
571  double x = _ns.attr<double>(translation,_U(x));
572  double y = _ns.attr<double>(translation,_U(y));
573  double z = _ns.attr<double>(translation,_U(z));
574  pos = Position(x,y,z);
575  }
576  if ( rotation.ptr() ) {
577  double x = _ns.attr<double>(rotation,_U(x));
578  double y = _ns.attr<double>(rotation,_U(y));
579  double z = _ns.attr<double>(rotation,_U(z));
580  rot = RotationZYX(z,y,x);
581  }
582  else if ( refRotation.ptr() ) {
583  rot = _ns.rotation(refRotation.nameStr());
584  }
585  *tr = Transform3D(rot,pos);
586 }
587 
589 template <> void Converter<DDLPosPart>::operator()(xml_h element) const {
590  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
591  xml_dim_t e(element);
592  int copy = e.attr<int>(_CMU(copyNumber));
593  string parent_nam = _ns.attr<string>(e.child(_CMU(rParent)),_U(name));
594  string child_nam = _ns.attr<string>(e.child(_CMU(rChild)),_U(name));
595  Volume parent = _ns.volume(parent_nam);
596  Volume child = _ns.volume(child_nam, false);
597 
598  printout(_ns.context->debug_placements ? ALWAYS : DEBUG, "MyDDCMS",
599  "+++ %s Parent: %-24s [%s] Child: %-32s [%s] copy:%d",
600  e.tag().c_str(),
601  parent_nam.c_str(), parent.isValid() ? "VALID" : "INVALID",
602  child_nam.c_str(), child.isValid() ? "VALID" : "INVALID",
603  copy);
604  PlacedVolume pv;
605  if ( child.isValid() ) {
606  Transform3D trafo;
607  Converter<DDLTransform3D>(description,param,&trafo)(element);
608  pv = parent.placeVolume(child,copy,trafo);
609  }
610  if ( !pv.isValid() ) {
611  printout(ERROR,"MyDDCMS","+++ Placement FAILED! Parent:%s Child:%s Valid:%s",
612  parent.name(), child_nam.c_str(), yes_no(child.isValid()));
613  }
614 }
615 
616 template <typename TYPE>
617 static void convert_boolean(cms::DDParsingContext* context, xml_h element) {
618  cms::DDNamespace _ns(context);
619  xml_dim_t e(element);
620  string nam = e.nameStr();
621  Solid solids[2];
622  Solid boolean;
623  int cnt = 0;
624 
625  if ( e.hasChild(_CMU(rSolid)) ) { // Old version
626  for(xml_coll_t c(element, _CMU(rSolid)); cnt<2 && c; ++c, ++cnt)
627  solids[cnt] = _ns.solid(c.attr<string>(_U(name)));
628  }
629  else {
630  if ( (solids[0] = _ns.solid(e.attr<string>(_CMU(firstSolid)))).isValid() ) ++cnt;
631  if ( (solids[1] = _ns.solid(e.attr<string>(_CMU(secondSolid)))).isValid() ) ++cnt;
632  }
633  if ( cnt != 2 ) {
634  except("MyDDCMS","+++ Failed to create boolean solid %s. Found only %d parts.",nam.c_str(), cnt);
635  }
636  printout(_ns.context->debug_placements ? ALWAYS : DEBUG, "MyDDCMS",
637  "+++ BooleanSolid: %s Left: %-32s Right: %-32s",
638  nam.c_str(), solids[0]->GetName(), solids[1]->GetName());
639 
640  if ( solids[0].isValid() && solids[1].isValid() ) {
641  Transform3D trafo;
642  Converter<DDLTransform3D>(*context->description,context,&trafo)(element);
643  boolean = TYPE(solids[0],solids[1],trafo);
644  }
645  if ( !boolean.isValid() )
646  except("MyDDCMS","+++ FAILED to construct subtraction solid: %s",nam.c_str());
647  _ns.addSolid(nam,boolean);
648 }
649 
651 template <> void Converter<DDLUnionSolid>::operator()(xml_h element) const {
652  convert_boolean<UnionSolid>(_param<cms::DDParsingContext>(),element);
653 }
654 
656 template <> void Converter<DDLSubtractionSolid>::operator()(xml_h element) const {
657  convert_boolean<SubtractionSolid>(_param<cms::DDParsingContext>(),element);
658 }
659 
661 template <> void Converter<DDLIntersectionSolid>::operator()(xml_h element) const {
662  convert_boolean<IntersectionSolid>(_param<cms::DDParsingContext>(),element);
663 }
664 
666 template <> void Converter<DDLPolycone>::operator()(xml_h element) const {
667  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
668  xml_dim_t e(element);
669  string nam = e.nameStr();
670  double startPhi = _ns.attr<double>(e,_CMU(startPhi));
671  double deltaPhi = _ns.attr<double>(e,_CMU(deltaPhi));
672  vector<double> z, rmin, rmax, r;
673 
674  for(xml_coll_t rzpoint(element, _CMU(RZPoint)); rzpoint; ++rzpoint) {
675  z.emplace_back(_ns.attr<double>(rzpoint,_U(z)));
676  r.emplace_back(_ns.attr<double>(rzpoint,_U(r)));
677  }
678  if( z.empty()) {
679  for(xml_coll_t zplane(element, _CMU(ZSection)); zplane; ++zplane) {
680  rmin.emplace_back(_ns.attr<double>(zplane,_CMU(rMin)));
681  rmax.emplace_back(_ns.attr<double>(zplane,_CMU(rMax)));
682  z.emplace_back(_ns.attr<double>(zplane,_U(z)));
683  }
684  printout(_ns.context->debug_shapes ? ALWAYS : DEBUG, "MyDDCMS",
685  "+ Polycone: startPhi=%10.3f [rad] deltaPhi=%10.3f [rad] %4ld z-planes",
686  startPhi, deltaPhi, z.size());
687  _ns.addSolid(nam, Polycone(startPhi,deltaPhi,rmin,rmax,z));
688  }
689  else {
690  printout(_ns.context->debug_shapes ? ALWAYS : DEBUG, "MyDDCMS",
691  "+ Polycone: startPhi=%10.3f [rad] deltaPhi=%10.3f [rad] %4ld z-planes and %4ld radii",
692  startPhi, deltaPhi, z.size(), r.size());
693  _ns.addSolid(nam, Polycone(startPhi,deltaPhi,r,z));
694  }
695 }
696 
698 template <> void Converter<DDLExtrudedPolygon>::operator()(xml_h element) const {
699  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
700  xml_dim_t e(element);
701  string nam = e.nameStr();
702  vector<double> pt_x, pt_y, sec_x, sec_y, sec_z, sec_scale;
703 
704  for(xml_coll_t sec(element, _CMU(ZXYSection)); sec; ++sec) {
705  sec_z.emplace_back(_ns.attr<double>(sec,_U(z)));
706  sec_x.emplace_back(_ns.attr<double>(sec,_U(x)));
707  sec_y.emplace_back(_ns.attr<double>(sec,_U(y)));
708  sec_scale.emplace_back(_ns.attr<double>(sec,_CMU(scale),1.0));
709  }
710  for(xml_coll_t pt(element, _CMU(XYPoint)); pt; ++pt) {
711  pt_x.emplace_back(_ns.attr<double>(pt,_U(x)));
712  pt_y.emplace_back(_ns.attr<double>(pt,_U(y)));
713  }
714  printout(_ns.context->debug_shapes ? ALWAYS : DEBUG, "MyDDCMS",
715  "+ ExtrudedPolygon: %4ld points %4ld zxy sections",
716  pt_x.size(), sec_z.size());
717  _ns.addSolid(nam,ExtrudedPolygon(pt_x,pt_y,sec_z,sec_x,sec_y,sec_scale));
718 }
719 
721 template <> void Converter<DDLPolyhedra>::operator()(xml_h element) const {
722  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
723  xml_dim_t e(element);
724  string nam = e.nameStr();
725  double numSide = _ns.attr<int>(e,_CMU(numSide));
726  double startPhi = _ns.attr<double>(e,_CMU(startPhi));
727  double deltaPhi = _ns.attr<double>(e,_CMU(deltaPhi));
728  vector<double> z, rmin, rmax;
729 
730  for(xml_coll_t zplane(element, _CMU(RZPoint)); zplane; ++zplane) {
731  rmin.emplace_back(0.0);
732  rmax.emplace_back(_ns.attr<double>(zplane,_U(r)));
733  z.emplace_back(_ns.attr<double>(zplane,_U(z)));
734  }
735  for(xml_coll_t zplane(element, _CMU(ZSection)); zplane; ++zplane) {
736  rmin.emplace_back(_ns.attr<double>(zplane,_CMU(rMin)));
737  rmax.emplace_back(_ns.attr<double>(zplane,_CMU(rMax)));
738  z.emplace_back(_ns.attr<double>(zplane,_U(z)));
739  }
740  printout(_ns.context->debug_shapes ? ALWAYS : DEBUG, "MyDDCMS",
741  "+ Polyhedra:startPhi=%8.3f [rad] deltaPhi=%8.3f [rad] %4d sides %4ld z-planes",
742  startPhi, deltaPhi, numSide, z.size());
743  _ns.addSolid(nam, Polyhedra(numSide,startPhi,deltaPhi,z,rmin,rmax));
744 }
745 
747 template <> void Converter<DDLSphere>::operator()(xml_h element) const {
748  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
749  xml_dim_t e(element);
750  string nam = e.nameStr();
751  double rinner = _ns.attr<double>(e,_CMU(innerRadius));
752  double router = _ns.attr<double>(e,_CMU(outerRadius));
753  double startPhi = _ns.attr<double>(e,_CMU(startPhi));
754  double deltaPhi = _ns.attr<double>(e,_CMU(deltaPhi));
755  double startTheta = _ns.attr<double>(e,_CMU(startTheta));
756  double deltaTheta = _ns.attr<double>(e,_CMU(deltaTheta));
757  printout(_ns.context->debug_shapes ? ALWAYS : DEBUG, "MyDDCMS",
758  "+ Sphere: r_inner=%8.3f [cm] r_outer=%8.3f [cm]"
759  " startPhi=%8.3f [rad] deltaPhi=%8.3f startTheta=%8.3f delteTheta=%8.3f [rad]",
760  rinner, router, startPhi, deltaPhi, startTheta, deltaTheta);
761  _ns.addSolid(nam, Sphere(rinner, router, startTheta, deltaTheta, startPhi, deltaPhi));
762 }
763 
765 template <> void Converter<DDLTorus>::operator()(xml_h element) const {
766  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
767  xml_dim_t e(element);
768  string nam = e.nameStr();
769  double r = _ns.attr<double>(e,_CMU(torusRadius));
770  double rinner = _ns.attr<double>(e,_CMU(innerRadius));
771  double router = _ns.attr<double>(e,_CMU(outerRadius));
772  double startPhi = _ns.attr<double>(e,_CMU(startPhi));
773  double deltaPhi = _ns.attr<double>(e,_CMU(deltaPhi));
774  printout(_ns.context->debug_shapes ? ALWAYS : DEBUG, "MyDDCMS",
775  "+ Torus: r=%10.3f [cm] r_inner=%10.3f [cm] r_outer=%10.3f [cm]"
776  " startPhi=%10.3f [rad] deltaPhi=%10.3f [rad]",
777  r, rinner, router, startPhi, deltaPhi);
778  _ns.addSolid(nam, Torus(r, rinner, router, startPhi, deltaPhi));
779 }
780 
782 template <> void Converter<DDLPseudoTrap>::operator()(xml_h element) const {
783  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
784  xml_dim_t e(element);
785  string nam = e.nameStr();
786  double dx1 = _ns.attr<double>(e,_CMU(dx1));
787  double dy1 = _ns.attr<double>(e,_CMU(dy1));
788  double dx2 = _ns.attr<double>(e,_CMU(dx2));
789  double dy2 = _ns.attr<double>(e,_CMU(dy2));
790  double dz = _ns.attr<double>(e,_U(dz));
791  double r = _ns.attr<double>(e,_U(radius));
792  bool atMinusZ = _ns.attr<bool> (e,_CMU(atMinusZ));
793  printout(_ns.context->debug_shapes ? ALWAYS : DEBUG, "MyDDCMS",
794  "+ Pseudotrap: dz=%8.3f [cm] dx1:%.3f dy1:%.3f dx2=%.3f dy2=%.3f radius:%.3f atMinusZ:%s",
795  dz, dx1, dy1, dx2, dy2, r, yes_no(atMinusZ));
796  _ns.addSolid(nam, PseudoTrap(dx1, dx2, dy1, dy2, dz, r, atMinusZ));
797 }
798 
800 template <> void Converter<DDLTrapezoid>::operator()(xml_h element) const {
801  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
802  xml_dim_t e(element);
803  string nam = e.nameStr();
804  double dz = _ns.attr<double>(e,_U(dz));
805  double alp1 = _ns.attr<double>(e,_CMU(alp1));
806  double bl1 = _ns.attr<double>(e,_CMU(bl1));
807  double tl1 = _ns.attr<double>(e,_CMU(tl1));
808  double h1 = _ns.attr<double>(e,_CMU(h1));
809  double alp2 = _ns.attr<double>(e,_CMU(alp2));
810  double bl2 = _ns.attr<double>(e,_CMU(bl2));
811  double tl2 = _ns.attr<double>(e,_CMU(tl2));
812  double h2 = _ns.attr<double>(e,_CMU(h2));
813  double phi = _ns.attr<double>(e,_U(phi),0.0);
814  double theta = _ns.attr<double>(e,_U(theta),0.0);
815 
816  printout(_ns.context->debug_shapes ? ALWAYS : DEBUG, "MyDDCMS",
817  "+ Trapezoid: dz=%10.3f [cm] alp1:%.3f bl1=%.3f tl1=%.3f alp2=%.3f bl2=%.3f tl2=%.3f h2=%.3f phi=%.3f theta=%.3f",
818  dz, alp1, bl1, tl1, h1, alp2, bl2, tl2, h2, phi, theta);
819  _ns.addSolid( nam, Trap( dz, theta, phi, h1, bl1, tl1, alp1, h2, bl2, tl2, alp2 ));
820 }
821 
823 template <> void Converter<DDLTrd1>::operator()(xml_h element) const {
824  cms::DDNamespace _ns( _param<cms::DDParsingContext>());
825  xml_dim_t e( element );
826  string nam = e.nameStr();
827  double dx1 = _ns.attr<double>( e, _CMU( dx1 ));
828  double dy1 = _ns.attr<double>( e, _CMU( dy1 ));
829  double dx2 = _ns.attr<double>( e, _CMU( dx2 ), 0.0 );
830  double dy2 = _ns.attr<double>( e, _CMU( dy2 ), dy1 );
831  double dz = _ns.attr<double>( e, _CMU( dz ));
832  printout(_ns.context->debug_shapes ? ALWAYS : DEBUG, "MyDDCMS",
833  "+ Trd1: dz=%8.3f [cm] dx1:%.3f dy1:%.3f dx2:%.3f dy2:%.3f",
834  dz, dx1, dy1, dx2, dy2);
835  _ns.addSolid( nam, Trapezoid( dx1, dx2, dy1, dy2, dz ));
836 }
837 
839 template <> void Converter<DDLTubs>::operator()(xml_h element) const {
840  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
841  xml_dim_t e(element);
842  string nam = e.nameStr();
843  double dz = _ns.attr<double>(e,_CMU(dz));
844  double rmin = _ns.attr<double>(e,_CMU(rMin));
845  double rmax = _ns.attr<double>(e,_CMU(rMax));
846  double startPhi = _ns.attr<double>(e,_CMU(startPhi),0.0);
847  double deltaPhi = _ns.attr<double>(e,_CMU(deltaPhi),2*M_PI);
848  printout(_ns.context->debug_shapes ? ALWAYS : DEBUG, "MyDDCMS",
849  "+ Tubs: dz=%8.3f [cm] rmin=%8.3f [cm] rmax=%8.3f [cm]"
850  " startPhi=%8.3f [rad] deltaPhi=%8.3f [rad]", dz, rmin, rmax, startPhi, deltaPhi);
851  _ns.addSolid(nam, Tube(rmin,rmax,dz,startPhi,deltaPhi));
852 }
853 
855 template <> void Converter<DDLCutTubs>::operator()(xml_h element) const {
856  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
857  xml_dim_t e(element);
858  string nam = e.nameStr();
859  double dz = _ns.attr<double>(e,_CMU(dz));
860  double rmin = _ns.attr<double>(e,_CMU(rMin));
861  double rmax = _ns.attr<double>(e,_CMU(rMax));
862  double startPhi = _ns.attr<double>(e,_CMU(startPhi));
863  double deltaPhi = _ns.attr<double>(e,_CMU(deltaPhi));
864  double lx = _ns.attr<double>(e,_CMU(lx));
865  double ly = _ns.attr<double>(e,_CMU(ly));
866  double lz = _ns.attr<double>(e,_CMU(lz));
867  double tx = _ns.attr<double>(e,_CMU(tx));
868  double ty = _ns.attr<double>(e,_CMU(ty));
869  double tz = _ns.attr<double>(e,_CMU(tz));
870  printout(_ns.context->debug_shapes ? ALWAYS : DEBUG, "MyDDCMS",
871  "+ CutTube: dz=%8.3f [cm] rmin=%8.3f [cm] rmax=%8.3f [cm]"
872  " startPhi=%8.3f [rad] deltaPhi=%8.3f [rad]...",
873  dz, rmin, rmax, startPhi, deltaPhi);
874  _ns.addSolid(nam, CutTube(rmin,rmax,dz,startPhi,deltaPhi,lx,ly,lz,tx,ty,tz));
875 }
876 
878 template <> void Converter<DDLTruncTubs>::operator()(xml_h element) const {
879  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
880  xml_dim_t e(element);
881  string nam = e.nameStr();
882  double zhalf = _ns.attr<double>(e,_CMU(zHalf));
883  double rmin = _ns.attr<double>(e,_CMU(rMin));
884  double rmax = _ns.attr<double>(e,_CMU(rMax));
885  double startPhi = _ns.attr<double>(e,_CMU(startPhi));
886  double deltaPhi = _ns.attr<double>(e,_CMU(deltaPhi));
887  double cutAtStart = _ns.attr<double>(e,_CMU(cutAtStart));
888  double cutAtDelta = _ns.attr<double>(e,_CMU(cutAtDelta));
889  bool cutInside = _ns.attr<bool>(e,_CMU(cutInside));
890  printout(_ns.context->debug_shapes ? ALWAYS : DEBUG, "MyDDCMS",
891  "+ TruncTube:zHalf=%8.3f [cm] rmin=%8.3f [cm] rmax=%8.3f [cm]"
892  " startPhi=%8.3f [rad] deltaPhi=%8.3f [rad] atStart=%8.3f [cm] atDelta=%8.3f [cm] inside:%s",
893  zhalf, rmin, rmax, startPhi, deltaPhi, cutAtStart, cutAtDelta, yes_no(cutInside));
894  _ns.addSolid(nam, TruncatedTube(zhalf,rmin,rmax,startPhi,deltaPhi,cutAtStart,cutAtDelta,cutInside));
895 }
896 
898 template <> void Converter<DDLEllipticalTube>::operator()(xml_h element) const {
899  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
900  xml_dim_t e(element);
901  string nam = e.nameStr();
902  double dx = _ns.attr<double>(e,_CMU(xSemiAxis));
903  double dy = _ns.attr<double>(e,_CMU(ySemiAxis));
904  double dz = _ns.attr<double>(e,_CMU(zHeight));
905  printout(_ns.context->debug_shapes ? ALWAYS : DEBUG, "MyDDCMS",
906  "+ EllipticalTube xSemiAxis=%8.3f [cm] ySemiAxis=%8.3f [cm] zHeight=%8.3f [cm]",dx,dy,dz);
907  _ns.addSolid(nam, EllipticalTube(dx,dy,dz));
908 }
909 
911 template <> void Converter<DDLCone>::operator()(xml_h element) const {
912  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
913  xml_dim_t e(element);
914  string nam = e.nameStr();
915  double dz = _ns.attr<double>(e,_CMU(dz));
916  double rmin1 = _ns.attr<double>(e,_CMU(rMin1));
917  double rmin2 = _ns.attr<double>(e,_CMU(rMin2));
918  double rmax1 = _ns.attr<double>(e,_CMU(rMax1));
919  double rmax2 = _ns.attr<double>(e,_CMU(rMax2));
920  double startPhi = _ns.attr<double>(e,_CMU(startPhi));
921  double deltaPhi = _ns.attr<double>(e,_CMU(deltaPhi));
922  double phi2 = startPhi + deltaPhi;
923  printout(_ns.context->debug_shapes ? ALWAYS : DEBUG, "MyDDCMS",
924  "+ Cone: dz=%8.3f [cm]"
925  " rmin1=%8.3f [cm] rmax1=%8.3f [cm]"
926  " rmin2=%8.3f [cm] rmax2=%8.3f [cm]"
927  " startPhi=%8.3f [rad] deltaPhi=%8.3f [rad]",
928  dz, rmin1, rmax1, rmin2, rmax2, startPhi, deltaPhi);
929  _ns.addSolid(nam, ConeSegment(dz,rmin1,rmax1,rmin2,rmax2,startPhi,phi2));
930 }
931 
933 template <> void Converter<DDLShapeless>::operator()(xml_h element) const {
934  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
935  xml_dim_t e(element);
936  string nam = e.nameStr();
937  printout(_ns.context->debug_shapes ? ALWAYS : DEBUG, "MyDDCMS",
938  "+ Shapeless: THIS ONE CAN ONLY BE USED AT THE VOLUME LEVEL -> Assembly%s", nam.c_str());
939  _ns.addSolid(nam, Box(1,1,1));
940 }
941 
943 template <> void Converter<DDLBox>::operator()(xml_h element) const {
944  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
945  xml_dim_t e(element);
946  string nam = e.nameStr();
947  double dx = _ns.attr<double>(e,_CMU(dx));
948  double dy = _ns.attr<double>(e,_CMU(dy));
949  double dz = _ns.attr<double>(e,_CMU(dz));
950  printout(_ns.context->debug_shapes ? ALWAYS : DEBUG, "MyDDCMS",
951  "+ Box: dx=%10.3f [cm] dy=%10.3f [cm] dz=%10.3f [cm]", dx, dy, dz);
952  _ns.addSolid(nam, Box(dx,dy,dz));
953 }
954 
956 template <> void Converter<include_load>::operator()(xml_h element) const {
957  string fname = element.attr<string>(_U(ref));
958  edm::FileInPath fp( fname );
959  xml::Document doc;
960  doc = xml::DocumentHandler().load( fp.fullPath());
961  printout(_param<cms::DDParsingContext>()->debug_includes ? ALWAYS : DEBUG,
962  "MyDDCMS","+++ Processing the CMS detector description %s", fname.c_str());
963  _option<DDRegistry>()->includes.emplace_back( doc );
964 }
965 
967 template <> void Converter<include_unload>::operator()(xml_h element) const {
968  string fname = xml::DocumentHandler::system_path(element);
969  xml::DocumentHolder(xml_elt_t(element).document()).assign(nullptr);
970  printout(_param<cms::DDParsingContext>()->debug_includes ? ALWAYS : DEBUG,
971  "MyDDCMS","+++ Finished processing %s",fname.c_str());
972 }
973 
975 template <> void Converter<include_constants>::operator()(xml_h element) const {
976  xml_coll_t(element, _CMU(ConstantsSection)).for_each(Converter<ConstantsSection>(description,param,optional));
977 }
978 
980 template <> void Converter<DDLAlgorithm>::operator()(xml_h element) const {
981  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
982  xml_dim_t e(element);
983  string name = e.nameStr();
984  if ( _ns.context->disabledAlgs.find(name) != _ns.context->disabledAlgs.end() ) {
985  printout(INFO,"MyDDCMS","+++ Skip disabled algorithms: %s",name.c_str());
986  return;
987  }
988  try {
989  size_t idx;
991  string type = "DDCMS_"+_ns.realName(name);
992  while(( idx = type.find( NAMESPACE_SEP )) != string::npos ) type[idx] = '_';
993 
994  // SensitiveDetector and Segmentation currently are undefined. Let's keep it like this
995  // until we found something better.....
996  printout(_ns.context->debug_algorithms ? ALWAYS : DEBUG,
997  "MyDDCMS","+++ Start executing algorithm %s....",type.c_str());
998 
999  long ret = PluginService::Create<long>(type, &description, _ns.context, &element, &sd);
1000  if ( ret == 1 ) {
1001  printout(_ns.context->debug_algorithms ? ALWAYS : DEBUG,
1002  "MyDDCMS", "+++ Executed algorithm: %08lX = %s", ret, name.c_str());
1003  return;
1004  }
1005 #if 0
1006  Segmentation seg;
1007  DetElement det(PluginService::Create<NamedObject*>(type, &description, _ns.context, &element, &sd));
1008  if (det.isValid()) {
1009  // setChildTitles(make_pair(name, det));
1010  if ( sd.isValid() ) {
1011  det->flag |= DetElement::Object::HAVE_SENSITIVE_DETECTOR;
1012  }
1013  if ( seg.isValid() ) {
1014  seg->sensitive = sd;
1015  seg->detector = det;
1016  }
1017  }
1018  if (!det.isValid()) {
1019  PluginDebug dbg;
1020  PluginService::Create<NamedObject*>(type, &description, _ns.context, &element, &sd);
1021  except("MyDDCMS","Failed to execute subdetector creation plugin. " + dbg.missingFactory(type));
1022  }
1023  description.addDetector(det);
1024 #endif
1025  printout(ERROR, "MyDDCMS", "++ FAILED NOT ADDING SUBDETECTOR %08lX = %s",ret, name.c_str());
1027  return;
1028  }
1029  catch (const exception& exc) {
1030  printout(ERROR, "MyDDCMS", "++ FAILED to convert subdetector: %s: %s", name.c_str(), exc.what());
1031  terminate();
1032  }
1033  catch (...) {
1034  printout(ERROR, "MyDDCMS", "++ FAILED to convert subdetector: %s: %s", name.c_str(), "UNKNONW Exception");
1035  terminate();
1036  }
1037 }
1038 
1039 template <class InputIt, class ForwardIt, class BinOp>
1040 void for_each_token( InputIt first, InputIt last,
1041  ForwardIt s_first, ForwardIt s_last,
1042  BinOp binary_op)
1043 {
1044  while( first != last ) {
1045  const auto pos = std::find_first_of( first, last, s_first, s_last );
1046  binary_op( first, pos );
1047  if( pos == last ) break;
1048  first = std::next( pos );
1049  }
1050 }
1051 
1052 vector<double>
1053 splitNumeric( const string& str, const string& delims = "," )
1054 {
1055  vector<double> output;
1056 
1057  for_each_token( cbegin( str ), cend( str ),
1058  cbegin( delims ), cend( delims ),
1059  [&output] ( auto first, auto second ) {
1060  if( first != second ) {
1061  output.emplace_back(stod(string( first, second )));
1062  }
1063  });
1064  return output;
1065 }
1066 
1067 vector<string>
1068 splitString( const string& str, const string& delims = "," )
1069 {
1070  vector<string> output;
1071 
1072  for_each_token( cbegin( str ), cend( str ),
1073  cbegin( delims ), cend( delims ),
1074  [&output] ( auto first, auto second ) {
1075  if( first != second ) {
1076  output.emplace_back( first, second );
1077  }
1078  });
1079  return output;
1080 }
1081 
1084 template <> void Converter<DDLVector>::operator()( xml_h element ) const {
1086 
1087  cms::DDNamespace _ns( _param<cms::DDParsingContext>());
1088  xml_dim_t e( element );
1089  string name = e.nameStr();
1090  string type = _ns.attr<string>(e,_U(type));
1091  string nEntries = _ns.attr<string>(e,_CMU(nEntries));
1092  string val = e.text();
1093  val.erase( remove_if( val.begin(), val.end(), [](unsigned char x){return isspace(x);}), val.end());
1094 
1095  printout( _ns.context->debug_constants ? ALWAYS : DEBUG,
1096  "MyDDCMS","+++ Vector<%s>: %s[%s]: %s", type.c_str(), name.c_str(),
1097  nEntries.c_str(), val.c_str());
1098 
1099  vector<double> results = splitNumeric( val );
1100  registry->insert( { name, results } );
1101 
1102  for( auto it : results )
1103  cout << it << " ";
1104  cout << "\n";
1105 }
1106 
1107 template <> void Converter<debug>::operator()(xml_h dbg) const {
1108  cms::DDNamespace _ns(_param<cms::DDParsingContext>());
1109  if ( dbg.hasChild(_CMU(debug_visattr)) ) _ns.context->debug_visattr = true;
1110  if ( dbg.hasChild(_CMU(debug_constants)) ) _ns.context->debug_constants = true;
1111  if ( dbg.hasChild(_CMU(debug_materials)) ) _ns.context->debug_materials = true;
1112  if ( dbg.hasChild(_CMU(debug_rotations)) ) _ns.context->debug_rotations = true;
1113  if ( dbg.hasChild(_CMU(debug_shapes)) ) _ns.context->debug_shapes = true;
1114  if ( dbg.hasChild(_CMU(debug_volumes)) ) _ns.context->debug_volumes = true;
1115  if ( dbg.hasChild(_CMU(debug_placements)) ) _ns.context->debug_placements = true;
1116  if ( dbg.hasChild(_CMU(debug_namespaces)) ) _ns.context->debug_namespaces = true;
1117  if ( dbg.hasChild(_CMU(debug_includes)) ) _ns.context->debug_includes = true;
1118  if ( dbg.hasChild(_CMU(debug_algorithms)) ) _ns.context->debug_algorithms = true;
1119 }
1120 
1121 template <> void Converter<DDRegistry>::operator()(xml_h /* element */) const {
1122  cms::DDParsingContext* context = _param<cms::DDParsingContext>();
1123  DDRegistry* res = _option<DDRegistry>();
1124  cms::DDNamespace _ns(context);
1125  int count = 0;
1126 
1127  printout(context->debug_constants ? ALWAYS : DEBUG,
1128  "MyDDCMS","+++ RESOLVING %ld unknown constants.....",res->unresolvedConst.size());
1129  while ( !res->unresolvedConst.empty() ) {
1130  for(auto i=res->unresolvedConst.begin(); i!=res->unresolvedConst.end(); ++i ) {
1131  const string& n = (*i).first;
1132  string rep;
1133  string& v = (*i).second;
1134  size_t idx, idq;
1135  for(idx=v.find('[',0); idx != string::npos; idx = v.find('[',idx+1) ) {
1136  idq = v.find(']',idx+1);
1137  rep = v.substr(idx+1,idq-idx-1);
1138  auto r = res->allConst.find(rep);
1139  if ( r != res->allConst.end() ) {
1140  rep = "("+(*r).second+")";
1141  v.replace(idx,idq-idx+1,rep);
1142  }
1143  }
1144  if ( v.find(']') == string::npos ) {
1145  if ( v.find("-+") != string::npos || v.find("+-") != string::npos ) {
1146  while ( (idx=v.find("-+")) != string::npos )
1147  v.replace(idx,2,"-");
1148  while ( (idx=v.find("+-")) != string::npos )
1149  v.replace(idx,2,"-");
1150  }
1151  printout(context->debug_constants ? ALWAYS : DEBUG,
1152  "MyDDCMS","+++ [%06ld] ---------- %-40s = %s",
1153  res->unresolvedConst.size()-1,n.c_str(),res->originalConst[n].c_str());
1154  _ns.addConstantNS(n, v, "number");
1155  res->unresolvedConst.erase(i);
1156  break;
1157  }
1158  }
1159  if ( ++count > 1000 ) break;
1160  }
1161  if ( !res->unresolvedConst.empty() ) {
1162  for(const auto& e : res->unresolvedConst )
1163  printout(ERROR,"MyDDCMS","+++ Unresolved constant: %-40s = %s.",e.first.c_str(), e.second.c_str());
1164  except("MyDDCMS","++ FAILED to resolve %ld constant entries:",res->unresolvedConst.size());
1165  }
1166  res->unresolvedConst.clear();
1167  res->originalConst.clear();
1168  res->allConst.clear();
1169 }
1170 
1171 template <> void Converter<print_xml_doc>::operator()(xml_h element) const {
1172  string fname = xml::DocumentHandler::system_path(element);
1173  printout(_param<cms::DDParsingContext>()->debug_includes ? ALWAYS : DEBUG,
1174  "MyDDCMS","+++ Processing data from: %s",fname.c_str());
1175 }
1176 
1178 static long load_dddefinition(Detector& det, xml_h element) {
1179  static cms::DDParsingContext context(&det);
1180  cms::DDNamespace _ns(context);
1181  xml_elt_t dddef(element);
1182  string fname = xml::DocumentHandler::system_path(element);
1183  bool open_geometry = dddef.hasChild(_CMU(open_geometry));
1184  bool close_geometry = dddef.hasChild(_CMU(close_geometry));
1185 
1186  xml_coll_t(dddef, _U(debug)).for_each(Converter<debug>(det,&context));
1187 
1188  // Here we define the order how XML elements are processed.
1189  // Be aware of dependencies. This can only defined once.
1190  // At the end it is a limitation of DOM....
1191  printout(INFO,"MyDDCMS","+++ Processing the CMS detector description %s",fname.c_str());
1192 
1193  xml::Document doc;
1194  Converter<print_xml_doc> print_doc(det,&context);
1195  try {
1196  DDRegistry res;
1197  print_doc((doc=dddef.document()).root());
1198  xml_coll_t(dddef, _CMU(DisabledAlgo)).for_each(Converter<disabled_algo>(det,&context,&res));
1199  xml_coll_t(dddef, _CMU(ConstantsSection)).for_each(Converter<ConstantsSection>(det,&context,&res));
1200  xml_coll_t(dddef, _CMU(VisSection)).for_each(Converter<vissection>(det,&context));
1201  xml_coll_t(dddef, _CMU(RotationSection)).for_each(Converter<RotationSection>(det,&context));
1202  xml_coll_t(dddef, _CMU(MaterialSection)).for_each(Converter<MaterialSection>(det,&context));
1203 
1204  xml_coll_t(dddef, _CMU(IncludeSection)).for_each(_CMU(Include), Converter<include_load>(det,&context,&res));
1205 
1206  for(xml::Document d : res.includes ) {
1207  print_doc((doc=d).root());
1208  Converter<include_constants>(det,&context,&res)((doc=d).root());
1209  }
1210  // Before we continue, we have to resolve all constants NOW!
1211  Converter<DDRegistry>(det,&context,&res)(dddef);
1212  // Now we can process the include files one by one.....
1213  for(xml::Document d : res.includes ) {
1214  print_doc((doc=d).root());
1215  xml_coll_t(d.root(),_CMU(MaterialSection)).for_each(Converter<MaterialSection>(det,&context));
1216  }
1217  if ( open_geometry ) {
1218  context.geo_inited = true;
1219  det.init();
1220  _ns.addVolume(det.worldVolume());
1221  }
1222  for(xml::Document d : res.includes ) {
1223  print_doc((doc=d).root());
1224  xml_coll_t(d.root(),_CMU(RotationSection)).for_each(Converter<RotationSection>(det,&context));
1225  }
1226  for(xml::Document d : res.includes ) {
1227  print_doc((doc=d).root());
1228  xml_coll_t(d.root(), _CMU(SolidSection)).for_each(Converter<SolidSection>(det,&context));
1229  }
1230  for(xml::Document d : res.includes ) {
1231  print_doc((doc=d).root());
1232  xml_coll_t(d.root(), _CMU(LogicalPartSection)).for_each(Converter<LogicalPartSection>(det,&context));
1233  }
1234  for(xml::Document d : res.includes ) {
1235  print_doc((doc=d).root());
1236  xml_coll_t(d.root(), _CMU(Algorithm)).for_each(Converter<DDLAlgorithm>(det,&context));
1237  }
1238  for(xml::Document d : res.includes ) {
1239  print_doc((doc=d).root());
1240  xml_coll_t(d.root(), _CMU(PosPartSection)).for_each(Converter<PosPartSection>(det,&context));
1241  }
1242 
1244  for(xml::Document d : res.includes ) Converter<include_unload>(det,&context,&res)(d.root());
1245 
1246  print_doc((doc=dddef.document()).root());
1247  // Now process the actual geometry items
1248  xml_coll_t(dddef, _CMU(SolidSection)).for_each(Converter<SolidSection>(det,&context));
1249  xml_coll_t(dddef, _CMU(LogicalPartSection)).for_each(Converter<LogicalPartSection>(det,&context));
1250  xml_coll_t(dddef, _CMU(Algorithm)).for_each(Converter<DDLAlgorithm>(det,&context));
1251  xml_coll_t(dddef, _CMU(PosPartSection)).for_each(Converter<PosPartSection>(det,&context));
1252  }
1253  catch(const exception& e) {
1254  printout(ERROR,"MyDDCMS","Exception while processing xml source:%s",doc.uri().c_str());
1255  printout(ERROR,"MyDDCMS","----> %s", e.what());
1256  throw;
1257  }
1258 
1260  if ( close_geometry ) {
1261  det.endDocument();
1262  }
1263  printout(INFO,"DDDefinition","+++ Finished processing %s",fname.c_str());
1264  return 1;
1265 }
1266 
1267 // Now declare the factory entry for the plugin mechanism
1268 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:212
dd4hep::Solid solid(const std::string &name) const
Definition: DDNamespace.cc:263
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:91
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:7
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:1190
#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:349
#define debug
Definition: HDRShower.cc:19
dd4hep::Solid addSolid(const std::string &name, dd4hep::Solid solid) const
Definition: DDNamespace.cc:257
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
#define NAMESPACE_SEP
Definition: DDNamespace.h:75
T angle(T x1, T y1, T z1, T x2, T y2, T z2)
Definition: angle.h:11