Beispiel #1
0
def storeHistogram(var, lumi, cut, histo, filename):
    # Plotting Data
    # ------------------
    bp = BasicPlot(filename, var)
    bp.addHistogram(histo, 'E', copy=True)
    bp.titles.append(
        "#sqrt{{s}} = 13 TeV, #scale[0.5]{{#int}}Ldt = {0:.2f} fb^{{-1}}".
        format(lumi / 1000.))
    bp.yVariable = Variable("Events")
    bp.normalizeByBinWidth = False
    bp.showBinWidthY = False
    bp.normalized = False
    bp.logY = False
    bp.yVariable.binning.low = 0  # start y-axis at 0
    bp.draw()
    bp.saveAsAll(os.path.join(outFolder, bp.title))
    # storing histogram in root file
    # ------------------------------
    from ROOT import TFile, TObject
    rootfile = TFile.Open(outFolder + '/' + filename + '.root', 'update')
    if rootfile.IsOpen():
        print "writing in file {}".format(outFolder + '/' + filename + '.root')
        rootfile.cd()
        histogram = histo.Clone('h_' + var.name)
        histogram.SetTitle('h_' + var.name)
        histogram.Write('h_' + var.name, TObject.kOverwrite)
        rootfile.Close()
    else:
        print "Problem opening file {}".format(outFolder + '/' + filename +
                                               '.root')
Beispiel #2
0
def PlotAndStore(var,
                 filename,
                 lumi,
                 h_1,
                 h_2=None,
                 h_3=None,
                 drawOneLine=False,
                 simulation=False,
                 yLabel="Fake Rate",
                 yLow=0,
                 yHigh=None):
    bp = BasicPlot(filename, var)
    # Add histos
    SetHistStyle(h_1, 0)
    bp.addHistogram(h_1, 'E', copy=True)
    if h_2:
        SetHistStyle(h_2, 1)
        bp.addHistogram(h_2, 'E', copy=True)
    if h_3:
        SetHistStyle(h_3, 2)
        bp.addHistogram(h_3, 'E', copy=True)
    # Draw a line at 1 e.g. for SF plots
    if drawOneLine:
        h_tmp = var.createHistogram('')
        for i_bin in range(h_tmp.GetNbinsX() + 2):
            h_tmp.SetBinContent(i_bin, 1)
            h_tmp.SetBinError(i_bin, 0)
        h_tmp.SetLineColor(kGray)
        bp.addHistogram(h_tmp, 'E')
    # Some further settings
    bp.titles.append(
        '#sqrt{{s}} = 13 TeV, #scale[0.5]{{#int}}Ldt = {0:.2f} fb^{{-1}}'.
        format(lumi / 1000.))
    bp.legendDecorator.textSize = 0.045
    if simulation:
        bp.titles.append('Simulation')
    bp.showBinWidthY = False
    bp.logY = False
    bp.normalized = False
    bp.normalizeByBinWidth = False
    bp.yVariable = Variable(yLabel)
    bp.yVariable.binning.low = yLow
    bp.yVariable.binning.up = yHigh
    # Plot and store
    bp.draw()
    bp.saveAsAll(os.path.join(outFolder, bp.title))
Beispiel #3
0
		for i_set in range(n_sets): # data or mc plot ?
		    if i_type == 2: # SF plots have a different naming convention
			bp = BasicPlot( 'FakeRate_'+var.name+name_type[i_type]+sel_p_suffix[p], var )
		    else:
			bp = BasicPlot( 'FakeRate_'+var.name+name_type[i_type]+sel_p_suffix[p]+name_set[i_set], var )
		    bp.showBinWidthY = False
		    if name_type[i_type] == '_SF': # Draw a line at 1 for SF plots
			h_tmp = var.createHistogram( '' )
			for i_bin in range( h_tmp.GetNbinsX() + 2 ):
			    h_tmp.SetBinContent( i_bin, 1 )
			    h_tmp.SetBinError( i_bin, 0 )
			h_tmp.SetLineColor( kGray )
			bp.addHistogram( h_tmp, 'E' )
		    for i in range(id_range[i_type]): # Loop over different IDs
			histo_set[len(name_set)*i_type+i_set][i].SetLineColor( sel_colors[i] )
			histo_set[len(name_set)*i_type+i_set][i].SetMarkerColor( sel_colors[i] )
			histo_set[len(name_set)*i_type+i_set][i].SetMarkerStyle( sel_marker[i] )
			bp.addHistogram( histo_set[len(name_set)*i_type+i_set][i], 'E', copy=True )
		    bp.titles.append( '#sqrt{{s}} = 13 TeV, #scale[0.5]{{#int}}Ldt = {0:.2f} fb^{{-1}}'.format(lumi/1000.) )
		    bp.legendDecorator.textSize = 0.045
		    bp.normalizeByBinWidth = normByWidth[i_type]
		    bp.yVariable = Variable( yLabel[i_type] )
		    bp.logY = logScale[i_type]
		    bp.normalized = False
		    if i_type == 1 or i_type == 2: # For FR and SF plots start y-axis at 0
			bp.yVariable.binning.low = 0
		    bp.draw()
		    bp.saveAsAll( os.path.join( "plots/", bp.title ) )
    # --------------------
    print 'everything is done!'
