Geant4  10.02.p01
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 //
28 //
29 // $Id: RunAction.cc 82401 2014-06-18 14:43:54Z 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 
44 #include "Randomize.hh"
45 #include <iomanip>
46 
47 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
48 
50 :G4UserRunAction(),fDetector(det), fPrimary(kin), fHistoManager(0)
51 {
52  fHistoManager = new HistoManager();
53 }
54 
55 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
56 
58 {
59  delete fHistoManager;
60 }
61 
62 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
63 
65 {
66  //initialisation
67  //
68  fEnergyDeposit = 0.;
69 
70  fNbCharged = fNbNeutral = 0;
72  fEmin[0] = fEmin[1] = DBL_MAX;
73  fEmax[0] = fEmax[1] = 0.;
74 
75  fNbSteps = 0;
76  fTrackLength = 0.;
77 
78  //histograms
79  //
80  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
81  if ( analysisManager->IsActive() ) {
82  analysisManager->OpenFile();
83  }
84 
85  // do not save Rndm status
87  CLHEP::HepRandom::showEngineStatus();
88 }
89 
90 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
91 
92 void RunAction::EndOfRunAction(const G4Run* aRun)
93 {
94  G4int nbEvents = aRun->GetNumberOfEvent();
95  if (nbEvents == 0) return;
96 
97  G4Material* material = fDetector->GetMaterial();
98  G4double length = fDetector->GetSize();
99  G4double density = material->GetDensity();
100 
103  G4String partName = particle->GetParticleName();
105 
106  G4int prec = G4cout.precision(3);
107  G4cout << "\n ======================== run summary ======================\n";
108  G4cout << "\n The run was " << nbEvents << " " << partName << " of "
109  << G4BestUnit(ePrimary,"Energy") << " through "
110  << G4BestUnit(length,"Length") << " of "
111  << material->GetName() << " (density: "
112  << G4BestUnit(density,"Volumic Mass") << ")";
113  G4cout << "\n ===========================================================\n";
114  G4cout << G4endl;
115 
116  //save histograms
117  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
118  if ( analysisManager->IsActive() ) {
119  analysisManager->Write();
120  analysisManager->CloseFile();
121  }
122 
123  if (particle->GetPDGCharge() == 0.) return;
124 
125  G4cout.precision(5);
126 
127  //track length
128  //
129  G4double trackLPerEvent = fTrackLength/nbEvents;
130  G4double nbStepPerEvent = double(fNbSteps)/nbEvents;
131  G4double stepSize = fTrackLength/fNbSteps;
132 
133  G4cout
134  << "\n TrackLength= "
135  << G4BestUnit(trackLPerEvent, "Length")
136  << "\t nb of steps= " << nbStepPerEvent
137  << " stepSize= " << G4BestUnit(stepSize, "Length")
138  << G4endl;
139 
140  //charged secondaries (ionization, direct pair production)
141  //
142  G4double energyPerEvent = fEnergyCharged/nbEvents;
143  G4double nbPerEvent = double(fNbCharged)/nbEvents;
144  G4double meanEkin = 0.;
145  if (fNbCharged) meanEkin = fEnergyCharged/fNbCharged;
146 
147  G4cout
148  << "\n d-rays : eLoss/primary= "
149  << G4BestUnit(energyPerEvent, "Energy")
150  << "\t nb of d-rays= " << nbPerEvent
151  << " <Tkin>= " << G4BestUnit(meanEkin, "Energy")
152  << " Tmin= " << G4BestUnit(fEmin[0], "Energy")
153  << " Tmax= " << G4BestUnit(fEmax[0], "Energy")
154  << G4endl;
155 
156  //neutral secondaries (bremsstrahlung, pixe)
157  //
158  energyPerEvent = fEnergyNeutral/nbEvents;
159  nbPerEvent = double(fNbNeutral)/nbEvents;
160  meanEkin = 0.;
161  if (fNbNeutral) meanEkin = fEnergyNeutral/fNbNeutral;
162 
163  G4cout
164  << "\n gamma : eLoss/primary= "
165  << G4BestUnit(energyPerEvent, "Energy")
166  << "\t nb of gammas= " << nbPerEvent
167  << " <Tkin>= " << G4BestUnit(meanEkin, "Energy")
168  << " Tmin= " << G4BestUnit(fEmin[1], "Energy")
169  << " Tmax= " << G4BestUnit(fEmax[1], "Energy")
170  << G4endl;
171 
172 
173  G4EmCalculator emCal;
174 
175  //local energy deposit
176  //
177  energyPerEvent = fEnergyDeposit/nbEvents;
178  //
179  G4double r0 = emCal.GetRangeFromRestricteDEDX(ePrimary,particle,material);
180  G4double r1 = r0 - trackLPerEvent;
181  G4double etry = ePrimary - energyPerEvent;
182  G4double efinal = 0.;
183  if (r1 > 0.) efinal = GetEnergyFromRestrictedRange(r1,particle,material,etry);
184  G4double dEtable = ePrimary - efinal;
185  G4double ratio = 0.;
186  if (dEtable > 0.) ratio = energyPerEvent/dEtable;
187 
188  G4cout
189  << "\n deposit : eLoss/primary= "
190  << G4BestUnit(energyPerEvent, "Energy")
191  << "\t <dEcut > table= "
192  << G4BestUnit(dEtable, "Energy")
193  << " ---> simul/reference= " << ratio
194  << G4endl;
195 
196  //total energy transferred
197  //
199  energyPerEvent = energyTotal/nbEvents;
200  //
201  r0 = emCal.GetCSDARange(ePrimary,particle,material);
202  r1 = r0 - trackLPerEvent;
203  etry = ePrimary - energyPerEvent;
204  efinal = 0.;
205  if (r1 > 0.) efinal = GetEnergyFromCSDARange(r1,particle,material,etry);
206  dEtable = ePrimary - efinal;
207  ratio = 0.;
208  if (dEtable > 0.) ratio = energyPerEvent/dEtable;
209 
210  G4cout
211  << "\n total : eLoss/primary= "
212  << G4BestUnit(energyPerEvent, "Energy")
213  << "\t <dEfull> table= "
214  << G4BestUnit(dEtable, "Energy")
215  << " ---> simul/reference= " << ratio
216  << G4endl;
217 
218  G4cout.precision(prec);
219 
220  // show Rndm status
221  CLHEP::HepRandom::showEngineStatus();
222 }
223 
224 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
225 
227  G4ParticleDefinition* particle, G4Material* material, G4double Etry)
228 {
229  G4EmCalculator emCal;
230 
231  G4double Energy = Etry, dE = 0., dEdx;
232  G4double r, dr;
233  G4double err = 1., errmax = 0.00001;
234  G4int iter = 0 , itermax = 10;
235  while (err > errmax && iter < itermax) {
236  iter++;
237  Energy -= dE;
238  r = emCal.GetRangeFromRestricteDEDX(Energy,particle,material);
239  dr = r - range;
240  dEdx = emCal.GetDEDX(Energy,particle,material);
241  dE = dEdx*dr;
242  err = std::abs(dE)/Energy;
243  }
244  if (iter == itermax) {
245  G4cout
246  << "\n ---> warning: RunAction::GetEnergyFromRestRange() did not converge"
247  << " Etry = " << G4BestUnit(Etry,"Energy")
248  << " Energy = " << G4BestUnit(Energy,"Energy")
249  << " err = " << err
250  << " iter = " << iter << G4endl;
251  }
252 
253  return Energy;
254 }
255 
256 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
257 
259  G4ParticleDefinition* particle, G4Material* material, G4double Etry)
260 {
261  G4EmCalculator emCal;
262 
263  G4double Energy = Etry, dE = 0., dEdx;
264  G4double r, dr;
265  G4double err = 1., errmax = 0.00001;
266  G4int iter = 0 , itermax = 10;
267  while (err > errmax && iter < itermax) {
268  iter++;
269  Energy -= dE;
270  r = emCal.GetCSDARange(Energy,particle,material);
271  dr = r - range;
272  dEdx = emCal.ComputeTotalDEDX(Energy,particle,material);
273  dE = dEdx*dr;
274  err = std::abs(dE)/Energy;
275  }
276  if (iter == itermax) {
277  G4cout
278  << "\n ---> warning: RunAction::GetEnergyFromCSDARange() did not converge"
279  << " Etry = " << G4BestUnit(Etry,"Energy")
280  << " Energy = " << G4BestUnit(Energy,"Energy")
281  << " err = " << err
282  << " iter = " << iter << G4endl;
283  }
284 
285  return Energy;
286 }
287 
288 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
PrimaryGeneratorAction class.
G4double GetEnergyFromCSDARange(G4double, G4ParticleDefinition *, G4Material *, G4double)
Definition: RunAction.cc:258
void BeginOfRunAction(const G4Run *)
Definition: RunAction.cc:57
G4double fEmax[2]
Definition: RunAction.hh:92
G4double fEnergyDeposit
Definition: RunAction.hh:89
const G4String & GetName() const
Definition: G4Material.hh:178
G4double GetDensity() const
Definition: G4Material.hh:180
G4double ComputeTotalDEDX(G4double kinEnergy, const G4ParticleDefinition *, const G4Material *, G4double cut=DBL_MAX)
#define G4BestUnit(a, b)
#define G4_USE_G4BESTUNIT_FOR_VERBOSE 1
void SetRandomNumberStore(G4bool flag)
int G4int
Definition: G4Types.hh:78
DetectorConstruction * fDetector
Definition: RunAction.hh:63
G4int fNbCharged
Definition: RunAction.hh:95
const G4String & GetParticleName() const
HistoManager * fHistoManager
Definition: RunAction.hh:66
G4double GetDEDX(G4double kinEnergy, const G4ParticleDefinition *, const G4Material *, const G4Region *r=0)
G4long fNbSteps
Definition: RunAction.hh:94
G4double fEnergyCharged
Definition: RunAction.hh:91
G4double GetCSDARange(G4double kinEnergy, const G4ParticleDefinition *, const G4Material *, const G4Region *r=0)
G4double density
Definition: TRTMaterials.hh:39
G4int fNbNeutral
Definition: RunAction.hh:95
static const G4double dE
static const double prec
Definition: RanecuEngine.cc:58
G4GLOB_DLL std::ostream G4cout
G4int GetNumberOfEvent() const
Definition: G4Run.hh:79
void EndOfRunAction(const G4Run *)
Definition: RunAction.cc:260
G4double GetRangeFromRestricteDEDX(G4double kinEnergy, const G4ParticleDefinition *, const G4Material *, const G4Region *r=0)
Definition: G4Run.hh:46
ExG4HbookAnalysisManager G4AnalysisManager
Definition: g4hbook_defs.hh:66
G4double fEmin[2]
Definition: RunAction.hh:92
~RunAction()
Definition: RunAction.cc:52
static G4RunManager * GetRunManager()
Definition: G4RunManager.cc:79
G4ParticleGun * GetParticleGun()
G4ParticleDefinition * GetParticleDefinition() const
G4double GetEnergyFromRestrictedRange(G4double, G4ParticleDefinition *, G4Material *, G4double)
Definition: RunAction.cc:226
Detector construction class to demonstrate various ways of placement.
#define G4endl
Definition: G4ios.hh:61
G4double fTrackLength
Definition: RunAction.hh:90
double G4double
Definition: G4Types.hh:76
G4double fEnergyNeutral
Definition: RunAction.hh:91
G4double GetPDGCharge() const
#define DBL_MAX
Definition: templates.hh:83
G4double GetParticleEnergy() const
PrimaryGeneratorAction * fPrimary
Definition: RunAction.hh:64
const G4double r0