Skip to content

Fix precision of eta at negative z in GenVector - #24233

Merged
guitargeek merged 2 commits into
root-project:masterfrom
felicepantaleo:genvector-eta-negative-z
Nov 19, 2026
Merged

guitargeek merged 2 commits into
root-project:masterfrom
felicepantaleo:genvector-eta-negative-z

Conversation

@felicepantaleo

Copy link
Copy Markdown
Contributor

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) computes log(zs + sqrt(zs * zs + 1)) with zs = z / rho. For zs < 0 the sum subtracts two nearly equal numbers. The error of eta is then about zs * zs times the rounding error of the scalar type. For large negative zs (|zs| above epsilon^-0.25, which is 54 for float and 8192 for double) the function returns -log(-2 * zs): it drops the 0.5 / zs term that it keeps for positive zs.

Cartesian3D, Cylindrical3D, PxPyPzE4D and PxPyPzM4D call this function. The problem is thus in Eta() of XYZVector, XYZPoint, RhoZPhiVector, PxPyPzEVector, PxPyPzMVector and their float variants.

Example: ROOT::Math::XYZVectorF with x = 30, y = 40 (rho = 50) and |z| = 340. The exact |eta| is 2.61543304, and the nearest float is 2.61543298.

master this PR
Eta() at z = +340 2.61543298 2.61543298
Eta() at z = -340 -2.61543393 -2.61543298
error at z = +340 0.26 ULP 0.26 ULP
error at z = -340 3.74 ULP 0.26 ULP
change of eta for one float step of z at z = +340 2.4e-7 2.4e-7
change of eta for one float step of z at z = -340 9.5e-7 2.4e-7

Context

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 XYZVector and 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):

Events where the two implementations take a different decision master this PR
in the endcap at z < 0 22 2
in the endcap at z > 0 1 1
total 23 3

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 of zs to 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.

  • Eta is an odd function of z, bit for bit.
  • The results at z > 0 do not change, bit for bit (0 of 24e6 tested points differ).
  • The error at z < 0 is equal to the error at z > 0.

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.

Scalar Points z > 0, master and this PR z < 0, master z < 0, this PR
float rho in [1, 300], |z| in (0, 600], |z|/rho >= 1 1.32 / 0.80 256 / 115 1.32 / 0.80
float rho in [1, 300], |z| in (0, 600], |z|/rho < 1 1.49 / 0.89 1.72 / 0.97 1.49 / 0.89
float rho in [0.001, 1], |z| in (0, 600], |z|/rho >= 1 1.10 / 0.53 256 / 78 1.10 / 0.53
float rho in [1, 300], |z|/rho in [10, 1e10] 0.79 / 0.52 256 / 139 0.79 / 0.52
double rho in [1, 300], |z| in (0, 600], |z|/rho >= 1 2.65 / 1.21 9.0e4 / 800 2.65 / 1.21
double rho in [0.001, 1], |z| in (0, 600], |z|/rho >= 1 1.98 / 0.56 6.2e6 / 3.3e6 1.98 / 0.56
double rho in [1, 300], |z|/rho in [10, 1e10] 1.11 / 0.56 6.1e6 / 2.2e6 1.11 / 0.56

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

  • For double and |z|/rho < 1, 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 by Polar3D) has no such subtraction and does not change.
  • MathX/GenVectorX/MathHeaders.h has two more copies of Eta_FromRhoZ, outside the Impl namespace. No code calls them. They do not change.

Tests

The new test math/genvector/test/testEta.cxx (testGenVectorEta, Google Test) checks Cartesian3D, Cylindrical3D, PxPyPzE4D and PxPyPzM4D, for z/rho from 3e-5 to 7e8:

  • eta(-z) is equal to -eta(z) bit for bit, for float and double;
  • eta of a float vector is within 2 ULP of 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. coordinates3D and coordinates4D pass on master and with this PR.

Checklist:

  • tested changes locally (standalone build of the GenVector headers, of the new test and of coordinates3D and coordinates4D, with GCC 8.5 and GCC 14.3; no full ROOT build)
  • updated the docs (if necessary): no change of the documentation is necessary

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.
@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Test Results

    22 files      22 suites   3d 11h 25m 22s ⏱️
 3 893 tests  3 893 ✅ 0 💤 0 ❌
76 213 runs  76 213 ✅ 0 💤 0 ❌

Results for commit 585a8d2.

♻️ This comment has been updated with latest results.

@felicepantaleo felicepantaleo mentioned this pull request Nov 19, 2026
13 tasks done

@guitargeek guitargeek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks a lot for the contribution

@guitargeek
guitargeek merged commit bd5a3e8 into root-project:master Nov 19, 2026
54 of 59 checks passed
@guitargeek

Copy link
Copy Markdown
Contributor

/backport to 6.42

@root-project-bot

Copy link
Copy Markdown

Preparing to backport PR #24233 to branch 6.42 requested by guitargeek

@root-project-bot

Copy link
Copy Markdown

This PR has been backported to branch 6.42: #24262

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants