DD4hep  1.31.0
Detector Description Toolkit for High Energy Physics
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
TGeoCodeGenerator.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : M.Frank
11 //
12 //==========================================================================
13 #include <DD4hep/Factories.h>
14 #include <DD4hep/Shapes.h>
15 #include <DD4hep/Volumes.h>
16 #include <DD4hep/Detector.h>
17 #include <DD4hep/MatrixHelpers.h>
18 #include <DD4hep/DD4hepUnits.h>
19 #include <DD4hep/Printout.h>
20 #include <DD4hep/Path.h>
21 
22 // C/C++ include files
23 #include <iostream>
24 #include <fstream>
25 
26 // ROOT includes
27 #include <TClass.h>
28 #include <TGeoMatrix.h>
29 #include <TGeoBoolNode.h>
30 #include <TGeoCompositeShape.h>
31 
32 using namespace dd4hep;
33 
34 namespace {
35  std::string prefix = "\t";
36 
38 
44  struct Actor {
45  std::set<const TGeoNode*> nodes;
46  std::set<const TGeoVolume*> volumes;
47  std::set<const TGeoShape*> solids;
48  std::set<const TGeoMatrix*> matrices;
49  std::set<const TGeoMedium*> materials;
50  std::set<const TGeoElement*> elements;
51  std::string function {"run_geometry"};
52  bool dump_vis = false;
53  bool dump_mat = false;
54 
55  Actor() = default;
56  ~Actor() = default;
57  std::ostream& handleHeader (std::ostream& log);
58  std::ostream& handleTrailer (std::ostream& log);
59  std::ostream& handleSolid (std::ostream& log, const TGeoShape* sh);
60  std::ostream& handleMatrix (std::ostream& log, TGeoMatrix* mat);
61  std::ostream& handleElement (std::ostream& log, TGeoElement* elt);
62  std::ostream& handleMaterial (std::ostream& log, TGeoMedium* mat);
63  std::ostream& handlePlacement(std::ostream& log, TGeoNode* parent, TGeoNode* node);
64  };
65  typedef void* pvoid_t;
66 
67  std::ostream& newline(std::ostream& log) {
68  return log << std::endl << prefix;
69  }
70 
71  std::ostream& Actor::handleHeader (std::ostream& log) {
72  log << "#include \"TClass.h\"" << std::endl
73  << "#include \"TGeoNode.h\"" << std::endl
74  << "#include \"TGeoExtension.h\"" << std::endl
75  << "#include \"TGeoShapeAssembly.h\"" << std::endl
76  << "#include \"TGeoMedium.h\"" << std::endl
77  << "#include \"TGeoVolume.h\"" << std::endl
78  << "#include \"TGeoShape.h\"" << std::endl
79  << "#include \"TGeoPhysicalNode.h\"" << std::endl
80  << "#include \"TGeoCone.h\"" << std::endl
81  << "#include \"TGeoParaboloid.h\"" << std::endl
82  << "#include \"TGeoPgon.h\"" << std::endl
83  << "#include \"TGeoPcon.h\"" << std::endl
84  << "#include \"TGeoSphere.h\"" << std::endl
85  << "#include \"TGeoArb8.h\"" << std::endl
86  << "#include \"TGeoTrd1.h\"" << std::endl
87  << "#include \"TGeoTrd2.h\"" << std::endl
88  << "#include \"TGeoTube.h\"" << std::endl
89  << "#include \"TGeoEltu.h\"" << std::endl
90  << "#include \"TGeoXtru.h\"" << std::endl
91  << "#include \"TGeoHype.h\"" << std::endl
92  << "#include \"TGeoTorus.h\"" << std::endl
93  << "#include \"TGeoHalfSpace.h\"" << std::endl
94  << "#include \"TGeoCompositeShape.h\"" << std::endl
95  << "#include \"TGeoShapeAssembly.h\"" << std::endl
96  << "#include \"TGeoMatrix.h\"" << std::endl
97  << "#include \"TGeoBoolNode.h\"" << std::endl
98  << "#include \"TGeoCompositeShape.h\"" << std::endl
99  << "#include \"TGeoManager.h\"" << std::endl
100  << "#include <vector>" << std::endl
101  << "#include <map>" << std::endl
102  << "#include <set>" << std::endl << std::endl << std::endl;
103  log << "TGeoVolume* generate_geometry() {" << newline;
104  return log;
105  }
106 
107  std::ostream& Actor::handleTrailer (std::ostream& log) {
108  log << std::endl << "}" << std::endl << std::endl;
109  log << "void " << function << "() {" << newline
110  << "if ( !gGeoManager ) gGeoManager = new TGeoManager();" << newline
111  << "TGeoVolume* vol_top = generate_geometry();" << newline
112  << "gGeoManager->SetTopVolume(vol_top);" << newline
113  << "vol_top->Draw(\"ogl\");" << newline
114  << std::endl << "}" << std::endl;
115  return log;
116  }
117 
118  std::ostream& Actor::handlePlacement(std::ostream& log, TGeoNode* parent, TGeoNode* node) {
119  if ( node && nodes.find(node) == nodes.end() ) {
120  TGeoVolume* vol = node->GetVolume();
121  TGeoMatrix* mat = node->GetMatrix();
122  nodes.insert(node);
123  handleMatrix(log, mat);
124 
125  if ( vol && volumes.find(vol) == volumes.end() ) {
126  volumes.insert(vol);
127  if ( vol->IsA() == TGeoVolumeAssembly::Class() ) {
128  log << "TGeoVolume* vol_" << pvoid_t(vol)
129  << " = new TGeoVolumeAssembly(\"" << vol->GetName() << "\");" << newline;
130  }
131  else {
132  TGeoMedium* med = vol->GetMedium();
133  TGeoShape* sh = vol->GetShape();
134  handleSolid(log, sh);
135  handleMaterial(log, med);
136  log << "TGeoVolume* vol_" << pvoid_t(vol) << " = new TGeoVolume(\"" << vol->GetName() << "\", "
137  << "shape_" << pvoid_t(sh) << ", material_" << pvoid_t(med) << ");" << newline;
138  if ( dump_vis ) {
139  log << "vol_" << pvoid_t(vol) << "->SetLineColor(Color_t(" << int(vol->GetLineColor()) << "));" << newline;
140  log << "vol_" << pvoid_t(vol) << "->SetLineStyle(" << vol->GetLineStyle() << ");" << newline;
141  log << "vol_" << pvoid_t(vol) << "->SetLineWidth(" << vol->GetLineWidth() << ");" << newline;
142  log << "vol_" << pvoid_t(vol) << "->SetFillColor(Color_t(" << int(vol->GetFillColor()) << "));" << newline;
143  log << "vol_" << pvoid_t(vol) << "->SetFillStyle(" << vol->GetFillStyle() << ");" << newline;
144  }
145  }
146  }
147 
148  for (Int_t idau = 0, ndau = node->GetNdaughters(); idau < ndau; ++idau) {
149  TGeoNode* daughter = node->GetDaughter(idau);
150  handlePlacement(log, node, daughter);
151  }
152 
153  if ( parent ) {
154  Int_t ndau = parent->GetNdaughters();
155  TGeoVolume* pvol = parent->GetVolume();
156  log << "vol_" << pvoid_t(pvol)
157  << "->AddNode(vol_" << pvoid_t(vol) << ", " << ndau << ", matrix_" << pvoid_t(mat)
158  << ");" << newline;
159  log << "TGeoNode* node_" << pvoid_t(node) << " = vol_" << pvoid_t(pvol)
160  << "->GetNode(" << ndau << ");" << newline;
161  }
162  else {
163  log << "return vol_" << pvoid_t(vol) << ";" << std::endl;
164  }
165  }
166  return log;
167  }
168 
169  std::ostream& Actor::handleElement (std::ostream& log, TGeoElement* elt) {
170  if ( elt && elements.find(elt) == elements.end() ) {
171  elements.insert(elt);
172  log << "TGeoElement* elt_" << pvoid_t(elt) << " = new TGeoElement(\""
173  << elt->GetName() << "\", \"" << elt->GetTitle() << "\", ";
174  if ( elt->GetNisotopes() > 0 ) {
175  log << elt->GetNisotopes() << ");" << newline;
176  for(Int_t i=0; i<elt->GetNisotopes(); ++i) {
177  TGeoIsotope* iso = elt->GetIsotope(i);
178  log << "elt_" << pvoid_t(elt) << "->AddIsotope("
179  << "new TGeoIsotope(\"" << elt->GetName() << "_" << iso->GetN()
180  << "\", " << iso->GetZ() << ", " << iso->GetN() << ", " << iso->GetA() << "));"
181  << newline;
182  }
183  }
184  else {
185  log << elt->Z() << ", " << elt->N() << ", " << elt->A() << ");" << newline;
186  }
187  }
188  return log;
189  }
190 
191  std::ostream& Actor::handleMaterial(std::ostream& log, TGeoMedium* medium) {
192  if ( medium && materials.find(medium) == materials.end() ) {
193  materials.insert(medium);
194  if ( !dump_mat ) {
195  log << "TGeoMedium* material_" << pvoid_t(medium) << " = gGeoManager->GetMedium(\"IRON\");"
196  << newline;
197  return log;
198  }
199  TGeoMaterial* mat = medium->GetMaterial();
200  if ( mat->IsMixture() ) {
201  TGeoMixture* mix = (TGeoMixture*) mat;
202  int nElements = mix->GetNelements();
203  double W_total = 0.0;
204  for (int i = 0; i < nElements; ++i) {
205  handleElement(log, mix->GetElement(i));
206  W_total += (mix->GetWmixt())[i];
207  }
208  log << "TGeoMixture* mat_" << pvoid_t(mat) << " = new TGeoMixture(\""
209  << mix->GetName() << "\", " << nElements << ", " << mix->GetDensity() << ");"
210  << newline;
211  for (int i = 0; i < nElements; ++i) {
212  TGeoElement* e = mix->GetElement(i);
213  log << "mat_" << pvoid_t(mat) << "->AddElement(elt_" << pvoid_t(e)
214  << ", " << ((mix->GetWmixt())[i]/W_total) << ");"
215  << newline;
216  }
217  mix->SetRadLen(0e0);
218  mix->ComputeDerivedQuantities();
219  }
220  else {
221  double z = mat->GetZ(), a = mat->GetA();
222  if ( z < 1.0000001 ) z = 1.0;
223  if ( a < 0.5000001 ) a = 1.0;
224  log << "TGeoMaterial* mat_" << pvoid_t(mat) << " = new TGeoMaterial(\""
225  << mat->GetName() << "\", " << a << ", " << z
226  << ", " << mat->GetDensity()
227  << ", " << mat->GetRadLen()
228  << ", " << mat->GetIntLen() << ");"
229  << newline;
230  }
231  log << "mat_" << pvoid_t(mat) << "->SetState(TGeoMaterial::EGeoMaterialState("
232  << mat->GetState() << "));" << newline
233  << "mat_" << pvoid_t(mat) << "->SetPressure(" << mat->GetPressure() << ");" << newline
234  << "mat_" << pvoid_t(mat) << "->SetTemperature(" << mat->GetTemperature() << ");" << newline
235  << "mat_" << pvoid_t(mat) << "->SetTransparency(" << int(mat->GetTransparency()) << ");" << newline;
236  log << "TGeoMedium* material_" << pvoid_t(medium) << " = new TGeoMedium(\""
237  << medium->GetName() << "\", " << medium->GetId()
238  << ", mat_" << pvoid_t(mat) << ");" << newline;
239  }
240  return log;
241  }
242 
243  std::ostream& Actor::handleMatrix(std::ostream& log, TGeoMatrix* mat) {
244  if ( mat && matrices.find(mat) == matrices.end() ) {
245  const Double_t* rot = mat->GetRotationMatrix();
246  const Double_t* tra = mat->GetTranslation();
247  const Double_t* sca = mat->GetScale();
248  log << "TGeoHMatrix* matrix_" << pvoid_t(mat) << " = new TGeoHMatrix(\"" << mat->GetName() << "\");"
249  << newline << "{" << newline;
250  if ( mat->IsTranslation() ) {
251  log << "\t Double_t trans[] = {";
252  for(size_t i=0; tra && i<3; ++i) {
253  log << tra[i];
254  log << ((i<2) ? ", " : "};");
255  }
256  log << newline << "\t matrix_" << pvoid_t(mat) << "->SetTranslation(trans);" << newline;
257  }
258  if ( mat->IsRotation() ) {
259  log << "\t Double_t rot[] = {";
260  for(size_t i=0; rot && i<9; ++i) {
261  log << rot[i];
262  log << ((i<8) ? ", " : "};");
263  }
264  log << newline << "\t matrix_" << pvoid_t(mat) << "->SetRotation(rot);" << newline;
265  }
266  if ( mat->IsScale() ) {
267  log << "\t Double_t scale[] = {";
268  for(size_t i=0; sca && i<3; ++i) {
269  log << sca[i];
270  log << ((i<2) ? ", " : "};");
271  }
272  log << newline << "\t matrix_" << pvoid_t(mat) << "->SetScale(scale);" << newline;
273  }
274  log << "}" << newline;
275  }
276  return log;
277  }
278 
280  std::ostream& Actor::handleSolid(std::ostream& log, const TGeoShape* shape) {
281  if ( !shape || solids.find(shape) != solids.end() ) {
282  return log;
283  }
284  solids.insert(shape);
285 
286  TClass* cl = shape->IsA();
287  void* pvoid = (void*)shape;
288  if ( cl == TGeoBBox::Class() ) {
289  TGeoBBox* sh = (TGeoBBox*) shape;
290  log << cl->GetName() << "* shape_" << pvoid << " = "
291  << "new " << cl->GetName() << "(\"" << sh->GetName() << '"'
292  << ", " << sh->GetDX()
293  << ", " << sh->GetDY()
294  << ", " << sh->GetDZ() << ");" << newline;
295  }
296  else if (cl == TGeoHalfSpace::Class()) {
297  TGeoHalfSpace* sh = (TGeoHalfSpace*)(const_cast<TGeoShape*>(shape));
298  log << cl->GetName() << "* shape_" << (void*)shape << " = 0;" << newline
299  << "{" << newline
300  << "\t Double_t* point_ << " << pvoid << " = {"
301  << sh->GetPoint()[0] << ", " << sh->GetPoint()[1] << ", " << sh->GetPoint()[2] << "}; " << newline
302  << "\t Double_t* norm_ << " << pvoid << " = {"
303  << sh->GetNorm()[0] << ", " << sh->GetNorm()[1] << ", " << sh->GetNorm()[2] << "}; " << newline
304  << "shape_" << pvoid << " = "
305  << "new " << cl->GetName() << "(\"" << sh->GetName() << "\" "
306  << ", point_" << pvoid << ", norm_" << pvoid << ");" << newline
307  << "}" << newline;
308  }
309  else if (cl == TGeoTube::Class()) {
310  const TGeoTube* sh = (const TGeoTube*) shape;
311  log << cl->GetName() << "* shape_" << pvoid << " = "
312  << "new " << cl->GetName() << "(\"" << sh->GetName() << '"'
313  << ", " << sh->GetRmin()
314  << ", " << sh->GetRmax()
315  << ", " << sh->GetDz()
316  << ");" << newline;
317  }
318  else if (cl == TGeoTubeSeg::Class()) {
319  const TGeoTubeSeg* sh = (const TGeoTubeSeg*) shape;
320  log << cl->GetName() << "* shape_" << pvoid << " = "
321  << "new " << cl->GetName() << "(\"" << sh->GetName() << '"'
322  << ", " << sh->GetRmin()
323  << ", " << sh->GetRmax()
324  << ", " << sh->GetDz()
325  << ", " << sh->GetPhi1()
326  << ", " << sh->GetPhi2()
327  << ");" << newline;
328  }
329  else if (cl == TGeoCtub::Class()) {
330  const TGeoCtub* sh = (const TGeoCtub*) shape;
331  const Double_t* hi = sh->GetNhigh();
332  const Double_t* lo = sh->GetNlow();
333  log << cl->GetName() << "* shape_" << pvoid << " = "
334  << "new " << cl->GetName() << "(\"" << sh->GetName() << '"'
335  << ", " << sh->GetRmin()
336  << ", " << sh->GetRmax()
337  << ", " << sh->GetDz()
338  << ", " << sh->GetPhi1()
339  << ", " << sh->GetPhi2()
340  << ", " << lo[0]
341  << ", " << lo[1]
342  << ", " << lo[2]
343  << ", " << hi[0]
344  << ", " << hi[1]
345  << ", " << hi[2]
346  << ");" << newline;
347  }
348  else if (cl == TGeoEltu::Class()) {
349  const TGeoEltu* sh = (const TGeoEltu*) shape;
350  log << cl->GetName() << "* shape_" << pvoid << " = "
351  << "new " << cl->GetName() << "(\"" << sh->GetName() << '"'
352  << ", " << sh->GetA()
353  << ", " << sh->GetB()
354  << ", " << sh->GetDz()
355  << ");" << newline;
356  }
357  else if (cl == TGeoTrd1::Class()) {
358  const TGeoTrd1* sh = (const TGeoTrd1*) shape;
359  log << cl->GetName() << "* shape_" << pvoid << " = "
360  << "new " << cl->GetName() << "(\"" << sh->GetName() << '"'
361  << ", " << sh->GetDx1()
362  << ", " << sh->GetDx2()
363  << ", " << sh->GetDy()
364  << ", " << sh->GetDz()
365  << ");" << newline;
366  }
367  else if (cl == TGeoTrd2::Class()) {
368  const TGeoTrd2* sh = (const TGeoTrd2*) shape;
369  log << cl->GetName() << "* shape_" << pvoid << " = "
370  << "new " << cl->GetName() << "(\"" << sh->GetName() << '"'
371  << ", " << sh->GetDx1()
372  << ", " << sh->GetDx2()
373  << ", " << sh->GetDy1()
374  << ", " << sh->GetDy2()
375  << ", " << sh->GetDz()
376  << ");" << newline;
377  }
378  else if (cl == TGeoTrap::Class()) {
379  const TGeoTrap* sh = (const TGeoTrap*) shape;
380  log << cl->GetName() << "* shape_" << pvoid << " = "
381  << "new " << cl->GetName() << "(\"" << sh->GetName() << '"'
382  << ", " << sh->GetDz() << ", " << sh->GetTheta() << ", " << sh->GetPhi()
383  << ", " << sh->GetH1() << ", " << sh->GetBl1() << ", " << sh->GetTl1() << ", " << sh->GetAlpha1()
384  << ", " << sh->GetH2() << ", " << sh->GetBl2() << ", " << sh->GetTl2() << ", " << sh->GetAlpha2()
385  << ");" << newline;
386  }
387  else if (cl == TGeoHype::Class()) {
388  const TGeoHype* sh = (const TGeoHype*) shape;
389  log << cl->GetName() << "* shape_" << pvoid << " = "
390  << "new " << cl->GetName() << "(\"" << sh->GetName() << '"'
391  << ", " << sh->GetRmin() << ", " << sh->GetRmax() << ", " << sh->GetDz()
392  << ", " << sh->GetStIn() << ", " << sh->GetStOut()
393  << ");" << newline;
394  }
395  else if (cl == TGeoPgon::Class()) {
396  const TGeoPgon* sh = (const TGeoPgon*) shape;
397  std::vector<double> params;
398  params.emplace_back(sh->GetPhi1());
399  params.emplace_back(sh->GetDphi());
400  params.emplace_back(double(sh->GetNedges()));
401  params.emplace_back(double(sh->GetNz()));
402  for(int i=0, n=sh->GetNz(); i<n; ++i) {
403  params.emplace_back(sh->GetZ(i));
404  params.emplace_back(sh->GetRmin(i));
405  params.emplace_back(sh->GetRmax(i));
406  }
407  log << cl->GetName() << "* shape_" << pvoid << " = "
408  << "new " << cl->GetName() << "(" << &params[0] << ");" << newline;
409  log << "shape_" << pvoid << "->SetName(\"" << sh->GetName() << "\");" << newline;
410  }
411  else if (cl == TGeoPcon::Class()) {
412  const TGeoPcon* sh = (const TGeoPcon*) shape;
413  std::vector<double> params;
414  params.emplace_back(sh->GetPhi1());
415  params.emplace_back(sh->GetDphi());
416  params.emplace_back(double(sh->GetNz()));
417  for(int i=0, n=sh->GetNz(); i<n; ++i) {
418  params.emplace_back(sh->GetZ(i));
419  params.emplace_back(sh->GetRmin(i));
420  params.emplace_back(sh->GetRmax(i));
421  }
422  log << cl->GetName() << "* shape_" << pvoid << " = "
423  << "new " << cl->GetName() << "(" << &params[0] << ");" << newline;
424  log << "shape_" << pvoid << "->SetName(\"" << sh->GetName() << "\");" << newline;
425  }
426  else if (cl == TGeoCone::Class()) {
427  const TGeoCone* sh = (const TGeoCone*) shape;
428  log << cl->GetName() << "* shape_" << pvoid << " = "
429  << "new " << cl->GetName() << "(\"" << sh->GetName() << '"'
430  << ", " << sh->GetRmin1() << ", " << sh->GetRmin2() << ", "
431  << ", " << sh->GetRmax1() << ", " << sh->GetRmax2()
432  << ", " << sh->GetDz()
433  << ");" << newline;
434  }
435  else if (cl == TGeoConeSeg::Class()) {
436  const TGeoConeSeg* sh = (const TGeoConeSeg*) shape;
437  log << cl->GetName() << "* shape_" << pvoid << " = "
438  << "new " << cl->GetName() << "(\"" << sh->GetName() << '"'
439  << ", " << sh->GetRmin1() << ", " << sh->GetRmin2() << ", "
440  << ", " << sh->GetRmax1() << ", " << sh->GetRmax2()
441  << ", " << sh->GetDz()
442  << ", " << sh->GetPhi1() << ", " << sh->GetPhi2()
443  << ");" << newline;
444  }
445  else if (cl == TGeoParaboloid::Class()) {
446  const TGeoParaboloid* sh = (const TGeoParaboloid*) shape;
447  log << cl->GetName() << "* shape_" << pvoid << " = "
448  << "new " << cl->GetName() << "(\"" << sh->GetName() << '"'
449  << ", " << sh->GetRlo() << ", " << sh->GetRhi() << ", " << sh->GetDz() << ");" << newline;
450  }
451  else if (cl == TGeoSphere::Class()) {
452  const TGeoSphere* sh = (const TGeoSphere*) shape;
453  log << cl->GetName() << "* shape_" << pvoid << " = "
454  << "new " << cl->GetName() << "(\"" << sh->GetName() << '"'
455  << ", " << sh->GetRmin() << ", " << sh->GetRmax()
456  << ", " << sh->GetPhi1() << ", " << sh->GetPhi2()
457  << ", " << sh->GetTheta1() << ", " << sh->GetTheta2()
458  << ");" << newline;
459  }
460  else if (cl == TGeoTorus::Class()) {
461  const TGeoTorus* sh = (const TGeoTorus*) shape;
462  log << cl->GetName() << "* shape_" << pvoid << " = "
463  << "new " << cl->GetName() << "(\"" << sh->GetName() << '"'
464  << ", " << sh->GetRmin() << ", " << sh->GetRmax() << ", " << sh->GetR()
465  << ", " << sh->GetPhi1() << ", " << sh->GetDphi()
466  << ");" << newline;
467  }
468  else if (cl == TGeoArb8::Class()) {
469  TGeoArb8* sh = (TGeoArb8*) shape;
470  const Double_t* v = sh->GetVertices();
471  log << cl->GetName() << "* shape_" << pvoid << " = 0;" << newline
472  << "{" << newline
473  << "\tstd::vector<double> vertices_" << pvoid << ";" << newline;
474  for(int i=0; i<8; ++i) {
475  log << "\tvertices_" << pvoid << ".emplace_back(" << *v << ");" << newline; ++v;
476  log << "\tvertices_" << pvoid << ".emplace_back(" << *v << ");" << newline; ++v;
477  }
478  log << "\tshape_" << pvoid << " = new " << cl->GetName()
479  << "(\"" << sh->GetName() << '"'
480  << ", " << sh->GetDz() << ", &vertices_" << pvoid << "[0]);" << newline
481  << "}" << newline;
482  }
483  else if (cl == TGeoXtru::Class()) {
484  Solid sol(shape);
485  const TGeoXtru* sh = (const TGeoXtru*) shape;
486  std::vector<double> pars = sol.dimensions();
487  log << cl->GetName() << "* shape_" << pvoid << " = 0;" << newline
488  << "{" << newline
489  << "\tstd::vector<double> param_" << pvoid << ";" << newline;
490  for( auto p : pars)
491  log << "\tparam_" << pvoid << ".emplace_back(" << p << ");" << newline;
492  log << "\tshape_" << pvoid << " = new " << cl->GetName()
493  << "( &param_" << pvoid << "[0]);" << newline;
494  log << "shape_" << pvoid << "->SetName(\"" << sh->GetName() << "\");" << newline;
495  }
496  else if (shape->IsA() == TGeoCompositeShape::Class()) {
497  const TGeoCompositeShape* sh = (const TGeoCompositeShape*) shape;
498  const TGeoBoolNode* boolean = sh->GetBoolNode();
499  const TGeoShape* left = boolean->GetLeftShape();
500  const TGeoShape* right = boolean->GetRightShape();
501  TGeoBoolNode::EGeoBoolType oper = boolean->GetBooleanOperator();
502  handleSolid(log, left);
503  handleSolid(log, right);
504  handleMatrix(log, boolean->GetRightMatrix());
505  log << "TGeoCompositeShape* shape_" << pvoid_t(sh) << " = 0;" << newline;
506  log << "{" << newline;
507  if (oper == TGeoBoolNode::kGeoSubtraction)
508  log << "\t TGeoSubtraction* boolean = new TGeoSubtraction";
509  else if (oper == TGeoBoolNode::kGeoUnion)
510  log << "\t TGeoUnion* boolean = new TGeoUnion";
511  else if (oper == TGeoBoolNode::kGeoIntersection)
512  log << "\t TGeoIntersection* boolean = new TGeoIntersection";
513  log << "(shape_" << pvoid_t(left) << ", shape_" << pvoid_t(right) << ", 0, ";
514  log << "matrix_" << pvoid_t(boolean->GetRightMatrix()) << ");" << newline;
515  log << "\t shape_" << pvoid_t(sh)
516  << " = new TGeoCompositeShape(\"" << sh->GetName() << "\", boolean);" << newline;
517  log << "}" << newline;
518  }
519  else if (shape->IsA() == TGeoShapeAssembly::Class()) {
520  //const TGeoShapeAssembly* sh = (const TGeoShapeAssembly*)shape;
521  // Nothing to do here: All handled in the handling of TGeoVolumeAssembly
522  }
523  else {
524  except("CxxRootGenerator","++ Unknown shape transformation request: %s", shape->IsA()->GetName());
525  }
526  return log;
527  }
528 
529 }
530 
531 static long generate_cxx(Detector& description, int argc, char** argv) {
532  std::string output;
533  Actor actor;
534 
535  for(int i=0; i<argc; ++i) {
536  char c = ::tolower(argv[i][0]);
537  if ( c == '-' ) { c = ::tolower(argv[i][1]); }
538  if ( c == '-' ) { c = ::tolower(argv[i][1]); }
539  if ( c == 'o' && i+1<argc )
540  output = argv[++i];
541  else if ( c == 'f' && i+1<argc )
542  actor.function = argv[++i];
543  else if ( c == 'v' )
544  actor.dump_vis = true;
545  else if ( c == 'm' )
546  actor.dump_mat = true;
547  else {
548  std::cout <<
549  "Usage: -plugin DD4hep_CxxRootGenerator -arg [-arg] \n"
550  " -output <string> Set output file for generated code. Default: stdout \n"
551  " -visualization Also dump visualization attributes of volumes \n"
552  " -materials Also dump proper materials. Default to IRON \n"
553  " -help Show thi help message \n"
554  "\tArguments given: " << arguments(argc,argv) << std::endl << std::flush;
555  ::exit(EINVAL);
556  }
557  }
558  std::unique_ptr<std::ofstream> out;
559  std::ostream* os = &std::cout;
560  if ( !output.empty() ) {
561  Path path(output);
562  out.reset(new std::ofstream(path.c_str()));
563  if ( !out->good() ) {
564  out.reset();
565  except("CxxRootGenerator",
566  "++ Failed to open output files: %s [%s]",
567  path.c_str(), ::strerror(errno));
568  }
569  os = out.get();
570  actor.function = path.filename();
571  if ( actor.function.rfind('.') != std::string::npos )
572  actor.function = actor.function.substr(0, actor.function.rfind('.'));
573  printout(INFO, "CxxRootGenerator",
574  "++ Dump generated code to output files: %s [function: %s()]",
575  path.c_str(), actor.function.c_str());
576  }
577  else if ( actor.function.empty() ) {
578  actor.function = "run_geometry";
579  }
580  DetElement de = description.world();
581  PlacedVolume pv = de.placement();
582  actor.handleHeader(*os);
583  actor.handlePlacement(*os, 0, pv.ptr());
584  actor.handleTrailer(*os);
585  out.reset();
586  return 1;
587 }
588 
589 DECLARE_APPLY(DD4hep_TGeoCxxGenerator,generate_cxx)
dd4hep::Detector::world
virtual DetElement world() const =0
Return reference to the top-most (world) detector element.
Volumes.h
v
View * v
Definition: MultiView.cpp:28
Path.h
MatrixHelpers.h
Detector.h
dd4hep::PlacedVolume
Handle class holding a placed volume (also called physical volume)
Definition: Volumes.h:164
dd4hep::DetElement::placement
PlacedVolume placement() const
Access to the physical volume of this detector element.
Definition: DetElement.cpp:321
DECLARE_APPLY
#define DECLARE_APPLY(name, func)
Definition: Factories.h:281
dd4hep::Solid_type< TGeoShape >
Factories.h
dd4hep::DetElement
Handle class describing a detector element.
Definition: DetElement.h:187
mix
#define mix(a, b, c)
Definition: Geant4EventSeed.h:113
Shapes.h
dd4hep::Handle::ptr
T * ptr() const
Access to the held object.
Definition: Handle.h:151
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::Detector
The main interface to the dd4hep detector description package.
Definition: Detector.h:90
TGeoConeSeg
Class of the ROOT toolkit. See http://root.cern.ch/root/htmldoc/ClassIndex.html.
Definition: ROOTClasses.h:17
DD4hepUnits.h
Printout.h
dd4hep::Path
Path handling class.
Definition: Path.h:45