Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-01 07:32:40

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 #pragma once
0010 
0011 #include "Acts/Definitions/Units.hpp"
0012 #include "Acts/Propagator/ConstrainedStep.hpp"
0013 #include "Acts/Surfaces/BoundaryTolerance.hpp"
0014 #include "Acts/Surfaces/Surface.hpp"
0015 #include "Acts/Utilities/Enumerate.hpp"
0016 #include "Acts/Utilities/Intersection.hpp"
0017 #include "Acts/Utilities/Logger.hpp"
0018 
0019 #include <limits>
0020 
0021 namespace Acts {
0022 
0023 /// This is the condition that the pathLimit has been reached
0024 struct PathLimitReached {
0025   /// Internal path limit for loop protection
0026   double internalLimit = std::numeric_limits<double>::max();
0027 
0028   /// boolean operator for abort condition without using the result
0029   ///
0030   /// @tparam propagator_state_t Type of the propagator state
0031   /// @tparam stepper_t Type of the stepper
0032   /// @tparam navigator_t Type of the navigator
0033   ///
0034   /// @param [in,out] state The propagation state object
0035   /// @param [in] stepper Stepper used for propagation
0036   /// @param [in] navigator Navigator used for propagation
0037   /// @param logger a logger instance
0038   /// @return True if path limit exceeded and propagation should abort
0039   template <typename propagator_state_t, typename stepper_t,
0040             typename navigator_t>
0041   bool checkAbort(propagator_state_t& state, const stepper_t& stepper,
0042                   const navigator_t& navigator, const Logger& logger) const {
0043     static_cast<void>(navigator);
0044 
0045     // Check if the maximum allowed step size has to be updated
0046     double distance =
0047         std::abs(internalLimit) - std::abs(state.stepping.pathAccumulated);
0048     double tolerance = state.options.surfaceTolerance;
0049     bool limitReached = (std::abs(distance) < std::abs(tolerance));
0050     if (limitReached) {
0051       ACTS_VERBOSE("PathLimit aborter | " << "Path limit reached at distance "
0052                                           << distance);
0053       return true;
0054     }
0055     stepper.updateStepSize(state.stepping, distance,
0056                            ConstrainedStep::Type::Actor);
0057     ACTS_VERBOSE("PathLimit aborter | "
0058                  << "Target stepSize (path limit) updated to "
0059                  << stepper.outputStepSize(state.stepping));
0060     return false;
0061   }
0062 };
0063 
0064 /// This is the condition that the Surface has been reached it then triggers a
0065 /// propagation abort
0066 struct SurfaceReached {
0067   /// Target surface to reach for propagation termination
0068   const Surface* surface = nullptr;
0069   /// Boundary tolerance for surface intersection checks
0070   BoundaryTolerance boundaryTolerance = BoundaryTolerance::None();
0071 
0072   // TODO https://github.com/acts-project/acts/issues/2738
0073   /// Distance limit to discard intersections "behind us"
0074   /// @note this is only necessary because some surfaces have more than one
0075   ///       intersection
0076   double nearLimit = -100 * UnitConstants::um;
0077 
0078   SurfaceReached() = default;
0079   /// Constructor with custom near limit
0080   /// @param nLimit Distance limit to discard intersections "behind us"
0081   explicit SurfaceReached(double nLimit) : nearLimit(nLimit) {}
0082 
0083   /// boolean operator for abort condition without using the result
0084   ///
0085   /// @tparam propagator_state_t Type of the propagator state
0086   /// @tparam stepper_t Type of the stepper
0087   /// @tparam navigator_t Type of the navigator
0088   ///
0089   /// @param [in,out] state The propagation state object
0090   /// @param [in] stepper Stepper used for propagation
0091   /// @param [in] navigator Navigator used for propagation
0092   /// @param logger a logger instance
0093   /// @return true if abort condition is met (surface reached)
0094   template <typename propagator_state_t, typename stepper_t,
0095             typename navigator_t>
0096   bool checkAbort(propagator_state_t& state, const stepper_t& stepper,
0097                   const navigator_t& navigator, const Logger& logger) const {
0098     if (surface == nullptr) {
0099       ACTS_VERBOSE("SurfaceReached aborter | Target surface not set.");
0100       return false;
0101     }
0102 
0103     if (navigator.currentSurface(state.navigation) == surface) {
0104       ACTS_VERBOSE("SurfaceReached aborter | Target surface reached.");
0105       return true;
0106     }
0107 
0108     // not using the stepper overstep limit here because it does not always work
0109     // for perigee surfaces
0110     // note: the near limit is necessary for surfaces with more than one
0111     // intersection in order to discard the ones which are behind us
0112     const double farLimit = std::numeric_limits<double>::max();
0113     const double tolerance = state.options.surfaceTolerance;
0114 
0115     const MultiIntersection3D multiIntersection = surface->intersect(
0116         state.geoContext, stepper.position(state.stepping),
0117         state.options.direction * stepper.direction(state.stepping),
0118         boundaryTolerance, tolerance);
0119     const Intersection3D closestIntersection = multiIntersection.closest();
0120 
0121     bool reached = false;
0122 
0123     if (closestIntersection.status() == IntersectionStatus::onSurface) {
0124       const double distance = closestIntersection.pathLength();
0125       ACTS_VERBOSE(
0126           "SurfaceReached aborter | "
0127           "Target surface reached at distance (tolerance) "
0128           << distance << " (" << tolerance << ")");
0129       reached = true;
0130     }
0131 
0132     if (const auto intersectionIt = std::ranges::find_if(
0133             multiIntersection,
0134             [&](const auto& intersection) {
0135               return intersection.isValid() &&
0136                      detail::checkPathLength(intersection.pathLength(),
0137                                              nearLimit, farLimit, logger);
0138             });
0139         intersectionIt != multiIntersection.end()) {
0140       stepper.updateSurfaceStatus(
0141           state.stepping, *surface, intersectionIt - multiIntersection.begin(),
0142           state.options.direction, boundaryTolerance,
0143           state.options.surfaceTolerance, ConstrainedStep::Type::Actor, logger);
0144       ACTS_VERBOSE(
0145           "SurfaceReached aborter | "
0146           "Target stepSize (surface) updated to "
0147           << stepper.outputStepSize(state.stepping));
0148     } else {
0149       ACTS_VERBOSE(
0150           "SurfaceReached aborter | "
0151           "Target intersection not found. Maybe next time?");
0152     }
0153 
0154     return reached;
0155   }
0156 };
0157 
0158 /// Similar to SurfaceReached, but with an infinite overstep limit.
0159 ///
0160 /// This can be used to force the propagation to the target surface.
0161 struct ForcedSurfaceReached : SurfaceReached {
0162   ForcedSurfaceReached()
0163       : SurfaceReached(std::numeric_limits<double>::lowest()) {}
0164 };
0165 
0166 /// This is the condition that the end of world has been reached
0167 /// it then triggers an propagation abort
0168 struct EndOfWorldReached {
0169   /// boolean operator for abort condition without using the result
0170   ///
0171   /// @tparam propagator_state_t Type of the propagator state
0172   /// @tparam navigator_t Type of the navigator
0173   ///
0174   /// @param [in,out] state The propagation state object
0175   /// @param [in] navigator The navigator object
0176   /// @return True if end of world reached and propagation should abort
0177   template <typename propagator_state_t, typename stepper_t,
0178             typename navigator_t>
0179   bool checkAbort(propagator_state_t& state, const stepper_t& /*stepper*/,
0180                   const navigator_t& navigator,
0181                   const Logger& /*logger*/) const {
0182     bool endOfWorld = navigator.endOfWorldReached(state.navigation);
0183     return endOfWorld;
0184   }
0185 };
0186 
0187 /// This is the condition that the end of world has been reached
0188 /// it then triggers a propagation abort
0189 struct VolumeConstraintAborter {
0190   /// boolean operator for abort condition without using the result
0191   ///
0192   /// @tparam propagator_state_t Type of the propagator state
0193   /// @tparam navigator_t Type of the navigator
0194   ///
0195   /// @param [in,out] state The propagation state object
0196   /// @param [in] navigator The navigator object
0197   /// @param logger a logger instance
0198   /// @return True if volume constraints violated and propagation should abort
0199   template <typename propagator_state_t, typename stepper_t,
0200             typename navigator_t>
0201   bool checkAbort(propagator_state_t& state, const stepper_t& /*stepper*/,
0202                   const navigator_t& navigator, const Logger& logger) const {
0203     const auto& constrainToVolumeIds = state.options.constrainToVolumeIds;
0204     const auto& endOfWorldVolumeIds = state.options.endOfWorldVolumeIds;
0205 
0206     if (constrainToVolumeIds.empty() && endOfWorldVolumeIds.empty()) {
0207       return false;
0208     }
0209     const auto* currentVolume = navigator.currentVolume(state.navigation);
0210 
0211     // We need a volume to check its ID
0212     if (currentVolume == nullptr) {
0213       return false;
0214     }
0215 
0216     const auto currentVolumeId =
0217         static_cast<std::uint32_t>(currentVolume->geometryId().volume());
0218 
0219     if (!constrainToVolumeIds.empty() &&
0220         !rangeContainsValue(constrainToVolumeIds, currentVolumeId)) {
0221       ACTS_VERBOSE(
0222           "VolumeConstraintAborter aborter | Abort with volume constrain "
0223           << currentVolumeId);
0224       return true;
0225     }
0226 
0227     if (!endOfWorldVolumeIds.empty() &&
0228         rangeContainsValue(endOfWorldVolumeIds, currentVolumeId)) {
0229       ACTS_VERBOSE(
0230           "VolumeConstraintAborter aborter | Abort with additional end of "
0231           "world volume "
0232           << currentVolumeId);
0233       return true;
0234     }
0235 
0236     return false;
0237   }
0238 };
0239 
0240 /// Aborter that checks if the propagation has reached any surface
0241 struct AnySurfaceReached {
0242   /// Check if any surface has been reached during propagation
0243   /// @tparam propagator_state_t Type of the propagator state
0244   /// @tparam stepper_t Type of the stepper
0245   /// @tparam navigator_t Type of the navigator
0246   /// @param state The propagation state object
0247   /// @param stepper Stepper used for propagation (unused)
0248   /// @param navigator Navigator used for propagation
0249   /// @param logger Logger instance (unused)
0250   /// @return true if any surface has been reached, false otherwise
0251   template <typename propagator_state_t, typename stepper_t,
0252             typename navigator_t>
0253   bool checkAbort(propagator_state_t& state, const stepper_t& stepper,
0254                   const navigator_t& navigator, const Logger& logger) const {
0255     static_cast<void>(stepper);
0256     static_cast<void>(logger);
0257 
0258     const Surface* startSurface = navigator.startSurface(state.navigation);
0259     const Surface* targetSurface = navigator.targetSurface(state.navigation);
0260     const Surface* currentSurface = navigator.currentSurface(state.navigation);
0261 
0262     // `startSurface` is excluded because we want to reach a new surface
0263     // `targetSurface` is excluded because another aborter should handle it
0264     if (currentSurface != nullptr && currentSurface != startSurface &&
0265         currentSurface != targetSurface) {
0266       return true;
0267     }
0268 
0269     return false;
0270   }
0271 };
0272 
0273 }  // namespace Acts