def checkOnMC(unfolding, method):
    global bins, nbins
    RooUnfold.SVD_n_toy = 1000
    pulls = []
    for sub in range(1,9):
        inputFile2 = File('../data/unfolding_merged_sub%d.root' % sub, 'read')
        h_data = asrootpy(inputFile2.unfoldingAnalyserElectronChannel.measured.Rebin(nbins, 'measured', bins))
        nEvents = inputFile2.EventFilter.EventCounter.GetBinContent(1)
        lumiweight = 164.5 * 5050 / nEvents
#        print sub, nEvents
        h_data.Scale(lumiweight)
        doUnfoldingSequence(unfolding, h_data, method, '_sub%d' %sub)
        pull = unfolding.pull_inputErrorOnly()
#        unfolding.printTable()
        pulls.append(pull)
        unfolding.Reset()
    allpulls = []

    for pull in pulls:
        allpulls.extend(pull)
    h_allpulls = Hist(100,-30,30)
    filling = h_allpulls.Fill
    for entry in allpulls:
        filling(entry)
    fit = h_allpulls.Fit('gaus', 'WWS')
    h_fit = asrootpy(h_allpulls.GetFunction("gaus").GetHistogram())
    canvas = Canvas(width=1600, height=1000)
    canvas.SetLeftMargin(0.15)
    canvas.SetBottomMargin(0.15)
    canvas.SetTopMargin(0.10)
    canvas.SetRightMargin(0.05)
    h_allpulls.Draw()
    fit.Draw('same')
    canvas.SaveAs('plots/Pull_allBins_withFit.png')
    
    
    
    plt.figure(figsize=(16, 10), dpi=100)
    rplt.errorbar(h_allpulls, label=r'Pull distribution for all bins',  emptybins=False)
    rplt.hist(h_fit, label=r'fit')
    plt.xlabel('(unfolded-true)/error', CMS.x_axis_title)
    plt.ylabel('entries', CMS.y_axis_title)
    plt.title('Pull distribution for all bins', CMS.title)
    plt.tick_params(**CMS.axis_label_major)
    plt.tick_params(**CMS.axis_label_minor)
    plt.legend(numpoints=1)
    plt.savefig('plots/Pull_allBins.png')
    
    #individual bins
    for bin_i in range(nbins):
        h_pull = Hist(100,-30,30)
        for pull in pulls:
            h_pull.Fill(pull[bin_i])
        plt.figure(figsize=(16, 10), dpi=100)
        rplt.errorbar(h_pull, label=r'Pull distribution for bin %d' % (bin_i + 1), emptybins=False)
        plt.xlabel('(unfolded-true)/error', CMS.x_axis_title)
        plt.ylabel('entries', CMS.y_axis_title)
        plt.title('Pull distribution for  bin %d' % (bin_i + 1), CMS.title)
        plt.savefig('Pull_bin_%d.png' % (bin_i + 1))
Exemple #2
0
def plot_comparisons(input_file, comparison_plots):
  with root_open(input_file) as file:
    for name,histo_config in comparison_plots.items():
      canvas = Canvas(1200,700)
      canvas.SetLeftMargin(0.08)
      canvas.SetRightMargin(0.12)
      histogram = file.Get(histo_config.name)
      if len(histo_config.range)==2:
        histogram.SetAxisRange(histo_config.range[0], histo_config.range[1], 'X')
      histogram.color = histo_config.color
      histogram.markerstyle = histo_config.marker
      histogram.Draw()
      canvas.Print('./plots/{}.png'.format(name))
Exemple #3
0
    def CreatCanvas(self):
        H_ref = 600
        W_ref = 800
        W = W_ref
        H = H_ref
        T = 0.08 * H_ref
        B = 0.12 * H_ref
        L = 0.12 * W_ref
        R = 0.04 * W_ref
        # Set the tdr style

        canvas = Canvas(width=W, height=H)
        canvas.SetFillColor(0)
        canvas.SetBorderMode(0)
        canvas.SetFrameFillStyle(0)
        canvas.SetFrameBorderMode(0)
        canvas.SetLeftMargin(L / W)
        canvas.SetRightMargin(R / W)
        canvas.SetTopMargin(T / H)
        canvas.SetBottomMargin(B / H)
        canvas.SetTickx(0)
        canvas.SetTicky(0)
        return canvas
Exemple #4
0
    this_n = np.array(this_n)
    this_r0 = np.array(this_r0)
    this_R = np.array(this_R)
    B += magnetic_field(r, this_n, this_r0, this_R)

