Example #1
0
 def _plotFigure(self):
     self.ax = plt.subplot(self.gs[0])
     for i, hist in enumerate(self.hists):
         if self.plotType[i] == 'Data':
             rplt.errorbar(hist,
                           label=self.label[i],
                           markerfacecolor=self.colour[i],
                           markersize=8,
                           xerr=False,
                           yerr=False,
                           elinewidth=0,
                           markeredgewidth=0,
                           markeredgecolor=self.colour[i],
                           emptybins=False,
                           zorder=self.zorder[i],
                           axes=self.ax)
         elif self.plotType[i] == 'Simulation':
             rplt.hist(hist,
                       label=self.label[i],
                       color=self.colour[i],
                       alpha=self.alpha[i],
                       fill=self.fill[i],
                       zorder=self.zorder[i],
                       axes=self.ax)
         elif self.plotType[i] == 'CMSSW':
             rplt.step(hist,
                       label=self.label[i],
                       color=self.colour[i],
                       zorder=self.zorder[i],
                       linewidth=2,
                       axes=self.ax)
         else:
             pass
     return None
Example #2
0
def plot_weights(input_filename):
    weights = parse_weights(input_filename)

    hist = Hist(
        np.logspace(math.log(float(1e-8), math.e),
                    math.log(1, math.e),
                    25,
                    base=np.e))

    hist.fill_array(weights)

    # plt.hist(weights, bins=200, normed=1)
    rplt.hist(hist)

    print max(weights)

    # print "Total number of events = ", len(weights)

    plt.xscale("log")
    plt.yscale("log")

    plt.autoscale()

    plt.xlabel("Weight")
    plt.ylabel("# of Events")

    plt.savefig("plots/weights.pdf")
    plt.clf()
Example #3
0
def plot_weights(input_filename):
	weights = parse_weights(input_filename)

	hist = Hist(np.logspace(math.log(float(1e-8), math.e), math.log(1, math.e), 25, base=np.e))

	hist.fill_array(weights)

	# plt.hist(weights, bins=200, normed=1)
	rplt.hist(hist)

	print max(weights)


	# print "Total number of events = ", len(weights)

	plt.xscale("log")
	plt.yscale("log")

	plt.autoscale()

	plt.xlabel("Weight")
	plt.ylabel("# of Events")

	plt.savefig("plots/weights.pdf")
	plt.clf()
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))
def make_plots_matplotlib(histograms, category, output_folder, histname):
    global variable, variables_latex_matplotlib, measurements_latex_matplotlib, k_value
    
    channel = 'electron'
    if 'electron' in histname:
        channel = 'electron'
    elif 'muon' in histname:
        channel = 'muon'
    else:
        channel = 'combined'
        
    # plot with matplotlib
    hist_data = histograms['unfolded']
    hist_measured = histograms['measured']
    
    hist_data.markersize = 2
    hist_measured.markersize = 2
    hist_data.marker = 'o'
    hist_measured.marker = 'o'
    hist_measured.color = 'red'

    plt.figure(figsize=(14, 10), dpi=200, facecolor='white')
    axes = plt.axes()
    axes.minorticks_on()
    
    plt.xlabel('$%s$ [GeV]' % variables_latex_matplotlib[variable], CMS.x_axis_title)
    plt.ylabel(r'$\frac{1}{\sigma} \times \frac{d\sigma}{d' + variables_latex_matplotlib[variable] + '} \left[\mathrm{GeV}^{-1}\\right]$', CMS.y_axis_title)
    plt.tick_params(**CMS.axis_label_major)
    plt.tick_params(**CMS.axis_label_minor)

    rplt.errorbar(hist_data, axes=axes, label='unfolded', xerr=False)
    rplt.errorbar(hist_measured, axes=axes, label='measured', xerr=False)
    
    for key, hist in histograms.iteritems():
        if not 'unfolded' in key and not 'measured' in key:
            hist.linestyle = 'dashed'
            hist.linewidth = 2
#            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(kMagenta + 3)
            elif 'scaledown' in key:
                hist.SetLineColor(kGreen)
            rplt.hist(hist, axes=axes, label=measurements_latex_matplotlib[key])
    
    plt.legend(numpoints=1, loc='upper right', prop=CMS.legend_properties)
    plt.title(get_cms_labels_matplotlib(channel), CMS.title)
    plt.tight_layout()

    path = output_folder + str(measurement_config.centre_of_mass) + 'TeV/' + variable + '/' + category
    make_folder_if_not_exists(path)
    for output_format in output_formats:
        plt.savefig(path + '/' + histname + '_kv' + str(k_value) + '.' + output_format)
Example #6
0
def make_plot( histogram, histogram_label, histogram_properties = Histogram_properties(),
                                 save_folder = 'plots/',
                                 save_as = ['pdf', 'png'],
                                 normalise = False,
                                 draw_errorbar = False,
                                 draw_legend = True
                                 ):
    
    histogram.SetTitle( histogram_label )
#    histogram.SetMarkerSize(CMS.data_marker_size)
    # to be changed
    histogram.fillcolor = '0.75'
    histogram.fillstyle = 'solid'
    if normalise:
        histogram.Scale( 1 / histogram.Integral() )
    
    # plot with matplotlib
    plt.figure( figsize = CMS.figsize, dpi = CMS.dpi, facecolor = CMS.facecolor )
    axes = plt.axes()
    
    if draw_errorbar:
        rplt.errorbar( histogram, xerr = False, emptybins = False, axes = axes, elinewidth = 2, capsize = 10, capthick = 2 )
    else:
        rplt.hist( histogram )
    

    if draw_legend:
        plt.legend( numpoints = 1, loc = histogram_properties.legend_location, prop = CMS.legend_properties )
    
    adjust_axis_limits( axes, histogram_properties )

    x_limits = histogram_properties.x_limits
    y_limits = histogram_properties.y_limits
    if len( x_limits ) == 2:
        axes.set_xlim( xmin = x_limits[0], xmax = x_limits[1] )
    if len( y_limits ) == 2:
        axes.set_ylim( ymin = y_limits[0], ymax = y_limits[1] )
    
    if histogram_properties.set_log_y:
        axes.set_yscale( 'log', nonposy = "clip" )
        if not len( histogram_properties.y_limits ) == 2:  # if not user set y-limits, calculate the limits from the tuple values
            value_range = sorted( list( histogram.y() ) )
            for i, value in enumerate(value_range):
                if value == 0:
                    del value_range[i]
            axes.set_ylim( ymin = min(value_range)/10, ymax = max(value_range)*10 )

    set_labels( plt, histogram_properties )

    if CMS.tight_layout:
        plt.tight_layout()
    
    for save in save_as:
        plt.savefig( save_folder + histogram_properties.name + '.' + save )
    plt.close()
Example #7
0
def makePurityStabilityPlots(input_file, histogram, bin_edges, channel, variable, isVisiblePhaseSpace):
    global output_folder, output_formats
 
    hist = get_histogram_from_file( histogram, input_file )
    print "bin edges contents   : ", bin_edges
    new_hist = rebin_2d( hist, bin_edges, bin_edges ).Clone()

    # get_bin_content = hist.ProjectionX().GetBinContent
    purities = calculate_purities( new_hist.Clone() )
    stabilities = calculate_stabilities( new_hist.Clone() )
    # n_events = [int( get_bin_content( i ) ) for i in range( 1, len( bin_edges ) )]
    print "purities contents    : ", purities
    print "stabilities contents : ", stabilities

    hist_stability = value_tuplelist_to_hist(stabilities, bin_edges)
    hist_purity = value_tuplelist_to_hist(purities, bin_edges)

    hist_purity.color = 'red'
    hist_stability.color = 'blue'

    hist_stability.linewidth = 4
    hist_purity.linewidth = 4

    x_limits = [bin_edges[0], bin_edges[-1]]
    y_limits = [0,1]
    plt.figure( figsize = ( 20, 16 ), dpi = 200, facecolor = 'white' )

    ax0 = plt.axes()
    ax0.minorticks_on()
#     ax0.grid( True, 'major', linewidth = 2 )
#     ax0.grid( True, 'minor' )
    plt.tick_params( **CMS.axis_label_major )
    plt.tick_params( **CMS.axis_label_minor )
    ax0.xaxis.labelpad = 12
    ax0.yaxis.labelpad = 12
    rplt.hist( hist_stability , stacked=False, axes = ax0, cmap = my_cmap, vmin = 1, label = 'Stability' )
    rplt.hist( hist_purity, stacked=False, axes = ax0, cmap = my_cmap, vmin = 1, label = 'Purity' )

    ax0.set_xlim( x_limits )
    ax0.set_ylim( y_limits )

    plt.tick_params( **CMS.axis_label_major )
    plt.tick_params( **CMS.axis_label_minor )

    x_title = '$' + variables_latex[variable] + '$ [GeV]'
    plt.xlabel( x_title, CMS.x_axis_title )

    leg = plt.legend(loc=4,prop={'size':40})

    plt.tight_layout()

    plt.savefig('test.pdf')
    save_as_name = 'purityStability_'+channel + '_' + variable + '_' + str(options.CoM) + 'TeV'
    for output_format in output_formats:
        plt.savefig( output_folder + save_as_name + '.' + output_format )
def makePurityStabilityPlots(input_file, histogram, bin_edges, channel, variable, isVisiblePhaseSpace):
    global output_folder, output_formats
 
    hist = get_histogram_from_file( histogram, input_file )
    print "bin edges contents   : ", bin_edges
    new_hist = rebin_2d( hist, bin_edges, bin_edges ).Clone()

    # get_bin_content = hist.ProjectionX().GetBinContent
    purities = calculate_purities( new_hist.Clone() )
    stabilities = calculate_stabilities( new_hist.Clone() )
    # n_events = [int( get_bin_content( i ) ) for i in range( 1, len( bin_edges ) )]
    print "purities contents    : ", purities
    print "stabilities contents : ", stabilities

    hist_stability = value_tuplelist_to_hist(stabilities, bin_edges)
    hist_purity = value_tuplelist_to_hist(purities, bin_edges)

    hist_purity.color = 'red'
    hist_stability.color = 'blue'

    hist_stability.linewidth = 4
    hist_purity.linewidth = 4

    x_limits = [bin_edges[0], bin_edges[-1]]
    y_limits = [0,1]
    plt.figure( figsize = ( 20, 16 ), dpi = 200, facecolor = 'white' )

    ax0 = plt.axes()
    ax0.minorticks_on()
#     ax0.grid( True, 'major', linewidth = 2 )
#     ax0.grid( True, 'minor' )
    plt.tick_params( **CMS.axis_label_major )
    plt.tick_params( **CMS.axis_label_minor )
    ax0.xaxis.labelpad = 12
    ax0.yaxis.labelpad = 12
    rplt.hist( hist_stability , stacked=False, axes = ax0, cmap = my_cmap, vmin = 1, label = 'Stability' )
    rplt.hist( hist_purity, stacked=False, axes = ax0, cmap = my_cmap, vmin = 1, label = 'Purity' )

    ax0.set_xlim( x_limits )
    ax0.set_ylim( y_limits )

    plt.tick_params( **CMS.axis_label_major )
    plt.tick_params( **CMS.axis_label_minor )

    x_title = '$' + variables_latex[variable] + '$ [GeV]'
    plt.xlabel( x_title, CMS.x_axis_title )

    leg = plt.legend(loc=4,prop={'size':40})

    plt.tight_layout()

    plt.savefig('test.pdf')
    save_as_name = 'purityStability_'+channel + '_' + variable + '_' + str(options.CoM) + 'TeV'
    for output_format in output_formats:
        plt.savefig( output_folder + save_as_name + '.' + output_format )
def make_control_region_comparison(control_region_1, control_region_2,
                                   name_region_1, name_region_2, variable='mttbar',
                                   x_label=r'$m(\mathrm{t}\bar{\mathrm{t}})$ [GeV]',
                                   x_min=300, x_max=1800,
                                   y_label='arbitrary units/ 50 GeV', y_min = 0, y_max = 0.15):
    #make copies in order not to mess with existing histograms
    control_region_1 = deepcopy(control_region_1)
    control_region_2 = deepcopy(control_region_2)
    # normalise as we are comparing shapes
    control_region_1.Scale(1 / control_region_1.Integral())
    control_region_2.Scale(1 / control_region_2.Integral())
    
    ratio = control_region_1.Clone('ratio')
    ratio.Divide(control_region_2)
    ratio.SetMarkerSize(3)
    
    control_region_1.fillcolor = 'yellow'
    control_region_2.fillcolor = 'red'
    control_region_1.fillstyle = 'solid'
    control_region_2.fillstyle = 'solid'
    
    # plot with matplotlib
    plt.figure(figsize=(16, 16), dpi=200, facecolor='white')
    gs = gridspec.GridSpec(2, 1, height_ratios=[5, 1]) 
    ax0 = plt.subplot(gs[0])
    ax0.minorticks_on()
    
    rplt.hist(control_region_1, axes=ax0)
    rplt.hist(control_region_2, axes=ax0, alpha=0.5)
    
    plt.ylabel(y_label, CMS.y_axis_title)
    plt.tick_params(**CMS.axis_label_major)
    plt.tick_params(**CMS.axis_label_minor)
    plt.title(get_title(variable), CMS.title)
    plt.legend([name_region_1 + ' (1)', name_region_2 + ' (2)'], numpoints=1, loc='upper right', prop=CMS.legend_properties)
    ax0.set_xlim(xmin=x_min, xmax=x_max)
    ax0.set_ylim(ymin=y_min, ymax=y_max)
    plt.setp(ax0.get_xticklabels(), visible=False)
    
    ax1 = plt.subplot(gs[1])
    ax1.minorticks_on()
    ax1.grid(True, 'major', linewidth=1)
    ax1.yaxis.set_major_locator(MultipleLocator(1.0))
    ax1.yaxis.set_minor_locator(MultipleLocator(0.5))
    plt.tick_params(**CMS.axis_label_major)
    plt.tick_params(**CMS.axis_label_minor)
    plt.xlabel(x_label, CMS.x_axis_title)
    plt.ylabel('(1)/(2)', CMS.y_axis_title)
    rplt.errorbar(ratio, xerr=True, emptybins=False, axes=ax1)
    ax1.set_xlim(xmin=x_min, xmax=x_max)
    ax1.set_ylim(ymin= -0.5, ymax=4)
    plt.tight_layout()
    plt.savefig(variable + '_shape_comparision.png')  
    plt.savefig(variable + '_shape_comparision.pdf')  
