12 #include <type_traits>
17 template <
typename T_EnumClass >
31 ISvcLocator *pSvcLocator)
46 return StatusCode::SUCCESS;
49 template <
bool IsDebug>
50 template <
class T_OutStream>
53 out <<
"Measurements per track :" << m_measPerTrack << std::endl
54 << m_measPerTrack.histogramToString() << std::endl
55 <<
"TruthParticles per track :" << m_truthParticlesPerTrack << std::endl
56 << m_truthParticlesPerTrack.histogramToString();
59 template <
bool IsDebug>
62 std::lock_guard<std::mutex>
lock(m_mutex);
63 m_measPerTrack.add(n_measurements);
64 m_truthParticlesPerTrack.add(n_particles);
70 std::stringstream
label;
79 ATH_MSG_WARNING(
"Encountered measurements not compaible with provided association maps in "
85 for (
const std::atomic<std::size_t> &elm : m_nTracksWithAssociatedTruth) {
86 msg() << std::setw(20) << elm <<
" "
88 ? std::string(
" tracks without associated truth particles.")
89 : (
idx==m_nTracksWithAssociatedTruth.size()-1
91 m_nTracksWithAssociatedTruth.size()-1,
92 "associated truth particles.")
95 "associated truth particle(s).")));
108 return StatusCode::SUCCESS;
113 std::unique_ptr<TrackToTruthParticleAssociation>
114 track_association( std::make_unique<TrackToTruthParticleAssociation>() );
117 if (!pixelClustersToTruthAssociation.
isValid()) {
119 return StatusCode::FAILURE;
122 if (!stripClustersToTruthAssociation.
isValid()) {
124 return StatusCode::FAILURE;
128 if (!tracksContainer.
isValid()) {
130 return StatusCode::FAILURE;
132 track_association->resize( tracksContainer->size() );
138 measurement_to_truth_association_maps{};
145 if (!hgtdClustersToTruthAssociation.
isValid()) {
153 return assoc ? assoc->size() : 0
ul;
160 unsigned int track_i=0;
161 std::array<unsigned int,s_NCounterForAssociatedTruth> tracks_with_associated_truth{};
162 std::pair<unsigned int, unsigned int> compatible_assoc_container_counts{};
166 ++compatible_assoc_container_counts.second;
169 std::vector<unsigned int> counted_truth_particles;
170 counted_truth_particles.reserve(10);
172 for (
const typename ActsTrk::TrackContainer::ConstTrackProxy
track : *tracksContainer) {
173 const auto lastMeasurementIndex =
track.tipIndex();
175 unsigned int n_measurements=0
u;
177 HitCounterArray &reco_hits = track_association->at(track_i).totalCounts();
178 HitCounterArray &noise_hits = track_association->at(track_i).noiseCounts();
180 tracksContainer->trackStateContainer().visitBackwards(
181 lastMeasurementIndex,
184 &measurement_to_truth_association_maps,
185 &truth_particle_counts,
188 &counted_truth_particles,
189 &compatible_assoc_container_counts
190 ](
const typename ActsTrk::TrackStateBackend::ConstTrackStateProxy &state) ->
void
192 if (!state.typeFlags().test(Acts::TrackStateFlag::OutlierFlag) && state.hasUncalibratedSourceLink()) {
193 auto sl = state.getUncalibratedSourceLink().template get<ATLASUncalibSourceLink>();
194 assert( sl != nullptr );
195 const xAOD::UncalibratedMeasurement &uncalibMeas = getUncalibratedMeasurement(sl);
197 const ActsTrk::MeasurementToTruthParticleAssociation *association_map = measurement_to_truth_association_maps.at(to_underlying(uncalibMeas.type()));
198 if (association_map) {
199 if (!association_map->isCompatibleWith(dynamic_cast< const xAOD::UncalibratedMeasurementContainer *>(uncalibMeas.container()))) {
200 if (compatible_assoc_container_counts.second==0) {
201 ATH_MSG_ERROR(
"MeasurementToTruthParticleAssociation for measurement type " << to_underlying(uncalibMeas.type())
202 <<
" is not compatible with the measurement on track.");
204 ++compatible_assoc_container_counts.second;
208 ++compatible_assoc_container_counts.first;
211 counted_truth_particles.clear();
212 for (const xAOD::TruthParticle *truth_particle : association_map->at(uncalibMeas.index()) ) {
213 const xAOD::TruthParticle *mother_particle = m_elasticDecayUtil.getMother(*truth_particle, m_maxEnergyLoss.value());
216 if (std::find(counted_truth_particles.begin(), counted_truth_particles.end(), mother_particle->index())
217 ==counted_truth_particles.end()) {
218 counted_truth_particles.push_back(mother_particle->index());
220 ActsTrk::HitCountsPerTrack::container::iterator
221 hit_count_iter = std::find_if(truth_particle_counts.begin(),
222 truth_particle_counts.end(),
223 [mother_particle](const std::pair<const xAOD::TruthParticle *, HitCounterArray > &a) {
224 return a.first == mother_particle;
226 if (hit_count_iter == truth_particle_counts.end()) {
227 truth_particle_counts.push_back( std::make_pair(mother_particle, HitCounterArray{}));
228 hit_count_iter = truth_particle_counts.end()-1;
230 ++(hit_count_iter->second.at( to_underlying(uncalibMeas.type())));
233 if (association_map->at(uncalibMeas.index()).empty()) {
234 ++noise_hits.at( to_underlying(uncalibMeas.type()));
241 std::sort( truth_particle_counts.begin(),
242 truth_particle_counts.end(),
243 [](
const std::pair<const xAOD::TruthParticle *, HitCounterArray > &
a,
244 const std::pair<const xAOD::TruthParticle *, HitCounterArray > &
b) {
245 return std::accumulate(a.second.begin(),a.second.end(),0u) > std::accumulate(b.second.begin(),b.second.end(),0u);
248 ++(tracks_with_associated_truth[
std::min(truth_particle_counts.size(),tracks_with_associated_truth.size()-1
u)]);
252 for (
unsigned int elm : tracks_with_associated_truth) {
253 m_nTracksWithAssociatedTruth[
idx] += elm;
256 m_nIncompatibleMeasurementContainer += compatible_assoc_container_counts.second;
257 m_nCcompatibleMeasurementContainer += compatible_assoc_container_counts.first;
260 if (associationOutHandle.record( std::move(track_association)).isFailure()) {
261 ATH_MSG_ERROR(
"Failed to record track to truth assocition with key " << m_trackToTruthOut.key() );
262 return StatusCode::FAILURE;
264 return StatusCode::SUCCESS;