From 595710b3964335fd29379d2cdcda70996b77b1c6 Mon Sep 17 00:00:00 2001 From: Chris Jones <jonesc@hep.phy.cam.ac.uk> Date: Mon, 23 Oct 2023 14:52:28 +0100 Subject: [PATCH] RichMCOpticalPhotonUtils: New accessor for MC photons associated to a given MCParticle --- .../RichMCOpticalPhotonUtils.h | 33 ++++++++++++++----- .../src/lib/RichMCOpticalPhotonUtils.cpp | 16 ++++++--- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/Rich/RichFutureMCUtils/include/RichFutureMCUtils/RichMCOpticalPhotonUtils.h b/Rich/RichFutureMCUtils/include/RichFutureMCUtils/RichMCOpticalPhotonUtils.h index 78cde1194f8..8646ebeaea6 100644 --- a/Rich/RichFutureMCUtils/include/RichFutureMCUtils/RichMCOpticalPhotonUtils.h +++ b/Rich/RichFutureMCUtils/include/RichFutureMCUtils/RichMCOpticalPhotonUtils.h @@ -15,6 +15,8 @@ #include "Kernel/RichSmartID.h" // Event model +#include "Event/MCParticle.h" +#include "Event/MCRichHit.h" #include "Event/MCRichOpticalPhoton.h" // Rich Utils @@ -61,31 +63,46 @@ namespace Rich::Future::MC::Relations { // Get the list of all MC photons associated to the hits auto mcPhots = mcOpticalPhotons( hits ); // filter out those not linked to mcPs - const auto it = std::remove_if( mcPhots.begin(), mcPhots.end(), // - [&mcPs]( auto&& phot ) { - // Get the MCParticle associated to this photon - const auto mcP = - ( phot && phot->mcRichHit() ? phot->mcRichHit()->mcParticle() : nullptr ); - // if MCP not in supplied list return true to erase - return ( !mcP || std::find( mcPs.begin(), mcPs.end(), mcP ) == mcPs.end() ); - } ); + const auto it = // + std::remove_if( mcPhots.begin(), mcPhots.end(), // + [&mcPs]( auto&& phot ) { + // Get the MCParticle associated to this photon + const auto mcH = ( phot ? phot->mcRichHit() : nullptr ); + const auto mcP = ( mcH ? mcH->mcParticle() : nullptr ); + // if MCP not in supplied list return true to erase + return ( !mcP || std::find( mcPs.begin(), mcPs.end(), mcP ) == mcPs.end() ); + } ); // remove the entries selected for deletion mcPhots.erase( it, mcPhots.end() ); // return return mcPhots; } + /// Get MCRichOpticalPhotons for a given MCParticle + const auto& mcOpticalPhotons( const LHCb::MCParticle* mcP ) const { + // Null entry for when missing + static LHCb::MCRichOpticalPhoton::ConstVector missing; + // Do we have this MCP in the map + const auto mcOPs = m_mcpToPhots.find( mcP ); + // return + return ( mcOPs != m_mcpToPhots.end() ? mcOPs->second : missing ); + } + private: // types /// Type for mapping from MCRichHit to MCRichOpticalPhoton using MCRichHitToOpPhot = Rich::Map<const LHCb::MCRichHit*, const LHCb::MCRichOpticalPhoton*>; + /// Type for mapping MCParticle to MCRichOpticalPhotons + using MCPartToOpPhots = Rich::Map<const LHCb::MCParticle*, LHCb::MCRichOpticalPhoton::ConstVector>; private: // cached data /// Mapping of hits to photons MCRichHitToOpPhot m_hitToPhot; + /// Mapping of MCParticles to photons + MCPartToOpPhots m_mcpToPhots; }; } // namespace Rich::Future::MC::Relations diff --git a/Rich/RichFutureMCUtils/src/lib/RichMCOpticalPhotonUtils.cpp b/Rich/RichFutureMCUtils/src/lib/RichMCOpticalPhotonUtils.cpp index 158876af12a..2999f0181da 100644 --- a/Rich/RichFutureMCUtils/src/lib/RichMCOpticalPhotonUtils.cpp +++ b/Rich/RichFutureMCUtils/src/lib/RichMCOpticalPhotonUtils.cpp @@ -9,15 +9,23 @@ * or submit itself to any jurisdiction. * \*****************************************************************************/ -// local #include "RichFutureMCUtils/RichMCOpticalPhotonUtils.h" using namespace Rich::Future::MC::Relations; MCOpticalPhotonUtils::MCOpticalPhotonUtils( const LHCb::MCRichOpticalPhotons& mcphotons ) { - // loop over mc photons for ( const auto mcphot : mcphotons ) { - // save hit -> phot link - if ( mcphot && mcphot->mcRichHit() ) { m_hitToPhot[mcphot->mcRichHit()] = mcphot; } + if ( mcphot ) { + const auto mcH = mcphot->mcRichHit(); + if ( mcH ) { + m_hitToPhot[mcH] = mcphot; + const auto mcP = mcH->mcParticle(); + if ( mcP ) { + auto& phots = m_mcpToPhots[mcP]; + if ( phots.empty() ) { phots.reserve( 40 ); } + phots.push_back( mcphot ); + } + } + } } } -- GitLab