13 #include <G4RunManager.hh>
15 #include <G4VProcess.hh>
16 #include <HepMC3/Attribute.h>
17 #include <HepMC3/Units.h>
21 namespace ActsExamples::Geant4::HepMC3 {
31 : G4UserSteppingAction(),
32 m_eventRejectionProcess(std::
move(eventRejectionProcess)) {
34 throw std::logic_error(
"Attempted to duplicate a singleton");
47 step->GetPostStepPoint()
48 ->GetProcessDefinedStep()
51 G4RunManager::GetRunManager()->AbortEvent();
66 constexpr
double convertLength = 1. /
CLHEP::mm;
67 constexpr
double convertEnergy = 1. /
CLHEP::GeV;
68 constexpr
double convertTime = 1. /
CLHEP::s;
71 auto* track = step->GetTrack();
73 auto postStepMomentum = track->GetMomentum() * convertEnergy;
74 auto postStepEnergy = track->GetTotalEnergy() * convertEnergy;
75 ::HepMC3::FourVector mom4{postStepMomentum[0], postStepMomentum[1],
76 postStepMomentum[2], postStepEnergy};
77 auto postParticle = std::make_shared<::HepMC3::GenParticle>(
78 mom4, track->GetDynamicParticle()->GetPDGcode());
81 auto process = std::make_shared<::HepMC3::StringAttribute>(
82 step->GetPostStepPoint()->GetProcessDefinedStep()->GetProcessName());
87 auto* preStep = step->GetPreStepPoint();
88 auto prePosition = preStep->GetPosition() * convertLength;
89 auto preTime = preStep->GetGlobalTime() * convertTime;
90 ::HepMC3::FourVector prePos{prePosition[0], prePosition[1], prePosition[2],
94 if (
event.vertices().empty()) {
95 auto vertex = std::make_shared<::HepMC3::GenVertex>(prePos);
96 vertex->add_particle_out(postParticle);
99 vertex->add_attribute(
"NextProcessOf" + trackId, process);
103 if (
vertex->position() == prePos) {
105 vertex->add_particle_out(postParticle);
106 vertex->add_attribute(
"NextProcessOf-" + trackId, process);
107 auto preStepMomentum =
108 step->GetPreStepPoint()->GetMomentum() * convertEnergy;
110 step->GetPreStepPoint()->GetTotalEnergy() * convertEnergy;
111 auto preMom4 = std::make_shared<::HepMC3::VectorDoubleAttribute>(
112 std::vector<double>{preStepMomentum[0], preStepMomentum[1],
113 preStepMomentum[2], preStepEnergy});
114 vertex->add_attribute(
"InitialParametersOf-" + trackId, preMom4);
118 if (track->GetCreatorProcess() !=
nullptr) {
119 postParticle->add_attribute(
120 "CreatorProcessOf-" + trackId,
121 std::make_shared<::HepMC3::StringAttribute>(
122 track->GetCreatorProcess()->GetProcessName()));
131 auto* postStep = step->GetPostStepPoint();
132 auto postPosition = postStep->GetPosition() * convertLength;
133 auto postTime = postStep->GetGlobalTime() * convertTime;
134 ::HepMC3::FourVector postPos{postPosition[0], postPosition[1],
135 postPosition[2], postTime};
145 postParticle->add_attribute(
146 "TrackID", std::make_shared<::HepMC3::IntAttribute>(track->GetTrackID()));
147 postParticle->add_attribute(
149 std::make_shared<::HepMC3::IntAttribute>(track->GetParentID()));
150 const double X0 = track->GetMaterial()->GetRadlen() * convertLength;
152 track->GetMaterial()->GetNuclearInterLength() * convertLength;
153 const double stepLength = track->GetStepLength();
154 postParticle->add_attribute(
"NextX0",
155 std::make_shared<::HepMC3::DoubleAttribute>(X0));
156 postParticle->add_attribute(
"NextL0",
157 std::make_shared<::HepMC3::DoubleAttribute>(L0));
158 postParticle->add_attribute(
159 "StepLength", std::make_shared<::HepMC3::DoubleAttribute>(stepLength));
160 postParticle->set_status(1);
163 if (track->GetTrackStatus() != fAlive) {
164 process = std::make_shared<::HepMC3::StringAttribute>(
"Death");