Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use std::ranges:find, find_if, find_if_not #3614

Merged
merged 8 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions Core/include/Acts/TrackFitting/KalmanFitter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "Acts/Utilities/Result.hpp"
#include "Acts/Utilities/TrackHelpers.hpp"

#include <algorithm>
#include <functional>
#include <limits>
#include <map>
Expand Down Expand Up @@ -483,11 +484,9 @@ class KalmanFitter {
result.fittedStates->applyBackwards(
result.lastMeasurementIndex, [&](auto trackState) {
auto fSurface = &trackState.referenceSurface();
auto surface_it = std::find_if(
result.passedAgainSurfaces.begin(),
result.passedAgainSurfaces.end(),
[=](const Surface* s) { return s == fSurface; });
if (surface_it == result.passedAgainSurfaces.end()) {
if (std::ranges::none_of(
result.passedAgainSurfaces,
[=](const Surface* s) { return s == fSurface; })) {
// If reversed filtering missed this surface, then there is
// no smoothed parameter
trackState.unset(TrackStatePropMask::Smoothed);
Expand Down
4 changes: 2 additions & 2 deletions Core/include/Acts/Vertexing/AdaptiveMultiVertexFitter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "Acts/Vertexing/VertexingError.hpp"
#include "Acts/Vertexing/VertexingOptions.hpp"

#include <algorithm>
#include <functional>

namespace Acts {
Expand Down Expand Up @@ -81,8 +82,7 @@ class AdaptiveMultiVertexFitter {

Result<void> removeVertexFromCollection(Vertex& vtxToRemove,
const Logger& logger) {
auto it = std::find(vertexCollection.begin(), vertexCollection.end(),
&vtxToRemove);
auto it = std::ranges::find(vertexCollection, &vtxToRemove);
// Check if the value was found before erasing
if (it == vertexCollection.end()) {
ACTS_ERROR("vtxToRemove is not part of vertexCollection.");
Expand Down
21 changes: 9 additions & 12 deletions Core/src/Geometry/CylinderVolumeBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,19 +274,17 @@ Acts::CylinderVolumeBuilder::trackingVolume(
double tolerance = m_cfg.ringTolerance;
// Search for the rmin value - and insert if necessary
double rMin = discBounds->rMin();
auto innerSearch = std::find_if(
innerRadii.begin(), innerRadii.end(), [&](double reference) {
return std::abs(rMin - reference) < tolerance;
});
auto innerSearch = std::ranges::find_if(innerRadii, [&](double r) {
return std::abs(rMin - r) < tolerance;
});
if (innerSearch == innerRadii.end()) {
innerRadii.push_back(rMin);
}
// Search for the rmax value - and insert if necessary
double rMax = discBounds->rMax();
auto outerSearch = std::find_if(
outerRadii.begin(), outerRadii.end(), [&](double reference) {
return std::abs(rMax - reference) < tolerance;
});
auto outerSearch = std::ranges::find_if(outerRadii, [&](double r) {
return std::abs(rMax - r) < tolerance;
});
if (outerSearch == outerRadii.end()) {
outerRadii.push_back(rMax);
}
Expand Down Expand Up @@ -356,10 +354,9 @@ Acts::CylinderVolumeBuilder::trackingVolume(
double test = elay->surfaceRepresentation().binningPositionValue(
gctx, BinningValue::binR);
// Find the right bin
auto ringVolume = std::find_if(
volumeRminRmax.begin(), volumeRminRmax.end(),
[&](const auto& reference) {
return (test > reference.first && test < reference.second);
auto ringVolume =
std::ranges::find_if(volumeRminRmax, [&](const auto& vrr) {
return (test > vrr.first && test < vrr.second);
});
if (ringVolume != volumeRminRmax.end()) {
unsigned int ringBin =
Expand Down
8 changes: 3 additions & 5 deletions Core/src/Geometry/Layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,9 @@ Acts::Layer::compatibleSurfaces(
double farLimit = options.farLimit;

auto isUnique = [&](const SurfaceIntersection& b) {
auto find_it = std::find_if(
sIntersections.begin(), sIntersections.end(), [&b](const auto& a) {
return a.object() == b.object() && a.index() == b.index();
});
return find_it == sIntersections.end();
return std::ranges::none_of(sIntersections, [&b](const auto& a) {
return a.object() == b.object() && a.index() == b.index();
});
};

// lemma 0 : accept the surface
Expand Down
6 changes: 2 additions & 4 deletions Core/src/Surfaces/VerticesHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@ std::vector<Acts::ActsScalar> Acts::detail::VerticesHelper::phiSegments(
if (!phiRefs.empty()) {
for (const auto& phiRef : phiRefs) {
// Trying to find the right patch
auto match = std::find_if(
phiSegments.begin(), phiSegments.end(), [&](ActsScalar phiSeg) {
if (std::ranges::none_of(phiSegments, [&](ActsScalar phiSeg) {
return std::abs(phiSeg - phiRef) < phiTolerance;
});
if (match == phiSegments.end()) {
})) {
phiSegments.push_back(phiRef);
}
}
Expand Down
8 changes: 4 additions & 4 deletions Core/src/TrackFinding/MeasurementSelector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ MeasurementSelector::Cuts MeasurementSelector::getCutsByTheta(
// look at the positive half of the Z axis
const double constrainedTheta = std::min(theta, M_PI - theta);

auto it = std::find_if(config.begin(), config.end(),
[constrainedTheta](const InternalCutBin& cuts) {
return constrainedTheta < cuts.maxTheta;
});
auto it = std::ranges::find_if(
config, [constrainedTheta](const InternalCutBin& cuts) {
return constrainedTheta < cuts.maxTheta;
});
assert(it != config.end());
return {it->maxNumMeasurements, it->maxChi2Measurement, it->maxChi2Outlier};
}
Expand Down
3 changes: 1 addition & 2 deletions Core/src/Utilities/BinningType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ const std::vector<BinningValue>& allBinningValues() {
}

BinningValue binningValueFromName(const std::string& name) {
auto it =
std::find(s_binningValueNames.begin(), s_binningValueNames.end(), name);
auto it = std::ranges::find(s_binningValueNames, name);
if (it == s_binningValueNames.end()) {
throw std::invalid_argument("Unknown binning value name: " + name);
}
Expand Down
17 changes: 8 additions & 9 deletions Core/src/Vertexing/AdaptiveMultiVertexFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "Acts/Vertexing/IVertexFinder.hpp"
#include "Acts/Vertexing/VertexingError.hpp"

#include <algorithm>

namespace Acts {

Result<std::vector<Vertex>> AdaptiveMultiVertexFinder::find(
Expand Down Expand Up @@ -364,9 +366,8 @@ std::pair<int, bool> AdaptiveMultiVertexFinder::checkVertexAndCompatibleTracks(
!m_cfg.useFastCompatibility)) {
// TODO: Understand why looking for compatible tracks only in seed tracks
// and not also in all tracks
auto foundIter =
std::find_if(seedTracks.begin(), seedTracks.end(),
[&trk](auto seedTrk) { return trk == seedTrk; });
auto foundIter = std::ranges::find_if(
AJPfleger marked this conversation as resolved.
Show resolved Hide resolved
seedTracks, [&trk](auto seedTrk) { return trk == seedTrk; });
if (foundIter != seedTracks.end()) {
nCompatibleTracks++;
ACTS_DEBUG("Compatible track found.");
Expand Down Expand Up @@ -399,9 +400,8 @@ auto AdaptiveMultiVertexFinder::removeCompatibleTracksFromSeedTracks(
trkAtVtx.chi2Track < m_cfg.maxVertexChi2 &&
!m_cfg.useFastCompatibility)) {
// Find and remove track from seedTracks
auto foundSeedIter =
std::find_if(seedTracks.begin(), seedTracks.end(),
[&trk](auto seedTrk) { return trk == seedTrk; });
auto foundSeedIter = std::ranges::find_if(
seedTracks, [&trk](auto seedTrk) { return trk == seedTrk; });
if (foundSeedIter != seedTracks.end()) {
seedTracks.erase(foundSeedIter);
removedSeedTracks.push_back(trk);
Expand All @@ -425,9 +425,8 @@ bool AdaptiveMultiVertexFinder::removeTrackIfIncompatible(
double compatibility = trkAtVtx.vertexCompatibility;
if (compatibility > maxCompatibility) {
// Try to find track in seed tracks
auto foundSeedIter =
std::find_if(seedTracks.begin(), seedTracks.end(),
[&trk](auto seedTrk) { return trk == seedTrk; });
auto foundSeedIter = std::ranges::find_if(
AJPfleger marked this conversation as resolved.
Show resolved Hide resolved
seedTracks, [&trk](auto seedTrk) { return trk == seedTrk; });
if (foundSeedIter != seedTracks.end()) {
maxCompatibility = compatibility;
maxCompSeedIt = foundSeedIter;
Expand Down
34 changes: 15 additions & 19 deletions Core/src/Vertexing/IterativeVertexFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,9 @@ inline void Acts::IterativeVertexFinder::removeTracks(
const BoundTrackParameters& params = m_cfg.extractParameters(trk);
// Find track in seedTracks
auto foundIter =
std::find_if(seedTracks.begin(), seedTracks.end(),
[&params, this](const auto seedTrk) {
return params == m_cfg.extractParameters(seedTrk);
});
std::ranges::find_if(seedTracks, [&params, this](const auto seedTrk) {
return params == m_cfg.extractParameters(seedTrk);
});
if (foundIter != seedTracks.end()) {
// Remove track from seed tracks
seedTracks.erase(foundIter);
Expand Down Expand Up @@ -284,10 +283,9 @@ Acts::Result<void> Acts::IterativeVertexFinder::removeUsedCompatibleTracks(
}
// Find and remove track from seedTracks
auto foundSeedIter =
std::find_if(seedTracks.begin(), seedTracks.end(),
[&trackAtVtx](const auto& seedTrk) {
return trackAtVtx.originalParams == seedTrk;
});
std::ranges::find_if(seedTracks, [&trackAtVtx](const auto& seedTrk) {
return trackAtVtx.originalParams == seedTrk;
});
if (foundSeedIter != seedTracks.end()) {
seedTracks.erase(foundSeedIter);
} else {
Expand All @@ -296,10 +294,9 @@ Acts::Result<void> Acts::IterativeVertexFinder::removeUsedCompatibleTracks(

// Find and remove track from tracksToFit
auto foundFitIter =
std::find_if(tracksToFit.begin(), tracksToFit.end(),
[&trackAtVtx](const auto& fitTrk) {
return trackAtVtx.originalParams == fitTrk;
});
std::ranges::find_if(tracksToFit, [&trackAtVtx](const auto& fitTrk) {
return trackAtVtx.originalParams == fitTrk;
});
if (foundFitIter != tracksToFit.end()) {
tracksToFit.erase(foundFitIter);
} else {
Expand Down Expand Up @@ -334,9 +331,8 @@ Acts::Result<void> Acts::IterativeVertexFinder::removeUsedCompatibleTracks(
// check if sufficiently compatible with last fitted vertex
// (quite loose constraint)
if (chi2 < m_cfg.maximumChi2cutForSeeding) {
auto foundIter =
std::find_if(seedTracks.begin(), seedTracks.end(),
[&trk](const auto& seedTrk) { return trk == seedTrk; });
auto foundIter = std::ranges::find_if(
seedTracks, [&trk](const auto& seedTrk) { return trk == seedTrk; });
if (foundIter != seedTracks.end()) {
// Remove track from seed tracks
seedTracks.erase(foundIter);
Expand All @@ -345,8 +341,8 @@ Acts::Result<void> Acts::IterativeVertexFinder::removeUsedCompatibleTracks(
} else {
// Track not compatible with vertex
// Remove track from current vertex
auto foundIter = std::find_if(
tracksAtVertex.begin(), tracksAtVertex.end(),
auto foundIter = std::ranges::find_if(
tracksAtVertex,
[&trk](auto trkAtVtx) { return trk == trkAtVtx.originalParams; });
if (foundIter != tracksAtVertex.end()) {
// Remove track from seed tracks
Expand Down Expand Up @@ -495,8 +491,8 @@ Acts::Result<bool> Acts::IterativeVertexFinder::reassignTracksToNewVertex(
// delete it later
// when all tracks used to fit current vertex are deleted
seedTracks.push_back(tracksIter->originalParams);
// seedTracks.push_back(*std::find_if(
// origTracks.begin(), origTracks.end(),
// seedTracks.push_back(*std::ranges::find_if(
// origTracks,
// [&origParams, this](auto origTrack) {
// return origParams == m_extractParameters(*origTrack);
// }));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,10 @@ Pythia8Generator::operator()(RandomEngine& rng) {

// check if an existing vertex is close enough
auto it =
std::find_if(vertices.begin(), vertices.end(),
[&pos4, this](const SimVertex& other) {
return (pos4.head<3>() - other.position()).norm() <
m_cfg.spatialVertexThreshold;
});
std::ranges::find_if(vertices, [&pos4, this](const SimVertex& v) {
return (pos4.head<3>() - v.position()).norm() <
m_cfg.spatialVertexThreshold;
});

if (it != vertices.end()) {
particleId.setVertexSecondary(std::distance(vertices.begin(), it));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <Acts/Surfaces/SurfaceBounds.hpp>
#include <Acts/Surfaces/TrapezoidBounds.hpp>

#include <algorithm>
#include <fstream>
#include <map>
#include <mutex>
Expand Down Expand Up @@ -93,14 +94,10 @@ class MappingMaterialDecorator : public IMaterialDecorator {
///
/// @param volume to be looped onto
void volumeLoop(const Acts::TrackingVolume* tVolume) {
auto sameId =
[tVolume](
const std::pair<Acts::GeometryIdentifier,
std::shared_ptr<const IVolumeMaterial>>& pair) {
return (tVolume->geometryId() == pair.first);
};
if (std::find_if(m_volumeMaterialMap.begin(), m_volumeMaterialMap.end(),
sameId) != m_volumeMaterialMap.end()) {
auto sameId = [tVolume](const auto& pair) {
return (tVolume->geometryId() == pair.first);
};
if (std::ranges::any_of(m_volumeMaterialMap, sameId)) {
// this volume was already visited
return;
}
Expand Down
4 changes: 2 additions & 2 deletions Examples/Algorithms/Vertexing/src/VertexingHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "ActsExamples/Framework/AlgorithmContext.hpp"
#include "ActsExamples/Framework/DataHandle.hpp"

#include <algorithm>
#include <memory>
#include <vector>

Expand Down Expand Up @@ -55,8 +56,7 @@ inline ProtoVertexContainer makeProtoVertices(
protoVertex.reserve(vertex.tracks().size());

for (const auto& track : vertex.tracks()) {
auto it = std::find(inputTracks.begin(), inputTracks.end(),
track.originalParams);
auto it = std::ranges::find(inputTracks, track.originalParams);
if (it != inputTracks.end()) {
protoVertex.push_back(std::distance(inputTracks.begin(), it));
} else {
Expand Down
2 changes: 1 addition & 1 deletion Examples/Framework/src/Utilities/EventDataTransforms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const ActsExamples::SimSpacePoint* ActsExamples::findSpacePointForIndex(
});
};

auto found = std::find_if(spacepoints.begin(), spacepoints.end(), match);
auto found = std::ranges::find_if(spacepoints, match);

if (found == spacepoints.end()) {
return nullptr;
Expand Down
7 changes: 3 additions & 4 deletions Examples/Framework/src/Validation/TrackClassification.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ inline void increaseHitCount(
std::vector<ActsExamples::ParticleHitCount>& particleHitCounts,
ActsFatras::Barcode particleId) {
// linear search since there is no ordering
auto it = std::find_if(particleHitCounts.begin(), particleHitCounts.end(),
[=](const ActsExamples::ParticleHitCount& phc) {
return (phc.particleId == particleId);
});
auto it = std::ranges::find_if(particleHitCounts, [=](const auto& phc) {
return (phc.particleId == particleId);
});
// either increase count if we saw the particle before or add it
if (it != particleHitCounts.end()) {
it->hitCount += 1u;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "Acts/Utilities/Concepts.hpp"
#include "Acts/Utilities/Helpers.hpp"

#include <algorithm>
#include <array>
#include <cassert>
#include <fstream>
Expand Down Expand Up @@ -607,7 +608,7 @@ inline void NamedTupleDsvReader<Delimiter, NamedTuple>::parse_header(
m_extra_columns.clear();
for (std::size_t i = 0; i < m_columns.size(); ++i) {
// find the position of the column in the tuple.
auto it = std::find(names.begin(), names.end(), m_columns[i]);
auto it = std::ranges::find(names, m_columns[i]);
if (it != names.end()) {
// establish mapping between column and tuple item position
m_tuple_column_map[std::distance(names.begin(), it)] = i;
Expand Down
8 changes: 3 additions & 5 deletions Examples/Io/EDM4hep/src/EDM4hepReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,9 @@ ProcessCode EDM4hepReader::read(const AlgorithmContext& ctx) {
vtxPos /= Acts::UnitConstants::mm;

// linear search for vector
auto it = std::find_if(
primaryVertices.begin(), primaryVertices.end(),
[&vtxPos](
const std::pair<Acts::Vector3, std::vector<edm4hep::MCParticle>>&
pair) { return pair.first == vtxPos; });
auto it = std::ranges::find_if(primaryVertices, [&vtxPos](const auto& v) {
return v.first == vtxPos;
});

if (it == primaryVertices.end()) {
ACTS_DEBUG("Found primary vertex at " << vtx.x << ", " << vtx.y << ", "
Expand Down
6 changes: 3 additions & 3 deletions Examples/Io/Root/src/RootSimHitReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ std::pair<std::size_t, std::size_t> RootSimHitReader::availableEvents() const {
}

ProcessCode RootSimHitReader::read(const AlgorithmContext& context) {
auto it = std::find_if(
m_eventMap.begin(), m_eventMap.end(),
[&](const auto& a) { return std::get<0>(a) == context.eventNumber; });
auto it = std::ranges::find_if(m_eventMap, [&](const auto& a) {
return std::get<0>(a) == context.eventNumber;
});

if (it == m_eventMap.end()) {
// explicitly warn if it happens for the first or last event as that might
Expand Down
Loading
Loading