ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4TAtomicHitsMap.hh
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4TAtomicHitsMap.hh
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
28 //
29 //
30 //
31 //
40 //
41 //
42 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
43 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
44 
45 
46 
47 
48 #ifndef G4TAtomicHitsMap_h
49 #define G4TAtomicHitsMap_h 1
50 
51 #include "G4THitsCollection.hh"
52 #include "G4THitsMap.hh"
53 #include "globals.hh"
54 #include "G4atomic.hh"
55 #include "G4Threading.hh"
56 #include "G4AutoLock.hh"
57 
58 #include <map>
59 #include <type_traits>
60 
61 // class description:
62 //
63 // This is a template class of hits map and parametrized by
64 // The concrete class of G4VHit. This is a uniform collection for
65 // a particular concrete hit class objects.
66 // An intermediate layer class G4HitsMap appeared in this
67 // header file is used just for G4Allocator, because G4Allocator
68 // cannot be instansiated with a template class. Thus G4HitsMap
69 // class MUST NOT be directly used by the user.
70 
71 template <typename T>
73 {
74 protected:
75  static_assert(std::is_fundamental<T>::value,
76  "G4TAtomicHitsMap must use fundamental type");
77 
78 public:
79  typedef G4atomic<T> value_type;
81  typedef typename std::map<G4int, mapped_type> container_type;
82  typedef typename container_type::iterator iterator;
83  typedef typename container_type::const_iterator const_iterator;
84 
85 public:
87 
88 public: // with description
89  G4TAtomicHitsMap(G4String detName, G4String colNam);
90  // constructor.
91 
92 public:
93  virtual ~G4TAtomicHitsMap();
97 
98 public: // with description
99  virtual void DrawAllHits();
100  virtual void PrintAllHits();
101  // These two methods invokes Draw() and Print() methods of all of
102  // hit objects stored in this map, respectively.
103 
104 public: // with description
105  inline value_type* operator[](G4int key) const;
106 
107  // Returns a pointer to a concrete hit object.
108  inline container_type* GetMap() const
109  { return theCollection; }
110  // Returns a collection map.
111  inline G4int add(const G4int & key, value_type*& aHit) const;
112  inline G4int add(const G4int & key, T& aHit) const;
113  // Insert a hit object. Total number of hit objects stored in this
114  // map is returned.
115  inline G4int set(const G4int & key, value_type*& aHit) const;
116  inline G4int set(const G4int & key, T& aHit) const;
117  // Overwrite a hit object. Total number of hit objects stored in this
118  // map is returned.
119  inline G4int entries() const
120  {
121  return theCollection->size();
122  }
123  // Returns the number of hit objects stored in this map
124  inline void clear();
125 
126 public:
127  virtual G4VHit* GetHit(size_t) const {return 0;}
128  virtual size_t GetSize() const
129  {
130  return theCollection->size();
131  }
132 
133  virtual size_t size() const { return theCollection->size(); }
134 
135 public:
136  iterator begin() { return theCollection->begin(); }
137  iterator end() { return theCollection->end(); }
138 
139  const_iterator begin() const { return theCollection->begin(); }
140  const_iterator end() const { return theCollection->end(); }
141 
142  const_iterator cbegin() const { return theCollection->cbegin(); }
143  const_iterator cend() const { return theCollection->cend(); }
144 
145  iterator find(G4int p) { return theCollection->find(p); }
146  const_iterator find(G4int p) const { return theCollection->find(p); }
147 
148 private:
150  mutable G4Mutex fMutex;
151 
152 };
153 
154 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
155 template <typename T>
157  : theCollection(new container_type)
158 { }
159 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
160 template <typename T>
162  G4String colNam)
163  : G4VHitsCollection(detName,colNam),
164  theCollection(new container_type)
165 { }
166 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
167 template <typename T>
169 {
170  for(auto itr = theCollection->begin(); itr != theCollection->end(); itr++)
171  delete itr->second;
172 
173  delete theCollection;
174 }
175 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
176 template <typename T>
178 {
179  return (collectionName == right.collectionName);
180 }
181 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
182 template <typename T>
185 {
186  for(auto itr = rhs.GetMap()->begin(); itr != rhs.GetMap()->end(); itr++)
187  add(itr->first, *(itr->second));
188 
189  return (G4TAtomicHitsMap<T>&)(*this);
190 }
191 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
192 template <typename T>
195 {
196  for(auto itr = rhs.GetMap()->begin(); itr != rhs.GetMap()->end(); itr++)
197  add(itr->first, *(itr->second));
198 
199  return (G4TAtomicHitsMap<T>&)(*this);
200 }
201 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
202 template <typename T>
203 inline typename G4TAtomicHitsMap<T>::value_type*
205 {
206  if(theCollection->find(key) != theCollection->end())
207  return theCollection->find(key)->second;
208  else
209  {
210  G4AutoLock l(&fMutex);
211  if(theCollection->find(key) == theCollection->end())
212  {
213  value_type* ptr = new value_type;
214  (*theCollection)[key] = ptr;
215  return ptr;
216  } else
217  return theCollection->find(key)->second;
218  }
219 }
220 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
221 template <typename T>
222 inline G4int
223 G4TAtomicHitsMap<T>::add(const G4int& key, value_type*& aHit) const
224 {
225  if(theCollection->find(key) != theCollection->end())
226  *(*theCollection)[key] += *aHit;
227  else
228  {
229  G4AutoLock l(&fMutex);
230  (*theCollection)[key] = aHit;
231  }
232  G4AutoLock l(&fMutex);
233  return theCollection->size();
234 }
235 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
236 template <typename T>
237 inline G4int
238 G4TAtomicHitsMap<T>::add(const G4int& key, T& aHit) const
239 {
240 
241  if(theCollection->find(key) != theCollection->end())
242  *(*theCollection)[key] += aHit;
243  else
244  {
245  value_type* hit = new value_type;
246  *hit = aHit;
247  G4AutoLock l(&fMutex);
248  (*theCollection)[key] = hit;
249  }
250  G4AutoLock l(&fMutex);
251  return theCollection->size();
252 }
253 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
254 template <typename T>
255 inline G4int
256 G4TAtomicHitsMap<T>::set(const G4int& key, value_type*& aHit) const
257 {
258  if(theCollection->find(key) != theCollection->end())
259  delete (*theCollection)[key]->second;
260 
261  (*theCollection)[key] = aHit;
262  G4AutoLock l(&fMutex);
263  return theCollection->size();
264 }
265 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
266 template <typename T>
267 inline G4int
268 G4TAtomicHitsMap<T>::set(const G4int& key, T& aHit) const
269 {
270  if(theCollection->find(key) != theCollection->end())
271  *(*theCollection)[key] = aHit;
272  else
273  {
274  value_type* hit = new value_type;
275  *hit = aHit;
276  (*theCollection)[key] = hit;
277  }
278  G4AutoLock l(&fMutex);
279  return theCollection->size();
280 }
281 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
282 template <typename T>
284 { }
285 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
286 template <typename T>
288 {
289  G4cout << "G4TAtomicHitsMap " << SDname << " / " << collectionName << " --- "
290  << entries() << " entries" << G4endl;
291 }
292 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
293 template <typename T>
295 {
296  G4AutoLock l(&fMutex);
297 
298  for(auto itr = theCollection->begin(); itr != theCollection->end(); itr++)
299  delete itr->second;
300 
301  theCollection->clear();
302 
303 }
304 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
305 
306 #endif