16 #include <HepMC3/GenEvent.h>
17 #include <HepMC3/GenParticle.h>
18 #include <HepMC3/GenVertex.h>
28 HepMC3::ConstGenParticlePtr searchProcessParticleById(
29 const HepMC3::ConstGenVertexPtr&
vertex,
const int id) {
31 for (
const auto&
particle : vertex->particles_out()) {
33 particle->attribute<HepMC3::IntAttribute>(
"TrackID")->
value();
48 void setPassedMaterial(
const HepMC3::ConstGenVertexPtr& vertex,
const int id,
52 HepMC3::ConstGenParticlePtr currentParticle =
nullptr;
53 HepMC3::ConstGenVertexPtr currentVertex = vertex;
55 while (currentVertex && !currentVertex->particles_in().empty() &&
56 currentVertex->particles_in()[0]->attribute<HepMC3::IntAttribute>(
58 currentVertex->particles_in()[0]
59 ->attribute<HepMC3::IntAttribute>(
"TrackID")
62 currentParticle = currentVertex->particles_in()[0];
63 const double stepLength =
64 currentParticle->attribute<HepMC3::DoubleAttribute>(
"StepLength")
69 currentParticle->attribute<HepMC3::DoubleAttribute>(
"NextX0")->
value();
72 currentParticle->attribute<HepMC3::DoubleAttribute>(
"NextL0")->
value();
73 currentVertex = currentParticle->production_vertex();
86 std::vector<ActsExamples::SimParticle> selectOutgoingParticles(
87 const HepMC3::ConstGenVertexPtr& vertex,
const int trackID) {
88 std::vector<ActsExamples::SimParticle> finalStateParticles;
91 HepMC3::ConstGenParticlePtr procPart =
92 searchProcessParticleById(vertex, trackID);
95 HepMC3::ConstGenVertexPtr
endVertex = procPart->end_vertex();
97 ->attribute<HepMC3::StringAttribute>(
"NextProcessOf-" +
99 ->
value() !=
"Death") {
101 finalStateParticles.push_back(
105 for (
const HepMC3::ConstGenParticlePtr& procPartOut :
106 endVertex->particles_out()) {
107 if (procPartOut->attribute<HepMC3::IntAttribute>(
"TrackID")->value() ==
109 procPartOut->end_vertex()) {
110 for (
const HepMC3::ConstGenParticlePtr& dyingPartOut :
111 procPartOut->end_vertex()->particles_out()) {
112 finalStateParticles.push_back(
120 const std::vector<std::string> attributes = endVertex->attribute_names();
121 for (
const auto& att : attributes) {
123 if (att.find(
"InitialParametersOf") != std::string::npos) {
124 const std::vector<double> mom4 =
125 endVertex->attribute<HepMC3::VectorDoubleAttribute>(att)->
value();
126 const HepMC3::FourVector&
pos4 = endVertex->position();
127 const int id = stoi(att.substr(att.find(
"-") + 1));
128 HepMC3::ConstGenParticlePtr genParticle =
129 searchProcessParticleById(endVertex,
id);
135 simParticle.setPosition4(pos4.x(), pos4.y(), pos4.z(), pos4.t());
137 simParticle.setDirection(mom3.normalized());
138 simParticle.setAbsoluteMomentum(mom3.norm());
141 finalStateParticles.push_back(simParticle);
145 return finalStateParticles;
155 for (
auto& interaction : interactions) {
156 for (
auto cit = interaction.after.cbegin();
157 cit != interaction.after.cend();) {
160 cit->absoluteMomentum() < cfg.
pMin) {
161 interaction.after.erase(cit);
169 for (
auto& interaction : interactions) {
170 std::sort(interaction.after.begin(), interaction.after.end(),
183 : ActsExamples::
IAlgorithm(
"HepMCProcessExtractor", level),
186 throw std::invalid_argument(
"Missing input event collection");
189 throw std::invalid_argument(
"Missing output collection");
192 throw std::invalid_argument(
"Missing extraction process");
202 const auto events = m_inputEvents(context);
207 if (
event.particles().empty() ||
event.vertices().empty()) {
212 HepMC3::ConstGenParticlePtr initialParticle =
event.particles()[0];
218 std::vector<ActsExamples::SimParticle> finalStateParticles;
220 bool vertexFound =
false;
221 for (
const auto& vertex :
event.vertices()) {
222 const std::vector<std::string> attributes = vertex->attribute_names();
223 for (
const auto& attribute : attributes) {
224 if (vertex->attribute_as_string(attribute).find(
225 m_cfg.extractionProcess) != std::string::npos) {
226 const int procID = stoi(attribute.substr(attribute.find(
"-") + 1));
228 particleToInteraction =
231 setPassedMaterial(vertex, procID, particleToInteraction);
233 finalStateParticles = selectOutgoingParticles(vertex, procID);
243 simParticle, particleToInteraction, finalStateParticles});
247 filterAndSort(
m_cfg, fractions);
249 ACTS_INFO(events.size() <<
" processed");
252 m_outputSimulationProcesses(context,
std::move(fractions));