Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-12-01 09:18:37

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/Direction.hpp"
0012 #include "Acts/Definitions/Units.hpp"
0013 #include "Acts/Geometry/Layer.hpp"
0014 #include "Acts/Geometry/TrackingGeometry.hpp"
0015 #include "Acts/Geometry/TrackingVolume.hpp"
0016 #include "Acts/Propagator/NavigationTarget.hpp"
0017 #include "Acts/Propagator/NavigatorInitializeArguments.hpp"
0018 #include "Acts/Propagator/NavigatorOptions.hpp"
0019 #include "Acts/Propagator/NavigatorStatistics.hpp"
0020 #include "Acts/Propagator/detail/AdditionalSurfaceNavigation.hpp"
0021 #include "Acts/Surfaces/BoundaryTolerance.hpp"
0022 #include "Acts/Surfaces/Surface.hpp"
0023 #include "Acts/Utilities/Enumerate.hpp"
0024 #include "Acts/Utilities/Intersection.hpp"
0025 #include "Acts/Utilities/Logger.hpp"
0026 #include "Acts/Utilities/Result.hpp"
0027 
0028 #include <limits>
0029 #include <memory>
0030 
0031 namespace Acts {
0032 
0033 /// @brief A fully guided navigator
0034 ///
0035 /// This is a fully guided navigator that progresses through a provided sequence
0036 /// of surfaces.
0037 ///
0038 /// This can either be used as a validation tool, for truth tracking, or track
0039 /// refitting.
0040 ///
0041 class DirectNavigator {
0042  public:
0043   /// @brief The nested configuration struct
0044   struct Config {};
0045 
0046   /// @brief The nested options struct
0047   struct Options : public NavigatorPlainOptions {
0048     /// Constructor from geometry context
0049     /// @param gctx The geometry context
0050     explicit Options(const GeometryContext& gctx)
0051         : NavigatorPlainOptions(gctx) {
0052       // TODO https://github.com/acts-project/acts/issues/2738
0053       // The direct navigator uses a relaxed near limit to discard
0054       // intersections "behind us". This is only necessary because some
0055       // surfaces have more than one intersection.
0056       nearLimit = -100 * UnitConstants::um;
0057     }
0058 
0059     /// The ordered sequence of surfaces to walk through
0060     std::vector<const Surface*> surfaceSequence;
0061 
0062     /// Set the plain navigator options
0063     /// @param options The plain navigator options to copy
0064     void setPlainOptions(const NavigatorPlainOptions& options) {
0065       // Preserve the direct-navigator-specific near limit, which differs from
0066       // the plain default.
0067       const double keepNearLimit = nearLimit;
0068       static_cast<NavigatorPlainOptions&>(*this) = options;
0069       nearLimit = keepNearLimit;
0070     }
0071   };
0072 
0073   /// @brief Nested State struct
0074   ///
0075   /// It acts as an internal state which is created for every
0076   /// propagation/extrapolation step and keep thread-local navigation
0077   /// information
0078   struct State {
0079     /// Constructor from options
0080     /// @param options_ The navigator options
0081     explicit State(const Options& options_) : options(options_) {}
0082 
0083     /// Configuration options for the direct navigator
0084     Options options;
0085 
0086     /// Propagation direction (forward or backward)
0087     Direction direction = Direction::Forward();
0088 
0089     /// Surface where the navigation started
0090     const Surface* startSurface = nullptr;
0091 
0092     /// Surface that is the target of the navigation
0093     const Surface* targetSurface = nullptr;
0094 
0095     /// Index of the next surface to try
0096     /// @note -1 means before the first surface in the sequence and size()
0097     ///       means after the last surface in the sequence
0098     std::int32_t surfaceIndex = -1;
0099 
0100     /// Navigation state - external interface: the current surface
0101     const Surface* currentSurface = nullptr;
0102 
0103     /// Navigation state - external interface: a break has been detected
0104     bool navigationBreak = false;
0105 
0106     /// Additional surfaces and candidate history
0107     detail::AdditionalSurfaceNavigation additional;
0108 
0109     /// Navigation statistics
0110     NavigatorStatistics statistics;
0111 
0112     /// Get the current navigation surface
0113     /// @return Reference to the surface at the current surface index
0114     const Surface& navSurface() const {
0115       return *options.surfaceSequence.at(surfaceIndex);
0116     }
0117 
0118     /// Move to the next surface in the sequence
0119     /// Increments or decrements surface index based on propagation direction
0120     void nextSurface() {
0121       if (direction == Direction::Forward()) {
0122         ++surfaceIndex;
0123       } else {
0124         --surfaceIndex;
0125       }
0126     }
0127 
0128     /// Check if we have reached the end of the surface sequence
0129     /// @return True if no more surfaces remain in the propagation direction
0130     bool endOfSurfaces() const {
0131       if (direction == Direction::Forward()) {
0132         return surfaceIndex >= static_cast<int>(options.surfaceSequence.size());
0133       }
0134       return surfaceIndex < 0;
0135     }
0136 
0137     /// Get the number of surfaces remaining in the sequence
0138     /// @return Number of surfaces left to process in the propagation direction
0139     int remainingSurfaces() const {
0140       if (direction == Direction::Forward()) {
0141         return static_cast<int>(options.surfaceSequence.size()) - surfaceIndex;
0142       }
0143       return surfaceIndex + 1;
0144     }
0145 
0146     /// Reset the surface index to the initial position
0147     /// Sets index to before first surface (forward) or after last surface
0148     /// (backward)
0149     void resetSurfaceIndex() {
0150       surfaceIndex = direction == Direction::Forward()
0151                          ? -1
0152                          : static_cast<int>(options.surfaceSequence.size());
0153     }
0154   };
0155 
0156   /// Constructor with optional logger
0157   /// @param _logger Logger instance for navigation messages
0158   explicit DirectNavigator(std::unique_ptr<const Logger> _logger =
0159                                getDefaultLogger("DirectNavigator",
0160                                                 Logging::INFO))
0161       : m_logger{std::move(_logger)} {}
0162 
0163   /// Create a new navigation state from options
0164   /// @param options The navigator options
0165   /// @return New state initialized with the provided options
0166   State makeState(const Options& options) const {
0167     State state(options);
0168     return state;
0169   }
0170 
0171   /// Get the current surface from the navigation state
0172   /// @param state The navigation state
0173   /// @return Pointer to current surface, nullptr if none
0174   const Surface* currentSurface(const State& state) const {
0175     return state.currentSurface;
0176   }
0177 
0178   /// Get the current tracking volume (not used by DirectNavigator)
0179   /// @param state The navigation state (unused)
0180   /// @return Always returns nullptr as DirectNavigator doesn't use volumes
0181   const TrackingVolume* currentVolume(const State& state) const {
0182     static_cast<void>(state);
0183     return nullptr;
0184   }
0185 
0186   /// Get the current volume material (not used by DirectNavigator)
0187   /// @param state The navigation state (unused)
0188   /// @return Always returns nullptr as DirectNavigator doesn't use volume material
0189   const IVolumeMaterial* currentVolumeMaterial(const State& state) const {
0190     static_cast<void>(state);
0191     return nullptr;
0192   }
0193 
0194   /// Get the start surface from the navigation state
0195   /// @param state The navigation state
0196   /// @return Pointer to start surface, nullptr if none
0197   const Surface* startSurface(const State& state) const {
0198     return state.startSurface;
0199   }
0200 
0201   /// Get the target surface from the navigation state
0202   /// @param state The navigation state
0203   /// @return Pointer to target surface, nullptr if none
0204   const Surface* targetSurface(const State& state) const {
0205     return state.targetSurface;
0206   }
0207 
0208   /// Check if the end of world has been reached (not applicable for
0209   /// DirectNavigator)
0210   /// @param state The navigation state (unused)
0211   /// @return Always returns false as DirectNavigator operates on a surface sequence
0212   bool endOfWorldReached(State& state) const {
0213     static_cast<void>(state);
0214     return false;
0215   }
0216 
0217   /// Check if navigation should break/stop
0218   /// @param state The navigation state
0219   /// @return True if navigation should break, false otherwise
0220   bool navigationBreak(const State& state) const {
0221     return state.navigationBreak;
0222   }
0223 
0224   /// @brief Initialize the navigator
0225   ///
0226   /// This function initializes the navigator for a new propagation.
0227   ///
0228   /// @param state The navigation state
0229   /// @param args The initialization arguments of this navigation run
0230   /// @return Always returns success result as DirectNavigator initialization cannot fail
0231   [[nodiscard]] Result<void> initialize(
0232       State& state, const NavigatorInitializeArguments& args) const {
0233     ACTS_VERBOSE("Initialize. Surface sequence for navigation:");
0234     for (const Surface* surface : state.options.surfaceSequence) {
0235       ACTS_VERBOSE(surface->geometryId()
0236                    << " - "
0237                    << surface->center(state.options.geoContext).transpose());
0238     }
0239 
0240     state.direction = args.propagationDirection;
0241     ACTS_VERBOSE("Navigation direction is " << args.propagationDirection);
0242 
0243     state.startSurface = args.startSurface;
0244     state.targetSurface = args.targetSurface;
0245     state.additional.initialize(state.options, args.targetSurface);
0246     state.statistics = {};
0247 
0248     // We set the current surface to the start surface
0249     state.currentSurface = state.startSurface;
0250     if (state.currentSurface != nullptr) {
0251       ACTS_VERBOSE("Current surface set to start surface "
0252                    << state.currentSurface->geometryId());
0253     } else {
0254       ACTS_VERBOSE("Current surface set to nullptr");
0255     }
0256 
0257     // Find initial index.
0258     auto found =
0259         std::ranges::find(state.options.surfaceSequence, state.startSurface);
0260 
0261     if (found != state.options.surfaceSequence.end()) {
0262       // The sequence continues after the start surface
0263       state.surfaceIndex = static_cast<std::int32_t>(
0264           std::distance(state.options.surfaceSequence.begin(), found));
0265     } else {
0266       ACTS_DEBUG(
0267           "Did not find the start surface in the sequence. Assuming it is not "
0268           "part of the sequence. Trusting the correctness of the input "
0269           "sequence. Resetting the surface index.");
0270       state.resetSurfaceIndex();
0271     }
0272 
0273     state.navigationBreak = false;
0274 
0275     return Result<void>::success();
0276   }
0277 
0278   /// @brief Get the next target surface
0279   ///
0280   /// This function gets the next target surface for the propagation. For
0281   /// the direct navigator this is the next surface in the sequence, unless an
0282   /// additional surface is closer.
0283   ///
0284   /// @param state The navigation state
0285   /// @param position The current position
0286   /// @param direction The current direction
0287   /// @param curvature Current global geometric curvature (inverse length)
0288   ///
0289   /// @return The next target surface
0290   NavigationTarget nextTarget(
0291       State& state, const Vector3& position, const Vector3& direction,
0292       [[maybe_unused]] const Vector3& curvature = Vector3::Zero()) const {
0293     // Navigator target always resets the current surface
0294     state.currentSurface = nullptr;
0295 
0296     // The additional surfaces outlive the surface sequence
0297     const TrackingVolume* noVolume = nullptr;
0298     return state.additional.nextTarget(
0299         state.options, position, direction, noVolume,
0300         [&] { return nextSequenceTarget(state, position, direction); });
0301   }
0302 
0303   /// @brief Check if the current target is still valid
0304   ///
0305   /// The surface sequence stays valid, so only the additional surfaces can
0306   /// take over the target.
0307   ///
0308   /// @param state The navigation state
0309   /// @param position The current position
0310   /// @param direction The current direction
0311   /// @param curvature Current global geometric curvature (inverse length)
0312   ///
0313   /// @return True if the target is valid
0314   bool checkTargetValid(
0315       State& state, const Vector3& position, const Vector3& direction,
0316       [[maybe_unused]] const Vector3& curvature = Vector3::Zero()) const {
0317     return state.additional.checkTargetValid(state.options, position, direction,
0318                                              nullptr, true);
0319   }
0320 
0321   /// @brief Handle the surface reached
0322   ///
0323   /// This function handles the surface reached. For the direct navigator this
0324   /// effectively sets the current surface to the reached surface.
0325   ///
0326   /// @param state The navigation state
0327   /// @param position The current position
0328   /// @param direction The current direction
0329   /// @param surface The surface reached
0330   /// @param curvature Current global geometric curvature (inverse length)
0331   void handleSurfaceReached(
0332       State& state, const Vector3& position, const Vector3& direction,
0333       const Surface& surface,
0334       [[maybe_unused]] const Vector3& curvature = Vector3::Zero()) const {
0335     static_cast<void>(position);
0336     static_cast<void>(direction);
0337 
0338     ACTS_VERBOSE("DirectNavigator::handleSurfaceReached");
0339 
0340     // Reaching an additional surface does not advance the sequence, unless
0341     // the sequence targets the surface too
0342     if (state.additional.handleSurfaceReached(surface)) {
0343       state.currentSurface = &surface;
0344       ACTS_VERBOSE("Current surface set to additional surface "
0345                    << surface.geometryId());
0346       return;
0347     }
0348 
0349     if (state.navigationBreak) {
0350       return;
0351     }
0352 
0353     // Set the current surface
0354     state.currentSurface = &state.navSurface();
0355     ACTS_VERBOSE("Current surface set to "
0356                  << state.currentSurface->geometryId());
0357   }
0358 
0359  private:
0360   /// Get the next surface of the sequence
0361   NavigationTarget nextSequenceTarget(State& state, const Vector3& position,
0362                                       const Vector3& direction) const {
0363     if (state.navigationBreak) {
0364       return NavigationTarget::None();
0365     }
0366 
0367     ACTS_VERBOSE("DirectNavigator::nextTarget");
0368 
0369     // Move the sequence to the next surface
0370     state.nextSurface();
0371 
0372     while (!state.endOfSurfaces()) {
0373       ACTS_VERBOSE("Next surface candidate is "
0374                    << state.navSurface().geometryId() << ". "
0375                    << state.remainingSurfaces() << " out of "
0376                    << state.options.surfaceSequence.size()
0377                    << " surfaces remain to try.");
0378 
0379       // Establish & update the surface status
0380       // TODO we do not know the intersection index - passing the closer one
0381       const Surface& surface = state.navSurface();
0382       const double farLimit = std::numeric_limits<double>::max();
0383       const NavigationTarget target = chooseIntersection(
0384           state.options.geoContext, surface, position, direction,
0385           BoundaryTolerance::Infinite(), state.options.nearLimit, farLimit,
0386           state.options.surfaceTolerance);
0387       if (target.isValid()) {
0388         return target;
0389       }
0390 
0391       ACTS_VERBOSE("No valid intersection found with surface "
0392                    << surface.geometryId() << ", trying next surface.");
0393       state.nextSurface();
0394     }
0395 
0396     ACTS_VERBOSE("End of surfaces reached, navigation break.");
0397     state.navigationBreak = true;
0398     return NavigationTarget::None();
0399   }
0400 
0401   NavigationTarget chooseIntersection(
0402       const GeometryContext& gctx, const Surface& surface,
0403       const Vector3& position, const Vector3& direction,
0404       const BoundaryTolerance& boundaryTolerance, double nearLimit,
0405       double farLimit, double tolerance) const {
0406     auto intersections = surface.intersect(gctx, position, direction,
0407                                            boundaryTolerance, tolerance);
0408 
0409     for (auto [intersectionIndex, intersection] :
0410          Acts::enumerate(intersections)) {
0411       if (intersection.isValid() &&
0412           detail::checkPathLength(intersection.pathLength(), nearLimit,
0413                                   farLimit, logger())) {
0414         return NavigationTarget(intersection, intersectionIndex, surface,
0415                                 boundaryTolerance);
0416       }
0417     }
0418 
0419     return NavigationTarget::None();
0420   }
0421 
0422   const Logger& logger() const { return *m_logger; }
0423 
0424   std::unique_ptr<const Logger> m_logger;
0425 };
0426 
0427 }  // namespace Acts