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