DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
GeometryWalk.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/Printout.h>
17 #include <DD4hep/Factories.h>
18 #include <DD4hep/IDDescriptor.h>
19 #include <DD4hep/VolumeManager.h>
20 #include <DD4hep/DetectorTools.h>
21 
22 // C/C++ include files
23 #include <stdexcept>
24 #include <algorithm>
25 
26 using namespace dd4hep;
27 
30 
31 namespace {
32 
41  struct GeometryWalk {
43  struct FND {
44  const std::string& test;
45  FND(const std::string& c) : test(c) {}
46  bool operator()(const PlacedVolume::VolIDs::value_type& c) const { return c.first == test; }
47  };
48  VolumeManager m_mgr;
49  DetElement m_det;
50 
52  GeometryWalk(Detector& description, DetElement sdet);
54  virtual ~GeometryWalk() {}
56  void walk(DetElement de, PlacedVolume::VolIDs ids) const;
58  void print(DetElement e, PlacedVolume pv, const PlacedVolume::VolIDs& child_ids) const;
60  static long run(Detector& description,int argc,char** argv);
61  };
62 }
63 
65 
67 GeometryWalk::GeometryWalk(Detector& description, DetElement sdet) : m_det(sdet) {
68  m_mgr = description.volumeManager();
69  if ( !m_det.isValid() ) {
70  std::stringstream err;
71  err << "The subdetector " << m_det.name() << " is not known to the geometry.";
72  printout(INFO,"GeometryWalk",err.str().c_str());
73  throw std::runtime_error(err.str());
74  }
75  walk(m_det,PlacedVolume::VolIDs());
76 }
77 
79 void GeometryWalk::print(DetElement e, PlacedVolume pv, const PlacedVolume::VolIDs& /* child_ids */) const {
80  std::stringstream log;
81  PlacementPath all_nodes;
82  ElementPath det_elts;
83  detail::tools::elementPath(e,det_elts);
84  detail::tools::placementPath(e,all_nodes);
85  std::string elt_path = detail::tools::elementPath(det_elts);
86  std::string node_path = detail::tools::placementPath(all_nodes);
87  log << "Lookup " << std::left << std::setw(32) << pv.name() << " Detector[" << det_elts.size() << "]: " << elt_path;
88  printout(INFO,m_det.name(),log.str());
89  log.str("");
90  log << " " << std::left << std::setw(32) << " " << " Places[" << all_nodes.size() << "]: " << node_path;
91  printout(INFO,m_det.name(),log.str());
92  log.str("");
93  log << " " << std::left << std::setw(32) << " " << " detail::matrix[" << all_nodes.size() << "]: ";
94  for(PlacementPath::const_iterator i=all_nodes.begin(); i!=all_nodes.end(); ++i) {
95  log << (void*)((*i)->GetMatrix()) << " ";
96  if ( i+1 == all_nodes.end() ) log << "( -> " << (*i)->GetName() << ")";
97  }
98  printout(INFO, m_det.name(), log.str());
99 }
100 
102 void GeometryWalk::walk(DetElement e, PlacedVolume::VolIDs ids) const {
103  const _C& children = e.children();
104  PlacedVolume pv = e.placement();
105  PlacedVolume::VolIDs child_ids(ids);
106  print(e,pv,ids);
107  child_ids.insert(child_ids.end(),pv.volIDs().begin(),pv.volIDs().end());
108  for (_C::const_iterator i=children.begin(); i!=children.end(); ++i) {
109  walk((*i).second,child_ids);
110  }
111 }
112 
114 long GeometryWalk::run(Detector& description,int argc,char** argv) {
115  std::cout << "++ Processing plugin....GeometryWalker.." << std::endl;
116  DetElement world = description.world();
117  for(int in=1; in < argc; ++in) {
118  std::string name = argv[in]+1;
119  if ( name == "all" || name == "All" || name == "ALL" ) {
120  const _C& children = world.children();
121  for (_C::const_iterator i=children.begin(); i!=children.end(); ++i) {
122  DetElement sdet = (*i).second;
123  std::cout << "++ Processing subdetector: " << sdet.name() << std::endl;
124  GeometryWalk test(description, sdet);
125  }
126  return 1;
127  }
128  std::cout << "++ Processing subdetector: " << name << std::endl;
129  GeometryWalk test(description, description.detector(name));
130  }
131  return 1;
132 }
133 
134 
135 namespace dd4hep {
136  namespace detail {
137  using ::GeometryWalk;
138  }
139 }
140 DECLARE_APPLY(GeometryWalker,GeometryWalk::run)
dd4hep::DetElement::children
const Children & children() const
Access to the list of children.
Definition: DetElement.cpp:207
dd4hep::Detector::world
virtual DetElement world() const =0
Return reference to the top-most (world) detector element.
dd4hep::Detector::detector
virtual DetElement detector(const std::string &name) const =0
Retrieve a subdetector element by its name from the detector description.
dd4hep::detail::tools::ElementPath
std::vector< DetElement > ElementPath
Definition: DetectorTools.h:38
Detector.h
_C
DetElement::Children _C
Definition: GeometryWalk.cpp:64
dd4hep::PlacedVolume
Handle class holding a placed volume (also called physical volume)
Definition: Volumes.h:173
dd4hep::DetElement::placement
PlacedVolume placement() const
Access to the physical volume of this detector element.
Definition: DetElement.cpp:321
dd4hep::detail::tools::placementPath
std::string placementPath(DetElement element)
Assemble the placement path from a given detector element to the world volume.
Definition: DetectorTools.cpp:276
DECLARE_APPLY
#define DECLARE_APPLY(name, func)
Definition: Factories.h:281
Factories.h
dd4hep::Handle::name
const char * name() const
Access the object name (or "" if not supported by the object)
dd4hep::Detector::volumeManager
virtual VolumeManager volumeManager() const =0
Return handle to the VolumeManager.
VolumeManager.h
dd4hep::DetElement
Handle class describing a detector element.
Definition: DetElement.h:188
dd4hep::PlacedVolumeExtension::VolIDs
Volume ID container.
Definition: Volumes.h:88
dd4hep::VolumeManager
Class to support the retrieval of detector elements and volumes given a valid identifier.
Definition: VolumeManager.h:135
DetectorTools.h
ElementPath
detail::tools::ElementPath ElementPath
Definition: DetectorInterna.cpp:30
dd4hep::detail::tools::elementPath
std::string elementPath(DetElement element)
Assemble the path of a particular detector element.
Definition: DetectorTools.cpp:207
dd4hep::DetElement::Children
std::map< std::string, DetElement > Children
Definition: DetElement.h:206
IDDescriptor.h
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::detail::tools::PlacementPath
std::vector< PlacedVolume > PlacementPath
Definition: DetectorTools.h:39
dd4hep::Detector
The main interface to the dd4hep detector description package.
Definition: Detector.h:90
dd4hep::PlacedVolume::volIDs
const PlacedVolumeExtension::VolIDs & volIDs() const
Access to the volume IDs.
Definition: Volumes.cpp:480
PlacementPath
detail::tools::PlacementPath PlacementPath
Definition: DetectorInterna.cpp:29
Printout.h