示例#1
0
    def mergeDataSets(self, categories, inputFiles, lepton=None, dilepton=None):
        keys = inputFiles.keys()

        data = RootTools.getDataSet(inputFiles[keys[0]], "RMRTree")
        args = data.get(0)

        if lepton is not None:
            args.add(lepton)
        if dilepton is not None:
            args.add(dilepton)

        args = ["MergedDataSet", "MergedDataSet", args, rt.RooFit.Index(categories), rt.RooFit.Import(keys[0], data)]
        for k in keys[1:]:
            d = RootTools.getDataSet(inputFiles[k], "RMRTree")
            args.append(rt.RooFit.Import(k, d))

        a = tuple(args)
        data = rt.RooDataSet(*a)

        if lepton is not None:
            a = rt.RooArgSet(lepton)
            aa = rt.RooDataSet("Leptons", "Leptons", a)

            for i in xrange(data.numEntries()):
                row = data.get(i)
                box = row.getCatLabel("Boxes")
                if box in ["Mu", "Ele", "MuMu", "EleEle", "MuEle"]:
                    lepton.setLabel("Lepton")
                else:
                    lepton.setLabel("Hadron")
                aa.add(rt.RooArgSet(lepton))
            data.merge(aa)

        if dilepton is not None:
            a = rt.RooArgSet(dilepton)
            aa = rt.RooDataSet("DiLeptons", "DiLeptons", a)
            for i in xrange(data.numEntries()):
                row = data.get(i)
                box = row.getCatLabel("Boxes")
                if box in ["MuMu", "EleEle", "MuEle"]:
                    dilepton.setLabel("DiLepton")
                elif box in ["Mu", "Ele"]:
                    dilepton.setLabel("SingleLepton")
                else:
                    dilepton.setLabel("NonLepton")
                aa.add(rt.RooArgSet(dilepton))
            data.merge(aa)

        return data
示例#2
0
    def plot2D(self, inputFile, xvarname, yvarname, ranges=None, data = None):
        
        if ranges is None:
            ranges = ['']
        
        #before I find a better way
        if data is None:
            data = RootTools.getDataSet(inputFile,'RMRTree')
        #data = data.reduce(self.getVarRangeCutNamed(ranges=ranges))
        toyData = self.workspace.pdf(self.fitmodel).generate(rt.RooArgSet(self.workspace.argSet(xvarname+","+yvarname)), 50*data.numEntries())
        toyData = toyData.reduce(self.getVarRangeCutNamed(ranges=ranges))

        xmin = min([self.workspace.var(xvarname).getMin(r) for r in ranges])
        xmax = max([self.workspace.var(xvarname).getMax(r) for r in ranges])
        ymin = min([self.workspace.var(yvarname).getMin(r) for r in ranges])
        ymax = max([self.workspace.var(yvarname).getMax(r) for r in ranges])

        # define 2D histograms
        histoData = rt.TH2D("histoData", "histoData",
                            100, xmin, xmax, 
                            100, ymin, ymax)
        histoToy = rt.TH2D("histoToy", "histoToy",
                            100, xmin, xmax, 
                            100, ymin, ymax)
        # reduced the data to the region in which we generate the toy
        reducedData = data.reduce(self.getVarRangeCutNamed(ranges))
        # project the data on the histograms
        reducedData.fillHistogram(histoData,rt.RooArgList(self.workspace.var(xvarname),self.workspace.var(yvarname)))
        toyData.fillHistogram(histoToy,rt.RooArgList(self.workspace.var(xvarname),self.workspace.var(yvarname)))
        histoToy.Scale(histoData.Integral()/histoToy.Integral())
        histoData.Add(histoToy, -1)
        histoData.SetName('Compare_Data_MC_%s' % '_'.join(ranges) )
        return histoData
def writeCocktail(box, files, outdir):
    """Write a cocktail file using the weights from the datasets"""
    
    components = [os.path.basename(f).split('_')[0] for f in files]
    if len(components) != len(set(components)):
        raise Exception('Some components not unique for box %s: %s' % (box,components))
    
    ds = []
    for f in files:
        ds.append(RootTools.getDataSet(f,'RMRTree'))
    if not ds:
        raise Exception('Not enough datasets found: %i' % len(ds))

    row = ds[0].get()
    row.Print("V")
    
    mRmin = row['MR'].getMin()
    rsqMin = row['Rsq'].getMin()
    rMin = rt.TMath.Sqrt(rsqMin)
    
    #tData = rt.RooDataSet('RMRTree','Total Dataset',row)
    #for d in ds:
    #    tData.append(d)
    #wData = rt.RooDataSet('RMRTree','Weighted Cocktail',tData,row,'MR>=0.','W')
    
    wData = ds[0].Clone('RMRTree')
    for ids in range(1,len(ds)):
        wData.append(ds[ids])
    
    output = rt.TFile.Open(outdir+"/SMCocktail_MR"+str(mRmin)+"_R"+str(rMin)+"_"+box+'.root','RECREATE')
    print 'Writing',output.GetName()
    wData.Write()
    output.Close()
示例#4
0
    def plotObservables(self, inputFile, name = None, range = ''):
        """Make control plots for variables defined in the 'variables' part of the config"""

        if name is None:
            name = self.fitmodel

        data = RootTools.getDataSet(inputFile,'RMRTree',self.cut)
        fitmodel = self.workspace.pdf(name)
        
        plots = []
        parameters = self.workspace.set("variables")
        #use a binned dataset to make the plots as it is faster        
        hvars = rt.RooArgSet()
        for p in RootTools.RootIterator.RootIterator(parameters):
            p.setBins(100)
            hvars.add(p)
        hdata = rt.RooDataHist('projData_%s' % self.name,'projData',hvars,data.reduce(rt.RooFit.CutRange(range)))
        hdata = hdata.reduce(self.getVarRangeCutNamed(ranges=range.split(',')))
        
        for p in RootTools.RootIterator.RootIterator(parameters):
            
            xmin = min([p.getMin(r) for r in range.split(',')])
            xmax = max([p.getMax(r) for r in range.split(',')])
            
            frame = p.frame(xmin,xmax)
            frame.SetName("autoVarPlot_%s_%s" % (p.GetName(), '_'.join(range.split(',')) ) )
            #
            hdata.plotOn(frame)
            fitmodel.plotOn(frame, rt.RooFit.NumCPU(RootTools.Utils.determineNumberOfCPUs()),rt.RooFit.Range(xmin,xmax),rt.RooFit.ProjWData(hdata))
            fitmodel.paramOn(frame)
            plots.append(frame)
            
        return plots
示例#5
0
 def makeRooHistPdf(self, inputFile, modelName):
     
     vars = self.workspace.set('variables')
     #signal = RootTools.getDataSet(inputFile,'RMRTree', self.cut)
     
     #hvars = rt.RooArgSet()
     #for p in RootTools.RootIterator.RootIterator(vars):
     #    p.setBins(100)
     #    hvars.add(p)
     
     #create a binned dataset in the parameter   
     #hdata = rt.RooDataHis(t'%sHist' % modelName,'%sHist' % modelName,hvars, signal)
     hdata = RootTools.getDataSet(inputFile,'RMRHistTree_%s' %self.name)
     sigNorm =  RootTools.getHistNorm(inputFile,'wHisto_%s' %self.name)
     self.importToWS(hdata)
     hpdf = rt.RooHistPdf('%sPdf' % modelName,'%sPdf' % modelName,vars,hdata)
     self.importToWS(hpdf)
     return (hpdf.GetName(),sigNorm)
示例#6
0
    def defineMRData(self, inputFile):
        # get the full data
        mRmin = self.workspace.var("MR").getMin()
        mRmax = self.workspace.var("MR").getMax()
        dataBIS = RootTools.getDataSet(inputFile,'RMRTree').reduce("MR>=%f && MR<%f" % (mRmin, mRmax))
        # create the binned dataset
        rsqmin = self.workspace.var("Rsq").getMin()
        data1 = dataBIS.reduce("Rsq>=%f && Rsq<%f" % (rsqmin, rsqmin+0.02))
        data2 = dataBIS.reduce("Rsq>=%f && Rsq<%f" % (rsqmin+0.02, rsqmin+0.04))
        data3 = dataBIS.reduce("Rsq>=%f && Rsq<%f" % (rsqmin+0.04, rsqmin+0.06))
        data4 = dataBIS.reduce("Rsq>=%f && Rsq<%f" % (rsqmin+0.06, rsqmin+0.08))
        data5 = dataBIS.reduce("Rsq>=%f && Rsq<%f" % (rsqmin+0.08, rsqmin+0.10))
        data6 = dataBIS.reduce("Rsq>=%f" % (rsqmin+0.10))
        # create the index Category
        c = rt.RooCategory("c","c") ;
        c.defineType("Bin1") ;
        c.defineType("Bin2") ;
        c.defineType("Bin3") ;
        c.defineType("Bin4") ;
        c.defineType("Bin5") ;
        c.defineType("Bin6") ;
        self.importToWS(c)

        #initialize the inclusive yields to right values and fix them
        self.workspace.var("N6").setVal(data6.numEntries())
        self.workspace.var("N5").setVal(data5.numEntries()+self.workspace.var("N6").getVal())
        self.workspace.var("N4").setVal(data4.numEntries()+self.workspace.var("N5").getVal())
        self.workspace.var("N3").setVal(data3.numEntries()+self.workspace.var("N4").getVal())
        self.workspace.var("N2").setVal(data2.numEntries()+self.workspace.var("N3").getVal())
        self.workspace.var("N1").setVal(data1.numEntries()+self.workspace.var("N2").getVal())
        self.workspace.var("N1").setConstant(rt.kTRUE)
        self.workspace.var("N2").setConstant(rt.kTRUE)
        self.workspace.var("N3").setConstant(rt.kTRUE)
        self.workspace.var("N4").setConstant(rt.kTRUE)
        self.workspace.var("N5").setConstant(rt.kTRUE)
        self.workspace.var("N6").setConstant(rt.kTRUE)
        # Create a dataset that imports contents of all the above datasets mapped by index category 
        data = rt.RooDataSet("RMRTree2","RMRTree2", rt.RooArgSet(self.workspace.var("MR")),rt.RooFit.Index(c),
                             rt.RooFit.Import("Bin1",data1),
                             rt.RooFit.Import("Bin2",data2),
                             rt.RooFit.Import("Bin3",data3),
                             rt.RooFit.Import("Bin4",data4),
                             rt.RooFit.Import("Bin5",data5),
                             rt.RooFit.Import("Bin6",data6))
        #import the dataset to the workspace
        self.importToWS(data)
        print 'Reduced dataset'