Example #10
0
def draw(fig, h, heid, row):
    ax = plt.subplot2grid(shape=(2,5), loc=(row,0), colspan=2)
    lbls = labels_count if row==1 else [_]
    ax.set_xticklabels(lbls)
    for t in ax.xaxis.get_major_ticks():
        t.label.set_rotation(30)
    rplt.hist(h, axes=ax)
    npass = sum(list(h)[2:])
    p = plt.fill_between([1.5,4.5],[npass,npass], [0.01,0.01])
    p.set_facecolors('none')
    for path in p.get_paths():
        p1 = PathPatch(path, fc='none', hatch='\\\\\\', color='blue')
        ax.add_patch(p1)
        p1.set_zorder(p.get_zorder()-0.1)
    ax.set_yscale('log')
    ylim((0.01,3*h.GetMaximum()))
    text(0.9, 0.75,'charge skim', fontsize=12, color='blue',
         transform=ax.transAxes, horizontalalignment='right', verticalalignment='bottom')
    text(0.9, 0.68, '%.1f'%(100*npass)+'$\%$', fontsize=14, color='blue',
         transform=ax.transAxes, horizontalalignment='right', verticalalignment='top')
    
    ax = plt.subplot2grid(shape=(2,5), loc=(row,2), colspan=3)
    lbls = labels_eid if row==1 else [_]
    ax.set_xticklabels(lbls)
    for t in ax.xaxis.get_major_ticks():
        t.label.set_rotation(30)
    rplt.hist(heid, axes=ax)
    X = [0.5,6.5]
    Y1 = [npass,npass]
    Y2 = [heid.GetMinimum(), heid.GetMinimum()]
    # plot(X, Y2, 'r-.')
    p = plt.fill_between([0.5,1.5],[npass,npass])
    p.set_facecolors('none')
    for path in p.get_paths():
        p1 = PathPatch(path, fc='none', hatch='\\\\\\', color='blue')
        ax.add_patch(p1)
        p1.set_zorder(p.get_zorder()-0.1)
    p = plt.fill_between([5.5,6.5],Y2)
    p.set_facecolors('none')
    for path in p.get_paths():
        p1 = PathPatch(path, fc='none', hatch='xxx', color='green')
        ax.add_patch(p1)
        p1.set_zorder(p.get_zorder()-0.1)
    ylim((0.5*heid.GetMinimum(),1.1*heid.GetMaximum()))
    text(0.95, 0.75, 'refined electron\nidentification', fontsize=12, color='green',
         transform=ax.transAxes, horizontalalignment='right', verticalalignment='bottom')
    text(0.95, 0.68, '%.1f'%(100*heid.GetMinimum())+'$\%$', fontsize=14, color='green',
         transform=ax.transAxes, horizontalalignment='right', verticalalignment='top')
    
    # subplots_adjust(bottom=0.5, wspace=0.6, hspace=0.1)
    subplots_adjust(left=0.1, bottom=0.15, wspace=0.6, hspace=0.1)
    if row==1: # text(0,-0.0025,'selection criteria', fontsize=12)
        text(0.5,0,'selection criteria', fontsize=12, transform=fig.transFigure, horizontalalignment='center')
        text(0, 0.5, 'events per trigger', fontsize=12, transform=fig.transFigure, verticalalignment='center', rotation=90)
Example #11
0
def draw_mem_data_mc(*args, **kwargs):
    a1, a2, hs = draw_data_mc(*args, **kwargs)
    plt.sca(a1)
    h = hs["tth_13TeV_phys14"].Clone()
    h.fillstyle = "hollow"
    h.linewidth = 2
    h.title = h.title + " x10"
    h.Scale(10)
    hist(h)
    plt.legend(loc=(1.01,0.0))
    a1.set_ylim(bottom=0)
    return a1, a2, hs
Example #12
0
def drawMulti(ax, hMeas, hTrue):
    # ax.set_ylim([1e-5,1000])
    ax.set_xlabel(r"$N_{tracks}$")
    ax.set_ylabel(r"$\frac{1}{N_{jets}}\frac{dN}{dN_{tracks}}$", fontsize=18)
    ax.set_yscale("log")
    # ax.set_xscale('log')

    hMeas.linecolor = "blue"
    hTrue.linecolor = "red"
    rplt.hist(hMeas, axes=ax, label="Measured")
    rplt.hist(hTrue, axes=ax, label="True")  # Plot jT histogram,
    ax.set_xlim([0, 30])
Example #13
0
def subtract_bkg(h_sig, sp_sig, sp_bkg, color='k', ax=None, axins=None):
    x = [h_sig.GetBinCenter(x) for x in range(1, h_sig.GetNbinsX())]
    h_new = h_sig.Clone()
    for x in range(0, h_sig.GetNbinsX()):
        h_new.SetBinContent(
            x,
            max(0,
                sp_sig(h_sig.GetBinCenter(x)) - sp_bkg(h_sig.GetBinCenter(x))))
    rplt.hist(h_new, color=color, axes=ax)
    if axins is not None:
        rplt.hist(h_new, color=color, axes=axins)
    return h_new
def saveUnfolding(unfolding, outputfile, **kwargs):
    if not unfolding.unfolded_data:
        print 'Run unfold function first'
        return
    setDrawStyles(unfolding)
    # TODO: change this to be more generic
    plt.figure(figsize=(16, 10), dpi=100)
    rplt.hist(unfolding.truth, label=r'SM $\mathrm{t}\bar{\mathrm{t}}$ truth', stacked=False)
    rplt.hist(unfolding.data, label=r'$\mathrm{t}\bar{\mathrm{t}}$ from fit', stacked=False)
    rplt.errorbar(unfolding.unfolded_data, label='unfolded')
    plt.xlabel('$E_{\mathrm{T}}^{miss}$')
    plt.ylabel('Events')
    plt.title('Unfolding')
    plt.legend()
    plt.savefig(outputfile)
def saveClosureTest(unfolding, outputfile, **kwargs):
    
    if not unfolding.unfolded_closure:
        print 'Run closureTest function first'
        return
    setDrawStyles(unfolding)
    # TODO: change this to be more generic
    plt.figure(figsize=(16, 10), dpi=100)
    rplt.hist(unfolding.truth, label=r'truth', stacked=False)
    rplt.hist(unfolding.measured, label=r'measured', stacked=False)
    rplt.errorbar(unfolding.unfolded_closure, label='unfolded')
    plt.xlabel('$E_{\mathrm{T}}^{miss}$')
    plt.ylabel('Events')
    plt.title('Unfolding closure test')
    plt.legend()
    plt.savefig('Unfolding_' + unfolding.method + '_closureTest.png')
Example #16
0
def make_control_region_data_mc_comparision(
        histograms,
        histogram_name,
        variable,
        x_label=r'$m(\mathrm{t}\bar{\mathrm{t}})$ [GeV]',
        x_min=300,
        x_max=1800,
        y_label='Events/50 GeV'):
    qcd = histograms['QCD'][histogram_name]
    ttjet = histograms['TTJet'][histogram_name]
    wjets = histograms['WJets'][histogram_name]
    zjets = histograms['ZJets'][histogram_name]
    single_top = histograms['SingleTop'][histogram_name]
    other = ttjet + wjets + zjets + single_top
    data = histograms['data'][histogram_name]
    data.SetMarkerSize(3)
    qcd.SetTitle('QCD from data')
    other.SetTitle('Combined other background')
    data.SetTitle('Data')

    qcd.fillcolor = 'yellow'
    other.fillcolor = 'red'

    qcd.fillstyle = 'solid'
    other.fillstyle = 'solid'
    stack = HistStack()
    stack.Add(other)
    stack.Add(qcd)

    # plot with matplotlib
    plt.figure(figsize=(16, 12), dpi=200, facecolor='white')
    axes = plt.axes()

    rplt.hist(stack, stacked=True, axes=axes)
    rplt.errorbar(data, xerr=None, emptybins=False, axes=axes)

    plt.xlabel(x_label, CMS.x_axis_title)
    plt.ylabel(y_label, CMS.y_axis_title)
    plt.tick_params(**CMS.axis_label_major)
    plt.tick_params(**CMS.axis_label_minor)
    plt.title(get_title(variable), CMS.title)
    plt.legend(numpoints=1, loc='upper right', prop=CMS.legend_properties)
    axes.set_xlim(xmin=x_min, xmax=x_max)
    axes.set_ylim(ymin=0)
    plt.tight_layout()
    plt.savefig(variable + '.png')
    plt.savefig(variable + '.pdf')
Example #17
0
def mc_stack(
    hlist,
    hs_syst,
    systematics,
    colors="auto"
    ):
    if colors=="auto":
        coloriter = iter(plt.cm.jet(np.linspace(0,1,len(hlist))))
        for h in hlist:
            h.color = next(coloriter)
    elif isinstance(colors, list) and len(colors) == len(hlist):
        for h, c in zip(hlist, colors):
            h.color = c

    for h in hlist:
        h.fillstyle = "solid"

    #FIXME: Temporary workaround for failed fill, only works when hatch is specified
    stack = hist(hlist, stacked=True, hatch=".", lw=2)
    htot = sum(hlist)
    htot.color="black"

    htot_u = htot.Clone()
    htot_d = htot.Clone()
    for i in range(1, htot.nbins()+1):
        htot_u.set_bin_content(i, htot.get_bin_content(i) + htot.get_bin_error(i))
        htot_d.set_bin_content(i, htot.get_bin_content(i) - htot.get_bin_error(i))

    htot_u.color="black"
    htot_d.color="black"

    fill_between(htot_u, htot_d,
        color="black", hatch="////////",
        alpha=1.0, linewidth=0, facecolor="none", edgecolor="black", zorder=10,
    )

    #add systematic uncertainties
    hstat = htot_u - htot_d
    errs = np.array([y for y in hstat.y()])
    errs = np.abs(errs)

    htot_usyst = htot.Clone()
    htot_dsyst = htot.Clone()
    for systUp, systDown in systematics:
        errs_syst_up = np.array([y for y in sum(hs_syst[systUp].values()).y()])
        errs_syst_down = np.array([y for y in sum(hs_syst[systDown].values()).y()])
        errs_syst = np.abs(errs_syst_up - errs_syst_down)
        errs = np.power(errs, 2) + np.power(errs_syst, 2)
        errs = np.sqrt(errs)
    for i in range(len(errs)):
        htot_usyst.SetBinContent(i+1, htot_usyst.GetBinContent(i+1) + errs[i]/2)
        htot_dsyst.SetBinContent(i+1, htot_dsyst.GetBinContent(i+1) - errs[i]/2)

    fill_between(htot_usyst, htot_dsyst,
        color="gray", hatch=r"\\\\",
        alpha=1.0, linewidth=0, facecolor="none", edgecolor="gray", zorder=10,
    )

    return {"hists":stack, "tot":htot, "tot_u":htot_u, "tot_d":htot_d, "tot_usyst":htot_usyst, "tot_dsyst":htot_dsyst}
def plotting_purity_stability(variable, channel, binning_criteria, bin_edges ):
    '''
    Purity, stability and resolution plots.
    '''
    p = binning_criteria['p_i']
    s = binning_criteria['s_i']

    hist_stability = value_tuplelist_to_hist(s, bin_edges)
    hist_purity = value_tuplelist_to_hist(p, bin_edges)

    hist_purity.color = 'red'
    hist_stability.color = 'blue'

    hist_stability.linewidth = 4
    hist_purity.linewidth = 4

    fig = plt.figure( figsize = ( 20, 16 ), dpi = 200, facecolor = 'white' )
    axes = plt.axes()
    axes.minorticks_on()
    axes.set_xlim( [bin_edges[0], bin_edges[-1]] )
    axes.set_ylim( [0,1] )

    axes.xaxis.labelpad = 12
    axes.yaxis.labelpad = 12

    rplt.hist( hist_stability , stacked=False, axes = axes, label = 'Stability' )
    rplt.hist( hist_purity, stacked=False, axes = axes, label = 'Purity' )

    plt.tick_params( **CMS.axis_label_major )
    plt.tick_params( **CMS.axis_label_minor )

    x_title = '$' + variables_latex[variable] + '$'
    if variable in ['HT', 'ST', 'MET', 'lepton_pt', 'WPT']: x_title += '[GeV]'
    plt.xlabel( x_title, CMS.x_axis_title )

    leg = plt.legend(loc=4,prop={'size':40})

    plt.tight_layout()

    plot_filepath = 'plots/binning/purity_stability/'
    make_folder_if_not_exists(plot_filepath)
    plot_filename = channel + '_' + variable+'_purityStability.pdf'
    fig.savefig(plot_filepath+plot_filename, bbox_inches='tight')
Example #19
0
def plotting_purity_stability(variable, channel, binning_criteria, bin_edges):
    '''
    Purity, stability and resolution plots.
    '''
    p = binning_criteria['p_i']
    s = binning_criteria['s_i']

    hist_stability = value_tuplelist_to_hist(s, bin_edges)
    hist_purity = value_tuplelist_to_hist(p, bin_edges)

    hist_purity.color = 'red'
    hist_stability.color = 'blue'

    hist_stability.linewidth = 4
    hist_purity.linewidth = 4

    fig = plt.figure(figsize=(20, 16), dpi=200, facecolor='white')
    axes = plt.axes()
    axes.minorticks_on()
    axes.set_xlim([bin_edges[0], bin_edges[-1]])
    axes.set_ylim([0, 1])

    axes.xaxis.labelpad = 12
    axes.yaxis.labelpad = 12

    rplt.hist(hist_stability, stacked=False, axes=axes, label='Stability')
    rplt.hist(hist_purity, stacked=False, axes=axes, label='Purity')

    plt.tick_params(**CMS.axis_label_major)
    plt.tick_params(**CMS.axis_label_minor)

    x_title = '$' + variables_latex[variable] + '$'
    if variable in ['HT', 'ST', 'MET', 'lepton_pt', 'WPT']: x_title += '[GeV]'
    plt.xlabel(x_title, CMS.x_axis_title)

    leg = plt.legend(loc=4, prop={'size': 40})

    plt.tight_layout()

    plot_filepath = 'plots/binning/purity_stability/'
    make_folder_if_not_exists(plot_filepath)
    plot_filename = channel + '_' + variable + '_purityStability.pdf'
    fig.savefig(plot_filepath + plot_filename, bbox_inches='tight')
