Skip to content

fix: Fix pixel kernel sub-tick timing when the tick is coarser than the response table - #5

Open
pgranger23 wants to merge 1 commit into
DeepLearnPhysics:mainfrom
pgranger23:fix/pixel-subtick-timing
Open

pgranger23 wants to merge 1 commit into
DeepLearnPhysics:mainfrom
pgranger23:fix/pixel-subtick-timing

Conversation

@pgranger23

Copy link
Copy Markdown

Fix pixel kernel sub-tick timing when the tick is coarser than the response table

Summary

With a simulation tick coarser than the 0.1 µs pixel response table (rebin_factor > 1, so every pixel preset at the default 2 MHz), the time of a pixel signal depends on where inside the tick the charge arrives. The signal time is a sawtooth in the arrival time, 0.85 µs peak to peak on the ND-LAr preset (±0.65 mm in drift). At a 0.1 µs tick (rebin_factor = 1) nothing is wrong and this PR changes nothing.

After the fix the kernel output equals the tick average of the response arriving at the true time, and the signal is continuous in the arrival time.

Cause

interpolate_pixel_response_kernel splits the sub-tick offset into whole 0.1 µs bins t_sub_bin and a fraction t_alpha:

interpolated = t_alpha * v_interp[:-1] + (1 - t_alpha) * v_interp[1:]   # moves the trace later as t_alpha grows: correct
...
shifted = jnp.roll(output_fine, -t_sub_bin, axis=-1)                    # moves it EARLIER as t_sub_bin grows
  1. The two shifts act in opposite directions. Within a tick the signal slides earlier by 0.1 µs for every 0.1 µs the charge arrives later, then jumps back at the tick boundary.
  2. time_zero_bin = (time_zero_bin_npz - 1) // rebin_factor drops the remainder, so the rebin groups do not start on a tick boundary relative to the arrival. With the roll sign corrected alone, every signal would be a constant 0.4 µs late for the shipped response at 0.5 µs.

Reproduction

Public functions only, shipped config/pixel_response.npz, run from the repository root:

import numpy as np
from tools.kernels import load_pixel_response_kernel, interpolate_pixel_response_kernel

dt = 0.5                                              # us, the presets' 2 MHz tick
pk = load_pixel_response_kernel("config/pixel_response.npz", time_spacing=dt, pixel_pitch_cm=0.372)
npz = np.load("config/pixel_response.npz")
resp = npz["response"][0, 0]                          # centre pixel, charge at the pixel centre
t_resp = (np.arange(resp.size) + 0.5) * 0.1 - float(npz["drift_time"])
c_resp = (t_resp * resp).sum() / resp.sum()           # response centroid relative to the arrival

