#!/usr/bin/env python # Copyright (c) 2019, Anthony Latorre # # 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 . from __future__ import print_function, division import yaml try: from yaml import CLoader as Loader except ImportError: from yaml.loader import SafeLoader as Loader 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") 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 if __name__ == '__main__': import argparse import matplotlib.pyplot as plt import numpy as np parser = argparse.ArgumentParser("plot fit results") parser.add_argument("filenames", nargs='+', help="input files") args = parser.parse_args() for filename in args.filenames: print(filename) with open(filename) as f: data = yaml.load(f.read(),Loader=Loader) dx = [] dy = [] dz = [] dT = [] thetas = [] likelihood_ratio = [] t_electron = [] t_muon = [] psi = [] for event in data['data']: # get the particle ID id = event['mcgn'][0]['id'] if 'ev' not in event: continue if 'fit' not in event['ev'][0]: # if nhit < 100 we don't fit the event continue if id not in event['ev'][0]['fit']: continue fit = event['ev'][0]['fit'] mass = SNOMAN_MASS[id] # for some reason it's the *second* track which seems to contain the # initial energy true_energy = event['mcgn'][0]['energy'] # The MCTK bank has the particle's total energy (except for neutrons) # so we need to convert it into kinetic energy ke = true_energy - mass energy = fit[id]['energy'] dT.append(energy-ke) true_posx = event['mcgn'][0]['posx'] posx = fit[id]['posx'] dx.append(posx-true_posx) true_posy = event['mcgn'][0]['posy'] posy = fit[id]['posy'] dy.append(posy-true_posy) true_posz = event['mcgn'][0]['posz'] posz = fit[id]['posz'] dz.append(posz-true_posz) dirx = event['mcgn'][0]['dirx'] diry = event['mcgn'][0]['diry'] dirz = event['mcgn'][0]['dirz'] true_dir = [dirx,diry,dirz] true_dir = np.array(true_dir)/np.linalg.norm(true_dir) theta = fit[id]['theta'] phi = fit[id]['phi'] dir = [np.sin(theta)*np.cos(phi),np.sin(theta)*np.sin(phi),np.cos(theta)] dir = np.array(dir)/np.linalg.norm(dir) thetas.append(np.degrees(np.arccos(np.dot(true_dir,dir)))) if IDP_E_MINUS in fit and IDP_MU_MINUS in fit: fmin_electron = fit[IDP_E_MINUS]['fmin'] fmin_muon = fit[IDP_MU_MINUS]['fmin'] likelihood_ratio.append(fmin_muon-fmin_electron) if IDP_E_MINUS in fit: t_electron.append(fit[IDP_E_MINUS]['time']) if IDP_MU_MINUS in fit: t_muon.append(fit[IDP_MU_MINUS]['time']) if 'nhit' in event['ev'][0]: nhit = event['ev'][0]['nhit'] psi.append(fit[id]['psi']/nhit) if len(t_electron) < 2 or len(t_muon) < 2: continue mean, mean_error, std, std_error = get_stats(dT) print("dT = %.2g +/- %.2g" % (mean, mean_error)) print("std(dT) = %.2g +/- %.2g" % (std, std_error)) mean, mean_error, std, std_error = get_stats(dx) print("dx = %4.2g +/- %.2g" % (mean, mean_error)) print("std(dx) = %4.2g +/- %.2g" % (std, std_error)) mean, mean_error, std, std_error = get_stats(dy) print("dy = %4.2g +/- %.2g" % (mean, mean_error)) print("std(dy) = %4.2g +/- %.2g" % (std, std_error)) mean, mean_error, std, std_error = get_stats(dz) print("dz = %4.2g +/- %.2g" % (mean, mean_error)) print("std(dz) = %4.2g +/- %.2g" % (std, std_error)) mean, mean_error, std, std_error = get_stats(thetas) print("std(theta) = %4.2g +/- %.2g" % (std, std_error)) plt.figure(1) plot_hist(dT, label=filename) plt.xlabel("Kinetic Energy difference (MeV)") plt.figure(2) plot_hist(dx, label=filename) plt.xlabel("X Position difference (cm)") plt.figure(3) plot_hist(dy, label=filename) plt.xlabel("Y Position difference (cm)") plt.figure(4) plot_hist(dz, label=filename) plt.xlabel("Z Position difference (cm)") plt.figure(5) plot_hist(thetas, label=filename) plt.xlabel(r"$\theta$ (deg)") plt.figure(6) plot_hist(likelihood_ratio, label=filename) plt.xlabel(r"Log Likelihood Ratio ($e/\mu$)") plt.figure(7) plot_hist(np.array(t_electron)/1e3/60.0, label=filename) plt.xlabel(r"Electron Fit time (minutes)") plt.figure(8) plot_hist(np.array(t_muon)/1e3/60.0, label=filename) plt.xlabel(r"Muon Fit time (minutes)") if len(psi): plt.figure(9) plot_hist(psi, label=filename) plt.xlabel(r"$\Psi$/Nhit") plot_legend(1) plot_legend(2) plot_legend(3) plot_legend(4) plot_legend(5) plot_legend(6) plot_legend(7) plot_legend(8) if len(psi): plot_legend(9) plt.show()