Beispiel #4
0
                        h_mc.append(h_id)
                        sys.stdout.write('.')
                        sys.stdout.flush()
            print "\tgot all{0} histograms for {1}".format(
                sel_p_names[p], var.name)
            # Plotting
            # --------
            if do_plots:
                bp = BasicPlot(var.name + sel_p_suffix[p], var)
                bp.legendDecorator.textSize = 0.045
                bp.showBinWidthY = False
                for i in range(
                        len(selections)):  # Loop over different selections
                    h_mc[i].SetLineColor(sel_colors[i])
                    h_mc[i].SetMarkerColor(sel_colors[i])
                    h_mc[i].SetMarkerStyle(sel_marker[i])
                    bp.addHistogram(h_mc[i], 'E', copy=True)
                bp.titles.append(
                    'Simulation, #sqrt{{s}} = 13 TeV, #scale[0.5]{{#int}}Ldt = {0:.2f} fb^{{-1}}'
                    .format(lumi / 1000.))
                bp.normalizeByBinWidth = False
                bp.normalized = True
                bp.yVariable = Variable('Normalized')
                #bp.yVariable = Variable( 'Events' )
                bp.logY = logBool[i_var]
                bp.draw()
                bp.saveAsAll(os.path.join(outFolder, bp.title))
        print "\t\tDone plotting {0}".format(var.name)
    # --------------------
    print 'everything is done!'
