Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-11 08:09:24

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 /// \file G4TAtomicHitsMap.hh
0027 /// \brief Definition of the G4TAtomicHitsMap class
0028 ///
0029 /// This is an implementation of G4THitsMap<T> where the underlying
0030 ///     type is G4atomic<T>, not just T. A static assert is provided to
0031 ///     ensure that T is fundamental. This class should be used in lieu
0032 ///     of G4THitsMap<T> when memory is a concern. Atomics are
0033 ///     thread-safe and *generally* faster that mutexes (as long as the
0034 ///     STL implementation is lock-free) but the synchronization does
0035 ///     not come without a cost. If performance is the primary concern,
0036 ///     use G4THitsMap<T> in thread-local instances.
0037 //
0038 //
0039 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0040 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0041 
0042 #ifndef G4TAtomicHitsMap_h
0043 #define G4TAtomicHitsMap_h 1
0044 
0045 #include "G4AutoLock.hh"
0046 #include "G4THitsCollection.hh"
0047 #include "G4THitsMap.hh"
0048 #include "G4Threading.hh"
0049 #include "G4atomic.hh"
0050 #include "globals.hh"
0051 
0052 #include <map>
0053 #include <type_traits>
0054 
0055 /// class description:
0056 ///
0057 ///  This is a template class of hits map and parametrized by
0058 /// The concrete class of G4VHit. This is a uniform collection for
0059 /// a particular concrete hit class objects.
0060 ///  An intermediate layer class G4HitsMap appeared in this
0061 /// header file is used just for G4Allocator, because G4Allocator
0062 /// cannot be instansiated with a template class. Thus G4HitsMap
0063 /// class MUST NOT be directly used by the user.
0064 
0065 template<typename T>
0066 class G4TAtomicHitsMap : public G4VHitsCollection
0067 {
0068   protected:
0069     static_assert(std::is_fundamental<T>::value, "G4TAtomicHitsMap must use fundamental type");
0070 
0071   public:
0072     typedef G4atomic<T> value_type;
0073     typedef value_type* mapped_type;
0074     typedef typename std::map<G4int, mapped_type> container_type;
0075     typedef typename container_type::iterator iterator;
0076     typedef typename container_type::const_iterator const_iterator;
0077 
0078   public:
0079     G4TAtomicHitsMap();
0080 
0081   public:  // with description
0082     G4TAtomicHitsMap(G4String detName, G4String colNam);
0083     // constructor.
0084 
0085   public:
0086     virtual ~G4TAtomicHitsMap();
0087     G4bool operator==(const G4TAtomicHitsMap<T>& right) const;
0088     G4TAtomicHitsMap<T>& operator+=(const G4TAtomicHitsMap<T>& right) const;
0089     G4TAtomicHitsMap<T>& operator+=(const G4THitsMap<T>& right) const;
0090 
0091   public:  // with description
0092     virtual void DrawAllHits();
0093     virtual void PrintAllHits();
0094     //  These two methods invokes Draw() and Print() methods of all of
0095     // hit objects stored in this map, respectively.
0096 
0097   public:  // with description
0098     inline value_type* operator[](G4int key) const;
0099 
0100     //  Returns a pointer to a concrete hit object.
0101     inline container_type* GetMap() const { return theCollection; }
0102     //  Returns a collection map.
0103     inline G4int add(const G4int& key, value_type*& aHit) const;
0104     inline G4int add(const G4int& key, T& aHit) const;
0105     //  Insert a hit object. Total number of hit objects stored in this
0106     // map is returned.
0107     inline G4int set(const G4int& key, value_type*& aHit) const;
0108     inline G4int set(const G4int& key, T& aHit) const;
0109     //  Overwrite a hit object. Total number of hit objects stored in this
0110     // map is returned.
0111     inline G4int entries() const { return theCollection->size(); }
0112     //  Returns the number of hit objects stored in this map
0113     inline void clear();
0114 
0115   public:
0116     virtual G4VHit* GetHit(size_t) const { return 0; }
0117     virtual size_t GetSize() const { return theCollection->size(); }
0118 
0119     virtual size_t size() const { return theCollection->size(); }
0120 
0121   public:
0122     iterator begin() { return theCollection->begin(); }
0123     iterator end() { return theCollection->end(); }
0124 
0125     const_iterator begin() const { return theCollection->begin(); }
0126     const_iterator end() const { return theCollection->end(); }
0127 
0128     const_iterator cbegin() const { return theCollection->cbegin(); }
0129     const_iterator cend() const { return theCollection->cend(); }
0130 
0131     iterator find(G4int p) { return theCollection->find(p); }
0132     const_iterator find(G4int p) const { return theCollection->find(p); }
0133 
0134   private:
0135     container_type* theCollection;
0136     mutable G4Mutex fMutex;
0137 };
0138 
0139 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0140 template<typename T>
0141 G4TAtomicHitsMap<T>::G4TAtomicHitsMap() : theCollection(new container_type)
0142 {}
0143 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0144 template<typename T>
0145 G4TAtomicHitsMap<T>::G4TAtomicHitsMap(G4String detName, G4String colNam)
0146   : G4VHitsCollection(detName, colNam), theCollection(new container_type)
0147 {}
0148 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0149 template<typename T>
0150 G4TAtomicHitsMap<T>::~G4TAtomicHitsMap()
0151 {
0152   for (auto itr = theCollection->begin(); itr != theCollection->end(); itr++)
0153     delete itr->second;
0154 
0155   delete theCollection;
0156 }
0157 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0158 template<typename T>
0159 G4bool G4TAtomicHitsMap<T>::operator==(const G4TAtomicHitsMap<T>& right) const
0160 {
0161   return (collectionName == right.collectionName);
0162 }
0163 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0164 template<typename T>
0165 G4TAtomicHitsMap<T>& G4TAtomicHitsMap<T>::operator+=(const G4TAtomicHitsMap<T>& rhs) const
0166 {
0167   for (auto itr = rhs.GetMap()->begin(); itr != rhs.GetMap()->end(); itr++)
0168     add(itr->first, *(itr->second));
0169 
0170   return (G4TAtomicHitsMap<T>&)(*this);
0171 }
0172 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0173 template<typename T>
0174 G4TAtomicHitsMap<T>& G4TAtomicHitsMap<T>::operator+=(const G4THitsMap<T>& rhs) const
0175 {
0176   for (auto itr = rhs.GetMap()->begin(); itr != rhs.GetMap()->end(); itr++)
0177     add(itr->first, *(itr->second));
0178 
0179   return (G4TAtomicHitsMap<T>&)(*this);
0180 }
0181 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0182 template<typename T>
0183 inline typename G4TAtomicHitsMap<T>::value_type* G4TAtomicHitsMap<T>::operator[](G4int key) const
0184 {
0185   if (theCollection->find(key) != theCollection->end())
0186     return theCollection->find(key)->second;
0187   else {
0188     G4AutoLock l(&fMutex);
0189     if (theCollection->find(key) == theCollection->end()) {
0190       value_type* ptr = new value_type;
0191       (*theCollection)[key] = ptr;
0192       return ptr;
0193     }
0194     else
0195       return theCollection->find(key)->second;
0196   }
0197 }
0198 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0199 template<typename T>
0200 inline G4int G4TAtomicHitsMap<T>::add(const G4int& key, value_type*& aHit) const
0201 {
0202   if (theCollection->find(key) != theCollection->end())
0203     *(*theCollection)[key] += *aHit;
0204   else {
0205     G4AutoLock l(&fMutex);
0206     (*theCollection)[key] = aHit;
0207   }
0208   G4AutoLock l(&fMutex);
0209   return theCollection->size();
0210 }
0211 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0212 template<typename T>
0213 inline G4int G4TAtomicHitsMap<T>::add(const G4int& key, T& aHit) const
0214 {
0215   if (theCollection->find(key) != theCollection->end())
0216     *(*theCollection)[key] += aHit;
0217   else {
0218     value_type* hit = new value_type;
0219     *hit = aHit;
0220     G4AutoLock l(&fMutex);
0221     (*theCollection)[key] = hit;
0222   }
0223   G4AutoLock l(&fMutex);
0224   return theCollection->size();
0225 }
0226 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0227 template<typename T>
0228 inline G4int G4TAtomicHitsMap<T>::set(const G4int& key, value_type*& aHit) const
0229 {
0230   if (theCollection->find(key) != theCollection->end()) delete (*theCollection)[key]->second;
0231 
0232   (*theCollection)[key] = aHit;
0233   G4AutoLock l(&fMutex);
0234   return theCollection->size();
0235 }
0236 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0237 template<typename T>
0238 inline G4int G4TAtomicHitsMap<T>::set(const G4int& key, T& aHit) const
0239 {
0240   if (theCollection->find(key) != theCollection->end())
0241     *(*theCollection)[key] = aHit;
0242   else {
0243     value_type* hit = new value_type;
0244     *hit = aHit;
0245     (*theCollection)[key] = hit;
0246   }
0247   G4AutoLock l(&fMutex);
0248   return theCollection->size();
0249 }
0250 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0251 template<typename T>
0252 void G4TAtomicHitsMap<T>::DrawAllHits()
0253 {}
0254 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0255 template<typename T>
0256 void G4TAtomicHitsMap<T>::PrintAllHits()
0257 {
0258   G4cout << "G4TAtomicHitsMap " << SDname << " / " << collectionName << " --- " << entries()
0259          << " entries" << G4endl;
0260 }
0261 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0262 template<typename T>
0263 void G4TAtomicHitsMap<T>::clear()
0264 {
0265   G4AutoLock l(&fMutex);
0266 
0267   for (auto itr = theCollection->begin(); itr != theCollection->end(); itr++)
0268     delete itr->second;
0269 
0270   theCollection->clear();
0271 }
0272 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0273 
0274 #endif