Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-23 09:22:12

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