#include "MySensitiveDetector.hh" #include "G4SDManager.hh" #include "G4SystemOfUnits.hh" #include "MyHit.hh" MySensitiveDetector :: MySensitiveDetector(G4String name): G4VSensitiveDetector(name) { collectionName.insert("MyHitsCollection"); //sdName = name; //hitID = -1; } //MySensitiveDetector:: ~MySensitiveDetector() {} void MySensitiveDetector::Initialize(G4HCofThisEvent* hce) { fhitsCollection = new MyHitsCollection(SensitiveDetectorName, collectionName[0]); if (fhitID < 0) fhitID = G4SDManager::GetSDMpointer()->GetCollectionID(fhitsCollection); hce->AddHitsCollection(fhitID, fhitsCollection); } G4bool MySensitiveDetector :: ProcessHits(G4Step *Step, G4TouchableHistory*) { G4Track *track = Step->GetTrack(); G4StepPoint *preStepPoint = Step->GetPreStepPoint(); G4StepPoint *postStepPoint = Step->GetPostStepPoint(); const G4VTouchable *touchable = Step->GetPreStepPoint()->GetTouchable(); G4int copyNo = touchable->GetCopyNumber(); auto hitTime = preStepPoint->GetGlobalTime(); G4double edep = Step->GetTotalEnergyDeposit(); if (edep == 0.0) return true; G4TouchableHistory* newtouchable = (G4TouchableHistory*)(Step->GetPreStepPoint()->GetTouchable()); // Get the cell ID G4int cell = newtouchable->GetReplicaNumber(0); G4int slice = newtouchable->GetReplicaNumber(1); G4int layer = newtouchable->GetReplicaNumber(2); G4int cellID = ((layer * 100 + slice) * 100 + cell); G4double time = Step->GetPreStepPoint()->GetGlobalTime(); // Search for a hit with the same layer number G4int nHits = fhitsCollection->GetSize(); MyHit* hit(nullptr); for (G4int i = 0; i < nHits; ++i) { MyHit* tempHit = (*fhitsCollection)[i]; if ((tempHit->GetCellID() == cellID) && (std::abs(time - tempHit->GetTime()) <= 1.0 *ns)) { hit = tempHit; break; } } // If hit with the same cellID and within 1 ns not found, create a new hit if (!hit) { hit = new MyHit(); hit->SetCellID(cellID); hit->SetTime(time); hit->SetEdep(edep); fhitsCollection->insert(hit); } else { hit->AddEdep(edep); } return true; } //void MySensitiveDetector::EndOfEvent(G4HCofThisEvent*) {