Repository navigation
Description
Summary
When a helix crosses a plane, goes slightly past it and comes back, KinKal::intersect(helix, plane, trange, tol, tdir)
can return the earlier crossing, which lies before trange, instead of the return crossing inside trange. The
result is flagged onsurface_ = true, inbounds_ = true, inrange_ = false, so the caller sees "no crossing in this
range" and the real crossing is lost. ParticleTrajectory intersect then moves on to the next piece, so it can
report a later crossing, or none.
A second symptom has the same cause: if the range starts at the turning point itself, intersect returns no usable
intersection, although the return crossing is inside the range (LoopHelix: none; CentralHelix: a root on another
turn of the helix, ~10^12 ns away, inrange_ = false).
Found while writing Mu2e/Offline#2084 (KKExtrap sweep). That PR works around it in Mu2e code (see Workaround); the
workaround should go once KinKal is fixed.
Version: v3.9.1 (the helix-plane path of Geometry/Intersect.hh is unchanged since at least v3.7.0).
Cause
hpIntersect calls stepIntersect (Geometry/Intersect.hh):
- It steps from the start of the range in steps of
(2*sqrt(2*rbend*tol) + tol)/vTuntilisInsidechanges sign. - It backs up one step and refines with a ray (Newton) iteration:
Plane::intersect(ray, dist, false, tol), then
ttest += dist/speed, until|dist| < tol.
Step 2 is not confined to the step that showed the sign change. With forwards = false, dist can be negative, so
the iteration can leave the bracket in either direction. If that step contains an extremum of the signed plane
distance (the helix turns around beyond the plane) and its start lies before the extremum, the helix there is moving
away from the plane, the tangent ray meets the plane behind it, and the iteration runs back to the earlier root.
inrange_ is then computed correctly as false, but the in-range root is never tried.
The same holds for any other root outside the bracket: the iteration can also converge to a root after the bracket.
If the backed-up start is the turning point, the tangent ray is parallel to the plane and the first ray step is of
order 10^15 mm: for LoopHelix the iteration fails its convergence test and returns an empty intersection; for
CentralHelix it converges on a crossing of a far-away turn of the helix (t = ±1.9×10^12 ns).
Toy reproduction (KinKal only)
PlaneReturn_unit.cc (full source at the end; drop into Tests/ and add to TEST_SOURCE_FILES):
- muon (105.658 MeV), p = (100, 0, 20) MeV/c at the origin, t = 0, B = (0, 0, 1) T, so R = 333.56 mm;
Rectanglewith normal +x through the helix position 0.15 mm before its x extremum (x = R), so the helix crosses
it, goes 0.15 mm past and crosses back;- query from the outgoing leg, 0.5 half-widths before the extremum (0.11 mm beyond the plane, outside the slab), to
1.5 half-widths after it;tol = 0.1mm (the Mu2eIntersectionTolerance); - the same query started exactly at the turning point (second symptom);
- LoopHelix and CentralHelix, forwards and backwards;
- controls: the first query with
tol = 0.05mm, which must find the return.
Built against v3.9.1 (GCC 13.3, ROOT 6.38) with
g++ -O2 $(root-config --cflags) -I$KINKAL_INC PlaneReturn_unit.cc -lKinKal_Geometry -lKinKal_Trajectory -lKinKal_General $(root-config --libs).
All 8 cases miss the return crossing (exit status 1), and all 4 controls find it:
| Case (LoopHelix and CentralHelix agree unless noted) | query (ns) | intersect returns |
right answer |
|---|---|---|---|
| forwards, start 0.5 half-widths before the turn | [2.5419873899, 2.6399897668] | 2.51748679341, onsurface 1 inbounds 1 inrange 0 (the first crossing) |
2.61548917256 |
| backwards, same | [-2.6399897668, -2.5419873899] | -2.51748679341, onsurface 1 inbounds 1 inrange 0 |
-2.61548917256 |
| forwards, start at the turn | [2.56648798412, 2.6399897668] | LoopHelix: nothing (onsurface 0); CentralHelix: 1.91908793842e+12, onsurface 1 inbounds 0 inrange 0 |
2.61548917256 |
| backwards, start at the turn | [-2.6399897668, -2.56648798412] | LoopHelix: nothing; CentralHelix: -1.91908793842e+12, inrange 0 |
-2.61548917256 |
control, tol = 0.05 mm, both directions |
as the first two rows | ±2.61548947854, onsurface 1 inbounds 1 inrange 1 |
±2.61548917256 |
The failure needs the bracketing step to straddle the turn, as the controls show: with tol = 0.05 mm the steps are
smaller and the backed-up start is already past the turn. A shallower excursion or a larger bend radius brings it
back at smaller tolerances.
Reproduction in Mu2e jobs
Both symptoms were reproduced on Offline 7fcbd6a7b (Mu2e/Offline#2109, which contains #2084) with the stock
spack KinKal v3.9.1 (envset p110), using a temporary diagnostic patch in ExtrapolateSweep::firstCrossing:
KKEXTRAP_PLANE_XCHECK=1: for every plane query, also callKinKal::intersect(piece, *surf, query, intertol, tdir)
on the same piece and time range, and print aPlaneXcheckline when the two disagree (KinKal returns an
out-of-range root, KinKal returns nothing, the bracketed search returns nothing, or the roots differ by more than
the tolerance), with per-trajectory-type totals (PlaneXcheckSummary) at exit;KKEXTRAP_PLANE_KINKAL=1: use KinKal's result for planes instead (behaviour before the workaround).
1. Offline unit test (ExtrapolateSweepTest). shallowRecross builds the toy geometry with Mu2e targets (single
and split pieces, single and windowed queries, both directions). The test passes, and the cross-check prints:
| Trajectory | plane queries | KinKal returns the earlier, out-of-range root | KinKal returns nothing |
|---|---|---|---|
| LoopHelix | 268 | 8 | 30 |
| CentralHelix | 194 | 8 | 30 |
| KinematicLine | 380 | 0 | 0 |
- The 8 out-of-range cases per helix type are the toy query ([2.54199, 2.63999] ns forwards, [-2.63999, -2.54199] ns
backwards): the bracketed search finds the return at ±2.6155 ns and KinKal the first crossing at ±2.5175 ns. - 16 of the 30 "nothing" cases are the second symptom: queries starting exactly at the turning point (±2.56649 ns),
where the split pieces and windows begin; the return at ±2.6155 ns is inside the query. - The other 14 (
TT_Front,planeContacts) are tangent touches within the tolerance that never cross the plane. Mu2e
records those as contacts on purpose; KinKal not reporting them is expected, not part of this issue.
On 2026-10-28 an earlier version of the sweep that used KinKal::intersect for planes failed this test with
"shallow resolved depart-return was suppressed"; under GDB, KinKal returned t = 2.5174867955768883 with
onsurface=true, inbounds=true, inrange=false for the query [2.5419873899, 2.6399897668]. With
KKEXTRAP_PLANE_KINKAL=1 the current test stops earlier, at the tangent-contact check ("plane contact/incidence
policy differs"), so the cross-check above is the clean reproduction.
2. Reconstruction. The same patch with KKEXTRAP_PLANE_XCHECK=1, every plane query of the KKExtrap sweep
(2026-11-06, Offline 7fcbd6a7b, KinKal v3.9.1):
| Sample | Events | Fit | Plane queries | Disagreements (events) | earlier out-of-range root | nothing | different root |
|---|---|---|---|---|---|---|---|
nightly reco_03 (CeMLeadingLogMix1BB, MDC2025au) |
16,508 | LoopHelix | 33,109,583 | 326 (318) | 2 | 256 | 68 |
| nightly off-spill cosmics (MDC2025an), CRV extrapolation | 10,000 | LoopHelix | 8,783,942 | 99 (87) | 5 | 80 | 14 |
| CentralHelix | 79,607 | 0 | |||||
| same events, production off-spill setting | 10,000 | CentralHelix | 55,032 | 0 |
(The production setting's LoopHelix fits are the same events and give the same 99 cases.) KinKal never found a root
that the bracketed search missed.
- Stopping-target foils, 423 of the 425 cases. Low-momentum
LoopHelixfits looping near the foils at grazing
incidence (|cos| < 0.02 at the query start in 399 of them): the foil plane is a z plane, and the piece's field tilt
gives the plane distance an extremum. 421 of them are roots on the foil plane outside the foil annulus, which the
sweep discards anyway. Two are on a foil (1430:1691:683 foil 4, KinKal returns nothing; 1430:1691:3726 foil 20,
searching backwards in time, KinKal returns the second root in the search direction, 729.131 ns, instead of the first, 729.235 ns), both at
|cos| < 0.003, below Mu2e's 0.02 foil incidence floor. So on these samples no foil material changes. - TSDA, 2 cases, both real misses. In 1430:6261:44492 (p 35.5 MeV/c, |cos| 0.53 at the query start) and
1430:7067:59398 (p 32.5 MeV/c, |cos| 0.03), the bracketed search finds an in-bounds TSDA crossing 0.0-0.1 ns into a
0.2 ns query and KinKal returns no intersection. The first is not a grazing or turning-point case; its mechanism is
not yet understood (needs the piece parameters, which the cross-check does not print). - CRV, strongbacks, concrete (
CentralHelixcosmics): no disagreement in 134,639 queries.
Each PlaneXcheck line follows the event's "Begin processing" line; reprocess one with --nskip N -n 1 to debug it.
KinKal's Intersection print omits the time for out-of-bounds roots, so for the 81 off-foil "different root" cases
it is not known which root is first.
Workaround in Mu2e/Offline#2084 (temporary)
For planes only, ExtrapolateSweep does not call KinKal::intersect:
visitPlaneExtrema: for helix and line pieces the normal velocity is constant + a·cos(ωt) + b·sin(ωt), so the
extrema of the signed plane distance are known in closed form (three velocity samples give the coefficients);planeCrossing: between consecutive extrema the distance is monotonic, so each root is bracketed and found by
bisection, in sweep order, and can never fall outside the query.
Cylinders and other surfaces still use KinKal::intersect; no similar failure has been seen there, but
stepIntersect has the same unbracketed iteration for every surface. These functions will be marked as temporary
and removed once KinKal is fixed.
Suggested fix
In stepIntersect, keep the sign-change bracket [ttest - tstep, ttest] and refine inside it: accept a Newton
step only if it stays inside the bracket and fall back to bisection otherwise (a safeguarded Newton), shrinking the
bracket by the sign of surf.distance (or isInside) at each new time. That fixes every surface type, not just
planes. For planes, the closed-form extrema above could also give exact monotonic brackets in hpIntersect.
A KinKal unit test with the toy geometry (both helix types, both time directions, query starting outside and inside
the slab) would cover it.
Related
The trajectory-truncation follow-up from the same review (letting Track shorten the fitted trajectory at a
range-out, so KinKal does not extend the end piece a domain past the stop) will be a separate PR.
Tests/PlaneReturn_unit.cc
// Toy reproduction for the KinKal issue "helix-plane intersection returns an out-of-range root and misses
// the in-range one". KinKal only, no Mu2e code. Drop into KinKal/Tests and add it to TEST_SOURCE_FILES.
//
// A helix crosses a plane, goes 0.15 mm past it, turns and crosses back. A query that starts on the
// outgoing leg (outside the tolerance slab) and ends after the return must find the return root.
// stepIntersect's first step straddles the turning point, so it backs up to the query start, where the
// helix moves away from the plane; the unbracketed ray (Newton) iteration then runs backwards and
// converges to the earlier root, before the query. The result is onsurface, inbounds, !inrange, and the
// in-range root is never reported. A query that starts at the turning point returns no intersection at all.
#include "KinKal/General/ParticleState.hh"
#include "KinKal/General/TimeRange.hh"
#include "KinKal/General/TimeDir.hh"
#include "KinKal/Trajectory/LoopHelix.hh"
#include "KinKal/Trajectory/CentralHelix.hh"
#include "KinKal/Geometry/Rectangle.hh"
#include "KinKal/Geometry/Intersection.hh"
#include "KinKal/Geometry/Intersect.hh"
#include <cmath>
#include <iostream>
#include <iomanip>
#include <string>
using namespace KinKal;
template <class HELIX> int planeReturn(TimeDir tdir, double excursion, double tol, double startfrac, std::string const& name) {
double sign = tdir == TimeDir::forwards ? 1.0 : -1.0;
// 100 MeV/c transverse, 20 MeV/c along a 1 T field, muon mass; R = 333.6 mm
ParticleState state(VEC3(0.0,0.0,0.0),VEC3(100.0,0.0,20.0),0.0,105.658,1);
HELIX helix(state,VEC3(0.0,0.0,1.0),TimeRange(-100.0,100.0));
double omega = helix.transverseSpeed()/helix.bendRadius(); // |dphi/dt|
double tmax = sign*0.5*M_PI/omega; // x is extremal here (|x| = R)
double radius = std::fabs(helix.position3(tmax).X());
double halfwidth = std::acos(1.0-excursion/radius)/omega; // time from a root to the extremum
double first = tmax - sign*halfwidth; // the crossing into the excursion
double ret = tmax + sign*halfwidth; // the return crossing (expected answer)
Rectangle plane(VEC3(1.0,0.0,0.0),VEC3(0.0,1.0,0.0),helix.position3(first),2000.0,2000.0);
// query: from the outgoing leg (startfrac half-widths before the turning point; 0 = at it), past the return
double qstart = tmax - sign*startfrac*halfwidth;
double qend = tmax + sign*1.5*halfwidth;
TimeRange query = tdir == TimeDir::forwards ? TimeRange(qstart,qend) : TimeRange(qend,qstart);
auto inter = KinKal::intersect(helix,plane,query,tol,tdir);
bool ok = inter.good() && std::fabs(inter.time_-ret)*helix.speed(ret) < tol;
std::cout << std::setprecision(12) << name << (tdir == TimeDir::forwards ? " forwards" : " backwards")
<< " start " << startfrac << " half-widths before the turn, excursion " << excursion << " mm tol " << tol << " mm\n"
<< " query [" << query.begin() << ", " << query.end() << "] ns, start "
<< plane.distance(helix.position3(qstart)) << " mm from the plane\n"
<< " roots: first " << first << " (outside query), return " << ret << " (inside query)\n"
<< " intersect: time " << inter.time_ << " onsurface " << inter.onsurface_ << " inbounds "
<< inter.inbounds_ << " inrange " << inter.inrange_ << " -> " << (ok ? "OK" : "FAIL: return root missed") << "\n";
return ok ? 0 : 1;
}
int main() {
int nfail(0), ncontrol(0);
for(auto tdir : {TimeDir::forwards, TimeDir::backwards}){
for(double startfrac : {0.5, 0.0}){ // 0.5: earlier out-of-range root returned; 0.0: no usable root returned
nfail += planeReturn<LoopHelix>(tdir,0.15,0.1,startfrac,"LoopHelix");
nfail += planeReturn<CentralHelix>(tdir,0.15,0.1,startfrac,"CentralHelix");
}
// controls, same geometry and query: with tol 0.05 mm the steps are smaller, the bracketing step starts past the
// turn, and the return root is found
ncontrol += planeReturn<LoopHelix>(tdir,0.15,0.05,0.5,"control LoopHelix");
ncontrol += planeReturn<CentralHelix>(tdir,0.15,0.05,0.5,"control CentralHelix");
}
std::cout << nfail << " of 8 cases miss the in-range root; " << ncontrol << " of 4 controls fail" << std::endl;
return nfail == 0 ? 0 : 1;
}
Activity