ATLAS Offline Software
Loading...
Searching...
No Matches
ScoreBasedAmbiguityResolutionAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7// Athena
10
11// ACTS
12#include <fstream>
13#include <iostream>
14#include <vector>
15
16#include "Acts/AmbiguityResolution/ScoreBasedAmbiguityResolution.hpp"
17#include "Acts/Definitions/Units.hpp"
18#include "Acts/EventData/VectorMultiTrajectory.hpp"
19#include "Acts/EventData/VectorTrackContainer.hpp"
20#include "ActsPlugins/Json/AmbiguityConfigJsonConverter.hpp"
21#include "Acts/Utilities/Logger.hpp"
23#include "ActsInterop/Logger.h"
28
29namespace {
30static std::size_t sourceLinkHash(const Acts::SourceLink &slink) {
31 const ActsTrk::ATLASUncalibSourceLink &atlasSourceLink =
33 const xAOD::UncalibratedMeasurement &uncalibMeas =
35 return uncalibMeas.identifier();
36}
37
38static bool sourceLinkEquality(const Acts::SourceLink &a, const Acts::SourceLink &b) {
39 const xAOD::UncalibratedMeasurement &uncalibMeas_a =
42 const xAOD::UncalibratedMeasurement &uncalibMeas_b =
45
46 return uncalibMeas_a.identifier() == uncalibMeas_b.identifier();
47}
48
49} // namespace
50
51namespace ActsTrk {
52
54 const std::string &name, ISvcLocator *pSvcLocator)
55 : AthReentrantAlgorithm(name, pSvcLocator) {}
56
58 {
59 Acts::ScoreBasedAmbiguityResolution::Config cfg;
60 Acts::ConfigPair configPair;
61 nlohmann::json json_file;
62
63 std::string fileName = PathResolver::find_file(
64 "ActsConfig/ActsAmbiguityConfig.json", "DATAPATH");
65
66 std::ifstream file(fileName.c_str());
67 if (!file.is_open()) {
68 std::cerr << "Error opening file: " << fileName << std::endl;
69 return {};
70 }
71 file >> json_file;
72 file.close();
73
74 Acts::from_json(json_file, configPair);
75
76 cfg.volumeMap = configPair.first;
77 cfg.detectorConfigs = configPair.second;
78 cfg.minScore = m_minScore;
79 cfg.minScoreSharedTracks = m_minScoreSharedTracks;
80 cfg.maxSharedTracksPerMeasurement = m_maxSharedTracksPerMeasurement;
81 cfg.maxShared = m_maxShared;
82 cfg.minUnshared = m_minUnshared;
83 cfg.useAmbiguityScoring = m_useAmbiguityScoring;
84
85 m_ambi = std::make_unique<Acts::ScoreBasedAmbiguityResolution>(
86 std::move(cfg), makeActsAthenaLogger(this, "Acts"));
87 assert(m_ambi);
88 }
89
90 ATH_CHECK(m_monTool.retrieve(EnableTool{not m_monTool.empty()}));
91 ATH_CHECK(m_tracksKey.initialize());
92 ATH_CHECK(m_resolvedTracksKey.initialize());
93
94 return StatusCode::SUCCESS;
95}
96
98 ATH_MSG_INFO("Score-based Ambiguity Resolution statistics" << std::endl
99 << makeTable(m_stat,
100 std::array<std::string, kNStat>{
101 "Input tracks",
102 "Resolved tracks",
103 "Total shared hits"}).columnWidth(10));
104 return StatusCode::SUCCESS;
105}
106
108 const EventContext &ctx) const {
109 auto timer = Monitored::Timer<std::chrono::milliseconds>("TIME_execute");
110 auto mon = Monitored::Group(m_monTool, timer);
111
114 ATH_CHECK(trackHandle.isValid());
115 const ActsTrk::TrackContainer* trackContainer = trackHandle.cptr();
116 m_stat[kNInputTracks] += trackContainer->size();
117
118 // creates mutable tracks from the input tracks to add summary information
119 // NOTE: this operation likely needs to moved outside ambiguity resolution
120 auto updatedTracks =
122
123 // create the optional cuts for the ambiguity resolution
124 Acts::ScoreBasedAmbiguityResolution::Optionals<
125 typename decltype(updatedTracks)::ConstTrackProxy>
126 Optionals;
127
128 // Adding optional cuts
129 Optionals.cuts.push_back(ScoreBasedSolverCutsImpl::etaDependentCuts);
130
131 // Adding optional Score Modifiers
132 Optionals.scores.push_back(ScoreBasedSolverCutsImpl::doubleHolesScore);
133 Optionals.scores.push_back(ScoreBasedSolverCutsImpl::nSCTPixelHitsScore);
134 Optionals.scores.push_back(
136 Optionals.scores.push_back(ScoreBasedSolverCutsImpl::ContribPixelLayersScore);
137
138 // Call the ambiguity resolution algorithm with the optional cuts on the
139 // updated tracks
140 std::vector<int> goodTracks = m_ambi->solveAmbiguity(
141 updatedTracks, &sourceLinkHash, &sourceLinkEquality, Optionals);
142
143 ATH_MSG_DEBUG("Resolved to " << goodTracks.size() << " tracks from "
144 << updatedTracks.size());
145 m_stat[kNResolvedTracks] += goodTracks.size();
146
147
148 // we start shortlisting the container
149 Acts::VectorTrackContainer resolvedTrackBackend;
150 Acts::VectorMultiTrajectory resolvedTrackStateBackend;
151 detail::RecoTrackContainer resolvedTracksContainer(resolvedTrackBackend, resolvedTrackStateBackend);
152
153 resolvedTracksContainer.ensureDynamicColumns(updatedTracks);
154
155 detail::MeasurementIndex measurementIndex;
156 detail::SharedHitCounter sharedHits;
157
158 std::size_t totalShared = 0;
159 for (auto iTrack : goodTracks) {
160 auto destProxy = resolvedTracksContainer.getTrack(resolvedTracksContainer.addTrack());
161 destProxy.copyFrom(updatedTracks.getTrack(iTrack));
162
163 if (m_countSharedHits) {
164 auto [nShared, nBadTrackMeasurements] = sharedHits.computeSharedHitsDynamic(destProxy, resolvedTracksContainer, measurementIndex);
165 if (nBadTrackMeasurements > 0) {
166 ATH_MSG_ERROR("computeSharedHits: " << nBadTrackMeasurements << " track measurements not found in input track");
167 }
168 totalShared += nShared;
169 }
170 }
171
172 if (m_countSharedHits) {
173 ATH_MSG_DEBUG("total number of shared hits = " << totalShared);
174 m_stat[kNSharedHits] += totalShared;
175 }
176
177 // make const collection
178 Acts::ConstVectorTrackContainer storableTrackBackend( std::move(resolvedTrackBackend) );
179 Acts::ConstVectorMultiTrajectory storableTrackStateBackend( std::move(resolvedTrackStateBackend) );
180 std::unique_ptr< ActsTrk::TrackContainer > storableTracksContainer = std::make_unique< ActsTrk::TrackContainer >( std::move(storableTrackBackend),
181 std::move(storableTrackStateBackend) );
183 if (resolvedTrackHandle.record( std::move(storableTracksContainer)).isFailure()) {
184 ATH_MSG_ERROR("Failed to record resolved ACTS tracks with key " << m_resolvedTracksKey.key() );
185 return StatusCode::FAILURE;
186 }
187
188 return StatusCode::SUCCESS;
189}
190
191} // namespace ActsTrk
#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)
static Double_t a
Header file to be included by clients of the Monitored infrastructure.
TableUtils::StatTable< T > makeTable(const std::array< T, N > &counter, const std::array< std::string, N > &label)
Definition TableUtils.h:523
std::unique_ptr< const Acts::Logger > makeActsAthenaLogger(IMessageSvc *svc, const std::string &name, int level, std::optional< std::string > parent_name)
std::unique_ptr< Acts::ScoreBasedAmbiguityResolution > m_ambi
ScoreBasedAmbiguityResolutionAlg(const std::string &name, ISvcLocator *pSvcLocator)
SG::ReadHandleKey< ActsTrk::TrackContainer > m_tracksKey
SG::WriteHandleKey< ActsTrk::TrackContainer > m_resolvedTracksKey
virtual StatusCode execute(const EventContext &ctx) const override
auto computeSharedHitsDynamic(typename track_container_t::TrackProxy &track, track_container_t &tracks, MeasurementIndex &measurementIndex, bool removeSharedHits=false) -> ReturnSharedAndBad
An algorithm that can be simultaneously executed in multiple threads.
Group of local monitoring quantities and retain correlation when filling histograms
A monitored timer.
static std::string find_file(const std::string &logical_file_name, const std::string &search_path)
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.
DetectorIdentType identifier() const
Returns the full Identifier of the measurement.
void innermostPixelLayerHitsScore(const trackProxy_t &track, double &score)
Score modifier for tracks based on innermost pixel layer hits.
bool etaDependentCuts(const trackProxy_t &track)
Filter for tracks based on eta dependent cuts.
void nSCTPixelHitsScore(const trackProxy_t &track, double &score)
Score modifier for tracks based on number of SCT and pixel hits.
void doubleHolesScore(const trackProxy_t &track, double &score)
Filter for tracks based on double holes.
void ContribPixelLayersScore(const trackProxy_t &track, double &score)
Score modifier for tracks based on number of contributing pixel layers.
trackContainer_t addSummaryInformation(const ActsTrk::TrackContainer &trackContainer)
Adds summary information to the track container.
Acts::TrackContainer< Acts::VectorTrackContainer, Acts::VectorMultiTrajectory > RecoTrackContainer
The AlignStoreProviderAlg loads the rigid alignment corrections and pipes them through the readout ge...
const xAOD::UncalibratedMeasurement & getUncalibratedMeasurement(const ATLASUncalibSourceLink &source_link)
const xAOD::UncalibratedMeasurement * ATLASUncalibSourceLink
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
UncalibratedMeasurement_v1 UncalibratedMeasurement
Define the version of the uncalibrated measurement class.
TFile * file