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 "G4AnalysisManager.hh" >> 41 #include "G4Threading.hh" >> 42 #include "CommandLineParser.hh" >> 43 >> 44 using namespace G4DNAPARSER; >> 45 >> 46 void PrintNParticles(std::map<const G4ParticleDefinition*, int>& container); 43 47 44 //....oooOO0OOooo........oooOO0OOooo........oo 48 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 45 49 46 RunAction::RunAction() : G4UserRunAction() << 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) 47 { 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 } >> 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 >> 161 /////////////// >> 162 // Write Histo >> 163 // >> 164 WriteHistogram(); >> 165 >> 166 /////////////// >> 167 // Printouts >> 168 // >> 169 std::map<const G4ParticleDefinition*, int>& >> 170 particlesCreatedInWorld = fpTrackingAction->GetNParticlesCreatedInWorld(); >> 171 >> 172 G4cout << "Number and type of particles created outside region \"Target\" :" >> 173 << G4endl; >> 174 >> 175 PrintNParticles(particlesCreatedInWorld); >> 176 >> 177 G4cout << "_______________________" << G4endl; >> 178 >> 179 std::map<const G4ParticleDefinition*, int>& >> 180 particlesCreatedInTarget = fpTrackingAction->GetNParticlesCreatedInTarget(); >> 181 >> 182 G4cout << "Number and type of particles created in region \"Target\" :" >> 183 << G4endl; >> 184 >> 185 PrintNParticles(particlesCreatedInTarget); >> 186 } >> 187 >> 188 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... >> 189 >> 190 void RunAction::InitializeWorker(const G4Run*) >> 191 { >> 192 RunInitManager::Instance()->Initialize(); >> 193 >> 194 if (fpTrackingAction == 0) >> 195 { >> 196 fpTrackingAction = (TrackingAction*) G4RunManager::GetRunManager()-> >> 197 GetUserTrackingAction(); >> 198 >> 199 if(fpTrackingAction == 0 && isMaster == false) >> 200 { >> 201 G4ExceptionDescription exDescrption ; >> 202 exDescrption << "fpTrackingAction is a null pointer. " >> 203 "Has it been correctly initialized ?"; >> 204 G4Exception("RunAction::BeginOfRunAction", >> 205 "RunAction001",FatalException, exDescrption); >> 206 } >> 207 } >> 208 >> 209 fInitialized = true; >> 210 } >> 211 >> 212 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... >> 213 >> 214 void RunAction::CreateHistogram() >> 215 { >> 216 // Book histograms, ntuple >> 217 48 // Create analysis manager 218 // Create analysis manager 49 219 50 G4cout << "##### Create analysis manager " << 220 CommandLineParser* parser = CommandLineParser::GetParser(); 51 << " " << this << G4endl; << 221 Command* command(0); 52 auto analysisManager = G4AnalysisManager::In << 222 if((command = parser->GetCommandIfActive("-out"))==0) return; 53 223 >> 224 G4cout << "##### Create analysis manager " << " " << this << G4endl; >> 225 G4AnalysisManager* analysisManager = G4AnalysisManager::Instance(); 54 analysisManager->SetDefaultFileType("root"); 226 analysisManager->SetDefaultFileType("root"); 55 analysisManager->SetNtupleMerging(true); << 56 227 57 G4cout << "Using " << analysisManager->GetTy << 228 G4cout << "Using " << analysisManager->GetType() << >> 229 " analysis manager" << G4endl; >> 230 >> 231 // Create directories 58 232 >> 233 //analysisManager->SetHistoDirectoryName("histograms"); >> 234 //analysisManager->SetNtupleDirectoryName("ntuple"); 59 analysisManager->SetVerboseLevel(1); 235 analysisManager->SetVerboseLevel(1); 60 236 61 // Create ntuple << 237 // Open an output file >> 238 G4String fileName; >> 239 if(command->GetOption().empty() == false) >> 240 { >> 241 fileName = command->GetOption(); >> 242 } >> 243 else >> 244 { >> 245 fileName = "microdosimetry"; >> 246 } >> 247 analysisManager->OpenFile(fileName); >> 248 >> 249 // Creating ntuple 62 250 63 analysisManager->CreateNtuple("dna", "dnaphy << 251 analysisManager->CreateNtuple("microdosimetry", "physics"); 64 analysisManager->CreateNtupleDColumn("flagPa 252 analysisManager->CreateNtupleDColumn("flagParticle"); 65 analysisManager->CreateNtupleDColumn("flagPr 253 analysisManager->CreateNtupleDColumn("flagProcess"); 66 analysisManager->CreateNtupleDColumn("x"); 254 analysisManager->CreateNtupleDColumn("x"); 67 analysisManager->CreateNtupleDColumn("y"); 255 analysisManager->CreateNtupleDColumn("y"); 68 analysisManager->CreateNtupleDColumn("z"); 256 analysisManager->CreateNtupleDColumn("z"); 69 analysisManager->CreateNtupleDColumn("totalE 257 analysisManager->CreateNtupleDColumn("totalEnergyDeposit"); 70 analysisManager->CreateNtupleDColumn("stepLe 258 analysisManager->CreateNtupleDColumn("stepLength"); 71 analysisManager->CreateNtupleDColumn("kineti 259 analysisManager->CreateNtupleDColumn("kineticEnergyDifference"); 72 analysisManager->CreateNtupleDColumn("kineti 260 analysisManager->CreateNtupleDColumn("kineticEnergy"); 73 analysisManager->CreateNtupleDColumn("cosThe 261 analysisManager->CreateNtupleDColumn("cosTheta"); 74 analysisManager->CreateNtupleIColumn("eventI 262 analysisManager->CreateNtupleIColumn("eventID"); 75 analysisManager->CreateNtupleIColumn("trackI 263 analysisManager->CreateNtupleIColumn("trackID"); 76 analysisManager->CreateNtupleIColumn("parent 264 analysisManager->CreateNtupleIColumn("parentID"); 77 analysisManager->CreateNtupleIColumn("stepID 265 analysisManager->CreateNtupleIColumn("stepID"); 78 analysisManager->FinishNtuple(); 266 analysisManager->FinishNtuple(); 79 } 267 } 80 268 81 //....oooOO0OOooo........oooOO0OOooo........oo 269 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 82 270 83 RunAction::~RunAction() {} << 271 void RunAction::WriteHistogram() 84 << 85 //....oooOO0OOooo........oooOO0OOooo........oo << 86 << 87 void RunAction::BeginOfRunAction(const G4Run*) << 88 { 272 { 89 auto analysisManager = G4AnalysisManager::In << 273 CommandLineParser* parser = CommandLineParser::GetParser(); >> 274 Command* commandLine(0); >> 275 if((commandLine = parser->GetCommandIfActive("-out"))==0) return; >> 276 >> 277 // print histogram statistics >> 278 // >> 279 G4AnalysisManager* analysisManager = G4AnalysisManager::Instance(); >> 280 // if(!analysisManager->IsActive()) {return; } 90 281 91 // Open an output file << 282 // save histograms 92 G4String fileName = "dna"; << 283 // 93 analysisManager->OpenFile(fileName); << 284 analysisManager->Write(); >> 285 analysisManager->CloseFile(); >> 286 analysisManager->Clear(); >> 287 >> 288 if(fDebug) >> 289 { >> 290 G4cout << "================ ROOT FILES HAVE BEEN WRITTEN" >> 291 << G4endl; >> 292 } 94 } 293 } 95 294 96 //....oooOO0OOooo........oooOO0OOooo........oo 295 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 97 296 98 void RunAction::EndOfRunAction(const G4Run* aR << 297 void RunAction::PrintRunInfo(const G4Run* run) 99 { 298 { 100 G4int nofEvents = aRun->GetNumberOfEvent(); << 299 G4cout << "================ Run is = " 101 if (nofEvents == 0) return; << 300 << run->GetRunID() << G4endl; >> 301 G4cout << "================ Run type is = " >> 302 << G4RunManager::GetRunManager()->GetRunManagerType() << G4endl; >> 303 G4cout << "================ Event processed = " >> 304 << run->GetNumberOfEventToBeProcessed() << G4endl; >> 305 G4cout << "================ Nevent = " >> 306 << run->GetNumberOfEvent() << G4endl; >> 307 } 102 308 103 // Print histogram statistics << 309 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 104 auto analysisManager = G4AnalysisManager::In << 105 310 106 // Save histograms << 311 void PrintNParticles(std::map<const G4ParticleDefinition*, int>& container) 107 analysisManager->Write(); << 312 { 108 analysisManager->CloseFile(); << 313 std::map<const G4ParticleDefinition*, int>::iterator it; >> 314 for(it = container.begin() ; >> 315 it != container.end(); it ++) >> 316 { >> 317 G4cout << "N " << it->first->GetParticleName() << " : " >> 318 << it->second << G4endl; >> 319 } 109 } 320 } 110 321