Ejemplo n.º 1
0
def drawFromDC():
    config = BetterConfigParser()
    config.read(opts.config)
    print "opts.config:", opts.config
    dataname = ''
    if 'Zmm' in opts.bin: dataname = 'Zmm'
    elif 'Zee' in opts.bin: dataname = 'Zee'
    elif 'Wmunu' in opts.bin: dataname = 'Wmn'
    elif 'Wenu' in opts.bin: dataname = 'Wen'
    elif 'Znunu' in opts.bin: dataname = 'Znn'
    elif 'Wtn' in opts.bin: dataname = 'Wtn'

    print 'Variable printing'
    print opts.var
    if (opts.var == ''):
        var = 'BDT'
        if dataname == 'Zmm' or dataname == 'Zee': var = 'BDT_Zll'
        elif dataname == 'Wmn' or dataname == 'Wen': var = 'BDT_Wln'
        elif dataname == 'Znn':
            if 'HighPt' in opts.bin: var = 'BDT_ZnnHighPt'
            elif 'LowPt' in opts.bin: var = 'BDT_ZnnLowPt'
            elif 'LowCSV' in opts.bin: var = 'BDT_ZnnLowCSV'
            else: var = 'BDT_Znn'
        if dataname == '' or var == 'BDT':
            raise RuntimeError, "Did not recognise mode or var from %s" % opts.bin
    else:
        var = opts.var

    region = 'BDT'
    ws_var = config.get('plotDef:%s' % var, 'relPath')
    ws_var = ROOT.RooRealVar(ws_var, ws_var, -1., 1.)
    blind = eval(config.get('Plot:%s' % region, 'blind'))
    Stack = StackMaker(config, var, region, True)

    if 'LowPt' in opts.bin or 'ch1_Wenu' == opts.bin or 'ch2_Wmunu' == opts.bin:
        print 'Niklas %s' % opts.bin
        Stack.addFlag2 = 'Low p_{T}(V)'
    elif 'MedPt' in opts.bin or 'ch1_Wenu2' == opts.bin or 'ch2_Wmunu2' == opts.bin:
        Stack.addFlag2 = 'Intermediate p_{T}(V)'
    elif 'HighPt' in opts.bin or 'ch1_Wenu3' == opts.bin or 'ch2_Wmunu3' == opts.bin:
        Stack.addFlag2 = 'High p_{T}(V)'
    else:
        Stack.addFlag2 = ''

    preFit = False
    addName = 'PostFit_%s' % (opts.fit)
    if not opts.mlfit:
        addName = 'PreFit'
        preFit = True

    Stack.options['pdfName'] = '%s_%s_%s.pdf' % (var, opts.bin, addName)

    log = eval(config.get('Plot:%s' % region, 'log'))

    setup = config.get('Plot_general', 'setup').split(',')
    if dataname == 'Zmm' or dataname == 'Zee':
        try:
            setup.remove('W1b')
            setup.remove('W2b')
            setup.remove('Wlight')
            setup.remove('WH')
        except:
            print '@INFO: Wb / Wligh / WH not present in the datacard'
#    if not dataname == 'Znn' and 'QCD' in setup:
#        setup.remove('QCD')
    Stack.setup = setup

    Dict = eval(config.get('LimitGeneral', 'Dict'))
    lumi = eval(config.get('Plot_general', 'lumi'))

    options = copy(opts)
    options.dataname = "data_obs"
    options.mass = 0
    options.format = "%8.3f +/- %6.3f"
    options.channel = opts.bin
    options.excludeSyst = []
    options.norm = False
    options.stat = False
    options.bin = True  # fake that is a binary output, so that we parse shape lines
    options.out = "tmp.root"
    options.fileName = args[0]
    options.cexpr = False
    options.fixpars = False
    options.libs = []
    options.verbose = 0
    options.poisson = 0
    options.nuisancesToExclude = []
    options.noJMax = None
    theBinning = ROOT.RooFit.Binning(Stack.nBins, Stack.xMin, Stack.xMax)

    file = open(opts.dc, "r")
    os.chdir(os.path.dirname(opts.dc))
    DC = parseCard(file, options)
    if not DC.hasShapes: DC.hasShapes = True
    MB = ShapeBuilder(DC, options)
    theShapes = {}
    theSyst = {}
    nuiVar = {}
    if opts.mlfit:
        nuiVar = readBestFit(opts.mlfit)
    if not opts.bin in DC.bins:
        raise RuntimeError, "Cannot open find %s in bins %s of %s" % (
            opts.bin, DC.bins, opts.dc)
    for b in DC.bins:
        if options.channel != None and (options.channel != b): continue
        exps = {}
        expNui = {}
        shapeNui = {}
        reducedShapeNui = {}
        for (p, e) in DC.exp[b].items(
        ):  # so that we get only self.DC.processes contributing to this bin
            exps[p] = [e, []]
            expNui[p] = [e, []]
        for (lsyst, nofloat, pdf, pdfargs, errline) in DC.systs:
            if pdf in ('param', 'flatParam'): continue
            # begin skip systematics
            skipme = False
            for xs in options.excludeSyst:
                if re.search(xs, lsyst):
                    skipme = True
            if skipme: continue
            # end skip systematics
            counter = 0
            for p in DC.exp[b].keys(
            ):  # so that we get only self.DC.processes contributing to this bin
                if errline[b][p] == 0: continue
                #                if p == 'QCD' and not 'QCD' in setup: continue
                if pdf == 'gmN':
                    exps[p][1].append(1 / sqrt(pdfargs[0] + 1))
                elif pdf == 'gmM':
                    exps[p][1].append(errline[b][p])
                elif type(errline[b][p]) == list:
                    kmax = max(errline[b][p][0], errline[b][p][1],
                               1.0 / errline[b][p][0], 1.0 / errline[b][p][1])
                    exps[p][1].append(kmax - 1.)
                elif pdf == 'lnN':
                    lnNVar = max(errline[b][p], 1.0 / errline[b][p]) - 1.
                    if not nuiVar.has_key('%s_%s' % (opts.fit, lsyst)):
                        nui = 0.
                    else:
                        nui = nuiVar['%s_%s' % (opts.fit, lsyst)][0]
                        lnNVar = lnNVar * nuiVar['%s_%s' %
                                                 (opts.fit, lsyst)][1]
                    exps[p][1].append(lnNVar)
                    expNui[p][1].append(abs(1 - errline[b][p]) * nui)
                elif ("shape" in pdf):
                    #print 'shape %s %s: %s'%(pdf,p,lsyst)
                    s0 = MB.getShape(b, p)
                    sUp = MB.getShape(b, p, lsyst + "Up")
                    sDown = MB.getShape(b, p, lsyst + "Down")
                    if (s0.InheritsFrom("RooDataHist")):
                        s0 = ROOT.RooAbsData.createHistogram(
                            s0, p, ws_var, theBinning)
                        s0.SetName(p)
                        sUp = ROOT.RooAbsData.createHistogram(
                            sUp, p + lsyst + 'Up', ws_var, theBinning)
                        sUp.SetName(p + lsyst + 'Up')
                        sDown = ROOT.RooAbsData.createHistogram(
                            sDown, p + lsyst + 'Down', ws_var, theBinning)
                        sDown.SetName(p + lsyst + 'Down')
                    theShapes[p] = s0.Clone()
                    theShapes[p + lsyst + 'Up'] = sUp.Clone()
                    theShapes[p + lsyst + 'Down'] = sDown.Clone()
                    if not nuiVar.has_key('%s_%s' % (opts.fit, lsyst)):
                        nui = 0.
                        reducedNui = 1.
                    else:
                        nui = nuiVar['%s_%s' % (opts.fit, lsyst)][0]
                        reducedNui = nuiVar['%s_%s' % (opts.fit, lsyst)][1]
                    shapeNui[p + lsyst] = nui
                    reducedShapeNui[lsyst] = reducedNui
                    if not 'CMS_vhbb_stat' in lsyst:
                        if counter == 0:
                            theSyst[lsyst] = s0.Clone()
                            theSyst[lsyst + 'Up'] = sUp.Clone()
                            theSyst[lsyst + 'Down'] = sDown.Clone()
                        else:
                            theSyst[lsyst].Add(s0)
                            theSyst[lsyst + 'Up'].Add(sUp.Clone())
                            theSyst[lsyst + 'Down'].Add(sDown.Clone())
                        counter += 1

    procs = DC.exp[b].keys()
    procs.sort()
    print "Original procs:", procs
    #    if not 'QCD' in setup and 'QCD' in procs:
    #        procs.remove('QCD')
    if not 'W2b' in setup and 'WjHF' in procs:
        procs.remove('WjHF')
    if not 'Wlight' in setup and 'WjLF' in procs:
        procs.remove('WjLF')
    fmt = ("%%-%ds " % max([len(p) for p in procs])) + "  " + options.format
    #Compute norm uncertainty and best fit
    theNormUncert = {}
    theBestFit = {}
    for p in procs:
        relunc = sqrt(sum([x * x for x in exps[p][1]]))
        print fmt % (p, exps[p][0], exps[p][0] * relunc)
        theNormUncert[p] = relunc
        absBestFit = sum([x for x in expNui[p][1]])
        theBestFit[p] = 1. + absBestFit

    histos = []
    typs = []

    setup2 = copy(setup)

    shapesUp = [[] for _ in range(0, len(setup2))]
    shapesDown = [[] for _ in range(0, len(setup2))]

    sigCount = 0
    signalList = ['ZH', 'WH']
    #signalList = ['VVb']
    for p in procs:
        b = opts.bin
        for s in setup:
            if not Dict[s] == p: continue
            if s in signalList:
                if sigCount == 0:
                    Overlay = copy(theShapes[Dict[s]])
                else:
                    Overlay.Add(theShapes[Dict[s]])
                sigCount += 1
            else:
                histos.append(theShapes[Dict[s]])
                typs.append(s)
            for (lsyst, nofloat, pdf, pdfargs, errline) in DC.systs:
                if errline[b][p] == 0: continue
                if ("shape" in pdf) and not 'CMS_vhbb_stat' in lsyst:
                    print 'syst %s' % lsyst
                    shapesUp[setup2.index(s)].append(theShapes[Dict[s] +
                                                               lsyst + 'Up'])
                    shapesDown[setup2.index(s)].append(
                        theShapes[Dict[s] + lsyst + 'Down'])

    #-------------
    #Compute absolute uncertainty from shapes
    counter = 0
    for (lsyst, nofloat, pdf, pdfargs, errline) in DC.systs:
        sumErr = 0
        for p in procs:
            sumErr += errline[b][p]
        if ("shape"
                in pdf) and not 'CMS_vhbb_stat' in lsyst and not sumErr == 0:
            theSystUp = theSyst[lsyst + 'Up'].Clone()
            theSystUp.Add(theSyst[lsyst].Clone(), -1.)
            theSystUp.Multiply(theSystUp)
            theSystDown = theSyst[lsyst + 'Down'].Clone()
            theSystDown.Add(theSyst[lsyst].Clone(), -1.)
            theSystDown.Multiply(theSystDown)
            theSystUp.Scale(reducedShapeNui[lsyst])
            theSystDown.Scale(reducedShapeNui[lsyst])
            if counter == 0:
                theAbsSystUp = theSystUp.Clone()
                theAbsSystDown = theSystDown.Clone()
            else:
                theAbsSystUp.Add(theSystUp.Clone())
                theAbsSystDown.Add(theSystDown.Clone())
            counter += 1

    #-------------
    #Best fit for shapes
    if not preFit:
        histos, Overlay, typs = getBestFitShapes(procs, theShapes, shapeNui,
                                                 theBestFit, DC, setup, opts,
                                                 Dict)

    counter = 0
    errUp = []
    total = []
    errDown = []
    nBins = histos[0].GetNbinsX()
    print 'total bins %s' % nBins
    Error = ROOT.TGraphAsymmErrors(histos[0])
    theTotalMC = histos[0].Clone()
    for h in range(1, len(histos)):
        theTotalMC.Add(histos[h])

    total = [[]] * nBins
    errUp = [[]] * nBins
    errDown = [[]] * nBins
    for bin in range(1, nBins + 1):
        binError = theTotalMC.GetBinError(bin)
        if math.isnan(binError):
            binError = 0.
        total[bin - 1] = theTotalMC.GetBinContent(bin)
        #Stat uncertainty of the MC outline
        errUp[bin - 1] = [binError]
        errDown[bin - 1] = [binError]
        #Relative norm uncertainty of the individual MC
        for h in range(0, len(histos)):
            print "h:", h
            print "bin:", bin
            print "histos:", histos
            print "theNormUncert:", theNormUncert
            print "histos[h]:", histos[h]
            errUp[bin - 1].append(histos[h].GetBinContent(bin) *
                                  theNormUncert[histos[h].GetName()])
            errDown[bin - 1].append(histos[h].GetBinContent(bin) *
                                    theNormUncert[histos[h].GetName()])
    #Shape uncertainty of the MC
    for bin in range(1, nBins + 1):
        #print sqrt(theSystUp.GetBinContent(bin))
        errUp[bin - 1].append(sqrt(theAbsSystUp.GetBinContent(bin)))
        errDown[bin - 1].append(sqrt(theAbsSystDown.GetBinContent(bin)))

    #Add all in quadrature
    totErrUp = [sqrt(sum([x**2 for x in bin])) for bin in errUp]
    totErrDown = [sqrt(sum([x**2 for x in bin])) for bin in errDown]

    #Make TGraph with errors
    for bin in range(1, nBins + 1):
        if not total[bin - 1] == 0:
            point = histos[0].GetXaxis().GetBinCenter(bin)
            Error.SetPoint(bin - 1, point, 1)
            Error.SetPointEYlow(bin - 1, totErrDown[bin - 1] / total[bin - 1])
            print 'down %s' % (totErrDown[bin - 1] / total[bin - 1])
            Error.SetPointEYhigh(bin - 1, totErrUp[bin - 1] / total[bin - 1])
            print 'up   %s' % (totErrUp[bin - 1] / total[bin - 1])

    #-----------------------
    #Read data
    data0 = MB.getShape(opts.bin, 'data_obs')
    if (data0.InheritsFrom("RooDataHist")):
        data0 = ROOT.RooAbsData.createHistogram(data0, 'data_obs', ws_var,
                                                theBinning)
        data0.SetName('data_obs')
    datas = [data0]
    datatyps = [None]
    datanames = [dataname]

    print "blind:", blind
    print "'BDT' in var:", 'BDT' in var
    if blind and 'BDT' in var:
        print "I'm blinding..."
        for bin in range(datas[0].GetNbinsX() / 2, datas[0].GetNbinsX() + 1):
            datas[0].SetBinContent(bin, 0)

    histos.append(copy(Overlay))
    if 'ZH' in signalList and 'WH' in signalList:
        typs.append('VH')
        if 'ZH' in Stack.setup: Stack.setup.remove('ZH')
        if 'WH' in Stack.setup: Stack.setup.remove('WH')
        Stack.setup.insert(0, 'VH')
    elif 'ZH' in signalList:
        typs.append('ZH')
    elif 'WH' in signalList:
        typs.append('WH')
    elif 'VVb' in signalList:
        typs.append('VVb')
    print Stack.setup

    Stack.histos = histos
    Stack.typs = typs
    Stack.datas = datas
    Stack.datatyps = datatyps
    Stack.datanames = datanames
    Stack.overlay = [Overlay]
    Stack.AddErrors = Error
    if dataname == 'Wtn':
        lumi = 18300.
    Stack.lumi = lumi
    Stack.doPlot()

    print 'i am done!\n'
