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 "G4RunManager.hh"
39 #include "Analysis.hh"
40 #include "G4Threading.hh"
41 #include "CommandLineParser.hh"
42 
43 using namespace G4DNAPARSER;
44 
45 void PrintNParticles(std::map<const G4ParticleDefinition*, int>& container);
46 
47 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
48 
50  fInitialized(0), fDebug(false)
51 {}
52 
53 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
54 
56 {}
57 
58 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
59 
60 void RunAction::BeginOfRunAction(const G4Run* run)
61 {
62  // In this example, we considered that the same class was
63  // used for both master and worker threads.
64  // However, in case the run action is long,
65  // for better code review, this practice is not recommanded.
66  //
67  // Please note, in the example provided with the Geant4 X beta version,
68  // this RunAction class were not used by the master thread.
69 
70 
71  bool sequential = (G4RunManager::GetRunManager()->GetRunManagerType() ==
73 
74  if(isMaster && sequential == false )
75  // WARNING : in sequential mode, isMaster == true
76  {
77  BeginMaster(run);
78  }
79  else BeginWorker(run);
80 }
81 
82 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
83 
84 void RunAction::EndOfRunAction(const G4Run* run)
85 {
86 
87  bool sequential = (G4RunManager::GetRunManager()->GetRunManagerType() ==
89 
90  if(isMaster && sequential == false)
91  {
92  EndMaster(run);
93  }
94  else
95  {
96  EndWorker(run);
97  }
98 }
99 
100 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
101 
102 void RunAction::BeginMaster(const G4Run* run)
103 {
104  if(fDebug)
105  {
106  bool sequential = (G4RunManager::GetRunManager()->GetRunManagerType() ==
108  G4cout << "===================================" << G4endl;
109  if(!sequential)
110  G4cout << "================ RunAction::BeginMaster" << G4endl;
111  PrintRunInfo(run);
112  G4cout << "===================================" << G4endl;
113  }
114 }
115 
116 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
117 
118 void RunAction::BeginWorker(const G4Run* run)
119 {
120  if (fDebug)
121  {
122  G4cout << "===================================" << G4endl;
123  G4cout << "================ RunAction::BeginWorker" << G4endl;
124  PrintRunInfo(run);
125  G4cout << "===================================" << G4endl;
126  }
127  if(fInitialized == false) InitializeWorker(run);
128 
129  CreateNtuple();
130 }
131 
132 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
133 
134 void RunAction::EndMaster(const G4Run*)
135 {
136 }
137 
138 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
139 
140 void RunAction::EndWorker(const G4Run* run)
141 {
142  if(fDebug)
143  {
144  PrintRunInfo(run);
145  }
146 
147  G4int nofEvents = run->GetNumberOfEvent();
148  if ( nofEvents == 0 )
149  {
150  if(fDebug)
151  {
152  G4cout << "================ NO EVENTS TREATED IN THIS RUN ==> Exit"
153  << G4endl;
154  }
155  return;
156  }
157 
159  // Write Ntuple
160  //
161  WriteNtuple();
162 
164  // Complete cleanup
165  //
166  delete G4AnalysisManager::Instance();
167 }
168 
169 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
170 
172 {
173  // Initialize worker here
174  // example: you want to retrieve pointers to other user actions
175  fInitialized = true;
176 }
177 
178 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
179 
181 {
182  // Book histograms, ntuple
183 
184  // Create analysis manager
185  // The choice of analysis technology is done via selection of a namespace
186  // in Analysis.hh
187 
188  CommandLineParser* parser = CommandLineParser::GetParser();
189  Command* command(0);
190  if((command = parser->GetCommandIfActive("-out"))==0) return;
191 
192  G4cout << "##### Create analysis manager " << " " << this << G4endl;
193  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
194 // if(!analysisManager->IsActive()) {return; }
195 
196  G4cout << "Using " << analysisManager->GetType() <<
197  " analysis manager" << G4endl;
198 
199  // Create directories
200 
201  //analysisManager->SetHistoDirectoryName("histograms");
202  //analysisManager->SetNtupleDirectoryName("ntuple");
203  analysisManager->SetVerboseLevel(1);
204 
205  // Open an output file
206  G4String fileName;
207  if(command->GetOption().empty() == false)
208  {
209  fileName = command->GetOption();
210  }
211  else
212  {
213  fileName = "wholeNuclearDNA";
214 // fileName = command->GetDefaultOption(); // should work as well
215  }
216  analysisManager->OpenFile(fileName);
217 
218  // Creating ntuple
219  analysisManager->CreateNtuple("ntuple", "geom_dna");
220  analysisManager->CreateNtupleDColumn("flagParticle");
221  analysisManager->CreateNtupleDColumn("flagProcess");
222  analysisManager->CreateNtupleDColumn("flagVolume");
223  analysisManager->CreateNtupleDColumn("x");
224  analysisManager->CreateNtupleDColumn("y");
225  analysisManager->CreateNtupleDColumn("z");
226  analysisManager->CreateNtupleDColumn("edep");
227  analysisManager->CreateNtupleDColumn("stepLength");
228 
229  analysisManager->FinishNtuple();
230 
231 }
232 
233 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
234 
236 {
237  CommandLineParser* parser = CommandLineParser::GetParser();
238  Command* commandLine(0);
239  if((commandLine = parser->GetCommandIfActive("-out"))==0) return;
240 
241  // print histogram statistics
242  //
243  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
244 // if(!analysisManager->IsActive()) {return; }
245 
246  // save histograms
247  //
248  analysisManager->Write();
249  analysisManager->CloseFile();
250 }
251 
252 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
253 
254 void RunAction::PrintRunInfo(const G4Run* run)
255 {
256  G4cout << "================ Run is = "
257  << run->GetRunID() << G4endl;
258  G4cout << "================ Run type is = "
260  G4cout << "================ Event processed = "
262  G4cout << "================ Nevent = "
263  << run->GetNumberOfEvent() << G4endl;
264 }
265 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
void BeginOfRunAction(const G4Run *)
Definition: RunAction.cc:57
Command * GetCommandIfActive(const G4String &marker)
void PrintRunInfo(const G4Run *run)
Definition: RunAction.cc:258
void EndWorker(const G4Run *)
Definition: RunAction.cc:135
bool fDebug
Definition: RunAction.hh:83
int G4int
Definition: G4Types.hh:78
void CreateNtuple()
Definition: RunAction.cc:180
void PrintNParticles(std::map< const G4ParticleDefinition *, int > &container)
Definition: RunAction.cc:267
G4GLOB_DLL std::ostream G4cout
void EndMaster(const G4Run *)
Definition: RunAction.cc:125
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
Definition: G4Run.hh:46
bool fInitialized
Definition: RunAction.hh:82
ExG4HbookAnalysisManager G4AnalysisManager
Definition: g4hbook_defs.hh:66
CommandLineParser * parser(0)
void WriteNtuple()
Definition: RunAction.cc:235
~RunAction()
Definition: RunAction.cc:52
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
virtual const G4String & GetOption()