Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:14:26

0001 //==========================================================================
0002 //  AIDA Detector description implementation 
0003 //--------------------------------------------------------------------------
0004 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
0005 // All rights reserved.
0006 //
0007 // For the licensing terms see $DD4hepINSTALL/LICENSE.
0008 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
0009 //
0010 // Author     : M.Frank
0011 //
0012 //==========================================================================
0013 
0014 // Framework include files
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 /// Initializing Constructor
0028 Geant4HitWrapper::HitManipulator::HitManipulator(const ComponentCast& c, const ComponentCast& v)
0029   : cast(c), vec_type(v) {
0030   InstanceCount::increment(this);
0031 }
0032 
0033 /// Default destructor
0034 Geant4HitWrapper::HitManipulator::~HitManipulator() {
0035   InstanceCount::decrement(this);
0036 }
0037 
0038 /// Default destructor
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 /// Geant4 required object allocator
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 /// Geat4 required object destroyer
0055 void Geant4HitWrapper::operator delete(void *p) {
0056   HitWrapperAllocator->FreeSingle((Geant4HitWrapper*) p);
0057 }
0058 
0059 /// Pointer/Object release
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 /// Pointer/Object release
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 /// Default destructor
0076 Geant4HitCollection::Compare::~Compare()  {
0077 }
0078 
0079 /// Default destructor
0080 Geant4HitCollection::~Geant4HitCollection() {
0081   m_hits.clear();
0082   m_keys.clear();
0083   InstanceCount::decrement(this);
0084 }
0085 
0086 /// Type information of the object stored
0087 const dd4hep::ComponentCast& Geant4HitCollection::type() const {
0088   return m_manipulator->cast;
0089 }
0090 
0091 /// Type information of the vector type for extracting data
0092 const dd4hep::ComponentCast& Geant4HitCollection::vector_type() const {
0093   return m_manipulator->vec_type;
0094 }
0095 
0096 /// Notification to increase the instance counter
0097 void Geant4HitCollection::newInstance() {
0098   InstanceCount::increment(this);
0099 }
0100 
0101 /// Clear the collection (Deletes all valid references to real hits)
0102 void Geant4HitCollection::clear()   {
0103   m_lastHit = ULONG_MAX;
0104   m_hits.clear();
0105   m_keys.clear();
0106 }
0107 
0108 /// Find hit in a collection by comparison of attributes
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 /// Find hit in a collection by comparison of the key
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 /// Release all hits from the Geant4 container and pass ownership to the caller
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 /// Release all hits from the Geant4 container. Ownership stays with the container
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 /// Release all hits from the Geant4 container and pass ownership to the caller
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 /// Release all hits from the Geant4 container. Ownership stays with the container
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 }