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$
00028 //
00029 #ifndef G4THitsMap_h
00030 #define G4THitsMap_h 1
00031 
00032 #include "G4THitsCollection.hh"
00033 #include "globals.hh"
00034 #include <map>
00035 
00036 // class description:
00037 //
00038 //  This is a template class of hits map and parametrized by
00039 // The concrete class of G4VHit. This is a uniform collection for
00040 // a particular concrete hit class objects.
00041 //  An intermediate layer class G4HitsMap appeared in this
00042 // header file is used just for G4Allocator, because G4Allocator
00043 // cannot be instansiated with a template class. Thus G4HitsMap
00044 // class MUST NOT be directly used by the user.
00045 
00046 template <typename T> class G4THitsMap : public G4HitsCollection 
00047 {
00048   public:
00049       G4THitsMap();
00050   public: // with description
00051       G4THitsMap(G4String detName,G4String colNam);
00052       // constructor.
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: // with description
00059       virtual void DrawAllHits();
00060       virtual void PrintAllHits();
00061       //  These two methods invokes Draw() and Print() methods of all of
00062       // hit objects stored in this map, respectively.
00063 
00064   public: // with description
00065       inline T* operator[](G4int key) const;
00066 
00067       //  Returns a pointer to a concrete hit object.
00068       inline std::map<G4int,T*>* GetMap() const
00069       { return (std::map<G4int,T*>*)theCollection; }
00070       //  Returns a collection map.
00071       inline G4int add(const G4int & key, T * &aHit) const;
00072       inline G4int add(const G4int & key, T &aHit) const;
00073       //  Insert a hit object. Total number of hit objects stored in this
00074       // map is returned.
00075       inline G4int set(const G4int & key, T * &aHit) const;
00076       inline G4int set(const G4int & key, T &aHit) const;
00077       //  Overwrite a hit object. Total number of hit objects stored in this
00078       // map is returned.
00079       inline G4int entries() const
00080       { return ((std::map<G4int,T*>*)theCollection)->size(); }
00081       //  Returns the number of hit objects stored in this map
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 /*----- commented out for the use-case where <T> cannot be initialized
00197         to be zero or does not support += operator.
00198  std::map<G4int,T*> * theHitsMap = GetMap();
00199  typename std::map<G4int, T*>::iterator itr = theHitsMap->begin();
00200  T sum = 0.;
00201  for(; itr != theHitsMap->end(); itr++) {
00203   sum += *(itr->second);
00204  }
00205  G4cout << "             Total : " << sum << G4endl;
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 

Generated on Mon May 27 17:50:00 2013 for Geant4 by  doxygen 1.4.7