Example #20
0
def draw_shape(f, samples, hn, **kwargs):
    rebin = kwargs.get("rebin", 1)

    hs = []
    for s in samples:
        h = f.get(s[0] + hn).Clone()
        h.Scale(1.0 / h.Integral())
        h.rebin(rebin)
        h.title = s[1]
        hs += [h]

    coloriter = iter(plt.cm.jet(np.linspace(0,1,len(hs))))

    for h in hs:
        h.color = next(coloriter)
        errorbar(h)
    plt.legend()
    for h in hs:
        hist(h, lw=1, ls="-")
def make_control_region_data_mc_comparision(histograms, histogram_name,
                                            variable, x_label=r'$m(\mathrm{t}\bar{\mathrm{t}})$ [GeV]',
                                            x_min=300, x_max=1800,
                                            y_label='Events/50 GeV'):
    qcd = histograms['QCD'][histogram_name]
    ttjet = histograms['TTJet'][histogram_name]
    wjets = histograms['WJets'][histogram_name]
    zjets = histograms['ZJets'][histogram_name]
    single_top = histograms['SingleTop'][histogram_name]
    other = ttjet + wjets + zjets + single_top
    data = histograms['data'][histogram_name]
    data.SetMarkerSize(3)
    qcd.SetTitle('QCD from data')
    other.SetTitle('Combined other background')
    data.SetTitle('Data')
    
    qcd.fillcolor = 'yellow'
    other.fillcolor = 'red'
    
    qcd.fillstyle = 'solid'
    other.fillstyle = 'solid'
    stack = HistStack()
    stack.Add(other)
    stack.Add(qcd)
    
    # plot with matplotlib
    plt.figure(figsize=(16, 12), dpi=200, facecolor='white')
    axes = plt.axes()
    
    rplt.hist(stack, stacked=True, axes=axes)
    rplt.errorbar(data, xerr=False, emptybins=False, axes=axes)
    
    plt.xlabel(x_label, CMS.x_axis_title)
    plt.ylabel(y_label, CMS.y_axis_title)
    plt.tick_params(**CMS.axis_label_major)
    plt.tick_params(**CMS.axis_label_minor)
    plt.title(get_title(variable), CMS.title)
    plt.legend(numpoints=1, loc='upper right', prop=CMS.legend_properties)
    axes.set_xlim(xmin=x_min, xmax=x_max)
    axes.set_ylim(ymin=0)
    plt.tight_layout()
    plt.savefig(variable + '.png')  
    plt.savefig(variable + '.pdf')  
Example #22
0
def make_plot(histogram, histogram_label, histogram_properties = Histogram_properties(),
                                 save_folder='plots/',
                                 save_as=['pdf', 'png'],
                                 normalise = False
                                 ):
    
    histogram.SetTitle(histogram_label)
#    histogram.SetMarkerSize(CMS.data_marker_size)
    #to be changed
    histogram.fillcolor = '0.75'
    histogram.fillstyle = 'solid'
    if normalise:
        histogram.Scale(1/histogram.Integral())
    
    # plot with matplotlib
    plt.figure(figsize=CMS.figsize, dpi=CMS.dpi, facecolor=CMS.facecolor)
    axes = plt.axes()
    
    rplt.hist(histogram)
    
    
    plt.xlabel(histogram_properties.x_axis_title, CMS.x_axis_title)
    plt.ylabel(histogram_properties.y_axis_title, CMS.y_axis_title)
    plt.tick_params(**CMS.axis_label_major)
    plt.tick_params(**CMS.axis_label_minor)
    plt.title(histogram_properties.title, CMS.title)
    
    plt.legend(numpoints=1, loc=histogram_properties.legend_location, prop=CMS.legend_properties)
    
    x_limits = histogram_properties.x_limits
    if len(x_limits) == 2:
        axes.set_xlim(xmin=x_limits[0], xmax=x_limits[1])
    y_limits = histogram_properties.y_limits
    if len(y_limits) == 2:
        axes.set_ylim(ymin=y_limits[0], ymax=y_limits[1])
    else:
        axes.set_ylim(ymin=0)
    plt.tight_layout()    
    
    for save in save_as:
        plt.savefig(save_folder + histogram_properties.name + '.' + save)
    plt.close()
def draw_result(result, axes):
    graph = result["unfolded"]
    graph_with_systematics = result["unfolded_with_systematics"]
    madgraph = result["MADGRAPH"]
    powheg = result["POWHEG"]
    mcatnlo = result["MCATNLO"]
    # styles
    graph.markersize = 2
    graph.marker = "o"
    graph_with_systematics.markersize = 2
    graph_with_systematics.marker = "o"
    powheg.linestyle = "longdashdot"
    powheg.SetLineColor(kBlue)
    madgraph.linestyle = "solid"
    madgraph.SetLineColor(kRed + 1)
    mcatnlo.linestyle = "dotted"
    mcatnlo.SetLineColor(kMagenta + 3)

    rplt.errorbar(graph, xerr=False, emptybins=False, axes=axes, elinewidth=2, capsize=10, capthick=2, zorder=6)
    rplt.errorbar(
        graph_with_systematics,
        xerr=False,
        emptybins=False,
        axes=axes,
        elinewidth=2,
        capsize=0,
        zorder=5,
        label="unfolded data",
    )
    rplt.hist(madgraph, axes=axes, label="MADGRAPH", zorder=1)
    rplt.hist(powheg, axes=axes, label="POWHEG", zorder=2)
    rplt.hist(mcatnlo, axes=axes, label="MCATNLO", zorder=3)
Example #24
0
def compare_mbb_signal_and_data():
    fig, ax = plt.subplots(2, 2, figsize=(20, 10), dpi=100)
    ax[0, 0].set_title("mBB: VBF Simulation (correction applied)")
    ax[0, 1].set_title("mBB: VBF Simulation (no correction)")
    ax[1, 0].set_title("mBB: Data, signal region removed (correction applied)")
    ax[1, 1].set_title("mBB: Data, signal region removed (no correction)")
    rplt.hist(make_root_1D_hist(mbbs_signal, 50, 0, 5e2), axes=ax[0, 0])
    rplt.hist(make_root_1D_hist(mbbs_signal_no_corr, 50, 0, 5e2),
              axes=ax[0, 1])
    rplt.hist(make_root_1D_hist(mbbs_data, 50, 0, 5e2), axes=ax[1, 0])
    rplt.hist(make_root_1D_hist(mbbs_data_no_corr, 50, 0, 5e2), axes=ax[1, 1])

    if not ROOT.gROOT.IsBatch():
        plt.show()
Example #25
0
def plotting_resolution(variable, channel, residual, resolution, bin_number,
                        bin_low, bin_high):
    '''
    Resolution plots.
    '''
    bin_width = bin_high - bin_low

    unit = ''
    if variable in ['HT', 'ST', 'MET', 'lepton_pt', 'WPT']: unit += '[GeV]'

    title = "channel = {}, variable = ${}${}, {}-{}".format(
        channel, variables_latex[variable], unit, bin_low, bin_high)

    fig = plt.figure()
    axes = plt.axes()
    rplt.hist(residual, axes=axes, label='Residuals')
    plt.axvline(x=resolution, linewidth=1, color='r', label='Resolution')
    plt.axvline(x=bin_width / 2, linewidth=1, color='blue', label='Bin Width')

    axes.set_ylim(ymin=0)
    axes.set_xlabel('Residual')
    axes.set_ylabel('N')
    fig.suptitle('Residual Distribution', fontsize=14, fontweight='bold')
    plt.title(title, loc='right')

    leg = plt.legend(loc='best')
    leg.draw_frame(False)

    plt.tight_layout()

    plot_filepath = 'plots/binning/residuals/'
    make_folder_if_not_exists(plot_filepath)
    plot_filename = '{}_{}_{}_Residual.pdf'.format(channel, variable,
                                                   str(bin_number))
    fig.savefig(plot_filepath + plot_filename, bbox_inches='tight')
    fig.clf()
    plt.close()
    gc.collect()
    return
Example #26
0
def score_plot(sig_arr, bkg_arr, sig_weight, bkg_weight):
    '''
    make a plot of the score for bkg and signal
    '''

    hsig = Hist(100, -0.5, 0.5)
    hbkg = Hist(100, -0.5, 0.5)

    fill_hist(hsig, sig_arr, sig_weight)
    fill_hist(hbkg, bkg_arr, bkg_weight)
    hsig /= hsig.Integral()
    hbkg /= hbkg.Integral()
    hsig.color = 'red'
    hbkg.color = 'blue'
    hsig.title = 'signal'
    hbkg.title = 'background'
    fig = plt.figure()
    rmpl.hist([hsig, hbkg], stacked=False)
    plt.ylabel('Arbitrary Unit')
    plt.xlabel('BDT Score')
    plt.legend(loc='upper right')
    return fig
def make_template_plots(histograms, category, channel):
    global variable, output_folder
    
    for variable_bin in variable_bins_ROOT[variable]:
        path = output_folder + str(measurement_config.centre_of_mass) + 'TeV/' + variable + '/' + category + '/fit_templates/'
        make_folder_if_not_exists(path)
        plotname = path + channel + '_templates_bin_' + variable_bin 
        
        # check if template plots exist already
        for output_format in output_formats:
            if os.path.isfile(plotname + '.' + output_format):
                continue
        
        # plot with matplotlib
        h_signal = histograms[variable_bin]['signal']
        h_VJets = histograms[variable_bin]['V+Jets']
        h_QCD = histograms[variable_bin]['QCD']
        
        h_signal.linecolor = 'red'
        h_VJets.linecolor = 'green'
        h_QCD.linecolor = 'gray'
        h_VJets.linestyle = 'dashed'
        h_QCD.linestyle = 'dotted'# currently not working
        #bug report: http://trac.sagemath.org/sage_trac/ticket/13834
        
        h_signal.linewidth = 5
        h_VJets.linewidth = 5
        h_QCD.linewidth = 5
    
        plt.figure(figsize=(16, 16), dpi=200, facecolor='white')
        axes = plt.axes()
        axes.minorticks_on()
        
        plt.xlabel(r'lepton $|\eta|$', CMS.x_axis_title)
        plt.ylabel('normalised to unit area/0.2', CMS.y_axis_title)
        plt.tick_params(**CMS.axis_label_major)
        plt.tick_params(**CMS.axis_label_minor)

        rplt.hist(h_signal, axes=axes, label='signal')
        if (h_VJets.Integral() != 0):
            rplt.hist(h_VJets, axes=axes, label='V+Jets')
        else:
            print "WARNING: in %s bin %s, %s category, %s channel, V+Jets template is empty: not plotting." % (variable, variable_bin, category, channel)
        if (h_QCD.Integral() != 0):
            rplt.hist(h_QCD, axes=axes, label='QCD')
        else:
            print "WARNING: in %s bin %s, %s category, %s channel, QCD template is empty: not plotting." % (variable, variable_bin, category, channel)
        axes.set_ylim([0, 0.2])
        
        plt.legend(numpoints=1, loc='upper right', prop=CMS.legend_properties)
        plt.title(get_cms_labels(channel), CMS.title)
        plt.tight_layout()
    
        for output_format in output_formats:
            plt.savefig(plotname + '.' + output_format)
        
        plt.close()
        gc.collect()
def doUnfoldingSequence(unfolding, h_data, method, outputfile_suffix = '', doClosureTest = False, checkFakes = False):
    unfolding.unfold(h_data)
    saveUnfolding(unfolding, 'plots/Unfolding_' + method + outputfile_suffix + '.png')
    if doClosureTest:
        unfolding.closureTest()
        saveClosureTest(unfolding, 'plots/Unfolding_' + method + outputfile_suffix + '_closure.png')
    
    
    if checkFakes:
        fakes = asrootpy(unfolding.unfoldResponse.Hfakes())
        fakes.SetColor('red')
        h_fakes.SetColor('blue')
        fakes.SetFillStyle('\\')
        h_fakes.SetFillStyle('/')
        #cross check: are the fakes the same?
        plt.figure(figsize=(16, 10), dpi=100)
        rplt.hist(fakes, label=r'fakes from unfolding', stacked=False)
        rplt.hist(h_fakes, label=r'fakes from MC', stacked=False, alpha = 0.5)
        plt.xlabel('$E_{\mathrm{T}}^{miss}$')
        plt.ylabel('Events')
        plt.title('Unfolding')
        plt.legend()
        plt.savefig('plots/Fakes' + outputfile_suffix + '.png')
Example #29
0
def drawJetPtQA(ax, hMeas, hTrue, hInput, hReco):
    # ax.set_ylim([1e-5,1000])
    ax.set_xlabel(r"$p_{T}$")
    ax.set_ylabel(r"$\frac{dN}{dp_{jet}}$", fontsize=18)
    ax.set_yscale("log")
    ax.set_xscale("log")

    hMeas.linecolor = "blue"
    hTrue.linecolor = "red"
    hInput.linecolor = "pink"
    hReco.linecolor = "green"
    rplt.hist(hInput, axes=ax, label="Input")
    rplt.hist(hMeas, axes=ax, label="Measured")
    rplt.hist(hTrue, axes=ax, label="True")
    rplt.hist(hReco, axes=ax, label="Unfolded")
    ax.set_xlim([5, 150])
Example #30
0
def draw_pair(constructed, real, systematic):
    fig = plt.figure(figsize=CMS.figsize, dpi=CMS.dpi, facecolor=CMS.facecolor)
    axes = plt.axes([0.15, 0.15, 0.8, 0.8])
    axes.xaxis.set_minor_locator(AutoMinorLocator())
    axes.yaxis.set_minor_locator(AutoMinorLocator())
    axes.tick_params(which='major', labelsize=15, length=8)
    axes.tick_params(which='minor', length=4)
    axes.set_xlim(xmin=0, xmax=250)

    constructed.markersize = 1.2
    constructed.markercolor = 'green'
    constructed.linecolor = 'green'
    real.linecolor = 'red'
    constructed.SetTitle('constructed')
    real.SetTitle('real')

    rplt.errorbar(constructed, xerr=None, emptybins=False, axes=axes)
    rplt.hist(real)

    plt.xlabel('MET [GeV]', CMS.x_axis_title)
    plt.ylabel('normalised to unit area', CMS.y_axis_title)
    plt.legend(numpoints=1, prop=CMS.legend_properties)
    plt.savefig('validation_' + systematic + '.png')
