File indexing completed on 2025-01-18 09:15:54
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 #include "DD4hep/DD4hepUI.h"
0019 #include "DD4hep/DetFactoryHelper.h"
0020 #include "DD4hep/Detector.h"
0021 #include "DD4hep/DetectorTools.h"
0022 #include "DD4hep/Factories.h"
0023 #include "DD4hep/Memory.h"
0024 #include "DD4hep/Printout.h"
0025 #include "XML/DocumentHandler.h"
0026 #include "XML/Utilities.h"
0027
0028
0029 #include "TGDMLParse.h"
0030 #include "TGDMLWrite.h"
0031 #include "TGeoElement.h"
0032 #include "TGeoManager.h"
0033 #include "TInterpreter.h"
0034 #include "TUri.h"
0035
0036 using namespace std;
0037 using namespace dd4hep;
0038
0039 #if ROOT_VERSION_CODE >= ROOT_VERSION(6, 13, 0)
0040
0041
0042 static Ref_t create_detector(Detector& description, xml_h e, Ref_t ) {
0043 using namespace dd4hep::detail;
0044 xml_det_t x_det = e;
0045 int id = x_det.hasAttr(_U(id)) ? x_det.id() : 0;
0046 xml_dim_t x_pos(x_det.child(_U(position), false));
0047 xml_dim_t x_rot(x_det.child(_U(rotation), false));
0048 xml_dim_t x_gdml(x_det.child(_U(gdmlFile)));
0049 xml_dim_t x_par(x_det.child(_U(parent)));
0050 string name = x_det.nameStr();
0051 string par_nam = x_par.nameStr();
0052 string gdml = x_gdml.attr<string>(_U(ref));
0053 string gdml_physvol = dd4hep::getAttrOrDefault<string>(x_gdml, _Unicode(physvol), "");
0054 DetElement det_parent = description.detector(par_nam);
0055 TGDMLParse parser;
0056 if (!gdml.empty() && gdml[0] == '/') {
0057 TUri uri(gdml.c_str());
0058 gdml = uri.GetRelativePart();
0059 } else {
0060 string path = xml::DocumentHandler::system_path(e, gdml);
0061 TUri uri(path.c_str());
0062 gdml = uri.GetRelativePart();
0063 }
0064 if (!det_parent.isValid()) {
0065 except(name, "+++ Cannot access detector parent: %s", par_nam.c_str());
0066 }
0067 DetElement sdet(name, id);
0068 Volume volume = parser.GDMLReadFile(gdml.c_str());
0069 if (!volume.isValid()) {
0070 except("ROOTGDMLParse", "+++ Failed to parse GDML file:%s", gdml.c_str());
0071 }
0072 volume.import();
0073 printout(INFO, "ROOTGDMLParse", "+++ Attach GDML volume %s", volume.name());
0074 Volume mother = det_parent.volume();
0075 PlacedVolume pv;
0076
0077 if (!gdml_physvol.empty()) {
0078 PlacedVolume node = volume->FindNode(gdml_physvol.c_str());
0079 if (!node.isValid()) {
0080 printout(ERROR, "ROOTGDMLParse", "+++ Invalid gdml placed volume %s", gdml_physvol.c_str());
0081 printout(ERROR, "ROOTGDMLParse", "+++ Valid top-level nodes are:");
0082 volume->PrintNodes();
0083 except("ROOTGDMLParse", "+++ Failed to parse GDML file:%s for node:%s", gdml.c_str(),
0084 gdml_physvol.c_str());
0085 }
0086 volume = node.volume();
0087 }
0088
0089 if (x_pos && x_rot) {
0090 Rotation3D rot(RotationZYX(x_rot.z(), x_rot.y(), x_rot.x()));
0091 Transform3D transform(rot, Position(x_pos.x(), x_pos.y(), x_pos.z()));
0092 pv = mother.placeVolume(volume, transform);
0093 } else if (x_rot) {
0094 Rotation3D rot(RotationZYX(x_rot.z(), x_rot.y(), x_rot.x()));
0095 Transform3D transform(rot, Position(0, 0, 0));
0096 pv = mother.placeVolume(volume, transform);
0097 } else if (x_pos) {
0098 pv = mother.placeVolume(volume, Position(x_pos.x(), x_pos.y(), x_pos.z()));
0099 } else {
0100 pv = mother.placeVolume(volume);
0101 }
0102 volume.setVisAttributes(description, x_det.visStr());
0103 volume.setLimitSet(description, x_det.limitsStr());
0104 volume.setRegion(description, x_det.regionStr());
0105 if (id != 0) {
0106 pv.addPhysVolID("system", id);
0107 }
0108 sdet.setPlacement(pv);
0109 return sdet;
0110 }
0111
0112
0113 DECLARE_DETELEMENT(DD4hep_GdmlDetector, create_detector)
0114
0115 #endif