示例#7
0
    def plotRsqMR(self, inputFile):
        #before I find a better way
        data = RootTools.getDataSet(inputFile,'RMRTree')
        toyData = self.workspace.pdf("fitmodel").generate(rt.RooArgSet(self.workspace.argSet("MR,Rsq")), 10*data.numEntries())

        # define 2D histograms
        histoData = rt.TH2D("histoData", "histoData",
                            100, self.workspace.var("MR").getMin(), 3500.,
                            100, self.workspace.var("Rsq").getMin(), 1.)
        histoToy = rt.TH2D("histoToy", "histoToy",
                            100, self.workspace.var("MR").getMin(), 3500.,
                            100, self.workspace.var("Rsq").getMin(), 1.)
        # project the data on the histograms
        data.tree().Project("histoData","Rsq:MR")
        toyData.tree().Project("histoToy","Rsq:MR")
        histoToy.Scale(histoData.Integral()/histoToy.Integral())
        histoData.Add(histoToy, -1)
        return histoData
    def plot2D(self, inputFile, xvarname, yvarname):
        #before I find a better way
        data = RootTools.getDataSet(inputFile,'RMRTree')
        toyData = self.workspace.pdf("fitmodel").generate(rt.RooArgSet(self.workspace.argSet(xvarname+","+yvarname)), 20*data.numEntries())
        toyData = toyData.reduce(self.getVarRangeCut())

        # define 2D histograms
        histoData = rt.TH2D("histoData", "histoData",
                            100, self.workspace.var(xvarname).getMin(), self.workspace.var(xvarname).getMax(), 
                            100, self.workspace.var(yvarname).getMin(), self.workspace.var(yvarname).getMax())
        histoToy = rt.TH2D("histoToy", "histoToy",
                            100, self.workspace.var(xvarname).getMin(), self.workspace.var(xvarname).getMax(), 
                            100, self.workspace.var(yvarname).getMin(), self.workspace.var(yvarname).getMax())
        # project the data on the histograms
        data.tree().Project("histoData",yvarname+":"+xvarname)
        toyData.tree().Project("histoToy",yvarname+":"+xvarname)
        histoToy.Scale(histoData.Integral()/histoToy.Integral())
        histoData.Add(histoToy, -1)
        return histoData
示例#9
0
    def plotRsq(self, inputFile):
        # project the data on Rsq
        frameRsq = self.workspace.var("Rsq").frame(self.workspace.var("Rsq").getMin(), 1.5, 200)
        frameRsq.SetName("Rsqplot")
        frameRsq.SetTitle("Rsqplot")
        #before I find a better way
        data = RootTools.getDataSet(inputFile,'RMRTree')
        data.plotOn(frameRsq)

        N1 = self.workspace.var("Ntot").getVal()*(1-self.workspace.var("f2").getVal())
        N2 = self.workspace.var("Ntot").getVal()*self.workspace.var("f2").getVal()
        
        # project the full PDF
        self.workspace.pdf("fitmodel").plotOn(frameRsq, rt.RooFit.LineColor(rt.kBlue)) 
        # project the first component
        self.workspace.pdf("PDF1st").plotOn(frameRsq, rt.RooFit.LineColor(rt.kBlue), rt.RooFit.LineStyle(8), rt.RooFit.Normalization(N1/(N1+N2)))
        # project the second component
        self.workspace.pdf("PDF2nd").plotOn(frameRsq, rt.RooFit.LineColor(rt.kBlue), rt.RooFit.LineStyle(9), rt.RooFit.Normalization(N2/(N1+N2)))

        return frameRsq
示例#10
0
    def plotMR(self, inputFile):
        # project the data on R
        frameMR = self.workspace.var("MR").frame(self.workspace.var("MR").getMin(), 3000., 200)
        frameMR.SetName("MRplot")
        frameMR.SetTitle("MRplot")
        #        data = rt.RooDataSet(self.workspace.genobj("RMRTree"))
        #before I find a better way
        data = RootTools.getDataSet(inputFile,'RMRTree')
        data.plotOn(frameMR)
        # project the full PDF on the data
        self.workspace.pdf("fitmodel").plotOn(frameMR, rt.RooFit.LineColor(rt.kBlue))

        N1 = self.workspace.var("Ntot").getVal()*(1-self.workspace.var("f2").getVal())
        N2 = self.workspace.var("Ntot").getVal()*self.workspace.var("f2").getVal()

        # project the first component
        self.workspace.pdf("PDF1st").plotOn(frameMR, rt.RooFit.LineColor(rt.kBlue), rt.RooFit.LineStyle(8), rt.RooFit.Normalization(N1/(N1+N2)))
        # project the second component
        self.workspace.pdf("PDF2nd").plotOn(frameMR, rt.RooFit.LineColor(rt.kBlue), rt.RooFit.LineStyle(9), rt.RooFit.Normalization(N2/(N1+N2)))
        return frameMR
示例#11
0
    def define(self, inputFile):
        
        #create the dataset
        data = RootTools.getDataSet(inputFile,'RMRTree')
        #import the dataset to the workspace
        self.importToWS(data)
        print 'Reduced dataset'
        #data.Print("V")

        # define the two components
        self.workspace.factory("RooDalglish::PDF1st(MR,Rsq,Xhat1,Yhat1,b1,MR01,R01,C1,S1,Sx1,Sy1,Xoff1,Yoff1)")
        self.workspace.factory("RooDalglish::PDF2nd(MR,Rsq,Xhat2,Yhat2,b2,MR02,R02,C2,S2,Sx2,Sy2,Xoff2,Yoff2)")
        #define the two yields
        self.workspace.factory("expr::N_1st('@0*(1-@1)',Ntot,f2)")
        self.workspace.factory("expr::N_2nd('@0*@1',Ntot,f2)")
        #associate the yields to the pdfs through extended PDFs
        self.workspace.factory("RooExtendPdf::ePDF1st(PDF1st, N_1st)")
        self.workspace.factory("RooExtendPdf::ePDF2nd(PDF2nd, N_2nd)")
        # build the total PDF
        model = rt.RooAddPdf("fitmodel", "fitmodel", rt.RooArgList(self.workspace.pdf("ePDF1st"),self.workspace.pdf("ePDF2nd")))        
        # import the model in the workspace.
        self.importToWS(model)
        #print the workspace
        self.workspace.Print()
 def define(self, inputFile, cuts):
     
     if cuts.get('useC++',False):
         return self.defineCpp(inputFile, cuts)
     
     rcuts = cuts.get('rcuts',[])
     rcuts.sort()
     
     if len(rcuts) < 1:
         self.workspace.factory("RooTwoSideGaussianWithAnExponentialTail::fitmodel(MR,X0[0,400],SigmaL[0,500],SigmaR[0,500],S[0,0.5])")
     else:
         
         #get the dataset to play with
         reduce = cuts.get('reduce',None)
         data = RootTools.getDataSet(inputFile,'RMRTree', reduce)
         
         #our parameters of interest
         self.workspace.factory('A[0,0.1]')
         self.workspace.factory('B[0,0.1]')
         
         nrcuts = len(rcuts)
         
         #the full blooded fit from Yi
         for i in xrange(nrcuts):
             
             #estimate the bin yield to help the fit
             cut = 'R > %f' % rcuts[i]
             if(i != len(rcuts) -1):
                 cut = '%s && R <= %f' % (cut, rcuts[i+1])
             guessYield = data.reduce(rt.RooFit.Cut(cut)).sumEntries()
             
             #create the yield variables
             print 'Yield guessed for bin %d is %d' % (i, guessYield)
             self.workspace.factory('SingleBinYield_%d[%d,0,%d]' % (i,guessYield,guessYield*10) )
             
             #now add the PDF
             self.workspace.factory("""RooTwoSideGaussianWithAnExponentialTail::Model_%(index)d(
                 MR,
                 X0_%(index)d[0,400],
                 SigmaL_%(index)d[0,500],
                 SigmaR_%(index)d[50,0,500],
                 expr::S_%(index)d('@0 + %(r2)f * @1',A,B)
                 )""" % {'index':i,'r2':rcuts[i]*rcuts[i]}
             ) 
             
         # the yields need to be in a seperate loop so everything is defined
         for i in reversed(xrange(nrcuts)):
             if(i == nrcuts -1):
                 #last bin
                 self.workspace.factory("expr::Yield_%d('@0',SingleBinYield_%d)" % (i,i))
             else:
                 self.workspace.factory("expr::Yield_%d('@0 + @1',Yield_%d,SingleBinYield_%d)" % (i,i+1,i))
             self.workspace.factory("expr::NYield_%d('-@0',Yield_%d)" % (i,i))
         
         for i in xrange(nrcuts):
             if(i == nrcuts -1):
                 self.workspace.factory("RooAtLeast::Constraint_%d(R,RooConstVar(%f))" % (i,rcuts[i]))
                 self.workspace.factory("PROD::TopLevelModel_%d)")
                 
             else:
                 self.workspace.factory("expr::NormalizedYield_%d('@0 / (@0 - @1)',Yield_%d,Yield_%d)" % (i,i,i+1) ) 
                 self.workspace.factory("expr::NormalizedNegativeYield_%d('-@0 / (@0 - @1)',Yield_%d,Yield_%d)" % (i,i,i+1) ) 
                 
                 self.workspace.factory("SUM::ModelBeforeConstraint_%d(NormalizedYield_%d*Model_%d,NormalizedNegativeYield_%d*Model_%d)" % (i,i,i,i,i+1))
             
                 self.workspace.factory("RooSameAs::Constraint_%d(R,RooConstVar(%f),RooConstVar(%f))" % (i,0.5*(rcuts[i+1]+rcuts[i]),0.5*(rcuts[i+1]-rcuts[i])) )
    def define(self, inputFile):
        
        #create the dataset
        data = RootTools.getDataSet(inputFile,'RMRTree', self.cut)


        #create the signal PDF using RooHistPDF
        signal = RootTools.getDataSet('LM6_MR300.0_R0.3_'+self.name+'.root','RMRTree',self.cut)
        signalRDH = rt.RooDataHist("signal", "signal",rt.RooArgSet(self.workspace.var("MR"), self.workspace.var("Rsq")),signal)
        self.workspace.factory("expr::Nsig('%i*@0*@1/@2', Lumi, SigmaModel, Ngen)" % signal.numEntries())
        self.importToWS(signalRDH)
        signalModel = rt.RooHistPdf("signalModel", "signalModel", rt.RooArgSet(self.workspace.var("MR"),self.workspace.var("Rsq")), signalRDH)
        self.importToWS(signalModel)
        self.workspace.factory("RooExtendPdf::eBinPDF_Signal(signalModel, Nsig)")


        #import the dataset to the workspace
        self.importToWS(data)
        #self.workspace.Print('V')

        # add the different components:
        # - W+jets
        # - Zll+jets
        # - Znn+jets
        # - ttbar+jets
        self.addTailPdf("Wln")    
        self.addTailPdf("Zll")
        self.addTailPdf("Znn")
        self.addTailPdf("TTj")
        self.addTailPdf("QCD")

        # build the total PDF
        myPDFlist = rt.RooArgList(self.workspace.pdf("ePDF1st_Wln"),self.workspace.pdf("ePDF2nd_Wln"),
                                  self.workspace.pdf("ePDF1st_Zll"),self.workspace.pdf("ePDF2nd_Zll"),
                                                                    self.workspace.pdf("ePDF1st_Znn"),self.workspace.pdf("ePDF2nd_Znn"),
                                                                    self.workspace.pdf("ePDF1st_TTj"),self.workspace.pdf("ePDF2nd_TTj"))
        myPDFlist.add(self.workspace.pdf("ePDF1st_QCD"))
        myPDFlist.add(self.workspace.pdf("ePDF2nd_QCD"))    
        myPDFlist.add(self.workspace.pdf("eBinPDF_Signal"))    
        model = rt.RooAddPdf(self.fitmodel, self.fitmodel, myPDFlist)        
        
        # import the model in the workspace.
        self.importToWS(model)
        #print the workspace
        self.workspace.Print()

        ##### THIS IS A SIMPLIFIED FIT
        # fix all pdf parameters to the initial value
        self.fixPars("Zll")
        self.fixPars("Znn")
        self.fixPars("Wln")
        self.fixPars("TTj")
        self.fixPars("QCD")
        self.workspace.var("SigmaModel").setConstant(rt.kFALSE)
        self.workspace.var("Epsilon_TTj").setConstant(rt.kFALSE)
        self.workspace.var("Epsilon_Wln").setConstant(rt.kFALSE)
        self.workspace.var("Epsilon_QCD").setConstant(rt.kFALSE)
        
        
        
        
        #add penalty terms and float
        def float1stComponentWithPenalty(flavour):
            self.fixParsPenalty("MR01st_%s" % flavour)
            self.fixParsPenalty("R01st_%s" % flavour)
            self.fixParsPenalty("b1st_%s" % flavour)
        def float2ndComponentWithPenalty(flavour):
            self.fixParsPenalty("MR02nd_%s" % flavour)
            self.fixParsPenalty("R02nd_%s" % flavour)
            self.fixParsPenalty("b2nd_%s" % flavour)
        def floatFractionWithPenalty(flavour):
            self.fixParsPenalty("Epsilon_%s" % flavour, floatIfNoPenalty = True)
            self.fixPars("Epsilon_%s_s" % flavour)
            self.fixParsPenalty("f2_%s" % flavour)
            self.fixPars("f2_%s_s" % flavour)

        # switch off not-needed components (box by box)
        fixed = []
        for z in self.zeros:
            if self.name in self.zeros[z]:
                self.switchOff(z)
