Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
examples/extended/hadronic/Hadr03/src/RunAction.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 //
26 /// \file hadronic/Hadr03/src/RunAction.cc
27 /// \brief Implementation of the RunAction class
28 //
29 // $Id: RunAction.cc 70759 2013-06-05 12:26:43Z gcosmo $
30 //
31 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
32 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
33 
34 #include "RunAction.hh"
35 
36 #include "DetectorConstruction.hh"
37 #include "PrimaryGeneratorAction.hh"
38 #include "HistoManager.hh"
39 
40 #include "G4Run.hh"
41 #include "G4RunManager.hh"
43 #include "G4UnitsTable.hh"
44 #include "G4SystemOfUnits.hh"
45 
46 #include "Randomize.hh"
47 #include <iomanip>
48 
49 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
50 
52  : G4UserRunAction(),
53  fDetector(det), fPrimary(prim), fHistoManager(0)
54 {
55  fHistoManager = new HistoManager();
56 }
57 
58 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
59 
61 {
62  delete fHistoManager;
63 }
64 
65 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
66 
67 void RunAction::BeginOfRunAction(const G4Run* aRun)
68 {
69  G4cout << "### Run " << aRun->GetRunID() << " start." << G4endl;
70 
71  // save Rndm status
74 
75  fTotalCount = fGammaCount = 0;
76  fSumTrack = fSumTrack2 = 0.;
77  for (G4int i=0; i<3; i++) { fPbalance[i] = 0. ; }
78  for (G4int i=0; i<3; i++) { fNbGamma[i] = 0 ; }
79 
80  //histograms
81  //
82  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
83  if ( analysisManager->IsActive() ) {
84  analysisManager->OpenFile();
85  }
86 }
87 
88 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
89 
91 {
92  fParticleCount[name]++;
93  fEmean[name] += Ekin;
94  //update min max
95  if (fParticleCount[name] == 1) fEmin[name] = fEmax[name] = Ekin;
96  if (Ekin < fEmin[name]) fEmin[name] = Ekin;
97  if (Ekin > fEmax[name]) fEmax[name] = Ekin;
98 }
99 
100 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
101 
103 {
104  fNuclChannelCount[name]++;
105  fNuclChannelQ[name] += Q;
106 }
107 
108 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
109 
111 {
112  fPbalance[0] += Pbal;
113  //update min max
114  if (fTotalCount == 1) fPbalance[1] = fPbalance[2] = Pbal;
115  if (Pbal < fPbalance[1]) fPbalance[1] = Pbal;
116  if (Pbal > fPbalance[2]) fPbalance[2] = Pbal;
117 }
118 
119 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
120 
122 {
123  fGammaCount++;
124  fNbGamma[0] += nGamma;
125  //update min max
126  if (fGammaCount == 1) fNbGamma[1] = fNbGamma[2] = nGamma;
127  if (nGamma < fNbGamma[1]) fNbGamma[1] = nGamma;
128  if (nGamma > fNbGamma[2]) fNbGamma[2] = nGamma;
129 }
130 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
131 
132 void RunAction::EndOfRunAction(const G4Run* aRun)
133 {
134  G4int NbOfEvents = aRun->GetNumberOfEvent();
135  if (NbOfEvents == 0) return;
136 
137  G4int prec = 5, wid = prec + 2;
138  G4int dfprec = G4cout.precision(prec);
139 
140  G4Material* material = fDetector->GetMaterial();
141  G4double density = material->GetDensity();
142  G4int survive = 0;
143 
144  G4ParticleDefinition* particle =
145  fPrimary->GetParticleGun()->GetParticleDefinition();
146  G4String Particle = particle->GetParticleName();
148  G4cout << "\n The run consists of " << NbOfEvents << " "<< Particle << " of "
149  << G4BestUnit(energy,"Energy") << " through "
150  << G4BestUnit(fDetector->GetSize(),"Length") << " of "
151  << material->GetName() << " (density: "
152  << G4BestUnit(density,"Volumic Mass") << ")" << G4endl;
153 
154  //frequency of processes
155  G4cout << "\n Process calls frequency --->";
156  std::map<const G4VProcess*,G4int>::iterator it;
157  for (it = fProcCounter.begin(); it != fProcCounter.end(); it++) {
158  G4String procName = it->first->GetProcessName();
159  G4int count = it->second;
160  G4cout << "\t" << procName << "= " << count;
161  if (procName == "Transportation") survive = count;
162  }
163 
164  if (survive > 0) {
165  G4cout << "\n\n Nb of incident particles surviving after "
166  << G4BestUnit(fDetector->GetSize(),"Length") << " of "
167  << material->GetName() << " : " << survive << G4endl;
168  }
169 
170  if (fTotalCount == 0) fTotalCount = 1; //force printing anyway
171 
172  //compute mean free path and related quantities
173  //
174  G4double MeanFreePath = fSumTrack /fTotalCount;
175  G4double MeanTrack2 = fSumTrack2/fTotalCount;
176  G4double rms = std::sqrt(std::fabs(MeanTrack2 - MeanFreePath*MeanFreePath));
177  G4double CrossSection = 0.0;
178  if(MeanFreePath > 0.0) { CrossSection = 1./MeanFreePath; }
179  G4double massicMFP = MeanFreePath*density;
180  G4double massicCS = 0.0;
181  if(massicMFP > 0.0) { massicCS = 1./massicMFP; }
182 
183  G4cout << "\n\n MeanFreePath:\t" << G4BestUnit(MeanFreePath,"Length")
184  << " +- " << G4BestUnit( rms,"Length")
185  << "\tmassic: " << G4BestUnit(massicMFP, "Mass/Surface")
186  << "\n CrossSection:\t" << CrossSection*cm << " cm^-1 "
187  << "\t\tmassic: " << G4BestUnit(massicCS, "Surface/Mass")
188  << G4endl;
189 
190  //cross section per atom (only for single material)
191  //
192  if (material->GetNumberOfElements() == 1) {
193  G4double nbAtoms = material->GetTotNbOfAtomsPerVolume();
194  G4double crossSection = CrossSection/nbAtoms;
195  G4cout << " crossSection per atom:\t"
196  << G4BestUnit(crossSection,"Surface") << G4endl;
197  }
198  //check cross section from G4HadronicProcessStore
199  //
200  G4cout << "\n Verification : "
201  << "crossSections from G4HadronicProcessStore:";
202 
204  G4double sumc1 = 0.0, sumc2 = 0.0;
205  if (material->GetNumberOfElements() == 1) {
206  const G4Element* element = material->GetElement(0);
207  for (it = fProcCounter.begin(); it != fProcCounter.end(); it++) {
208  const G4VProcess* process = it->first;
209  G4double xs1 =
210  store->GetCrossSectionPerVolume(particle,energy,process,material);
211  G4double massSigma = xs1/density;
212  sumc1 += massSigma;
213  G4double xs2 =
214  store->GetCrossSectionPerAtom(particle,energy,process,element,material);
215  sumc2 += xs2;
216  G4String procName = process->GetProcessName();
217  G4cout << "\n" << std::setw(20) << procName << "= "
218  << G4BestUnit(massSigma, "Surface/Mass") << "\t"
219  << G4BestUnit(xs2, "Surface");
220 
221  }
222  G4cout << "\n" << std::setw(20) << "total" << "= "
223  << G4BestUnit(sumc1, "Surface/Mass") << "\t"
224  << G4BestUnit(sumc2, "Surface") << G4endl;
225  } else {
226  for (it = fProcCounter.begin(); it != fProcCounter.end(); it++) {
227  const G4VProcess* process = it->first;
228  G4double xs =
229  store->GetCrossSectionPerVolume(particle,energy,process,material);
230  G4double massSigma = xs/density;
231  sumc1 += massSigma;
232  G4String procName = process->GetProcessName();
233  G4cout << "\n" << std::setw(20) << procName << "= "
234  << G4BestUnit(massSigma, "Surface/Mass");
235  }
236  G4cout << "\n" << std::setw(20) << "total" << "= "
237  << G4BestUnit(sumc1, "Surface/Mass") << G4endl;
238  }
239 
240  //nuclear channel count
241  //
242  G4cout << "\n List of nuclear reactions: \n" << G4endl;
243 
244  std::map<G4String,G4int>::iterator ic;
245  for (ic = fNuclChannelCount.begin(); ic != fNuclChannelCount.end(); ic++) {
246  G4String name = ic->first;
247  G4int count = ic->second;
248  G4double Q = fNuclChannelQ[name]/count;
249 
250  G4cout << " " << std::setw(50) << name << ": " << std::setw(7) << count
251  << " Q = " << std::setw(wid) << G4BestUnit(Q, "Energy")
252  << G4endl;
253  }
254 
255  //Gamma count
256  //
257  if (fGammaCount > 0) {
258  G4cout << "\n" << std::setw(58) << "Number of gamma: N = "
259  << fNbGamma[1] << " --> " << fNbGamma[2] << G4endl;
260  }
261 
262  G4cout
263  << "\n --> NOTE: XXXX because neutronHP is unable to return target nucleus"
264  << G4endl;
265 
266  //particles count
267  //
268  G4cout << "\n List of generated particles: \n" << G4endl;
269 
270  std::map<G4String,G4int>::iterator ip;
271  for (ip = fParticleCount.begin(); ip != fParticleCount.end(); ip++) {
272  G4String name = ip->first;
273  G4int count = ip->second;
274  G4double eMean = fEmean[name]/count;
275  G4double eMin = fEmin[name], eMax = fEmax[name];
276 
277  G4cout << " " << std::setw(13) << name << ": " << std::setw(7) << count
278  << " Emean = " << std::setw(wid) << G4BestUnit(eMean, "Energy")
279  << "\t( " << G4BestUnit(eMin, "Energy")
280  << " --> " << G4BestUnit(eMax, "Energy")
281  << ")" << G4endl;
282  }
283 
284  //energy momentum balance
285  //
286  if (fTotalCount > 1) {
287  G4double Pbmean = fPbalance[0]/fTotalCount;
288  G4cout << "\n Momentum balance: Pmean = "
289  << std::setw(wid) << G4BestUnit(Pbmean, "Energy")
290  << "\t( " << G4BestUnit(fPbalance[1], "Energy")
291  << " --> " << G4BestUnit(fPbalance[2], "Energy")
292  << ") \n" << G4endl;
293  }
294 
295  //restore default format
296  G4cout.precision(dfprec);
297 
298  // remove all contents in fProcCounter
299  fProcCounter.clear();
300  // remove all contents in fNuclChannel
301  fNuclChannelCount.clear();
302  fNuclChannelQ.clear();
303  // remove all contents in fParticleCount
304  fParticleCount.clear();
305  fEmean.clear(); fEmin.clear(); fEmax.clear();
306 
307  //save histograms
308  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
309  ////G4double factor = 1./NbOfEvents;
310  ////analysisManager->ScaleH1(3,factor);
311  if ( analysisManager->IsActive() ) {
312  analysisManager->Write();
313  analysisManager->CloseFile();
314  }
315 
316  // show Rndm status
318 }
319 
320 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
static G4HadronicProcessStore * Instance()
G4int first(char) const
const G4String & GetName() const
Definition: G4Material.hh:176
G4double GetDensity() const
Definition: G4Material.hh:178
const XML_Char * name
#define G4BestUnit(a, b)
#define G4_USE_G4BESTUNIT_FOR_VERBOSE 1
void SetRandomNumberStore(G4bool flag)
const G4Element * GetElement(G4int iel) const
Definition: G4Material.hh:200
int G4int
Definition: G4Types.hh:78
const G4String & GetParticleName() const
string material
Definition: eplot.py:19
G4double density
Definition: TRTMaterials.hh:39
double precision function energy(A, Z)
Definition: dpm25nuc6.f:4106
G4GLOB_DLL std::ostream G4cout
void CountNuclearChannel(G4String, G4double)
G4int GetNumberOfEvent() const
Definition: G4Run.hh:79
G4int GetRunID() const
Definition: G4Run.hh:76
Definition: G4Run.hh:46
ExG4HbookAnalysisManager G4AnalysisManager
Definition: g4hbook_defs.hh:46
const G4String & GetProcessName() const
Definition: G4VProcess.hh:408
static void showEngineStatus()
Definition: Random.cc:203
G4double GetTotNbOfAtomsPerVolume() const
Definition: G4Material.hh:207
static G4RunManager * GetRunManager()
Definition: G4RunManager.cc:74
G4double GetCrossSectionPerVolume(const G4ParticleDefinition *particle, G4double kineticEnergy, const G4VProcess *process, const G4Material *material)
G4ParticleDefinition * GetParticleDefinition() const
#define G4endl
Definition: G4ios.hh:61
size_t GetNumberOfElements() const
Definition: G4Material.hh:184
double G4double
Definition: G4Types.hh:76
G4double GetCrossSectionPerAtom(const G4ParticleDefinition *particle, G4double kineticEnergy, const G4VProcess *process, const G4Element *element, const G4Material *material=0)
G4double GetParticleEnergy() const