29 #include "TGeoManager.h"
30 #include "TGeoMatrix.h"
31 #include "TGeoCompositeShape.h"
35 #include "TGeoBoolNode.h"
39 #include "TGeoTorus.h"
43 #include "Math/GenVector/RotationX.h"
44 #include "Math/GenVector/RotationZ.h"
46 #include "CLHEP/Units/GlobalSystemOfUnits.h"
91 : m_level(
pset.getUntrackedParameter<
int>(
"level")),
92 m_verbose(
pset.getUntrackedParameter<
bool>(
"verbose")),
93 m_fullname(
pset.getUntrackedParameter<
bool>(
"fullName")) {
101 desc.
addUntracked<
int>(
"level", 10)->setComment(
"How deep into the geometry hierarchy should the conversion go.");
103 desc.
addUntracked<
bool>(
"fullName",
true)->setComment(
"use fillname() instead of name() when generating node names");
105 conf.
add(
"TGeoMgrFromDdd", desc);
119 TGeoTranslation
t(iTrans.x() / cm, iTrans.y() / cm, iTrans.z() / cm);
121 return new TGeoCombiTrans(
t,
r);
135 return std::unique_ptr<TGeoManager>();
138 TGeoManager* geo_mgr =
new TGeoManager(
"cmsGeo",
"CMS Detector");
140 if (gGeoIdentity ==
nullptr) {
141 gGeoIdentity =
new TGeoIdentity(
"Identity");
144 std::cout <<
"about to initialize the DDCompactView walker"
145 <<
" with a root node " << viewH->
root() << std::endl;
147 auto walker = viewH->
walker();
148 auto info = walker.current();
153 if (!walker.firstChild()) {
154 return std::unique_ptr<TGeoManager>();
160 if (top ==
nullptr) {
161 return std::unique_ptr<TGeoManager>();
164 geo_mgr->SetTopVolume(top);
166 top->SetVisibility(kFALSE);
167 top->SetLineColor(kBlue);
169 std::vector<TGeoVolume*> parentStack;
170 parentStack.push_back(top);
173 auto info = walker.current();
176 for (
unsigned int i = 0;
i < parentStack.size(); ++
i) {
186 if (
nullptr !=
child &&
info.second !=
nullptr) {
187 parentStack.back()->AddNode(
189 child->SetLineColor(kBlue);
191 if (
info.second ==
nullptr) {
195 if (
nullptr ==
child || childAlreadyExists ||
m_level ==
int(parentStack.size())) {
196 if (
nullptr !=
child) {
197 child->SetLineColor(kRed);
200 if (!walker.nextSibling()) {
201 while (walker.parent()) {
202 parentStack.pop_back();
203 if (walker.nextSibling()) {
209 if (walker.firstChild()) {
210 parentStack.push_back(
child);
212 if (!walker.nextSibling()) {
213 while (walker.parent()) {
214 parentStack.pop_back();
215 if (walker.nextSibling()) {
222 }
while (!parentStack.empty());
224 geo_mgr->CloseGeometry();
226 geo_mgr->DefaultColors();
233 return std::unique_ptr<TGeoManager>(geo_mgr);
241 LogDebug(
"TGeoMgrFromDdd::createShape") <<
"with name: " << iName <<
" and solid: " << iSolid;
245 throw cms::Exception(
"TGeoMgrFromDdd::createShape * solid " + iName +
" is not declared * ");
247 throw cms::Exception(
"TGeoMgrFromDdd::createShape * solid " + defined.first->name() +
" is not defined *");
250 if (rSolid ==
nullptr) {
251 const std::vector<double>&
params = iSolid.parameters();
252 switch (iSolid.shape()) {
257 rSolid =
new TGeoConeSeg(iName.c_str(),
268 rSolid =
new TGeoTubeSeg(iName.c_str(),
277 rSolid =
new TGeoCtub(iName.c_str(),
291 rSolid =
new TGeoTrap(iName.c_str(),
305 rSolid =
new TGeoPcon(iName.c_str(),
params[0] / deg,
params[1] / deg, (
params.size() - 2) / 3);
313 for (std::vector<double>::iterator it =
temp.begin() + 3; it !=
temp.end(); ++it) {
316 rSolid->SetDimensions(&(*(
temp.begin())));
320 rSolid =
new TGeoPgon(
329 for (std::vector<double>::iterator it =
temp.begin() + 4; it !=
temp.end(); ++it) {
332 rSolid->SetDimensions(&(*(
temp.begin())));
337 std::vector<double>
x = extrPgon.
xVec();
339 std::vector<double>
y = extrPgon.
yVec();
341 std::vector<double>
z = extrPgon.
zVec();
342 std::vector<double> zx = extrPgon.
zxVec();
343 std::vector<double> zy = extrPgon.
zyVec();
344 std::vector<double> zscale = extrPgon.
zscaleVec();
346 TGeoXtru* mySolid =
new TGeoXtru(
z.size());
347 mySolid->DefinePolygon(
x.size(), &(*
x.begin()), &(*
y.begin()));
349 mySolid->DefineSection(
i,
z[
i] / cm, zx[
i] / cm, zy[
i] / cm, zscale[
i]);
359 double r =
pt.radius();
360 bool atMinusZ =
pt.atMinusZ();
363 bool intersec =
false;
370 double halfOpeningAngle = asin(
x /
std::abs(
r)) / deg;
371 double displacement = 0;
380 h =
pt.y1() <
pt.y2() ?
pt.y2() :
pt.y1();
383 displacement = -
pt.halfZ() -
delta;
384 startPhi = 90. - halfOpeningAngle;
386 displacement =
pt.halfZ() +
delta;
387 startPhi = -90. - halfOpeningAngle;
391 displacement = -
pt.halfZ() +
delta;
392 startPhi = 270. - halfOpeningAngle;
395 displacement =
pt.halfZ() -
delta;
396 startPhi = 90. - halfOpeningAngle;
403 std::unique_ptr<TGeoShape> trap(
404 new TGeoTrd2(
name.c_str(),
pt.x1() / cm,
pt.x2() / cm,
pt.y1() / cm,
pt.y2() / cm,
pt.halfZ() / cm));
406 std::unique_ptr<TGeoShape> tubs(
new TGeoTubeSeg(
name.c_str(),
411 startPhi + halfOpeningAngle * 2.));
413 TGeoSubtraction* sub =
new TGeoSubtraction(
415 rSolid =
new TGeoCompositeShape(iName.c_str(), sub);
417 std::unique_ptr<TGeoShape> box(
new TGeoBBox(1.1 *
x / cm, 1.1 *
h / cm,
sqrt(
r *
r -
x *
x) / cm));
419 TGeoSubtraction* sub =
new TGeoSubtraction(
422 std::unique_ptr<TGeoShape> tubicCap(
new TGeoCompositeShape(iName.c_str(), sub));
424 TGeoUnion* boolS =
new TGeoUnion(
427 rSolid =
new TGeoCompositeShape(iName.c_str(), boolS);
434 rSolid =
new TGeoTorus(iName.c_str(),
445 throw cms::Exception(
"GeomConvert") <<
"conversion to DDBooleanSolid failed";
452 if (
nullptr != left.get() &&
nullptr != right.get()) {
453 TGeoSubtraction* sub =
454 new TGeoSubtraction(left.release(),
458 rSolid =
new TGeoCompositeShape(iName.c_str(), sub);
465 throw cms::Exception(
"GeomConvert") <<
"conversion to DDTruncTubs failed";
467 double rIn(
tt.rIn());
468 double rOut(
tt.rOut());
469 double zHalf(
tt.zHalf());
470 double startPhi(
tt.startPhi());
472 double cutAtStart(
tt.cutAtStart());
473 double cutAtDelta(
tt.cutAtDelta());
474 bool cutInside(
bool(
tt.cutInside()));
478 if (rIn <= 0 || rOut <= 0 || cutAtStart <= 0 || cutAtDelta <= 0) {
479 std::string s =
"TruncTubs " +
name +
": 0 <= rIn,cutAtStart,rOut,cutAtDelta,rOut violated!";
486 if (startPhi != 0.) {
492 double r(cutAtStart);
493 double R(cutAtDelta);
496 std::unique_ptr<TGeoShape> tubs(
497 new TGeoTubeSeg(
name.c_str(), rIn / cm, rOut / cm, zHalf / cm, startPhi,
deltaPhi / deg));
499 double boxX(rOut), boxY(rOut);
502 double boxZ(1.1 * zHalf);
507 double cos_alpha = cath / hypo;
508 double alpha = -acos(cos_alpha);
522 std::unique_ptr<TGeoShape> box(
new TGeoBBox(
name.c_str(), boxX / cm, boxZ / cm, boxY / cm));
524 TGeoTranslation trans(xBox / cm, 0., 0.);
526 TGeoSubtraction* sub =
527 new TGeoSubtraction(tubs.release(), box.release(),
nullptr,
new TGeoCombiTrans(trans,
rot));
529 rSolid =
new TGeoCompositeShape(iName.c_str(), sub);
535 throw cms::Exception(
"GeomConvert") <<
"conversion to DDBooleanSolid failed";
544 if (
nullptr != left.get() &&
nullptr != right.get()) {
545 TGeoUnion* boolS =
new TGeoUnion(left.release(),
549 rSolid =
new TGeoCompositeShape(iName.c_str(), boolS);
556 throw cms::Exception(
"GeomConvert") <<
"conversion to DDBooleanSolid failed";
563 if (
nullptr != left.get() &&
nullptr != right.get()) {
564 TGeoIntersection* boolS =
565 new TGeoIntersection(left.release(),
569 rSolid =
new TGeoCompositeShape(iName.c_str(), boolS);
576 throw cms::Exception(
"GeomConvert") <<
"conversion to DDEllipticalTube failed";
586 if (rSolid ==
nullptr) {
587 std::cerr <<
"COULD NOT MAKE " << iName <<
" of a shape " << iSolid << std::endl;
590 LogDebug(
"TGeoMgrFromDdd::createShape") <<
"solid " << iName <<
" has been created.";
602 if (geo_med ==
nullptr) {
604 geo_med =
new TGeoMedium(mat_name.c_str(), 0, geo_mat);
608 v =
new TGeoVolume(iName.c_str(), solid, geo_med);
619 if (mat ==
nullptr) {
627 mat =
new TGeoMaterial(mat_name.c_str(), iMaterial.
a() * mole /
g, iMaterial.
z(), iMaterial.
density() * cm3 /
g);