def draw_pair(constructed, real, systematic):
    fig = plt.figure(figsize=CMS.figsize, dpi=CMS.dpi, facecolor=CMS.facecolor)
    axes = plt.axes([0.15, 0.15, 0.8, 0.8])
    axes.xaxis.set_minor_locator(AutoMinorLocator())
    axes.yaxis.set_minor_locator(AutoMinorLocator())
    axes.tick_params(which='major', labelsize=15, length=8)
    axes.tick_params(which='minor', length=4)
    axes.set_xlim(xmin=0, xmax=250)
    
    constructed.markersize=1.2
    constructed.markercolor = 'green'
    constructed.linecolor = 'green'
    real.linecolor = 'red'
    constructed.SetTitle('constructed')
    real.SetTitle('real')
    
    rplt.errorbar(constructed, xerr=False, emptybins=False, axes=axes)
    rplt.hist(real)
    
    plt.xlabel('MET [GeV]', CMS.x_axis_title)
    plt.ylabel('normalised to unit area', CMS.y_axis_title)
    plt.legend(numpoints=1, prop=CMS.legend_properties)
    plt.savefig('validation_' + systematic + '.png')
Example #32
0
def drawZ(ax, hMeas, hTrue, hInput, hFake):
    # ax.set_xlim([0,10])
    # ax.set_ylim([1e-5,1000])
    ax.set_xlabel(r"$Z$")
    ax.set_ylabel(r"$\frac{1}{N_{jets}}\frac{dN}{dZ}$", fontsize=18)
    ax.set_yscale("log")
    # ax.set_xscale('log')

    hMeas.linecolor = "blue"
    hTrue.linecolor = "red"
    hInput.linecolor = "pink"
    hFake.linecolor = "yellow"
    rplt.hist(hInput, axes=ax, label="Input")
    rplt.hist(hMeas, axes=ax, label="Measured")
    rplt.hist(hTrue, axes=ax, label="True")  # Plot jT histogram,
    rplt.hist(hFake, axes=ax, label="Fake")
def plotting_resolution(variable, channel, residual, resolution, bin_number, bin_low, bin_high ):
    '''
    Resolution plots.
    '''
    bin_width = bin_high - bin_low
    
    unit = ''
    if variable in ['HT', 'ST', 'MET', 'lepton_pt', 'WPT']: unit += '[GeV]'

    title = "channel = {}, variable = ${}${}, {}-{}".format(channel, variables_latex[variable], unit, bin_low, bin_high)

    fig = plt.figure()
    axes = plt.axes()
    rplt.hist(residual, axes = axes, label = 'Residuals')
    plt.axvline(x=resolution, linewidth=1, color='r', label = 'Resolution')
    plt.axvline(x=bin_width/2, linewidth=1, color='blue', label = 'Bin Width')

    axes.set_ylim(ymin = 0)
    axes.set_xlabel('Residual')
    axes.set_ylabel('N')
    fig.suptitle('Residual Distribution', fontsize=14, fontweight='bold')
    plt.title(title, loc='right')

    leg = plt.legend(loc='best')
    leg.draw_frame(False)   

    plt.tight_layout()

    plot_filepath = 'plots/binning/residuals/'
    make_folder_if_not_exists(plot_filepath)
    plot_filename = '{}_{}_{}_Residual.pdf'.format(channel, variable, str(bin_number))
    fig.savefig(plot_filepath+plot_filename, bbox_inches='tight')
    fig.clf()
    plt.close()
    gc.collect()
    return
Example #34
0
def test_hist():
    from rootpy.plotting import root2matplotlib as rplt
    h = Hist(100, -5, 5)
    h.FillRandom('gaus')
    rplt.hist(h)

    # stack
    h1 = h.Clone()
    stack = HistStack([h, h1])
    rplt.hist(stack)
    rplt.hist([h, h1])
Example #35
0
def test_hist():
    from rootpy.plotting import root2matplotlib as rplt
    h = Hist(100, -5, 5)
    h.FillRandom('gaus')
    rplt.hist(h)

    # stack
    h1 = h.Clone()
    stack = HistStack([h, h1])
    rplt.hist(stack)
    rplt.hist([h, h1])
Example #36
0
def compare_ptbb_signal_and_data():
    ptbbs_signal, ptbbs_signal_no_corr = retrieve_stats(signal_file,
                                                        var="ptbb")
    ptbbs_data, ptbbs_data_no_corr = retrieve_stats(data_file, var="ptbb")

    fig, ax = plt.subplots(2, 2, figsize=(20, 10), dpi=100)
    ax[0, 0].set_title("pTBB: VBF Simulation (correction applied)")
    ax[0, 1].set_title("pTBB: VBF Simulation (no correction)")
    ax[1,
       0].set_title("pTBB: Data, signal region removed (correction applied)")
    ax[1, 1].set_title("pTBB: Data, signal region removed (no correction)")
    rplt.hist(make_root_1D_hist(ptbbs_signal, 50, 0, 3e2), axes=ax[0, 0])
    rplt.hist(make_root_1D_hist(ptbbs_signal_no_corr, 50, 0, 3e2),
              axes=ax[0, 1])
    rplt.hist(make_root_1D_hist(ptbbs_data, 50, 0, 3e2), axes=ax[1, 0])
    rplt.hist(make_root_1D_hist(ptbbs_data_no_corr, 50, 0, 3e2), axes=ax[1, 1])

    if not ROOT.gROOT.IsBatch():
        plt.show()
def make_template_plots_matplotlib(histograms, category, channel):
    global variable, output_folder
    from matplotlib import rc
    rc('text', usetex=True)
    
    for variable_bin in variable_bins_ROOT[variable]:
        path = output_folder + str(measurement_config.centre_of_mass) + 'TeV/' + variable + '/' + category + '/fit_templates/'
        make_folder_if_not_exists(path)
        plotname = path + channel + '_templates_bin_' + variable_bin 
        
        # check if template plots exist already
        for output_format in output_formats:
            if os.path.isfile(plotname + '.' + output_format):
                continue
        
        # plot with matplotlib
        h_signal = histograms[variable_bin]['signal']
        h_VJets = histograms[variable_bin]['V+Jets']
        h_QCD = histograms[variable_bin]['QCD']
        
        h_signal.linecolor = 'red'
        h_VJets.linecolor = 'green'
        h_QCD.linecolor = 'yellow'
        
        h_signal.linewidth = 5
        h_VJets.linewidth = 5
        h_QCD.linewidth = 5
    
        plt.figure(figsize=(14, 10), dpi=200, facecolor='white')
        axes = plt.axes()
        axes.minorticks_on()
        
        plt.xlabel(r'lepton $|\eta|$', CMS.x_axis_title)
        plt.ylabel('normalised to unit area/0.2', CMS.y_axis_title)
        plt.tick_params(**CMS.axis_label_major)
        plt.tick_params(**CMS.axis_label_minor)

        rplt.hist(h_signal, axes=axes, label='signal')
        rplt.hist(h_VJets, axes=axes, label='V+Jets')
        rplt.hist(h_QCD, axes=axes, label='QCD')
        axes.set_ylim([0,0.2])
        
        plt.legend(numpoints=1, loc='upper right', prop=CMS.legend_properties)
        plt.title(get_cms_labels_matplotlib(channel), CMS.title)
        plt.tight_layout()
    
        for output_format in output_formats:
            plt.savefig(plotname + '.' + output_format) 
Example #38
0
def draw_result(result, axes):
    graph = result['unfolded']
    graph_with_systematics = result['unfolded_with_systematics']
    madgraph = result['MADGRAPH']
    powheg = result['POWHEG']
    mcatnlo = result['MCATNLO']
    # styles
    graph.markersize = 2
    graph.marker = 'o'
    graph_with_systematics.markersize = 2
    graph_with_systematics.marker = 'o'
    powheg.linestyle = 'longdashdot'
    powheg.SetLineColor(kBlue)
    madgraph.linestyle = 'solid'
    madgraph.SetLineColor(kRed + 1)
    mcatnlo.linestyle = 'dotted'
    mcatnlo.SetLineColor(kMagenta + 3)

    rplt.errorbar(graph,
                  xerr=None,
                  emptybins=False,
                  axes=axes,
                  elinewidth=2,
                  capsize=10,
                  capthick=2,
                  zorder=6)
    rplt.errorbar(graph_with_systematics,
                  xerr=None,
                  emptybins=False,
                  axes=axes,
                  elinewidth=2,
                  capsize=0,
                  zorder=5,
                  label='unfolded data')
    rplt.hist(madgraph, axes=axes, label='MADGRAPH', zorder=1)
    rplt.hist(powheg, axes=axes, label='POWHEG', zorder=2)
    rplt.hist(mcatnlo, axes=axes, label='MCATNLO', zorder=3)
def draw_result( result, axes ):
    graph = result['unfolded']
    graph_with_systematics = result['unfolded_with_systematics']
    madgraph = result['MADGRAPH']
    powheg = result['POWHEG']
    mcatnlo = result['MCATNLO']
    # styles
    graph.markersize = 2
    graph.marker = 'o'
    graph_with_systematics.markersize = 2
    graph_with_systematics.marker = 'o'
    powheg.linestyle = 'longdashdot'
    powheg.SetLineColor( kBlue )
    madgraph.linestyle = 'solid'
    madgraph.SetLineColor( kRed + 1 )
    mcatnlo.linestyle = 'dotted'
    mcatnlo.SetLineColor( kMagenta + 3 )
    
    
    rplt.errorbar( graph, xerr = None, emptybins = False, axes = axes, elinewidth = 2, capsize = 10, capthick = 2, zorder = 6)
    rplt.errorbar( graph_with_systematics, xerr = None, emptybins = False, axes = axes, elinewidth = 2, capsize = 0, zorder = 5, label = 'unfolded data')
    rplt.hist( madgraph, axes = axes, label = 'MADGRAPH', zorder = 1 )
    rplt.hist( powheg, axes = axes, label = 'POWHEG', zorder = 2 )
    rplt.hist( mcatnlo, axes = axes, label = 'MCATNLO', zorder = 3 )
def compare_measurements(models={},
                         measurements={},
                         show_measurement_errors=True,
                         histogram_properties=Histogram_properties(),
                         save_folder='plots/',
                         save_as=['pdf', 'png'],
                         match_models_to_measurements=False,
                         line_styles_for_models=None,
                         show_ratio_for_pairs={}):
    """
        This function takes one or more models and compares it to a set of measurements.
        Models and measurements are supplied as dictionaries in the form of {'label': histogram}
        @param models: a dictionary of one or more model input, i.e 
            theories = {'model1' : histogram1, 'model2' : histogram_2
            where histogram_1(2) is a root (or rootpy/matplotlib) histogram object.
        @param measurements: a dictionary of one or more measurement. Follows the same
            prescription as the models parameter.
        @param histogram_properties: a Histogram_properties object to describe the look of the histogram
    """
    save_folder = check_save_folder(save_folder)
    # plot with matplotlib
    plt.figure(figsize=CMS.figsize, dpi=CMS.dpi, facecolor=CMS.facecolor)
    axes = plt.axes()
    if len(show_ratio_for_pairs) > 0:
        gs = gridspec.GridSpec(2, 1, height_ratios=[5, 1])
        axes = plt.subplot(gs[0])
    # Set default color cycle to rgby
    # matplotlib
    # plt.rc( 'axes', color_cycle = ['r', 'g', 'b', 'y'] )
    # rootpy
    colors = ['green', 'red', 'blue', 'magenta', 'black']
    colorcycler = cycle(colors)

    #     markers = ['circle', 'triangledown', 'triangleup', 'diamond', 'square', 'star']
    markers = [20, 21, 22, 33, 23, 29]
    markercycler = cycle(markers)
    # matplotlib
    #     lines = ["-", "--", "-.", ":"]
    # rootpy
    lines = ["dashed"]
    if line_styles_for_models is not None:
        lines = line_styles_for_models
    linecycler = cycle(lines)

    for label, histogram in models.iteritems():
        if not histogram:  # skip empty ones
            continue
        histogram.linewidth = 4
        histogram.color = next(colorcycler)
        histogram.linestyle = next(linecycler)
        histogram.fillstyle = 0
        rplt.hist(histogram, axex=axes, label=label)

    if match_models_to_measurements:
        colorcycler = cycle(colors)
        markercycler = cycle(markers)
        linecycler = cycle(lines)

    for label, histogram in measurements.iteritems():
        histogram.markersize = 2
        histogram.markerstyle = next(markercycler)
        histogram.color = next(colorcycler)
        rplt.errorbar(histogram,
                      axes=axes,
                      label=label,
                      elinewidth=4,
                      yerr=show_measurement_errors,
                      xerr=histogram_properties.xerr)

    set_labels(plt,
               histogram_properties,
               axes=axes,
               show_x_label=len(show_ratio_for_pairs) == 0)

    l1 = axes.legend(numpoints=1,
                     frameon=histogram_properties.legend_color,
                     bbox_to_anchor=histogram_properties.legend_location,
                     bbox_transform=plt.gcf().transFigure,
                     prop=CMS.legend_properties,
                     ncol=histogram_properties.legend_columns)
    l1.set_zorder(102)

    all_hists = []
    all_hists.extend(models.values())
    all_hists.extend(measurements.values())
    adjust_axis_limits(axes, histogram_properties, all_hists)

    x_limits = histogram_properties.x_limits
    y_limits = histogram_properties.y_limits
    if len(x_limits) == 2:
        axes.set_xlim(xmin=x_limits[0], xmax=x_limits[1])
    if len(y_limits) == 2:
        axes.set_ylim(ymin=y_limits[0], ymax=y_limits[1])

    if histogram_properties.set_log_y:
        axes.set_yscale('log', nonposy="clip")
        if not len(
                histogram_properties.y_limits
        ) == 2:  # if not user set y-limits, calculate the limits from the tuple values
            value_range = sorted(list(histogram.y()))
            for i, value in enumerate(value_range):
                if value == 0:
                    del value_range[i]
            axes.set_ylim(ymin=min(value_range) / 10,
                          ymax=max(value_range) * 10)

    ratios = {}
    if len(show_ratio_for_pairs) > 0:

        for l, hists in show_ratio_for_pairs.iteritems():
            h1 = hists[0].clone()
            h2 = hists[1].clone()
            h1.Divide(h2)
            h1.linewidth = 4
            ratios[l] = h1
        plt.setp(axes.get_xticklabels(), visible=False)
        axes_ratio = plt.subplot(gs[1])
        axes_ratio.minorticks_on()
        axes_ratio.grid(True, 'major', linewidth=1)
        axes_ratio.axhline(y=1, linewidth=1, linestyle='dashed', color='black')
        set_labels(plt,
                   histogram_properties,
                   show_x_label=True,
                   show_title=False)
        plt.ylabel(histogram_properties.ratio_y_title, fontsize=25)
        axes_ratio.yaxis.set_label_coords(-0.115, 0.8)
        for label, ratio in ratios.items():
            rplt.hist(ratio, axes=axes_ratio, label=label)
        adjust_ratio_ticks(axes_ratio.yaxis, n_ticks=3)
        adjust_axis_limits(axes_ratio,
                           histogram_properties,
                           ratios.values(),
                           adjust_y=True,
                           y_limits=histogram_properties.ratio_y_limits)

    if CMS.tight_layout:
        plt.tight_layout()

    for save in save_as:
        plt.savefig(save_folder + histogram_properties.name + '.' + save)
    plt.close()
