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 //
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 // $ID$
35 
36 #include "RunAction.hh"
37 #include "G4Run.hh"
38 #include "TrackingAction.hh"
39 #include "G4ParticleDefinition.hh"
40 #include "G4RunManager.hh"
41 #include "Analysis.hh"
42 #include "G4Threading.hh"
43 #include "CommandLineParser.hh"
44 
45 using namespace G4DNAPARSER;
46 
47 void PrintNParticles(std::map<const G4ParticleDefinition*, int>& container);
48 
49 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
50 
52  fpTrackingAction(0), fInitialized(0), fDebug(false)
53 {}
54 
55 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
56 
58 {}
59 
60 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
61 
62 void RunAction::BeginOfRunAction(const G4Run* run)
63 {
64  // In this example, we considered that the same class was
65  // used for both master and worker threads.
66  // However, in case the run action is long,
67  // for better code review, this practice is not recommanded.
68  //
69  // Please note, in the example provided with the Geant4 X beta version,
70  // this RunAction class were not used by the master thread.
71 
72  bool sequential = (G4RunManager::GetRunManager()->GetRunManagerType() ==
74 
75  if(isMaster && sequential == false )
76  // WARNING : in sequential mode, isMaster == true
77  {
78  BeginMaster(run);
79  }
80  else BeginWorker(run);
81 }
82 
83 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
84 
85 void RunAction::EndOfRunAction(const G4Run* run)
86 {
87 
88  bool sequential = (G4RunManager::GetRunManager()->GetRunManagerType() ==
90 
91  if(isMaster && sequential == false)
92  {
93  EndMaster(run);
94  }
95  else
96  {
97  EndWorker(run);
98  }
99 }
100 
101 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
102 
103 void RunAction::BeginMaster(const G4Run* run)
104 {
105  if(fDebug)
106  {
107  bool sequential = (G4RunManager::GetRunManager()->GetRunManagerType() ==
109  G4cout << "===================================" << G4endl;
110  if(!sequential)
111  G4cout << "================ RunAction::BeginMaster" << G4endl;
112  PrintRunInfo(run);
113  G4cout << "===================================" << G4endl;
114  }
115 }
116 
117 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
118 
119 void RunAction::BeginWorker(const G4Run* run)
120 {
121  if (fDebug)
122  {
123  G4cout << "===================================" << G4endl;
124  G4cout << "================ RunAction::BeginWorker" << G4endl;
125  PrintRunInfo(run);
126  G4cout << "===================================" << G4endl;
127  }
128  if(fInitialized == false) InitializeWorker(run);
129 
130  CreateHistogram();
131 }
132 
133 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
134 
135 void RunAction::EndMaster(const G4Run*)
136 {
137 }
138 
139 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
140 
141 void RunAction::EndWorker(const G4Run* run)
142 {
143  if(fDebug)
144  {
145  G4cout << "===================================" << G4endl;
146  G4cout << "================ RunAction::EndWorker" << G4endl;
147  PrintRunInfo(run);
148  G4cout << "===================================" << G4endl;
149  }
150 
151  G4int nofEvents = run->GetNumberOfEvent();
152  if ( nofEvents == 0 )
153  {
154  if(fDebug)
155  {
156  G4cout << "================ NO EVENTS TREATED IN THIS RUN ==> Exit"
157  << G4endl;
158  }
159  return;
160  }
161 
163  // Write Histo
164  //
165  WriteHistogram();
166 
168  // Complete cleanup
169  //
170  delete G4AnalysisManager::Instance();
171 
173  // Printouts
174  //
175  std::map<const G4ParticleDefinition*, int>&
176  particlesCreatedInWorld = fpTrackingAction->GetNParticlesCreatedInWorld();
177 
178  G4cout << "Number and type of particles created outside region \"Target\" :"
179  << G4endl;
180 
181  PrintNParticles(particlesCreatedInWorld);
182 
183  G4cout << "_______________________" << G4endl;
184 
185  std::map<const G4ParticleDefinition*, int>&
186  particlesCreatedInTarget = fpTrackingAction->GetNParticlesCreatedInTarget();
187 
188  G4cout << "Number and type of particles created in region \"Target\" :"
189  << G4endl;
190 
191  PrintNParticles(particlesCreatedInTarget);
192 }
193 
194 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
195 
197 {
199 
200  if (fpTrackingAction == 0)
201  {
203  GetUserTrackingAction();
204 
205  if(fpTrackingAction == 0 && isMaster == false)
206  {
207  G4ExceptionDescription exDescrption ;
208  exDescrption << "fpTrackingAction is a null pointer. "
209  "Has it been correctly initialized ?";
210  G4Exception("RunAction::BeginOfRunAction",
211  "RunAction001",FatalException, exDescrption);
212  }
213  }
214 
215  fInitialized = true;
216 }
217 
218 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
219 
221 {
222  // Book histograms, ntuple
223 
224  // Create analysis manager
225  // The choice of analysis technology is done via selection of a namespace
226  // in Analysis.hh
227 
228  CommandLineParser* parser = CommandLineParser::GetParser();
229  Command* command(0);
230  if((command = parser->GetCommandIfActive("-out"))==0) return;
231 
232  G4cout << "##### Create analysis manager " << " " << this << G4endl;
233  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
234 // if(!analysisManager->IsActive()) {return; }
235 
236  G4cout << "Using " << analysisManager->GetType() <<
237  " analysis manager" << G4endl;
238 
239  // Create directories
240 
241  //analysisManager->SetHistoDirectoryName("histograms");
242  //analysisManager->SetNtupleDirectoryName("ntuple");
243  analysisManager->SetVerboseLevel(1);
244 
245  // Open an output file
246  G4String fileName;
247  if(command->GetOption().empty() == false)
248  {
249  fileName = command->GetOption();
250  }
251  else
252  {
253  fileName = "microdosimetry";
254  }
255  analysisManager->OpenFile(fileName);
256 
257  // Creating ntuple
258 
259  analysisManager->CreateNtuple("microdosimetry", "physics");
260  analysisManager->CreateNtupleDColumn("flagParticle");
261  analysisManager->CreateNtupleDColumn("flagProcess");
262  analysisManager->CreateNtupleDColumn("x");
263  analysisManager->CreateNtupleDColumn("y");
264  analysisManager->CreateNtupleDColumn("z");
265  analysisManager->CreateNtupleDColumn("totalEnergyDeposit");
266  analysisManager->CreateNtupleDColumn("stepLength");
267  analysisManager->CreateNtupleDColumn("kineticEnergyDifference");
268  analysisManager->FinishNtuple();
269 }
270 
271 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
272 
274 {
275  CommandLineParser* parser = CommandLineParser::GetParser();
276  Command* commandLine(0);
277  if((commandLine = parser->GetCommandIfActive("-out"))==0) return;
278 
279  // print histogram statistics
280  //
281  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
282  // if(!analysisManager->IsActive()) {return; }
283 
284  // save histograms
285  //
286  analysisManager->Write();
287  analysisManager->CloseFile();
288 
289  if(fDebug)
290  {
291  G4cout << "================ ROOT FILES HAVE BEEN WRITTEN"
292  << G4endl;
293  }
294 }
295 
296 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
297 
298 void RunAction::PrintRunInfo(const G4Run* run)
299 {
300  G4cout << "================ Run is = "
301  << run->GetRunID() << G4endl;
302  G4cout << "================ Run type is = "
304  G4cout << "================ Event processed = "
306  G4cout << "================ Nevent = "
307  << run->GetNumberOfEvent() << G4endl;
308 }
309 
310 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
311 
312 void PrintNParticles(std::map<const G4ParticleDefinition*, int>& container)
313 {
314  std::map<const G4ParticleDefinition*, int>::iterator it;
315  for(it = container.begin() ;
316  it != container.end(); it ++)
317  {
318  G4cout << "N " << it->first->GetParticleName() << " : "
319  << it->second << G4endl;
320  }
321 }
322 
323 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
void BeginOfRunAction(const G4Run *)
Definition: RunAction.cc:57
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
std::map< const G4ParticleDefinition *, int > & GetNParticlesCreatedInTarget()
Command * GetCommandIfActive(const G4String &marker)
void PrintRunInfo(const G4Run *run)
Definition: RunAction.cc:258
void CreateHistogram()
Definition: RunAction.cc:204
void EndWorker(const G4Run *)
Definition: RunAction.cc:135
bool fDebug
Definition: RunAction.hh:83
int G4int
Definition: G4Types.hh:78
std::map< const G4ParticleDefinition *, int > & GetNParticlesCreatedInWorld()
void PrintNParticles(std::map< const G4ParticleDefinition *, int > &container)
Definition: RunAction.cc:267
TrackingAction * fpTrackingAction
Definition: RunAction.hh:81
G4GLOB_DLL std::ostream G4cout
void EndMaster(const G4Run *)
Definition: RunAction.cc:125
static RunInitManager * Instance()
G4int GetNumberOfEvent() const
Definition: G4Run.hh:79
RMType GetRunManagerType() const
void InitializeWorker(const G4Run *)
Definition: RunAction.cc:185
void EndOfRunAction(const G4Run *)
Definition: RunAction.cc:260
G4int GetRunID() const
Definition: G4Run.hh:76
void WriteHistogram()
Definition: RunAction.cc:244
Definition: G4Run.hh:46
bool fInitialized
Definition: RunAction.hh:82
ExG4HbookAnalysisManager G4AnalysisManager
Definition: g4hbook_defs.hh:66
CommandLineParser * parser(0)
~RunAction()
Definition: RunAction.cc:52
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
static G4RunManager * GetRunManager()
Definition: G4RunManager.cc:79
void BeginWorker(const G4Run *)
Definition: RunAction.cc:108
void BeginMaster(const G4Run *)
Definition: RunAction.cc:83
G4int GetNumberOfEventToBeProcessed() const
Definition: G4Run.hh:83
#define G4endl
Definition: G4ios.hh:61