Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
examples/advanced/microdosimetry/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 // This example is provided by the Geant4-DNA collaboration
27 // Any report or published results obtained using the Geant4-DNA software
28 // shall cite the following Geant4-DNA collaboration publication:
29 // Med. Phys. 37 (2010) 4692-4708
30 // The Geant4-DNA web site is available at http://geant4-dna.org
31 //
32 
33 #include "RunAction.hh"
34 #include "G4Run.hh"
35 #include "TrackingAction.hh"
36 #include "G4ParticleDefinition.hh"
37 #include "G4RunManager.hh"
38 #include "Analysis.hh"
39 #include "G4Threading.hh"
40 
41 void PrintNParticles(std::map<const G4ParticleDefinition*, int>& container);
42 
43 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
44 
46 {
47  fpTrackingAction = 0;
48  fInitialized = 0;
49  fDebug = false;
50 }
51 
52 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
53 
55 {}
56 
57 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
58 
59 void RunAction::BeginOfRunAction(const G4Run* run)
60 {
61  // In this example, we considered that the same class was
62  // used for both master and worker threads.
63  // However, in case the run action is long,
64  // for better code review, this practice is not recommanded.
65  //
66  // Please note, in the example provided with the Geant4 X beta version,
67  // this RunAction class were not used by the master thread.
68 
69  if(isMaster) // WARNING : in sequential mode, isMaster == true
70  {
71  BeginMaster(run);
72  }
73  else BeginWorker(run);
74 }
75 
76 void RunAction::EndOfRunAction(const G4Run* run)
77 {
78  if(isMaster)
79  {
80  EndMaster(run);
81  }
82  else
83  {
84  EndWorker(run);
85  }
86 }
87 
88 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
89 
90 void RunAction::BeginMaster(const G4Run* run)
91 {
93 
94  if(fDebug)
95  {
96  G4cout << "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°" << G4endl;
97  if(!sequential)
98  G4cout << "°°°°°°°°°°°°°°°° RunAction::BeginMaster" << G4endl;
99  PrintRunInfo(run);
100  G4cout << "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°" << G4endl;
101  }
102 
103  if(sequential)
104  {
105  if(fInitialized == false) InitializeWorker(run);
106  // Note: fpTrackingAction could be used as a flag for initialization instead
107 
108  CreateHistogram();
109  }
110 }
111 
112 void RunAction::BeginWorker(const G4Run* run)
113 {
114  if(fDebug)
115  {
116  G4cout << "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°" << G4endl;
117  G4cout << "°°°°°°°°°°°°°°°° RunAction::BeginWorker" << G4endl;
118  PrintRunInfo(run);
119  G4cout << "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°" << G4endl;
120  }
121  if(fInitialized == false) InitializeWorker(run);
122 
123  CreateHistogram();
124 }
125 
126 void RunAction::EndMaster(const G4Run* run)
127 {
129  if(sequential)
130  {
131  EndWorker(run);
132  }
133 }
134 
135 void RunAction::EndWorker(const G4Run* run)
136 {
137  if(fDebug)
138  {
139  PrintRunInfo(run);
140  }
141 
142  G4int nofEvents = run->GetNumberOfEvent();
143  if ( nofEvents == 0 )
144  {
145  if(fDebug)
146  {
147  G4cout << "°°°°°°°°°°°°°°°° NO EVENTS TREATED IN THIS RUN ==> LEAVING RunAction::EndOfRunAction "<< G4endl;
148  }
149  return;
150  }
151 
152  ///////////////
153  // Write Histo
154  //
155  WriteHistogram();
156 
157  ///////////////
158  // Complete cleanup
159  //
160  delete G4AnalysisManager::Instance();
161 
162  ///////////////
163  // Printouts
164  //
165  std::map<const G4ParticleDefinition*, int>&
166  particlesCreatedInWorld = fpTrackingAction->GetNParticlesCreatedInWorld();
167 
168  G4cout << "Number and type of particles created outside region \"Target\" :" << G4endl;
169 
170  PrintNParticles(particlesCreatedInWorld);
171 
172  G4cout << "_______________________" << G4endl;
173 
174  std::map<const G4ParticleDefinition*, int>&
175  particlesCreatedInTarget = fpTrackingAction->GetNParticlesCreatedInTarget();
176 
177  G4cout << "Number and type of particles created in region \"Target\" :" << G4endl;
178 
179  PrintNParticles(particlesCreatedInTarget);
180 
181 }
182 
183 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
184 
185 void RunAction::InitializeWorker(const G4Run*)
186 {
187  if (fpTrackingAction == 0)
188  {
190 
191  if(fpTrackingAction == 0 && isMaster == false)
192  {
193  G4ExceptionDescription exDescrption ;
194  exDescrption << "fpTrackingAction is a null pointer. Has it been correctly initialized ?";
195  G4Exception("RunAction::BeginOfRunAction","RunAction001",FatalException, exDescrption);
196  }
197  }
198 
199  fInitialized = true;
200 }
201 
202 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
203 
204 void RunAction::CreateHistogram()
205 {
206  // Book histograms, ntuple
207 
208  // Create analysis manager
209  // The choice of analysis technology is done via selection of a namespace
210  // in Analysis.hh
211 
212  G4cout << "##### Create analysis manager " << " " << this << G4endl;
213  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
214 
215  G4cout << "Using " << analysisManager->GetType() << " analysis manager" << G4endl;
216 
217  // Create directories
218 
219  //analysisManager->SetHistoDirectoryName("histograms");
220  //analysisManager->SetNtupleDirectoryName("ntuple");
221  analysisManager->SetVerboseLevel(1);
222 
223  // Open an output file
224 
225  G4String fileName = "microdosimetry";
226  analysisManager->OpenFile(fileName);
227 
228  // Creating ntuple
229 
230  analysisManager->CreateNtuple("microdosimetry", "physics");
231  analysisManager->CreateNtupleDColumn("flagParticle");
232  analysisManager->CreateNtupleDColumn("flagProcess");
233  analysisManager->CreateNtupleDColumn("x");
234  analysisManager->CreateNtupleDColumn("y");
235  analysisManager->CreateNtupleDColumn("z");
236  analysisManager->CreateNtupleDColumn("totalEnergyDeposit");
237  analysisManager->CreateNtupleDColumn("stepLength");
238  analysisManager->CreateNtupleDColumn("kineticEnergyDifference");
239  analysisManager->FinishNtuple();
240 }
241 
242 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
243 
244 void RunAction::WriteHistogram()
245 {
246  // print histogram statistics
247  //
248  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
249 
250  // save histograms
251  //
252  analysisManager->Write();
253  analysisManager->CloseFile();
254 }
255 
256 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
257 
258 void RunAction::PrintRunInfo(const G4Run* run)
259 {
260  G4cout << "°°°°°°°°°°°°°°°° Run is = " << run->GetRunID() << G4endl;
261  G4cout << "°°°°°°°°°°°°°°°° Run type is = " << G4RunManager::GetRunManager()->GetRunManagerType() << G4endl;
262  G4cout << "°°°°°°°°°°°°°°°° Event processed = " << run->GetNumberOfEventToBeProcessed() << G4endl;
263  G4cout << "°°°°°°°°°°°°°°°° N° Event = " << run->GetNumberOfEvent() << G4endl;
264 }
265 
266 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
267 
268 void PrintNParticles(std::map<const G4ParticleDefinition*, int>& container)
269 {
270  std::map<const G4ParticleDefinition*, int>::iterator it;
271  for(it = container.begin() ;
272  it != container.end(); it ++)
273  {
274  G4cout << "N " << it->first->GetParticleName() << " : " << it->second << G4endl;
275  }
276 }
277 
278 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
279 
280 
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
std::map< const G4ParticleDefinition *, int > & GetNParticlesCreatedInTarget()
int G4int
Definition: G4Types.hh:78
std::map< const G4ParticleDefinition *, int > & GetNParticlesCreatedInWorld()
G4GLOB_DLL std::ostream G4cout
G4int GetNumberOfEvent() const
Definition: G4Run.hh:79
RMType GetRunManagerType() const
G4int GetRunID() const
Definition: G4Run.hh:76
Definition: G4Run.hh:46
const G4UserTrackingAction * GetUserTrackingAction() const
ExG4HbookAnalysisManager G4AnalysisManager
Definition: g4hbook_defs.hh:46
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
static G4RunManager * GetRunManager()
Definition: G4RunManager.cc:74
void PrintNParticles(std::map< const G4ParticleDefinition *, int > &container)
G4int GetNumberOfEventToBeProcessed() const
Definition: G4Run.hh:83
#define G4endl
Definition: G4ios.hh:61