def draw(ws, res, eff, var, cat, xtitle, trig, nfitparams, xlow=0, xup=400, ymax=1.1, oldres=[]):
    # Set variable-width binning
    tmpbins = [x * 0.05 for x in range(0, 20)]+[1,10,20,30,40,50,60,70,80,90,100,110,120,140,160,180,200,220,240,280,320,360,400,480,600,750,1000]
    if kTrig == "PFMET100HT400" or kTrig == "PFMET80HT400" or kTrig == "PFMET80CJ30x2CSV07" or kTrig == "PFMET100CJ80CSV07":  # low stat
        tmpbins = [0,10,20,30,40,50,60,70,80,90,100,110,120,140,160,200,240,320,400,480,600,750,1000]
    tmpbins2 = [xlow] + [x for x in tmpbins if (x > xlow and x < xup)] + [xup]
    bins = RooBinning(len(tmpbins2)-1, numpy.array(tmpbins2, dtype=float))

    kColor = 4  # kBlue
    kColorErr = 432  # kCyan
    new = len(oldres)>0
    if new:
        kColorOld = kColor
        kColorErrOld = kColorErr
        kColor = 618  # kMagenta+2
        kColorErr = 609  # kMagenta-7

    frame1 = ws.var(var).frame(RooFit.Bins(40), RooFit.Title("Data (all, accepted)"))
    ds.plotOn(frame1)
    ds.plotOn(frame1, RooFit.Cut("%s==%s::accept" % (cat,cat)), RooFit.MarkerColor(kColor), RooFit.LineColor(kColor))

    frame2 = ws.var(var).frame(RooFit.Title("; %s; HLT efficiency" % xtitle))
    if new:
        ws.saveSnapshot("2015alpha", "fit0,fit1,fit2")
        ws.loadSnapshot("2012D")
        ws.function(eff).plotOn(frame2, RooFit.VisualizeError(oldres[0]), RooFit.FillColor(kColorErrOld))
        ws.function(eff).plotOn(frame2, RooFit.LineColor(kColorOld))
        ws.loadSnapshot("2015alpha")

    ws.function(eff).plotOn(frame2, RooFit.VisualizeError(res), RooFit.FillColor(kColorErr))
    ws.function(eff).plotOn(frame2, RooFit.LineColor(kColor))
    ds.plotOn(frame2, RooFit.Binning(bins), RooFit.Efficiency(ws.cat(cat)))
    nchisq = frame2.chiSquare(nfitparams)
    ndof = bins.numBins()-nfitparams
    chisq = nchisq * ndof

    gPad.SetLeftMargin(0.15); frame2.GetYaxis().SetTitleOffset(1.25); frame2.GetYaxis().SetRangeUser(0,ymax)
    #frame1.Draw()
    frame2.Draw()
    CMS_label()

    latex.SetTextSize(0.044)
    latex.DrawLatex(0.60, 0.30, "#chi^{2}/ndof = %4.2f/%.0f" % (chisq,ndof))
    text = ["** "+trig+" **"]
    text.append("chi2/ndof      %6.2f  =  %4.2f/%.0f" % (nchisq, chisq, ndof))
    print "chi2/ndof      %6.2f  =  %4.2f/%.0f" % (nchisq, chisq, ndof)

    latex.SetTextSize(0.026)
    latex.DrawLatex(0.60, 0.26, trig)
    for i in xrange(3):
        v = ws.var("fit%i" % i)
        if v.getVal() > 1:
            text.append("p_%i            %6.2f  +/- %3.2f" % (i, v.getVal(), v.getError()))
            latex.DrawLatex(0.60, 0.26-(i+1)*0.03, "p_{%i}            %6.2f  +/- %3.2f" % (i, v.getVal(), v.getError()))
        else:
            text.append("p_%i             %6.3f +/- %4.3f" % (i, v.getVal(), v.getError()))
            latex.DrawLatex(0.60, 0.26-(i+1)*0.03, "p_{%i}             %6.3f +/- %4.3f" % (i, v.getVal(), v.getError()))
    line.DrawLine(xlow, 1, xup, 1)
    text.append("")
    return text
Ejemplo n.º 2
0
class Binned_MassPdf( MassPdf ) :
    def __init__( self, Name, Mass, **kwargs ) :
        self._name = Name
        self._mass = Mass

        # get binning
        self._bins = kwargs.pop( 'Binning', None )
        if not self._bins :
            # create binning
            from array import array
            binBounds = kwargs.pop( 'BinBoundaries', [ self._mass.getMin(), self._mass.getMax() ] )
            self._binBounds = array( 'd', binBounds )
            self._numBins = len(binBounds) - 1

            from ROOT import RooBinning
            self._bins = RooBinning( self._numBins, self._binBounds, self._name + '_binning' )
            self._mass.setBinning( self._bins, self._name + '_binning' )

        self._numBins = self._bins.numBins()

        # determine number of events in each bin
        self._data = kwargs.pop( 'Data', None )
        if self._data :
            assert self._mass._var in self._data.get(0),\
                    'Binned_MassPdf.__init__(): %s is not and observable in the provided data set' % self._mass.GetName()
            self._numEvents = self._data.sumEntries()
            self._numEventsBins = self._numBins * [ 0. ]
            for obsSet in self._data :
                bin = self._bins.binNumber( obsSet.getRealValue( self._mass.GetName() ) )
                self._numEventsBins[bin] += self._data.weight()
                

        # create bin coefficients
        self._coefs = [ ]
        for bin in range( 1, self._numBins ) :
            self._parseArg(  '%s_coef%d' % ( self._name, bin ), kwargs
                           , Title    = '%s bin coefficient %d' % ( self._name, bin )
                           , Value    = self._numEventsBins[bin] / self._numEvents if self._data else 1. / self._numBins
                           , MinMax   = ( 0., 1. )
                           , ContainerList = self._coefs
                          )

        # create a BinnedPdf
        from P2VV.RooFitWrappers import BinnedPdf
        pdf = BinnedPdf(  Name = self._name
                        , Observable = self._mass
                        , Binning = self._bins
                        , Coefficients = self._coefs
                        , BinIntegralCoefs = True
                       )

        # initialize
        MassPdf.__init__( self, pdf = pdf )
        self._check_extraneous_kw( kwargs )