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