Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-28 09:20:47

0001 // SPDX-License-Identifier: LGPL-3.0-or-later
0002 // Copyright (C) 2025 Wouter Deconinck
0003 
0004 #pragma once
0005 
0006 #include <algorithms/algorithm.h>
0007 #include <edm4eic/MCRecoParticleAssociationCollection.h>
0008 #include <edm4eic/ReconstructedParticleCollection.h>
0009 #include <edm4hep/MCParticleCollection.h>
0010 #include <atomic>
0011 #include <mutex>
0012 #include <stdint.h>
0013 #include <string>
0014 #include <string_view>
0015 #include <edm4eic/TruthinessCollection.h>
0016 
0017 #include "TruthinessConfig.h"
0018 #include "algorithms/interfaces/WithPodConfig.h"
0019 
0020 namespace eicrecon {
0021 
0022 using TruthinessAlgorithm = algorithms::Algorithm<
0023     algorithms::Input<edm4hep::MCParticleCollection, edm4eic::ReconstructedParticleCollection,
0024                       edm4eic::MCRecoParticleAssociationCollection>,
0025     algorithms::Output<edm4eic::TruthinessCollection>>;
0026 
0027 /**
0028  * Algorithm computing a "truthiness" metric that quantifies how well reconstructed
0029  * particles agree with the Monte Carlo (MC) truth information.
0030  *
0031  * The truthiness value is a scalar penalty score built from several contributions,
0032  * such as:
0033  *   - energy penalties for mismatches between reconstructed and MC energies,
0034  *   - momentum penalties for discrepancies in reconstructed vs MC momentum,
0035  *   - penalties for PID (particle identification) mismatches between reco and MC,
0036  *   - penalties for unassociated reconstructed particles or MC particles.
0037  *
0038  * These contributions are combined into a single number per event (and/or per
0039  * object, depending on configuration). Lower truthiness values indicate better
0040  * agreement between reconstruction and truth, while higher values indicate larger
0041  * or more confident disagreement — i.e. "more truthiness is more confidently
0042  * wrong". This class also keeps simple running statistics (average truthiness and
0043  * number of processed events) for monitoring purposes.
0044  */
0045 class Truthiness : public TruthinessAlgorithm, public WithPodConfig<TruthinessConfig> {
0046 
0047 private:
0048   mutable double m_average_truthiness{0.0};
0049   mutable std::atomic<uint64_t> m_event_count{0};
0050   mutable std::mutex m_stats_mutex;
0051 
0052 public:
0053   Truthiness(std::string_view name)
0054       : TruthinessAlgorithm{
0055             name,
0056             {"inputMCParticles", "inputReconstructedParticles", "inputAssociations"},
0057             {"outputTruthiness"},
0058             "Calculate truthiness metric comparing reconstructed particles to MC "
0059             "truth."} {}
0060 
0061   void init() final {}
0062   void process(const Input&, const Output&) const final;
0063 
0064   // Accessors for statistics
0065   double getAverageTruthiness() const {
0066     std::lock_guard<std::mutex> lock(m_stats_mutex);
0067     return m_average_truthiness;
0068   }
0069   uint64_t getEventCount() const { return m_event_count.load(); }
0070 };
0071 
0072 } // namespace eicrecon