Geant4 Cross Reference |
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 /// \file RunAction.cc 33 /// \brief Implementation of the RunAction class 34 35 #include "RunAction.hh" 36 37 #include "CommandLineParser.hh" 38 39 #include "G4AnalysisManager.hh" 40 #include "G4Run.hh" 41 #include "G4RunManager.hh" 42 #include "G4Threading.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(), fInitialized(0), fDebug(false) {} 51 52 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 53 54 RunAction::~RunAction() {} 55 56 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 57 58 void RunAction::BeginOfRunAction(const G4Run* run) 59 { 60 // In this example, we considered that the same class was 61 // used for both master and worker threads. 62 // However, in case the run action is long, 63 // for better code review, this practice is not recommanded. 64 // 65 // Please note, in the example provided with the Geant4 X beta version, 66 // this RunAction class were not used by the master thread. 67 68 bool sequential = 69 (G4RunManager::GetRunManager()->GetRunManagerType() == G4RunManager::sequentialRM); 70 71 if (isMaster && sequential == false) 72 // WARNING : in sequential mode, isMaster == true 73 { 74 BeginMaster(run); 75 } 76 else 77 BeginWorker(run); 78 } 79 80 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 81 82 void RunAction::EndOfRunAction(const G4Run* run) 83 { 84 bool sequential = 85 (G4RunManager::GetRunManager()->GetRunManagerType() == G4RunManager::sequentialRM); 86 87 if (isMaster && sequential == false) { 88 EndMaster(run); 89 } 90 else { 91 EndWorker(run); 92 } 93 } 94 95 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 96 97 void RunAction::BeginMaster(const G4Run* run) 98 { 99 if (fDebug) { 100 bool sequential = 101 (G4RunManager::GetRunManager()->GetRunManagerType() == G4RunManager::sequentialRM); 102 G4cout << "===================================" << G4endl; 103 if (!sequential) G4cout << "================ RunAction::BeginMaster" << G4endl; 104 PrintRunInfo(run); 105 G4cout << "===================================" << G4endl; 106 } 107 } 108 109 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 110 111 void RunAction::BeginWorker(const G4Run* run) 112 { 113 if (fDebug) { 114 G4cout << "===================================" << G4endl; 115 G4cout << "================ RunAction::BeginWorker" << G4endl; 116 PrintRunInfo(run); 117 G4cout << "===================================" << G4endl; 118 } 119 if (fInitialized == false) InitializeWorker(run); 120 121 CreateNtuple(); 122 } 123 124 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 125 126 void RunAction::EndMaster(const G4Run*) {} 127 128 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 129 130 void RunAction::EndWorker(const G4Run* run) 131 { 132 if (fDebug) { 133 PrintRunInfo(run); 134 } 135 136 G4int nofEvents = run->GetNumberOfEvent(); 137 if (nofEvents == 0) { 138 if (fDebug) { 139 G4cout << "================ NO EVENTS TREATED IN THIS RUN ==> Exit" << G4endl; 140 } 141 return; 142 } 143 144 /////////////// 145 // Write Ntuple 146 // 147 WriteNtuple(); 148 } 149 150 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 151 152 void RunAction::InitializeWorker(const G4Run*) 153 { 154 // Initialize worker here 155 // example: you want to retrieve pointers to other user actions 156 fInitialized = true; 157 } 158 159 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 160 161 void RunAction::CreateNtuple() 162 { 163 // Book histograms, ntuple 164 165 // Create analysis manager 166 167 CommandLineParser* parser = CommandLineParser::GetParser(); 168 Command* command(0); 169 if ((command = parser->GetCommandIfActive("-out")) == 0) return; 170 171 G4cout << "##### Create analysis manager " 172 << " " << this << G4endl; 173 G4AnalysisManager* analysisManager = G4AnalysisManager::Instance(); 174 analysisManager->SetDefaultFileType("root"); 175 // if(!analysisManager->IsActive()) {return; } 176 177 G4cout << "Using " << analysisManager->GetType() << " analysis manager" << G4endl; 178 179 // Create directories 180 181 // analysisManager->SetHistoDirectoryName("histograms"); 182 // analysisManager->SetNtupleDirectoryName("ntuple"); 183 analysisManager->SetVerboseLevel(1); 184 185 // Open an output file 186 G4String fileName; 187 if (command->GetOption().empty() == false) { 188 fileName = command->GetOption(); 189 } 190 else { 191 fileName = "wholeNuclearDNA"; 192 // fileName = command->GetDefaultOption(); // should work as well 193 } 194 analysisManager->OpenFile(fileName); 195 196 // Creating ntuple 197 analysisManager->CreateNtuple("ntuple", "geom_dna"); 198 analysisManager->CreateNtupleDColumn("flagParticle"); 199 analysisManager->CreateNtupleDColumn("flagProcess"); 200 analysisManager->CreateNtupleDColumn("flagVolume"); 201 analysisManager->CreateNtupleDColumn("x"); 202 analysisManager->CreateNtupleDColumn("y"); 203 analysisManager->CreateNtupleDColumn("z"); 204 analysisManager->CreateNtupleDColumn("edep"); 205 analysisManager->CreateNtupleDColumn("stepLength"); 206 207 analysisManager->FinishNtuple(); 208 } 209 210 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 211 212 void RunAction::WriteNtuple() 213 { 214 CommandLineParser* parser = CommandLineParser::GetParser(); 215 Command* commandLine(0); 216 if ((commandLine = parser->GetCommandIfActive("-out")) == 0) return; 217 218 // print histogram statistics 219 // 220 G4AnalysisManager* analysisManager = G4AnalysisManager::Instance(); 221 // if(!analysisManager->IsActive()) {return; } 222 223 // save histograms 224 // 225 analysisManager->Write(); 226 analysisManager->CloseFile(); 227 analysisManager->Clear(); 228 } 229 230 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 231 232 void RunAction::PrintRunInfo(const G4Run* run) 233 { 234 G4cout << "================ Run is = " << run->GetRunID() << G4endl; 235 G4cout << "================ Run type is = " << G4RunManager::GetRunManager()->GetRunManagerType() 236 << G4endl; 237 G4cout << "================ Event processed = " << run->GetNumberOfEventToBeProcessed() << G4endl; 238 G4cout << "================ Nevent = " << run->GetNumberOfEvent() << G4endl; 239 } 240 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 241