示例#1
0
    def fit_gausslin(self, x, y, dy):
        self.x = x
        self.y = y
        if dy is None:
            self.dy = np.ones_like(y)
        else:
            self.dy = dy

        self.pl = np.array([
            self.A_min(),
            self.x0_min(),
            self.sigma_min(),
            self.k0_min(),
            self.k1_min()
        ])
        self.pu = np.array([
            self.A_max(),
            self.x0_max(),
            self.sigma_max(),
            self.k0_max(),
            self.k1_max()
        ])
        self.fit_res = B.genfit(self.signal_lin_bkg,
                                self.fit_list,
                                self.x,
                                self.y,
                                y_err=self.dy,
                                bounds=(self.pl, self.pu))
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)
示例#3
0
def fit_shape(self, ts, Vt):
    # fit function
    # initialize fit function parameters
    alpha = B.Parameter(1. / self.par['decay_time'], 'alpha')
    beta = B.Parameter(1. / self.par['rise_time'], 'beta')
    x0 = B.Parameter(ts[np.argmax(Vt)], 'x0')
    #x0 = B.Parameter(self.par['position'], 'x0')
    H = B.Parameter(1., 'H')
    offset = B.Parameter(0., 'offset')  #self.offset

    # shift peak shape
    def signal(x):
        sig, y = fu.peak(x - x0(), alpha(), beta())
        return y * H() + offset()

    F = B.genfit(signal, [alpha, beta, x0, H, offset], x=ts, y=Vt)

    return F, alpha(), beta(), H(), offset(), x0()
示例#4
0
    def fit_shape(self, ts, Vt):
        """
        Fit standard peak shape to data ts, Vt

        Parameters
        ----------
        ts : numpy array (float)
            times
        Vt : numpy array (float)
            Voltages

        Returns
        -------
        F : genfit opject
            fit results.
        alpha: float
            fit parameter (1/rise_time)
        beta: float
            fit parameter. (1/decay_time)
        H: float
            fit parameter. (signal height)
        offset: float
            fit parameter (constant background)
        x0: float
            fit parameter (peak position)

        """
        alpha = B.Parameter(1. / self.decay_time, 'alpha')
        beta = B.Parameter(1. / self.rise_time, 'beta')
        x0 = B.Parameter(ts[np.argmax(Vt)], 'x0')
        H = B.Parameter(1., 'H')
        offset = B.Parameter(0., 'offset')

        # shift peak shape
        def signal(x):
            sig, y = UT.peak(x - x0(), alpha(), beta())
            return y * H() + offset()

        F = B.genfit(signal, [alpha, beta, x0, H, offset],
                     x=ts,
                     y=Vt,
                     plot_fit=False)

        return F, alpha(), beta(), H(), offset(), x0()
示例#5
0
    h = B.histo(M_inv_neighbor, bins=22)
    h_sel = h.bin_content > 0
    M = h.bin_center[h_sel]
    C = h.bin_content[h_sel]
    dC = h.bin_error[h_sel]

    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')
示例#6
0
        global r_maxis, z_maxis
        r_maxis, z_maxis = mag_axis[i]
        eff.append(views[i].get_eff(Em_func,
                                    use_all=use_all_variables,
                                    get_rate=False))
    return np.array(eff)


print('-------------------------------------------------------------------')
print('fitting  data set : ', use_data_set, ' with mode : ', model)
print('-------------------------------------------------------------------')

orbit_fit = B.genfit(S_f, current_fit_par ,\
              y = exp_data, \
              x = xv, \
              y_err = exp_err, \
              full_output=1,\
                nplot = 0, \
              ftol = 0.001, maxfev = 2000
                  )
# fitting using analytically calculated derivatives does not work well
#                  deriv = Em_func_der, \
#
# get fit values
stat = orbit_fit.stat
fitted_rate = stat['fitted values']
# get covariance matrix
if orbit_fit.covar == None:
    print("fit did not converge !' ")
    for k in list(stat.keys()):
        print('------------------------------------------------------')
        print(k, ' = ', stat[k])
示例#7
0
def tanh_bkg(x):
    return alpha() + beta()*np.tanh(gamma()*(x - delta()))
 

'''

# select fit range

##%%

#M_min = 0.10; M_max = 0.18
M_min = 0.90
M_max = 1.02
sel = (M_min < M) & (M < M_max)

fit = B.genfit(signal, [A, x0, sig, a0, a1], x=M[sel], y=C[sel], y_err=dC[sel])

# plot the fit
B.plot_line(fit.xpl, fit.ypl)
B.pl.xlabel(r'$M_{\gamma\gamma}$')
B.pl.ylabel('Counts')
B.pl.title(' Gaussian signal with linear background')
mr = np.linspace(M[0], M[-1], 1000)
B.plot_line(mr, gaus(mr))

#%% number of counts by integrating the fit function

#l = 0.935
#u = 0.980

mean = fit.parameters[1].value
示例#8
0
    # to worsen the fit
    neg_Sdl = np.empty_like(x)
    for i in x:
        Sdl_neg = views[i].Sdl < 0.
        neg_Sdl[i] = Sdl_neg.max()
        # eff[i] += -(views[i].Sdl[Sdl_neg]).sum()*penn_factor
        eff[i] += -(views[i].Sdl[Sdl_neg]).sum() * penn_factor
    if neg_Sdl.max():
        print("Neg. S values forced positive !")
    return eff


orbit_fit = B.genfit(S_f, current_fit_par ,\
                     y = exp_eff, \
                     x = xv, \
                     y_err = exp_err, \
                     full_output=1,\
                     nplot = 0, \
                     ftol = 0.001
                     )
# fitting using analytically calculated derivatives does not work well
#                  deriv = Em_func_der, \
#
# get fit values
stat = orbit_fit.stat
fit_eff = stat['fitted values']
# get covariance matrix
if orbit_fit.covar == None:
    for k in list(stat.keys()):
        print('------------------------------------------------------')
        print(k, ' = ', stat[k])
    print('------------------------------------------------------')
	normcounts.append(x)


#Defines the parameters and initial guess values
lambda1 = lt.Parameter(lambda1_start, 'Lambda1')
l1 = lt.Parameter(l1_start, 'L1')
a1 = lt.Parameter( a1_start, 'A1')
b1 = lt.Parameter( b1_start, 'B1')
alpha1 = lt.Parameter( alpha1_start, 'Alpha1')
beta1 = lt.Parameter( beta1_start, 'Beta1')
C1 = lt.Parameter( 0.0001, 'Background')

#Creates a single exponential fit
def single(x):
	return ( l1() * np.exp(-lambda1()*x) + C1())
L = lt.genfit(single, [l1, lambda1, C1], x = timescale, y = normcounts)

#Performs two fits to help find good starting values for the parameters 
#def f1(x):
#	return ( a1() * np.exp(-alpha1()*x) + C1())  
#def f2(x):
#	return ( b1() * np.exp(-beta1()*x) + C1())
#F1 = lt.genfit(f1, [a1, alpha1, C1], x = timescale, y = normcounts)
#F2 = lt.genfit(f2, [b1, beta1, C1], x = timescale, y = normcounts)

#Preserve data for later if needed
#a2 = a1.get()
#b2 = b1.get()
#alpha2 = alpha1.get()
#beta2 = beta1.get()
#C2 = C1.get()
示例#10
0
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
示例#11
0

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,
示例#12
0
alpha = B.Parameter(3000., 'alpha')
beta = B.Parameter(3000., 'beta')
gamma = B.Parameter(10., 'gamma')
delta = B.Parameter(1., 'delta')

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