コード例 #1
0
def drawChi2Dist(title="", chi2DistDict=[], legend=[]):
    #differnt injection
    ext = ".pdf"
    nList = []
    binsList = []
    pathcesList = []
    color = ['g', 'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w']
    i = 0
    with Canvas("Testchi2.pdf") as can:
        for tag in legend:
            pseudoTag = tag + "Pseudo"
            print("chi2DistDict[tag]: ", chi2DistDict[pseudoTag])
            print("color: ", color[i])
            #n, bins, patches, = plt.hist(chi2DistDict[pseudoTag], 50, normed=1, facecolor=color[i], alpha=0.75, label=legend[i])
            can.ax.hist(chi2DistDict[pseudoTag],
                        bins=20,
                        facecolor=color[i],
                        alpha=0.75,
                        label=legend[i])
            can.ax.set_xlim(0, 3)
            #bins = np.histogram(chi2DistDict[pseudotag], edges)

            #nList.append(n)
            #binsList.append(bins)
            #patchesList.append(patches)
            i = i + 1

        can.ax.legend(framealpha=0)
        can.save("testchi2.pdf")
コード例 #2
0
def drawSignalGaussianFit(signalBkgDataSet,
                          asignalDataSet,
                          doLog=False,
                          title=""):
    #drawing the signal
    ext = ".pdf"
    title = [s + "SignalGaussianFit" for s in title]
    with Canvas(f'%s{ext}' % title, "Gaussian Fit Sig", "", "", 2) as can:
        can.ax.errorbar(signalBkgDataSet.xData,
                        asignalDataSet.ySigData,
                        yerr=signalBkgDataSet.yerrData,
                        fmt='.g',
                        label="signal MC injected")  # drawing the points
        can.ax.set_ylim(0.1, 10000.0)
        if doLog:
            can.ax.set_yscale('log')
        can.ax.plot(signalBkgDataSet.xData,
                    asignalDataSet.yGaussianFit,
                    '-r',
                    label="Signal Gaussian Fit")
        can.ax.legend(framealpha=0)
        ##
        #can.ratio.stem(signalBkgDataSet.xData, asignalDataSet.gaussianFitSignificance, markerfmt='.', basefmt=' ')
        can.ratio[0].axhline(0, linewidth=1, alpha=0.5)
        #can.ax.plot(xSB, ymuGP_KernBkg_SB, '-g', label="GP bkgnd kernel") #drawing
        can.save(title)
コード例 #3
0
def drawSignalSubtractionFit(signalBkgDataSet,
                             ySignalData,
                             ySignalFit,
                             ySignalFitSignificance,
                             doLog=False,
                             title=""):
    title = title + "signalGPfit"
    #drawing the signal
    ext = ".pdf"
    with Canvas(f'%s{ext}' % title, "GPSig-GPBkgFit Sig", "", "", 2) as can:
        can.ax.errorbar(signalBkgDataSet.xData,
                        ySignalData,
                        yerr=signalBkgDataSet.yerrData,
                        fmt='.g',
                        label="signal MC injected")  # drawing the points
        can.ax.set_ylim(0.1, 10000.0)
        if doLog:
            can.ax.set_yscale('log')
        can.ax.plot(signalBkgDataSet.xData,
                    ySignalFit,
                    '-r',
                    label="GP Sig + bkg Kernel fit  - GP bkgnd kernel fit")
        can.ax.legend(framealpha=0)
        ##
        can.ratio[0].stem(signalBkgDataSet.xData,
                          ySignalFitSignificance,
                          markerfmt='.',
                          basefmt=' ')
        can.ratio[0].axhline(0, linewidth=1, alpha=0.5)
        #can.ax.plot(xSB, ymuGP_KernBkg_SB, '-g', label="GP bkgnd kernel") #drawing
        can.save(title)
コード例 #4
0
def drawAllSignalFit(signalBkgDataSet,
                     asignalDataSet,
                     doLog=False,
                     saveTxt=False,
                     title=""):
    #drawing the signal
    ext = ".pdf"
    title = title + "allSignalFit"
    with Canvas(f'%s{ext}' % title, "All Signal Fits", "", "", 2) as can:
        can.ax.errorbar(signalBkgDataSet.xData,
                        asignalDataSet.ySigData,
                        yerr=signalBkgDataSet.yerrData,
                        fmt='.k',
                        label="signal MC injected")  # drawing the points
        can.ax.set_ylim(0.1, 15000.0)
        if doLog:
            can.ax.set_yscale('log')
        can.ax.plot(signalBkgDataSet.xData,
                    asignalDataSet.yGaussianFit,
                    '-r',
                    label="Signal point Gaussian Fit(injected signal)")
        can.ax.plot(signalBkgDataSet.xData,
                    asignalDataSet.yGPSubtractionFit,
                    '-g',
                    label="Signal GP Fit subtraction")
        can.ax.plot(signalBkgDataSet.xData,
                    signalBkgDataSet.MAP_sig,
                    '-b',
                    label="Signal GP reconstructed Fit")
        #can.axplot(signalBkgDataset.xData, asignalDataSet.yReconsturcted)
        can.ax.legend(framealpha=0)
        #can.ratio.stem(signalBkgDataSet.xData, asignalDataSet.gaussianFitSignificance, markerfmt='.', basefmt=' ')
        can.ratio[0].axhline(0, linewidth=1, alpha=0.5)
        #can.ax.plot(xSB, ymuGP_KernBkg_SB, '-g', label="GP bkgnd kernel") #drawing
        can.save(title)