Ejemplo n.º 2
0
def drawFromDC():

    config = BetterConfigParser()
    config.read(opts.config)

    region = opts.region

    print "\nopts.config:",opts.config
    print "opts:", opts
    print "var:", opts.var
    print "bin:", opts.bin
    
    
    dataname = 'Zll'
    
    if 'Zmm' in opts.bin: dataname = 'Zmm'
    elif 'Zee' in opts.bin: dataname = 'Zee'
    elif 'Wmunu' in opts.bin: dataname = 'Wmn'
    elif 'Wenu' in opts.bin: dataname = 'Wen'
    elif 'Znunu' in opts.bin: dataname = 'Znn'
    elif 'Wtn' in opts.bin: dataname = 'Wtn'
    
    if (opts.var == ''):
        var = 'BDT'
        if dataname == 'Zmm' or dataname == 'Zee': var = 'BDT_Zll' 
        elif dataname == 'Wmn' or dataname == 'Wen': var = 'BDT_Wln' 
        elif dataname == 'Znn': 
            if 'HighPt' in opts.bin: var = 'BDT_ZnnHighPt' 
            if 'LowPt' in opts.bin: var = 'BDT_ZnnLowPt' 
            if 'LowCSV' in opts.bin: var = 'BDT_ZnnLowCSV' 
        if dataname == '' or var == 'BDT': raise RuntimeError, 'Did not recognise mode or var from '+opts.bin
    else:
        var = opts.var

    if 'BDT' in var:
        region = 'BDT'    
    else:
        region = opts.bin

    ws_var = config.get('plotDef:%s'%var,'relPath')

    if region == 'BDT':
        ws_var = ROOT.RooRealVar(ws_var,ws_var,-1.,1.)
    else:
        ws_var = ROOT.RooRealVar(ws_var,ws_var, 0, 1.)

    blind = eval(config.get('Plot:%s'%region,'blind'))

    print 'config:', config
    print 'var: ', var
    print 'region: ', region

    
    
    Stack=StackMaker(config,var,region,True)
    
    if 'LowPt' in opts.bin or 'ch1_Wenu' == opts.bin or 'ch2_Wmunu' == opts.bin:
        Stack.addFlag2 = 'Low p_{T}(V)'
    elif 'MedPt' in opts.bin or 'ch1_Wenu2' == opts.bin or 'ch2_Wmunu2' == opts.bin:
        Stack.addFlag2 = 'Intermediate p_{T}(V)'
    elif 'HighPt' in opts.bin or 'ch1_Wenu3' == opts.bin or 'ch2_Wmunu3' == opts.bin:
        Stack.addFlag2 = 'High p_{T}(V)'
        

    # check for pre or post fit options    
    preFit = False
    addName = 'PostFit_%s' %(opts.fit)
    if not opts.mlfit:
        addName = 'PreFit'
        preFit = True

    print '\n-----> Fit Type(opts.fit)  : ', opts.fit
    print '               (opts.mlfit): ', opts.mlfit
    print '               preFit      : ', preFit

        
    Stack.options['pdfName'] = '%s_%s_%s.pdf'  %(var,opts.bin,addName)
    
    log = eval(config.get('Plot:%s'%region,'log'))
    
    setup = config.get('Plot_general','setup').split(',')
    if dataname == 'Zmm' or dataname == 'Zee': 
        try:
            setup.remove('W1b')
            setup.remove('W2b')
            setup.remove('Wlight')
            setup.remove('WH')
        except:
            print '@INFO: Wb / Wligh / WH not present in the datacard'
    if not dataname == 'Znn' and 'QCD' in setup: 
        setup.remove('QCD')
    Stack.setup = setup

    Dict = eval(config.get('LimitGeneral','Dict'))
    lumi = eval(config.get('Plot_general','lumi'))
    
    options = copy(opts)
    options.dataname = "data_obs"
    options.mass = 0
    options.format = "%8.3f +/- %6.3f"
    options.channel = opts.bin
    options.excludeSyst = []
    options.norm = False
    options.stat = False
    options.bin = True # fake that is a binary output, so that we parse shape lines
    options.out = "tmp.root"
    options.fileName = args[0]
    options.cexpr = False
    options.fixpars = False
    options.libs = []
    options.verbose = 0
    options.poisson = 0
    options.nuisancesToExclude = []
    options.noJMax = None
    theBinning = ROOT.RooFit.Binning(Stack.nBins,Stack.xMin,Stack.xMax)

    file = open(opts.dc, "r")

    os.chdir(os.path.dirname(opts.dc))
    
    print '\nDC Path:', os.path.dirname(opts.dc)

    DC = parseCard(file, options)

    if not DC.hasShapes: DC.hasShapes = True

    MB = ShapeBuilder(DC, options)

    theShapes = {}
    theSyst = {}
    nuiVar = {}

    print '\n\n ------> Mlfit File: ', opts.mlfit
    
    if opts.mlfit:
        nuiVar = readBestFit(opts.mlfit)

    if not opts.bin in DC.bins: raise RuntimeError, "Cannot open find %s in bins %s of %s" % (opts.bin,DC.bins,opts.dc)

    print '\n-----> Looping over bins in datacard...'
    for b in DC.bins:

        print '  bin: ', b 
        
        if options.channel != None and (options.channel != b): continue
        exps = {}
        expNui = {}
        shapeNui = {}
        reducedShapeNui = {}

        for (p,e) in DC.exp[b].items(): # so that we get only self.DC.processes contributing to this bin
            exps[p] = [ e, [] ]
            expNui[p] = [ e, [] ]
            
        print '\n-----> Datacard systematics: ', DC.systs    
            
        for (lsyst,nofloat,pdf,pdfargs,errline) in DC.systs:

            print '\n-----> Looping over systematics in datacard: ', (lsyst,nofloat,pdf,pdfargs,errline)  

            if pdf in ('param', 'flatParam'): continue
            
            # begin skip systematics
            skipme = False
            for xs in options.excludeSyst:
                if re.search(xs, lsyst): 
                    skipme = True
            if skipme:
                print '\n-----> skipping systematics...'
                continue
            # end skip systematics

            counter = 0
            print '\n\t-----> Looping over keys in datacard: ', DC.exp[b].keys()
            
            for p in DC.exp[b].keys(): # so that we get only self.DC.processes contributing to this bin

                print '\n\t-----> Looping over process in this bin: ', p
                
                if errline[b][p] == 0: continue

                if p == 'QCD' and not 'QCD' in setup: continue

                if pdf == 'gmN':
                    exps[p][1].append(1/sqrt(pdfargs[0]+1));
                elif pdf == 'gmM':
                    exps[p][1].append(errline[b][p]);
                elif type(errline[b][p]) == list: 
                    kmax = max(errline[b][p][0], errline[b][p][1], 1.0/errline[b][p][0], 1.0/errline[b][p][1]);
                    exps[p][1].append(kmax-1.);
                elif pdf == 'lnN':
                     lnNVar = max(errline[b][p], 1.0/errline[b][p])-1.
                     if not nuiVar.has_key('%s_%s'%(opts.fit,lsyst)):
                         nui = 0.
                     else:
                        nui= nuiVar['%s_%s'%(opts.fit,lsyst)][0]
                        lnNVar = lnNVar*nuiVar['%s_%s'%(opts.fit,lsyst)][1]
                     exps[p][1].append(lnNVar)
                     expNui[p][1].append(abs(1-errline[b][p])*nui);

                elif 'shape' in pdf:
                    
                    print '\n\t-----> Filling the Shapes for this process...'
                    
                    #print 'shape %s %s: %s'%(pdf,p,lsyst)
                    s0 = MB.getShape(b,p)
                    sUp   = MB.getShape(b,p,lsyst+"Up")
                    sDown = MB.getShape(b,p,lsyst+"Down")
                    if (s0.InheritsFrom("RooDataHist")):
                        s0 = ROOT.RooAbsData.createHistogram(s0,p,ws_var,theBinning)
                        s0.SetName(p)
                        sUp = ROOT.RooAbsData.createHistogram(sUp,p+lsyst+'Up',ws_var,theBinning)
                        sUp.SetName(p+lsyst+'Up')
                        sDown = ROOT.RooAbsData.createHistogram(sDown,p+lsyst+'Down',ws_var,theBinning)
                        sDown.SetName(p+lsyst+'Down')
                    theShapes[p] = s0.Clone()
                    theShapes[p+lsyst+'Up'] = sUp.Clone()
                    theShapes[p+lsyst+'Down'] = sDown.Clone()
                    if not nuiVar.has_key('%s_%s'%(opts.fit,lsyst)):
                        nui = 0.
                        reducedNui = 1.
                    else:
                        nui= nuiVar['%s_%s'%(opts.fit,lsyst)][0]
                        reducedNui= nuiVar['%s_%s'%(opts.fit,lsyst)][1]
                    shapeNui[p+lsyst] = nui
                    reducedShapeNui[lsyst] = reducedNui
                    if not 'CMS_vhbb_stat' in lsyst:
                        if counter == 0:
                            theSyst[lsyst] = s0.Clone() 
                            theSyst[lsyst+'Up'] = sUp.Clone() 
                            theSyst[lsyst+'Down'] = sDown.Clone() 
                        else:
                            theSyst[lsyst].Add(s0)
                            theSyst[lsyst+'Up'].Add(sUp.Clone())
                            theSyst[lsyst+'Down'].Add(sDown.Clone()) 
                        counter += 1

    procs = DC.exp[b].keys(); procs.sort()

    if not 'QCD' in setup and 'QCD' in procs:
        procs.remove('QCD')
    if not 'W2b' in setup and 'WjHF' in procs:
        procs.remove('WjHF')
    if not 'Wlight' in setup and 'WjLF' in procs:
        procs.remove('WjLF')

    fmt = ("%%-%ds " % max([len(p) for p in procs]))+"  "+options.format;


    #Compute norm uncertainty and best fit
    theNormUncert = {}
    theBestFit = {}
    print '\n-----> Computing norm uncertaint and best fit...'    
    for p in procs:
        relunc = sqrt(sum([x*x for x in exps[p][1]]))
        print fmt % (p, exps[p][0], exps[p][0]*relunc)
        theNormUncert[p] = relunc
        absBestFit = sum([x for x in expNui[p][1]])
        theBestFit[p] = 1.+absBestFit

    
    histos = []
    typs = []

    setup2=copy(setup)

    shapesUp = [[] for _ in range(0,len(setup2))]
    shapesDown = [[] for _ in range(0,len(setup2))]
    
    sigCount = 0
    signalList = ['ZH','WH']

    # for shape analysis?
    for p in procs:

        b = opts.bin

        print 'process: ', p
        print 'setup:',setup
        print 'Dict:', Dict
        print 'theShapes:', theShapes

        for s in setup:
            print '-----> Fillings the shapes for: ', s
            if not Dict[s] == p: continue
            if s in signalList:
                if sigCount ==0:
                    Overlay=copy(theShapes[Dict[s]])
                else:
                    Overlay.Add(theShapes[Dict[s]])
                sigCount += 1
            else:
                histos.append(theShapes[Dict[s]])
                typs.append(s)
            for (lsyst,nofloat,pdf,pdfargs,errline) in DC.systs:
                if errline[b][p] == 0: continue
                if ("shape" in pdf) and not 'CMS_vhbb_stat' in lsyst:
                    print 'syst %s'%lsyst
                    shapesUp[setup2.index(s)].append(theShapes[Dict[s]+lsyst+'Up'])
                    shapesDown[setup2.index(s)].append(theShapes[Dict[s]+lsyst+'Down'])
    
    #-------------
    #Compute absolute uncertainty from shapes
    counter = 0
    for (lsyst,nofloat,pdf,pdfargs,errline) in DC.systs:
        sumErr = 0
        
        for p in procs:
            sumErr += errline[b][p]
        
            print '---> PDF:',pdf, lsyst

        if ("shape" in pdf) and not 'CMS_vhbb_stat' in lsyst and not sumErr == 0:
            theSystUp = theSyst[lsyst+'Up'].Clone()
            theSystUp.Add(theSyst[lsyst].Clone(),-1.)
            theSystUp.Multiply(theSystUp)
            theSystDown = theSyst[lsyst+'Down'].Clone()
            theSystDown.Add(theSyst[lsyst].Clone(),-1.)
            theSystDown.Multiply(theSystDown)
            theSystUp.Scale(reducedShapeNui[lsyst])
            theSystDown.Scale(reducedShapeNui[lsyst])
            if counter == 0:
                theAbsSystUp = theSystUp.Clone()
                theAbsSystDown = theSystDown.Clone()
            else:
                theAbsSystUp.Add(theSystUp.Clone())
                theAbsSystDown.Add(theSystDown.Clone())
            counter +=1
    
    #-------------
    #Best fit for shapes
    if not preFit:
        
        # Set the preFit as an overlay
        print '\n Making prefit overlay...'
        print procs

        i = 0
        for hist in theShapes:
            if hist not in procs: continue
            print 'Process:', hist
            print 'Shape:', theShapes[hist]
            print 'i:', i
            if i == 0:
                prefit_overlay=copy(theShapes[hist])
                #prefit_overlay=theShapes[hist]
                print 'First Integral:', theShapes[hist].Integral()
                i+=1
            else:
                #prefit_overlay.Add(theShapes[hist], 1.0)
                prefit_overlay.Add(theShapes[hist])
                print 'Integral:', theShapes[hist].Integral()

        print  'prefit_overlay:', prefit_overlay
        print 'Integral:', prefit_overlay.Integral()

        print '\n-----> Getting best fit shapes(for postFit)...'
        histos, Overlay, typs = getBestFitShapes(procs,theShapes,shapeNui,theBestFit,DC,setup,opts,Dict)
        

    

    counter = 0
    errUp=[]
    total=[]
    errDown=[]
    nBins = histos[0].GetNbinsX()

    #print histos
    # temp hack to get histo names right
    #names = ['ggZH','DY2B', 'DY1B', 'DYlight', 'TT', 'VV']
    #for name,i in enumerate(histos):
    #    i.SetName(names[name])
    #Overlay.SetName('ZH')    
    # end hack    

    print '\n total bins %s'%nBins
    print '\n histos: ',histos           
    print '\n theNormUncert: ',theNormUncert
    print '\n Overlay: ', Overlay
    
    Error = ROOT.TGraphAsymmErrors(histos[0])
    theTotalMC = histos[0].Clone()

    for h in range(1,len(histos)):
        theTotalMC.Add(histos[h])
    
    total = [[]]*nBins
    errUp = [[]]*nBins
    errDown = [[]]*nBins

    print '\n\n\t\t -----> The Histos: ', histos

    for bin in range(1,nBins+1):
        binError = theTotalMC.GetBinError(bin)
        if math.isnan(binError):
            binError = 0.
        total[bin-1]=theTotalMC.GetBinContent(bin)

        #Stat uncertainty of the MC outline
        errUp[bin-1] = [binError]
        errDown[bin-1] = [binError]

        # Temp hack to fix theNormUncert naming
        temp_theNormUncert = {}
        for i,hist in enumerate(histos):
            for x in theNormUncert:
                #print '\nx: ', x
                if x in histos[i].GetName():
                    temp_theNormUncert[histos[i].GetName()] = theNormUncert[x]
        #print  temp_theNormUncert    

        #Relative norm uncertainty of the individual MC
        for h in range(0,len(histos)):
            #errUp[bin-1].append(histos[h].GetBinContent(bin)*theNormUncert[histos[h].GetName()])
            #errDown[bin-1].append(histos[h].GetBinContent(bin)*theNormUncert[histos[h].GetName()])
            errUp[bin-1].append(histos[h].GetBinContent(bin)*temp_theNormUncert[histos[h].GetName()])
            errDown[bin-1].append(histos[h].GetBinContent(bin)*temp_theNormUncert[histos[h].GetName()])


    #Shape uncertainty of the MC
    for bin in range(1,nBins+1):
        #print sqrt(theSystUp.GetBinContent(bin))
        errUp[bin-1].append(sqrt(theAbsSystUp.GetBinContent(bin)))
        errDown[bin-1].append(sqrt(theAbsSystDown.GetBinContent(bin)))
    

    #Add all in quadrature
    totErrUp=[sqrt(sum([x**2 for x in bin])) for bin in errUp]
    totErrDown=[sqrt(sum([x**2 for x in bin])) for bin in errDown]

    #Make TGraph with errors
    for bin in range(1,nBins+1):
        if not total[bin-1] == 0:
            point=histos[0].GetXaxis().GetBinCenter(bin)
            Error.SetPoint(bin-1,point,1)
            Error.SetPointEYlow(bin-1,totErrDown[bin-1]/total[bin-1])
            #print 'down %s'%(totErrDown[bin-1]/total[bin-1])
            Error.SetPointEYhigh(bin-1,totErrUp[bin-1]/total[bin-1])
            #print 'up   %s'%(totErrUp[bin-1]/total[bin-1])

    #-----------------------
    #Read data
    data0 = MB.getShape(opts.bin,'data_obs')
    if (data0.InheritsFrom("RooDataHist")):
        data0 = ROOT.RooAbsData.createHistogram(data0,'data_obs',ws_var,theBinning)
        data0.SetName('data_obs')
    datas=[data0]
    datatyps = [None]
    datanames=[dataname] 
    
    print '\nDATA HIST:', data0
    print 'Data name:', dataname

    if blind and 'BDT' in var:
        for bin in range(10,datas[0].GetNbinsX()+1):
            datas[0].SetBinContent(bin,0)

    #for bin in range(0,datas[0].GetNbinsX()+1):
    #    print 'Data in bin x:', datas[0].GetBinContent(bin)

    histos.append(copy(Overlay))
    if 'ZH' in signalList and 'WH' in signalList:
        typs.append('ZH')
        if 'ZH' in Stack.setup: Stack.setup.remove('ZH')
        if 'WH' in Stack.setup: Stack.setup.remove('WH')
        Stack.setup.insert(0,'ZH')
    elif 'ZH' in signalList:
        typs.append('ZH')
    elif 'WH' in signalList:
        typs.append('WH')
    elif 'VVb' in signalList:
        typs.append('VVb')

    print '\n-----> Stack.setup(double check)...'
    print 'Histos:', histos
    print 'typs:', typs
    
    Stack.histos = histos
    Stack.typs = typs
    Stack.datas = datas
    Stack.datatyps = datatyps
    Stack.datanames= datanames
    
    Stack.prefit_overlay = [prefit_overlay]

    if region == 'BDT':
        Stack.overlay = [Overlay]
    print '\n\n\t\t Overlay: ',Stack.overlay
    
    Stack.AddErrors=Error
    if dataname == 'Wtn': 
        lumi = 18300.
    Stack.lumi = lumi
    Stack.doPlot()

    print 'i am done!\n'
Ejemplo n.º 3
0
def drawFromDC():

    config = BetterConfigParser()
    config.read(opts.config)

    region = opts.region

    print "\nopts.config:",opts.config
    print "opts:", opts
    print "var:", opts.var
    print "bin:", opts.bin


    #dataname = 'Zll'
    #dataname = 'SingleElectron_Run2016B_PromptReco'
    dataname = ['SingleElectron_Run2016B_PromptReco','SingleElectron_Run2016C_PromptReco','SingleElectron_Run2016D_PromptReco','SingleElectron_Run2016F_PromptReco','SingleElectron_Run2016G_PromptReco']

    #if 'Zuu' in opts.bin: dataname = 'Zuu'
    #elif 'Zee' in opts.bin: dataname = 'Zee'
    #elif 'Wmunu' in opts.bin: dataname = 'Wmn'
    #elif 'Wenu' in opts.bin: dataname = 'Wen'
    #elif 'Znunu' in opts.bin: dataname = 'Znn'
    #elif 'Wtn' in opts.bin: dataname = 'Wtn'

    #if (opts.var == ''):
    #    var = 'BDT'
    #    if dataname == 'Zmm' or dataname == 'Zee': var = 'BDT_Zll'
    #    elif dataname == 'Wmn' or dataname == 'Wen': var = 'BDT_Wln'
    #    elif dataname == 'Znn':
    #        if 'HighPt' in opts.bin: var = 'BDT_ZnnHighPt'
    #        if 'LowPt' in opts.bin: var = 'BDT_ZnnLowPt'
    #        if 'LowCSV' in opts.bin: var = 'BDT_ZnnLowCSV'
    #    if dataname == '' or var == 'BDT': raise RuntimeError, 'Did not recognise mode or var from '+opts.bin
    #else:
    #    var = opts.var


    if opts.var == 'BDT':
        if 'LowPt' in opts.bin: var = 'gg_plus_ZH125_low_Zpt'
        if 'MedPt' in opts.bin: var = 'gg_plus_ZH125_med_Zpt'
        if 'HighPt' in opts.bin: var = 'gg_plus_ZH125_high_Zpt'
        if 'VV' in opts.bin: var = 'VV_bdt'


    #if 'BDT' in var:
    #    region = 'BDT'
    #else:

    region = opts.bin

    var = opts.var

    ws_var = config.get('plotDef:%s'%var,'relPath')

    if 'gg_plus' in var:
        ws_var = ROOT.RooRealVar(ws_var,ws_var,-1.,1.)
    else:
        ws_var = ROOT.RooRealVar(ws_var,ws_var, 0, 1)

    #blind = eval(config.get('Plot:%s'%region,'blind'))
    #blind = True
    #blind = False
    blind = eval(opts.blind)

    print 'config:', config
    print 'var: ', var
    print 'region: ', region



    Stack=StackMaker(config,var,region,True)

    if 'LowPt' in opts.bin or 'ch1_Wenu' == opts.bin or 'ch2_Wmunu' == opts.bin:
        Stack.addFlag2 = 'Low p_{T}(V)'
    elif 'MedPt' in opts.bin or 'ch1_Wenu2' == opts.bin or 'ch2_Wmunu2' == opts.bin:
        Stack.addFlag2 = 'Intermediate p_{T}(V)'
    elif 'HighPt' in opts.bin or 'ch1_Wenu3' == opts.bin or 'ch2_Wmunu3' == opts.bin:
        Stack.addFlag2 = 'High p_{T}(V)'


    # check for pre or post fit options
    preFit = False
    addName = 'PostFit_%s' %(opts.fit)
    if not opts.mlfit:
        addName = 'PreFit'
        preFit = True

    print '\n-----> Fit Type(opts.fit)  : ', opts.fit
    print '               (opts.mlfit): ', opts.mlfit
    print '               preFit      : ', preFit


    Stack.options['pdfName'] = '%s_%s_%s.pdf'  %(var,opts.bin,addName)

    log = eval(config.get('Plot:%s'%region,'log'))

    setup = ['Z_udscg','Zb','Zbb','TT','VVlight','ST','VV2b','qqZHbb','ggZHbb']

    if dataname == 'Zmm' or dataname == 'Zee': 
        try:
            setup.remove('W1b')
            setup.remove('W2b')
            setup.remove('Wlight')
            setup.remove('WH')
        except: print '@INFO: Wb / Wligh / WH not present in the datacard'
    if not dataname == 'Znn' and 'QCD' in setup:
        setup.remove('QCD')
    Stack.setup = setup

    Dict = eval(config.get('LimitGeneral','Dict'))
    #Dict = eval(config.get('Plot_general','Group'))
    lumi = eval(config.get('Plot_general','lumi'))
    
    options = copy(opts)
    options.dataname = "data_obs"
    options.mass = 0
    options.format = "%8.3f +/- %6.3f"
    options.channel = opts.bin
    options.excludeSyst = []
    options.norm = False
    options.stat = False
    options.bin = True # fake that is a binary output, so that we parse shape lines
    options.out = "tmp.root"
    options.fileName = args[0]
    options.cexpr = False
    options.fixpars = False
    options.libs = []
    options.verbose = 0
    options.poisson = 0
    options.nuisancesToExclude = []
    options.noJMax = None
    theBinning = ROOT.RooFit.Binning(Stack.nBins,Stack.xMin,Stack.xMax)

    file = open(opts.dc, "r")

    os.chdir(os.path.dirname(opts.dc))

    print '\nDC Path:', os.path.dirname(opts.dc)

    DC = parseCard(file, options)

    if not DC.hasShapes: DC.hasShapes = True

    MB = ShapeBuilder(DC, options)

    theShapes = {}
    theSyst = {}
    nuiVar = {}

    print '\n\n ------> Mlfit File: ', opts.mlfit

    if opts.mlfit:
        nuiVar = readBestFit(opts.mlfit)

    if not opts.bin in DC.bins: raise RuntimeError, "Cannot open find %s in bins %s of %s" % (opts.bin,DC.bins,opts.dc)

    print '\n-----> Looping over bins in datacard...'
    for b in DC.bins:

        print '  bin: ', b

        if options.channel != None and (options.channel != b): continue
        exps = {}
        expNui = {}
        shapeNui = {}
        reducedShapeNui = {}

        for (p,e) in DC.exp[b].items(): # so that we get only self.DC.processes contributing to this bin
            exps[p] = [ e, [] ]
            expNui[p] = [ e, [] ]

        print '\n-----> Datacard systematics: ', DC.systs

        for (lsyst,nofloat,pdf,pdfargs,errline) in DC.systs:

            print '\n-----> Looping over systematics in datacard: ', (lsyst,nofloat,pdf,pdfargs,errline)

            if pdf in ('param', 'flatParam'): continue

            # begin skip systematics
            skipme = False
            for xs in options.excludeSyst:
                if re.search(xs, lsyst): 
                    skipme = True
            if skipme:
                print '\n-----> skipping systematics...'
                continue
            # end skip systematics

            counter = 0
            print '\n\t-----> Looping over keys in datacard: ', DC.exp[b].keys()

            for p in DC.exp[b].keys(): # so that we get only self.DC.processes contributing to this bin

                print '\n\t-----> Looping over process in this bin: ', p

                if errline[b][p] == 0: continue

                if p == 'QCD' and not 'QCD' in setup: continue

                if pdf == 'gmN':
                    exps[p][1].append(1/sqrt(pdfargs[0]+1));
                elif pdf == 'gmM':
                    exps[p][1].append(errline[b][p]);
                elif type(errline[b][p]) == list: 
                    kmax = max(errline[b][p][0], errline[b][p][1], 1.0/errline[b][p][0], 1.0/errline[b][p][1]);
                    exps[p][1].append(kmax-1.);
                elif pdf == 'lnN':
                     lnNVar = max(errline[b][p], 1.0/errline[b][p])-1.
                     if not nuiVar.has_key('%s_%s'%(opts.fit,lsyst)):
                         nui = 0.
                     else:
                        nui= nuiVar['%s_%s'%(opts.fit,lsyst)][0]
                        lnNVar = lnNVar*nuiVar['%s_%s'%(opts.fit,lsyst)][1]
                     exps[p][1].append(lnNVar)
                     expNui[p][1].append(abs(1-errline[b][p])*nui);

                elif 'shape' in pdf:

                    print '\n\t-----> Filling the Shapes for this process...'

                    #print 'shape %s %s: %s'%(pdf,p,lsyst)
                    s0 = MB.getShape(b,p)
                    sUp   = MB.getShape(b,p,lsyst+"Up")
                    sDown = MB.getShape(b,p,lsyst+"Down")
                    if (s0.InheritsFrom("RooDataHist")):
                        s0 = ROOT.RooAbsData.createHistogram(s0,p,ws_var,theBinning)
                        s0.SetName(p)
                        sUp = ROOT.RooAbsData.createHistogram(sUp,p+lsyst+'Up',ws_var,theBinning)
                        sUp.SetName(p+lsyst+'Up')
                        sDown = ROOT.RooAbsData.createHistogram(sDown,p+lsyst+'Down',ws_var,theBinning)
                        sDown.SetName(p+lsyst+'Down')
                    theShapes[p] = s0.Clone()
                    theShapes[p+lsyst+'Up'] = sUp.Clone()
                    theShapes[p+lsyst+'Down'] = sDown.Clone()
                    if not nuiVar.has_key('%s_%s'%(opts.fit,lsyst)):
                        nui = 0.
                        reducedNui = 1.
                    else:
                        nui= nuiVar['%s_%s'%(opts.fit,lsyst)][0]
                        reducedNui= nuiVar['%s_%s'%(opts.fit,lsyst)][1]
                    shapeNui[p+lsyst] = nui
                    reducedShapeNui[lsyst] = reducedNui
                    if not 'CMS_vhbb_stat' in lsyst:
                        if counter == 0:
                            theSyst[lsyst] = s0.Clone() 
                            theSyst[lsyst+'Up'] = sUp.Clone() 
                            theSyst[lsyst+'Down'] = sDown.Clone() 
                        else:
                            theSyst[lsyst].Add(s0)
                            theSyst[lsyst+'Up'].Add(sUp.Clone())
                            theSyst[lsyst+'Down'].Add(sDown.Clone()) 
                        counter += 1

    procs = DC.exp[b].keys(); procs.sort()

    if not 'QCD' in setup and 'QCD' in procs:
        procs.remove('QCD')
    if not 'W2b' in setup and 'WjHF' in procs:
        procs.remove('WjHF')
    if not 'Wlight' in setup and 'WjLF' in procs:
        procs.remove('WjLF')

    fmt = ("%%-%ds " % max([len(p) for p in procs]))+"  "+options.format;


    #Compute norm uncertainty and best fit
    theNormUncert = {}
    theBestFit = {}
    print '\n-----> Computing norm uncertaint and best fit...'
    for p in procs:
        relunc = sqrt(sum([x*x for x in exps[p][1]]))
        print fmt % (p, exps[p][0], exps[p][0]*relunc)
        theNormUncert[p] = relunc
        absBestFit = sum([x for x in expNui[p][1]])
        theBestFit[p] = 1.+absBestFit

    
    histos = []
    typs = []

    setup2=copy(setup)

    shapesUp = [[] for _ in range(0,len(setup2))]
    shapesDown = [[] for _ in range(0,len(setup2))]
    
    sigCount = 0
    #signalList = ['Zbb','WH']
    #signalList = ['Zbb']
    signalList = ['ZH']
    #signalList = ['ggZHbb','qqZHbb']
    Overlay ={}

    # for shape analysis?
    for p in procs:

        b = opts.bin

        print 'process: ', p
        print 'setup:',setup
        print 'Dict:', Dict
        #print 'theShapes:', theShapes


        for s in setup:
            print '-----> Fillings the shapes for: ', s
            #print Dict[s], p
            if Dict[s] != p:
                print 'not equal', p
                print 'not equal', Dict[s]
                continue
            if s in signalList:
                if sigCount ==0:
                    Overlay=copy(theShapes[Dict[s]])
                else:
                    Overlay.Add(theShapes[Dict[s]])
                sigCount += 1
            else:
                histos.append(theShapes[Dict[s]])
                typs.append(s)
            for (lsyst,nofloat,pdf,pdfargs,errline) in DC.systs:
                if errline[b][p] == 0: continue
                if ("shape" in pdf) and not 'CMS_vhbb_stat' in lsyst:
                    print 'syst %s'%lsyst
                    shapesUp[setup2.index(s)].append(theShapes[Dict[s]+lsyst+'Up'])
                    shapesDown[setup2.index(s)].append(theShapes[Dict[s]+lsyst+'Down'])

    #-------------
    #Compute absolute uncertainty from shapes
    counter = 0
    for (lsyst,nofloat,pdf,pdfargs,errline) in DC.systs:
        sumErr = 0

        for p in procs:
            sumErr += errline[b][p]

            print '---> PDF:',pdf, lsyst

        if ("shape" in pdf) and not 'CMS_vhbb_stat' in lsyst and not sumErr == 0:
            theSystUp = theSyst[lsyst+'Up'].Clone()
            theSystUp.Add(theSyst[lsyst].Clone(),-1.)
            theSystUp.Multiply(theSystUp)
            theSystDown = theSyst[lsyst+'Down'].Clone()
            theSystDown.Add(theSyst[lsyst].Clone(),-1.)
            theSystDown.Multiply(theSystDown)
            theSystUp.Scale(reducedShapeNui[lsyst])
            theSystDown.Scale(reducedShapeNui[lsyst])
            if counter == 0:
                theAbsSystUp = theSystUp.Clone()
                theAbsSystDown = theSystDown.Clone()
            else:
                theAbsSystUp.Add(theSystUp.Clone())
                theAbsSystDown.Add(theSystDown.Clone())
            counter +=1
    
    #-------------
    #Best fit for shapes
    if not preFit:

        # Set the preFit as an overlay
        print '\n Making prefit overlay...'
        print procs

        i = 0
        for hist in theShapes:
            if hist not in procs: continue
            print 'Process:', hist
            print 'Shape:', theShapes[hist]
            print 'i:', i
            if i == 0:
                prefit_overlay=copy(theShapes[hist])
                #prefit_overlay=theShapes[hist]
                print 'First Integral:', theShapes[hist].Integral()
                i+=1
            else:
                #prefit_overlay.Add(theShapes[hist], 1.0)
                prefit_overlay.Add(theShapes[hist])
                print 'Integral:', theShapes[hist].Integral()

        print  'prefit_overlay:', prefit_overlay
        print 'Integral:', prefit_overlay.Integral()

        print '\n-----> Getting best fit shapes(for postFit)...'
        histos, Overlay, typs = getBestFitShapes(procs,theShapes,shapeNui,theBestFit,DC,setup,opts,Dict)


    

    counter = 0
    errUp=[]
    total=[]
    errDown=[]
    nBins = histos[0].GetNbinsX()

    #print histos
    # temp hack to get histo names right
    #names = ['ggZH','DY2B', 'DY1B', 'DYlight', 'TT', 'VV']
    #for name,i in enumerate(histos):
    #    i.SetName(names[name])
    #Overlay.SetName('ZH')
    # end hack

    print '\n total bins %s'%nBins
    print '\n histos: ',histos
    print '\n theNormUncert: ',theNormUncert
    print '\n Overlay: ', Overlay

    Error = ROOT.TGraphAsymmErrors(histos[0])
    theTotalMC = histos[0].Clone()

    for h in range(1,len(histos)):
        theTotalMC.Add(histos[h])
    
    total = [[]]*nBins
    errUp = [[]]*nBins
    errDown = [[]]*nBins

    print '\n\n\t\t -----> The Histos: ', histos

    for bin in range(1,nBins+1):
        binError = theTotalMC.GetBinError(bin)
        if math.isnan(binError):
            binError = 0.
        total[bin-1]=theTotalMC.GetBinContent(bin)

        #Stat uncertainty of the MC outline
        errUp[bin-1] = [binError]
        errDown[bin-1] = [binError]

        # Temp hack to fix theNormUncert naming
        temp_theNormUncert = {}
        for i,hist in enumerate(histos):
            for x in theNormUncert:
                #print '\nx: ', x
                if x in histos[i].GetName():
                    temp_theNormUncert[histos[i].GetName()] = theNormUncert[x]
        #print  temp_theNormUncert

        #Relative norm uncertainty of the individual MC
        for h in range(0,len(histos)):
            #errUp[bin-1].append(histos[h].GetBinContent(bin)*theNormUncert[histos[h].GetName()])
            #errDown[bin-1].append(histos[h].GetBinContent(bin)*theNormUncert[histos[h].GetName()])
            errUp[bin-1].append(histos[h].GetBinContent(bin)*temp_theNormUncert[histos[h].GetName()])
            errDown[bin-1].append(histos[h].GetBinContent(bin)*temp_theNormUncert[histos[h].GetName()])


    #Shape uncertainty of the MC
    for bin in range(1,nBins+1):
        #print sqrt(theSystUp.GetBinContent(bin))
        errUp[bin-1].append(sqrt(theAbsSystUp.GetBinContent(bin)))
        errDown[bin-1].append(sqrt(theAbsSystDown.GetBinContent(bin)))
    

    #Add all in quadrature
    totErrUp=[sqrt(sum([x**2 for x in bin])) for bin in errUp]
    totErrDown=[sqrt(sum([x**2 for x in bin])) for bin in errDown]

    #Make TGraph with errors
    for bin in range(1,nBins+1):
        if not total[bin-1] == 0:
            point=histos[0].GetXaxis().GetBinCenter(bin)
            Error.SetPoint(bin-1,point,1)
            Error.SetPointEYlow(bin-1,totErrDown[bin-1]/total[bin-1])
            #print 'down %s'%(totErrDown[bin-1]/total[bin-1])
            Error.SetPointEYhigh(bin-1,totErrUp[bin-1]/total[bin-1])
            #print 'up   %s'%(totErrUp[bin-1]/total[bin-1])

    #-----------------------
    #Read data
    data0 = MB.getShape(opts.bin,'data_obs')
    if (data0.InheritsFrom("RooDataHist")):
        data0 = ROOT.RooAbsData.createHistogram(data0,'data_obs',ws_var,theBinning)
        data0.SetName('data_obs')
    datas=[data0]
    datatyps = [None]
    #datanames=[dataname]
    datanames= dataname

    print '\nDATA HIST:', data0
    print 'Data name:', dataname

    if blind:
        for bin in range(12,datas[0].GetNbinsX()+1):
            datas[0].SetBinContent(bin,0)

    #for bin in range(0,datas[0].GetNbinsX()+1):
    #    print 'Data in bin x:', datas[0].GetBinContent(bin)


    if 'VV' in opts.bin:
        signalList = ['VVb',' VVlight']

    print 'Signal List:', signalList

    histos.append(copy(Overlay))
    if 'Zbb' in signalList and 'WH' in signalList:
        typs.append('Zbb')
        if 'Zbb' in Stack.setup: Stack.setup.remove('Zbb')
        if 'WH' in Stack.setup: Stack.setup.remove('WH')
        Stack.setup.insert(0,'Zbb')
    elif 'Zbb' in signalList:
        typs.append('Zbb')
    elif 'WH' in signalList:
        typs.append('WH')
    elif 'ZH' in signalList:
        typs.append('ZH')
    if 'VVb' in signalList:
        typs.append('VVb')
        #typs.append('VVlight')


    print '\n-----> Stack.setup(double check)...'
    print 'Histos:', histos
    print 'typs:', typs

    Stack.histos = histos
    Stack.typs = typs
    Stack.datas = datas
    Stack.datatyps = datatyps
    Stack.datanames= datanames

    #Stack.prefit_overlay = [prefit_overlay]

    if '13TeV' in region:
        Stack.overlay = [Overlay]
    print '\n\n\t\t Overlay: ',Stack.overlay

    Stack.AddErrors=Error
    if dataname == 'Wtn': 
        lumi = 18300.
    Stack.lumi = lumi
    Stack.doPlot()

    print 'i am done!\n'
