ATLAS Offline Software
ScoreBasedAmbiguityResolutionAlg.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 
7 // Athena
10 
11 // ACTS
12 #include "Acts/AmbiguityResolution/ScoreBasedAmbiguityResolution.hpp"
13 #include "Acts/Definitions/Units.hpp"
14 #include "Acts/EventData/VectorMultiTrajectory.hpp"
15 #include "Acts/EventData/VectorTrackContainer.hpp"
16 #include "Acts/Utilities/Logger.hpp"
18 #include "ActsInterop/Logger.h"
19 #include "Acts/Plugins/Json/AmbiguityConfigJsonConverter.hpp"
20 
21 #include <fstream>
22 #include <iostream>
23 #include <vector>
24 
25 
26 namespace {
27 std::size_t sourceLinkHash(const Acts::SourceLink &slink) {
28  const ActsTrk::ATLASUncalibSourceLink &atlasSourceLink =
30  const xAOD::UncalibratedMeasurement &uncalibMeas =
31  ActsTrk::getUncalibratedMeasurement(atlasSourceLink);
32  return uncalibMeas.identifier();
33 }
34 
35 bool sourceLinkEquality(const Acts::SourceLink &a, const Acts::SourceLink &b) {
36  const xAOD::UncalibratedMeasurement &uncalibMeas_a =
39  const xAOD::UncalibratedMeasurement &uncalibMeas_b =
42 
43  return uncalibMeas_a.identifier() == uncalibMeas_b.identifier();
44 }
45 } // namespace
46 
47 namespace ActsTrk {
48 
50  const std::string &name, ISvcLocator *pSvcLocator)
51  : AthReentrantAlgorithm(name, pSvcLocator) {}
52 
54  {
55  Acts::ScoreBasedAmbiguityResolution::Config cfg;
56  Acts::ConfigPair configPair;
57  nlohmann::json json_file;
58 
59  std::string fileName = PathResolver::find_file( "ActsConfig/ActsAmbiguityConfig.json", "DATAPATH" );
60 
61  std::ifstream file(fileName.c_str());
62  if (!file.is_open()) {
63  std::cerr << "Error opening file: " << fileName << std::endl;
64  return {};
65  }
66  file >> json_file;
67  file.close();
68 
69  Acts::from_json(json_file, configPair);
70 
71  cfg.volumeMap = configPair.first;
72  cfg.detectorConfigs = configPair.second;
73  cfg.minScore = m_minScore;
74  cfg.minScoreSharedTracks = m_minScoreSharedTracks;
75  cfg.maxSharedTracksPerMeasurement = m_maxSharedTracksPerMeasurement;
76  cfg.maxShared = m_maxShared;
77  cfg.pTMin = m_pTMin;
78  cfg.pTMax = m_pTMax;
79  cfg.phiMin = m_phiMin;
80  cfg.phiMax = m_phiMax;
81  cfg.etaMin = m_etaMin;
82  cfg.etaMax = m_etaMax;
83  cfg.useAmbiguityFunction = m_useAmbiguityFunction;
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()}));
96  m_resolvedTracksKey.key()))); // TODO choose prefix related to the
97  // output tracks name
98  return StatusCode::SUCCESS;
99 }
100 
102  const EventContext &ctx) const {
103  auto timer = Monitored::Timer<std::chrono::milliseconds>("TIME_execute");
105 
108  ATH_CHECK(trackHandle.isValid());
109 
110  std::vector<std::vector<Acts::ScoreBasedAmbiguityResolution::MeasurementInfo>>
111  measurementsPerTracks;
112 
113  std::vector<std::vector<Acts::ScoreBasedAmbiguityResolution::TrackFeatures>>
114  trackFeaturesVectors;
115 
116  measurementsPerTracks = m_ambi->computeInitialState(*trackHandle, &sourceLinkHash,
117  &sourceLinkEquality, trackFeaturesVectors);
118 
119  std::vector<int> goodTracks = m_ambi->solveAmbiguity(
120  *trackHandle, measurementsPerTracks, trackFeaturesVectors);
121 
122  ATH_MSG_DEBUG("Resolved to " << goodTracks.size() << " tracks from "
123  << trackHandle->size());
124 
125  ActsTrk::MutableTrackContainer solvedTracks;
126  solvedTracks.ensureDynamicColumns(*trackHandle);
127 
128  for (auto iTrack : goodTracks) {
129  auto destProxy = solvedTracks.getTrack(solvedTracks.addTrack());
130  destProxy.copyFrom(trackHandle->getTrack(iTrack));
131  }
132  std::unique_ptr<ActsTrk::TrackContainer> outputTracks =
134  std::move(solvedTracks),
135  m_trackingGeometryTool->getGeometryContext(ctx).context(), ctx);
136  SG::WriteHandle<ActsTrk::TrackContainer> resolvedTrackHandle(
137  m_resolvedTracksKey, ctx);
138 
139  if (resolvedTrackHandle.record(std::move(outputTracks)).isFailure()) {
140  ATH_MSG_ERROR("Failed to record resolved ACTS tracks with key "
141  << m_resolvedTracksKey.key());
142  return StatusCode::FAILURE;
143  }
144 
145  return StatusCode::SUCCESS;
146 }
147 
148 } // namespace ActsTrk
xAOD::UncalibratedMeasurement_v1::identifier
DetectorIdentType identifier() const
Returns the full Identifier of the measurement.
ActsTrk::ScoreBasedAmbiguityResolutionAlg::m_monTool
ToolHandle< GenericMonitoringTool > m_monTool
Definition: ScoreBasedAmbiguityResolutionAlg.h:47
TrigDefs::Group
Group
Properties of a chain group.
Definition: GroupProperties.h:13
ActsTrk::MutableTrackContainerHandlesHelper::moveToConst
std::unique_ptr< ActsTrk::TrackContainer > moveToConst(ActsTrk::MutableTrackContainer &&tc, const Acts::GeometryContext &geoContext, const EventContext &evtContext) const
produces ActsTrk::ConstTrackContainer with all backends stored in SG
Definition: TrackContainerHandlesHelper.cxx:130
PathResolver::find_file
static std::string find_file(const std::string &logical_file_name, const std::string &search_path, SearchType search_type=LocalSearch)
Definition: PathResolver.cxx:251
ActsTrk::ScoreBasedAmbiguityResolutionAlg::m_etaMax
Gaudi::Property< double > m_etaMax
Definition: ScoreBasedAmbiguityResolutionAlg.h:84
ActsTrk::ScoreBasedAmbiguityResolutionAlg::m_phiMax
Gaudi::Property< double > m_phiMax
Definition: ScoreBasedAmbiguityResolutionAlg.h:78
json
nlohmann::json json
Definition: HistogramDef.cxx:9
ActsTrk::ScoreBasedAmbiguityResolutionAlg::m_phiMin
Gaudi::Property< double > m_phiMin
Definition: ScoreBasedAmbiguityResolutionAlg.h:75
ActsTrk::ScoreBasedAmbiguityResolutionAlg::m_useAmbiguityFunction
Gaudi::Property< bool > m_useAmbiguityFunction
Definition: ScoreBasedAmbiguityResolutionAlg.h:87
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
ActsTrk::ScoreBasedAmbiguityResolutionAlg::ScoreBasedAmbiguityResolutionAlg
ScoreBasedAmbiguityResolutionAlg(const std::string &name, ISvcLocator *pSvcLocator)
Definition: ScoreBasedAmbiguityResolutionAlg.cxx:49
ActsTrk::ScoreBasedAmbiguityResolutionAlg::m_resolvedTracksBackendHandles
ActsTrk::MutableTrackContainerHandlesHelper m_resolvedTracksBackendHandles
Definition: ScoreBasedAmbiguityResolutionAlg.h:54
ActsTrk::ScoreBasedAmbiguityResolutionAlg::execute
virtual StatusCode execute(const EventContext &ctx) const override
Definition: ScoreBasedAmbiguityResolutionAlg.cxx:101
ActsTrk::prefixFromTrackContainerName
std::string prefixFromTrackContainerName(const std::string &tracks)
Parse TrackContainer name to get the prefix for backends The name has to contain XYZTracks,...
Definition: TrackContainerHandlesHelper.cxx:18
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
ActsTrk::ScoreBasedAmbiguityResolutionAlg::m_tracksKey
SG::ReadHandleKey< ActsTrk::TrackContainer > m_tracksKey
Definition: ScoreBasedAmbiguityResolutionAlg.h:52
ActsTrk::MutableTrackContainerHandlesHelper::initialize
StatusCode initialize(const std::string &prefix)
Sets up the handles.
Definition: TrackContainerHandlesHelper.cxx:51
python.utils.AtlRunQueryTimer.timer
def timer(name, disabled=False)
Definition: AtlRunQueryTimer.py:86
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:83
ActsTrk::ScoreBasedAmbiguityResolutionAlg::m_ambi
std::unique_ptr< Acts::ScoreBasedAmbiguityResolution > m_ambi
Definition: ScoreBasedAmbiguityResolutionAlg.h:95
SG::makeHandle
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
Definition: ReadCondHandle.h:270
makeActsAthenaLogger
std::unique_ptr< const Acts::Logger > makeActsAthenaLogger(IMessageSvc *svc, const std::string &name, int level, std::optional< std::string > parent_name)
Definition: Tracking/Acts/ActsInterop/src/Logger.cxx:64
FortranAlgorithmOptions.fileName
fileName
Definition: FortranAlgorithmOptions.py:13
xAOD::UncalibratedMeasurement_v1
Definition: UncalibratedMeasurement_v1.h:13
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
ActsTrk::ScoreBasedAmbiguityResolutionAlg::m_minScore
Gaudi::Property< double > m_minScore
Definition: ScoreBasedAmbiguityResolutionAlg.h:59
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
file
TFile * file
Definition: tile_monitor.h:29
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
Monitored.h
Header file to be included by clients of the Monitored infrastructure.
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
PathResolver.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
ActsTrk::ScoreBasedAmbiguityResolutionAlg::m_trackingGeometryTool
ToolHandle< IActsTrackingGeometryTool > m_trackingGeometryTool
Definition: ScoreBasedAmbiguityResolutionAlg.h:49
ActsTrk::getUncalibratedMeasurement
const xAOD::UncalibratedMeasurement & getUncalibratedMeasurement(const ATLASUncalibSourceLink &source_link)
Definition: ATLASSourceLink.h:26
WriteCaloSwCorrections.cfg
cfg
Definition: WriteCaloSwCorrections.py:23
ActsTrk::ScoreBasedAmbiguityResolutionAlg::m_pTMin
Gaudi::Property< double > m_pTMin
Definition: ScoreBasedAmbiguityResolutionAlg.h:70
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
ActsTrk::ScoreBasedAmbiguityResolutionAlg::m_minScoreSharedTracks
Gaudi::Property< double > m_minScoreSharedTracks
Definition: ScoreBasedAmbiguityResolutionAlg.h:61
ActsTrk::ScoreBasedAmbiguityResolutionAlg::m_maxShared
Gaudi::Property< std::size_t > m_maxShared
Definition: ScoreBasedAmbiguityResolutionAlg.h:67
a
TList * a
Definition: liststreamerinfos.cxx:10
SG::WriteHandle::record
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
ActsTrk::ScoreBasedAmbiguityResolutionAlg::m_etaMin
Gaudi::Property< double > m_etaMin
Definition: ScoreBasedAmbiguityResolutionAlg.h:82
plotBeamSpotMon.mon
mon
Definition: plotBeamSpotMon.py:67
ScoreBasedAmbiguityResolutionAlg.h
ActsTrk
The AlignStoreProviderAlg loads the rigid alignment corrections and pipes them through the readout ge...
Definition: MuonDetectorBuilderTool.cxx:49
ActsTrk::ScoreBasedAmbiguityResolutionAlg::m_maxSharedTracksPerMeasurement
Gaudi::Property< std::size_t > m_maxSharedTracksPerMeasurement
Definition: ScoreBasedAmbiguityResolutionAlg.h:64
ActsTrk::ScoreBasedAmbiguityResolutionAlg::m_resolvedTracksKey
SG::WriteHandleKey< ActsTrk::TrackContainer > m_resolvedTracksKey
Definition: ScoreBasedAmbiguityResolutionAlg.h:55
Logger.h
JetPileupTag::from_json
void from_json(const nlohmann::json &j, NNJvtBinning &binning)
Definition: NNJvtBinning.cxx:24
ActsTrk::MutableTrackContainer
Definition: TrackContainer.h:122
Monitored::Timer
A monitored timer.
Definition: MonitoredTimer.h:32
ActsTrk::ScoreBasedAmbiguityResolutionAlg::initialize
virtual StatusCode initialize() override
Definition: ScoreBasedAmbiguityResolutionAlg.cxx:53
ActsTrk::ScoreBasedAmbiguityResolutionAlg::m_pTMax
Gaudi::Property< double > m_pTMax
Definition: ScoreBasedAmbiguityResolutionAlg.h:72