ATLAS Offline Software
Loading...
Searching...
No Matches
TruthParticleHitCountAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
6
9#include <iomanip>
10#include <cmath>
11#include <type_traits>
12#include <typeinfo>
13#include <numeric>
14
15// for erase_if detection i.e. >=c++20
16#include <version>
17
18#ifndef __cpp_lib_erase_if
19namespace std {
20 template <class T_container, class T_Func>
21 inline std::size_t erase_if(T_container &container, T_Func pred) {
22 std::size_t orig_size = container->size();
23 for ( typename T_container::iterator iter = container.begin();
24 iter != container.end();
25 /* increment only if nothing was erased! */ ) {
26 if (pred(*iter)) {
27 iter = erase(iter);
28 }
29 else {
30 ++iter;
31 }
32 }
33 return orig_size - container->size();
34 }
35}
36#endif
37
38namespace ActsTrk
39{
40 // to dump
41 inline MsgStream &operator<<(MsgStream &out, const ActsUtils::Stat &stat) {
42 ActsUtils::dumpStat(out, stat);
43 return out;
44 }
45
47 ISvcLocator *pSvcLocator)
48 : AthReentrantAlgorithm(name, pSvcLocator)
49 {
50 }
51
53 {
55 ATH_CHECK( m_pixelClustersToTruth.initialize() );
56 ATH_CHECK( m_stripClustersToTruth.initialize() );
57 ATH_CHECK( m_hgtdClustersToTruth.initialize(not m_hgtdClustersToTruth.empty()) );
58
59 ATH_CHECK( m_truthHitCountsOut.initialize() );
60
61 m_elasticDecayUtil.setEnergyLossBinning(m_energyLossBinning.value());
62 return StatusCode::SUCCESS;
63 }
64
65 template <bool IsDebug>
66 template <class T_OutStream>
68 if constexpr(IsDebug) {
69 out << "Measurements per truth particle :" << m_measPerTruthParticle << std::endl
70 << m_measPerTruthParticle.histogramToString();
71 }
72 }
73 template <bool IsDebug>
74 void inline TruthParticleHitCountAlg::AssociationCounter<IsDebug>::fillStatistics(unsigned int n_measurements) const {
75 if constexpr(IsDebug) {
76 std::lock_guard<std::mutex> lock(m_mutex);
77 m_measPerTruthParticle.add(n_measurements);
78 }
79 }
80
82 {
83 ATH_MSG_INFO("Number of truth particles with hits : " << m_nTruthParticlesWithHits);
84 if (msgLvl(MSG::INFO)) {
86 m_elasticDecayUtil.dumpStatistics(msg());
87 msg() << std::endl;
88 m_associationCounter.dumpStatistics(msg());
89 }
90 msg() << endmsg;
91 }
92 return StatusCode::SUCCESS;
93 }
95 StatusCode TruthParticleHitCountAlg::execute(const EventContext &ctx) const
96 {
97 std::unique_ptr<TruthParticleHitCounts>
98 truth_particle_hit_counts( std::make_unique<TruthParticleHitCounts>() );
99
100
101 const ActsTrk::MeasurementToTruthParticleAssociation* pixelClustersToTruthAssociation{nullptr};
102 ATH_CHECK(SG::get(pixelClustersToTruthAssociation, m_pixelClustersToTruth, ctx));
103
104 const ActsTrk::MeasurementToTruthParticleAssociation* stripClustersToTruthAssociation{nullptr};
105 ATH_CHECK(SG::get(stripClustersToTruthAssociation, m_stripClustersToTruth, ctx));
106
107 const ActsTrk::MeasurementToTruthParticleAssociation* hgtdClustersToTruthAssociation{nullptr};
108 ATH_CHECK(SG::get(hgtdClustersToTruthAssociation, m_hgtdClustersToTruth, ctx));
109
110
111 Acts::GeometryContext tgContext = m_trackingGeometryTool->getGeometryContext(ctx).context();
112
114 static_cast< std::underlying_type<xAOD::UncalibMeasType>::type >(xAOD::UncalibMeasType::nTypes)>
115 measurement_to_truth_association_maps{};
116
117 measurement_to_truth_association_maps[Acts::toUnderlying(xAOD::UncalibMeasType::PixelClusterType)]=pixelClustersToTruthAssociation;
118 measurement_to_truth_association_maps[Acts::toUnderlying(xAOD::UncalibMeasType::StripClusterType)]=stripClustersToTruthAssociation;
119 measurement_to_truth_association_maps[Acts::toUnderlying(xAOD::UncalibMeasType::HGTDClusterType)]=hgtdClustersToTruthAssociation;
120 auto assocSize = [&measurement_to_truth_association_maps](xAOD::UncalibMeasType type) {
121 const ActsTrk::MeasurementToTruthParticleAssociation *assoc = measurement_to_truth_association_maps[Acts::toUnderlying(type)];
122 return assoc ? assoc->size() : 0ul;
123 };
124
125 ATH_MSG_DEBUG("Measurement association entries: " << assocSize(xAOD::UncalibMeasType::PixelClusterType)
126 << " + " << assocSize(xAOD::UncalibMeasType::StripClusterType)
127 << " + " << assocSize(xAOD::UncalibMeasType::HGTDClusterType)
128 );
129
130 unsigned int measurement_type_i=0;
131 --measurement_type_i;
132 for( const ActsTrk::MeasurementToTruthParticleAssociation *associated_truth_particles : measurement_to_truth_association_maps ) {
133 ++measurement_type_i;
134 if (!associated_truth_particles) continue;
135 for (const ActsTrk::ParticleVector &truth_particles : *associated_truth_particles) {
136 for (const xAOD::TruthParticle *truth_particle : truth_particles) {
137 assert (truth_particle);
138 const xAOD::TruthParticle *mother_particle = m_elasticDecayUtil.getMother(*truth_particle, m_maxEnergyLoss.value());
139 if (mother_particle) {
140 assert(measurement_type_i < (*truth_particle_hit_counts)[mother_particle].size());
141 ++(*truth_particle_hit_counts)[mother_particle][measurement_type_i];
142 }
143 }
144 }
145 }
146 unsigned int n_hits_min = m_nHitsMin.value();
147 unsigned int truth_particles_without_enough_measurements
148 = std::erase_if( *truth_particle_hit_counts,
149 [this, n_hits_min](const std::pair<const xAOD::TruthParticle * const,HitCounterArray> &elm) {
150 unsigned int n_measurements=std::accumulate(elm.second.begin(), elm.second.end(),0u);
151 this->m_associationCounter.fillStatistics(n_measurements);
152 return n_measurements<n_hits_min;
153 });
154
155 m_nTruthParticlesWithHits += truth_particle_hit_counts->size();
156
157 ATH_MSG_DEBUG("Truth particles with hits:" << truth_particle_hit_counts->size()
158 << ", without enough hits: " << truth_particles_without_enough_measurements);
159
160 SG::WriteHandle<TruthParticleHitCounts> truth_particle_hit_counts_out_handle(m_truthHitCountsOut, ctx);
161 if (truth_particle_hit_counts_out_handle.record( std::move(truth_particle_hit_counts)).isFailure()) {
162 ATH_MSG_ERROR("Failed to record track to truth assocition with key " << m_truthHitCountsOut.key() );
163 return StatusCode::FAILURE;
164 }
165
166 return StatusCode::SUCCESS;
167 }
168
169} // namespace
#define endmsg
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_DEBUG(x)
virtual void lock()=0
Interface to allow an object to lock itself when made const in SG.
size_t size() const
Number of registered mappings.
ElasticDecayUtil< TruthParticleHitCountDebugHists > m_elasticDecayUtil
Gaudi::Property< unsigned int > m_nHitsMin
SG::ReadHandleKey< MeasurementToTruthParticleAssociation > m_hgtdClustersToTruth
PublicToolHandle< ActsTrk::ITrackingGeometryTool > m_trackingGeometryTool
std::conditional< TruthParticleHitCountDebugHists, Gaudi::Property< std::vector< float > >, EmptyProperty >::type m_energyLossBinning
TruthParticleHitCountAlg(const std::string &name, ISvcLocator *pSvcLocator)
virtual StatusCode execute(const EventContext &ctx) const override
SG::WriteHandleKey< TruthParticleHitCounts > m_truthHitCountsOut
SG::ReadHandleKey< MeasurementToTruthParticleAssociation > m_pixelClustersToTruth
SG::ReadHandleKey< MeasurementToTruthParticleAssociation > m_stripClustersToTruth
virtual StatusCode initialize() override
AssociationCounter< TruthParticleHitCountDebugHists > m_associationCounter
bool msgLvl(const MSG::Level lvl) const
An algorithm that can be simultaneously executed in multiple threads.
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
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.
STL namespace.
std::size_t erase_if(T_container &container, T_Func pred)
TruthParticle_v1 TruthParticle
Typedef to implementation.
UncalibMeasType
Define the type of the uncalibrated measurement.