|
| 1 | +// This file is part of the ACTS project. |
| 2 | +// |
| 3 | +// Copyright (C) 2016 CERN for the benefit of the ACTS project |
| 4 | +// |
| 5 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 8 | + |
| 9 | +#include "Acts/EventData/MultiTrajectory.hpp" |
| 10 | +#include "Acts/EventData/TrackContainer.hpp" |
| 11 | +#include "Acts/EventData/VectorMultiTrajectory.hpp" |
| 12 | +#include "Acts/EventData/VectorTrackContainer.hpp" |
| 13 | +#include "Acts/EventData/detail/CorrectedTransformationFreeToBound.hpp" |
| 14 | +#include "Acts/Geometry/GeometryIdentifier.hpp" |
| 15 | +#include "Acts/Propagator/DirectNavigator.hpp" |
| 16 | +#include "Acts/Propagator/Navigator.hpp" |
| 17 | +#include "Acts/Propagator/Propagator.hpp" |
| 18 | +#include "Acts/Propagator/SympyStepper.hpp" |
| 19 | +#include "Acts/TrackFitting/GainMatrixUpdater.hpp" |
| 20 | +#include "Acts/TrackFitting/MbfSmoother.hpp" |
| 21 | +#include "Acts/TrackFitting/ReferenceTrajectoryBuilder.hpp" |
| 22 | +#include "Acts/Utilities/Logger.hpp" |
| 23 | +#include "Acts/Utilities/TrackHelpers.hpp" |
| 24 | +#include "ActsExamples/EventData/IndexSourceLink.hpp" |
| 25 | +#include "ActsExamples/EventData/MeasurementCalibration.hpp" |
| 26 | +#include "ActsExamples/EventData/Track.hpp" |
| 27 | +#include "ActsExamples/TrackFitting/RefittingCalibrator.hpp" |
| 28 | +#include "ActsExamples/TrackFitting/TrackFitterFunction.hpp" |
| 29 | + |
| 30 | +#include <memory> |
| 31 | +#include <utility> |
| 32 | +#include <vector> |
| 33 | + |
| 34 | +namespace { |
| 35 | + |
| 36 | +using Stepper = Acts::SympyStepper; |
| 37 | +using Propagator = Acts::Propagator<Stepper, Acts::Navigator>; |
| 38 | +using ReferenceTrajectoryBuilder = |
| 39 | + Acts::Experimental::ReferenceTrajectoryBuilder<Propagator, |
| 40 | + Acts::VectorMultiTrajectory>; |
| 41 | +using DirectPropagator = Acts::Propagator<Stepper, Acts::DirectNavigator>; |
| 42 | +using DirectReferenceTrajectoryBuilder = |
| 43 | + Acts::Experimental::ReferenceTrajectoryBuilder<DirectPropagator, |
| 44 | + Acts::VectorMultiTrajectory>; |
| 45 | + |
| 46 | +using TrackContainer = |
| 47 | + Acts::TrackContainer<Acts::VectorTrackContainer, |
| 48 | + Acts::VectorMultiTrajectory, std::shared_ptr>; |
| 49 | + |
| 50 | +using namespace ActsExamples; |
| 51 | + |
| 52 | +struct KalmanReferenceTrajectoryFitterFunctionImpl final |
| 53 | + : public TrackFitterFunction { |
| 54 | + Propagator extrapolator; |
| 55 | + |
| 56 | + ReferenceTrajectoryBuilder referenceTrajectoryBuilder; |
| 57 | + DirectReferenceTrajectoryBuilder directReferenceTrajectoryBuilder; |
| 58 | + |
| 59 | + Acts::GainMatrixUpdater kfUpdater; |
| 60 | + Acts::MbfSmoother kfSmoother; |
| 61 | + |
| 62 | + bool multipleScattering = false; |
| 63 | + bool energyLoss = false; |
| 64 | + Acts::FreeToBoundCorrection freeToBoundCorrection; |
| 65 | + |
| 66 | + IndexSourceLink::SurfaceAccessor slSurfaceAccessor; |
| 67 | + |
| 68 | + std::unique_ptr<const Acts::Logger> m_smootherLogger; |
| 69 | + std::unique_ptr<const Acts::Logger> m_extrapolationLogger; |
| 70 | + |
| 71 | + KalmanReferenceTrajectoryFitterFunctionImpl( |
| 72 | + Propagator&& ext, ReferenceTrajectoryBuilder&& rtBuilder, |
| 73 | + DirectReferenceTrajectoryBuilder&& dRtBuilder, |
| 74 | + const Acts::TrackingGeometry& trkGeo, const Acts::Logger& logger) |
| 75 | + : extrapolator(std::move(ext)), |
| 76 | + referenceTrajectoryBuilder(std::move(rtBuilder)), |
| 77 | + directReferenceTrajectoryBuilder(std::move(dRtBuilder)), |
| 78 | + slSurfaceAccessor{trkGeo} { |
| 79 | + m_smootherLogger = logger.cloneWithSuffix("Smoother"); |
| 80 | + m_extrapolationLogger = logger.cloneWithSuffix("Extrapolation"); |
| 81 | + } |
| 82 | + |
| 83 | + auto makeReferenceTrajectoryBuilderOptions( |
| 84 | + const GeneralFitterOptions& options) const { |
| 85 | + Acts::Experimental::ReferenceTrajectoryBuilderOptions< |
| 86 | + Acts::VectorMultiTrajectory> |
| 87 | + rtOptions(options.geoContext, options.magFieldContext, |
| 88 | + options.propOptions, nullptr); |
| 89 | + |
| 90 | + rtOptions.multipleScattering = multipleScattering; |
| 91 | + rtOptions.energyLoss = energyLoss; |
| 92 | + rtOptions.freeToBoundCorrection = freeToBoundCorrection; |
| 93 | + |
| 94 | + return rtOptions; |
| 95 | + } |
| 96 | + |
| 97 | + TrackFitterResult operator()(const std::vector<Acts::SourceLink>& sourceLinks, |
| 98 | + const TrackParameters& initialParameters, |
| 99 | + const GeneralFitterOptions& options, |
| 100 | + const MeasurementCalibratorAdapter& calibrator, |
| 101 | + TrackContainer& tracks) const override { |
| 102 | + const auto referenceTrajectoryBuilderOptions = |
| 103 | + makeReferenceTrajectoryBuilderOptions(options); |
| 104 | + |
| 105 | + Acts::SourceLinkSurfaceAccessor sourceLinkAccessorDelegate; |
| 106 | + sourceLinkAccessorDelegate |
| 107 | + .connect<&IndexSourceLink::SurfaceAccessor::operator()>( |
| 108 | + &slSurfaceAccessor); |
| 109 | + |
| 110 | + ReferenceTrajectoryBuilder::Calibrator calibratorDelegate; |
| 111 | + calibratorDelegate.connect<&MeasurementCalibratorAdapter::calibrate>( |
| 112 | + &calibrator); |
| 113 | + |
| 114 | + ReferenceTrajectoryBuilder::Updater updaterDelegate; |
| 115 | + updaterDelegate.connect< |
| 116 | + &Acts::GainMatrixUpdater::operator()<Acts::VectorMultiTrajectory>>( |
| 117 | + &kfUpdater); |
| 118 | + |
| 119 | + auto buildResult = referenceTrajectoryBuilder.build( |
| 120 | + initialParameters, referenceTrajectoryBuilderOptions, tracks); |
| 121 | + if (!buildResult.ok()) { |
| 122 | + return buildResult.error(); |
| 123 | + } |
| 124 | + auto track = *buildResult; |
| 125 | + |
| 126 | + referenceTrajectoryBuilder.attachSourceLinks(track, sourceLinks, |
| 127 | + sourceLinkAccessorDelegate); |
| 128 | + |
| 129 | + referenceTrajectoryBuilder.calibrateMeasurements(options.geoContext, |
| 130 | + options.calibrationContext, |
| 131 | + track, calibratorDelegate); |
| 132 | + |
| 133 | + auto fitterResult = referenceTrajectoryBuilder.filter( |
| 134 | + options.geoContext, track, updaterDelegate); |
| 135 | + if (!fitterResult.ok()) { |
| 136 | + return fitterResult.error(); |
| 137 | + } |
| 138 | + |
| 139 | + auto smoothRes = |
| 140 | + kfSmoother(options.geoContext, tracks.trackStateContainer(), |
| 141 | + track.tipIndex(), *m_smootherLogger); |
| 142 | + if (!smoothRes.ok()) { |
| 143 | + return smoothRes.error(); |
| 144 | + } |
| 145 | + |
| 146 | + if (!track.hasReferenceSurface() && options.referenceSurface != nullptr) { |
| 147 | + typename Propagator::template Options<> extrapolationOptions( |
| 148 | + options.geoContext, options.magFieldContext); |
| 149 | + auto extrapolationResult = extrapolateTrackToReferenceSurface( |
| 150 | + track, *options.referenceSurface, extrapolator, extrapolationOptions, |
| 151 | + Acts::TrackExtrapolationStrategy::first, *m_extrapolationLogger); |
| 152 | + |
| 153 | + if (!extrapolationResult.ok()) { |
| 154 | + return extrapolationResult.error(); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + Acts::calculateTrackQuantities(track); |
| 159 | + |
| 160 | + return TrackFitterResult::success(track); |
| 161 | + } |
| 162 | + |
| 163 | + TrackFitterResult operator()( |
| 164 | + const std::vector<Acts::SourceLink>& sourceLinks, |
| 165 | + const TrackParameters& initialParameters, |
| 166 | + const GeneralFitterOptions& options, |
| 167 | + const RefittingCalibrator& calibrator, |
| 168 | + const std::vector<const Acts::Surface*>& surfaceSequence, |
| 169 | + TrackContainer& tracks) const override { |
| 170 | + const auto referenceTrajectoryBuilderOptions = |
| 171 | + makeReferenceTrajectoryBuilderOptions(options); |
| 172 | + |
| 173 | + Acts::SourceLinkSurfaceAccessor sourceLinkAccessorDelegate; |
| 174 | + sourceLinkAccessorDelegate.connect<&RefittingCalibrator::accessSurface>(); |
| 175 | + |
| 176 | + DirectReferenceTrajectoryBuilder::Calibrator calibratorDelegate; |
| 177 | + calibratorDelegate.connect<&RefittingCalibrator::calibrate>(&calibrator); |
| 178 | + |
| 179 | + DirectReferenceTrajectoryBuilder::Updater updaterDelegate; |
| 180 | + updaterDelegate.connect< |
| 181 | + &Acts::GainMatrixUpdater::operator()<Acts::VectorMultiTrajectory>>( |
| 182 | + &kfUpdater); |
| 183 | + |
| 184 | + auto buildResult = directReferenceTrajectoryBuilder.build( |
| 185 | + initialParameters, referenceTrajectoryBuilderOptions, surfaceSequence, |
| 186 | + tracks); |
| 187 | + if (!buildResult.ok()) { |
| 188 | + return buildResult.error(); |
| 189 | + } |
| 190 | + auto track = *buildResult; |
| 191 | + |
| 192 | + directReferenceTrajectoryBuilder.attachSourceLinks( |
| 193 | + track, sourceLinks, sourceLinkAccessorDelegate); |
| 194 | + |
| 195 | + directReferenceTrajectoryBuilder.calibrateMeasurements( |
| 196 | + options.geoContext, options.calibrationContext, track, |
| 197 | + calibratorDelegate); |
| 198 | + |
| 199 | + directReferenceTrajectoryBuilder.filter(options.geoContext, track, |
| 200 | + updaterDelegate); |
| 201 | + |
| 202 | + auto smoothRes = kfSmoother(options.geoContext, |
| 203 | + tracks.trackStateContainer(), track.tipIndex()); |
| 204 | + if (!smoothRes.ok()) { |
| 205 | + return smoothRes.error(); |
| 206 | + } |
| 207 | + |
| 208 | + if (!track.hasReferenceSurface() && options.referenceSurface != nullptr) { |
| 209 | + typename Propagator::template Options<> extrapolationOptions( |
| 210 | + options.geoContext, options.magFieldContext); |
| 211 | + auto extrapolationResult = extrapolateTrackToReferenceSurface( |
| 212 | + track, *options.referenceSurface, extrapolator, extrapolationOptions, |
| 213 | + Acts::TrackExtrapolationStrategy::first); |
| 214 | + |
| 215 | + if (!extrapolationResult.ok()) { |
| 216 | + return extrapolationResult.error(); |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + Acts::calculateTrackQuantities(track); |
| 221 | + |
| 222 | + return TrackFitterResult::success(track); |
| 223 | + } |
| 224 | +}; |
| 225 | + |
| 226 | +} // namespace |
| 227 | + |
| 228 | +std::shared_ptr<TrackFitterFunction> |
| 229 | +ActsExamples::makeKalmanReferenceTrajectoryFitterFunction( |
| 230 | + std::shared_ptr<const Acts::TrackingGeometry> trackingGeometry, |
| 231 | + std::shared_ptr<const Acts::MagneticFieldProvider> magneticField, |
| 232 | + bool multipleScattering, bool energyLoss, |
| 233 | + const Acts::FreeToBoundCorrection& freeToBoundCorrection, |
| 234 | + bool useJosephFormulation, const Acts::Logger& logger) { |
| 235 | + const Stepper stepper(std::move(magneticField)); |
| 236 | + |
| 237 | + const auto& geo = *trackingGeometry; |
| 238 | + Acts::Navigator::Config cfg{std::move(trackingGeometry)}; |
| 239 | + cfg.resolvePassive = false; |
| 240 | + cfg.resolveMaterial = true; |
| 241 | + cfg.resolveSensitive = true; |
| 242 | + Acts::Navigator navigator(cfg, logger.cloneWithSuffix("Navigator")); |
| 243 | + Propagator propagator(stepper, navigator, |
| 244 | + logger.cloneWithSuffix("Propagator")); |
| 245 | + ReferenceTrajectoryBuilder referenceTrajectoryBuilder( |
| 246 | + std::move(propagator), |
| 247 | + logger.cloneWithSuffix("ReferenceTrajectoryBuilder")); |
| 248 | + |
| 249 | + Acts::DirectNavigator directNavigator{ |
| 250 | + logger.cloneWithSuffix("DirectNavigator")}; |
| 251 | + DirectPropagator directPropagator(stepper, std::move(directNavigator), |
| 252 | + logger.cloneWithSuffix("DirectPropagator")); |
| 253 | + DirectReferenceTrajectoryBuilder directReferenceTrajectoryBuilder( |
| 254 | + std::move(directPropagator), |
| 255 | + logger.cloneWithSuffix("DirectReferenceTrajectoryBuilder")); |
| 256 | + |
| 257 | + Propagator extrapolator(stepper, navigator, |
| 258 | + logger.cloneWithSuffix("Extrapolator")); |
| 259 | + |
| 260 | + auto fitterFunction = |
| 261 | + std::make_shared<KalmanReferenceTrajectoryFitterFunctionImpl>( |
| 262 | + std::move(extrapolator), std::move(referenceTrajectoryBuilder), |
| 263 | + std::move(directReferenceTrajectoryBuilder), geo, logger); |
| 264 | + fitterFunction->multipleScattering = multipleScattering; |
| 265 | + fitterFunction->energyLoss = energyLoss; |
| 266 | + fitterFunction->freeToBoundCorrection = freeToBoundCorrection; |
| 267 | + fitterFunction->kfUpdater = Acts::GainMatrixUpdater(useJosephFormulation); |
| 268 | + |
| 269 | + return fitterFunction; |
| 270 | +} |
0 commit comments