File indexing completed on 2026-04-27 07:32:54
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
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
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
0060
0061 G4double EventAction::GetSum(G4THitsMap<G4double>* hitsMap) const
0062 {
0063 G4double sumValue = 0.;
0064 for (auto it : *hitsMap->GetMap()) {
0065
0066 sumValue += *(it.second);
0067 }
0068 return sumValue;
0069 }
0070
0071
0072
0073 void EventAction::PrintEventStatistics(G4double absoEdep, G4double absoTrackLength,
0074 G4double gapEdep, G4double gapTrackLength) const
0075 {
0076
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
0086
0087 void EventAction::BeginOfEventAction(const G4Event* ) {}
0088
0089
0090
0091 void EventAction::EndOfEventAction(const G4Event* event)
0092 {
0093
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
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
0110 auto analysisManager = G4AnalysisManager::Instance();
0111
0112
0113
0114 analysisManager->FillH1(0, absoEdep);
0115 analysisManager->FillH1(1, gapEdep);
0116 analysisManager->FillH1(2, absoTrackLength);
0117 analysisManager->FillH1(3, gapTrackLength);
0118
0119
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
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
0138
0139 }