Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 09:15:47

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 // G4THitsCollection & G4HitsCollection
0027 //
0028 // Class description:
0029 //
0030 // This is a template class of hits collection and parametrised by
0031 // The concrete class of G4VHit. This is a uniform collection for
0032 // a particular concrete hit class objects.
0033 // An intermediate layer class G4HitsCollection appeared in this
0034 // header file is used just for G4Allocator, because G4Allocator
0035 // cannot be instantiated with a template class. Thus G4HitsCollection
0036 // class MUST NOT be directly used by the user.
0037 //
0038 // Author: Makoto Asai
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     // Invoke Draw() method on all hit objects in collection
0083     void DrawAllHits() override;
0084 
0085     // Invoke Print() method on all hit objects in collection
0086     void PrintAllHits() override;
0087 
0088     //  Returns a pointer to the concrete hit object
0089     // Returns pointer to the concrete hit object at given index
0090     // Not bounds checked
0091     inline T* operator[](std::size_t i) const { return (*((std::vector<T*>*)theCollection))[i]; }
0092 
0093     // Return pointer to hit collection
0094     inline std::vector<T*>* GetVector() const { return (std::vector<T*>*)theCollection; }
0095 
0096     // Insert a hit object in the collection, taking onwenership
0097     // Returns the total number of hit objects stored after insertion
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     // Returns the number of hit objects stored in this collection.
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