File indexing completed on 2025-01-18 09:14:26
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <DD4hep/InstanceCount.h>
0016 #include <DDG4/Geant4HitCollection.h>
0017 #include <DDG4/Geant4Data.h>
0018 #include <G4Allocator.hh>
0019
0020 using namespace dd4hep::sim;
0021
0022 G4ThreadLocal G4Allocator<Geant4HitWrapper>* HitWrapperAllocator = 0;
0023
0024 Geant4HitWrapper::InvalidHit::~InvalidHit() {
0025 }
0026
0027
0028 Geant4HitWrapper::HitManipulator::HitManipulator(const ComponentCast& c, const ComponentCast& v)
0029 : cast(c), vec_type(v) {
0030 InstanceCount::increment(this);
0031 }
0032
0033
0034 Geant4HitWrapper::HitManipulator::~HitManipulator() {
0035 InstanceCount::decrement(this);
0036 }
0037
0038
0039 Geant4HitWrapper::~Geant4HitWrapper() {
0040 if (m_data.first && m_data.second) {
0041 (*m_data.second->cast.destroy)(m_data.first);
0042 m_data.first = 0;
0043 }
0044 }
0045
0046
0047 void* Geant4HitWrapper::operator new(size_t) {
0048 if ( HitWrapperAllocator )
0049 return HitWrapperAllocator->MallocSingle();
0050 HitWrapperAllocator = new G4Allocator<Geant4HitWrapper>;
0051 return HitWrapperAllocator->MallocSingle();
0052 }
0053
0054
0055 void Geant4HitWrapper::operator delete(void *p) {
0056 HitWrapperAllocator->FreeSingle((Geant4HitWrapper*) p);
0057 }
0058
0059
0060 void* Geant4HitWrapper::release() {
0061 void* p = m_data.first;
0062 m_data.first = 0;
0063 m_data.second = manipulator<InvalidHit>();
0064 return p;
0065 }
0066
0067
0068 Geant4HitWrapper::Wrapper Geant4HitWrapper::releaseData() {
0069 Wrapper w = m_data;
0070 m_data.first = 0;
0071 m_data.second = manipulator<InvalidHit>();
0072 return w;
0073 }
0074
0075
0076 Geant4HitCollection::Compare::~Compare() {
0077 }
0078
0079
0080 Geant4HitCollection::~Geant4HitCollection() {
0081 m_hits.clear();
0082 m_keys.clear();
0083 InstanceCount::decrement(this);
0084 }
0085
0086
0087 const dd4hep::ComponentCast& Geant4HitCollection::type() const {
0088 return m_manipulator->cast;
0089 }
0090
0091
0092 const dd4hep::ComponentCast& Geant4HitCollection::vector_type() const {
0093 return m_manipulator->vec_type;
0094 }
0095
0096
0097 void Geant4HitCollection::newInstance() {
0098 InstanceCount::increment(this);
0099 }
0100
0101
0102 void Geant4HitCollection::clear() {
0103 m_lastHit = ULONG_MAX;
0104 m_hits.clear();
0105 m_keys.clear();
0106 }
0107
0108
0109 void* Geant4HitCollection::findHit(const Compare& cmp) {
0110 void* p = 0;
0111 WrappedHits::const_iterator i = m_hits.begin();
0112 if ( m_flags.bits.repeatedLookup && m_lastHit < m_hits.size() ) {
0113 if ( (p = cmp(*(i+m_lastHit))) != 0 ) return p;
0114 }
0115 for (size_t cnt=0; i != m_hits.end(); ++i, ++cnt) {
0116 if ((p = cmp(*i)) != 0) {
0117 m_lastHit = cnt;
0118 return p;
0119 }
0120 }
0121 return p;
0122 }
0123
0124
0125 Geant4HitWrapper* Geant4HitCollection::findHitByKey(VolumeID key) {
0126 Keys::const_iterator i=m_keys.find(key);
0127 if ( i == m_keys.end() ) return 0;
0128 m_lastHit = (*i).second;
0129 return &m_hits.at(m_lastHit);
0130 }
0131
0132
0133 void Geant4HitCollection::releaseData(const ComponentCast& cast, std::vector<void*>* result) {
0134 result->reserve(m_hits.size());
0135 for (size_t j = 0, n = m_hits.size(); j < n; ++j) {
0136 Geant4HitWrapper& w = m_hits.at(j);
0137 Manip* m = w.manip();
0138 if (&cast == &m->cast)
0139 result->emplace_back(w.release());
0140 else
0141 result->emplace_back(m->cast.apply_downCast(cast, w.release()));
0142 }
0143 m_lastHit = ULONG_MAX;
0144 m_keys.clear();
0145 }
0146
0147
0148 void Geant4HitCollection::getData(const ComponentCast& cast, std::vector<void*>* result) {
0149 result->reserve(m_hits.size());
0150 for (size_t j = 0, n = m_hits.size(); j < n; ++j) {
0151 Geant4HitWrapper& w = m_hits.at(j);
0152 Manip* m = w.manip();
0153 if (&cast == &m->cast)
0154 result->emplace_back(w.data());
0155 else
0156 result->emplace_back(m->cast.apply_downCast(cast, w.data()));
0157 }
0158 }
0159
0160
0161 void Geant4HitCollection::releaseHitsUnchecked(std::vector<void*>& result) {
0162 result.reserve(m_hits.size());
0163 for (size_t j = 0, n = m_hits.size(); j < n; ++j) {
0164 Geant4HitWrapper& w = m_hits.at(j);
0165 result.emplace_back(w.release());
0166 }
0167 m_lastHit = ULONG_MAX;
0168 m_keys.clear();
0169 }
0170
0171
0172 void Geant4HitCollection::getHitsUnchecked(std::vector<void*>& result) {
0173 result.reserve(m_hits.size());
0174 for (size_t j = 0, n = m_hits.size(); j < n; ++j) {
0175 Geant4HitWrapper& w = m_hits.at(j);
0176 result.emplace_back(w.data());
0177 }
0178 }