source: trunk/source/digits_hits/hits/include/G4THitsMap.hh @ 1337

Last change on this file since 1337 was 1337, checked in by garnier, 14 years ago

tag geant4.9.4 beta 1 + modifs locales

File size: 7.8 KB
Line 
1//
2// ********************************************************************
3// * License and Disclaimer                                           *
4// *                                                                  *
5// * The  Geant4 software  is  copyright of the Copyright Holders  of *
6// * the Geant4 Collaboration.  It is provided  under  the terms  and *
7// * conditions of the Geant4 Software License,  included in the file *
8// * LICENSE and available at  http://cern.ch/geant4/license .  These *
9// * include a list of copyright holders.                             *
10// *                                                                  *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work  make  any representation or  warranty, express or implied, *
14// * regarding  this  software system or assume any liability for its *
15// * use.  Please see the license in the file  LICENSE  and URL above *
16// * for the full disclaimer and the limitation of liability.         *
17// *                                                                  *
18// * This  code  implementation is the result of  the  scientific and *
19// * technical work of the GEANT4 collaboration.                      *
20// * By using,  copying,  modifying or  distributing the software (or *
21// * any work based  on the software)  you  agree  to acknowledge its *
22// * use  in  resulting  scientific  publications,  and indicate your *
23// * acceptance of all terms of the Geant4 Software license.          *
24// ********************************************************************
25//
26//
27// $Id: G4THitsMap.hh,v 1.9 2007/08/30 05:13:03 asaim Exp $
28// GEANT4 tag $Name: geant4-09-04-beta-01 $
29//
30#ifndef G4THitsMap_h
31#define G4THitsMap_h 1
32
33#include "G4THitsCollection.hh"
34#include "globals.hh"
35#include <map>
36
37// class description:
38//
39//  This is a template class of hits map and parametrized by
40// The concrete class of G4VHit. This is a uniform collection for
41// a particular concrete hit class objects.
42//  An intermediate layer class G4HitsMap appeared in this
43// header file is used just for G4Allocator, because G4Allocator
44// cannot be instansiated with a template class. Thus G4HitsMap
45// class MUST NOT be directly used by the user.
46
47template <typename T> class G4THitsMap : public G4HitsCollection
48{
49  public:
50      G4THitsMap();
51  public: // with description
52      G4THitsMap(G4String detName,G4String colNam);
53      // constructor.
54  public:
55      virtual ~G4THitsMap();
56      G4int operator==(const G4THitsMap<T> &right) const;
57      G4THitsMap<T> & operator+=(const G4THitsMap<T> &right) const;
58
59  public: // with description
60      virtual void DrawAllHits();
61      virtual void PrintAllHits();
62      //  These two methods invokes Draw() and Print() methods of all of
63      // hit objects stored in this map, respectively.
64
65  public: // with description
66      inline T* operator[](G4int key) const;
67
68      //  Returns a pointer to a concrete hit object.
69      inline std::map<G4int,T*>* GetMap() const
70      { return (std::map<G4int,T*>*)theCollection; }
71      //  Returns a collection map.
72      inline G4int add(const G4int & key, T * &aHit) const;
73      inline G4int add(const G4int & key, T &aHit) const;
74      //  Insert a hit object. Total number of hit objects stored in this
75      // map is returned.
76      inline G4int set(const G4int & key, T * &aHit) const;
77      inline G4int set(const G4int & key, T &aHit) const;
78      //  Overwrite a hit object. Total number of hit objects stored in this
79      // map is returned.
80      inline G4int entries() const
81      { return ((std::map<G4int,T*>*)theCollection)->size(); }
82      //  Returns the number of hit objects stored in this map
83      inline void clear();
84
85  public:
86    virtual G4VHit* GetHit(size_t) const {return 0;}
87    virtual size_t GetSize() const
88    { return ((std::map<G4int,T*>*)theCollection)->size(); }
89
90};
91
92template <typename T> G4THitsMap<T>::G4THitsMap()
93{ 
94  theCollection = (void*)new std::map<G4int,T*>;
95}
96
97template <typename T> G4THitsMap<T>::G4THitsMap(G4String detName,G4String colNam)
98    : G4HitsCollection(detName,colNam)
99{ 
100    theCollection = (void*)new std::map<G4int,T*>;
101}
102
103template <typename T> G4THitsMap<T>::~G4THitsMap()
104{
105  typename std::map<G4int,T*> * theHitsMap = GetMap();
106  typename std::map<G4int,T*>::iterator itr = theHitsMap->begin();
107  for(; itr != theHitsMap->end(); itr++) {
108      delete itr->second;
109  }
110
111  delete theHitsMap;
112}
113
114template <typename T> G4int G4THitsMap<T>::operator==(const G4THitsMap<T> &right) const
115{ return (collectionName==right.collectionName); }
116
117template <typename T> G4THitsMap<T> &
118G4THitsMap<T>::operator+=(const G4THitsMap<T> &right) const
119{
120    std::map<G4int,T*> * aHitsMap = right.GetMap();
121    typename std::map<G4int,T*>::iterator itr = aHitsMap->begin();
122    for(; itr != aHitsMap->end(); itr++) {
123        add(itr->first, *(itr->second));
124    }
125    return (G4THitsMap<T>&)(*this);
126}
127
128template <typename T> inline T* 
129G4THitsMap<T>::operator[](G4int key) const {
130    std::map<G4int,T*> * theHitsMap = GetMap();
131    if(theHitsMap->find(key) != theHitsMap->end()) {
132        return theHitsMap->find(key)->second;
133    } else {
134        return 0;
135    }
136}
137
138template <typename T> inline G4int
139G4THitsMap<T>::add(const G4int & key, T * &aHit) const {
140
141    typename std::map<G4int,T*> * theHitsMap = GetMap();
142    if(theHitsMap->find(key) != theHitsMap->end()) {
143        *(*theHitsMap)[key] += *aHit;
144    } else {
145        (*theHitsMap)[key] = aHit;
146    }
147    return theHitsMap->size();
148}
149
150template <typename T> inline G4int
151G4THitsMap<T>::add(const G4int & key, T &aHit) const {
152
153    typename std::map<G4int,T*> * theHitsMap = GetMap();
154    if(theHitsMap->find(key) != theHitsMap->end()) {
155        *(*theHitsMap)[key] += aHit;
156    } else {
157        T * hit = new T;
158        *hit = aHit;
159        (*theHitsMap)[key] = hit;
160    }
161
162    return theHitsMap->size();
163}
164
165template <typename T> inline G4int
166G4THitsMap<T>::set(const G4int & key, T * &aHit) const {
167                                                                                             
168    typename std::map<G4int,T*> * theHitsMap = GetMap();
169    if(theHitsMap->find(key) != theHitsMap->end()) {
170        delete (*theHitsMap)[key]->second;
171    }
172    (*theHitsMap)[key] = aHit;
173    return theHitsMap->size();
174}
175                                                                                             
176template <typename T> inline G4int
177G4THitsMap<T>::set(const G4int & key, T &aHit) const {
178                                                                                             
179    typename std::map<G4int,T*> * theHitsMap = GetMap();
180    if(theHitsMap->find(key) != theHitsMap->end()) {
181        *(*theHitsMap)[key] = aHit;
182    } else {
183        T * hit = new T;
184        *hit = aHit;
185        (*theHitsMap)[key] = hit;
186    }
187                                                                                             
188    return theHitsMap->size();
189}
190                                                                                             
191template <typename T> void G4THitsMap<T>::DrawAllHits() 
192{;}
193
194template <typename T> void G4THitsMap<T>::PrintAllHits() 
195{
196 G4cout << "G4THitsMap " << SDname << " / " << collectionName << " --- " << entries() << " entries" << G4endl;
197 std::map<G4int,T*> * theHitsMap = GetMap();
198 typename std::map<G4int, T*>::iterator itr = theHitsMap->begin();
199 G4double sum = 0.;
200 for(; itr != theHitsMap->end(); itr++) {
201  ///////////////////////////////G4cout << "  " << itr->first << " : " << *(itr->second) << G4endl;
202  sum += *(itr->second);
203 }
204 G4cout << "             Total : " << sum << G4endl;
205}
206
207template <typename T> void G4THitsMap<T>::clear() {
208
209    std::map<G4int,T*> * theHitsMap = GetMap();
210    typename std::map<G4int, T*>::iterator itr = theHitsMap->begin();
211    for(; itr != theHitsMap->end(); itr++) {
212        delete itr->second;
213    }
214    theHitsMap->clear();
215
216}
217
218#endif
219
Note: See TracBrowser for help on using the repository browser.