Repository navigation
Binary search in PiecewiseTrajectory::nearestIndex - #227
Merged
Merged
Conversation
brownd1978
approved these changes
Oct 17, 2026
brownd1978
left a comment
Collaborator
There was a problem hiding this comment.
Thanks Rob for this useful improvement!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
PiecewiseTrajectory::nearestIndexfound the piece holding a time by scanning the piece deque from the front. It iscalled for every closest-approach update of a hit or material crossing, every
append,nearestPieceandDomainWall::updateReference, so in Mu2e reconstruction the scan is one of the two most expensive functions wherevera KinKal fit runs (callgrind, Offline main + KinKal 3.8.0):
nearestIndexshare of all event-processing instructionsIts cost grows with the number of pieces, i.e. with the number of B-field domains, so long tracks pay most.
The pieces are time-ordered and contiguous (
append/prepend/addkeepbegin(i+1) == end(i)), so this PR replacesthe scan with
std::lower_boundover the piece end times: O(log N) instead of O(N). The clamps for times before thefirst and after the last piece are unchanged.
Why the result is identical
Inside the range, the loop stopped at the first piece
iwithinRange(t) || t <= end(i). SinceinRange(t)impliest < end(i), that is the first piece withend(i) >= t, which is whatlower_boundwith the predicateend(i) < treturns, given non-decreasing end times. A time on a boundary,t == end(i) == begin(i+1), returnsiin both; zero-length pieces and NaN also give the same index.
pieces_is astd::deque, so the iterators are randomaccess.
Tests
10,000 pieces (every piece's begin, end, end minus one ulp and midpoint, random times in and around the range, NaN,
zero-length interior pieces): 9,346,772 queries, 0 mismatches. Per call: 0.9x at 10 pieces, 2.5x faster at 100,
17x at 1,000, 108x at 10,000.
TrigReports on
Modules that do not use KinKal are unchanged within run-to-run noise. The excluded off-spill event is a pathological
seed with ~157k B-field domains, handled separately in Mu2e/Offline#2044; this change also makes it about 50 times
cheaper, but the numbers above do not depend on it.
Not changed
pieceRange(both overloads) andsetRange(trim=true)still walk the deque linearly; they do not show up in theprofiles. They could reuse the same search in a follow-up.