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