Bx = B[:, 0]
By = B[:, 1]
Bz = B[:, 2]
Bx.shape = X.shape
By.shape = Y.shape
Bz.shape = Z.shape

Bnorm = np.sqrt(Bx**2 + By**2 + Bz**2)

c2 = Canvas()
c2.SetRightMargin(0.1)
h2_B_XY = Hist2D(101, -5, 5, 101, -5, 5)
print "lenght X=%d , Y=%d, Z=%d, B=%d " % (len(X), len(Y), len(Z), len(Bnorm))

for i in range(0, len(X)):
    for j in range(0, len(Y)):
        h2_B_XY.Fill(X[i][j][121], Y[i][j][121], Bnorm[i][j][121])
h2_B_XY.Draw('COLZ')
#c2.Print("Helmoltz_XY.jpg")
c3 = Canvas()
c3.SetRightMargin(0.1)

h2_B_ZY = Hist2D(241, -12, 12, 101, -5, 5)
for i in range(0, 241):
    for j in range(0, len(Y)):
        h2_B_ZY.Fill(Z[51][j][i], Y[51][j][i], Bnorm[51][j][i])
Exemple #5
0
# easily set visual attributes
h_simple.linecolor = 'blue'
h_simple.fillcolor = 'green'
h_simple.fillstyle = '/'

# attributes may be accessed in the same way
print(h_simple.name)
print(h_simple.title)
print(h_simple.markersize)

# plot
canvas = Canvas(width=700, height=500)
canvas.SetLeftMargin(0.15)
canvas.SetBottomMargin(0.15)
canvas.SetTopMargin(0.10)
canvas.SetRightMargin(0.05)
h_simple.Draw()

# create the legend
legend = Legend([h_simple],
                pad=canvas,
                header='Header',
                leftmargin=0.05,
                rightmargin=0.5)
legend.Draw()

