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
10#include <iomanip>
11#include <cmath>
12#include <type_traits>
13#include <typeinfo>
14#include <numeric>
15
16// for erase_if detection i.e. >=c++20
17#include <version>
18
19#ifndef __cpp_lib_erase_if
20namespace std {
21 template <class T_container, class T_Func>
22 inline std::size_t erase_if(T_container &container, T_Func pred) {
23 std::size_t orig_size = container->size();
24 for ( typename T_container::iterator iter = container.begin();
25 iter != container.end();
26 /* increment only if nothing was erased! */ ) {
27 if (pred(*iter)) {
28 iter = erase(iter);
29 }
30 else {
31 ++iter;
32 }
33 }
34 return orig_size - container->size();
35 }
36}
37#endif
38
39namespace ActsTrk
40{
41 // to dump
42 inline MsgStream &operator<<(MsgStream &out, const ActsUtils::Stat &stat) {
43 ActsUtils::dumpStat(out, stat);
44 return out;
45 }
46
48 ISvcLocator *pSvcLocator)
49 : AthReentrantAlgorithm(name, pSvcLocator)
50 {
51 }
52
54 {
56 ATH_CHECK( m_pixelClustersToTruth.initialize() );
57 ATH_CHECK( m_stripClustersToTruth.initialize() );
58 ATH_CHECK( m_hgtdClustersToTruth.initialize(not m_hgtdClustersToTruth.empty()) );
59
60 ATH_CHECK( m_truthHitCountsOut.initialize() );
61
62 m_elasticDecayUtil.setEnergyLossBinning(m_energyLossBinning.value());
63 return StatusCode::SUCCESS;
64 }
65
66 template <bool IsDebug>
67 template <class T_OutStream>
69 if constexpr(IsDebug) {
70 out << "Measurements per truth particle :" << m_measPerTruthParticle << std::endl
71 << m_measPerTruthParticle.histogramToString();
72 }
73 }
74 template <bool IsDebug>
75 void inline TruthParticleHitCountAlg::AssociationCounter<IsDebug>::fillStatistics(unsigned int n_measurements) const {
76 if constexpr(IsDebug) {
77 std::lock_guard<std::mutex> lock(m_mutex);
78 m_measPerTruthParticle.add(n_measurements);
79 }
80 }
81
83 {
84 ATH_MSG_INFO("Number of truth particles with hits : " << m_nTruthParticlesWithHits);
85 if (msgLvl(MSG::INFO)) {
87 m_elasticDecayUtil.dumpStatistics(msg());
88 msg() << std::endl;
89 m_associationCounter.dumpStatistics(msg());
90 }
91 msg() << endmsg;
92 }
93 return StatusCode::SUCCESS;
94 }
96 StatusCode TruthParticleHitCountAlg::execute(const EventContext &ctx) const
97 {
98 std::unique_ptr<TruthParticleHitCounts>
99 truth_particle_hit_counts( std::make_unique<TruthParticleHitCounts>() );
100
101
102 const ActsTrk::MeasurementToTruthParticleAssociation* pixelClustersToTruthAssociation{nullptr};
103 ATH_CHECK(SG::get(pixelClustersToTruthAssociation, m_pixelClustersToTruth, ctx));
104
105 const ActsTrk::MeasurementToTruthParticleAssociation* stripClustersToTruthAssociation{nullptr};
106 ATH_CHECK(SG::get(stripClustersToTruthAssociation, m_stripClustersToTruth, ctx));
107
108 const ActsTrk::MeasurementToTruthParticleAssociation* hgtdClustersToTruthAssociation{nullptr};
109 ATH_CHECK(SG::get(hgtdClustersToTruthAssociation, m_hgtdClustersToTruth, ctx));
110
111
112 Acts::GeometryContext tgContext = m_trackingGeometryTool->getGeometryContext(ctx).context();
113
115 static_cast< std::underlying_type<xAOD::UncalibMeasType>::type >(xAOD::UncalibMeasType::nTypes)>
116 measurement_to_truth_association_maps{};
117
118 measurement_to_truth_association_maps[Acts::toUnderlying(xAOD::UncalibMeasType::PixelClusterType)]=pixelClustersToTruthAssociation;
119 measurement_to_truth_association_maps[Acts::toUnderlying(xAOD::UncalibMeasType::StripClusterType)]=stripClustersToTruthAssociation;
120 measurement_to_truth_association_maps[Acts::toUnderlying(xAOD::UncalibMeasType::HGTDClusterType)]=hgtdClustersToTruthAssociation;
121 auto assocSize = [&measurement_to_truth_association_maps](xAOD::UncalibMeasType type) {
122 const ActsTrk::MeasurementToTruthParticleAssociation *assoc = measurement_to_truth_association_maps[Acts::toUnderlying(type)];
123 return assoc ? assoc->size() : 0ul;
124 };
125
126 ATH_MSG_DEBUG("Measurement association entries: " << assocSize(xAOD::UncalibMeasType::PixelClusterType)
127 << " + " << assocSize(xAOD::UncalibMeasType::StripClusterType)
128 << " + " << assocSize(xAOD::UncalibMeasType::HGTDClusterType)
129 );
130
131 unsigned int measurement_type_i=0;
132 --measurement_type_i;
133 for( const ActsTrk::MeasurementToTruthParticleAssociation *associated_truth_particles : measurement_to_truth_association_maps ) {
134 ++measurement_type_i;
135 if (!associated_truth_particles) continue;
136 for (const ActsTrk::ParticleVector &truth_particles : *associated_truth_particles) {
137 for (const xAOD::TruthParticle *truth_particle : truth_particles) {
138 assert (truth_particle);
139 const xAOD::TruthParticle *mother_particle = m_elasticDecayUtil.getMother(*truth_particle, m_maxEnergyLoss.value());
140 if (mother_particle) {
141 assert(measurement_type_i < (*truth_particle_hit_counts)[mother_particle].size());
142 ++(*truth_particle_hit_counts)[mother_particle][measurement_type_i];
143 }
144 }
145 }
146 }
147 unsigned int n_hits_min = m_nHitsMin.value();
148 unsigned int truth_particles_without_enough_measurements
149 = std::erase_if( *truth_particle_hit_counts,
150 [this, n_hits_min](const std::pair<const xAOD::TruthParticle * const,HitCounterArray> &elm) {
151 unsigned int n_measurements=std::accumulate(elm.second.begin(), elm.second.end(),0u);
152 this->m_associationCounter.fillStatistics(n_measurements);
153 return n_measurements<n_hits_min;
154 });
155
156 m_nTruthParticlesWithHits += truth_particle_hit_counts->size();
157
158 ATH_MSG_DEBUG("Truth particles with hits:" << truth_particle_hit_counts->size()
159 << ", without enough hits: " << truth_particles_without_enough_measurements);
160
161 SG::WriteHandle<TruthParticleHitCounts> truth_particle_hit_counts_out_handle(m_truthHitCountsOut, ctx);
162 if (truth_particle_hit_counts_out_handle.record( std::move(truth_particle_hit_counts)).isFailure()) {
163 ATH_MSG_ERROR("Failed to record track to truth assocition with key " << m_truthHitCountsOut.key() );
164 return StatusCode::FAILURE;
165 }
166
167 return StatusCode::SUCCESS;
168 }
169
170} // 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)
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.