DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
Geant4HitCollection.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/InstanceCount.h>
17 #include <DDG4/Geant4Data.h>
18 #include <G4Allocator.hh>
19 
20 using namespace dd4hep::sim;
21 
22 G4ThreadLocal G4Allocator<Geant4HitWrapper>* HitWrapperAllocator = 0;
23 
25 }
26 
28 Geant4HitWrapper::HitManipulator::HitManipulator(const ComponentCast& c, const ComponentCast& v)
29  : cast(c), vec_type(v) {
31 }
32 
36 }
37 
40  if (m_data.first && m_data.second) {
41  (*m_data.second->cast.destroy)(m_data.first);
42  m_data.first = 0;
43  }
44 }
45 
47 void* Geant4HitWrapper::operator new(size_t) {
48  if ( HitWrapperAllocator )
49  return HitWrapperAllocator->MallocSingle();
50  HitWrapperAllocator = new G4Allocator<Geant4HitWrapper>;
51  return HitWrapperAllocator->MallocSingle();
52 }
53 
55 void Geant4HitWrapper::operator delete(void *p) {
56  HitWrapperAllocator->FreeSingle((Geant4HitWrapper*) p);
57 }
58 
61  void* p = m_data.first;
62  m_data.first = 0;
63  m_data.second = manipulator<InvalidHit>();
64  return p;
65 }
66 
69  Wrapper w = m_data;
70  m_data.first = 0;
71  m_data.second = manipulator<InvalidHit>();
72  return w;
73 }
74 
77 }
78 
81  m_hits.clear();
82  m_keys.clear();
84 }
85 
87 const dd4hep::ComponentCast& Geant4HitCollection::type() const {
88  return m_manipulator->cast;
89 }
90 
92 const dd4hep::ComponentCast& Geant4HitCollection::vector_type() const {
93  return m_manipulator->vec_type;
94 }
95 
99 }
100 
103  m_lastHit = ULONG_MAX;
104  m_hits.clear();
105  m_keys.clear();
106 }
107 
110  void* p = 0;
111  WrappedHits::const_iterator i = m_hits.begin();
112  if ( m_flags.bits.repeatedLookup && m_lastHit < m_hits.size() ) {
113  if ( (p = cmp(*(i+m_lastHit))) != 0 ) return p;
114  }
115  for (size_t cnt=0; i != m_hits.end(); ++i, ++cnt) {
116  if ((p = cmp(*i)) != 0) {
117  m_lastHit = cnt;
118  return p;
119  }
120  }
121  return p;
122 }
123 
126  Keys::const_iterator i=m_keys.find(key);
127  if ( i == m_keys.end() ) return 0;
128  m_lastHit = (*i).second;
129  return &m_hits.at(m_lastHit);
130 }
131 
133 void Geant4HitCollection::releaseData(const ComponentCast& cast, std::vector<void*>* result) {
134  result->reserve(m_hits.size());
135  for (size_t j = 0, n = m_hits.size(); j < n; ++j) {
136  Geant4HitWrapper& w = m_hits.at(j);
137  Manip* m = w.manip();
138  if (&cast == &m->cast)
139  result->emplace_back(w.release());
140  else
141  result->emplace_back(m->cast.apply_downCast(cast, w.release()));
142  }
143  m_lastHit = ULONG_MAX;
144  m_keys.clear();
145 }
146 
148 void Geant4HitCollection::getData(const ComponentCast& cast, std::vector<void*>* result) {
149  result->reserve(m_hits.size());
150  for (size_t j = 0, n = m_hits.size(); j < n; ++j) {
151  Geant4HitWrapper& w = m_hits.at(j);
152  Manip* m = w.manip();
153  if (&cast == &m->cast)
154  result->emplace_back(w.data());
155  else
156  result->emplace_back(m->cast.apply_downCast(cast, w.data()));
157  }
158 }
159 
161 void Geant4HitCollection::releaseHitsUnchecked(std::vector<void*>& result) {
162  result.reserve(m_hits.size());
163  for (size_t j = 0, n = m_hits.size(); j < n; ++j) {
164  Geant4HitWrapper& w = m_hits.at(j);
165  result.emplace_back(w.release());
166  }
167  m_lastHit = ULONG_MAX;
168  m_keys.clear();
169 }
170 
172 void Geant4HitCollection::getHitsUnchecked(std::vector<void*>& result) {
173  result.reserve(m_hits.size());
174  for (size_t j = 0, n = m_hits.size(); j < n; ++j) {
175  Geant4HitWrapper& w = m_hits.at(j);
176  result.emplace_back(w.data());
177  }
178 }
Geant4HitCollection.h
dd4hep::sim::Geant4HitCollection::Compare
Generic class template to compare/select hits in Geant4HitCollection objects.
Definition: Geant4HitCollection.h:220
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::release
void * release()
Pointer/Object release.
Definition: Geant4HitCollection.cpp:60
dd4hep::InstanceCount::increment
static void increment(T *)
Increment count according to type information.
Definition: InstanceCount.h:98
Geant4Data.h
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
dd4hep::sim::Geant4HitCollection::findHit
void * findHit(const Compare &cmp)
Find hit in a collection by comparison of attributes.
Definition: Geant4HitCollection.cpp:109
dd4hep::sim::Geant4HitWrapper::data
void * data()
Pointer/Object access.
Definition: Geant4HitCollection.h:143
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::Geant4HitWrapper::Wrapper
HitManipulator::Wrapper Wrapper
Definition: Geant4HitCollection.h:106
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::InstanceCount::decrement
static void decrement(T *)
Decrement count according to type information.
Definition: InstanceCount.h:102
dd4hep::sim::Geant4HitCollection::findHitByKey
Geant4HitWrapper * findHitByKey(VolumeID key)
Find hit in a collection by comparison of the key.
Definition: Geant4HitCollection.cpp:125
dd4hep::sim::Geant4HitWrapper::releaseData
Wrapper releaseData()
Release data for copy.
Definition: Geant4HitCollection.cpp:68
HitWrapperAllocator
G4ThreadLocal G4Allocator< Geant4HitWrapper > * HitWrapperAllocator
Definition: Geant4HitCollection.cpp:22
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::type
const ComponentCast & type() const
Type information of the object stored.
Definition: Geant4HitCollection.cpp:87
dd4hep::sim::Geant4HitCollection::~Geant4HitCollection
virtual ~Geant4HitCollection()
Default destructor.
Definition: Geant4HitCollection.cpp:80
VolumeID
dd4hep::DDSegmentation::VolumeID VolumeID
Definition: SegmentationDictionary.h:50
key
unsigned char key
Definition: AlignmentsCalculator.cpp:69
dd4hep::sim
Namespace for the Geant4 based simulation part of the AIDA detector description toolkit.
Definition: Geant4Output2EDM4hep.cpp:49
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::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::Geant4HitWrapper::manip
HitManipulator * manip() const
Access to cast grammar.
Definition: Geant4HitCollection.h:139
dd4hep::sim::Geant4HitWrapper::HitManipulator::~HitManipulator
~HitManipulator()
Default destructor.
Definition: Geant4HitCollection.cpp:34
InstanceCount.h
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
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