ATLAS Offline Software
Loading...
Searching...
No Matches
TrackToTruthAssociationAlg.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
8
11#include <iomanip>
12#include <cmath>
13#include <type_traits>
14#include <typeinfo>
15#include <numeric>
16
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 {
28 ATH_CHECK( m_ctxProvider.initialize() );
29 ATH_CHECK( m_tracksContainerKey.initialize() );
30 ATH_CHECK( m_pixelClustersToTruth.initialize() );
31 ATH_CHECK( m_stripClustersToTruth.initialize() );
32 if (!m_hgtdClustersToTruth.key().empty()) ATH_CHECK( m_hgtdClustersToTruth.initialize() );
33 ATH_CHECK( m_trackToTruthOut.initialize() );
34
35 m_elasticDecayUtil.setEnergyLossBinning(m_energyLossBinning.value());
36 return StatusCode::SUCCESS;
37 }
38
39 template <bool IsDebug>
40 template <class T_OutStream>
42 if constexpr(IsDebug) {
43 out << "Measurements per track :" << m_measPerTrack << std::endl
44 << m_measPerTrack.histogramToString() << std::endl
45 << "TruthParticles per track :" << m_truthParticlesPerTrack << std::endl
46 << m_truthParticlesPerTrack.histogramToString();
47 }
48 }
49 template <bool IsDebug>
50 void inline TrackToTruthAssociationAlg::AssociationCounter<IsDebug>::fillStatistics(unsigned int n_measurements, unsigned int n_particles) const {
51 if constexpr(IsDebug) {
52 std::lock_guard<std::mutex> lock(m_mutex);
53 m_measPerTrack.add(n_measurements);
54 m_truthParticlesPerTrack.add(n_particles);
55 }
56 }
57
58 namespace {
59 std::string makeLabel(const std::string &header, unsigned int i, const std::string &tail) {
60 std::stringstream label;
61 label << header << " " << i << " " << tail;
62 return label.str();
63 }
64 }
65
67 {
69 ATH_MSG_WARNING( "Encountered measurements not compaible with provided association maps in "
71 }
72 if (msgLvl(MSG::INFO)) {
73 msg(MSG::INFO) << "-- Statistics:" << std::endl;
74 unsigned int idx=0;
75 for (const std::atomic<std::size_t> &elm : m_nTracksWithAssociatedTruth) {
76 msg() << std::setw(20) << elm << " "
77 << (idx==0
78 ? std::string(" tracks without associated truth particles.")
79 : (idx==m_nTracksWithAssociatedTruth.size()-1
80 ? makeLabel(" tracks with more than",
81 m_nTracksWithAssociatedTruth.size()-1,
82 "associated truth particles.")
83 : makeLabel(" tracks with",
84 idx,
85 "associated truth particle(s).")));
86 if (idx!=m_nTracksWithAssociatedTruth.size()-1 || TrackToTruthParticleAssociationDebugHists) {
87 msg() << std::endl;
88 }
89 ++idx;
90 }
92 m_elasticDecayUtil.dumpStatistics(msg());
93 msg() << std::endl;
94 m_associationCounter.dumpStatistics(msg());
95 }
96 msg() << endmsg;
97 }
98 return StatusCode::SUCCESS;
99 }
100
101 StatusCode TrackToTruthAssociationAlg::execute(const EventContext &ctx) const
102 {
103 std::unique_ptr<TrackToTruthParticleAssociation>
104 track_association( std::make_unique<TrackToTruthParticleAssociation>() );
105
107 if (!pixelClustersToTruthAssociation.isValid()) {
108 ATH_MSG_ERROR("No pixel clusterss for key " << m_pixelClustersToTruth.key() );
109 return StatusCode::FAILURE;
110 }
112 if (!stripClustersToTruthAssociation.isValid()) {
113 ATH_MSG_ERROR("No strip clusterss for key " << m_stripClustersToTruth.key() );
114 return StatusCode::FAILURE;
115 }
116
118 if (!tracksContainer.isValid()) {
119 ATH_MSG_ERROR("No tracks for key " << m_tracksContainerKey.key() );
120 return StatusCode::FAILURE;
121 }
122 track_association->resize( tracksContainer->size() );
123 track_association->setSourceContainer(DataLink<ActsTrk::TrackContainer>(*tracksContainer,ctx));
124 Acts::GeometryContext tgContext = m_ctxProvider.getGeometryContext(ctx);
125
127 static_cast< std::underlying_type<xAOD::UncalibMeasType>::type >(xAOD::UncalibMeasType::nTypes)>
128 measurement_to_truth_association_maps{};
129
130 measurement_to_truth_association_maps[Acts::toUnderlying(xAOD::UncalibMeasType::PixelClusterType)]=pixelClustersToTruthAssociation.cptr();
131 measurement_to_truth_association_maps[Acts::toUnderlying(xAOD::UncalibMeasType::StripClusterType)]=stripClustersToTruthAssociation.cptr();
132
133 if (!m_hgtdClustersToTruth.key().empty()) {
135 if (!hgtdClustersToTruthAssociation.isValid()) {
136 ATH_MSG_DEBUG("No HGTD clusterss for key " << m_hgtdClustersToTruth.key() );
137 }
138 measurement_to_truth_association_maps[Acts::toUnderlying(xAOD::UncalibMeasType::HGTDClusterType)]=hgtdClustersToTruthAssociation.cptr();
139 }
140
141 auto assocSize = [&measurement_to_truth_association_maps](xAOD::UncalibMeasType type) {
142 const ActsTrk::MeasurementToTruthParticleAssociation *assoc = measurement_to_truth_association_maps[Acts::toUnderlying(type)];
143 return assoc ? assoc->size() : 0ul;
144 };
145
146 ATH_MSG_DEBUG("Measurement association entries: " << assocSize(xAOD::UncalibMeasType::PixelClusterType)
147 << " + " << assocSize(xAOD::UncalibMeasType::StripClusterType)
148 << " + " << assocSize(xAOD::UncalibMeasType::HGTDClusterType)
149 );
150 unsigned int track_i=0;
151 std::array<unsigned int,s_NCounterForAssociatedTruth> tracks_with_associated_truth{};
152 std::pair<unsigned int, unsigned int> compatible_assoc_container_counts{};
154 // used to suppress reoccurring error messages
156 ++compatible_assoc_container_counts.second;
157 }
158
159 std::vector<unsigned int> counted_truth_particles;
160 counted_truth_particles.reserve(10);
161
162 for (const typename ActsTrk::TrackContainer::ConstTrackProxy track : *tracksContainer) {
163 const auto lastMeasurementIndex = track.tipIndex();
164
165 unsigned int n_measurements=0u;
166
167 HitCounterArray &reco_hits = track_association->at(track_i).totalCounts();
168 HitCounterArray &noise_hits = track_association->at(track_i).noiseCounts();
169 ActsTrk::HitCountsPerTrack::container &truth_particle_counts = track_association->at(track_i).countsPerTruthParticle();
170 tracksContainer->trackStateContainer().visitBackwards(
171 lastMeasurementIndex,
172 [this,
173 &n_measurements,
174 &measurement_to_truth_association_maps,
175 &truth_particle_counts,
176 &reco_hits,
177 &noise_hits,
178 &counted_truth_particles,
179 &compatible_assoc_container_counts
180 ](const typename ActsTrk::TrackStateBackend::ConstTrackStateProxy &state) -> void
181 {
182 if (!state.typeFlags().isOutlier() && state.hasUncalibratedSourceLink()) {
183 const auto* uncalibMeas = detail::xAODUncalibMeasCalibrator::unpack(state.getUncalibratedSourceLink());
184
185 const ActsTrk::MeasurementToTruthParticleAssociation *association_map = measurement_to_truth_association_maps.at(Acts::toUnderlying(uncalibMeas->type()));
186 if (association_map) {
187 if (!association_map->isCompatibleWith(dynamic_cast< const xAOD::UncalibratedMeasurementContainer *>(uncalibMeas->container()))) {
188 if (compatible_assoc_container_counts.second==0) {
189 ATH_MSG_ERROR("MeasurementToTruthParticleAssociation for measurement type " << uncalibMeas->type()
190 << " is not compatible with the measurement on track.");
191 }
192 ++compatible_assoc_container_counts.second;
193 return;
194 }
195 else {
196 ++compatible_assoc_container_counts.first;
197 }
198 ++n_measurements;
199 counted_truth_particles.clear();
200 for (const xAOD::TruthParticle *truth_particle : association_map->at(uncalibMeas->index()) ) {
201 const xAOD::TruthParticle *mother_particle = m_elasticDecayUtil.getMother(*truth_particle, m_maxEnergyLoss.value());
202
203 // do not count hits of associated truth particles again if they are associated to the same elastic decay chain:
204 if (std::find(counted_truth_particles.begin(), counted_truth_particles.end(), mother_particle->index())
205 ==counted_truth_particles.end()) {
206 counted_truth_particles.push_back(mother_particle->index());
207
208 ActsTrk::HitCountsPerTrack::container::iterator
209 hit_count_iter = std::find_if(truth_particle_counts.begin(),
210 truth_particle_counts.end(),
211 [mother_particle](const std::pair<const xAOD::TruthParticle *, HitCounterArray > &a) {
212 return a.first == mother_particle;
213 });
214 if (hit_count_iter == truth_particle_counts.end()) {
215 truth_particle_counts.push_back( std::make_pair(mother_particle, HitCounterArray{}));
216 hit_count_iter = truth_particle_counts.end()-1;
217 }
218 ++(hit_count_iter->second.at( Acts::toUnderlying(uncalibMeas->type())));
219 }
220 }
221 if (association_map->at(uncalibMeas->index()).empty()) {
222 ++noise_hits.at( Acts::toUnderlying(uncalibMeas->type()));
223 }
224 }
225 ++reco_hits.at( Acts::toUnderlying(uncalibMeas->type()));
226 }
227
228 });
229 std::sort( truth_particle_counts.begin(),
230 truth_particle_counts.end(),
231 [](const std::pair<const xAOD::TruthParticle *, HitCounterArray > &a,
232 const std::pair<const xAOD::TruthParticle *, HitCounterArray > &b) {
233 return std::accumulate(a.second.begin(),a.second.end(),0u) > std::accumulate(b.second.begin(),b.second.end(),0u);
234 });
235 m_associationCounter.fillStatistics(n_measurements, truth_particle_counts.size());
236 ++(tracks_with_associated_truth[std::min(truth_particle_counts.size(),tracks_with_associated_truth.size()-1u)]);
237 ++track_i;
238 }
239 unsigned int idx=0;
240 for (unsigned int elm : tracks_with_associated_truth) {
241 m_nTracksWithAssociatedTruth[idx] += elm;
242 ++idx;
243 }
244 m_nIncompatibleMeasurementContainer += compatible_assoc_container_counts.second;
245 m_nCcompatibleMeasurementContainer += compatible_assoc_container_counts.first;
246
248 if (associationOutHandle.record( std::move(track_association)).isFailure()) {
249 ATH_MSG_ERROR("Failed to record track to truth assocition with key " << m_trackToTruthOut.key() );
250 return StatusCode::FAILURE;
251 }
252 return StatusCode::SUCCESS;
253 }
254
255} // namespace
#define endmsg
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
virtual void lock()=0
Interface to allow an object to lock itself when made const in SG.
static Double_t a
boost::container::small_vector< std::pair< const xAOD::TruthParticle *, HitCounterArray >, NTruthParticlesPerTrack > container
bool isCompatibleWith(const xAOD::UncalibratedMeasurementContainer *container) const
std::atomic< std::size_t > m_nCcompatibleMeasurementContainer
AssociationCounter< TrackToTruthParticleAssociationDebugHists > m_associationCounter
SG::WriteHandleKey< TrackToTruthParticleAssociation > m_trackToTruthOut
std::atomic< std::size_t > m_nIncompatibleMeasurementContainer
SG::ReadHandleKey< ActsTrk::TrackContainer > m_tracksContainerKey
virtual StatusCode execute(const EventContext &ctx) const override
ContextUtility m_ctxProvider
Context provider for geometry, magnetic field and calibration contexts.
std::conditional< TrackToTruthParticleAssociationDebugHists, Gaudi::Property< std::vector< float > >, EmptyProperty >::type m_energyLossBinning
SG::ReadHandleKey< MeasurementToTruthParticleAssociation > m_hgtdClustersToTruth
ElasticDecayUtil< TrackToTruthParticleAssociationDebugHists > m_elasticDecayUtil
SG::ReadHandleKey< MeasurementToTruthParticleAssociation > m_pixelClustersToTruth
SG::ReadHandleKey< MeasurementToTruthParticleAssociation > m_stripClustersToTruth
static const xAOD::UncalibratedMeasurement * unpack(const Acts::SourceLink &sl)
Helper method to unpack an Acts source link to an uncalibrated measurement.
Simple class to gather statistics : min, max, mean, rms.
Definition StatUtils.h:17
bool msgLvl(const MSG::Level lvl) const
virtual bool isValid() override final
Can the handle be successfully dereferenced?
const_pointer_type cptr()
Dereference the pointer.
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
std::string tail(std::string s, const std::string &pattern)
tail of a string
std::string label(const std::string &format, int i)
Definition label.h:19
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.
constexpr bool TrackToTruthParticleAssociationDebugHists
void dumpStat(T_Stream &out, const Stat &stat)
Dump the given statistics object to the given output stream.
Definition StatUtils.h:63
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.
TruthParticle_v1 TruthParticle
Typedef to implementation.
UncalibMeasType
Define the type of the uncalibrated measurement.
UncalibratedMeasurementContainer_v1 UncalibratedMeasurementContainer
Define the version of the uncalibrated measurement container.
void fillStatistics(unsigned int n_measurements, unsigned int n_particles) const