XENONnT / straxen

Streaming analysis for XENON
BSD 3-Clause "New" or "Revised" License
21 stars 33 forks source link

Fixed bug in theta uncertainty in event position contour #1465

Closed napoliion closed 1 week ago

napoliion commented 2 weeks ago

Before you submit this PR: make sure to put all operations-related information in a wiki-note, a PR should be about code and is publicly accessible

What does the code in this PR do / what does it improve?

This fixes a bug found by @sebvetter (figure below) in how theta uncertainties were handled due to contours that passed over the -pi and pi boundary. These contours would have an uncertainty that is an order of magnitude smaller than the contours that do not cross this boundary.

theta_uncertainty_bug_svetter

Can you briefly describe how it works?

The theta uncertainty is calculated as the difference between the min and max theta of a contour. The correction is to rotate the contour around (0,0) by the average $\theta$ within the contour so that the contour is now around $\theta = 0$ to avoid the seam. Then add $\pi$ and modulo $2\pi$ to handle large angular contours, before taking the difference between the minimum and maximum angles.

theta_array_shift = (np.subtract(theta_array, avg_theta) + np.pi)%(2*np.pi)
theta_min = np.min(theta_array_shift, axis=1)
theta_max = np.max(theta_array_shift, axis=1)

theta_diff = theta_max - theta_min

Can you give a minimal working example (or illustrate with a figure)?

An example edge case is some generic array around the $-\pi$ and $+\pi$ boundary where the min and max $\theta$ are -3.14 and 3.14 respectively.

test_theta_array = np.hstack([np.random.uniform(3.14, 3.13, 8), np.random.uniform(-3.14, -3.13, 7), np.array([3.14]), np.array([-3.14])])

We expect the uncertainty to be np.pi - 3.14 = 0.0015926535897929917.

I have also loaded this with an existing run using a few data points that fall along the -pi and +pi seam.

Notes on testing

All italic comments can be removed from this template.