示例#1
0
def plot(name, title, variable, *tree_parts, **kwargs):

    fallthrough = kwargs.pop("fallthrough", 0)
    def get_name(tree_part):
        for i in xrange(fallthrough):
            if not tree_part.parent:
                break
            tree_part = tree_part.parent
        return tree_part.name

    logy = kwargs.pop("logy", None)
    normalize = kwargs.pop("normalize", None)
    pos = kwargs.pop("pos", "RT")
    x_range = kwargs.pop("x_range", None)
    y_range = kwargs.pop("y_range", None)
    x_title = kwargs.pop("x_title", None)
    y_title = kwargs.pop("y_title", None)
    legend_header = kwargs.pop("legend_header", "")
    rebin = kwargs.pop("rebin", None)

    with canvas() as c:
        if logy: c.SetLogy()
        
        namehs = [("%s %s" % (tp.basename, get_name(tp)), fixup_hist_units(tp[variable])) for tp in tree_parts]
        if normalize is True:
            title += " (normalized)"
            for n, h in namehs:
                #h.GetYaxis().SetTitle("Arbitrary Units")
                y_title = "Arbitrary Units"
                h.Scale(1. / h.Integral())
        elif normalize:
            try:
                normalize_hists([h for n, h in namehs], normalize)
                title += " (normalized to %s above %i GeV)" % (namehs[0][0], normalize)
            except ZeroDivisionError:
                pass
            
        if rebin:        
            for n, h in namehs:
                h.Rebin(rebin)
                
        hs = [(h, None, hname) for hname, h in namehs]
        sl = C.StackLegend(name, pos, title, legend_header=legend_header, *hs)
        
        if x_range is not None:
            sl.set_x_range(x_range)
        if y_range is not None:
            # Doesn't seem to work
            sl.Y.SetRangeUser(*y_range)
        if x_title is not None:
            sl.X.SetTitle(x_title)
        if y_title is not None:
            sl.Y.SetTitle(y_title)
            
        sl.Draw()
        name = "_".join(expand_hname(name, variable))
        c.SaveAs("plots/purity/%s.eps" % name)
示例#2
0
文件: fit.py 项目: pwaller/pwa
def multifit(self, params):
    import rootils.uncert as U
    from minty.utils import wait, canvas
    gROOT.SetBatch()
    R.gStyle.SetOptStat(1000000) # Integral only
    R.gStyle.SetOptFit(11)
    
    f = TFile("all.root")
    h = f.Get("corrected/ph/boson/mass_wide")
    
    window_size = 25
    
    bins = array('d', [])
    values = array('d', [])
    errors = array('d', [])
    
    for i in xrange(80, 600, 5):
        h.GetXaxis().SetRangeUser(i - window_size/2, i + window_size/2)
        
        with canvas() as c:
            const, slope = U.from_fitresultp(h.Fit("expo", "LLISQ"))
            c.SaveAs("plots/massfit-{0}GeV.png".format(i))
        
        integral = h.Integral()
        
        bins.append(i)
        values.append(slope.nominal_value)
        errors.append(slope.std_dev())
     
    gROOT.SetBatch(0)
    with canvas() as c:
        g = R.TGraphErrors(len(bins), bins, values, array('d', [0]*len(bins)), errors)
        g.Draw("A*P")
        c.SaveAs("plots/massfit-pieces.png")
        
        wait()