コード例 #5
0
def drawAllSignalFitYvonne(signalBkgDataSet,
                           asignalDataSet,
                           doLog=False,
                           saveTxt=False,
                           title="",
                           significanceSig=None,
                           sig=None):
    #drawing the signal
    ext = ".pdf"
    title = title + "allSignalFit"
    with Canvas(f'%s{ext}' % title, "All Signal Fits", "", "", 2) as can:
        can.ax.errorbar(signalBkgDataSet.xData,
                        asignalDataSet.ySigData,
                        yerr=signalBkgDataSet.yerrData,
                        fmt='.k',
                        label="signal MC injected")  # drawing the points
        can.ax.set_ylim(0.1, 15000.0)
        if doLog:
            can.ax.set_yscale('log')
        can.ax.plot(signalBkgDataSet.xData,
                    asignalDataSet.yGaussianFit,
                    '-r',
                    label="Signal point Gaussian Fit(injected signal)")
        can.ax.plot(signalBkgDataSet.xData,
                    asignalDataSet.yGPSubtractionFit,
                    '-g',
                    label="Signal GP Fit subtraction")
        #can.ax.plot(signalBkgDataSet.xData, asignalDataSet.sig['Gaussian'],'-m', label="Signal GP Kernel reconstructed Gaussian Fit")
        print(asignalDataSet.sig['custom'])
        can.ax.plot(signalBkgDataSet.xData,
                    asignalDataSet.sig['custom'],
                    '-b',
                    label="Signal GP Kernel reconstructed signal template Fit")
        #can.ax.plot(signalBkgDataSet.xData, asignalDataSet.sig['customTest'],'-k', label="Signal GP Kernel reconstructed signal template Fit default")
        #accidentally broke gaussian signal
        #print both signal
        #        if sig:
        #            for i in range(len(sig)):
        #                print("i", i)
        #                can.ratio[i].stem(signalBkgDataSet.xData, significanceSig[i], markerfmt='.', basefmt=' ')
        #                can.ratio[i].set_ylabel(sig[i])
        #                print("legend: ", sig[i])
        #                can.ratio[i].axhline(0, linewidth=1, alpha=0.5)
        #                can.ax.legend(framealpha=0)

        can.ratio[1].stem(signalBkgDataSet.xData,
                          significanceSig[1],
                          markerfmt='.',
                          basefmt=' ')
        can.ratio[1].set_ylabel(sig[1])
        print("legend: ", sig[1])
        can.ratio[1].axhline(0, linewidth=1, alpha=0.5)
        can.ax.legend(framealpha=0)
        #can.ratio.stem(signalBkgDataSet.xData, asignalDataSet.gaussianFitSignificance, markerfmt='.', basefmt=' ')
        #can.ratio.axhline(0, linewidth=1, alpha=0.5)
        #can.ax.plot(xSB, ymuGP_KernBkg_SB, '-g', label="GP bkgnd kernel") #drawing
        can.save("results/" + title)
コード例 #6
0
def run():
    args = parse_args()
    ext = args.output_file_extension
    from pygp.canvas import Canvas

    with File(args.input_file, 'r') as h5file:
        x, y, xerr, yerr = get_xy_pts(
            h5file['dijetgamma_g85_2j65']['Zprime_mjj_var'], FIT_RANGE)

    if args.load_pars:
        with open(args.load_pars, 'r') as pars_file:
            best_fit = json.load(pars_file)
    else:
        lnProb = logLike_minuit(x, y, yerr)
        _, best_fit = fit_gp_minuit(args.n_fits, lnProb)
    if args.save_pars:
        with open(args.save_pars, 'w') as pars_file:
            json.dump(best_fit, pars_file, indent=2)

    fit_pars = [best_fit[x] for x in FIT_PARS]
    with Canvas(f'points{ext}') as can:
        can.ax.errorbar(x, y, yerr=yerr, fmt='.')
        can.ax.set_yscale('log')
    kargs = {x: y for x, y in best_fit.items() if x not in FIT_PARS}
    kernel_new = get_kernel(**kargs)
    gp_new = george.GP(kernel_new, mean=Mean(fit_pars), fit_mean=True)

    ext = args.output_file_extension
    gp_new.compute(x, yerr)
    plot_gp(x, y, xerr, yerr, gp_new, name=f'spectrum{ext}')

    if not args.signal_file:
        return

    with File(args.signal_file, 'r') as h5file:
        x_sig, y_sig, xerr_sig, yerr_sig = get_xy_pts(
            h5file['dijetgamma_g85_2j65']['Zprime_mjj_var'], FIT_RANGE)
        y_sig *= args.signal_multiplier
        yerr_sig *= args.signal_multiplier
        tot_error = (yerr**2 + yerr_sig**2)**(1 / 2)
        plot_gp(x_sig,
                y + y_sig,
                xerr_sig,
                tot_error,
                gp_new,
                name=f'with-signal{ext}',
                y_bg=y,
                yerr_bg=yerr,
                signal_multiplier=args.signal_multiplier)
コード例 #7
0
def plot_gp(x,
            y,
            xerr,
            yerr,
            gp_new,
            name,
            y_bg=None,
            yerr_bg=None,
            signal_multiplier=1.0):
    from pygp.canvas import Canvas
    t = np.linspace(np.min(x), np.max(x), 500)
    mu, cov = gp_new.predict(y, t)
    mu_x, cov_x = gp_new.predict(y, x)
    signif = (y - mu_x) / np.sqrt(np.diag(cov_x) + yerr**2)
    fit_mean_smooth = gp_new.mean.get_value(t)
    std = np.sqrt(np.diag(cov))

    with Canvas(name) as can:
        sm = signal_multiplier
        sig_label = f'sig * {sm:.3g} + bg' if sm != 1.0 else 'sig + bg'
        can.ax.errorbar(x, y, yerr=yerr, fmt='.', label=sig_label)
        if y_bg is not None:
            can.ax.errorbar(x, y_bg, yerr=yerr_bg, fmt='.', label='bg')
        can.ax.set_yscale('log')
        can.ax.plot(t, mu, '-r')
        # can.ax.plot(t, fit_mean_smooth, '-k', label='fit function')
        can.ax.fill_between(t,
                            mu - std,
                            mu + std,
                            facecolor=(0, 1, 0, 0.5),
                            zorder=5,
                            label=r'GP error = $1\sigma$')
        can.ax.legend(framealpha=0)
        can.ax.set_ylabel('events')
        can.ratio.stem(x, signif, markerfmt='.', basefmt=' ')
        can.ratio.axhline(0, linewidth=1, alpha=0.5)
        can.ratio.set_xlabel(r'$m_{jj}$ [GeV]', ha='right', x=0.98)
        can.ratio.set_ylabel('significance')
