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