Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
examples/extended/medical/dna/microdosimetry/src/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 //
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 
235  G4cout << "Using " << analysisManager->GetType() <<
236  " analysis manager" << G4endl;
237 
238  // Create directories
239 
240  //analysisManager->SetHistoDirectoryName("histograms");
241  //analysisManager->SetNtupleDirectoryName("ntuple");
242  analysisManager->SetVerboseLevel(1);
243 
244  // Open an output file
245  G4String fileName;
246  if(command->GetOption().empty() == false)
247  {
248  fileName = command->GetOption();
249  }
250  else
251  {
252  fileName = "microdosimetry";
253  }
254  analysisManager->OpenFile(fileName);
255 
256  // Creating ntuple
257 
258  analysisManager->CreateNtuple("microdosimetry", "physics");
259  analysisManager->CreateNtupleDColumn("flagParticle");
260  analysisManager->CreateNtupleDColumn("flagProcess");
261  analysisManager->CreateNtupleDColumn("x");
262  analysisManager->CreateNtupleDColumn("y");
263  analysisManager->CreateNtupleDColumn("z");
264  analysisManager->CreateNtupleDColumn("totalEnergyDeposit");
265  analysisManager->CreateNtupleDColumn("stepLength");
266  analysisManager->CreateNtupleDColumn("kineticEnergyDifference");
267  analysisManager->CreateNtupleDColumn("kineticEnergy");
268  analysisManager->CreateNtupleDColumn("cosTheta");
269  analysisManager->CreateNtupleIColumn("eventID");
270  analysisManager->CreateNtupleIColumn("trackID");
271  analysisManager->CreateNtupleIColumn("parentID");
272  analysisManager->CreateNtupleIColumn("stepID");
273  analysisManager->FinishNtuple();
274 }
275 
276 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
277 
279 {
280  CommandLineParser* parser = CommandLineParser::GetParser();
281  Command* commandLine(0);
282  if((commandLine = parser->GetCommandIfActive("-out"))==0) return;
283 
284  // print histogram statistics
285  //
286  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
287  // if(!analysisManager->IsActive()) {return; }
288 
289  // save histograms
290  //
291  analysisManager->Write();
292  analysisManager->CloseFile();
293 
294  if(fDebug)
295  {
296  G4cout << "================ ROOT FILES HAVE BEEN WRITTEN"
297  << G4endl;
298  }
299 }
300 
301 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
302 
303 void RunAction::PrintRunInfo(const G4Run* run)
304 {
305  G4cout << "================ Run is = "
306  << run->GetRunID() << G4endl;
307  G4cout << "================ Run type is = "
309  G4cout << "================ Event processed = "
311  G4cout << "================ Nevent = "
312  << run->GetNumberOfEvent() << G4endl;
313 }
314 
315 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
316 
317 void PrintNParticles(std::map<const G4ParticleDefinition*, int>& container)
318 {
319  std::map<const G4ParticleDefinition*, int>::iterator it;
320  for(it = container.begin() ;
321  it != container.end(); it ++)
322  {
323  G4cout << "N " << it->first->GetParticleName() << " : "
324  << it->second << G4endl;
325  }
326 }
static G4RunManager * GetRunManager()
Definition: G4RunManager.cc:80
std::ostringstream G4ExceptionDescription
Definition: G4Exception.hh:45
#define G4endl
Definition: G4ios.hh:61
std::map< const G4ParticleDefinition *, int > & GetNParticlesCreatedInWorld()
std::map< const G4ParticleDefinition *, int > & GetNParticlesCreatedInTarget()
G4int GetNumberOfEvent() const
Definition: G4Run.hh:79
static RunInitManager * Instance()
G4int GetRunID() const
Definition: G4Run.hh:76
void PrintNParticles(std::map< const G4ParticleDefinition *, int > &container)
G4CsvAnalysisManager G4AnalysisManager
Definition: g4csv_defs.hh:77
CommandLineParser * parser(0)
Definition: G4Run.hh:46
G4int GetNumberOfEventToBeProcessed() const
Definition: G4Run.hh:83
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.hh:65
Command * GetCommandIfActive(const G4String &marker)
int G4int
Definition: G4Types.hh:78
G4GLOB_DLL std::ostream G4cout
RMType GetRunManagerType() const