コード例 #8
0
def drawFit2(xData=None,
             yerr=None,
             yData=None,
             yFit=None,
             yFit2=None,
             sig=None,
             signiLegend=None,
             title=None,
             saveTxt=False,
             saveTxtDir=None):
    # draw the data set using diffrent fits
    ext = ".pdf"
    with Canvas(f'%s{ext}' % title, "UA2", "", 2) as can:
        can.ax.errorbar(xData, yData, yerr, fmt='.g',
                        label="datapoints")  # drawing the points
        can.ax.set_yscale('log')
        can.ax.plot(xData, yFit, '-g', label="bkgndKernelFit")  #drawing
        can.ax.plot(xData, yFit2, '-r', label="GP background+signal")  #drawing
        if saveTxtDir == None:
            saveTxtDir = ""
        with open(saveTxtDir + "dataPoints.txt", "w") as f0:
            writer = csv.writer(f0, delimiter="\t")
            writer.writerows(zip(xData, yData))
        with open(saveTxtDir + "bkgndReconstructed.txt", "w") as f0:
            writer = csv.writer(f0, delimiter="\t")
            writer.writerows(zip(xData, yFit))
        with open(saveTxtDir + "bkgndSigReconstructed.txt", "w") as f0:
            writer = csv.writer(f0, delimiter="\t")
            writer.writerows(zip(xData, yFit2))
        can.ax.legend(framealpha=0)
        if sig:
            for i in range(len(sig)):
                print("i", i)
                can.ratio[i].stem(xData, sig[i], markerfmt='.', basefmt=' ')
                if signiLegend != None:
                    can.ratio[i].set_ylabel(signiLegend[i])
                can.ratio[i].axhline(0, linewidth=1, alpha=0.5)
        can.save("results/" + title)
コード例 #9
0
def drawFit(xData=None,
            yerr=None,
            yData=None,
            yFit=None,
            sig=None,
            title=None,
            saveTxt=False,
            saveTxtDir=None):
    # draw the data set using diffrent fits
    ext = ".pdf"
    with Canvas(f'%s{ext}' % title, "UA2", "", 2) as can:
        can.ax.errorbar(xData, yData, yerr, fmt='.g',
                        label="datapoints")  # drawing the points
        can.ax.set_yscale('log')
        can.ax.plot(xData, yFit, '-g', label="UA2Fit")  #drawing
        #ratio plot:
        print("x: ", xData)
        print("sig: ", sig)
        if sig:
            can.ratio[0].stem(xData, sig, markerfmt='.', basefmt=' ')
            can.ratio[0].set_ylabel("significance")
            can.ratio[0].axhline(0, linewidth=1, alpha=0.5)
        can.save(title)
コード例 #10
0
def drawFit3(xData=None,
             yerr=None,
             yData=None,
             yFit=None,
             yFit2=None,
             yFit3=None,
             legend=[],
             sigList=None,
             title=None,
             saveTxt=False,
             saveTxtDir=None):
    # draw the data set using diffrent fits
    ext = ".pdf"
    with Canvas(f'%s{ext}' % title, "UA2", "", 2) as can:
        can.ax.errorbar(xData, yData, yerr, fmt='.g',
                        label="datapoints")  # drawing the points
        can.ax.set_yscale('log')
        if legend == []:
            legend = [
                "bkgKernelFit", "GP bakcground +signal kernel fit",
                "GP backgtound+signal fit -custom signal template reconsturct"
            ]

        can.ax.plot(xData, yFit, '-g', label=legend[0])  #drawing
        can.ax.plot(xData, yFit2, '-r', label=legend[1])  #drawing
        can.ax.plot(xData, yFit3, '-b', label=legend[2])  #drawing
        #ratio plot:
        #print("x: ", xData)
        #print("sig: ", sig)

        can.ax.legend(framealpha=0)
        i = 0
        for sig in sigList:
            can.ratio[i].stem(xData, sig[i], markerfmt='.', basefmt=' ')
            can.ratio[i].set_ylabel("significance")
            can.ratio[i].axhline(0, linewidth=1, alpha=0.5)
        can.save(title)
コード例 #11
0
def drawSignalReconstructed(signalBkgDataSet,
                            asignalDataSet,
                            doLog=False,
                            title=""):
    #drawing the signal with meghan's method
    ext = args.output_file_extension
    title = title + "signalonlyMeghan"
    with Canvas(f'%s{ext}' % title, "GPSig-GPSBFit Sig", "", "", 2) as can:
        can.ax.errorbar(xSB,
                        ySig,
                        yerr=yerrSB,
                        fmt='.g',
                        label="signal MC injected")  # drawing the points
        can.ax.set_ylim(0.1, 6000000)
        can.ax.set_yscale('log')
        can.ax.plot(xSB, MAP_bkg, '-r', label="GP Sig Kernel Only fit")
        can.ax.legend(framealpha=0)
        can.ratio[0].stem(xSB,
                          GPSigOnlySignificanceMeg,
                          markerfmt='.',
                          basefmt=' ')
        can.ratio[0].axhline(0, linewidth=1, alpha=0.5)
        #can.ax.plot(xSB, ymuGP_KernBkg_SB, '-g', label="GP bkgnd kernel") #drawing
        can.save(title)