#! /usr/bin/env python
"""To merge datasets"""

import ROOT as rt
import RootTools
import sys

if __name__ == '__main__':
    ARGS = sys.argv[1:]
    if not ARGS:
        print "Need input files!"
        sys.exit(1)

    TMPDATA = ARGS[0]
    DATASET = RootTools.getDataSet(TMPDATA, 'RMRTree')
    ENTRIES = DATASET.numEntries()

    for i in range(1, len(ARGS)):
        INPUT_FILE = ARGS[i]
        print INPUT_FILE
        WDATA = RootTools.getDataSet(INPUT_FILE, 'RMRTree')

        print WDATA.numEntries()
        ENTRIES += WDATA.numEntries()
        DATASET.append(WDATA)

    OUTFILE = rt.TFile.Open("Parked_BJetHS.root", 'RECREATE')
    DATASET.Write()
    OUTFILE.Close()
    print ENTRIES
    def plot1D(self, inputFile, varname, nbin=200, xmin=-99, xmax=-99):
        # set the integral precision
        rt.RooAbsReal.defaultIntegratorConfig().setEpsAbs(1e-10) ;
        rt.RooAbsReal.defaultIntegratorConfig().setEpsRel(1e-10) ;
        # get the max and min (if different than default)
        if xmax==xmin:
            xmin = self.workspace.var(varname).getMin()
            xmax = self.workspace.var(varname).getMax()
        # project the data on the variable
        frameMR = self.workspace.var(varname).frame(xmin, xmax, nbin)
        frameMR.SetName(varname+"plot")
        frameMR.SetTitle(varname+"plot")
        data = RootTools.getDataSet(inputFile,'RMRTree')
        data_cut = data.reduce(self.cut)
        data.plotOn(frameMR, rt.RooFit.LineColor(rt.kRed),rt.RooFit.MarkerColor(rt.kRed))
        data_cut.plotOn(frameMR)
        # project the full PDF on the data
        self.workspace.pdf(self.fitmodel).plotOn(frameMR, rt.RooFit.LineColor(rt.kBlue))

        # plot each individual component: Wln
        N1_Wln = self.workspace.function("Ntot_Wln").getVal()*(1-self.workspace.var("f2_Wln").getVal())
        N2_Wln = self.workspace.function("Ntot_Wln").getVal()*self.workspace.var("f2_Wln").getVal()
        # plot each individual component: Zll
        N1_Zll = self.workspace.function("Ntot_Zll").getVal()*(1-self.workspace.var("f2_Zll").getVal())
        N2_Zll = self.workspace.function("Ntot_Zll").getVal()*self.workspace.var("f2_Zll").getVal()
        # plot each individual component: Znn
        N1_Znn = self.workspace.function("Ntot_Znn").getVal()*(1-self.workspace.var("f2_Znn").getVal())
        N2_Znn = self.workspace.function("Ntot_Znn").getVal()*self.workspace.var("f2_Znn").getVal()
        # plot each individual component: TTj
        N1_TTj = self.workspace.function("Ntot_TTj").getVal()*(1-self.workspace.var("f2_TTj").getVal())
        N2_TTj = self.workspace.function("Ntot_TTj").getVal()*self.workspace.var("f2_TTj").getVal()
        # plot each individual component: QCD
        N1_QCD = self.workspace.function("Ntot_QCD").getVal()*(1-self.workspace.var("f2_QCD").getVal())
        N2_QCD = self.workspace.function("Ntot_QCD").getVal()*self.workspace.var("f2_QCD").getVal()        
        # plot each individual component: Signal
        Nsig = self.workspace.function("Nsig").getVal()

        Ntot = N1_Wln+N2_Wln+N1_Zll+N2_Zll+N1_Znn+N2_Znn+N1_TTj+N2_TTj+N1_QCD+N2_QCD+Nsig

        if N1_Wln+N2_Wln >0:
            # project the first component: Wln
            self.workspace.pdf("PDF1st_Wln").plotOn(frameMR, rt.RooFit.LineColor(rt.kRed), rt.RooFit.LineStyle(8), rt.RooFit.Normalization(N1_Wln/Ntot))
            # project the second component: Wln
            self.workspace.pdf("PDF2nd_Wln").plotOn(frameMR, rt.RooFit.LineColor(rt.kRed), rt.RooFit.LineStyle(9), rt.RooFit.Normalization(N2_Wln/Ntot))
        if N1_Zll+N2_Zll >0:
            # project the first component: Zll
            self.workspace.pdf("PDF1st_Zll").plotOn(frameMR, rt.RooFit.LineColor(rt.kMagenta), rt.RooFit.LineStyle(8), rt.RooFit.Normalization(N1_Zll/Ntot))
            # project the second component: Zll
            self.workspace.pdf("PDF2nd_Zll").plotOn(frameMR, rt.RooFit.LineColor(rt.kMagenta), rt.RooFit.LineStyle(9), rt.RooFit.Normalization(N2_Zll/Ntot))
        if N1_Znn+N2_Znn >0:
            # project the first component: Znn
            self.workspace.pdf("PDF1st_Znn").plotOn(frameMR, rt.RooFit.LineColor(rt.kGreen), rt.RooFit.LineStyle(8), rt.RooFit.Normalization(N1_Znn/Ntot))
            # project the second component: Znn
            self.workspace.pdf("PDF2nd_Znn").plotOn(frameMR, rt.RooFit.LineColor(rt.kGreen), rt.RooFit.LineStyle(9), rt.RooFit.Normalization(N2_Znn/Ntot))
        if N1_TTj+N2_TTj >0:
            # project the first component: TTj
            self.workspace.pdf("PDF1st_TTj").plotOn(frameMR, rt.RooFit.LineColor(rt.kOrange), rt.RooFit.LineStyle(8), rt.RooFit.Normalization(N1_TTj/Ntot))
            # project the second component: TTj
            self.workspace.pdf("PDF2nd_TTj").plotOn(frameMR, rt.RooFit.LineColor(rt.kOrange), rt.RooFit.LineStyle(9), rt.RooFit.Normalization(N2_TTj/Ntot))
        if N1_QCD+N2_QCD >0:
            # project the first component: QCD
            self.workspace.pdf("PDF1st_QCD").plotOn(frameMR, rt.RooFit.LineColor(rt.kViolet), rt.RooFit.LineStyle(8), rt.RooFit.Normalization(N1_QCD/Ntot))
            # project the second component: QCD
            self.workspace.pdf("PDF2nd_QCD").plotOn(frameMR, rt.RooFit.LineColor(rt.kViolet), rt.RooFit.LineStyle(9), rt.RooFit.Normalization(N2_QCD/Ntot))            
        if Nsig>0:
            # project the first component: Signal
            self.workspace.pdf("signalModel").plotOn(frameMR, rt.RooFit.LineColor(rt.kBlack), rt.RooFit.LineStyle(8), rt.RooFit.Normalization(Nsig/Ntot)) 
            
        #leg = rt.TLegend("leg", "leg", 0.6, 0.6, 0.9, 0.9)
        #leg.AddEntry("PDF1st_Wln", "W+jets 1st")
        #leg.AddEntry("PDF2nd_Wln", "W+jets 2nd")
        #leg.AddEntry("PDF1st_Zll", "Z(ll)+jets 1st")
        #leg.AddEntry("PDF2nd_Zll", "Z(ll)+jets 2nd")
        #leg.AddEntry("PDF1st_Znn", "Z(#nu#nu)+jets 1st")
        #leg.AddEntry("PDF2nd_Znn", "Z(#nu#nu)+jets 2nd")
        #leg.AddEntry("PDF1st_TTj", "t#bar{t}+jets 1st")
        #leg.AddEntry("PDF2nd_TTj", "t#bar{t}+jets 2nd")
        
        return frameMR
