19 std::unique_ptr<simcore::hepmc::HepMCEvent> hepmc_event =
22 if (hepmc_event !=
nullptr) {
24 std::vector<std::pair<int, G4PrimaryVertex*>> vertices;
26 const auto& particles = hepmc_event->getParticles();
27 for (
const auto& particle : particles) {
28 const int vtx_id = particle->getProductionVertexId();
30 std::find_if(vertices.begin(), vertices.end(),
31 [vtx_id](
const auto& v) { return v.first == vtx_id; });
32 G4PrimaryVertex* vertex{
nullptr};
33 if (it == vertices.end()) {
35 const HepMC3::FourVector pos = particle->getProductionPosition();
37 vertex =
new G4PrimaryVertex(pos.x() +
vertex_[0], pos.y() +
vertex_[1],
38 pos.z() +
vertex_[2], pos.t() / c_light);
40 if (vertices.empty()) vertex->SetWeight(hepmc_event->getEventWeight());
41 vertices.emplace_back(vtx_id, vertex);
48 G4PrimaryParticle* primary =
new G4PrimaryParticle();
50 primary->SetPDGcode(particle->getPdgId());
54 primary->Set4Momentum(particle->getMomentum(0) * GeV,
55 particle->getMomentum(1) * GeV,
56 particle->getMomentum(2) * GeV,
57 particle->getMomentum(3) * GeV
60 auto primary_info = std::make_unique<UserPrimaryParticleInformation>();
61 primary_info->setHepEvtStatus(particle->getStatus());
62 primary->SetUserInformation(primary_info.release());
64 vertex->SetPrimary(primary);
69 G4PrimaryVertex* first = vertices.front().second;
70 const G4ThreeVector before = first->GetPosition();
72 const G4ThreeVector shift = first->GetPosition() - before;
73 for (std::size_t i = 1; i < vertices.size(); ++i) {
74 auto* vertex = vertices[i].second;
75 vertex->SetPosition(vertex->GetX0() + shift.x(),
76 vertex->GetY0() + shift.y(),
77 vertex->GetZ0() + shift.z());
81 for (
auto& [vtx_id, vertex] : vertices) anEvent->AddPrimaryVertex(vertex);
84 ldmx_log(error) <<
"Ran out of input events so run will be aborted!";
85 G4RunManager::GetRunManager()->AbortRun(
true);
86 anEvent->SetEventAborted();