DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
ImportPlainRoot.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 
14 // Framework include files
15 #include <DD4hep/Detector.h>
16 #include <DD4hep/Memory.h>
17 #include <DD4hep/DD4hepUI.h>
18 #include <DD4hep/Factories.h>
19 #include <DD4hep/Printout.h>
20 #include <DD4hep/DetectorData.h>
21 #include <DD4hep/DetectorTools.h>
22 
23 // ROOT includes
24 #include <TInterpreter.h>
25 #include <TGeoElement.h>
26 #include <TGeoManager.h>
27 #include <TFile.h>
28 #include <TUri.h>
29 
30 using namespace std;
31 using namespace dd4hep;
32 
34 
41 static long plain_root_dump(Detector& description, int argc, char** argv) {
42  struct TGeoManip {
43  DetectorData* detector;
44  size_t num_nodes = 0;
45  size_t num_volume_patches = 0;
46  size_t num_placement_patches = 0;
47  int max_level = 9999;
48  bool import = false;
49  PrintLevel printLevel = DEBUG;
50  TGeoManip(DetectorData* dsc, int mx, bool imp, PrintLevel p)
51  : detector(dsc), max_level(mx), import(imp), printLevel(p)
52  {
53  }
54  ~TGeoManip() {
55  printout(INFO, "PlainROOTDump","+++ Scanned a total of %9ld NODES",num_nodes);
56  if ( import ) {
57  printout(INFO,"PlainROOTDump","+++ Scanned a total of %9ld VOLUMES",num_volume_patches);
58  printout(INFO,"PlainROOTDump","+++ Scanned a total of %9ld PLACEMENTS",num_placement_patches);
59  }
60  }
61  void operator()(int lvl, TGeoNode* n) {
62  char fmt[255];
63  int npatch = 0;
64  TGeoVolume* v = n->GetVolume();
65  bool v_ext = false, a_ext = false, p_ext = false;
66  if ( import ) {
67  if ( !v->GetUserExtension() ) {
68  if ( v->IsA() == TGeoVolume::Class() ) {
69  v->SetUserExtension(new Volume::Object());
70  v_ext = true;
71  }
72  else {
73  v->SetUserExtension(new Assembly::Object());
74  a_ext = true;
75  }
76  ++npatch;
77  ++num_volume_patches;
78  }
79  if ( !n->GetUserExtension() ) {
80  n->SetUserExtension(new PlacedVolume::Object());
81  ++num_placement_patches;
82  p_ext = true;
83  ++npatch;
84  }
85  if ( lvl == 0 ) {
86  }
87  }
88  ++num_nodes;
89  if ( lvl <= max_level ) {
90  if ( !import || (import && npatch > 0) ) {
91  ::snprintf(fmt,sizeof(fmt),"%-5d %%-%ds %%s NDau:%%d Ext:%%p Vol:%%s Mother:%%s Ext:%%p Mat:%%s",lvl,lvl);
92  TGeoVolume* mother = n->GetMotherVolume();
93  printout(printLevel,"PlainROOTDump",fmt,"",
94  n->GetName(), n->GetNdaughters(), n->GetUserExtension(),
95  v->GetName(), mother ? mother->GetName() : "-----",
96  v->GetUserExtension(), v->GetMedium()->GetName());
97  if ( import ) {
98  if ( v_ext ) {
99  ::snprintf(fmt,sizeof(fmt),"%-5d %%-%ds --> Adding VOLUME extension object",lvl,lvl);
100  printout(printLevel,"PlainROOTDump",fmt,"");
101  }
102  else if ( a_ext ) {
103  ::snprintf(fmt,sizeof(fmt),"%-5d %%-%ds --> Adding VOLUME ASSEMBLY extension object",lvl,lvl);
104  printout(printLevel,"PlainROOTDump",fmt,"");
105  }
106  else if ( p_ext ) {
107  ::snprintf(fmt,sizeof(fmt),"%-5d %%-%ds --> Adding PLACEMENT extension object",lvl,lvl);
108  printout(printLevel,"PlainROOTDump",fmt,"");
109  }
110  if ( lvl == 0 ) {
111  }
112  }
113  }
114  }
115  for (Int_t idau = 0, ndau = n->GetNdaughters(); idau < ndau; ++idau) {
116  TGeoNode* daughter = n->GetDaughter(idau);
117  this->operator()(lvl+1, daughter);
118  }
119  }
120  };
121  if ( argc > 0 ) {
122  int level = 99999;
123  PrintLevel prt = DEBUG;
124  bool do_import = false;
125  string input, in_obj = "Geometry", air, vacuum;
126  for(int i = 0; i < argc && argv[i]; ++i) {
127  if ( 0 == ::strncmp("-input",argv[i],5) && (i+1)<argc )
128  input = argv[++i];
129  else if ( 0 == ::strncmp("-object",argv[i],5) && (i+1)<argc )
130  in_obj = argv[++i];
131  else if ( 0 == ::strncmp("-air",argv[i],5) && (i+1)<argc )
132  air = argv[++i];
133  else if ( 0 == ::strncmp("-vacuum",argv[i],5) && (i+1)<argc )
134  vacuum = argv[++i];
135  else if ( 0 == ::strncmp("-level",argv[i],5) && (i+1)<argc )
136  level = ::atol(argv[++i]);
137  else if ( 0 == ::strncmp("-print",argv[i],5) && (i+1)<argc )
138  prt = decodePrintLevel(argv[++i]);
139  else if ( 0 == ::strncmp("-import",argv[i],5) )
140  do_import = true;
141  else
142  goto Error;
143  }
144  if ( input.empty() || in_obj.empty() ) {
145  Error:
146  cout <<
147  "Usage: -plugin <name> -arg [-arg] \n"
148  " name: factory name DD4hep_PlainROOT \n"
149  " -input <string> Input file name. \n"
150  " -object <string> Name of geometry object in file. Default: \"Geometry\"\n"
151  "\tArguments given: " << arguments(argc,argv) << endl << flush;
152  ::exit(EINVAL);
153  }
154  printout(INFO,"ImportROOT","+++ Read geometry from GDML file file:%s",input.c_str());
155  DetectorData::unpatchRootStreamer(TGeoVolume::Class());
156  DetectorData::unpatchRootStreamer(TGeoNode::Class());
157  TFile* f = TFile::Open(input.c_str());
158  if ( f && !f->IsZombie() ) {
159  DetectorData* det = dynamic_cast<DetectorData*>(&description);
160  TGeoManager* mgr = (TGeoManager*)f->Get(in_obj.c_str());
161  if ( det && mgr ) {
162  TGeoManip manip(det, level, do_import, prt);
163  DetectorData::patchRootStreamer(TGeoVolume::Class());
164  DetectorData::patchRootStreamer(TGeoNode::Class());
165  det->m_manager = mgr;
166  manip(0, mgr->GetTopNode());
167  det->m_worldVol = mgr->GetTopNode()->GetVolume();
168  if ( !air.empty() ) {
169  description.addConstant(Constant("Air",air));
170  }
171  if ( !vacuum.empty() ) {
172  description.addConstant(Constant("Vacuum",vacuum));
173  }
174  description.init();
175  description.endDocument();
176  detail::deleteObject(f);
177  return 1;
178  }
179  }
180  detail::deleteObject(f);
181  }
182  DetectorData::patchRootStreamer(TGeoVolume::Class());
183  DetectorData::patchRootStreamer(TGeoNode::Class());
184  except("ImportROOT","+++ No input file name given.");
185  return 0;
186 }
187 DECLARE_APPLY(DD4hep_PlainROOT,plain_root_dump)
dd4hep::VolumeExtension
Implementation class extending the ROOT volume (TGeoVolume)
Definition: Volumes.h:310
v
View * v
Definition: MultiView.cpp:28
Detector.h
dd4hep::PlacedVolumeExtension
Implementation class extending the ROOT placed volume.
Definition: Volumes.h:79
DECLARE_APPLY
#define DECLARE_APPLY(name, func)
Definition: Factories.h:281
Factories.h
dd4hep::Detector::addConstant
virtual Detector & addConstant(const Handle< NamedObject > &element)=0
Add a new constant by named reference to the detector description.
dd4hep::Constant
Handle class describing a constant (define) object in description.
Definition: Objects.h:209
dd4hep::Detector::init
virtual void init()=0
Initialize geometry.
DetectorTools.h
dd4hep::Detector::endDocument
virtual void endDocument(bool close_geometry=true)=0
Finalize the geometry.
DetectorData.h
Memory.h
std
Definition: Plugins.h:30
DD4hepUI.h
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
det
DetElement::Object * det
Definition: AlignmentsCalculator.cpp:66
dd4hep::DetectorData
Data implementation class of the Detector interface.
Definition: DetectorData.h:38
dd4hep::Detector
The main interface to the dd4hep detector description package.
Definition: Detector.h:90
Printout.h