def fit_histo(h2d): #define empty lists for parameters and their errors from fits A_a, x0_a, sigma_a, c0_a, b0_a = [], [], [], [], [] for i in range(h2d.nbins_y): # looping over pi0mass bins h = h2d.project_x(bins=[i]) M = h.bin_center C = h.bin_content dC = h.bin_error mr = np.linspace(M[0], M[-1], 1000) f = cf.gauss_fit() # set initial parameters for fitting f.A.set(C.max()) f.x0.set(0.956) f.sigma.set(0.01) f.b0.set(0.05) f.db0.set(0.5) f.c0.set(500) # set bounds for the fit parameters f.A_min.set(0.) f.A_max.set(1e5) f.x0_min.set(0.95) f.x0_max.set(0.96) f.sigma_min.set(0.008) f.sigma_max.set(0.016) f.c0_min.set(0.) f.c0_max.set(1e5) f.b0_min.set(0.00) f.b0_max.set(0.20) f.set_fit_list(fit=['A', 'x0', 'sigma', 'c0', 'b0']) f.fit_gaussbt(M, C, dC) # gauss peak with bernstein bkg A_a.append([f.A.value, f.A.err]) x0_a.append([f.x0.value, f.x0.err]) sigma_a.append([f.sigma.value, f.sigma.err]) b0_a.append([f.b0.value, f.b0.err]) c0_a.append([f.c0.value, f.c0.err]) plt.figure() h.plot_exp() f.plot_fit() B.plot_line(mr, f.gauss(mr)) B.plot_line(mr, f.bt_bkg(mr)) return np.array(A_a), np.array(x0_a), np.array(sigma_a), np.array( b0_a), np.array(c0_a)
def fit_draw_histo(h2d, Fit=False): A_a, x0_a, sigma_a, a0_a, a1_a, Q = [], [], [], [], [], [] for i in range(h2d.nbins_y): h_xproj = h2d.project_x(bins=[i]) M = h_xproj.bin_center #select the region to fit minimum = 0.90 maximum = 1.02 sel = (minimum < h_xproj.bin_center) & (h_xproj.bin_center < maximum) fit = B.genfit(signal, [A, x0, sig, a0, a1], x=h_xproj.bin_center[sel], y=h_xproj.bin_content[sel], y_err=h_xproj.bin_error[sel], print_results=False) #get the fit parameters #amplitude A_a.append([fit.parameters[0].value, fit.parameters[0].err]) #mean x0_a.append([fit.parameters[1].value, fit.parameters[1].value]) #sigma sigma_a.append([fit.parameters[2].value, fit.parameters[2].err]) #intercept a0_a.append([fit.parameters[3].value, fit.parameters[3].err]) #slope a1_a.append([fit.parameters[4].value, fit.parameters[4].err]) #plot the data and the fit, both peak and bkg plt.figure() h_xproj.plot_exp() mr = np.linspace(M[sel].min(), M[sel].max(), 1000) B.plot_line(fit.xpl, fit.ypl) B.plot_line(mr, gaus(mr)) B.plot_line(mr, lin_bkg(mr)) ws = gaus(fit.parameters[1].value) wb = lin_bkg(fit.parameters[1].value) # calculate q-factor q = ws / (ws + wb) Q.append(q) return np.array(A_a), np.array(x0_a), np.array(sigma_a), np.array( a0_a), np.array(a1_a), np.array(Q)
A = B.Parameter(C.max(), 'A') x0 = B.Parameter(0.956, 'x0') sigma = B.Parameter(.005, 'sigma') a0 = B.Parameter(1., 'a0') a1 = B.Parameter(1., 'a1') fit = B.genfit(signal, [A, x0, sigma, a0, a1], x=M, y=C, y_err=dC, print_results=False) if do_plot: plt.figure() h.plot_exp() B.plot_line(fit.xpl, fit.ypl) mr = np.linspace(M[0], M[-1], 1000) B.plot_line(mr, gaus(mr)) B.plot_line(mr, lin_bkg(mr)) A = B.Parameter(fit.parameters[0].value, 'A') x0 = B.Parameter(fit.parameters[1].value, 'x0') sigma = B.Parameter(fit.parameters[2].value, 'sigma') a0 = B.Parameter(fit.parameters[3].value, 'a0') a1 = B.Parameter(fit.parameters[4].value, 'a1') #x0 = fit.parameters[1].value #sigma = fit.parameters[2].value #a0 = fit.parameters[3].value #a1 = fit.parameters[4].value ws = gaus(M_inv_loc)
A_fit.A_min.set(100.) A_fit.A_max.set(3000.) A_fit.x0_min.set(0.13) A_fit.x0_max.set(0.14) A_fit.sigma_min.set(0.003) A_fit.sigma_max.set(0.03) A_fit.b0_min.set(0.5) A_fit.b0_max.set(0.9) A_fit.c0_min.set(45.) A_fit.c0_max.set(70.) A_fit.fit_gaussbt(pi0m, A_value, A_err) # fit gauss peak with linear bkg plt.figure() A_fit.plot_fit() B.plot_line(pi0m, A_fit.gauss(pi0m)) B.plot_line(pi0m, A_fit.bt_bkg(pi0m)) B.plot_exp(pi0m, A_value, A_err, plot_title='Fit the fit parameter A', x_label=' $M(\gamma\gamma)$') plt.figure() px0 = B.polyfit(pi0m, x0_value, x0_err, order=1) B.plot_exp(pi0m, x0_value, x0_err, plot_title='Fit the fit parameter $x0$', x_label='bin centers of y-axis in 2D plot $M(\pi^{0})$') B.plot_line(px0.xpl, px0.ypl)
f.c0.set(500) # set bounds for the fit parameters f.A_min.set(0.); f.A_max.set(1e5) f.x0_min.set(0.95); f.x0_max.set(0.96) f.sigma_min.set(0.008); f.sigma_max.set(0.016) f.c0_min.set(0.); f.c0_max.set(1e5) f.b0_min.set(0.00); f.b0_max.set(0.20) f.set_fit_list(fit = ['A', 'x0', 'sigma', 'c0', 'b0']) f.fit_gaussbt(M, C, dC) # gauss peak with bernstein bkg h.plot_exp() f.plot_fit() B.plot_line(mr, f.gauss(mr)) B.plot_line(mr, f.bt_bkg(mr)) #%% s = np.abs(f.gauss(metap)) b = np.abs(f.bt_bkg(metap)) qs_1d = s/(s+b) hs = B.histo( metap, bins = mep_bins, range = [mep_min, mep_max] , title = 'etaprime', xlabel = "$M(\pi^{+}\pi^{-}\eta)$", weights = qs_1d ) hb = B.histo( metap, bins = mep_bins, range = [mep_min, mep_max] , title = 'etaprime', xlabel = "$M(\pi^{+}\pi^{-}\eta)$", weights = 1 - qs_1d )
def plot_fit(self): B.plot_line(self.fit_res.xpl, self.fit_res.ypl)
# get the data into arrays t = B.get_data(mf, 'time') dexp = B.get_data(mf,'dist') derr = B.get_data(mf, 'd_err') # print the values in a for loop for i, D in enumerate(dexp): # indentation is important print 'time = ', t[i], 'distance = ', D, 'error = ', derr[i] # end of the loop = end of indentation # plot the data B.plot_exp(t, dexp, derr) # fit a line fit = B.linefit(t, dexp, derr) # draw the fit result B.plot_line(fit.xpl, fit.ypl) # add the labels B.pl.xlabel('t (sec)') B.pl.ylabel('Distance (m)') B.pl.title('Distance vs time exp.') # save the plot B.pl.savefig('my_plot.pdf') # print the covariance matrix: print "The Convariance Matrix is : ", fit.cov
phil.append(phil[0] + 360.) phil = np.array(phil) dphil = np.diff(phil) # small R data sd = B.get_file('small_R.data') rs = sd.get_data('r') zs = sd.get_data('z') phis = 0. dphis = 360. if plot_side: # plot poloidal projection for i, r in enumerate(rl): z = zl[i] B.plot_line(r, z) B.pl.xlim((0., 2.)) B.pl.ylim((-2., 2.)) B.pl.gca().set_aspect('equal') B.pl.xlabel = 'R' B.pl.ylabel = 'Z' B.pl.title = '' # inner side B.plot_line(rs, zs) else: R_mid = [] phi_mid = [] # plot mid-plane at zp
fig = plt.figure() ax = fig.add_subplot(111) plt.xlim(0, max(timescale)) plt.ylim(0, max(normcounts)+0.0002) lt.plot_exp(timescale, normcounts, marker = '|', color = 'grey', x_label = 'Decay Time ($\mu s$)', y_label = 'Probability Density ($\%$ of total detected)', plot_title = 'Muon Decay - Single Exponential Fit'), lt.plot_line(L.xpl, L.ypl, linewidth = 1.5, # linestyle = '', color = 'b', label = r'Single Exponential: $\tau$ = {0}$\mu s$'.format(round(1/lambda1r, 5))) handles, labels = ax.get_legend_handles_labels() ax.legend(handles, labels) plt.savefig('graph_{0}_SnglExp.png'.format(filename)) #Plot 2 - Double Exponential Fit V. Single Exponential Fit fig = plt.figure() ax = fig.add_subplot(111) plt.xlim(0, max(timescale))
sigma_strobe = 0.05 sigma_B = np.sqrt((((mu_naught / (2 * np.pi * R)) * df1['Sigmai(A)'])**2) + (((-mu_naught * 3.00) / (2 * np.pi * ((R)**2)) * sigma_R)**2)) sigma_ang_vel = np.sqrt(sigma_strobe) sigma_Imoment = (2 / 5) * np.sqrt(((((rBall)**2) * sigma_mBall)**2) + ((2 * (rBall * mBall * sigma_rBall))**2)) sigma_L = np.sqrt(((ang_vel * sigma_Imoment)**2) + ((moment_I * sigma_ang_vel)**2)) sigma_mu = np.sqrt((((1 / BoverL) * sigma_omega)**2) + (((-omega * L / ((0.002541)**2)) * sigma_B)**2) + (((omega / 0.002541) * sigma_L)**2)) errorbar = sigma_omega B.plot_exp(BoverL, omega, 0.008) B.pl.title("Precesional Frequency over B/L") B.pl.ylabel("Frequency (HZ)") B.pl.xlabel("B/L") fit2 = B.linefit(BoverL, omega) B.plot_line(fit2.xpl, fit2.ypl) B.pl.show()
miu = param.Parameter(mean, 'mu') sigma = param.Parameter(SD, 'sigma') height = param.Parameter(700., 'height') factor = len(box) * (8. / 40.) def gauss(x): gaussgraph = stats.norm.pdf((x - miu()) / sigma()) * factor return gaussgraph fit = gen.genfit(gauss, [height, miu, sigma], x=graphx, y=graphy) B.plot_line(fit.xpl, fit.ypl, color='yellow', linewidth=2) graph.plot() graph.fit() B.pl.show() #ignore below _____________________________________________________________________________________________________________________________ # def generateData(length): # x,y = [],[] # for _ in range(length): # x.append(random.random()) # y.append(random.random()) # randomData = { # 'x':x,
import LT_Fit.gen_fit as G df = pd.read_csv( 'C:\\Users\\iw2ba\\Desktop\\Senior Physics\\NMR\\T1A7.18B3.54.csv') DIF = df['Voltage(V)'] tao = df['tao(s)'] y = np.log(0.5 * (1 - (DIF / 4.055432285469148))) B.plot_exp(tao, y, 0.02) B.pl.title('Spin Echo VS Tau Linear') B.pl.ylabel('Spin Echo (v)') B.pl.xlabel('Tau(s)') fit1 = B.linefit(tao, y) B.plot_line(fit1.xpl, fit1.ypl) slope = fit1.par[1] T1 = -1 / slope print(T1) print(1 / 0.26248527624448215) # M0 = P.Parameter(4.055432285469148, 'M0') # T1 = P.Parameter(0.04900121822053086, 'T1') # c1= P.Parameter (0,'c1') # fit1 = B.polyfit(tao, DIF, 0.02) # B.plot_line(fit1.xpl, fit1.ypl, color='green') # def myfun(x): # value = M0()*(1-2*np.exp(-(x/T1())))+c1()
def fit_histo(epm=True): #define empty lists for parameters and their errors from fits Aa = [] Aa_err = [] X0 = [] X0_err = [] S = [] S_err = [] Al = [] Al_err = [] Be = [] Be_err = [] Ga = [] Ga_err = [] De = [] De_err = [] for i in range(h_epi0.bin_content.shape[1]): h = h_epi0.project_x(bins=[i]) M = h.bin_center C = h.bin_content dC = h.bin_error fit = B.genfit(signal, [Ae, x0e, se, alpha, beta, gamma, delta], x=M, y=C, y_err=dC) if i == 9: plt.figure() #B.plot_exp(M, C, dC, x_label = '$M(\pi^{+}\pi^{-}\eta)$', plot_title = 'Gaussian peak on Tanh bkg ') h.plot_exp() B.plot_line(fit.xpl, fit.ypl) #fit.parameters_sav[0].value #fit.parameters_sav[0].err fit.save_parameters() Aa.append(fit.fit_result.x[0]) Aa_err.append(fit.parameters_sav[0].err) X0.append(fit.fit_result.x[1]) X0_err.append(fit.parameters_sav[1].err) S.append(fit.fit_result.x[2]) S_err.append(fit.parameters_sav[2].err) Al.append(fit.fit_result.x[3]) Al_err.append(fit.parameters_sav[3].err) Be.append(fit.fit_result.x[4]) Be_err.append(fit.parameters_sav[4].err) Ga.append(fit.fit_result.x[5]) Ga_err.append(fit.parameters_sav[5].err) De.append(fit.fit_result.x[6]) De_err.append(fit.parameters_sav[6].err) Aa = np.array(Aa) Aa_err = np.array(Aa_err) X0 = np.array(X0) X0_err = np.array(X0_err) S = np.array(S) S_err = np.array(S_err) Al = np.array(Al) Al_err = np.array(Al_err) Be = np.array(Be) Be_err = np.array(Be_err) Ga = np.array(Ga) Ga_err = np.array(Ga_err) De = np.array(De) De_err = np.array(De_err) return Aa, X0, S, Al, Be, Ga, De, Aa_err, X0_err, S_err, Al_err, Be_err, Ga_err, De_err
a0 = B.Parameter(0., 'a0') a1 = B.Parameter(0., 'a1') #a2 = B.Parameter(0., 'a2') def lin_bkgp(x): return a0() + a1() * x def signal_p(x): return gaus_p(x) + lin_bkgp(x) fit_Aa = B.genfit(signal_p, [A, x0, sig, a0, a1], x=pi0m, y=Aa, y_err=Aa_err) plt.figure() B.plot_line(fit_Aa.xpl, fit_Aa.ypl) #B.plot_line(pi0m, gaus_p(pi0m)) #B.plot_line(pi0m, lin_bkgp(pi0m)) B.plot_exp(pi0m, Aa, Aa_err, plot_title='Fit the fit parameter A', x_label=' $M(\gamma\gamma)$') plt.figure() pM = B.polyfit(pi0m, X0, X0_err, order=1) B.plot_exp(pi0m, X0, X0_err, plot_title='Fit the fit parameter $mean$', x_label='bin centers of y-axis in 2D plot $M(\pi^{0})$')
def tanh_bkg(x): return alpha() + beta()*np.tanh(gamma()*(x - delta())) def signal(x): return tanh_bkg(x) + gaus_peak(x) M = hep.bin_center C = hep.bin_content dC = hep.bin_error fit = B.genfit(signal, [ A, x0, s, alpha, beta, gamma, delta], x = M, y = C, y_err = dC ) plt.figure() B.plot_exp(M, C, dC, x_label = '$M(\pi^{+}\pi^{-}\eta)$', plot_title = 'Gaussian peak on Tanh bkg ') B.plot_line(fit.xpl, fit.ypl) fit.save_parameters() al = fit.parameters_sav[3].value be = fit.parameters_sav[4].value ga = fit.parameters_sav[5].value de = fit.parameters_sav[6].value def etap_bkg(x): return al + be*np.tanh(ga*(x - de))