Back to home page

EIC code displayed by LXR

 
 

    


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

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 #include "Acts/Propagator/detail/SympyCovarianceEngine.hpp"
0010 
0011 #include "Acts/Propagator/detail/JacobianEngine.hpp"
0012 #include "Acts/Propagator/detail/SympyJacobianEngine.hpp"
0013 
0014 #include "codegen/sympy_cov_math.hpp"
0015 
0016 namespace Acts::detail {
0017 
0018 /// Some type defs
0019 using Jacobian = BoundMatrix;
0020 using BoundState = std::tuple<BoundTrackParameters, Jacobian, double>;
0021 using CurvilinearState =
0022     std::tuple<CurvilinearTrackParameters, Jacobian, double>;
0023 
0024 Result<BoundState> sympy::boundState(
0025     const GeometryContext& geoContext, const Surface& surface,
0026     BoundSquareMatrix& boundCovariance, BoundMatrix& fullTransportJacobian,
0027     FreeMatrix& freeTransportJacobian, FreeVector& freeToPathDerivatives,
0028     BoundToFreeMatrix& boundToFreeJacobian, FreeVector& freeParameters,
0029     const ParticleHypothesis& particleHypothesis, bool covTransport,
0030     double accumulatedPath,
0031     const FreeToBoundCorrection& freeToBoundCorrection) {
0032   // Create the bound parameters
0033   Result<BoundVector> bv =
0034       transformFreeToBoundParameters(freeParameters, surface, geoContext);
0035   if (!bv.ok()) {
0036     return bv.error();
0037   }
0038 
0039   // Covariance transport
0040   std::optional<BoundSquareMatrix> cov = std::nullopt;
0041   if (covTransport) {
0042     // Calculate the jacobian and transport the covarianceMatrix to final local.
0043     // Then reinitialize the transportJacobian, derivatives and the
0044     // boundToFreeJacobian
0045     transportCovarianceToBound(geoContext, surface, boundCovariance,
0046                                fullTransportJacobian, freeTransportJacobian,
0047                                freeToPathDerivatives, boundToFreeJacobian,
0048                                freeParameters, freeToBoundCorrection);
0049     cov = boundCovariance;
0050   }
0051 
0052   // Create the bound state
0053   return std::make_tuple(
0054       BoundTrackParameters(surface.getSharedPtr(), *bv, std::move(cov),
0055                            particleHypothesis),
0056       fullTransportJacobian, accumulatedPath);
0057 }
0058 
0059 CurvilinearState sympy::curvilinearState(
0060     BoundSquareMatrix& boundCovariance, BoundMatrix& fullTransportJacobian,
0061     FreeMatrix& freeTransportJacobian, FreeVector& freeToPathDerivatives,
0062     BoundToFreeMatrix& boundToFreeJacobian, const FreeVector& freeParameters,
0063     const ParticleHypothesis& particleHypothesis, bool covTransport,
0064     double accumulatedPath) {
0065   const Vector3& direction = freeParameters.segment<3>(eFreeDir0);
0066 
0067   // Covariance transport
0068   std::optional<BoundSquareMatrix> cov = std::nullopt;
0069   if (covTransport) {
0070     // Calculate the jacobian and transport the covarianceMatrix to final local.
0071     // Then reinitialize the transportJacobian, derivatives and the
0072     // boundToFreeJacobian
0073     transportCovarianceToCurvilinear(
0074         boundCovariance, fullTransportJacobian, freeTransportJacobian,
0075         freeToPathDerivatives, boundToFreeJacobian, direction);
0076     cov = boundCovariance;
0077   }
0078 
0079   // Create the curvilinear parameters
0080   Vector4 pos4 = Vector4::Zero();
0081   pos4[ePos0] = freeParameters[eFreePos0];
0082   pos4[ePos1] = freeParameters[eFreePos1];
0083   pos4[ePos2] = freeParameters[eFreePos2];
0084   pos4[eTime] = freeParameters[eFreeTime];
0085   CurvilinearTrackParameters curvilinearParams(
0086       pos4, direction, freeParameters[eFreeQOverP], std::move(cov),
0087       particleHypothesis);
0088   // Create the curvilinear state
0089   return {std::move(curvilinearParams), fullTransportJacobian, accumulatedPath};
0090 }
0091 
0092 void sympy::transportCovarianceToBound(
0093     const GeometryContext& geoContext, const Surface& surface,
0094     BoundSquareMatrix& boundCovariance, BoundMatrix& fullTransportJacobian,
0095     FreeMatrix& freeTransportJacobian, FreeVector& freeToPathDerivatives,
0096     BoundToFreeMatrix& boundToFreeJacobian, FreeVector& freeParameters,
0097     const FreeToBoundCorrection& freeToBoundCorrection) {
0098   // Calculate the full jacobian from local parameters at the start surface to
0099   // current bound parameters
0100   sympy::boundToBoundTransportJacobian(
0101       geoContext, surface, freeParameters, boundToFreeJacobian,
0102       freeTransportJacobian, freeToPathDerivatives, fullTransportJacobian);
0103 
0104   bool correction = false;
0105   if (freeToBoundCorrection) {
0106     BoundToFreeMatrix startBoundToFinalFreeJacobian =
0107         freeTransportJacobian * boundToFreeJacobian;
0108     FreeSquareMatrix freeCovariance = startBoundToFinalFreeJacobian *
0109                                       boundCovariance *
0110                                       startBoundToFinalFreeJacobian.transpose();
0111 
0112     auto transformer =
0113         detail::CorrectedFreeToBoundTransformer(freeToBoundCorrection);
0114     auto correctedRes =
0115         transformer(freeParameters, freeCovariance, surface, geoContext);
0116 
0117     if (correctedRes.has_value()) {
0118       auto correctedValue = correctedRes.value();
0119       BoundVector boundParams = std::get<BoundVector>(correctedValue);
0120       // 1. Update the free parameters with the corrected bound parameters
0121       freeParameters =
0122           transformBoundToFreeParameters(surface, geoContext, boundParams);
0123 
0124       // 2. Update the bound covariance
0125       boundCovariance = std::get<BoundSquareMatrix>(correctedValue);
0126 
0127       correction = true;
0128     }
0129   }
0130 
0131   if (!correction) {
0132     // Apply the actual covariance transport to get covariance of the current
0133     // bound parameters
0134     BoundMatrix newBoundCovariance;
0135     transportCovarianceToBoundImpl(boundCovariance.data(),
0136                                    fullTransportJacobian.data(),
0137                                    newBoundCovariance.data());
0138     boundCovariance = newBoundCovariance;
0139   }
0140 
0141   // Reinitialize jacobian components:
0142   // ->The transportJacobian is reinitialized to Identity
0143   // ->The derivatives is reinitialized to Zero
0144   // ->The boundToFreeJacobian is initialized to that at the current surface
0145   reinitializeJacobians(geoContext, surface, freeTransportJacobian,
0146                         freeToPathDerivatives, boundToFreeJacobian,
0147                         freeParameters);
0148 }
0149 
0150 void sympy::transportCovarianceToCurvilinear(
0151     BoundSquareMatrix& boundCovariance, BoundMatrix& fullTransportJacobian,
0152     FreeMatrix& freeTransportJacobian, FreeVector& freeToPathDerivatives,
0153     BoundToFreeMatrix& boundToFreeJacobian, const Vector3& direction) {
0154   // Calculate the full jacobian from local parameters at the start surface to
0155   // current curvilinear parameters
0156   sympy::boundToCurvilinearTransportJacobian(
0157       direction, boundToFreeJacobian, freeTransportJacobian,
0158       freeToPathDerivatives, fullTransportJacobian);
0159 
0160   // Apply the actual covariance transport to get covariance of the current
0161   // curvilinear parameters
0162   BoundMatrix newBoundCovariance;
0163   transportCovarianceToBoundImpl(boundCovariance.data(),
0164                                  fullTransportJacobian.data(),
0165                                  newBoundCovariance.data());
0166   boundCovariance = newBoundCovariance;
0167 
0168   // Reinitialize jacobian components:
0169   // ->The free transportJacobian is reinitialized to Identity
0170   // ->The path derivatives is reinitialized to Zero
0171   // ->The boundToFreeJacobian is reinitialized to that at the current
0172   // curvilinear surface
0173   reinitializeJacobians(freeTransportJacobian, freeToPathDerivatives,
0174                         boundToFreeJacobian, direction);
0175 }
0176 
0177 }  // namespace Acts::detail