Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
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 // $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 #include "G4TiMemory.hh"
62 
63 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
64 
65 std::vector<G4TAtomicHitsMap<G4double>*> TSRun::fAtomicRunMaps;
66 
67 std::map<G4String, TSRun::MutexHitsMap_t> TSRun::fMutexRunMaps;
68 
69 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
70 
71 TSRun::TSRun(const G4String& mfd_name)
72 : G4Run()
73 {
74  ConstructMFD(mfd_name);
75 }
76 
77 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
78 
80 {
81  //--- Clear HitsMap for RUN
82  for(unsigned i = 0; i < fRunMaps.size(); ++i)
83  delete fRunMaps[i];
84 
86  {
87  for(unsigned i = 0; i < fAtomicRunMaps.size(); ++i)
88  delete fAtomicRunMaps[i];
89 
90  fAtomicRunMaps.clear();
91  fMutexRunMaps.clear();
92  }
93 }
94 
95 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
96 
97 // clear all data members.
98 void TSRun::ConstructMFD(const G4String& mfdName)
99 {
100 
102  //=================================================
103  // Initalize RunMaps for accumulation.
104  // Get CollectionIDs for HitCollections.
105  //=================================================
108  //
109  if ( mfd )
110  {
111  //--- Loop over the registered primitive scorers.
112  for (G4int icol = 0; icol < mfd->GetNumberOfPrimitives(); icol++){
113  // Get Primitive Scorer object.
114  G4VPrimitiveScorer* scorer = mfd->GetPrimitive(icol);
115  // collection name and collectionID for HitsCollection,
116  // where type of HitsCollection is G4THitsMap in case
117  // of primitive scorer.
118  // The collection name is given by <MFD name>/<Primitive
119  // Scorer name>.
120  G4String collectionName = scorer->GetName();
121  G4String fullCollectionName = mfdName+"/"+collectionName;
122  G4int collectionID = SDman->GetCollectionID(fullCollectionName);
123  //
124  if ( collectionID >= 0 ){
125  G4cout << "++ " << fullCollectionName<< " id " << collectionID
126  << G4endl;
127  // Store obtained HitsCollection information into data members.
128  // And, creates new G4THitsMap for accumulating quantities during RUN.
129  fCollNames.push_back(fullCollectionName);
130  fCollIDs.push_back(collectionID);
131  fRunMaps.push_back(new G4THitsMap<G4double>(mfdName,
132  collectionName));
134  {
136  (mfdName, collectionName));
137  fMutexRunMaps[fCollNames[collectionID]].clear();
138  }
139  } else {
140  G4cout << "** collection " << fullCollectionName << " not found. "
141  <<G4endl;
142  }
143  }
144  }
145 
146 }
147 
148 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
149 //
150 // RecordEvent is called at end of event.
151 // For scoring purpose, the resultant quantity in a event,
152 // is accumulated during a TSRun.
153 void TSRun::RecordEvent(const G4Event* aEvent)
154 {
155  G4Run::RecordEvent(aEvent);
156 
157  //=============================
158  // HitsCollection of This Event
159  //============================
160  G4HCofThisEvent* HCE = aEvent->GetHCofThisEvent();
161  if (!HCE) return;
162 
163  for(unsigned i = 0; i < fCollIDs.size(); ++i)
164  {
165  G4int fCollID = fCollIDs.at(i);
166  //=======================================================
167  // Sum up HitsMap of this Event into HitsMap of this RUN
168  //=======================================================
169  G4THitsMap<G4double>* EvtMap = 0;
170  if ( fCollID >= 0 ) // Collection is attached to HCE
171  EvtMap = static_cast<G4THitsMap<G4double>*>(HCE->GetHC(fCollID));
172  else
173  G4cout <<" Error EvtMap Not Found " << G4endl;
174 
175  if ( EvtMap )
176  {
177  //=== Sum up HitsMap of this event to HitsMap of RUN.===
178  {
179  TIMEMORY_AUTO_TIMER("[standard_run_map]");
180  *fRunMaps[fCollID] += *EvtMap;
181  }
182  // atomic run map
183  {
184  TIMEMORY_AUTO_TIMER("[atomic_run_map]");
185  *fAtomicRunMaps[fCollID] += *EvtMap;
186  }
187  TIMEMORY_AUTO_TIMER("[mutex_run_map]");
188  // mutex run map
189  static G4Mutex mtx = G4MUTEX_INITIALIZER;
190  {
191  G4AutoLock lock(&mtx);
192  for(const auto& itr : *EvtMap)
193  {
194  fMutexRunMaps[fCollNames[fCollID]][itr.first]
195  += *itr.second;
196  }
197  }
198  //----------------------------------------------------------------//
199  }
200  }
201 
202 }
203 
204 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
205 
206 // Merge hits map from threads
207 void TSRun::Merge(const G4Run* aTSRun)
208 {
209  const TSRun* localTSRun = static_cast<const TSRun*>(aTSRun);
210 
211  for(unsigned i = 0; i < fRunMaps.size(); ++i)
212  *fRunMaps[i] += *localTSRun->fRunMaps[i];
213 
214  G4Run::Merge(aTSRun);
215 }
216 
217 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
218 
219 // Access HitsMap.
220 // by full description of collection name, that is
221 // <MultiFunctional Detector Name>/<Primitive Scorer Name>
223 {
224  for(unsigned i = 0; i < fCollNames.size(); ++i)
225  {
226  if(collName == fCollNames[i])
227  return fRunMaps[i];
228  }
229 
230  G4Exception("TSRun", collName.c_str(), JustWarning,
231  "GetHitsMap failed to locate the requested HitsMap");
232  return nullptr;
233 }
234 
235 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
236 
237 // Access AtomicsHitsMap.
238 // by full description of collection name, that is
239 // <MultiFunctional Detector Name>/<Primitive Scorer Name>
241 TSRun::GetAtomicHitsMap(const G4String& collName) const
242 {
243  for(unsigned i = 0; i < fCollNames.size(); ++i)
244  {
245  if(collName == fCollNames[i])
246  return fAtomicRunMaps[i];
247  }
248 
249  G4Exception("TSRun", collName.c_str(), JustWarning,
250  "GetHitsMap failed to locate the requested AtomicHitsMap");
251  return nullptr;
252 }
253 
254 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
255 
256 // Access AtomicsHitsMap.
257 // by full description of collection name, that is
258 // <MultiFunctional Detector Name>/<Primitive Scorer Name>
260 TSRun::GetMutexHitsMap(const G4String& collName) const
261 {
262  if(fMutexRunMaps.find(collName) != fMutexRunMaps.end())
263  return &fMutexRunMaps[collName];
264 
265  G4Exception("TSRun", collName.c_str(), JustWarning,
266  "GetHitsMap failed to locate the requested MutexHitsMap");
267  return nullptr;
268 }
269 
270 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
G4int GetCollectionID(G4String colName)
Definition: G4SDManager.cc:135
Definition of the TSRun class.
G4String GetName() const
G4VPrimitiveScorer * GetPrimitive(G4int id) const
G4VSensitiveDetector * FindSensitiveDetector(G4String dName, G4bool warning=true)
Definition: G4SDManager.cc:128
#define G4endl
Definition: G4ios.hh:61
virtual ~TSRun()
Definition: TSRun.cc:79
MutexHitsMap_t * GetMutexHitsMap(const G4String &) const
Definition: TSRun.cc:260
G4HCofThisEvent * GetHCofThisEvent() const
Definition: G4Event.hh:185
virtual void Merge(const G4Run *)
Definition: TSRun.cc:207
Definition: TSRun.hh:70
std::map< G4int, G4double > MutexHitsMap_t
Definition: TSRun.hh:73
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:88
G4bool IsWorkerThread()
Definition: G4Threading.cc:129
static std::vector< G4TAtomicHitsMap< G4double > * > fAtomicRunMaps
Definition: TSRun.hh:97
std::vector< G4THitsMap< G4double > * > fRunMaps
Definition: TSRun.hh:96
TSRun(const G4String &)
Definition: TSRun.cc:71
Definition: G4Run.hh:46
virtual void RecordEvent(const G4Event *)
Definition: G4Run.cc:51
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.hh:65
G4TAtomicHitsMap< G4double > * GetAtomicHitsMap(const G4String &) const
Definition: TSRun.cc:241
static G4SDManager * GetSDMpointer()
Definition: G4SDManager.cc:40
int G4int
Definition: G4Types.hh:78
G4GLOB_DLL std::ostream G4cout
G4THitsMap< G4double > * GetHitsMap(const G4String &collname) const
Definition: TSRun.cc:222
#define TIMEMORY_AUTO_TIMER(str)
Definition: G4TiMemory.hh:66
virtual void Merge(const G4Run *)
Definition: G4Run.cc:54
static std::map< G4String, MutexHitsMap_t > fMutexRunMaps
Definition: TSRun.hh:98
void ConstructMFD(const G4String &)
Definition: TSRun.cc:98
std::vector< G4String > fCollNames
Definition: TSRun.hh:94
std::vector< G4int > fCollIDs
Definition: TSRun.hh:95
virtual void RecordEvent(const G4Event *)
Definition: TSRun.cc:153
std::mutex G4Mutex
Definition: G4Threading.hh:84