From 4e68ec4656111e728840fc3ee786be2249e763e8 Mon Sep 17 00:00:00 2001 From: David Nathan Brown Date: Wed, 28 Oct 2026 14:35:48 -0700 Subject: [PATCH 1/5] Provide backup t0 algorithm for inconsistent piecewise trajectories --- General/TimeRange.hh | 4 ++++ Trajectory/PiecewiseTrajectory.hh | 17 +++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/General/TimeRange.hh b/General/TimeRange.hh index 2967f327..77cd7ce2 100644 --- a/General/TimeRange.hh +++ b/General/TimeRange.hh @@ -40,6 +40,10 @@ namespace KinKal { bool beyond(double time, TimeDir tdir) const { return tdir == TimeDir::forwards ? time > range_[1] : time < range_[0]; } + // return the minimum time to this range (0 if in range) + double minTimeTo(double t) const { + return inRange(t) ? 0.0 : std::min(fabs(t-begin()),fabs(t-end())); + } // augment another range void combine(TimeRange const& other ) { range_[0] = std::min(begin(),other.begin()); diff --git a/Trajectory/PiecewiseTrajectory.hh b/Trajectory/PiecewiseTrajectory.hh index 5963ab5e..1bc62eaf 100644 --- a/Trajectory/PiecewiseTrajectory.hh +++ b/Trajectory/PiecewiseTrajectory.hh @@ -242,18 +242,31 @@ namespace KinKal { } template double PiecewiseTrajectory::t0() const { - // find a self-consistent t0 + // failsafe algorithm to find a maximally self-consistent t0 if(pieces_.empty())throw std::length_error("Empty PiecewiseTrajectory!"); + // first try using the traj itself to find the right piece. This works for most cases. double t0 = pieces_.front()->t0(); auto index = nearestIndex(t0); int oldindex = -1; unsigned niter(0); - while(static_cast(index) != oldindex && niter < 10){ + static const unsigned maxniter(5); // almost all trajs converge in at most 2 iterations + while(static_cast(index) != oldindex && niter < maxniter){ ++niter; t0 = pieces_[index]->t0(); oldindex = index; index = nearestIndex(t0); } + if(niter >= maxniter){ + // no self-consistent segment found. Exhaustive search for the segment with t0 closest to its range + double mindt = std::numeric_limits::max(); + for(auto piece :pieces_ ){ + double dt = piece->range().minTimeTo(piece->t0()); + if(dt < mindt){ + mindt = dt; + t0 = piece->t0(); + } + } + } return t0; } From 486d5b20988210f74406cebd858621e0c7cd2fe1 Mon Sep 17 00:00:00 2001 From: David Nathan Brown Date: Thu, 29 Oct 2026 07:32:34 -0700 Subject: [PATCH 2/5] Update comment. Fix fallback test. Improve loop efficiency --- Trajectory/PiecewiseTrajectory.hh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Trajectory/PiecewiseTrajectory.hh b/Trajectory/PiecewiseTrajectory.hh index 1bc62eaf..f45a43e8 100644 --- a/Trajectory/PiecewiseTrajectory.hh +++ b/Trajectory/PiecewiseTrajectory.hh @@ -242,7 +242,7 @@ namespace KinKal { } template double PiecewiseTrajectory::t0() const { - // failsafe algorithm to find a maximally self-consistent t0 + // find the maximally self-consistent t0. This is NOT guaranteed to be in range. if(pieces_.empty())throw std::length_error("Empty PiecewiseTrajectory!"); // first try using the traj itself to find the right piece. This works for most cases. double t0 = pieces_.front()->t0(); @@ -256,10 +256,10 @@ namespace KinKal { oldindex = index; index = nearestIndex(t0); } - if(niter >= maxniter){ - // no self-consistent segment found. Exhaustive search for the segment with t0 closest to its range + if(static_cast(index) != oldindex){ + // no self-consistent segment found. Find the segment with t0 closest to its range double mindt = std::numeric_limits::max(); - for(auto piece :pieces_ ){ + for(auto const& piece :pieces_ ){ double dt = piece->range().minTimeTo(piece->t0()); if(dt < mindt){ mindt = dt; From a6fad58be681313fdb33a105ece565a7ee1df835 Mon Sep 17 00:00:00 2001 From: David Nathan Brown Date: Fri, 30 Oct 2026 06:49:38 -0700 Subject: [PATCH 3/5] Add test for first and last piece in t0 search --- Trajectory/PiecewiseTrajectory.hh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Trajectory/PiecewiseTrajectory.hh b/Trajectory/PiecewiseTrajectory.hh index f45a43e8..9a18bc79 100644 --- a/Trajectory/PiecewiseTrajectory.hh +++ b/Trajectory/PiecewiseTrajectory.hh @@ -242,10 +242,14 @@ namespace KinKal { } template double PiecewiseTrajectory::t0() const { - // find the maximally self-consistent t0. This is NOT guaranteed to be in range. + // find the maximally self-consistent t0. This is NOT guaranteed to be in range or unique. if(pieces_.empty())throw std::length_error("Empty PiecewiseTrajectory!"); // first try using the traj itself to find the right piece. This works for most cases. double t0 = pieces_.front()->t0(); + // if the front piece t0 precedes its range the search will fail. In this case try from the back + if(t0 < pieces_.front()->range().begin()) t0 = pieces_.back()->t0(); + // Check for the same problem at the back; in that case, start with the midpoint + if(t0 > pieces_.back()->range().end())t0 = this->range().mid(); auto index = nearestIndex(t0); int oldindex = -1; unsigned niter(0); From 69375ef37ffc7c6bee2ffa54a2f39af3f0697cd1 Mon Sep 17 00:00:00 2001 From: David Nathan Brown Date: Fri, 30 Oct 2026 09:46:43 -0700 Subject: [PATCH 4/5] Add additional convergence test --- Trajectory/PiecewiseTrajectory.hh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Trajectory/PiecewiseTrajectory.hh b/Trajectory/PiecewiseTrajectory.hh index 9a18bc79..25e168d2 100644 --- a/Trajectory/PiecewiseTrajectory.hh +++ b/Trajectory/PiecewiseTrajectory.hh @@ -260,7 +260,7 @@ namespace KinKal { oldindex = index; index = nearestIndex(t0); } - if(static_cast(index) != oldindex){ + if(static_cast(index) != oldindex || !pieces_[index]->range().inRange(t0)){ // no self-consistent segment found. Find the segment with t0 closest to its range double mindt = std::numeric_limits::max(); for(auto const& piece :pieces_ ){ From e2a4265e382cf07998d50f7351e786681a545667 Mon Sep 17 00:00:00 2001 From: David Nathan Brown Date: Mon, 2 Nov 2026 11:23:59 -0800 Subject: [PATCH 5/5] Update comments. Restore iteration limit (you win Claude). Simplify convergence test --- Trajectory/PiecewiseTrajectory.hh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Trajectory/PiecewiseTrajectory.hh b/Trajectory/PiecewiseTrajectory.hh index 25e168d2..ab1e1640 100644 --- a/Trajectory/PiecewiseTrajectory.hh +++ b/Trajectory/PiecewiseTrajectory.hh @@ -244,7 +244,7 @@ namespace KinKal { template double PiecewiseTrajectory::t0() const { // find the maximally self-consistent t0. This is NOT guaranteed to be in range or unique. if(pieces_.empty())throw std::length_error("Empty PiecewiseTrajectory!"); - // first try using the traj itself to find the right piece. This works for most cases. + // first try using the traj itself to find the right piece. This works for most cases. Start with the earliest time. double t0 = pieces_.front()->t0(); // if the front piece t0 precedes its range the search will fail. In this case try from the back if(t0 < pieces_.front()->range().begin()) t0 = pieces_.back()->t0(); @@ -253,14 +253,14 @@ namespace KinKal { auto index = nearestIndex(t0); int oldindex = -1; unsigned niter(0); - static const unsigned maxniter(5); // almost all trajs converge in at most 2 iterations + static const unsigned maxniter(10); // almost all trajs converge in at most 2 iterations while(static_cast(index) != oldindex && niter < maxniter){ ++niter; t0 = pieces_[index]->t0(); oldindex = index; index = nearestIndex(t0); } - if(static_cast(index) != oldindex || !pieces_[index]->range().inRange(t0)){ + if(!pieces_[index]->range().inRange(t0)){ // no self-consistent segment found. Find the segment with t0 closest to its range double mindt = std::numeric_limits::max(); for(auto const& piece :pieces_ ){