Time-walk Correction: Apply¶
Luna outputs timewalk correction matrix and lookup tables for correcting the ToA based on the magnitude of the ToT and a relationship inferred from your data. That relationship can be examined to refine your curve by adjusting the ctot-cut parameter.
However once you are happy with your lookup table application of the correction is a simple matter of lookup and subtraction.
Luna Command¶
tpx3dump process -i /Users/Ciaran/atlassian-bitbucket-pipelines-runner/temp/e71169e4-520a-5b30-a5ab-ee8a44eb5fac/build/docs/source/_static/example_data.tpx3 -o /Users/Ciaran/atlassian-bitbucket-pipelines-runner/temp/e71169e4-520a-5b30-a5ab-ee8a44eb5fac/build/docs/source/_static/example_data.hdf5 --eps-t 150ns --eps-s 1 --ctot-cut 500
Python Script¶
Apply the timewalk correction¶
1import os, sys
2from typing import *
3import lmfit
4import pandas as pd # ensure you have `pip install pandas`
5import warnings
6import seaborn as sns
7import matplotlib.pyplot as plt
8from matplotlib import cm
9from matplotlib.colors import Normalize
10
11warnings.filterwarnings("ignore") # suppress warnings from plotting libraries.
12
13sns.set_context(context="talk")
14
15# add some paths to PYTHONPATH
16for directory in ["..", "."]:
17 sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), directory)))
18
19# on our system "EXAMPLE_DATA_HDF5" refers to the absolute path
20# to a hdf5 file generated by luna. Replace with your own!
21from env_vars_for_docs_examples import EXAMPLE_DATA_HDF5, PLOTS_DIRECTORY
22
23# re-use functions from previous example
24from ex2_read_data_time_units import load_pixel_hits, load_timewalk_lookup_table, TimeUnit
25
26
27def apply_timewalk_correction(pixel_data: pd.DataFrame, timewalk_lut: pd.DataFrame):
28 """Takes the timewalk look up table (LUT) that was calculated by luna (using some
29 ctot-cut parameter x) and uses it to correct ToA for timewalk effects
30 """
31 pixel_data["corrected_toa"] = pixel_data["toa"] - pixel_data["tot"].map(timewalk_lut["AverageDToA"])
32 return pixel_data
33
34
35def plot_lookup_table(timewalk_lut: pd.DataFrame, toa_unit: TimeUnit):
36 """Plot a scatter graph with error bars (Std) of the timewakl lookup table"""
37 fig = plt.figure()
38 print(timewalk_lut)
39 timewalk_lut = timewalk_lut.dropna(subset=["AverageDToA", "Std"])
40 plt.errorbar(timewalk_lut.index, timewalk_lut["AverageDToA"], yerr=timewalk_lut["Std"],
41 marker="x", color="black", linewidth=1, linestyle="None" )
42 plt.xlabel("ToT (ns)")
43 plt.ylabel(f"Mean dToA \n({toa_unit})\n (Err=STD))")
44 plt.title("Effect of ToT magnitude on ToA")
45
46 sns.despine(fig=fig)
47
48 fname = os.path.join(PLOTS_DIRECTORY, f"ex7_correct_toa_for_timewalk.png")
49 plt.savefig(fname, bbox_inches="tight", dpi=300)
50
51
52if __name__ == "__main__":
53 toa_unit = TimeUnit.Nanoseconds
54 pixel_data: pd.DataFrame = load_pixel_hits(EXAMPLE_DATA_HDF5, toa_unit)
55 timewalk_lut: Optional[pd.DataFrame] = load_timewalk_lookup_table(EXAMPLE_DATA_HDF5, toa_unit)
56
57 if timewalk_lut is not None:
58 pixel_data = apply_timewalk_correction(pixel_data=pixel_data, timewalk_lut=timewalk_lut)
59
60 plot_lookup_table(timewalk_lut=timewalk_lut, toa_unit=toa_unit)
61
62 print("Timewalk corrected ToA's")
63 print(pixel_data.head(15).to_string())
Script Output¶
Example Output¶
hdf5 datasets: ['Clusters', 'ExposureTimeBoundaries', 'PixelHits', 'TimewalkLookupTable', 'TimewalkMatrix']
AverageDToA SumSquareDiff Std Count
ToT
25 76.245934 803962240.0 23.546906 146
50 70.257935 642109440.0 20.419456 155
75 60.629959 549626880.0 18.476557 162
100 50.204540 444413952.0 16.562899 163
125 43.402039 267351552.0 12.729150 166
... ... ... ... ...
6200 0.000000 0.0 0.000000 1
6400 -1.562500 0.0 0.000000 1
6675 0.000000 0.0 0.000000 1
6825 0.000000 0.0 0.000000 1
6850 0.000000 0.0 0.000000 1
[212 rows x 4 columns]
Timewalk corrected ToA's
toa corrected_toa cid dtoa tot x y
0 1.103455e+10 1.103455e+10 0 0 200 140 193
1 1.103455e+10 1.103455e+10 1 0 150 143 193
2 1.103459e+10 1.103459e+10 2 0 1475 68 92
3 1.103459e+10 1.103459e+10 2 15625 850 67 92
4 1.103459e+10 1.103459e+10 2 218750 300 68 91
5 1.103459e+10 1.103459e+10 2 453125 150 67 91
6 1.103464e+10 1.103464e+10 3 0 800 80 80
7 1.103464e+10 1.103464e+10 3 218750 250 80 81
8 1.103464e+10 1.103464e+10 3 296875 250 79 80
9 1.103470e+10 1.103470e+10 4 0 375 14 18
10 1.103470e+10 1.103470e+10 4 31250 375 14 19
11 1.103493e+10 1.103493e+10 5 0 375 49 5
12 1.103506e+10 1.103506e+10 6 0 1675 146 106
13 1.103506e+10 1.103506e+10 6 78125 425 146 105
14 1.103506e+10 1.103506e+10 6 171875 350 145 106
Note
Since we are (necessarily) using a small tpx3 file for the examples there is some instability between the ToT=2000ns - 4000ns. This is because the time walk lookup table is inferred directly from your data and since this dataset is minimal there may not be enough data to fully inform the timewalk matrix.