def getUnweightedToy(box, files, outdir):
    """Get the cocktail dataset from the file"""
    
    components = [os.path.basename(f).split('_')[0] for f in files]
    if len(components) != len(set(components)):
        raise Exception('Some components not unique for box %s: %s' % (box,components))

    f = files[0]
    wdata = RootTools.getDataSet(f,'RMRTree')
    if not wdata:
        raise Exception('No dataset found')
    row = wdata.get()
    row.Print("V")

    MR = row['MR']
    Rsq = row['Rsq']
    nBtag = row['nBtag']
    #CHARGE = row['CHARGE']
    
    

    #varSet = rt.RooArgSet(MR,Rsq,nBtag,CHARGE)
    varSet = rt.RooArgSet(MR,Rsq,nBtag)
    varList = rt.RooArgList(MR,Rsq,nBtag)
    varList2D = rt.RooArgList(MR,Rsq)
    uwdata = rt.RooDataSet('RMRTree','Unweighted Cocktail',varSet)
    

    
    mRmin = row['MR'].getMin()
    mRmax = row['MR'].getMax()
    rsqMin = row['Rsq'].getMin()
    rsqMax = row['Rsq'].getMax()
    nbtagMin = row['nBtag'].getMin()
    nbtagMax = row['nBtag'].getMax()
    
    rMin = rt.TMath.Sqrt(rsqMin)
    MRbins, Rsqbins, nBtagbins = makeBluePlot.Binning(box,False)

    #to double the number of bins in MR and Rsq
    #MRbinsAve = [int(0.5*(MRbins[k]+MRbins[k+1])) for k in range(len(MRbins)-1)]
    #for k in range(len(MRbinsAve)):
    #    MRbins.insert(2*k+1,MRbinsAve[k])
    #RsqbinsAve = [0.5*(Rsqbins[k]+Rsqbins[k+1]) for k in range(len(Rsqbins)-1)]
    #for k in range(len(RsqbinsAve)):
    #    Rsqbins.insert(2*k+1,RsqbinsAve[k])
    #print MRbins
    #print Rsqbins
    
    x = array('d',MRbins)
    y = array('d',Rsqbins)
    z = array('d',nBtagbins)
    
    #myTH3 = rt.TH3D("h%s"%box, "h%s"%box, len(MRbins)-1, x, len(Rsqbins)-1, y, len(nBtagbins)-1, z)
    #myTH2 = rt.TH2D("h", "h", len(MRbins)-1, x, len(Rsqbins)-1, y)
    myTH3 = rt.TH3D("h%s"%box, "h%s"%box, 100, mRmin, mRmax, 70, rsqMin, rsqMax, int(nbtagMax-nbtagMin), nbtagMin, nbtagMax)
    myTH2 = rt.TH2D("h", "h", 100, mRmin, mRmax, 70, rsqMin, rsqMax)
    myTH2.Sumw2()
    #myTH2 = rt.TH2D("h", "h", len(MRbins)-1, x, len(Rsqbins)-1, y)

    # fills automatically with weight
    wdata.fillHistogram(myTH3, varList,"MR>0")
    wdata.fillHistogram(myTH2, varList2D,"MR>0")
    c = rt.TCanvas()
    rt.gStyle.SetOptStat(1001000011)
    myTH2.SetTitle("Weighted %s"%box)
    sumW2 = 0
    for i in range(0,wdata.numEntries()):
        wdata.get(i)
        sumW2+=(wdata.weight())*(wdata.weight())
    print "sum (weights)^2 = %.1f" %sumW2
    print "(sum weights)^2 = %.1f" %((wdata.sumEntries())*(wdata.sumEntries()))
    effEntries = (((wdata.sumEntries())*(wdata.sumEntries()))/sumW2)
    print "effective entries = %.1f"%effEntries
    myTH2.Draw("colz")
    c.Print("Cocktail_%s_DatasetWeighted.pdf"%box)
    
    print wdata.weight()
    Nev = myTH3.Integral()
    Nent = myTH3.GetEntries()
    print "weighted events %.1f"% Nev
    print "entries  %d"% Nent
    Npois = rt.RooRandom.randomGenerator().Poisson(Nev)
    for i in range(0,Npois):
        myMR = rt.Double()
        myRsq = rt.Double()
        mynBtag = rt.Double()
        myTH3.GetRandom3(myMR,myRsq,mynBtag)
        mynBtag = int(mynBtag)
        varSet.setRealValue('MR',myMR)
        varSet.setRealValue('Rsq',myRsq)
        varSet.setRealValue('nBtag',mynBtag)
        #varSet.setRealValue('CHARGE',1.0)
        uwdata.add(varSet)
    

    myTH2Toy = rt.TH2D("h", "h", 100, mRmin, mRmax,70, rsqMin, rsqMax)
    #myTH2Toy = rt.TH2D("h", "h",len(MRbins)-1, x, len(Rsqbins)-1, y)
    uwdata.fillHistogram(myTH2Toy, varList2D,"MR>0")
    myTH2Toy.SetTitle("Unweighted %s"%box)
    myTH2Toy.Draw("colz")
    c.Print("Cocktail_%s_ToyUnweighted.pdf"%box)
    output = rt.TFile.Open(outdir+"/SMCocktail_GENTOY_MR"+str(mRmin)+"_R"+str(rMin)+"_"+box+'.root','RECREATE')
    print 'Writing',output.GetName()
    uwdata.Write()
    myTH3.Write()
    output.Close()
示例#17
0
 def addDataSet(self, inputFile):
     #create the dataset
     data = RootTools.getDataSet(inputFile,'RMRTree', self.cut)
     #import the dataset to the workspace
     self.importToWS(data)
示例#18
0
 def predictBackground(self, fr, inputFile, nRepeats = 100):
     data = RootTools.getDataSet(inputFile,'RMRTree')
     return self.predictBackgroundData(fr, data, nRepeats)
示例#19
0
    def plot1DHistoAllComponents(self, inputFile, xvarname, nbins = 25, ranges=None, data = None):
        
        rangeNone = False
        if ranges is None:
            rangeNone = True
            ranges = ['']
            
        #before I find a better way
        rangeCut = self.getVarRangeCutNamed(ranges=ranges)
        if data is None:
            data = RootTools.getDataSet(inputFile,'RMRTree', self.cut)
            data = data.reduce(rangeCut)
        toyData = self.workspace.pdf(self.fitmodel).generate(self.workspace.set('variables'), 50*data.numEntries())
        toyData = toyData.reduce(self.getVarRangeCutNamed(ranges=ranges))

        xmin = min([self.workspace.var(xvarname).getMin(r) for r in ranges])
        xmax = max([self.workspace.var(xvarname).getMax(r) for r in ranges])

        # define 1D histograms
        histoData = self.setPoissonErrors(rt.TH1D("histoData", "histoData",nbins, xmin, xmax))
        histoToy = self.setPoissonErrors(rt.TH1D("histoToy", "histoToy",nbins, xmin, xmax))
        histoToyTTj = self.setPoissonErrors(rt.TH1D("histoToyTTj", "histoToyTTj",nbins, xmin, xmax))
        histoToyWln = self.setPoissonErrors(rt.TH1D("histoToyWln", "histoToyWln",nbins, xmin, xmax))

        def setName(h, name):
            h.SetName('%s_%s_%s_ALLCOMPONENTS' % (h.GetName(),name,'_'.join(ranges)) )
            h.GetXaxis().SetTitle(name)
        
        def SetErrors(histo, nbins):
            for i in range(1, nbins+1):
                histo.SetBinError(i,rt.TMath.Sqrt(histo.GetBinContent(i)))

        # project the data on the histograms
        #data.tree().Project("histoData",xvarname)
        data.fillHistogram(histoData,rt.RooArgList(self.workspace.var(xvarname)))
        toyData.fillHistogram(histoToy,rt.RooArgList(self.workspace.var(xvarname)))
 
        #Cache the numbers
        Ntt = self.workspace.var("Ntot_TTj").getVal()
        NWln = self.workspace.var("Ntot_Wln").getVal()

        #Generate the TTj component
        self.workspace.var("Ntot_Wln").getVal()
        toyDataTTj = self.workspace.pdf(self.fitmodel).generate(self.workspace.set('variables'), int(50*(data.numEntries()-NWln)))
        toyDataTTj.fillHistogram(histoToyTTj,rt.RooArgList(self.workspace.var(xvarname)))
        histoToyTTj.SetLineColor(rt.kRed)
        histoToyTTj.SetLineWidth(2)
        self.workspace.var("Ntot_Wln").setVal(NWln)

        #Generate the Wln component
        self.workspace.var("Ntot_TTj").getVal()
        toyDataWln = self.workspace.pdf(self.fitmodel).generate(self.workspace.set('variables'), int(50*(data.numEntries()-Ntt)))
        toyDataWln.fillHistogram(histoToyWln,rt.RooArgList(self.workspace.var(xvarname)))
        histoToyWln.SetLineColor(rt.kYellow+2)
        histoToyWln.SetLineWidth(2)
        self.workspace.var("Ntot_TTj").setVal(Ntt)

        scaleFactor = 1.0
        if abs(histoToy.Integral()-0.0) > 1e-8:
            scaleFactor = histoData.Integral()/histoToy.Integral()

        histoToy.Scale(scaleFactor)
        histoToyTTj.Scale(scaleFactor)
        histoToyWln.Scale(scaleFactor)
        SetErrors(histoToy, nbins)
        SetErrors(histoToyTTj, nbins)
        SetErrors(histoToyWln, nbins)
        setName(histoData,xvarname)
        setName(histoToy,xvarname)
        setName(histoToyTTj,xvarname)
        setName(histoToyWln,xvarname)
        histoData.SetMarkerStyle(20)
        histoToy.SetLineColor(rt.kBlue)
        histoToy.SetLineWidth(2)

        st = rt.TStyle()
        st.SetCanvasColor(2)
        st.SetFrameBorderMode(0);
        st.SetCanvasBorderMode(0);
        st.SetPadBorderMode(0);
        st.SetPadColor(2);
        st.SetFillStyle(0);
        st.SetLegendBorderSize(0);

        c = rt.TCanvas()
        c.SetName('DataMC_%s_%s_ALLCOMPONENTS' % (xvarname,'_'.join(ranges)) )
        histoData.Draw("pe")

        histoToyTTj.DrawCopy('histsame')
        histoToyTTj.SetFillColor(rt.kRed)
        histoToyTTj.SetFillStyle(3018)
        histoToyTTj.Draw('e2same')        

        histoToyWln.DrawCopy('histsame')
        histoToyWln.SetFillColor(rt.kYellow+2)
        histoToyWln.SetFillStyle(3018)
        histoToyWln.Draw('e2same')        

        histoToy.DrawCopy('histsame')
        histoToy.SetFillColor(rt.kBlue)
        histoToy.SetFillStyle(3018)
        histoToy.Draw('e2same')
        #histoData.Draw("pesame")

        #leg = rt.TLegend(0.7,0.7,0.9,0.9)
        #leg.SetFillColor(0)
        #leg.AddEntry(histoToy.GetName(),"Total","l")
        #leg.AddEntry(histoToyTTj.GetName(),"t#bar{t}","l")
        #leg.AddEntry(histoToyWln.GetName(),"V+jets","l")
        #leg.Draw()

        histToReturn = [histoToy, histoToyTTj, histoToyWln, histoData, c]
        return histToReturn
