ATLAS Offline Software
TrackExtensionAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
5 #include "src/TrackFindingAlg.h"
6 #include "Acts/Propagator/PropagatorOptions.hpp"
8 
9 // Athena
10 #include "AsgTools/ToolStore.h"
19 
20 // ACTS
21 #include "Acts/Definitions/Units.hpp"
22 #include "Acts/Geometry/TrackingGeometry.hpp"
23 #include "Acts/Geometry/GeometryIdentifier.hpp"
24 #include "Acts/MagneticField/MagneticFieldProvider.hpp"
25 #include "Acts/Surfaces/Surface.hpp"
26 #include "Acts/TrackFinding/MeasurementSelector.hpp"
27 #include "Acts/TrackFinding/CombinatorialKalmanFilter.hpp"
28 #include "Acts/Surfaces/PerigeeSurface.hpp"
29 #include "Acts/Utilities/TrackHelpers.hpp"
30 
31 // ActsTrk
36 #include "ActsInterop/Logger.h"
37 #include "ActsInterop/TableUtils.h"
41 
42 // STL
43 #include <initializer_list>
44 #include <sstream>
45 #include <functional>
46 #include <tuple>
47 #include <utility>
48 #include <algorithm>
49 
50 
51 namespace ActsTrk{
52 
54  ISvcLocator* pSvcLocator)
55  : AthReentrantAlgorithm(name, pSvcLocator) {}
56 
65  ATH_CHECK(m_extrapolationTool.retrieve());
66  ATH_CHECK(m_pixelCalibTool.retrieve(EnableTool{not m_pixelCalibTool.empty()}));
67  ATH_CHECK(m_stripCalibTool.retrieve(EnableTool{not m_stripCalibTool.empty()}));
69  ATH_CHECK(m_trackStatePrinter.retrieve(EnableTool{not m_trackStatePrinter.empty()}));
70  ATH_CHECK(m_actsFitter.retrieve());
72 
73  auto magneticField = std::make_unique<ATLASMagneticFieldWrapper>();
74  std::shared_ptr<const Acts::TrackingGeometry> trackingGeometry = m_trackingGeometryTool->trackingGeometry();
75 
76  detail::Stepper stepper(std::move(magneticField));
77  detail::Navigator::Config cfg{trackingGeometry};
78  cfg.resolvePassive = false;
79  cfg.resolveMaterial = true;
80  cfg.resolveSensitive = true;
81  detail::Navigator navigator(cfg, m_logger->cloneWithSuffix("Navigator"));
82  detail::Propagator propagator(std::move(stepper), std::move(navigator), m_logger->cloneWithSuffix("Prop"));
83 
84  // Using the CKF propagator as extrapolator
85  detail::Extrapolator extrapolator = propagator;
86 
87  // most trivial measurement selector
88  Acts::MeasurementSelectorCuts measurementSelectorCuts({-4.0, 4.0});
89  Acts::MeasurementSelector measurementSelector(measurementSelectorCuts);
90 
91 
92  // update once shared code for configuring this is available
93  Acts::TrackSelector::EtaBinnedConfig trackSelectorCfg(std::vector<double>({0, 4}));
94  trackSelectorCfg.cutSets[0].ptMin = 1000;
95  trackSelectorCfg.cutSets[0].ptMax = 1000000;
96  trackSelectorCfg.cutSets[0].minMeasurements = 3;
97  trackSelectorCfg.cutSets[0].maxHoles = 1;
98  trackSelectorCfg.cutSets[0].maxOutliers = 1;
99  trackSelectorCfg.cutSets[0].maxSharedHits = 1;
100  trackSelectorCfg.cutSets[0].maxChi2 = 25.;
101 
102  detail::CKF_config ckfConfig{
103  std::move(extrapolator),
104  {std::move(propagator), m_logger->cloneWithSuffix("CKF")},
105  measurementSelector,
106  {},
107  trackSelectorCfg};
108 
109  m_ckfConfig = std::make_unique<detail::CKF_config>(std::move(ckfConfig));
110  return StatusCode::SUCCESS;
111  }
112 
113 
114 
115 
116  StatusCode TrackExtensionAlg::execute(const EventContext& context) const {
118 
119  // track finding goes here
120  ActsTrk::MutableTrackContainer trackContainer;
121  Acts::VectorTrackContainer trackBackend;
122  Acts::VectorMultiTrajectory trackStateBackend;
123  detail::RecoTrackContainer tracksContainerTemp(trackBackend, trackStateBackend);
124  std::shared_ptr<Acts::PerigeeSurface> perigeeSurface = Acts::Surface::makeShared<Acts::PerigeeSurface>(Acts::Vector3::Zero());
125 
126  Acts::GeometryContext tgContext = m_trackingGeometryTool->getGeometryContext(context).context();
127  Acts::MagneticFieldContext mfContext = m_extrapolationTool->getMagneticFieldContext(context);
128 
130  detectorElementToGeometryIdMap{m_detectorElementToGeometryIdMapKey, context};
131  ATH_CHECK(detectorElementToGeometryIdMap.isValid());
132 
133  const Acts::TrackingGeometry *
134  acts_tracking_geometry = m_trackingGeometryTool->trackingGeometry().get();
135  ATH_CHECK( acts_tracking_geometry != nullptr);
136 
137  detail::TrackFindingMeasurements measurements = collectMeasurements(context, **detectorElementToGeometryIdMap);
138 
140  Acts::SourceLinkAccessorDelegate<ActsTrk::detail::UncalibSourceLinkAccessor::Iterator> slAccessorDelegate;
141  slAccessorDelegate.connect<&ActsTrk::detail::UncalibSourceLinkAccessor::range>(&slAccessor);
142 
143  Acts::PropagatorPlainOptions plainOptions(tgContext, mfContext);
144  plainOptions.maxSteps = 1000;
145  plainOptions.direction= m_propagateForward ? Acts::Direction::Forward : Acts::Direction::Backward;
146 
147 
149  mfContext,
151  slAccessorDelegate,
152  m_ckfConfig->ckfExtensions,
153  plainOptions,
154  perigeeSurface.get());
155 
157  *acts_tracking_geometry,
158  **detectorElementToGeometryIdMap,
161  options.extensions.calibrator.connect<&detail::OnTrackCalibrator<detail::RecoTrackStateContainer>::calibrate>(&calibrator);
162  if ( not m_truthParticlesKey.empty() ) {
163  auto truthHandle = SG::ReadHandle(m_truthParticlesKey, context);
164  for ( auto truthParticle: *truthHandle ) {
165  ATH_MSG_DEBUG("truth: eta: " << truthParticle->eta() << " phi: " << truthParticle->phi() << " pt: " << truthParticle->pt());
166  }
167  }
168  ATH_MSG_DEBUG("Size of proto tracks collection " << protoTracksHandle->size());
169  for (const ActsTrk::ProtoTrack& protoTrack : *protoTracksHandle) {
170  if(protoTrack.measurements.empty()) continue;
171 
172  const Acts::Surface* refSurface = ActsTrk::getSurfaceOfMeasurement(*acts_tracking_geometry, **detectorElementToGeometryIdMap, *protoTrack.measurements[0]);
173 // ActsTrk::getSurfaceOfMeasurement( *m_trackingGeometryTool->trackingGeometry(), **detectorElementToGeometryIdMap, *protoTrack.measurements[0]);
174 
175  auto res = m_actsFitter->fit(context, protoTrack.measurements,*protoTrack.parameters,
176  m_trackingGeometryTool->getGeometryContext(context).context(),
177  m_extrapolationTool->getMagneticFieldContext(context),
178  Acts::CalibrationContext(),
179  **detectorElementToGeometryIdMap,
180  refSurface);
181  if(!res) continue;
182  if (res->size() == 0 ) continue;
183  ATH_MSG_DEBUG(".......Done fit of track with "<< protoTrack.measurements.size() << " measurements");
184  const auto trackProxy = res->getTrack(0);
185  if (not trackProxy.hasReferenceSurface()) {
186  ATH_MSG_INFO("There is not reference surface for this track");
187  continue;
188  }
189  Acts::BoundTrackParameters parametersAtRefSurface( trackProxy.referenceSurface().getSharedPtr(),
190  trackProxy.parameters(),
191  trackProxy.covariance(),
192  trackProxy.particleHypothesis());
193 
194 
195 
196  ATH_MSG_DEBUG("proto track: eta: " << -1 * log(tan( parametersAtRefSurface.theta() * 0.5)) << " phi: " << parametersAtRefSurface.phi() << " pt:" << abs(1./protoTrack.parameters->qOverP() * sin(protoTrack.parameters->theta())));
197  ATH_MSG_DEBUG("Extending proto track of " << protoTrack.measurements.size() << " measurements");
198  auto result = m_ckfConfig->ckf.findTracks(parametersAtRefSurface, options,
199  tracksContainerTemp);
200 
201 
202  ATH_MSG_DEBUG("Built " << tracksContainerTemp.size() << " tracks from it");
203  for (detail::RecoTrackContainer::TrackProxy tempTrackProxy : tracksContainerTemp) {
204  ActsTrk::MutableTrackContainer::TrackProxy destTrackProxy = trackContainer.makeTrack();
205  ATH_MSG_DEBUG("Reco MTJ size " << trackStateBackend.size() );
206  for ( size_t stateIndex=0; stateIndex < trackStateBackend.size(); ++stateIndex) {
207  auto state = trackStateBackend.getTrackState(stateIndex);
208  if (m_trackStatePrinter.isSet()) m_trackStatePrinter->printTrackState(tgContext, state, measurements.measurementContainerOffsets(), false);
209  }
210  ATH_MSG_DEBUG("Track has: " << tempTrackProxy.nMeasurements() << " measurements ");
211  ATH_MSG_DEBUG("track: eta: " << -1 * log(tan( tempTrackProxy.theta() * 0.5)) << " phi: " << tempTrackProxy.phi() << " pt:" << abs(1./tempTrackProxy.qOverP() * sin(protoTrack.parameters->theta())));
212 
213  destTrackProxy.copyFrom(tempTrackProxy);
214  }
215  }
216 
217  std::unique_ptr<ActsTrk::TrackContainer> constTracksContainer =
219  std::move(trackContainer),
220  m_trackingGeometryTool->getGeometryContext(context).context(),
221  context);
222  SG::WriteHandle<ActsTrk::TrackContainer> trackContainerHandle(m_trackContainerKey, context);
223  ATH_CHECK(trackContainerHandle.record(std::move(constTracksContainer)));
224 
225  return StatusCode::SUCCESS;
226  }
227 
228 
230  const EventContext& context,
231  const ActsTrk::DetectorElementToActsGeometryIdMap &detectorElementToGeometryIdMap) const {
232  SG::ReadHandle<xAOD::PixelClusterContainer> pixelClustersHandle(m_pixelClusters, context);
233 
234  detail::TrackFindingMeasurements measurements(1u /* only one measurement collection: pixel clusters*/);
235  ATH_MSG_DEBUG("Measurements (pixels only) size: " << pixelClustersHandle->size());
236  // potential TODO: filtering only certain layers
237  measurements.addMeasurements(0, *pixelClustersHandle, detectorElementToGeometryIdMap);
238  return measurements;
239  }
240 } // EOF namespace
ActsTrk::TrackExtensionAlg::m_pixelClusters
SG::ReadHandleKey< xAOD::PixelClusterContainer > m_pixelClusters
Definition: TrackExtensionAlg.h:59
ActsTrk::TrackExtensionAlg::CKFOptions
Acts::CombinatorialKalmanFilterOptions< ActsTrk::detail::UncalibSourceLinkAccessor::Iterator, detail::RecoTrackContainer > CKFOptions
Definition: TrackExtensionAlg.h:56
SCT_ClusterOnTrack.h
ActsTrk::TrackExtensionAlg::m_extrapolationTool
ToolHandle< IActsExtrapolationTool > m_extrapolationTool
Definition: TrackExtensionAlg.h:78
get_generator_info.result
result
Definition: get_generator_info.py:21
TrackParameters.h
ActsTrk::TrackExtensionAlg::execute
virtual StatusCode execute(const EventContext &context) const override
Definition: TrackExtensionAlg.cxx:116
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
SG::ReadCondHandle
Definition: ReadCondHandle.h:44
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
ActsTrk::detail::TrackFindingMeasurements
Definition: TrackFindingMeasurements.h:17
ActsTrk::DetectorElementToActsGeometryIdMap
Definition: DetectorElementToActsGeometryIdMap.h:31
ActsGeometryContext.h
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
ActsTrk::detail::Navigator
Acts::Navigator Navigator
Definition: Tracking/Acts/ActsTrackReconstruction/src/detail/Definitions.h:31
ActsTrk::ProtoTrack
Definition: ProtoTrack.h:15
ActsTrk::detail::Extrapolator
Propagator Extrapolator
Definition: Tracking/Acts/ActsTrackReconstruction/src/detail/Definitions.h:34
ATLASMagneticFieldWrapper.h
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
ActsTrk::TrackExtensionAlg::m_actsFitter
ToolHandle< ActsTrk::IFitterTool > m_actsFitter
Definition: TrackExtensionAlg.h:88
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
SG::VarHandleKey::empty
bool empty() const
Test if the key is blank.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:150
ActsTrk::detail::RecoTrackContainer
Acts::TrackContainer< Acts::VectorTrackContainer, Acts::VectorMultiTrajectory > RecoTrackContainer
Definition: Tracking/Acts/ActsTrackReconstruction/src/detail/Definitions.h:23
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:77
ActsTrk::TrackExtensionAlg::m_trackContainerKey
SG::WriteHandleKey< ActsTrk::TrackContainer > m_trackContainerKey
Definition: TrackExtensionAlg.h:66
ActsTrk::MutableTrackContainerHandlesHelper::initialize
StatusCode initialize(const std::string &prefix)
Sets up the handles.
Definition: TrackContainerHandlesHelper.cxx:51
ActsTrk::getSurfaceOfMeasurement
const Acts::Surface * getSurfaceOfMeasurement(const Acts::TrackingGeometry &tracking_geometry, const DetectorElementToActsGeometryIdMap &detector_element_to_geoid, const xAOD::UncalibratedMeasurement &measurement)
Definition: SurfaceOfMeasurementUtil.h:13
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:83
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
ActsTrk::TrackExtensionAlg::initialize
virtual StatusCode initialize() override
Definition: TrackExtensionAlg.cxx:57
OnTrackCalibrator.h
ActsTrk::TrackExtensionAlg::m_stripCalibTool
ToolHandle< ActsTrk::IOnTrackCalibratorTool< detail::RecoTrackStateContainer > > m_stripCalibTool
Definition: TrackExtensionAlg.h:84
SCT_ClusterCollection.h
ActsTrk::TrackExtensionAlg::m_propagateForward
Gaudi::Property< bool > m_propagateForward
Definition: TrackExtensionAlg.h:91
ActsTrk::detail::GenUncalibSourceLinkAccessor
Accessor for the above source link container.
Definition: AtlasUncalibSourceLinkAccessor.h:105
ActsTrk::TrackExtensionAlg::m_trackingGeometryTool
ToolHandle< IActsTrackingGeometryTool > m_trackingGeometryTool
Definition: TrackExtensionAlg.h:76
ActsTrk::detail::GenUncalibSourceLinkAccessor::range
std::pair< Iterator, Iterator > range(const Acts::Surface &surface) const
Definition: AtlasUncalibSourceLinkAccessor.h:161
ActsTrk::TrackExtensionAlg::m_truthParticlesKey
SG::ReadHandleKey< xAOD::TruthParticleContainer > m_truthParticlesKey
Definition: TrackExtensionAlg.h:63
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
ActsTrk::detail::CKF_config
Definition: Tracking/Acts/ActsTrackReconstruction/src/detail/Definitions.h:40
ActsTrk::detail::TrackFindingMeasurements::measurementContainerOffsets
std::vector< std::pair< const xAOD::UncalibratedMeasurementContainer *, std::size_t > > measurementContainerOffsets() const
Definition: TrackFindingMeasurements.cxx:82
res
std::pair< std::vector< unsigned int >, bool > res
Definition: JetGroupProductTest.cxx:14
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
ActsTrk::TrackExtensionAlg::TrackExtensionAlg
TrackExtensionAlg(const std::string &name, ISvcLocator *pSvcLocator)
Definition: TrackExtensionAlg.cxx:53
python.AtlRunQueryLib.options
options
Definition: AtlRunQueryLib.py:379
drawFromPickle.tan
tan
Definition: drawFromPickle.py:36
TrackSummary.h
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
ActsDetectorElement.h
PixelClusterCollection.h
Monitored.h
Header file to be included by clients of the Monitored infrastructure.
ActsTrk::TrackExtensionAlg::m_detectorElementToGeometryIdMapKey
SG::ReadCondHandleKey< ActsTrk::DetectorElementToActsGeometryIdMap > m_detectorElementToGeometryIdMapKey
Definition: TrackExtensionAlg.h:71
ActsTrk::TrackExtensionAlg::m_protoTrackCollectionKey
SG::ReadHandleKey< ActsTrk::ProtoTrackCollection > m_protoTrackCollectionKey
Definition: TrackExtensionAlg.h:61
ActsTrk::detail::TrackFindingMeasurements::addMeasurements
void addMeasurements(std::size_t typeIndex, const xAOD::UncalibratedMeasurementContainer &clusterContainer, const DetectorElementToActsGeometryIdMap &detectorElementToGeoid)
Definition: TrackFindingMeasurements.cxx:14
ActsTrk::TrackExtensionAlg::m_pixelCalibTool
ToolHandle< ActsTrk::IOnTrackCalibratorTool< detail::RecoTrackStateContainer > > m_pixelCalibTool
Definition: TrackExtensionAlg.h:81
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
RIO_OnTrack.h
AtlasMeasurementSelector.h
SG::CondHandleKey::initialize
StatusCode initialize(bool used=true)
WriteCaloSwCorrections.cfg
cfg
Definition: WriteCaloSwCorrections.py:23
ActsTrk::detail::TrackFindingMeasurements::measurementRanges
const ActsTrk::detail::MeasurementRangeList & measurementRanges() const
SurfaceOfMeasurementUtil.h
ActsTrk::detail::Stepper
Acts::SympyStepper Stepper
Adapted from Acts Examples/Algorithms/TrackFinding/src/TrackFindingAlgorithmFunction....
Definition: Tracking/Acts/ActsTrackReconstruction/src/detail/Definitions.h:30
ActsTrk::TrackExtensionAlg::m_tracksBackendHandlesHelper
ActsTrk::MutableTrackContainerHandlesHelper m_tracksBackendHandlesHelper
Definition: TrackExtensionAlg.h:69
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
FitterHelperFunctions.h
ActsTrk::detail::Propagator
Acts::Propagator< Stepper, Navigator > Propagator
Definition: Tracking/Acts/ActsTrackReconstruction/src/detail/Definitions.h:32
SG::WriteHandle::record
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
TrackFindingAlg.h
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
ActsTrk::TrackExtensionAlg::m_ckfConfig
std::unique_ptr< detail::CKF_config > m_ckfConfig
Definition: TrackExtensionAlg.h:94
ActsTrk
The AlignStoreProviderAlg loads the rigid alignment corrections and pipes them through the readout ge...
Definition: MuonDetectorBuilderTool.cxx:54
TrackExtensionAlg.h
TableUtils.h
ActsTrk::TrackExtensionAlg::m_logger
std::unique_ptr< const Acts::Logger > m_logger
Definition: TrackExtensionAlg.h:95
ActsTrk::detail::OnTrackCalibrator
Definition: IOnTrackCalibratorTool.h:14
ToolStore.h
drawFromPickle.sin
sin
Definition: drawFromPickle.py:36
Logger.h
PixelClusterOnTrack.h
ActsTrk::TrackExtensionAlg::m_trackStatePrinter
ToolHandle< ActsTrk::TrackStatePrinterTool > m_trackStatePrinter
Definition: TrackExtensionAlg.h:86
ActsTrk::MutableTrackContainer
Definition: TrackContainer.h:122
SG::AllowEmpty
@ AllowEmpty
Definition: StoreGate/StoreGate/VarHandleKey.h:30
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
TrackContainer.h
ActsTrk::TrackExtensionAlg::m_calibrationContext
Acts::CalibrationContext m_calibrationContext
Definition: TrackExtensionAlg.h:103
generate::Zero
void Zero(TH1D *hin)
Definition: generate.cxx:32
ActsTrk::TrackExtensionAlg::collectMeasurements
detail::TrackFindingMeasurements collectMeasurements(const EventContext &context, const ActsTrk::DetectorElementToActsGeometryIdMap &detectorElementToGeometryIdMap) const
Definition: TrackExtensionAlg.cxx:229