diff --git a/Calo/CaloAssociators/src/CaloClusterEfficiency.cpp b/Calo/CaloAssociators/src/CaloClusterEfficiency.cpp index ab10ab810b70a03a06c4683d5ed55b3cac8b6e8c..de147a77362c86a2fc5d6acf5abf7496db8bd67d 100644 --- a/Calo/CaloAssociators/src/CaloClusterEfficiency.cpp +++ b/Calo/CaloAssociators/src/CaloClusterEfficiency.cpp @@ -69,7 +69,9 @@ namespace LHCb::Calo::Algorithms { const LHCb::Event::Calo::v2::Clusters& clusters ) const override { const auto cluster_index = clusters.index(); - if ( !cluster_index ) { /* OOPS */ + if ( !cluster_index ) { + ++m_canNotIndexClusters; + return; } // tuple @@ -128,9 +130,8 @@ namespace LHCb::Calo::Algorithms { m_eff += is_reconstructed; continue; } - auto cl = *it; // get cluster from Iterator using operator* - auto cl_pos = cl.position(); - auto cl_e = cl.e(); + auto cl_pos = it->position(); + auto cl_e = it->e(); // fraction of cluster energy from this MCParticle float f_cl = ( cl_e > 0. ) ? weight / cl_e : 0.; @@ -177,6 +178,8 @@ namespace LHCb::Calo::Algorithms { mutable Gaudi::Accumulators::MsgCounter m_multVtx{this, "More than 1 endVertex found for signal"}; mutable Gaudi::Accumulators::MsgCounter m_clNotFound{ this, "CellID from relations table not found in clusters"}; + mutable Gaudi::Accumulators::MsgCounter m_canNotIndexClusters{this, + "Failed to index cluster container"}; bool check_endVtx( LHCb::MCParticle* mcp, Tuple tuple ) const { // get endVertices @@ -223,10 +226,9 @@ namespace LHCb::Calo::Algorithms { // ========================================================================== }; + DECLARE_COMPONENT_WITH_ID( ClusterEfficiency, "CaloClusterEfficiency" ) } // namespace LHCb::Calo::Algorithms -DECLARE_COMPONENT_WITH_ID( LHCb::Calo::Algorithms::ClusterEfficiency, "CaloClusterEfficiency" ) - // ============================================================================ // The END // ============================================================================ diff --git a/Calo/CaloAssociators/src/CaloClusterResolution.cpp b/Calo/CaloAssociators/src/CaloClusterResolution.cpp index 47c5d035068b4370c9a420aba87c3556af35f363..e351eea3410df85f7fbf431004a7712415be69b5 100644 --- a/Calo/CaloAssociators/src/CaloClusterResolution.cpp +++ b/Calo/CaloAssociators/src/CaloClusterResolution.cpp @@ -15,6 +15,7 @@ #include "Event/CaloCluster.h" #include "Event/CaloClusters_v2.h" #include "Event/CaloDataFunctor.h" +#include "Event/CaloHypos_v2.h" #include "Event/MCParticle.h" #include "GaudiAlg/Consumer.h" #include "GaudiAlg/GaudiTupleAlg.h" @@ -92,6 +93,10 @@ namespace { return in.position().z(); } + std::optional RetrieveZPosition( LHCb::Event::Calo::Hypotheses::const_reference in ) { + return RetrieveZPosition( in.clusters().front() ); + } + std::optional RetrieveZPosition( const LHCb::CaloHypo* in ) { const auto* pos = in->position(); // pi0->gamma gamma special treatment: return hypo of first gamma @@ -110,6 +115,10 @@ namespace { return {in.position().x(), in.position().y(), in.e()}; } + LHCb::CaloPosition::Parameters RetrieveParameters( LHCb::Event::Calo::Hypotheses::const_reference in ) { + return RetrieveParameters( in.clusters().front() ); + } + LHCb::CaloPosition::Parameters RetrieveParameters( const LHCb::CaloHypo* in ) { const auto* pos = in->position(); if ( !pos ) { @@ -239,6 +248,7 @@ namespace LHCb::Calo { DECLARE_COMPONENT_WITH_ID( ClusterResolution, "CaloClusterResolution" ) DECLARE_COMPONENT_WITH_ID( ClusterResolution, "CaloFutureClusterResolution" ) DECLARE_COMPONENT_WITH_ID( ClusterResolution, "CaloHypoResolution" ) + DECLARE_COMPONENT_WITH_ID( ClusterResolution, "CaloFutureHypoResolution" ) } // namespace LHCb::Calo // ============================================================================ // The END diff --git a/Calo/CaloAssociators/src/CaloDigit2MCLinks2Table.cpp b/Calo/CaloAssociators/src/CaloDigit2MCLinks2Table.cpp index 5b17b685957cdcde83df1cc30b7ce8ea047de56c..1e96fd3ea048c1f74b35db9d78942c9d26900796 100644 --- a/Calo/CaloAssociators/src/CaloDigit2MCLinks2Table.cpp +++ b/Calo/CaloAssociators/src/CaloDigit2MCLinks2Table.cpp @@ -57,7 +57,7 @@ LHCb::Calo2MC::DigitTable CaloDigit2MCLinks2Table::operator()( const LHCb::CaloD // loop over all digits for ( const auto* digit : digits ) { - if ( 0 == digit ) { continue; } + if ( !digit ) continue; // use the auxillary container to be immune agains // potential bugs (if any..), like duplicated entries, etc diff --git a/Calo/CaloAssociators/src/CaloDigitMCTruth.cpp b/Calo/CaloAssociators/src/CaloDigitMCTruth.cpp index 9df93ba03d1b174b2055c207b529a021f4f27b27..129a2f103179cf338044e4e72c0e0423331798a7 100644 --- a/Calo/CaloAssociators/src/CaloDigitMCTruth.cpp +++ b/Calo/CaloAssociators/src/CaloDigitMCTruth.cpp @@ -18,7 +18,6 @@ #include "GaudiAlg/GaudiAlgorithm.h" #include "Kernel/CaloCellIDKeyTraits.h" #include "Linker/LinkerWithKey.h" -// // ============================================================================ /** @class CaloDigitMCTruth CaloDigitMCTruth.cpp @@ -152,8 +151,6 @@ StatusCode CaloDigitMCTruth::execute() { typedef Over_Et_Threshold OverET; /// helper class to build MC history tree typedef MCCaloHistory MCHistory; - /// auxillary structure for conversion "CaloCellID" -> "int" - // typedef Containers::key_traits Key ; // get digits from TES Digits* digits = getIfExists( m_input ); @@ -165,10 +162,8 @@ StatusCode CaloDigitMCTruth::execute() { // scale factor for recalculation of eActive into eTotal const double activeToTotal = detector->activeToTotal(); - { // check the availability of the global MC truth - LHCb::MCCaloDigits* _mc = mcTruth( digits ); - if ( 0 == _mc ) { Error( "MCCaloDigits* point to NULL" ).ignore(); } - } + // check the availability of the global MC truth + if ( !mcTruth( digits ) ) { Error( "MCCaloDigits* point to NULL" ).ignore(); } // create the Linker object LHCb::Calo2MC::DigitLink linker( eventSvc(), msgSvc(), m_input ); @@ -182,9 +177,8 @@ StatusCode CaloDigitMCTruth::execute() { unsigned long nLinks = 0; // loop over all digits - for ( Digits::const_iterator idigit = digits->begin(); digits->end() != idigit; ++idigit ) { - Digit* digit = *idigit; - if ( 0 == digit ) { continue; } // skip invalid + for ( const Digit* digit : *digits ) { + if ( !digit ) { continue; } // skip invalid // skip extra small energy depositions if ( !overE( digit ) && !overET( digit ) ) { continue; } @@ -205,28 +199,26 @@ StatusCode CaloDigitMCTruth::execute() { // copy history map into the separate container CaloMCMap map2( map1 ); - { // update the copy of history map - for ( CaloMCMap::iterator entry = map1.begin(); map1.end() != entry; ++entry ) { - const LHCb::MCParticle* particle = entry->first; - const double energy = entry->second; - StatusCode up = updateCaloMCMap( particle, energy, map2 ); - if ( up.isFailure() ) warning() << "updateCaloMCMap FAILED " << endmsg; - } + // update the copy of history map + for ( const auto& [particle, energy] : map1 ) { + updateCaloMCMap( particle, energy, map2 ) + .orElse( [&] { warning() << "updateCaloMCMap FAILED " << endmsg; } ) + .ignore(); } // threshold value const double threshold = m_minFr * digit->e(); // fill the relation table - for ( CaloMCMap::iterator entry = map2.begin(); map2.end() != entry; ++entry ) { - const LHCb::MCParticle* particle = entry->first; - if ( 0 == particle ) { continue; } + for ( auto [particle, energy] : map2 ) { + if ( !particle ) { continue; } + + // cumulative energy deposition from this particle + energy *= activeToTotal; // use only more or less "energetic" particles if ( particle->momentum().e() < m_minPE ) { continue; } - // cumulative energy deposition from this particle - const double energy = activeToTotal * entry->second; // skip very small energy depositions if ( energy < m_minE && energy * sinTheta < m_minET && energy < threshold ) { continue; } diff --git a/Calo/CaloAssociators/src/CaloFutureClusterMCTruth.cpp b/Calo/CaloAssociators/src/CaloFutureClusterMCTruth.cpp index 2674103767f3e5158e1c77b58b7101b78e111631..cc72a372a3353d2cc34a1eeb384dd31a74ed3c11 100644 --- a/Calo/CaloAssociators/src/CaloFutureClusterMCTruth.cpp +++ b/Calo/CaloAssociators/src/CaloFutureClusterMCTruth.cpp @@ -46,7 +46,7 @@ */ namespace LHCb::Calo::Associators { class Cluster2MCParticle : public Gaudi::Functional::Transformer { + const LHCb::CaloFuture2MC::DigitTable&, const LHCb::Event::Calo::Clusters& )> { // ========================================================================== // set the appropriate default values for input data Gaudi::Property m_sFilter{this, "StatusFilter", static_cast( LHCb::CaloDigitStatus::Mask::UseForEnergy )}; @@ -72,7 +72,7 @@ namespace LHCb::Calo::Associators { /// algorithm execution LHCb::CaloFuture2MC::ClusterTable operator()( const LHCb::CaloFuture2MC::DigitTable& digTable, - const LHCb::CaloClusters& clusters ) const override { + const LHCb::Event::Calo::Clusters& clusters ) const override { using namespace LHCb::CaloDataFunctor; using namespace CaloMCTools; @@ -83,19 +83,20 @@ namespace LHCb::Calo::Associators { for ( const auto& cluster : clusters ) { // auxillary container to collect all links from individual digits - CaloMCMap mcMap; + std::map mcMap; // loop over all digits in the cluster and collect // the links from individual digits - for ( const auto& entry : cluster->entries() ) { + for ( const auto& entry : cluster.entries() ) { if ( m_sFilter >= 0 && entry.status().noneOf( LHCb::CaloDigitStatus::Status( m_sFilter ) ) ) continue; // get all MC relations from this entry / digit // loop over all MC entries and collect the enegy - for ( const auto& [_, particle, energy] : digTable.relations( entry.digit()->cellID() ) ) { + for ( const auto& [_, particle, energy] : digTable.relations( entry.cellID() ) ) { // accumulate the energy from the same particle - mcMap[particle] += energy; + auto [it, ok] = mcMap.try_emplace( particle, energy ); + if ( !ok ) it->second += energy; } } // end of loop over all entries in Cluster @@ -107,10 +108,10 @@ namespace LHCb::Calo::Associators { // MC particle & its cumulative energy deposition to the cluster for ( const auto& [particle, energy] : mcMap ) { if ( !particle ) { continue; } - if ( cluster->e() > 0 && energy < m_wFilter * cluster->e() ) { continue; } + if ( cluster.e() > 0 && energy < m_wFilter * cluster.e() ) { continue; } // fill the relation table: ATTENTION "i_push" is used! - table.i_push( cluster->seed(), particle, energy ); // NB: "i_push" + table.i_push( cluster.cellID(), particle, energy ); // NB: "i_push" } } // end of loop over clusters diff --git a/Calo/CaloAssociators/src/CaloFutureDigit2MCLinks2Table.cpp b/Calo/CaloAssociators/src/CaloFutureDigit2MCLinks2Table.cpp index 1e9ad00554ea1e86d88e285849860d33b43cfe49..48fde345e2896b9c80e14b5cd798a48ce8830832 100644 --- a/Calo/CaloAssociators/src/CaloFutureDigit2MCLinks2Table.cpp +++ b/Calo/CaloAssociators/src/CaloFutureDigit2MCLinks2Table.cpp @@ -50,36 +50,31 @@ namespace LHCb::Calo::Associators { // create the relation table LHCb::CaloFuture2MC::DigitTable table{3000}; - // loop over all digits for ( const auto& digit : digits ) { - // use the auxillary container to be immune agains // potential bugs (if any..), like duplicated entries, etc // in the Linkers - CaloMCTools::CaloMCMap mcMap; + std::map mcMap; links.applyToLinks( Containers::key_traits::identifier( digit.cellID() ), [&mcMap, &mcParticles]( unsigned int, unsigned int tgtIndex, float weight ) { - // use Linker const LHCb::MCParticle* particle = static_cast( mcParticles.containedObject( tgtIndex ) ); - if ( particle ) mcMap[particle] += weight; // update the auxillary container + auto [it, ok] = mcMap.try_emplace( particle, weight ); + if ( !ok ) it->second += weight; } ); - // convert the auxillary container into Relation Table + // copy contents of the auxillary container into Relation Table for ( auto [particle, energy] : mcMap ) { // use fast i_push method! table.i_push( digit.cellID(), particle, energy ); // NB !! } - - } // end of the loop over digits in the container + } // mandatory operation after "i_push"! table.i_sort(); // NB !! m_emptyTable += table.relations().empty(); - m_cd2mcLinkscounter += table.relations().size(); - return table; } }; diff --git a/Calo/CaloAssociators/src/CaloFutureHypoMCTruth.cpp b/Calo/CaloAssociators/src/CaloFutureHypoMCTruth.cpp index d1a0a38d40d1f6b2d0fc2f82936e4bea0893e571..91398bcc6d0e953c559c1d0ed00f1d576689ff30 100644 --- a/Calo/CaloAssociators/src/CaloFutureHypoMCTruth.cpp +++ b/Calo/CaloAssociators/src/CaloFutureHypoMCTruth.cpp @@ -10,9 +10,9 @@ \*****************************************************************************/ #include "CaloFutureUtils/CaloFuture2MC.h" #include "CaloUtils/CaloAlgUtils.h" -#include "Event/CaloHypo.h" +#include "Event/CaloHypos_v2.h" #include "Event/MCParticle.h" -#include "GaudiAlg/GaudiAlgorithm.h" +#include "GaudiAlg/Transformer.h" #include "Linker/LinkerWithKey.h" #include "Relations/RelationWeighted1D.h" @@ -26,63 +26,56 @@ */ namespace LHCb::Calo::Associators { - class Hypo2MCParticle : public GaudiAlgorithm { + using namespace LHCb::CaloFuture2MC; + + class Hypo2MCParticle + : public Gaudi::Functional::Transformer { public: - /// Standard constructor Hypo2MCParticle( const std::string& name, ISvcLocator* pSvcLocator ); + HypoTable operator()( const LHCb::Event::Calo::Hypotheses&, const ClusterTable& ) const override; - StatusCode execute() override; ///< Algorithm execution private: - Gaudi::Property m_cluster2MCLoc{this, "Cluster2MCTable", - "Relations/" + LHCb::CaloClusterLocation::Default}; - Gaudi::Property> m_inputs{this, "CaloHypos", {}}; ///< input data + mutable Gaudi::Accumulators::AveragingCounter m_links_per_hypo{this, "#links/hypo"}; + mutable Gaudi::Accumulators::AveragingCounter m_links{this, "#links"}; }; - Hypo2MCParticle::Hypo2MCParticle( const std::string& name, ISvcLocator* pSvc ) : GaudiAlgorithm( name, pSvc ) { - using namespace LHCb::CaloHypoLocation; - m_inputs.value() = {LHCb::CaloAlgUtils::PathFromContext( context(), Photons ), - LHCb::CaloAlgUtils::PathFromContext( context(), Electrons ), - LHCb::CaloAlgUtils::PathFromContext( context(), MergedPi0s ), - LHCb::CaloAlgUtils::PathFromContext( context(), SplitPhotons )}; - } - // ============================================================================ - - // ============================================================================ - // declare algorithm factory - // ============================================================================ DECLARE_COMPONENT_WITH_ID( Hypo2MCParticle, "CaloFutureHypoMCTruth" ) - // ============================================================================ // ============================================================================ - /// algorithm execution + + Hypo2MCParticle::Hypo2MCParticle( const std::string& name, ISvcLocator* pSvc ) + : Transformer{name, + pSvc, + {KeyValue{"InputHypos", ""}, + KeyValue{"InputClusterTable", "Relations/" + LHCb::CaloClusterLocation::Default}}, + KeyValue{"OutputTable", ""}} {} + // ============================================================================ - StatusCode Hypo2MCParticle::execute() { - //--- load cluster->MC - const auto* cluster2MC = getIfExists( m_cluster2MCLoc ); - if ( !cluster2MC ) - return Warning( " Cluster <-> MC relation table not found at " + m_cluster2MCLoc, StatusCode::FAILURE ); - for ( const auto& container : m_inputs ) { - LHCb::CaloFuture2MC::HypoLink linker( eventSvc(), msgSvc(), container ); - const LHCb::CaloHypos* hypos = getIfExists( container ); - if ( !hypos ) { - counter( "No container " + container ) += 1; - continue; - } - int links = 0; - for ( const auto* hypo : *hypos ) { - const LHCb::CaloCluster* cluster = LHCb::CaloAlgUtils::ClusterFromHypo( hypo ); - int hlinks = 0; - for ( const auto& ir : cluster2MC->relations( cluster->seed() ) ) { - linker.link( hypo, ir.to(), ir.weight() ); - hlinks++; - links++; + HypoTable Hypo2MCParticle::operator()( const LHCb::Event::Calo::Hypotheses& hypos, + const ClusterTable& cluster2MC ) const { + + HypoTable out; + int links = 0; + for ( const auto& hypo : hypos ) { + std::map map; + for ( const auto& cluster : hypo.clusters() ) { + for ( const auto& ir : cluster2MC.relations( cluster.cellID() ) ) { + auto [it, ok] = map.try_emplace( ir.to(), ir.weight() ); + if ( !ok ) it->second += ir.weight(); } - counter( "#links/hypo " + container ) += hlinks; } - counter( "#links " + container ) += links; + int hlinks = 0; + for ( const auto& [particle, weight] : map ) { + out.i_push( hypo.cellID(), particle, weight ); + ++hlinks; + ++links; + } + m_links_per_hypo += hlinks; } - return StatusCode::SUCCESS; + m_links += links; + out.i_sort(); + return out; } } // namespace LHCb::Calo::Associators // ============================================================================ diff --git a/Calo/CaloAssociators/src/CaloHypo2Tracks.cpp b/Calo/CaloAssociators/src/CaloHypo2Tracks.cpp index 3d60ca68540f70a15b1a7aa3eefe739c50b00934..8d9b9a7cb0e259ac97f4833552d917287586c825 100644 --- a/Calo/CaloAssociators/src/CaloHypo2Tracks.cpp +++ b/Calo/CaloAssociators/src/CaloHypo2Tracks.cpp @@ -13,8 +13,8 @@ #include "Relations/RelationWeighted2D.h" -#include "Event/CaloCluster.h" -#include "Event/CaloHypo.h" +#include "Event/CaloClusters_v2.h" +#include "Event/CaloHypos_v2.h" #include "Event/Track.h" #include "GaudiAlg/Transformer.h" @@ -30,31 +30,30 @@ namespace LHCb::Calo::Asssociators { - using TrackMatchTable = RelationWeighted2D; + using TrackMatchTable = RelationWeighted2D; - struct CaloHypo2Tracks : Gaudi::Functional::Transformer { + struct Hypo2Tracks + : Gaudi::Functional::Transformer { Gaudi::Property m_electrChi2Cut{this, "ElectrMaxChi2", 0., "Threshold on maximum electron cluster track match chi2"}; - CaloHypo2Tracks( const std::string& name, ISvcLocator* pSvc ) + Hypo2Tracks( const std::string& name, ISvcLocator* pSvc ) : Transformer( name, pSvc, - {KeyValue( "InputHypos", "" ), KeyValue( "InputTable", {CaloFutureIdLocation::ClusterMatch} )}, + {KeyValue( "InputHypos", "" ), KeyValue( "InputTable", CaloFutureIdLocation::ClusterMatch )}, {KeyValue( "OutputTracks", "" )} ) {} - LHCb::Tracks operator()( const CaloHypos& hypos, const TrackMatchTable& table ) const override { - LHCb::Tracks tracks; - for ( auto* hypo : hypos ) { - for ( auto& cluster : hypo->clusters() ) { - for ( auto& entry : table.relations( cluster, m_electrChi2Cut, false ) ) { - auto track = std::make_unique( *entry.to() ); - tracks.insert( track.release() ); + Tracks operator()( const Event::Calo::Hypotheses& hypos, const TrackMatchTable& table ) const override { + Tracks tracks; + for ( const auto& hypo : hypos ) { + for ( const auto& cluster : hypo.clusters() ) { + for ( auto& entry : table.relations( cluster.cellID(), m_electrChi2Cut, false ) ) { + if ( !tracks.object( entry.to()->key() ) ) tracks.insert( entry.to() ); } } } return tracks; } }; + DECLARE_COMPONENT( Hypo2Tracks ) } // namespace LHCb::Calo::Asssociators - -DECLARE_COMPONENT( LHCb::Calo::Asssociators::CaloHypo2Tracks ) diff --git a/Calo/CaloAssociators/src/CaloHypoMCTruth.cpp b/Calo/CaloAssociators/src/CaloHypoMCTruth.cpp index 2729f4833ed16f7c001512330a7d2d614e5b75a7..b70870a67ccd0569994a41a9ac89770bf2fb5102 100644 --- a/Calo/CaloAssociators/src/CaloHypoMCTruth.cpp +++ b/Calo/CaloAssociators/src/CaloHypoMCTruth.cpp @@ -61,7 +61,7 @@ std::tuple CaloHypoMCTruth:: LHCb::LinksByKey linksByKey; linksByKey.setSourceClassID( LHCb::CaloHypo::classID() ); linksByKey.setTargetClassID( LHCb::MCParticle::classID() ); - for ( const LHCb::CaloHypo* hypo : hypos ) { + for ( const auto hypo : hypos ) { int hlinks = 0; for ( const auto& ir : cluster2MC.relations( LHCb::CaloAlgUtils::ClusterFromHypo( hypo ) ) ) { if ( hypo && ir.to() ) {