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