示例#20
0
    def plot1DHistoAllComponents(self, inputFile, xvarname, nbins = 25, ranges=None, data = None):
        
        rangeNone = False
        if ranges is None:
            rangeNone = True
            ranges = ['']
        
        factor = 50    
        rangeCut = self.getVarRangeCutNamed(ranges=ranges)
        if data is None:
            data = RootTools.getDataSet(inputFile,'RMRTree', self.cut)
            data = data.reduce(rangeCut)
        
        # variable binning for plots
        bins = self.getBinning(self.name, xvarname)
        nbins =len(bins)-1
        xedge = array("d",bins)
        
        # define 1D histograms
        histoData = self.setPoissonErrors(rt.TH1D("histoData", "histoData",nbins, xedge))
        histoToy = self.setPoissonErrors(rt.TH1D("histoToy", "histoToy",nbins, xedge))
        histoToyUEC = self.setPoissonErrors(rt.TH1D("histoToyUEC", "histoToyUEC",nbins, xedge))
        histoToyTTj = self.setPoissonErrors(rt.TH1D("histoToyTTj", "histoToyTTj",nbins, xedge))
        histoToyQCD = self.setPoissonErrors(rt.TH1D("histoToyQCD", "histoToyQCD",nbins, xedge))

        def setName(h, name):
            h.SetName('%s_%s_%s_ALLCOMPONENTS' % (h.GetName(),name,'_'.join(ranges)) )
            h.GetXaxis().SetTitle(name)
        
        def SetErrors(histo, nbins):
            for i in range(1, nbins+1):
                histo.SetBinError(i,rt.TMath.Sqrt(histo.GetBinContent(i)))
                #print i, histo.GetName(), histo.GetBinContent(i), histo.GetBinError(i)

        # project the data on the histograms
        #data.tree().Project("histoData",xvarname)
        data.fillHistogram(histoData,rt.RooArgList(self.workspace.var(xvarname)))

        #Cache the numbers
        Nuec = self.workspace.var("Ntot_UEC").getVal()
        Nttj = self.workspace.var("Ntot_TTj").getVal()
        Nqcd = self.workspace.var("Ntot_QCD").getVal()
        Ndata = data.sumEntries()

        print 'Nuec, ...:', Nuec, Nttj, Nqcd, Ndata

        #Generate the TTj first component component
        if self.workspace.var("Ntot_TTj") != None and Nttj > 0:
            self.workspace.var("Ntot_UEC").setVal(0.)
            self.workspace.var("Ntot_QCD").setVal(0.)
            toyDataTTj = self.workspace.pdf(self.fitmodel).generate(self.workspace.set('variables'), int(factor*(Nttj)))
            print "toydata:"
            print toyDataTTj.Print("V")
            toyDataTTj.fillHistogram(histoToyTTj,rt.RooArgList(self.workspace.var(xvarname)))
            histoToyTTj.SetLineColor(rt.kMagenta)
            histoToyTTj.SetLineWidth(2)
            self.workspace.var("Ntot_UEC").setVal(Nuec)
            self.workspace.var("Ntot_QCD").setVal(Nqcd)
            
        #Generate the UEC component
        if self.workspace.var("Ntot_UEC") != None and Nuec > 0:
            self.workspace.var("Ntot_TTj").setVal(0.)
            self.workspace.var("Ntot_QCD").setVal(0.)
            #toyDataUEC = self.workspace.pdf(self.fitmodel).generate(self.workspace.set('variables'), int(factor*(data.numEntries()-Nqcd)))
            toyDataUEC = self.workspace.pdf(self.fitmodel).generate(self.workspace.set('variables'), int(factor*(Nuec)))
            toyDataUEC.fillHistogram(histoToyUEC,rt.RooArgList(self.workspace.var(xvarname)))
            histoToyUEC.SetLineColor(rt.kRed)
            histoToyUEC.SetLineWidth(2)
            self.workspace.var("Ntot_TTj").setVal(Nttj)
            self.workspace.var("Ntot_QCD").setVal(Nqcd)
        
        #Generate the QCD component
        if self.workspace.var("Ntot_QCD") != None and Nqcd > 0:
            self.workspace.var("Ntot_UEC").setVal(0.)
            self.workspace.var("Ntot_TTj").setVal(0.)
            #toyDataQCD = self.workspace.pdf(self.fitmodel).generate(self.workspace.set('variables'), int(factor*(data.numEntries()-Nuec)))
            toyDataQCD = self.workspace.pdf(self.fitmodel).generate(self.workspace.set('variables'), int(factor*(Nqcd)))
            toyDataQCD.fillHistogram(histoToyQCD,rt.RooArgList(self.workspace.var(xvarname)))
            histoToyQCD.SetLineColor(rt.kGreen)
            histoToyQCD.SetLineWidth(2)
            self.workspace.var("Ntot_UEC").setVal(Nuec)
            self.workspace.var("Ntot_TTj").setVal(Nttj)
        
        if self.workspace.var("Ntot_UEC") != None and Nuec>1 :histoToy.Add(histoToyUEC, +1)
        if self.workspace.var("Ntot_TTj") != None and Nttj>1 :histoToy.Add(histoToyTTj, +1)
        if self.workspace.var("Ntot_QCD") != None and Nqcd>1: histoToy.Add(histoToyQCD, +1)

        #put some protection in for divide by zero
        scaleFactor = 1.0
        if abs(histoToy.Integral()-0.0) > 1e-8:
            scaleFactor = histoData.Integral()/histoToy.Integral()

        histoToy.Scale(scaleFactor)
        histoToyUEC.Scale(scaleFactor)
        histoToyTTj.Scale(scaleFactor)
        histoToyQCD.Scale(scaleFactor)
        SetErrors(histoToy, nbins)
        SetErrors(histoToyUEC, nbins)
        SetErrors(histoToyTTj, nbins)
        SetErrors(histoToyQCD, nbins)
        setName(histoData,xvarname)
        setName(histoToy,xvarname)
        setName(histoToyUEC,xvarname)
        setName(histoToyTTj,xvarname)
        setName(histoToyQCD,xvarname)
        histoData.SetMarkerStyle(20)
        histoToy.SetLineColor(rt.kBlue)
        histoToy.SetLineWidth(2)

        def fchisqndof(hd, hf):
            chisq = 0
            ndof = 0
            for i in range(1, hd.GetNbinsX()+1):
                ndata = hd.GetBinContent(i)
                nfit = hf.GetBinContent(i)
                if ndata >= 3:
                    x = ((ndata - nfit)*(ndata - nfit)) / nfit
                    chisq = chisq + x
                    ndof = ndof + 1.0
            chisqndof = chisq / ndof
            return chisq, ndof, chisqndof

        chisq, ndof, chisqndof = fchisqndof(histoData, histoToy)
        print 'chisq, ndof, chisqndof: ', xvarname, chisq, ndof, chisqndof

        c = rt.TCanvas()
        c.SetName('DataMC_%s_%s_ALLCOMPONENTS' % (xvarname,'_'.join(ranges)) )
        c.SetLogy(1)
        histoData.Draw("pe")
        
        histoToyUEC.DrawCopy('histsame')
        histoToyUEC.SetLineColor(rt.kMagenta+2)
        histoToyUEC.SetFillColor(rt.kMagenta+2)
        histoToyUEC.SetFillStyle(3018)
        histoToyUEC.Draw('e2same')  

        if self.workspace.var("Ntot_TTj") != None and Nttj > 0:
            histoToyTTj.DrawCopy('histsame')
            histoToyTTj.SetLineColor(rt.kCyan)
            histoToyTTj.SetFillColor(rt.kCyan)
            histoToyTTj.SetFillStyle(3018)
            histoToyTTj.Draw('e2same')  

        if self.workspace.var("Ntot_QCD") != None and Nqcd > 0:
            histoToyQCD.DrawCopy('histsame')
            histoToyQCD.SetLineColor(rt.kGreen)
            histoToyQCD.SetFillColor(rt.kGreen)
            histoToyQCD.SetFillStyle(3018)
            histoToyQCD.Draw('e2same')  

        histoToy.DrawCopy('histsame')
        histoToy.SetFillColor(rt.kBlue)
        histoToy.SetFillStyle(3018)
        histoToy.Draw('e2same')
        #c.SetLogy()
        c.Print("canvas_%s.pdf"%xvarname)

        if self.workspace.var("Ntot_QCD") != None and Nqcd > 0:
            histToReturn = [histoToy, histoToyQCD, histoToyTTj, histoToyUEC, histoData, c]
        else:
            histToReturn = [histoToy, histoToyTTj, histoToyUEC, histoData, c]

        return histToReturn
