Skip to content

Commit 9786ea9

Browse files
committed
ENH: Fit WEPL to angle standard deviation in pctweplfit
1 parent 62a07c5 commit 9786ea9

1 file changed

Lines changed: 45 additions & 10 deletions

File tree

applications/pctweplfit/pctweplfit.py

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ def add_detector(name, translation, attach_to_phantom=False):
107107
"EventID",
108108
"TrackID",
109109
"Position",
110+
"Direction",
110111
"PreGlobalTime",
111112
"KineticEnergy",
112113
]
@@ -139,13 +140,15 @@ def process_phantom_length(
139140
tofs,
140141
wepls,
141142
elosses,
143+
angles,
142144
verbose,
143145
):
144146
import uproot
145147

146148
tofs_phantom_length = []
147149
wepls_phantom_length = []
148150
elosses_phantom_length = []
151+
angles_phantom_length = []
149152

150153
data = uproot.concatenate(f"{output}/l{int(phantom_length)}_*.root", library="np")
151154
pv(
@@ -177,15 +180,20 @@ def process_phantom_length(
177180
):
178181
continue
179182

183+
us = data["Position_X"][event_mask]
184+
vs = data["Position_Y"][event_mask]
185+
ws = data["Position_Z"][event_mask]
186+
dus = data["Direction_X"][event_mask]
187+
dvs = data["Direction_Y"][event_mask]
188+
dws = data["Direction_Z"][event_mask]
189+
180190
times = data["PreGlobalTime"][event_mask]
191+
181192
tof = times[-1] - times[0]
182193

183194
if phantom_length == 0.0:
184195
wepl = 0.0
185196
else:
186-
us = data["Position_X"][event_mask]
187-
vs = data["Position_Y"][event_mask]
188-
ws = data["Position_Z"][event_mask]
189197
if path_type == "phantom_length":
190198
# Length of the phantom
191199
wepl = phantom_length
@@ -215,13 +223,38 @@ def process_phantom_length(
215223
data["KineticEnergy"][event_mask][0] - data["KineticEnergy"][event_mask][-1]
216224
)
217225

226+
d_in_uw = np.array([dus[0], dws[0]])
227+
d_in_vw = np.array([dvs[0], dws[0]])
228+
d_out_uw = np.array([dus[-1], dws[-1]])
229+
d_out_vw = np.array([dvs[-1], dws[-1]])
230+
angle = [
231+
np.arccos(
232+
np.minimum(
233+
1.0,
234+
d_in_uw
235+
@ d_out_uw
236+
/ (np.sqrt(d_in_uw @ d_in_uw) * np.sqrt(d_out_uw @ d_out_uw)),
237+
)
238+
),
239+
np.arccos(
240+
np.minimum(
241+
1.0,
242+
d_in_vw
243+
@ d_out_vw
244+
/ (np.sqrt(d_in_vw @ d_in_vw) * np.sqrt(d_out_vw @ d_out_vw)),
245+
)
246+
),
247+
]
248+
218249
tofs_phantom_length.append(tof)
219250
wepls_phantom_length.append(wepl)
220251
elosses_phantom_length.append(eloss)
252+
angles_phantom_length += angle
221253

222254
tofs[phantom_length] = tofs_phantom_length
223255
wepls[phantom_length] = wepls_phantom_length
224256
elosses[phantom_length] = elosses_phantom_length
257+
angles[phantom_length] = angles_phantom_length
225258

226259

227260
def tof_fit(
@@ -240,6 +273,7 @@ def tof_fit(
240273
tofs = manager.dict()
241274
wepls = manager.dict()
242275
elosses = manager.dict()
276+
angles = manager.dict()
243277

244278
results = []
245279
with Pool() as pool:
@@ -254,6 +288,7 @@ def tof_fit(
254288
tofs,
255289
wepls,
256290
elosses,
291+
angles,
257292
verbose,
258293
),
259294
)
@@ -263,9 +298,11 @@ def tof_fit(
263298
for result in results:
264299
result.get()
265300

266-
def fit(xs, ys, xlabel, ylabel):
301+
def fit(xs, ys, xlabel, ylabel, xreduction=np.median):
267302

268-
xmedians = [np.median(xs[phantom_length]) for phantom_length in phantom_lengths]
303+
xmedians = [
304+
xreduction(xs[phantom_length]) for phantom_length in phantom_lengths
305+
]
269306
ymedians = [np.median(ys[phantom_length]) for phantom_length in phantom_lengths]
270307
xpercentile25 = [
271308
np.percentile(xs[phantom_length], 25) for phantom_length in phantom_lengths
@@ -301,17 +338,14 @@ def fit(xs, ys, xlabel, ylabel):
301338
import matplotlib.pyplot as plt
302339

303340
plt.figure()
304-
plt.plot(xmedians, ymedians, "+", label="Medians")
341+
plt.plot(xmedians, ymedians, "+")
305342

306343
tof_xs = np.linspace(np.min(xmedians), np.max(xmedians), 100)
307344
for d, p in ps.items():
308-
plt.plot(
309-
tof_xs, np.polyval(p, tof_xs), label=f"Polynomial fit (degree {d})"
310-
)
345+
plt.plot(tof_xs, np.polyval(p, tof_xs))
311346

312347
plt.xlabel(xlabel)
313348
plt.ylabel(ylabel)
314-
plt.legend()
315349

316350
if savefig:
317351
plt.savefig(f"{output}/{xlabel}_to_{ylabel}_fit.pdf")
@@ -326,6 +360,7 @@ def fit(xs, ys, xlabel, ylabel):
326360

327361
fit(tofs, wepls, "tof", "wepl")
328362
fit(elosses, wepls, "eloss", "wepl")
363+
fit(angles, wepls, xlabel="angle", ylabel="wepl", xreduction=np.std)
329364

330365

331366
def pctweplfit(

0 commit comments

Comments
 (0)