Ejemplo n.º 4
0
def drawFromDC():

    config = BetterConfigParser()
    config.read(opts.config)

    region = opts.region

    print "\nopts.config:", opts.config
    print "opts:", opts
    print "var:", opts.var
    print "bin:", opts.bin

    dataname = 'Zll'

    if 'Zuu' in opts.bin: dataname = 'Zuu'
    elif 'Zee' in opts.bin: dataname = 'Zee'
    elif 'Wmn' in opts.bin: dataname = 'Wmn'
    elif 'Wen' in opts.bin: dataname = 'Wen'
    elif 'Znn' in opts.bin: dataname = 'Znn'
    elif 'Wtn' in opts.bin: dataname = 'Wtn'

    if (opts.var == ''):
        var = 'BDT'
        if dataname == 'Zmm' or dataname == 'Zee': var = 'BDT_Zll'
        elif dataname == 'Wmn' or dataname == 'Wen': var = 'BDT_Wln'
        elif dataname == 'Znn': var = 'BDT_Znn'
        #if 'HighPt' in opts.bin: var = 'BDT_ZnnHighPt'
        #if 'LowPt' in opts.bin: var = 'BDT_ZnnLowPt'
        #if 'LowCSV' in opts.bin: var = 'BDT_ZnnLowCSV'
        if dataname == '' or var == 'BDT':
            raise RuntimeError, 'Did not recognise mode or var from ' + opts.bin
    else:
        var = opts.var

    if opts.var == 'BDT':
        if 'LowPt' in opts.bin: var = 'gg_plus_ZH125_low_Zpt'
        elif 'MedPt' in opts.bin: var = 'gg_plus_ZH125_low_Zpt'
        elif 'HighPt' in opts.bin: var = 'gg_plus_ZH125_high_Zpt'
        elif 'VV' in opts.bin: var = 'VV_bdt'
        else: var = 'gg_plus_ZH125_high_Zpt'

    if opts.var == 'CRBDT':
        if 'LowPt' in opts.bin: var = 'gg_plus_ZH125_low_Zpt_CR'
        elif 'MedPt' in opts.bin: var = 'gg_plus_ZH125_low_Zpt_CR'
        elif 'HighPt' in opts.bin: var = 'gg_plus_ZH125_high_Zpt_CR'
        else: var = 'gg_plus_ZH125_high_Zpt_CR'

    #if 'BDT' in var:
    #    region = 'BDT'
    #else:

    region = opts.bin

    ws_var = config.get('plotDef:%s' % var, 'relPath')

    print 'ws_var:', ws_var

    if opts.var == 'BDT':
        ws_var = ROOT.RooRealVar(ws_var, ws_var, -1, 1.)
    else:
        ws_var = ROOT.RooRealVar(ws_var, ws_var, -1, 1.)

    print 'config:', config
    print 'var: ', var

    #if 'Wen' in region:
    #    region = 'WenHighPt'
    #if 'Wmn' in region:
    #    region = 'WmnHighPt'

    if 'Znn' in region:
        region = 'Znn_13TeV_Signal'

    print 'region: ', region

    blind = eval(config.get('Plot:%s' % region, 'blind'))

    Stack = StackMaker(config, var, region, True)

    if 'low' in opts.bin or 'ch1_Wenu' == opts.bin or 'ch2_Wmunu' == opts.bin:
        Stack.addFlag2 = 'Low p_{T}(V)'
    elif 'MedPt' in opts.bin or 'ch1_Wenu2' == opts.bin or 'ch2_Wmunu2' == opts.bin:
        Stack.addFlag2 = 'Intermediate p_{T}(V)'
    elif 'high' in opts.bin or 'ch1_Wenu3' == opts.bin or 'ch2_Wmunu3' == opts.bin:
        Stack.addFlag2 = 'High p_{T}(V)'

    # check for pre or post fit options
    preFit = False
    addName = 'PostFit_%s' % (opts.fit)
    if not opts.mlfit:
        addName = 'PreFit'
        preFit = True

    print '\n-----> Fit Type(opts.fit)  : ', opts.fit
    print '               (opts.mlfit): ', opts.mlfit
    print '               preFit      : ', preFit
    print '               opts.bin    : ', opts.bin

    Stack.options['pdfName'] = '%s_%s_%s.pdf' % (var, opts.bin, addName)

    log = eval(config.get('Plot:%s' % region, 'log'))

    if 'Zll' in opts.bin or 'Zee' in opts.bin or 'Zuu' in opts.bin or 'Zmm' in opts.bin:
        #setup = config.get('Plot_general','setup').split(',')
        setup = [
            'ZH', 'ggZH', 'DY2b', 'DY1b', 'DYlight', 'TT', 'VVHF', 'VVLF', 'ST'
        ]
        signalList = ['ZH']

        channel = 'ZllHbb'

        if 'Zee' in opts.bin: lep_channel = 'Zee'
        elif 'Zuu' in opts.bin: lep_channel = 'Zmm'

        region_dic = {
            'BDT': 'SIG',
            ' Zlf': 'Zlf',
            'Zhf': 'Zhf',
            'TT': 'TT',
            '13TeV': 'SIG'
        }
        region_name = [
            region_dic[key] for key in region_dic if (key in opts.bin)
        ]

        region_name = 'SIG'

        if 'Zlf' in opts.bin: region_name = 'Zlf'
        if 'Zhf' in opts.bin: region_name = 'Zhf'
        if 'ttbar' in opts.bin: region_name = 'TT'

        if 'low' in opts.bin or 'Low' in opts.bin: pt_region_name = 'low'
        if 'high' in opts.bin or 'High' in opts.bin: pt_region_name = 'high'
        if 'med' in opts.bin or 'Med' in opts.bin: pt_region_name = 'med'

        # ['ZllHbb', 'ch1', 'Zmm', 'SIG', 'low']
        if 'Zmm' in lep_channel and 'low' in pt_region_name: zll_index = 'ch1'
        elif 'Zmm' in lep_channel and 'high' in pt_region_name:
            zll_index = 'ch3'
        elif 'Zee' in lep_channel and 'high' in pt_region_name:
            zll_index = 'ch4'
        elif 'Zee' in lep_channel and 'low' in pt_region_name:
            zll_index = 'ch2'
        else:
            zll_index = 'ch2'

        # binning
        nBins = Stack.nBins
        xMin = Stack.xMin
        xMax = Stack.xMax

        # for stat hists
        if 'Zee' in lep_channel and 'low' in pt_region_name and 'minCMVA' not in opts.var:
            stat_name = opts.bin
        elif 'Zee' in lep_channel and 'high' in pt_region_name and 'minCMVA' not in opts.var:
            stat_name = opts.bin
        elif 'Zmm' in lep_channel and 'low' in pt_region_name and 'minCMVA' not in opts.var:
            stat_name = opts.bin
        elif 'Zmm' in lep_channel and 'high' in pt_region_name and 'minCMVA' not in opts.var:
            stat_name = opts.bin
        else:
            stat_name = opts.bin

        if 'minCMVA' in opts.var:
            if 'low' in pt_region_name:
                if 'Zmm' in lep_channel:
                    if 'Zlf' in region_name: stat_name = 'Zlf_low_Zuu'
                    if 'Zhf' in region_name: stat_name = 'Zhf_low_Zuu'
                    if 'TT' in region_name: stat_name = 'ttbar_low_Zuu'
                else:
                    if 'Zlf' in region_name: stat_name = 'Zlf_low_Zee'
                    if 'Zhf' in region_name: stat_name = 'Zhf_low_Zee'
                    if 'TT' in region_name: stat_name = 'ttbar_low_Zee'

            if 'high' in pt_region_name:
                if 'Zmm' in lep_channel:
                    if 'Zlf' in region_name: stat_name = 'Zlf_high_Zuu'
                    if 'Zhf' in region_name: stat_name = 'Zhf_high_Zuu'
                    if 'TT' in region_name: stat_name = 'ttbar_high_Zuu'
                else:
                    if 'Zlf' in region_name: stat_name = 'Zlf_high_Zee'
                    if 'Zhf' in region_name: stat_name = 'Zhf_high_Zee'
                    if 'TT' in region_name: stat_name = 'ttbar_high_Zee'

    if 'Wmn' in opts.bin or 'Wen' in opts.bin:
        setup = [
            'WH', 'ZH', 'Wj2b', 'Wj1b', 'Wj0b', 'DY2b', 'DY1b', 'DYlight',
            'TT', 'VVHF', 'VVLF', 'ST'
        ]
        signalList = ['ZH', 'WH']

        channel = 'WlnHbb'
        if 'Wen' in opts.bin:
            lep_channel = 'Wen'
            if 'tt' in region: region_name = 'ttWen'
            elif 'whf' in region:
                if 'High' in opts.bin:
                    region_name = 'whfWenHigh'
                else:
                    region_name = 'whfWenLow'
            elif 'wlf' in region:
                region_name = 'wlfWen'

            else:
                region_name = opts.bin

        if 'Wmn' in opts.bin:
            lep_channel = 'Wmn'
            if 'tt' in region: region_name = 'ttWmn'
            elif 'whf' in region:
                if 'High' in opts.bin:
                    region_name = 'whfWmnHigh'
                else:
                    region_name = 'whfWmnLow'
            elif 'wlf' in region:
                region_name = 'wlfWmn'

            else:
                region_name = opts.bin

        pt_region_name = 'none'

        nBins = 40
        xMin = -1
        xMax = 1

        stat_name = 'BDT_' + opts.bin + '_'

    if 'Znn' in opts.bin:
        setup = [
            'ZH', 'ggZH', 'WH', 'DY2b', 'DY1b', 'DYlight', 'Wj2b', 'Wj1b',
            'Wj0b', 'TT', 'VVHF', 'VVLF', 'ST'
        ]
        signalList = ['ZH']

        channel = 'Znn'
        lep_channel = 'Znn'

        if 'nAddJet1' in opts.bin:
            pt_region_name = 'nAddJet1'
        elif 'nAddJet0' in opts.bin:
            pt_region_name = 'nAddJet0'
        else:
            pt_region_name = 'High'

        region_name = 'SR'

        nBins = 25
        xMin = -1
        xMax = 1

        if 'QCD' in opts.dc:
            region_name = 'QCD'
            stat_name = 'Znn_13TeV_QCD'
        elif 'TT' in opts.dc:
            region_name = 'TT'
            stat_name = 'Znn_13TeV_TT'
        elif 'Zbb' in opts.dc:
            region_name = 'Zbb'
            stat_name = 'Znn_13TeV_Zbb'
        elif 'Zlight' in opts.dc:
            region_name = 'Zlight'
            stat_name = 'Znn_13TeV_Zlight'
        else:
            stat_name = opts.bin

    print '############'
    print 'Channel is', channel
    print 'lepton channel is', lep_channel
    print 'region_name is', region_name
    print 'pt region_name is', pt_region_name

    print '/n----> The Binning:'
    print 'nBins:', nBins
    print 'xMin:', xMin
    print 'xMax:', xMax

    if dataname == 'Zmm' or dataname == 'Zee':
        try:
            setup.remove('W1b')
            setup.remove('W2b')
            setup.remove('Wlight')
            setup.remove('WH')
        except:
            print '@INFO: Wb / Wligh / WH not present in the datacard'
    if not dataname == 'Znn' and 'QCD' in setup:
        setup.remove('QCD')

    Stack.setup = setup

    Dict = eval(config.get('LimitGeneral', 'Dict'))
    lumi = eval(config.get('Plot_general', 'lumi'))

    options = copy(opts)
    options.dataname = "data_obs"
    options.mass = 0
    options.format = "%8.3f +/- %6.3f"
    options.channel = opts.bin
    options.excludeSyst = []
    options.norm = False
    options.stat = False
    options.bin = True  # fake that is a binary output, so that we parse shape lines
    options.out = "tmp.root"
    options.fileName = args[0]
    options.filename = region
    options.cexpr = False
    options.fixpars = False
    options.libs = []
    options.verbose = 0
    options.poisson = 0
    options.nuisancesToExclude = []
    options.noJMax = None

    theBinning = ROOT.RooFit.Binning(nBins, xMin, xMax)

    # for prefit erros
    prefit_error_histos = []
    postfit_error_histos = []

    histos = []
    typs = []
    shapes = {}
    shapesUp = [[] for _ in range(0, len(setup))]
    shapesDown = [[] for _ in range(0, len(setup))]

    sigCount = 0
    Overlay = []
    prefit_overlay = []

    dirname = ''

    #Open the mlfit.root and retrieve the mc
    file = ROOT.TFile.Open(opts.mlfit)
    if file == None: raise RuntimeError, "Cannot open file %s" % opts.mlfit
    print '\n\n-----> Fit File: ', file

    for dir in ROOT.gDirectory.GetListOfKeys():

        dirinfo = dir.GetName().split('_')

        print 'dirinfo:', dirinfo

        if 'Znn' in dirinfo[0] and 'Znn' not in opts.bin: continue
        if 'ZllHbb' in dirinfo[0] and ('Zmm' not in lep_channel
                                       and 'Zee' not in lep_channel):
            continue
        if 'W' in dirinfo[0] and 'W' not in opts.bin: continue

        if 'W' in opts.bin:
            print 'channel, lepton channel, region_name:', channel, lep_channel, region_name
            #if not (dirinfo[0] == channel and dirinfo[1] == lep_channel and dirinfo[2] == region_name):
            if not (dirinfo[0] == region_name and dirinfo[1] == 'postfit'):
                continue
            else:
                print '!!! Match !!!'

        if 'Znn' in opts.bin:
            print 'channel, lepton channel, region_name, pt_region_name:', channel, lep_channel, region_name, pt_region_name
            #if not (dirinfo[0] == lep_channel and dirinfo[2] == pt_region_name and dirinfo[3] == 'postfit'):
            if not (dirinfo[0] == lep_channel and dirinfo[2] == region_name
                    and dirinfo[3] == 'postfit'):
                continue
            else:
                print '!!! Match !!!'

        if 'Zuu' in opts.bin or 'Zee' in opts.bin:
            print 'channel, zll index, lepton channel, region_name, pt_region_name:', channel, zll_index, lep_channel, region_name, pt_region_name
            if not (dirinfo[0] == lep_channel and dirinfo[1] == region_name and
                    dirinfo[2] == pt_region_name and dirinfo[3] == 'postfit'):
                continue
            else:
                print '!!! Match !!!'

        print 'Directory:', dir.GetName()

        dirname = dir.GetName()

        # Pull out the MC stat uncertainties first
        #hists_WenHighPt40.root
        #vhbb_WenHighPt40_13TeV.txt

        if 'W' in opts.bin:
            stat_filename = opts.dc.replace('.txt', '.root')
            stat_filename = stat_filename.replace('vhbb', 'hists')
            stat_filename = stat_filename.replace('_13TeV', '')

        else:
            stat_filename = opts.dc.replace('.txt', '.root')
            stat_filename = stat_filename.replace('DC_', '')

        print '\n Opening card for MC stat hists:', stat_filename
        print 'Dir name:', stat_name

        stat_file = ROOT.TFile.Open(stat_filename)
        if 'W' not in opts.bin: ROOT.gDirectory.cd(stat_name)
        stat_hists = {}

        for s in setup:
            for dir in ROOT.gDirectory.GetListOfKeys():
                #print 'dir:', dir.GetName()

                if 'W' in opts.bin:
                    wlvname = dir.GetName().replace(stat_name, '')
                    if wlvname == Dict[s]:
                        stat_hists[wlvname] = ROOT.gDirectory.Get(
                            dir.GetName()).Clone()

                if dir.GetName() == Dict[s]:
                    stat_hists[dir.GetName()] = ROOT.gDirectory.Get(
                        dir.GetName()).Clone()

        #stat_file.Close()
        print '\nStat_hists:', stat_hists

        file = ROOT.TFile.Open(opts.mlfit)
        #ROOT.gDirectory.cd('shapes_fit_s')
        #ROOT.gDirectory.cd(fit_dir)

        ROOT.gDirectory.cd(dirname)
        subdir_list = [x for x in ROOT.gDirectory.GetListOfKeys()]
        for s in setup:
            print '\ns:', s
            found = False

            #for subdir in ROOT.gDirectory.GetListOfKeys():
            for subdir in subdir_list:
                print 'subdir name is', subdir.GetName()
                print 'Dict Key is', Dict[s]
                if subdir.GetName() == Dict[s]:
                    found = True

                    # Set Histos postFit shapes and preFit errors
                    hist = rebinHist(
                        ROOT.gDirectory.Get(subdir.GetName()).Clone(), nBins,
                        xMin, xMax, dirname, subdir, prefit_error_histos,
                        postfit_error_histos, stat_hists)

                    histos.append(hist)
                    typs.append(s)

                    if s in signalList:
                        hist.SetTitle(s)
                        Overlay.append(hist)
                        #print 'the Histogram title is', hist.GetTitle()

                    break

            if not found:
                print '@ERROR: didn\'t find  the postfit histogram. Aborting'
                hist = ROOT.TH1F(Dict[s], Dict[s], nBins, xMin, xMax)
                histos.append(hist)
                typs.append(s)
                #sys.exit()
        #ROOT.gDirectory.cd('/shapes_prefit/'+dirname)
        #total = rebinHist(ROOT.gDirectory.Get('total').Clone(), Stack.nBins, Stack.xMin, Stack.xMax)
        #total.SetTitle('prefit')
        #prefit_overlay.append(total)
        break

    # Get the total pre/post fit error
    #print '\n Calculating final pre/post fit errors'
    #print prefit_error_histos
    for i, iErrorHist in enumerate(prefit_error_histos):
        #print i,iErrorHist
        if i == 0:
            temp_prefit_error = iErrorHist.Clone()
        else:
            temp_prefit_error.Add(iErrorHist)

    for i, iErrorHist in enumerate(postfit_error_histos):
        #print i,iErrorHist
        if i == 0:
            temp_postfit_error = iErrorHist.Clone()
        else:
            temp_postfit_error.Add(iErrorHist)

    final_prefit_error = ROOT.TGraphAsymmErrors(temp_prefit_error)
    final_postfit_error = ROOT.TGraphAsymmErrors(temp_postfit_error)

    total = [[]] * nBins
    errUp = [[]] * nBins
    errDown = [[]] * nBins

    total_post = [[]] * nBins
    errUp_post = [[]] * nBins
    errDown_post = [[]] * nBins

    # rebin the final errors
    for bin in range(1, nBins + 1):
        binError = temp_prefit_error.GetBinError(bin)
        total[bin - 1] = temp_prefit_error.GetBinContent(bin)
        errUp[bin - 1] = [binError]
        errDown[bin - 1] = [binError]

        binError_post = temp_postfit_error.GetBinError(bin)
        total_post[bin - 1] = temp_postfit_error.GetBinContent(bin)
        errUp_post[bin - 1] = [binError_post]
        errDown_post[bin - 1] = [binError_post]

    #Add all in quadrature
    totErrUp = [sqrt(sum([x**2 for x in bin])) for bin in errUp]
    totErrDown = [sqrt(sum([x**2 for x in bin])) for bin in errDown]

    totErrUp_post = [sqrt(sum([x**2 for x in bin])) for bin in errUp_post]
    totErrDown_post = [sqrt(sum([x**2 for x in bin])) for bin in errDown_post]

    for bin in range(1, nBins + 1):
        if not total[bin - 1] == 0:
            point = histos[0].GetXaxis().GetBinCenter(bin)
            final_prefit_error.SetPoint(bin - 1, point, 1)
            final_prefit_error.SetPointEYlow(
                bin - 1, totErrDown[bin - 1] / total[bin - 1])
            final_prefit_error.SetPointEYhigh(
                bin - 1, totErrUp[bin - 1] / total[bin - 1])

        if not total_post[bin - 1] == 0:
            point = histos[0].GetXaxis().GetBinCenter(bin)
            final_postfit_error.SetPoint(bin - 1, point, 1)
            final_postfit_error.SetPointEYlow(
                bin - 1, totErrDown_post[bin - 1] / total_post[bin - 1])
            final_postfit_error.SetPointEYhigh(
                bin - 1, totErrUp_post[bin - 1] / total_post[bin - 1])

    # =================================================
    ##### Read data
    print '\n#### Datafile is ', opts.dc
    dc_file = open(opts.dc, "r")
    os.chdir(os.path.dirname(opts.dc))
    DC = parseCard(dc_file, options)
    if not DC.hasShapes: DC.hasShapes = True
    MB = ShapeBuilder(DC, options)
    data0 = MB.getShape(opts.bin, 'data_obs')
    print data0
    if (data0.InheritsFrom("RooDataHist")):
        data0 = ROOT.RooAbsData.createHistogram(data0, 'data_obs', ws_var,
                                                theBinning)
        data0.SetName('data_obs')
    datas = [data0]
    datatyps = [None]
    datanames = [dataname]

    print '\nDATA HIST:', data0
    print 'Data name:', dataname

    # if opts.var == 'BDT':
    #     print '!!!! Blinding !!!!'

    #     if 'Zee' in dataname or 'Zuu' in dataname:
    #         for bin in range(4,datas[0].GetNbinsX()+1):
    #             datas[0].SetBinContent(bin,0)

    #     if 'Znn' in dataname:
    #         for bin in range(20,datas[0].GetNbinsX()+1):
    #             datas[0].SetBinContent(bin,0)

    #     if 'W' in dataname:
    #         for bin in range(4,datas[0].GetNbinsX()+1):
    #             #print datas[0].GetBinContent(bin,0)
    #             datas[0].SetBinContent(bin,0)

    #else:
    for bin in range(0, datas[0].GetNbinsX() + 1):
        print datas[0].GetBinContent(bin, 0)

    # =======================================================

    #if 'VV' in opts.bin:
    if isVV:
        signalList = ['VVHF', ' VVHF']

    print 'Signal List:', signalList

    #histos.append(copy(Overlay))

    if 'ZH' in signalList and 'WH' in signalList:
        #typs.append('WH')
        #if 'ZH' in Stack.setup: Stack.setup.remove('ZH')
        #if 'WH' in Stack.setup: Stack.setup.remove('WH')
        #Stack.setup.insert(0,'WH')
        #print 'Stack.setup:', Stack.setup
        typs.append('WH')
        typs.append('ZH')

    #elif 'ZH' in signalList:
    #Stack.setup.remove('WH')
    #typs.append('ggZH')
    #    typs.append('ZH')

    if 'VVb' in signalList or 'VVHF' in signalList:
        #typs.append('WH')
        typs.append('ZH')
        typs.append('VVHF')

        if 'VVHF' in Stack.setup:
            Stack.setup.remove('VVHF')
            Stack.setup.insert(0, 'VVHF')

        if 'ZH' in Stack.setup:
            Stack.setup.remove('ZH')
            Stack.setup.insert(-1, 'ZH')

        if 'WH' in Stack.setup:
            Stack.setup.remove('WH')
            Stack.setup.insert(-1, 'WH')

        if 'ggZH' in Stack.setup:
            Stack.setup.remove('ggZH')
            Stack.setup.insert(-1, 'ggZH')

    Stack.nBins = nBins
    Stack.xMin = xMin
    Stack.xMax = xMax

    print '\n-----> Stack.setup(double check)...', Stack
    print 'Post Histos:', histos
    print 'Datas:', datas
    print 'typs:', typs

    Stack.histos = histos
    Stack.typs = typs
    Stack.datas = datas
    Stack.datatyps = datatyps
    Stack.datanames = datanames
    Stack.filename = region

    #if opts.var is not 'BDT':
    #Stack.prefit_overlay = [prefit_overlay]

    #if '13TeV' in region:
    #Stack.overlay = [Overlay]
    print '\n\n\t\t Overlay: ', Stack.overlay

    # Add custom postFit errors
    #Stack.AddErrors = final_prefit_error
    Stack.AddErrors_Postfit = final_postfit_error

    if dataname == 'Wtn':
        lumi = 18300.
    Stack.lumi = lumi
    Stack.doPlot()

    print 'i am done!\n'
