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 00030 #ifndef G4THitsCollection_h 00031 #define G4THitsCollection_h 1 00032 00033 #include "G4VHitsCollection.hh" 00034 #include "G4Allocator.hh" 00035 #include "globals.hh" 00036 //#include "g4rw/tpordvec.h" 00037 #include <vector> 00038 00039 // class description: 00040 // 00041 // This is a template class of hits collection and parametrized by 00042 // The concrete class of G4VHit. This is a uniform collection for 00043 // a particular concrete hit class objects. 00044 // An intermediate layer class G4HitsCollection appeared in this 00045 // header file is used just for G4Allocator, because G4Allocator 00046 // cannot be instansiated with a template class. Thus G4HitsCollection 00047 // class MUST NOT be directly used by the user. 00048 00049 class G4HitsCollection : public G4VHitsCollection 00050 { 00051 public: 00052 G4HitsCollection(); 00053 G4HitsCollection(G4String detName,G4String colNam); 00054 virtual ~G4HitsCollection(); 00055 G4int operator==(const G4HitsCollection &right) const; 00056 00057 protected: 00058 void* theCollection; 00059 }; 00060 00061 #if defined G4DIGI_ALLOC_EXPORT 00062 extern G4DLLEXPORT G4Allocator<G4HitsCollection> anHCAllocator; 00063 #else 00064 extern G4DLLIMPORT G4Allocator<G4HitsCollection> anHCAllocator; 00065 #endif 00066 00067 template <class T> class G4THitsCollection : public G4HitsCollection 00068 { 00069 public: 00070 G4THitsCollection(); 00071 public: // with description 00072 G4THitsCollection(G4String detName,G4String colNam); 00073 // constructor. 00074 public: 00075 virtual ~G4THitsCollection(); 00076 G4int operator==(const G4THitsCollection<T> &right) const; 00077 00078 inline void *operator new(size_t); 00079 inline void operator delete(void* anHC); 00080 public: // with description 00081 virtual void DrawAllHits(); 00082 virtual void PrintAllHits(); 00083 // These two methods invokes Draw() and Print() methods of all of 00084 // hit objects stored in this collection, respectively. 00085 00086 public: // with description 00087 inline T* operator[](size_t i) const 00088 { return (*((std::vector<T*>*)theCollection))[i]; } 00089 // Returns a pointer to a concrete hit object. 00090 inline std::vector<T*>* GetVector() const 00091 { return (std::vector<T*>*)theCollection; } 00092 // Returns a collection vector. 00093 inline G4int insert(T* aHit) 00094 { 00095 std::vector<T*>*theHitsCollection 00096 = (std::vector<T*>*)theCollection; 00097 theHitsCollection->push_back(aHit); 00098 return theHitsCollection->size(); 00099 } 00100 // Insert a hit object. Total number of hit objects stored in this 00101 // collection is returned. 00102 inline G4int entries() const 00103 { 00104 std::vector<T*>*theHitsCollection 00105 = (std::vector<T*>*)theCollection; 00106 return theHitsCollection->size(); 00107 } 00108 // Returns the number of hit objects stored in this collection 00109 00110 public: 00111 virtual G4VHit* GetHit(size_t i) const 00112 { return (*((std::vector<T*>*)theCollection))[i]; } 00113 virtual size_t GetSize() const 00114 { return ((std::vector<T*>*)theCollection)->size(); } 00115 00116 }; 00117 00118 template <class T> inline void* G4THitsCollection<T>::operator new(size_t) 00119 { 00120 void* anHC; 00121 anHC = (void*)anHCAllocator.MallocSingle(); 00122 return anHC; 00123 } 00124 00125 template <class T> inline void G4THitsCollection<T>::operator delete(void* anHC) 00126 { 00127 anHCAllocator.FreeSingle((G4HitsCollection*)anHC); 00128 } 00129 00130 template <class T> G4THitsCollection<T>::G4THitsCollection() 00131 { 00132 std::vector<T*> * theHitsCollection 00133 = new std::vector<T*>; 00134 theCollection = (void*)theHitsCollection; 00135 } 00136 00137 template <class T> G4THitsCollection<T>::G4THitsCollection(G4String detName,G4String colNam) 00138 : G4HitsCollection(detName,colNam) 00139 { 00140 std::vector<T*> * theHitsCollection 00141 = new std::vector<T*>; 00142 theCollection = (void*)theHitsCollection; 00143 } 00144 00145 template <class T> G4THitsCollection<T>::~G4THitsCollection() 00146 { 00147 std::vector<T*> * theHitsCollection 00148 = (std::vector<T*>*)theCollection; 00149 //theHitsCollection->clearAndDestroy(); 00150 for(size_t i=0;i<theHitsCollection->size();i++) 00151 { delete (*theHitsCollection)[i]; } 00152 theHitsCollection->clear(); 00153 delete theHitsCollection; 00154 } 00155 00156 template <class T> G4int G4THitsCollection<T>::operator==(const G4THitsCollection<T> &right) const 00157 { return (collectionName==right.collectionName); } 00158 00159 template <class T> void G4THitsCollection<T>::DrawAllHits() 00160 { 00161 std::vector<T*> * theHitsCollection 00162 = (std::vector<T*>*)theCollection; 00163 size_t n = theHitsCollection->size(); 00164 for(size_t i=0;i<n;i++) 00165 { (*theHitsCollection)[i]->Draw(); } 00166 } 00167 00168 template <class T> void G4THitsCollection<T>::PrintAllHits() 00169 { 00170 std::vector<T*> * theHitsCollection 00171 = (std::vector<T*>*)theCollection; 00172 size_t n = theHitsCollection->size(); 00173 for(size_t i=0;i<n;i++) 00174 { (*theHitsCollection)[i]->Print(); } 00175 } 00176 00177 #endif 00178