DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
Geant4HitCollection.h
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 #ifndef DDG4_GEANT4HITCOLLECTION_H
14 #define DDG4_GEANT4HITCOLLECTION_H
15 
16 // Framework include files
17 #include <DD4hep/Handle.h>
18 #include <DDG4/ComponentUtils.h>
19 #include <G4VHitsCollection.hh>
20 #include <G4VHit.hh>
21 
22 // C/C++ include files
23 #include <vector>
24 #include <string>
25 #include <climits>
26 #include <typeinfo>
27 #include <stdexcept>
28 
30 namespace dd4hep {
31 
33  namespace sim {
34 
35  // Forward declarations
36  class Geant4Sensitive;
37  class Geant4HitCollection;
38  class Geant4HitWrapper;
39 
41 
50  class Geant4HitWrapper: public G4VHit {
51  private:
52  public:
54  class InvalidHit {
55  public:
56  virtual ~InvalidHit();
57  };
58 
60 
66  public:
67  typedef std::pair<void*, HitManipulator*> Wrapper;
68 #ifdef __CINT__
69  const ComponentCast* cast;
70  const ComponentCast* vec_type;
71 #else
72  const ComponentCast& cast;
73  const ComponentCast& vec_type;
74 #endif
75  HitManipulator(const ComponentCast& c, const ComponentCast& v);
80  template <typename TYPE> Wrapper castHit(TYPE* obj) {
81  if (obj) {
82  ComponentCast& me = ComponentCast::instance<TYPE>();
83  Wrapper wrap(obj, this);
84  if (&cast == &me) {
85  return wrap;
86  }
87  // Need to ensure that the container types are the same!
88  wrap.first = me.apply_dynCast(cast, obj);
89  return wrap;
90  }
91  throw std::runtime_error("Invalid hit pointer passed to collection!");
92  }
94  template <typename TYPE> static void deleteHit(Wrapper& obj) {
95  TYPE* p = (TYPE*) obj.first;
96  if (p)
97  delete p;
98  obj.first = 0;
99  }
100  template <typename TYPE> static HitManipulator* instance() {
101  static HitManipulator hm(ComponentCast::instance<TYPE>(), ComponentCast::instance<std::vector<TYPE*> >());
102  return &hm;
103  }
104  };
105 
107  protected:
109  mutable Wrapper m_data;
110 
111  public:
112 
115  m_data.second = manipulator<InvalidHit>();
116  m_data.first = 0;
117  }
120  m_data = v.m_data;
121  v.m_data.first = 0;
122  //v.release();
123  }
126  m_data = v;
127  }
129  virtual ~Geant4HitWrapper();
131  void *operator new(size_t);
133  void operator delete(void *ptr);
135  void* release();
140  return m_data.second;
141  }
143  void* data() {
144  return m_data.first;
145  }
147  void* data() const {
148  return m_data.first;
149  }
151  template <typename TYPE> static HitManipulator* manipulator() {
152  return HitManipulator::instance<TYPE>();
153  }
156  if ( this != & v ) {
157  m_data = v.m_data;
158  v.m_data.first = 0;
159  //v.release();
160  }
161  return *this;
162  }
164  template <typename TYPE> operator TYPE*() const {
165  return (TYPE*) m_data.second->
166  cast.apply_dynCast(ComponentCast::instance<TYPE>(),m_data.first);
167  //cast.apply_downCast(ComponentCast::instance<TYPE>(),m_data.first);
168  }
169  };
170 
172 
202  public:
204  typedef std::vector<Geant4HitWrapper> WrappedHits;
209  typedef std::map<VolumeID, size_t> Keys;
210 
212 
220  class Compare {
221  public:
223  virtual ~Compare();
225  virtual void* operator()(const Geant4HitWrapper& w) const = 0;
226  };
227 
231  unsigned long value;
233  struct BitItems {
234  unsigned repeatedLookup:1;
235  unsigned mappedLookup:1;
236  } bits;
237  };
238 
239  protected:
247  size_t m_lastHit;
252 
253  protected:
255  void newInstance();
257  void* findHit(const Compare& cmp);
261  void releaseData(const ComponentCast& cast, std::vector<void*>* result);
263  void getData(const ComponentCast& cast, std::vector<void*>* result);
264 
265  public:
272  };
273 
275  template <typename TYPE>
276  Geant4HitCollection(const std::string& det, const std::string& coll, Geant4Sensitive* sd)
277  : G4VHitsCollection(det, coll), m_detector(sd),
278  m_manipulator(Geant4HitWrapper::manipulator<TYPE>()),
279  m_lastHit(ULONG_MAX)
280  {
281  newInstance();
282  m_hits.reserve(200);
284  }
286  template <typename TYPE>
287  Geant4HitCollection(const std::string& det, const std::string& coll, Geant4Sensitive* sd, const TYPE*)
288  : G4VHitsCollection(det, coll), m_detector(sd),
289  m_manipulator(Geant4HitWrapper::manipulator<TYPE>()),
290  m_lastHit(ULONG_MAX)
291  {
292  newInstance();
293  m_hits.reserve(200);
295  }
297  virtual ~Geant4HitCollection();
299  const ComponentCast& type() const;
301  const ComponentCast& vector_type() const;
303  virtual void clear();
305  void setOptimize(int flag) {
306  m_flags.value |= flag;
307  }
309  void setSensitive(Geant4Sensitive* detector) {
310  m_detector = detector;
311  }
314  return m_detector;
315  }
317  virtual G4VHit* GetHit(size_t which) const override {
318  return (G4VHit*) &m_hits.at(which);
319  }
321  virtual size_t GetSize() const override {
322  return m_hits.size();
323  }
325  Geant4HitWrapper& hit(size_t which) {
326  return m_hits.at(which);
327  }
329  const Geant4HitWrapper& hit(size_t which) const {
330  return m_hits.at(which);
331  }
333  template <typename TYPE> void add(TYPE* hit_pointer) {
334  Geant4HitWrapper w(m_manipulator->castHit(hit_pointer));
335  m_lastHit = m_hits.size();
336  m_hits.emplace_back(w);
337  }
339  template <typename TYPE> void add(VolumeID key, TYPE* hit_pointer) {
340  m_lastHit = m_hits.size();
341  std::pair<Keys::iterator,bool> ret = m_keys.emplace(key,m_lastHit);
342  if ( ret.second ) {
343  Geant4HitWrapper w(m_manipulator->castHit(hit_pointer));
344  m_hits.emplace_back(w);
345  return;
346  }
347  throw std::runtime_error("Attempt to insert hit with same key to G4 hit-collection "+GetName());
348  }
350  template <typename TYPE> TYPE* find(const Compare& cmp) {
351  return (TYPE*) findHit(cmp);
352  }
354  template <typename TYPE> TYPE* findByKey(VolumeID key) {
355  Keys::const_iterator i=m_keys.find(key);
356  if ( i == m_keys.end() ) return 0;
357  m_lastHit = (*i).second;
358  TYPE* obj = m_hits.at(m_lastHit);
359  return obj;
360  }
362  template <typename TYPE> std::vector<TYPE*> releaseHits() {
363  std::vector<TYPE*> vec;
364  if (m_hits.size() != 0) {
365  releaseData(ComponentCast::instance<TYPE>(), (std::vector<void*>*) &vec);
366  }
367  m_lastHit = ULONG_MAX;
368  m_keys.clear();
369  return vec;
370  }
372  void releaseHitsUnchecked(std::vector<void*>& result);
373 
375  template <typename TYPE> std::vector<TYPE*> getHits() {
376  std::vector<TYPE*> vec;
377  if (m_hits.size() != 0) {
378  getData(ComponentCast::instance<TYPE>(), (std::vector<void*>*) &vec);
379  }
380  return vec;
381  }
383  void getHitsUnchecked(std::vector<void*>& result);
384  };
385 
386 
388 
395  template<typename TYPE, typename POS> class PositionCompare : public Geant4HitCollection::Compare {
396  public:
397  const POS& pos;
399  PositionCompare(const POS& p) : pos(p) { }
401  virtual void* operator()(const Geant4HitWrapper& w) const;
402  };
403 
404  template <typename TYPE, typename POS>
406  TYPE* h = w;
407  return pos == h->position ? h : 0;
408  }
409 
411 
418  template<typename TYPE> class CellIDCompare : public Geant4HitCollection::Compare {
419  public:
420  long long int id;
422  CellIDCompare(long long int i) : id(i) { }
424  virtual void* operator()(const Geant4HitWrapper& w) const;
425  };
426 
427  template <typename TYPE>
429  TYPE* h = w;
430  if ( id == h->cellID )
431  return h;
432  return 0;
433  }
434 
435  } // End namespace sim
436 } // End namespace dd4hep
437 
438 #endif // DDG4_GEANT4HITCOLLECTION_H
dd4hep::sim::Geant4HitCollection::m_lastHit
size_t m_lastHit
Memorize for speedup the last searched hit.
Definition: Geant4HitCollection.h:247
dd4hep::sim::Geant4HitWrapper::data
void * data() const
Pointer/Object access (CONST)
Definition: Geant4HitCollection.h:147
dd4hep::sim::Geant4HitCollection::GetSize
virtual size_t GetSize() const override
Access the collection size.
Definition: Geant4HitCollection.h:321
dd4hep::sim::Geant4HitCollection::CollectionFlags
Union defining the hit collection flags for processing.
Definition: Geant4HitCollection.h:229
dd4hep::sim::Geant4HitCollection::Manip
Geant4HitWrapper::HitManipulator Manip
Hit manipulator.
Definition: Geant4HitCollection.h:207
dd4hep::sim::Geant4HitCollection::Compare
Generic class template to compare/select hits in Geant4HitCollection objects.
Definition: Geant4HitCollection.h:220
dd4hep::sim::Geant4HitWrapper::HitManipulator::instance
static HitManipulator * instance()
Definition: Geant4HitCollection.h:100
dd4hep::sim::Geant4HitWrapper::Geant4HitWrapper
Geant4HitWrapper()
Default constructor.
Definition: Geant4HitCollection.h:114
dd4hep::sim::Geant4HitCollection::clear
virtual void clear()
Clear the collection (Deletes all valid references to real hits)
Definition: Geant4HitCollection.cpp:102
v
View * v
Definition: MultiView.cpp:28
dd4hep::sim::Geant4HitWrapper::HitManipulator::HitManipulator
HitManipulator(const ComponentCast &c, const ComponentCast &v)
Initializing Constructor.
Definition: Geant4HitCollection.cpp:28
dd4hep::sim::Geant4HitWrapper::HitManipulator::Wrapper
std::pair< void *, HitManipulator * > Wrapper
Definition: Geant4HitCollection.h:67
dd4hep::sim::Geant4HitCollection::WrappedHits
std::vector< Geant4HitWrapper > WrappedHits
Hit wrapper.
Definition: Geant4HitCollection.h:205
dd4hep::sim::Geant4HitWrapper::release
void * release()
Pointer/Object release.
Definition: Geant4HitCollection.cpp:60
dd4hep::sim::Geant4HitCollection
Generic hit container class using Geant4HitWrapper objects.
Definition: Geant4HitCollection.h:201
Handle.h
dd4hep::sim::Geant4HitCollection::find
TYPE * find(const Compare &cmp)
Find hits in a collection by comparison of attributes.
Definition: Geant4HitCollection.h:350
dd4hep::sim::Geant4HitCollection::hit
Geant4HitWrapper & hit(size_t which)
Access the hit wrapper.
Definition: Geant4HitCollection.h:325
G4VHitsCollection
Class of the Geant4 toolkit. See http://www-geant4.kek.jp/Reference.
Definition: Geant4Classes.h:47
dd4hep::sim::Geant4HitCollection::Keys
std::map< VolumeID, size_t > Keys
Hit key map for fast random lookup.
Definition: Geant4HitCollection.h:209
dd4hep::sim::Geant4HitCollection::getHitsUnchecked
void getHitsUnchecked(std::vector< void * > &result)
Release all hits from the Geant4 container. Ownership stays with the container.
Definition: Geant4HitCollection.cpp:172
ComponentUtils.h
dd4hep::sim::Geant4HitCollection::CollectionFlags::BitItems::mappedLookup
unsigned mappedLookup
Definition: Geant4HitCollection.h:235
dd4hep::sim::Geant4HitCollection::CollectionFlags::BitItems::repeatedLookup
unsigned repeatedLookup
Definition: Geant4HitCollection.h:234
dd4hep::sim::Geant4HitCollection::findHit
void * findHit(const Compare &cmp)
Find hit in a collection by comparison of attributes.
Definition: Geant4HitCollection.cpp:109
dd4hep::sim::Geant4HitCollection::OPTIMIZE_MAPPEDLOOKUP
@ OPTIMIZE_MAPPEDLOOKUP
Definition: Geant4HitCollection.h:270
dd4hep::sim::CellIDCompare::CellIDCompare
CellIDCompare(long long int i)
Constructor.
Definition: Geant4HitCollection.h:422
dd4hep::sim::Geant4HitCollection::add
void add(VolumeID key, TYPE *hit_pointer)
Add a new hit with a check, that the hit is of the same type.
Definition: Geant4HitCollection.h:339
dd4hep::sim::Geant4HitWrapper::data
void * data()
Pointer/Object access.
Definition: Geant4HitCollection.h:143
dd4hep::sim::Geant4HitWrapper::manipulator
static HitManipulator * manipulator()
Generate manipulator object.
Definition: Geant4HitCollection.h:151
dd4hep::sim::PositionCompare
Specialized hit selector based on the hit's position.
Definition: Geant4HitCollection.h:395
dd4hep::sim::Geant4HitCollection::Geant4HitCollection
Geant4HitCollection(const std::string &det, const std::string &coll, Geant4Sensitive *sd, const TYPE *)
Initializing constructor.
Definition: Geant4HitCollection.h:287
dd4hep::sim::Geant4HitWrapper::~Geant4HitWrapper
virtual ~Geant4HitWrapper()
Default destructor.
Definition: Geant4HitCollection.cpp:39
dd4hep::sim::Geant4HitWrapper
Generic wrapper class for hit structures created in Geant4 sensitive detectors.
Definition: Geant4HitCollection.h:50
dd4hep::sim::Geant4HitCollection::sensitive
Geant4Sensitive * sensitive() const
Access the sensitive detector.
Definition: Geant4HitCollection.h:313
dd4hep::sim::Geant4HitWrapper::Wrapper
HitManipulator::Wrapper Wrapper
Definition: Geant4HitCollection.h:106
dd4hep::Utilities::GetName
const char * GetName(T *p)
Definition: Utilities.h:45
dd4hep::sim::Geant4HitCollection::vector_type
const ComponentCast & vector_type() const
Type information of the vector type for extracting data.
Definition: Geant4HitCollection.cpp:92
dd4hep::sim::Geant4HitCollection::Compare::~Compare
virtual ~Compare()
Default destructor.
Definition: Geant4HitCollection.cpp:76
dd4hep::sim::Geant4HitCollection::findByKey
TYPE * findByKey(VolumeID key)
Find hits in a collection by comparison of key value.
Definition: Geant4HitCollection.h:354
dd4hep::sim::Geant4HitCollection::m_flags
CollectionFlags m_flags
Optimization flags.
Definition: Geant4HitCollection.h:251
dd4hep::sim::Geant4HitCollection::findHitByKey
Geant4HitWrapper * findHitByKey(VolumeID key)
Find hit in a collection by comparison of the key.
Definition: Geant4HitCollection.cpp:125
dd4hep::sim::Geant4HitCollection::getHits
std::vector< TYPE * > getHits()
Release all hits from the Geant4 container. Ownership stays with the container.
Definition: Geant4HitCollection.h:375
dd4hep::sim::Geant4HitWrapper::releaseData
Wrapper releaseData()
Release data for copy.
Definition: Geant4HitCollection.cpp:68
dd4hep::sim::Geant4HitCollection::releaseHitsUnchecked
void releaseHitsUnchecked(std::vector< void * > &result)
Release all hits from the Geant4 container and pass ownership to the caller.
Definition: Geant4HitCollection.cpp:161
dd4hep::sim::Geant4HitCollection::newInstance
void newInstance()
Notification to increase the instance counter.
Definition: Geant4HitCollection.cpp:97
dd4hep::sim::Geant4HitCollection::CollectionFlags::BitItems
Individual hit collection bits.
Definition: Geant4HitCollection.h:233
dd4hep::sim::Geant4HitCollection::OPTIMIZE_REPEATEDLOOKUP
@ OPTIMIZE_REPEATEDLOOKUP
Definition: Geant4HitCollection.h:269
dd4hep::sim::Geant4HitCollection::m_manipulator
Manip * m_manipulator
The type of the objects in this collection. Set by the constructor.
Definition: Geant4HitCollection.h:245
dd4hep::sim::Geant4HitCollection::m_hits
WrappedHits m_hits
The collection of hit pointers in the wrapped format.
Definition: Geant4HitCollection.h:241
dd4hep::sim::PositionCompare::pos
const POS & pos
Definition: Geant4HitCollection.h:397
dd4hep::sim::Geant4HitCollection::OptimizationFlags
OptimizationFlags
Enumeration for collection optimization types.
Definition: Geant4HitCollection.h:267
dd4hep::sim::Geant4HitCollection::type
const ComponentCast & type() const
Type information of the object stored.
Definition: Geant4HitCollection.cpp:87
dd4hep::sim::Geant4HitWrapper::HitManipulator::vec_type
const ComponentCast & vec_type
Definition: Geant4HitCollection.h:73
dd4hep::sim::PositionCompare::PositionCompare
PositionCompare(const POS &p)
Constructor.
Definition: Geant4HitCollection.h:399
dd4hep::sim::Geant4HitCollection::OPTIMIZE_LAST
@ OPTIMIZE_LAST
Definition: Geant4HitCollection.h:271
dd4hep::sim::Geant4HitCollection::~Geant4HitCollection
virtual ~Geant4HitCollection()
Default destructor.
Definition: Geant4HitCollection.cpp:80
dd4hep::sim::Geant4HitCollection::OPTIMIZE_NONE
@ OPTIMIZE_NONE
Definition: Geant4HitCollection.h:268
dd4hep::sim::Geant4HitWrapper::Geant4HitWrapper
Geant4HitWrapper(const Wrapper &v)
Copy constructor.
Definition: Geant4HitCollection.h:125
VolumeID
dd4hep::DDSegmentation::VolumeID VolumeID
Definition: SegmentationDictionary.h:50
key
unsigned char key
Definition: AlignmentsCalculator.cpp:69
dd4hep::sim::Geant4HitCollection::m_detector
Geant4Sensitive * m_detector
Handle to the sensitive detector.
Definition: Geant4HitCollection.h:243
dd4hep::sim::Geant4HitCollection::hit
const Geant4HitWrapper & hit(size_t which) const
Access the hit wrapper (CONST)
Definition: Geant4HitCollection.h:329
dd4hep::sim::CellIDCompare
Specialized hit selector based on the hit's cell identifier.
Definition: Geant4HitCollection.h:418
dd4hep::sim::Geant4HitWrapper::HitManipulator::castHit
Wrapper castHit(TYPE *obj)
Check pointer to be of proper type.
Definition: Geant4HitCollection.h:80
dd4hep::sim::Geant4HitWrapper::HitManipulator::cast
const ComponentCast & cast
Definition: Geant4HitCollection.h:72
dd4hep::sim::Geant4HitWrapper::HitManipulator
Generic type manipulation class for generic hit structures created in Geant4 sensitive detectors.
Definition: Geant4HitCollection.h:65
dd4hep::sim::Geant4HitWrapper::HitManipulator::deleteHit
static void deleteHit(Wrapper &obj)
Static function to delete contained hits.
Definition: Geant4HitCollection.h:94
dd4hep::sim::PositionCompare::operator()
virtual void * operator()(const Geant4HitWrapper &w) const
Comparison function.
Definition: Geant4HitCollection.h:405
dd4hep::sim::Geant4HitCollection::m_keys
Keys m_keys
Hit key map for fast random lookup.
Definition: Geant4HitCollection.h:249
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::sim::Geant4HitWrapper::InvalidHit
Helper class to indicate invalid hit wrappers or containers.
Definition: Geant4HitCollection.h:54
dd4hep::sim::Geant4HitWrapper::operator=
Geant4HitWrapper & operator=(const Geant4HitWrapper &v)
Assignment transfers the pointer ownership.
Definition: Geant4HitCollection.h:155
det
DetElement::Object * det
Definition: AlignmentsCalculator.cpp:66
dd4hep::sim::Geant4HitCollection::releaseData
void releaseData(const ComponentCast &cast, std::vector< void * > *result)
Release all hits from the Geant4 container and pass ownership to the caller.
Definition: Geant4HitCollection.cpp:133
dd4hep::sim::Geant4HitCollection::setOptimize
void setOptimize(int flag)
Set optimization flags.
Definition: Geant4HitCollection.h:305
dd4hep::sim::CellIDCompare::operator()
virtual void * operator()(const Geant4HitWrapper &w) const
Comparison function.
Definition: Geant4HitCollection.h:428
dd4hep::sim::Geant4HitWrapper::manip
HitManipulator * manip() const
Access to cast grammar.
Definition: Geant4HitCollection.h:139
dd4hep::sim::Geant4HitCollection::add
void add(TYPE *hit_pointer)
Add a new hit with a check, that the hit is of the same type.
Definition: Geant4HitCollection.h:333
dd4hep::sim::Geant4HitWrapper::Geant4HitWrapper
Geant4HitWrapper(const Geant4HitWrapper &v)
Copy constructor.
Definition: Geant4HitCollection.h:119
dd4hep::sim::Geant4HitWrapper::HitManipulator::~HitManipulator
~HitManipulator()
Default destructor.
Definition: Geant4HitCollection.cpp:34
dd4hep::sim::Geant4Sensitive
The base class for Geant4 sensitive detector actions implemented by users.
Definition: Geant4SensDetAction.h:121
dd4hep::sim::Geant4HitCollection::CollectionFlags::value
unsigned long value
Full value.
Definition: Geant4HitCollection.h:231
dd4hep::sim::Geant4HitCollection::Compare::operator()
virtual void * operator()(const Geant4HitWrapper &w) const =0
Comparison function.
dd4hep::sim::Geant4HitCollection::setSensitive
void setSensitive(Geant4Sensitive *detector)
Set the sensitive detector.
Definition: Geant4HitCollection.h:309
dd4hep::sim::Geant4HitCollection::getData
void getData(const ComponentCast &cast, std::vector< void * > *result)
Release all hits from the Geant4 container. Ownership stays with the container.
Definition: Geant4HitCollection.cpp:148
G4VHit
Class of the Geant4 toolkit. See http://www-geant4.kek.jp/Reference.
Definition: Geant4Classes.h:43
dd4hep::sim::Geant4HitCollection::CollectionFlags::bits
struct dd4hep::sim::Geant4HitCollection::CollectionFlags::BitItems bits
dd4hep::sim::Geant4HitCollection::releaseHits
std::vector< TYPE * > releaseHits()
Release all hits from the Geant4 container and pass ownership to the caller.
Definition: Geant4HitCollection.h:362
dd4hep::sim::Geant4HitCollection::GetHit
virtual G4VHit * GetHit(size_t which) const override
Access individual hits.
Definition: Geant4HitCollection.h:317
dd4hep::sim::CellIDCompare::id
long long int id
Definition: Geant4HitCollection.h:420
dd4hep::sim::Geant4HitWrapper::m_data
Wrapper m_data
Wrapper data.
Definition: Geant4HitCollection.h:109
dd4hep::sim::Geant4HitWrapper::InvalidHit::~InvalidHit
virtual ~InvalidHit()
Definition: Geant4HitCollection.cpp:24
dd4hep::sim::Geant4HitCollection::Geant4HitCollection
Geant4HitCollection(const std::string &det, const std::string &coll, Geant4Sensitive *sd)
Initializing constructor (C++ version)
Definition: Geant4HitCollection.h:276