MarlinMT  0.1.0
DD4hepGeometry.cc
Go to the documentation of this file.
1 
2 // -- marlinmt headers
5 
6 // -- dd4hep headers
7 #include "DD4hep/Detector.h"
8 #include "DD4hep/DetType.h"
9 #include "DD4hep/DetectorSelector.h"
10 #include "DD4hep/DD4hepUnits.h"
11 #include "DDRec/Surface.h"
12 #include "DDRec/DetectorSurfaces.h"
13 #include "DDRec/DetectorData.h"
14 #include "DDRec/SurfaceHelper.h"
15 
16 using namespace dd4hep ;
17 using namespace dd4hep::detail ;
18 using namespace dd4hep::rec ;
19 
20 namespace marlinmt {
21 
26  class DD4hepGeometry : public GeometryPlugin {
27  public:
28  DD4hepGeometry(const DD4hepGeometry &) = delete ;
29  DD4hepGeometry& operator=(const DD4hepGeometry &) = delete ;
30 
31  public:
32  DD4hepGeometry() ;
33 
34  protected:
35  void loadGeometry() ;
36  const void *handle() const ;
37  void destroy() ;
38  std::type_index typeIndex() const ;
39  void dumpGeometry() const ;
40 
41  private:
42  void printDetectorData( dd4hep::DetElement det ) const ;
43  void printDetectorSets( const std::string &name, unsigned int includeFlag, unsigned int excludeFlag ) const ;
44 
45  private:
46  StringParameter _compactFile {*this, "CompactFile",
47  "The DD4hep geometry compact XML file" } ;
48 
49  BoolParameter _dumpDetectorData {*this, "DumpDetectorData",
50  "Whether to dump detector data while dumping the geometry", false } ;
51 
52  BoolParameter _dumpDetectorSurfaces {*this, "DumpDetectorSurfaces",
53  "Whether to dump surfaces while dumping the geometry", false } ;
54  };
55 
56  //--------------------------------------------------------------------------
57  //--------------------------------------------------------------------------
58 
59  DD4hepGeometry::DD4hepGeometry() :
60  GeometryPlugin("DD4hep") {
61  setDescription( "DD4hep geometry plugin. Use geoMgr->geometry<dd4hep::Detector>() to access the geometry" ) ;
62  }
63 
64  //--------------------------------------------------------------------------
65 
67  dd4hep::Detector& theDetector = dd4hep::Detector::getInstance() ;
68  theDetector.fromCompact( _compactFile ) ;
69  }
70 
71  //--------------------------------------------------------------------------
72 
73  const void *DD4hepGeometry::handle() const {
74  return &dd4hep::Detector::getInstance() ;
75  }
76 
77  //--------------------------------------------------------------------------
78 
80  dd4hep::Detector::getInstance().destroyInstance() ;
81  }
82 
83  //--------------------------------------------------------------------------
84 
85  std::type_index DD4hepGeometry::typeIndex() const {
86  return std::type_index( typeid( dd4hep::Detector ) ) ;
87  }
88 
89  //--------------------------------------------------------------------------
90 
91  void DD4hepGeometry::printDetectorData( DetElement det ) const {
92  try {
93  FixedPadSizeTPCData* d = det.extension<FixedPadSizeTPCData>() ;
94  _logger->log<MESSAGE>() << *d ;
95  }
96  catch(...){}
97  try {
98  ZPlanarData* d = det.extension<ZPlanarData>() ;
99  _logger->log<MESSAGE>() << *d ;
100  }
101  catch(...){}
102  try {
103  ZDiskPetalsData* d = det.extension<ZDiskPetalsData>() ;
104  _logger->log<MESSAGE>() << *d ;
105  }
106  catch(...){}
107  try {
108  ConicalSupportData* d = det.extension<ConicalSupportData>() ;
109  _logger->log<MESSAGE>() << *d ;
110  }
111  catch(...){}
112  try {
113  LayeredCalorimeterData* d = det.extension<LayeredCalorimeterData>() ;
114  _logger->log<MESSAGE>() << *d ;
115  }
116  catch(...){}
117  }
118 
119  //--------------------------------------------------------------------------
120 
121  void DD4hepGeometry::printDetectorSets( const std::string &name, unsigned int includeFlag, unsigned int excludeFlag ) const {
122  Detector& description = Detector::getInstance();
123  const std::vector<DetElement>& dets = DetectorSelector(description).detectors( includeFlag, excludeFlag ) ;
124  _logger->log<MESSAGE>() << " " << name ;
125  for(int i=0,N=dets.size();i<N;++i) {
126  _logger->log<MESSAGE>() << dets[i].name() << ", " ;
127  }
128  _logger->log<MESSAGE>() << std::endl ;
129  }
130 
131  //--------------------------------------------------------------------------
132 
134  Detector& description = Detector::getInstance();
135  _logger->log<MESSAGE>() << "############################################################################### " << std::endl ;
136  Header h = description.header() ;
137  _logger->log<MESSAGE>()
138  << " detector model : " << h.name() << std::endl
139  << " title : " << h.title() << std::endl
140  << " author : " << h.author() << std::endl
141  << " status : " << h.status() << std::endl ;
142  // print a few sets of detectors (mainly to demonstrate the usage of the detector types )
143  printDetectorSets( " barrel trackers : " , ( DetType::TRACKER | DetType::BARREL ) , ( DetType::VERTEX) ) ;
144  printDetectorSets( " endcap trackers : " , ( DetType::TRACKER | DetType::ENDCAP ) , ( DetType::VERTEX) ) ;
145  printDetectorSets( " vertex barrel trackers : " , ( DetType::TRACKER | DetType::BARREL | DetType::VERTEX), DetType::IGNORE ) ;
146  printDetectorSets( " vertex endcap trackers : " , ( DetType::TRACKER | DetType::ENDCAP | DetType::VERTEX), DetType::IGNORE ) ;
147  printDetectorSets( " barrel calorimeters : " , ( DetType::CALORIMETER | DetType::BARREL ), DetType::IGNORE ) ;
148  printDetectorSets( " endcap calorimeters : " , ( DetType::CALORIMETER | DetType::ENDCAP ), DetType::IGNORE ) ;
149  // everything that is not TRACKER or CALORIMETER
150  printDetectorSets( " other detecors : " , ( DetType::IGNORE ) , ( DetType::CALORIMETER | DetType::TRACKER ) ) ;
151  // dump detector data
152  if( _dumpDetectorData ) {
153  _logger->log<MESSAGE>() << "############################################################################### " << std::endl ;
154  dd4hep::Detector::HandleMap dets = description.detectors() ;
155  for( dd4hep::Detector::HandleMap::const_iterator it = dets.begin() ; it != dets.end() ; ++it ) {
156  DetElement det = it->second ;
157  _logger->log<MESSAGE>() << " ---------------------------- " << det.name() << " ----------------------------- " << std::endl ;
158  DetType type( det.typeFlag() ) ;
159  _logger->log<MESSAGE>() << " ------ " << type << std:: endl ;
160  printDetectorData( det ) ;
161  }
162  _logger->log<MESSAGE>() << "############################################################################### " << std::endl ;
163  }
164  dd4hep::Detector::HandleMap sensDet = description.sensitiveDetectors() ;
165  _logger->log<MESSAGE>() << "############################################################################### " << std::endl ;
166  _logger->log<MESSAGE>() << " sensitive detectors: " << std::endl ;
167  for( dd4hep::Detector::HandleMap::const_iterator it = sensDet.begin() ; it != sensDet.end() ; ++it ){
168  SensitiveDetector sDet = it->second ;
169  _logger->log<MESSAGE>() << " " << it->first << " : type = " << sDet.type() << std::endl ;
170  }
171  std::cout << "############################################################################### " << std::endl << std::endl ;
172  DetElement world = description.world() ;
173  //------------------ breadth first tree traversal ---------
174  std::list< DetElement > dets ;
175  std::list< DetElement > daugs ;
176  std::list< DetElement > gdaugs ;
177  daugs.push_back( world ) ;
178  while( ! daugs.empty() ) {
179  for( std::list< DetElement >::iterator li=daugs.begin() ; li != daugs.end() ; ++li ){
180  DetElement dau = *li ;
181  DetElement::Children chMap = dau.children() ;
182  for ( DetElement::Children::const_iterator it=chMap.begin() ; it != chMap.end() ; ++it ){
183  DetElement de = (*it).second ;
184  gdaugs.push_back( de ) ;
185  }
186  }
187  dets.splice( dets.end() , daugs ) ;
188  daugs.splice( daugs.end() , gdaugs ) ;
189  }
190  //------------------ end tree traversal ---------
191 
192 
193  for ( std::list< DetElement >::const_iterator it=dets.begin() ; it != dets.end() ; ++it ) {
194  DetElement de = (*it) ;
195  DetElement mother = de.parent() ;
196  unsigned parentCount = 0 ;
197  while( mother.isValid() ) {
198  mother = mother.parent() ;
199  ++parentCount ;
200  }
201  SurfaceHelper surfMan( de ) ;
202  const SurfaceList& sL = surfMan.surfaceList() ;
203  _logger->log<MESSAGE>() << "DetElement: " ;
204  for(unsigned i=0 ; i < parentCount ; ++i ) {
205  _logger->log<MESSAGE>() << "\t" ;
206  }
207  _logger->log<MESSAGE>()
208  << de.name()
209  << "[ path: "<< de.placementPath () << "] (id: " << de.id() << ") - sens type : "
210  << description.sensitiveDetector( de.name() ).type()
211  << "\t surfaces : " << ( sL.empty() ? 0 : sL.size() )
212  << std::endl ;
213  if( _dumpDetectorSurfaces ) {
214  for( SurfaceList::const_iterator sit = sL.begin() ; sit != sL.end() ; ++sit ) {
215  const ISurface* surf = *sit ;
216  _logger->log<MESSAGE>() << " ------------------------- "
217  << " surface: " << *surf << std::endl
218  << " ------------------------- " << std::endl ;
219  }
220  }
221  }
222  _logger->log<MESSAGE>() << "############################################################################### " << std::endl << std::endl ;
223  }
224 
226 
227 } // namespace marlinmt
const std::string & name() const
Get the component name.
Definition: Component.cc:23
#define MARLINMT_DECLARE_GEOMETRY(Class)
Definition: PluginManager.h:40
BoolParameter _dumpDetectorData
LoggerPtr _logger
The logger instance.
Definition: Component.h:173
DD4hepGeometry class Responsible for loading DD4hep geometry in MarlinMT.
const std::string & type() const
Get the component name.
Definition: Component.cc:17
void setDescription(const std::string &desc)
Set the component description.
Definition: Component.cc:41
const std::string & description() const
Get the component description.
Definition: Component.cc:35
void printDetectorSets(const std::string &name, unsigned int includeFlag, unsigned int excludeFlag) const
void dumpGeometry() const
Dump the geometry in the console.
const void * handle() const
Get a handle on the geometry instance.
std::type_index typeIndex() const
Get a type index object from the geometry handle.
BoolParameter _dumpDetectorSurfaces
void printDetectorData(dd4hep::DetElement det) const
GeometryPlugin class Responsible for loading geometry in Marlin and providing access to it through th...
void loadGeometry()
Load the geometry.
StringParameter _compactFile
void destroy()
Cleanup geometry.