コード例 #12
0
ファイル: plot-meghan.py プロジェクト: Yvonne-Ng/GP
def run_vs_3paramFit():
    args = parse_args()
    ext = args.output_file_extension
    from pygp.canvas import Canvas
    xMinFit = 300
    # Getting data points
    xRaw, yRaw, xerrRaw, yerrRaw = getDataPoints(args.input_file,
                                                 'dijetgamma_g85_2j65',
                                                 'Zprime_mjj_var')
    xSig, ySig, xerrSig, yerrSig = getDataPoints(args.signal_file,
                                                 'dijetgamma_g85_2j65',
                                                 'Zprime_mjj_var')

    #Data processing, cutting out the not desired range
    x, y, xerr, yerr = dataCut(xMinFit, 1500, 0, xRaw, yRaw, xerrRaw, yerrRaw)
    xFit, yFit, xerrFit, yerrFit = dataCut(xMinFit, 1500, 0, xRaw, yRaw,
                                           xerrRaw, yerrRaw)

    # make an evently spaced x
    t = np.linspace(np.min(x), np.max(x), 500)

    #calculating the log-likihood and minimizing for the gp
    lnProb = logLike_minuit(x, y, xerr)

    #
    #        idecay = np.random.random() * 0.64
    #        ilength = np.random.random() * 5e5
    #        ipower = np.random.random() * 1.0
    #        isub = np.random.random() * 1.0
    #        'amp': 5701461179.0,
    #        'p0': 0.23,
    #        'p1': 0.46,
    #        'p2': 0.89
    #
    #dan crap initial guess->returns infiinity
    print("prob:", lnProb(5701461179.0, 0.64, 5e5, 1.0, 1.0, 0.23, 0.46, 0.89))
    print(
        "prob:",
        lnProb(7894738685.23, 91.507809530688036, 152892.47486888882,
               0.77936430405302681, 393.57106174440003, 0.46722747265429021,
               0.92514297129196166, 1.7803224928740065))

    #def __call__(self, Amp, decay, length, power, sub, p0, p1, p2):

    min_likelihood, best_fit = fit_gp_minuit(10, lnProb)

    fit_pars = [best_fit[x] for x in FIT3_PARS]

    #making the GP
    kargs = {x: y for x, y in best_fit.items() if x not in FIT3_PARS}
    kernel_new = get_kernel(**kargs)
    print(kernel_new.get_parameter_names())
    #making the kernel
    gp_new = george.GP(kernel_new, mean=Mean(fit_pars), fit_mean=True)
    gp_new.compute(x, yerr)
    mu, cov = gp_new.predict(y, t)
    mu_x, cov_x = gp_new.predict(y, x)
    # calculating the fit function value

    #GP compute minimizes the log likelihood of the best_fit function
    best = [best_fit[x] for x in FIT3_PARS]
    print("best param meghan GP minmization:", best)
    meanFromGPFit = Mean(best)

    fit_meanM = meanFromGPFit.get_value(
        x, xerr)  #so this is currently shit. can't get the xErr thing working
    print("fit_meanM:", fit_meanM)

    #fit_mean_smooth = gp_new.mean.get_value(t)

    #----3 param fit function in a different way
    lnProb = logLike_3ff(xFit, yFit, xerrFit)
    minimumLLH, best_fit_params = fit_3ff(100, lnProb)
    fit_mean = model_3param(xFit, best_fit_params, xerrFit)

    ##----4 param fit function
    #lnProb = logLike_4ff(xFit,yFit,xerrFit)
    #minimumLLH, best_fit_params = fit_4ff(100, lnProb)
    #fit_mean = model_4param(xFit, best_fit_params, xerrFit)

    #calculating significance
    signif = significance(mu_x, y, cov_x, yerr)

    initialCutPos = np.argmax(x > xMinFit)

    #sigFit = (fit_mean - y[initialCutPos:]) / np.sqrt(np.diag(cov_x[initialCutPos:]) + yerr[initialCutPos:]**2)
    sigFit = significance(fit_mean, y[initialCutPos:], cov_x[initialCutPos:],
                          yerr[initialCutPos:])
    #sigit = (mu_x)
    std = np.sqrt(np.diag(cov))

    ext = args.output_file_extension
    title = "test"
    with Canvas(f'%s{ext}' % title) as can:
        can.ax.errorbar(x, y, yerr=yerr, fmt='.')
        can.ax.set_yscale('log')
        # can.ax.set_ylim(1, can.ax.get_ylim()[1])
        can.ax.plot(t, mu, '-r')
        can.ax.plot(xFit, fit_mean, '-b')
        #this only works with the xErr part of Mean commented out
        #can.ax.plot(x, fit_meanM, '.g')
        # can.ax.plot(t, fit_mean_smooth, '--b')
        #can.ax.fill_between(t, mu - std, mu + std,
        #facecolor=(0, 1, 0, 0.5),
        #zorder=5, label='err = 1')
        #can.ratio.stem(x, signif, markerfmt='.', basefmt=' ')
        can.ratio.stem(xFit, sigFit, markerfmt='.', basefmt=' ')
        can.ratio.axhline(0, linewidth=1, alpha=0.5)
        can.ratio2.stem(x, signif, markerfmt='.', basefmt=' ')
        can.save(title)
コード例 #13
0
ファイル: plot-meghan.py プロジェクト: Yvonne-Ng/GP
def run_bkgndVswithSig_GP():
    args = parse_args()
    ext = args.output_file_extension
    from pygp.canvas import Canvas

    mass = 750
    # Getting data points
    xRaw, yRaw, xerrRaw, yerrRaw = getDataPoints(args.input_file,
                                                 'dijetgamma_g85_2j65',
                                                 'Zprime_mjj_var')
    xSig, ySig, xerrSig, yerrSig = getDataPoints(args.signal_file,
                                                 'dijetgamma_g85_2j65',
                                                 'Zprime_mjj_var')

    #Data processing, cutting out the not desired range
    xBkg, yBkg, xerrBkg, yerrBkg = dataCut(300, 1500, 0, xRaw, yRaw, xerrRaw,
                                           yerrRaw)
    xSig, ySig, xerrSig, yerrSig = dataCut(300, 1500, 0, xSig, ySig, xerrSig,
                                           yerrSig)

    # make an evently spaced x
    t = np.linspace(np.min(xBkg), np.max(xBkg), 500)

    #Drawing the raw data of just the bkgnd and Sig + bkgnd
    with Canvas(f'compareRawBkgndAndBkgndWithSig{ext}') as can:
        can.ax.errorbar(xBkg, yBkg, yerr=yerrBkg, fmt='.g')
        can.ax.errorbar(xSig, ySig, yerr=yerrSig, fmt='.r')
        can.ax.set_yscale('log')

    #bkgnd calculating the log-likihood and minimizing for the gp
    lnProbBkg = logLike_minuit(xBkg, yBkg, xerrBkg)
    min_likelihood, best_fit = fit_gp_minuit(100, lnProbBkg)
    print(best_fit)

    fit_pars = [best_fit[x] for x in FIT3_PARS]

    #making the GP
    kargs = {
        xBkg: yBkg
        for xBkg, yBkg in best_fit.items() if xBkg not in FIT3_PARS
    }
    kernel_Bkg = get_kernel(**kargs)
    print(kernel_Bkg.get_parameter_names())
    #making the kernel
    gp_Bkg = george.GP(kernel_Bkg, mean=Mean(fit_pars), fit_mean=True)
    gp_Bkg.compute(xBkg, yerrBkg)
    muBkg, covBkg = gp_Bkg.predict(yBkg, t)
    mu_xBkg, cov_xBkg = gp_Bkg.predict(yBkg, xBkg)

    #signal calulating the log_likihood and minizming for the GP
    # calculating the fit function value
    lnProbSig = logLike_gp_fitgpsig(xSig, ySig, xerrSig)
    min_likelihoodSig, best_fitSig = fit_gp_fitgpsig_minuit(lnProbSig)

    fit_pars = [best_fit[xSig] for xSig in FIT3_PARS]

    Args = kargs + best_fitSig
    kernel_Sig = get_kernelXtreme(**Arg)
    print(kernel_Sig.get_parameter_names())
    #making the kernel
    gp_sig = george.GP(kernel_Sig, mean=Mean(fit_pars), fit_mean=True)
    gp_sig.compute(xSig, yerrSig)
    muSig, covSig = gp_sig.predict(ySig, t)
    mu_xSig, cov_xSig = gp_Sig.predict(ySig, xSig)
    #GP compute minimizes the log likelihood of the best_fit function
    best = [best_fit[x] for x in FIT3_PARS]

    #calculating significance
    signBkg = significance(mu_xBkg, yBkg, cov_xBkg, yerrBkg)

    #sigFit = (fit_mean - y[initialCutPos:]) / np.sqrt(np.diag(cov_x[initialCutPos:]) + yerr[initialCutPos:]**2)
    signSig = significance(mu_xSig, ySig, cov_xSig, yerrSig)
    #sigit = (mu_x)
    std = np.sqrt(np.diag(cov_xBkg))

    ext = args.output_file_extension
    with Canvas(f'compareGPvsSignal{ext}') as can:
        can.ax.errorbar(xBkg, yBkg, yerr=yerrBkg, fmt='.g')
        can.ax.set_yscale('log')
        # can.ax.set_ylim(1, can.ax.get_ylim()[1])
        can.ax.plot(xBkg, muSig, '-g')
        can.ax.plot(xSig, muSig, '-r')
        #this only works with the xErr part of Mean commented out
        #can.ax.plot(x, fit_meanM, '.g')
        # can.ax.plot(t, fit_mean_smooth, '--b')
        #can.ax.fill_between(t, mubkg - std, mu + std,
        #facecolor=(0, 1, 0, 0.5),
        #zorder=5, label='err = 1')
        can.ratio.stem(xBkg, signBkg, markerfmt='.', basefmt=' ')
        can.ratio2.stem(xSig, signSig, markerfmt='.', basefmt=' ')
        can.ratio.axhline(0, linewidth=1, alpha=0.5)
コード例 #14
0
ファイル: plot-meghan.py プロジェクト: Yvonne-Ng/GP
def run_bkgnd():
    xMin = 300
    xMax = 1500
    xMinFit = 300
    xMaxFit = 1500

    args = parse_args()
    ext = args.output_file_extension
    from pygp.canvas import Canvas

    # Getting data points
    xRaw, yRaw, xerrRaw, yerrRaw = getDataPoints(args.input_file,
                                                 'dijetgamma_g85_2j65',
                                                 'Zprime_mjj_var')

    #Data processing, cutting out the not desired range
    xBkg, yBkg, xerrBkg, yerrBkg = dataCut(
        xMin, xMax, 0, xRaw, yRaw, xerrRaw,
        yerrRaw)  # for GP bkgnd kernel and signal kernel
    xBkgFit, yBkgFit, xerrBkgFit, yerrBkgFit = dataCut(
        xMinFit, xMaxFit, 0, xRaw, yRaw, xerrRaw, yerrRaw)  # for fit function

    # make an evently spaced x
    t = np.linspace(np.min(xBkg), np.max(xBkg), 500)

    #Drawing the raw data of just the bkgnd and Sig + bkgnd
    with Canvas(
            f'RawBkgnd') as can:  # just to check and see everything is okay
        can.ax.errorbar(xBkg, yBkg, yerr=yerrBkg, fmt='.g')
        can.ax.set_yscale('log')

    #GP bkgnd calculating the log-likihood and minimizing for the gp
    lnProbBkg = logLike_minuit(xBkg, yBkg, xerrBkg)
    min_likelihood, best_fit = fit_gp_minuit(10, lnProbBkg)
    print(best_fit)

    fit_pars = [best_fit[x] for x in FIT3_PARS]

    #making the GP
    kargs = {
        xBkg: yBkg
        for xBkg, yBkg in best_fit.items() if xBkg not in FIT3_PARS
    }
    kernel_Bkg = get_kernel(**kargs)
    print(kernel_Bkg.get_parameter_names())
    #making the kernel
    gp_Bkg = george.GP(kernel_Bkg, mean=Mean(fit_pars), fit_mean=True)
    gp_Bkg.compute(xBkg, yerrBkg)
    #muBkg, covBkg = gp_Bkg.predict(yBkg, t)
    mu_xBkg, cov_xBkg = gp_Bkg.predict(yBkg, xBkg)

    #finding the fit y values
    fit_mean = y_bestFit3Params(xBkgFit, yBkgFit, xerrBkgFit, 10)
    ext = args.output_file_extension
    title = "test"
    with Canvas(f'%s{ext}' % title, "Fit Function", "GP bkgnd kernel",
                "GP signal+bkgnd kernel", 3) as can:
        can.ax.errorbar(xBkg, yBkg, yerr=yerrBkg,
                        fmt='.g')  # drawing the points
        can.ax.set_yscale('log')
        can.ax.plot(xBkg, mu_xBkg, '-g')  #drawing
        can.ax.plot(xBkgFit, fit_mean, '-r')
        #can.ax.xticks(xBkg)
        #this only works with the xErr part of Mean commented out
        #can.ax.plot(x, fit_meanM, '.g')
        # can.ax.plot(t, fit_mean_smooth, '--b')
        #can.ax.fill_between(t, mubkg - std, mu + std,
        #facecolor=(0, 1, 0, 0.5),
        #zorder=5, label='err = 1')
        #can.ratio.stem(xBkg, signBkg, markerfmt='.', basefmt=' ')
        #can.ratio2.stem(xSig, signSig, markerfmt='.', basefmt=' ')
        can.ratio.axhline(0, linewidth=1, alpha=0.5)
        can.save(title)