示例#21
0
    def define(self, inputFile):
        
        #define the ranges
        mR  = self.workspace.var("MR")
        Rsq = self.workspace.var("Rsq")

        # add the different components:
        self.addTailPdf("QCD", False)
        self.addTailPdf("UEC", True)
        self.addTailPdf("TTj", True)

        # build the total PDF
        myPDFlist = rt.RooArgList(self.workspace.pdf("ePDF_UEC"),self.workspace.pdf("ePDF_TTj"),self.workspace.pdf("ePDF_QCD"))
        #probably this needs to be hacked to accommodate a non-extended pdf
        # self.fitmodel is simply the name of our fit model.  We use it to define the name and the title of our fit model.
        print 'self.fitmodel:', self.fitmodel
        model = rt.RooAddPdf(self.fitmodel, self.fitmodel, myPDFlist)
        
        # import the model in the workspace.
        self.importToWS(model)

        print '*** WS after importing the model:'
        #print the workspace
        self.workspace.Print()

        self.workspace.var("n_TTj").setVal(1)

        ##### THIS IS A SIMPLIFIED FIT
        # fix all pdf parameters to the initial value
        #self.fixPars("QCD")
        #self.fixPars("UEC")
        #self.fixPars("TTj")
        #self.fixPars("MR0_")
        #self.fixPars("R0_")
        #self.fixPars("b_")
        #self.fixPars("MR0_TTj")
        #self.fixPars("R0_TTj")
        #self.fixPars("b_TTj")
        self.fixPars("n_TTj")

        def floatSomething(z):
            """Switch on or off whatever you want here"""
            # the "effective" first component in the Had box
            self.floatYield(z)
            self.floatComponent(z)

        fixed = []
        for z in self.zeros:
            if self.name in self.zeros[z]:
                self.fixPars(z)
                self.switchOff(z)
            else:
                if not z in fixed:
                    floatSomething(z)
                    fixed.append(z)


        
        # set normalizations
        Nuec = self.workspace.var("Ntot_UEC").getVal()
        Nttj = self.workspace.var("Ntot_TTj").getVal()
        Nqcd = self.workspace.var("Ntot_QCD").getVal()
        data = RootTools.getDataSet(inputFile,'RMRTree', self.cut)

 
        #in the case that the input file is an MC input file
        if data is None or not data:
            return None
 
        Ndata = data.sumEntries()
        self.workspace.var("Ntot_UEC").setVal(Ndata*Nuec/(Nuec+Nttj+Nqcd))
        self.workspace.var("Ntot_TTj").setVal(Ndata*Nttj/(Nuec+Nttj+Nqcd))
        self.workspace.var("Ntot_QCD").setVal(Ndata*Nqcd/(Nuec+Nttj+Nqcd))
 
        del data
示例#22
0
    def define(self, inputFile):
        xmin = self.workspace.var("MR").getMin()
        xmax = self.workspace.var("MR").getMax()
        ymin = self.workspace.var("Rsq").getMin()
        ymax = self.workspace.var("Rsq").getMax()
        
        #create the dataset
        data = RootTools.getDataSet(inputFile,'RMRTree').reduce("MR >= %f && MR <= %f" %(xmin, xmax)).reduce("Rsq >= %f && Rsq < %f" %(ymin, ymax))
        #use weights
        data
        #import the dataset to the workspace
        self.importToWS(data)
        print 'Reduced dataset'
        #data.Print("V")

        #define the yield as eps*r*L*sigma
        self.yieldToCrossSection()

        #define the ranges
        mR  = self.workspace.var("MR")
        Rsq = self.workspace.var("Rsq")

        # TIGHT
        mR.setRange("B1", 300, 650.)
        Rsq.setRange("B1", 0.09, 0.2)
        mR.setRange("B2", 300, 450.)
        Rsq.setRange("B2", 0.2, 0.3)
        mR.setRange("B3", 300, 350.)
        Rsq.setRange("B3", 0.3, 0.5)
        mR.setRange("FULL", 300, 3500.)
        Rsq.setRange("FULL", 0.09, 0.5)

        #LOOSE
        #mR.setRange("B1", 200, 650.)
        #Rsq.setRange("B1", 0.04, 0.2)
        #mR.setRange("B2", 200, 450.)
        #Rsq.setRange("B2", 0.2, 0.3)
        #mR.setRange("B3", 200, 350.)
        #Rsq.setRange("B3", 0.3, 0.5)
        #mR.setRange("FULL", 200, 3500.)
        #Rsq.setRange("FULL", 0.04, 0.5)

        # define the two components
        self.workspace.factory("RooRazor2DTail::PDF1st(MR,Rsq,MR01st,R01st,b1st)")
        self.workspace.factory("RooRazor2DTail::PDF2nd(MR,Rsq,MR02nd,R02nd,b2nd)")
        #define the two yields
        self.workspace.factory("expr::N_1st('@0*(1-@1*@2)',Ntot,f2,rf)")
        self.workspace.factory("expr::N_2nd('@0*@1*@2',Ntot,f2,rf)")
        #associate the yields to the pdfs through extended PDFs
        self.workspace.factory("RooExtendPdf::ePDF1st(PDF1st, N_1st)")
        self.workspace.factory("RooExtendPdf::ePDF2nd(PDF2nd, N_2nd)")
        # build the total PDF
        model = rt.RooAddPdf("fitmodel", "fitmodel", rt.RooArgList(self.workspace.pdf("ePDF1st"),self.workspace.pdf("ePDF2nd")))        
        # import the model in the workspace.
        self.importToWS(model)
        
        if self.workspace.var('b1st_m') and self.workspace.var('b1st_s'): 
            self.fixVariable('b1st','b1st_m','b1st_s')
        
        #print the workspace
        self.workspace.Print()
示例#23
0
    def plot1DHisto(self, inputFile, xvarname, ranges=None, data = None):
        
        rangeNone = False
        if ranges is None:
            rangeNone = True
            ranges = ['']
            
        #before I find a better way
        rangeCut = self.getVarRangeCutNamed(ranges=ranges)
        if data is None:
            data = RootTools.getDataSet(inputFile,'RMRTree', self.cut)
            data = data.reduce(rangeCut)
        toyData = self.workspace.pdf(self.fitmodel).generate(self.workspace.set('variables'), 50*data.numEntries())
        toyData = toyData.reduce(self.getVarRangeCutNamed(ranges=ranges))

        xmin = min([self.workspace.var(xvarname).getMin(r) for r in ranges])
        xmax = max([self.workspace.var(xvarname).getMax(r) for r in ranges])
        nbins = 25

        # define 1D histograms
        histoData = self.setPoissonErrors(rt.TH1D("histoData", "histoData",nbins, xmin, xmax))
        histoToy = self.setPoissonErrors(rt.TH1D("histoToy", "histoToy",nbins, xmin, xmax))

        def setName(h, name):
            h.SetName('%s_%s_%s' % (h.GetName(),name,'_'.join(ranges)) )
            h.GetXaxis().SetTitle(name)
        
        #this code works, but plots range by range (which looks a bit weird)
        if False:
            #go over the ranges one by one
            for r in ranges:
                cut = self.getVarRangeCut(r)
            
                hD = rt.TH1D("histoData_%s" % r, "histoData",nbins, xmin, xmax)
                hT = rt.TH1D("histoToy_%s" % r, "histoToy",nbins, xmin, xmax)
            
                # project the data on the histograms just in the current range
                data.tree().Project("histoData_%s" % r, xvarname, cut)
                toyData.tree().Project("histoToy_%s" % r, xvarname, cut)
                hT.Scale(hD.Integral()/hT.Integral())
                
                w = data.reduce(cut).numEntries()/(data.numEntries()*1.0)
                histoToy.Add(hT,w)
                histoData.Add(hD,w)

        else:
                # project the data on the histograms
                data.tree().Project("histoData",xvarname)
                toyData.tree().Project("histoToy",xvarname)
                histoToy.Scale(histoData.Integral()/histoToy.Integral())

        setName(histoData,xvarname)
        setName(histoToy,xvarname)

        histoData.SetOption('e1')
        histoData.SetMarkerStyle(20)
        histoToy.SetLineColor(rt.kBlue)
        histoToy.SetLineWidth(2)
        histoToy.SetOption('e3')
        
        c = rt.TCanvas()
        c.SetName('DataMC_%s_%s' % (xvarname,'_'.join(ranges)) )
        histoData.Draw()
        histoToy.Draw('same')

        return [histoData,histoToy,c]
示例#24
0
    def plot1DHistoAllComponents(self, inputFile, xvarname, nbins = 25, ranges=None, data = None):
        rangeNone = False
        if ranges is None:
            rangeNone = True
            ranges = ['']
            
        #before I find a better way
        rangeCut = self.getVarRangeCutNamed(ranges=ranges)
        if data is None:
            data = RootTools.getDataSet(inputFile,'RMRTree', self.cut)
            data = data.reduce(rangeCut)

        #GENERATE 50 times the statistics in the form of toys
        toyData = self.workspace.pdf(self.fitmodel).generate(self.workspace.set('variables'), 50*data.numEntries())

        print toyData
        print ranges
        
        toyData = toyData.reduce(self.getVarRangeCutNamed(ranges=ranges))

        xmin = min([self.workspace.var(xvarname).getMin(r) for r in ranges])
        xmax = max([self.workspace.var(xvarname).getMax(r) for r in ranges])
        
        # define 1D histograms
        bintuple = Binning("Had", True)
        
        rsq_bins = bintuple[1]
        print rsq_bins
        rsq_array = array("d",rsq_bins)

        mr_bins = bintuple[0]
        print mr_bins
        mr_array = array("d",mr_bins)

        

        if xmin < .4: #THIS IS AN RSQ
            histoData = rt.TH1D("histoData", "histoData",len(rsq_bins)-1,rsq_array)
            histoToy = rt.TH1D("histoToy", "histoToy",len(rsq_bins)-1, rsq_array)
        else: #THIS IS AN MR HIST
            histoData = rt.TH1D("histoData", "histoData",len(mr_bins)-1,mr_array)
            histoToy = rt.TH1D("histoToy", "histoToy",len(mr_bins)-1, mr_array)

        
        def setName(h, name):
            h.SetName('%s_%s_%s_ALLCOMPONENTS' % (h.GetName(),name,'_'.join(ranges)) )
            h.GetXaxis().SetTitle(name)
            # x axis
            if name == "MR": h.GetXaxis().SetTitle("M_{R} [GeV]")
            elif name == "Rsq": h.GetXaxis().SetTitle("R^{2}")
            # y axis
            if name == "MR": h.GetYaxis().SetTitle("N Events")
            elif name == "Rsq": h.GetYaxis().SetTitle("N Events")
            # axis labels
            h.GetXaxis().SetTitleSize(0.05)
            h.GetYaxis().SetTitleSize(0.05)
            h.GetXaxis().SetLabelSize(0.05)
            h.GetYaxis().SetLabelSize(0.05)
            h.GetXaxis().SetTitleOffset(0.90)
            h.GetYaxis().SetTitleOffset(0.93)
                                                                                                    
            
        def SetErrors(histo, nbins):
            for i in range(1, nbins+1):
                histo.SetBinError(i,rt.TMath.Sqrt(histo.GetBinContent(i)))

        # project the data on the histograms
        #data.tree().Project("histoData",xvarname)
        data.fillHistogram(histoData,rt.RooArgList(self.workspace.var(xvarname)))
        toyData.fillHistogram(histoToy,rt.RooArgList(self.workspace.var(xvarname)))
        print "histData " + str(histoData.Integral())
        print "histToy " + str(histoToy.Integral())
        toyData.Print()
        if histoToy.Integral() > 0:
            scaleFactor = histoData.Integral()/histoToy.Integral()
        else:
            scaleFactor = 1
            print "no toys! Integral = 0"        

        histoToy.Scale(scaleFactor)
        SetErrors(histoToy, nbins)
        setName(histoData,xvarname)
        setName(histoToy,xvarname)
        histoData.SetMarkerStyle(20)
        histoToy.SetLineColor(rt.kBlue)
        histoToy.SetLineWidth(2)

        c = rt.TCanvas()
        c.SetName('DataMC_%s_%s_ALLCOMPONENTS' % (xvarname,'_'.join(ranges)) )

        histoData.Draw("pe")
        histoToy.DrawCopy('histsame')
        histoToy.SetFillColor(rt.kBlue)
        histoToy.SetFillStyle(3018)
        histoToy.Draw('e2same')

        #histoData.Draw("pesame")

        #leg = rt.TLegend(0.6,0.6,0.9,0.9)
        #leg.SetFillColor(0)
        #leg.AddEntry(histoToyWln.GetName(),"W+jets","l")
        #leg.AddEntry(histoToyTTj.GetName(),"t#bar{t}","l")
        #leg.AddEntry(histoToy.GetName(),"Total","l")
        #leg.Draw()

        histToReturn = [histoToy, histoData, c]

        return histToReturn
示例#25
0
    def plot1D(self, inputFile, varname, nbins=100, ranges=None, data=None, fitmodel="none", frName="none"):
        rangeCut = self.getVarRangeCutNamed(ranges=ranges)
        
        # set the integral precision
        #rt.RooAbsReal.defaultIntegratorConfig().setEpsAbs(1e-30)
        #rt.RooAbsReal.defaultIntegratorConfig().setEpsRel(1e-30)

        
        # get the max and min (if different than default)
        xmin = min([self.workspace.var(varname).getMin(myRange) for myRange in ranges])
        xmax = max([self.workspace.var(varname).getMax(myRange) for myRange in ranges])
        if data is None:
            data = RootTools.getDataSet(inputFile,'MDRTree')
        if fitmodel=="none":
            fitmodel = self.fitmodel
        if frName=="none":
            frName = "independentFR"


        self.workspace.var(varname).setBins(int(nbins))
        frame = self.workspace.var(varname).frame(rt.RooFit.Range(xmin,xmax),rt.RooFit.Bins(int(nbins)))


        frame.SetName("multib_"+varname+"_rooplot_"+fitmodel+"_"+data.GetName())
        frame.SetTitle("")
        if varname=="MDR":
            frame.SetXTitle("M_{#Delta}^{R} [TeV]")
            
        # get fit result to visualize error
        fr = self.workspace.obj(frName)
        

        # to get error band
        errorArgSet = rt.RooArgSet()
        components = ["TTj"]
        for z in components:
            if self.name not in self.zeros[z]:
                errorArgSet.add(self.workspace.set("pdfpars_%s"%z))
                errorArgSet.add(self.workspace.set("otherpars_%s"%z))
            
        errorArgSet.Print("v")


        # plot the data
        data_cut = data.reduce(rangeCut)
        data_cut.plotOn(frame,rt.RooFit.Name("Data") )

        
        pdf =  self.workspace.pdf(fitmodel)
        
        # PLOT TOTAL ERROR +- 1sigma, +- 2 sigma
        [pdf.plotOn(frame,rt.RooFit.LineColor(rt.kBlue-10), rt.RooFit.FillColor(rt.kBlue-10),rt.RooFit.Range(myrange),rt.RooFit.VisualizeError(fr,errorArgSet,2,True),rt.RooFit.Name("twoSigma")) for myrange  in ranges]
        [pdf.plotOn(frame,rt.RooFit.LineColor(rt.kBlue-9), rt.RooFit.FillColor(rt.kBlue-9),rt.RooFit.Range(myrange),rt.RooFit.VisualizeError(fr,errorArgSet,1,True),rt.RooFit.Name("oneSigma")) for myrange  in ranges]
        
        # PLOT THE TOTAL NOMINAL
        [pdf.plotOn(frame, rt.RooFit.Name("Totalpm1sigma"), rt.RooFit.LineColor(rt.kBlue), rt.RooFit.FillColor(rt.kBlue-9),rt.RooFit.Range(myrange)) for myrange in ranges]
        [pdf.plotOn(frame, rt.RooFit.Name("Totalpm2sigma"), rt.RooFit.LineColor(rt.kBlue), rt.RooFit.FillColor(rt.kBlue-10),rt.RooFit.Range(myrange)) for myrange in ranges]

        # plot the data again
        data_cut.plotOn(frame,rt.RooFit.Name("Data") )

        d = rt.TCanvas("d","d",600,500)
        
        pad1 = rt.TPad("pad1","pad1",0,0.25,1,1)
        pad2 = rt.TPad("pad2","pad2",0,0,1,0.25)
        
        pad1.Range(-213.4588,-0.3237935,4222.803,5.412602);
        pad2.Range(-213.4588,-2.206896,4222.803,3.241379);

        pad1.SetLeftMargin(0.15)
        pad2.SetLeftMargin(0.15)
        pad1.SetRightMargin(0.05)
        pad2.SetRightMargin(0.05)
        pad1.SetTopMargin(0.05)
        pad2.SetTopMargin(0.)
        pad1.SetBottomMargin(0.)
        pad2.SetBottomMargin(0.47)
        
        pad1.Draw()
        pad1.cd()
        d.SetName('DataMC_%s_%s' % (varname,'_'.join(ranges)) )
        
        rt.gPad.SetLogy()
        frame.SetMaximum(5e6)
        frame.SetMinimum(0.005)
        frame.Draw()



        # get curves from frame
        curve1 = frame.getCurve("oneSigma").Clone("newcurve1")
        curve2 = frame.getCurve("twoSigma").Clone("newcurve2")
        nomcurve = frame.getCurve("Totalpm1sigma").Clone("newnomcurve")
        datahist = frame.getHist("Data").Clone("newdatahist")

        # get chisq from curves and paramter set: errorArgList
        chisqdof = nomcurve.chiSquare(datahist,errorArgSet.getSize())
        print "chisq/dof = %f" %chisqdof
        nomfunction = pdf.asTF(rt.RooArgList(self.workspace.var(varname)))
        
        # legend
        leg = rt.TLegend(0.65,0.6,0.93,0.93)
        leg.SetFillColor(0)
        leg.SetTextFont(42)
        leg.SetLineColor(0)
        leg.AddEntry("Data","Data","pe")
        leg.AddEntry("Totalpm1sigma","Total Bkgd #pm 1#sigma","lf")
        leg.AddEntry("Totalpm2sigma","Total Bkgd #pm 2#sigma","lf")
        leg.AddEntry(rt.TObject(),"#chi^{2}/dof = %.2f"%chisqdof,"")
        leg.Draw()
        
        pt = rt.TPaveText(0.25,0.75,0.6,0.87,"ndc")
        pt.SetBorderSize(0)
        pt.SetFillColor(0)
        pt.SetFillStyle(0)
        pt.SetLineColor(0)
        pt.SetTextAlign(21)
        pt.SetTextFont(42)
        pt.SetTextSize(0.065)
        Preliminary = "Simulation"
        Lumi = 19.3
        Energy = 8
        text = pt.AddText("CMS %s #sqrt{s} = %i TeV" %(Preliminary,int(Energy)))
        #text = pt.AddText("Razor %s Box #int L = %3.1f fb^{-1}" %(self.name,Lumi))
        #text = pt.AddText("Razor MultiB %s Box" %(self.name))
        text = pt.AddText("Razor MultiB All Boxes")
        pt.Draw()


        # ratio plot
        d.Update()
        d.cd()
        pad2.Draw()
        pad2.cd()

        
        rt.gPad.SetLogy(0)

        # get histogram from RooDataSet
        histData = rt.TH1D("histData","histData",nbins,xmin,xmax)
        data.fillHistogram(histData,rt.RooArgList(self.workspace.var(varname)))
        
        
        for i in range(1,histData.GetNbinsX()):
            xup = histData.GetXaxis().GetBinUpEdge(i)
            xlow = histData.GetXaxis().GetBinLowEdge(i)
            nomintegral = nomfunction.Integral(xlow,xup)/nomfunction.Integral(xmin,xmax)
            nomevents = pdf.expectedEvents(rt.RooArgSet(self.workspace.var(varname))) * nomintegral
            if nomevents>0.:
                histData.SetBinContent(i, histData.GetBinContent(i)/nomevents)
                histData.SetBinError(i, histData.GetBinError(i)/nomevents)

                
        
        histData.SetMarkerStyle(20)
        histData.SetLineWidth(1)
        histData.SetLineColor(rt.kBlack)
        

        for curve in [curve1,curve2]:
            for i in range(0,curve.GetN()):
                x = array('d',[0])
                y = array('d',[0])
                curve.GetPoint(i, x, y)
                if nomcurve.Eval(x[0])>0.:
                    curve.SetPoint(i, x[0], y[0]/nomcurve.Eval(x[0]))

        for j in range(0,nomcurve.GetN()):
            nomx = array('d',[0])
            nomy = array('d',[0])
            nomcurve.GetPoint(j, nomx, nomy)
            nomcurve.SetPoint(j, nomx[0], 1.)

        
        for obj in [curve1, curve2, nomcurve, histData]:
            obj.SetMinimum(0.)
            obj.SetMaximum(3.5)
            obj.GetXaxis().SetTitleOffset(0.97)
            obj.GetXaxis().SetLabelOffset(0.02)
            obj.GetXaxis().SetTitleSize(0.2)
            obj.GetXaxis().SetLabelSize(0.17)
            if varname == "MDR":
                obj.GetXaxis().SetTitle("M_{#Delta}^{R} [TeV]")
            obj.GetYaxis().SetNdivisions(504,rt.kTRUE)
            obj.GetYaxis().SetTitleOffset(0.27)
            obj.GetYaxis().SetTitleSize(0.2)
            obj.GetYaxis().SetLabelSize(0.17)
            obj.GetYaxis().SetTitle("Data/Bkgd")
            obj.GetXaxis().SetTicks("+")
            obj.GetXaxis().SetTickLength(0.07)
            obj.GetXaxis().SetRangeUser(xmin,xmax)

        
        histData.Draw("pe1")
        curve2.Draw("f same")
        curve1.Draw("f same")
        nomcurve.Draw("c same")
        histData.Draw("pe1 same")


        pad2.Update()
        pad1.cd()
        pad1.Update()
        pad1.Draw()
        d.cd()


        fitregionLabel = '_'.join(self.fitregion.split(","))
        
        d.Print("razor_rooplot_%s_%s_%s_%s.pdf"%(varname,fitregionLabel,data.GetName(),self.name))
        

        rt.gROOT.ProcessLine("delete gDirectory->FindObject(\"d\");")
        rt.gROOT.ProcessLine("delete gDirectory->FindObject(\"pad1\");")
        rt.gROOT.ProcessLine("delete gDirectory->FindObject(\"pad2\");")
        return [frame]
    def plot1DHistoAllComponents(self, inputFile, xvarname, nbins = 25, ranges=None, data = None):
        
        rangeNone = False
        if ranges is None:
            rangeNone = True
            ranges = ['']
        
        factor = 50    
        #before I find a better way
        rangeCut = self.getVarRangeCutNamed(ranges=ranges)
        if data is None:
            data = RootTools.getDataSet(inputFile,'RMRTree', self.cut)
            data = data.reduce(rangeCut)
        toyData = self.workspace.pdf(self.fitmodel).generate(self.workspace.set('variables'), factor*data.numEntries())
        toyData = toyData.reduce(self.getVarRangeCutNamed(ranges=ranges))
        
        #also show the data with loose leptons
        dataLep = data.reduce('nLepton > 0')

        xmin = min([self.workspace.var(xvarname).getMin(r) for r in ranges])
        xmax = max([self.workspace.var(xvarname).getMax(r) for r in ranges])

        # define 1D histograms
        histoData = self.setPoissonErrors(rt.TH1D("histoData", "histoData",nbins, xmin, xmax))
        histoDataLep = self.setPoissonErrors(rt.TH1D("histoDataLep", "histoDataLep",nbins, xmin, xmax))
        histoToy = self.setPoissonErrors(rt.TH1D("histoToy", "histoToy",nbins, xmin, xmax))
        histoToyTTj = self.setPoissonErrors(rt.TH1D("histoToyTTj", "histoToyTTj",nbins, xmin, xmax))
        histoToyQCD = self.setPoissonErrors(rt.TH1D("histoToyQCD", "histoToyQCD",nbins, xmin, xmax))

        def setName(h, name):
            h.SetName('%s_%s_%s_ALLCOMPONENTS' % (h.GetName(),name,'_'.join(ranges)) )
            h.GetXaxis().SetTitle(name)
        
        def SetErrors(histo, nbins):
            for i in range(1, nbins+1):
                histo.SetBinError(i,rt.TMath.Sqrt(histo.GetBinContent(i)))

        # project the data on the histograms
        #data.tree().Project("histoData",xvarname)
        data.fillHistogram(histoData,rt.RooArgList(self.workspace.var(xvarname)))
        dataLep.fillHistogram(histoDataLep,rt.RooArgList(self.workspace.var(xvarname)))
        toyData.fillHistogram(histoToy,rt.RooArgList(self.workspace.var(xvarname)))
        
        #Cache the numbers
        Ntt = self.workspace.var("Ntot_TTj").getVal()
        Nqcd = self.workspace.var("Ntot_QCD").getVal()
        
        #Generate the TTj component    
        self.workspace.var("Ntot_QCD").setVal(0.)
        toyDataTTj = self.workspace.pdf(self.fitmodel).generate(self.workspace.set('variables'), int(factor*(data.numEntries()-Nqcd)))
        toyDataTTj.fillHistogram(histoToyTTj,rt.RooArgList(self.workspace.var(xvarname)))
        histoToyTTj.SetLineColor(rt.kRed)
        histoToyTTj.SetLineWidth(2)
        self.workspace.var("Ntot_QCD").setVal(Nqcd)
        
        #Generate the QCD component
        self.workspace.var("Ntot_TTj").setVal(0.)
        toyDataQCD = self.workspace.pdf(self.fitmodel).generate(self.workspace.set('variables'), int(factor*(data.numEntries()-Ntt)))
        toyDataQCD.fillHistogram(histoToyQCD,rt.RooArgList(self.workspace.var(xvarname)))
        histoToyQCD.SetLineColor(rt.kGreen)
        histoToyQCD.SetLineWidth(2)
        self.workspace.var("Ntot_TTj").setVal(Ntt)        
        
        #put some protection in for divide by zero
        scaleFactor = 1.0
        if abs(histoToy.Integral()-0.0) > 1e-8:
            scaleFactor = histoData.Integral()/histoToy.Integral()

        histoToy.Scale(scaleFactor)
        histoToyTTj.Scale(scaleFactor)
        histoToyQCD.Scale(scaleFactor)
        SetErrors(histoToy, nbins)
        SetErrors(histoToyTTj, nbins)
        SetErrors(histoToyQCD, nbins)
        setName(histoData,xvarname)
        setName(histoDataLep,xvarname)
        setName(histoToy,xvarname)
        setName(histoToyTTj,xvarname)
        setName(histoToyQCD,xvarname)
        histoData.SetMarkerStyle(20)
        histoDataLep.SetLineWidth(2)
        histoDataLep.SetLineStyle(rt.kDashed)
        histoToy.SetLineColor(rt.kBlue)
        histoToy.SetLineWidth(2)

        c = rt.TCanvas()
        c.SetName('DataMC_%s_%s_ALLCOMPONENTS' % (xvarname,'_'.join(ranges)) )
        histoData.Draw("pe")
        
        histoDataLep.Draw("histsame")

        histoToyTTj.DrawCopy('histsame')
        histoToyTTj.SetFillColor(rt.kRed)
        histoToyTTj.SetFillStyle(3018)
        histoToyTTj.Draw('e2same')  

        histoToyQCD.DrawCopy('histsame')
        histoToyQCD.SetFillColor(rt.kGreen)
        histoToyQCD.SetFillStyle(3018)
        histoToyQCD.Draw('e2same')  

        histoToy.DrawCopy('histsame')
        histoToy.SetFillColor(rt.kBlue)
        histoToy.SetFillStyle(3018)
        histoToy.Draw('e2same')

        histToReturn = [histoToy, histoToyQCD, histoToyTTj, histoData, histoDataLep, c]

        return histToReturn
