ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TSRun.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file TSRun.cc
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 //
31 //
66 //
67 //
68 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
69 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
70 
71 
72 #include "TSRun.hh"
73 
74 #include "G4SDManager.hh"
76 #include "G4VPrimitiveScorer.hh"
77 #include "G4TiMemory.hh"
79 
80 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
81 
82 std::vector<G4TAtomicHitsMap<G4double>*> TSRun::fAtomicRunMaps;
83 
84 std::map<G4String, TSRun::MutexHitsMap_t> TSRun::fMutexRunMaps;
85 
86 std::vector<G4StatContainer<G4ConvergenceTester>*> TSRun::fConvMaps;
87 
88 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
89 
90 TSRun::TSRun(const G4String& mfd_name)
91 : G4Run()
92 {
93  ConstructMFD(mfd_name);
94 }
95 
96 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
97 
99 {
100  //--- Clear HitsMap for RUN
101  for(unsigned i = 0; i < fRunMaps.size(); ++i)
102  delete fRunMaps[i];
103 
105  {
106  for(unsigned i = 0; i < fAtomicRunMaps.size(); ++i)
107  delete fAtomicRunMaps[i];
108 
109  for(auto& itr: fConvMaps)
110  delete itr;
111 
112  fAtomicRunMaps.clear();
113  fMutexRunMaps.clear();
114  fConvMaps.clear();
115  }
116 }
117 
118 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
119 
120 // clear all data members.
121 void TSRun::ConstructMFD(const G4String& mfdName)
122 {
123 
125  //=================================================
126  // Initalize RunMaps for accumulation.
127  // Get CollectionIDs for HitCollections.
128  //=================================================
131  //
132  if ( mfd )
133  {
134  //--- Loop over the registered primitive scorers.
135  for (G4int icol = 0; icol < mfd->GetNumberOfPrimitives(); icol++){
136  // Get Primitive Scorer object.
137  G4VPrimitiveScorer* scorer = mfd->GetPrimitive(icol);
138  // collection name and collectionID for HitsCollection,
139  // where type of HitsCollection is G4THitsMap in case
140  // of primitive scorer.
141  // The collection name is given by <MFD name>/<Primitive
142  // Scorer name>.
143  G4String collectionName = scorer->GetName();
144  G4String fullCollectionName = mfdName+"/"+collectionName;
145  G4int collectionID = SDman->GetCollectionID(fullCollectionName);
146  //
147  if ( collectionID >= 0 ){
148  G4cout << "++ " << fullCollectionName<< " id " << collectionID
149  << G4endl;
150  // Store obtained HitsCollection information into data members.
151  // And, creates new G4THitsMap for accumulating quantities during RUN.
152  fCollNames.push_back(fullCollectionName);
153  fCollIDs.push_back(collectionID);
154  fRunMaps.push_back(new G4THitsMap<G4double>(mfdName,
155  collectionName));
157  mfdName,
158  collectionName,
159  TSDetectorConstruction::Instance()->GetTotalTargets()));
161  {
163  (mfdName, collectionName));
164  fMutexRunMaps[fCollNames[collectionID]].clear();
166  mfdName,
167  collectionName,
168  TSDetectorConstruction::Instance()->GetTotalTargets()));
169  }
170  } else {
171  G4cout << "** collection " << fullCollectionName << " not found. "
172  <<G4endl;
173  }
174  }
175  }
176 
177 }
178 
179 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
180 //
181 // RecordEvent is called at end of event.
182 // For scoring purpose, the resultant quantity in a event,
183 // is accumulated during a TSRun.
184 void TSRun::RecordEvent(const G4Event* aEvent)
185 {
186  G4Run::RecordEvent(aEvent);
187 
188  //=============================
189  // HitsCollection of This Event
190  //============================
191  G4HCofThisEvent* HCE = aEvent->GetHCofThisEvent();
192  if (!HCE)
193  return;
194 
195  for(unsigned i = 0; i < fCollIDs.size(); ++i)
196  {
197  G4int fCollID = fCollIDs.at(i);
198  //=======================================================
199  // Sum up HitsMap of this Event into HitsMap of this RUN
200  //=======================================================
201  G4THitsMap<G4double>* EvtMap = 0;
202  if ( fCollID >= 0 ) // Collection is attached to HCE
203  EvtMap = static_cast<G4THitsMap<G4double>*>(HCE->GetHC(fCollID));
204  else
205  G4cout <<" Error EvtMap Not Found " << G4endl;
206 
207  TIMEMORY_AUTO_TIMER("[" + fCollNames.at(i) + "]");
208 
209  if ( EvtMap )
210  {
211  //=== Sum up HitsMap of this event to HitsMap of RUN.===
212  {
213  TIMEMORY_BASIC_AUTO_TIMER("[standard_run_map]");
214  *fRunMaps[fCollID] += *EvtMap;
215  }
216  //=== Sum up HitsMap of this event to atomic HitsMap of RUN.===
217  {
218  TIMEMORY_BASIC_AUTO_TIMER("[atomic_run_map]");
219  *fAtomicRunMaps[fCollID] += *EvtMap;
220  }
221  //=== Sum up HitsMap of this event to StatMap of RUN.===
222  {
223  TIMEMORY_BASIC_AUTO_TIMER("[stat_analysis_map]");
224  // G4StatAnalysis map
225  *fStatMaps[fCollID] += *EvtMap;
226  }
227  //=== Sum up HitsMap of this event to MutexMap of RUN.===
228  {
229  TIMEMORY_BASIC_AUTO_TIMER("[convergence_test_map]");
230  // G4ConvergenceTester run map
231  static G4Mutex mtx = G4MUTEX_INITIALIZER;
232  G4AutoLock lock(&mtx);
233  *fConvMaps[fCollID] += *EvtMap;
234  }
235  //=== Sum up HitsMap of this event to MutexMap of RUN.===
236  {
237  TIMEMORY_BASIC_AUTO_TIMER("[mutex_run_map]");
238  // mutex run map
239  static G4Mutex mtx = G4MUTEX_INITIALIZER;
240  G4AutoLock lock(&mtx);
241  for(const auto& itr : *EvtMap)
242  fMutexRunMaps[fCollNames[fCollID]][itr.first]
243  += *itr.second;
244  }
245  }
246  }
247 }
248 
249 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
250 
251 // Merge hits map from threads
252 void TSRun::Merge(const G4Run* aTSRun)
253 {
254  const TSRun* localTSRun = static_cast<const TSRun*>(aTSRun);
255 
256  for(unsigned i = 0; i < fRunMaps.size(); ++i)
257  {
258  *fRunMaps[i] += *localTSRun->fRunMaps[i];
259  *fStatMaps[i] += *localTSRun->fStatMaps[i];
260  }
261 
262  G4Run::Merge(aTSRun);
263 }
264 
265 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
266 
267 // Access HitsMap.
268 // by full description of collection name, that is
269 // <MultiFunctional Detector Name>/<Primitive Scorer Name>
271 {
272  for(unsigned i = 0; i < fCollNames.size(); ++i)
273  {
274  if(collName == fCollNames[i])
275  return fRunMaps[i];
276  }
277 
278  G4Exception("TSRun", collName.c_str(), JustWarning,
279  "GetHitsMap failed to locate the requested HitsMap");
280  return nullptr;
281 }
282 
283 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
284 
285 // Access AtomicsHitsMap.
286 // by full description of collection name, that is
287 // <MultiFunctional Detector Name>/<Primitive Scorer Name>
289 TSRun::GetAtomicHitsMap(const G4String& collName) const
290 {
291  for(unsigned i = 0; i < fCollNames.size(); ++i)
292  {
293  if(collName == fCollNames[i])
294  return fAtomicRunMaps[i];
295  }
296 
297  G4Exception("TSRun", collName.c_str(), JustWarning,
298  "GetHitsMap failed to locate the requested AtomicHitsMap");
299  return nullptr;
300 }
301 
302 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
303 
304 // Access AtomicsHitsMap.
305 // by full description of collection name, that is
306 // <MultiFunctional Detector Name>/<Primitive Scorer Name>
308 TSRun::GetMutexHitsMap(const G4String& collName) const
309 {
310  if(fMutexRunMaps.find(collName) != fMutexRunMaps.end())
311  return &fMutexRunMaps[collName];
312 
313  G4Exception("TSRun", collName.c_str(), JustWarning,
314  "GetHitsMap failed to locate the requested MutexHitsMap");
315  return nullptr;
316 }
317 
318 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
319 
320 // Access StatMap.
321 // by full description of collection name, that is
322 // <MultiFunctional Detector Name>/<Primitive Scorer Name>
324 TSRun::GetStatMap(const G4String& collName) const
325 {
326  for(unsigned i = 0; i < fCollNames.size(); ++i)
327  {
328  if(collName == fCollNames[i])
329  return fStatMaps[i];
330  }
331 
332  G4Exception("TSRun", collName.c_str(), JustWarning,
333  "GetStatMap failed to locate the requested StatMap");
334  return nullptr;
335 }
336 
337 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
338 
340 TSRun::GetConvMap(const G4String& collName) const
341 {
342  for(unsigned i = 0; i < fCollNames.size(); ++i)
343  {
344  if(collName == fCollNames[i])
345  return fConvMaps[i];
346  }
347 
348  G4Exception("TSRun", collName.c_str(), JustWarning,
349  "GetHitsMap failed to locate the requested AtomicHitsMap");
350  return nullptr;
351 }