コード例 #15
0
ファイル: plot-meghan.py プロジェクト: Yvonne-Ng/GP
def run_vs_4paramFit():
    args = parse_args()
    ext = args.output_file_extension
    from pygp.canvas import Canvas

    # Getting data points
    xRaw, yRaw, xerrRaw, yerrRaw = getDataPoints(args.input_file,
                                                 'dijetgamma_g85_2j65',
                                                 'Zprime_mjj_var')
    xSig, ySig, xerrSig, yerrSig = getDataPoints(args.signal_file,
                                                 'dijetgamma_g85_2j65',
                                                 'Zprime_mjj_var')

    #Data processing, cutting out the not desired range
    x, y, xerr, yerr = dataCut(0, 1500, 0, xRaw, yRaw, xerrRaw, yerrRaw)
    xMinFit = 300
    xFit, yFit, xerrFit, yerrFit = dataCut(xMinFit, 1500, 0, xRaw, yRaw,
                                           xerrRaw, yerrRaw)

    # make an evently spaced x
    t = np.linspace(np.min(x), np.max(x), 500)

    #calculating the log-likihood and minimizing for the gp
    lnProb = logLike_minuit(x, y, xerr)
    min_likelihood, best_fit = fit_gp_minuit(20, lnProb)
    fit_pars = [best_fit[x] for x in FIT3_PARS]
    #making the GP
    kargs = {x: y for x, y in best_fit.items() if x not in FIT3_PARS}
    kernel_new = get_kernel(**kargs)
    print(kernel_new.get_parameter_names())
    #making the kernel
    gp_new = george.GP(kernel_new, mean=Mean(fit_pars), fit_mean=True)
    gp_new.compute(x, yerr)
    mu, cov = gp_new.predict(y, t)
    mu_x, cov_x = gp_new.predict(y, x)

    ##----4 param fit function
    lnProb = logLike_4ff(xFit, yFit, xerrFit)
    minimumLLH, best_fit_params = fit_4ff(100, lnProb)
    fit_mean = model_4param(xFit, best_fit_params, xerrFit)

    #calculating significance
    signif = significance(mu_x, y, cov_x, yerr)

    initialCutPos = np.argmax(x > xMinFit)

    sigFit = significance(fit_mean, y[initialCutPos:], cov_x[initialCutPos:],
                          yerr[initialCutPos:])
    std = np.sqrt(np.diag(cov))

    ext = args.output_file_extension
    with Canvas(f'compareGPvs4Param{ext}', 'GP vs 4 param fit') as can:
        can.ax.errorbar(x, y, yerr=yerr, fmt='.')
        can.ax.set_yscale('log')
        # can.ax.set_ylim(1, can.ax.get_ylim()[1])
        can.ax.plot(t, mu, '-r')
        can.ax.plot(xFit, fit_mean, '-g')
        can.ax.fill_between(t,
                            mu - std,
                            mu + std,
                            facecolor=(0, 1, 0, 0.5),
                            zorder=5,
                            label='err = 1')
        can.ratio.stem(x, signif, markerfmt='.', basefmt=' ')
        can.ratio2.stem(xFit, sigFit, markerfmt='.', basefmt=' ')
        can.ratio.axhline(0, linewidth=1, alpha=0.5)
