Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // This example is provided by the Geant4-DNA 26 // This example is provided by the Geant4-DNA collaboration 27 // Any report or published results obtained us << 27 // Any report or published results obtained using the Geant4-DNA software 28 // shall cite the following Geant4-DNA collabo << 28 // shall cite the following Geant4-DNA collaboration publication: 29 // Med. Phys. 45, (2018) e722-e739 << 30 // Phys. Med. 31 (2015) 861-874 << 31 // Med. Phys. 37 (2010) 4692-4708 29 // Med. Phys. 37 (2010) 4692-4708 32 // Int. J. Model. Simul. Sci. Comput. 1 (2010) << 33 // The Geant4-DNA web site is available at htt 30 // The Geant4-DNA web site is available at http://geant4-dna.org 34 // 31 // 35 // << 36 /// \file RunAction.cc 32 /// \file RunAction.cc 37 /// \brief Implementation of the RunAction cla 33 /// \brief Implementation of the RunAction class 38 34 39 #include "RunAction.hh" 35 #include "RunAction.hh" 40 << 41 #include "G4AnalysisManager.hh" << 42 #include "G4Run.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 >> 50 RunAction::RunAction() : G4UserRunAction(), >> 51 fpTrackingAction(0), fInitialized(0), fDebug(false) >> 52 {} >> 53 >> 54 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... >> 55 >> 56 RunAction::~RunAction() >> 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() == >> 72 G4RunManager::sequentialRM); >> 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() == >> 88 G4RunManager::sequentialRM); >> 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() == >> 107 G4RunManager::sequentialRM); >> 108 G4cout << "===================================" << G4endl; >> 109 if(!sequential) >> 110 G4cout << "================ RunAction::BeginMaster" << G4endl; >> 111 PrintRunInfo(run); >> 112 G4cout << "===================================" << G4endl; >> 113 } >> 114 } 43 115 44 //....oooOO0OOooo........oooOO0OOooo........oo 116 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 45 117 46 RunAction::RunAction() : G4UserRunAction() << 118 void RunAction::BeginWorker(const G4Run* run) 47 { 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 >> 161 /////////////// >> 162 // Write Histo >> 163 // >> 164 WriteHistogram(); >> 165 >> 166 /////////////// >> 167 // Complete cleanup >> 168 // >> 169 delete G4AnalysisManager::Instance(); >> 170 >> 171 /////////////// >> 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 >> 195 void RunAction::InitializeWorker(const G4Run*) >> 196 { >> 197 RunInitManager::Instance()->Initialize(); >> 198 >> 199 if (fpTrackingAction == 0) >> 200 { >> 201 fpTrackingAction = (TrackingAction*) G4RunManager::GetRunManager()-> >> 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 >> 219 void RunAction::CreateHistogram() >> 220 { >> 221 // Book histograms, ntuple >> 222 48 // Create analysis manager 223 // Create analysis manager >> 224 // The choice of analysis technology is done via selection of a namespace >> 225 // in Analysis.hh 49 226 50 G4cout << "##### Create analysis manager " << 227 CommandLineParser* parser = CommandLineParser::GetParser(); 51 << " " << this << G4endl; << 228 Command* command(0); 52 auto analysisManager = G4AnalysisManager::In << 229 if((command = parser->GetCommandIfActive("-out"))==0) return; 53 230 54 analysisManager->SetDefaultFileType("root"); << 231 G4cout << "##### Create analysis manager " << " " << this << G4endl; 55 analysisManager->SetNtupleMerging(true); << 232 G4AnalysisManager* analysisManager = G4AnalysisManager::Instance(); 56 233 57 G4cout << "Using " << analysisManager->GetTy << 234 G4cout << "Using " << analysisManager->GetType() << >> 235 " analysis manager" << G4endl; 58 236 >> 237 // Create directories >> 238 >> 239 //analysisManager->SetHistoDirectoryName("histograms"); >> 240 //analysisManager->SetNtupleDirectoryName("ntuple"); 59 analysisManager->SetVerboseLevel(1); 241 analysisManager->SetVerboseLevel(1); 60 242 61 // Create ntuple << 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); 62 254 63 analysisManager->CreateNtuple("dna", "dnaphy << 255 // Creating ntuple >> 256 >> 257 analysisManager->CreateNtuple("microdosimetry", "physics"); 64 analysisManager->CreateNtupleDColumn("flagPa 258 analysisManager->CreateNtupleDColumn("flagParticle"); 65 analysisManager->CreateNtupleDColumn("flagPr 259 analysisManager->CreateNtupleDColumn("flagProcess"); 66 analysisManager->CreateNtupleDColumn("x"); 260 analysisManager->CreateNtupleDColumn("x"); 67 analysisManager->CreateNtupleDColumn("y"); 261 analysisManager->CreateNtupleDColumn("y"); 68 analysisManager->CreateNtupleDColumn("z"); 262 analysisManager->CreateNtupleDColumn("z"); 69 analysisManager->CreateNtupleDColumn("totalE 263 analysisManager->CreateNtupleDColumn("totalEnergyDeposit"); 70 analysisManager->CreateNtupleDColumn("stepLe 264 analysisManager->CreateNtupleDColumn("stepLength"); 71 analysisManager->CreateNtupleDColumn("kineti 265 analysisManager->CreateNtupleDColumn("kineticEnergyDifference"); 72 analysisManager->CreateNtupleDColumn("kineti 266 analysisManager->CreateNtupleDColumn("kineticEnergy"); 73 analysisManager->CreateNtupleDColumn("cosThe 267 analysisManager->CreateNtupleDColumn("cosTheta"); 74 analysisManager->CreateNtupleIColumn("eventI 268 analysisManager->CreateNtupleIColumn("eventID"); 75 analysisManager->CreateNtupleIColumn("trackI 269 analysisManager->CreateNtupleIColumn("trackID"); 76 analysisManager->CreateNtupleIColumn("parent 270 analysisManager->CreateNtupleIColumn("parentID"); 77 analysisManager->CreateNtupleIColumn("stepID 271 analysisManager->CreateNtupleIColumn("stepID"); 78 analysisManager->FinishNtuple(); 272 analysisManager->FinishNtuple(); 79 } 273 } 80 274 81 //....oooOO0OOooo........oooOO0OOooo........oo 275 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 82 276 83 RunAction::~RunAction() {} << 277 void RunAction::WriteHistogram() 84 << 85 //....oooOO0OOooo........oooOO0OOooo........oo << 86 << 87 void RunAction::BeginOfRunAction(const G4Run*) << 88 { 278 { 89 auto analysisManager = G4AnalysisManager::In << 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; } 90 287 91 // Open an output file << 288 // save histograms 92 G4String fileName = "dna"; << 289 // 93 analysisManager->OpenFile(fileName); << 290 analysisManager->Write(); >> 291 analysisManager->CloseFile(); >> 292 >> 293 if(fDebug) >> 294 { >> 295 G4cout << "================ ROOT FILES HAVE BEEN WRITTEN" >> 296 << G4endl; >> 297 } 94 } 298 } 95 299 96 //....oooOO0OOooo........oooOO0OOooo........oo 300 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 97 301 98 void RunAction::EndOfRunAction(const G4Run* aR << 302 void RunAction::PrintRunInfo(const G4Run* run) 99 { 303 { 100 G4int nofEvents = aRun->GetNumberOfEvent(); << 304 G4cout << "================ Run is = " 101 if (nofEvents == 0) return; << 305 << run->GetRunID() << G4endl; >> 306 G4cout << "================ Run type is = " >> 307 << G4RunManager::GetRunManager()->GetRunManagerType() << G4endl; >> 308 G4cout << "================ Event processed = " >> 309 << run->GetNumberOfEventToBeProcessed() << G4endl; >> 310 G4cout << "================ Nevent = " >> 311 << run->GetNumberOfEvent() << G4endl; >> 312 } 102 313 103 // Print histogram statistics << 314 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 104 auto analysisManager = G4AnalysisManager::In << 105 315 106 // Save histograms << 316 void PrintNParticles(std::map<const G4ParticleDefinition*, int>& container) 107 analysisManager->Write(); << 317 { 108 analysisManager->CloseFile(); << 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 } 109 } 325 } 110 326