DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
dumpdetector.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 //==========================================================================
11 //
12 // Simple program to dump the complete DetElement hierarchy
13 //
14 // Author : F.Gaede, CERN/DESY
15 // Date : 07 Nov 2014
16 //==========================================================================
17 
18 // Framework include files
19 #include "DD4hep/Detector.h"
20 #include "DD4hep/DetType.h"
22 #include "DD4hep/DD4hepUnits.h"
23 
24 #include "DDRec/Surface.h"
25 #include "DDRec/DetectorSurfaces.h"
26 #include "DDRec/DetectorData.h"
27 #include "DDRec/SurfaceHelper.h"
28 
29 // C/C++ include files
30 #include <list>
31 
32 
33 using namespace std ;
34 using namespace dd4hep ;
35 using namespace dd4hep::detail;
36 using namespace dd4hep::rec;
37 
38 
39 //=============================================================================
40 static void printDetectorData( DetElement det ){
41 
42  try{
43  FixedPadSizeTPCData* d = det.extension<FixedPadSizeTPCData>() ;
44  std::cout << *d ;
45  } catch(...){}
46  try{
47  ZPlanarData* d = det.extension<ZPlanarData>() ;
48  std::cout << *d ;
49  } catch(...){}
50  try{
51  ZDiskPetalsData* d = det.extension<ZDiskPetalsData>() ;
52  std::cout << *d ;
53  } catch(...){}
54  try{
55  ConicalSupportData* d = det.extension<ConicalSupportData>() ;
56  std::cout << *d ;
57  } catch(...){}
58  try{
60  std::cout << *d ;
61  } catch(...){}
62 
63 }
64 
65 static void printDetectorSets( std::string name, unsigned int includeFlag, unsigned int excludeFlag=DetType::IGNORE ){
66 
67  Detector& description = Detector::getInstance();
68  const std::vector<DetElement>& dets = DetectorSelector(description).detectors( includeFlag, excludeFlag ) ;
69  std::cout << " " << name ;
70  for(int i=0,N=dets.size();i<N;++i)
71  std::cout << dets[i].name() << ", " ;
72  std::cout << endl ;
73 }
74 
75 
76 //=============================================================================
77 
78 static int invoke_dump_detector(int argc, char** argv ){
79 
80  if( argc < 2 ) {
81  std::cout << " usage: dumpdetector compact.xml [-s]"
82  << " -d : only print DetectorData objects "
83  << " -s : also print surfaces "
84  << std::endl ;
85 
86  exit(1) ;
87  }
88 
89  std::string inFile = argv[1] ;
90 
91 
92  bool printDetData = ( argc>2 && !strcmp( argv[2] , "-d" ) );
93 
94  bool printSurfaces = ( argc>2 && !strcmp( argv[2] , "-s" ) );
95 
96 
97  Detector& description = Detector::getInstance();
98 
99  description.fromCompact( inFile );
100 
101  DetElement world = description.world() ;
102 
103 
104  std::cout << "############################################################################### " << std::endl ;
105 
106  Header h = description.header() ;
107 
108  std::cout << " detector model : " << h.name() << std::endl
109  << " title : " << h.title() << std::endl
110  << " author : " << h.author() << std::endl
111  << " status : " << h.status() << std::endl ;
112 
113 
114  // print a few sets of detectors (mainly to demonstrate the usage of the detector types )
115 
116  printDetectorSets( " barrel trackers : " , ( DetType::TRACKER | DetType::BARREL ) , ( DetType::VERTEX) ) ;
117  printDetectorSets( " endcap trackers : " , ( DetType::TRACKER | DetType::ENDCAP ) , ( DetType::VERTEX) ) ;
118 
119  printDetectorSets( " vertex barrel trackers : " , ( DetType::TRACKER | DetType::BARREL | DetType::VERTEX) ) ;
120  printDetectorSets( " vertex endcap trackers : " , ( DetType::TRACKER | DetType::ENDCAP | DetType::VERTEX) ) ;
121 
122  printDetectorSets( " barrel calorimeters : " , ( DetType::CALORIMETER | DetType::BARREL ) ) ;
123  printDetectorSets( " endcap calorimeters : " , ( DetType::CALORIMETER | DetType::ENDCAP ) ) ;
124 
125  // everything that is not TRACKER or CALORIMETER
126  printDetectorSets( " other detecors : " , ( DetType::IGNORE ) , ( DetType::CALORIMETER | DetType::TRACKER ) ) ;
127 
128 
129  if( printDetData ){
130 
131  dd4hep::Detector::HandleMap dets = description.detectors() ;
132 
133  for( dd4hep::Detector::HandleMap::const_iterator it = dets.begin() ; it != dets.end() ; ++it ){
134 
135  DetElement det = it->second ;
136 
137  std::cout << " ---------------------------- " << det.name() << " ----------------------------- " << std::endl ;
138 
139  DetType type( det.typeFlag() ) ;
140 
141  std::cout << " ------ " << type << std:: endl ;
142 
143  printDetectorData( det ) ;
144 
145  }
146 
147  std::cout << "############################################################################### " << std::endl ;
148 
149  return 0;
150  }
151 
152 
153 
154 
155  dd4hep::Detector::HandleMap sensDet = description.sensitiveDetectors() ;
156 
157 
158  std::cout << "############################################################################### " << std::endl
159  << " sensitive detectors: " << std::endl ;
160 
161  for( dd4hep::Detector::HandleMap::const_iterator it = sensDet.begin() ; it != sensDet.end() ; ++it ){
162 
163  SensitiveDetector sDet = it->second ;
164  std::cout << " " << it->first << " : type = " << sDet.type() << std::endl ;
165  }
166 
167 
168  std::cout << "############################################################################### " << std::endl << std::endl ;
169 
170 
171  //------------------ breadth first tree traversal ---------
172  std::list< DetElement > dets ;
173  std::list< DetElement > daugs ;
174  std::list< DetElement > gdaugs ;
175  daugs.emplace_back( world ) ;
176  while( ! daugs.empty() ) {
177  for( std::list< DetElement >::iterator li=daugs.begin() ; li != daugs.end() ; ++li ){
178  DetElement dau = *li ;
179  DetElement::Children chMap = dau.children() ;
180  for ( DetElement::Children::const_iterator it=chMap.begin() ; it != chMap.end() ; ++it ){
181  DetElement de = (*it).second ;
182  gdaugs.emplace_back( de ) ;
183  }
184  }
185  dets.splice( dets.end() , daugs ) ;
186  daugs.splice( daugs.end() , gdaugs ) ;
187  }
188  //------------------ end tree traversal ---------
189 
190 
191  for ( std::list< DetElement >::const_iterator it=dets.begin() ; it != dets.end() ; ++it ){
192 
193  DetElement de = (*it) ;
194 
195  DetElement mother = de.parent() ;
196  unsigned parentCount = 0 ;
197  while( mother.isValid() ) {
198  mother = mother.parent() ;
199  ++parentCount ;
200  }
201 
202  SurfaceHelper surfMan( de ) ;
203 
204  const SurfaceList& sL = surfMan.surfaceList() ;
205 
206  std::cout << "DetElement: " ;
207 
208  for(unsigned i=0 ; i < parentCount ; ++i ) std::cout << "\t" ;
209 
210  std::cout << de.name() << "[ path: "<< de.placementPath () << "] (id: " << de.id() << ") - sens type : " << description.sensitiveDetector( de.name() ).type() << "\t surfaces : " << ( sL.empty() ? 0 : sL.size() ) << std::endl ;
211 
212 
213  // printDetectorData( de ) ;
214 
215  if( printSurfaces ){
216  for( SurfaceList::const_iterator sit = sL.begin() ; sit != sL.end() ; ++sit ){
217  const ISurface* surf = *sit ;
218  std::cout << " ------------------------- "
219  << " surface: " << *surf << std::endl
220  << " ------------------------- " << std::endl ;
221  }
222  }
223  }
224 
225  std::cout << "############################################################################### " << std::endl << std::endl ;
226 
227  // FixedPadSizeTPCData* tpc = tpcDE.extension<FixedPadSizeTPCData>() ;
228 
229  return 0;
230 }
231 
232 
233 int main(int argc, char** argv ){
234  try {
235  return invoke_dump_detector(argc,argv);
236  }
237  catch(const std::exception& e) {
238  std::cout << "Got uncaught exception: " << e.what() << std::endl;
239  }
240  catch (...) {
241  std::cout << "Got UNKNOWN uncaught exception." << std::endl;
242  }
243  return EINVAL;
244 }
245 
246 //=============================================================================
dd4hep::DetElement::children
const Children & children() const
Access to the list of children.
Definition: DetElement.cpp:207
main
int main(int argc, char **argv)
Definition: dumpdetector.cpp:233
dd4hep::Detector::world
virtual DetElement world() const =0
Return reference to the top-most (world) detector element.
DetectorData.h
dd4hep::rec
Namespace for the reconstruction part of the AIDA detector description toolkit.
Definition: SurfaceInstaller.h:28
dd4hep::DetType
Helper class for encoding sub detector types in a flag word.
Definition: DetType.h:33
dd4hep::SensitiveDetector
Handle class to hold the information of a sensitive detector.
Definition: DetElement.h:44
dd4hep::DetElement::parent
DetElement parent() const
Access to the detector elements's parent.
Definition: DetElement.cpp:239
dd4hep::Header::title
const std::string title() const
Accessor to object title.
Definition: Objects.cpp:78
Detector.h
dd4hep::rec::ISurface
Definition: ISurface.h:39
dd4hep::exception
void exception(const std::string &src, const std::string &msg)
Definition: RootDictionary.h:69
dd4hep::Detector::detectors
virtual const HandleMap & detectors() const =0
Accessor to the map of sub-detectors.
dd4hep::Handle::isValid
bool isValid() const
Check the validity of the object held by the handle.
Definition: Handle.h:128
SurfaceHelper.h
dd4hep::Handle::name
const char * name() const
Access the object name (or "" if not supported by the object)
DetType.h
dd4hep::Header::name
const std::string name() const
Accessor to object name.
Definition: Objects.cpp:68
Surface.h
dd4hep::DetElement
Handle class describing a detector element.
Definition: DetElement.h:188
dd4hep::rec::SurfaceList
Definition: Surface.h:681
dd4hep::detail
DD4hep internal namespace.
Definition: Alignments.h:32
DetectorSelector.h
dd4hep::Detector::sensitiveDetector
virtual SensitiveDetector sensitiveDetector(const std::string &name) const =0
Retrieve a sensitive detector by its name from the detector description.
dd4hep::DetectorSelector::detectors
const Result & detectors(const std::string &type)
Access a set of subdetectors according to the sensitive type.
Definition: DetectorSelector.cpp:21
DetectorSurfaces.h
dd4hep::Header::author
const std::string & author() const
Accessor to object author.
Definition: Objects.cpp:98
dd4hep::DetElement::placementPath
const std::string & placementPath() const
Access to the full path to the placed object.
Definition: DetElement.cpp:85
dd4hep::Detector::sensitiveDetectors
virtual const HandleMap & sensitiveDetectors() const =0
Accessor to the map of sub-detectors.
dd4hep::Header
Handle class describing the basic information about geometry objects as it is defined in Detector.
Definition: Objects.h:158
dd4hep::Detector::fromCompact
virtual void fromCompact(const std::string &fname, DetectorBuildType type=BUILD_DEFAULT)=0
Deprecated call (use fromXML): Read compact geometry description or alignment file.
dd4hep::SensitiveDetector::type
std::string type() const
Access the type of the sensitive detector.
Definition: DetElement.cpp:409
dd4hep::Detector::HandleMap
std::map< std::string, Handle< NamedObject > > HandleMap
Type definition of a map of named handles.
Definition: Detector.h:93
dd4hep::DetElement::Children
std::map< std::string, DetElement > Children
Definition: DetElement.h:206
std
Definition: Plugins.h:30
dd4hep::rec::StructExtension
Definition: DetectorData.h:37
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
det
DetElement::Object * det
Definition: AlignmentsCalculator.cpp:66
dd4hep::Detector
The main interface to the dd4hep detector description package.
Definition: Detector.h:90
dd4hep::DetElement::id
int id() const
Get the detector identifier.
Definition: DetElement.cpp:169
dd4hep::Header::status
const std::string & status() const
Accessor to object status.
Definition: Objects.cpp:108
DD4hepUnits.h
dd4hep::rec::SurfaceHelper
Definition: SurfaceHelper.h:29
dd4hep::Detector::header
virtual Header header() const =0
Accessor to the map of header entries.
dd4hep::DetectorSelector
View on Detector to select detectors according to certain criteria.
Definition: DetectorSelector.h:41