Ejemplo n.º 1
0
def drawRatio(histRatio,stack,Region,BLINDEDLIST,nbins,xmin,xmax):
    #histRatio = datastack.sum.Clone()
    #histRatio.merge_bins([(0, 1), (-2, -1)])

    tempDataStack = histRatio.Clone()
    ratioMinimum=0
    ratioMaximum=2
    histRatio.SetMinimum(ratioMinimum)
    histRatio.SetMaximum(ratioMaximum)
    histRatio.yaxis.divisions = 5
    histRatio.Divide(stack.sum)

    if Region not in BLINDEDLIST: 
        histRatio.Draw("PE")
    
    line = Line(float(xmin)+1e-2*float(xmax),1.,float(xmax)-1e-2*float(xmax),1.);
    line.SetLineWidth(4);
    line.SetLineColor("red");
    line.Draw();
    

    if Region not in BLINDEDLIST:
        histRatio.Draw("SAME EP")

    MCerrorband = stack.sum.Clone()
    MCerrorband.SetLineWidth(10)
    MCerrorband.SetFillStyle(3244)
    MCerrorband.SetFillColor(922)


    for i in range(1,int(nbins)+1):
        if stack.sum.GetBinContent(i) != 0:
            if tempDataStack.GetBinContent(i)/stack.sum.GetBinContent(i) >= 2:
                xcoord = stack.sum.GetBinCenter(i)
                arrow = Arrow(xcoord,1.67,xcoord,1.87,0.015,"|>")
                arrow.SetAngle(50)
                arrow.SetLineWidth(6)
                arrow.SetLineColor(2)
                arrow.SetFillColor(2)
                arrow.Draw()
            if tempDataStack.GetBinContent(i)/stack.sum.GetBinContent(i) <= -2:
                xcoord = stack.sum.GetBinCenter(i)
                arrow = Arrow(xcoord,0.33,xcoord,0.13,0.015,"|>")
                arrow.SetAngle(50)
                arrow.SetLineWidth(6)
                arrow.SetLineColor(2)
                arrow.SetFillColor(2)
                arrow.Draw();
        
        mcerror = 0.0
        content = stack.sum.GetBinContent(i)
        error = stack.sum.GetBinError(i)
        if content != 0 :
            mcerror = (content+error)/content - 1.0 
        MCerrorband.SetBinContent(i,1)
        MCerrorband.SetBinError(i,mcerror)
        
    MCerrorband.Draw("E2PSAME")
Ejemplo n.º 2
0
def plotratio(datastack, stack):
    histRatio = datastack.sum.Clone()
    #histRatio.merge_bins([(0, 1), (-2, -1)])
    ratioMinimum = 0
    ratioMaximum = 2
    histRatio.SetMinimum(ratioMinimum)
    histRatio.SetMaximum(ratioMaximum)

    histRatio.xaxis.SetTitle(xtitle)
    histRatio.yaxis.SetTitle("Data/SM")
    histRatio.yaxis.CenterTitle()
    histRatio.yaxis.divisions = 5
    histRatio.Divide(stack.sum)
    histRatio.xaxis.SetTitleOffset(3.4)
    histRatio.yaxis.SetTitleOffset(2)
    histRatio.xaxis.set_label_size(int(canvasheight * labelscale * 1.5))
    histRatio.yaxis.set_label_size(int(canvasheight * labelscale * 1.5))

    if plotData:
        histRatio.Draw("PE")

    line = Line(
        float(xmin) + 1e-2 * float(xmax), 1.,
        float(xmax) - 1e-2 * float(xmax), 1.)
    line.SetLineWidth(4)
    line.SetLineColor("red")

    if plotData:
        line.Draw("Same")
    else:
        line.Draw()

    if plotData:
        histRatio.Draw("SAME EP")

    MCerrorband = stack.sum.Clone()
    MCerrorband.SetLineWidth(10)
    MCerrorband.SetFillStyle(3244)
    MCerrorband.SetFillColor(922)

    for i in range(1, int(nbins) + 1):
        if stack.sum.GetBinContent(i) != 0:
            if datastack.sum.GetBinContent(i) / stack.sum.GetBinContent(
                    i) >= 2:
                xcoord = stack.sum.GetBinCenter(i)
                arrow = Arrow(xcoord, 1.67, xcoord, 1.87, 0.015, "|>")
                arrow.SetAngle(50)
                arrow.SetLineWidth(6)
                arrow.SetLineColor(2)
                arrow.SetFillColor(2)
                arrow.Draw()
            if datastack.sum.GetBinContent(i) / stack.sum.GetBinContent(
                    i) <= -2:
                xcoord = stack.sum.GetBinCenter(i)
                arrow = Arrow(xcoord, 0.33, xcoord, 0.13, 0.015, "|>")
                arrow.SetAngle(50)
                arrow.SetLineWidth(6)
                arrow.SetLineColor(2)
                arrow.SetFillColor(2)
                arrow.Draw()

        mcerror = 0.0
        content = stack.sum.GetBinContent(i)
        error = stack.sum.GetBinError(i)
        if content != 0:
            mcerror = (content + error) / content - 1.0
        MCerrorband.SetBinContent(i, 1)
        MCerrorband.SetBinError(i, mcerror)

    MCerrorband.Draw("E2PSAME")
