Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-23 08:28:34

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 EventAction.cc
0027 /// \brief Implementation of the B4c::EventAction class
0028 
0029 #include "EventAction.hh"
0030 
0031 #include "CalorHit.hh"
0032 
0033 #include "G4AnalysisManager.hh"
0034 #include "G4Event.hh"
0035 #include "G4HCofThisEvent.hh"
0036 #include "G4RunManager.hh"
0037 #include "G4SDManager.hh"
0038 #include "G4UnitsTable.hh"
0039 
0040 #include <iomanip>
0041 
0042 namespace B4c
0043 {
0044 
0045 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0046 
0047 CalorHitsCollection* EventAction::GetHitsCollection(G4int hcID, const G4Event* event) const
0048 {
0049   auto hitsCollection = static_cast<CalorHitsCollection*>(event->GetHCofThisEvent()->GetHC(hcID));
0050 
0051   if (!hitsCollection) {
0052     G4ExceptionDescription msg;
0053     msg << "Cannot access hitsCollection ID " << hcID;
0054     G4Exception("EventAction::GetHitsCollection()", "MyCode0003", FatalException, msg);
0055   }
0056 
0057   return hitsCollection;
0058 }
0059 
0060 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0061 
0062 void EventAction::PrintEventStatistics(G4double absoEdep, G4double absoTrackLength,
0063                                        G4double gapEdep, G4double gapTrackLength) const
0064 {
0065   // print event statistics
0066   G4cout << "   Absorber: total energy: " << std::setw(7) << G4BestUnit(absoEdep, "Energy")
0067          << "       total track length: " << std::setw(7) << G4BestUnit(absoTrackLength, "Length")
0068          << G4endl << "        Gap: total energy: " << std::setw(7) << G4BestUnit(gapEdep, "Energy")
0069          << "       total track length: " << std::setw(7) << G4BestUnit(gapTrackLength, "Length")
0070          << G4endl;
0071 }
0072 
0073 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0074 
0075 void EventAction::BeginOfEventAction(const G4Event* /*event*/) {}
0076 
0077 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0078 
0079 void EventAction::EndOfEventAction(const G4Event* event)
0080 {
0081   // Get hits collections IDs (only once)
0082   if (fAbsHCID == -1) {
0083     fAbsHCID = G4SDManager::GetSDMpointer()->GetCollectionID("AbsorberHitsCollection");
0084     fGapHCID = G4SDManager::GetSDMpointer()->GetCollectionID("GapHitsCollection");
0085   }
0086 
0087   // Get hits collections
0088   auto absoHC = GetHitsCollection(fAbsHCID, event);
0089   auto gapHC = GetHitsCollection(fGapHCID, event);
0090 
0091   // Get hit with total values
0092   auto absoHit = (*absoHC)[absoHC->entries() - 1];
0093   auto gapHit = (*gapHC)[gapHC->entries() - 1];
0094 
0095   // Print per event (modulo n)
0096   //
0097   auto eventID = event->GetEventID();
0098   auto printModulo = G4RunManager::GetRunManager()->GetPrintProgress();
0099   if ((printModulo > 0) && (eventID % printModulo == 0)) {
0100     PrintEventStatistics(absoHit->GetEdep(), absoHit->GetTrackLength(), gapHit->GetEdep(),
0101                          gapHit->GetTrackLength());
0102     G4cout << "--> End of event: " << eventID << "\n" << G4endl;
0103   }
0104 
0105   // Fill histograms, ntuple
0106   //
0107 
0108   // get analysis manager
0109   auto analysisManager = G4AnalysisManager::Instance();
0110 
0111   // fill histograms
0112   analysisManager->FillH1(0, absoHit->GetEdep());
0113   analysisManager->FillH1(1, gapHit->GetEdep());
0114   analysisManager->FillH1(2, absoHit->GetTrackLength());
0115   analysisManager->FillH1(3, gapHit->GetTrackLength());
0116 
0117   // fill ntuple
0118   analysisManager->FillNtupleDColumn(0, absoHit->GetEdep());
0119   analysisManager->FillNtupleDColumn(1, gapHit->GetEdep());
0120   analysisManager->FillNtupleDColumn(2, absoHit->GetTrackLength());
0121   analysisManager->FillNtupleDColumn(3, gapHit->GetTrackLength());
0122   analysisManager->AddNtupleRow();
0123 }
0124 
0125 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0126 
0127 }  // namespace B4c