33 #include <TGeoMatrix.h>
34 #include <TGeoBoolNode.h>
35 #include <TGeoCompositeShape.h>
41 std::string prefix =
"\t";
45 std::map<const TGeoNode*,std::string> placements;
46 std::map<const TGeoVolume*,std::string> volumes;
47 std::map<const TGeoShape*,std::string> shapes;
48 std::map<const TGeoMatrix*,std::string> matrices;
49 std::map<const TGeoMedium*,std::string> materials;
50 std::map<DetElement,std::string> detelements;
52 std::string
function {
"run_geometry"};
53 bool dump_vis =
false;
54 bool dump_structure =
false;
58 std::ostream& handleHeader (std::ostream& log);
59 std::ostream& handleTrailer (std::ostream& log);
60 std::ostream& handleSolid (std::ostream& log,
const TGeoShape* sh);
61 std::ostream& handleMatrix (std::ostream& log, TGeoMatrix* mat);
62 std::ostream& handleMaterial (std::ostream& log, TGeoMedium* mat);
63 std::ostream& handlePlacement(std::ostream& log, TGeoNode* parent, TGeoNode* node);
66 typedef void* pvoid_t;
68 std::ostream& newline(std::ostream& log) {
69 return log << std::endl << prefix;
71 template <
typename T>
const void* pointer(
const Handle<T>& h) {
74 template <
typename T>
const void* pointer(
const T* h) {
77 template <
typename T>
inline std::string obj_name(
const std::string& pref,
const T* ptr) {
78 std::stringstream name;
79 name << pref <<
"_" << pointer(ptr);
84 std::ostream& Actor::handleHeader (std::ostream& log) {
85 log <<
"#include \"TClass.h\"" << std::endl
86 <<
"#include \"TGeoNode.h\"" << std::endl
87 <<
"#include \"TGeoExtension.h\"" << std::endl
88 <<
"#include \"TGeoShapeAssembly.h\"" << std::endl
89 <<
"#include \"TGeoMedium.h\"" << std::endl
90 <<
"#include \"TGeoVolume.h\"" << std::endl
91 <<
"#include \"TGeoShape.h\"" << std::endl
92 <<
"#include \"TGeoPhysicalNode.h\"" << std::endl
93 <<
"#include \"TGeoCone.h\"" << std::endl
94 <<
"#include \"TGeoParaboloid.h\"" << std::endl
95 <<
"#include \"TGeoPgon.h\"" << std::endl
96 <<
"#include \"TGeoPcon.h\"" << std::endl
97 <<
"#include \"TGeoSphere.h\"" << std::endl
98 <<
"#include \"TGeoArb8.h\"" << std::endl
99 <<
"#include \"TGeoTrd1.h\"" << std::endl
100 <<
"#include \"TGeoTrd2.h\"" << std::endl
101 <<
"#include \"TGeoTube.h\"" << std::endl
102 <<
"#include \"TGeoEltu.h\"" << std::endl
103 <<
"#include \"TGeoXtru.h\"" << std::endl
104 <<
"#include \"TGeoHype.h\"" << std::endl
105 <<
"#include \"TGeoTorus.h\"" << std::endl
106 <<
"#include \"TGeoHalfSpace.h\"" << std::endl
107 <<
"#include \"TGeoCompositeShape.h\"" << std::endl
108 <<
"#include \"TGeoShapeAssembly.h\"" << std::endl
109 <<
"#include \"TGeoMatrix.h\"" << std::endl
110 <<
"#include \"TGeoBoolNode.h\"" << std::endl
111 <<
"#include \"TGeoCompositeShape.h\"" << std::endl
112 <<
"#include \"TGeoManager.h\"" << std::endl << std::endl
113 <<
"#include \"DD4hep/Factories.h\"" << std::endl
114 <<
"#include \"DD4hep/Shapes.h\"" << std::endl
115 <<
"#include \"DD4hep/Volumes.h\"" << std::endl
116 <<
"#include \"DD4hep/Detector.h\"" << std::endl
117 <<
"#include \"DD4hep/DetElement.h\"" << std::endl
118 <<
"#include \"DD4hep/MatrixHelpers.h\"" << std::endl
119 <<
"#include \"DD4hep/DD4hepUnits.h\"" << std::endl
120 <<
"#include \"DD4hep/Printout.h\"" << std::endl
121 <<
"#include \"DD4hep/Path.h\"" << std::endl
122 <<
"#include \"DD4hep/detail/ObjectsInterna.h\"" << std::endl
123 <<
"#include \"DD4hep/detail/DetectorInterna.h\"" << std::endl
124 <<
"#include <vector>" << std::endl
125 <<
"#include <map>" << std::endl
126 <<
"#include <set>" << std::endl << std::endl << std::endl;
127 log <<
"using namespace std;" << std::endl;
128 log <<
"using namespace dd4hep;" << std::endl;
129 log <<
"extern PlacedVolume _addNode(TGeoVolume* par, TGeoVolume* daughter, int id, TGeoMatrix* transform);" << std::endl;
131 log <<
"namespace {" << newline
132 <<
"\t struct CodeGeo {" << newline
133 <<
"\t\t map<unsigned long, TGeoNode*> placements;" << newline
134 <<
"\t\t map<unsigned long, TGeoVolume*> volumes;" << newline
135 <<
"\t\t map<unsigned long, TGeoShape*> shapes;" << newline
136 <<
"\t\t map<unsigned long, TGeoMatrix*> matrices;" << newline
137 <<
"\t\t map<unsigned long, TGeoMedium*> materials;" << newline
138 <<
"\t\t map<unsigned long, DetElement> structure;" << newline
139 <<
"\t\t Detector& detector;" << newline
140 <<
"\t\t CodeGeo(Detector& d) : detector(d) {}" << newline
141 <<
"\t\t TGeoVolume* load_geometry();" << newline
142 <<
"\t\t DetElement load_structure();" << newline
143 <<
"\t\t void add_vis(Detector& d, VisAttr& v) {" << newline
144 <<
"\t\t try { d.add(v); } catch(...) {}" << newline
145 <<
"\t\t }" << newline
146 <<
"\t };" << std::endl
147 <<
"}" << std::endl << std::endl << std::endl;
148 log <<
"TGeoVolume* CodeGeo::load_geometry() {" << newline;
150 const auto& constants = detector.constants();
151 log <<
"/// Handling " << constants.size() <<
" Constants" << newline;
152 for(
const auto& o : constants) {
154 log <<
"detector.add(Constant(\"" << c.
name() <<
"\",\""
155 << c->type <<
"\",\"" << c->
dataType <<
"\")); " << newline;
159 const auto& vis = detector.visAttributes();
160 log <<
"/// Handling " << vis.size() <<
" Visualization attributes" << newline;
161 for(
const auto& o : vis) {
165 log <<
"{ VisAttr v(\"" << o.first <<
"\"); "
166 <<
"v.setColor(" << r << sep << g << sep << b <<
"); "
167 <<
"v.setShowDaughters(" <<
v.showDaughters() <<
"); "
168 <<
"v.setVisible(" <<
v.visible() <<
"); " << newline
169 <<
" v.setLineStyle(" <<
v.lineStyle() <<
"); "
170 <<
"v.setDrawingStyle(" <<
v.drawingStyle() <<
"); "
171 <<
"v.setAlpha(" <<
v.alpha() <<
"); "
172 <<
"add_vis(detector,v); }" << newline;
175 const auto& limits = detector.limitsets();
176 log <<
"/// Handling " << limits.size() <<
" Limit Sets" << newline;
177 for(
const auto& o : limits) {
179 log <<
"{ LimitSet ls(string(\"" << ls.
name() <<
"\")); " << newline;
180 const std::set<Limit>& lims = ls.
limits();
181 const std::set<Limit>& cuts = ls.
cuts();
182 for(
const auto& l : lims) {
183 log <<
" { Limit l; l.particles = \"" << l.particles <<
"\";"
184 <<
" l.name = \"" << l.name <<
"\";"
185 <<
" l.unit = \"" << l.unit <<
"\";"
186 <<
" l.content = \"" << l.content <<
"\";"
187 <<
" l.value = " << l.value <<
";"
188 <<
" ls.addLimit(l); } " << newline;
190 for(
const auto& l : cuts) {
191 log <<
" { Limit l; l.particles = \"" << l.particles <<
"\";"
192 <<
" l.name = \"" << l.name <<
"\";"
193 <<
" l.unit = \"" << l.unit <<
"\";"
194 <<
" l.content = \"" << l.content <<
"\";"
195 <<
" l.value = " << l.value <<
";"
196 <<
" ls.addLimit(l); } " << newline;
198 log <<
" detector.addLimitSet(ls); } " << newline;
201 const auto& regions = detector.regions();
202 log <<
"/// Handling " << regions.size() <<
" Region settings " << newline;
203 for(
const auto& o : regions) {
205 log <<
"{ Region r(\"" << r.
name() <<
"\")"
206 <<
"; r->store_secondaries = " << r->store_secondaries
207 <<
"; r->was_threshold_set = " << r->was_threshold_set
208 <<
"; r->use_default_cut = " << r->use_default_cut
210 <<
"; r->cut = " << r->
cut <<
";" << newline;
211 if ( !r->user_limits.empty() ) {
212 log <<
" vector<string> user_limits = {";
213 for(
size_t i=0, n=r->user_limits.size(); i<n; ++i)
214 log <<
"r->user_limits.emplace_back(\"" << r->user_limits[i] <<
"\");" << newline;
218 const auto& ids = detector.idSpecifications();
219 log <<
"/// Handling " << ids.size() <<
" Id Specifications " << newline;
220 for(
const auto& o : ids) {
222 log <<
"{ IDDescriptor i(\"" << i.
name() <<
"\", \"" << i->description <<
"\");"
223 <<
" detector.add(i); }" << newline;
226 const auto& segments = detector.readouts();
227 log <<
"/// Handling " << segments.size() <<
" Segmentations " << newline
228 <<
"list<Segmentation> segs; " << newline;
229 for(
const auto& o : segments) {
233 log <<
"{ Segmentation s(\"" << s.
name() <<
"\");"
234 <<
" segs.emplace_back(s); }" << newline;
238 const auto& readouts = detector.readouts();
239 log <<
"/// Handling " << readouts.size() <<
" Readout settings " << newline;
240 for(
const auto& o : readouts) {
242 log <<
"{ Readout r(string(\"" << r.
name() <<
"\"));"
243 <<
" r->SetTitle(\"" << r->GetTitle() <<
"\"); ";
245 log <<
" r->segmentation = segs.front(); segs.pop_front(); ";
247 log <<
" r->id = detector.idSpecification(\"" << r->id.
name() <<
"\"); ";
248 for(
const auto& c : r->hits) {
250 log <<
"{ HitCollection c(\"" << c.
name <<
"\",\"" << c.key <<
"\", "
251 << c.key_min << sep << c.key_max <<
"); "
252 <<
" r->hits.emplace_back(c); } ";
254 if ( !r->hits.empty() ) log << newline;
255 log <<
" detector.add(r); }" << newline;
260 std::ostream& Actor::handleTrailer (std::ostream& log) {
261 log <<
"static long generate_dd4hep(Detector& detector, int, char**) {" << newline
262 <<
"CodeGeo gen(detector);" << newline
263 <<
"TGeoVolume* vol_top = gen.load_geometry();" << newline
264 <<
"detector.manager().SetTopVolume(vol_top);" << newline
265 <<
"detector.init();" << newline;
266 if ( dump_structure ) {
267 TGeoManager& mgr = detector.manager();
268 handleMaterial(log, mgr.GetMedium(
"Air"));
269 handleMaterial(log, mgr.GetMedium(
"Vacuum"));
270 log <<
"gen.structure[" << pointer(detector.world()) <<
"] = detector.world(); " << newline
271 <<
"gen.load_structure();" << newline;
274 << std::endl <<
"}" << std::endl << std::endl
275 <<
"DECLARE_APPLY(DD4hep_Run_" <<
function <<
", generate_dd4hep)"
281 if ( de.
isValid() && detelements.find(de) == detelements.end() ) {
282 std::string name = obj_name(
"de", de.
ptr());
283 detelements.emplace(de,name);
285 std::cout <<
"No parent: " << de.
path() <<
" " << pointer(de) <<
" " << pointer(detector.world()) << std::endl;
287 <<
"DetElement CodeGeo::load_structure() {" << newline;
290 log <<
"{" << newline
291 <<
"\t DetElement par = structure[" << pointer(parent) <<
"];" << newline
292 <<
"\t DetElement de(par,\"" << de.
name() <<
"\"," << de.
id() <<
");" << newline
293 <<
"\t de->SetTitle(\"" << de->GetTitle() <<
"\");" << newline
294 <<
"\t de->combineHits = " << de->
combineHits <<
";" << newline
295 <<
"\t de.setTypeFlag(" << de.
typeFlag() <<
");" << newline;
297 log <<
"\t de.setPlacement(placements[" << pointer(de.
placement()) <<
"]);" << newline;
300 std::cout <<
"Placement od DetElement " << de.
path() <<
" Is not valid! [Ignored]" << std::endl;
302 log <<
"\t structure[" << pointer(de) <<
"] = de; " << newline
305 for(
const auto& d : de.
children() ) {
306 handleStructure(log, de, d.second);
309 log <<
"return structure[" << pointer(de) <<
"];" << std::endl
310 <<
"}" << std::endl << std::endl;
316 std::ostream& Actor::handlePlacement(std::ostream& log, TGeoNode* parent, TGeoNode* node) {
317 if ( node && placements.find(node) == placements.end() ) {
319 TGeoVolume* vol = node->GetVolume();
320 TGeoMatrix* mat = node->GetMatrix();
322 std::string name = obj_name(
"vol", vol);
323 placements.emplace(node,name);
325 handleMatrix(log, mat);
327 if ( vol && volumes.find(vol) == volumes.end() ) {
328 volumes.emplace(vol,name);
329 if ( vol->IsA() == TGeoVolumeAssembly::Class() ) {
330 log <<
"{" << newline;
331 log <<
"\t Assembly vol(\"" << vol->GetName() <<
"\");" << newline;
335 TGeoMedium* med = vol->GetMedium();
336 TGeoShape* sh = vol->GetShape();
337 handleSolid(log, sh);
338 handleMaterial(log, med);
339 log <<
"{" << newline;
340 log <<
"\t Volume vol(\"" << vol->GetName() <<
"\", "
341 <<
"Solid(shapes[" << pointer(sh) <<
"]), "
342 <<
"Material(materials[" << pointer(med) <<
"]));" << newline;
343 if ( ::strlen(vol->GetTitle()) != 0 )
344 log <<
"\t vol->SetTitle(\"" << vol->GetTitle() <<
"\");" << newline;
345 if ( !
v.option().empty() )
346 log <<
"\t vol.setOption(\"" <<
v.option() <<
"\"); " << newline;
347 if (
v.region().isValid() )
348 log <<
"\t vol.setRegion(detector, \"" <<
v.region().
name() <<
"\");" << newline;
349 if (
v.limitSet().isValid() )
350 log <<
"\t vol.setLimitSet(detector, \"" <<
v.limitSet().
name() <<
"\");" << newline;
351 if (
v.sensitiveDetector().isValid() )
352 log <<
"\t vol.setSensitiveDetector(detector.sensitiveDetector(\""
353 <<
v.sensitiveDetector().
name() <<
"\"));" << newline;
354 if ( dump_vis &&
v.visAttributes().isValid() )
355 log <<
"\t vol.setVisAttributes(detector, \"" <<
v.visAttributes().
name() <<
"\");" << newline;
357 log <<
"\t volumes[" << pointer(vol) <<
"] = vol;" << newline;
360 log <<
"{" << newline
361 <<
"\t Volume vol = volumes[" << pointer(vol) <<
"];" << newline;
364 log <<
"\t PlacedVolume pv = _addNode(volumes[" << pointer(parent->GetVolume())
365 <<
"],vol.ptr()," << pv.copyNumber() << sep
366 <<
"matrices[" << pointer(mat) <<
"]);" << newline
367 <<
"\t placements[" << pointer(node) <<
"] = pv.ptr(); " << newline;
368 for(
const auto& vid : pv.volIDs() )
369 log <<
"\t pv.addPhysVolID(\"" << vid.first <<
"\", " << vid.second <<
");" << newline;
371 log <<
"}" << newline;
372 for (Int_t idau = 0, ndau = node->GetNdaughters(); idau < ndau; ++idau) {
373 TGeoNode* daughter = node->GetDaughter(idau);
374 handlePlacement(log, node, daughter);
377 log <<
"return volumes[" << pointer(vol) <<
"];" << std::endl
378 <<
"}" << std::endl << std::endl << std::endl;
384 std::ostream& Actor::handleMaterial(std::ostream& log, TGeoMedium* medium) {
385 if ( medium && materials.find(medium) == materials.end() ) {
386 std::string name = obj_name(
"material",medium);
387 materials.emplace(medium,name);
388 TGeoMaterial* material = medium->GetMaterial();
389 log <<
"{" << newline
390 <<
"\t TGeoManager& mgr = detector.manager();" << newline
391 <<
"\t TGeoMedium* med = mgr.GetMedium(\""<< medium->GetName() <<
"\");" << newline
392 <<
"\t if ( 0 == med ) {" << newline
393 <<
"\t TGeoMaterial* mat = mgr.GetMaterial(\"" << material->GetName() <<
"\");" << newline
394 <<
"\t if ( 0 == mat ) {" << newline
395 <<
"\t mat = new TGeoMaterial(\"" << material->GetName() <<
"\", "
396 << material->GetA() << sep << material->GetZ() << sep << material->GetDensity() << sep
397 << material->GetRadLen() << sep << material->GetIntLen() <<
");" << newline
399 <<
"\t med = new TGeoMedium(\""<< medium->GetName() <<
"\"," << medium->GetId() <<
", mat);" << newline
401 <<
"\t materials[" << pointer(medium) <<
"] = med;" << newline
407 std::ostream& Actor::handleMatrix(std::ostream& log, TGeoMatrix* mat) {
408 if ( mat && matrices.find(mat) == matrices.end() ) {
409 std::string name = obj_name(
"matrix",mat);
410 log <<
"{" << newline
411 <<
"\t TGeoHMatrix* mat = new TGeoHMatrix(\"" << mat->GetName() <<
"\");" << newline
412 <<
"\t matrices[" << pointer(mat) <<
"] = mat;" << newline;
413 if ( mat->IsTranslation() ) {
414 const Double_t* tra = mat->GetTranslation();
415 log <<
"\t Double_t trans[] = {";
416 for(
size_t i=0; tra && i<3; ++i) {
418 log << ((i<2) ? sep :
"};");
420 log << newline <<
"\t mat->SetTranslation(trans);" << newline;
422 if ( mat->IsRotation() ) {
423 const Double_t* rot = mat->GetRotationMatrix();
424 if ( rot && (rot[0] != 1e0 || rot[4] != 1e0 || rot[8] != 1e0) ) {
425 log <<
"\t Double_t rot[] = {";
426 for(
size_t i=0; rot && i<9; ++i) {
428 log << ((i<8) ? sep :
"};");
430 log << newline <<
"\t mat->SetRotation(rot);" << newline;
433 if ( mat->IsScale() ) {
434 const Double_t* sca = mat->GetScale();
435 log <<
"\t Double_t scale[] = {";
436 for(
size_t i=0; sca && i<3; ++i) {
438 log << ((i<2) ? sep :
"};");
440 log << newline <<
"\t mat->SetScale(scale);" << newline;
442 log <<
"}" << newline;
448 std::ostream& Actor::handleSolid(std::ostream& log,
const TGeoShape* shape) {
449 std::string name = obj_name(
"solid", shape);
451 if ( shapes.find(shape) != shapes.end() ) {
454 else if (shape->IsA() == TGeoShapeAssembly::Class()) {
459 shapes.emplace(shape,name);
461 TClass* cl = shape->IsA();
462 log <<
"{" << newline;
463 if ( cl == TGeoBBox::Class() ) {
464 TGeoBBox* sh = (TGeoBBox*) shape;
465 log <<
"\t Solid " << name <<
"(new " << cl->GetName() <<
"(\"" << sh->GetName() <<
'"'
466 << sep << sh->GetDX()
467 << sep << sh->GetDY()
468 << sep << sh->GetDZ() <<
"));" << newline;
470 else if (cl == TGeoHalfSpace::Class()) {
471 TGeoHalfSpace* sh = (TGeoHalfSpace*)(
const_cast<TGeoShape*
>(shape));
472 log <<
"\t Double_t* point = {"
473 << sh->GetPoint()[0] << sep << sh->GetPoint()[1] << sep << sh->GetPoint()[2] <<
"}; " << newline
474 <<
"\t Double_t* norm = {"
475 << sh->GetNorm()[0] << sep << sh->GetNorm()[1] << sep << sh->GetNorm()[2] <<
"}; " << newline
476 <<
"\t Solid " << name <<
" = Solid(new " << cl->GetName() <<
"(\""
477 << sh->GetName() <<
"\", point, norm));" << newline;
479 else if (cl == TGeoTube::Class()) {
480 const TGeoTube* sh = (
const TGeoTube*) shape;
481 log <<
"\t Solid " << name <<
"(new " << cl->GetName() <<
"(\"" << sh->GetName() <<
'"'
482 << sep << sh->GetRmin()
483 << sep << sh->GetRmax()
484 << sep << sh->GetDz()
487 else if (cl == TGeoTubeSeg::Class()) {
488 const TGeoTubeSeg* sh = (
const TGeoTubeSeg*) shape;
489 log <<
"\t Solid " << name <<
"(new " << cl->GetName() <<
"(\"" << sh->GetName() <<
'"'
490 << sep << sh->GetRmin()
491 << sep << sh->GetRmax()
492 << sep << sh->GetDz()
493 << sep << sh->GetPhi1()
494 << sep << sh->GetPhi2()
497 else if (cl == TGeoCtub::Class()) {
498 const TGeoCtub* sh = (
const TGeoCtub*) shape;
499 const Double_t* hi = sh->GetNhigh();
500 const Double_t* lo = sh->GetNlow();
501 log <<
"\t Solid " << name <<
"(new " << cl->GetName() <<
"(\"" << sh->GetName() <<
'"'
502 << sep << sh->GetRmin()
503 << sep << sh->GetRmax()
504 << sep << sh->GetDz()
505 << sep << sh->GetPhi1()
506 << sep << sh->GetPhi2()
515 else if (cl == TGeoEltu::Class()) {
516 const TGeoEltu* sh = (
const TGeoEltu*) shape;
517 log <<
"\t Solid " << name <<
"(new " << cl->GetName() <<
"(\"" << sh->GetName() <<
'"'
520 << sep << sh->GetDz()
523 else if (cl == TGeoTrd1::Class()) {
524 const TGeoTrd1* sh = (
const TGeoTrd1*) shape;
525 log <<
"\t Solid " << name <<
"(new " << cl->GetName() <<
"(\"" << sh->GetName() <<
'"'
526 << sep << sh->GetDx1()
527 << sep << sh->GetDx2()
528 << sep << sh->GetDy()
529 << sep << sh->GetDz()
532 else if (cl == TGeoTrd2::Class()) {
533 const TGeoTrd2* sh = (
const TGeoTrd2*) shape;
534 log <<
"\t Solid " << name <<
"(new " << cl->GetName() <<
"(\"" << sh->GetName() <<
'"'
535 << sep << sh->GetDx1()
536 << sep << sh->GetDx2()
537 << sep << sh->GetDy1()
538 << sep << sh->GetDy2()
539 << sep << sh->GetDz()
542 else if (cl == TGeoTrap::Class()) {
543 const TGeoTrap* sh = (
const TGeoTrap*) shape;
544 log <<
"\t Solid " << name <<
"(new " << cl->GetName() <<
"(\"" << sh->GetName() <<
'"'
545 << sep << sh->GetDz() << sep << sh->GetTheta() << sep << sh->GetPhi()
546 << sep << sh->GetH1() << sep << sh->GetBl1() << sep << sh->GetTl1() << sep << sh->GetAlpha1()
547 << sep << sh->GetH2() << sep << sh->GetBl2() << sep << sh->GetTl2() << sep << sh->GetAlpha2()
550 else if (cl == TGeoHype::Class()) {
551 const TGeoHype* sh = (
const TGeoHype*) shape;
552 log <<
"\t Solid " << name <<
"(new " << cl->GetName() <<
"(\"" << sh->GetName() <<
'"'
553 << sep << sh->GetRmin() << sep << sh->GetRmax() << sep << sh->GetDz()
554 << sep << sh->GetStIn() << sep << sh->GetStOut()
557 else if (cl == TGeoPgon::Class()) {
558 const TGeoPgon* sh = (
const TGeoPgon*) shape;
559 log <<
"double params[] = {" << sh->GetPhi1() << sep << sh->GetDphi() << sep
560 << sh->GetNedges() << sep << sh->GetNz();
561 for(
int i=0, n=sh->GetNz(); i<n; ++i)
562 log << sep << sh->GetZ(i) << sep << sh->GetRmin(i) << sep << sh->GetRmax(i);
563 log <<
"};" << newline
564 <<
"\t Solid " << name <<
" = Solid(new " << cl->GetName() <<
"(params));" << newline
565 << name <<
"->SetName(\"" << sh->GetName() <<
"\");" << newline;
567 else if (cl == TGeoPcon::Class()) {
568 const TGeoPcon* sh = (
const TGeoPcon*) shape;
569 log <<
"double params[] = {" << sh->GetPhi1() << sep << sh->GetDphi() << sep << sh->GetNz();
570 for(
int i=0, n=sh->GetNz(); i<n; ++i)
571 log << sep << sh->GetZ(i) << sep << sh->GetRmin(i) << sep << sh->GetRmax(i);
572 log <<
"};" << newline
573 <<
"\t Solid " << name <<
" = Solid(new " << cl->GetName() <<
"(params));" << newline
574 << name <<
"->SetName(\"" << sh->GetName() <<
"\");" << newline;
576 else if (cl == TGeoCone::Class()) {
577 const TGeoCone* sh = (
const TGeoCone*) shape;
578 log <<
"\t Solid " << name <<
"(new " << cl->GetName() <<
"(\"" << sh->GetName() <<
'"'
579 << sep << sh->GetDz()
580 << sep << sh->GetRmin1() << sep << sh->GetRmax1()
581 << sep << sh->GetRmin2() << sep << sh->GetRmax2()
584 else if (cl == TGeoConeSeg::Class()) {
586 log <<
"\t Solid " << name <<
"(new " << cl->GetName() <<
"(\"" << sh->GetName() <<
'"'
587 << sep << sh->GetDz()
588 << sep << sh->GetRmin1() << sep << sh->GetRmax1()
589 << sep << sh->GetRmin2() << sep << sh->GetRmax2()
590 << sep << sh->GetPhi1() << sep << sh->GetPhi2()
593 else if (cl == TGeoParaboloid::Class()) {
594 const TGeoParaboloid* sh = (
const TGeoParaboloid*) shape;
595 log <<
"\t Solid " << name <<
"(new " << cl->GetName() <<
"(\"" << sh->GetName() <<
'"'
596 << sep << sh->GetRlo() << sep << sh->GetRhi() << sep << sh->GetDz() <<
"));" << newline;
598 else if (cl == TGeoSphere::Class()) {
599 const TGeoSphere* sh = (
const TGeoSphere*) shape;
600 log <<
"\t Solid " << name <<
"(new " << cl->GetName() <<
"(\"" << sh->GetName() <<
'"'
601 << sep << sh->GetRmin() << sep << sh->GetRmax()
602 << sep << sh->GetPhi1() << sep << sh->GetPhi2()
603 << sep << sh->GetTheta1() << sep << sh->GetTheta2()
606 else if (cl == TGeoTorus::Class()) {
607 const TGeoTorus* sh = (
const TGeoTorus*) shape;
608 log <<
"\t Solid " << name <<
"(new " << cl->GetName() <<
"(\"" << sh->GetName() <<
'"'
609 << sep << sh->GetRmin() << sep << sh->GetRmax() << sep << sh->GetR()
610 << sep << sh->GetPhi1() << sep << sh->GetDphi()
613 else if (cl == TGeoArb8::Class()) {
614 TGeoArb8* sh = (TGeoArb8*) shape;
615 const Double_t*
v = sh->GetVertices();
616 log <<
"double vertices[] = {";
617 for(
int i=0; i<8; ++i,
v+=2)
618 log <<
v[0] << sep <<
v[1] << ((i<7) ?
',' :
' ');
619 log <<
"};" << newline
620 <<
"\t Solid " << name <<
" = Solid(new " << cl->GetName()
621 <<
"(\"" << sh->GetName() <<
"\"" << sep << sh->GetDz() << sep <<
"vertices);" << newline;
623 else if (cl == TGeoXtru::Class()) {
625 const TGeoXtru* sh = (
const TGeoXtru*) shape;
626 std::vector<double> pars = sol.dimensions();
627 log <<
"double param[] = {" << newline;
628 for(
size_t i=0; i < pars.size(); ++i )
629 log << pars[i] << ((i+1<pars.size()) ?
',' :
' ');
630 log <<
"};" << newline
631 <<
"\t Solid " << name <<
" = Solid(new " << cl->GetName() <<
"(param));" << newline
632 <<
"\t " << name <<
"->SetName(\"" << sh->GetName() <<
"\");" << newline;
634 else if (shape->IsA() == TGeoCompositeShape::Class()) {
635 const TGeoCompositeShape* sh = (
const TGeoCompositeShape*) shape;
636 const TGeoBoolNode*
boolean = sh->GetBoolNode();
637 const TGeoShape* left =
boolean->GetLeftShape();
638 const TGeoShape* right =
boolean->GetRightShape();
639 TGeoBoolNode::EGeoBoolType oper =
boolean->GetBooleanOperator();
640 handleSolid(log, left);
641 handleSolid(log, right);
642 handleMatrix(log, boolean->GetRightMatrix());
643 if (oper == TGeoBoolNode::kGeoSubtraction)
644 log <<
"\t TGeoSubtraction* boolean" << pointer(shape) <<
" = new TGeoSubtraction";
645 else if (oper == TGeoBoolNode::kGeoUnion)
646 log <<
"\t TGeoUnion* boolean" << pointer(shape) <<
" = new TGeoUnion";
647 else if (oper == TGeoBoolNode::kGeoIntersection)
648 log <<
"\t TGeoIntersection* boolean" << pointer(shape) <<
" = new TGeoIntersection";
649 log <<
"(shapes[" << pointer(left) <<
"], "
650 <<
" shapes[" << pointer(right) <<
"], 0, ";
651 log <<
" matrices[" << pointer(boolean->GetRightMatrix()) <<
"]);" << newline;
652 log <<
"\t Solid " << name <<
" = Solid(new TGeoCompositeShape(\""
653 << sh->GetName() <<
"\", boolean" << pointer(shape) <<
"));" << newline;
656 except(
"CxxRootGenerator",
"++ Unknown shape transformation request: %s", shape->IsA()->GetName());
658 log <<
"\t shapes[" << pointer(shape) <<
"] = " << name <<
";" << newline
664 static long generate_cxx(
Detector& description,
int argc,
char** argv) {
666 Actor actor(description);
668 for(
int i=0; i<argc; ++i) {
669 char c = ::tolower(argv[i][0]);
670 if ( c ==
'-' ) { c = ::tolower(argv[i][1]); }
671 if ( c ==
'-' ) { c = ::tolower(argv[i][1]); }
672 if ( c ==
'o' && i+1<argc )
674 else if ( c ==
'f' && i+1<argc )
675 actor.function = argv[++i];
677 actor.dump_vis =
true;
679 actor.dump_structure =
true;
682 "Usage: -plugin DD4hep_CxxRootGenerator -arg [-arg] \n"
683 " -output <string> Set output file for generated code. Default: stdout \n"
684 " -help Show thi help message \n"
685 "\tArguments given: " << arguments(argc,argv) << std::endl << std::flush;
689 std::unique_ptr<std::ofstream> out;
690 std::ostream* os = &std::cout;
691 if ( !output.empty() ) {
693 out.reset(
new std::ofstream(path.c_str()));
694 if ( !out->good() ) {
696 except(
"CxxRootGenerator",
697 "++ Failed to open output files: %s [%s]",
698 path.c_str(), ::strerror(errno));
701 actor.function = path.filename();
702 if ( actor.function.rfind(
'.') != std::string::npos )
703 actor.function = actor.function.substr(0, actor.function.rfind(
'.'));
704 printout(INFO,
"CxxRootGenerator",
705 "++ Dump generated code to output files: %s [function: %s()]",
706 path.c_str(), actor.function.c_str());
708 else if ( actor.function.empty() ) {
709 actor.function =
"run_geometry";
713 actor.handleHeader(*os);
714 actor.handlePlacement(*os, 0, pv.
ptr());
716 actor.handleTrailer(*os);