Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:59:10

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 //
0027 //
0028 
0029 #ifndef G4THitsCollection_h
0030 #define G4THitsCollection_h 1
0031 
0032 #include "G4Allocator.hh"
0033 #include "G4VHitsCollection.hh"
0034 #include "globals.hh"
0035 
0036 #include <vector>
0037 
0038 // class description:
0039 //
0040 //  This is a template class of hits collection and parametrized by
0041 // The concrete class of G4VHit. This is a uniform collection for
0042 // a particular concrete hit class objects.
0043 //  An intermediate layer class G4HitsCollection appeared in this
0044 // header file is used just for G4Allocator, because G4Allocator
0045 // cannot be instansiated with a template class. Thus G4HitsCollection
0046 // class MUST NOT be directly used by the user.
0047 
0048 class G4HitsCollection : public G4VHitsCollection
0049 {
0050  public:
0051   using G4VHitsCollection::G4VHitsCollection;
0052   ~G4HitsCollection() override = default;
0053   G4bool operator==(const G4HitsCollection& right) const;
0054 
0055  protected:
0056   void* theCollection = nullptr;
0057 };
0058 
0059 #if defined G4DIGI_ALLOC_EXPORT
0060 extern G4DLLEXPORT G4Allocator<G4HitsCollection>*& anHCAllocator_G4MT_TLS_();
0061 #else
0062 extern G4DLLIMPORT G4Allocator<G4HitsCollection>*& anHCAllocator_G4MT_TLS_();
0063 #endif
0064 
0065 template <class T>
0066 class G4THitsCollection : public G4HitsCollection
0067 {
0068  public:
0069   G4THitsCollection();
0070   G4THitsCollection(G4String detName, G4String colNam);
0071   ~G4THitsCollection() override;
0072 
0073   G4bool operator==(const G4THitsCollection<T>& right) const;
0074 
0075   inline void* operator new(size_t);
0076   inline void operator delete(void* anHC);
0077 
0078   // Invoke Draw() method on all hit objects in collection
0079   void DrawAllHits() override;
0080 
0081   // Invoke Print() method on all hit objects in collection
0082   void PrintAllHits() override;
0083 
0084   //  Returns a pointer to the concrete hit object
0085   // Returns pointer to the concrete hit object at given index
0086   // Not bounds checked
0087   inline T* operator[](size_t i) const { return (*((std::vector<T*>*)theCollection))[i]; }
0088 
0089   // Return pointer to hit collection
0090   inline std::vector<T*>* GetVector() const { return (std::vector<T*>*)theCollection; }
0091 
0092   // Insert a hit object in the collection, taking onwenership
0093   // Returns the total number of hit objects stored after insertion
0094   inline size_t insert(T* aHit)
0095   {
0096     auto theHitsCollection = (std::vector<T*>*)theCollection;
0097     theHitsCollection->push_back(aHit);
0098     return theHitsCollection->size();
0099   }
0100 
0101   // Returns the number of hit objects stored in this collection.
0102   inline size_t entries() const
0103   {
0104     auto theHitsCollection = (std::vector<T*>*)theCollection;
0105     return theHitsCollection->size();
0106   }
0107 
0108   G4VHit* GetHit(size_t i) const override { return (*((std::vector<T*>*)theCollection))[i]; }
0109 
0110   size_t GetSize() const override { return ((std::vector<T*>*)theCollection)->size(); }
0111 };
0112 
0113 template <class T>
0114 inline void* G4THitsCollection<T>::operator new(size_t)
0115 {
0116   if (anHCAllocator_G4MT_TLS_() == nullptr) {
0117     anHCAllocator_G4MT_TLS_() = new G4Allocator<G4HitsCollection>;
0118   }
0119   return (void*)anHCAllocator_G4MT_TLS_()->MallocSingle();
0120 }
0121 
0122 template <class T>
0123 inline void G4THitsCollection<T>::operator delete(void* anHC)
0124 {
0125   anHCAllocator_G4MT_TLS_()->FreeSingle((G4HitsCollection*)anHC);
0126 }
0127 
0128 template <class T>
0129 G4THitsCollection<T>::G4THitsCollection()
0130 {
0131   auto theHitsCollection = new std::vector<T*>;
0132   theCollection = (void*)theHitsCollection;
0133 }
0134 
0135 template <class T>
0136 G4THitsCollection<T>::G4THitsCollection(G4String detName, G4String colNam)
0137   : G4HitsCollection(detName, colNam)
0138 {
0139   auto theHitsCollection = new std::vector<T*>;
0140   theCollection = (void*)theHitsCollection;
0141 }
0142 
0143 template <class T>
0144 G4THitsCollection<T>::~G4THitsCollection()
0145 {
0146   auto theHitsCollection = (std::vector<T*>*)theCollection;
0147   for (const auto* hit : *theHitsCollection) {
0148     delete hit;
0149   }
0150   theHitsCollection->clear();
0151   delete theHitsCollection;
0152 }
0153 
0154 template <class T>
0155 G4bool G4THitsCollection<T>::operator==(const G4THitsCollection<T>& right) const
0156 {
0157   return (collectionName == right.collectionName);
0158 }
0159 
0160 template <class T>
0161 void G4THitsCollection<T>::DrawAllHits()
0162 {
0163   auto theHitsCollection = (std::vector<T*>*)theCollection;
0164   for (auto* hit : *theHitsCollection) {
0165     hit->Draw();
0166   }
0167 }
0168 
0169 template <class T>
0170 void G4THitsCollection<T>::PrintAllHits()
0171 {
0172   auto theHitsCollection = (std::vector<T*>*)theCollection;
0173   for (auto* hit : *theHitsCollection) {
0174     hit->Print();
0175   }
0176 }
0177 
0178 #endif