def plot_systematic_uncertainties(systematic_uncertainties,
                                  bin_edges,
                                  variable,
                                  output_folder,
                                  subcategories=[],
                                  subname='',
                                  plot_largest=False,
                                  plot_foreground=None):
    '''
	Plot the systematic uncertainties
	'''
    if not subcategories: subcategories = systematic_uncertainties.keys()

    x_limits = [bin_edges[0], bin_edges[-1]]
    # y_limits = [-0.6,0.6]
    y_limits = [0, 0.4]

    fig_syst = plt.figure(figsize=(20, 16), dpi=400, facecolor='white')
    ax_syst = fig_syst.add_subplot(1, 1, 1)
    ax_syst.minorticks_on()
    ax_syst.xaxis.labelpad = 12
    ax_syst.yaxis.labelpad = 12

    error_hists_up = {}
    error_hists_down = {}
    stat_hist = None

    for syst, vals in systematic_uncertainties.iteritems():
        if syst == 'central':
            n = len(systematic_uncertainties[syst])
            continue
        elif syst == 'statistical':
            stat_hist_up = values_and_errors_to_hist(vals, [], bin_edges)
            stat_hist_down = values_and_errors_to_hist(-vals, [], bin_edges)
        elif syst == 'systematic':
            syst_hist_up = values_and_errors_to_hist(vals, [], bin_edges)
            syst_hist_down = values_and_errors_to_hist(-vals, [], bin_edges)
        elif syst in subcategories:
            error_hists_up[syst] = values_and_errors_to_hist(
                vals, [], bin_edges)
            error_hists_down[syst] = values_and_errors_to_hist(
                -vals, [], bin_edges)
        else:
            continue

    if plot_largest:
        largest_syst = []
        for bin_i in range(n):
            high = []
            for syst, vals in systematic_uncertainties.iteritems():
                if syst == 'central': continue
                if syst == 'statistical': continue
                if syst == 'systematic': continue
                high.append([syst, vals[bin_i]])
            high = sorted(high, key=itemgetter(1), reverse=True)
            # Retrieve highest systematics
            if high[0][0] not in largest_syst: largest_syst.append(high[0][0])
            elif high[1][0] not in largest_syst:
                largest_syst.append(high[1][0])
            else:
                continue

    rplt.fill_between(syst_hist_up,
                      syst_hist_down,
                      color='gold',
                      label='Syst.')
    rplt.fill_between(stat_hist_down,
                      stat_hist_up,
                      color='0.75',
                      label='Stat.')

    plt.tick_params(**CMS.axis_label_major)
    plt.tick_params(**CMS.axis_label_minor)

    colours = [
        'red', 'blue', 'green', 'chartreuse', 'indigo', 'magenta',
        'darkmagenta', 'hotpink', 'cyan', 'darkred', 'darkgoldenrod',
        'mediumvioletred', 'mediumspringgreen', 'darkgoldenrod', 'slategray',
        'dodgerblue', 'cadetblue', 'darkblue', 'seagreen', 'deeppink',
        'deepskyblue'
    ] * 2
    if len(colours) < len(error_hists_up.keys()):
        print '---> Need to add more colours!!!'

    for error_hists in [error_hists_up, error_hists_down]:
        for i, source, in enumerate(error_hists.keys()):
            hist = error_hists[source]
            hist.linewidth = 4
            hist.color = colours[i]
            if plot_largest:
                if source not in largest_syst:
                    hist.linestyle = 'dashed'
                    hist.alpha = 0.4
                    hist.linewidth = 2
            # Only label systematic once
            if plot_foreground and plot_foreground in error_hists.keys():
                source = ''
            if error_hists == error_hists_up:
                rplt.hist(hist, stacked=False, label=source)
            else:
                rplt.hist(hist, stacked=False, label='')
        if plot_foreground and plot_foreground in error_hists.keys():
            hist = error_hists[plot_foreground]
            hist.color = 'black'
            rplt.hist(hist, stacked=False, label=source)

    leg = plt.legend(loc='upper right', prop={'size': 25}, ncol=3)
    # leg = plt.legend(loc='upper right',prop={'size':20},ncol=4)
    leg.draw_frame(False)

    x_title = variables_NonLatex[variable]
    if variable in ['HT', 'MET', 'WPT', 'ST', 'lepton_pt']:
        x_title += ' [GeV]'

    ax_syst.set_xlim(x_limits)
    ax_syst.set_ylim(y_limits)
    plt.xlabel(x_title, CMS.x_axis_title)
    plt.ylabel('Relative Uncertainty', CMS.y_axis_title)

    template = '%.1f fb$^{-1}$ (%d TeV)'
    label = template % (measurement_config.new_luminosity / 1000.,
                        measurement_config.centre_of_mass_energy)
    plt.title(label, loc='right', **CMS.title)

    logo_location = (0.05, 0.98)
    prelim_location = (0.05, 0.92)
    channel_location = (0.05, 0.86)
    # plt.text(logo_location[0], logo_location[1],
    # 	"CMS",
    # 	transform=ax_syst.transAxes,
    # 	fontsize=42,
    # 	verticalalignment='top',
    # 	horizontalalignment='left'
    # )
    # # preliminary
    # plt.text(prelim_location[0], prelim_location[1],
    # 	r"\emph{Preliminary}",
    # 	transform=ax_syst.transAxes,
    # 	fontsize=42,
    # 	verticalalignment='top',
    # 	horizontalalignment='left'
    # )
    # # channel text
    # plt.text(channel_location[0], channel_location[1],
    # 	r"\emph{%s}" % channel,
    # 	transform=ax_syst.transAxes,
    # 	fontsize=40,
    # 	verticalalignment='top',
    # 	horizontalalignment='left'
    # )

    plt.tight_layout()

    file_template = output_folder + '{var}_systematics_{com}TeV'.format(
        var=variable,
        com=measurement_config.centre_of_mass_energy,
    )
    if subname: file_template = file_template + '_' + subname
    file_template += '.pdf'
    fig_syst.savefig(file_template)
    print "Written plots to {f}".format(f=file_template)
    # plt.show()
    return
def make_eff_plots(input_files, file_path, split=''):
	'''
	1D and 2D efficiency plotter
	'''
	#################################################################################################################################
	# PLOT EFFICIENCY
	#################################################################################################################################
	f = TFile(file_path, "OPEN")
	make_folder_if_not_exists('plots/BTagEfficiency/')

	# #################################################################################################################################
	# # PLOT 2D EFFICIENCY
	# #################################################################################################################################
	for generator in input_files.values():
		b_Hist = f.Get(generator+"/bQuarkJets_Ratio_Hist")
		c_Hist = f.Get(generator+"/cQuarkJets_Ratio_Hist")
		udsg_Hist = f.Get(generator+"/udsgQuarkJets_Ratio_Hist")

		b_Canvas = TCanvas("bQuarkJet","bQuarkJet", 0, 0, 800, 600)
		b_Hist.SetTitle("b quark b tagging efficiency ; pt; eta")
		b_Hist.Draw("colz")
		b_Canvas.Update()
		b_Canvas.SaveAs("plots/BTagEfficiency/bQuarkJet_"+generator+"_BTagEfficiency.png")

		c_Canvas = TCanvas("cQuarkJet","cQuarkJet", 0, 0, 800, 600)
		c_Hist.SetTitle("c quark b tagging efficiency ; pt; eta")
		c_Hist.Draw("colz")
		c_Canvas.Update()
		c_Canvas.SaveAs("plots/BTagEfficiency/cQuarkJet_"+generator+"_BTagEfficiency.png")
		
		udsg_Canvas = TCanvas("udsgQuarkJet","udsgQuarkJet", 0, 0, 800, 600)
		udsg_Hist.SetTitle("udsg quark b tagging efficiency ; pt; eta")
		udsg_Hist.Draw("colz")
		udsg_Canvas.Update()
		udsg_Canvas.SaveAs("plots/BTagEfficiency/udsgQuarkJet_"+generator+"_BTagEfficiency.png")

	#################################################################################################################################
	# PLOT 1D EFFICIENCY
	#################################################################################################################################

	b_pt 		= {}
	b_eta 		= {}
	c_pt 		= {}
	c_eta 		= {}
	udsg_pt 	= {}
	udsg_eta 	= {}

	for generator in input_files.values():
		# Only get generators if they exist in the file
		if not f.GetListOfKeys().Contains(generator): continue

		# Split into categories
		if split == 'generator':
			if generator not in [
				'PowhegPythia8', 
				'PowhegHerwigpp', 
				'aMCatNLOPythia8', 
				'Madgraph'
				]: continue
		if split == 'Shape':
			if generator not in [
				'PowhegPythia8', 
				'PowhegPythia8_plusJES', 
				'PowhegPythia8_minusJES', 
				'PowhegPythia8_plusJER', 
				'PowhegPythia8_minusJER',  
				'PowhegPythia8_mtop1695', 
				'PowhegPythia8_mtop1755'
				]: continue
		if split == 'Tune':
			if generator not in [
				'PowhegPythia8', 
				'PowhegPythia8_fsrup', 
				'PowhegPythia8_fsrdown', 
				'PowhegPythia8_isrup', 
				'PowhegPythia8_isrdown',
				'PowhegPythia8_up', 
				'PowhegPythia8_down'
				]: continue

		b_pt[generator] 	= asrootpy( f.Get(generator+"/bQuarkJets_Ratio_Pt_Hist") )
		b_eta[generator] 	= asrootpy( f.Get(generator+"/bQuarkJets_Ratio_Eta_Hist") )
		c_pt[generator] 	= asrootpy( f.Get(generator+"/cQuarkJets_Ratio_Pt_Hist") )
		c_eta[generator] 	= asrootpy( f.Get(generator+"/cQuarkJets_Ratio_Eta_Hist") )
		udsg_pt[generator] 	= asrootpy( f.Get(generator+"/udsgQuarkJets_Ratio_Pt_Hist") )
		udsg_eta[generator] = asrootpy( f.Get(generator+"/udsgQuarkJets_Ratio_Eta_Hist") )

	eff_to_plot = {
		"b parton tagging effienciency (pt)" : b_pt,
		"b parton tagging effienciency (eta)" : b_eta,
		"c parton tagging effienciency (pt)" : c_pt,
		"c parton tagging effienciency (eta)" : c_eta,
		"udsg parton tagging effienciency (pt)" : udsg_pt,
		"udsg parton tagging effienciency (eta)" : udsg_eta,
	}

	print b_pt

	colours = [
		'red', 'blue', 'green', 'chartreuse', 'indigo', 
		'magenta', 'darkmagenta', 'hotpink', 'cyan', 'darkred', 
		'darkgoldenrod', 'mediumvioletred', 'mediumspringgreen', 
		'gold', 'darkgoldenrod', 'slategray', 'dodgerblue', 
		'cadetblue', 'darkblue', 'seagreen', 'deeppink', 'deepskyblue' 
	]

	for title, efficiencies in eff_to_plot.iteritems():

		# create figure
		fig_eff = plt.figure( figsize = ( 20, 16 ), dpi = 400, facecolor = 'white' )
		ax_eff = fig_eff.add_subplot(1, 1, 1)
		ax_eff.minorticks_on()
		ax_eff.xaxis.labelpad = 12
		ax_eff.yaxis.labelpad = 12
		ax_eff.set_ylim( 0, 1 )
		plt.tick_params( **CMS.axis_label_major )
		plt.tick_params( **CMS.axis_label_minor )

		# plot specifics
		var = 'pt'
		if 'eta' in title:
			var = 'eta'
		parton = 'b'
		if "c parton" in title: parton = 'c'
		if "udsg parton" in title: parton = 'udsg'

		ylimits=[]
		if parton == 'b':
			if var == 'pt':
				y_limits = [0.480,0.75]
			if var == 'eta':
				y_limits = [0.510,0.75]
		if parton == 'c':
			if var == 'pt':
				y_limits = [0.150,0.200]
			if var == 'eta':
				y_limits = [0.125,0.21]
		if parton == 'udsg':
			if var == 'pt':
				y_limits = [0.015,0.050]
			if var == 'eta':
				y_limits = [0.014,0.034]
		# labels
		x_title = var
		if var == 'pt':
			x_title+=' [GeV]'
		plt.xlabel( x_title, CMS.x_axis_title )
		plt.ylabel( 'Efficiency', CMS.y_axis_title)
		template = '%.1f fb$^{-1}$ (%d TeV)'
		label = template % ( measurement_config.new_luminosity/1000, measurement_config.centre_of_mass_energy)
		plt.title( label,loc='right', **CMS.title )

		# plot histograms
		i=0
		for label, h in efficiencies.iteritems():
			hist = asrootpy( h )
			h.linewidth = 4
			h.color = 'black'
			if label != 'PowhegPythia8':
				hist.linestyle = 'dashed'
				# hist.alpha = 0.8	
				hist.linewidth = 2
				h.color = colours[i]

			rplt.hist( h, stacked=False, label = label )
			i+=1

		# plot legend
		leg = plt.legend(loc='best',prop={'size':25},ncol=2)
		leg.draw_frame(False)	

		ax_eff.set_ylim( y_limits )

		# additional text 
		# logo_location = (0.05, 0.95)
		# plt.text(logo_location[0], logo_location[1], 
		# 	"CMS", 
		# 	transform=ax_eff.transAxes, 
		# 	fontsize=42,
		# 	verticalalignment='top',
		# 	horizontalalignment='left'
		# )
		# title_location = (0.05, 0.90)
		# plt.text(title_location[0], title_location[1], 
		# 	title,
		# 	transform=ax_eff.transAxes, 
		# 	fontsize=42,
		# 	verticalalignment='top',
		# 	horizontalalignment='left'
		# )

		# filename and saving
		name_template = '{parton}_{var}_{split}efficiency.pdf'
		s=''
		if split: s = split+'_' 
		name = name_template.format(
			parton=parton,
			var=var,
			split=s,
		)
		plt.tight_layout()
		fig_eff.savefig('plots/BTagEfficiency/'+name)

	f.Close()
	return
