File indexing completed on 2025-10-31 09:03:38
0001 
0002 
0003 
0004 
0005 
0006 
0007 
0008 
0009 
0010 
0011 
0012 
0013 
0014 
0015 
0016 
0017 
0018 
0019 
0020 
0021 
0022 
0023 
0024 
0025 
0026 
0027 
0028 
0029 
0030 
0031 
0032 
0033 
0034 
0035 
0036 
0037 
0038 
0039 
0040 #ifndef G4THitsCollection_h
0041 #define G4THitsCollection_h 1
0042 
0043 #include "G4Allocator.hh"
0044 #include "G4VHitsCollection.hh"
0045 #include "globals.hh"
0046 
0047 #include <vector>
0048 
0049 class G4HitsCollection : public G4VHitsCollection
0050 {
0051   public:
0052 
0053     using G4VHitsCollection::G4VHitsCollection;
0054     ~G4HitsCollection() override = default;
0055     G4bool operator==(const G4HitsCollection& right) const;
0056 
0057   protected:
0058 
0059     void* theCollection = nullptr;
0060 };
0061 
0062 #if defined G4DIGI_ALLOC_EXPORT
0063 extern G4DLLEXPORT G4Allocator<G4HitsCollection>*& anHCAllocator_G4MT_TLS_();
0064 #else
0065 extern G4DLLIMPORT G4Allocator<G4HitsCollection>*& anHCAllocator_G4MT_TLS_();
0066 #endif
0067 
0068 template <class T>
0069 class G4THitsCollection : public G4HitsCollection
0070 {
0071   public:
0072 
0073     G4THitsCollection();
0074     G4THitsCollection(const G4String& detName, const G4String& colNam);
0075     ~G4THitsCollection() override;
0076 
0077     G4bool operator==(const G4THitsCollection<T>& right) const;
0078 
0079     inline void* operator new(std::size_t);
0080     inline void operator delete(void* anHC);
0081 
0082     
0083     void DrawAllHits() override;
0084 
0085     
0086     void PrintAllHits() override;
0087 
0088     
0089     
0090     
0091     inline T* operator[](std::size_t i) const { return (*((std::vector<T*>*)theCollection))[i]; }
0092 
0093     
0094     inline std::vector<T*>* GetVector() const { return (std::vector<T*>*)theCollection; }
0095 
0096     
0097     
0098     inline std::size_t insert(T* aHit)
0099     {
0100       auto theHitsCollection = (std::vector<T*>*)theCollection;
0101       theHitsCollection->push_back(aHit);
0102       return theHitsCollection->size();
0103     }
0104 
0105     
0106     inline std::size_t entries() const
0107     {
0108       auto theHitsCollection = (std::vector<T*>*)theCollection;
0109       return theHitsCollection->size();
0110     }
0111 
0112     G4VHit* GetHit(std::size_t i) const override { return (*((std::vector<T*>*)theCollection))[i]; }
0113 
0114     std::size_t GetSize() const override { return ((std::vector<T*>*)theCollection)->size(); }
0115 };
0116 
0117 template <class T>
0118 inline void* G4THitsCollection<T>::operator new(std::size_t)
0119 {
0120   if (anHCAllocator_G4MT_TLS_() == nullptr) {
0121     anHCAllocator_G4MT_TLS_() = new G4Allocator<G4HitsCollection>;
0122   }
0123   return (void*)anHCAllocator_G4MT_TLS_()->MallocSingle();
0124 }
0125 
0126 template <class T>
0127 inline void G4THitsCollection<T>::operator delete(void* anHC)
0128 {
0129   anHCAllocator_G4MT_TLS_()->FreeSingle((G4HitsCollection*)anHC);
0130 }
0131 
0132 template <class T>
0133 G4THitsCollection<T>::G4THitsCollection()
0134 {
0135   auto theHitsCollection = new std::vector<T*>;
0136   theCollection = (void*)theHitsCollection;
0137 }
0138 
0139 template <class T>
0140 G4THitsCollection<T>::G4THitsCollection(const G4String& detName, const G4String& colNam)
0141   : G4HitsCollection(detName, colNam)
0142 {
0143   auto theHitsCollection = new std::vector<T*>;
0144   theCollection = (void*)theHitsCollection;
0145 }
0146 
0147 template <class T>
0148 G4THitsCollection<T>::~G4THitsCollection()
0149 {
0150   auto theHitsCollection = (std::vector<T*>*)theCollection;
0151   for (const auto* hit : *theHitsCollection) {
0152     delete hit;
0153   }
0154   theHitsCollection->clear();
0155   delete theHitsCollection;
0156 }
0157 
0158 template <class T>
0159 G4bool G4THitsCollection<T>::operator==(const G4THitsCollection<T>& right) const
0160 {
0161   return (collectionName == right.collectionName);
0162 }
0163 
0164 template <class T>
0165 void G4THitsCollection<T>::DrawAllHits()
0166 {
0167   auto theHitsCollection = (std::vector<T*>*)theCollection;
0168   for (auto* hit : *theHitsCollection) {
0169     hit->Draw();
0170   }
0171 }
0172 
0173 template <class T>
0174 void G4THitsCollection<T>::PrintAllHits()
0175 {
0176   auto theHitsCollection = (std::vector<T*>*)theCollection;
0177   for (auto* hit : *theHitsCollection) {
0178     hit->Print();
0179   }
0180 }
0181 
0182 #endif