00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef G4THitsMap_h
00031 #define G4THitsMap_h 1
00032
00033 #include "G4THitsCollection.hh"
00034 #include "globals.hh"
00035 #include <map>
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 template <typename T> class G4THitsMap : public G4HitsCollection
00048 {
00049 public:
00050 G4THitsMap();
00051 public:
00052 G4THitsMap(G4String detName,G4String colNam);
00053
00054 public:
00055 virtual ~G4THitsMap();
00056 G4int operator==(const G4THitsMap<T> &right) const;
00057 G4THitsMap<T> & operator+=(const G4THitsMap<T> &right) const;
00058
00059 public:
00060 virtual void DrawAllHits();
00061 virtual void PrintAllHits();
00062
00063
00064
00065 public:
00066 inline T* operator[](G4int key) const;
00067
00068
00069 inline std::map<G4int,T*>* GetMap() const
00070 { return (std::map<G4int,T*>*)theCollection; }
00071
00072 inline G4int add(const G4int & key, T * &aHit) const;
00073 inline G4int add(const G4int & key, T &aHit) const;
00074
00075
00076 inline G4int set(const G4int & key, T * &aHit) const;
00077 inline G4int set(const G4int & key, T &aHit) const;
00078
00079
00080 inline G4int entries() const
00081 { return ((std::map<G4int,T*>*)theCollection)->size(); }
00082
00083 inline void clear();
00084
00085 public:
00086 virtual G4VHit* GetHit(size_t) const {return 0;}
00087 virtual size_t GetSize() const
00088 { return ((std::map<G4int,T*>*)theCollection)->size(); }
00089
00090 };
00091
00092 template <typename T> G4THitsMap<T>::G4THitsMap()
00093 {
00094 theCollection = (void*)new std::map<G4int,T*>;
00095 }
00096
00097 template <typename T> G4THitsMap<T>::G4THitsMap(G4String detName,G4String colNam)
00098 : G4HitsCollection(detName,colNam)
00099 {
00100 theCollection = (void*)new std::map<G4int,T*>;
00101 }
00102
00103 template <typename T> G4THitsMap<T>::~G4THitsMap()
00104 {
00105 typename std::map<G4int,T*> * theHitsMap = GetMap();
00106 typename std::map<G4int,T*>::iterator itr = theHitsMap->begin();
00107 for(; itr != theHitsMap->end(); itr++) {
00108 delete itr->second;
00109 }
00110
00111 delete theHitsMap;
00112 }
00113
00114 template <typename T> G4int G4THitsMap<T>::operator==(const G4THitsMap<T> &right) const
00115 { return (collectionName==right.collectionName); }
00116
00117 template <typename T> G4THitsMap<T> &
00118 G4THitsMap<T>::operator+=(const G4THitsMap<T> &right) const
00119 {
00120 std::map<G4int,T*> * aHitsMap = right.GetMap();
00121 typename std::map<G4int,T*>::iterator itr = aHitsMap->begin();
00122 for(; itr != aHitsMap->end(); itr++) {
00123 add(itr->first, *(itr->second));
00124 }
00125 return (G4THitsMap<T>&)(*this);
00126 }
00127
00128 template <typename T> inline T*
00129 G4THitsMap<T>::operator[](G4int key) const {
00130 std::map<G4int,T*> * theHitsMap = GetMap();
00131 if(theHitsMap->find(key) != theHitsMap->end()) {
00132 return theHitsMap->find(key)->second;
00133 } else {
00134 return 0;
00135 }
00136 }
00137
00138 template <typename T> inline G4int
00139 G4THitsMap<T>::add(const G4int & key, T * &aHit) const {
00140
00141 typename std::map<G4int,T*> * theHitsMap = GetMap();
00142 if(theHitsMap->find(key) != theHitsMap->end()) {
00143 *(*theHitsMap)[key] += *aHit;
00144 } else {
00145 (*theHitsMap)[key] = aHit;
00146 }
00147 return theHitsMap->size();
00148 }
00149
00150 template <typename T> inline G4int
00151 G4THitsMap<T>::add(const G4int & key, T &aHit) const {
00152
00153 typename std::map<G4int,T*> * theHitsMap = GetMap();
00154 if(theHitsMap->find(key) != theHitsMap->end()) {
00155 *(*theHitsMap)[key] += aHit;
00156 } else {
00157 T * hit = new T;
00158 *hit = aHit;
00159 (*theHitsMap)[key] = hit;
00160 }
00161
00162 return theHitsMap->size();
00163 }
00164
00165 template <typename T> inline G4int
00166 G4THitsMap<T>::set(const G4int & key, T * &aHit) const {
00167
00168 typename std::map<G4int,T*> * theHitsMap = GetMap();
00169 if(theHitsMap->find(key) != theHitsMap->end()) {
00170 delete (*theHitsMap)[key]->second;
00171 }
00172 (*theHitsMap)[key] = aHit;
00173 return theHitsMap->size();
00174 }
00175
00176 template <typename T> inline G4int
00177 G4THitsMap<T>::set(const G4int & key, T &aHit) const {
00178
00179 typename std::map<G4int,T*> * theHitsMap = GetMap();
00180 if(theHitsMap->find(key) != theHitsMap->end()) {
00181 *(*theHitsMap)[key] = aHit;
00182 } else {
00183 T * hit = new T;
00184 *hit = aHit;
00185 (*theHitsMap)[key] = hit;
00186 }
00187
00188 return theHitsMap->size();
00189 }
00190
00191 template <typename T> void G4THitsMap<T>::DrawAllHits()
00192 {;}
00193
00194 template <typename T> void G4THitsMap<T>::PrintAllHits()
00195 {
00196 G4cout << "G4THitsMap " << SDname << " / " << collectionName << " --- " << entries() << " entries" << G4endl;
00197 std::map<G4int,T*> * theHitsMap = GetMap();
00198 typename std::map<G4int, T*>::iterator itr = theHitsMap->begin();
00199 G4double sum = 0.;
00200 for(; itr != theHitsMap->end(); itr++) {
00202 sum += *(itr->second);
00203 }
00204 G4cout << " Total : " << sum << G4endl;
00205 }
00206
00207 template <typename T> void G4THitsMap<T>::clear() {
00208
00209 std::map<G4int,T*> * theHitsMap = GetMap();
00210 typename std::map<G4int, T*>::iterator itr = theHitsMap->begin();
00211 for(; itr != theHitsMap->end(); itr++) {
00212 delete itr->second;
00213 }
00214 theHitsMap->clear();
00215
00216 }
00217
00218 #endif
00219