for arrival in 100.0 + np.arange(0, 1.01, 0.1):       # us, swept over two ticks
    tick = int(arrival // dt)
    out = np.asarray(interpolate_pixel_response_kernel(
        pk.DKernel, 0.0, 0.0, 0.0, arrival / dt - tick, pk.pixel_spacing,
        pk.kernel_py, pk.kernel_pz, pk.rebin_factor))[pk.py_zero_bin, pk.pz_zero_bin]
    t = (tick - pk.time_zero_bin + np.arange(out.size) + 0.5) * dt      # simulator placement rule
    print(f"arrival {arrival:6.1f} us   signal centroid - expected = {(t * out).sum() / out.sum() - (arrival + c_resp):+.3f} us")

Signal centroid minus expected, in µs, for arrivals 100.0, 100.1, … 101.0 µs:

100.0 100.1 100.2 100.3 100.4 100.5 100.6 100.7 100.8 100.9 101.0
main +0.412 +0.237 +0.025 −0.234 −0.439 +0.412 +0.237 +0.025 −0.234 −0.439 +0.412
this PR −0.039 −0.034 +0.025 +0.037 +0.012 −0.039 −0.034 +0.025 +0.037 +0.012 −0.039

The same through DetectorSimulator end to end (ND-LAr preset, one volume, point deposits at pixel centres):

image

Centre-pixel signal centroid minus (true arrival + response centroid) against the true arrival time. Orange: main. Blue: this PR. Solid: no diffusion. Dashed: default diffusion.

The fix

  • interpolate_pixel_response_kernel: delay the fine trace by +t_sub_bin bins, zero-filled, so nothing wraps around from the tail.
  • load_pixel_response_kernel: prepend (1 - time_zero_bin_npz) % rebin_factor zero time bins to the response before the table is built, so that time_zero_bin_npz - 1 is a multiple of rebin_factor and the rebin groups line up with the simulation ticks.

Linear interpolation between adjacent 0.1 µs bins is the exact bin average of a current that is constant within each bin, and the mean over rebin_factor such bins is the exact tick average. So the output is now exact for the table as stored, not an approximation.

The padding is lossless (zeros in front). kernel_time grows by one tick when padding is needed: 69 → 70 for the shipped response at 0.5 µs, unchanged at 0.1 µs.

Effect on derivatives

On main the derivative with respect to the arrival time is locally correct, but the signal jumps at every 0.1 µs of arrival time, so that derivative cannot be used to fit a time. After the fix the signal is continuous.

image

One deposit through the ND-LAr pixel kernel at a 0.5 µs tick. Left: signal centroid minus arrival time. Middle: squared difference to the signal of a deposit arriving at 35.63 µs, against the trial arrival time; triangles show where 15 gradient-descent fits end (main: 1 of 15 reaches the true time; this PR: 15 of 15). Right: derivative of that loss by jax.grad (lines) and by finite differences (circles); shaded where the derivative on main points away from the true time.

Tests

New tests/test_pixel_kernel_timing.py (7 tests, synthetic response written to tmp_path, CPU, about 6 s):

  • the placed kernel output equals the exact tick average of the response delayed to the true arrival time, at rebin_factor 1 and 5 and for both phases of the table's time origin (tolerance 1e-5 of the peak);
  • signal centroid minus arrival time is constant across ticks (0.005 µs);
  • the signal is continuous across a tick boundary, and jax.grad with respect to the arrival time matches finite differences on either side of a tick boundary and of a 0.1 µs bin boundary.
main this PR
new tests 4 failed, 3 passed 7 passed
fast suite (-m "not slow") 197 passed, 23 skipped 204 passed, 23 skipped

The four failures on main are the two rebin_factor = 5 comparisons, the centroid test and the continuity test.

Other checks

Check Result
Kernel derivatives, jax.grad against finite differences, 300 random points on the ND-LAr kernel arrival time 3e-7, transverse offsets and diffusion level below 1e-8 (relative to the mean gradient)
Run time, kernel alone, 20,000 deposits (CPU) 564 → 575 ms (+1.9 %)
Run time, full pixel path, 10,000 deposits (CPU) 3.79 → 3.86 s (+1.8 %)
Compilation time unchanged
Charge per pixel unchanged (centre-pixel integral 1.0000 before and after)

Run times are the best of three alternating runs on a shared CPU machine; not measured on GPU. The cost matches the one extra output tick.

What remains in Figure 1 after the fix

  • ±0.04 µs ripple without diffusion. This is the centroid estimator, not the simulation: a centroid taken at tick centres is biased when the pulse is narrower than a tick. The exact tick average of the table, computed in numpy without JAXTPC and passed through the same estimator, reproduces the curve point by point. With longitudinal diffusion alone the ripple is 0.0003 µs.
  • Constant +0.10 µs with diffusion. This is in the response table and is the same on main at a 0.1 µs tick. Longitudinal diffusion alone gives 0.0000 µs; transverse diffusion alone gives +0.1015 µs. In the table the centre-pixel pulse comes later when the charge lands away from the pixel centre (centroid −0.355 µs at the centre, +0.048 µs at the edge); a calculation from the table alone predicts +0.105 µs for this drift.
image

Impact on existing users

  • Pixel samples produced at a 0.5 µs tick carry the timing sawtooth: up to ±0.4 µs, correlated with the sub-tick arrival position. Waveform shapes on the tick grid change with this PR; charge per pixel does not.
  • Configurations at a 0.1 µs tick are bit-for-bit unchanged.
  • kernel_time is one tick longer at 0.5 µs (per-deposit kernel memory +1.4 %). The analytic box size from compute_box_dims follows automatically.
  • The wire path is not touched.
  • No stored reference output in the repository depends on pixel timing.

interpolate_pixel_response_kernel delays the fine (NPZ-resolution) trace
by the sub-bin fraction, then applied jnp.roll(-t_sub_bin), which moves it
EARLIER by the whole-bin part of the offset. With rebin_factor > 1 (any
tick coarser than the 0.1 us NPZ bin, e.g. the default 0.5 us) the signal
time is a sawtooth in the deposit's arrival time: 0.85 us peak-to-peak on
the ND-LAr preset. rebin_factor = 1 (0.1 us tick) is unaffected.

- Delay by +t_sub_bin with zero fill (no wrap-around of the tail).
- Align the rebin groups with the simulation ticks: prepend
  (1 - time_zero_bin_npz) % rebin_factor zero time bins so that the
  arrival bin minus one is a multiple of rebin_factor. Without this the
  arrival lands ((time_zero_bin_npz - 1) % rebin_factor) NPZ bins late
  (0.4 us for the shipped response at 0.5 us).

The placed kernel output now equals the tick average of the response
arriving at the true time. kernel_time grows by one tick when padding is
needed (69 -> 70 for the shipped response at 0.5 us).

tests: test_pixel_kernel_timing.py (exact comparison with the delayed
response at rebin 1 and 5, centroid linearity, d/d(arrival) continuity
and finite differences across tick and NPZ-bin boundaries).

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant