DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
GeoHandler.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/GeoHandler.h>
18 
19 // ROOT includes
20 #include <TGeoManager.h>
21 #include <TGeoCompositeShape.h>
22 #include <TGeoBoolNode.h>
23 #include <TClass.h>
24 
25 // C/C++ include files
26 #include <algorithm>
27 #include <iostream>
28 
29 using namespace dd4hep;
30 
31 namespace {
32 
33  void collectSolid(detail::GeoHandler::GeometryInfo& geo,
34  const std::string& name,
35  const std::string& node,
36  TGeoShape* shape,
37  TGeoMatrix* matrix)
38  {
39  if ( 0 == ::strncmp(shape->GetName(), "TGeo", 4) ) {
40  shape->SetName(name.c_str());
41  }
42  if ( shape->IsA() == TGeoCompositeShape::Class() ) {
43  const TGeoCompositeShape* s = (const TGeoCompositeShape*) shape;
44  const TGeoBoolNode* boolean = s->GetBoolNode();
45  collectSolid(geo, name + "_left", name + "_left", boolean->GetLeftShape(), boolean->GetLeftMatrix());
46  collectSolid(geo, name + "_right", name + "_right", boolean->GetRightShape(), boolean->GetRightMatrix());
47  }
48  if(geo.solid_set.emplace(shape).second) {
49  geo.solids.push_back(shape);
50  }
51  geo.trafos.emplace_back(node, matrix);
52  }
53 }
54 
57  m_data = new std::map<int, std::vector<const TGeoNode*> >();
58  m_set_data = new std::map<int, std::set<const TGeoNode*> >();
59 }
60 
62 detail::GeoHandler::GeoHandler(std::map<int, std::vector<const TGeoNode*> >* ptr,
63  std::map<int, std::set<const TGeoNode*> >* ptr_set,
64  std::map<const TGeoNode*, std::vector<TGeoNode*> >* daus)
65  : m_data(ptr), m_set_data(ptr_set), m_daughters(daus)
66 {
67 }
68 
71  if (m_data)
72  delete m_data;
73  if (m_set_data)
74  delete m_set_data;
75 
76  m_data = nullptr;
77  m_set_data = nullptr;
78 }
79 
80 std::map<int, std::vector<const TGeoNode*> >* detail::GeoHandler::release() {
82  std::map<int, std::vector<const TGeoNode*> >* d = m_data;
83  m_data = nullptr;
84 
88  delete m_set_data;
89  m_set_data = nullptr;
90 
91  return d;
92 }
93 
96  bool old = m_propagateRegions;
97  m_propagateRegions = value;
98  return old;
99 }
100 
102  DetElement par = element.parent();
103  TGeoNode* par_node = par.isValid() ? par.placement().ptr() : nullptr;
104  m_data->clear();
105  m_set_data->clear();
106  return i_collect(par_node, element.placement().ptr(), 0, Region(), LimitSet());
107 }
108 
110  DetElement par = element.parent();
111  TGeoNode* par_node = par.isValid() ? par.placement().ptr() : nullptr;
112  m_data->clear();
113  m_set_data->clear();
114  i_collect(par_node, element.placement().ptr(), 0, Region(), LimitSet());
115  for ( auto i = m_data->rbegin(); i != m_data->rend(); ++i ) {
116  const auto& mapped = (*i).second;
117  for ( const TGeoNode* n : mapped ) {
118  TGeoVolume* v = n->GetVolume();
119  if ( v ) {
120  Material mat(v->GetMedium());
121  Volume vol(v);
122  // Note : assemblies and the world do not have a real volume nor a material
123  if ( info.volumeSet.find(vol) == info.volumeSet.end() ) {
124  info.volumeSet.emplace(vol);
125  info.volumes.emplace_back(vol);
126  }
127  if ( mat.isValid() )
128  info.materials.emplace(mat);
129  if (dynamic_cast<Volume::Object*>(v)) {
130  VisAttr vis = vol.visAttributes();
131  //Region reg = vol.region();
132  //LimitSet lim = vol.limitSet();
133  //SensitiveDetector det = vol.sensitiveDetector();
134 
135  if (vis.isValid())
136  info.vis.emplace(vis);
137  //if ( lim.isValid() ) info.limits[lim.ptr()].emplace(v);
138  //if ( reg.isValid() ) info.regions[reg.ptr()].emplace(v);
139  //if ( det.isValid() ) info.sensitives[det.ptr()].emplace(v);
140  }
141  collectSolid(info, v->GetName(), n->GetName(), v->GetShape(), n->GetMatrix());
142  }
143  }
144  }
145  return *this;
146 }
147 
148 detail::GeoHandler& detail::GeoHandler::i_collect(const TGeoNode* /* parent */,
149  const TGeoNode* current,
150  int level, Region rg, LimitSet ls)
151 {
152  TGeoVolume* vol = current->GetVolume();
153  TObjArray* nodes = vol->GetNodes();
154  Volume volume = vol;
155  Region region = volume.region();
156  LimitSet limits = volume.limitSet();
157 
158  if ( m_propagateRegions ) {
159  if ( !region.isValid() && rg.isValid() ) {
160  region = rg;
161  volume.setRegion(region);
162  }
163  if ( !limits.isValid() && ls.isValid() ) {
164  limits = ls;
165  volume.setLimitSet(limits);
166  }
167  }
170  if ( (*m_set_data)[level].emplace(current).second ) {
171  (*m_data)[level].push_back(current);
172  }
173  int num = nodes ? nodes->GetEntriesFast() : 0;
174  for (int i = 0; i < num; ++i)
175  i_collect(current, (TGeoNode*)nodes->At(i), level + 1, region, limits);
177  if ( m_daughters && m_daughters->find(current) == m_daughters->end() ) {
178  auto [idau,success] = m_daughters->emplace(current, std::vector<TGeoNode*>());
179  for (int i = 0; i < num; ++i)
180  idau->second.push_back((TGeoNode*)nodes->At(i));
181  }
182  return *this;
183 }
184 
187  m_data = GeoHandler().collect(e).release();
188 }
189 
192  GeoHandler h;
193  h.setPropagateRegions(propagate);
194  m_data = h.collect(e).release();
195 }
196 
199  if (m_data)
200  delete m_data;
201  m_data = 0;
202 }
203 
206  return *this;
207 }
208 
dd4hep::Volume::setLimitSet
const Volume & setLimitSet(const Detector &description, const std::string &name) const
Set the limits to the volume. Note: If the name string is empty, the action is ignored.
Definition: Volumes.cpp:1261
dd4hep::VolumeExtension
Implementation class extending the ROOT volume (TGeoVolume)
Definition: Volumes.h:310
v
View * v
Definition: MultiView.cpp:28
dd4hep::DetElement::parent
DetElement parent() const
Access to the detector elements's parent.
Definition: DetElement.cpp:239
Detector.h
dd4hep::info
std::size_t info(const std::string &src, const std::string &msg)
Definition: RootDictionary.h:65
dd4hep::detail::GeoHandler::i_collect
GeoHandler & i_collect(const TGeoNode *parent, const TGeoNode *node, int level, Region rg, LimitSet ls)
Internal helper to collect geometry information from traversal.
Definition: GeoHandler.cpp:148
dd4hep::VisAttr
Handle class describing visualization attributes.
Definition: Objects.h:324
dd4hep::detail::GeoHandlerTypes::GeometryInfo
Data container to store information obtained during the geometry scan.
Definition: GeoHandler.h:60
dd4hep::DetElement::placement
PlacedVolume placement() const
Access to the physical volume of this detector element.
Definition: DetElement.cpp:321
dd4hep::Handle::isValid
bool isValid() const
Check the validity of the object held by the handle.
Definition: Handle.h:128
dd4hep::Volume::setRegion
const Volume & setRegion(const Detector &description, const std::string &name) const
Set the regional attributes to the volume. Note: If the name string is empty, the action is ignored.
Definition: Volumes.cpp:1242
dd4hep::detail::GeoScan
Geometry scanner (handle object)
Definition: GeoHandler.h:137
dd4hep::Material
Handle class describing a material.
Definition: Objects.h:272
dd4hep::detail::GeoHandler::m_set_data
std::map< int, std::set< const TGeoNode * > > * m_set_data
redundant container with std::set (for lookup purpose)
Definition: GeoHandler.h:95
dd4hep::DetElement
Handle class describing a detector element.
Definition: DetElement.h:188
dd4hep::Volume
Handle class holding a placed volume (also called physical volume)
Definition: Volumes.h:378
dd4hep::detail::GeoHandler
The base class for all dd4hep geometry crawlers.
Definition: GeoHandler.h:87
dd4hep::detail::GeoScan::GeoScan
GeoScan(DetElement e)
Initializing constructor.
Definition: GeoHandler.cpp:186
dd4hep::detail::GeoHandler::~GeoHandler
virtual ~GeoHandler()
Default destructor.
Definition: GeoHandler.cpp:70
dd4hep::LimitSet
Handle class describing a set of limits as they are used for simulation.
Definition: Objects.h:425
dd4hep::Volume::limitSet
LimitSet limitSet() const
Access to the limit set.
Definition: Volumes.cpp:1275
dd4hep::detail::GeoHandler::release
std::map< int, std::vector< const TGeoNode * > > * release()
Access to collected node list.
Definition: GeoHandler.cpp:80
dd4hep::Volume::region
Region region() const
Access to the handle to the region structure.
Definition: Volumes.cpp:1256
dd4hep::Region
Handle class describing a region as used in simulation.
Definition: Objects.h:462
dd4hep::detail::GeoHandler::GeoHandler
GeoHandler()
Default constructor.
Definition: GeoHandler.cpp:56
dd4hep::detail::GeoHandler::m_data
std::map< int, std::vector< const TGeoNode * > > * m_data
actual container with std::vector (preserves order)
Definition: GeoHandler.h:93
dd4hep::detail::GeoScan::operator()
virtual GeoScan & operator()()
Work callback.
Definition: GeoHandler.cpp:205
dd4hep::detail::GeoHandler::setPropagateRegions
bool setPropagateRegions(bool value)
Propagate regions. Returns the previous value.
Definition: GeoHandler.cpp:95
dd4hep::Handle::ptr
T * ptr() const
Access to the held object.
Definition: Handle.h:153
ObjectsInterna.h
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::detail::GeoScan::~GeoScan
virtual ~GeoScan()
Default destructor.
Definition: GeoHandler.cpp:198
dd4hep::Volume::visAttributes
VisAttr visAttributes() const
Access the visualisation attributes.
Definition: Volumes.cpp:1210
dd4hep::detail::GeoHandler::collect
GeoHandler & collect(DetElement top)
Collect geometry information from traversal.
Definition: GeoHandler.cpp:101
GeoHandler.h