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
|
#!/usr/bin/env python
# Copyright (c) 2019, Anthony Latorre <tlatorre at uchicago>
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <https://www.gnu.org/licenses/>.
from __future__ import print_function, division
import numpy as np
from scipy.stats import iqr
from matplotlib.lines import Line2D
# on retina screens, the default plots are way too small
# by using Qt5 and setting QT_AUTO_SCREEN_SCALE_FACTOR=1
# Qt5 will scale everything using the dpi in ~/.Xresources
import matplotlib
matplotlib.use("Qt5Agg")
matplotlib.rc('font', size=22)
IDP_E_MINUS = 20
IDP_MU_MINUS = 22
SNOMAN_MASS = {
20: 0.511,
21: 0.511,
22: 105.658,
23: 105.658
}
def plot_hist(x, label=None):
# determine the bin width using the Freedman Diaconis rule
# see https://en.wikipedia.org/wiki/Freedman%E2%80%93Diaconis_rule
h = 2*iqr(x)/len(x)**(1/3)
n = max(int((np.max(x)-np.min(x))/h),10)
bins = np.linspace(np.min(x),np.max(x),n)
plt.hist(x, bins=bins, histtype='step', label=label)
def plot_legend(n):
plt.figure(n)
ax = plt.gca()
handles, labels = ax.get_legend_handles_labels()
new_handles = [Line2D([],[],c=h.get_edgecolor()) for h in handles]
plt.legend(handles=new_handles,labels=labels)
def get_stats(x):
"""
Returns a tuple (mean, error mean, std, error std) for the values in x.
The formula for the standard error on the standard deviation comes from
https://stats.stackexchange.com/questions/156518.
"""
mean = np.mean(x)
std = np.std(x)
n = len(x)
u4 = np.mean((x-mean)**4)
error = np.sqrt((u4-(n-3)*std**4/(n-1))/n)/(2*std)
return mean, std/np.sqrt(n), std, error
def std_err(x):
x = x.dropna()
mean = np.mean(x)
std = np.std(x)
n = len(x)
if n == 0:
return np.nan
u4 = np.mean((x-mean)**4)
error = np.sqrt((u4-(n-3)*std**4/(n-1))/n)/(2*std)
return error
if __name__ == '__main__':
import argparse
import matplotlib.pyplot as plt
import numpy as np
import h5py
import pandas as pd
parser = argparse.ArgumentParser("plot fit results")
parser.add_argument("filenames", nargs='+', help="input files")
args = parser.parse_args()
# Read in all the data.
#
# Note: We have to add the filename as a column to each dataset since this
# script is used to analyze MC data which all has the same run number.
ev = pd.concat([pd.read_hdf(filename, "ev").assign(filename=filename) for filename in args.filenames])
fits = pd.concat([pd.read_hdf(filename, "fits").assign(filename=filename) for filename in args.filenames])
mcgn = pd.concat([pd.read_hdf(filename, "mcgn").assign(filename=filename) for filename in args.filenames])
# get rid of 2nd events like Michel electrons
ev = ev.sort_values(['run','gtid']).groupby(['filename','evn'],as_index=False).first()
# Now, we merge all three datasets together to produce a single
# dataframe. To do so, we join the ev dataframe with the mcgn frame
# on the evn column, and then join with the fits on the run and
# gtid columns.
#
# At the end we will have a single dataframe with one row for each
# fit, i.e. it will look like:
#
# >>> data
# run gtid nhit, ... mcgn_x, mcgn_y, mcgn_z, ..., fit_id1, fit_x, fit_y, fit_z, ...
#
# Before merging, we prefix the primary seed track table with mcgn_
# and the fit table with fit_ just to make things easier.
# Prefix track and fit frames
mcgn = mcgn.add_prefix("mcgn_")
fits = fits.add_prefix("fit_")
# merge ev and mcgn on evn
data = ev.merge(mcgn,left_on=['filename','evn'],right_on=['mcgn_filename','mcgn_evn'])
# merge data and fits on run and gtid
data = data.merge(fits,left_on=['filename','run','gtid'],right_on=['fit_filename','fit_run','fit_gtid'])
# calculate true kinetic energy
mass = [SNOMAN_MASS[id] for id in data['mcgn_id'].values]
data['T'] = data['mcgn_energy'].values - mass
data['dx'] = data['fit_x'].values - data['mcgn_x'].values
data['dy'] = data['fit_y'].values - data['mcgn_y'].values
data['dz'] = data['fit_z'].values - data['mcgn_z'].values
data['dT'] = data['fit_energy1'].values - data['T'].values
true_dir = np.dstack((data['mcgn_dirx'],data['mcgn_diry'],data['mcgn_dirz'])).squeeze()
dir = np.dstack((np.sin(data['fit_theta1'])*np.cos(data['fit_phi1']),
np.sin(data['fit_theta1'])*np.sin(data['fit_phi1']),
np.cos(data['fit_theta1']))).squeeze()
data['theta'] = np.degrees(np.arccos((true_dir*dir).sum(axis=-1)))
# only select fits which have at least 2 fits
data = data.groupby(['filename','run','gtid']).filter(lambda x: len(x) > 1)
data_true = data[data['fit_id1'] == data['mcgn_id']]
data_e = data[data['fit_id1'] == IDP_E_MINUS]
data_mu = data[data['fit_id1'] == IDP_MU_MINUS]
data_true = data_true.set_index(['filename','run','gtid'])
data_e = data_e.set_index(['filename','run','gtid'])
data_mu = data_mu.set_index(['filename','run','gtid'])
data_true['ratio'] = data_mu['fit_fmin']-data_e['fit_fmin']
data_true['te'] = data_e['fit_time']
data_true['tm'] = data_mu['fit_time']
data_true['Te'] = data_e['fit_energy1']
# 100 bins between 50 MeV and 1 GeV
bins = np.arange(50,1000,100)
for id in [IDP_E_MINUS, IDP_MU_MINUS]:
events = data_true[data_true['mcgn_id'] == id]
pd_bins = pd.cut(events['T'],bins)
dT = events.groupby(pd_bins)['dT'].agg(['mean','sem','std',std_err])
dx = events.groupby(pd_bins)['dx'].agg(['mean','sem','std',std_err])
dy = events.groupby(pd_bins)['dy'].agg(['mean','sem','std',std_err])
dz = events.groupby(pd_bins)['dz'].agg(['mean','sem','std',std_err])
theta = events.groupby(pd_bins)['theta'].agg(['mean','sem','std',std_err])
label = 'Muon' if id == IDP_MU_MINUS else 'Electron'
T = (bins[1:] + bins[:-1])/2
plt.figure(1)
plt.errorbar(T,dT['mean']*100/T,yerr=dT['sem']*100/T,fmt='o',label=label)
plt.xlabel("Kinetic Energy (MeV)")
plt.ylabel("Energy bias (%)")
plt.title("Energy Bias")
plt.legend()
plt.figure(2)
plt.errorbar(T,dT['std']*100/T,yerr=dT['std_err']*100/T,fmt='o',label=label)
plt.xlabel("Kinetic Energy (MeV)")
plt.ylabel("Energy resolution (%)")
plt.title("Energy Resolution")
plt.legend()
plt.figure(3)
plt.errorbar(T,dx['mean'],yerr=dx['sem'],fmt='o',label='%s (x)' % label)
plt.errorbar(T,dy['mean'],yerr=dy['sem'],fmt='o',label='%s (y)' % label)
plt.errorbar(T,dz['mean'],yerr=dz['sem'],fmt='o',label='%s (z)' % label)
plt.xlabel("Kinetic Energy (MeV)")
plt.ylabel("Position bias (cm)")
plt.title("Position Bias")
plt.legend()
plt.figure(4)
plt.errorbar(T,dx['std'],yerr=dx['std_err'],fmt='o',label='%s (x)' % label)
plt.errorbar(T,dy['std'],yerr=dy['std_err'],fmt='o',label='%s (y)' % label)
plt.errorbar(T,dz['std'],yerr=dz['std_err'],fmt='o',label='%s (z)' % label)
plt.xlabel("Kinetic Energy (MeV)")
plt.ylabel("Position resolution (cm)")
plt.title("Position Resolution")
plt.ylim((0,plt.gca().get_ylim()[1]))
plt.legend()
plt.figure(5)
plt.errorbar(T,theta['std'],yerr=theta['std_err'],fmt='o',label=label)
plt.xlabel("Kinetic Energy (MeV)")
plt.ylabel("Angular resolution (deg)")
plt.title("Angular Resolution")
plt.ylim((0,plt.gca().get_ylim()[1]))
plt.legend()
plt.figure(6)
plt.scatter(events['Te'],events['ratio'],label=label)
plt.xlabel("Reconstructed Electron Energy (MeV)")
plt.ylabel(r"Log Likelihood Ratio (e/$\mu$)")
plt.title("Log Likelihood Ratio vs Reconstructed Electron Energy")
plt.legend()
plt.show()
|