File indexing completed on 2026-09-18 08:32:53
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 "Par03EventAction.hh"
0030
0031 #include "Par03DetectorConstruction.hh"
0032 #include "Par03Hit.hh"
0033
0034 #include "G4AnalysisManager.hh"
0035 #include "G4Event.hh"
0036 #include "G4EventManager.hh"
0037 #include "G4HCofThisEvent.hh"
0038 #include "G4SDManager.hh"
0039
0040 Par03EventAction::Par03EventAction(Par03DetectorConstruction* aDetector)
0041 : G4UserEventAction(), fHitCollectionID(-1), fTimer(), fDetector(aDetector)
0042 {}
0043
0044
0045
0046 Par03EventAction::~Par03EventAction() = default;
0047
0048
0049
0050 void Par03EventAction::BeginOfEventAction(const G4Event*)
0051 {
0052 fTimer.Start();
0053 }
0054
0055
0056
0057 void Par03EventAction::EndOfEventAction(const G4Event* aEvent)
0058 {
0059 fTimer.Stop();
0060
0061 if (fHitCollectionID == -1) {
0062 fHitCollectionID = G4SDManager::GetSDMpointer()->GetCollectionID("hits");
0063 }
0064
0065 auto hitsCollection =
0066 static_cast<Par03HitsCollection*>(aEvent->GetHCofThisEvent()->GetHC(fHitCollectionID));
0067
0068 if (hitsCollection == nullptr) {
0069 G4ExceptionDescription msg;
0070 msg << "Cannot access hitsCollection ID " << fHitCollectionID;
0071 G4Exception("Par03EventAction::GetHitsCollection()", "MyCode0001", FatalException, msg);
0072 }
0073
0074 auto analysisManager = G4AnalysisManager::Instance();
0075
0076 if (fCellSizeZ == 0) {
0077 fCellSizeZ = fDetector->GetLength() / fDetector->GetNbOfLayers();
0078 fCellSizeRho = fDetector->GetRadius() / fDetector->GetNbOfRhoCells();
0079 }
0080
0081
0082
0083 auto primaryVertex =
0084 G4EventManager::GetEventManager()->GetConstCurrentEvent()->GetPrimaryVertex();
0085 auto primaryParticle = primaryVertex->GetPrimary(0);
0086 G4double primaryEnergy = primaryParticle->GetTotalEnergy();
0087
0088
0089 auto primaryDirection = primaryParticle->GetMomentumDirection();
0090 auto primaryEntrance =
0091 primaryVertex->GetPosition() - primaryVertex->GetPosition().z() * primaryDirection;
0092 G4double cosDirection = std::cos(primaryDirection.theta());
0093 G4double sinDirection = std::sin(primaryDirection.theta());
0094
0095
0096 Par03Hit* hit = nullptr;
0097 G4double hitEn = 0;
0098 G4double totalEnergy = 0;
0099 G4int hitZ = -1;
0100 G4int hitRho = -1;
0101 G4int hitType = -1;
0102 G4double tDistance = 0., rDistance = 0.;
0103 G4double tFirstMoment = 0., tSecondMoment = 0.;
0104 G4double rFirstMoment = 0., rSecondMoment = 0.;
0105 for (size_t iHit = 0; iHit < hitsCollection->entries(); iHit++) {
0106 hit = static_cast<Par03Hit*>(hitsCollection->GetHit(iHit));
0107 hitZ = hit->GetZid();
0108 hitRho = hit->GetRhoId();
0109 hitEn = hit->GetEdep();
0110 hitType = hit->GetType();
0111 if (hitEn > 0) {
0112 totalEnergy += hitEn;
0113 tDistance = hitZ * fCellSizeZ * cosDirection
0114 + (hitRho * fCellSizeRho - primaryEntrance.perp()) * sinDirection;
0115 rDistance = hitZ * fCellSizeZ * (-sinDirection)
0116 + (hitRho * fCellSizeRho - primaryEntrance.perp()) * cosDirection;
0117 tFirstMoment += hitEn * tDistance;
0118 rFirstMoment += hitEn * rDistance;
0119 analysisManager->FillH1(4, tDistance, hitEn);
0120 analysisManager->FillH1(5, rDistance, hitEn);
0121 analysisManager->FillH1(10, hitType);
0122 }
0123 }
0124 tFirstMoment /= totalEnergy;
0125 rFirstMoment /= totalEnergy;
0126 analysisManager->FillH1(0, primaryEnergy / GeV);
0127 analysisManager->FillH1(1, totalEnergy / GeV);
0128 analysisManager->FillH1(2, totalEnergy / primaryEnergy);
0129 analysisManager->FillH1(3, fTimer.GetRealElapsed());
0130 analysisManager->FillH1(6, tFirstMoment);
0131 analysisManager->FillH1(7, rFirstMoment);
0132
0133
0134 for (size_t iHit = 0; iHit < hitsCollection->entries(); iHit++) {
0135 hit = static_cast<Par03Hit*>(hitsCollection->GetHit(iHit));
0136 hitEn = hit->GetEdep();
0137 hitZ = hit->GetZid();
0138 hitRho = hit->GetRhoId();
0139 if (hitEn > 0) {
0140 tDistance = hitZ * fCellSizeZ * cosDirection
0141 + (hitRho * fCellSizeRho - primaryEntrance.r()) * sinDirection;
0142 rDistance = hitZ * fCellSizeZ * (-sinDirection)
0143 + (hitRho * fCellSizeRho - primaryEntrance.r()) * cosDirection;
0144 tSecondMoment += hitEn * std::pow(tDistance - tFirstMoment, 2);
0145 rSecondMoment += hitEn * std::pow(rDistance - rFirstMoment, 2);
0146 }
0147 }
0148 tSecondMoment /= totalEnergy;
0149 rSecondMoment /= totalEnergy;
0150 analysisManager->FillH1(8, tSecondMoment);
0151 analysisManager->FillH1(9, rSecondMoment);
0152 }