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
IDDecoder.cpp
Go to the documentation of this file.
1 /*
2  * IDDecoder.cpp
3  *
4  * Created on: Dec 12, 2013
5  * Author: Christian Grefe, CERN
6  */
7 
8 #include "DDRec/API/IDDecoder.h"
9 #include "DDRec/API/Exceptions.h"
10 
11 #include "DD4hep/LCDD.h"
12 #include "DD4hep/VolumeManager.h"
13 
14 namespace DD4hep {
15 namespace DDRec {
16 
18 using Geometry::LCDD;
19 using Geometry::PlacedVolume;
20 using Geometry::Readout;
21 using Geometry::Solid;
22 using Geometry::VolumeManager;
23 using Geometry::Volume;
24 using Geometry::Position;
25 using std::set;
26 
28  static IDDecoder idd;
29  return idd;
30 }
31 
34  LCDD& lcdd = LCDD::getInstance();
36  if (not _volumeManager.isValid()) {
37  _volumeManager = VolumeManager(lcdd, "volman", lcdd.world(), Readout(), VolumeManager::TREE);
38  }
39  _tgeoMgr = lcdd.world().volume()->GetGeoManager();
40 }
41 
45 CellID IDDecoder::cellIDFromLocal(const Position& local, const VolumeID volID) const {
46  double l[3];
47  double g[3];
48  local.GetCoordinates(l);
49  // FIXME: direct lookup of transformations seems to be broken
50  //const TGeoMatrix& localToGlobal = _volumeManager.worldTransformation(volID);
51  DetElement det = this->detectorElement(volID);
52  const TGeoMatrix& localToGlobal = det.worldTransformation();
53  localToGlobal.LocalToMaster(l, g);
54  Position global(g[0], g[1], g[2]);
55  return this->findReadout(det).segmentation().cellID(local, global, volID);
56 }
57 
61 CellID IDDecoder::cellID(const Position& global) const {
62  VolumeID volID = volumeID(global);
63  double l[3];
64  double g[3];
65  global.GetCoordinates(g);
66  // FIXME: direct lookup of transformations seems to be broken
67  //const TGeoMatrix& localToGlobal = _volumeManager.worldTransformation(volID);
68  DetElement det = this->detectorElement(volID);
69  const TGeoMatrix& localToGlobal = det.worldTransformation();
70  localToGlobal.MasterToLocal(g, l);
71  Position local(l[0], l[1], l[2]);
72  return this->findReadout(det).segmentation().cellID(local, global, volID);
73 }
74 
78 Position IDDecoder::position(const CellID& cell) const {
79  double l[3];
80  double g[3];
81  DetElement det = this->detectorElement(cell);
82  Position local = this->findReadout(det).segmentation().position(cell);
83  local.GetCoordinates(l);
84  // FIXME: direct lookup of transformations seems to be broken
85  //const TGeoMatrix& localToGlobal = _volumeManager.worldTransformation(cell);
86  const TGeoMatrix& localToGlobal = det.worldTransformation();
87  localToGlobal.LocalToMaster(l, g);
88  return Position(g[0], g[1], g[2]);
89 }
90 
91 /*
92  * Returns the local position from a given cell ID
93  */
95  DetElement det = this->detectorElement(cell);
96  return this->findReadout(det).segmentation().position(cell);
97 }
98 
99 /*
100  * Returns the volume ID of a given cell ID
101  */
102 VolumeID IDDecoder::volumeID(const CellID& cell) const {
103  DetElement det = this->detectorElement(cell);
104  return this->findReadout(det).segmentation().volumeID(cell);
105 }
106 
107 /*
108  * Returns the volume ID of a given global position
109  */
111  DetElement det = this->detectorElement(pos);
112  return det.volumeID();
113 }
114 
115 /*
116  * Returns the placement for a given cell ID
117  */
119  return _volumeManager.lookupPlacement(cell);
120 }
121 
122 /*
123  * Returns the placement for a given global position
124  */
126  return placement(volumeID(pos));
127 }
128 
129 /*
130  * Returns the subdetector for a given cell ID
131  */
133  return _volumeManager.lookupDetector(cell);
134 }
135 
136 /*
137  * Returns the subdetector for a given global position
138  */
140  return subDetector(volumeID(pos));
141 }
142 
143 /*
144  * Returns the closest detector element in the hierarchy for a given cell ID
145  */
147  return _volumeManager.lookupDetElement(cell);
148 }
149 
150 /*
151  * Returns the closest detector element in the hierarchy for a given global position
152  */
155  DetElement det = getClosestDaughter(world, pos);
156  if (not det.isValid()) {
157  throw invalid_position("DD4hep::DDRec::IDDecoder::detectorElement", pos);
158  }
159  std::cout << det.name() << std::endl;
160  return det;
161 }
162 
165  DetElement det = this->detectorElement(cell);
166  return this->findReadout(det);
167 }
168 
171  DetElement det = this->detectorElement(global);
172  return this->findReadout(det);
173 }
174 
175 /*
176  * Calculates the neighbours of the given cell ID and adds them to the list of neighbours
177  */
178 void IDDecoder::neighbours(const CellID& cell, set<CellID>& neighbour_cells) const {
179  DetElement det = this->detectorElement(cell);
180  this->findReadout(det).segmentation().neighbours(cell, neighbour_cells);
181 }
182 
183 /*
184  * Checks if the given cell IDs are neighbours
185  */
186 bool IDDecoder::areNeighbours(const CellID& cell, const CellID& otherCellID) const {
187  set<CellID> neighbour_cells;
188  DetElement det = this->detectorElement(cell);
189  this->findReadout(det).segmentation().neighbours(cell, neighbour_cells);
190  return neighbour_cells.count(otherCellID) != 0;
191 }
192 
195  Readout r = this->readout(cell);
196  return BarrelEndcapFlag(r.idSpec().field(this->barrelIdentifier())->value(cell));
197 }
198 
200 long int IDDecoder::layerIndex(const CellID& cell) const {
201  Readout r = this->readout(cell);
202  return r.idSpec().field(this->layerIdentifier())->value(cell);
203 }
204 
206 long int IDDecoder::systemIndex(const CellID& cell) const {
207  Readout r = this->readout(cell);
208  return r.idSpec().field(this->systemIdentifier())->value(cell);
209 }
210 
211 // helper method to find the corresponding Readout object to a DetElement
213 
214  // first check if top level is a sensitive detector
215  if (det.volume().isValid() and det.volume().isSensitive()) {
217  if (sd.isValid() and sd.readout().isValid()) {
218  return sd.readout();
219  }
220  }
221 
222  // check all children recursively for the first valid Readout object
223  const DetElement::Children& children = det.children();
224  DetElement::Children::const_iterator it = children.begin();
225  while (it != children.end()) {
226  Readout r = findReadout(it->second);
227  if (r.isValid()) {
228  return r;
229  }
230  ++it;
231  }
232 
233  // neither this or any daughter is sensitive
234  return Readout();
235 }
236 
237 // helper method to get the closest daughter DetElement to the position starting from the given DetElement
239  DetElement result;
240 
241  // check if we have a shape and see if we are inside
242  if (det.volume().isValid() and det.volume().solid().isValid()) {
243  double globalPosition[3] = { position.x(), position.y(), position.z() };
244  double localPosition[3] = { 0., 0., 0. };
245  det.worldTransformation().MasterToLocal(globalPosition, localPosition);
246  if (det.volume().solid()->Contains(localPosition)) {
247  result = det;
248  } else {
249  // assuming that any daughter shape would be inside this shape
250  return DetElement();
251  }
252  }
253 
254  const DetElement::Children& children = det.children();
255  DetElement::Children::const_iterator it = children.begin();
256  while (it != children.end()) {
257  DetElement daughterDet = getClosestDaughter(it->second, position);
258  if (daughterDet.isValid()) {
259  result = daughterDet;
260  break;
261  }
262  ++it;
263  }
264  return result;
265 }
266 
267 } /* namespace DDRec */
268 } /* namespace DD4hep */
Handle class to hold the information of a sensitive detector.
Definition: Detector.h:47
IDDecoder()
Default constructor.
Definition: IDDecoder.cpp:33
Ref_t sensitiveDetector() const
Access to the handle to the sensitive detector.
Definition: Volumes.cpp:743
static Geometry::DetElement getClosestDaughter(const Geometry::DetElement &det, const Geometry::Position &position)
Helper method to get the closest daughter DetElement to the position starting from the given DetEleme...
Definition: IDDecoder.cpp:238
Handle class holding a placed volume (also called physical volume)
Definition: Volumes.h:135
Solid solid() const
Access to Solid (Shape)
Definition: Volumes.cpp:693
virtual DetElement world() const =0
Return reference to the top-most (world) detector element.
static std::string systemIdentifier()
Definition: IDDecoder.h:142
Volume volume() const
Access to the logical volume of the detector element's placement.
Definition: Detector.cpp:311
Geometry::Position position(const CellID &cellID) const
Returns the global position from a given cell ID.
Definition: IDDecoder.cpp:78
const char * name() const
Access the object name (or "" if not supported by the object)
Definition: Handle.inl:36
bool isValid() const
Check the validity of the object held by the handle.
Definition: Handle.h:124
DDSegmentation::CellID CellID
Definition: IDDecoder.h:24
void neighbours(const CellID &cellID, std::set< CellID > &neighbours) const
Calculates the neighbours of the given cell ID and adds them to the list of neighbours.
PlacedVolume lookupPlacement(VolumeID volume_id) const
Lookup a physical (placed) volume identified by its 64 bit hit ID.
Geometry::DetElement detectorElement(const CellID &cellID) const
Returns the closest detector element in the hierarchy for a given cell ID.
Definition: IDDecoder.cpp:146
VolumeID volumeID(const CellID &cellID) const
Returns the volume ID of a given cell ID.
Definition: IDDecoder.cpp:102
BarrelEndcapFlag barrelEndcapFlag(const CellID &cellID) const
Access to the barrel-endcap flag.
Definition: IDDecoder.cpp:194
Geometry::Readout findReadout(const Geometry::DetElement &det) const
Helper method to find the corresponding Readout object to a DetElement.
Definition: IDDecoder.cpp:212
Geometry::Position localPosition(const CellID &cellID) const
Returns the local position from a given cell ID.
Definition: IDDecoder.cpp:94
Position position(const long64 &cellID) const
determine the local position based on the cell ID
const Children & children() const
Access to the list of children.
Definition: Detector.cpp:207
static LCDD & getInstance(void)
—Factory method----—
Definition: LCDDImp.cpp:87
Handle to the implementation of the readout structure of a subdetector.
Definition: Readout.h:46
DD4hep::Geometry::DetElement DetElement
Geometry::PlacedVolume placement(const CellID &cellID) const
Returns the placement for a given cell ID.
Definition: IDDecoder.cpp:118
VolumeID volumeID() const
The cached VolumeID of this subdetector element.
Definition: Detector.cpp:303
CellID cellIDFromLocal(const Geometry::Position &local, const VolumeID volumeID) const
Returns the cell ID from the local position in the given volume ID.
Definition: IDDecoder.cpp:45
Geometry::Readout readout(const CellID &cellID) const
Access to the Readout object for a given cell ID.
Definition: IDDecoder.cpp:164
static std::string barrelIdentifier()
Definition: IDDecoder.h:134
Field field(const std::string &field_name) const
Get the field descriptor of one field by name.
static std::string layerIdentifier()
Definition: IDDecoder.h:138
long int systemIndex(const CellID &cellID) const
Access to the system index.
Definition: IDDecoder.cpp:206
IDDescriptor idSpec() const
Access IDDescription structure.
Definition: Readout.cpp:109
TGeoManager * _tgeoMgr
Definition: IDDecoder.h:148
void neighbours(const CellID &cellID, std::set< CellID > &neighbours) const
Calculates the neighbours of the given cell ID and adds them to the list of neighbours.
Definition: IDDecoder.cpp:178
long int layerIndex(const CellID &cellID) const
Access to the layer index.
Definition: IDDecoder.cpp:200
ROOT::Math::XYZVector Position
Definition: Objects.h:75
Solid_type< TGeoShape > Solid
Definition: Shapes.h:108
Segmentation segmentation() const
Access segmentation structure.
Definition: Readout.cpp:130
Geometry::DetElement subDetector(const CellID &cellID) const
Returns the subdetector for a given cell ID.
Definition: IDDecoder.cpp:132
static const double g
Definition: DD4hepUnits.h:162
DDSegmentation::VolumeID VolumeID
Definition: IDDecoder.h:25
VolumeID volumeID(const CellID &cellID) const
Determine the volume ID from the full cell ID by removing all local fields.
DetElement lookupDetElement(VolumeID volume_id) const
Lookup the closest subdetector detector element in the hierarchy according to a contained 64 bit hit ...
DetElement lookupDetector(VolumeID volume_id) const
Lookup a top level subdetector detector element according to a contained 64 bit hit ID...
Handle class describing a detector element.
Definition: Detector.h:172
Class to support the retrieval of detector elements and volumes given a valid identifier.
Definition: VolumeManager.h:70
Geometry::VolumeManager _volumeManager
Definition: IDDecoder.h:147
Readout readout() const
Access readout structure of the sensitive detector.
Definition: Detector.cpp:451
bool areNeighbours(const CellID &cellID, const CellID &otherCellID) const
Checks if the given cell IDs are neighbours.
Definition: IDDecoder.cpp:186
The main interface to the DD4hep detector description package.
Definition: LCDD.h:82
std::map< std::string, DetElement > Children
Definition: Detector.h:202
virtual VolumeManager volumeManager() const =0
Return handle to the VolumeManager.
long64 cellID(const Position &localPosition, const Position &globalPosition, const long64 &volumeID) const
determine the cell ID based on the local position
const TGeoHMatrix & worldTransformation() const
Set detector element for reference transformations. Will delete existing reference trafo...
Definition: Detector.cpp:375
bool isSensitive() const
Accessor if volume is sensitive (ie. is attached to a sensitive detector)
Definition: Volumes.cpp:749
static IDDecoder & getInstance()
Access to the global IDDecoder instance.
Definition: IDDecoder.cpp:27
CellID cellID(const Geometry::Position &global) const
Returns the global cell ID from a given global position.
Definition: IDDecoder.cpp:61