Skip to content

Commit

Permalink
refactor: seeding always sorts SPs (#1910)
Browse files Browse the repository at this point in the history
Sorting has been shown to be less expensive then looping over all elements to select bottom and top sps for seeds. By sorting the SPs, we can avoid a considerable number of iterations, especially for high pile-up.
This PR removes the option to not sort the SPs during the seeding algorithm. @noemina
  • Loading branch information
LuisFelipeCoelho committed Mar 9, 2023
1 parent 5cf1328 commit c942f23
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 78 deletions.
53 changes: 28 additions & 25 deletions Core/include/Acts/Seeding/BinnedSPGroup.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <boost/container/flat_set.hpp>

template <typename external_spacepoint_t>
template <typename spacepoint_iterator_t, typename callable_t>
Acts::BinnedSPGroup<external_spacepoint_t>::BinnedSPGroup(
Expand Down Expand Up @@ -43,9 +46,10 @@ Acts::BinnedSPGroup<external_spacepoint_t>::BinnedSPGroup(
// binSizeR allows to increase or reduce numRBins if needed
size_t numRBins = static_cast<size_t>((config.rMax + options.beamPos.norm()) /
config.binSizeR);
std::vector<
std::vector<std::unique_ptr<InternalSpacePoint<external_spacepoint_t>>>>
rBins(numRBins);

// keep track of changed bins while sorting
boost::container::flat_set<size_t> rBinsIndex;

for (spacepoint_iterator_t it = spBegin; it != spEnd; it++) {
if (*it == nullptr) {
continue;
Expand Down Expand Up @@ -78,33 +82,32 @@ Acts::BinnedSPGroup<external_spacepoint_t>::BinnedSPGroup(
if (rIndex >= numRBins) {
continue;
}
rBins[rIndex].push_back(std::move(isp));
}

// if requested, it is possible to force sorting in R for each (z, phi) grid
// bin
if (config.forceRadialSorting) {
for (auto& rbin : rBins) {
std::sort(
rbin.begin(), rbin.end(),
[](std::unique_ptr<InternalSpacePoint<external_spacepoint_t>>& a,
std::unique_ptr<InternalSpacePoint<external_spacepoint_t>>& b) {
return a->radius() < b->radius();
});
// fill rbins into grid
Acts::Vector2 spLocation(isp->phi(), isp->z());
std::vector<std::unique_ptr<InternalSpacePoint<external_spacepoint_t>>>&
rbin = grid->atPosition(spLocation);
rbin.push_back(std::move(isp));

// keep track of the bins we modify so that we can later sort the SPs in
// those bins only
if (rbin.size() > 1) {
rBinsIndex.insert(grid->globalBinFromPosition(spLocation));
}
}

// fill rbins into grid such that each grid bin is sorted in r
// space points with delta r < rbin size can be out of order is sorting is not
// requested
for (auto& rbin : rBins) {
for (auto& isp : rbin) {
Acts::Vector2 spLocation(isp->phi(), isp->z());
std::vector<std::unique_ptr<InternalSpacePoint<external_spacepoint_t>>>&
bin = grid->atPosition(spLocation);
bin.push_back(std::move(isp));
}
// sort SPs in R for each filled (z, phi) bin
for (auto& binIndex : rBinsIndex) {
std::vector<std::unique_ptr<InternalSpacePoint<external_spacepoint_t>>>&
rbin = grid->atPosition(binIndex);
std::sort(
rbin.begin(), rbin.end(),
[](std::unique_ptr<InternalSpacePoint<external_spacepoint_t>>& a,
std::unique_ptr<InternalSpacePoint<external_spacepoint_t>>& b) {
return a->radius() < b->radius();
});
}

m_binnedSP = std::move(grid);
m_bottomBinFinder = botBinFinder;
m_topBinFinder = tBinFinder;
Expand Down
15 changes: 5 additions & 10 deletions Core/include/Acts/Seeding/SeedFilter.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void SeedFilter<external_spacepoint_t>::filterSeeds_2SpFixed(
topSPIndexVec[i] = i;
}

if (m_cfg.curvatureSortingInFilter and topSpVec.size() > 2) {
if (topSpVec.size() > 2) {
// sort indexes based on comparing values in invHelixDiameterVec
std::sort(topSPIndexVec.begin(), topSPIndexVec.end(),
[&invHelixDiameterVec](const size_t& i1, const size_t& i2) {
Expand Down Expand Up @@ -103,18 +103,13 @@ void SeedFilter<external_spacepoint_t>::filterSeeds_2SpFixed(

// curvature difference within limits?
if (invHelixDiameterVec[compatibleTopSPIndex] < lowerLimitCurv) {
// if SPs are sorted in curvature we skip unnecessary iterations
if (m_cfg.curvatureSortingInFilter) {
beginCompTopIndex = variableCompTopIndex + 1;
}
// the SPs are sorted in curvature so we skip unnecessary iterations
beginCompTopIndex = variableCompTopIndex + 1;
continue;
}
if (invHelixDiameterVec[compatibleTopSPIndex] > upperLimitCurv) {
// if SPs are sorted in curvature we skip unnecessary iterations
if (m_cfg.curvatureSortingInFilter) {
break;
}
continue;
// the SPs are sorted in curvature so we skip unnecessary iterations
break;
}
// compared top SP should have at least deltaRMin distance
float deltaR = currentTopR - otherTopR;
Expand Down
3 changes: 0 additions & 3 deletions Core/include/Acts/Seeding/SeedFilterConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ struct SeedFilterConfig {
size_t compatSeedLimit = 2;
// Tool to apply experiment specific cuts on collected middle space points

// sort vectors vectors by curvature
bool curvatureSortingInFilter = false;

// increment in seed weight if the number of compatible seeds is larger than
// numSeedIncrement, this is used in case of high occupancy scenarios if we
// want to increase the weight of the seed by seedWeightIncrement when the
Expand Down
24 changes: 8 additions & 16 deletions Core/include/Acts/Seeding/SeedFinder.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,8 @@ void SeedFinder<external_spacepoint_t, platform_t>::createSeedsForGroup(
continue;
}
if (rM > rMiddleSPRange.max()) {
// break if SP are sorted in r
if (m_config.forceRadialSorting) {
break;
}
continue;
// break because SPs are sorted in r
break;
}
} else if (not m_config.rRangeMiddleSP.empty()) {
/// get zBin position of the middle SP
Expand All @@ -82,22 +79,17 @@ void SeedFinder<external_spacepoint_t, platform_t>::createSeedsForGroup(
continue;
}
if (rM > m_config.rRangeMiddleSP[zBin][1]) {
// break if SP are sorted in r
if (m_config.forceRadialSorting) {
break;
}
continue;
// break because SPs are sorted in r
break;
}
} else {
if (rM > m_config.rMaxMiddle) {
continue;
}
if (rM < m_config.rMinMiddle) {
if (m_config.forceRadialSorting) {
break;
}
continue;
}
if (rM > m_config.rMaxMiddle) {
// break because SPs are sorted in r
break;
}
}

getCompatibleDoublets(options, topSPs, *spM, state.compatTopSP,
Expand Down
3 changes: 0 additions & 3 deletions Core/include/Acts/Seeding/SeedFinderConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ struct SeedFinderConfig {
// radial bin size for filling space point grid
float binSizeR = 1. * Acts::UnitConstants::mm;

// force sorting of middle SPs in radius
bool forceRadialSorting = false;

// radial range for middle SP
// variable range based on SP radius
bool useVariableMiddleSPRange = false;
Expand Down
4 changes: 0 additions & 4 deletions Examples/Python/python/acts/examples/itk.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,6 @@ def itkSeedingAlgConfig(inputSpacePointsType):
] # if useVariableMiddleSPRange is set to false, the vector rRangeMiddleSP can be used to define a fixed r range for each z bin: {{rMin, rMax}, ...}. If useVariableMiddleSPRange is set to false and the vector is empty, the cuts won't be applied
useVariableMiddleSPRange = True # if useVariableMiddleSPRange is true, the values in rRangeMiddleSP will be calculated based on r values of the SPs and deltaRMiddleSPRange
binSizeR = 1 * u.mm
forceRadialSorting = True
seedConfirmation = True
centralSeedConfirmationRange = acts.SeedConfirmationRangeConfig(
zMinSeedConf=-250 * u.mm,
Expand All @@ -357,7 +356,6 @@ def itkSeedingAlgConfig(inputSpacePointsType):
)
zOriginWeightFactor = 1
compatSeedWeight = 100
curvatureSortingInFilter = True
phiMin = 0
phiMax = 2 * math.pi
phiBinDeflectionCoverage = 3
Expand Down Expand Up @@ -500,7 +498,6 @@ def itkSeedingAlgConfig(inputSpacePointsType):
rRangeMiddleSP=rRangeMiddleSP,
useVariableMiddleSPRange=useVariableMiddleSPRange,
binSizeR=binSizeR,
forceRadialSorting=forceRadialSorting,
seedConfirmation=seedConfirmation,
centralSeedConfirmationRange=centralSeedConfirmationRange,
forwardSeedConfirmationRange=forwardSeedConfirmationRange,
Expand All @@ -523,7 +520,6 @@ def itkSeedingAlgConfig(inputSpacePointsType):
numSeedIncrement=numSeedIncrement,
seedWeightIncrement=seedWeightIncrement,
seedConfirmation=seedConfirmation,
curvatureSortingInFilter=curvatureSortingInFilter,
maxSeedsPerSpMConf=maxSeedsPerSpMConf,
maxQualitySeedsPerSpMConf=maxQualitySeedsPerSpMConf,
useDeltaRorTopRadius=useDeltaRorTopRadius,
Expand Down
13 changes: 4 additions & 9 deletions Examples/Python/python/acts/examples/reconstruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
"rRangeMiddleSP",
"useVariableMiddleSPRange",
"binSizeR",
"forceRadialSorting",
"seedConfirmation",
"centralSeedConfirmationRange",
"forwardSeedConfirmationRange",
Expand All @@ -57,7 +56,7 @@
"r", # (min,max)
"z", # (min,max)
],
defaults=[None] * 21 + [(None, None)] * 7,
defaults=[None] * 20 + [(None, None)] * 7,
)
SeedFinderOptionsArg = namedtuple(
"SeedFinderOptions", ["beamPos", "bFieldInZ"], defaults=[(None, None), None]
Expand All @@ -73,13 +72,12 @@
"numSeedIncrement",
"seedWeightIncrement",
"seedConfirmation",
"curvatureSortingInFilter",
"maxSeedsPerSpMConf",
"maxQualitySeedsPerSpMConf",
"useDeltaRorTopRadius",
"deltaRMin",
],
defaults=[None] * 12,
defaults=[None] * 11,
)

SpacePointGridConfigArg = namedtuple(
Expand Down Expand Up @@ -206,12 +204,12 @@ def addSeeding(
initialVarInflation : list
List of 6 scale factors to inflate the initial covariance matrix
Defaults (all 1) specified in Examples/Algorithms/TruthTracking/ActsExamples/TruthTracking/ParticleSmearing.hpp
seedFinderConfigArg : SeedFinderConfigArg(maxSeedsPerSpM, cotThetaMax, sigmaScattering, radLengthPerSeed, minPt, impactMax, deltaPhiMax, interactionPointCut, arithmeticAverageCotTheta, deltaZMax, maxPtScattering, zBinEdges, skipPreviousTopSP, zBinsCustomLooping, rRangeMiddleSP, useVariableMiddleSPRange, binSizeR, forceRadialSorting, seedConfirmation, centralSeedConfirmationRange, forwardSeedConfirmationRange, deltaR, deltaRBottomSP, deltaRTopSP, deltaRMiddleSPRange, collisionRegion, r, z)
seedFinderConfigArg : SeedFinderConfigArg(maxSeedsPerSpM, cotThetaMax, sigmaScattering, radLengthPerSeed, minPt, impactMax, deltaPhiMax, interactionPointCut, arithmeticAverageCotTheta, deltaZMax, maxPtScattering, zBinEdges, skipPreviousTopSP, zBinsCustomLooping, rRangeMiddleSP, useVariableMiddleSPRange, binSizeR, seedConfirmation, centralSeedConfirmationRange, forwardSeedConfirmationRange, deltaR, deltaRBottomSP, deltaRTopSP, deltaRMiddleSPRange, collisionRegion, r, z)
SeedFinderConfig settings. deltaR, deltaRBottomSP, deltaRTopSP, deltaRMiddleSPRange, collisionRegion, r, z are ranges specified as a tuple of (min,max). beamPos is specified as (x,y).
Defaults specified in Core/include/Acts/Seeding/SeedFinderConfig.hpp
seedFinderOptionsArg : SeedFinderOptionsArg(bFieldInZ, beamPos)
Defaults specified in Core/include/Acts/Seeding/SeedFinderConfig.hpp
seedFilterConfigArg : SeedFilterConfigArg(compatSeedWeight, compatSeedLimit, numSeedIncrement, seedWeightIncrement, seedConfirmation, curvatureSortingInFilter, maxSeedsPerSpMConf, maxQualitySeedsPerSpMConf, useDeltaRorTopRadius)
seedFilterConfigArg : SeedFilterConfigArg(compatSeedWeight, compatSeedLimit, numSeedIncrement, seedWeightIncrement, seedConfirmation, maxSeedsPerSpMConf, maxQualitySeedsPerSpMConf, useDeltaRorTopRadius)
Defaults specified in Core/include/Acts/Seeding/SeedFilterConfig.hpp
spacePointGridConfigArg : SpacePointGridConfigArg(rMax, zBinEdges, phiBinDeflectionCoverage, phi, impactMax)
SpacePointGridConfigArg settings. phi is specified as a tuple of (min,max).
Expand Down Expand Up @@ -532,7 +530,6 @@ def addStandardSeeding(
rRangeMiddleSP=seedFinderConfigArg.rRangeMiddleSP,
useVariableMiddleSPRange=seedFinderConfigArg.useVariableMiddleSPRange,
binSizeR=seedFinderConfigArg.binSizeR,
forceRadialSorting=seedFinderConfigArg.forceRadialSorting,
seedConfirmation=seedFinderConfigArg.seedConfirmation,
centralSeedConfirmationRange=seedFinderConfigArg.centralSeedConfirmationRange,
forwardSeedConfirmationRange=seedFinderConfigArg.forwardSeedConfirmationRange,
Expand Down Expand Up @@ -565,7 +562,6 @@ def addStandardSeeding(
seedConfirmation=seedFilterConfigArg.seedConfirmation,
centralSeedConfirmationRange=seedFinderConfig.centralSeedConfirmationRange,
forwardSeedConfirmationRange=seedFinderConfig.forwardSeedConfirmationRange,
curvatureSortingInFilter=seedFilterConfigArg.curvatureSortingInFilter,
maxSeedsPerSpMConf=seedFilterConfigArg.maxSeedsPerSpMConf,
maxQualitySeedsPerSpMConf=seedFilterConfigArg.maxQualitySeedsPerSpMConf,
useDeltaRorTopRadius=seedFilterConfigArg.useDeltaRorTopRadius,
Expand Down Expand Up @@ -705,7 +701,6 @@ def addOrthogonalSeeding(
numSeedIncrement=seedFilterConfigArg.numSeedIncrement,
seedWeightIncrement=seedFilterConfigArg.seedWeightIncrement,
seedConfirmation=seedFilterConfigArg.seedConfirmation,
curvatureSortingInFilter=seedFilterConfigArg.curvatureSortingInFilter,
maxSeedsPerSpMConf=seedFilterConfigArg.maxSeedsPerSpMConf,
maxQualitySeedsPerSpMConf=seedFilterConfigArg.maxQualitySeedsPerSpMConf,
useDeltaRorTopRadius=seedFilterConfigArg.useDeltaRorTopRadius,
Expand Down
2 changes: 0 additions & 2 deletions Examples/Python/src/TrackFinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ void addTrackFinding(Context& ctx) {
ACTS_PYTHON_MEMBER(deltaRMin);
ACTS_PYTHON_MEMBER(maxSeedsPerSpM);
ACTS_PYTHON_MEMBER(compatSeedLimit);
ACTS_PYTHON_MEMBER(curvatureSortingInFilter);
ACTS_PYTHON_MEMBER(seedConfirmation);
ACTS_PYTHON_MEMBER(centralSeedConfirmationRange);
ACTS_PYTHON_MEMBER(forwardSeedConfirmationRange);
Expand Down Expand Up @@ -111,7 +110,6 @@ void addTrackFinding(Context& ctx) {
ACTS_PYTHON_MEMBER(rMinMiddle);
ACTS_PYTHON_MEMBER(rMaxMiddle);
ACTS_PYTHON_MEMBER(binSizeR);
ACTS_PYTHON_MEMBER(forceRadialSorting);
ACTS_PYTHON_MEMBER(seedConfirmation);
ACTS_PYTHON_MEMBER(centralSeedConfirmationRange);
ACTS_PYTHON_MEMBER(forwardSeedConfirmationRange);
Expand Down
12 changes: 6 additions & 6 deletions Examples/Python/tests/root_file_hashes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ test_volume_material_mapping__propagation-volume-material.root: 9d57609d7e0ea818
test_digitization_example__measurements.root: 96c75125b172d206f3e8c0458ace8f7e7687a5ed651dc00200d08682b5275e9d
test_digitization_example_input__particles.root: 8549ba6e20338004ab8ba299fc65e1ee5071985b46df8f77f887cb6fef56a8ec
test_digitization_example_input__measurements.root: 6c156d872cd1b160e6a573752dab164081469da49b7a1fb4c595a043da69c19b
test_ckf_tracks_example[generic-full_seeding]__trackstates_ckf.root: c81618ec91b822cdb4d24066dcd42b0043d3944e22ea22041fca01ea346b674e
test_ckf_tracks_example[generic-full_seeding]__tracksummary_ckf.root: 9f2f00520fb76f29d4d7fada4c8aa2233e3a055adf57c7d24a952cd650c8bd31
test_ckf_tracks_example[generic-full_seeding]__performance_seeding.root: 0e0676ffafdb27112fbda50d1cf627859fa745760f98073261dcf6db3f2f991e
test_ckf_tracks_example[generic-full_seeding]__trackstates_ckf.root: 69b0fb7826de54242cbf6d4e79ddfec328d33d27276d58ef42c562dc8f0e7cfd
test_ckf_tracks_example[generic-full_seeding]__tracksummary_ckf.root: df5f5e7a2afbe65f9f907d7c8842369e6224808410ae08fed830b8c9024f58f4
test_ckf_tracks_example[generic-full_seeding]__performance_seeding_trees.root: 0e0676ffafdb27112fbda50d1cf627859fa745760f98073261dcf6db3f2f991e
test_ckf_tracks_example[generic-truth_estimated]__trackstates_ckf.root: b0529ffddb5dd06bcc29bc34cfd6849000ec2efbfd756dbd8623840041160f1f
test_ckf_tracks_example[generic-truth_estimated]__tracksummary_ckf.root: a0163cfba125c1529507cea0de43574a1533073dcc1311e39e0f78fff845b63b
test_ckf_tracks_example[generic-truth_estimated]__performance_seeding.root: 1facb05c066221f6361b61f015cdf0918e94d9f3fce2269ec7b6a4dffeb2bc7e
test_ckf_tracks_example[generic-truth_smeared]__trackstates_ckf.root: 1638aa206b0beda87885d2d9a4059f7b50811dd358c8a2ed2e1705726cfe8b86
test_ckf_tracks_example[generic-truth_smeared]__tracksummary_ckf.root: f764f23555517fa4a864fb009be1518c1c92084b3132bd21e7446d76487f62a3
test_ckf_tracks_example[odd-full_seeding]__trackstates_ckf.root: f58153e013d799fe142a8b62e3be2ef35f80df28a6647fe37e268e8fcf741196
test_ckf_tracks_example[odd-full_seeding]__tracksummary_ckf.root: 22084a82fa5c275978375e9783fc2577bcd541350238555a9a8788760ca5e011
test_ckf_tracks_example[odd-full_seeding]__performance_seeding.root: 43c58577aafe07645e5660c4f43904efadf91d8cda45c5c04c248bbe0f59814f
test_ckf_tracks_example[odd-full_seeding]__trackstates_ckf.root: 3d03e58087a14cd7f59f09accaf6a43f18d89a57fbbe3f0efdc9cbda761fed72
test_ckf_tracks_example[odd-full_seeding]__tracksummary_ckf.root: f63da5c9080dac574787e921d3616242697858992c7dab4700959b050c4e73a7
test_ckf_tracks_example[odd-full_seeding]__performance_seeding_trees.root: 43c58577aafe07645e5660c4f43904efadf91d8cda45c5c04c248bbe0f59814f
test_ckf_tracks_example[odd-truth_estimated]__trackstates_ckf.root: d9c7c85d4f523953b2c648357ae6778062763d92ab1798a060dd44a387ace8a6
test_ckf_tracks_example[odd-truth_estimated]__tracksummary_ckf.root: 37ff73e7c04ecc00d09bf21a75187981cdee85ceacba7ef1677c757051b9d807
test_ckf_tracks_example[odd-truth_estimated]__performance_seeding.root: 1a36b7017e59f1c08602ef3c2cb0483c51df248f112e3780c66594110719c575
Expand Down

0 comments on commit c942f23

Please sign in to comment.