-
Notifications
You must be signed in to change notification settings - Fork 0
/
signal_to_noise_dt.py
281 lines (209 loc) · 8.35 KB
/
signal_to_noise_dt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 7 10:24:11 2022
@author: beriksso
"""
import numpy as np
import sys
sys.path.insert(0, 'C:/python/useful_definitions/')
import useful_defs as udfs
import matplotlib.pyplot as plt
udfs.set_nes_plot_style()
import scipy as sp
def import_data(file):
"""Return data."""
p = udfs.unpickle(file)
bgr_level = np.append(np.flip(p['bgr_level'][1:]), p['bgr_level'])
return p['bins'], p['counts'], bgr_level
def plot_data(bins, counts, bgr_level, title):
"""Plot data."""
plt.figure(title)
plt.plot(bins, counts, 'k.')
plt.errorbar(bins, counts, yerr=np.sqrt(counts), color='k',
linestyle='None')
plt.plot(bins, bgr_level, 'C0--', label='random coincidences')
plt.xlabel('$t_{TOF}$ (ns)')
plt.ylabel('counts')
def gaussian(amplitude, mu, sigma, x, bgr_level):
"""Gaussian function with background."""
return amplitude * np.exp(-(x - mu)**2 / sigma**2) + bgr_level
def fit_function(parameters, bins, counts, bgr_level, fit_range):
"""Fit function for fitting a Gaussian."""
# Make gaussian
g = gaussian(*parameters, bins, bgr_level)
# Calculate chi2 over fit range
mask = ((bins > fit_range[0]) & (bins < fit_range[1]))
chi2 = np.sum((counts[mask] - g[mask])**2 / g[mask])
return chi2
def plot_gaussian(parameters, bins, counts, bgr_level, title):
"""Plot Gaussian and total fit on data."""
plt.figure(title)
gauss = gaussian(*parameters, bins, 0)
total = gaussian(*parameters, bins, bgr_level)
plt.plot(bins, gauss, 'k-.', label='gaussian fit')
plt.plot(bins, total, 'r-', label='total')
plt.legend()
def signal_to_noise(parameters, bins, counts, bgr_level):
"""Calculate signal to noise ratio for 3 sigma interval."""
# Make Gaussian without background component
gauss = gaussian(*parameters, bins, 0)
# Find 3 sigma interval
mu = parameters[1]
sigma = parameters[2]
mask = ((bins > mu - 3 * sigma) & (bins < mu + 3 * sigma))
# Integrate trapezoidally
signal = np.trapz(gauss[mask], dx=np.diff(bins)[0])
noise = np.trapz(bgr_level[mask], dx=np.diff(bins)[0])
return signal / noise
def plot_for_paper_dt(shot_number):
"""Create plot for technical TOFu paper."""
fig, axes = plt.subplots(2, 1, sharex=True)
suffixes = ['NoKinCut', 'KinCut']
for i, ax in enumerate(axes.flatten()):
# Import data
file = f'data/{shot_number}/{shot_number}_{suffixes[i]}.pickle'
bins, counts, bgr_level = import_data(file)
# Fit Gaussian to DT peak
initial_guess = (100, 26, 1)
fit_range = (15, 35)
popt = sp.optimize.minimize(fit_function, initial_guess,
args=(bins, counts, bgr_level, fit_range))
# Plot data
ax.plot(bins, counts, 'k.', markersize=3)
ax.errorbar(bins, counts, yerr=np.sqrt(counts), linestyle='None',
color='k')
# Plot total fit
gauss = gaussian(*popt.x, bins, 0)
ax.plot(bins, gauss + bgr_level, 'r-', label='total')
# Plot background
ax.plot(bins, bgr_level, 'C0--', label='background')
# Plot Gaussian fit
ax.plot(bins, gauss, 'C1-.', label='Gaussian fit')
ax.set_ylabel('counts')
# Configure plots
axes[1].set_xlabel('$t_{TOF}$ (ns)')
axes[0].set_ylim(-15, 350)
axes[1].set_xlim(10, 45)
axes[1].set_ylim(-15, 200)
axes[0].legend(loc='upper right')
axes[0].text(0.05, 0.9, '(a)', transform=axes[0].transAxes)
axes[1].text(0.05, 0.9, '(b)', transform=axes[1].transAxes)
fig.set_size_inches(4, 7)
plt.subplots_adjust(hspace=0.05)
def plot_for_paper_dd(shot_number):
"""Create plot for technical TOFu paper."""
fig, axes = plt.subplots(2, 1, sharex=True)
suffixes = ['NoKinCut', 'KinCut']
for i, ax in enumerate(axes.flatten()):
# Import data
file = f'data/{shot_number}/{shot_number}_{suffixes[i]}.pickle'
bins, counts, bgr_level = import_data(file)
# Fit Gaussian to DD peak
initial_guess = (100, 63.4, 1)
fit_range = (57, 71)
popt = sp.optimize.minimize(fit_function, initial_guess,
args=(bins, counts, bgr_level, fit_range))
# Plot data
ax.plot(bins, counts, 'k.', markersize=3)
ax.errorbar(bins, counts, yerr=np.sqrt(counts), linestyle='None',
color='k')
# Plot total fit
gauss = gaussian(*popt.x, bins, 0)
ax.plot(bins, gauss + bgr_level, 'r-', label='total')
# Plot background
ax.plot(bins, bgr_level, 'C0--', label='background')
# Plot Gaussian fit
ax.plot(bins, gauss, 'C1-.', label='Gaussian fit')
ax.set_ylabel('counts')
# Configure plots
axes[1].set_xlabel('$t_{TOF}$ (ns)')
axes[0].set_ylim(-50, 2000)
axes[1].set_xlim(35, 80)
axes[1].set_ylim(-50, 1700)
axes[0].legend(loc='center left')
axes[0].text(0.05, 0.9, '(a)', transform=axes[0].transAxes)
axes[1].text(0.05, 0.9, '(b)', transform=axes[1].transAxes)
fig.set_size_inches(4, 7)
plt.subplots_adjust(hspace=0.05)
def plot_for_paper(shot_number):
"""Create plot for technical TOFu paper."""
fig, axes = plt.subplots(3, 1)
suffixes = ['NoKinCut', 'KinCut']
# Plot full TOF spectrum
file = f'data/{shot_number}/{shot_number}_{suffixes[0]}.pickle'
bins, counts, bgr_level = import_data(file)
axes[0].plot(bins, counts, 'k.', markersize=1)
axes[0].errorbar(bins, counts, yerr=np.sqrt(counts), linestyle='None',
color='k')
for i, ax in enumerate(axes[1:]):
# Import data
file = f'data/{shot_number}/{shot_number}_{suffixes[i]}.pickle'
bins, counts, bgr_level = import_data(file)
# Fit Gaussian to DT peak
initial_guess = (100, 26, 1)
fit_range = (15, 35)
popt = sp.optimize.minimize(fit_function, initial_guess,
args=(bins, counts, bgr_level, fit_range))
# Plot data
ax.plot(bins, counts, 'k.', markersize=3)
ax.errorbar(bins, counts, yerr=np.sqrt(counts), linestyle='None',
color='k')
# Plot total fit
gauss = gaussian(*popt.x, bins, 0)
ax.plot(bins, gauss + bgr_level, 'r-', label='total')
# Plot background
ax.plot(bins, bgr_level, 'C0--', label='background')
# Plot Gaussian fit
ax.plot(bins, gauss, 'C1-.', label='Gaussian fit')
ax.set_ylabel('counts')
# Labels
axes[2].set_xlabel('$t_{TOF}$ (ns)')
axes[1].legend(loc='upper right')
# Limits
axes[0].set_yscale('log')
axes[0].set_ylim(100, 4000)
axes[1].set_ylim(-15, 350)
axes[2].set_ylim(-15, 200)
axes[0].set_xlim(10, 90)
axes[1].set_xlim(10, 45)
axes[2].set_xlim(10, 45)
# Letters
axes[0].text(0.03, 0.9, '(a)', transform=axes[0].transAxes)
axes[1].text(0.03, 0.9, '(b)', transform=axes[1].transAxes)
axes[2].text(0.03, 0.9, '(c)', transform=axes[2].transAxes)
# Lines
axes[0].axvline(20, color='k', linestyle='dotted')
axes[0].axvline(33, color='k', linestyle='dotted')
fig.set_size_inches(4, 10)
plt.subplots_adjust(hspace=0.15)
def main(shot_number, suffix):
"""Run analysis for one input."""
# Import data
file = f'data/{shot_number}/{shot_number}_{suffix}.pickle'
bins, counts, bgr_level = import_data(file)
# Plot data
plot_data(bins, counts, bgr_level, suffix)
# Fit Gaussian to DT peak
initial_guess = (100, 26.4, 1)
fit_range = (15, 35)
# initial_guess = (100, 63.4, 1)
# fit_range = (57, 71)
popt = sp.optimize.minimize(fit_function, initial_guess,
args=(bins, counts, bgr_level, fit_range))
# Plot
plot_gaussian(popt.x, bins, counts, bgr_level, suffix)
# Calculate signal to noise ratio
sb = signal_to_noise(popt.x, bins, counts, bgr_level)
return sb
if __name__ == '__main__':
shot_number = 100850
# shot_number = 99552
sb_a = main(shot_number, 'KinCut')
sb_b = main(shot_number, 'NoKinCut')
print(f'S/B (a): {sb_a:.3f}')
print(f'S/B (b): {sb_b:.3f}')
print(f'Improvement: {sb_a/sb_b:.3f}')
plot_for_paper_dt(shot_number)
plot_for_paper_dd(shot_number)
plot_for_paper(shot_number)