Beispiel #5
0
                                 "Region 2",
                                 cut=selections[0] +
                                 sel_p[i_p] +
                                 cut_option_region2,
                                 luminosity=lumi,
                                 weight=tmp_weight)
 sys.stdout.write('.')
 sys.stdout.flush()
 h_var_r1.SetLineColor(kRed)
 h_var_r1.SetMarkerColor(kRed)
 h_var_r2.SetLineColor(kBlue)
 h_var_r2.SetMarkerColor(kBlue)
 bp = BasicPlot(
     'Q-G-Separation' + sel_p_suffix[i_p] + '_' +
     var.name + cut_option_suffix, var)
 bp.yVariable = Variable("Events with quark match")
 bp.yVariable.binning.low = 0
 bp.yVariable.binning.up = None
 bp.showBinWidthY = False
 bp.addHistogram(h_var_r1, 'E', copy=True)
 bp.addHistogram(h_var_r2, 'E', copy=True)
 bp.titles.append(
     '#sqrt{{s}} = 13 TeV, #scale[0.5]{{#int}}Ldt = {0:.2f} fb^{{-1}}, '
     .format(lumi / 1000.))
 bp.draw()
 bp.saveAsAll(os.path.join("plots/", bp.title))
 # print quark rate
 h_var_tot_r1 = process.getHistogram(var,
                                     "Region 1",
                                     cut=selections[3] +
                                     sel_p[i_p] +
Beispiel #6
0
def TemplateFit(filename,
                data,
                mc_q,
                mc_g,
                w_q=None,
                w_g=None,
                pull_widths=[1, 1],
                doPlots=True):
    n_mc = 2

    # Define MC samples
    mc = TObjArray(n_mc)
    mc.Add(mc_q)
    mc.Add(mc_g)

    # Names
    mc_type = []
    mc_type.append('Quarks')
    mc_type.append('Gluons')

    # Perform Fit
    fit = TFractionFitter(data, mc, "q")
    if w_q and w_g:
        fit.SetWeight(0, w_q)
        fit.SetWeight(1, w_g)
    fit.Constrain(0, 0.0, 1.0)  #quarks
    fit.Constrain(1, 0.0, 1.0)  #gluons
    fit.Fit()

    # Printing results
    val_q = ROOT.Double()
    err_q = ROOT.Double()
    val_g = ROOT.Double()
    err_g = ROOT.Double()
    fit.GetResult(0, val_q, err_q)
    fit.GetResult(1, val_g, err_g)
    err_q = err_q * pull_widths[
        0]  # correcting the error for the width of the pull plot
    err_g = err_g * pull_widths[
        1]  # correcting the error for the width of the pull plot
    for i in range(n_mc):
        val = ROOT.Double()
        err = ROOT.Double()
        fit.GetResult(i, val, err)
        out_file.write("fit result {}: {}, {}\n".format(mc_type[i], val, err))
    out_file.write("X2 = {}\n".format(fit.GetChisquare()))
    out_file.write("NDF = {}\n".format(fit.GetNDF()))
    red_chi = fit.GetChisquare() / fit.GetNDF()
    out_file.write("red. X2 = {}\n".format(red_chi))
    out_file.write("\n")

    if doPlots:
        print "storing plot as {}".format(filename)
        # Preparing Histograms
        h_fit = fit.GetPlot()
        h_q = fit.GetMCPrediction(0)
        h_g = fit.GetMCPrediction(1)
        h_fit.SetTitle("Fit")
        h_q.SetTitle("Quarks")
        h_g.SetTitle("Gluons")
        h_fit.SetLineColor(kGreen)
        h_q.SetLineColor(kRed)
        h_g.SetLineColor(kBlue)
        h_q.SetMarkerColor(kRed)
        h_g.SetMarkerColor(kBlue)

        # Scaling to the fit result
        int_fit = h_fit.Integral()
        int_q = h_q.Integral()
        int_g = h_g.Integral()
        h_q.Sumw2()
        h_g.Sumw2()
        h_q.Scale(val_q * int_fit / int_q)
        h_g.Scale(val_g * int_fit / int_g)

        # Checking fit vs. templates
        h_check = h_fit.Clone('Fit - Templates Check')
        h_check.Add(h_q, -1)
        h_check.Add(h_g, -1)
        check_maxBin = h_check.GetMaximumBin()
        out_file.write(
            "Maximal bin of fit minus templates divided by content of that bin in fit: {} / {}\n\n"
            .format(h_fit.GetBinContent(check_maxBin),
                    h_check.GetBinContent(check_maxBin)))

        # Plotting results
        bp = BasicPlot(filename, var_tau0_width)
        bp.addHistogram(h_q, 'E', copy=True)
        bp.addHistogram(h_g, 'E', copy=True)
        bp.addHistogram(h_fit, 'HIST', copy=True)
        bp.addHistogram(data, 'E', copy=True)
        bp.titles.append(
            'Quarks: {:.3} #pm {:.2}, Gluons: {:.3} #pm {:.2}'.format(
                val_q, err_q, val_g, err_g))
        bp.titles.append(
            '#sqrt{{s}} = 13 TeV, #scale[0.5]{{#int}}Ldt = {0:.2f} fb^{{-1}}'.
            format(lumi / 1000.))
        bp.titles.append('#chi^{{2}}/ndf: {:.3}'.format(red_chi))
        bp.yVariable = Variable("Events")
        bp.legendDecorator.textSize = 0.045
        bp.normalizeByBinWidth = False
        bp.showBinWidthY = False
        bp.normalized = False
        bp.logY = False
        bp.yVariable.binning.low = 0  # start y-axis at 0
        bp.draw()
        bp.saveAsAll(os.path.join(outFolder, bp.title))

    return fit
Beispiel #7
0
def PullPlot(filename,
             h_data,
             h_quarks,
             h_gluons,
             w_quarks=None,
             w_gluons=None,
             pull_widths=[1, 1]):
    # Creating a pull-plot
    # --------------------
    ratio_q = []
    error_q = []
    ratio_g = []
    error_g = []
    n_runs = 10000
    # getting fit results for toy experiments
    for i in range(1, n_runs + 1):
        if i == 1: print "    starting loop"
        h_tmp = var.createHistogram(str(i))
        h_tmp.FillRandom(h_data)
        filename_tmp = filename + '_' + str(i)
        out_file.write(str(i) + ":\n")

        fit = TemplateFit(filename,
                          h_tmp,
                          h_quarks,
                          h_gluons,
                          w_quarks,
                          w_gluons,
                          pull_widths,
                          doPlots=False)

        val_q = ROOT.Double()
        err_q = ROOT.Double()
        val_g = ROOT.Double()
        err_g = ROOT.Double()
        fit.GetResult(0, val_q, err_q)
        fit.GetResult(1, val_g, err_g)
        del fit
        err_q = err_q * pull_widths[
            0]  # correcting the error for the width of the pull plot
        err_g = err_g * pull_widths[
            1]  # correcting the error for the width of the pull plot

        ratio_q.append(val_q)
        error_q.append(err_q)
        ratio_g.append(val_g)
        error_g.append(err_g)

        if i % (n_runs / 10) == 0 or i == 1 or i == n_runs:
            print "    {}\tQ: {} +- {}\tG: {} +- {}".format(
                i, val_q, err_q, val_g, err_g)

    # creating pull histogram for quarks
    h_pull = var_pull.createHistogram()
    ratio_q_mean = sum(ratio_q) / len(ratio_q)
    for i in range(n_runs):
        tmp = (ratio_q[i] - ratio_q_mean) / error_q[i]
        h_pull.Fill(tmp)

    fit_result = h_pull.Fit('gaus', 'QS')
    f_gaus = h_pull.GetFunction('gaus')
    v_q_mu = fit_result.Parameter(1)
    v_q_mu_err = fit_result.ParError(1)
    v_q_sigma = fit_result.Parameter(2)
    v_q_sigma_err = fit_result.ParError(2)
    chi = fit_result.Chi2()
    ndf = fit_result.Ndf()
    #red_chi = chi / ndf

    filename_tmp = filename + '_PullPlot_q'
    bp = BasicPlot(filename_tmp, var_pull)
    bp.titles.append(
        'Fit Results: #mu = {:05.3f}#pm{:05.3f}, #sigma = {:05.3f}#pm{:05.3f}'.
        format(v_q_mu, v_q_mu_err, v_q_sigma, v_q_sigma_err))
    bp.titles.append('#chi^{{2}}/ndf: {:.3}/{}'.format(chi, ndf))
    bp.addHistogram(h_pull, 'E', copy=True)
    bp.yVariable = Variable("Events")
    bp.normalizeByBinWidth = False
    bp.showBinWidthY = False
    bp.normalized = False
    bp.logY = False
    bp.yVariable.binning.low = 0  # start y-axis at 0
    bp.draw()
    bp.saveAsAll(os.path.join(outFolder, bp.title))

    del h_pull
    del bp

    # creating pull histogram for gluons
    h_pull = var_pull.createHistogram()
    ratio_g_mean = sum(ratio_g) / len(ratio_g)
    for i in range(n_runs):
        tmp = (ratio_g[i] - ratio_g_mean) / error_g[i]
        h_pull.Fill(tmp)

    fit_result = h_pull.Fit('gaus', 'QS')
    f_gaus = h_pull.GetFunction('gaus')
    v_g_mu = fit_result.Parameter(1)
    v_g_mu_err = fit_result.ParError(1)
    v_g_sigma = fit_result.Parameter(2)
    v_g_sigma_err = fit_result.ParError(2)
    chi = fit_result.Chi2()
    ndf = fit_result.Ndf()
    #red_chi = chi / ndf

    filename_tmp = filename + '_PullPlot_g'
    bp = BasicPlot(filename_tmp, var_pull)
    bp.titles.append(
        'Fit Results: #mu = {:05.3f}#pm{:05.3f}, #sigma = {:05.3f}#pm{:05.3f}'.
        format(v_g_mu, v_g_mu_err, v_g_sigma, v_g_sigma_err))
    bp.titles.append('#chi^{{2}}/ndf: {:.3}/{}'.format(chi, ndf))
    bp.addHistogram(h_pull, 'E', copy=True)
    bp.yVariable = Variable("Events")
    bp.normalizeByBinWidth = False
    bp.showBinWidthY = False
    bp.normalized = False
    bp.logY = False
    bp.yVariable.binning.low = 0  # start y-axis at 0
    bp.draw()
    bp.saveAsAll(os.path.join(outFolder, bp.title))

    return [v_q_sigma, v_g_sigma]