Ejemplo n.º 5
0
def drawFromDC():

    config = BetterConfigParser()
    config.read(opts.config)

    region = opts.region

    print "\nopts.config:", opts.config
    print "opts:", opts
    print "var:", opts.var
    print "bin:", opts.bin

    dataname = 'Zll'

    if 'Zuu' in opts.bin: dataname = 'Zuu'
    elif 'Zee' in opts.bin: dataname = 'Zee'
    elif 'Wmn' in opts.bin: dataname = 'Wmn'
    elif 'Wen' in opts.bin: dataname = 'Wen'
    elif 'Znn' in opts.bin: dataname = 'Znn'
    elif 'Wtn' in opts.bin: dataname = 'Wtn'

    if (opts.var == ''):
        var = 'BDT'
        if dataname == 'Zmm' or dataname == 'Zee': var = 'BDT_Zll'
        elif dataname == 'Wmn' or dataname == 'Wen': var = 'BDT_Wln'
        elif dataname == 'Znn': var = 'BDT_Znn'
        #if 'HighPt' in opts.bin: var = 'BDT_ZnnHighPt'
        #if 'LowPt' in opts.bin: var = 'BDT_ZnnLowPt'
        #if 'LowCSV' in opts.bin: var = 'BDT_ZnnLowCSV'
        if dataname == '' or var == 'BDT':
            raise RuntimeError, 'Did not recognise mode or var from ' + opts.bin
    else:
        var = opts.var

    if opts.var == 'BDT':
        if 'LowPt' in opts.bin: var = 'gg_plus_ZH125_low_Zpt'
        elif 'MedPt' in opts.bin: var = 'gg_plus_ZH125_med_Zpt'
        elif 'HighPt' in opts.bin: var = 'gg_plus_ZH125_high_Zpt'
        elif 'VV' in opts.bin: var = 'VV_bdt'
        else: var = 'gg_plus_ZH125_high_Zpt'

    #if 'BDT' in var:
    #    region = 'BDT'
    #else:

    region = opts.bin

    ws_var = config.get('plotDef:%s' % var, 'relPath')

    print 'ws_var:', ws_var

    if opts.var == 'BDT':
        ws_var = ROOT.RooRealVar(ws_var, ws_var, -1., 1.)
    else:
        ws_var = ROOT.RooRealVar(ws_var, ws_var, 0, 1.)

    blind = eval(config.get('Plot:%s' % region, 'blind'))
    #blind = True

    # If Zvv change region to relevant CR
    if 'Znn' in region:
        if 'QCD' in opts.dc:
            region = 'Zvv_QCD'
        if 'TT' in opts.dc:
            region = 'Zvv_TT'
        if 'Zbb' in opts.dc:
            region = 'Zvv_Zbb'
        if 'Zlight' in opts.dc:
            region = 'Zvv_Zlight'

    print 'config:', config
    print 'var: ', var
    print 'region: ', region

    Stack = StackMaker(config, var, region, True)

    if 'low' in opts.bin or 'ch1_Wenu' == opts.bin or 'ch2_Wmunu' == opts.bin:
        Stack.addFlag2 = 'Low p_{T}(V)'
    elif 'MedPt' in opts.bin or 'ch1_Wenu2' == opts.bin or 'ch2_Wmunu2' == opts.bin:
        Stack.addFlag2 = 'Intermediate p_{T}(V)'
    elif 'high' in opts.bin or 'ch1_Wenu3' == opts.bin or 'ch2_Wmunu3' == opts.bin:
        Stack.addFlag2 = 'High p_{T}(V)'

    # check for pre or post fit options
    preFit = False
    addName = 'PostFit_%s' % (opts.fit)
    if not opts.mlfit:
        addName = 'PreFit'
        preFit = True

    print '\n-----> Fit Type(opts.fit)  : ', opts.fit
    print '               (opts.mlfit): ', opts.mlfit
    print '               preFit      : ', preFit
    print '               opts.bin    : ', opts.bin

    Stack.options['pdfName'] = '%s_%s_%s.pdf' % (var, opts.bin, addName)

    log = eval(config.get('Plot:%s' % region, 'log'))

    if 'Zll' in opts.bin or 'Zee' in opts.bin or 'Zuu' in opts.bin or 'minCMVA' in opts.var or 'Zmm' in opts.bin:
        #setup = config.get('Plot_general','setup').split(',')
        setup = [
            'ZH', 'ggZH', 'DY2b', 'DY1b', 'DYlight', 'TT', 'VVHF', 'VVLF', 'ST'
        ]
        signalList = ['ZH']

        #channel = 'ZllHbb'

        # For my own fits
        channel = ''

        if 'Zee' in opts.bin: lep_channel = 'Zee'
        elif 'Zuu' in opts.bin: lep_channel = 'Zmm'

        region_dic = {
            'BDT': 'SIG',
            ' Zlf': 'Zlf',
            'Zhf': 'Zhf',
            'TT': 'TT',
            '13TeV': 'SIG'
        }
        region_name = [
            region_dic[key] for key in region_dic if (key in opts.bin)
        ]

        if 'minCMVA' not in opts.var:
            region_name = region_name[0]
        else:
            if 'Zlf' in opts.bin: region_name = 'Zlf'
            if 'Zhf' in opts.bin: region_name = 'Zhf'
            if 'ttbar' in opts.bin: region_name = 'TT'

        pt_region_dic = {
            'lowpt': 'low',
            'highpt': 'high',
            'LowPt': 'low',
            'HighPt': 'high'
        }
        pt_region_name = [
            pt_region_dic[key] for key in pt_region_dic if (key in opts.bin)
        ]
        #pt_region_name = pt_region_name[0]
        if 'low' in opts.bin: pt_region_name = 'low'
        if 'high' in opts.bin: pt_region_name = 'high'

        print 'region_name is', region_name
        print 'pt region_name is', pt_region_name

    if 'Wmn' in opts.bin or 'Wen' in opts.bin:
        setup = [
            'WH', 'ZH', 'DY2b', 'DY1b', 'DYlight', 'TT', 'VVHF', 'VVLF', 'ST',
            'Wj0b', 'Wj1b', 'Wj2b'
        ]
        signalList = ['ZH', 'WH']

    if 'Znn' in opts.bin:
        setup = [
            'ZH', 'ggZH', 'DY2b', 'DY1b', 'DYlight', 'TT', 'VVHF', 'ST', 'WH',
            'Wj0b', 'Wj1b', 'Wj2b'
        ]
        signalList = ['ZH']

        lep_channel = 'Znn'
        pt_region_name = 'HighPt'
        if opts.var == 'BDT':
            region_name = ''
            region_type = 'SR'

        print 'region_name is', region_name
        print 'region_type is', region_type
        print 'pt region_name is', pt_region_name
        print 'Lepton channel:', lep_channel

    if dataname == 'Zmm' or dataname == 'Zee':
        try:
            setup.remove('W1b')
            setup.remove('W2b')
            setup.remove('Wlight')
            setup.remove('WH')
        except:
            print '@INFO: Wb / Wligh / WH not present in the datacard'
    if not dataname == 'Znn' and 'QCD' in setup:
        setup.remove('QCD')

    Stack.setup = setup

    Dict = eval(config.get('LimitGeneral', 'Dict'))
    lumi = eval(config.get('Plot_general', 'lumi'))

    options = copy(opts)
    options.dataname = "data_obs"
    options.mass = 0
    options.format = "%8.3f +/- %6.3f"
    options.channel = opts.bin
    options.excludeSyst = []
    options.norm = False
    options.stat = False
    options.bin = True  # fake that is a binary output, so that we parse shape lines
    options.out = "tmp.root"
    options.fileName = args[0]
    options.filename = region
    options.cexpr = False
    options.fixpars = False
    options.libs = []
    options.verbose = 0
    options.poisson = 0
    options.nuisancesToExclude = []
    options.noJMax = None

    theBinning = ROOT.RooFit.Binning(Stack.nBins, Stack.xMin, Stack.xMax)

    if 'Wmn' in opts.bin or 'Wen' in opts.bin or 'Znn' in opts.bin:  # SET: WLV MINCSV BINNING
        if 'CSV' in opts.var:
            Stack.nBins = 15
            print '\n\t Changing Wlv/Zvv CSV bins to ', 15
            theBinning = ROOT.RooFit.Binning(15, Stack.xMin, Stack.xMax)

    print '/n----> The Binning:'
    print 'nBins:', Stack.nBins
    print 'xMin:', Stack.xMin
    print 'xMax:', Stack.xMax

    error_histos = []
    histos = []
    typs = []
    shapes = {}
    shapesUp = [[] for _ in range(0, len(setup))]
    shapesDown = [[] for _ in range(0, len(setup))]

    sigCount = 0
    Overlay = []
    prefit_overlay = []

    dirname = ''
    ####
    #Open the mlfit.root and retrieve the mc
    file = ROOT.TFile.Open(opts.mlfit)
    if file == None: raise RuntimeError, "Cannot open file %s" % opts.mlfit
    print '\n\n-----> Fit File: ', file

    if not ROOT.gDirectory.cd('shapes_fit_s'):
        print '@ERROR: didn\'t find the shapes_fit_s directory. Aborting'
        sys.exit()

    for dir in ROOT.gDirectory.GetListOfKeys():

        dirinfo = dir.GetName().split('_')

        print 'dirinfo:', dirinfo

        if 'Znn' in opts.bin and 'BDT' in opts.var:
            print 'lepton channel, pt_region_name, region_type:', dirinfo[
                0], dirinfo[1], dirinfo[2]
            if not (dirinfo[0] == lep_channel and dirinfo[1] == pt_region_name
                    and dirinfo[2] == region_type):
                continue

        elif len(dirinfo) == 5:
            print 'channel, lepton channel, region_name, pt_region_name:', dirinfo[
                0], dirinfo[1], dirinfo[2], dirinfo[3], dirinfo[4]
            if not (dirinfo[0] == channel and dirinfo[2] == lep_channel
                    and dirinfo[3] == region_name
                    and dirinfo[4] == pt_region_name):
                continue

        elif len(dirinfo) == 4:
            print 'channel, lepton channel, region_name, pt_region_name:', dirinfo[
                0], dirinfo[1], dirinfo[2], dirinfo[3]
            if not (dirinfo[1] == lep_channel and dirinfo[2] == region_name
                    and dirinfo[3] == pt_region_name):
                continue

        print 'Directory:', dir.GetName()

        dirname = dir.GetName()

        ROOT.gDirectory.cd(dirname)
        for s in setup:
            found = False
            for subdir in ROOT.gDirectory.GetListOfKeys():
                print 'subdir name is', subdir.GetName()
                if subdir.GetName() == Dict[s]:
                    found = True

                    # Set Histos postFit shapes and preFit errors
                    hist = rebinHist(
                        ROOT.gDirectory.Get(subdir.GetName()).Clone(),
                        Stack.nBins, Stack.xMin, Stack.xMax)

                    histos.append(hist)
                    #error_histos.append(PostFit_Errors)
                    typs.append(s)
                    print 's is', s
                    print 'signalList is', signalList
                    if s in signalList:
                        hist.SetTitle(s)
                        Overlay.append(hist)
                        print 'the Histogram title is', hist.GetTitle()

            if not found:
                print '@ERROR: didn\'t find  the postfit histogram. Aborting'
                sys.exit()
        ROOT.gDirectory.cd('/shapes_prefit/' + dirname)
        total = rebinHist(
            ROOT.gDirectory.Get('total').Clone(), Stack.nBins, Stack.xMin,
            Stack.xMax)
        total.SetTitle('prefit')
        prefit_overlay.append(total)
        break

    # =================================================
    ##### Read data
    print 'file is ', opts.dc
    dc_file = open(opts.dc, "r")
    os.chdir(os.path.dirname(opts.dc))
    DC = parseCard(dc_file, options)
    if not DC.hasShapes: DC.hasShapes = True
    MB = ShapeBuilder(DC, options)
    data0 = MB.getShape(opts.bin, 'data_obs')
    if (data0.InheritsFrom("RooDataHist")):
        data0 = ROOT.RooAbsData.createHistogram(data0, 'data_obs', ws_var,
                                                theBinning)
        data0.SetName('data_obs')
    datas = [data0]
    datatyps = [None]
    datanames = [dataname]

    print '\nDATA HIST:', data0
    print 'Data name:', dataname

    if opts.var == 'BDT':
        for bin in range(11, datas[0].GetNbinsX() + 1):
            datas[0].SetBinContent(bin, 0)
    # =======================================================

    #if 'VV' in opts.bin:
    if isVV:
        signalList = ['VVHF', ' VVHF']

    print 'Signal List:', signalList

    #histos.append(copy(Overlay))

    if 'ZH' in signalList and 'WH' in signalList:
        #typs.append('WH')
        #if 'ZH' in Stack.setup: Stack.setup.remove('ZH')
        #if 'WH' in Stack.setup: Stack.setup.remove('WH')
        #Stack.setup.insert(0,'WH')
        #print 'Stack.setup:', Stack.setup
        typs.append('WH')
        typs.append('ZH')

    #elif 'ZH' in signalList:
    #Stack.setup.remove('WH')
    #typs.append('ggZH')
    #    typs.append('ZH')

    if 'VVb' in signalList or 'VVHF' in signalList:
        #typs.append('WH')
        typs.append('ZH')
        typs.append('VVHF')

        if 'VVHF' in Stack.setup:
            Stack.setup.remove('VVHF')
            Stack.setup.insert(0, 'VVHF')

        if 'ZH' in Stack.setup:
            Stack.setup.remove('ZH')
            Stack.setup.insert(-1, 'ZH')

        if 'WH' in Stack.setup:
            Stack.setup.remove('WH')
            Stack.setup.insert(-1, 'WH')

        if 'ggZH' in Stack.setup:
            Stack.setup.remove('ggZH')
            Stack.setup.insert(-1, 'ggZH')

    print '\n-----> Stack.setup(double check)...', Stack
    print 'Post Histos:', histos
    print 'Datas:', datas
    print 'typs:', typs

    Stack.histos = histos
    Stack.typs = typs
    Stack.datas = datas
    Stack.datatyps = datatyps
    Stack.datanames = datanames
    Stack.filename = region

    #if opts.var is not 'BDT':
    #Stack.prefit_overlay = [prefit_overlay]

    #if '13TeV' in region:
    #Stack.overlay = [Overlay]
    print '\n\n\t\t Overlay: ', Stack.overlay

    # Add custom postFit errors
    #Stack.AddErrors = theErrorGraph

    if dataname == 'Wtn':
        lumi = 18300.
    Stack.lumi = lumi
    Stack.doPlot()

    print 'i am done!\n'