Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:11:41

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/Definitions/Direction.hpp"
0012 #include "Acts/Definitions/TrackParametrization.hpp"
0013 #include "Acts/EventData/MultiTrajectory.hpp"
0014 #include "Acts/EventData/TrackContainer.hpp"
0015 #include "Acts/EventData/TrackStatePropMask.hpp"
0016 #include "Acts/EventData/VectorMultiTrajectory.hpp"
0017 #include "Acts/EventData/VectorTrackContainer.hpp"
0018 #include "Acts/EventData/detail/CorrectedTransformationFreeToBound.hpp"
0019 #include "Acts/Geometry/GeometryIdentifier.hpp"
0020 #include "Acts/Propagator/DirectNavigator.hpp"
0021 #include "Acts/Propagator/Navigator.hpp"
0022 #include "Acts/Propagator/Propagator.hpp"
0023 #include "Acts/Propagator/SympyStepper.hpp"
0024 #include "Acts/TrackFitting/GlobalChiSquareFitter.hpp"
0025 #include "Acts/TrackFitting/KalmanFitter.hpp"
0026 #include "Acts/Utilities/Delegate.hpp"
0027 #include "Acts/Utilities/Logger.hpp"
0028 #include "ActsExamples/EventData/IndexSourceLink.hpp"
0029 #include "ActsExamples/EventData/MeasurementCalibration.hpp"
0030 #include "ActsExamples/EventData/Track.hpp"
0031 #include "ActsExamples/TrackFitting/RefittingCalibrator.hpp"
0032 #include "ActsExamples/TrackFitting/TrackFitterFunction.hpp"
0033 
0034 #include <algorithm>
0035 #include <cmath>
0036 #include <functional>
0037 #include <memory>
0038 #include <utility>
0039 #include <vector>
0040 
0041 namespace Acts {
0042 class MagneticFieldProvider;
0043 class SourceLink;
0044 class Surface;
0045 class TrackingGeometry;
0046 }  // namespace Acts
0047 
0048 namespace {
0049 
0050 using Stepper = Acts::SympyStepper;
0051 using Propagator = Acts::Propagator<Stepper, Acts::Navigator>;
0052 using Fitter =
0053     Acts::Experimental::Gx2Fitter<Propagator, Acts::VectorMultiTrajectory>;
0054 using DirectPropagator = Acts::Propagator<Stepper, Acts::DirectNavigator>;
0055 using DirectFitter =
0056     Acts::KalmanFitter<DirectPropagator, Acts::VectorMultiTrajectory>;
0057 
0058 using TrackContainer =
0059     Acts::TrackContainer<Acts::VectorTrackContainer,
0060                          Acts::VectorMultiTrajectory, std::shared_ptr>;
0061 
0062 using namespace ActsExamples;
0063 
0064 struct GlobalChiSquareFitterFunctionImpl final : public TrackFitterFunction {
0065   Fitter fitter;
0066   DirectFitter directFitter;
0067 
0068   bool multipleScattering = false;
0069   bool energyLoss = false;
0070   Acts::FreeToBoundCorrection freeToBoundCorrection;
0071   std::size_t nUpdateMax = 5;
0072   double relChi2changeCutOff = 1e-7;
0073 
0074   IndexSourceLink::SurfaceAccessor m_slSurfaceAccessor;
0075 
0076   GlobalChiSquareFitterFunctionImpl(Fitter&& f, DirectFitter&& df,
0077                                     const Acts::TrackingGeometry& trkGeo)
0078       : fitter(std::move(f)),
0079         directFitter(std::move(df)),
0080         m_slSurfaceAccessor{trkGeo} {}
0081 
0082   template <typename calibrator_t>
0083   auto makeGx2fOptions(const GeneralFitterOptions& options,
0084                        const calibrator_t& calibrator) const {
0085     Acts::Experimental::Gx2FitterExtensions<Acts::VectorMultiTrajectory>
0086         extensions;
0087     extensions.calibrator.connect<&calibrator_t::calibrate>(&calibrator);
0088 
0089     if (options.doRefit) {
0090       extensions.surfaceAccessor.connect<&RefittingCalibrator::accessSurface>();
0091     } else {
0092       extensions.surfaceAccessor
0093           .connect<&IndexSourceLink::SurfaceAccessor::operator()>(
0094               &m_slSurfaceAccessor);
0095     }
0096 
0097     const Acts::Experimental::Gx2FitterOptions gx2fOptions(
0098         options.geoContext, options.magFieldContext, options.calibrationContext,
0099         extensions, options.propOptions, &(*options.referenceSurface),
0100         multipleScattering, energyLoss, freeToBoundCorrection, nUpdateMax,
0101         relChi2changeCutOff);
0102 
0103     return gx2fOptions;
0104   }
0105 
0106   TrackFitterResult operator()(const std::vector<Acts::SourceLink>& sourceLinks,
0107                                const TrackParameters& initialParameters,
0108                                const GeneralFitterOptions& options,
0109                                const MeasurementCalibratorAdapter& calibrator,
0110                                TrackContainer& tracks) const override {
0111     const auto gx2fOptions = makeGx2fOptions(options, calibrator);
0112     return fitter.fit(sourceLinks.begin(), sourceLinks.end(), initialParameters,
0113                       gx2fOptions, tracks);
0114   }
0115 
0116   // We need a placeholder for the directNavigator overload. Otherwise, we would
0117   // have an unimplemented pure virtual method in a final class.
0118   TrackFitterResult operator()(
0119       const std::vector<Acts::SourceLink>& /*sourceLinks*/,
0120       const TrackParameters& /*initialParameters*/,
0121       const GeneralFitterOptions& /*options*/,
0122       const RefittingCalibrator& /*calibrator*/,
0123       const std::vector<const Acts::Surface*>& /*surfaceSequence*/,
0124       TrackContainer& /*tracks*/) const override {
0125     throw std::runtime_error(
0126         "direct navigation with GX2 fitter is not implemented");
0127   }
0128 };
0129 
0130 }  // namespace
0131 
0132 std::shared_ptr<ActsExamples::TrackFitterFunction>
0133 ActsExamples::makeGlobalChiSquareFitterFunction(
0134     std::shared_ptr<const Acts::TrackingGeometry> trackingGeometry,
0135     std::shared_ptr<const Acts::MagneticFieldProvider> magneticField,
0136     bool multipleScattering, bool energyLoss,
0137     Acts::FreeToBoundCorrection freeToBoundCorrection, std::size_t nUpdateMax,
0138     double relChi2changeCutOff, const Acts::Logger& logger) {
0139   // Stepper should be copied into the fitters
0140   const Stepper stepper(std::move(magneticField));
0141 
0142   // Standard fitter
0143   const auto& geo = *trackingGeometry;
0144   Acts::Navigator::Config cfg{std::move(trackingGeometry)};
0145   cfg.resolvePassive = false;
0146   cfg.resolveMaterial = true;
0147   cfg.resolveSensitive = true;
0148   Acts::Navigator navigator(cfg, logger.cloneWithSuffix("Navigator"));
0149   Propagator propagator(stepper, std::move(navigator),
0150                         logger.cloneWithSuffix("Propagator"));
0151   Fitter trackFitter(std::move(propagator), logger.cloneWithSuffix("Fitter"));
0152 
0153   // Direct fitter
0154   Acts::DirectNavigator directNavigator{
0155       logger.cloneWithSuffix("DirectNavigator")};
0156   DirectPropagator directPropagator(stepper, std::move(directNavigator),
0157                                     logger.cloneWithSuffix("DirectPropagator"));
0158   DirectFitter directTrackFitter(std::move(directPropagator),
0159                                  logger.cloneWithSuffix("DirectFitter"));
0160 
0161   // build the fitter function. owns the fitter object.
0162   auto fitterFunction = std::make_shared<GlobalChiSquareFitterFunctionImpl>(
0163       std::move(trackFitter), std::move(directTrackFitter), geo);
0164   fitterFunction->multipleScattering = multipleScattering;
0165   fitterFunction->energyLoss = energyLoss;
0166   fitterFunction->freeToBoundCorrection = freeToBoundCorrection;
0167   fitterFunction->nUpdateMax = nUpdateMax;
0168   fitterFunction->relChi2changeCutOff = relChi2changeCutOff;
0169 
0170   return fitterFunction;
0171 }