DD4hep - The AIDA detector description toolkit for high energy physics experiments
DD4hep  Rev:Unversioneddirectory
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GeoHandler.cpp
Go to the documentation of this file.
1 // $Id$
2 //==========================================================================
3 // AIDA Detector description implementation for LCD
4 //--------------------------------------------------------------------------
5 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
6 // All rights reserved.
7 //
8 // For the licensing terms see $DD4hepINSTALL/LICENSE.
9 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
10 //
11 // Author : M.Frank
12 //
13 //==========================================================================
14 
15 // Framework include files
16 #include "DD4hep/LCDD.h"
17 #include "DD4hep/GeoHandler.h"
19 
20 // ROOT includes
21 #include "TGeoManager.h"
22 #include "TGeoCompositeShape.h"
23 #include "TGeoBoolNode.h"
24 #include "TClass.h"
25 
26 // C/C++ include files
27 #include <iostream>
28 
29 using namespace DD4hep::Geometry;
30 using namespace DD4hep;
31 using namespace std;
32 
33 namespace {
34  void collectSolid(GeoHandler::GeometryInfo& geo, const string& name, const string& node, TGeoShape* shape,
35  TGeoMatrix* matrix) {
36  if (0 == ::strncmp(shape->GetName(), "TGeo", 4)) {
37  shape->SetName(name.c_str());
38  }
39  if (shape->IsA() == TGeoCompositeShape::Class()) {
40  const TGeoCompositeShape* s = (const TGeoCompositeShape*) shape;
41  const TGeoBoolNode* boolean = s->GetBoolNode();
42  collectSolid(geo, name + "_left", name + "_left", boolean->GetLeftShape(), boolean->GetLeftMatrix());
43  collectSolid(geo, name + "_right", name + "_right", boolean->GetRightShape(), boolean->GetRightMatrix());
44  }
45  geo.solids.insert(shape);
46  geo.trafos.push_back(make_pair(node, matrix));
47  }
48 }
49 
51 GeoHandler::GeoHandler() : m_propagateRegions(false) {
52  m_data = new Data();
53 }
54 
57  : m_propagateRegions(false), m_data(ptr) {
58 }
59 
62  if (m_data)
63  delete m_data;
64  m_data = 0;
65 }
66 
68  Data* d = m_data;
69  m_data = 0;
70  return d;
71 }
72 
75  bool old = m_propagateRegions;
76  m_propagateRegions = value;
77  return old;
78 }
79 
81  m_data->clear();
82  return i_collect(element.placement().ptr(), 0, Region(), LimitSet());
83 }
84 
86  m_data->clear();
87  i_collect(element.placement().ptr(), 0, Region(), LimitSet());
88  for (Data::const_reverse_iterator i = m_data->rbegin(); i != m_data->rend(); ++i) {
89  const Data::mapped_type& mapped = (*i).second;
90  for (Data::mapped_type::const_iterator j = mapped.begin(); j != mapped.end(); ++j) {
91  const TGeoNode* n = *j;
92  TGeoVolume* v = n->GetVolume();
93  if (v) {
94  Material m(v->GetMedium());
95  Volume vol = Ref_t(v);
96  // Note : assemblies and the world do not have a real volume nor a material
97  if (info.volumeSet.find(vol) == info.volumeSet.end()) {
98  info.volumeSet.insert(vol);
99  info.volumes.push_back(vol);
100  }
101  if (m.isValid())
102  info.materials.insert(m);
103  if (dynamic_cast<Volume::Object*>(v)) {
104  VisAttr vis = vol.visAttributes();
105  //Region reg = vol.region();
106  //LimitSet lim = vol.limitSet();
107  //SensitiveDetector det = vol.sensitiveDetector();
108 
109  if (vis.isValid())
110  info.vis.insert(vis);
111  //if ( lim.isValid() ) info.limits[lim.ptr()].insert(v);
112  //if ( reg.isValid() ) info.regions[reg.ptr()].insert(v);
113  //if ( det.isValid() ) info.sensitives[det.ptr()].insert(v);
114  }
115  collectSolid(info, v->GetName(), n->GetName(), v->GetShape(), n->GetMatrix());
116  }
117  }
118  }
119  return *this;
120 }
121 
122 GeoHandler& GeoHandler::i_collect(const TGeoNode* current, int level, Region rg, LimitSet ls) {
123  TGeoVolume* volume = current->GetVolume();
124  TObjArray* nodes = volume->GetNodes();
125  int num_children = nodes ? nodes->GetEntriesFast() : 0;
126  Volume vol(volume);
127  Region region = vol.region();
128  LimitSet limits = vol.limitSet();
129 
130  if ( m_propagateRegions ) {
131  if ( !region.isValid() && rg.isValid() ) {
132  region = rg;
133  vol.setRegion(region);
134  }
135  if ( !limits.isValid() && ls.isValid() ) {
136  limits = ls;
137  vol.setLimitSet(limits);
138  }
139  }
140  (*m_data)[level].insert(current);
141  //printf("GeoHandler: collect level:%d %s\n",level,current->GetName());
142  if (num_children > 0) {
143  for (int i = 0; i < num_children; ++i) {
144  TGeoNode* node = (TGeoNode*) nodes->At(i);
145  i_collect(node, level + 1, region, limits);
146  }
147  }
148  return *this;
149 }
150 
153  m_data = GeoHandler().collect(e).release();
154 }
155 
157 GeoScan::GeoScan(DetElement e, bool propagate) {
158  GeoHandler h;
159  h.setPropagateRegions(propagate);
160  m_data = h.collect(e).release();
161 }
162 
165  if (m_data)
166  delete m_data;
167  m_data = 0;
168 }
169 
172  return *this;
173 }
174 
const Volume & setLimitSet(const LCDD &lcdd, 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:717
bool setPropagateRegions(bool value)
Propagate regions. Returns the previous value.
Definition: GeoHandler.cpp:74
Handle class describing a material.
Definition: Objects.h:300
bool isValid() const
Check the validity of the object held by the handle.
Definition: Handle.h:124
LimitSet limitSet() const
Access to the limit set.
Definition: Volumes.cpp:731
GeoScan(DetElement e)
Initializing constructor.
Definition: GeoHandler.cpp:152
GeoHandler()
Default constructor.
Definition: GeoHandler.cpp:51
GeoHandler & i_collect(const TGeoNode *node, int level, Region rg, LimitSet ls)
Internal helper to collect geometry information from traversal.
Definition: GeoHandler.cpp:122
TGeoShape * s
Definition: Volumes.cpp:294
return e
Definition: Volumes.cpp:297
T * ptr() const
Access to the held object.
Definition: Handle.h:149
Handle class holding a placed volume (also called physical volume)
Definition: Volumes.h:237
GeoHandler & collect(DetElement top)
Collect geometry information from traversal.
Definition: GeoHandler.cpp:80
virtual ~GeoScan()
Default destructor.
Definition: GeoHandler.cpp:164
The base class for all DD4hep geometry crawlers.
Definition: GeoHandler.h:102
virtual ~GeoHandler()
Default destructor.
Definition: GeoHandler.cpp:61
Data * release()
Access to collected node list.
Definition: GeoHandler.cpp:67
virtual GeoScan & operator()()
Work callback.
Definition: GeoHandler.cpp:171
GeoHandler::Data * m_data
Data holder.
Definition: GeoHandler.h:146
PlacedVolume placement() const
Access to the physical volume of this detector element.
Definition: Detector.cpp:279
Region region() const
Access to the handle to the region structure.
Definition: Volumes.cpp:712
Handle< NamedObject > Ref_t
Default Ref_t definition describing named objects.
Definition: Handle.h:176
Geometry scanner (handle object)
Definition: GeoHandler.h:143
std::map< int, std::set< const TGeoNode * > > Data
Definition: GeoHandler.h:61
Handle class describing visualization attributes.
Definition: Objects.h:341
Handle class describing a detector element.
Definition: Detector.h:172
View * v
Definition: MultiView.cpp:30
Handle class describing a set of limits as they are used for simulation.
Definition: Objects.h:475
Handle class describing a region as used in simulation.
Definition: Objects.h:516
const Volume & setRegion(const LCDD &lcdd, 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:698
TGeoShape TGeoMedium * m
Definition: Volumes.cpp:294