コード例 #16
0
def run_bkgnd():
    xMin = 300
    xMax = 1500
    xMinFit = 300
    xMaxFit = 1500

    args = parse_args()
    ext = args.output_file_extension
    from pygp.canvas import Canvas

    # Getting data points
    xRaw, yRaw, xerrRaw, yerrRaw = getDataPoints(args.input_file,
                                                 'dijetgamma_g85_2j65',
                                                 'Zprime_mjj_var')
    #data points from official fit function
    xFitRaw, yFitRaw, xerrFitRaw, yerrFitRaw = getDataPoints(
        args.fitFromOfficial, "", "basicBkgFrom4ParamFit")
    print("xFit: ", xFitRaw)

    #Data processing, cutting out the not desired range
    xBkg, yBkg, xerrBkg, yerrBkg = dataCut(
        xMin, xMax, 0, xRaw, yRaw, xerrRaw,
        yerrRaw)  # for GP bkgnd kernel and signal kernel
    xBkgFit, yBkgFit, xerrBkgFit, yerrBkgFit = dataCut(
        xMinFit, xMaxFit, 0, xRaw, yRaw, xerrRaw, yerrRaw)  # for fit function
    xFit, yFit, xerrFit, yerrFit = dataCut(xMinFit, xMaxFit, 0, xFitRaw,
                                           yFitRaw, xerrFitRaw, yerrRaw)

    # calculate the min log likelihood of the official fit function
    logLikeOff = logLike_3ffOff(xBkg, yBkg, xFit, yFit, xerrBkg)
    # make an evently spaced x
    t = np.linspace(np.min(xBkg), np.max(xBkg), 500)

    #Drawing the raw data of just the bkgnd and Sig + bkgnd
    with Canvas(
            f'RawBkgnd') as can:  # just to check and see everything is okay
        can.ax.errorbar(xBkg, yBkg, yerr=yerrBkg, fmt='.g')
        can.ax.set_yscale('log')

    #GP bkgnd: finidng the mean and covaraicne
    mu_xBkg, cov_xBkg, bestFitBkg = y_bestFitGP(xBkg,
                                                yBkg,
                                                xerrBkg,
                                                yerrBkg,
                                                30,
                                                kernelType="bkg")

    # GP Signal: finind the mean of covariance
    mu_xSig, cov_xSig, bestFitSig = y_bestFitGP(xBkg,
                                                yBkg,
                                                xerrBkg,
                                                yerrBkg,
                                                30,
                                                kernelType="sig")

    #finding the fit y values
    fit_mean = y_bestFit3Params(xBkgFit, yBkgFit, xerrBkgFit, 1)

    #Signal only fit
    ySigFit = mu_xSig - mu_xBkg
    print("ySigFit: ", ySigFit)

    #finding signifiance
    GPSignificance, chi2 = res_significance(yBkg, mu_xBkg)
    fitSignificance, chi2fit = res_significance(yBkgFit, fit_mean)
    GPSigSignificance, chi2SignalFit = res_significance(yBkg, mu_xSig)
    FitSigFromOff, chi2FitOff = res_significance(yBkgFit, yFit)
    #drawing the result
    ext = args.output_file_extension
    title = "test"
    with Canvas(f'%s{ext}' % title, "Fit Function official ",
                "GP bkgnd kernel", "GP signal+bkgnd kernel", 3) as can:
        can.ax.errorbar(xBkg, yBkg, yerr=yerrBkg,
                        fmt='.g')  # drawing the points
        can.ax.set_yscale('log')
        #can.ax.plot(xBkgFit, fit_mean, '-r', label="fit function")
        can.ax.plot(xFit, yFit, '-r', label="fit function official")
        can.ax.plot(xBkg, mu_xBkg, '-g', label="GP bkg kernel")  #drawing
        can.ax.plot(xBkg, mu_xSig, '-b', label="GP signal kernel")
        can.ax.legend(framealpha=0)
        can.ratio.stem(xFit, FitSigFromOff, markerfmt='.', basefmt=' ')
        can.ratio.set_ylabel("significance")
        can.ratio2.stem(xBkg, GPSignificance, markerfmt='.', basefmt=' ')
        can.ratio2.set_ylabel("significance")
        can.ratio3.set_ylabel("significance")
        can.ratio3.stem(xBkg, GPSigSignificance, markerfmt='.', basefmt=' ')
        can.ratio.axhline(0, linewidth=1, alpha=0.5)
        can.ratio2.axhline(0, linewidth=1, alpha=0.5)
        can.ratio3.axhline(0, linewidth=1, alpha=0.5)
        can.save(title)

    fitChi2List, GPChi2List = psuedoTest(100, yBkgFit, yerrBkgFit, fit_mean,
                                         yBkg, yerrBkg, mu_xBkg)
    print("fitChi2List", fitChi2List)
    print("GPChi2List", GPChi2List)
    #n, bins, patches = plt.hist(fitChi2List, 50, normed=1, facecolor='green', alpha=0.75)
    #plt.plot(n, drawstyle="steps")
    #plt.savefig("chi2.pdf")

    value, edges = np.histogram(fitChi2List)
    print("value %r, edges %r" % (value, edges))
    binCenters = (edges[1:] + edges[:-1]) / 2
    valueGP, edgesGP = np.histogram(GPChi2List)
    print("value %r, edges %r" % (valueGP, edgesGP))
    binCentersGP = (edgesGP[1:] + edgesGP[:-1]) / 2
    #plt.step(binCenters,value)
    #plt.savefig("chi2.pdf")

    n = binCenters.size - 1
    nGP = binCentersGP.size - 1

    title = "chi2"
    with Canvas(f'%s{ext}' % title, "Fit Function", "GP bkgnd kernel", "",
                2) as can:
        can.ax.step(binCenters / n, value, label="Fit Function", where="mid")
        can.ax.step(binCentersGP / nGP, valueGP, label="GP bkgnd", where="mid")
        can.ax.legend(framealpha=0)
        can.save(title)
    print("min LL of off: ", logLikeOff)

    #drawing the signal only
    ext = args.output_file_extension
    title = "signalonlybkg"
    with Canvas(f'%s{ext}' % title, "GPSig-GPBkgFit Sig", "", "", 2) as can:
        #can.ax.errorbar(xBkg, ySig, yerr=yerrBkg, fmt='.g', label="signal MC injected") # drawing the points
        can.ax.set_ylim(-1000, 1000.0)
        #can.ax.set_yscale('log')
        can.ax.plot(xBkg,
                    ySigFit,
                    '-r',
                    label="GP Sig + bkg Kernel fit  - GP bkgnd kernel fit")
        can.ax.legend(framealpha=0)
        ##
        #can.ratio.stem(xBkg, GPSigOnlySignificance, markerfmt='.', basefmt=' ')
        can.ratio.axhline(0, linewidth=1, alpha=0.5)
        #can.ax.plot(xBkg, mu_xBkg, '-g', label="GP bkgnd kernel") #drawing
        can.save(title)
