def drift_velocity(dst, plots_dir, opt_dict): length_demo = 310 run = opt_dict["run"] fit_z_min = float(opt_dict["fit_z_min"]) fit_z_max = float(opt_dict["fit_z_max"]) fit_z_range = (fit_z_min, fit_z_max) print(f'fit z range = {fit_z_range}') Z = dst[in_range(dst.Z, fit_z_min, fit_z_max)].Z sigmoid = lambda x, A, B, C, D: A / (1 + np.exp((x - B) / C)) + D mypdf = Extended(sigmoid) #describe(mypdf) bx2 = BinnedChi2(mypdf, Z, bins=50, bound=fit_z_range) #create cost function #bx2.show(args={'A':1400, 'B':340, 'C':1.1, 'D':43 , 'N':0.1}) #another way to draw it m = Minuit(bx2, A=1400, B=340, C=1.1, D=43, N=0.1) m.migrad() my_parmloc = (0.60, 0.90) bx2.draw(m, parmloc=my_parmloc) v = length_demo / m.args[1] print(v) v_err = (length_demo - 2) / (m.args[1] - m.errors[1]) v_u = v - v_err print(f"Drift velocity = {v:.4f} +/- {v_u:.4f} ") plt.savefig(f'{plots_dir}/fit_drift_vel_{run}.pdf') print(f'plots saved in {plots_dir}/fit_drift_vel_{run}.pdf')
def energy_resolution(dst, fout, plots_dir, opt_dict): run = opt_dict["run"] fit_e_min = float(opt_dict["fit_e_min"]) fit_e_max = float(opt_dict["fit_e_max"]) fit_seed = float(opt_dict["fit_seed"]) fit_erange = (fit_e_min, fit_e_max) chi2_func = BinnedChi2(gaussC, dst.S2e, bins=50, bound=fit_erange) m = Minuit(chi2_func, mu=fit_seed, sigma=90, N=10, Ny=0) m.migrad() chi2_val = m.fval / chi2_func.ndof mean = m.values[0] mean_u = m.errors[0] reso, fig = plot_fits(dst, 1, fit_erange, m, chi2_val) fout.write(f'e_reso {reso:.3f}\n') fout.write(f's2_fit {mean:.3f}\n') fout.write(f's2u_fit {mean_u:.5f}\n') fout.write(f'chi2_fit {chi2_val:.3f}\n') print(f'Resolution = {reso}') plt.savefig(f'{plots_dir}/fit_energy_reso_{run}.pdf') plt.savefig(f'/Users/neus/current-work/ana-reso/fit_energy_reso_{run}.pdf') print(f'plots saved in {plots_dir}fit_energy_reso_{run}.pdf') print( f'plots saved in /Users/neus/current-work/ana-reso/fit_energy_reso_{run}.pdf' ) return fig, reso, chi2_val, mean, mean_u
def drift_velocity(plots_dir, dst, label=''): """ Input Z distribution Return value for the drift velocity """ # To do: make plotting of drift velocities as for energy resolution Z = dst[in_range(dst.Z, 305, 350)].Z fit_z_range = (305, 350) fig = plt.figure(figsize=(12, 4)) ax = fig.add_subplot(1, 2, 1) (_) = hist(dst.DT, bins=100, range=(0, 400), histtype='stepfilled', color='crimson') labels('Drit time ($\mu$s)', 'Entries') plt.legend(loc='upper right') ax = fig.add_subplot(1, 2, 2) (_) = hist(Z, bins=100, range=fit_z_range, histtype='stepfilled', color='crimson') labels('Drit time ($\mu$s)', 'Entries') plt.legend(loc='upper right') plt.show() sigmoid = lambda x, A, B, C, D: A / (1 + np.exp((x - B) / C)) + D mypdf = Extended(sigmoid) #describe(mypdf) chi2 = BinnedChi2(mypdf, Z, bins=100, bound=fit_z_range) #create cost function plt.figure(figsize=(7, 5)) chi2.show(args={ 'A': 1400, 'B': 330, 'C': 1.1, 'D': 43, 'N': 1 }) #another way to draw it m = Minuit(chi2, A=800, B=330, C=1.3, D=100, N=1) m.migrad() my_parmloc = (0.60, 0.90) #chi2.draw(m, parmloc=my_parmloc) plt.figure(figsize=(7, 5)) chi2.show(m, parmloc=my_parmloc) plt.savefig(f'{plots_dir}/fit_energy_reso_{label}.png') print('plots saved in ' + plots_dir)
def energy_resolution(plots_dir, dst, label=''): """ Input s2 energy signal Return value for the energy resolution """ fit_erange = (4500, 5300) # To do: work in the plotting plt.figure(figsize=(7, 5)) #fig = plt.figure(figsize=(8,6)) #ax = fig.add_subplot(5, 2, 1) chi2 = BinnedChi2(gaussC, dst.S2e, bins=50, bound=fit_erange) #create cost function chi2.show(args={ 'mu': 4900, 'sigma': 70, 'N': 400, 'Ny': 25 }) #another way to draw it plt.figure(figsize=(7, 5)) #ax = fig.add_subplot(5, 2, 2) m = Minuit(chi2, mu=4900, sigma=70, N=400, Ny=25) m.migrad() chi2.show(m) #plt.show() mean = minuit.values[0] mean_u = minuit.errors[0] sigma = minuit.values[1] sigma_u = minuit.errors[1] N = minuit.values[2] N_u = minuit.errors[2] N2 = minuit.values[3] N2_u = minuit.errors[3] print(f'Mean: {mean:.2f} +/- {mean_u:.2f} ') print(f'Sigma: {sigma:.2f} +/- {sigma_u:.2f} ') print(f'N: {N:.1f} +/- {N_u:.1f} ') print(f'N2: {N2:.1f} +/- {N2_u:.1f} ') # Pass an Tuple instead of a long list of variables plt.style.use('classic') plot_residuals_E_reso_gaussC(plots_dir, label, dst.S2e, 50, fit_erange, mean, mean_u, sigma, sigma_u, N, N_u, N2, N2_u)
def energy_reso_vs_z_r(dst, corr, file_fits, file_plot, ring='yes'): """ """ pp = PdfPages(file_fits) fit_erange = (4700, 5250) bins_r = (0, 30, 40, 50, 60, 70) bins_z = (0, 50, 100, 150, 200, 250, 300) #bins_r = (0,30) #bins_z = (0,50,100) dst_list = [] reso_list = [] num = 0 print(f'-----> Start fits to dst selected by r and z ranges\n') for i in range(len(bins_r) - 1): for j in range(len(bins_z) - 1): if ring == 'yes': dst_inrange = select_dst_ring_r_z(dst, bins_r, bins_z, i, j) elif ring == 'no': dst_inrange = select_dst_disk_r_z(dst, bins_r, bins_z, i, j) print(f'Region id = {num}, i index = {i}, j index = {j}') dst_list.append(dst_inrange) chi2 = BinnedChi2(gaussC, dst_inrange.S2e * corr, bins=50, bound=fit_erange) m = Minuit(chi2, mu=4900, sigma=70, N=400, Ny=25) m.migrad() reso, fig = plot_fits(dst_inrange, corr, fit_erange, m) reso_list.append(reso) pp.savefig(fig) num += 1 print(f'-----> Fits saved in {file_fits}---->\n') pp.close() plot_e_resolution_vs_z_r(reso_list, file_plot)
# But, binned likelihood support both extended and unextended fit. # <codecell> from probfit import Extended, BinnedChi2 seed(0) gdata = randn(10000) # <codecell> mypdf = Extended(gaussian) describe(mypdf) # just basically N*gaussian(x,mean,sigma) # <codecell> bx2 = BinnedChi2(mypdf, gdata, bound=(-3,3))#create cost function bx2.show(args={'mean':1.0, 'sigma':1.0, 'N':10000}) #another way to draw it # <codecell> m = Minuit(bx2, mean=1.0, sigma=1.0, N=1000.) m.migrad() bx2.show(m) # <markdowncell> # ####Binned Likelihood # Poisson binned log likelihood with minimum subtractacted(aka likelihood ratio). # <codecell>
from iminuit import Minuit from probfit import BinnedChi2, Extended, gaussian from matplotlib import pyplot as plt from numpy.random import randn, seed seed(0) data = randn(1000) * 2 + 1 ext_gauss = Extended(gaussian) ulh = BinnedChi2(ext_gauss, data) m = Minuit(ulh, mean=0., sigma=0.5, N=800) plt.figure(figsize=(8, 3)) plt.subplot(121) ulh.draw(m) plt.title('Before') m.migrad() # fit plt.subplot(122) ulh.draw(m) plt.title('After')