Geant4  10.01
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 
73  bool sequential = (G4RunManager::GetRunManager()->GetRunManagerType() ==
75 
76  if(isMaster && sequential == false )
77  // WARNING : in sequential mode, isMaster == true
78  {
79  BeginMaster(run);
80  }
81  else BeginWorker(run);
82 }
83 
84 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
85 
86 void RunAction::EndOfRunAction(const G4Run* run)
87 {
88 
89  bool sequential = (G4RunManager::GetRunManager()->GetRunManagerType() ==
91 
92  if(isMaster && sequential == false)
93  {
94  EndMaster(run);
95  }
96  else
97  {
98  EndWorker(run);
99  }
100 }
101 
102 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
103 
104 void RunAction::BeginMaster(const G4Run* run)
105 {
106  if(fDebug)
107  {
108  bool sequential = (G4RunManager::GetRunManager()->GetRunManagerType() ==
110  G4cout << "===================================" << G4endl;
111  if(!sequential)
112  G4cout << "================ RunAction::BeginMaster" << G4endl;
113  PrintRunInfo(run);
114  G4cout << "===================================" << G4endl;
115  }
116 }
117 
118 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
119 
120 void RunAction::BeginWorker(const G4Run* run)
121 {
122  if (fDebug)
123  {
124  G4cout << "===================================" << G4endl;
125  G4cout << "================ RunAction::BeginWorker" << G4endl;
126  PrintRunInfo(run);
127  G4cout << "===================================" << G4endl;
128  }
129  if(fInitialized == false) InitializeWorker(run);
130 
131  CreateHistogram();
132 }
133 
134 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
135 
136 void RunAction::EndMaster(const G4Run*)
137 {
138 }
139 
140 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
141 
142 void RunAction::EndWorker(const G4Run* run)
143 {
144  if(fDebug)
145  {
146  G4cout << "===================================" << G4endl;
147  G4cout << "================ RunAction::EndWorker" << G4endl;
148  PrintRunInfo(run);
149  G4cout << "===================================" << G4endl;
150  }
151 
152  G4int nofEvents = run->GetNumberOfEvent();
153  if ( nofEvents == 0 )
154  {
155  if(fDebug)
156  {
157  G4cout << "================ NO EVENTS TREATED IN THIS RUN ==> Exit"
158  << G4endl;
159  }
160  return;
161  }
162 
164  // Write Histo
165  //
166  WriteHistogram();
167 
169  // Complete cleanup
170  //
171  delete G4AnalysisManager::Instance();
172 
174  // Printouts
175  //
176  std::map<const G4ParticleDefinition*, int>&
177  particlesCreatedInWorld = fpTrackingAction->GetNParticlesCreatedInWorld();
178 
179  G4cout << "Number and type of particles created outside region \"Target\" :"
180  << G4endl;
181 
182  PrintNParticles(particlesCreatedInWorld);
183 
184  G4cout << "_______________________" << G4endl;
185 
186  std::map<const G4ParticleDefinition*, int>&
187  particlesCreatedInTarget = fpTrackingAction->GetNParticlesCreatedInTarget();
188 
189  G4cout << "Number and type of particles created in region \"Target\" :"
190  << G4endl;
191 
192  PrintNParticles(particlesCreatedInTarget);
193 }
194 
195 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
196 
198 {
200 
201  if (fpTrackingAction == 0)
202  {
204  GetUserTrackingAction();
205 
206  if(fpTrackingAction == 0 && isMaster == false)
207  {
208  G4ExceptionDescription exDescrption ;
209  exDescrption << "fpTrackingAction is a null pointer. "
210  "Has it been correctly initialized ?";
211  G4Exception("RunAction::BeginOfRunAction",
212  "RunAction001",FatalException, exDescrption);
213  }
214  }
215 
216  fInitialized = true;
217 }
218 
219 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
220 
222 {
223  // Book histograms, ntuple
224 
225  // Create analysis manager
226  // The choice of analysis technology is done via selection of a namespace
227  // in Analysis.hh
228 
229  CommandLineParser* parser = CommandLineParser::GetParser();
230  Command* command(0);
231  if((command = parser->GetCommandIfActive("-out"))==0) return;
232 
233  G4cout << "##### Create analysis manager " << " " << this << G4endl;
234  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
235 // if(!analysisManager->IsActive()) {return; }
236 
237  G4cout << "Using " << analysisManager->GetType() <<
238  " analysis manager" << G4endl;
239 
240  // Create directories
241 
242  //analysisManager->SetHistoDirectoryName("histograms");
243  //analysisManager->SetNtupleDirectoryName("ntuple");
244  analysisManager->SetVerboseLevel(1);
245 
246  // Open an output file
247  G4String fileName;
248  if(command->GetOption().empty() == false)
249  {
250  fileName = command->GetOption();
251  }
252  else
253  {
254  fileName = "microdosimetry";
255  }
256  analysisManager->OpenFile(fileName);
257 
258  // Creating ntuple
259 
260  analysisManager->CreateNtuple("microdosimetry", "physics");
261  analysisManager->CreateNtupleDColumn("flagParticle");
262  analysisManager->CreateNtupleDColumn("flagProcess");
263  analysisManager->CreateNtupleDColumn("x");
264  analysisManager->CreateNtupleDColumn("y");
265  analysisManager->CreateNtupleDColumn("z");
266  analysisManager->CreateNtupleDColumn("totalEnergyDeposit");
267  analysisManager->CreateNtupleDColumn("stepLength");
268  analysisManager->CreateNtupleDColumn("kineticEnergyDifference");
269  analysisManager->FinishNtuple();
270 }
271 
272 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
273 
275 {
276  CommandLineParser* parser = CommandLineParser::GetParser();
277  Command* commandLine(0);
278  if((commandLine = parser->GetCommandIfActive("-out"))==0) return;
279 
280  // print histogram statistics
281  //
282  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
283 // if(!analysisManager->IsActive()) {return; }
284 
285  // save histograms
286  //
287  analysisManager->Write();
288  analysisManager->CloseFile();
289 
290  if(fDebug)
291  {
292  G4cout << "================ ROOT FILES HAVE BEEN WRITTEN"
293  << G4endl;
294  }
295 }
296 
297 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
298 
299 void RunAction::PrintRunInfo(const G4Run* run)
300 {
301  G4cout << "================ Run is = "
302  << run->GetRunID() << G4endl;
303  G4cout << "================ Run type is = "
305  G4cout << "================ Event processed = "
307  G4cout << "================ Nevent = "
308  << run->GetNumberOfEvent() << G4endl;
309 }
310 
311 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
312 
313 void PrintNParticles(std::map<const G4ParticleDefinition*, int>& container)
314 {
315  std::map<const G4ParticleDefinition*, int>::iterator it;
316  for(it = container.begin() ;
317  it != container.end(); it ++)
318  {
319  G4cout << "N " << it->first->GetParticleName() << " : "
320  << it->second << G4endl;
321  }
322 }
323 
324 //....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:61
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