File indexing completed on 2026-05-11 08:09:24
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
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
0056
0057
0058
0059
0060
0061
0062
0063
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:
0082 G4TAtomicHitsMap(G4String detName, G4String colNam);
0083
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:
0092 virtual void DrawAllHits();
0093 virtual void PrintAllHits();
0094
0095
0096
0097 public:
0098 inline value_type* operator[](G4int key) const;
0099
0100
0101 inline container_type* GetMap() const { return theCollection; }
0102
0103 inline G4int add(const G4int& key, value_type*& aHit) const;
0104 inline G4int add(const G4int& key, T& aHit) const;
0105
0106
0107 inline G4int set(const G4int& key, value_type*& aHit) const;
0108 inline G4int set(const G4int& key, T& aHit) const;
0109
0110
0111 inline G4int entries() const { return theCollection->size(); }
0112
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
0140 template<typename T>
0141 G4TAtomicHitsMap<T>::G4TAtomicHitsMap() : theCollection(new container_type)
0142 {}
0143
0144 template<typename T>
0145 G4TAtomicHitsMap<T>::G4TAtomicHitsMap(G4String detName, G4String colNam)
0146 : G4VHitsCollection(detName, colNam), theCollection(new container_type)
0147 {}
0148
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
0158 template<typename T>
0159 G4bool G4TAtomicHitsMap<T>::operator==(const G4TAtomicHitsMap<T>& right) const
0160 {
0161 return (collectionName == right.collectionName);
0162 }
0163
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
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
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
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
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
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
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
0251 template<typename T>
0252 void G4TAtomicHitsMap<T>::DrawAllHits()
0253 {}
0254
0255 template<typename T>
0256 void G4TAtomicHitsMap<T>::PrintAllHits()
0257 {
0258 G4cout << "G4TAtomicHitsMap " << SDname << " / " << collectionName << " --- " << entries()
0259 << " entries" << G4endl;
0260 }
0261
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
0273
0274 #endif