Example #1
0
# set visual attributes
h1.fillstyle = 'solid'
h1.fillcolor = 'green'
h1.linecolor = 'green'
h1.linewidth = 0

h2.fillstyle = 'solid'
h2.fillcolor = 'red'
h2.linecolor = 'red'
h2.linewidth = 0

stack = HistStack()
stack.Add(h1)
stack.Add(h2)
# hack to change y-axis range in ROOT
stack.SetMaximum(stack.GetMaximum() * 1.2)

# plot with ROOT
style = get_style('ATLAS')
style.SetEndErrorSize(3)
set_style(style)
canvas = Canvas(width=700, height=500)
stack.Draw('HIST E1 X0')
h3.Draw('SAME E1 X0')
stack.xaxis.SetTitle('Mass')
stack.yaxis.SetTitle('Events')
# set the number of expected legend entries
legend = Legend(3, leftmargin=0.45, margin=0.3)
legend.AddEntry(h1, style='F')
legend.AddEntry(h2, style='F')
legend.AddEntry(h3, style='LEP')
Example #2
0
def plotHistos(histos, text="", option="", statbox=True):
    """ 
    Plots a list of histograms
    """
    log = logging.getLogger('pyroplot')
    import rootpy
    from rootpy.plotting import Hist, HistStack
    from ROOT import kRed, gPad, TPaveStats
    # only for 1D and 2D type histograms:
    # determine and set maximum for all histograms
    stack = HistStack()
    for hist in histos:
        if not hist.__class__.__name__ == "Hist3D":
            stack.Add(hist)
    # determine maximum value and set it for all histograms
    if stack.GetHists():
        maxplot = stack.GetMaximum()
        minplot = stack.GetMinimum()
        for hist in stack.GetHists():
            hist.SetMaximum(maxplot)
            # special treatment for log scale Y
            if gPad.GetLogy():
                hist.SetMinimum(1.)
            else:
                # if histogram minimum is positive, set to 0.
                if minplot > 0:
                    hist.SetMinimum(0.)
    for idx, hist in enumerate(histos):
        try:
            thisopt = option
            # if not first histo, add "same" to options so previous ones are not overwritten
            if idx:
                if statbox:
                    thisopt += "sames"
                else:
                    thisopt += "same"
            histcopy = hist.DrawCopy(thisopt)
            # on first go: print identifying text on pad
            if not idx and text:
                markPad(text=text, x=.14, y=.85, size=0.041)
                if statbox:
                    thisopt += "sames"
                else:
                    thisopt += "same"
                histcopy.Draw(thisopt)
            gPad.Update()
            if statbox:
                try:
                    statBox = histcopy.GetListOfFunctions().FindObject("stats")
                    # offset from last statbox
                    offset = .18
                    # needs to be larger for Profile & 2D histos
                    if (hist.__class__.__name__ == "Profile"
                            or hist.__class__.__name__ == "Hist2D"
                            or hist.__class__.__name__ == "Profile2D"
                            or hist.__class__.__name__ == "Hist3D"):
                        offset = .26
                    statBox.SetY1NDC(statBox.GetY1NDC() - offset * (idx))
                    statBox.SetY2NDC(statBox.GetY2NDC() - offset * (idx))
                    statBox.SetTextColor(hist.GetLineColor())
                    statBox.SetBorderSize(2)
                except AttributeError:
                    log.debug("Could not get statbox for histogram " +
                              hist.GetName())

        except rootpy.ROOTError, e:
            log.error(
                "Drawing histogram %s caused ROOTError exception ('%s')" %
                (hist.GetName(), e.msg))
            gPad.Clear()  # otherwise this happens again when drawing..
            markPad(text="Could not draw %s ('%s')" % (hist.GetName(), e.msg),
                    x=0.14,
                    y=0.4,
                    size=0.023,
                    color=kRed)
            return  # give up!