def plot_systematic_uncertainties(systematic_uncertainties, bin_edges, variable, output_folder, subcategories = [], subname = '', plot_largest = False):
	'''
	Plot the systematic uncertainties
	'''
	print subcategories
	if not subcategories: subcategories = systematic_uncertainties.keys()

	x_limits = [bin_edges[0], bin_edges[-1]]
	# y_limits = [-0.6,0.6]
	y_limits = [0,0.4]

	fig_syst = plt.figure( figsize = ( 20, 16 ), dpi = 400, facecolor = 'white' )
	ax_syst = fig_syst.add_subplot(1, 1, 1)
	ax_syst.minorticks_on()
	ax_syst.xaxis.labelpad = 12
	ax_syst.yaxis.labelpad = 12

	error_hists_up = {}
	error_hists_down = {}
	stat_hist = None

	for syst, vals in systematic_uncertainties.iteritems():
		if syst == 'central': 
			n = len(systematic_uncertainties[syst])
			continue
		elif syst == 'statistical':
			stat_hist_up = values_and_errors_to_hist( vals, [], bin_edges )
			stat_hist_down = values_and_errors_to_hist( -vals, [], bin_edges )
		elif syst == 'systematic':
			syst_hist_up = values_and_errors_to_hist( vals, [], bin_edges )
			syst_hist_down = values_and_errors_to_hist( -vals, [], bin_edges )
		elif syst in subcategories:
			error_hists_up[syst] = values_and_errors_to_hist( vals, [], bin_edges )
			error_hists_down[syst] = values_and_errors_to_hist( -vals, [], bin_edges )
		else: continue

	if plot_largest:
		largest_syst = []
		for bin_i in range( n ):
			high = []
			for syst, vals in systematic_uncertainties.iteritems():
				if syst == 'central': continue
				if syst == 'statistical': continue
				if syst == 'systematic': continue
				high.append([syst,vals[bin_i]])
			high = sorted(high, key = itemgetter(1), reverse=True)
			# Retrieve highest systematics
			if high[0][0] not in largest_syst: largest_syst.append(high[0][0])
			elif high[1][0] not in largest_syst: largest_syst.append(high[1][0])
			else: continue

	rplt.fill_between( syst_hist_up, syst_hist_down, color = 'yellow', label='Syst.' )
	rplt.fill_between( stat_hist_down, stat_hist_up, color = 'grey', label='Stat.' )

	plt.tick_params( **CMS.axis_label_major )
	plt.tick_params( **CMS.axis_label_minor )

	colours = ['red', 'blue', 'green', 'chartreuse', 'indigo', 'magenta', 'darkmagenta', 'hotpink', 'cyan', 'darkred', 'darkgoldenrod', 'mediumvioletred', 'mediumspringgreen', 'gold', 'darkgoldenrod', 'slategray', 'dodgerblue', 'cadetblue', 'darkblue', 'seagreen', 'deeppink', 'deepskyblue' ]
	# if len(colours) < len(error_hists.keys()):
	# 	print '---> Need to add more colours!!!'

	for error_hists in [error_hists_up, error_hists_down]:
		for i, source, in enumerate(error_hists.keys()):
			hist = error_hists[source]
			hist.linewidth = 4
			hist.color = colours[i]
			if plot_largest:
				if source not in largest_syst:
					hist.linestyle = 'dashed'
					hist.alpha = 0.4	
					hist.linewidth = 2
			# Only label systematic once
			if error_hists == error_hists_up:
				rplt.hist( hist, stacked=False, label = source )
			else:
				rplt.hist( hist, stacked=False, label = '' )

	leg = plt.legend(loc='upper right',prop={'size':25},ncol=3)
	# leg = plt.legend(loc='upper right',prop={'size':20},ncol=4)
	leg.draw_frame(False)	

	x_title = variables_NonLatex[variable]
	if variable in ['HT', 'MET', 'WPT', 'ST', 'lepton_pt']:
		x_title += ' [GeV]'

	ax_syst.set_xlim( x_limits )
	ax_syst.set_ylim( y_limits )
	plt.xlabel( x_title, CMS.x_axis_title )
	plt.ylabel( 'Relative Uncertainty', CMS.y_axis_title)

	template = '%.1f fb$^{-1}$ (%d TeV)'
	label = template % ( measurement_config.new_luminosity/1000, measurement_config.centre_of_mass_energy)
	plt.title( label,loc='right', **CMS.title )

	logo_location = (0.05, 0.98)
	prelim_location = (0.05, 0.92)
	channel_location = ( 0.05, 0.86)
	# plt.text(logo_location[0], logo_location[1], 
	# 	"CMS", 
	# 	transform=ax_syst.transAxes, 
	# 	fontsize=42,
	# 	verticalalignment='top',
	# 	horizontalalignment='left'
	# )
	# # preliminary
	# plt.text(prelim_location[0], prelim_location[1], 
	# 	r"\emph{Preliminary}",
	# 	transform=ax_syst.transAxes, 
	# 	fontsize=42,
	# 	verticalalignment='top',
	# 	horizontalalignment='left'
	# )
	# # channel text
	# plt.text(channel_location[0], channel_location[1], 
	# 	r"\emph{%s}" % channel, 
	# 	transform=ax_syst.transAxes, 
	# 	fontsize=40,
	# 	verticalalignment='top',
	# 	horizontalalignment='left'
	# )

	plt.tight_layout()

	file_template = output_folder + '{var}_systematics_{com}TeV'.format(
		var = variable, 
		com = measurement_config.centre_of_mass_energy,
	)
	if subname: file_template = file_template + '_' + subname
	file_template += '.pdf'
	fig_syst.savefig(file_template)
	print "Written plots to {f}".format(f = file_template)
	# plt.show()
	return
hTrue= Hist(40, -10.0, 10.0);
hMeas= Hist(40, -10.0, 10.0);
hTrue.SetLineColor('red')
hMeas.SetLineColor('blue')
#  Test with a Gaussian, mean 0 and width 2.
for i in xrange(10000):
    xt= gRandom.Gaus (0.0, 2.0)
    x= smear (xt);
    hTrue.Fill(xt);
    if x!=None: hMeas.Fill(x);

print "==================================== UNFOLD ==================================="
unfold= RooUnfoldBayes     (response, hMeas, 4);    #  OR
# unfold= RooUnfoldSvd     (response, hMeas, 20);   #  OR
# unfold= RooUnfoldTUnfold (response, hMeas);

hReco= unfold.Hreco();
h_unfolded = asrootpy(hReco)
unfold.PrintTable (cout, hTrue);

plt.figure(figsize=(16, 12), dpi=100)
rplt.hist(hTrue, label='truth', stacked=False)
rplt.hist(hMeas, label='measured', stacked=False)
rplt.errorbar(h_unfolded, label='unfolded')
plt.xlabel('var')
plt.ylabel('Events')
plt.title('Unfolding')
plt.legend()
plt.savefig('plots/RooUnfoldBayesExample.png')
print 'Done'
Example #45
0
    
    bins = array('d', [0, 25, 45, 70, 100, 1000])
    h_data = Hist(bins.tolist())
    h_data.SetBinContent(1, 2146)
    h_data.SetBinError(1, 145)
    h_data.SetBinContent(2, 3399)
    h_data.SetBinError(2, 254)
    h_data.SetBinContent(3, 3723)
    h_data.SetBinError(3, 69)
    h_data.SetBinContent(4, 2256)
    h_data.SetBinError(4, 53)
    h_data.SetBinContent(5, 1722)
    h_data.SetBinError(5, 91)
    h_data.SetTitle('input distribution')
    h_new = generate_toy_MC_from_distribution(h_data)
    h_new.SetTitle('toy MC')
    fig = plt.figure(figsize=(16, 10), dpi=100, facecolor='white')
    axes = plt.axes([0.15, 0.15, 0.8, 0.8])
    axes.xaxis.set_minor_locator(AutoMinorLocator())
    axes.yaxis.set_minor_locator(AutoMinorLocator())
    axes.tick_params(which='major', labelsize=15, length=8)
    axes.tick_params(which='minor', length=4)
    rplt.hist(h_new, axes=axes)
    rplt.errorbar(h_data, emptybins=False, axes=axes)
    plt.xlabel('Mass', position=(1., 0.), ha='right')
    plt.ylabel('Events', position=(0., 1.), va='top')
    plt.legend(numpoints=1)
    plt.savefig('toy_MC_test.png')
    print  list( h_data.y() )
    print list( h_new.y() )
def make_error_plot( errorHists, bins ):
    global output_folder, variable
    # For each up/down source, reduce to one set of numbers
    symmetricErrorHists = {}
    for source, hist in errorHists.iteritems():
        if ( variable == 'HT' or variable == 'NJets' or variable == 'lepton_pt' or variable == 'abs_lepton_eta'  ) and source in measurement_config.met_systematics and not 'JES' in source and not 'JER' in source:
            continue

        if 'down' in source or '-' in source or 'lower' in source or 'Down' in source:
            # Find up version
            upHist = None
            newSource = ''
            if 'down' in source:
                upHist = errorHists[source.replace('down','up')]
                newSource = source.replace('down','')
            elif 'Down' in source:
                upHist = errorHists[source.replace('Down','Up')]
                newSource = source.replace('Down','')
            elif '-' in source:
                upHist = errorHists[source.replace('-','+')]
                newSource = source.replace('-','')
            elif 'lower' in source:
                upHist = errorHists[source.replace('lower','upper')]
                newSource = source.replace('lower','')

            if newSource[-1] == '_':
                newSource = newSource[:-1]
            # if '_' in newSource:
            #     newSource = newSource.replace('_','')

            symmetricErrorHists[newSource] = []
            for errorup, errordown in zip(hist, upHist):
                newError = max( abs(errorup), abs(errordown) )
                symmetricErrorHists[newSource].append(newError)
        elif 'TTJets_hadronisation' in source or 'QCD_shape' in source or 'TTJets_NLOgenerator' in source:        
            symmetricErrorHists[source] = [ abs(i) for i in hist ]

    x_limits = [bins[0], bins[-1]]
    y_limits = [0,0.6]
    plt.figure( figsize = ( 20, 16 ), dpi = 200, facecolor = 'white' )

    ax0 = plt.axes()
    ax0.minorticks_on()
    ax0.xaxis.labelpad = 12
    ax0.yaxis.labelpad = 12
    ax0.set_xlim( x_limits )
    plt.tick_params( **CMS.axis_label_major )
    plt.tick_params( **CMS.axis_label_minor )


    statisticalErrorHists = values_and_errors_to_hist( errorHists['statistical'], [], bins )
    for source, hist in symmetricErrorHists.iteritems():
        symmetricErrorHists[source] = values_and_errors_to_hist( hist, [], bins )

    colours = ['silver', 'r', 'tan', 'chartreuse', 'cadetblue', 'dodgerblue', 'pink', 'hotpink', 'coral', 'forestgreen', 'cyan', 'teal', 'crimson', 'darkmagenta', 'olive', 'slateblue', 'deepskyblue', 'orange', 'r' ]
    for source, colour in zip( symmetricErrorHists.keys(), colours):
        hist = symmetricErrorHists[source]
        hist.linewidth = 4
        hist.color = colour
        rplt.hist( hist, stacked=False, axes = ax0, cmap = my_cmap, vmin = 1, label = source )

    statisticalErrorHists.linewidth = 4
    statisticalErrorHists.color = 'black'
    statisticalErrorHists.linestyle = 'dashed'
    rplt.hist( statisticalErrorHists, stacked=False, axes = ax0, cmap = my_cmap, vmin = 1, label = 'stat.' )

    ax0.set_ylim( y_limits )
    leg = plt.legend(loc=1,prop={'size':40},ncol=2)
    leg.draw_frame(False)
    x_title = variables_NonLatex[variable]
    if variable in ['HT', 'MET', 'WPT', 'ST', 'lepton_pt']:
        x_title += ' [GeV]'
    plt.xlabel( x_title, CMS.x_axis_title )
    plt.ylabel( 'Relative Uncertainty', CMS.y_axis_title)
    plt.tight_layout()

    path = output_folder + '/'  + variable + '/'
    make_folder_if_not_exists(path)
    file_template = path + '/%s_systematics_%dTeV_%s.pdf' % (variable, measurement_config.centre_of_mass_energy, channel)
    plt.savefig(file_template)
    pass
