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()}));
68  ATH_CHECK(m_hgtdCalibTool.retrieve(EnableTool{not m_hgtdCalibTool.empty()}));
70  ATH_CHECK(m_trackStatePrinter.retrieve(EnableTool{not m_trackStatePrinter.empty()}));
71  ATH_CHECK(m_actsFitter.retrieve());
73 
74  auto magneticField = std::make_unique<ATLASMagneticFieldWrapper>();
75  std::shared_ptr<const Acts::TrackingGeometry> trackingGeometry = m_trackingGeometryTool->trackingGeometry();
76 
77  detail::Stepper stepper(std::move(magneticField));
78  detail::Navigator::Config cfg{trackingGeometry};
79  cfg.resolvePassive = false;
80  cfg.resolveMaterial = true;
81  cfg.resolveSensitive = true;
82  detail::Navigator navigator(cfg, m_logger->cloneWithSuffix("Navigator"));
83  detail::Propagator propagator(std::move(stepper), std::move(navigator), m_logger->cloneWithSuffix("Prop"));
84 
85  // Using the CKF propagator as extrapolator
86  detail::Extrapolator extrapolator = propagator;
87 
88  // most trivial measurement selector
89  Acts::MeasurementSelectorCuts measurementSelectorCuts({-4.0, 4.0});
90  Acts::MeasurementSelector measurementSelector(measurementSelectorCuts);
91 
92 
93  // update once shared code for configuring this is available
94  Acts::TrackSelector::EtaBinnedConfig trackSelectorCfg(std::vector<double>({0, 4}));
95  trackSelectorCfg.cutSets[0].ptMin = 1000;
96  trackSelectorCfg.cutSets[0].ptMax = 1000000;
97  trackSelectorCfg.cutSets[0].minMeasurements = 3;
98  trackSelectorCfg.cutSets[0].maxHoles = 1;
99  trackSelectorCfg.cutSets[0].maxOutliers = 1;
100  trackSelectorCfg.cutSets[0].maxSharedHits = 1;
101  trackSelectorCfg.cutSets[0].maxChi2 = 25.;
102 
103  detail::CKF_config ckfConfig{
104  std::move(extrapolator),
105  {std::move(propagator), m_logger->cloneWithSuffix("CKF")},
106  measurementSelector,
107  {},
108  trackSelectorCfg};
109 
110  m_ckfConfig = std::make_unique<detail::CKF_config>(std::move(ckfConfig));
111  return StatusCode::SUCCESS;
112  }
113 
114 
115 
116 
117  StatusCode TrackExtensionAlg::execute(const EventContext& context) const {
119 
120  // track finding goes here
121  ActsTrk::MutableTrackContainer trackContainer;
122  Acts::VectorTrackContainer trackBackend;
123  Acts::VectorMultiTrajectory trackStateBackend;
124  detail::RecoTrackContainer tracksContainerTemp(trackBackend, trackStateBackend);
125  std::shared_ptr<Acts::PerigeeSurface> perigeeSurface = Acts::Surface::makeShared<Acts::PerigeeSurface>(Acts::Vector3::Zero());
126 
127  Acts::GeometryContext tgContext = m_trackingGeometryTool->getGeometryContext(context).context();
128  Acts::MagneticFieldContext mfContext = m_extrapolationTool->getMagneticFieldContext(context);
129 
131  detectorElementToGeometryIdMap{m_detectorElementToGeometryIdMapKey, context};
132  ATH_CHECK(detectorElementToGeometryIdMap.isValid());
133 
134  const Acts::TrackingGeometry *
135  acts_tracking_geometry = m_trackingGeometryTool->trackingGeometry().get();
136  ATH_CHECK( acts_tracking_geometry != nullptr);
137 
138  detail::TrackFindingMeasurements measurements = collectMeasurements(context, **detectorElementToGeometryIdMap);
139 
141  Acts::SourceLinkAccessorDelegate<ActsTrk::detail::UncalibSourceLinkAccessor::Iterator> slAccessorDelegate;
142  slAccessorDelegate.connect<&ActsTrk::detail::UncalibSourceLinkAccessor::range>(&slAccessor);
143 
144  Acts::PropagatorPlainOptions plainOptions(tgContext, mfContext);
145  plainOptions.maxSteps = 1000;
146  plainOptions.direction= m_propagateForward ? Acts::Direction::Forward : Acts::Direction::Backward;
147 
148 
150  mfContext,
152  slAccessorDelegate,
153  m_ckfConfig->ckfExtensions,
154  plainOptions,
155  perigeeSurface.get());
156 
158  *acts_tracking_geometry,
159  **detectorElementToGeometryIdMap,
163  options.extensions.calibrator.connect<&detail::OnTrackCalibrator<detail::RecoTrackStateContainer>::calibrate>(&calibrator);
164 
165  if ( not m_truthParticlesKey.empty() ) {
166  auto truthHandle = SG::ReadHandle(m_truthParticlesKey, context);
167  for ( auto truthParticle: *truthHandle ) {
168  ATH_MSG_DEBUG("truth: eta: " << truthParticle->eta() << " phi: " << truthParticle->phi() << " pt: " << truthParticle->pt());
169  }
170  }
171  ATH_MSG_DEBUG("Size of proto tracks collection " << protoTracksHandle->size());
172  for (const ActsTrk::ProtoTrack& protoTrack : *protoTracksHandle) {
173  if(protoTrack.measurements.empty()) continue;
174 
175  const Acts::Surface* refSurface = ActsTrk::getSurfaceOfMeasurement(*acts_tracking_geometry, **detectorElementToGeometryIdMap, *protoTrack.measurements[0]);
176 // ActsTrk::getSurfaceOfMeasurement( *m_trackingGeometryTool->trackingGeometry(), **detectorElementToGeometryIdMap, *protoTrack.measurements[0]);
177 
178  auto res = m_actsFitter->fit(context, protoTrack.measurements,*protoTrack.parameters,
179  m_trackingGeometryTool->getGeometryContext(context).context(),
180  m_extrapolationTool->getMagneticFieldContext(context),
181  Acts::CalibrationContext(),
182  **detectorElementToGeometryIdMap,
183  refSurface);
184  if(!res) continue;
185  if (res->size() == 0 ) continue;
186  ATH_MSG_DEBUG(".......Done fit of track with "<< protoTrack.measurements.size() << " measurements");
187  const auto trackProxy = res->getTrack(0);
188  if (not trackProxy.hasReferenceSurface()) {
189  ATH_MSG_INFO("There is not reference surface for this track");
190  continue;
191  }
192  Acts::BoundTrackParameters parametersAtRefSurface( trackProxy.referenceSurface().getSharedPtr(),
193  trackProxy.parameters(),
194  trackProxy.covariance(),
195  trackProxy.particleHypothesis());
196 
197 
198 
199  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())));
200  ATH_MSG_DEBUG("Extending proto track of " << protoTrack.measurements.size() << " measurements");
201  auto result = m_ckfConfig->ckf.findTracks(parametersAtRefSurface, options,
202  tracksContainerTemp);
203 
204 
205  ATH_MSG_DEBUG("Built " << tracksContainerTemp.size() << " tracks from it");
206  for (detail::RecoTrackContainer::TrackProxy tempTrackProxy : tracksContainerTemp) {
207  ActsTrk::MutableTrackContainer::TrackProxy destTrackProxy = trackContainer.makeTrack();
208  ATH_MSG_DEBUG("Reco MTJ size " << trackStateBackend.size() );
209  for ( size_t stateIndex=0; stateIndex < trackStateBackend.size(); ++stateIndex) {
210  auto state = trackStateBackend.getTrackState(stateIndex);
211  if (m_trackStatePrinter.isSet()) m_trackStatePrinter->printTrackState(tgContext, state, measurements.measurementContainerOffsets(), false);
212  }
213  ATH_MSG_DEBUG("Track has: " << tempTrackProxy.nMeasurements() << " measurements ");
214  ATH_MSG_DEBUG("track: eta: " << -1 * log(tan( tempTrackProxy.theta() * 0.5)) << " phi: " << tempTrackProxy.phi() << " pt:" << abs(1./tempTrackProxy.qOverP() * sin(protoTrack.parameters->theta())));
215 
216  destTrackProxy.copyFrom(tempTrackProxy);
217  }
218  }
219 
220  std::unique_ptr<ActsTrk::TrackContainer> constTracksContainer =
222  std::move(trackContainer),
223  m_trackingGeometryTool->getGeometryContext(context).context(),
224  context);
225  SG::WriteHandle<ActsTrk::TrackContainer> trackContainerHandle(m_trackContainerKey, context);
226  ATH_CHECK(trackContainerHandle.record(std::move(constTracksContainer)));
227 
228  return StatusCode::SUCCESS;
229  }
230 
231 
233  const EventContext& context,
234  const ActsTrk::DetectorElementToActsGeometryIdMap &detectorElementToGeometryIdMap) const {
235  SG::ReadHandle<xAOD::PixelClusterContainer> pixelClustersHandle(m_pixelClusters, context);
236 
237  detail::TrackFindingMeasurements measurements(1u /* only one measurement collection: pixel clusters*/);
238  ATH_MSG_DEBUG("Measurements (pixels only) size: " << pixelClustersHandle->size());
239  // potential TODO: filtering only certain layers
240  measurements.addMeasurements(0, *pixelClustersHandle, detectorElementToGeometryIdMap);
241  return measurements;
242  }
243 } // 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:117
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:91
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:94
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:228
RIO_OnTrack.h
ActsTrk::TrackExtensionAlg::m_hgtdCalibTool
ToolHandle< ActsTrk::IOnTrackCalibratorTool< detail::RecoTrackStateContainer > > m_hgtdCalibTool
Definition: TrackExtensionAlg.h:87
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:97
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:98
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:89
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:106
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:232