From d52465c73cf356a7b1e80e39608902025f27f9d7 Mon Sep 17 00:00:00 2001 From: Andrii Verbytskyi <andrii.verbytskyi@cern.ch> Date: Tue, 29 Sep 2020 08:37:10 +0000 Subject: [PATCH] Migration of ISF_HepMC_Tools to HepmC3 --- .../ISF_HepMC_Interfaces/IGenParticleFilter.h | 5 ++ .../src/GenParticleFinalStateFilter.cxx | 21 +++++++ .../src/GenParticleFinalStateFilter.h | 9 +++ .../src/GenParticleGenericFilter.cxx | 20 +++++- .../src/GenParticleGenericFilter.h | 8 +++ .../src/GenParticleInteractingFilter.cxx | 15 +++++ .../src/GenParticleInteractingFilter.h | 6 ++ .../src/GenParticleLifetimeFilter.cxx | 12 ++++ .../src/GenParticleLifetimeFilter.h | 4 ++ .../src/GenParticlePositionFilter.cxx | 8 +++ .../src/GenParticlePositionFilter.h | 4 ++ .../src/GenParticleSimWhiteList.cxx | 62 +++++++++++++++++++ .../src/GenParticleSimWhiteList.h | 9 +++ 13 files changed, 182 insertions(+), 1 deletion(-) diff --git a/Simulation/ISF/ISF_HepMC/ISF_HepMC_Interfaces/ISF_HepMC_Interfaces/IGenParticleFilter.h b/Simulation/ISF/ISF_HepMC/ISF_HepMC_Interfaces/ISF_HepMC_Interfaces/IGenParticleFilter.h index a9827852815..ef30901c088 100644 --- a/Simulation/ISF/ISF_HepMC/ISF_HepMC_Interfaces/ISF_HepMC_Interfaces/IGenParticleFilter.h +++ b/Simulation/ISF/ISF_HepMC/ISF_HepMC_Interfaces/ISF_HepMC_Interfaces/IGenParticleFilter.h @@ -37,8 +37,13 @@ namespace ISF { /// Creates the InterfaceID and interfaceID() method DeclareInterfaceID(IGenParticleFilter, 1, 0); +#ifdef HEPMC3 + /** Returns a boolean if the particle has passed or not */ + virtual bool pass(HepMC::ConstGenParticlePtr particle) const = 0; +#else /** Returns a boolean if the particle has passed or not */ virtual bool pass(const HepMC::GenParticle& particle) const = 0; +#endif }; diff --git a/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleFinalStateFilter.cxx b/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleFinalStateFilter.cxx index 7ce0aa81d86..954e498d03e 100644 --- a/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleFinalStateFilter.cxx +++ b/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleFinalStateFilter.cxx @@ -39,6 +39,17 @@ StatusCode ISF::GenParticleFinalStateFilter::initialize() /** returns true if the the particle is considered stable */ +#ifdef HEPMC3 +bool ISF::GenParticleFinalStateFilter::pass(HepMC::ConstGenParticlePtr particle) const +{ + bool passFilter = true; + passFilter &= isFinalState(particle); + passFilter &= (!m_checkGenSimStable) || MC::isSimStable(particle); + passFilter &= (!m_checkGenInteracting) || MC::isSimInteracting(particle); + return passFilter; +} + +#else bool ISF::GenParticleFinalStateFilter::pass(const HepMC::GenParticle& particle) const { bool passFilter = true; @@ -48,6 +59,7 @@ bool ISF::GenParticleFinalStateFilter::pass(const HepMC::GenParticle& particle) return passFilter; } +#endif StatusCode ISF::GenParticleFinalStateFilter::finalize() @@ -57,9 +69,18 @@ StatusCode ISF::GenParticleFinalStateFilter::finalize() } /** checks if the particle is in its final state (no end vertex) */ +#ifdef HEPMC3 +bool ISF::GenParticleFinalStateFilter::isFinalState(HepMC::ConstGenParticlePtr p) const { + // particle is in its final state if both: + // * no end_vertex + // * status==1 + return ( !p->end_vertex() && p->status()==1 ); +} +#else bool ISF::GenParticleFinalStateFilter::isFinalState(const HepMC::GenParticle &p) const { // particle is in its final state if both: // * no end_vertex // * status==1 return ( !p.end_vertex() && p.status()==1 ); } +#endif diff --git a/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleFinalStateFilter.h b/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleFinalStateFilter.h index 1e6e3dceb61..b00e661f55a 100644 --- a/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleFinalStateFilter.h +++ b/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleFinalStateFilter.h @@ -42,12 +42,21 @@ namespace ISF { StatusCode initialize(); StatusCode finalize(); +#ifdef HEPMC3 + /** Returns the Particle Stack, should register truth */ + bool pass(HepMC::ConstGenParticlePtr particle) const; + private: + /** checks if the particle is in its final state (no end vertex) */ + bool isFinalState( HepMC::ConstGenParticlePtr p) const; +#else + /** Returns the Particle Stack, should register truth */ bool pass(const HepMC::GenParticle& particle) const; private: /** checks if the particle is in its final state (no end vertex) */ bool isFinalState( const HepMC::GenParticle& p) const; +#endif bool m_checkGenSimStable; //!< boolean switch to check on sim stable bool m_checkGenInteracting; //!< boolean switch to check on gen interacting diff --git a/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleGenericFilter.cxx b/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleGenericFilter.cxx index f4a54d5845c..efee0b2b74b 100644 --- a/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleGenericFilter.cxx +++ b/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleGenericFilter.cxx @@ -81,17 +81,29 @@ StatusCode ISF::GenParticleGenericFilter::finalize() /** Returns whether the given particle passes all cuts or not */ +#ifdef HEPMC3 +bool ISF::GenParticleGenericFilter::pass(HepMC::ConstGenParticlePtr particle) const +#else bool ISF::GenParticleGenericFilter::pass(const HepMC::GenParticle& particle) const +#endif { bool pass = true; +#ifdef HEPMC3 + HepMC::ConstGenVertexPtr productionVertex = particle->production_vertex(); +#else HepMC::ConstGenVertexPtr productionVertex = particle.production_vertex(); +#endif const auto* position = productionVertex ? &productionVertex->position() : nullptr; if (!position || position->perp()<=m_maxApplicableRadius) { pass = check_cuts_passed(particle); } +#ifdef HEPMC3 + const auto& momentum = particle->momentum(); +#else const auto& momentum = particle.momentum(); +#endif ATH_MSG_VERBOSE( "GenParticle '" << particle << "' with " << (position ? "pos: r=" + std::to_string(position->perp()) : "") << ", mom: eta=" << momentum.eta() << " phi=" << momentum.phi() @@ -102,12 +114,18 @@ bool ISF::GenParticleGenericFilter::pass(const HepMC::GenParticle& particle) con /** Check whether the given particle passes all configure cuts or not */ +#ifdef HEPMC3 +bool ISF::GenParticleGenericFilter::check_cuts_passed(HepMC::ConstGenParticlePtr particle) const { + const auto momentum = particle->momentum(); + int pdg = particle->pdg_id(); +#else bool ISF::GenParticleGenericFilter::check_cuts_passed(const HepMC::GenParticle &particle) const { const auto& momentum = particle.momentum(); + int pdg = particle.pdg_id(); +#endif double mom = std::sqrt(momentum.x()*momentum.x()+momentum.y()*momentum.y()+momentum.z()*momentum.z()); double eta = momentum.eta(); double phi = momentum.phi(); - int pdg = particle.pdg_id(); // check the particle pdg code if( m_pdgs.size() && std::find(std::begin(m_pdgs), std::end(m_pdgs), pdg) == std::end(m_pdgs) ) { diff --git a/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleGenericFilter.h b/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleGenericFilter.h index b4c7129bcac..ed77e642c0e 100644 --- a/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleGenericFilter.h +++ b/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleGenericFilter.h @@ -52,11 +52,19 @@ typedef std::vector<int> PDGCodes; StatusCode finalize(); /// Interface method that returns whether the given particle passes all cuts or not +#ifdef HEPMC3 + bool pass(HepMC::ConstGenParticlePtr particle) const; +#else bool pass(const HepMC::GenParticle& particle) const; +#endif private: /// Check whether the given particle passes all configure cuts or not +#ifdef HEPMC3 + bool check_cuts_passed(HepMC::ConstGenParticlePtr particle) const; +#else bool check_cuts_passed(const HepMC::GenParticle& particle) const; +#endif /// the cuts defined by the use double m_minEta; //!< min pseudorapidity cut diff --git a/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleInteractingFilter.cxx b/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleInteractingFilter.cxx index dbf3eff7450..c5d93d002fd 100644 --- a/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleInteractingFilter.cxx +++ b/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleInteractingFilter.cxx @@ -11,6 +11,7 @@ // HepMC includes #include "AtlasHepMC/GenParticle.h" +#include "AtlasHepMC/Flow.h" // Helper function #include "TruthUtils/HepMCHelpers.h" @@ -67,6 +68,19 @@ StatusCode ISF::GenParticleInteractingFilter::initialize() } /** passes through to the private version of the filter */ +#ifdef HEPMC3 +bool ISF::GenParticleInteractingFilter::pass(HepMC::ConstGenParticlePtr particle) const +{ + const int pdg_id = particle->pdg_id(); + const bool isInteracting = find(m_additionalInteractingParticleTypes.begin(), + m_additionalInteractingParticleTypes.end(), + pdg_id) != m_additionalInteractingParticleTypes.end(); + const bool isNonInteracting = find(m_additionalNonInteractingParticleTypes.begin(), + m_additionalNonInteractingParticleTypes.end(), + pdg_id) != m_additionalNonInteractingParticleTypes.end(); + return !(MC::isNonInteracting( particle ) || isNonInteracting) || isInteracting; +} +#else bool ISF::GenParticleInteractingFilter::pass(const HepMC::GenParticle& particle) const { const int& pdg_id = particle.pdg_id(); @@ -78,4 +92,5 @@ bool ISF::GenParticleInteractingFilter::pass(const HepMC::GenParticle& particle) pdg_id) != m_additionalNonInteractingParticleTypes.end(); return !(MC::isNonInteracting( &particle ) || isNonInteracting) || isInteracting; } +#endif diff --git a/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleInteractingFilter.h b/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleInteractingFilter.h index 97f0011043c..ea2f829f047 100644 --- a/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleInteractingFilter.h +++ b/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleInteractingFilter.h @@ -18,6 +18,7 @@ // STL includes #include <string> +#include "AtlasHepMC/GenParticle.h" namespace ISF { class ISFParticle; @@ -40,9 +41,14 @@ namespace ISF { /** Framework methods */ virtual StatusCode initialize() override; +#ifdef HEPMC3 + /** passes through to the private version */ + virtual bool pass(HepMC::ConstGenParticlePtr particle ) const override; +#else /** passes through to the private version */ virtual bool pass(const HepMC::GenParticle& particle ) const override; +#endif /** Additional PDG codes to classify as interacting */ std::vector<int> m_additionalInteractingParticleTypes; diff --git a/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleLifetimeFilter.cxx b/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleLifetimeFilter.cxx index dd4edc16bdb..10891c17de6 100644 --- a/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleLifetimeFilter.cxx +++ b/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleLifetimeFilter.cxx @@ -27,10 +27,18 @@ ISF::GenParticleLifetimeFilter::GenParticleLifetimeFilter( const std::string& t, /** does the given particle pass the filter? */ +#ifdef HEPMC3 +bool ISF::GenParticleLifetimeFilter::pass(HepMC::ConstGenParticlePtr particle) const +#else bool ISF::GenParticleLifetimeFilter::pass(const HepMC::GenParticle& particle) const +#endif { // the GenParticle end vertex +#ifdef HEPMC3 + auto endVtx = particle->end_vertex(); +#else auto endVtx = particle.end_vertex(); +#endif // no production vertex? if (!endVtx) { ATH_MSG_DEBUG("GenParticle does not have an end vertex, this is fine"); @@ -40,7 +48,11 @@ bool ISF::GenParticleLifetimeFilter::pass(const HepMC::GenParticle& particle) co const auto& end4Vec = endVtx->position(); // the GenParticle production vertex +#ifdef HEPMC3 + auto prodVtx = particle->production_vertex(); +#else auto prodVtx = particle.production_vertex(); +#endif // no production vertex? if (!prodVtx) { ATH_MSG_DEBUG("GenParticle does not have a production vertex, filtering it out"); diff --git a/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleLifetimeFilter.h b/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleLifetimeFilter.h index f5629f8adde..b434bb8c10d 100644 --- a/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleLifetimeFilter.h +++ b/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleLifetimeFilter.h @@ -34,7 +34,11 @@ namespace ISF { ~GenParticleLifetimeFilter(){} /** does the given particle pass the filter? */ +#ifdef HEPMC3 + bool pass(HepMC::ConstGenParticlePtr particle) const; +#else bool pass(const HepMC::GenParticle& particle) const; +#endif private: double m_minLifetime{0.000001}; //units of c*ns diff --git a/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticlePositionFilter.cxx b/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticlePositionFilter.cxx index 597933ec224..33031f1e66d 100644 --- a/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticlePositionFilter.cxx +++ b/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticlePositionFilter.cxx @@ -12,6 +12,7 @@ // HepMC includes #include "AtlasHepMC/GenParticle.h" #include "AtlasHepMC/GenVertex.h" +#include "AtlasHepMC/SimpleVector.h" /** Constructor **/ ISF::GenParticlePositionFilter::GenParticlePositionFilter( const std::string& t, @@ -48,10 +49,17 @@ StatusCode ISF::GenParticlePositionFilter::initialize() /** does the given particle pass the filter? */ +#ifdef HEPMC3 +bool ISF::GenParticlePositionFilter::pass(HepMC::ConstGenParticlePtr particle) const +{ + // the GenParticle production vertex + auto vtx = particle->production_vertex(); +#else bool ISF::GenParticlePositionFilter::pass(const HepMC::GenParticle& particle) const { // the GenParticle production vertex HepMC::GenVertexPtr vtx = particle.production_vertex(); +#endif // no production vertex? if (!vtx) { diff --git a/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticlePositionFilter.h b/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticlePositionFilter.h index 18b0c320154..8bbe7b4c062 100644 --- a/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticlePositionFilter.h +++ b/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticlePositionFilter.h @@ -45,7 +45,11 @@ namespace ISF { StatusCode finalize(); /** does the given particle pass the filter? */ +#ifdef HEPMC3 + bool pass(HepMC::ConstGenParticlePtr particle) const; +#else bool pass(const HepMC::GenParticle& particle) const; +#endif private: ServiceHandle<IGeoIDSvc> m_geoIDSvc; diff --git a/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleSimWhiteList.cxx b/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleSimWhiteList.cxx index c4fefcb3daf..dcffa77c2c0 100644 --- a/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleSimWhiteList.cxx +++ b/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleSimWhiteList.cxx @@ -72,6 +72,33 @@ StatusCode ISF::GenParticleSimWhiteList::initialize() } /** passes through to the private version of the filter */ +#ifdef HEPMC3 +bool ISF::GenParticleSimWhiteList::pass(HepMC::ConstGenParticlePtr particle) const +{ + + ATH_MSG_VERBOSE( "Checking whether " << particle << " passes the filter." ); + + static std::vector<int> vertices(500); + vertices.clear(); + bool so_far_so_good = pass( particle , vertices ); + + // Test all parent particles + if (so_far_so_good && particle->production_vertex() && m_qs){ + for (auto pit: particle->production_vertex()->particles_in()){ + // Loop breaker + if ( HepMC::barcode(pit) == HepMC::barcode(particle) ) continue; + // Check this particle + vertices.clear(); + bool parent_all_clear = pass( pit , vertices ); + ATH_MSG_VERBOSE( "Parent all clear: " << parent_all_clear << + "\nIf true, will not pass the daughter because it should have been picked up through the parent already (to avoid multi-counting)." ); + so_far_so_good = so_far_so_good && !parent_all_clear; + } // Loop over parents + } // particle had parents + + return so_far_so_good; +} +#else bool ISF::GenParticleSimWhiteList::pass(const HepMC::GenParticle& particle) const { @@ -98,8 +125,42 @@ bool ISF::GenParticleSimWhiteList::pass(const HepMC::GenParticle& particle) cons return so_far_so_good; } +#endif /** returns true if the the particle and all daughters are on the white list */ +#ifdef HEPMC3 +bool ISF::GenParticleSimWhiteList::pass(HepMC::ConstGenParticlePtr particle , std::vector<int> & used_vertices ) const +{ + // See if the particle is in the white list + bool passFilter = std::binary_search( m_pdgId.begin() , m_pdgId.end() , particle->pdg_id() ) || MC::PID::isNucleus( particle->pdg_id() ); + // Remove documentation particles + passFilter = passFilter && particle->status()<3; + // Test all daughter particles + if (particle->end_vertex() && m_qs && passFilter){ + // Break loops + if ( std::find( used_vertices.begin() , used_vertices.end() , HepMC::barcode(particle->end_vertex()) )==used_vertices.end() ){ + used_vertices.push_back( HepMC::barcode(particle->end_vertex()) ); + for (auto pit: particle->end_vertex()->particles_out()){ + passFilter = passFilter && pass( pit , used_vertices ); + if (!passFilter) { + ATH_MSG_VERBOSE( "Daughter particle " << pit << " does not pass." ); + break; + } + } // Loop over daughters + } // Break loops + } // particle had daughters + else if (!particle->end_vertex() && !passFilter && particle->status()<3) { // no daughters... No end vertex... Check if this isn't trouble + ATH_MSG_ERROR( "Found a particle with no end vertex that does not appear in the white list." ); + ATH_MSG_ERROR( "This is VERY likely pointing to a problem with either the configuration you "); + ATH_MSG_ERROR( "are using, or a bug in the generator. Either way it should be fixed. The"); + ATH_MSG_ERROR( "particle will come next, and then we will throw."); + ATH_MSG_ERROR( particle ); + throw; + } + + return passFilter; +} +#else bool ISF::GenParticleSimWhiteList::pass(const HepMC::GenParticle& particle , std::vector<int> & used_vertices ) const { // See if the particle is in the white list @@ -132,6 +193,7 @@ bool ISF::GenParticleSimWhiteList::pass(const HepMC::GenParticle& particle , std return passFilter; } +#endif StatusCode ISF::GenParticleSimWhiteList::finalize() { diff --git a/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleSimWhiteList.h b/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleSimWhiteList.h index bcd25eebef1..9dd60edc95a 100644 --- a/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleSimWhiteList.h +++ b/Simulation/ISF/ISF_HepMC/ISF_HepMC_Tools/src/GenParticleSimWhiteList.h @@ -19,6 +19,7 @@ #include <string> #include <vector> +#include "AtlasHepMC/GenParticle.h" namespace ISF { class ISFParticle; @@ -44,11 +45,19 @@ namespace ISF { StatusCode finalize(); /** passes through to the private version */ +#ifdef HEPMC3 + bool pass(HepMC::ConstGenParticlePtr particle ) const; +#else bool pass(const HepMC::GenParticle& particle ) const; +#endif private: /** returns true if the the particle and all daughters are on the white list */ +#ifdef HEPMC3 + bool pass(HepMC::ConstGenParticlePtr particle , std::vector<int> & used_vertices ) const; +#else bool pass(const HepMC::GenParticle& particle , std::vector<int> & used_vertices ) const; +#endif std::vector<std::string> m_whiteLists; //!< The location of the white lists std::vector<long int> m_pdgId; //!< Allowed PDG IDs bool m_qs; //!< Switch for quasi-stable particle simulation -- GitLab