Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-15 08:28:06

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 #include "RunAction.hh"
0030 
0031 #include "G4AnalysisManager.hh"
0032 #include "G4GenericMessenger.hh"
0033 #include "G4Run.hh"
0034 #include "G4RunManager.hh"
0035 #include "G4SystemOfUnits.hh"
0036 #include "G4UnitsTable.hh"
0037 
0038 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0039 
0040 RunAction::RunAction()
0041 {
0042   // Create or get analysis manager
0043   G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0044   analysisManager->SetDefaultFileType("root");
0045   analysisManager->SetVerboseLevel(1);
0046 // Only merge in MT mode to avoid warning when running in Sequential mode
0047 #ifdef G4MULTITHREADED
0048   analysisManager->SetNtupleMerging(true);
0049 #endif
0050 
0051   // Create directories
0052   analysisManager->SetHistoDirectoryName("histo");
0053   analysisManager->SetNtupleDirectoryName("ntuple");
0054 
0055   // Create histograms.
0056   // Histogram ids are generated automatically starting from 0.
0057   // The start value can be changed by:
0058   // analysisManager->SetFirstHistoId(1);
0059 
0060   // id = 0
0061   analysisManager->CreateH1("EAbs", "Edep in absorber (MeV)", 100, 0., 800 * MeV);
0062   // id = 1
0063   analysisManager->CreateH1("EGap", "Edep in gap (MeV)", 100, 0., 100 * MeV);
0064   // id = 2
0065   analysisManager->CreateH1("LAbs", "trackL in absorber (mm)", 100, 0., 1 * m);
0066   // id = 3
0067   analysisManager->CreateH1("LGap", "trackL in gap (mm)", 100, 0., 50 * cm);
0068 
0069   // Create ntuples.
0070   // Ntuples ids are generated automatically starting from 0.
0071   // The start value can be changed by:
0072   // analysisManager->SetFirstMtupleId(1);
0073 
0074   // Create 1st ntuple (id = 0)
0075   analysisManager->CreateNtuple("Ntuple1", "Edep");
0076   analysisManager->CreateNtupleDColumn("Eabs");  // column Id = 0
0077   analysisManager->CreateNtupleDColumn("Egap");  // column Id = 1
0078   analysisManager->FinishNtuple();
0079 
0080   // Create 2nd ntuple (id = 1)
0081   //
0082   analysisManager->CreateNtuple("Ntuple2", "TrackL");
0083   analysisManager->CreateNtupleDColumn("Labs");  // column Id = 0
0084   analysisManager->CreateNtupleDColumn("Lgap");  // column Id = 1
0085   analysisManager->FinishNtuple();
0086 
0087   DefineCommands();
0088 }
0089 
0090 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0091 
0092 RunAction::~RunAction() = default;
0093 
0094 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0095 
0096 void RunAction::BeginOfRunAction(const G4Run* run)
0097 {
0098   G4cout << "### Run " << run->GetRunID() << " start." << G4endl;
0099 }
0100 
0101 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0102 
0103 void RunAction::EndOfRunAction(const G4Run* /*run*/) {}
0104 
0105 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0106 
0107 void RunAction::PrintStatistic()
0108 {
0109   G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0110 
0111   G4cout << "\n ----> print histograms statistic \n" << G4endl;
0112   for (G4int i = 0; i < analysisManager->GetNofH1s(); ++i) {
0113     auto h1 = analysisManager->GetH1(i);
0114     // skip if histogram was deleted
0115     if (h1 == nullptr) continue;
0116 
0117     G4String name = analysisManager->GetH1Name(i);
0118     G4String unitCategory;
0119     if (name[0U] == 'E') {
0120       unitCategory = "Energy";
0121     }
0122     if (name[0U] == 'L') {
0123       unitCategory = "Length";
0124     }
0125     // we use an explicit unsigned int type for operator [] argument
0126     // to avoid problems with windows compiler
0127 
0128     G4cout << name << ": mean = " << G4BestUnit(h1->mean(), unitCategory)
0129            << " rms = " << G4BestUnit(h1->rms(), unitCategory) << G4endl;
0130   }
0131 }
0132 
0133 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0134 
0135 void RunAction::DefineCommands()
0136 {
0137   // Define /AnaEx03/runAction command directory using generic messenger class
0138   fMessenger = new G4GenericMessenger(this, "/AnaEx03/runAction/", "Run action commands");
0139 
0140   // printStatistic command
0141   auto& printStatisticCmd = fMessenger->DeclareMethod("printStatistic", &RunAction::PrintStatistic,
0142                                                       "Print statistic at the end of Run.");
0143   printStatisticCmd.SetToBeBroadcasted(false);
0144 }
0145 
0146 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......