Ejemplo n.º 3
0
def pvalue_plot(poi,
                pvalues,
                pad=None,
                xtitle='X',
                ytitle='P_{0}',
                linestyle=None,
                linecolor=None,
                yrange=None,
                verbose=False):
    """
    Draw a pvalue plot

    Parameters
    ----------
    poi : list
        List of POI values tested
    pvalues : list
        List of p-values or list of lists of p-values to overlay
        multiple p-value curves
    pad : Canvas or Pad, optional (default=None)
        Pad to draw onto. Create new pad if None.
    xtitle : str, optional (default='X')
        The x-axis label (POI name)
    ytitle : str, optional (default='P_{0}')
        The y-axis label
    linestyle : str or list, optional (default=None)
        Line style for the p-value graph or a list of linestyles for
        multiple p-value graphs.
    linecolor : str or list, optional (default=None)
        Line color for the p-value graph or a list of linestyles for
        multiple p-value graphs.

    Returns
    -------
    pad : Canvas
        The pad.
    graphs : list of Graph
        The p-value graphs

    """
    if not pvalues:
        raise ValueError("pvalues is empty")
    if not poi:
        raise ValueError("poi is empty")
    # determine if pvalues is list or list of lists
    if not isinstance(pvalues[0], (list, tuple)):
        pvalues = [pvalues]
    if linecolor is not None:
        if not isinstance(linecolor, list):
            linecolor = [linecolor]
        linecolor = cycle(linecolor)
    if linestyle is not None:
        if not isinstance(linestyle, list):
            linestyle = [linestyle]
        linestyle = cycle(linestyle)

    with preserve_current_canvas():
        if pad is None:
            pad = Canvas()
        pad.cd()
        pad.SetLogy()

        # create the axis
        min_poi, max_poi = min(poi), max(poi)
        haxis = Hist(1000, min_poi, max_poi)
        xaxis = haxis.xaxis
        yaxis = haxis.yaxis
        xaxis.SetRangeUser(min_poi, max_poi)
        haxis.Draw('AXIS')

        min_pvalue = float('inf')
        graphs = []
        for ipv, pv in enumerate(pvalues):
            graph = Graph(len(poi),
                          linestyle='dashed',
                          drawstyle='L',
                          linewidth=2)
            for idx, (point, pvalue) in enumerate(zip(poi, pv)):
                graph.SetPoint(idx, point, pvalue)
            if linestyle is not None:
                graph.linestyle = linestyle.next()
            if linecolor is not None:
                graph.linecolor = linecolor.next()
            graphs.append(graph)
            curr_min_pvalue = min(pv)
            if curr_min_pvalue < min_pvalue:
                min_pvalue = curr_min_pvalue

        if verbose:
            for graph in graphs:
                log.info(['{0:1.1f}'.format(xval) for xval in list(graph.x())])
                log.info(['{0:0.3f}'.format(yval) for yval in list(graph.y())])

        # automatically handles axis limits
        axes, bounds = draw(graphs,
                            pad=pad,
                            same=True,
                            logy=True,
                            xtitle=xtitle,
                            ytitle=ytitle,
                            xaxis=xaxis,
                            yaxis=yaxis,
                            ypadding=(0.2, 0.1),
                            logy_crop_value=1E-300)

        if yrange is not None:
            xaxis, yaxis = axes
            yaxis.SetLimits(*yrange)
            yaxis.SetRangeUser(*yrange)
            min_pvalue = yrange[0]

        # draw sigma levels up to minimum of pvalues
        line = Line()
        line.SetLineStyle(2)
        line.SetLineColor(2)
        latex = ROOT.TLatex()
        latex.SetNDC(False)
        latex.SetTextSize(20)
        latex.SetTextColor(2)
        sigma = 0
        while True:
            pvalue = gaussian_cdf_c(sigma)
            if pvalue < min_pvalue:
                break
            keepalive(
                pad,
                latex.DrawLatex(max_poi, pvalue, " {0}#sigma".format(sigma)))
            keepalive(pad, line.DrawLine(min_poi, pvalue, max_poi, pvalue))
            sigma += 1

        pad.RedrawAxis()
        pad.Update()
    return pad, graphs