# 2D and 3D histograms are handled in the same way
# the constructor arguments are repetitions of #bins, left bound, right bound.
h2d = Hist2D(10, 0, 1, 50, -40, 10, name='2d hist')
h3d = Hist3D(3, -1, 4, 10, -1000, -200, 2, 0, 1, name='3d hist')
Exemple #6
0
            if yplot == "Plot":

                print variable + " " + min + " " + max + " " + nbins + " " + title + " " + ytitle
                for (xvariable, xmin, xmax, xnbins, xtitle, xytitle,
                     xplot) in zip(xvariable_list, xmin_list, xmax_list,
                                   xnbins_list, xtitle_list, xytitle_list,
                                   xplot_list):
                    if xplot == "Plot":
                        if xvariable == variable:
                            continue
                        # Canvas Variables and declaration

                        print xvariable + " " + xmin + " " + xmax + " " + xnbins + " " + xtitle + " " + xytitle
                        canvas = Canvas(width=700, height=500)
                        #canvas.SetLeftMargin(0.1)
                        canvas.SetRightMargin(0.15)
                        canvas.SetTopMargin(0.05)
                        canvas.Draw()

                        #canvas.SetLogy()
                        canvas.SetGrid()

                        histname = "hist" + xvariable + "_" + variable + background_name
                        print histname
                        histogram = histname + "(" + xnbins + "," + xmin + "," + xmax + "," + nbins + "," + min + "," + max + ")"
                        print histogram
                        histname = compressed.Draw(xvariable + ":" + variable +
                                                   ">>" + histogram,
                                                   selection=weight * total,
                                                   drawstyle='COLZ')
                        histname.set_title(signalprocess + " " +
Exemple #7
0
        while True:
          # non-empty dict, so break
          if plots_paths.get(topLevelPath, {}):
            break
          topLevelPath = os.path.dirname(topLevelPath)
          # top level path empty so break
          if not topLevelPath:
            break
        plots_path = plots_paths.get(topLevelPath, {})

        # create new canvas
        canvasConfigs = copy.copy(plots_config.get('canvas', {}))
        canvasConfigs.update(plots_path.get('canvas', {}))
        canvas = Canvas(canvasConfigs.get('width', 500), canvasConfigs.get('height', 500))

        canvas.SetRightMargin(canvasConfigs.get('rightmargin', 0.1))
        canvas.SetBottomMargin(canvasConfigs.get('bottommargin', 0.2))
        canvas.SetLeftMargin(canvasConfigs.get('leftmargin', 0.2))

        if canvasConfigs.get('logy', False) == True:
          canvas.set_logy()

        # create a legend (an entry for each group)
        legendConfigs = copy.copy(plots_config.get('legend', {}))
        legendConfigs.update(plots_path.get('legend', {}))
        legend_numColumns = legendConfigs.get('numcolumns', 1)
        if "numcolumns" in legendConfigs: del legendConfigs['numcolumns']
        legend = Legend(len(h), **legendConfigs)
        legend.SetNColumns(legend_numColumns)

        # scale the histograms before doing anything else
def plot_central_and_systematics(channel):
    global variable, translate_options, k_value, b_tag_bin, maximum, categories
    ROOT.TH1.SetDefaultSumw2(False)
    ROOT.gROOT.SetBatch(True)
    ROOT.gROOT.ProcessLine('gErrorIgnoreLevel = 1001;')
    plotting.setStyle()
    gStyle.SetTitleYOffset(1.4)
    ROOT.gROOT.ForceStyle()
    canvas = Canvas(width=700, height=500)
    canvas.SetLeftMargin(0.15)
    canvas.SetBottomMargin(0.15)
    canvas.SetTopMargin(0.05)
    canvas.SetRightMargin(0.05)
    legend = plotting.create_legend(x0=0.6, y1=0.5)

    hist_data_central = read_xsection_measurement_results(
        'central', channel)[0]['unfolded']

    hist_data_central.GetXaxis().SetTitle(translate_options[variable] +
                                          ' [GeV]')
    hist_data_central.GetYaxis().SetTitle('#frac{1}{#sigma} #frac{d#sigma}{d' +
                                          translate_options[variable] +
                                          '} [GeV^{-1}]')
    hist_data_central.GetXaxis().SetTitleSize(0.05)
    hist_data_central.GetYaxis().SetTitleSize(0.05)
    hist_data_central.SetMinimum(0)
    hist_data_central.SetMaximum(maximum[variable])
    hist_data_central.SetMarkerSize(1)
    hist_data_central.SetMarkerStyle(20)
    #    plotAsym = TGraphAsymmErrors(hist_data)
    #    plotStatErr = TGraphAsymmErrors(hist_data)
    gStyle.SetEndErrorSize(20)
    hist_data_central.Draw('P')
    #    plotStatErr.Draw('same P')
    #    plotAsym.Draw('same P Z')
    legend.AddEntry(hist_data_central, 'measured (unfolded)', 'P')

    for systematic in categories:
        if systematic != 'central':
            hist_data_systematic = read_xsection_measurement_results(
                systematic, channel)[0]['unfolded']
            hist_data_systematic.SetMarkerSize(0.5)
            hist_data_systematic.SetMarkerStyle(20)
            colour_number = categories.index(systematic) + 1
            if colour_number == 10:
                colour_number = 42
            hist_data_systematic.SetMarkerColor(colour_number)
            hist_data_systematic.Draw('same P')
            legend.AddEntry(hist_data_systematic, systematic, 'P')


#    for central_generator in ['MADGRAPH', 'POWHEG', 'MCATNLO']:
#        hist_MC = read_xsection_measurement_results('central', channel)[0][central_generator]
#        hist_MC.SetLineStyle(7)
#        hist_MC.SetLineWidth(2)
#        #setting colours
#        if central_generator == 'POWHEG':
#            hist_MC.SetLineColor(kBlue)
#        elif central_generator == 'MADGRAPH':
#            hist_MC.SetLineColor(kRed + 1)
#        elif central_generator == 'MCATNLO':
#            hist_MC.SetLineColor(kMagenta + 3)
#        hist_MC.Draw('hist same')
#legend.AddEntry(hist_MC, translate_options[central_generator], 'l')

    legend.Draw()

    mytext = TPaveText(0.5, 0.97, 1, 1.01, "NDC")
    channelLabel = TPaveText(0.18, 0.97, 0.5, 1.01, "NDC")
    if channel == 'electron':
        channelLabel.AddText(
            "e, %s, %s, k_v = %s" %
            ("#geq 4 jets", b_tag_bins_latex[b_tag_bin], k_value))
    elif channel == 'muon':
        channelLabel.AddText(
            "#mu, %s, %s, k_v = %s" %
            ("#geq 4 jets", b_tag_bins_latex[b_tag_bin], k_value))
    else:
        channelLabel.AddText(
            "combined, %s, %s, k_v = %s" %
            ("#geq 4 jets", b_tag_bins_latex[b_tag_bin], k_value))
    mytext.AddText("CMS Preliminary, L = %.1f fb^{-1} at #sqrt{s} = 8 TeV" %
                   (5.8))

    mytext.SetFillStyle(0)
    mytext.SetBorderSize(0)
    mytext.SetTextFont(42)
    mytext.SetTextAlign(13)

    channelLabel.SetFillStyle(0)
    channelLabel.SetBorderSize(0)
    channelLabel.SetTextFont(42)
    channelLabel.SetTextAlign(13)
    mytext.Draw()
    if not channel == 'combination':
        channelLabel.Draw()

    canvas.Modified()
    canvas.Update()

    path = save_path + '/' + variable
    make_folder_if_not_exists(path)
    canvas.SaveAs(path + '/normalised_xsection_' + channel + '_altogether_kv' +
                  str(k_value) + '.png')
    canvas.SaveAs(path + '/normalised_xsection_' + channel + '_altogether_kv' +
                  str(k_value) + '.pdf')
def make_plots_ROOT(histograms, category, save_path, histname, channel):
    global variable, translateOptions, k_value, b_tag_bin, maximum
    ROOT.TH1.SetDefaultSumw2(False)
    ROOT.gROOT.SetBatch(True)
    ROOT.gROOT.ProcessLine('gErrorIgnoreLevel = 1001;')
    plotting.setStyle()
    gStyle.SetTitleYOffset(2.)
    ROOT.gROOT.ForceStyle()
    canvas = Canvas(width=700, height=500)
    canvas.SetLeftMargin(0.18)
    canvas.SetBottomMargin(0.15)
    canvas.SetTopMargin(0.05)
    canvas.SetRightMargin(0.05)
    legend = plotting.create_legend(x0=0.6, y1=0.5)

    hist_data = histograms['unfolded']
    hist_data.GetXaxis().SetTitle(translate_options[variable] + ' [GeV]')
    hist_data.GetYaxis().SetTitle('#frac{1}{#sigma} #frac{d#sigma}{d' +
                                  translate_options[variable] + '} [GeV^{-1}]')
    hist_data.GetXaxis().SetTitleSize(0.05)
    hist_data.GetYaxis().SetTitleSize(0.05)
    hist_data.SetMinimum(0)
    hist_data.SetMaximum(maximum[variable])
    hist_data.SetMarkerSize(1)
    hist_data.SetMarkerStyle(8)
    plotAsym = TGraphAsymmErrors(hist_data)
    plotStatErr = TGraphAsymmErrors(hist_data)

    xsections = read_unfolded_xsections(channel)
    bins = variable_bins_ROOT[variable]
    assert (len(bins) == len(xsections['central']))

    for bin_i in range(len(bins)):
        scale = 1  # / width
        centralresult = xsections['central'][bin_i]
        fit_error = centralresult[1]
        uncertainty = calculateTotalUncertainty(xsections, bin_i)
        uncertainty_total_plus = uncertainty['Total+'][0]
        uncertainty_total_minus = uncertainty['Total-'][0]
        uncertainty_total_plus, uncertainty_total_minus = symmetriseErrors(
            uncertainty_total_plus, uncertainty_total_minus)
        error_up = sqrt(fit_error**2 + uncertainty_total_plus**2) * scale
        error_down = sqrt(fit_error**2 + uncertainty_total_minus**2) * scale
        plotStatErr.SetPointEYhigh(bin_i, fit_error * scale)
        plotStatErr.SetPointEYlow(bin_i, fit_error * scale)
        plotAsym.SetPointEYhigh(bin_i, error_up)
        plotAsym.SetPointEYlow(bin_i, error_down)

    gStyle.SetEndErrorSize(20)
    plotAsym.SetLineWidth(2)
    plotStatErr.SetLineWidth(2)
    hist_data.Draw('P')
    plotStatErr.Draw('same P')
    plotAsym.Draw('same P Z')
    legend.AddEntry(hist_data, 'unfolded', 'P')

    hist_measured = histograms['measured']
    hist_measured.SetMarkerSize(1)
    hist_measured.SetMarkerStyle(20)
    hist_measured.SetMarkerColor(2)
    #hist_measured.Draw('same P')
    #legend.AddEntry(hist_measured, 'measured', 'P')

    for key, hist in sorted(histograms.iteritems()):
        if not 'unfolded' in key and not 'measured' in key:
            hist.SetLineStyle(7)
            hist.SetLineWidth(2)
            # setting colours
            if 'POWHEG' in key or 'matchingdown' in key:
                hist.SetLineColor(kBlue)
            elif 'MADGRAPH' in key or 'matchingup' in key:
                hist.SetLineColor(kRed + 1)
            elif 'MCATNLO' in key or 'scaleup' in key:
                hist.SetLineColor(kGreen - 3)
            elif 'scaledown' in key:
                hist.SetLineColor(kMagenta + 3)
            hist.Draw('hist same')
            legend.AddEntry(hist, translate_options[key], 'l')

    legend.Draw()

    mytext = TPaveText(0.5, 0.97, 1, 1.01, "NDC")
    channelLabel = TPaveText(0.18, 0.97, 0.5, 1.01, "NDC")
    if 'electron' in histname:
        channelLabel.AddText(
            "e, %s, %s, k = %s" %
            ("#geq 4 jets", b_tag_bins_latex[b_tag_bin], k_value))
    elif 'muon' in histname:
        channelLabel.AddText(
            "#mu, %s, %s, k = %s" %
            ("#geq 4 jets", b_tag_bins_latex[b_tag_bin], k_value))
    else:
        channelLabel.AddText(
            "combined, %s, %s, k = %s" %
            ("#geq 4 jets", b_tag_bins_latex[b_tag_bin], k_value))
    mytext.AddText("CMS Preliminary, L = %.1f fb^{-1} at #sqrt{s} = 8 TeV" %
                   (5.8))

    mytext.SetFillStyle(0)
    mytext.SetBorderSize(0)
    mytext.SetTextFont(42)
    mytext.SetTextAlign(13)

    channelLabel.SetFillStyle(0)
    channelLabel.SetBorderSize(0)
    channelLabel.SetTextFont(42)
    channelLabel.SetTextAlign(13)
    mytext.Draw()
    channelLabel.Draw()

    canvas.Modified()
    canvas.Update()

    path = save_path + '/' + variable + '/' + category
    make_folder_if_not_exists(path)
    canvas.SaveAs(path + '/' + histname + '_kv' + str(k_value) + '.png')
    canvas.SaveAs(path + '/' + histname + '_kv' + str(k_value) + '.pdf')
def plot_fit_results(histograms, category, channel):
    global variable, translate_options, b_tag_bin, save_path
    #ROOT.TH1.SetDefaultSumw2(False)
    ROOT.gROOT.SetBatch(True)
    ROOT.gROOT.ProcessLine('gErrorIgnoreLevel = 1001;')
    plotting.setStyle()
    gStyle.SetTitleYOffset(1.4)
    ROOT.gROOT.ForceStyle()

    for variable_bin in variable_bins_ROOT[variable]:
        path = save_path + '/' + variable + '/' + category + '/fit_results/'
        make_folder_if_not_exists(path)
        plotname = path + channel + '_bin_' + variable_bin + '.png'
        # check if template plots exist already
        if os.path.isfile(plotname):
            continue
        canvas = Canvas(width=700, height=500)
        canvas.SetLeftMargin(0.15)
        canvas.SetBottomMargin(0.15)
        canvas.SetTopMargin(0.05)
        canvas.SetRightMargin(0.05)
        legend = plotting.create_legend(x0=0.7, y1=0.8)
        h_data = histograms[variable_bin]['data']
        h_signal = histograms[variable_bin]['signal']
        h_background = histograms[variable_bin]['background']

        h_data.GetXaxis().SetTitle('Lepton #eta')
        h_data.GetYaxis().SetTitle('Number of Events')
        h_data.GetXaxis().SetTitleSize(0.05)
        h_data.GetYaxis().SetTitleSize(0.05)
        h_data.SetMinimum(0)
        h_data.SetMarkerSize(1)
        h_data.SetMarkerStyle(20)
        gStyle.SetEndErrorSize(20)
        h_data.Draw('P')

        h_signal.SetFillColor(kRed + 1)
        h_background.SetFillColor(kGreen - 3)
        h_signal.SetLineWidth(2)
        h_background.SetLineWidth(2)
        h_signal.SetFillStyle(1001)
        h_background.SetFillStyle(1001)

        mcStack = THStack("MC", "MC")
        mcStack.Add(h_background)
        mcStack.Add(h_signal)

        mcStack.Draw('hist same')
        h_data.Draw('error P same')
        legend.AddEntry(h_data, 'data', 'P')
        legend.AddEntry(h_signal, 'signal', 'F')
        legend.AddEntry(h_background, 'background', 'F')
        legend.Draw()

        mytext = TPaveText(0.5, 0.97, 1, 1.01, "NDC")
        channelLabel = TPaveText(0.18, 0.97, 0.5, 1.01, "NDC")
        if channel == 'electron':
            channelLabel.AddText("e, %s, %s" %
                                 ("#geq 4 jets", b_tag_bins_latex[b_tag_bin]))
        elif channel == 'muon':
            channelLabel.AddText("#mu, %s, %s" %
                                 ("#geq 4 jets", b_tag_bins_latex[b_tag_bin]))
        else:
            channelLabel.AddText("combined, %s, %s" %
                                 ("#geq 4 jets", b_tag_bins_latex[b_tag_bin]))
        mytext.AddText(
            "CMS Preliminary, L = %.1f fb^{-1} at #sqrt{s} = 8 TeV" % (5.8))

        mytext.SetFillStyle(0)
        mytext.SetBorderSize(0)
        mytext.SetTextFont(42)
        mytext.SetTextAlign(13)

        channelLabel.SetFillStyle(0)
        channelLabel.SetBorderSize(0)
        channelLabel.SetTextFont(42)
        channelLabel.SetTextAlign(13)
        mytext.Draw()
        channelLabel.Draw()

        canvas.Modified()
        canvas.Update()
        canvas.SaveAs(plotname)
        canvas.SaveAs(plotname.replace('png', 'pdf'))
def make_template_plots(histograms, category, channel):
    global variable, translate_options, b_tag_bin, save_path
    ROOT.TH1.SetDefaultSumw2(False)
    ROOT.gROOT.SetBatch(True)
    ROOT.gROOT.ProcessLine('gErrorIgnoreLevel = 1001;')
    plotting.setStyle()
    gStyle.SetTitleYOffset(1.4)
    ROOT.gROOT.ForceStyle()

    for variable_bin in variable_bins_ROOT[variable]:
        path = save_path + '/' + variable + '/' + category + '/fit_templates/'
        make_folder_if_not_exists(path)
        plotname = path + channel + '_templates_bin_' + variable_bin + '.png'
        # check if template plots exist already
        if os.path.isfile(plotname):
            continue
        canvas = Canvas(width=700, height=500)
        canvas.SetLeftMargin(0.15)
        canvas.SetBottomMargin(0.15)
        canvas.SetTopMargin(0.05)
        canvas.SetRightMargin(0.05)
        legend = plotting.create_legend(x0=0.7, y1=0.8)
        h_signal = histograms[variable_bin]['signal']
        h_VJets = histograms[variable_bin]['V+Jets']
        h_QCD = histograms[variable_bin]['QCD']

        h_signal.GetXaxis().SetTitle('Lepton #eta')
        h_signal.GetYaxis().SetTitle('Normalised Events')
        h_signal.GetXaxis().SetTitleSize(0.05)
        h_signal.GetYaxis().SetTitleSize(0.05)
        h_signal.SetMinimum(0)
        h_signal.SetMaximum(0.2)
        h_signal.SetLineWidth(2)
        h_VJets.SetLineWidth(2)
        h_QCD.SetLineWidth(2)
        h_signal.SetLineColor(kRed + 1)
        h_VJets.SetLineColor(kBlue)
        h_QCD.SetLineColor(kYellow)
        h_signal.Draw('hist')
        h_VJets.Draw('hist same')
        h_QCD.Draw('hist same')
        legend.AddEntry(h_signal, 'signal', 'l')
        legend.AddEntry(h_VJets, 'V+Jets', 'l')
        legend.AddEntry(h_QCD, 'QCD', 'l')
        legend.Draw()

        mytext = TPaveText(0.5, 0.97, 1, 1.01, "NDC")
        channelLabel = TPaveText(0.18, 0.97, 0.5, 1.01, "NDC")
        if channel == 'electron':
            channelLabel.AddText("e, %s, %s" %
                                 ("#geq 4 jets", b_tag_bins_latex[b_tag_bin]))
        elif channel == 'muon':
            channelLabel.AddText("#mu, %s, %s" %
                                 ("#geq 4 jets", b_tag_bins_latex[b_tag_bin]))
        else:
            channelLabel.AddText("combined, %s, %s" %
                                 ("#geq 4 jets", b_tag_bins_latex[b_tag_bin]))
        mytext.AddText(
            "CMS Preliminary, L = %.1f fb^{-1} at #sqrt{s} = 8 TeV" % (5.8))

        mytext.SetFillStyle(0)
        mytext.SetBorderSize(0)
        mytext.SetTextFont(42)
        mytext.SetTextAlign(13)

        channelLabel.SetFillStyle(0)
        channelLabel.SetBorderSize(0)
        channelLabel.SetTextFont(42)
        channelLabel.SetTextAlign(13)
        mytext.Draw()
        channelLabel.Draw()

        canvas.Modified()
        canvas.Update()
        canvas.SaveAs(plotname)
        canvas.SaveAs(plotname.replace('png', 'pdf'))
Exemple #12
0
def significance_scan_2d(
        x,
        bins=(16, 2000., 6000., 10, 100., 300.),
        xtitle='#font[12]{H}_{#font[132]{T}}  [GeV]',
        ytitle='#font[12]{E}_{#font[132]{T}}^{#font[132]{miss}}  [GeV]',
        selection='',
        weight=''):
    """
    The signficance is calculated using Cowan's analytic formula for discovery
    significance of a signal with background and uncertainty on the background.
    See: https://www.pp.rhul.ac.uk/~cowan/stat/notes/medsigNote.pdf
    """

    global data
    global bkgs
    global sigs
    global treename
    global datasearchpath
    global datadrivensearchpath
    global bkgsearchpath
    global sigsearchpath
    global lumi

    assert x
    assert len(bins) == 6
    assert bkgs
    assert sigs
    assert lumi
    assert bkgsearchpath
    assert sigsearchpath

    save = ['pdf', 'png']

    ## only use first sig
    assert len(sigs) == 1
    sig = sigs[0]

    ## save stuff to bookkeep and return
    stuff = dict()
    stuff['x'] = x

    ## get background histograms
    h_bkgs = list()
    n_bkgs = list()
    if bkgs:
        for bkg in bkgs:
            if isinstance(bkg, list):
                h_subtotal = None
                for dsid in bkg:
                    assert isinstance(dsid, str)
                    h_bkg = None
                    if dsid.isdigit():
                        ## mc backgrounds
                        sp = bkgsearchpath % int(dsid)
                        newx = '%s::%s::%s' % (sp, treename, x)
                        h_bkg = ipyhep.tree.project(newx,
                                                    bins=bins,
                                                    xtitle=xtitle,
                                                    ytitle=ytitle,
                                                    selection=selection,
                                                    weight=weight)
                    else:
                        ## data-driven backgrounds
                        assert dsid == 'fakes' or dsid == 'efakes'
                        sp = datadrivensearchpath
                        newx = '%s::%s::%s' % (sp, treename, x)
                        h_bkg = ipyhep.tree.project(newx,
                                                    bins=bins,
                                                    xtitle=xtitle,
                                                    ytitle=ytitle,
                                                    selection=selection,
                                                    weight=weight)
                    if h_bkg:
                        if h_subtotal:
                            h_subtotal.Add(h_bkg)
                        else:
                            h_subtotal = h_bkg.Clone()
                if h_subtotal:
                    h_bkgs.append(h_subtotal)
                    dsid = bkg[
                        0]  ## lists of combined backgrounds use the first dsid
                    n_bkgs.append(dsid)
            else:
                dsid = bkg
                assert isinstance(dsid, str)
                h_bkg = None
                if dsid.isdigit():
                    ## mc backgrounds
                    sp = bkgsearchpath % int(dsid)
                    newx = '%s::%s::%s' % (sp, treename, x)
                    h_bkg = ipyhep.tree.project(newx,
                                                bins=bins,
                                                xtitle=xtitle,
                                                ytitle=ytitle,
                                                selection=selection,
                                                weight=weight)
                else:
                    ## data-driven backgrounds
                    assert dsid == 'fakes' or dsid == 'efakes'
                    sp = datadrivensearchpath
                    newx = '%s::%s::%s' % (sp, treename, x)
                    h_bkg = ipyhep.tree.project(newx,
                                                bins=bins,
                                                xtitle=xtitle,
                                                ytitle=ytitle,
                                                selection=selection,
                                                weight=weight)
                if h_bkg:
                    h_bkgs.append(h_bkg)
                    n_bkgs.append(dsid)
    assert h_bkgs

    ## get signal histograms
    h_sig = None
    if sig:
        dsid = sig
        sp = sigsearchpath % int(dsid)
        newx = '%s::%s::%s' % (sp, treename, x)
        h_sig = ipyhep.tree.project(newx,
                                    bins=bins,
                                    xtitle=xtitle,
                                    ytitle=ytitle,
                                    selection=selection,
                                    weight=weight)
    assert h_sig

    ## scale background histograms
    if h_bkgs:
        assert len(h_bkgs) == len(n_bkgs), '%s\n%s' % (h_bkgs, n_bkgs)
        for h, dsid in zip(h_bkgs, n_bkgs):
            sf = ipyhep.sampleops.get_sf(dsid)
            if dsid.isdigit():
                sf *= lumi / __ntuple_lumi
            h.Scale(sf)

    ## scale signal histogram
    if h_sig:
        sf = ipyhep.sampleops.get_sf(sig)
        sf *= lumi / __ntuple_lumi
        h_sig.Scale(sf)

    ## set systematic errors on backgrounds
    if h_bkgs:
        assert len(h_bkgs) == len(n_bkgs), '%s\n%s' % (h_bkgs, n_bkgs)

        # HACK: systematics updated 2017-06-08 for the SUSY diphoton analysis
        syst_fracs = dict()
        syst_fracs['407013'] = 0.50  # #gamma#gamma
        syst_fracs['361039'] = 0.50  # #gammaj+jj
        syst_fracs['fakes'] = 0.50  # fakes
        syst_fracs['efakes'] = 0.20  # efakes
        syst_fracs['301890'] = 0.20  # W#gamma
        syst_fracs['301899'] = 0.20  # Z#gamma
        syst_fracs['407022'] = 0.27  # W#gamma#gamma
        syst_fracs['407025'] = 0.45  # Z#gamma#gamma
        syst_fracs['407028'] = 0.45  # Z#gamma#gamma

        for h, dsid in zip(h_bkgs, n_bkgs):
            nbinsx = h.GetNbinsX()
            nbinsy = h.GetNbinsY()
            for i_x in xrange(nbinsx):
                for i_y in xrange(nbinsy):
                    c = h.GetBinContent(i_x, i_y)
                    e = h.GetBinError(i_x, i_y)
                    syst = syst_fracs.get(dsid, 0.0)
                    assert syst
                    h.SetBinError(i_x, i_y,
                                  math.sqrt(e * e + c * c * syst * syst))

    ## total background
    h_bkg_total = None
    if h_bkgs:
        for h_bkg in h_bkgs:
            if h_bkg_total:
                h_bkg_total.Add(h_bkg)
            else:
                h_bkg_total = h_bkg.Clone()
    assert h_bkg_total

    ## significance scan
    h_signif = Hist2D(*bins)
    h_signif.SetTitle(';%s;%s' % (xtitle, ytitle))
    nbinsx = h_signif.GetNbinsX()
    nbinsy = h_signif.GetNbinsY()
    for i_x in xrange(1, nbinsx + 1):
        for i_y in xrange(1, nbinsy + 1):
            sigma = ROOT.Double(0)
            s = h_sig.Integral(i_x, nbinsx + 1, i_y, nbinsy + 1)
            b = h_bkg_total.IntegralAndError(i_x, nbinsx + 1, i_y, nbinsy + 1,
                                             sigma)
            sigma = float(sigma)
            #            if b < 0.10: ## HACK: force background to be at least 0.10 events with 100% uncert.
            #                b = 0.10
            #                sigma = 0.10
            if b < 0.20 and sigma < 0.20:
                sigma = 0.20
            za = _calculate_significance(s, b, sigma)
            xval = h_signif.GetXaxis().GetBinLowEdge(i_x)
            yval = h_signif.GetYaxis().GetBinLowEdge(i_y)
            h_signif.Fill(xval, yval, za)

    ## make canvas
    canvas = Canvas(800, 600)
    canvas.SetRightMargin(0.17)
    canvas.cd()
    stuff['canvas'] = canvas

    ## draw
    ROOT.gStyle.SetPaintTextFormat("3.1f")
    h_signif.Draw('COLZ')
    h_signif2 = h_signif.Clone()
    h_signif2.SetMarkerSize(1.2)
    h_signif2.SetMarkerColor(ipyhep.style.black)
    h_signif2.Draw('TEXT SAME')
    stuff['h_signif'] = h_signif
    stuff['h_signif2'] = h_signif2

    canvas.Update()

    ## save figures
    if save:
        ipyhep.file.save_figures(canvas, x, save)

    return stuff