Geant4  10.02
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 67992 2013-03-13 10:59:57Z 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 description:
37 //
38 // This is a template class of hits map and parametrized by
39 // The concrete class of G4VHit. This is a uniform collection for
40 // a particular concrete hit class objects.
41 // An intermediate layer class G4HitsMap appeared in this
42 // header file is used just for G4Allocator, because G4Allocator
43 // cannot be instansiated with a template class. Thus G4HitsMap
44 // class MUST NOT be directly used by the user.
45 
46 template <typename T> class G4THitsMap : public G4HitsCollection
47 {
48  public:
49  G4THitsMap();
50  public: // with description
51  G4THitsMap(G4String detName,G4String colNam);
52  // constructor.
53  public:
54  virtual ~G4THitsMap();
55  G4int operator==(const G4THitsMap<T> &right) const;
56  G4THitsMap<T> & operator+=(const G4THitsMap<T> &right) const;
57 
58  public: // with description
59  virtual void DrawAllHits();
60  virtual void PrintAllHits();
61  // These two methods invokes Draw() and Print() methods of all of
62  // hit objects stored in this map, respectively.
63 
64  public: // with description
65  inline T* operator[](G4int key) const;
66 
67  // Returns a pointer to a concrete hit object.
68  inline std::map<G4int,T*>* GetMap() const
69  { return (std::map<G4int,T*>*)theCollection; }
70  // Returns a collection map.
71  inline G4int add(const G4int & key, T * &aHit) const;
72  inline G4int add(const G4int & key, T &aHit) const;
73  // Insert a hit object. Total number of hit objects stored in this
74  // map is returned.
75  inline G4int set(const G4int & key, T * &aHit) const;
76  inline G4int set(const G4int & key, T &aHit) const;
77  // Overwrite a hit object. Total number of hit objects stored in this
78  // map is returned.
79  inline G4int entries() const
80  { return ((std::map<G4int,T*>*)theCollection)->size(); }
81  // Returns the number of hit objects stored in this map
82  inline void clear();
83 
84  public:
85  virtual G4VHit* GetHit(size_t) const {return 0;}
86  virtual size_t GetSize() const
87  { return ((std::map<G4int,T*>*)theCollection)->size(); }
88 
89 };
90 
91 template <typename T> G4THitsMap<T>::G4THitsMap()
92 {
93  theCollection = (void*)new std::map<G4int,T*>;
94 }
95 
96 template <typename T> G4THitsMap<T>::G4THitsMap(G4String detName,G4String colNam)
97  : G4HitsCollection(detName,colNam)
98 {
99  theCollection = (void*)new std::map<G4int,T*>;
100 }
101 
102 template <typename T> G4THitsMap<T>::~G4THitsMap()
103 {
104  typename std::map<G4int,T*> * theHitsMap = GetMap();
105  typename std::map<G4int,T*>::iterator itr = theHitsMap->begin();
106  for(; itr != theHitsMap->end(); itr++) {
107  delete itr->second;
108  }
109 
110  delete theHitsMap;
111 }
112 
113 template <typename T> G4int G4THitsMap<T>::operator==(const G4THitsMap<T> &right) const
114 { return (collectionName==right.collectionName); }
115 
116 template <typename T> G4THitsMap<T> &
118 {
119  std::map<G4int,T*> * aHitsMap = right.GetMap();
120  typename std::map<G4int,T*>::iterator itr = aHitsMap->begin();
121  for(; itr != aHitsMap->end(); itr++) {
122  add(itr->first, *(itr->second));
123  }
124  return (G4THitsMap<T>&)(*this);
125 }
126 
127 template <typename T> inline T*
129  std::map<G4int,T*> * theHitsMap = GetMap();
130  if(theHitsMap->find(key) != theHitsMap->end()) {
131  return theHitsMap->find(key)->second;
132  } else {
133  return 0;
134  }
135 }
136 
137 template <typename T> inline G4int
138 G4THitsMap<T>::add(const G4int & key, T * &aHit) const {
139 
140  typename std::map<G4int,T*> * theHitsMap = GetMap();
141  if(theHitsMap->find(key) != theHitsMap->end()) {
142  *(*theHitsMap)[key] += *aHit;
143  } else {
144  (*theHitsMap)[key] = aHit;
145  }
146  return theHitsMap->size();
147 }
148 
149 template <typename T> inline G4int
150 G4THitsMap<T>::add(const G4int & key, T &aHit) const {
151 
152  typename std::map<G4int,T*> * theHitsMap = GetMap();
153  if(theHitsMap->find(key) != theHitsMap->end()) {
154  *(*theHitsMap)[key] += aHit;
155  } else {
156  T * hit = new T;
157  *hit = aHit;
158  (*theHitsMap)[key] = hit;
159  }
160 
161  return theHitsMap->size();
162 }
163 
164 template <typename T> inline G4int
165 G4THitsMap<T>::set(const G4int & key, T * &aHit) const {
166 
167  typename std::map<G4int,T*> * theHitsMap = GetMap();
168  if(theHitsMap->find(key) != theHitsMap->end()) {
169  delete (*theHitsMap)[key]->second;
170  }
171  (*theHitsMap)[key] = aHit;
172  return theHitsMap->size();
173 }
174 
175 template <typename T> inline G4int
176 G4THitsMap<T>::set(const G4int & key, T &aHit) const {
177 
178  typename std::map<G4int,T*> * theHitsMap = GetMap();
179  if(theHitsMap->find(key) != theHitsMap->end()) {
180  *(*theHitsMap)[key] = aHit;
181  } else {
182  T * hit = new T;
183  *hit = aHit;
184  (*theHitsMap)[key] = hit;
185  }
186 
187  return theHitsMap->size();
188 }
189 
190 template <typename T> void G4THitsMap<T>::DrawAllHits()
191 {;}
192 
193 template <typename T> void G4THitsMap<T>::PrintAllHits()
194 {
195  G4cout << "G4THitsMap " << SDname << " / " << collectionName << " --- " << entries() << " entries" << G4endl;
196 /*----- commented out for the use-case where <T> cannot be initialized
197  to be zero or does not support += operator.
198  std::map<G4int,T*> * theHitsMap = GetMap();
199  typename std::map<G4int, T*>::iterator itr = theHitsMap->begin();
200  T sum = 0.;
201  for(; itr != theHitsMap->end(); itr++) {
203  sum += *(itr->second);
204  }
205  G4cout << " Total : " << sum << G4endl;
206 ----------------------------------------------------------------------*/
207 }
208 
209 template <typename T> void G4THitsMap<T>::clear() {
210 
211  std::map<G4int,T*> * theHitsMap = GetMap();
212  typename std::map<G4int, T*>::iterator itr = theHitsMap->begin();
213  for(; itr != theHitsMap->end(); itr++) {
214  delete itr->second;
215  }
216  theHitsMap->clear();
217 
218 }
219 
220 #endif
221 
void clear()
Definition: G4THitsMap.hh:209
G4int operator==(const G4THitsMap< T > &right) const
Definition: G4THitsMap.hh:113
virtual void DrawAllHits()
Definition: G4THitsMap.hh:190
T * operator[](G4int key) const
Definition: G4THitsMap.hh:128
Definition: G4VHit.hh:48
int G4int
Definition: G4Types.hh:78
G4THitsMap< T > & operator+=(const G4THitsMap< T > &right) const
Definition: G4THitsMap.hh:117
G4GLOB_DLL std::ostream G4cout
G4int add(const G4int &key, T *&aHit) const
Definition: G4THitsMap.hh:138
virtual size_t GetSize() const
Definition: G4THitsMap.hh:86
G4int entries() const
Definition: G4THitsMap.hh:79
virtual void PrintAllHits()
Definition: G4THitsMap.hh:193
std::map< G4int, T * > * GetMap() const
Definition: G4THitsMap.hh:68
virtual ~G4THitsMap()
Definition: G4THitsMap.hh:102
#define G4endl
Definition: G4ios.hh:61
virtual G4VHit * GetHit(size_t) const
Definition: G4THitsMap.hh:85
G4int set(const G4int &key, T *&aHit) const
Definition: G4THitsMap.hh:165