Repository navigation
Fix precision of eta at negative z in GenVector - #24233
Merged
guitargeek merged 2 commits intoNov 19, 2026
Merged
Conversation
Eta_FromRhoZ computes log(zs + sqrt(zs^2 + 1)) with zs = z/rho. For zs < 0 the sum subtracts two nearly equal numbers. The error of eta is then about zs^2 times the rounding error of the scalar type. For large negative zs the function also drops the 0.5/zs term that it keeps for positive zs. For a float vector with rho = 50 and |z| = 340, eta(+z) = 2.61543298 and eta(-z) = -2.61543393. On 3e6 points with rho in [1, 300], |z| in (0, 600] and |z|/rho >= 1, the largest error against asinhl is 1.3 ULP (float) and 2.6 ULP (double) at z > 0, and 256 ULP (float) and 9.0e4 ULP (double) at z < 0. A cut on eta is then less stable against rounding in one half of a detector than in the other half. Compute the formula for |zs| and apply the sign of zs to the result. Eta is then an odd function of z, bit for bit. The results at z > 0 do not change. The error at z < 0 is equal to the error at z > 0.
Eta_FromRhoZ of GenVectorX is a copy of the function of GenVector and has the same loss of precision for z < 0. Compute the formula for |zs| and apply the sign of zs to the result, as in GenVector.
felicepantaleo
requested review from
bellenot,
guitargeek and
hageboeck
as code owners
November 18, 2026 16:02
Test Results 22 files 22 suites 3d 11h 25m 22s ⏱️ Results for commit 585a8d2. ♻️ This comment has been updated with latest results. |
13 tasks done
guitargeek
approved these changes
Nov 19, 2026
guitargeek
left a comment
Contributor
There was a problem hiding this comment.
LGTM! Thanks a lot for the contribution
Contributor
|
/backport to 6.42 |
|
Preparing to backport PR #24233 to branch 6.42 requested by guitargeek |
|
This PR has been backported to branch 6.42: #24262 |
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.
This Pull request:
Makes
Eta()of the GenVector coordinate systems that compute eta from rho and z precise for negative z, and an odd function of z bit for bit.Changes or fixes:
The problem
ROOT::Math::Impl::Eta_FromRhoZ(rho, z)computeslog(zs + sqrt(zs * zs + 1))withzs = z / rho. Forzs < 0the sum subtracts two nearly equal numbers. The error of eta is then aboutzs * zstimes the rounding error of the scalar type. For large negativezs(|zs|aboveepsilon^-0.25, which is 54 for float and 8192 for double) the function returns-log(-2 * zs): it drops the0.5 / zsterm that it keeps for positivezs.Cartesian3D,Cylindrical3D,PxPyPzE4DandPxPyPzM4Dcall this function. The problem is thus inEta()ofXYZVector,XYZPoint,RhoZPhiVector,PxPyPzEVector,PxPyPzMVectorand their float variants.Example:
ROOT::Math::XYZVectorFwith x = 30, y = 40 (rho = 50) and |z| = 340. The exact |eta| is 2.61543304, and the nearest float is 2.61543298.Eta()at z = +340Eta()at z = -340Context
The problem shows in the reconstruction of the CMS HGCAL endcap calorimeters, which are at z > 0 and at z < 0. CMS runs the same reconstruction in two implementations: one written for GPUs (run on the CPU here) and one written for the CPU. On the same input, the cluster positions of the two implementations differ only by float rounding: 1.7e-7 relative (99% quantile), the same at z > 0 and at z < 0. A later step reads the cluster position as a float
XYZVectorand selects track-cluster pairs with windows in eta. The eta difference between the two implementations is not the same on the two sides: its 99% quantile is 2.4e-7 at z > 0 and 4.5e-6 at z < 0. A window cut on eta then changes its decision about 20 times more often in the negative endcap.With the reconstruction built against this fix, on the same 1037 simulated events with 200 pileup interactions (the 21 files of a CMS validation sample of 60,710 events that contain an event in which the two implementations disagree):
The 3 events that remain come from the float rounding of the cluster positions, which is the same on both sides.
The fix
The function computes the formula for
|zs|, where the sum has no cancellation, and applies the sign ofzsto the result. The branch for large|zs|uses the same expression for both signs. The branch for rho = 0 does not change. The scalar type does not change.The first commit changes GenVector and adds the test. The second commit makes the same change in the copy of the function in GenVectorX (
MathX/GenVectorX/eta.h).Measurements
A standalone program compares the function of master and the function of this PR with
asinhl(z / rho)in long double. Each line has 4e6 random points, each evaluated at +z and at -z. The error is in units of the last place (ULP) of the scalar type: largest value / 99% quantile. x86-64, GCC 8.5,-O2.Number of points with
eta(-z) != -eta(z)on master, for rho in [1, 300] and |z| in (0, 600]: 2301690 of 4e6 (float) and 3530696 of 4e6 (double). With this PR: 0 in all samples.A second test moves each of x, y and z of a float position by -1, 0 or +1 float step (rho in [30, 270], |z| in [320, 520], 2e6 positions) and reads the change of eta. At z > 0 the 99% quantile is 2.4e-7 and the largest value is 4.8e-7, on master and with this PR. At z < 0 the 99% quantile goes from 6.7e-6 to 2.4e-7 and the largest value goes from 3.0e-5 to 4.8e-7.
Not in this PR
log(zs + sqrt(zs * zs + 1))loses relative precision when |zs| is small, because the argument of the log is near 1. The error is the same for both signs of z (99% quantile 50 ULP, largest value 8.7e5 ULP for rho in [1, 300] and |z| in (0, 600]), on master and with this PR. A fix for that changes the results at z > 0, so it is not part of this change.Eta_FromTheta(used byPolar3D) has no such subtraction and does not change.MathX/GenVectorX/MathHeaders.hhas two more copies ofEta_FromRhoZ, outside theImplnamespace. No code calls them. They do not change.Tests
The new test
math/genvector/test/testEta.cxx(testGenVectorEta, Google Test) checksCartesian3D,Cylindrical3D,PxPyPzE4DandPxPyPzM4D, for z/rho from 3e-5 to 7e8:asinh(z / rho)in long double, for both signs of z (the largest error on the grid of the test is 1.14 ULP).The test fails on master (21742 failed checks) and passes with this PR.
coordinates3Dandcoordinates4Dpass on master and with this PR.Checklist:
coordinates3Dandcoordinates4D, with GCC 8.5 and GCC 14.3; no full ROOT build)