digits_hits/hits/include/G4THitsMap.hh

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id: G4THitsMap.hh,v 1.9 2007/08/30 05:13:03 asaim Exp $
00028 // GEANT4 tag $Name: geant4-09-01-patch-01 $
00029 //
00030 #ifndef G4THitsMap_h
00031 #define G4THitsMap_h 1
00032 
00033 #include "G4THitsCollection.hh"
00034 #include "globals.hh"
00035 #include <map>
00036 
00037 // class description:
00038 //
00039 //  This is a template class of hits map and parametrized by
00040 // The concrete class of G4VHit. This is a uniform collection for
00041 // a particular concrete hit class objects.
00042 //  An intermediate layer class G4HitsMap appeared in this
00043 // header file is used just for G4Allocator, because G4Allocator
00044 // cannot be instansiated with a template class. Thus G4HitsMap
00045 // class MUST NOT be directly used by the user.
00046 
00047 template <typename T> class G4THitsMap : public G4HitsCollection 
00048 {
00049   public:
00050       G4THitsMap();
00051   public: // with description
00052       G4THitsMap(G4String detName,G4String colNam);
00053       // constructor.
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: // with description
00060       virtual void DrawAllHits();
00061       virtual void PrintAllHits();
00062       //  These two methods invokes Draw() and Print() methods of all of
00063       // hit objects stored in this map, respectively.
00064 
00065   public: // with description
00066       inline T* operator[](G4int key) const;
00067 
00068       //  Returns a pointer to a concrete hit object.
00069       inline std::map<G4int,T*>* GetMap() const
00070       { return (std::map<G4int,T*>*)theCollection; }
00071       //  Returns a collection map.
00072       inline G4int add(const G4int & key, T * &aHit) const;
00073       inline G4int add(const G4int & key, T &aHit) const;
00074       //  Insert a hit object. Total number of hit objects stored in this
00075       // map is returned.
00076       inline G4int set(const G4int & key, T * &aHit) const;
00077       inline G4int set(const G4int & key, T &aHit) const;
00078       //  Overwrite a hit object. Total number of hit objects stored in this
00079       // map is returned.
00080       inline G4int entries() const
00081       { return ((std::map<G4int,T*>*)theCollection)->size(); }
00082       //  Returns the number of hit objects stored in this map
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 

Generated on Fri Apr 11 17:09:46 2008 for Geant4 by  doxygen 1.4.7