Geant4  10.02.p02
TSRunAction.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: TSRunAction.cc 93110 2015-11-05 08:37:42Z jmadsen $
31 //
32 //
33 //
34 //
35 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
36 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
37 
38 
39 #include "TSRunAction.hh"
41 
42 #include "G4RunManager.hh"
43 #include "G4Run.hh"
44 #include "G4Timer.hh"
45 
46 #include "TSRun.hh"
48 
49 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
50 
52 : fDetector(TSDetectorConstruction::Instance()),
53  fName(fDetector->GetMFDName())
54 { }
55 
56 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
57 
59 { }
60 
61 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
62 
64 {
65  return new TSRun(fName);
66 }
67 
68 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
69 
71 {
72  G4int evts_to_process = aRun->GetNumberOfEventToBeProcessed();
73  G4RunManager::GetRunManager()->SetPrintProgress((evts_to_process > 100)
74  ? evts_to_process/100
75  : 1);
76 }
77 
78 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
79 
81 {
82 
83  if(IsMaster())
84  {
85  G4cout << " ###### EndOfTSRunAction ###### " << G4endl;
86 
87  aRun->GetNumberOfEvent();
88  std::ofstream fileout;
89  G4String fname = "";
90  std::stringstream separator;
91 
92  separator
93  << "============================================================";
94 
95  typedef std::set<G4int> IDSet_t;
96  IDSet_t IDs;
97 
98  //- TSRun object.
99  const TSRun* tsRun = static_cast<const TSRun*>(aRun);
100  //--- Dump all scored quantities involved in TSRun.
101 
102  //---------------------------------------------
103  // Dump accumulated quantities for this RUN.
104  //---------------------------------------------
105  std::vector<G4String> primScorerNames { "EnergyDeposit",
106  "NumberOfSteps" };
107  std::vector<G4String> fnames { "mfd_tl", "mfd_tg" };
108  std::vector<G4double> units { CLHEP::eV, CLHEP::keV, 1, 1 };
109  std::vector<G4String> unitstr { "keV", "steps" };
110 
111  for(unsigned i = 0; i < primScorerNames.size(); ++i)
112  {
113  for(unsigned j = 0; j < fnames.size(); ++j)
114  {
115  //----------------------------------------------------------------//
116  auto print = [] (std::ostream& fout,
117  G4int first, G4double second,
118  G4double unit1, G4double unit2, G4String unit2str)
119  {
120  if(fout)
121  fout << first
122  << " " << second/unit1
123  << G4endl;
124 
125  G4cout
126  << " " << std::setw(10) << first
127  << " " << std::setw(15) << std::setprecision(6)
128  << std::fixed << second/unit2 << " " << unit2str
129  << G4endl;
130  G4cout.unsetf(std::ios::fixed);
131  };
132  //----------------------------------------------------------------//
133 
134  fname = fnames.at(j) + "_" + primScorerNames.at(i) + ".out";
135  fileout.open(fname);
136  G4cout << separator.str() << G4endl;
137  G4cout << " opened file " << fname << " for output" << G4endl;
138  G4cout << separator.str() << G4endl;
139 
140  G4bool valid = true;
141  if(j == 0)
142  {
143  G4THitsMap<G4double>* hitmap
144  = tsRun->GetHitsMap(fName + "/" + primScorerNames.at(i));
145  if(hitmap && hitmap->GetMap()->size() != 0)
146  {
147  for(auto itr = hitmap->GetMap()->begin();
148  itr != hitmap->GetMap()->end(); itr++)
149  {
150  IDs.insert(itr->first);
151  std::get<0>(fTypeCompare[primScorerNames.at(i)][itr->first])
152  = *itr->second/units.at(i);
153  print(fileout, itr->first, *itr->second,
154  units.at(i), units.at(i+1), unitstr.at(i));
155  }
156  } else {
157  valid = false;
158  }
159  } else {
161  = tsRun->GetAtomicHitsMap(fName + "/" +
162  primScorerNames.at(i));
163  if(hitmap && hitmap->GetMap()->size() != 0)
164  {
165  for(auto itr = hitmap->GetMap()->begin();
166  itr != hitmap->GetMap()->end(); itr++)
167  {
168  IDs.insert(itr->first);
169  std::get<1>(fTypeCompare[primScorerNames.at(i)][itr->first])
170  = *itr->second/units.at(i);
171  print(fileout, itr->first, *itr->second,
172  units.at(i), units.at(i+1), unitstr.at(i));
173  }
174  } else {
175  valid = false;
176  }
177  if(!valid)
178  {
179  G4Exception("TSRunAction", "000", JustWarning,
180  G4String(primScorerNames.at(i) +
181  " HitsMap is either not "
182  "created or the HitsMap was empty").c_str());
183  }
184  }
185 
186  fileout.close();
187  G4cout << separator.str() << G4endl;
188  G4cout << " closed file " << fname << " for output" << G4endl;
189  }
190  // add the mutex data
191  TSRun::MutexHitsMap_t* hitmap
192  = tsRun->GetMutexHitsMap(fName + "/" +
193  primScorerNames.at(i));
194  if(hitmap && hitmap->size() != 0)
195  {
196  for(auto itr = hitmap->begin();
197  itr != hitmap->end(); itr++)
198  {
199  IDs.insert(itr->first);
200  std::get<2>(fTypeCompare[primScorerNames.at(i)][itr->first])
201  = itr->second/units.at(i);
202  }
203  }
204 
205  }
206 
207  //--------------------------------------------------------------------//
208  // Check that the values are equivalent and there are no
209  // IDs in one container that aren't in another
210  //--------------------------------------------------------------------//
211 
212  fname = "mfd_diff.out";
213  fileout.open(fname);
214 
215  G4cout << separator.str() << G4endl;
216  G4cout << " opened file " << fname << " for difference output" << G4endl;
217  G4cout << separator.str() << G4endl;
218 
219  fileout
220  << " " << std::setw(10) << "ID"
221  << " "
222  << std::setw(30) << std::setprecision(12) << std::fixed
223  << "MFD value"
224  << " "
225  << std::setw(30) << std::setprecision(12) << std::fixed
226  << "Atomic Hits Map value"
227  << " "
228  << std::setw(30) << std::setprecision(12) << std::fixed
229  << "Difference"
230  << " "
231  << std::setw(30) << std::setprecision(12) << std::fixed
232  << "Diff (MFD - MUTEXED)"
233  << " "
234  << std::setw(30) << std::setprecision(12) << std::fixed
235  << "Diff (ATOM_HIT_MAP - MUTEXED)"
236  << G4endl << G4endl;
237 
238  for(auto itr1 = fTypeCompare.begin();
239  itr1 != fTypeCompare.end(); ++itr1)
240  {
241  fileout << "\n\nType = " << itr1->first << "\n" << G4endl;
242  for(auto itr2 = itr1->second.begin();
243  itr2 != itr1->second.end(); ++itr2)
244  {
245  fileout
246  << " " << std::setw(10) << itr2->first
247  << " "
248  << std::setw(30) << std::setprecision(12) << std::fixed
249  << std::get<0>(itr2->second)
250  << " "
251  << std::setw(30) << std::setprecision(12) << std::fixed
252  << std::get<1>(itr2->second)
253  << " "
254  << std::setw(30) << std::setprecision(12) << std::fixed
255  << (std::fabs(std::get<0>(itr2->second) - std::get<1>(itr2->second)))
256  << " "
257  << std::setw(30) << std::setprecision(12) << std::fixed
258  << (std::fabs(std::get<0>(itr2->second) - std::get<2>(itr2->second)))
259  << " "
260  << std::setw(30) << std::setprecision(12) << std::fixed
261  << (std::fabs(std::get<1>(itr2->second) - std::get<2>(itr2->second)))
262  << G4endl;
263 
264  }
265  }
266 
267  fileout.close();
268  G4cout << " closed file " << fname << " for difference output" << G4endl;
269  G4cout << separator.str() << G4endl;
270 
271  }
272 
273 
274 }
275 
276 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
277 
278 
279 
280 
G4String fName
Definition: G4AttUtils.hh:55
G4String fName
Definition: TSRunAction.hh:70
virtual void BeginOfRunAction(const G4Run *)
Definition: TSRunAction.cc:70
void SetPrintProgress(G4int i)
Definition of the TSActionInitialization class.
MutexHitsMap_t * GetMutexHitsMap(const G4String &) const
Definition: TSRun.cc:250
Definition of the TSRun class.
Definition: TSRun.hh:70
int G4int
Definition: G4Types.hh:78
virtual void EndOfRunAction(const G4Run *)
Definition: TSRunAction.cc:80
G4THitsMap< G4double > * GetHitsMap(const G4String &collname) const
Definition: TSRun.cc:212
virtual G4Run * GenerateRun()
Definition: TSRunAction.cc:63
G4GLOB_DLL std::ostream G4cout
Definition of the TSRunAction class.
G4int GetNumberOfEvent() const
Definition: G4Run.hh:79
bool G4bool
Definition: G4Types.hh:79
std::map< G4int, G4double > MutexHitsMap_t
Definition: TSRun.hh:73
G4bool IsMaster() const
Definition: G4Run.hh:46
static const double second
Definition: G4SIunits.hh:156
void print(G4double elem)
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
TypeCompare_t fTypeCompare
Definition: TSRunAction.hh:71
static G4RunManager * GetRunManager()
Definition: G4RunManager.cc:79
static const double eV
Definition: G4SIunits.hh:212
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.
std::map< G4int, T * > * GetMap() const
Definition: G4THitsMap.hh:68
G4int GetNumberOfEventToBeProcessed() const
Definition: G4Run.hh:83
#define G4endl
Definition: G4ios.hh:61
Definition of the TSDetectorConstruction class.
static const double keV
Definition: G4SIunits.hh:213
G4TAtomicHitsMap< G4double > * GetAtomicHitsMap(const G4String &) const
Definition: TSRun.cc:231
double G4double
Definition: G4Types.hh:76
virtual ~TSRunAction()
Definition: TSRunAction.cc:58