From 02e8d99498fa81c2c1d7793dd955b8038cecb0ec Mon Sep 17 00:00:00 2001 From: Juan Miguel Carceller <22276694+jmcarcell@users.noreply.github.com> Date: Wed, 21 Aug 2024 14:12:52 +0200 Subject: [PATCH] Use std::shared_ptr for compatibility with HepMC3 3.3.0 (#39) Co-authored-by: jmcarcell --- k4Gen/src/components/EDMToHepMCConverter.cpp | 19 +++++++++---------- k4Gen/src/components/HepMCFullMerge.cpp | 20 ++++++++++---------- k4Gen/src/components/HepMCSimpleMerge.cpp | 14 +++++++------- 3 files changed, 26 insertions(+), 27 deletions(-) diff --git a/k4Gen/src/components/EDMToHepMCConverter.cpp b/k4Gen/src/components/EDMToHepMCConverter.cpp index 2a9e34f..1d2526e 100644 --- a/k4Gen/src/components/EDMToHepMCConverter.cpp +++ b/k4Gen/src/components/EDMToHepMCConverter.cpp @@ -27,20 +27,19 @@ StatusCode EDMToHepMCConverter::execute(const EventContext&) const { event->set_units(HepMC3::Units::GEV, HepMC3::Units::MM); - for (auto p : *(particles)) { + for (auto p : *particles) { if (p.getGeneratorStatus() == 1) { // only final state particles const auto& mom = p.getMomentum(); - auto* pHepMC = - new GenParticle(HepMC3::FourVector(mom.x, mom.y, mom.z, p.getMass()), - p.getPDG(), - p.getGeneratorStatus()); // hepmc status code for final state particle + auto pHepMC = std::make_shared(HepMC3::FourVector(mom.x, mom.y, mom.z, p.getMass()), + p.getPDG(), + p.getGeneratorStatus()); // hepmc status code for final state particle const auto& pos = p.getVertex(); - auto* v = - new HepMC3::GenVertex(HepMC3::FourVector(pos.x, - pos.y, - pos.z, - p.getTime() / Gaudi::Units::c_light)); + auto v = + std::make_shared(HepMC3::FourVector(pos.x, + pos.y, + pos.z, + p.getTime() / Gaudi::Units::c_light)); v->add_particle_out(pHepMC); event->add_vertex(v); diff --git a/k4Gen/src/components/HepMCFullMerge.cpp b/k4Gen/src/components/HepMCFullMerge.cpp index c58aeb1..06af151 100644 --- a/k4Gen/src/components/HepMCFullMerge.cpp +++ b/k4Gen/src/components/HepMCFullMerge.cpp @@ -26,21 +26,21 @@ StatusCode HepMCFullMerge::initialize() { StatusCode HepMCFullMerge::merge(HepMC3::GenEvent& signalEvent, const std::vector& eventVector) { for (auto it = eventVector.cbegin(), end = eventVector.cend(); it != end; ++it) { // keep track of which vertex in full event corresponds to which vertex in merged event - std::unordered_map inputToMergedVertexMap; - for (auto v: (*it).vertices()) { - HepMC3::GenVertex* outvertex = new HepMC3::GenVertex(v->position()); - inputToMergedVertexMap[v.get()] = outvertex; + std::unordered_map, std::shared_ptr> inputToMergedVertexMap; + for (auto& v: it->vertices()) { + auto outvertex = std::make_shared(v->position()); + inputToMergedVertexMap[v] = outvertex; signalEvent.add_vertex(outvertex); } - for (auto p: (*it).particles()) { + for (auto& p: it->particles()) { // ownership of the particle is given to the vertex - HepMC3::GenParticle* newparticle = new HepMC3::GenParticle(*p); + auto newparticle = std::make_shared(*p); // attach particles to correct vertices in merged event - if (nullptr != p->end_vertex()) { - inputToMergedVertexMap[p->end_vertex().get()]->add_particle_in(newparticle); + if (p->end_vertex()) { + inputToMergedVertexMap[p->end_vertex()]->add_particle_in(newparticle); } - if (nullptr != p->production_vertex()) { - inputToMergedVertexMap[p->production_vertex().get()]->add_particle_out(newparticle); + if (p->production_vertex()) { + inputToMergedVertexMap[p->production_vertex()]->add_particle_out(newparticle); } } } diff --git a/k4Gen/src/components/HepMCSimpleMerge.cpp b/k4Gen/src/components/HepMCSimpleMerge.cpp index 62f2923..5f556e7 100644 --- a/k4Gen/src/components/HepMCSimpleMerge.cpp +++ b/k4Gen/src/components/HepMCSimpleMerge.cpp @@ -26,21 +26,21 @@ StatusCode HepMCSimpleMerge::initialize() { StatusCode HepMCSimpleMerge::merge(HepMC3::GenEvent& signalEvent, const std::vector& eventVector) { // iterate over vertices and add them to signalEvent for (auto it = eventVector.cbegin(), end = eventVector.cend(); it != end; ++it) { - std::unordered_map inputToMergedVertexMap; - for (auto v: (*it).vertices()) { - HepMC3::GenVertex* newVertex = new HepMC3::GenVertex(v->position()); - inputToMergedVertexMap[v.get()] = newVertex; + std::unordered_map, std::shared_ptr> inputToMergedVertexMap; + for (auto& v: it->vertices()) { + auto newVertex = std::make_shared(v->position()); + inputToMergedVertexMap[v] = newVertex; } - for (auto p: (*it).particles()) { + for (auto& p: it->particles()) { // simple check if final-state particle: // has no end vertex and correct status code meaning no further decays if (!p->end_vertex() && p->status() == 1) { // ownership of the particle (newParticle) is then given to the vertex (newVertex) - HepMC3::GenParticle* newParticle = new HepMC3::GenParticle(*p); + auto newParticle = std::make_shared(*p); // each pile up particle is associated to a new production vertex // the position information is preserved // ownership of the vertex (newVertex) is given to the event (newEvent) - HepMC3::GenVertex* newVertex = inputToMergedVertexMap[p->production_vertex().get()]; + auto newVertex = inputToMergedVertexMap[p->production_vertex()]; newVertex->add_particle_out(newParticle); signalEvent.add_vertex(newVertex); }