ATLAS Offline Software
Loading...
Searching...
No Matches
TruthParticleHitCountAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7#include "Acts/Utilities/Enumerate.hpp"
8
11#include <iomanip>
12#include <cmath>
13#include <type_traits>
14#include <typeinfo>
15#include <numeric>
16#include <ranges>
17
18namespace ActsTrk
19{
20 // to dump
21 inline MsgStream &operator<<(MsgStream &out, const ActsUtils::Stat &stat) {
22 ActsUtils::dumpStat(out, stat);
23 return out;
24 }
25
27 ATH_CHECK( m_ctxProvider.initialize() );
28 ATH_CHECK( m_measAssocKeys.initialize());
29 ATH_CHECK( m_truthHitCountsOut.initialize() );
30
31 m_elasticDecayUtil.setEnergyLossBinning(m_energyLossBinning.value());
32 return StatusCode::SUCCESS;
33 }
34
35 template <bool IsDebug>
36 template <class T_OutStream>
38 if constexpr(IsDebug) {
39 out << "Measurements per truth particle :" << m_measPerTruthParticle << std::endl
40 << m_measPerTruthParticle.histogramToString();
41 }
42 }
43 template <bool IsDebug>
44 void inline TruthParticleHitCountAlg::AssociationCounter<IsDebug>::fillStatistics(unsigned int n_measurements) const {
45 if constexpr(IsDebug) {
46 std::lock_guard<std::mutex> lock(m_mutex);
47 m_measPerTruthParticle.add(n_measurements);
48 }
49 }
50
52 {
53 ATH_MSG_INFO("Number of truth particles with hits : " << m_nTruthParticlesWithHits);
54 if (msgLvl(MSG::INFO)) {
56 m_elasticDecayUtil.dumpStatistics(msg());
57 msg() << std::endl;
58 m_associationCounter.dumpStatistics(msg());
59 }
60 msg() << endmsg;
61 }
62 return StatusCode::SUCCESS;
63 }
64
65 StatusCode TruthParticleHitCountAlg::execute(const EventContext &ctx) const {
66 auto truth_particle_hit_counts = std::make_unique<TruthParticleHitCounts>();
67
68 const Acts::GeometryContext tgContext = m_ctxProvider.getGeometryContext(ctx);
69 using AssocMapVec_t = std::vector<const ActsTrk::MeasurementToTruthParticleAssociation *>;
70 std::array<AssocMapVec_t, Acts::toUnderlying(xAOD::UncalibMeasType::nTypes)>
71 measurement_to_truth_association_maps{};
72 for (const auto& key : m_measAssocKeys) {
74 ATH_CHECK(SG::get(assocMap, key, ctx));
75 if (assocMap->empty()) {
76 continue;
77 }
78 const auto typeIdx = Acts::toUnderlying(assocMap->sourceContainer()->front()->type());
79 measurement_to_truth_association_maps[typeIdx].push_back(assocMap);
80 }
81
82 for(const auto&[measurement_type_i, mapsPerType]: Acts::enumerate(measurement_to_truth_association_maps) ) {
83 for (const ActsTrk::MeasurementToTruthParticleAssociation* associated_truth_particles : mapsPerType) {
84 for (const ActsTrk::ParticleVector &truth_particles : *associated_truth_particles) {
85 for (const xAOD::TruthParticle *truth_particle : truth_particles) {
86 assert (truth_particle);
87 const xAOD::TruthParticle *mother_particle = m_elasticDecayUtil.getMother(*truth_particle, m_maxEnergyLoss.value());
88 if (mother_particle) {
89 ++(*truth_particle_hit_counts)[mother_particle][measurement_type_i];
90 }
91 }
92 }
93 }
94 }
95 unsigned int n_hits_min = m_nHitsMin.value();
96 unsigned int truth_particles_without_enough_measurements
97 = std::erase_if( *truth_particle_hit_counts,
98 [this, n_hits_min](const std::pair<const xAOD::TruthParticle * const,HitCounterArray> &elm) {
99 unsigned int n_measurements=std::accumulate(elm.second.begin(), elm.second.end(),0u);
100 this->m_associationCounter.fillStatistics(n_measurements);
101 return n_measurements<n_hits_min;
102 });
103
104 m_nTruthParticlesWithHits += truth_particle_hit_counts->size();
105
106 ATH_MSG_DEBUG("Truth particles with hits:" << truth_particle_hit_counts->size()
107 << ", without enough hits: " << truth_particles_without_enough_measurements);
108
109 SG::WriteHandle<TruthParticleHitCounts> truth_particle_hit_counts_out_handle(m_truthHitCountsOut, ctx);
110 if (truth_particle_hit_counts_out_handle.record( std::move(truth_particle_hit_counts)).isFailure()) {
111 ATH_MSG_ERROR("Failed to record track to truth assocition with key " << m_truthHitCountsOut.key() );
112 return StatusCode::FAILURE;
113 }
114
115 return StatusCode::SUCCESS;
116 }
117
118} // namespace
#define endmsg
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_DEBUG(x,...)
#define ATH_MSG_ERROR(x,...)
#define ATH_MSG_INFO(x,...)
virtual void lock()=0
Interface to allow an object to lock itself when made const in SG.
const xAOD::UncalibratedMeasurementContainer * sourceContainer() const
ElasticDecayUtil< TruthParticleHitCountDebugHists > m_elasticDecayUtil
Gaudi::Property< std::vector< float > > m_energyLossBinning
Gaudi::Property< unsigned int > m_nHitsMin
ContextUtility m_ctxProvider
Context provider for geometry, magnetic field and calibration contexts.
virtual StatusCode execute(const EventContext &ctx) const override
SG::WriteHandleKey< TruthParticleHitCounts > m_truthHitCountsOut
SG::ReadHandleKeyArray< MeasurementToTruthParticleAssociation > m_measAssocKeys
virtual StatusCode initialize() override
AssociationCounter< TruthParticleHitCountDebugHists > m_associationCounter
bool msgLvl(const MSG::Level lvl) const
const T * front() const
Access the first element in the collection as an rvalue.
virtual xAOD::UncalibMeasType type() const =0
Returns the type of the measurement type as a simple enumeration.
The AlignStoreProviderAlg loads the rigid alignment corrections and pipes them through the readout ge...
std::ostream & operator<<(std::ostream &ostr, const DetectorType type)
Pipe the detector type to an outstream object.
boost::container::small_vector< const xAOD::TruthParticle *, NTruthParticlesPerMeasurement > ParticleVector
constexpr bool TruthParticleHitCountDebugHists
void dumpStat(T_Stream &out, const Stat &stat)
Dump the given statistics object to the given output stream.
Definition StatUtils.h:63
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.
TruthParticle_v1 TruthParticle
Typedef to implementation.