File indexing completed on 2026-09-08 08:29:43
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 "Par04SensitiveDetector.hh"
0030
0031 #include "Par04EventInformation.hh" // for Par04EventInformation
0032 #include "Par04Hit.hh" // for Par04Hit, Par04HitsCollection
0033
0034 #include "G4Event.hh" // for G4Event
0035 #include "G4EventManager.hh" // for G4EventManager
0036 #include "G4HCofThisEvent.hh" // for G4HCofThisEvent
0037 #include "G4SDManager.hh" // for G4SDManager
0038 #include "G4Step.hh" // for G4Step
0039 #include "G4Track.hh" // for G4Track
0040
0041 #include <CLHEP/Vector/Rotation.h> // for HepRotation
0042 #include <CLHEP/Vector/ThreeVector.h> // for Hep3Vector
0043 #include <G4CollectionNameVector.hh> // for G4CollectionNameVector
0044 #include <G4FastHit.hh> // for G4FastHit
0045 #include <G4FastTrack.hh> // for G4FastTrack
0046 #include <G4RotationMatrix.hh> // for G4RotationMatrix
0047 #include <G4StepPoint.hh> // for G4StepPoint
0048 #include <G4THitsCollection.hh> // for G4THitsCollection
0049 #include <G4ThreeVector.hh> // for G4ThreeVector
0050 #include <G4VSensitiveDetector.hh> // for G4VSensitiveDetector
0051 #include <G4VUserEventInformation.hh> // for G4VUserEventInformation
0052 #include <cmath> // for floor
0053 #include <cstddef> // for size_t
0054 #include <vector> // for vector
0055
0056
0057
0058 Par04SensitiveDetector::Par04SensitiveDetector(G4String aName) : G4VSensitiveDetector(aName)
0059 {
0060 collectionName.insert("hits");
0061 }
0062
0063
0064 Par04SensitiveDetector::Par04SensitiveDetector(G4String aName, G4ThreeVector aNb,
0065 G4ThreeVector aSize)
0066 : G4VSensitiveDetector(aName), fMeshNbOfCells(aNb), fMeshSizeOfCells(aSize)
0067 {
0068 collectionName.insert("hits");
0069 }
0070
0071
0072
0073 Par04SensitiveDetector::~Par04SensitiveDetector() = default;
0074
0075
0076
0077 void Par04SensitiveDetector::Initialize(G4HCofThisEvent* aHCE)
0078 {
0079 fHitsCollection = new Par04HitsCollection(SensitiveDetectorName, collectionName[0]);
0080 if (fHitCollectionID < 0) {
0081 fHitCollectionID = G4SDManager::GetSDMpointer()->GetCollectionID(fHitsCollection);
0082 }
0083 aHCE->AddHitsCollection(fHitCollectionID, fHitsCollection);
0084
0085
0086 fEntrancePosition.set(-1, -1, -1);
0087 fEntranceDirection.set(-1, -1, -1);
0088 }
0089
0090
0091
0092 G4bool Par04SensitiveDetector::ProcessHits(G4Step* aStep, G4TouchableHistory*)
0093 {
0094 G4double edep = aStep->GetTotalEnergyDeposit();
0095 if (edep == 0.) return true;
0096
0097 auto hit = RetrieveAndSetupHit(aStep->GetPostStepPoint()->GetPosition());
0098 if (hit == nullptr) return true;
0099
0100
0101 hit->AddEdep(edep);
0102
0103 hit->AddNdep(1);
0104
0105
0106
0107 if (hit->GetTime() == -1 || hit->GetTime() > aStep->GetTrack()->GetGlobalTime())
0108 hit->SetTime(aStep->GetTrack()->GetGlobalTime());
0109
0110
0111
0112 if (hit->GetType() != 1) hit->SetType(0);
0113
0114 return true;
0115 }
0116
0117
0118
0119 G4bool Par04SensitiveDetector::ProcessHits(const G4FastHit* aHit, const G4FastTrack* aTrack,
0120 G4TouchableHistory*)
0121 {
0122 G4double edep = aHit->GetEnergy();
0123 if (edep == 0.) return true;
0124
0125 auto hit = RetrieveAndSetupHit(aHit->GetPosition());
0126 if (hit == nullptr) return true;
0127
0128
0129 hit->AddEdep(edep);
0130
0131 hit->AddNdep(1);
0132
0133
0134
0135 if (hit->GetTime() == -1 || hit->GetTime() > aTrack->GetPrimaryTrack()->GetGlobalTime()) {
0136 hit->SetTime(aTrack->GetPrimaryTrack()->GetGlobalTime());
0137 }
0138
0139
0140
0141 hit->SetType(1);
0142
0143 return true;
0144 }
0145
0146
0147
0148 Par04Hit* Par04SensitiveDetector::RetrieveAndSetupHit(G4ThreeVector aGlobalPosition)
0149 {
0150 if (fEntrancePosition.x() == -1) {
0151 auto info = dynamic_cast<Par04EventInformation*>(
0152 G4EventManager::GetEventManager()->GetConstCurrentEvent()->GetUserInformation());
0153 if (info == nullptr) return nullptr;
0154 fEntrancePosition = info->GetPosition();
0155 fEntranceDirection = info->GetDirection();
0156 }
0157
0158 auto delta = aGlobalPosition - fEntrancePosition;
0159
0160
0161
0162 G4RotationMatrix rotMatrix = G4RotationMatrix();
0163 double particleTheta = fEntranceDirection.theta();
0164 double particlePhi = fEntranceDirection.phi();
0165 rotMatrix.rotateZ(-particlePhi);
0166 rotMatrix.rotateY(-particleTheta);
0167 G4RotationMatrix rotMatrixInv = CLHEP::inverseOf(rotMatrix);
0168
0169 delta = rotMatrix * delta;
0170
0171 G4int rhoNo = std::floor(delta.perp() / fMeshSizeOfCells.x());
0172 G4int phiNo = std::floor((CLHEP::pi + delta.phi()) / fMeshSizeOfCells.y());
0173 G4int zNo = std::floor(delta.z() / fMeshSizeOfCells.z());
0174
0175 std::size_t hitID =
0176 fMeshNbOfCells.x() * fMeshNbOfCells.z() * phiNo + fMeshNbOfCells.z() * rhoNo + zNo;
0177
0178 if (zNo >= fMeshNbOfCells.z() || rhoNo >= fMeshNbOfCells.x() || zNo < 0) {
0179 return nullptr;
0180 }
0181
0182 auto hit = fHitsMap[hitID].get();
0183 if (hit == nullptr) {
0184 fHitsMap[hitID] = std::unique_ptr<Par04Hit>(new Par04Hit());
0185 hit = fHitsMap[hitID].get();
0186 hit->SetPhiId(phiNo);
0187 hit->SetRhoId(rhoNo);
0188 hit->SetZid(zNo);
0189 hit->SetRot(rotMatrixInv);
0190 hit->SetPos(fEntrancePosition
0191 + rotMatrixInv * G4ThreeVector(0, 0, (zNo + 0.5) * fMeshSizeOfCells.z()));
0192 }
0193 return hit;
0194 }
0195
0196
0197
0198 void Par04SensitiveDetector::EndOfEvent(G4HCofThisEvent*)
0199 {
0200 for (const auto& hits : fHitsMap) {
0201 fHitsCollection->insert(new Par04Hit(*hits.second.get()));
0202 }
0203 fHitsMap.clear();
0204 }