geant4-B1-example  1.0
 All Classes Files Functions Variables Pages
B1RunAction.cc
Go to the documentation of this file.
1 /// @file B1RunAction.cc
2 /// @brief Implementation of the B1RunAction class
3 
4 #include "B1RunAction.hh"
7 // #include "B1Run.hh"
8 
9 #include "G4RunManager.hh"
10 #include "G4Run.hh"
11 #include "G4ParameterManager.hh"
12 #include "G4LogicalVolumeStore.hh"
13 #include "G4LogicalVolume.hh"
14 #include "G4UnitsTable.hh"
15 #include "G4SystemOfUnits.hh"
16 
17 /*!
18  + define units (milligray, microgray, ... )
19  + Register our fEdep and fEdep2 parameters by parameterManager.
20 */
22  G4UserRunAction(),
23  fEdep("Edep", 0.),
24  fEdep2("Edep2", 0.)
25 
26 {
27 
28  // add new units for dose
29  const G4double milligray = 1.e-3*gray;
30  const G4double microgray = 1.e-6*gray;
31  const G4double nanogray = 1.e-9*gray;
32  const G4double picogray = 1.e-12*gray;
33 
34 
35  // ( name , symbol , category, value )
36  new G4UnitDefinition("milligray", "milliGy" , "Dose", milligray);
37  new G4UnitDefinition("microgray", "microGy" , "Dose", microgray);
38  new G4UnitDefinition("nanogray" , "nanoGy" , "Dose", nanogray);
39  new G4UnitDefinition("picogray" , "picoGy" , "Dose", picogray);
40 
41  // Register parameter to the parameter manager
42  G4ParameterManager* parameterManager = G4ParameterManager::Instance();
43  parameterManager->RegisterParameter(fEdep);
44  parameterManager->RegisterParameter(fEdep2);
45 }
46 
47 /// do nothing..
49 
50 
51 
52 
53 /*!
54  + BeginOfRunAction() will be invoked at the begining of BeamOn()
55  + set to not record random number.
56  + call parameterManager to reset all registered parameters to zero.
57 */
59 {
60 
61 
62  // inform the runManager to save random number seed
63  G4RunManager::GetRunManager()->SetRandomNumberStore(false);
64 
65  // reset parameters to their initial values
66  G4ParameterManager* parameterManager = G4ParameterManager::Instance();
67 
68  parameterManager->Reset(); // set fEdep and fEdep2 to 0
69 
70 }
71 
72 
73 
74 
75 
76 
77 /*!
78  + do parameter merging ( for multi thread )
79  + from fEdep and fEdep2, calculate rms.
80  + from rms, calculate rms/dos
81  + print out data on the screen ( only for sequence mode )
82 
83 */
84 void B1RunAction::EndOfRunAction(const G4Run* run)
85 {
86  G4int nofEvents = run->GetNumberOfEvent();
87  if (nofEvents == 0) return;
88 
89  // Merge parameters for multi-thread purpose.
90  G4ParameterManager* parameterManager = G4ParameterManager::Instance();
91  parameterManager->Merge();
92 
93 
94 
95 
96  // Compute dose = total energy deposit in a run and its variance
97  G4double edep = fEdep.GetValue();
98  G4double edep2 = fEdep2.GetValue();
99 
100  G4double rms = edep2 - edep*edep/nofEvents;
101  if (rms > 0.) rms = std::sqrt(rms); else rms = 0.;
102 
103 
104 
105  // retrieve our detectorConstruction
106  const B1DetectorConstruction* detectorConstruction
107  = static_cast<const B1DetectorConstruction*>
108  (G4RunManager::GetRunManager()->GetUserDetectorConstruction());
109 
110 
111 
112  G4double mass = detectorConstruction->GetScoringVolume()->GetMass();
113  G4double dose = edep/mass;
114  G4double rmsDose = rms/mass;
115 
116 
117 
118 
119 
120  // Run conditions
121  // note: There is no primary generator action object for "master"
122  // run manager for multi-threaded mode.
123 
124  const B1PrimaryGeneratorAction* generatorAction
125  = static_cast<const B1PrimaryGeneratorAction*>
126  (G4RunManager::GetRunManager()->GetUserPrimaryGeneratorAction());
127 
128 
129 
130  G4String runCondition;
131 
132  if (generatorAction)
133  {
134  // if the retrival is ok.
135 
136  const G4ParticleGun* particleGun = generatorAction->GetParticleGun();
137 
138  runCondition += particleGun->GetParticleDefinition()->GetParticleName();
139 
140  runCondition += " of ";
141 
142  G4double particleEnergy = particleGun->GetParticleEnergy();
143 
144  runCondition += G4BestUnit(particleEnergy,"Energy");
145  }
146 
147 
148 
149 
150 
151 
152 
153 
154  // Print
155  //
156  if (IsMaster()) {
157  G4cout
158  << G4endl
159  << "--------------------End of Global Run-----------------------";
160  }
161  else {
162  G4cout
163  << G4endl
164  << "--------------------End of Local Run------------------------";
165  }
166 
167  G4cout
168  << G4endl
169  << " The run consists of " << nofEvents << " "<< runCondition
170  << G4endl
171  << " Cumulated dose per run, in scoring volume : "
172  << G4BestUnit(dose,"Dose") << " rms = " << G4BestUnit(rmsDose,"Dose")
173  << G4endl
174  << "------------------------------------------------------------"
175  << G4endl
176  << G4endl;
177 }
178 
179 
180 
181 /*!
182  @brief for a user event action object to update fEdep and fEdep2. <br>
183  fEdep represents the energy deposit of a run, which consists many events.
184 
185  @param edep
186  the deposit energy from a given event.
187 */
188 void B1RunAction::AddEdep(G4double edep)
189 {
190  // note: G4Parameter<G4double> fEdep;
191  fEdep += edep;
192  fEdep2 += edep*edep;
193 
194 }
195 
196 
197 
virtual void EndOfRunAction(const G4Run *)
Definition: B1RunAction.cc:84
Definition of the B1DetectorConstruction class.
Detector construction class to define materials and geometry.
Definition of the B1PrimaryGeneratorAction class.
void AddEdep(G4double edep)
for a user event action object to update fEdep and fEdep2. fEdep represents the energy deposit of a...
Definition: B1RunAction.cc:188
G4LogicalVolume * GetScoringVolume() const
virtual void BeginOfRunAction(const G4Run *)
Definition: B1RunAction.cc:58
const G4ParticleGun * GetParticleGun() const
virtual ~B1RunAction()
do nothing..
Definition: B1RunAction.cc:48