def draw_d_i( d_i ):
    global variable, output_folder, output_formats, test, k_values, k_values_crosscheck
    plt.figure( figsize = CMS.figsize, dpi = CMS.dpi, facecolor = CMS.facecolor )
    fit_all_bins = TF1("fit_all_bins", 'expo', 0, d_i.nbins(0)-1)

    fit_exclude_first_bin = TF1("fit_exclude_first_bin", 'expo', 1, d_i.nbins(0)-1)
    fit_all_bins_plus_flat = TF1("fit_all_bins_plus_flat", 'TMath::Exp([0]*x+[1])+[2]', 0, d_i.nbins(0)-1)
    fit_all_bins_plus_flat.SetParameter(0,-2.)
    fit_all_bins_plus_flat.SetParameter(1,3.)
    fit_all_bins_plus_flat.SetParameter(2,1.)
    # fit_all_bins_plus_flat.SetParLimits(2,0.1,2.)    

    fit_all_bins_plus_linear = TF1("fit_all_bins_plus_linear", 'TMath::Exp([0]*x+[1])+[2]+[3]*x', 0, d_i.nbins(0)-1)
    fit_all_bins_plus_linear.SetParameter(0,-2.)
    fit_all_bins_plus_linear.SetParameter(1,3.)
    fit_all_bins_plus_linear.SetParameter(2,0.)
    fit_all_bins_plus_linear.SetParameter(3,0.)
    # fit_all_bins_plus_linear.SetParLimits(2,0.1,10.)   
    fit_all_bins_plus_linear.SetParLimits(3,-10.,0.)
    
    d_i.Fit(fit_all_bins, 'WWSQR')
    d_i.Fit(fit_exclude_first_bin, 'WWSQR')
    d_i.Fit(fit_all_bins_plus_flat, 'WWSQR')
    d_i.Fit(fit_all_bins_plus_linear, 'WWSQR')

    p0_all_bins = ufloat(fit_all_bins.GetParameter(0), fit_all_bins.GetParError(0))
    p1_all_bins = ufloat(fit_all_bins.GetParameter(1), fit_all_bins.GetParError(1))
    k_value_all_bins = -p0_all_bins / p1_all_bins # other way of estimation: crossing the d = 1 line by exponential
    
    p0_exclude_first_bin = ufloat(fit_exclude_first_bin.GetParameter(0), fit_exclude_first_bin.GetParError(0))
    p1_exclude_first_bin = ufloat(fit_exclude_first_bin.GetParameter(1), fit_exclude_first_bin.GetParError(1))
    k_value_exclude_first_bin = -p0_exclude_first_bin / p1_exclude_first_bin # other way of estimation: crossing the d = 1 line by exponential

    p0_fit_all_bins_plus_flat = ufloat(fit_all_bins_plus_flat.GetParameter(0), fit_all_bins_plus_flat.GetParError(0))
    p1_fit_all_bins_plus_flat = ufloat(fit_all_bins_plus_flat.GetParameter(1), fit_all_bins_plus_flat.GetParError(1))
    p2_fit_all_bins_plus_flat = ufloat(fit_all_bins_plus_flat.GetParameter(2), fit_all_bins_plus_flat.GetParError(2))
    k_value_fit_all_bins_plus_flat = -1.0 / p0_fit_all_bins_plus_flat.nominal_value * ( p1_fit_all_bins_plus_flat.nominal_value - numpy.log( p2_fit_all_bins_plus_flat.nominal_value*0.1 ) )
    # Either round value to nearest integer, or pick the bin the value is in
    # Chosen latter for now
    # k_value_fit_all_bins_plus_flat = numpy.round(1+k_value_fit_all_bins_plus_flat,0)
    k_value_fit_all_bins_plus_flat = 1+ int( k_value_fit_all_bins_plus_flat )
    if k_value_fit_all_bins_plus_flat < 2:
        k_value_fit_all_bins_plus_flat = 2
    
    p0_fit_all_bins_plus_linear = ufloat(fit_all_bins_plus_linear.GetParameter(0), fit_all_bins_plus_linear.GetParError(0))
    p1_fit_all_bins_plus_linear = ufloat(fit_all_bins_plus_linear.GetParameter(1), fit_all_bins_plus_linear.GetParError(1))
    p2_fit_all_bins_plus_linear = ufloat(fit_all_bins_plus_linear.GetParameter(2), fit_all_bins_plus_linear.GetParError(2))
    p3_fit_all_bins_plus_linear = ufloat(fit_all_bins_plus_linear.GetParameter(3), fit_all_bins_plus_linear.GetParError(3))
    # k_value_fit_all_bins_plus_linear = -1.0 / p0_fit_all_bins_plus_linear.nominal_value * ( p1_fit_all_bins_plus_linear.nominal_value - numpy.log( p2_fit_all_bins_plus_linear.std_dev ) )

    x = 1
    y = numpy.exp( p0_fit_all_bins_plus_linear.nominal_value * x + p1_fit_all_bins_plus_linear.nominal_value )
    while y > 0.1 * ( p2_fit_all_bins_plus_linear.nominal_value + p3_fit_all_bins_plus_linear.nominal_value*x):
        x += 0.01
        y = numpy.exp( p0_fit_all_bins_plus_linear.nominal_value * x + p1_fit_all_bins_plus_linear.nominal_value )
        if x > 10: break
    # Either round value to nearest integer, or pick the bin the value is in
    # Chosen latter for now
    # k_value_fit_all_bins_plus_linear = numpy.round(1+x,0)
    k_value_fit_all_bins_plus_linear = 1+int(x)
    if k_value_fit_all_bins_plus_linear < 2:
        k_value_fit_all_bins_plus_linear = 2

    # print 'Fitted f_all = $e^{%.2f \\times i + %.2f }$' % ( p1_all_bins.nominal_value, p0_all_bins.nominal_value )
    # print 'Best k-value for %s using exponential fitted to all bins crossing d=1 method: %.2f +- %.2f' % (variable, k_value_all_bins.nominal_value, k_value_all_bins.std_dev)

    # print 'p1_all_bins: %f +- %f ' % ( p1_all_bins.nominal_value, p1_all_bins.std_dev )
    # print 'p0_all_bins: %f +- %f ' % ( p0_all_bins.nominal_value, p0_all_bins.std_dev )

    # print 'Fitted fit_exclude_first_bin = $e^{%.2f \\times i + %.2f }$' % ( p1_exclude_first_bin.nominal_value, p0_exclude_first_bin.nominal_value )
    # print 'Best k-value for %s using exponential fitted to all but 1st bin crossing d=1 method: %.2f +- %.2f' % (variable, k_value_exclude_first_bin.nominal_value, k_value_exclude_first_bin.std_dev)

    # print 'p1_exclude_first_bin: %f +- %f ' % ( p1_exclude_first_bin.nominal_value, p1_exclude_first_bin.std_dev )
    # print 'p0_exclude_first_bin: %f +- %f ' % ( p0_exclude_first_bin.nominal_value, p0_exclude_first_bin.std_dev )

    # print 'Fitted fit_all_bins_plus_flat = $e^{%.2f \\times i + %.2f }$' % ( p1_fit_all_bins_plus_flat.nominal_value, p0_fit_all_bins_plus_flat.nominal_value )

    print 'Fit exponential plus flat'
    print 'p0_fit_all_bins_plus_flat: %f +- %f ' % ( p0_fit_all_bins_plus_flat.nominal_value, p0_fit_all_bins_plus_flat.std_dev )
    print 'p1_fit_all_bins_plus_flat: %f +- %f ' % ( p1_fit_all_bins_plus_flat.nominal_value, p1_fit_all_bins_plus_flat.std_dev )
    print 'p2_fit_all_bins_plus_flat: %f +- %f ' % ( p2_fit_all_bins_plus_flat.nominal_value, p2_fit_all_bins_plus_flat.std_dev )

    print 'Fit exponential plus linear'
    print 'p0_fit_all_bins_plus_linear: %f +- %f ' % ( p0_fit_all_bins_plus_linear.nominal_value, p0_fit_all_bins_plus_linear.std_dev )
    print 'p1_fit_all_bins_plus_linear: %f +- %f ' % ( p1_fit_all_bins_plus_linear.nominal_value, p1_fit_all_bins_plus_linear.std_dev )
    print 'p2_fit_all_bins_plus_linear: %f +- %f ' % ( p2_fit_all_bins_plus_linear.nominal_value, p2_fit_all_bins_plus_linear.std_dev )
    print 'p3_fit_all_bins_plus_linear: %f +- %f ' % ( p3_fit_all_bins_plus_linear.nominal_value, p3_fit_all_bins_plus_linear.std_dev )

    # print '============== Suggested k value : ',1+k_value_fit_all_bins_plus_flat, numpy.round(1+k_value_fit_all_bins_plus_flat,0)

    k_values[variable] = k_value_fit_all_bins_plus_flat
    k_values_crosscheck[variable] = k_value_fit_all_bins_plus_linear
    print '============== ',variable,channel,k_value_fit_all_bins_plus_flat
    print '=== Other ',x,y,k_value_fit_all_bins_plus_linear

    rplt.hist( d_i )
    plt.title( r'SVD unfolding $d_i$ for $' + variables_latex[variable] + '$', CMS.title )
    plt.xlabel( r'$i$', CMS.x_axis_title )
    plt.ylabel( r'$d_i$', CMS.y_axis_title )
    axes = plt.axes()

    # x_all_bins = numpy.linspace(fit_all_bins.GetXmin(), fit_all_bins.GetXmax(), fit_all_bins.GetNpx()*4)#*4 for a very smooth curve
    # function_data_all_bins = numpy.frompyfunc(fit_all_bins.Eval, 1, 1)
    # plot(x_all_bins, function_data_all_bins(x_all_bins), axes=axes, color='red', linewidth=2)
    
    # x_exclude_first_bin = numpy.linspace(fit_exclude_first_bin.GetXmin(), fit_exclude_first_bin.GetXmax(), fit_exclude_first_bin.GetNpx()*4)#*4 for a very smooth curve
    # function_data_exclude_first_bin = numpy.frompyfunc(fit_exclude_first_bin.Eval, 1, 1)
    # plot(x_exclude_first_bin, function_data_exclude_first_bin(x_exclude_first_bin), axes=axes, color='red', linewidth=2)

    # #fill between fits
    x = numpy.arange(0, d_i.nbins(0), 0.1)
    # exp1 = numpy.exp( p1_all_bins.nominal_value*x + p0_all_bins.nominal_value )
    # exp2 = numpy.exp( p1_exclude_first_bin.nominal_value*x + p0_exclude_first_bin.nominal_value )
    # plt.fill_between(x, exp1, exp2, color='k', alpha=.5)

    x_all_bins_plus_flat = numpy.linspace(fit_all_bins_plus_flat.GetXmin(), fit_all_bins_plus_flat.GetXmax(), fit_all_bins_plus_flat.GetNpx()*4)#*4 for a very smooth curve
    function_data_all_bins_plus_flat = numpy.frompyfunc(fit_all_bins_plus_flat.Eval, 1, 1)
    plot(x_all_bins_plus_flat, function_data_all_bins_plus_flat(x_all_bins_plus_flat), axes=axes, color='green', linewidth=2)

    # x_all_bins_plus_linear = numpy.linspace(fit_all_bins_plus_linear.GetXmin(), fit_all_bins_plus_linear.GetXmax(), fit_all_bins_plus_linear.GetNpx()*4)#*4 for a very smooth curve
    # function_data_all_bins_plus_linear = numpy.frompyfunc(fit_all_bins_plus_linear.Eval, 1, 1)
    # plot(x_all_bins_plus_linear, function_data_all_bins_plus_linear(x_all_bins_plus_linear), axes=axes, color='blue', linewidth=2, linestyle = 'dashed')

    plt.tick_params( **CMS.axis_label_major )
    plt.tick_params( **CMS.axis_label_minor )

    axes.set_yscale( 'log', nonposy = "clip" )

    #adjust the y limits so all d_i are visible on log scale
    value_range = sorted( list( d_i.y() ) )
    for i, value in enumerate(value_range):
        if value == 0:
            del value_range[i]
    axes.set_ylim( ymin = min(value_range)/10 )

    text = 'Fit function:' 
    # text += '\n$f_{all~bins} = e^{%.2f \\times i + %.2f }$' % ( p1_all_bins.nominal_value, p0_all_bins.nominal_value )
    # text += '\n$f_{excl.1st} = e^{%.2f \\times i + %.2f }$' % ( p1_exclude_first_bin.nominal_value, p0_exclude_first_bin.nominal_value )
    text += '\n$f_{exp~plus~flat} = e^{%.2f \\times i + %.2f } + %.2f$' % ( p0_fit_all_bins_plus_flat.nominal_value, p1_fit_all_bins_plus_flat.nominal_value, p2_fit_all_bins_plus_flat.nominal_value )
    # text += '\n$f_{exp~plus~linear} = e^{%.2f \\times i + %.2f } + %.2f  %.2f \\times i$ ' % ( p0_fit_all_bins_plus_linear.nominal_value, p1_fit_all_bins_plus_linear.nominal_value, p2_fit_all_bins_plus_linear.nominal_value, p3_fit_all_bins_plus_linear.nominal_value )
    axes.text(0.3, 0.8, text,
        verticalalignment='bottom', horizontalalignment='left',
        transform=axes.transAxes,
        color='black', fontsize=30, bbox=dict(facecolor='white', edgecolor='none', alpha=0.5))

    # text = 'k value : %.1g (%.1g)' % ( k_value_fit_all_bins_plus_flat, k_value_fit_all_bins_plus_linear )
    text = 'k value : %.1g ' % ( k_value_fit_all_bins_plus_flat )

    axes.text(0.3, 0.7, text,
        verticalalignment='bottom', horizontalalignment='left',
        transform=axes.transAxes,
        color='black', fontsize=30, bbox=dict(facecolor='white', edgecolor='none', alpha=0.5))

    # Plot error band on flat component only
    line = plt.axhline( y = p2_fit_all_bins_plus_flat.nominal_value, linewidth = 2, color = 'red', linestyle = 'dashed' )
    # +/- 10%
    plt.fill_between(x, 0.9*p2_fit_all_bins_plus_flat.nominal_value, 1.1*p2_fit_all_bins_plus_flat.nominal_value, color='k', alpha=.25)
    # # +/- 1 sigma
    # plt.fill_between(x, p2_fit_all_bins_plus_flat.nominal_value-p2_fit_all_bins_plus_flat.std_dev, p2_fit_all_bins_plus_flat.nominal_value+p2_fit_all_bins_plus_flat.std_dev, color='k', alpha=.25)

    if CMS.tight_layout:
        plt.tight_layout()

    save_as_name = 'k_from_d_i_%s_channel_%s_%s' % ( channel, variable, test )

    for output_format in output_formats:
        plt.savefig(output_folder + save_as_name + '.' + output_format)
