Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
examples/extended/medical/electronScattering/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 medical/electronScattering/src/RunAction.cc
27 /// \brief Implementation of the RunAction class
28 //
29 // $Id: RunAction.cc 69009 2013-04-15 09:33:05Z gcosmo $
30 //
31 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
32 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
33 
34 #include "RunAction.hh"
35 #include "DetectorConstruction.hh"
36 #include "PrimaryGeneratorAction.hh"
37 #include "HistoManager.hh"
38 
39 #include "G4Run.hh"
40 #include "G4RunManager.hh"
41 #include "G4UnitsTable.hh"
42 
43 #include "G4PhysicalConstants.hh"
44 #include "G4SystemOfUnits.hh"
45 #include "Randomize.hh"
46 #include <iomanip>
47 
48 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
49 
51 :fDetector(det), fPrimary(kin), fHistoManager(0)
52 {
53  fHistoManager = new HistoManager();
54 }
55 
56 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
57 
59 {
60  delete fHistoManager;
61 }
62 
63 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
64 
65 void RunAction::BeginOfRunAction(const G4Run* aRun)
66 {
67  G4cout << "### Run " << aRun->GetRunID() << " start." << G4endl;
68 
69  InitFluence();
70 
71  // save Rndm status
74 
75  //histograms
76  //
77  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
78  if ( analysisManager->IsActive() ) {
79  analysisManager->OpenFile();
80  }
81 }
82 
83 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
84 
85 void RunAction::EndOfRunAction(const G4Run* aRun)
86 {
87  G4int TotNbofEvents = aRun->GetNumberOfEvent();
88  if (TotNbofEvents == 0) return;
89 
90  //Scatter foil
91  //
92  G4Material* material = fDetector->GetMaterialScatter();
93  G4double length = fDetector->GetThicknessScatter();
94  G4double density = material->GetDensity();
95 
96  G4ParticleDefinition* particle = fPrimary->GetParticleGun()
98  G4String partName = particle->GetParticleName();
100 
101  G4cout << "\n ======================== run summary ======================\n";
102 
103  G4int prec = G4cout.precision(3);
104 
105  G4cout << "\n The run was " << TotNbofEvents << " " << partName << " of "
106  << G4BestUnit(energy,"Energy") << " through "
107  << G4BestUnit(length,"Length") << " of "
108  << material->GetName() << " (density: "
109  << G4BestUnit(density,"Volumic Mass") << ")" << G4endl;
110 
111  G4cout.precision(prec);
112 
113  // normalize histograms
114  //
115  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
116  G4double fac = 1./(double(TotNbofEvents));
117  analysisManager->ScaleH1(1,fac);
118  analysisManager->ScaleH1(2,fac);
119  analysisManager->ScaleH1(3,fac);
120  analysisManager->ScaleH1(5,fac);
121  analysisManager->ScaleH1(6,fac);
122 
124  PrintFluence(TotNbofEvents);
125 
126  // save histograms
127  if ( analysisManager->IsActive() ) {
128  analysisManager->Write();
129  analysisManager->CloseFile();
130  }
131 
132  // show Rndm status
134 }
135 
136 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
137 
139 {
140  // create Fluence histo in any case
141  //
142  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
143  G4int ih = 4;
144  analysisManager->SetH1(ih, 120, 0*mm, 240*mm, "mm");
145 
146  //construct vectors for fluence distribution
147  //
148  fNbBins = analysisManager->GetH1Nbins(ih);
149  fDr = analysisManager->GetH1Width(ih);
150  fluence.resize(fNbBins, 0.);
151  fluence1.resize(fNbBins, 0.);
152  fluence2.resize(fNbBins, 0.);
153  fNbEntries.resize(fNbBins, 0);
154 }
155 
156 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
157 
159 {
160  G4int ibin = (int)(r/fDr);
161  if (ibin >= fNbBins) return;
162  fNbEntries[ibin]++;
163  fluence[ibin] += fl;
164  fluence2[ibin] += fl*fl;
165 }
166 
167 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
168 
170 {
171  //compute rms
172  //
173  G4double ds,variance,rms;
174  G4double rmean = -0.5*fDr;
175 
176  for (G4int bin=0; bin<fNbBins; bin++) {
177  rmean += fDr;
178  ds = twopi*rmean*fDr;
179  fluence[bin] /= ds;
180  fluence2[bin] /= (ds*ds);
181  variance = 0.;
182  if (fNbEntries[bin] > 0)
183  variance = fluence2[bin] - (fluence[bin]*fluence[bin])/fNbEntries[bin];
184  rms = 0.;
185  if(variance > 0.) rms = std::sqrt(variance);
186  fluence2[bin] = rms;
187  }
188 
189  //normalize to first bins, compute error and fill histo
190  //
191  G4double rnorm(4*mm), radius(0.), fnorm(0.), fnorm2(0.);
192  G4int inorm = -1;
193  do {
194  inorm++; radius += fDr; fnorm += fluence[inorm]; fnorm2 += fluence2[inorm];
195  } while (radius < rnorm);
196  fnorm /= (inorm+1);
197  fnorm2 /= (inorm+1);
198  //
199  G4double ratio, error;
200  G4double scale = 1./fnorm;
201  G4double err0 = fnorm2/fnorm, err1 = 0.;
202  //
203  rmean = -0.5*fDr;
204 
205  for (G4int bin=0; bin<fNbBins; bin++) {
206  ratio = fluence[bin]*scale;
207  error = 0.;
208  if (ratio > 0.) {
209  err1 = fluence2[bin]/fluence[bin];
210  error = ratio*std::sqrt(err1*err1 + err0*err0);
211  }
212  fluence1[bin] = ratio;
213  fluence2[bin] = error;
214  rmean += fDr;
215  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
216  analysisManager->FillH1(4,rmean,ratio);
217  }
218 }
219 
220 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
221 
222 #include <fstream>
223 
225 {
226  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
227  G4String name = analysisManager->GetFileName();
228  G4String fileName = name + ".ascii";
229  std::ofstream File(fileName, std::ios::out);
230 
231  std::ios::fmtflags mode = File.flags();
232  File.setf( std::ios::scientific, std::ios::floatfield );
233  G4int prec = File.precision(3);
234 
235  File << " Fluence density distribution \n "
236  << "\n ibin \t radius (mm) \t Nb \t fluence\t norma fl\t rms/nfl (%) \n"
237  << G4endl;
238 
239  G4double rmean = -0.5*fDr;
240  for (G4int bin=0; bin<fNbBins; bin++) {
241  rmean +=fDr;
242  G4double error = 0.;
243  if (fluence1[bin] > 0.) error = 100*fluence2[bin]/fluence1[bin];
244  File << " " << bin << "\t " << rmean/mm << "\t " << fNbEntries[bin]
245  << "\t " << fluence[bin]/double(TotEvents) << "\t " << fluence1[bin]
246  << "\t " << error
247  << G4endl;
248  }
249 
250  // restaure default formats
251  File.setf(mode,std::ios::floatfield);
252  File.precision(prec);
253 }
254 
255 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
256 
tuple bin
Definition: plottest35.py:22
typedef int(XMLCALL *XML_NotStandaloneHandler)(void *userData)
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)
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
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
static void showEngineStatus()
Definition: Random.cc:203
static G4RunManager * GetRunManager()
Definition: G4RunManager.cc:74
G4ParticleDefinition * GetParticleDefinition() const
#define G4endl
Definition: G4ios.hh:61
double G4double
Definition: G4Types.hh:76
G4double GetParticleEnergy() const