ATLAS Offline Software
ActsExtrapolationTool.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
8 #include "GaudiKernel/IInterface.h"
9 
10 // PACKAGE
14 #include "ActsInterop/Logger.h"
15 
16 // ACTS
17 #include "Acts/Propagator/Navigator.hpp"
18 #include "Acts/Propagator/EigenStepper.hpp"
19 #include "Acts/Propagator/Propagator.hpp"
20 #include "Acts/Propagator/ActorList.hpp"
21 #include <Acts/Propagator/StraightLineStepper.hpp>
22 #include "Acts/Propagator/EigenStepperDefaultExtension.hpp"
23 
24 #include "Acts/Utilities/Logger.hpp"
25 
26 // BOOST
27 #include <boost/variant/variant.hpp>
28 #include <boost/variant/apply_visitor.hpp>
29 #include <boost/variant/static_visitor.hpp>
30 
31 // STL
32 #include <iostream>
33 #include <memory>
34 
35 namespace ActsExtrapolationDetail {
36 
37 
38  using VariantPropagatorBase = boost::variant<
39  Acts::Propagator<Acts::EigenStepper<Acts::EigenStepperDefaultExtension>, Acts::Navigator>>;
40 
42  {
43  public:
45  };
46 
47 }
48 
50 
51 
52 ActsExtrapolationTool::ActsExtrapolationTool(const std::string& type, const std::string& name,
53  const IInterface* parent)
54  : base_class(type, name, parent)
55 {
56 }
57 
58 
60 {
61 }
62 
63 
66 {
67  using namespace std::literals::string_literals;
68  m_logger = makeActsAthenaLogger(this, std::string("ActsExtrapTool"), std::string("Prop"));
69 
70  ATH_MSG_INFO("Initializing ACTS extrapolation");
71 
72  m_logger = makeActsAthenaLogger(this, std::string("Prop"), std::string("ActsExtrapTool"));
73 
74  ATH_CHECK( m_trackingGeometryTool.retrieve() );
75  std::shared_ptr<const Acts::TrackingGeometry> trackingGeometry
76  = m_trackingGeometryTool->trackingGeometry();
77 
78  Acts::Navigator navigator( Acts::Navigator::Config{ trackingGeometry } );
79 
80  if (m_fieldMode == "ATLAS"s) {
81  ATH_MSG_INFO("Using ATLAS magnetic field service");
82  using Stepper = Acts::EigenStepper<Acts::EigenStepperDefaultExtension>;
83 
85  auto bField = std::make_shared<ATLASMagneticFieldWrapper>();
86 
87  auto stepper = Stepper(std::move(bField));
88  auto propagator = Acts::Propagator<Stepper, Acts::Navigator>(std::move(stepper),
89  std::move(navigator),
90  logger().cloneWithSuffix("Prop"));
91  m_varProp = std::make_unique<VariantPropagator>(propagator);
92  }
93  else if (m_fieldMode == "Constant") {
94  if (m_constantFieldVector.value().size() != 3)
95  {
96  ATH_MSG_ERROR("Incorrect field vector size. Using empty field.");
97  return StatusCode::FAILURE;
98  }
99 
100  Acts::Vector3 constantFieldVector = Acts::Vector3(m_constantFieldVector[0],
103 
104  ATH_MSG_INFO("Using constant magnetic field: (Bx, By, Bz) = (" << m_constantFieldVector[0] << ", "
105  << m_constantFieldVector[1] << ", "
106  << m_constantFieldVector[2] << ")");
107 
108  using Stepper = Acts::EigenStepper<Acts::EigenStepperDefaultExtension>;
109 
110  auto bField = std::make_shared<Acts::ConstantBField>(constantFieldVector);
111  auto stepper = Stepper(std::move(bField));
112  auto propagator = Acts::Propagator<Stepper, Acts::Navigator>(std::move(stepper),
113  std::move(navigator));
114  m_varProp = std::make_unique<VariantPropagator>(propagator);
115  }
116 
117  ATH_MSG_INFO("ACTS extrapolation successfully initialized");
118  return StatusCode::SUCCESS;
119 }
120 
121 
124  const Acts::BoundTrackParameters& startParameters,
125  Acts::Direction navDir /*= Acts::Direction::Forward*/,
126  double pathLimit /*= std::numeric_limits<double>::max()*/) const
127 {
128 
129  ATH_MSG_VERBOSE(name() << "::" << __FUNCTION__ << " begin");
130 
131  Acts::MagneticFieldContext mctx = getMagneticFieldContext(ctx);
132  const ActsGeometryContext& geo_ctx
133  = m_trackingGeometryTool->getGeometryContext(ctx);
134  auto anygctx = geo_ctx.context();
135 
137 
138  auto res = boost::apply_visitor([&](const auto& propagator) -> ResultType {
139  using Propagator = std::decay_t<decltype(propagator)>;
140 
141  // Action list and abort list
142  using ActorList =
143  Acts::ActorList<SteppingLogger, Acts::MaterialInteractor, EndOfWorld>;
144  using Options = typename Propagator::template Options<ActorList>;
145 
146  Options options = prepareOptions<Options>(anygctx, mctx, startParameters, navDir, pathLimit);
147 
148  auto result = propagator.propagate(startParameters, options);
149  if (!result.ok()) {
150  return result.error();
151  }
152  auto& propRes = *result;
153 
154  auto steppingResults = propRes.template get<SteppingLogger::result_type>();
155  auto materialResult = propRes.template get<Acts::MaterialInteractor::result_type>();
156  output.first = std::move(steppingResults.steps);
157  output.second = std::move(materialResult);
158  // try to force return value optimization, not sure this is necessary
159  return std::move(output);
160  }, *m_varProp);
161 
162  if (!res.ok()) {
163  ATH_MSG_ERROR("Got error during propagation: "
164  << res.error() << " " << res.error().message()
165  << ". Returning empty step vector.");
166  return {};
167  }
168  output = std::move(*res);
169 
170  ATH_MSG_VERBOSE("Collected " << output.first.size() << " steps");
171  if(output.first.size() == 0) {
172  ATH_MSG_WARNING("ZERO steps returned by stepper, that is not typically a good sign");
173  }
174 
175  ATH_MSG_VERBOSE(name() << "::" << __FUNCTION__ << " end");
176 
177  return output;
178 }
179 
180 
181 
182 std::optional<const Acts::CurvilinearTrackParameters>
183 ActsExtrapolationTool::propagate(const EventContext& ctx,
184  const Acts::BoundTrackParameters& startParameters,
185  Acts::Direction navDir /*= Acts::Direction::Forward*/,
186  double pathLimit /*= std::numeric_limits<double>::max()*/) const
187 {
188  ATH_MSG_VERBOSE(name() << "::" << __FUNCTION__ << " begin");
189 
190  Acts::MagneticFieldContext mctx = getMagneticFieldContext(ctx);
191  const ActsGeometryContext& geo_ctx
192  = m_trackingGeometryTool->getGeometryContext(ctx);
193  auto anygctx = geo_ctx.context();
194 
195  auto parameters = boost::apply_visitor([&](const auto& propagator) -> std::optional<const Acts::CurvilinearTrackParameters> {
196  using Propagator = std::decay_t<decltype(propagator)>;
197 
198  // Action list and abort list
199  using ActorList =
200  Acts::ActorList<Acts::MaterialInteractor, EndOfWorld>;
201  using Options = typename Propagator::template Options<ActorList>;
202 
203  Options options = prepareOptions<Options>(anygctx, mctx, startParameters, navDir, pathLimit);
204 
205 
206  auto result = propagator.propagate(startParameters, options);
207  if (!result.ok()) {
208  ATH_MSG_ERROR("Got error during propagation:" << result.error()
209  << ". Returning empty parameters.");
210  return std::nullopt;
211  }
212  return result.value().endParameters;
213  }, *m_varProp);
214 
215  return parameters;
216 }
217 
220  const Acts::BoundTrackParameters& startParameters,
221  const Acts::Surface& target,
222  Acts::Direction navDir /*= Acts::Direction::Forward*/,
223  double pathLimit /*= std::numeric_limits<double>::max()*/) const
224 {
225  ATH_MSG_VERBOSE(name() << "::" << __FUNCTION__ << " begin");
226 
228 
229  Acts::MagneticFieldContext mctx = getMagneticFieldContext(ctx);
230  const ActsGeometryContext& geo_ctx
231  = m_trackingGeometryTool->getGeometryContext(ctx);
232  auto anygctx = geo_ctx.context();
233 
234  auto res = boost::apply_visitor([&](const auto& propagator) -> ResultType {
235  using Propagator = std::decay_t<decltype(propagator)>;
236 
237  // Action list and abort list
238  using ActorList =
239  Acts::ActorList<SteppingLogger, Acts::MaterialInteractor>;
240  using Options = typename Propagator::template Options<ActorList>;
241 
242  Options options = prepareOptions<Options>(anygctx, mctx, startParameters, navDir, pathLimit);
243  auto result = target.type() == Acts::Surface::Perigee ?
244  propagator.template propagate<Acts::BoundTrackParameters, Options,
245  Acts::ForcedSurfaceReached, Acts::PathLimitReached>(startParameters, target, options) :
246  propagator.template propagate<Acts::BoundTrackParameters, Options,
247  Acts::SurfaceReached, Acts::PathLimitReached>(startParameters, target, options);
248 
249 
250  if (!result.ok()) {
251  return result.error();
252  }
253  auto& propRes = *result;
254 
255  auto steppingResults = propRes.template get<SteppingLogger::result_type>();
256  auto materialResult = propRes.template get<Acts::MaterialInteractor::result_type>();
257  output.first = std::move(steppingResults.steps);
258  output.second = std::move(materialResult);
259  return std::move(output);
260  }, *m_varProp);
261 
262  if (!res.ok()) {
263  ATH_MSG_ERROR("Got error during propagation:" << res.error()
264  << ". Returning empty step vector.");
265  return {};
266  }
267  output = std::move(*res);
268 
269  ATH_MSG_VERBOSE("Collected " << output.first.size() << " steps");
270  ATH_MSG_VERBOSE(name() << "::" << __FUNCTION__ << " end");
271 
272  return output;
273 }
274 
275 std::optional<const Acts::BoundTrackParameters>
276 ActsExtrapolationTool::propagate(const EventContext& ctx,
277  const Acts::BoundTrackParameters& startParameters,
278  const Acts::Surface& target,
279  Acts::Direction navDir /*= Acts::Direction::Forward*/,
280  double pathLimit /*= std::numeric_limits<double>::max()*/) const
281 {
282 
283  ATH_MSG_VERBOSE(name() << "::" << __FUNCTION__ << " begin");
284 
285  Acts::MagneticFieldContext mctx = getMagneticFieldContext(ctx);
286  const ActsGeometryContext& geo_ctx
287  = m_trackingGeometryTool->getGeometryContext(ctx);
288  auto anygctx = geo_ctx.context();
289 
290  auto parameters = boost::apply_visitor([&](const auto& propagator) -> std::optional<const Acts::BoundTrackParameters> {
291  using Propagator = std::decay_t<decltype(propagator)>;
292 
293  // Action list and abort list
294  using ActorList =
295  Acts::ActorList<Acts::MaterialInteractor>;
296  using Options = typename Propagator::template Options<ActorList>;
297 
298  Options options = prepareOptions<Options>(anygctx, mctx, startParameters, navDir, pathLimit);
299  auto result = target.type() == Acts::Surface::Perigee ?
300  propagator.template propagate<Acts::BoundTrackParameters, Options,
301  Acts::ForcedSurfaceReached, Acts::PathLimitReached>(startParameters, target, options) :
302  propagator.template propagate<Acts::BoundTrackParameters, Options,
303  Acts::SurfaceReached, Acts::PathLimitReached>(startParameters, target, options);
304  if (!result.ok()) {
305  ATH_MSG_ERROR("Got error during propagation: " << result.error()
306  << ". Returning empty parameters.");
307  return std::nullopt;
308  }
309  return result.value().endParameters;
310  }, *m_varProp);
311 
312  return parameters;
313 }
314 
315 Acts::MagneticFieldContext ActsExtrapolationTool::getMagneticFieldContext(const EventContext& ctx) const {
317  if (!readHandle.isValid()) {
318  std::stringstream msg;
319  msg << "Failed to retrieve magnetic field condition data " << m_fieldCacheCondObjInputKey.key() << ".";
320  throw std::runtime_error(msg.str());
321  }
322  const AtlasFieldCacheCondObj* fieldCondObj{*readHandle};
323 
324  return Acts::MagneticFieldContext(fieldCondObj);
325 }
326 
327 template<typename OptionsType>
328 OptionsType ActsExtrapolationTool::prepareOptions(const Acts::GeometryContext& gctx,
329  const Acts::MagneticFieldContext& mctx,
330  const Acts::BoundTrackParameters& startParameters,
331  Acts::Direction navDir,
332  double pathLimit) const {
333  using namespace Acts::UnitLiterals;
334  OptionsType options(gctx, mctx);
335 
336  options.pathLimit = pathLimit;
337  options.loopProtection
338  = (Acts::VectorHelpers::perp(startParameters.momentum())
339  < m_ptLoopers * 1_MeV);
340  options.maxSteps = m_maxStep;
341  options.direction = navDir;
342  options.stepping.maxStepSize = m_maxStepSize * 1_m;
343 
344  auto& mInteractor = options.actorList.template get<Acts::MaterialInteractor>();
345  mInteractor.multipleScattering = m_interactionMultiScatering;
346  mInteractor.energyLoss = m_interactionEloss;
347  mInteractor.recordInteractions = m_interactionRecord;
348 
349  return options;
350 }
ActsExtrapolationTool::propagate
virtual std::optional< const Acts::CurvilinearTrackParameters > propagate(const EventContext &ctx, const Acts::BoundTrackParameters &startParameters, Acts::Direction navDir=Acts::Direction::Forward, double pathLimit=std::numeric_limits< double >::max()) const override
Definition: ActsExtrapolationTool.cxx:183
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
get_generator_info.result
result
Definition: get_generator_info.py:21
SG::ReadCondHandle
Definition: ReadCondHandle.h:44
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
AtlasFieldCacheCondObj
Definition: AtlasFieldCacheCondObj.h:19
ActsExtrapolationTool::ActsExtrapolationTool
ActsExtrapolationTool(const std::string &type, const std::string &name, const IInterface *parent)
Definition: ActsExtrapolationTool.cxx:52
ActsGeometryContext.h
perp
Scalar perp() const
perp method - perpenticular length
Definition: AmgMatrixBasePlugin.h:44
ActsTrk::detail::Navigator
Acts::Navigator Navigator
Definition: Tracking/Acts/ActsTrackReconstruction/src/detail/Definitions.h:31
ActsExtrapolationTool::getMagneticFieldContext
virtual Acts::MagneticFieldContext getMagneticFieldContext(const EventContext &ctx) const override
Definition: ActsExtrapolationTool.cxx:315
taskman.template
dictionary template
Definition: taskman.py:317
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
ActsExtrapolationTool::ResultType
Acts::Result< ActsPropagationOutput > ResultType
Definition: ActsExtrapolationTool.h:67
ActsExtrapolationDetail::VariantPropagatorBase
boost::variant< Acts::Propagator< Acts::EigenStepper< Acts::EigenStepperDefaultExtension >, Acts::Navigator > > VariantPropagatorBase
Definition: ActsExtrapolationTool.cxx:39
ActsExtrapolationTool::propagationSteps
virtual ActsPropagationOutput propagationSteps(const EventContext &ctx, const Acts::BoundTrackParameters &startParameters, Acts::Direction navDir=Acts::Direction::Forward, double pathLimit=std::numeric_limits< double >::max()) const override
Definition: ActsExtrapolationTool.cxx:123
ActsExtrapolationTool.h
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
ActsGeometryContext::context
Acts::GeometryContext context() const
Definition: ActsGeometryContext.h:45
ActsExtrapolationTool::logger
const Acts::Logger & logger() const
Definition: ActsExtrapolationTool.h:114
ActsExtrapolationTool::m_maxStep
Gaudi::Property< double > m_maxStep
Definition: ActsExtrapolationTool.h:129
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
ActsExtrapolationTool::~ActsExtrapolationTool
~ActsExtrapolationTool()
Definition: ActsExtrapolationTool.cxx:59
ActsTrackingGeometryTool.h
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ActsExtrapolationTool::m_ptLoopers
Gaudi::Property< double > m_ptLoopers
Definition: ActsExtrapolationTool.h:127
res
std::pair< std::vector< unsigned int >, bool > res
Definition: JetGroupProductTest.cxx:14
test_pyathena.parent
parent
Definition: test_pyathena.py:15
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
python.AtlRunQueryLib.options
options
Definition: AtlRunQueryLib.py:379
ActsExtrapolationTool::m_trackingGeometryTool
ToolHandle< IActsTrackingGeometryTool > m_trackingGeometryTool
Definition: ActsExtrapolationTool.h:122
ActsExtrapolationTool::m_interactionMultiScatering
Gaudi::Property< bool > m_interactionMultiScatering
Definition: ActsExtrapolationTool.h:132
ActsExtrapolationTool::initialize
virtual StatusCode initialize() override
Definition: ActsExtrapolationTool.cxx:65
ActsExtrapolationDetail
Definition: ActsExtrapolationTool.h:48
ActsGeometryContext
Include the GeoPrimitives which need to be put first.
Definition: ActsGeometryContext.h:27
ActsExtrapolationTool::prepareOptions
OptionsType prepareOptions(const Acts::GeometryContext &gctx, const Acts::MagneticFieldContext &mctx, const Acts::BoundTrackParameters &startParameters, Acts::Direction navDir, double pathLimit) const
Definition: ActsExtrapolationTool.cxx:328
ActsExtrapolationDetail::VariantPropagator
Definition: ActsExtrapolationTool.cxx:42
merge.output
output
Definition: merge.py:17
ActsExtrapolationTool::m_interactionRecord
Gaudi::Property< bool > m_interactionRecord
Definition: ActsExtrapolationTool.h:134
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
ActsExtrapolationTool::m_fieldCacheCondObjInputKey
SG::ReadCondHandleKey< AtlasFieldCacheCondObj > m_fieldCacheCondObjInputKey
Definition: ActsExtrapolationTool.h:120
checkNSWValTree.Options
Options
Definition: checkNSWValTree.py:15
ActsExtrapolationTool::m_interactionEloss
Gaudi::Property< bool > m_interactionEloss
Definition: ActsExtrapolationTool.h:133
ActsExtrapolationTool::m_fieldMode
Gaudi::Property< std::string > m_fieldMode
Definition: ActsExtrapolationTool.h:124
SG::CondHandleKey::initialize
StatusCode initialize(bool used=true)
ActsExtrapolationTool::m_varProp
std::unique_ptr< const ActsExtrapolationDetail::VariantPropagator > m_varProp
Definition: ActsExtrapolationTool.h:117
ActsExtrapolationTool::m_maxStepSize
Gaudi::Property< double > m_maxStepSize
Definition: ActsExtrapolationTool.h:128
ActsTrk::detail::Stepper
Acts::SympyStepper Stepper
Adapted from Acts Examples/Algorithms/TrackFinding/src/TrackFindingAlgorithmFunction....
Definition: Tracking/Acts/ActsTrackReconstruction/src/detail/Definitions.h:30
ActsTrk::detail::Propagator
Acts::Propagator< Stepper, Navigator > Propagator
Definition: Tracking/Acts/ActsTrackReconstruction/src/detail/Definitions.h:32
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
copySelective.target
string target
Definition: copySelective.py:37
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
ActsExtrapolationTool::m_logger
std::unique_ptr< const Acts::Logger > m_logger
Definition: ActsExtrapolationTool.h:118
physics_parameters.parameters
parameters
Definition: physics_parameters.py:144
ActsExtrapolationTool::m_constantFieldVector
Gaudi::Property< std::vector< double > > m_constantFieldVector
Definition: ActsExtrapolationTool.h:125
Logger.h
ActsTrackingGeometrySvc.h
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7
ActsPropagationOutput
std::pair< std::vector< Acts::detail::Step >, ActsRecordedMaterial > ActsPropagationOutput
Finally the output of the propagation test.
Definition: IActsExtrapolationTool.h:25