Geant4  10.03
G4THitsMap.hh
Go to the documentation of this file.
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 //
26 //
27 // $Id: G4THitsMap.hh 99262 2016-09-09 13:18:19Z gcosmo $
28 //
29 #ifndef G4THitsMap_h
30 #define G4THitsMap_h 1
31 
32 #include "G4THitsCollection.hh"
33 #include "globals.hh"
34 #include <map>
35 
36 class G4StatDouble;
37 
38 // class description:
39 //
40 // This is a template class of hits map and parametrized by
41 // The concrete class of G4VHit. This is a uniform collection for
42 // a particular concrete hit class objects.
43 // An intermediate layer class G4HitsMap appeared in this
44 // header file is used just for G4Allocator, because G4Allocator
45 // cannot be instansiated with a template class. Thus G4HitsMap
46 // class MUST NOT be directly used by the user.
47 
48 template <typename T> class G4THitsMap : public G4HitsCollection
49 {
50  public:
51  G4THitsMap();
52  public: // with description
53  G4THitsMap(G4String detName,G4String colNam);
54  // constructor.
55  public:
56  virtual ~G4THitsMap();
57  G4int operator==(const G4THitsMap<T> &right) const;
58 
59  public: // with description
60  // Operator += between same kind of classes
61  template <typename U = T,
62  typename std::enable_if<std::is_same<U,T>::value,int>::type=0>
63  G4THitsMap<T> & operator+=(const G4THitsMap<T> &right) const
64  {
65  std::map<G4int,T*> * aHitsMap = right.GetMap();
66  typename std::map<G4int,T*>::iterator itr = aHitsMap->begin();
67  for(; itr != aHitsMap->end(); itr++) {
68  add(itr->first, *(itr->second));
69  }
70  return (G4THitsMap<T>&)(*this);
71  }
72  // Operator for G4THitsMap<G4StatDouble> += G4THitsMap<G4double>
73  template <typename U = T,
74  typename std::enable_if<std::is_same<U,G4double>::value &&
75  std::is_same<T,G4StatDouble>::value,int>::type=0>
76  G4THitsMap<T> & operator+=(const G4THitsMap<U> &right) const
77  {
78  std::map<G4int,U*> * aHitsMap = right.GetMap();
79  typename std::map<G4int,U*>::iterator itr = aHitsMap->begin();
80  for(; itr != aHitsMap->end(); itr++) {
81  typename std::map<G4int,T*>::iterator mapItr = this->GetMap()->find(itr->first);
82  if(mapItr==this->GetMap()->end())
83  { (*this->GetMap())[itr->first] = new T(0.); }
84  add(itr->first, *(itr->second));
85  }
86  return (G4THitsMap<T>&)(*this);
87  }
88 
89  public: // with description
90  virtual void DrawAllHits();
91  virtual void PrintAllHits();
92  // These two methods invokes Draw() and Print() methods of all of
93  // hit objects stored in this map, respectively.
94 
95  public: // with description
96  // Returns a pointer to a concrete hit object.
97  inline T* operator[](G4int key) const;
98  // Returns a collection map.
99  inline std::map<G4int,T*>* GetMap() const
100  { return (std::map<G4int,T*>*)theCollection; }
101 
102  // Insert a hit object. Total number of hit objects stored in this
103  // map is returned.
104  template <typename U = T,
105  typename std::enable_if<std::is_same<U,T>::value,int>::type=0>
106  inline G4int add(const G4int & key, T * &aHit) const
107  {
108  typename std::map<G4int,T*> * theHitsMap = GetMap();
109  if(theHitsMap->find(key) != theHitsMap->end()) {
110  *(*theHitsMap)[key] += *aHit;
111  } else {
112  (*theHitsMap)[key] = aHit;
113  }
114  return theHitsMap->size();
115  }
116  template <typename U = T,
117  typename std::enable_if<std::is_same<U,T>::value,int>::type=0>
118  inline G4int add(const G4int & key, T &aHit) const
119  {
120  typename std::map<G4int,T*> * theHitsMap = GetMap();
121  if(theHitsMap->find(key) == theHitsMap->end()) {
122  (*theHitsMap)[key] = new T(0.);
123  }
124  *(*theHitsMap)[key] += aHit;
125  return theHitsMap->size();
126  }
127  template <typename U = T,
128  typename std::enable_if<std::is_same<U,G4double>::value &&
129  std::is_same<T,G4StatDouble>::value,int>::type=0>
130  inline G4int add(const G4int & key, U * &aHit) const
131  {
132  typename std::map<G4int,T*> * theHitsMap = GetMap();
133  if(theHitsMap->find(key) == theHitsMap->end()) {
134  (*theHitsMap)[key] = new T(0.);
135  }
136  *(*theHitsMap)[key] += *aHit;
137  return theHitsMap->size();
138  }
139  template <typename U = T,
140  typename std::enable_if<std::is_same<U,G4double>::value &&
141  std::is_same<T,G4StatDouble>::value,int>::type=0>
142  inline G4int add(const G4int & key, U &aHit) const
143  {
144  typename std::map<G4int,T*> * theHitsMap = GetMap();
145  if(theHitsMap->find(key) == theHitsMap->end()) {
146  (*theHitsMap)[key] = new T(0.);
147  }
148  *(*theHitsMap)[key] += aHit;
149  return theHitsMap->size();
150  }
151 
152  // Overwrite a hit object. Total number of hit objects stored in this
153  // map is returned.
154  template <typename U = T,
155  typename std::enable_if<std::is_same<U,T>::value,int>::type=0>
156  inline G4int set(const G4int & key, T * &aHit) const
157  {
158  typename std::map<G4int,T*> * theHitsMap = GetMap();
159  if(theHitsMap->find(key) != theHitsMap->end()) {
160  delete (*theHitsMap)[key]->second;
161  }
162  (*theHitsMap)[key] = aHit;
163  return theHitsMap->size();
164  }
165  template <typename U = T,
166  typename std::enable_if<std::is_same<U,T>::value,int>::type=0>
167  inline G4int set(const G4int & key, T &aHit) const
168  {
169  typename std::map<G4int,T*> * theHitsMap = GetMap();
170  if(theHitsMap->find(key) == theHitsMap->end())
171  { (*theHitsMap)[key] = new T(0.); }
172  *(*theHitsMap)[key] = aHit;
173  return theHitsMap->size();
174  }
175  template <typename U = T,
176  typename std::enable_if<std::is_same<U,G4double>::value &&
177  std::is_same<T,G4StatDouble>::value,int>::type=0>
178  inline G4int set(const G4int & key, U * &aHit) const
179  {
180  typename std::map<G4int,T*> * theHitsMap = GetMap();
181  if(theHitsMap->find(key) != theHitsMap->end()) {
182  delete (*theHitsMap)[key]->second;
183  }
184  (*theHitsMap)[key] = aHit;
185  return theHitsMap->size();
186  }
187  template <typename U = T,
188  typename std::enable_if<std::is_same<U,G4double>::value &&
189  std::is_same<T,G4StatDouble>::value,int>::type=0>
190  inline G4int set(const G4int & key, U &aHit) const
191  {
192  typename std::map<G4int,T*> * theHitsMap = GetMap();
193  if(theHitsMap->find(key) == theHitsMap->end())
194  { (*theHitsMap)[key] = new T(0.); }
195  *(*theHitsMap)[key] = aHit;
196  return theHitsMap->size();
197  }
198 
199  // Returns the number of hit objects stored in this map
200  inline G4int entries() const
201  { return ((std::map<G4int,T*>*)theCollection)->size(); }
202 
203  // Clear the entry
204  inline void clear();
205 
206  public:
207  virtual G4VHit* GetHit(size_t) const {return 0;}
208  virtual size_t GetSize() const
209  { return ((std::map<G4int,T*>*)theCollection)->size(); }
210 
211 };
212 
213 template <typename T> G4THitsMap<T>::G4THitsMap()
214 {
215  theCollection = (void*)new std::map<G4int,T*>;
216 }
217 
218 template <typename T> G4THitsMap<T>::G4THitsMap(G4String detName,G4String colNam)
219  : G4HitsCollection(detName,colNam)
220 {
221  theCollection = (void*)new std::map<G4int,T*>;
222 }
223 
224 template <typename T> G4THitsMap<T>::~G4THitsMap()
225 {
226  typename std::map<G4int,T*> * theHitsMap = GetMap();
227  typename std::map<G4int,T*>::iterator itr = theHitsMap->begin();
228  for(; itr != theHitsMap->end(); itr++) {
229  delete itr->second;
230  }
231 
232  delete theHitsMap;
233 }
234 
235 template <typename T> G4int G4THitsMap<T>::operator==(const G4THitsMap<T> &right) const
236 { return (collectionName==right.collectionName); }
237 
238 template <typename T> inline T*
240  std::map<G4int,T*> * theHitsMap = GetMap();
241  if(theHitsMap->find(key) != theHitsMap->end()) {
242  return theHitsMap->find(key)->second;
243  } else {
244  return 0;
245  }
246 }
247 
248 template <typename T> void G4THitsMap<T>::DrawAllHits()
249 {;}
250 
251 template <typename T> void G4THitsMap<T>::PrintAllHits()
252 {
253  G4cout << "G4THitsMap " << SDname << " / " << collectionName << " --- " << entries() << " entries" << G4endl;
254 /*----- commented out for the use-case where <T> cannot be initialized
255  to be zero or does not support += operator.
256  std::map<G4int,T*> * theHitsMap = GetMap();
257  typename std::map<G4int, T*>::iterator itr = theHitsMap->begin();
258  T sum = 0.;
259  for(; itr != theHitsMap->end(); itr++) {
261  sum += *(itr->second);
262  }
263  G4cout << " Total : " << sum << G4endl;
264 ----------------------------------------------------------------------*/
265 }
266 
267 template <typename T> void G4THitsMap<T>::clear() {
268 
269  std::map<G4int,T*> * theHitsMap = GetMap();
270  typename std::map<G4int, T*>::iterator itr = theHitsMap->begin();
271  for(; itr != theHitsMap->end(); itr++) {
272  delete itr->second;
273  }
274  theHitsMap->clear();
275 
276 }
277 
278 #endif
279 
void clear()
Definition: G4THitsMap.hh:267
G4int set(const G4int &key, T *&aHit) const
Definition: G4THitsMap.hh:156
G4int operator==(const G4THitsMap< T > &right) const
Definition: G4THitsMap.hh:235
G4int add(const G4int &key, U &aHit) const
Definition: G4THitsMap.hh:142
G4int set(const G4int &key, T &aHit) const
Definition: G4THitsMap.hh:167
virtual void DrawAllHits()
Definition: G4THitsMap.hh:248
T * operator[](G4int key) const
Definition: G4THitsMap.hh:239
G4int add(const G4int &key, T &aHit) const
Definition: G4THitsMap.hh:118
Definition: G4VHit.hh:48
int G4int
Definition: G4Types.hh:78
G4GLOB_DLL std::ostream G4cout
virtual size_t GetSize() const
Definition: G4THitsMap.hh:208
G4THitsMap< T > & operator+=(const G4THitsMap< U > &right) const
Definition: G4THitsMap.hh:76
G4int entries() const
Definition: G4THitsMap.hh:200
G4int add(const G4int &key, T *&aHit) const
Definition: G4THitsMap.hh:106
G4THitsMap< T > & operator+=(const G4THitsMap< T > &right) const
Definition: G4THitsMap.hh:63
virtual void PrintAllHits()
Definition: G4THitsMap.hh:251
G4int set(const G4int &key, U &aHit) const
Definition: G4THitsMap.hh:190
std::map< G4int, T * > * GetMap() const
Definition: G4THitsMap.hh:99
G4int add(const G4int &key, U *&aHit) const
Definition: G4THitsMap.hh:130
virtual ~G4THitsMap()
Definition: G4THitsMap.hh:224
#define G4endl
Definition: G4ios.hh:61
virtual G4VHit * GetHit(size_t) const
Definition: G4THitsMap.hh:207
G4int set(const G4int &key, U *&aHit) const
Definition: G4THitsMap.hh:178