示例#27
0
    def plot1DHistoAllComponents(self, inputFile, xvarname, nbins = 25, ranges=None, data = None):
        
        rangeNone = False
        if ranges is None:
            rangeNone = True
            ranges = ['']
            
        #before I find a better way
        rangeCut = self.getVarRangeCutNamed(ranges=ranges)
        if data is None:
            data = RootTools.getDataSet(inputFile,'RMRTree', self.cut)
            data = data.reduce(rangeCut)
        toyData = self.workspace.pdf(self.fitmodel).generate(self.workspace.set('variables'), 50*data.numEntries())
        toyData = toyData.reduce(self.getVarRangeCutNamed(ranges=ranges))

        xmin = min([self.workspace.var(xvarname).getMin(r) for r in ranges])
        xmax = max([self.workspace.var(xvarname).getMax(r) for r in ranges])

        # define 1D histograms
        histoData = self.setPoissonErrors(rt.TH1D("histoData", "histoData",nbins, xmin, xmax))
        histoToy = self.setPoissonErrors(rt.TH1D("histoToy", "histoToy",nbins, xmin, xmax))
        histoToyTTj = self.setPoissonErrors(rt.TH1D("histoToyTTj", "histoToyTTj",nbins, xmin, xmax))
        histoToyWln = self.setPoissonErrors(rt.TH1D("histoToyWln", "histoToyWln",nbins, xmin, xmax))
        histoToyZnn = self.setPoissonErrors(rt.TH1D("histoToyZnn", "histoToyZnn",nbins, xmin, xmax))

        def setName(h, name):
            h.SetName('%s_%s_%s_ALLCOMPONENTS' % (h.GetName(),name,'_'.join(ranges)) )
            h.GetXaxis().SetTitle(name)
        
        def SetErrors(histo, nbins):
            for i in range(1, nbins+1):
                histo.SetBinError(i,rt.TMath.Sqrt(histo.GetBinContent(i)))

        # project the data on the histograms
        #data.tree().Project("histoData",xvarname)
        data.fillHistogram(histoData,rt.RooArgList(self.workspace.var(xvarname)))
        toyData.fillHistogram(histoToy,rt.RooArgList(self.workspace.var(xvarname)))
        scaleFactor = histoData.Integral()/histoToy.Integral()

        histoToy.Scale(scaleFactor)
        SetErrors(histoToy, nbins)
        setName(histoData,xvarname)
        setName(histoToy,xvarname)
        histoData.SetMarkerStyle(20)
        histoToy.SetLineColor(rt.kBlue)
        histoToy.SetLineWidth(2)

        c = rt.TCanvas()
        c.SetLeftMargin(0.15)
        c.SetBottomMargin(0.15)
        c.SetName('DataMC_%s_%s_ALLCOMPONENTS' % (xvarname,'_'.join(ranges)) )
        histoData.Draw("pe")

        histoToy.DrawCopy('histsame')
        histoToy.SetFillColor(rt.kBlue)
        histoToy.SetFillStyle(3018)
        histoToy.Draw('e2same')

        #histoData.Draw("pesame")

        #leg = rt.TLegend(0.6,0.6,0.9,0.9)
        #leg.SetFillColor(0)
        #leg.AddEntry(histoToyWln.GetName(),"W+jets","l")
        #leg.AddEntry(histoToyTTj.GetName(),"t#bar{t}","l")
        #leg.AddEntry(histoToy.GetName(),"Total","l")
        #leg.Draw()

        histToReturn = [histoToy, histoData, c]

        return histToReturn
    """Create a TLegend"""

    title = {dsB_MR: "Run B", dsB_Rsq: "Run B",
             dsC_MR: "Run C", dsC_Rsq: "Run C",
             dsD_MR: "Run D", dsD_Rsq: "Run D"}

    leg = rt.TLegend(0.7, 0.5, 0.93, 0.93)
    leg.AddEntry(run1, title[run1])
    leg.AddEntry(run2, title[run2])

    return leg

if __name__ == '__main__':

    rt.gStyle.SetOptStat(0)
    dsB = RootTools.getDataSet(sys.argv[1], 'RMRTree')
    dsC = RootTools.getDataSet(sys.argv[2], 'RMRTree')
    dsD = RootTools.getDataSet(sys.argv[3], 'RMRTree')

    MR = rt.RooRealVar("MR", "MR", 400., 2000.)
    Rsq = rt.RooRealVar("Rsq", "Rsq", 0.08, 1.)
    nBtag = rt.RooRealVar("nBtag", "nBtag", 1.0, 5.0)

    dsB_MR = make_histogram(MR, dsB)
    dsC_MR = make_histogram(MR, dsC)
    dsD_MR = make_histogram(MR, dsD)
    dsB_Rsq = make_histogram(Rsq, dsB)
    dsC_Rsq = make_histogram(Rsq, dsC)
    dsD_Rsq = make_histogram(Rsq, dsD)

    # RunD V RunB