Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-20 09:22:20

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 // TODO We still use some Kalman Fitter functionalities. Check for replacement
0010 
0011 #include "Acts/EventData/MultiTrajectory.hpp"
0012 #include "Acts/EventData/TrackContainer.hpp"
0013 #include "Acts/EventData/VectorMultiTrajectory.hpp"
0014 #include "Acts/EventData/VectorTrackContainer.hpp"
0015 #include "Acts/EventData/detail/CorrectedTransformationFreeToBound.hpp"
0016 #include "Acts/Geometry/GeometryIdentifier.hpp"
0017 #include "Acts/Propagator/DirectNavigator.hpp"
0018 #include "Acts/Propagator/Navigator.hpp"
0019 #include "Acts/Propagator/Propagator.hpp"
0020 #include "Acts/Propagator/SympyStepper.hpp"
0021 #include "Acts/TrackFitting/GlobalChiSquareFitter.hpp"
0022 #include "Acts/TrackFitting/KalmanFitter.hpp"
0023 #include "Acts/Utilities/Delegate.hpp"
0024 #include "Acts/Utilities/Logger.hpp"
0025 #include "ActsExamples/EventData/IndexSourceLink.hpp"
0026 #include "ActsExamples/EventData/MeasurementCalibration.hpp"
0027 #include "ActsExamples/EventData/Track.hpp"
0028 #include "ActsExamples/TrackFitting/RefittingCalibrator.hpp"
0029 #include "ActsExamples/TrackFitting/TrackFitterFunction.hpp"
0030 
0031 #include <functional>
0032 #include <memory>
0033 #include <utility>
0034 #include <vector>
0035 
0036 namespace {
0037 
0038 using Stepper = Acts::SympyStepper;
0039 using Propagator = Acts::Propagator<Stepper, Acts::Navigator>;
0040 using Fitter =
0041     Acts::Experimental::Gx2Fitter<Propagator, Acts::VectorMultiTrajectory>;
0042 using DirectPropagator = Acts::Propagator<Stepper, Acts::DirectNavigator>;
0043 using DirectFitter =
0044     Acts::KalmanFitter<DirectPropagator, Acts::VectorMultiTrajectory>;
0045 
0046 using TrackContainer =
0047     Acts::TrackContainer<Acts::VectorTrackContainer,
0048                          Acts::VectorMultiTrajectory, std::shared_ptr>;
0049 
0050 using namespace ActsExamples;
0051 
0052 struct GlobalChiSquareFitterFunctionImpl final : public TrackFitterFunction {
0053   Fitter fitter;
0054   DirectFitter directFitter;
0055 
0056   bool multipleScattering = false;
0057   bool energyLoss = false;
0058   Acts::FreeToBoundCorrection freeToBoundCorrection;
0059   std::size_t nUpdateMax = 5;
0060   double relChi2changeCutOff = 1e-7;
0061 
0062   IndexSourceLink::SurfaceAccessor m_slSurfaceAccessor;
0063 
0064   GlobalChiSquareFitterFunctionImpl(Fitter&& f, DirectFitter&& df,
0065                                     const Acts::TrackingGeometry& trkGeo)
0066       : fitter(std::move(f)),
0067         directFitter(std::move(df)),
0068         m_slSurfaceAccessor{trkGeo} {}
0069 
0070   template <typename calibrator_t>
0071   auto makeGx2fOptions(const GeneralFitterOptions& options,
0072                        const calibrator_t& calibrator) const {
0073     Acts::Experimental::Gx2FitterExtensions<Acts::VectorMultiTrajectory>
0074         extensions;
0075     extensions.calibrator.connect<&calibrator_t::calibrate>(&calibrator);
0076 
0077     if (options.doRefit) {
0078       extensions.surfaceAccessor.connect<&RefittingCalibrator::accessSurface>();
0079     } else {
0080       extensions.surfaceAccessor
0081           .connect<&IndexSourceLink::SurfaceAccessor::operator()>(
0082               &m_slSurfaceAccessor);
0083     }
0084 
0085     const Acts::Experimental::Gx2FitterOptions gx2fOptions(
0086         options.geoContext, options.magFieldContext, options.calibrationContext,
0087         extensions, options.propOptions, &(*options.referenceSurface),
0088         multipleScattering, energyLoss, freeToBoundCorrection, nUpdateMax,
0089         relChi2changeCutOff);
0090 
0091     return gx2fOptions;
0092   }
0093 
0094   TrackFitterResult operator()(const std::vector<Acts::SourceLink>& sourceLinks,
0095                                const TrackParameters& initialParameters,
0096                                const GeneralFitterOptions& options,
0097                                const MeasurementCalibratorAdapter& calibrator,
0098                                TrackContainer& tracks) const override {
0099     const auto gx2fOptions = makeGx2fOptions(options, calibrator);
0100     return fitter.fit(sourceLinks.begin(), sourceLinks.end(), initialParameters,
0101                       gx2fOptions, tracks);
0102   }
0103 
0104   // The direct navigation is not implemented for the GX2F. Instead we ignore it
0105   // and fall back to the standard navigation.
0106   TrackFitterResult operator()(
0107       const std::vector<Acts::SourceLink>& sourceLinks,
0108       const TrackParameters& initialParameters,
0109       const GeneralFitterOptions& options,
0110       const RefittingCalibrator& calibrator,
0111       const std::vector<const Acts::Surface*>& /*surfaceSequence*/,
0112       TrackContainer& tracks) const override {
0113     const auto gx2fOptions = makeGx2fOptions(options, calibrator);
0114     return fitter.fit(sourceLinks.begin(), sourceLinks.end(), initialParameters,
0115                       gx2fOptions, tracks);
0116   }
0117 };
0118 
0119 }  // namespace
0120 
0121 std::shared_ptr<ActsExamples::TrackFitterFunction>
0122 ActsExamples::makeGlobalChiSquareFitterFunction(
0123     std::shared_ptr<const Acts::TrackingGeometry> trackingGeometry,
0124     std::shared_ptr<const Acts::MagneticFieldProvider> magneticField,
0125     bool multipleScattering, bool energyLoss,
0126     Acts::FreeToBoundCorrection freeToBoundCorrection, std::size_t nUpdateMax,
0127     double relChi2changeCutOff, const Acts::Logger& logger) {
0128   // Stepper should be copied into the fitters
0129   const Stepper stepper(std::move(magneticField));
0130 
0131   // Standard fitter
0132   const auto& geo = *trackingGeometry;
0133   Acts::Navigator::Config cfg{std::move(trackingGeometry)};
0134   cfg.resolvePassive = false;
0135   cfg.resolveMaterial = true;
0136   cfg.resolveSensitive = true;
0137   Acts::Navigator navigator(cfg, logger.cloneWithSuffix("Navigator"));
0138   Propagator propagator(stepper, std::move(navigator),
0139                         logger.cloneWithSuffix("Propagator"));
0140   Fitter trackFitter(std::move(propagator), logger.cloneWithSuffix("Fitter"));
0141 
0142   // Direct fitter
0143   Acts::DirectNavigator directNavigator{
0144       logger.cloneWithSuffix("DirectNavigator")};
0145   DirectPropagator directPropagator(stepper, std::move(directNavigator),
0146                                     logger.cloneWithSuffix("DirectPropagator"));
0147   DirectFitter directTrackFitter(std::move(directPropagator),
0148                                  logger.cloneWithSuffix("DirectFitter"));
0149 
0150   // build the fitter function. owns the fitter object.
0151   auto fitterFunction = std::make_shared<GlobalChiSquareFitterFunctionImpl>(
0152       std::move(trackFitter), std::move(directTrackFitter), geo);
0153   fitterFunction->multipleScattering = multipleScattering;
0154   fitterFunction->energyLoss = energyLoss;
0155   fitterFunction->freeToBoundCorrection = freeToBoundCorrection;
0156   fitterFunction->nUpdateMax = nUpdateMax;
0157   fitterFunction->relChi2changeCutOff = relChi2changeCutOff;
0158 
0159   return fitterFunction;
0160 }