Skip to content

fix: std::ranges fixes done by AI#5311

Open
asalzburger wants to merge 9 commits intoacts-project:mainfrom
asalzburger:chore-std-ranges
Open

fix: std::ranges fixes done by AI#5311
asalzburger wants to merge 9 commits intoacts-project:mainfrom
asalzburger:chore-std-ranges

Conversation

@asalzburger
Copy link
Copy Markdown
Contributor

@asalzburger asalzburger commented Apr 2, 2026

C++20 std::ranges cleanups (Sonar)

These have been done within cursor using the output of our SonarCloud run

Summary

This PR applies Sonar-driven updates to prefer std::ranges:: algorithms that take a range (and related range style) across Core, while keeping behaviour identical to the pre-change code. It also fixes a regression in getMinMaxAndBinCount introduced when first switching to std::ranges::unique, which had broken magnetic-field and material-map unit tests until the unique-count logic was corrected.

20 files, ~130 insertions / 129 deletions (no functional intent beyond the fixes below).

Motivation

Address a batch of Sonar issues whose messages ask to use std::ranges::find / transform / copy / upper_bound / lower_bound / min_element / max_element / count_if / remove_if / unique / all_of / any_of, or to prefer range-based loops / std::ranges::for_each where applicable.

(One reported item for std::ranges::set_difference in LayerCreator.cpp was not applied: that call site is not present in the current tree / line mapping was stale.)

What changed

Algorithms and loops

Area Change
Subspace / projector indices SubspaceHelpers: std::find / std::transformstd::ranges::find on subranges, std::ranges::transform; duplicate detection in checkSubspaceIndices uses a tail subrange. TrackStateProxyCommon: std::ranges::transform for projector subspace copy.
Geometry / binning GeometryHierarchyMap, SurfaceArrayCreator, Axis, TrackSelector, BinningData: upper_bound / lower_boundstd::ranges::* + std::ranges::distance where needed.
Bin utility BinUtility::inside: loop → std::ranges::all_of over binning data.
Propagator ConstrainedStep: std::ranges::min_element on step values. MultiStepperLoop: std::ranges::count_if; removal uses std::ranges::remove_if + erase on the returned tail (see below).
Gsf GsfUtils: range-based for for weight normalization; std::ranges::min_element for chi² minimum; std::all_of(begin, end, …) kept for finite-weight check because the const component iterable does not model std::ranges::input_range with libc++.
Utilities Grid: std::ranges::copy. BoundingBox.ipp: std::ranges::transform (+ <algorithm>). Table: range-for over columns; std::ranges::transform for lowercasing alignment strings. Helpers: see critical fix below.
Geometry sources CuboidVolumeBounds: std::ranges::any_of. CuboidVolumeStack / CylinderVolumeStack / CylinderVolumeBuilder: ranges::copy / transform / min_element / max_element. GreedyAmbiguityResolution: std::ranges::max_element on selected tracks.

libc++ / API constraints

  1. MultiStepperLoop: std::erase_if is not available for boost::container::small_vector on this standard library. Replaced with std::ranges::remove_if and erase on the subrange of elements to drop (same effect as erase–remove).

  2. GsfUtils checks: std::ranges::all_of on the const component iterable fails constraints; kept std::all_of with explicit iterators.

Bugfix: detail::getMinMaxAndBinCount (Helpers.hpp)

std::ranges::unique returns the subrange [ret, end) (duplicate tail), not the unique prefix. The unique-element count must be:

std::ranges::distance(xPos.begin(), uniqueTail.begin())

Using distance(uniqueTail) counted duplicates, broke xBinCount, stepX, and surfaced as NaN / inf and std::out_of_range in TextMagneticFieldIo, BFieldMapUtils, MaterialMapUtils, and RootMagneticFieldIo. This PR corrects that and documents it in the file comment.

Files touched

Core/include/Acts/EventData/SubspaceHelpers.hpp
Core/include/Acts/EventData/TrackStateProxyCommon.hpp
Core/include/Acts/Geometry/GeometryHierarchyMap.hpp
Core/include/Acts/Geometry/SurfaceArrayCreator.hpp
Core/include/Acts/Propagator/ConstrainedStep.hpp
Core/include/Acts/Propagator/MultiStepperLoop.ipp
Core/include/Acts/TrackFinding/TrackSelector.hpp
Core/include/Acts/TrackFitting/detail/GsfUtils.hpp
Core/include/Acts/Utilities/Axis.hpp
Core/include/Acts/Utilities/BinUtility.hpp
Core/include/Acts/Utilities/BinningData.hpp
Core/include/Acts/Utilities/BoundingBox.ipp
Core/include/Acts/Utilities/Grid.hpp
Core/include/Acts/Utilities/Helpers.hpp
Core/include/Acts/Utilities/Table.hpp
Core/src/AmbiguityResolution/GreedyAmbiguityResolution.cpp
Core/src/Geometry/CuboidVolumeBounds.cpp
Core/src/Geometry/CuboidVolumeStack.cpp
Core/src/Geometry/CylinderVolumeBuilder.cpp
Core/src/Geometry/CylinderVolumeStack.cpp

Testing

  • Full acts build acts-sonarcloud succeeds.
  • Previously failing tests pass after rebuild, including:
    • TextMagneticFieldIo, BFieldMapUtils, MaterialMapUtils, RootMagneticFieldIo
  • Recommend running the full unit suite before merge (e.g. acts test acts-sonarcloud).

Not in this PR

  • Updates under local install paths (e.g. Sonar JSON export / “resolved” flags) are tooling only, not part of this repository diff.

Maintainer checklist

  • CI / full test grid green
  • No behaviour change intended except getMinMaxAndBinCount fix (restores correct bin count vs broken intermediate ranges::unique usage)

--- END COMMIT MESSAGE ---

Any further description goes here, @-mentions are ok here!

  • Use a conventional commits prefix: quick summary
    • We mostly use feat, fix, refactor, docs, chore and build types.
  • A milestone will be assigned by one of the maintainers

@github-actions github-actions bot added this to the next milestone Apr 2, 2026
@asalzburger asalzburger changed the title chore: std::ranges fixes chore: std::ranges fixes done by AI Apr 2, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 2, 2026

📊: Physics performance monitoring for 1efbc4e

Full contents

physmon summary

@asalzburger asalzburger changed the title chore: std::ranges fixes done by AI fix: std::ranges fixes done by AI Apr 3, 2026
asalzburger and others added 7 commits April 4, 2026 07:08
Co-authored-by: Andreas Stefl <stefl.andreas@gmail.com>
Co-authored-by: Andreas Stefl <stefl.andreas@gmail.com>
Co-authored-by: Andreas Stefl <stefl.andreas@gmail.com>
Co-authored-by: Andreas Stefl <stefl.andreas@gmail.com>
Co-authored-by: Andreas Stefl <stefl.andreas@gmail.com>
Co-authored-by: Andreas Stefl <stefl.andreas@gmail.com>
@asalzburger asalzburger requested a review from andiwand April 4, 2026 07:29
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Apr 4, 2026

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants