Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
examples/extended/medical/fanoCavity2/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/fanoCavity2/src/RunAction.cc
27 /// \brief Implementation of the RunAction class
28 //
29 // $Id: RunAction.cc 71035 2013-06-10 09:17:35Z 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 #include "G4EmCalculator.hh"
43 #include "G4Electron.hh"
44 
45 #include "G4SystemOfUnits.hh"
46 #include "Randomize.hh"
47 #include <iomanip>
48 
49 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
50 
52 :fDetector(det),fKinematic(kin),fProcCounter(0),fHistoManager(0)
53 {
54  fHistoManager = new HistoManager();
55 }
56 
57 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
58 
60 {
61  delete fHistoManager;
62 }
63 
64 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
65 
66 void RunAction::BeginOfRunAction(const G4Run* aRun)
67 {
68  // do not save Rndm status
71 
72  G4cout << "### Run " << aRun->GetRunID() << " start." << G4endl;
73 
74  G4int NbofEvents = aRun->GetNumberOfEventToBeProcessed();
75  if (NbofEvents == 0) return;
76 
77  //run conditions
78  //
79  G4ParticleDefinition* particleGun
80  = fKinematic->GetParticleGun()->GetParticleDefinition();
81  G4String partName = particleGun->GetParticleName();
82  fEnergyGun = fKinematic->GetParticleGun()->GetParticleEnergy();
83 
84  //geometry : effective wall volume
85  //
86  G4double cavityThickness = fDetector->GetCavityThickness();
87  G4Material* mateCavity = fDetector->GetCavityMaterial();
88  G4double densityCavity = mateCavity->GetDensity();
89  fMassCavity = cavityThickness*densityCavity;
90 
91  G4double wallThickness = fDetector->GetWallThickness();
92  G4Material* mateWall = fDetector->GetWallMaterial();
93  G4double densityWall = mateWall->GetDensity();
94 
95  G4EmCalculator emCal;
96  G4double RangeWall = emCal.GetCSDARange(fEnergyGun,particleGun,mateWall);
97  G4double factor = 1.2;
98  G4double effWallThick = factor*RangeWall;
99  if ((effWallThick > wallThickness)||(effWallThick <= 0.))
100  effWallThick = wallThickness;
101  fMassWall = 2*effWallThick*densityWall;
102 
103  G4double massTotal = fMassWall + fMassCavity;
104  G4double fMassWallRatio = fMassWall/massTotal;
105  fKinematic->RunInitialisation(effWallThick, fMassWallRatio );
106 
107  G4double massRatio = fMassCavity/fMassWall;
108 
109  //check radius
110  //
111  G4double worldRadius = fDetector->GetWorldRadius();
112  G4double RangeCavity = emCal.GetCSDARange(fEnergyGun,particleGun,mateCavity);
113 
114  std::ios::fmtflags mode = G4cout.flags();
115  G4cout.setf(std::ios::fixed,std::ios::floatfield);
116  G4int prec = G4cout.precision(3);
117 
118  G4cout << "\n ======================== run conditions =====================\n";
119 
120  G4cout << "\n The run will be " << NbofEvents << " "<< partName << " of "
121  << G4BestUnit(fEnergyGun,"Energy") << " through 2*"
122  << G4BestUnit(effWallThick,"Length") << " of "
123  << mateWall->GetName() << " (density: "
124  << G4BestUnit(densityWall,"Volumic Mass") << "); Mass/cm2 = "
125  << G4BestUnit(fMassWall*cm2,"Mass")
126  << "\n csdaRange: " << G4BestUnit(RangeWall,"Length") << G4endl;
127 
128  G4cout << "\n the cavity is "
129  << G4BestUnit(cavityThickness,"Length") << " of "
130  << mateCavity->GetName() << " (density: "
131  << G4BestUnit(densityCavity,"Volumic Mass") << "); Mass/cm2 = "
132  << G4BestUnit(fMassCavity*cm2,"Mass")
133  << " --> massRatio = " << std::setprecision(6) << massRatio << G4endl;
134 
135  G4cout.precision(3);
136  G4cout << " World radius: " << G4BestUnit(worldRadius,"Length")
137  << "; range in cavity: " << G4BestUnit(RangeCavity,"Length")
138  << G4endl;
139 
140  G4cout << "\n ============================================================\n";
141 
142  //stopping power from EmCalculator
143  //
144  G4double dedxWall =
145  emCal.GetDEDX(fEnergyGun,G4Electron::Electron(),mateWall);
146  dedxWall /= densityWall;
147  G4double dedxCavity =
148  emCal.GetDEDX(fEnergyGun,G4Electron::Electron(),mateCavity);
149  dedxCavity /= densityCavity;
150 
151  G4cout << std::setprecision(4)
152  << "\n StoppingPower in wall = "
153  << G4BestUnit(dedxWall,"Energy*Surface/Mass")
154  << "\n in cavity = "
155  << G4BestUnit(dedxCavity,"Energy*Surface/Mass")
156  << G4endl;
157 
158  //process counter
159  //
160  fProcCounter = new ProcessesCount;
161 
162  //charged particles and energy flow in cavity
163  //
164  fPartFlowCavity[0] = fPartFlowCavity[1] = 0;
165  fEnerFlowCavity[0] = fEnerFlowCavity[1] = 0.;
166 
167  //total energy deposit and charged track segment in cavity
168  //
169  fEdepCavity = fEdepCavity2 = fTrkSegmCavity = 0.;
170  fNbEventCavity = 0;
171 
172  //stepLenth of charged particles
173  //
174  fStepWall = fStepWall2 = fStepCavity = fStepCavity2 =0.;
175  fNbStepWall = fNbStepCavity = 0;
176 
177  //histograms
178  //
179  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
180  if ( analysisManager->IsActive() ) {
181  analysisManager->OpenFile();
182  }
183 
184  // reset default formats
185  G4cout.setf(mode,std::ios::floatfield);
186  G4cout.precision(prec);
187 }
188 
189 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
190 
191 void RunAction::CountProcesses(G4String procName)
192 {
193  //does the process already encounted ?
194  size_t nbProc = fProcCounter->size();
195  size_t i = 0;
196  while ((i<nbProc)&&((*fProcCounter)[i]->GetName()!=procName)) i++;
197  if (i == nbProc) fProcCounter->push_back( new OneProcessCount(procName));
198 
199  (*fProcCounter)[i]->Count();
200 }
201 
202 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
203 
204 void RunAction::SurveyConvergence(G4int NbofEvents)
205 {
206  if (NbofEvents == 0) return;
207 
208 
209  //beam fluence
210  //
211  G4int Nwall = fKinematic->GetWallCount();
212  G4int Ncavity = fKinematic->GetCavityCount();
213  G4double Iwall = Nwall/fMassWall;
214  G4double Icavity = Ncavity/fMassCavity;
215  G4double Iratio = Icavity/Iwall;
216  G4double Itot = NbofEvents/(fMassWall+fMassCavity);
217  G4double energyFluence = fEnergyGun*Itot;
218 
219  //total dose in cavity
220  //
221  G4double doseCavity = fEdepCavity/fMassCavity;
222  G4double ratio = doseCavity/energyFluence;
223  G4double err = 100*(ratio-1.);
224 
225  std::ios::fmtflags mode = G4cout.flags();
226  G4cout.setf(std::ios::fixed,std::ios::floatfield);
227  G4int prec = G4cout.precision(5);
228 
229  G4cout << "\n--->evntNb= " << NbofEvents
230  << " Nwall= " << Nwall
231  << " Ncav= " << Ncavity
232  << " Ic/Iw= " << Iratio
233  << " Ne-_cav= " << fPartFlowCavity[0]
234  << " doseCavity/Ebeam= " << ratio
235  << " (100*(ratio-1) = " << err << " %)"
236  << G4endl;
237 
238  // reset default formats
239  G4cout.setf(mode,std::ios::floatfield);
240  G4cout.precision(prec);
241 }
242 
243 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
244 
245 void RunAction::EndOfRunAction(const G4Run* aRun)
246 {
247  std::ios::fmtflags mode = G4cout.flags();
248  G4cout.setf(std::ios::fixed,std::ios::floatfield);
249  G4int prec = G4cout.precision(3);
250 
251  G4int NbofEvents = aRun->GetNumberOfEvent();
252  if (NbofEvents == 0) return;
253 
254  //frequency of processes
255  //
256  G4cout << "\n Process calls frequency --->";
257  for (size_t i=0; i< fProcCounter->size();i++) {
258  G4String procName = (*fProcCounter)[i]->GetName();
259  G4int count = (*fProcCounter)[i]->GetCounter();
260  G4cout << " " << procName << "= " << count;
261  }
262  G4cout << G4endl;
263 
264  //charged particle flow in cavity
265  //
266  G4cout
267  << "\n Charged particle flow in cavity :"
268  << "\n Enter --> nbParticles = " << fPartFlowCavity[0]
269  << "\t Energy = " << G4BestUnit (fEnerFlowCavity[0], "Energy")
270  << "\n Exit --> nbParticles = " << fPartFlowCavity[1]
271  << "\t Energy = " << G4BestUnit (fEnerFlowCavity[1], "Energy")
272  << G4endl;
273 
274  if (fPartFlowCavity[0] == 0) return;
275 
276  //beam fluence
277  //
278  G4int Nwall = fKinematic->GetWallCount();
279  G4int Ncavity = fKinematic->GetCavityCount();
280  G4double Iwall = Nwall/fMassWall;
281  G4double Icavity = Ncavity/fMassCavity;
282  G4double Iratio = Icavity/Iwall;
283  G4double Itot = NbofEvents/(fMassWall+fMassCavity);
284  G4double energyFluence = fEnergyGun*Itot;
285 
286  G4cout.precision(5);
287  G4cout
288  << "\n beamFluence in wall = " << Nwall
289  << "\t in cavity = " << Ncavity
290  << "\t Icav/Iwall = " << Iratio
291  << "\t energyFluence = " << energyFluence/(MeV*cm2/mg) << " MeV*cm2/mg"
292  << G4endl;
293 
294  //error on Edep in cavity
295  //
296  if (fNbEventCavity == 0) return;
297  G4double meanEdep = fEdepCavity/fNbEventCavity;
298  G4double meanEdep2 = fEdepCavity2/fNbEventCavity;
299  G4double varianceEdep = meanEdep2 - meanEdep*meanEdep;
300  G4double dEoverE = 0.;
301  if(varianceEdep>0.) dEoverE = std::sqrt(varianceEdep/fNbEventCavity)/meanEdep;
302 
303  //total dose in cavity
304  //
305  G4double doseCavity = fEdepCavity/fMassCavity;
306  G4double ratio = doseCavity/energyFluence, error = ratio*dEoverE;
307 
308  G4cout
309  << "\n Total edep in cavity = " << G4BestUnit(fEdepCavity,"Energy")
310  << " +- " << 100*dEoverE << " %"
311  << "\n Total dose in cavity = " << doseCavity/(MeV*cm2/mg) << " MeV*cm2/mg"
312  << " +- " << 100*dEoverE << " %"
313  << "\n\n DoseCavity/EnergyFluence = " << ratio
314  << " +- " << error << G4endl;
315 
316 
317  //track length in cavity
318  G4double meantrack = fTrkSegmCavity/fPartFlowCavity[0];
319 
320  G4cout.precision(4);
321  G4cout
322  << "\n Total charged trackLength in cavity = "
323  << G4BestUnit(fTrkSegmCavity,"Length")
324  << " (mean value = " << G4BestUnit(meantrack,"Length") << ")"
325  << G4endl;
326 
327  //compute mean step size of charged particles
328  //
329  fStepWall /= fNbStepWall; fStepWall2 /= fNbStepWall;
330  G4double rms = fStepWall2 - fStepWall*fStepWall;
331  if (rms>0.) rms = std::sqrt(rms); else rms = 0.;
332  G4double nbTrackWall = fKinematic->GetWallCount();
333 
334  G4cout
335  << "\n StepSize of ch. tracks in wall = "
336  << G4BestUnit(fStepWall,"Length") << " +- " << G4BestUnit( rms,"Length")
337  << "\t (nbSteps/track = " << double(fNbStepWall)/nbTrackWall << ")";
338 
339  fStepCavity /= fNbStepCavity; fStepCavity2 /= fNbStepCavity;
340  rms = fStepCavity2 - fStepCavity*fStepCavity;
341  if (rms>0.) rms = std::sqrt(rms); else rms = 0.;
342 
343  G4cout
344  << "\n StepSize of ch. tracks in cavity = "
345  << G4BestUnit(fStepCavity,"Length") << " +- " << G4BestUnit( rms,"Length")
346  << "\t (nbSteps/track = " << double(fNbStepCavity)/fPartFlowCavity[0] << ")";
347 
348  G4cout << G4endl;
349 
350  // reset default formats
351  G4cout.setf(mode,std::ios::floatfield);
352  G4cout.precision(prec);
353 
354  // delete and remove all contents in fProcCounter
355  while (fProcCounter->size()>0){
356  OneProcessCount* aProcCount=fProcCounter->back();
357  fProcCounter->pop_back();
358  delete aProcCount;
359  }
360  delete fProcCounter;
361 
362  // save histograms
363  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
364  if ( analysisManager->IsActive() ) {
365  analysisManager->Write();
366  analysisManager->CloseFile();
367  }
368 
369  // show Rndm status
371 }
372 
373 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
std::vector< OneProcessCount * > ProcessesCount
const G4String & GetName() const
Definition: G4Material.hh:176
G4double GetDensity() const
Definition: G4Material.hh:178
#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
G4double GetDEDX(G4double kinEnergy, const G4ParticleDefinition *, const G4Material *, const G4Region *r=0)
G4double GetCSDARange(G4double kinEnergy, const G4ParticleDefinition *, const G4Material *, const G4Region *r=0)
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
G4int GetNumberOfEventToBeProcessed() const
Definition: G4Run.hh:83
static G4Electron * Electron()
Definition: G4Electron.cc:94
#define G4endl
Definition: G4ios.hh:61
double G4double
Definition: G4Types.hh:76
G4double GetParticleEnergy() const