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
DetectorInterna.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
18 #include "DD4hep/InstanceCount.h"
19 #include "DD4hep/DetectorTools.h"
20 #include "DD4hep/Handle.inl"
21 #include "DD4hep/Printout.h"
22 #include "TGeoVolume.h"
23 #include "TGeoMatrix.h"
24 #include "TGeoManager.h"
26 
27 using namespace std;
28 using namespace DD4hep;
29 using namespace DD4hep::Geometry;
32 
36 
38 SensitiveDetectorObject::SensitiveDetectorObject()
40  verbose(0), combineHits(0), ecut(0.0), readout(), region(), limits(), hitsCollection() {
41  printout(VERBOSE,"SensitiveDetectorObject","+++ Created new anonymous SensitiveDetectorObject()");
43 }
44 
48  verbose(0), combineHits(0), ecut(0.0), readout(), region(), limits(), hitsCollection() {
49  SetName(nam.c_str());
50  printout(VERBOSE,"SensitiveDetectorObject","+++ Created new SensitiveDetectorObject('%s')",nam.c_str());
52 }
53 
56  readout.clear();
57  region.clear();
58  limits.clear();
61 }
62 
66  flag(0), id(0), combineHits(0), typeFlag(0), level(-1), key(0), path(), placementPath(),
67  idealPlace(), placement(), volumeID(0), parent(), reference(), children(),
68  nominal(), survey(), alignments(), conditions(),
69  worldTrafo(), parentTrafo(), referenceTrafo(0) {
70  printout(VERBOSE,"DetElementObject","+++ Created new anonymous DetElementObject()");
72 }
73 
75 DetElementObject::DetElementObject(const std::string& nam, int ident)
77  flag(0), id(ident), combineHits(0), typeFlag(0), level(-1), key(0), path(), placementPath(),
78  idealPlace(), placement(), volumeID(0), parent(), reference(), children(),
79  nominal(), survey(), alignments(), conditions(),
80  worldTrafo(), parentTrafo(), referenceTrafo(0) {
81  SetName(nam.c_str());
82  printout(VERBOSE,"DetElementObject","+++ Created new DetElementObject('%s', %d)",nam.c_str(),id);
84 }
85 
96  placement.clear();
97  idealPlace.clear();
98  parent.clear();
101 }
102 
104 DetElementObject* DetElementObject::clone(int new_id, int flg) const {
105  DetElementObject* obj = new DetElementObject();
106  obj->id = new_id;
107  obj->typeFlag = typeFlag;
108  obj->flag = 0;
109  obj->key = 0;
110  obj->level = -1;
111  obj->combineHits = combineHits;
112  obj->nominal = Alignment();
113  obj->survey = Alignment();
116  obj->parent = DetElement();
117  if ( (flg & DetElement::COPY_PLACEMENT) == DetElement::COPY_PLACEMENT ) {
118  obj->placement = placement;
119  obj->idealPlace = idealPlace;
121  }
122  // This implicitly assumes that the children do not access the parent's extensions!
123  obj->ObjectExtensions::clear();
124  obj->ObjectExtensions::copyFrom(extensions, obj);
125 
126  obj->children.clear();
127  for (DetElement::Children::const_iterator i = children.begin(); i != children.end(); ++i) {
128  const NamedObject* pc = (*i).second.ptr();
129  const DetElementObject& d = (*i).second._data();
130  DetElement child(d.clone(d.id, DetElement::COPY_PLACEMENT), pc->GetName(), pc->GetTitle());
131  pair<Children::iterator, bool> r = obj->children.insert(make_pair(child.name(), child));
132  if (r.second) {
133  child._data().parent = obj;
134  }
135  else {
136  throw runtime_error("DD4hep: DetElement::copy: Element " + string(child.name()) +
137  " is already present [Double-Insert]");
138  }
139  }
140  return obj;
141 }
142 
145  if ( !privateWorld.isValid() ) {
146  DetElementObject* p = parent.ptr();
147  if ( 0 == p ) {
148  privateWorld = (WorldObject*)this;
149  return privateWorld;
150  }
151  return p->world();
152  }
153  return privateWorld;
154 }
155 
158  if ( (flag&HAVE_WORLD_TRAFO) == 0 ) {
159  PlacementPath nodes;
161  DetectorTools::placementPath(this, nodes);
163  }
164  return worldTrafo;
165 }
166 
169  if ( (flag&HAVE_PARENT_TRAFO) == 0 ) {
170  PlacementPath nodes;
174  }
175  return parentTrafo;
176 }
177 
180  if (!referenceTrafo) {
181  DetElement ref(reference);
182  DetElement self(this);
183  if ( ref.ptr() == self.ptr() ) {
184  referenceTrafo = new TGeoHMatrix(gGeoIdentity->Inverse());
185  }
186  else if ( DetectorTools::isParentElement(ref,self) ) {
187  PlacementPath nodes;
188  DetectorTools::placementPath(ref,self,nodes);
190  }
191  else if ( DetectorTools::isParentElement(self,ref) ) {
192  PlacementPath nodes;
193  DetectorTools::placementPath(self,ref,nodes);
195  }
196  else {
197  throw runtime_error("DD4hep: referenceTransformation: No path from " + string(self.name()) +
198  " to reference element " + string(ref.name()) + " present!");
199  }
200  }
201  return *referenceTrafo;
202 }
203 
205 void DetElementObject::revalidate(TGeoHMatrix* parent_world_trafo) {
206  PlacementPath par_path;
207  DetElement det(this);
208  DetElement par(det.parent());
209  DetElement wrld = world();
210  bool print = (DEBUG > printLevel());
211  string place = det.placementPath();
212  bool have_trafo = (flag&HAVE_WORLD_TRAFO);
213 
214  DetectorTools::placementPath(par, this, par_path);
215  PlacedVolume node = DetectorTools::findNode(wrld.placement(),place);
216  if ( !node.isValid() ) {
217  throw runtime_error("DD4hep: DetElement: The placement " + place + " is not part of the hierarchy.");
218  }
219  //print = (idealPlace.ptr() != node.ptr());
220  printout((idealPlace.ptr() != node.ptr()) ? INFO : DEBUG,
221  "DetElement","+++ Invalidate chache of %s -> %s Placement:%p --> %p %s",
222  det.path().c_str(), DetectorTools::placementPath(par_path).c_str(),
223  placement.ptr(), node.ptr(), (placement.ptr() == node.ptr()) ? "" : "[UPDATE]");
224 
225  placement = node;
226 
227  if ( have_trafo && print ) worldTrafo.Print();
228 
229  if ( (flag&HAVE_PARENT_TRAFO) ) {
231  }
232 
234  if ( parent_world_trafo ) {
235  // If possible use the pre-computed values from the parent
236  worldTrafo = *parent_world_trafo;
237  worldTrafo.Multiply(&parentTransformation());
239  }
240  else if ( have_trafo ) {
241  // Else re-compute the transformation to the world.
242  PlacementPath world_nodes;
243  DetectorTools::placementPath(this, world_nodes);
244  DetectorTools::placementTrafo(world_nodes,false,worldTrafo);
246  }
247 
248  if ( (flag&HAVE_PARENT_TRAFO) && print ) worldTrafo.Print();
250 
252  for(Children::const_iterator i = children.begin(); i!=children.end(); ++i) {
253  DetElement d((*i).second);
254  d->revalidate(&worldTrafo);
255  }
256 }
257 
259 void DetElementObject::removeAtUpdate(unsigned int typ, void* pointer) {
260  for(UpdateCallbacks::iterator i=updateCalls.begin(); i != updateCalls.end(); ++i) {
261  if ( (typ&((*i).second)) == typ && (*i).first.par == pointer ) {
262  updateCalls.erase(i);
263  return;
264  }
265  }
266 }
267 
269 void DetElementObject::update(unsigned int tags, void* param) {
270  DetElement det(this);
271  const void* args[3] = { (void*)((unsigned long)tags), this, param };
272  if ( (tags&DetElement::PLACEMENT_CHANGED)==DetElement::PLACEMENT_CHANGED &&
274  printout(INFO,"DetElement","+++ Need to update chaches and child caches of %s",
275  det.path().c_str());
276  DetElement par = det.parent();
277  TGeoHMatrix* parent_world_trafo = 0;
278  if ( par.isValid() && (par->flag&HAVE_PARENT_TRAFO) ) {
279  parent_world_trafo = &par->worldTrafo;
280  }
281  revalidate(parent_world_trafo);
282  }
283  for(UpdateCallbacks::const_iterator i=updateCalls.begin(); i != updateCalls.end(); ++i) {
284  if ( (tags&((*i).second)) ) {
285  (*i).first.execute(args);
286  }
287  }
288 }
289 
291  if ( !conditions.isValid() ) {
292  conditions.assign(new Conditions::Container::Object(this),"conditions","");
293  }
294  return conditions;
295 }
296 
298 WorldObject::WorldObject(LCDD& _lcdd, const string& nam)
299  : DetElementObject(nam,0), lcdd(&_lcdd),
300  conditionsLoader(0), conditionsManager(0), alignmentsLoader(0), alignmentsManager(0)
301 {
302 }
303 
306 }
Alignment nominal
Basic ideal/nominal detector element alignment entry.
PlacedVolume idealPlace
The subdetector placement corresponding to the ideal detector element's volume.
void destroyHandle(T &h)
Helper to delete objects from heap and reset the handle.
Definition: Handle.h:179
Handle class holding a placed volume (also called physical volume)
Definition: Volumes.h:135
const char * name() const
Access the object name (or "" if not supported by the object)
Definition: Handle.inl:36
void deletePtr(T *&p)
Helper to delete objects from heap and reset the pointer. Saves many many lines of code...
Definition: Primitives.h:234
unsigned int typeFlag
Flag to encode detector types.
bool isValid() const
Check the validity of the object held by the handle.
Definition: Handle.h:124
Data class with properties of sensitive detectors.
DetectorTools::PlacementPath PlacementPath
Handle< T > & clear()
Release the object held by the handle.
Definition: Handle.h:132
The data class behind a conditions container handle.
void update(unsigned int tags, void *param)
Trigger update callbacks.
void placementTrafo(const PlacementPath &nodes, bool inverse, TGeoHMatrix *&mat)
Update cached matrix to transform to positions to an upper level Placement.
Extensions extensions
The extensions object.
PlacedVolume placement
The subdetector placement corresponding to the actual detector element's volume.
static void decrement(T *)
Decrement count according to type information.
Handle class to hold the information of the top DetElement object 'world'.
Definition: World.h:35
std::vector< DetElement > ElementPath
Definition: DetectorTools.h:40
void removeAtUpdate(unsigned int type, void *pointer)
Remove callback from object.
int combineHits
Flag to process hits.
void SetName(const char *nam)
Set name (used by Handle)
Definition: NamedObject.h:58
bool isParentElement(DetElement parent, DetElement child)
Find path between the child element and the parent element.
DetElementObject()
Default constructor.
Container class for condition handles aggregated by a detector element.
Definition: Conditions.h:206
const TGeoHMatrix & worldTransformation()
Create cached matrix to transform to world coordinates.
TGeoHMatrix parentTrafo
Intermediate buffer to store the transformation to the parent detector element.
PrintLevel printLevel()
Access the current printer level.
Definition: Printout.cpp:323
T * ptr() const
Access to the held object.
Definition: Handle.h:149
DetElement reference
Reference element for stored transformations.
DD4hep::Geometry::DetElement DetElement
const char * GetTitle() const
Get name (used by Handle)
Definition: NamedObject.h:66
Implementation of a named object.
Definition: NamedObject.h:31
std::vector< PlacedVolume > PlacementPath
Definition: DetectorTools.h:41
const TGeoHMatrix & referenceTransformation()
Create cached matrix to transform to reference coordinates.
unsigned int key
Access hash key of this detector element (Only valid once geometry is closed!)
std::string placementPath(DetElement element)
Assemble the placement path from a given detector element to the world volume.
int id
Unique integer identifier of the detector instance.
UpdateCallbacks updateCalls
Placeholder for structure with update callbacks.
Alignments::Container AlignmentsContainer
ConditionsContainer assign_conditions()
std::string name
The object name.
Definition: NamedObject.h:34
void revalidate(TGeoHMatrix *parent_world_trafo)
Revalidate the caches.
void assign(Implementation *n, const std::string &nam, const std::string &title)
Assign a new named object. Note: object references must be managed by the user.
Definition: Handle.inl:27
Alignment survey
Basic detector element alignment entry containing the survey data.
ConditionsContainer conditions
The detector elements conditions access.
DetElement parent
Reference to the parent element.
PlacedVolume findNode(PlacedVolume top_place, const std::string &place)
Find a given node in the hierarchy starting from the top node (absolute placement!) ...
const std::string & path() const
Path of the detector element (not necessarily identical to placement path!)
Definition: Detector.cpp:160
World world()
Access to the world object. Only possible once the geometry is closed.
Conditions::Container ConditionsContainer
int level
Hierarchical level within the detector description.
void clear(bool destroy=true)
Clear all extensions.
TGeoHMatrix * referenceTrafo
Intermediate buffer for the transformation to an arbitrary DetElement.
DetElement parent() const
Access to the detector elements's parent.
Definition: Detector.cpp:222
static void increment(T *)
Increment count according to type information.
Definition: InstanceCount.h:98
std::string placementPath
The path to the placement of the detector element (if placed)
static const double pc
Definition: DD4hepUnits.h:85
WorldObject()
Default constructor.
Handle class describing a detector element.
Definition: Detector.h:172
void destroyHandles(M &m)
Functional created of map destruction functors.
Definition: Handle.h:199
const TGeoHMatrix & parentTransformation()
Create cached matrix to transform to parent coordinates.
unsigned int flag
Flag to remember internally calculated quatities.
virtual ~SensitiveDetectorObject()
Internal object destructor: release extension object(s)
The main interface to the DD4hep detector description package.
Definition: LCDD.h:82
DetectorTools::ElementPath ElementPath
World i_access_world()
Resolve the world object. Internal use ONLY.
virtual ~WorldObject()
Internal object destructor: release extension object(s)
Data class with properties of a detector element.
Data class with properties of a detector element.
const char * GetName() const
Access name.
Definition: NamedObject.h:54
int printout(PrintLevel severity, const char *src, const char *fmt,...)
Calls the display action with a given severity level.
Definition: Printout.cpp:111
World privateWorld
Reference to the parent element.
Implementation of an object supporting arbitrary user extensions.
Children children
The array of children.
virtual ~DetElementObject()
Internal object destructor: release extension object(s)
TGeoHMatrix worldTrafo
Intermediate buffer to store the transformation to the world coordination system. ...
DD4HEP_INSTANTIATE_HANDLE_NAMED(WorldObject)
std::ostream & print(const T &object, std::ostream &os=std::cout, const std::string &indent="")
Definition: Printout.h:260
AlignmentsContainer alignments
The detector elements alignments access.
unsigned long long int magic_word()
Access to the magic word, which is protecting some objects against memory corruptions.
Definition: Handle.h:55
virtual DetElementObject * clone(int new_id, int flag) const
Deep object copy to replicate DetElement trees e.g. for reflection.
const std::string & placementPath() const
Access to the full path to the placed object.
Definition: Detector.cpp:86