File indexing completed on 2025-02-23 09:20:23
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 #include "RunAction.hh"
0030 #include "G4Run.hh"
0031 #include "TrackingAction.hh"
0032 #include "G4ParticleDefinition.hh"
0033 #include "G4RunManager.hh"
0034 #include "G4AnalysisManager.hh"
0035 #include "G4Threading.hh"
0036
0037 void PrintNParticles(std::map<const G4ParticleDefinition*, int>& container);
0038
0039
0040
0041 RunAction::RunAction()
0042 {
0043 fFileName = "microelectronics";
0044 fpTrackingAction = 0;
0045 fInitialized = 0;
0046 fDebug = false;
0047 }
0048
0049
0050
0051 RunAction::~RunAction()
0052 {}
0053
0054
0055
0056 void RunAction::BeginOfRunAction(const G4Run* run)
0057 {
0058
0059
0060
0061
0062
0063
0064 if(isMaster)
0065 BeginMaster(run);
0066 else
0067 BeginWorker(run);
0068 }
0069
0070
0071
0072 void RunAction::EndOfRunAction(const G4Run* run)
0073 {
0074 if(isMaster)
0075 EndMaster(run);
0076 else
0077 EndWorker(run);
0078 }
0079
0080
0081
0082
0083 void RunAction::BeginMaster(const G4Run* run)
0084 {
0085 bool sequential = (G4RunManager::GetRunManager()->GetRunManagerType() == G4RunManager::sequentialRM);
0086
0087 if(fDebug)
0088 {
0089 G4cout << "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°" << G4endl;
0090 if(!sequential)
0091 G4cout << "°°°°°°°°°°°°°°°° RunAction::BeginMaster" << G4endl;
0092 PrintRunInfo(run);
0093 G4cout << "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°" << G4endl;
0094 }
0095
0096 if(sequential)
0097 {
0098 if(!fInitialized)
0099 InitializeWorker(run);
0100
0101
0102 CreateHistogram();
0103 }
0104 }
0105
0106
0107
0108 void RunAction::BeginWorker(const G4Run* run)
0109 {
0110 if(fDebug)
0111 {
0112 G4cout << "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°" << G4endl;
0113 G4cout << "°°°°°°°°°°°°°°°° RunAction::BeginWorker" << G4endl;
0114 PrintRunInfo(run);
0115 G4cout << "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°" << G4endl;
0116 }
0117 if(!fInitialized)
0118 InitializeWorker(run);
0119
0120 CreateHistogram();
0121 }
0122
0123
0124
0125 void RunAction::EndMaster(const G4Run* run)
0126 {
0127 bool sequential = (G4RunManager::GetRunManager()->GetRunManagerType()
0128 == G4RunManager::sequentialRM);
0129 if(sequential)
0130 EndWorker(run);
0131 }
0132
0133
0134
0135 void RunAction::EndWorker(const G4Run* run)
0136 {
0137 if(fDebug)
0138 {
0139 PrintRunInfo(run);
0140 }
0141
0142 G4int nofEvents = run->GetNumberOfEvent();
0143 if ( nofEvents == 0 )
0144 {
0145 if(fDebug)
0146 {
0147 G4cout << "°°°°°°°°°°°°°°°° NO EVENTS TREATED IN THIS RUN ==> LEAVING RunAction::EndOfRunAction "<< G4endl;
0148 }
0149 return;
0150 }
0151
0152
0153
0154
0155 WriteHistogram();
0156
0157
0158
0159
0160 G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0161 analysisManager->Clear();
0162
0163
0164
0165
0166 std::map<const G4ParticleDefinition*, int>&
0167 particlesCreatedInWorld = fpTrackingAction->GetNParticlesCreatedInWorld();
0168
0169 G4cout << "Number and type of particles created outside region \"Target\" :" << G4endl;
0170
0171 PrintNParticles(particlesCreatedInWorld);
0172
0173 G4cout << "_______________________" << G4endl;
0174
0175 std::map<const G4ParticleDefinition*, int>&
0176 particlesCreatedInTarget = fpTrackingAction->GetNParticlesCreatedInTarget();
0177
0178 G4cout << "Number and type of particles created in region \"Target\" :" << G4endl;
0179
0180 PrintNParticles(particlesCreatedInTarget);
0181
0182 }
0183
0184
0185
0186 void RunAction::InitializeWorker(const G4Run*)
0187 {
0188 if (fpTrackingAction == 0)
0189 {
0190 fpTrackingAction = (TrackingAction*) G4RunManager::GetRunManager()->GetUserTrackingAction();
0191
0192 if(fpTrackingAction == 0 && isMaster == false)
0193 {
0194 G4ExceptionDescription exDescrption ;
0195 exDescrption << "fpTrackingAction is a null pointer. Has it been correctly initialized ?";
0196 G4Exception("RunAction::BeginOfRunAction","RunAction001",FatalException, exDescrption);
0197 }
0198 }
0199
0200 fInitialized = true;
0201 }
0202
0203
0204
0205 void RunAction::CreateHistogram()
0206 {
0207
0208
0209
0210
0211
0212
0213 G4cout << "##### Create analysis manager " << " " << this << G4endl;
0214 G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0215 analysisManager->SetDefaultFileType("root");
0216
0217 G4cout << "Using " << analysisManager->GetType() << " analysis manager" << G4endl;
0218
0219
0220
0221
0222
0223 analysisManager->SetVerboseLevel(1);
0224
0225
0226
0227 analysisManager->OpenFile(fFileName);
0228
0229
0230
0231 analysisManager->CreateNtuple("microelectronics", "physics");
0232 analysisManager->CreateNtupleDColumn("flagParticle");
0233 analysisManager->CreateNtupleDColumn("flagProcess");
0234 analysisManager->CreateNtupleDColumn("x");
0235 analysisManager->CreateNtupleDColumn("y");
0236 analysisManager->CreateNtupleDColumn("z");
0237 analysisManager->CreateNtupleDColumn("totalEnergyDeposit");
0238 analysisManager->CreateNtupleDColumn("stepLength");
0239 analysisManager->CreateNtupleDColumn("kineticEnergyDifference");
0240 analysisManager->FinishNtuple();
0241 }
0242
0243
0244
0245 void RunAction::WriteHistogram()
0246 {
0247
0248
0249 G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0250
0251
0252
0253 analysisManager->Write();
0254 analysisManager->CloseFile();
0255 }
0256
0257
0258
0259 void RunAction::PrintRunInfo(const G4Run* run)
0260 {
0261 G4cout << "°°°°°°°°°°°°°°°° Run is = " << run->GetRunID() << G4endl;
0262 G4cout << "°°°°°°°°°°°°°°°° Run type is = " << G4RunManager::GetRunManager()->GetRunManagerType() << G4endl;
0263 G4cout << "°°°°°°°°°°°°°°°° Event processed = " << run->GetNumberOfEventToBeProcessed() << G4endl;
0264 G4cout << "°°°°°°°°°°°°°°°° N° Event = " << run->GetNumberOfEvent() << G4endl;
0265 }
0266
0267
0268 void PrintNParticles(std::map<const G4ParticleDefinition*, int>& container)
0269 {
0270 std::map<const G4ParticleDefinition*, int>::iterator it;
0271 for(it = container.begin() ;
0272 it != container.end(); it ++)
0273 {
0274 G4cout << "N " << it->first->GetParticleName() << " : " << it->second << G4endl;
0275 }
0276 }
0277
0278