Geant4  10.02.p02
TSRun.cc
Go to the documentation of this file.
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 //
28 //
29 //
30 // $Id: TSRun.cc 93110 2015-11-05 08:37:42Z jmadsen $
31 //
32 //
50 //
51 //
52 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
53 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
54 
55 
56 #include "TSRun.hh"
57 
58 #include "G4SDManager.hh"
60 #include "G4VPrimitiveScorer.hh"
61 
62 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
63 
64 std::vector<G4TAtomicHitsMap<G4double>*> TSRun::fAtomicRunMaps;
65 
66 std::map<G4String, TSRun::MutexHitsMap_t> TSRun::fMutexRunMaps;
67 
68 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
69 
70 TSRun::TSRun(const G4String& mfd_name)
71 : G4Run()
72 {
73  ConstructMFD(mfd_name);
74 }
75 
76 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
77 
79 {
80  //--- Clear HitsMap for RUN
81  for(unsigned i = 0; i < fRunMaps.size(); ++i)
82  fRunMaps[i]->clear();
83 
85  for(unsigned i = 0; i < fAtomicRunMaps.size(); ++i)
86  fAtomicRunMaps[i]->clear();
87 
88 }
89 
90 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
91 
92 // clear all data members.
93 void TSRun::ConstructMFD(const G4String& mfdName)
94 {
95 
97  //=================================================
98  // Initalize RunMaps for accumulation.
99  // Get CollectionIDs for HitCollections.
100  //=================================================
103  //
104  if ( mfd )
105  {
106  //--- Loop over the registered primitive scorers.
107  for (G4int icol = 0; icol < mfd->GetNumberOfPrimitives(); icol++){
108  // Get Primitive Scorer object.
109  G4VPrimitiveScorer* scorer = mfd->GetPrimitive(icol);
110  // collection name and collectionID for HitsCollection,
111  // where type of HitsCollection is G4THitsMap in case
112  // of primitive scorer.
113  // The collection name is given by <MFD name>/<Primitive
114  // Scorer name>.
115  G4String collectionName = scorer->GetName();
116  G4String fullCollectionName = mfdName+"/"+collectionName;
117  G4int collectionID = SDman->GetCollectionID(fullCollectionName);
118  //
119  if ( collectionID >= 0 ){
120  G4cout << "++ " << fullCollectionName<< " id " << collectionID
121  << G4endl;
122  // Store obtained HitsCollection information into data members.
123  // And, creates new G4THitsMap for accumulating quantities during RUN.
124  fCollNames.push_back(fullCollectionName);
125  fCollIDs.push_back(collectionID);
126  fRunMaps.push_back(new G4THitsMap<G4double>(mfdName,
127  collectionName));
129  {
131  (mfdName, collectionName));
132  }
133  } else {
134  G4cout << "** collection " << fullCollectionName << " not found. "
135  <<G4endl;
136  }
137  }
138  }
139 
140 }
141 
142 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
143 //
144 // RecordEvent is called at end of event.
145 // For scoring purpose, the resultant quantity in a event,
146 // is accumulated during a TSRun.
147 void TSRun::RecordEvent(const G4Event* aEvent)
148 {
149  numberOfEvent++; // This is an original line.
150 
151  //=============================
152  // HitsCollection of This Event
153  //============================
154  G4HCofThisEvent* HCE = aEvent->GetHCofThisEvent();
155  if (!HCE) return;
156 
157  for(unsigned i = 0; i < fCollIDs.size(); ++i)
158  {
159  G4int fCollID = fCollIDs.at(i);
160  //=======================================================
161  // Sum up HitsMap of this Event into HitsMap of this RUN
162  //=======================================================
163  G4THitsMap<G4double>* EvtMap = 0;
164  if ( fCollID >= 0 ) // Collection is attached to HCE
165  EvtMap = static_cast<G4THitsMap<G4double>*>(HCE->GetHC(fCollID));
166  else
167  G4cout <<" Error EvtMap Not Found " << G4endl;
168 
169  if ( EvtMap )
170  {
171  //=== Sum up HitsMap of this event to HitsMap of RUN.===
172  *fRunMaps[fCollID] += *EvtMap;
173  // atomic run map
174  *fAtomicRunMaps[fCollID] += *EvtMap;
175  // mutex run map
176  static G4Mutex mtx = G4MUTEX_INITIALIZER;
177  {
178  G4AutoLock lock(&mtx);
179  auto itr = EvtMap->GetMap()->begin();
180  for(; itr != EvtMap->GetMap()->end(); itr++)
181  {
182  fMutexRunMaps[fCollNames[fCollID]][itr->first] += *itr->second;
183  }
184  }
185  //----------------------------------------------------------------//
186  }
187 
188  }
189 
190  G4Run::RecordEvent(aEvent);
191 
192 }
193 
194 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
195 
196 // Merge hits map from threads
197 void TSRun::Merge(const G4Run* aTSRun)
198 {
199  const TSRun* localTSRun = static_cast<const TSRun*>(aTSRun);
200 
201  for(unsigned i = 0; i < fRunMaps.size(); ++i)
202  *fRunMaps[i] += *localTSRun->fRunMaps[i];
203 
204  G4Run::Merge(aTSRun);
205 }
206 
207 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
208 
209 // Access HitsMap.
210 // by full description of collection name, that is
211 // <MultiFunctional Detector Name>/<Primitive Scorer Name>
213 {
214  for(unsigned i = 0; i < fCollNames.size(); ++i)
215  {
216  if(collName == fCollNames[i])
217  return fRunMaps[i];
218  }
219 
220  G4Exception("TSRun", collName.c_str(), JustWarning,
221  "GetHitsMap failed to locate the requested HitsMap");
222  return nullptr;
223 }
224 
225 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
226 
227 // Access AtomicsHitsMap.
228 // by full description of collection name, that is
229 // <MultiFunctional Detector Name>/<Primitive Scorer Name>
231 TSRun::GetAtomicHitsMap(const G4String& collName) const
232 {
233  for(unsigned i = 0; i < fCollNames.size(); ++i)
234  {
235  if(collName == fCollNames[i])
236  return fAtomicRunMaps[i];
237  }
238 
239  G4Exception("TSRun", collName.c_str(), JustWarning,
240  "GetHitsMap failed to locate the requested AtomicHitsMap");
241  return nullptr;
242 }
243 
244 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
245 
246 // Access AtomicsHitsMap.
247 // by full description of collection name, that is
248 // <MultiFunctional Detector Name>/<Primitive Scorer Name>
250 TSRun::GetMutexHitsMap(const G4String& collName) const
251 {
252  if(fMutexRunMaps.find(collName) != fMutexRunMaps.end())
253  return &fMutexRunMaps[collName];
254 
255  G4Exception("TSRun", collName.c_str(), JustWarning,
256  "GetHitsMap failed to locate the requested MutexHitsMap");
257  return nullptr;
258 }
259 
260 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
virtual void Merge(const G4Run *)
Definition: G4Run.cc:54
G4int numberOfEvent
Definition: G4Run.hh:59
G4String GetName() const
G4VHitsCollection * GetHC(G4int i)
G4int GetCollectionID(G4String colName)
Definition: G4SDManager.cc:135
MutexHitsMap_t * GetMutexHitsMap(const G4String &) const
Definition: TSRun.cc:250
Definition of the TSRun class.
virtual void RecordEvent(const G4Event *)
Definition: TSRun.cc:147
Definition: TSRun.hh:70
int G4int
Definition: G4Types.hh:78
std::vector< G4String > fCollNames
Definition: TSRun.hh:95
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:175
G4THitsMap< G4double > * GetHitsMap(const G4String &collname) const
Definition: TSRun.cc:212
TSRun(const G4String &)
Definition: TSRun.cc:70
static std::map< G4String, MutexHitsMap_t > fMutexRunMaps
Definition: TSRun.hh:100
G4GLOB_DLL std::ostream G4cout
virtual ~TSRun()
Definition: TSRun.cc:78
G4VPrimitiveScorer * GetPrimitive(G4int id) const
std::map< G4int, G4double > MutexHitsMap_t
Definition: TSRun.hh:73
virtual void Merge(const G4Run *)
Definition: TSRun.cc:197
static std::vector< G4TAtomicHitsMap< G4double > * > fAtomicRunMaps
TSRun contains three collections of hits maps: a thread-local hits map, a global atomic hits map (imp...
Definition: TSRun.hh:99
Definition: G4Run.hh:46
void ConstructMFD(const G4String &)
Definition: TSRun.cc:93
G4VSensitiveDetector * FindSensitiveDetector(G4String dName, G4bool warning=true)
Definition: G4SDManager.cc:128
std::vector< G4THitsMap< G4double > * > fRunMaps
Definition: TSRun.hh:97
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4bool IsWorkerThread()
Definition: G4Threading.cc:129
G4int G4Mutex
Definition: G4Threading.hh:173
This is an implementation of G4THitsMap where the underlying type is G4atomic, not just T. A static assert is provided to ensure that T is fundamental. This class should be used in lieu of G4THitsMap when memory is a concern. Atomics are thread-safe and generally faster that mutexes (as long as the STL implementation is lock-free) but the synchronization does not come without a cost. If performance is the primary concern, use G4THitsMap in thread-local instances.
static G4SDManager * GetSDMpointer()
Definition: G4SDManager.cc:40
virtual void RecordEvent(const G4Event *)
Definition: G4Run.cc:51
std::vector< G4int > fCollIDs
Definition: TSRun.hh:96
std::map< G4int, T * > * GetMap() const
Definition: G4THitsMap.hh:68
#define G4endl
Definition: G4ios.hh:61
G4HCofThisEvent * GetHCofThisEvent() const
Definition: G4Event.hh:185
G4TAtomicHitsMap< G4double > * GetAtomicHitsMap(const G4String &) const
Definition: TSRun.cc:231