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 // -------------------------------------------------------------------
27 // -------------------------------------------------------------------
28 
29 #include "RunAction.hh"
30 #include "G4Run.hh"
31 #include "TrackingAction.hh"
32 #include "G4ParticleDefinition.hh"
33 #include "G4RunManager.hh"
34 #include "Analysis.hh"
35 #include "G4Threading.hh"
36 
37 void PrintNParticles(std::map<const G4ParticleDefinition*, int>& container);
38 
39 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
40 
42 {
43  fFileName = "microelectronics";
44  fpTrackingAction = 0;
45  fInitialized = 0;
46  fDebug = false;
47 }
48 
49 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
50 
52 {}
53 
54 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
55 
56 void RunAction::BeginOfRunAction(const G4Run* run)
57 {
58  // In this example, we considered that the same class was
59  // used for both master and worker threads.
60  // However, in case the run action is long,
61  // for better code review, this practice is not recommanded.
62  //
63 
64  if(isMaster) // WARNING : in sequential mode, isMaster == true
65  BeginMaster(run);
66  else
67  BeginWorker(run);
68 }
69 
70 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
71 
72 void RunAction::EndOfRunAction(const G4Run* run)
73 {
74  if(isMaster)
75  EndMaster(run);
76  else
77  EndWorker(run);
78 }
79 
80 
81 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
82 
83 void RunAction::BeginMaster(const G4Run* run)
84 {
86 
87  if(fDebug)
88  {
89  G4cout << "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°" << G4endl;
90  if(!sequential)
91  G4cout << "°°°°°°°°°°°°°°°° RunAction::BeginMaster" << G4endl;
92  PrintRunInfo(run);
93  G4cout << "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°" << G4endl;
94  }
95 
96  if(sequential)
97  {
98  if(!fInitialized)
99  InitializeWorker(run);
100  // Note: fpTrackingAction could be used as a flag for initialization instead
101 
102  CreateHistogram();
103  }
104 }
105 
106 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
107 
109 {
110  if(fDebug)
111  {
112  G4cout << "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°" << G4endl;
113  G4cout << "°°°°°°°°°°°°°°°° RunAction::BeginWorker" << G4endl;
114  PrintRunInfo(run);
115  G4cout << "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°" << G4endl;
116  }
117  if(!fInitialized)
118  InitializeWorker(run);
119 
120  CreateHistogram();
121 }
122 
123 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
124 
125 void RunAction::EndMaster(const G4Run* run)
126 {
127  bool sequential = (G4RunManager::GetRunManager()->GetRunManagerType()
129  if(sequential)
130  EndWorker(run);
131 }
132 
133 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
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 
153  // Write Histo
154  //
155  WriteHistogram();
156 
158  // Complete cleanup
159  //
161 
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 
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 
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  analysisManager->OpenFile(fFileName);
226 
227  // Creating ntuple
228 
229  analysisManager->CreateNtuple("microelectronics", "physics");
230  analysisManager->CreateNtupleDColumn("flagParticle");
231  analysisManager->CreateNtupleDColumn("flagProcess");
232  analysisManager->CreateNtupleDColumn("x");
233  analysisManager->CreateNtupleDColumn("y");
234  analysisManager->CreateNtupleDColumn("z");
235  analysisManager->CreateNtupleDColumn("totalEnergyDeposit");
236  analysisManager->CreateNtupleDColumn("stepLength");
237  analysisManager->CreateNtupleDColumn("kineticEnergyDifference");
238  analysisManager->FinishNtuple();
239 }
240 
241 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
242 
244 {
245  // print histogram statistics
246  //
247  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
248 
249  // save histograms
250  //
251  analysisManager->Write();
252  analysisManager->CloseFile();
253 }
254 
255 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
256 
258 {
259  G4cout << "°°°°°°°°°°°°°°°° Run is = " << run->GetRunID() << G4endl;
260  G4cout << "°°°°°°°°°°°°°°°° Run type is = " << G4RunManager::GetRunManager()->GetRunManagerType() << G4endl;
261  G4cout << "°°°°°°°°°°°°°°°° Event processed = " << run->GetNumberOfEventToBeProcessed() << G4endl;
262  G4cout << "°°°°°°°°°°°°°°°° N° Event = " << run->GetNumberOfEvent() << G4endl;
263 }
264 
265 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
266 void PrintNParticles(std::map<const G4ParticleDefinition*, int>& container)
267 {
268  std::map<const G4ParticleDefinition*, int>::iterator it;
269  for(it = container.begin() ;
270  it != container.end(); it ++)
271  {
272  G4cout << "N " << it->first->GetParticleName() << " : " << it->second << G4endl;
273  }
274 }
275 
276 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....