def make_control_region_comparison(
        control_region_1,
        control_region_2,
        name_region_1,
        name_region_2,
        histogram_properties=Histogram_properties(),
        #                                   show_ratio = True,
        save_folder='plots/',
        save_as=['pdf', 'png']):
    save_folder = check_save_folder(save_folder)
    # make copies in order not to mess with existing histograms
    control_region_1 = deepcopy(control_region_1)
    control_region_2 = deepcopy(control_region_2)
    # normalise as we are comparing shapes
    control_region_1.Scale(1 / control_region_1.Integral())
    control_region_2.Scale(1 / control_region_2.Integral())

    ratio = control_region_1.Clone('ratio')
    ratio.Divide(control_region_2)
    ratio.SetMarkerSize(3)

    control_region_1.fillcolor = 'yellow'
    control_region_2.fillcolor = 'red'
    control_region_1.fillstyle = 'solid'
    control_region_2.fillstyle = 'solid'
    control_region_1.legendstyle = 'F'
    control_region_2.legendstyle = 'F'

    # plot with matplotlib
    plt.figure(figsize=CMS.figsize, dpi=CMS.dpi, facecolor=CMS.facecolor)
    gs = gridspec.GridSpec(2, 1, height_ratios=[5, 1])
    axes = plt.subplot(gs[0])
    axes.minorticks_on()

    rplt.hist(control_region_1, axes=axes, alpha=0.5)
    rplt.hist(control_region_2, axes=axes, alpha=0.5)

    set_labels(plt, histogram_properties, show_x_label=False, axes=axes)

    handles, labels = axes.get_legend_handles_labels()

    labels.insert(0, name_region_1 + ' (1)')
    labels.insert(1, name_region_2 + ' (2)')

    l1 = axes.legend(handles,
                     labels,
                     numpoints=1,
                     frameon=histogram_properties.legend_color,
                     bbox_to_anchor=histogram_properties.legend_location,
                     bbox_transform=plt.gcf().transFigure,
                     prop=CMS.legend_properties,
                     ncol=histogram_properties.legend_columns)
    l1.set_zorder(102)

    x_limits = histogram_properties.x_limits
    y_limits = histogram_properties.y_limits
    if len(x_limits) == 2:
        axes.set_xlim(xmin=x_limits[0], xmax=x_limits[1])
    if len(y_limits) == 2:
        axes.set_ylim(ymin=y_limits[0], ymax=y_limits[1])
    else:
        y_max = get_best_max_y(
            [control_region_1, control_region_2],
            x_limits=x_limits) * histogram_properties.y_max_scale
        axes.set_ylim(ymin=0, ymax=y_max)
    plt.setp(axes.get_xticklabels(), visible=False)

    ax1 = plt.subplot(gs[1])
    ax1.minorticks_on()
    ax1.grid(True, 'major', linewidth=1)
    # dynamic tick placement
    adjust_ratio_ticks(ax1.yaxis, n_ticks=3)
    set_labels(plt, histogram_properties, show_x_label=True, show_title=False)
    plt.ylabel('(1)/(2)', CMS.y_axis_title)
    rplt.errorbar(ratio,
                  xerr=True,
                  emptybins=histogram_properties.emptybins,
                  axes=ax1)
    if len(x_limits) == 2:
        ax1.set_xlim(xmin=x_limits[0], xmax=x_limits[1])
    if len(histogram_properties.ratio_y_limits) == 2:
        ax1.set_ylim(ymin=histogram_properties.ratio_y_limits[0],
                     ymax=histogram_properties.ratio_y_limits[1])
    else:
        ax1.set_ylim(ymin=-0.5, ymax=4)
    # dynamic tick placement
    adjust_ratio_ticks(ax1.yaxis, n_ticks=3)
    ax1.set_ylim(ymin=-0.5, ymax=4)

    if CMS.tight_layout:
        plt.tight_layout()

    for save in save_as:
        plt.savefig(save_folder + histogram_properties.name + '.' + save)
    plt.close()
Example #49
0
## Set parameters for plotting
font_path = "/usr/share/fonts/stix/STIXGeneralItalic.otf"
font_name = font_manager.FontProperties(fname=font_path).get_name()
plt.rc('font',family=font_name)
plt.rcParams["figure.figsize"] = (10,6)
plt.rc('xtick',labelsize=20)
plt.rc('ytick',labelsize=20)
plt.title("b quark p$_{T}$",fontsize=15)
#plt.grid(which='major', linestyle='-.')
plt.xlabel("p$_{T}$ [GeV]", fontsize=15)
plt.ylabel("Number of Events", fontsize=15)
#plt.text(280, 500, "COMBINED",  ha='center',va='center',fontsize=25)
#plt.text(300, 75, "+ DELPHES3",  ha='center',va='center',fontsize=25)
#plt.plot(myhist1, label='W+')
#plt.plot(myhist2, label='W-')
#plt.legend(['W+', 'W-'], fontsize=15)
plt.minorticks_on()


## Draw hist
rplt.hist(G, linewidth=3,label='POWHEG v1 NLO',color="royalblue")
rplt.hist(H, linewidth=3,label='POWHEG v1 NNLO',color="red")
rplt.hist(I, linewidth=3,label='MADGRAPH5 NLO',color="black")
#rplt.hist(J, linewidth=3,label='nn23lo1 Delphes',color="green")


plt.legend(fontsize=15)
#plt.show()

plt.savefig("output.png")
def make_plot(histogram,
              histogram_properties=Histogram_properties(),
              save_folder='plots/',
              save_as=['pdf', 'png'],
              normalise=False,
              draw_errorbar=False,
              draw_legend=True):
    save_folder = check_save_folder(save_folder)
    histogram.SetTitle(histogram_properties.title)
    #    histogram.SetMarkerSize(CMS.data_marker_size)
    # to be changed
    histogram.fillcolor = '0.75'
    histogram.fillstyle = 'solid'
    if normalise:
        histogram.Scale(1 / histogram.Integral())

    # plot with matplotlib
    plt.figure(figsize=CMS.figsize, dpi=CMS.dpi, facecolor=CMS.facecolor)
    axes = plt.axes()

    if draw_errorbar:
        rplt.errorbar(histogram,
                      emptybins=histogram_properties.emptybins,
                      axes=axes,
                      elinewidth=2,
                      capsize=10,
                      capthick=2)
    else:
        rplt.hist(histogram)

    if draw_legend:
        l1 = axes.legend(numpoints=1,
                         frameon=histogram_properties.legend_color,
                         bbox_to_anchor=histogram_properties.legend_location,
                         bbox_transform=plt.gcf().transFigure,
                         prop=CMS.legend_properties,
                         ncol=histogram_properties.legend_columns)
        l1.set_zorder(102)

    adjust_axis_limits(axes, histogram_properties, [histogram])

    x_limits = histogram_properties.x_limits
    y_limits = histogram_properties.y_limits
    if len(x_limits) == 2:
        axes.set_xlim(xmin=x_limits[0], xmax=x_limits[1])
    if len(y_limits) == 2:
        axes.set_ylim(ymin=y_limits[0], ymax=y_limits[1])

    if histogram_properties.set_log_y:
        axes.set_yscale('log', nonposy="clip")
        if not len(
                histogram_properties.y_limits
        ) == 2:  # if not user set y-limits, calculate the limits from the tuple values
            value_range = sorted(list(histogram.y()))
            for i, value in enumerate(value_range):
                if value == 0:
                    del value_range[i]
            axes.set_ylim(ymin=min(value_range) / 10,
                          ymax=max(value_range) * 10)

    set_labels(plt, histogram_properties)

    if CMS.tight_layout:
        plt.tight_layout()

    for save in save_as:
        plt.savefig(save_folder + histogram_properties.name + '.' + save)
    plt.close()
def make_shape_comparison_plot(shapes=[],
                               names=[],
                               colours=[],
                               histogram_properties=Histogram_properties(),
                               fill_area=True,
                               make_ratio=False,
                               add_error_bars=True,
                               alpha=0.5,
                               save_folder='plots/',
                               save_as=['pdf', 'png'],
                               normalise_ratio_to_errors=False):
    save_folder = check_save_folder(save_folder)
    # make copies in order not to mess with existing histograms
    shapes_ = deepcopy(shapes)
    # normalise as we are comparing shapes
    for shape, colour, label in zip(shapes_, colours, names):
        shape.SetTitle(label)
        integral = shape.Integral()
        if integral > 0:
            shape.Sumw2()
            shape.Scale(1 / integral)
        if fill_area:
            shape.fillcolor = colour
            shape.fillstyle = 'solid'
            shape.legendstyle = 'F'
        else:
            shape.linecolor = colour
            shape.legendstyle = 'F'
            shape.linewidth = 5

    if not histogram_properties.y_limits:
        histogram_properties.y_limits = [0, get_best_max_y(shapes_, False)]
    # plot with matplotlib
    plt.figure(figsize=CMS.figsize, dpi=CMS.dpi, facecolor=CMS.facecolor)
    gs = gridspec.GridSpec(2, 1, height_ratios=[5, 1])
    axes = None
    if make_ratio:
        axes = plt.subplot(gs[0])
    else:
        axes = plt.axes()
    axes.minorticks_on()

    for shape in shapes_:
        rplt.hist(shape, axes=axes, alpha=alpha)

    set_labels(plt,
               histogram_properties,
               show_x_label=not make_ratio,
               axes=axes)
    handles, labels = axes.get_legend_handles_labels()
    for i, name in enumerate(names):
        labels.insert(i, name)

    # always fill legends
    if not fill_area:
        for handle in handles:
            handle.set_fill(True)
            handle.set_facecolor(handle.get_edgecolor())

    l1 = axes.legend(handles,
                     labels,
                     numpoints=1,
                     frameon=histogram_properties.legend_color,
                     bbox_to_anchor=histogram_properties.legend_location,
                     bbox_transform=plt.gcf().transFigure,
                     prop=CMS.legend_properties,
                     ncol=histogram_properties.legend_columns)
    l1.set_zorder(102)
    #add error bars
    graphs = spread_x(shapes_, list(shapes_[0].xedges()))
    if add_error_bars:
        for graph in graphs:
            rplt.errorbar(graph, axes=axes)

    adjust_axis_limits(axes, histogram_properties, shapes_)
    if make_ratio:
        plt.setp(axes.get_xticklabels(), visible=False)
        ratios = get_histogram_ratios(shapes_[0], shapes_[1:],
                                      normalise_ratio_to_errors)
        ax1 = plt.subplot(gs[1])
        ax1.minorticks_on()
        ax1.grid(True, 'major', linewidth=1)
        set_labels(plt,
                   histogram_properties,
                   show_x_label=True,
                   show_title=False)
        if normalise_ratio_to_errors:
            plt.ylabel(r'$\frac{1-2}{\sqrt{(\sigma_1)^2 + (\sigma_2)^2}}$',
                       CMS.y_axis_title)
        else:
            plt.ylabel('(1)/(2)', CMS.y_axis_title)
        for ratio in ratios:
            ratio.SetMarkerSize(2)
            rplt.errorbar(ratio,
                          xerr=True,
                          emptybins=histogram_properties.emptybins,
                          axes=ax1)
        if len(histogram_properties.x_limits) == 2:
            ax1.set_xlim(xmin=histogram_properties.x_limits[0],
                         xmax=histogram_properties.x_limits[1])
        if len(histogram_properties.ratio_y_limits) == 2:
            ax1.set_ylim(ymin=histogram_properties.ratio_y_limits[0],
                         ymax=histogram_properties.ratio_y_limits[1])
        # dynamic tick placement
        adjust_ratio_ticks(ax1.yaxis, n_ticks=3)

    if CMS.tight_layout:
        plt.tight_layout()

    for save in save_as:
        plt.savefig(save_folder + histogram_properties.name + '.' + save)
    plt.close()
Example #52
0
# --Show histogram using TCanvas
if (not args.save and args.TCanvas ):

	c1 = ROOT.TCanvas()
	c1.cd()
	histMass.GetXaxis().SetTitle("M_{T} [GeV]")
	histMass.GetYaxis().SetTitle("Events")
	histMass.Draw("Hist")
	dummy=input("Press Enter to continue...")


# --Show histogram using Matplotlib
if (not args.save and not args.TCanvas):
	# Set parametres for plotting
	plt.rcParams["figure.figsize"] = (10,6)
	plt.rc('xtick', labelsize=15)
	plt.rc('ytick', labelsize=15)
	plt.title("Transverse mass", fontsize=25)
	plt.xlabel("$M_{T} [GeV]$",fontsize=15)
	plt.ylabel("Events",fontsize=15)
	
	plt.grid(which='major', linestyle='-.')
	
	# Draw hist
	rplt.hist(histMass,linewidth=3, color="royalblue",label="W^{+} transverse mass")
	plt.xticks([0,20,40,60,80,100,120,140,160,180,200])
	plt.yticks([0,20,40,60,80,100,120,140])
	plt.legend()
	plt.show()

Example #53
0
    from utils import pycopy, hist_info

    for i, roc in enumerate(rocs):
        fig = plt.figure('ROC curve')
        axes = fig.add_subplot(111)
        axes.grid(axis='both')
        axes.set_title('ROC curve')
        axes.set_xlim(axis_range, 1)
        axes.set_ylim(axis_range, 1)
        axes.set_xlabel('Signal selection efficiency')
        axes.set_ylabel('Background rejection efficiency')
        axes.xaxis.set_label_coords(0.9, -0.05)
        for key, hist in roc.iteritems():
            info = hist_info(hist)
            hist = pycopy(Hist, ROOT.TH1, hist, *info[0], **info[1])
            line = rplt.hist(hist, stacked=False)
            print type(line), line
        axes.legend(fontsize=10, numpoints=1, frameon=False, ncol=3)

    if doprint:
        pp.savefig()
        pp.close()
    elif not options.batch:
        plt.show()
else:
    canvas = ROOT.TCanvas('canvas', '', 800, 600)
    if doprint:
        canvas.Print('{}_ROC_curves.pdf['.format(prefix))

    cols = (ROOT.kAzure, ROOT.kRed, ROOT.kBlack)
    legend = ROOT.TLegend(0.12, 0.15, 0.8, 0.6)