#include "SensitiveDetector.hh" //#include "HitCollection.hh" // Ensure this defines HitCollection #include "PhotonHit.hh" // Ensure this defines PhotonHit correctly #include "G4ThreeVector.hh" #include "G4SDManager.hh" #include "G4ios.hh" #include "G4TouchableHistory.hh" #include "G4AnalysisManager.hh" #include "G4RunManager.hh" #include "G4UnitsTable.hh" #include "G4Step.hh" #include "G4HCofThisEvent.hh" #include "G4Tubs.hh" #include "G4String.hh" #include "G4OpticalPhoton.hh" #include "G4OpBoundaryProcess.hh" #include "G4GeometryTolerance.hh" #include #include "SensitiveDetector.hh" #include "G4OpticalPhoton.hh" #include "G4Step.hh" #include "G4SDManager.hh" #include "PhotonHitCollection.hh" // Constructor SensitiveDetector::SensitiveDetector(const G4String &SDname, const G4String& HitCollectionName, const G4String& PhotonHitCollectionName) : G4VSensitiveDetector(SDname), fHitCollection(nullptr), fE(0.), fL(0.), fP(0), fPhotonHitCollection(nullptr), fPhL(0.), fPhID(0) { collectionName.insert(HitCollectionName); collectionName.insert(PhotonHitCollectionName); } // Destructor SensitiveDetector::~SensitiveDetector() {} // Initialize method void SensitiveDetector::Initialize(G4HCofThisEvent *HCE) { fHitCollection = new PhotonHitCollection(GetName(), collectionName[0]); static G4int HCID = G4SDManager::GetSDMpointer()->GetCollectionID(collectionName[0]); HCE->AddHitsCollection(HCID, fHitCollection); fPhotonHitCollection = new PhotonHitCollection(GetName(), collectionName[1]); static G4int PHCID = G4SDManager::GetSDMpointer()->GetCollectionID(collectionName[1]); HCE->AddHitsCollection(PHCID, fPhotonHitCollection); } // ProcessHits method G4bool SensitiveDetector::ProcessHits(G4Step *aStep, G4TouchableHistory *) { G4double eDep = aStep->GetTotalEnergyDeposit(); G4double deltaL = aStep->GetStepLength(); AddEdep(eDep); AddTrackLength(deltaL); G4String thisParticle = aStep->GetTrack()->GetParticleDefinition()->GetParticleName(); G4String thisVolume = aStep->GetTrack()->GetVolume()->GetName(); G4int pDetected = 0; if (thisParticle == "opticalphoton") { G4String procName = aStep->GetPostStepPoint()->GetProcessDefinedStep()->GetProcessName(); G4bool checkAbsorption = procName.find("Absorption") != std::string::npos; if (checkAbsorption && thisVolume.find("SiPM") != std::string::npos) pDetected += 1; } SetCounterStatus(pDetected); if (aStep->GetPostStepPoint()->GetStepStatus() != fWorldBoundary && IsAnOpticalPhoton(aStep)) { processPhotonHit(aStep); } return true; } // Define IsAnOpticalPhoton G4bool SensitiveDetector::IsAnOpticalPhoton(G4Step* aStep) const { const G4ParticleDefinition* particleDef = aStep->GetTrack()->GetParticleDefinition(); return (particleDef == G4OpticalPhoton::OpticalPhotonDefinition()); } void SensitiveDetector::processPhotonHit(G4Step *aStep) { G4String procName = aStep->GetPostStepPoint()->GetProcessDefinedStep()->GetProcessName(); G4String thisVolume = aStep->GetPostStepPoint()->GetPhysicalVolume()->GetName(); const G4VTouchable* Touchable = aStep->GetPostStepPoint()->GetTouchable(); G4bool checkAbsorption = procName.find("Absorption") != std::string::npos; G4int vol_id = -1; G4int dec_id = -1; if (checkAbsorption) { // Define constants locally const G4double h = CLHEP::h_Planck / (CLHEP::eV * CLHEP::ns); const G4double c = CLHEP::c_light / (CLHEP::nm / CLHEP::ns); G4double e0 = aStep->GetPostStepPoint()->GetKineticEnergy(); G4double e1 = e0 / CLHEP::eV; G4double wl = h * c / e1; G4double localTime = aStep->GetTrack()->GetLocalTime() / CLHEP::ns; G4double globalTime = aStep->GetTrack()->GetGlobalTime() / CLHEP::ns; if (thisVolume.find("Scintillator") != std::string::npos) vol_id = 0; //else if (thisVolume.find("Wrapping") != std::string::npos) // vol_id = 1; else if (thisVolume.find("SiPM") != std::string::npos) { vol_id = 2; dec_id = Touchable->GetCopyNumber(); } PhotonHit *aPhotonHit = new PhotonHit(); aPhotonHit->SetWavelength(wl); aPhotonHit->SetVolumeID(vol_id); aPhotonHit->SetDetectorID(dec_id); aPhotonHit->SetGlobalTime(globalTime); aPhotonHit->SetLocalTime(localTime); fPhotonHitCollection->insert(aPhotonHit); } } // EndOfEvent method void SensitiveDetector::EndOfEvent(G4HCofThisEvent*) { G4double TotE = GetTotalE(); G4double TotL = GetTotalTrackLength(); // Additional end-of-event processing }