9#include <G4LogicalVolume.hh>
11#include <G4StepPoint.hh>
12#include <G4SystemOfUnits.hh>
14#include <G4UserSteppingAction.hh>
15#include <G4UserTrackingAction.hh>
16#include <G4VProcess.hh>
17#include <G4VSensitiveDetector.hh>
18#include <SHiP/SimHit.hpp>
19#include <SHiP/SimParticle.hpp>
20#include <unordered_map>
26inline thread_local std::vector<SimHit>
tl_hits;
28inline thread_local std::unordered_map<int, std::size_t>
tl_track_map;
34 auto* pre = step->GetPreStepPoint();
35 auto pos = pre->GetPosition();
36 auto mom = pre->GetMomentum();
37 auto* lv = pre->GetTouchable()->GetVolume()->GetLogicalVolume();
40 auto it = detector_ids.find(lv);
41 hit.detectorId = it != detector_ids.end() ? it->second : -1;
42 hit.trackId = step->GetTrack()->GetTrackID();
43 hit.pdgCode = step->GetTrack()->GetDefinition()->GetPDGEncoding();
44 hit.position = {pos.x() / mm, pos.y() / mm, pos.z() / mm};
45 hit.momentum = {mom.x() / GeV, mom.y() / GeV, mom.z() / GeV};
46 hit.time = pre->GetGlobalTime() / ns;
53 : G4VSensitiveDetector(name), detector_ids_{std::move(detector_ids)} {}
55 G4bool
ProcessHits(G4Step* step, G4TouchableHistory*)
override {
56 double edep = step->GetTotalEnergyDeposit();
57 if (edep <= 0)
return false;
60 hit.energyDeposit = edep / GeV;
61 hit.pathLength = step->GetStepLength() / mm;
76 double ke_threshold_gev = 0.0)
77 : G4VSensitiveDetector(name),
78 detector_ids_{std::move(detector_ids)},
79 ke_threshold_{ke_threshold_gev * GeV} {}
81 G4bool
ProcessHits(G4Step* step, G4TouchableHistory*)
override {
82 if (!step->IsFirstStepInVolume())
return false;
84 auto* track = step->GetTrack();
85 if (track->GetKineticEnergy() < ke_threshold_)
return false;
88 hit.energyDeposit = 0;
89 hit.pathLength = track->GetTrackLength() / mm;
104 : ke_threshold_{ke_threshold_gev * GeV} {}
107 auto* track = step->GetTrack();
108 if (track->GetKineticEnergy() < ke_threshold_) {
109 track->SetTrackStatus(fStopAndKill);
114 double ke_threshold_;
120 : particle_ke_cut_{particle_ke_cut_gev * GeV} {}
123 if (particle_ke_cut_ > 0 && track->GetParentID() != 0 &&
124 track->GetKineticEnergy() < particle_ke_cut_)
128 p.trackId = track->GetTrackID();
129 p.parentId = track->GetParentID();
130 p.pdgCode = track->GetDefinition()->GetPDGEncoding();
132 auto pos = track->GetPosition();
133 auto mom = track->GetMomentum();
134 p.vertex = {pos.x() / mm, pos.y() / mm, pos.z() / mm};
135 p.momentum = {mom.x() / GeV, mom.y() / GeV, mom.z() / GeV};
136 p.energy = track->GetKineticEnergy() / GeV;
137 p.time = track->GetGlobalTime() / ns;
139 auto* creator = track->GetCreatorProcess();
140 p.creatorProcess = creator ? creator->GetProcessSubType() : 0;
149 auto pos = track->GetPosition();
150 tl_particles[it->second].endpoint = {pos.x() / mm, pos.y() / mm,
156 double particle_ke_cut_;
Definition geant4_sim_core.hpp:73
G4bool ProcessHits(G4Step *step, G4TouchableHistory *) override
Definition geant4_sim_core.hpp:81
CrossingSD(G4String const &name, DetectorIdMap detector_ids, double ke_threshold_gev=0.0)
Definition geant4_sim_core.hpp:75
Definition geant4_sim_core.hpp:101
void UserSteppingAction(const G4Step *step) override
Definition geant4_sim_core.hpp:106
EnergyCutAction(double ke_threshold_gev)
Definition geant4_sim_core.hpp:103
Definition geant4_sim_core.hpp:50
ScoringSD(G4String const &name, DetectorIdMap detector_ids)
Definition geant4_sim_core.hpp:52
G4bool ProcessHits(G4Step *step, G4TouchableHistory *) override
Definition geant4_sim_core.hpp:55
Definition geant4_sim_core.hpp:117
TrackingAction(double particle_ke_cut_gev=0.0)
Definition geant4_sim_core.hpp:119
void PostUserTrackingAction(const G4Track *track) override
Definition geant4_sim_core.hpp:146
void PreUserTrackingAction(const G4Track *track) override
Definition geant4_sim_core.hpp:122
Definition detector_construction.hpp:31
thread_local std::vector< SimParticle > tl_particles
Definition geant4_sim_core.hpp:27
thread_local std::unordered_map< int, std::size_t > tl_track_map
Definition geant4_sim_core.hpp:28
thread_local std::vector< SimHit > tl_hits
Definition geant4_sim_core.hpp:26
SimHit make_base_hit(G4Step const *step, DetectorIdMap const &detector_ids)
Definition geant4_sim_core.hpp:32
std::unordered_map< G4LogicalVolume *, int > DetectorIdMap
Definition geant4_sim_core.hpp:30