Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-04 07:53:10

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 /// \file RunAction.cc
0027 /// \brief Implementation of the RunAction class
0028 
0029 // This example is provided by the Geant4-DNA collaboration
0030 // Any report or published results obtained using the Geant4-DNA software
0031 // shall cite the following Geant4-DNA collaboration publication:
0032 // Med. Phys. 37 (2010) 4692-4708
0033 // The Geant4-DNA web site is available at http://geant4-dna.org
0034 //
0035 
0036 #include "RunAction.hh"
0037 
0038 #include "CommandLineParser.hh"
0039 
0040 #include "G4AnalysisManager.hh"
0041 #include "G4Run.hh"
0042 #include "G4RunManager.hh"
0043 #include "G4Threading.hh"
0044 
0045 using namespace G4DNAPARSER;
0046 
0047 void PrintNParticles(std::map<const G4ParticleDefinition*, int>& container);
0048 
0049 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0050 
0051 RunAction::RunAction() : G4UserRunAction(), fInitialized(0), fDebug(false) {}
0052 
0053 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0054 
0055 RunAction::~RunAction() {}
0056 
0057 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0058 
0059 void RunAction::BeginOfRunAction(const G4Run* run)
0060 {
0061   // In this example, we considered that the same class was
0062   // used for both master and worker threads.
0063   // However, in case the run action is long,
0064   // for better code review, this practice is not recommanded.
0065   //
0066   // Please note, in the example provided with the Geant4 X beta version,
0067   // this RunAction class were not used by the master thread.
0068 
0069   bool sequential =
0070     (G4RunManager::GetRunManager()->GetRunManagerType() == G4RunManager::sequentialRM);
0071 
0072   if (isMaster && sequential == false)
0073   // WARNING : in sequential mode, isMaster == true
0074   {
0075     BeginMaster(run);
0076   }
0077   else
0078     BeginWorker(run);
0079 }
0080 
0081 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0082 
0083 void RunAction::EndOfRunAction(const G4Run* run)
0084 {
0085   bool sequential =
0086     (G4RunManager::GetRunManager()->GetRunManagerType() == G4RunManager::sequentialRM);
0087 
0088   if (isMaster && sequential == false) {
0089     EndMaster(run);
0090   }
0091   else {
0092     EndWorker(run);
0093   }
0094 }
0095 
0096 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0097 
0098 void RunAction::BeginMaster(const G4Run* run)
0099 {
0100   if (fDebug) {
0101     bool sequential =
0102       (G4RunManager::GetRunManager()->GetRunManagerType() == G4RunManager::sequentialRM);
0103     G4cout << "===================================" << G4endl;
0104     if (!sequential) G4cout << "================ RunAction::BeginMaster" << G4endl;
0105     PrintRunInfo(run);
0106     G4cout << "===================================" << G4endl;
0107   }
0108 }
0109 
0110 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0111 
0112 void RunAction::BeginWorker(const G4Run* run)
0113 {
0114   if (fDebug) {
0115     G4cout << "===================================" << G4endl;
0116     G4cout << "================ RunAction::BeginWorker" << G4endl;
0117     PrintRunInfo(run);
0118     G4cout << "===================================" << G4endl;
0119   }
0120   if (fInitialized == false) InitializeWorker(run);
0121 
0122   CreateNtuple();
0123 }
0124 
0125 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0126 
0127 void RunAction::EndMaster(const G4Run*) {}
0128 
0129 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0130 
0131 void RunAction::EndWorker(const G4Run* run)
0132 {
0133   if (fDebug) {
0134     PrintRunInfo(run);
0135   }
0136 
0137   G4int nofEvents = run->GetNumberOfEvent();
0138   if (nofEvents == 0) {
0139     if (fDebug) {
0140       G4cout << "================ NO EVENTS TREATED IN THIS RUN ==> Exit" << G4endl;
0141     }
0142     return;
0143   }
0144 
0145   ///////////////
0146   // Write Ntuple
0147   //
0148   WriteNtuple();
0149 }
0150 
0151 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0152 
0153 void RunAction::InitializeWorker(const G4Run*)
0154 {
0155   // Initialize worker here
0156   // example: you want to retrieve pointers to other user actions
0157   fInitialized = true;
0158 }
0159 
0160 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0161 
0162 void RunAction::CreateNtuple()
0163 {
0164   // Book histograms, ntuple
0165 
0166   // Create analysis manager
0167 
0168   CommandLineParser* parser = CommandLineParser::GetParser();
0169   Command* command(0);
0170   if ((command = parser->GetCommandIfActive("-out")) == 0) return;
0171 
0172   G4cout << "##### Create analysis manager "
0173          << "  " << this << G4endl;
0174   G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0175   analysisManager->SetDefaultFileType("root");
0176   //  if(!analysisManager->IsActive()) {return; }
0177 
0178   G4cout << "Using " << analysisManager->GetType() << " analysis manager" << G4endl;
0179 
0180   // Create directories
0181 
0182   // analysisManager->SetHistoDirectoryName("histograms");
0183   // analysisManager->SetNtupleDirectoryName("ntuple");
0184   analysisManager->SetVerboseLevel(1);
0185 
0186   // Open an output file
0187   G4String fileName;
0188   if (command->GetOption().empty() == false) {
0189     fileName = command->GetOption();
0190   }
0191   else {
0192     fileName = "wholeNuclearDNA";
0193     //   fileName = command->GetDefaultOption(); // should work as well
0194   }
0195   analysisManager->OpenFile(fileName);
0196 
0197   // Creating ntuple
0198   analysisManager->CreateNtuple("ntuple", "geom_dna");
0199   analysisManager->CreateNtupleDColumn("flagParticle");
0200   analysisManager->CreateNtupleDColumn("flagProcess");
0201   analysisManager->CreateNtupleDColumn("flagVolume");
0202   analysisManager->CreateNtupleDColumn("x");
0203   analysisManager->CreateNtupleDColumn("y");
0204   analysisManager->CreateNtupleDColumn("z");
0205   analysisManager->CreateNtupleDColumn("edep");
0206   analysisManager->CreateNtupleDColumn("stepLength");
0207 
0208   analysisManager->FinishNtuple();
0209 }
0210 
0211 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0212 
0213 void RunAction::WriteNtuple()
0214 {
0215   CommandLineParser* parser = CommandLineParser::GetParser();
0216   Command* commandLine(0);
0217   if ((commandLine = parser->GetCommandIfActive("-out")) == 0) return;
0218 
0219   // print histogram statistics
0220   //
0221   G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0222   //  if(!analysisManager->IsActive()) {return; }
0223 
0224   // save histograms
0225   //
0226   analysisManager->Write();
0227   analysisManager->CloseFile();
0228   analysisManager->Clear();
0229 }
0230 
0231 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0232 
0233 void RunAction::PrintRunInfo(const G4Run* run)
0234 {
0235   G4cout << "================ Run is = " << run->GetRunID() << G4endl;
0236   G4cout << "================ Run type is = " << G4RunManager::GetRunManager()->GetRunManagerType()
0237          << G4endl;
0238   G4cout << "================ Event processed = " << run->GetNumberOfEventToBeProcessed() << G4endl;
0239   G4cout << "================ Nevent = " << run->GetNumberOfEvent() << G4endl;
0240 }
0241 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......