21 #include "TGeoShape.h"
23 #include "TGeoParaboloid.h"
26 #include "TGeoSphere.h"
27 #include "TGeoTorus.h"
32 #include "TGeoMatrix.h"
33 #include "TGeoBoolNode.h"
34 #include "TGeoCompositeShape.h"
36 #include "TGeoManager.h"
40 using namespace DD4hep::Geometry;
41 using namespace DD4hep;
48 ostream& m_output = cout;
50 void getAngles(
const Double_t*
m, Double_t &phi, Double_t &theta, Double_t &psi) {
53 if (TMath::Abs(1. - TMath::Abs(m[8])) < 1.
e-9) {
60 phi = TMath::ATan2(m[2], -m[5]);
61 Double_t sphi = TMath::Sin(phi);
62 if (TMath::Abs(sphi) < 1.
e-9)
63 theta = -TMath::ASin(m[5] / TMath::Cos(phi)) *
RAD_2_DEGREE;
74 TGeoShape* shape = vol->GetShape();
75 TGeoMedium* medium = vol->GetMedium();
76 int num = vol->GetNdaughters();
78 m_output <<
"\t\t<volume name=\"" << name <<
"\">" << endl;
79 m_output <<
"\t\t\t<solidref ref=\"" << shape->GetName() <<
"\"/>" << endl;
80 m_output <<
"\t\t\t<materialref ref=\"" << medium->GetName() <<
"\"/>" << endl;
82 m_output <<
"\t\t\t<visref ref=\"" << vis.
name() <<
"\"/>" << endl;
85 for (
int i = 0; i < num; ++i) {
87 TGeoNode* n = vol.
ptr()->GetNode(vol->GetNode(i)->GetName());
88 TGeoVolume*
v = n->GetVolume();
89 TGeoMatrix* m = n->GetMatrix();
90 m_output <<
"\t\t\t<physvol>" << endl;
91 m_output <<
"\t\t\t\t<volumeref ref=\"" << v->GetName() <<
"\"/>" << endl;
93 if (m->IsTranslation()) {
94 m_output <<
"\t\t\t\t<positionref ref=\"" << n->GetName() <<
"_pos\"/>" << endl;
96 if (m->IsRotation()) {
97 m_output <<
"\t\t\t\t<rotationref ref=\"" << n->GetName() <<
"_rot\"/>" << endl;
100 m_output <<
"\t\t\t</physvol>" << endl;
103 m_output <<
"\t\t</volume>" << endl;
110 if (shape->IsA() == TGeoBBox::Class()) {
111 const TGeoBBox*
s = (
const TGeoBBox*) shape;
112 m_output <<
"\t\t<box name=\"" << name <<
"_shape\" x=\"" << s->GetDX() <<
"\" y=\"" << s->GetDY() <<
"\" z=\""
113 << s->GetDZ() <<
"\" lunit=\"cm\"/>" << endl;
115 else if (shape->IsA() == TGeoTube::Class()) {
116 const TGeoTube*
s = (
const TGeoTube*) shape;
117 m_output <<
"\t\t<tube name=\"" << name <<
"_shape\" rmin=\"" << s->GetRmin() <<
"\" rmax=\"" << s->GetRmax() <<
"\" z=\""
118 << s->GetDz() <<
"\" startphi=\"0.0\" deltaphi=\"360.0\" aunit=\"deg\" lunit=\"cm\"/>" << endl;
120 else if (shape->IsA() == TGeoTubeSeg::Class()) {
121 const TGeoTubeSeg*
s = (
const TGeoTubeSeg*) shape;
122 m_output <<
"\t\t<tube name=\"" << name <<
"_shape\" rmin=\"" << s->GetRmin() <<
"\" rmax=\"" << s->GetRmax() <<
"\" z=\""
123 << s->GetDz() <<
"\" startphi=\"" << s->GetPhi1() <<
"\" deltaphi=\"" << s->GetPhi2()
124 <<
"\" aunit=\"deg\" lunit=\"cm\"/>" << endl;
126 else if (shape->IsA() == TGeoTrd1::Class()) {
127 const TGeoTrd1*
s = (
const TGeoTrd1*) shape;
128 m_output <<
"\t\t<tube name=\"" << name <<
"_shape\" x1=\"" << s->GetDx1() <<
"\" x2=\"" << s->GetDx2() <<
"\" y1=\""
129 << s->GetDy() <<
"\" y2=\"" << s->GetDy() <<
"\" z=\"" << s->GetDz() <<
"\" lunit=\"cm\"/>" << endl;
131 else if (shape->IsA() == TGeoTrd2::Class()) {
132 const TGeoTrd2*
s = (
const TGeoTrd2*) shape;
133 m_output <<
"\t\t<tube name=\"" << name <<
"_shape\" x1=\"" << s->GetDx1() <<
"\" x2=\"" << s->GetDx2() <<
"\" y1=\""
134 << s->GetDy1() <<
"\" y2=\"" << s->GetDy2() <<
"\" z=\"" << s->GetDz() <<
"\" lunit=\"cm\"/>" << endl;
136 else if (shape->IsA() == TGeoPgon::Class()) {
137 const TGeoPgon*
s = (
const TGeoPgon*) shape;
138 m_output <<
"\t\t<polyhedra name=\"" << name <<
"_shape\" startphi=\"" << s->GetPhi1() <<
"\" deltaphi=\"" << s->GetDphi()
139 <<
"\" numsides=\"" << s->GetNedges() <<
"\" aunit=\"deg\" lunit=\"cm\">" << endl;
140 for (
int i = 0; i < s->GetNz(); ++i) {
141 m_output <<
"\t\t\t<zplane z=\"" << s->GetZ(i) <<
"\" rmin=\"" << s->GetRmin(i) <<
"\" rmax=\"" << s->GetRmax(i)
142 <<
"\" lunit=\"cm\"/>" << endl;
144 m_output <<
"\t\t</polyhedra>" << endl;
146 else if (shape->IsA() == TGeoPcon::Class()) {
147 const TGeoPcon*
s = (
const TGeoPcon*) shape;
148 m_output <<
"\t\t<polycone name=\"" << name <<
"_shape\" startphi=\"" << s->GetPhi1() <<
"\" deltaphi=\"" << s->GetDphi()
149 <<
"\" aunit=\"deg\" lunit=\"cm\">" << endl;
150 for (
int i = 0; i < s->GetNz(); ++i) {
151 m_output <<
"\t\t\t<zplane z=\"" << s->GetZ(i) <<
"\" rmin=\"" << s->GetRmin(i) <<
"\" rmax=\"" << s->GetRmax(i)
152 <<
"\" lunit=\"cm\"/>" << endl;
154 m_output <<
"\t\t</polycone>" << endl;
156 else if (shape->IsA() == TGeoCompositeShape::Class()) {
158 const TGeoCompositeShape*
s = (
const TGeoCompositeShape*) shape;
159 const TGeoBoolNode*
boolean = s->GetBoolNode();
160 TGeoBoolNode::EGeoBoolType oper =
boolean->GetBooleanOperator();
162 handleSolid(name +
"_left", boolean->GetLeftShape());
163 handleSolid(name +
"_right", boolean->GetRightShape());
165 if (oper == TGeoBoolNode::kGeoSubtraction)
166 m_output <<
"\t\t<subtraction name=\"" << nn <<
"\">" << endl;
167 else if (oper == TGeoBoolNode::kGeoUnion)
168 m_output <<
"\t\t<union name=\"" << nn <<
"\">" << endl;
169 else if (oper == TGeoBoolNode::kGeoIntersection)
170 m_output <<
"\t\t<intersection name=\"" << nn <<
"\">" << endl;
172 m_output <<
"\t\t\t<first ref=\"" << nn <<
"_left\"/>" << endl;
173 m_output <<
"\t\t\t<second ref=\"" << nn <<
"_right\"/>" << endl;
175 handleTransformation(
"", boolean->GetRightMatrix());
178 if (oper == TGeoBoolNode::kGeoSubtraction)
179 m_output <<
"\t\t</subtraction>" << endl;
180 else if (oper == TGeoBoolNode::kGeoUnion)
181 m_output <<
"\t\t</union>" << endl;
182 else if (oper == TGeoBoolNode::kGeoIntersection)
183 m_output <<
"\t\t</intersection>" << endl;
186 cerr <<
"Failed to handle unknwon solid shape:" << shape->IsA()->GetName() << endl;
194 m_output <<
"\t<structure>" << endl;
195 for (VolumeSet::const_iterator i = volset.begin(); i != volset.end(); ++i)
196 handleVolume((*i)->GetName(), *i);
197 m_output <<
"\t</structure>" << endl;
203 if (m->IsTranslation()) {
204 const Double_t* f = m->GetTranslation();
205 m_output << indent <<
"\t\t<position ";
207 m_output <<
"name=\"" << name <<
"_pos\" ";
208 m_output <<
"x=\"" << f[0] <<
"\" " <<
"y=\"" << f[1] <<
"\" " <<
"z=\"" << f[2] <<
"\" unit=\"cm\"/>" << endl;
210 if (m->IsRotation()) {
211 const Double_t* mat = m->GetRotationMatrix();
212 Double_t theta = 0.0, phi = 0.0, psi = 0.0;
213 getAngles(mat, theta, phi, psi);
214 m_output << indent <<
"\t\t<rotation ";
216 m_output <<
"name=\"" << name <<
"_rot\" ";
217 m_output <<
"x=\"" << theta <<
"\" " <<
"y=\"" << psi <<
"\" " <<
"z=\"" << phi <<
"\" unit=\"deg\"/>" << endl;
225 m_output <<
"\t<define>" << endl;
226 for (TransformSet::const_iterator i = trafos.begin(); i != trafos.end(); ++i)
227 handleTransformation((*i).first, (*i).second);
228 m_output <<
"\t</define>" << endl;
233 m_output <<
"\t<solids>" << endl;
234 for (SolidSet::const_iterator i = solids.begin(); i != solids.end(); ++i)
235 handleSolid((*i)->GetName(), *i);
236 m_output <<
"\t</solids>" << endl;
241 m_output <<
"\t<define>" << endl;
242 for (LCDD::HandleMap::const_iterator i = defs.begin(); i != defs.end(); ++i)
243 m_output <<
"\t\t<constant name=\"" << (*i).second->name <<
"\" value=\"" << (*i).second->type <<
"\" />"
245 m_output <<
"\t</define>" << endl;
254 TGeoNode* current = pv.
ptr();
255 TGeoVolume* volume = current->GetVolume();
256 TObjArray* nodes = volume->GetNodes();
257 int num_children = nodes ? nodes->GetEntriesFast() : 0;
261 _path += current->GetName();
262 ::snprintf(fmt,
sizeof(fmt),
"%%4d %%%ds %%7s %%s\n", level * 2 + 5);
263 ::printf(fmt, level,
"",
" ->LV: ", volume->GetName());
264 ::printf(fmt, level,
"",
" ->PV: ", current->GetName());
265 ::printf(fmt, level,
"",
" ->path:",
_path.c_str());
266 if (num_children > 0) {
267 for (
int i = 0; i < num_children; ++i) {
268 TGeoNode* node =
static_cast<TGeoNode*
>(nodes->At(i));
272 _path =
_path.substr(0,
_path.length() - 1 - strlen(current->GetName()));
282 ::snprintf(fmt,
sizeof(fmt),
"%%4d %%%ds %%7s %%s\n", level * 2 + 5);
283 ::printf(fmt, level,
"",
"->path:",
_path.c_str());
285 ::printf(fmt, level,
"",
" ->placement:", parent.
placementPath().c_str());
286 ::printf(fmt, level,
"",
" ->logvol: ", pl->GetVolume()->GetName());
287 ::printf(fmt, level,
"",
" ->shape: ", pl->GetVolume()->GetShape()->GetName());
289 for (DetElement::Children::const_iterator i = children.begin(); i != children.end(); ++i) {
Handle class holding a placed volume (also called physical volume)
std::set< Volume > VolumeSet
const char * name() const
Access the object name (or "" if not supported by the object)
bool isValid() const
Check the validity of the object held by the handle.
virtual void handleSolids(const SolidSet &solids) const
Dump all solids in GDML format to output stream.
static void dumpDetectors(DetElement parent, int level)
virtual void handleDefines(const LCDD::HandleMap &defs) const
Dump all constants in GDML format to output stream.
virtual void * handleTransformation(const std::string &name, const TGeoMatrix *matrix) const
Dump single volume transformation in GDML format to output stream.
std::set< TGeoShape * > SolidSet
const Children & children() const
Access to the list of children.
VisAttr visAttributes() const
Access the visualisation attributes.
T * ptr() const
Access to the held object.
std::vector< std::pair< std::string, TGeoMatrix * > > TransformSet
Handle class holding a placed volume (also called physical volume)
virtual void * handleSolid(const std::string &name, const TGeoShape *volume) const
Dump solid in GDML format to output stream.
virtual void handleTransformations(const TransformSet &trafos) const
Dump Transformations in GDML format to output stream.
virtual void * handleVolume(const std::string &name, Volume volume) const
Dump logical volume in GDML format to output stream.
void create(DetElement top)
Main entry point: create required object(s)
PlacedVolume placement() const
Access to the physical volume of this detector element.
Handle class describing visualization attributes.
Handle class describing a detector element.
void handleVisualisation(const LCDD::HandleMap &vis) const
Dump all visualisation specs in LCDD format to output stream.
virtual void handleStructure(const VolumeSet &volset) const
Dump structure information in GDML format to output stream.
std::map< std::string, DetElement > Children
std::map< std::string, NamedHandle > HandleMap
static void dumpStructure(PlacedVolume pv, int level)
const std::string & placementPath() const
Access to the full path to the placed object.