コード例 #17
0
def drawFitDataSet(dataSet, title, saveTxt=False, saveTxtDir=None):
    # draw the data set using diffrent fits
    ext = ".pdf"
    with Canvas(f'%s{ext}' % title, "GP bkgnd kernel",
                "GP signal+bkgnd kernel", 2) as can:
        can.ax.errorbar(dataSet.xData,
                        dataSet.yData,
                        yerr=dataSet.yerrData,
                        fmt='.g',
                        label="datapoints")  # drawing the points
        can.ax.set_yscale('log')
        #can.ax.plot(dataSet.x_simpleFit, dataSet.yFit_simpleFit, '-r', label="fit function")
        #can.ax.plot(dataSet.xOffFit, dataSet.yFit_officialFit, '-m', label="fit function official")
        can.ax.plot(dataSet.xData,
                    dataSet.y_GPBkgKernelFit,
                    '-g',
                    label="GP bkgnd kernel")  #drawing
        #can.ax.plot(dataSet.xData, dataSet.y_GPSigPlusBkgKernelFit, '-b', label="GP signal kernel")
        if saveTxtDir:
            saveTxtDir = saveTxtDir + "/"
        if saveTxt:
            with open(saveTxtDir + "dataPoints.txt", "w") as f0:
                writer = csv.writer(f0, delimiter="\t")
                writer.writerows(zip(dataSet.xData, dataSet.yData))
                print("check: ", dataSet.xData, "   ", dataSet.yData)

            with open(saveTxtDir + "simpleFit.txt", "w") as f1:
                writer = csv.writer(f1, delimiter="\t")
                writer.writerows(
                    zip(dataSet.x_simpleFit, dataSet.yFit_simpleFit))

            with open(saveTxtDir + "fitFunctionOfficial.txt", "w") as f2:
                writer = csv.writer(f2, delimiter="\t")
                writer.writerows(zip(dataSet.xOffFit,
                                     dataSet.yFit_officialFit))

            with open(saveTxtDir + "GPBkgndKernel.txt", "w") as f3:
                writer = csv.writer(f3, delimiter="\t")
                writer.writerows(zip(dataSet.xData, dataSet.y_GPBkgKernelFit))

            with open(saveTxtDir + "GPSigPlusBkgKernel.txt", "w") as f4:
                writer = csv.writer(f4, delimiter="\t")
                writer.writerows(
                    zip(dataSet.xData, dataSet.y_GPSigPlusBkgKernelFit))

        can.ax.legend(framealpha=0)
        #can.ratio.stem(dataSet.x_simpleFit, dataSet.significance_simpleFit, markerfmt='.', basefmt=' ')
        #can.ratio.stem(xSBFit, fitSignificance, markerfmt='.', basefmt=' ')
        #can.ratio.stem(xSBFit, testsig, markerfmt='.', basefmt=' ')
        #can.ratio.set_ylabel("significance")
        can.ratio[0].stem(dataSet.xData,
                          dataSet.significance_GPBkgKernelFit,
                          markerfmt='.',
                          basefmt=' ')
        can.ratio[0].set_ylabel("significance")
        can.ratio[1].set_ylabel("significance")
        can.ratio[1].stem(dataSet.xData,
                          dataSet.significance_GPSigPlusBkgKernelFit,
                          markerfmt='.',
                          basefmt=' ')
        can.ratio[0].axhline(0, linewidth=1, alpha=0.5)
        can.save(title)
コード例 #18
0
ファイル: plot-meghan.py プロジェクト: Yvonne-Ng/GP
def run_vs_SearchPhase_UA2Fit():
    args = parse_args()
    ext = args.output_file_extension
    from pygp.canvas import Canvas

    # Getting data points
    xRaw, yRaw, xerrRaw, yerrRaw = getDataPoints(args.input_file,
                                                 'dijetgamma_g85_2j65',
                                                 'Zprime_mjj_var')

    #Data processing, cutting out the not desired range
    x, y, xerr, yerr = dataCut(0, 1500, 0, xRaw, yRaw, xerrRaw, yerrRaw)
    xMinFit = 303
    xMaxFit = 1500
    xFit, yFit, xerrFit, yerrFit = dataCut(xMinFit, xMaxFit, 0, xRaw, yRaw,
                                           xerrRaw, yerrRaw)

    # make an evently spaced x
    t = np.linspace(np.min(x), np.max(x), 500)

    #calculating the log-likihood and minimizing for the gp
    lnProb = logLike_minuit(x, y, xerr)
    min_likelihood, best_fit = fit_gp_minuit(20, lnProb)

    fit_pars = [best_fit[x] for x in FIT3_PARS]

    #making the GP
    kargs = {x: y for x, y in best_fit.items() if x not in FIT3_PARS}
    kernel_new = get_kernel(**kargs)
    print(kernel_new.get_parameter_names())
    #making the kernel
    gp_new = george.GP(kernel_new, mean=Mean(fit_pars), fit_mean=True)
    gp_new.compute(x, yerr)
    mu, cov = gp_new.predict(y, t)
    mu_x, cov_x = gp_new.predict(y, xFit)
    # calculating the fit function value

    #GP compute minimizes the log likelihood of the best_fit function
    best = [best_fit[x] for x in FIT3_PARS]
    print("best param meghan GP minmization:", best)
    meanFromGPFit = Mean(best)

    fit_meanM = meanFromGPFit.get_value(
        x, xerr)  #so this is currently shit. can't get the xErr thing working
    print("fit_meanM:", fit_meanM)

    #fit results from search phase
    best_fit_params = (1.0779, 2.04035, 58.5769, -157.945)
    fit_mean = model_UA2(xFit, best_fit_params, xerrFit)

    ##----4 param fit function
    #lnProb = logLike_4ff(xFit,yFit,xerrFit)
    #minimumLLH, best_fit_params = fit_4ff(100, lnProb)
    #fit_mean = model_4param(xFit, best_fit_params, xerrFit)

    #calculating significance
    signif = significance(mu_x, y, cov_x, yerr)

    initialCutPos = np.argmax(x > xMinFit)
    finalCutPos = np.argmin(x < xMaxFit)

    #sigFit = (fit_mean - y[initialCutPos:]) / np.sqrt(np.diag(cov_x[initialCutPos:]) + yerr[initialCutPos:]**2)
    sigFit = significance(fit_mean, y[initialCutPos:], cov_x[initialCutPos:],
                          yerr[initialCutPos:])
    #sigit = (mu_x)
    #std = np.sqrt(np.diag(cov))

    ext = args.output_file_extension
    title = "compareGPvsSearchPhaseUA2"
    with Canvas(f'%s{ext}' % title) as can:
        can.ax.errorbar(x, y, yerr=yerr, fmt='.')
        can.ax.set_yscale('log')
        # can.ax.set_ylim(1, can.ax.get_ylim()[1])
        can.ax.plot(t, mu, '-r')
        can.ax.plot(xFit, fit_mean, '-b')
        #this only works with the xErr part of Mean commented out
        #can.ax.plot(x, fit_meanM, '.g')
        # can.ax.plot(t, fit_mean_smooth, '--b')
        #can.ax.fill_between(t, mu - std, mu + std,
        #facecolor=(0, 1, 0, 0.5),
        #zorder=5, label='err = 1')
        #can.ratio.stem(x, signif, markerfmt='.', basefmt=' ')
        can.ratio.stem(xFit, sigFit, markerfmt='.', basefmt=' ')
        can.ratio.axhline(0, linewidth=1, alpha=0.5)
        can.save(title)