ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RunAction.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file RunAction.cc
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 //
34 
35 #include "RunAction.hh"
36 #include "G4Run.hh"
37 #include "TrackingAction.hh"
38 #include "G4ParticleDefinition.hh"
39 #include "G4RunManager.hh"
40 #include "Analysis.hh"
41 #include "G4Threading.hh"
42 #include "CommandLineParser.hh"
43 
44 using namespace G4DNAPARSER;
45 
46 void PrintNParticles(std::map<const G4ParticleDefinition*, int>& container);
47 
48 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
49 
51  fpTrackingAction(0), fInitialized(0), fDebug(false)
52 {}
53 
54 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
55 
57 {}
58 
59 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
60 
61 void RunAction::BeginOfRunAction(const G4Run* run)
62 {
63  // In this example, we considered that the same class was
64  // used for both master and worker threads.
65  // However, in case the run action is long,
66  // for better code review, this practice is not recommanded.
67  //
68  // Please note, in the example provided with the Geant4 X beta version,
69  // this RunAction class were not used by the master thread.
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  CreateHistogram();
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  G4cout << "===================================" << G4endl;
145  G4cout << "================ RunAction::EndWorker" << G4endl;
146  PrintRunInfo(run);
147  G4cout << "===================================" << G4endl;
148  }
149 
150  G4int nofEvents = run->GetNumberOfEvent();
151  if ( nofEvents == 0 )
152  {
153  if(fDebug)
154  {
155  G4cout << "================ NO EVENTS TREATED IN THIS RUN ==> Exit"
156  << G4endl;
157  }
158  return;
159  }
160 
162  // Write Histo
163  //
164  WriteHistogram();
165 
167  // Complete cleanup
168  //
170 
172  // Printouts
173  //
174  std::map<const G4ParticleDefinition*, int>&
175  particlesCreatedInWorld = fpTrackingAction->GetNParticlesCreatedInWorld();
176 
177  G4cout << "Number and type of particles created outside region \"Target\" :"
178  << G4endl;
179 
180  PrintNParticles(particlesCreatedInWorld);
181 
182  G4cout << "_______________________" << G4endl;
183 
184  std::map<const G4ParticleDefinition*, int>&
185  particlesCreatedInTarget = fpTrackingAction->GetNParticlesCreatedInTarget();
186 
187  G4cout << "Number and type of particles created in region \"Target\" :"
188  << G4endl;
189 
190  PrintNParticles(particlesCreatedInTarget);
191 }
192 
193 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
194 
196 {
198 
199  if (fpTrackingAction == 0)
200  {
202  GetUserTrackingAction();
203 
204  if(fpTrackingAction == 0 && isMaster == false)
205  {
206  G4ExceptionDescription exDescrption ;
207  exDescrption << "fpTrackingAction is a null pointer. "
208  "Has it been correctly initialized ?";
209  G4Exception("RunAction::BeginOfRunAction",
210  "RunAction001",FatalException, exDescrption);
211  }
212  }
213 
214  fInitialized = true;
215 }
216 
217 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
218 
220 {
221  // Book histograms, ntuple
222 
223  // Create analysis manager
224  // The choice of analysis technology is done via selection of a namespace
225  // in Analysis.hh
226 
227  CommandLineParser* parser = CommandLineParser::GetParser();
228  Command* command(0);
229  if((command = parser->GetCommandIfActive("-out"))==0) return;
230 
231  G4cout << "##### Create analysis manager " << " " << this << G4endl;
232  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
233 
234  G4cout << "Using " << analysisManager->GetType() <<
235  " analysis manager" << G4endl;
236 
237  // Create directories
238 
239  //analysisManager->SetHistoDirectoryName("histograms");
240  //analysisManager->SetNtupleDirectoryName("ntuple");
241  analysisManager->SetVerboseLevel(1);
242 
243  // Open an output file
244  G4String fileName;
245  if(command->GetOption().empty() == false)
246  {
247  fileName = command->GetOption();
248  }
249  else
250  {
251  fileName = "microdosimetry";
252  }
253  analysisManager->OpenFile(fileName);
254 
255  // Creating ntuple
256 
257  analysisManager->CreateNtuple("microdosimetry", "physics");
258  analysisManager->CreateNtupleDColumn("flagParticle");
259  analysisManager->CreateNtupleDColumn("flagProcess");
260  analysisManager->CreateNtupleDColumn("x");
261  analysisManager->CreateNtupleDColumn("y");
262  analysisManager->CreateNtupleDColumn("z");
263  analysisManager->CreateNtupleDColumn("totalEnergyDeposit");
264  analysisManager->CreateNtupleDColumn("stepLength");
265  analysisManager->CreateNtupleDColumn("kineticEnergyDifference");
266  analysisManager->CreateNtupleDColumn("kineticEnergy");
267  analysisManager->CreateNtupleDColumn("cosTheta");
268  analysisManager->CreateNtupleIColumn("eventID");
269  analysisManager->CreateNtupleIColumn("trackID");
270  analysisManager->CreateNtupleIColumn("parentID");
271  analysisManager->CreateNtupleIColumn("stepID");
272  analysisManager->FinishNtuple();
273 }
274 
275 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
276 
278 {
279  CommandLineParser* parser = CommandLineParser::GetParser();
280  Command* commandLine(0);
281  if((commandLine = parser->GetCommandIfActive("-out"))==0) return;
282 
283  // print histogram statistics
284  //
285  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
286  // if(!analysisManager->IsActive()) {return; }
287 
288  // save histograms
289  //
290  analysisManager->Write();
291  analysisManager->CloseFile();
292 
293  if(fDebug)
294  {
295  G4cout << "================ ROOT FILES HAVE BEEN WRITTEN"
296  << G4endl;
297  }
298 }
299 
300 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
301 
302 void RunAction::PrintRunInfo(const G4Run* run)
303 {
304  G4cout << "================ Run is = "
305  << run->GetRunID() << G4endl;
306  G4cout << "================ Run type is = "
308  G4cout << "================ Event processed = "
310  G4cout << "================ Nevent = "
311  << run->GetNumberOfEvent() << G4endl;
312 }
313 
314 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
315 
316 void PrintNParticles(std::map<const G4ParticleDefinition*, int>& container)
317 {
318  std::map<const G4ParticleDefinition*, int>::iterator it;
319  for(it = container.begin() ;
320  it != container.end(); it ++)
321  {
322  G4cout << "N " << it->first->GetParticleName() << " : "
323  << it->second << G4endl;
324  }
325 }