Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-27 07:32:54

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 B4d::EventAction class
0028 
0029 #include "EventAction.hh"
0030 
0031 #include "G4AnalysisManager.hh"
0032 #include "G4Event.hh"
0033 #include "G4HCofThisEvent.hh"
0034 #include "G4RunManager.hh"
0035 #include "G4SDManager.hh"
0036 #include "G4THitsMap.hh"
0037 #include "G4UnitsTable.hh"
0038 
0039 #include <iomanip>
0040 
0041 namespace B4d
0042 {
0043 
0044 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0045 
0046 G4THitsMap<G4double>* EventAction::GetHitsCollection(G4int hcID, const G4Event* event) const
0047 {
0048   auto hitsCollection = static_cast<G4THitsMap<G4double>*>(event->GetHCofThisEvent()->GetHC(hcID));
0049 
0050   if (!hitsCollection) {
0051     G4ExceptionDescription msg;
0052     msg << "Cannot access hitsCollection ID " << hcID;
0053     G4Exception("EventAction::GetHitsCollection()", "MyCode0003", FatalException, msg);
0054   }
0055 
0056   return hitsCollection;
0057 }
0058 
0059 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0060 
0061 G4double EventAction::GetSum(G4THitsMap<G4double>* hitsMap) const
0062 {
0063   G4double sumValue = 0.;
0064   for (auto it : *hitsMap->GetMap()) {
0065     // hitsMap->GetMap() returns the map of std::map<G4int, G4double*>
0066     sumValue += *(it.second);
0067   }
0068   return sumValue;
0069 }
0070 
0071 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0072 
0073 void EventAction::PrintEventStatistics(G4double absoEdep, G4double absoTrackLength,
0074                                        G4double gapEdep, G4double gapTrackLength) const
0075 {
0076   // Print event statistics
0077   //
0078   G4cout << "   Absorber: total energy: " << std::setw(7) << G4BestUnit(absoEdep, "Energy")
0079          << "       total track length: " << std::setw(7) << G4BestUnit(absoTrackLength, "Length")
0080          << G4endl << "        Gap: total energy: " << std::setw(7) << G4BestUnit(gapEdep, "Energy")
0081          << "       total track length: " << std::setw(7) << G4BestUnit(gapTrackLength, "Length")
0082          << G4endl;
0083 }
0084 
0085 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0086 
0087 void EventAction::BeginOfEventAction(const G4Event* /*event*/) {}
0088 
0089 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0090 
0091 void EventAction::EndOfEventAction(const G4Event* event)
0092 {
0093   // Get hist collections IDs
0094   if (fAbsoEdepHCID == -1) {
0095     fAbsoEdepHCID = G4SDManager::GetSDMpointer()->GetCollectionID("Absorber/Edep");
0096     fGapEdepHCID = G4SDManager::GetSDMpointer()->GetCollectionID("Gap/Edep");
0097     fAbsoTrackLengthHCID = G4SDManager::GetSDMpointer()->GetCollectionID("Absorber/TrackLength");
0098     fGapTrackLengthHCID = G4SDManager::GetSDMpointer()->GetCollectionID("Gap/TrackLength");
0099   }
0100 
0101   // Get sum values from hits collections
0102   //
0103   auto absoEdep = GetSum(GetHitsCollection(fAbsoEdepHCID, event));
0104   auto gapEdep = GetSum(GetHitsCollection(fGapEdepHCID, event));
0105 
0106   auto absoTrackLength = GetSum(GetHitsCollection(fAbsoTrackLengthHCID, event));
0107   auto gapTrackLength = GetSum(GetHitsCollection(fGapTrackLengthHCID, event));
0108 
0109   // get analysis manager
0110   auto analysisManager = G4AnalysisManager::Instance();
0111 
0112   // fill histograms
0113   //
0114   analysisManager->FillH1(0, absoEdep);
0115   analysisManager->FillH1(1, gapEdep);
0116   analysisManager->FillH1(2, absoTrackLength);
0117   analysisManager->FillH1(3, gapTrackLength);
0118 
0119   // fill ntuple
0120   //
0121   analysisManager->FillNtupleDColumn(0, absoEdep);
0122   analysisManager->FillNtupleDColumn(1, gapEdep);
0123   analysisManager->FillNtupleDColumn(2, absoTrackLength);
0124   analysisManager->FillNtupleDColumn(3, gapTrackLength);
0125   analysisManager->AddNtupleRow();
0126 
0127   // print per event (modulo n)
0128   //
0129   auto eventID = event->GetEventID();
0130   auto printModulo = G4RunManager::GetRunManager()->GetPrintProgress();
0131   if ((printModulo > 0) && (eventID % printModulo == 0)) {
0132     PrintEventStatistics(absoEdep, absoTrackLength, gapEdep, gapTrackLength);
0133     G4cout << "--> End of event: " << eventID << "\n" << G4endl;
0134   }
0135 }
0136 
0137 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0138 
0139 }  // namespace B4d