Exemplo n.º 1
0
def PurityHistograms(datasetsMgr, qcdDatasetName):
    '''
    '''
    Verbose("Plotting Purity Histograms")

    # Definitions
    histoNames  = []
    saveFormats = [".png"] #[".C", ".png", ".eps"]
    dataPath    = "ForDataDrivenCtrlPlots"
    histoList   = datasetsMgr.getDataset(qcdDatasetName).getDirectoryContent(dataPath)
    histoList   = [h for h in histoList if "_Purity" in h]
    # histoList   = [h for h in histoList if "_MCEWK" in h]
    histoPaths  = [dataPath+"/"+h for h in histoList]
    histoKwargs = GetHistoKwargs(histoPaths, opts)

    # For-loop: All histograms in list
    for histoName in histoPaths:
        if "_Vs_" in histoName:
            continue

        if "subldg" in histoName.lower():
            continue

        kwargs_  = histoKwargs[histoName]
        saveName = histoName.replace("/", "_")

        # Create the plotting object
        p = plots.MCPlot(datasetsMgr, histoName, normalizeToLumi=True, saveFormats=[])
        p.setLuminosity(GetLumi(datasetsMgr))

        # Apply QCD data-driven style
        p.histoMgr.forHisto(qcdDatasetName, styles.getQCDLineStyle())
        p.histoMgr.setHistoDrawStyle(qcdDatasetName, "AP")
        p.histoMgr.setHistoLegendStyle(qcdDatasetName, "LP")
        p.histoMgr.setHistoLegendLabelMany({
                #qcdDatasetName: "QCD (Data)",
                qcdDatasetName: "QCD",
                })

        # Draw and save the plot
        plots.drawPlot(p, saveName, **kwargs_) #the "**" unpacks the kwargs_ dictionary
        SavePlot(p, saveName, os.path.join(opts.saveDir, "", opts.optMode) )
    return
def PlotMC(datasetsMgr, histo, intLumi):

    kwargs = {}
    if opts.normaliseToOne:
        p = plots.MCPlot(datasetsMgr, histo, normalizeToOne=True, saveFormats=[], **kwargs)
    else:
        p = plots.MCPlot(datasetsMgr, histo, normalizeToLumi=intLumi, saveFormats=[], **kwargs)

    # Draw the histograms
    _cutBox = None
    _rebinX = 1
    _format = "%0.0f"
    _xlabel = None

    _opts   = {"ymin": 1e-3, "ymaxfactor": 1.0}
    #_opts   = {"ymin": 1e-3, "ymax": 60}


    if "pt" in histo.lower():
        _rebinX = 2
        _units  = "GeV/c"
        _format = "%0.0f " + _units
        _xlabel = "p_{T} (%s)" % _units
        _cutBox = {"cutValue": 173.21, "fillColor": 16, "box": False, "line": False, "greaterThan": True}
        #_opts["xmax"] = 505 #1005

    if "ht" in histo.lower():
        _rebinX = 2
        _units  = "GeV/c"
        _format = "%0.0f " + _units
        _xlabel = "H_{T} (%s)" % _units
        _opts["xmax"] = 2000
        #_cutBox = {"cutValue": 173.21, "fillColor": 16, "box": False, "line": False, "greaterThan": True}
        #_opts["xmax"] = 505 #1005
        
    if "eta" in histo.lower():
        _units  = ""
        _format = "%0.1f " + _units
        _xlabel = "#eta %s" % _units
        _cutBox = {"cutValue": 173.21, "fillColor": 16, "box": False, "line": False, "greaterThan": True}
        _opts["xmax"] = 2.5
        _opts["xmin"] = -2.5

    if "trijetmass" in histo.lower():
        _rebinX = 2
        _units  = "GeV/c^{2}"
        _format = "%0.0f " + _units
        _xlabel = "m_{jjb} (%s)" % _units
        _cutBox = {"cutValue": 173.21, "fillColor": 16, "box": False, "line": True, "greaterThan": True}
        _opts["xmax"] = 505 #1005
        if "ldg" in histo.lower():
            _xlabel = "m_{jjb}^{ldg} (%s)" % _units
    elif "tetrajetmass" in histo.lower():
        _rebinX = 10 #5 #10 #4
        _units  = "GeV/c^{2}"
        _format = "%0.0f " + _units
        _xlabel = "m_{jjbb} (%s)" % (_units)
        _format = "%0.0f " + _units
        _opts["xmax"] = 2500 #3500.0

    elif "tetrajetpt" in histo.lower():
        _rebinX = 2 #5 #10 #4
        _units  = "GeV/c"
        _format = "%0.0f " + _units
        _xlabel = "p_{T,jjbb} (%s)" % (_units)
        _format = "%0.0f " + _units
        _opts["xmax"] = 1000 #3500.0

    elif "dijetmass" in histo.lower():
        _rebinX = 2 #5 #10 #4
        _units  = "GeV/c^{2}"
        _format = "%0.0f " + _units
        _xlabel = "m_{jj} (%s)" % (_units)
        _format = "%0.0f " + _units
        _opts["xmax"] = 800 #3500.0
        _cutBox = {"cutValue": 80.4, "fillColor": 16, "box": False, "line": True, "greaterThan": True}
        _opts["xmax"] = 300
        if "ldg" in histo.lower():
            _xlabel = "m_{jj}^{ldg} (%s)" % (_units)
    elif "tetrajetbjetpt" in histo.lower():
        _rebinX = 2
        _units  = "GeV/c"
        _format = "%0.0f " + _units
        _xlabel = "p_{T,bjet}  (%s)" % (_units)
        _format = "%0.0f " + _units
        _opts["xmax"] = 800

    elif "ldgtrijetpt" in histo.lower():
        _rebinX = 2
#        logY    = False
        _units  = "GeV/c"
        _format = "%0.0f " + _units
        _xlabel = "p_{T,jjb}^{ldg}  (%s)" % (_units)
        _format = "%0.0f " + _units
        _opts["xmax"] = 800

    elif "ldgtrijetdijetpt" in histo.lower():
        _rebinX = 2
#        logY    = False
        _units  = "GeV/c"
        _format = "%0.0f " + _units
        _xlabel = "p_{T,jj}^{ldg}  (%s)" % (_units)
        _format = "%0.0f " + _units
        _opts["xmax"] = 800


    elif "topbdt_" in histo.lower():
        _format = "%0.1f"
        _cutBox = {"cutValue": +0.40, "fillColor": 16, "box": False, "line": False, "greaterThan": True}
        _xlabel = "BDTG discriminant"
            
    if "gentop_pt" in histo.lower():
        _rebinX = 1
    if "genquark_pt" in histo.lower():
        _rebinX = 1
        _opts["xmax"] = 500
    else:
        pass

    if "delta" in histo.lower():
        _format = "%0.1f "
        if "phi" in histo.lower():
            _xlabel = "#Delta #phi"
        if "deltar" in histo.lower():
            _rebinX = 2

    if "DiBjetMaxMass_Mass" in histo:
        _units = "GeV"
        _rebinX = 4
        _xlabel = "m_{max}(bb) (%s)" % (_units)
        _format = "%0.0f " + _units
        
    if "DiJetDeltaRmin_Mass" in histo:
        _units = "GeV"
        _format = "%0.0f " + _units
        _xlabel = "m_{#DeltaR_{min}(jj)}(jj) (%s)" % (_units)
        _opts["xmax"] = 200

    if "DiBjetDeltaRmin_Mass" in histo:
        _units = "GeV"
        _format = "%0.0f " + _units
        _rebinX= 2
        _xlabel = "m_{#DeltaR_{min}(bb)}(bb) (%s)" % (_units)
        _opts["xmax"] = 600

    if "LdgBjet_SubldgBjet_Mass" in histo:
        _units = "GeV"
        _format = "%0.0f " + _units
        _rebinX= 4
        _xlabel = "m(b_{ldg}b_{sldg}) (%s)" % (_units)
        
    if "DeltaR_LdgTop_DiBjetDeltaRmin" in histo:
        _rebinX= 2        
        _format = "%0.1f "
        _xlabel = "#DeltaR (top_{ldg}, bb_{#Delta Rmin})"

    if "DeltaR_SubldgTop_DiBjetDeltaRmin" in histo:
        _rebinX= 2        
        _format = "%0.1f "
        _xlabel = "#DeltaR (top_{sldg}, bb_{#Delta Rmin})"

    if "over" in histo.lower():        
        _opts["xmax"] = 15
        _opts["xmax"] = +0.8
        _opts["xmin"] = -0.8
        _xlabel = "#Delta p_{T}(j-q)/p_{T,q}"
        _cutBox = {"cutValue": +0.32, "fillColor": 16, "box": False, "line": True, "greaterThan": True}

    if "within" in histo.lower():
        _opts["xmax"] = +0.8
        _opts["xmin"] = -0.8
        _xlabel = "#Delta p_{T}(j-q)/p_{T,q}"
        _cutBox = {"cutValue": +0.32, "fillColor": 16, "box": False, "line": True, "greaterThan": True}

    if "axis2" in histo.lower():
        _opts["xmax"] = +0.2
        _opts["xmin"] = 0.0
        _xlabel = "axis2"
        _units = ""
        _format = "%0.1f "+_units

    if "axis2" in histo.lower():
        _opts["xmax"] = +0.2
        _opts["xmin"] = 0.0
        _xlabel = "axis2"
        _units  = ""
        _format = "%0.2f "+_units

    if "mass" in histo.lower():
        _xlabel = "M (GeV/c^{2})"
        _units  = "GeV/c^{2}"
        _format = "%0.0f "+_units

    if "mult" in histo.lower():
        _xlabel = "mult"
        _units  = ""
        _format = "%0.0f "+_units

    if "BQuarkFromH_Pt" in histo:
        _opts["xmax"] = 200
        _rebinX= 1

    if "order" in histo.lower():
        _opts["xmax"] = 15
        _rebinX= 1
        if "pt" in histo.lower():
            _xlabel = "indx_{p_{T}}"
        if "csv" in histo.lower():
            _xlabel = "indx_{csv}"

    if "phi_alpha" in histo.lower() or "phi_beta" in histo.lower() or "r_alpha" in histo.lower() or "r_beta" in histo.lower():
        _format = "%0.1f "
        _opts["xmin"] = 0.0
        if "phi" in histo.lower():
            _xlabel = "#phi"
            _opts["xmax"] = 5.5
        else:
            _xlabel = "r"
            _opts["xmax"] = 6.5
        if "alpha" in histo.lower():
            _xlabel = _xlabel+"_{#alpha}"
        else:
            _xlabel = _xlabel+"_{#beta}"
    if opts.normaliseToOne:
        logY    = False
        Ylabel  = "Arbitrary Units / %s" % (_format)
    else:
        logY    = True
        Ylabel  = "Events / %s" % (_format)

    if logY:
        yMaxFactor = 2.0
    else:
        yMaxFactor = 1.2

    _opts["ymaxfactor"] = yMaxFactor
    if opts.normaliseToOne:
        _opts["ymin"] = 1e-3
        #_opts   = {"ymin": 1e-3, "ymaxfactor": yMaxFactor, "xmax": None}
    else:
        _opts["ymin"] = 1e0
        if "order" in histo.lower():
            _opts["ymin"] = 1e-3
        #_opts["ymaxfactor"] = yMaxFactor
        #_opts   = {"ymin": 1e0, "ymaxfactor": yMaxFactor, "xmax": None}

    # Customise styling
    p.histoMgr.forEachHisto(lambda h: h.getRootHisto().SetLineStyle(ROOT.kSolid))

    if "QCD" in datasetsMgr.getAllDatasetNames():
        p.histoMgr.forHisto("QCD", styles.getQCDLineStyle()) #getQCDFillStyle() )
        p.histoMgr.setHistoDrawStyle("QCD", "HIST")
        p.histoMgr.setHistoLegendStyle("QCD", "F")

    if "TT" in datasetsMgr.getAllDatasetNames():
        TTStyle           = styles.StyleCompound([styles.StyleFill(fillColor=ROOT.kMagenta-2), styles.StyleLine(lineColor=ROOT.kMagenta-2, lineStyle=ROOT.kSolid, lineWidth=3),
                                                  styles.StyleMarker(markerSize=1.2, markerColor=ROOT.kMagenta-2, markerSizes=None, markerStyle=ROOT.kFullTriangleUp)])
        p.histoMgr.forHisto("TT", TTStyle)
        p.histoMgr.setHistoDrawStyle("TT", "HIST") #AP
        p.histoMgr.setHistoLegendStyle("TT", "F")  #LP
        
        
    # Customise style
    signalM = []
    for m in signalMass:
        signalM.append(m.rsplit("M_")[-1])
    for m in signalM:
        p.histoMgr.forHisto("ChargedHiggs_HplusTB_HplusToTB_M_%s" %m, styles.getSignalStyleHToTB_M(m))
        

    plots.drawPlot(p, 
                   histo,  
                   xlabel       = _xlabel,
                   ylabel       = Ylabel,
                   log          = logY,
                   rebinX       = _rebinX, cmsExtraText = "Preliminary", 
                   #createLegend = {"x1": 0.59, "y1": 0.65, "x2": 0.92, "y2": 0.92},
                   createLegend = {"x1": 0.59, "y1": 0.70, "x2": 0.92, "y2": 0.92},
                   #createLegend = {"x1": 0.73, "y1": 0.85, "x2": 0.97, "y2": 0.77},
                   opts         = _opts,
                   opts2        = {"ymin": 0.6, "ymax": 1.4},
                   cutBox       = _cutBox,
                   )

    # Save plot in all formats    
    saveName = histo.split("/")[-1]
    savePath = os.path.join(opts.saveDir, histo.split("/")[0], opts.optMode)

    '''
    if opts.normaliseToOne:
        save_path = savePath + opts.MVAcut
        if opts.noQCD:
            save_path = savePath + opts.MVAcut + "/noQCD/"
    else:
        save_path = savePath + opts.MVAcut + "/normToLumi/"
        if opts.noQCD:
            save_path = savePath + opts.MVAcut + "/noQCD/"
     '''
    
    SavePlot(p, saveName, savePath) 

    return
def main(opts):

    # Apply TDR style
    style = tdrstyle.TDRStyle()
    style.setGridX(False)
    style.setGridY(False)

    # If user does not define optimisation mode do all of them
    if opts.optMode == None:
        if len(optList) < 1:
            optList.append("")
        else:
            pass
        optModes = optList
    else:
        optModes = [opts.optMode]

    # For-loop: All optimisation modes
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities()  # from lumi.json
        datasetsMgr40 = GetDatasetsFromDir_second(
            opts, opts.mcrab.replace("_BDT85", "_BDT40"))
        datasetsMgr40.updateNAllEventsToPUWeighted()
        datasetsMgr40.loadLuminosities()  # from lumi.json

        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()

        # Set/Overwrite cross-sections
        for datasetsMgr_ in [datasetsMgr, datasetsMgr40]:
            for d in datasetsMgr_.getAllDatasets():
                if "ChargedHiggs" in d.getName():
                    datasetsMgr_.getDataset(d.getName()).setCrossSection(1.0)

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py)
        if 0:
            plots.mergeRenameReorderForDataMC(datasetsMgr)

        # Print dataset information before removing anything?
        if 0:
            datasetsMgr.PrintInfo()

        # Determine integrated Lumi before removing data
        if "Data" in datasetsMgr.getAllDatasetNames():
            intLumi = datasetsMgr.getDataset("Data").getLuminosity()
        else:
            intLumi = 35920

        # Remove datasets
        filterKeys = ["Data", "TTZToQQ", "TTWJets", "TTTT"]
        #filterKeys = ["Data", "TTZToQQ", "TTWJets", "TTTT"]
        for key in filterKeys:
            datasetsMgr.remove(
                filter(lambda name: key in name,
                       datasetsMgr.getAllDatasetNames()))
            datasetsMgr40.remove(
                filter(lambda name: key in name,
                       datasetsMgr40.getAllDatasetNames()))
        # Re-order datasets
        datasetOrder = []
        for d in datasetsMgr.getAllDatasets():
            datasetOrder.append(d.getName())

        # Select and re-order
        datasetsMgr.selectAndReorder(datasetOrder)
        datasetsMgr40.selectAndReorder(datasetOrder)

        # Print dataset information
        datasetsMgr.PrintInfo()

        # QCD multijet
        datasetsMgr.merge("QCD", GetListOfQCDatasets())
        datasetsMgr40.merge("QCD", GetListOfQCDatasets())
        plots._plotStyles["QCD"] = styles.getQCDLineStyle()

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py)
        plots.mergeRenameReorderForDataMC(datasetsMgr)

        # For-loop: All numerator-denominator pairs
        PlotEfficiency(datasetsMgr, datasetsMgr40, intLumi)
    return
def PlotMC(datasetsMgr, histo, intLumi):

    kwargs = {}
    if opts.normaliseToOne:
        p = plots.MCPlot(datasetsMgr, histo, normalizeToOne=True, saveFormats=[], **kwargs)
    else:
        p = plots.MCPlot(datasetsMgr, histo, normalizeToLumi=intLumi, saveFormats=[], **kwargs)

    # Draw the histograms
    _cutBox = None
    _rebinX = 1
    _format = "%0.0f"
    _xlabel = None

    _opts   = {"ymin": 1e-3, "ymaxfactor": 1.0}
    #_opts   = {"ymin": 1e-3, "ymax": 60}


    if "pt" in histo.lower():
        _rebinX = 2
        _units  = "GeV/c"
        _format = "%0.0f " + _units
        _xlabel = "p_{T} (%s)" % _units
        _cutBox = {"cutValue": 173.21, "fillColor": 16, "box": False, "line": False, "greaterThan": True}
        #_opts["xmax"] = 505 #1005
        
    if "eta" in histo.lower():
        _units  = ""
        _format = "%0.1f " + _units
        _xlabel = "#eta %s" % _units
        _cutBox = {"cutValue": 173.21, "fillColor": 16, "box": False, "line": False, "greaterThan": True}
        _opts["xmax"] = 2.5
        _opts["xmin"] = -2.5

    if "trijetmass" in histo.lower():
        _rebinX = 2
        _units  = "GeV/c^{2}"
        _format = "%0.0f " + _units
        _xlabel = "m_{jjb} (%s)" % _units
        _cutBox = {"cutValue": 173.21, "fillColor": 16, "box": False, "line": True, "greaterThan": True}
        _opts["xmax"] = 505 #1005
        if "ldg" in histo.lower():
            _xlabel = "m_{jjb}^{ldg} (%s)" % _units
    elif "tetrajetmass" in histo.lower():
        _rebinX = 5 #5 #10 #4
        _units  = "GeV/c^{2}"
        _format = "%0.0f " + _units
        _xlabel = "m_{jjbb} (%s)" % (_units)
        _format = "%0.0f " + _units
        _opts["xmax"] = 2500 #3500.0

    elif "tetrajetpt" in histo.lower():
        _rebinX = 2 #5 #10 #4
        _units  = "GeV/c"
        _format = "%0.0f " + _units
        _xlabel = "p_{T,jjbb} (%s)" % (_units)
        _format = "%0.0f " + _units
        _opts["xmax"] = 1000 #3500.0

    elif "dijetmass" in histo.lower():
        _rebinX = 2 #5 #10 #4
        _units  = "GeV/c^{2}"
        _format = "%0.0f " + _units
        _xlabel = "m_{jj} (%s)" % (_units)
        _format = "%0.0f " + _units
        _opts["xmax"] = 800 #3500.0
        _cutBox = {"cutValue": 80.4, "fillColor": 16, "box": False, "line": True, "greaterThan": True}
        _opts["xmax"] = 300
        if "ldg" in histo.lower():
            _xlabel = "m_{jj}^{ldg} (%s)" % (_units)
    elif "tetrajetbjetpt" in histo.lower():
        _rebinX = 2
        _units  = "GeV/c"
        _format = "%0.0f " + _units
        _xlabel = "p_{T,bjet}  (%s)" % (_units)
        _format = "%0.0f " + _units
        _opts["xmax"] = 800

    elif "ldgtrijetpt" in histo.lower():
        _rebinX = 2
#        logY    = False
        _units  = "GeV/c"
        _format = "%0.0f " + _units
        _xlabel = "p_{T,jjb}^{ldg}  (%s)" % (_units)
        _format = "%0.0f " + _units
        _opts["xmax"] = 800

    elif "ldgtrijetdijetpt" in histo.lower():
        _rebinX = 2
#        logY    = False
        _units  = "GeV/c"
        _format = "%0.0f " + _units
        _xlabel = "p_{T,jj}^{ldg}  (%s)" % (_units)
        _format = "%0.0f " + _units
        _opts["xmax"] = 800


    elif "topbdt_" in histo.lower():
        _format = "%0.1f"
        _cutBox = {"cutValue": +0.40, "fillColor": 16, "box": False, "line": False, "greaterThan": True}
        _xlabel = "BDTG discriminant"
            
    if "gentop_pt" in histo.lower():
        _rebinX = 1
    if "genquark_pt" in histo.lower():
        _rebinX = 1
        _opts["xmax"] = 500
    else:
        pass

    if "delta" in histo.lower():
        _format = "%0.1f "
        if "phi" in histo.lower():
            _xlabel = "#Delta #phi"
        if "deltar" in histo.lower():
            _rebinX = 2

    if "DiBjetMaxMass_Mass" in histo:
        _units = "GeV"
        _rebinX = 4
        _xlabel = "m_{max}(bb) (%s)" % (_units)
        _format = "%0.0f " + _units
        
    if "DiJetDeltaRmin_Mass" in histo:
        _units = "GeV"
        _format = "%0.0f " + _units
        _xlabel = "m_{#DeltaR_{min}(jj)}(jj) (%s)" % (_units)
        _opts["xmax"] = 200

    if "DiBjetDeltaRmin_Mass" in histo:
        _units = "GeV"
        _format = "%0.0f " + _units
        _rebinX= 2
        _xlabel = "m_{#DeltaR_{min}(bb)}(bb) (%s)" % (_units)
        _opts["xmax"] = 600

    if "LdgBjet_SubldgBjet_Mass" in histo:
        _units = "GeV"
        _format = "%0.0f " + _units
        _rebinX= 4
        _xlabel = "m(b_{ldg}b_{sldg}) (%s)" % (_units)
        
    if "DeltaR_LdgTop_DiBjetDeltaRmin" in histo:
        _rebinX= 2        
        _format = "%0.1f "
        _xlabel = "#DeltaR (top_{ldg}, bb_{#Delta Rmin})"

    if "DeltaR_SubldgTop_DiBjetDeltaRmin" in histo:
        _rebinX= 2        
        _format = "%0.1f "
        _xlabel = "#DeltaR (top_{sldg}, bb_{#Delta Rmin})"


    if "phi_alpha" in histo.lower() or "phi_beta" in histo.lower() or "r_alpha" in histo.lower() or "r_beta" in histo.lower():
        _format = "%0.1f "
        _opts["xmin"] = 0.0
        if "phi" in histo.lower():
            _xlabel = "#phi"
            _opts["xmax"] = 5.5
        else:
            _xlabel = "r"
            _opts["xmax"] = 6.5
        if "alpha" in histo.lower():
            _xlabel = _xlabel+"_{#alpha}"
        else:
            _xlabel = _xlabel+"_{#beta}"
    if opts.normaliseToOne:
        logY    = False
        Ylabel  = "Arbitrary Units / %s" % (_format)
    else:
        logY    = True
        Ylabel  = "Events / %s" % (_format)

    if logY:
        yMaxFactor = 2.0
    else:
        yMaxFactor = 1.2

    _opts["ymaxfactor"] = yMaxFactor
    if opts.normaliseToOne:
        _opts["ymin"] = 1e-3
        #_opts   = {"ymin": 1e-3, "ymaxfactor": yMaxFactor, "xmax": None}
    else:
        _opts["ymin"] = 1e0
        #_opts["ymaxfactor"] = yMaxFactor
        #_opts   = {"ymin": 1e0, "ymaxfactor": yMaxFactor, "xmax": None}

    # Customise styling
    p.histoMgr.forEachHisto(lambda h: h.getRootHisto().SetLineStyle(ROOT.kSolid))

    if "QCD" in datasetsMgr.getAllDatasetNames():
        p.histoMgr.forHisto("QCD", styles.getQCDLineStyle()) #getQCDFillStyle() )
        p.histoMgr.setHistoDrawStyle("QCD", "HIST")
        p.histoMgr.setHistoLegendStyle("QCD", "F")

    if "TT" in datasetsMgr.getAllDatasetNames():
        TTStyle           = styles.StyleCompound([styles.StyleFill(fillColor=ROOT.kMagenta-2), styles.StyleLine(lineColor=ROOT.kMagenta-2, lineStyle=ROOT.kSolid, lineWidth=3),
                                                  styles.StyleMarker(markerSize=1.2, markerColor=ROOT.kMagenta-2, markerSizes=None, markerStyle=ROOT.kFullTriangleUp)])
        p.histoMgr.forHisto("TT", TTStyle)
        p.histoMgr.setHistoDrawStyle("TT", "HIST") #AP
        p.histoMgr.setHistoLegendStyle("TT", "F")  #LP
        
        
    # Customise style
    signalM = []
    for m in signalMass:
        signalM.append(m.rsplit("M_")[-1])
    for m in signalM:
        p.histoMgr.forHisto("ChargedHiggs_HplusTB_HplusToTB_M_%s" %m, styles.getSignalStyleHToTB_M(m))
        

    plots.drawPlot(p, 
                   histo,  
                   xlabel       = _xlabel,
                   ylabel       = Ylabel,
                   log          = logY,
                   rebinX       = _rebinX, cmsExtraText = "Preliminary", 
                   #createLegend = {"x1": 0.59, "y1": 0.65, "x2": 0.92, "y2": 0.92},
                   createLegend = {"x1": 0.59, "y1": 0.70, "x2": 0.92, "y2": 0.92},
                   #createLegend = {"x1": 0.73, "y1": 0.85, "x2": 0.97, "y2": 0.77},
                   opts         = _opts,
                   opts2        = {"ymin": 0.6, "ymax": 1.4},
                   cutBox       = _cutBox,
                   )

    # Save plot in all formats    
    saveName = histo.split("/")[-1]
    savePath = os.path.join(opts.saveDir, histo.split("/")[0], opts.optMode)

    '''
    if opts.normaliseToOne:
        save_path = savePath + opts.MVAcut
        if opts.noQCD:
            save_path = savePath + opts.MVAcut + "/noQCD/"
    else:
        save_path = savePath + opts.MVAcut + "/normToLumi/"
        if opts.noQCD:
            save_path = savePath + opts.MVAcut + "/noQCD/"
     '''
    
    SavePlot(p, saveName, savePath) 

    return
Exemplo n.º 5
0
def PlotHistosAndCalculateTF(datasetsMgr, histoList, binLabels, opts):

    # Get the histogram customisations (keyword arguments)
    _kwargs = GetHistoKwargs(histoList[0])

    # Get the root histos for all datasets and Control Regions (CRs)
    regions = ["SR", "VR", "CRone", "CRtwo"]
    rhDict = GetRootHistos(datasetsMgr, histoList, regions, binLabels)

    #=========================================================================================
    # Calculate the Transfer Factor (TF) and save to file
    #=========================================================================================
    manager = FakeBNormalization.FakeBNormalizationManager(binLabels,
                                                           opts.mcrab,
                                                           opts.optMode,
                                                           verbose=False)
    if opts.inclusiveOnly:
        #manager.CalculateTransferFactor(binLabels[0], rhDict["CRone-FakeB"], rhDict["CRtwo-FakeB"])
        binLabel = "Inclusive"
        manager.CalculateTransferFactor("Inclusive",
                                        rhDict["FakeB-CRone-Inclusive"],
                                        rhDict["FakeB-CRtwo-Inclusive"])
    else:
        for bin in binLabels:
            manager.CalculateTransferFactor(bin,
                                            rhDict["FakeB-CRone-%s" % bin],
                                            rhDict["FakeB-CRtwo-%s" % bin])

    # Get unique a style for each region
    for k in rhDict:
        dataset = k.split("-")[0]
        region = k.split("-")[1]
        styles.getABCDStyle(region).apply(rhDict[k])
        if "FakeB" in k:
            styles.getFakeBStyle().apply(rhDict[k])
        # sr.apply(rhDict[k])

    # =========================================================================================
    # Create the final plot object
    # =========================================================================================
    rData_SR = rhDict["Data-SR-Inclusive"]
    rEWKGenuineB_SR = rhDict["EWK-SR-Inclusive-EWKGenuineB"]
    rBkgSum_SR = rhDict["FakeB-VR-Inclusive"].Clone("BkgSum-SR-Inclusive")
    rBkgSum_SR.Reset()

    if opts.inclusiveOnly:
        bin = "Inclusive"
        # Normalise the VR histogram with the Transfer Factor ( BkgSum = VR * (CR1/CR2) )
        binHisto_VR = rhDict["FakeB-VR-%s" % (bin)]
        VRtoSR_TF = manager.GetTransferFactor(bin)
        Print(
            "Applying TF = %s%0.6f%s to VR shape" %
            (ShellStyles.NoteStyle(), VRtoSR_TF, ShellStyles.NormalStyle()),
            True)
        binHisto_VR.Scale(VRtoSR_TF)
        # Add the normalised histogram to the final Inclusive SR (predicted) histo
        rBkgSum_SR.Add(binHisto_VR, +1)
    else:
        # For-loop: All bins
        for i, bin in enumerate(binLabels, 1):
            if bin == "Inclusive":
                continue
            # Normalise the VR histogram with the Transfer Factor ( BkgSum = VR * (CR1/CR2) )
            binHisto_VR = rhDict["FakeB-VR-%s" % (bin)]
            VRtoSR_TF = manager.GetTransferFactor(bin)
            Print(
                "Applying TF = %s%0.6f%s to VR shape" %
                (ShellStyles.NoteStyle(), VRtoSR_TF,
                 ShellStyles.NormalStyle()), i == 1)
            binHisto_VR.Scale(VRtoSR_TF)
            # Add the normalised histogram to the final Inclusive SR (predicted) histo
            rBkgSum_SR.Add(binHisto_VR, +1)

    #Print("Got Verification Region (VR) shape %s%s%s" % (ShellStyles.NoteStyle(), rFakeB_VR.GetName(), ShellStyles.NormalStyle()), True)

    # Normalise the VR histogram with the Transfer Factor ( BkgSum = VR * (CR1/CR2) )
    #VRtoSR_TF = manager.GetTransferFactor("Inclusive")
    #Print("Applying TF = %s%0.6f%s to VR shape" % (ShellStyles.NoteStyle(), VRtoSR_TF, ShellStyles.NormalStyle()), True)
    #rBkgSum_SR.Scale(VRtoSR_TF)

    # Plot histograms
    if opts.altPlot:
        # Add the SR EWK Genuine-b to the SR FakeB ( BkgSum = [FakeB] + [GenuineB-MC] = [VR * (CR1/CR2)] + [GenuineB-MC] )
        rBkgSum_SR.Add(rEWKGenuineB_SR, +1)

        # Change style
        styles.getGenuineBStyle().apply(rBkgSum_SR)

        # Remove unsupported settings of kwargs
        _kwargs["stackMCHistograms"] = False
        _kwargs["addLuminosityText"] = False

        # Create the plot
        p = plots.ComparisonManyPlot(rData_SR, [rBkgSum_SR], saveFormats=[])

        # Set draw / legend style
        p.histoMgr.setHistoDrawStyle("Data-SR-Inclusive", "P")
        p.histoMgr.setHistoLegendStyle("Data-SR-Inclusive", "LP")
        p.histoMgr.setHistoDrawStyle("BkgSum-SR-Inclusive", "HIST")
        p.histoMgr.setHistoLegendStyle("BkgSum-SR-Inclusive", "F")

        # Set legend labels
        p.histoMgr.setHistoLegendLabelMany({
            "Data-SR": "Data",
            "BkgSum-SR": "Fake-b + Gen-b",
        })
    else:
        # Create empty histogram stack list
        myStackList = []

        # Signal
        p2 = plots.DataMCPlot(datasetsMgr,
                              "ForTestQGLR/QGLR_SR/QGLR_SRInclusive",
                              saveFormats=[])

        hSignal_800 = p2.histoMgr.getHisto(
            'ChargedHiggs_HplusTB_HplusToTB_M_800').getRootHisto()
        hhSignal_800 = histograms.Histo(
            hSignal_800, 'ChargedHiggs_HplusTB_HplusToTB_M_800',
            "H^{+} m_{H^+}=800 GeV")
        hhSignal_800.setIsDataMC(isData=False, isMC=True)
        myStackList.append(hhSignal_800)

        hSignal_250 = p2.histoMgr.getHisto(
            'ChargedHiggs_HplusTB_HplusToTB_M_250').getRootHisto()
        hhSignal_250 = histograms.Histo(
            hSignal_250, 'ChargedHiggs_HplusTB_HplusToTB_M_250',
            "H^{+} m_{H^+}=250 GeV"
        )  #plots._legendLabels['ChargedHiggs_HplusTB_HplusToTB_M_250'])
        hhSignal_250.setIsDataMC(isData=False, isMC=True)
        #myStackList.append(hhSignal_250)

        hSignal_500 = p2.histoMgr.getHisto(
            'ChargedHiggs_HplusTB_HplusToTB_M_500').getRootHisto()
        hhSignal_500 = histograms.Histo(
            hSignal_500, 'ChargedHiggs_HplusTB_HplusToTB_M_500',
            "H^{+} m_{H^+}=500 GeV"
        )  #plots._legendLabels['ChargedHiggs_HplusTB_HplusToTB_M_500'])
        hhSignal_500.setIsDataMC(isData=False, isMC=True)
        #myStackList.append(hhSignal_500)

        hSignal_1000 = p2.histoMgr.getHisto(
            'ChargedHiggs_HplusTB_HplusToTB_M_1000').getRootHisto()
        hhSignal_1000 = histograms.Histo(
            hSignal_1000, 'ChargedHiggs_HplusTB_HplusToTB_M_1000',
            "H^{+} m_{H^+}=1000 GeV"
        )  #plots._legendLabels['ChargedHiggs_HplusTB_HplusToTB_M_500'])
        hhSignal_1000.setIsDataMC(isData=False, isMC=True)
        #myStackList.append(hhSignal_1000)

        # Add the FakeB data-driven background to the histogram list
        hFakeB = histograms.Histo(rBkgSum_SR, "FakeB", "Fake-b")
        hFakeB.setIsDataMC(isData=False, isMC=True)
        myStackList.append(hFakeB)

        # Add the EWKGenuineB MC background to the histogram list
        hGenuineB = histograms.Histo(rEWKGenuineB_SR, "GenuineB",
                                     "EWK Genuine-b")
        hGenuineB.setIsDataMC(isData=False, isMC=True)
        myStackList.append(hGenuineB)

        # Add the collision datato the histogram list
        hData = histograms.Histo(rData_SR, "Data", "Data")
        hData.setIsDataMC(isData=True, isMC=False)
        myStackList.insert(0, hData)

        p = plots.DataMCPlot2(myStackList, saveFormats=[])
        p.setLuminosity(opts.intLumi)
        p.setDefaultStyles()

    # Draw the plot and save it
    hName = "test"
    plots.drawPlot(p, hName, **_kwargs)
    SavePlot(p,
             hName,
             os.path.join(opts.saveDir, opts.optMode),
             saveFormats=[".png", ".pdf"])

    #==========================================================================================
    # Calculate Cut-Flow Efficiency
    #==========================================================================================
    kwargs = {
        "rebinX": 1,
        "xlabel": "QGLR",
        "ylabel": "Significance / %.02f ",
        "opts": {
            "ymin": 0.0,
            "ymaxfactor": 1.3
        },
        "createLegend": {
            "x1": 0.55,
            "y1": 0.70,
            "x2": 0.92,
            "y2": 0.92
        },
        #            "cutBox"           : {"cutValue": 0.0, "fillColor" : 16, "box": False, "line": False, "greaterThan": True},
    }

    efficiencyList = []
    xValues = []

    yValues_250 = []
    yValues_500 = []
    yValues_800 = []
    yValues_1000 = []
    yValues_Bkg = []

    nBins = hSignal_250.GetNbinsX() + 1

    hBkg = p.histoMgr.getHisto(
        "ChargedHiggs_HplusTB_HplusToTB_M_800").getRootHisto().Clone("Bkg")
    hBkg.Reset()

    # Bkg: FakeB + Genuine B
    hBkg.Add(hFakeB.getRootHisto(), +1)
    hBkg.Add(hGenuineB.getRootHisto(), +1)

    for i in range(0, nBins):

        # Cut value
        cut = hSignal_250.GetBinCenter(i)

        passed_250 = hSignal_250.Integral(i, hSignal_250.GetXaxis().GetNbins())
        passed_500 = hSignal_500.Integral(i, hSignal_500.GetXaxis().GetNbins())
        passed_800 = hSignal_800.Integral(i, hSignal_800.GetXaxis().GetNbins())
        passed_1000 = hSignal_1000.Integral(i,
                                            hSignal_1000.GetXaxis().GetNbins())

        passed_Bkg = hBkg.Integral(i, hBkg.GetXaxis().GetNbins())

        total_250 = hSignal_250.Integral()
        total_500 = hSignal_500.Integral()
        total_800 = hSignal_800.Integral()
        total_1000 = hSignal_1000.Integral()
        total_Bkg = hBkg.Integral()

        eff_250 = float(passed_250) / total_250
        eff_500 = float(passed_500) / total_500
        eff_800 = float(passed_800) / total_800
        eff_1000 = float(passed_1000) / total_1000

        eff_Bkg = float(passed_Bkg) / total_Bkg

        xValues.append(cut)
        yValues_250.append(eff_250)
        yValues_500.append(eff_500)
        yValues_800.append(eff_800)
        yValues_1000.append(eff_1000)

        yValues_Bkg.append(eff_Bkg)

    # Create the Efficiency Plot
    tGraph_250 = ROOT.TGraph(len(xValues), array.array("d", xValues),
                             array.array("d", yValues_250))
    tGraph_500 = ROOT.TGraph(len(xValues), array.array("d", xValues),
                             array.array("d", yValues_500))
    tGraph_800 = ROOT.TGraph(len(xValues), array.array("d", xValues),
                             array.array("d", yValues_800))
    tGraph_1000 = ROOT.TGraph(len(xValues), array.array("d", xValues),
                              array.array("d", yValues_1000))
    tGraph_Bkg = ROOT.TGraph(len(xValues), array.array("d", xValues),
                             array.array("d", yValues_Bkg))

    styles.getSignalStyleHToTB_M("200").apply(tGraph_250)
    styles.getSignalStyleHToTB_M("500").apply(tGraph_500)
    styles.getSignalStyleHToTB_M("800").apply(tGraph_800)
    styles.getSignalStyleHToTB_M("1000").apply(tGraph_1000)
    styles.getQCDLineStyle().apply(tGraph_Bkg)

    drawStyle = "CPE"
    effGraph_250 = histograms.HistoGraph(tGraph_250,
                                         "H^{+} m_{H^{+}} = 250 GeV", "lp",
                                         drawStyle)
    effGraph_500 = histograms.HistoGraph(tGraph_500,
                                         "H^{+} m_{H^{+}} = 500 GeV", "lp",
                                         drawStyle)
    effGraph_800 = histograms.HistoGraph(tGraph_800,
                                         "H^{+} m_{H^{+}} = 800 GeV", "lp",
                                         drawStyle)
    effGraph_1000 = histograms.HistoGraph(tGraph_1000,
                                          "H^{+} m_{H^{+}} = 1000 GeV", "lp",
                                          drawStyle)
    effGraph_Bkg = histograms.HistoGraph(tGraph_Bkg, "Bkg", "lp", drawStyle)

    efficiencyList.append(effGraph_250)
    efficiencyList.append(effGraph_500)
    efficiencyList.append(effGraph_800)
    efficiencyList.append(effGraph_1000)
    efficiencyList.append(effGraph_Bkg)

    # Efficiency plot
    pE = plots.PlotBase(efficiencyList, saveFormats=["pdf"])
    pE.createFrame("QGLR_Efficiency")
    pE.setEnergy("13")
    pE.getFrame().GetYaxis().SetLabelSize(18)
    pE.getFrame().GetXaxis().SetLabelSize(20)

    pE.getFrame().GetYaxis().SetTitle("Efficiency / 0.01")
    pE.getFrame().GetXaxis().SetTitle("QGLR Cut")

    # Add Standard Texts to plot
    histograms.addStandardTexts()

    # Customise Legend
    moveLegend = {"dx": -0.50, "dy": -0.5, "dh": -0.1}
    pE.setLegend(histograms.moveLegend(histograms.createLegend(),
                                       **moveLegend))

    pE.draw()
    #    plots.drawPlot(pE, "QGLR_Efficiency", **kwargs)
    SavePlot(pE,
             "QGLR_Efficiency",
             os.path.join(opts.saveDir, opts.optMode),
             saveFormats=[".png", ".pdf"])

    #==========================================================================================
    # Calculate Significance
    #==========================================================================================

    SignalName = "ChargedHiggs_HplusTB_HplusToTB_M_800"

    hSignif_250 = p.histoMgr.getHisto(SignalName).getRootHisto().Clone(
        SignalName)
    hSignif_500 = p.histoMgr.getHisto(SignalName).getRootHisto().Clone(
        SignalName)
    hSignif_800 = p.histoMgr.getHisto(SignalName).getRootHisto().Clone(
        SignalName)
    hSignif_1000 = p.histoMgr.getHisto(SignalName).getRootHisto().Clone(
        SignalName)

    hSignif_250.Reset()
    hSignif_500.Reset()
    hSignif_800.Reset()
    hSignif_1000.Reset()

    hBkg = p.histoMgr.getHisto(SignalName).getRootHisto().Clone("Bkg")
    hBkg.Reset()

    # Bkg: FakeB + Genuine B
    hBkg.Add(hFakeB.getRootHisto(), +1)
    hBkg.Add(hGenuineB.getRootHisto(), +1)

    nBins = hSignif_250.GetNbinsX() + 1

    # For-loop: All histo bins
    for i in range(1, nBins + 1):

        sigmaB = ROOT.Double(0)

        b = hBkg.IntegralAndError(i, nBins, sigmaB)

        s_250 = hSignal_250.Integral(i, nBins)
        s_500 = hSignal_500.Integral(i, nBins)
        s_800 = hSignal_800.Integral(i, nBins)
        s_1000 = hSignal_1000.Integral(i, nBins)

        # Calculate significance
        signif_250 = stat.significance(s_250, b, sigmaB,
                                       option="Simple")  #Asimov")
        signif_500 = stat.significance(s_500, b, sigmaB,
                                       option="Simple")  #Asimov")
        signif_800 = stat.significance(s_800, b, sigmaB,
                                       option="Simple")  #Asimov")
        signif_1000 = stat.significance(s_1000, b, sigmaB,
                                        option="Simple")  #"Asimov")

        # Set signif for this bin
        hSignif_250.SetBinContent(i, signif_250)
        hSignif_500.SetBinContent(i, signif_500)
        hSignif_800.SetBinContent(i, signif_800)
        hSignif_1000.SetBinContent(i, signif_1000)

        # Apply style
        s_250 = styles.getSignalStyleHToTB_M("200")
        s_250.apply(hSignif_250)

        s_500 = styles.getSignalStyleHToTB_M("500")
        s_500.apply(hSignif_500)

        s_800 = styles.getSignalStyleHToTB_M("800")
        s_800.apply(hSignif_800)

        s_1000 = styles.getSignalStyleHToTB_M("1000")
        s_1000.apply(hSignif_1000)

        hList = []
        hList.append(hSignif_250)
        hList.append(hSignif_500)
        hList.append(hSignif_800)
        hList.append(hSignif_1000)

        hSignif_250.SetName("H^{+} m_{H^{+}} = 250 GeV")
        hSignif_500.SetName("H^{+} m_{H^{+}} = 500 GeV")
        hSignif_800.SetName("H^{+} m_{H^{+}} = 800 GeV")
        hSignif_1000.SetName("H^{+} m_{H^{+}} = 1000 GeV")

    pS = plots.PlotBase(hList, saveFormats=["png", "pdf"])
    pS.setLuminosity(opts.intLumi)

    # Drawing style
    pS.histoMgr.setHistoDrawStyleAll("HIST")
    pS.histoMgr.setHistoLegendStyleAll("L")
    pS.histoMgr.forEachHisto(lambda h: h.getRootHisto().SetMarkerSize(1.0))
    pS.histoMgr.forEachHisto(
        lambda h: h.getRootHisto().SetLineStyle(ROOT.kSolid))
    pS.histoMgr.forEachHisto(
        lambda h: h.getRootHisto().SetMarkerStyle(ROOT.kFullCircle))

    # Draw the plot
    name = "QGLR_Signif" + "GE"

    plots.drawPlot(pS, name, **kwargs)
    SavePlot(pS,
             name,
             os.path.join(opts.saveDir, opts.optMode),
             saveFormats=[".png", ".pdf"])

    #=========================================================================================
    # Calculate the Transfer Factor (TF) and save to file
    #=========================================================================================
    Verbose("Write the normalisation factors to a python file", True)
    fileName = os.path.join(
        opts.mcrab, "FakeBTransferFactors%s.py" % (getModuleInfoString(opts)))
    manager.writeNormFactorFile(fileName, opts)
    return
def main(opts, signalMass):

    # Apply TDR style
    style = tdrstyle.TDRStyle()
    style.setOptStat(True)
    style.setGridX(False)
    style.setGridY(False)
    
    # If user does not define optimisation mode do all of them
    if opts.optMode == None:
        if len(optList) < 1:
            optList.append("")
        else:
            pass
        optModes = optList
    else:
        optModes = [opts.optMode]

    # For-loop: All optimisation modes
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager 
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities() # from lumi.json

        dir = opts.mcrab.replace("_BDT85", "_BDT")
        print dir

        datasetsMgr30 = GetDatasetsFromDir_second(opts, dir+"30")
        datasetsMgr30.updateNAllEventsToPUWeighted()
        datasetsMgr30.loadLuminosities() # from lumi.json                                                                                      

        datasetsMgr40 = GetDatasetsFromDir_second(opts, dir+"40")
        datasetsMgr40.updateNAllEventsToPUWeighted()
        datasetsMgr40.loadLuminosities() # from lumi.json                                                                                                                           

        datasetsMgr50 = GetDatasetsFromDir_second(opts, dir+"50")
        datasetsMgr50.updateNAllEventsToPUWeighted()
        datasetsMgr50.loadLuminosities() # from lumi.json                                                                                                                           
 
        datasetsMgr60 = GetDatasetsFromDir_second(opts, dir+"60")
        datasetsMgr60.updateNAllEventsToPUWeighted()
        datasetsMgr60.loadLuminosities() # from lumi.json                                                                                                                               

        datasetsMgr70 = GetDatasetsFromDir_second(opts, dir+"70")
        datasetsMgr70.updateNAllEventsToPUWeighted()
        datasetsMgr70.loadLuminosities() # from lumi.json                                                                                                                               

        datasetsMgr80 = GetDatasetsFromDir_second(opts, dir+"80")
        datasetsMgr80.updateNAllEventsToPUWeighted()
        datasetsMgr80.loadLuminosities() # from lumi.json                                                                                                                               

        datasetsMgr90 = GetDatasetsFromDir_second(opts, dir+"90")
        datasetsMgr90.updateNAllEventsToPUWeighted()
        datasetsMgr90.loadLuminosities() # from lumi.json                                                                                                                               

        datasetsMgr95 = GetDatasetsFromDir_second(opts, dir+"95")
        datasetsMgr95.updateNAllEventsToPUWeighted()
        datasetsMgr95.loadLuminosities() # from lumi.json             

        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()

        # Set/Overwrite cross-sections
        for datasetsMgr_ in [datasetsMgr, datasetsMgr30, datasetsMgr40, datasetsMgr50, datasetsMgr60, datasetsMgr70, datasetsMgr80, datasetsMgr90, datasetsMgr95]:
            for d in datasetsMgr_.getAllDatasets():
                if "ChargedHiggs" in d.getName():
                    datasetsMgr_.getDataset(d.getName()).setCrossSection(1.0)
                    #datasetsMgr.getDataset(d.getName()).setCrossSection(1.0)
                    #datasetsMgr50.getDataset(d.getName()).setCrossSection(1.0)
                    #datasetsMgr60.getDataset(d.getName()).setCrossSection(1.0)
                    #datasetsMgr70.getDataset(d.getName()).setCrossSection(1.0)
                    #datasetsMgr80.getDataset(d.getName()).setCrossSection(1.0)
                    #datasetsMgr90.getDataset(d.getName()).setCrossSection(1.0)
                    #datasetsMgr95.getDataset(d.getName()).setCrossSection(1.0)

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        #plots.mergeRenameReorderForDataMC(datasetsMgr) 
        
        # Print dataset information before removing anything?
        if 0:
            datasetsMgr.PrintInfo()

        # Determine integrated Lumi before removing data
        if "Data" in datasetsMgr.getAllDatasetNames():
            intLumi = datasetsMgr.getDataset("Data").getLuminosity()
        else:
            intLumi = 35920
        # Remove datasets
        filterKeys = ["Data", "TTZToQQ", "TTWJets", "TTTT"]
        #filterKeys = ["Data", "TTZToQQ", "TTWJets", "TTTT"]
        for key in filterKeys:
            datasetsMgr.remove(filter(lambda name: key in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr30.remove(filter(lambda name: key in name, datasetsMgr30.getAllDatasetNames()))
            datasetsMgr40.remove(filter(lambda name: key in name, datasetsMgr40.getAllDatasetNames()))
            datasetsMgr50.remove(filter(lambda name: key in name, datasetsMgr50.getAllDatasetNames()))
            datasetsMgr60.remove(filter(lambda name: key in name, datasetsMgr60.getAllDatasetNames()))
            datasetsMgr70.remove(filter(lambda name: key in name, datasetsMgr70.getAllDatasetNames()))
            datasetsMgr80.remove(filter(lambda name: key in name, datasetsMgr80.getAllDatasetNames()))
            datasetsMgr90.remove(filter(lambda name: key in name, datasetsMgr90.getAllDatasetNames()))
            datasetsMgr95.remove(filter(lambda name: key in name, datasetsMgr95.getAllDatasetNames()))
        # Re-order datasets
        datasetOrder = []
        for d in datasetsMgr.getAllDatasets():
            if "M_" in d.getName():
                if d not in signalMass:
                    continue
            datasetOrder.append(d.getName())
            
        # Append signal datasets
        for m in signalMass:
            datasetOrder.insert(0, m)
        datasetsMgr.selectAndReorder(datasetOrder)
        datasetsMgr30.selectAndReorder(datasetOrder)
        datasetsMgr40.selectAndReorder(datasetOrder)
        datasetsMgr50.selectAndReorder(datasetOrder)
        datasetsMgr60.selectAndReorder(datasetOrder)
        datasetsMgr70.selectAndReorder(datasetOrder)
        datasetsMgr80.selectAndReorder(datasetOrder)
        datasetsMgr90.selectAndReorder(datasetOrder)
        datasetsMgr95.selectAndReorder(datasetOrder)

        # Print dataset information
        datasetsMgr.PrintInfo()

        # Define the mapping histograms in numerator->denominator pairs

        Numerators = ["AllTopQuarkPt_MatchedBDT",
                      "AllTopQuarkPt_Matched",
                      "EventTrijetPt2T_MatchedBDT",
                      "EventTrijetPt2T_MatchedBDT",
                      "EventTrijetPt2T_MatchedBDT",
                      "AllTopQuarkPt_MatchedBDT",
                      "SelectedTrijetsPt_BjetPassCSVdisc_afterCuts",
                      "TrijetFakePt_BDT",
                      ]
        Denominators = ["AllTopQuarkPt_Matched",
                        "TopQuarkPt",
                        "EventTrijetPt2T_BDT",
                        "EventTrijetPt2T_Matched",
                        "EventTrijetPt2T",
                        "TopQuarkPt",
                        "SelectedTrijetsPt_afterCuts",
                        "TrijetFakePt",
                        ]

        
        if 1:
            datasetsMgr.merge("QCD", GetListOfQCDatasets())
            datasetsMgr30.merge("QCD", GetListOfQCDatasets())
            datasetsMgr40.merge("QCD", GetListOfQCDatasets())
            datasetsMgr50.merge("QCD", GetListOfQCDatasets())
            datasetsMgr60.merge("QCD", GetListOfQCDatasets())
            datasetsMgr70.merge("QCD", GetListOfQCDatasets())
            datasetsMgr80.merge("QCD", GetListOfQCDatasets())
            datasetsMgr90.merge("QCD", GetListOfQCDatasets())
            datasetsMgr95.merge("QCD", GetListOfQCDatasets())
            
            plots._plotStyles["QCD"] = styles.getQCDLineStyle()
        #Background1_Dataset = datasetsMgr.getDataset("QCD")

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        plots.mergeRenameReorderForDataMC(datasetsMgr) 
        
        # For-loop: All numerator-denominator pairs
        for i in range(len(Numerators)):
            numerator = os.path.join(opts.folder, Numerators[i])
            denominator = os.path.join(opts.folder, Denominators[i])
            PlotEfficiency_comparison(datasetsMgr, datasetsMgr30, datasetsMgr40, datasetsMgr50, datasetsMgr60, datasetsMgr70, datasetsMgr80, datasetsMgr90, datasetsMgr95, numerator, denominator, intLumi)
    return
Exemplo n.º 7
0
def DataEwkQcd(datasetsMgr, histoName, analysisType):
    '''
    Create plots with "Data", "QCD=Data-EWK", and "EWK" on the same canvas
    Mostly for sanity checks and visualisation purposes
    '''
    Verbose(
        "Plotting histogram %s for Data, EWK, QCD for %s" %
        (histoName, analysisType), True)

    # Sanity check
    IsBaselineOrInverted(analysisType)

    # Define histogram names (full path)
    h1 = "topSelection_Baseline/%s" % (histoName)
    h2 = "topSelection_Inverted/%s" % (histoName)

    # Create plot object for Data
    p1 = plots.ComparisonPlot(*getHistos(datasetsMgr, "Data", h1, h2))
    p1.histoMgr.normalizeMCToLuminosity(
        datasetsMgr.getDataset("Data").getLuminosity())
    # Create plot object for EWK
    p2 = plots.ComparisonPlot(*getHistos(datasetsMgr, "EWK", h1, h2))
    p2.histoMgr.normalizeMCToLuminosity(
        datasetsMgr.getDataset("Data").getLuminosity())

    # Get Data and EWK histos
    Data = p1.histoMgr.getHisto(analysisType +
                                "-Data").getRootHisto().Clone(analysisType +
                                                              "-Data")
    EWK = p2.histoMgr.getHisto(analysisType +
                               "-EWK").getRootHisto().Clone(analysisType +
                                                            "-EWK")

    # Create QCD histo: QCD = Data-EWK
    QCD = p1.histoMgr.getHisto(analysisType +
                               "-Data").getRootHisto().Clone(analysisType +
                                                             "-QCD")
    QCD.Add(EWK, -1)

    # Create the final plot object. The Data is treated as the reference histo.
    # All other histograms are compared with respect to that.
    p = plots.ComparisonManyPlot(Data, [QCD, EWK], saveFormats=[])
    p.setLuminosity(GetLumi(datasetsMgr))

    # Apply histo styles
    p.histoMgr.forHisto(analysisType + "-Data", styles.getDataStyle())
    p.histoMgr.forHisto(analysisType + "-QCD", styles.getQCDLineStyle())
    p.histoMgr.forHisto(analysisType + "-EWK", styles.getAltEWKStyle())

    # Set draw style
    p.histoMgr.setHistoDrawStyle(analysisType + "-Data", "P")
    p.histoMgr.setHistoLegendStyle(analysisType + "-Data", "P")
    p.histoMgr.setHistoDrawStyle(analysisType + "-QCD", "LP")
    p.histoMgr.setHistoLegendStyle(analysisType + "-QCD", "LP")
    p.histoMgr.setHistoDrawStyle(analysisType + "-EWK", "HIST")
    p.histoMgr.setHistoLegendStyle(analysisType + "-EWK", "LFP")
    # p.histoMgr.setHistoLegendStyleAll("LP")

    # Set legend labels
    p.histoMgr.setHistoLegendLabelMany({
        analysisType + "-Data": "Data",
        analysisType + "-QCD": "QCD",  #=Data-EWK",
        analysisType + "-EWK": "EWK",
        # analysisType + "-Data": "%s (Data)" % (analysisType),
        # analysisType + "-QCD" : "%s (QCD)"  % (analysisType),
        # analysisType + "-EWK" : "%s (EWK)"  % (analysisType),
    })

    # Draw the histograms
    _cutBox = None
    _rebinX = 1
    _opts = {"ymin": 1e0, "ymaxfactor": 10}
    _format = "%0.0f"

    if "mass" in histoName.lower():
        _rebinX = 2
        _format = "%0.0f GeV/c^{2}"
        if "TrijetMass" in histoName:
            _cutBox = {
                "cutValue": 173.21,
                "fillColor": 16,
                "box": False,
                "line": True,
                "greaterThan": True
            }
        if "DijetMass" in histoName:
            _cutBox = {
                "cutValue": 80.399,
                "fillColor": 16,
                "box": False,
                "line": True,
                "greaterThan": True
            }
    if "pt" in histoName.lower():
        _rebinX = 2
        _format = "%0.0f GeV/c"
    if "eta" in histoName.lower():
        _rebinX = 2
        _format = "%0.2f"
        _cutBox = {
            "cutValue": 0.,
            "fillColor": 16,
            "box": False,
            "line": True,
            "greaterThan": True
        }
    if "bdisc" in histoName.lower():
        _format = "%0.2f"
        _rebinX = 1
    if "chisqr" in histoName.lower():
        _format = "%0.0f"
        _rebinX = 10
        if "after" in histoName.lower():
            _rebinX = 1
            _opts["xmax"] = 20.0
    if "tetrajet" in histoName.lower():
        if "mass" in histoName.lower():
            _rebinX = 10
            _opts["xmax"] = 3500.0

    else:
        pass

    histoName += "_" + analysisType
    plots.drawPlot(
        p,
        histoName,
        ylabel="Events /  %s" % (_format),
        log=True,
        rebinX=_rebinX,
        cmsExtraText="Preliminary",
        createLegend={
            "x1": 0.75,
            "y1": 0.76,
            "x2": 0.92,
            "y2": 0.92
        },
        opts=_opts,
        opts2={
            "ymin": 1e-5,
            "ymax": 1e0
        },
        ratio=True,
        ratioInvert=False,
        ratioYlabel="Ratio",
        cutBox=_cutBox,
    )

    # Save plot in all formats
    SavePlot(p,
             histoName,
             os.path.join(opts.saveDir, "DataEwkQcd"),
             saveFormats=[".png"])
    return
def PlotMC(datasetsMgr, datasetsMgr1, histo, intLumi):

    kwargs = {}
    print "here1"
    if opts.normaliseToOne:
        p = plots.MCPlot(datasetsMgr, histo, normalizeToOne=True, saveFormats=[".pdf", "png"], **kwargs)
        p = plots.MCPlot(datasetsMgr1, histo, normalizeToOne=True, saveFormats=[".pdf", ".png"], **kwargs)
    else:
        p = plots.MCPlot(datasetsMgr, histo, normalizeToLumi=intLumi, saveFormats=[".pdf", ".png"], **kwargs)
        p = plots.MCPlot(datasetsMgr1, histo, normalizeToOne=True, saveFormats=[".pdf", "png"], **kwargs)

    # Draw the histograms
    _cutBox = None
    _rebinX = 1
    _format = "%0.0f"
    _xlabel = None
    logY    = False
    _opts   = {"ymin": 1e-3, "ymaxfactor": 1.0}
    print "here2"
    if "Pt" in histo:
        _rebinX = 2
        xMin = 0.0
#        rebinX = 2
        xMax = 805.0
#        xMax = 555.0 # For topPt < 500GeV
        xTitle = "p_{T} (GeV/c)"
        units = "GeV/c"
        _format = "%0.0f" + units
#        yTitle = "Efficiency / "   + str(binwidth) + " "+units
#        yMin = 0.0
#        yMax = 1.1


    else:
        pass

    if logY:
        yMaxFactor = 2.0
    else:
        yMaxFactor = 1.2

    _opts["ymaxfactor"] = yMaxFactor
    if opts.normaliseToOne:
        _opts["ymin"] = 1e-3
        #_opts   = {"ymin": 1e-3, "ymaxfactor": yMaxFactor, "xmax": None}
    else:
        _opts["ymin"] = 1e0
        #_opts["ymaxfactor"] = yMaxFactor
        #_opts   = {"ymin": 1e0, "ymaxfactor": yMaxFactor, "xmax": None}

    # Customise styling
    p.histoMgr.forEachHisto(lambda h: h.getRootHisto().SetLineStyle(ROOT.kSolid))

    print "here3"
    
    histoDef = datasetsMgr.getDataset("TT").getDatasetRootHisto(histo)
    histoDR = datasetsMgr1.getDataset("TT").getDatasetRootHisto(histo)

    histoDef.normalizeToOne()
    histoDR.normalizeToOne()

    hDef = histoDef.getHistogram()
    hDR = histoDR.getHistogram()

    p = plots.ComparisonPlot(histograms.Histo(hDR,"DeltaR08", "l", "L"),
                             histograms.Histo(hDef,"Default", "l", "L"))

    p.histoMgr.setHistoLegendLabelMany({"DeltaR08": "#DeltaR(q,q')>0.8",
                                        "Default": "Default"})

    p.histoMgr.forHisto("DeltaR08", styles.getQCDLineStyle() )
    p.histoMgr.forEachHisto(lambda h: h.getRootHisto().SetLineStyle(ROOT.kSolid))
    p.histoMgr.setHistoDrawStyle("DeltaR08", "HIST") #HIST
    p.histoMgr.setHistoLegendStyle("DeltaR08", "L") #F

    p.histoMgr.setHistoDrawStyle("Default", "HIST") #HIST
    p.histoMgr.setHistoLegendStyle("Default", "L") #F

    p.histoMgr.forHisto("Default", styles.ttStyle)  

    print "here4"

    # Customise style
    signalM = []
    for m in signalMass:
        signalM.append(m.rsplit("M_")[-1])
    for m in signalM:
        p.histoMgr.forHisto("ChargedHiggs_HplusTB_HplusToTB_M_%s" %m, styles.getSignalStyleHToTB_M(m))
    print "here5"
    plots.drawPlot(p, 
                   histo,  
                   xlabel       = _xlabel,
                   ylabel       = "Arbitrary Units / %s" % (_format),
                   log          = logY,
                   rebinX       = _rebinX, cmsExtraText = "Preliminary", 
                   createLegend = {"x1": 0.58, "y1": 0.75, "x2": 0.92, "y2": 0.92},
#                   createLegend = {"x1": 0.58, "y1": 0.65, "x2": 0.92, "y2": 0.92},
                   opts         = _opts,
                   opts2        = {"ymin": 0.6, "ymax": 1.4},
                   cutBox       = _cutBox,
                   )
    print "here6"
    # Save plot in all formats    
    saveName = histo.split("/")[-1]
    savePath = os.path.join(opts.saveDir, "HplusMasses", histo.split("/")[0], opts.optMode)
    SavePlot(p, saveName, savePath) 
    print "here7"
    return
Exemplo n.º 9
0
def EWKvQCD(datasetsMgr, histoName, analysisType=""):
    Verbose("Plotting EWK Vs QCD unity-normalised histograms for %s" %
            analysisType)

    # Sanity check
    IsBaselineOrInverted(analysisType)

    p1 = plots.ComparisonPlot(
        *getHistos(datasetsMgr, "Data", "topSelection_Baseline/%s" %
                   histoName, "topSelection_Inverted/%s" % histoName))
    p1.histoMgr.normalizeMCToLuminosity(
        datasetsMgr.getDataset("Data").getLuminosity())

    p2 = plots.ComparisonPlot(
        *getHistos(datasetsMgr, "EWK", "topSelection_Baseline/%s" %
                   histoName, "topSelection_Inverted/%s" % histoName))
    p2.histoMgr.normalizeMCToLuminosity(
        datasetsMgr.getDataset("Data").getLuminosity())

    # Get histos
    data = p1.histoMgr.getHisto(analysisType +
                                "-Data").getRootHisto().Clone(analysisType +
                                                              " -Data")
    EWK = p2.histoMgr.getHisto(analysisType +
                               "-EWK").getRootHisto().Clone(analysisType +
                                                            "-EWK")
    # Create QCD histos: QCD = Data-EWK
    QCD = p1.histoMgr.getHisto(analysisType +
                               "-Data").getRootHisto().Clone(analysisType +
                                                             "-QCD")
    QCD.Add(EWK, -1)

    # Normalize histograms to unit area
    QCD.Scale(1.0 / QCD.Integral())
    EWK.Scale(1.0 / EWK.Integral())

    # Create the final plot object
    p = plots.ComparisonManyPlot(QCD, [EWK],
                                 saveFormats=[])  #[".C", ".png", ".pdf"])
    p.setLuminosity(GetLumi(datasetsMgr))

    # Apply styles
    p.histoMgr.forHisto(analysisType + "-QCD", styles.getQCDLineStyle())
    p.histoMgr.forHisto(analysisType + "-EWK", styles.getAltEWKStyle())

    # Set draw style
    p.histoMgr.setHistoDrawStyle(analysisType + "-QCD", "LP")
    p.histoMgr.setHistoLegendStyle(analysisType + "-QCD", "LP")

    # Set legend labels
    p.histoMgr.setHistoLegendLabelMany({
        analysisType + "-QCD":
        analysisType + " (QCD)",
        analysisType + "-EWK":
        analysisType + " (EWK)",
    })

    # Append analysisType to histogram name
    saveName = histoName + "_" + analysisType

    # Draw the histograms #alex
    plots.drawPlot(p, saveName,
                   **GetHistoKwargs(histoName))  #the "**" unpacks the kwargs_

    # _kwargs = {"lessThan": True}
    # p.addCutBoxAndLine(cutValue=200, fillColor=ROOT.kRed, box=False, line=True, ***_kwargs)

    # Save plot in all formats
    saveDir = os.path.join(opts.saveDir, "EWKvQCD", opts.optMode)
    SavePlot(p, saveName, saveDir)
    return
def main(opts):

    # Apply TDR style
    style = tdrstyle.TDRStyle()
    style.setGridX(False)
    style.setGridY(False)
    
    # If user does not define optimisation mode do all of them
    if opts.optMode == None:
        if len(optList) < 1:
            optList.append("")
        else:
            pass
        optModes = optList
    else:
        optModes = [opts.optMode]

    # For-loop: All optimisation modes
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager 
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities() # from lumi.json
        datasetsMgr40 = GetDatasetsFromDir_second(opts, opts.mcrab.replace("_BDT85", "_BDT40"))
        datasetsMgr40.updateNAllEventsToPUWeighted()
        datasetsMgr40.loadLuminosities() # from lumi.json                                                                                                                           

        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()

        # Set/Overwrite cross-sections
        for datasetsMgr_ in [datasetsMgr, datasetsMgr40]:
            for d in datasetsMgr_.getAllDatasets():
                if "ChargedHiggs" in d.getName():
                    datasetsMgr_.getDataset(d.getName()).setCrossSection(1.0)

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        if 0:
            plots.mergeRenameReorderForDataMC(datasetsMgr) 
        
        # Print dataset information before removing anything?
        if 0:
            datasetsMgr.PrintInfo()

        # Determine integrated Lumi before removing data
        if "Data" in datasetsMgr.getAllDatasetNames():
            intLumi = datasetsMgr.getDataset("Data").getLuminosity()
        else:
            intLumi = 35920

        # Remove datasets
        filterKeys = ["Data", "TTZToQQ", "TTWJets", "TTTT"]
        #filterKeys = ["Data", "TTZToQQ", "TTWJets", "TTTT"]
        for key in filterKeys:
            datasetsMgr.remove(filter(lambda name: key in name, datasetsMgr.getAllDatasetNames()))
            datasetsMgr40.remove(filter(lambda name: key in name, datasetsMgr40.getAllDatasetNames()))
        # Re-order datasets
        datasetOrder = []
        for d in datasetsMgr.getAllDatasets():
            datasetOrder.append(d.getName())
            
        # Select and re-order
        datasetsMgr.selectAndReorder(datasetOrder)
        datasetsMgr40.selectAndReorder(datasetOrder)

        # Print dataset information
        datasetsMgr.PrintInfo()

        # QCD multijet
        datasetsMgr.merge("QCD", GetListOfQCDatasets())
        datasetsMgr40.merge("QCD", GetListOfQCDatasets())
        plots._plotStyles["QCD"] = styles.getQCDLineStyle()

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py) 
        plots.mergeRenameReorderForDataMC(datasetsMgr) 
        
        # For-loop: All numerator-denominator pairs
        PlotEfficiency(datasetsMgr, datasetsMgr40, intLumi)
    return
Exemplo n.º 11
0
def PlotHistosAndCalculateTF(datasetsMgr, histoList, binLabels, opts):

    # Get the histogram customisations (keyword arguments)
    _kwargs = GetHistoKwargs(histoList[0])

    # Get the root histos for all datasets and Control Regions (CRs)
    regions = ["SR", "VR", "CRone", "CRtwo"]
    rhDict  = GetRootHistos(datasetsMgr, histoList, regions, binLabels)

    #=========================================================================================
    # Calculate the Transfer Factor (TF) and save to file
    #=========================================================================================
    manager = FakeBNormalization.FakeBNormalizationManager(binLabels, opts.mcrab, opts.optMode, verbose=False)
    if opts.inclusiveOnly:
        #manager.CalculateTransferFactor(binLabels[0], rhDict["CRone-FakeB"], rhDict["CRtwo-FakeB"])
        binLabel = "Inclusive"
        manager.CalculateTransferFactor("Inclusive", rhDict["FakeB-CRone-Inclusive"], rhDict["FakeB-CRtwo-Inclusive"])
    else:
        for bin in binLabels:
            manager.CalculateTransferFactor(bin, rhDict["FakeB-CRone-%s" % bin], rhDict["FakeB-CRtwo-%s" % bin])

    # Get unique a style for each region
    for k in rhDict:
        dataset = k.split("-")[0]
        region  = k.split("-")[1]
        styles.getABCDStyle(region).apply(rhDict[k])
        if "FakeB" in k:
            styles.getFakeBStyle().apply(rhDict[k])
        # sr.apply(rhDict[k])

    # =========================================================================================
    # Create the final plot object
    # =========================================================================================
    rData_SR        = rhDict["Data-SR-Inclusive"] 
    rEWKGenuineB_SR = rhDict["EWK-SR-Inclusive-EWKGenuineB"]
    rBkgSum_SR      = rhDict["FakeB-VR-Inclusive"].Clone("BkgSum-SR-Inclusive")
    rBkgSum_SR.Reset()

    if opts.inclusiveOnly:
        bin = "Inclusive"
        # Normalise the VR histogram with the Transfer Factor ( BkgSum = VR * (CR1/CR2) )
        binHisto_VR = rhDict["FakeB-VR-%s" % (bin)]
        VRtoSR_TF   = manager.GetTransferFactor(bin)
        Print("Applying TF = %s%0.6f%s to VR shape" % (ShellStyles.NoteStyle(), VRtoSR_TF, ShellStyles.NormalStyle()), True)
        binHisto_VR.Scale(VRtoSR_TF) 
                # Add the normalised histogram to the final Inclusive SR (predicted) histo
        rBkgSum_SR.Add(binHisto_VR, +1)
    else:
        # For-loop: All bins
        for i, bin in enumerate(binLabels, 1):
            if bin == "Inclusive":
                continue
            # Normalise the VR histogram with the Transfer Factor ( BkgSum = VR * (CR1/CR2) )
            binHisto_VR = rhDict["FakeB-VR-%s" % (bin)]
            VRtoSR_TF   = manager.GetTransferFactor(bin)
            Print("Applying TF = %s%0.6f%s to VR shape" % (ShellStyles.NoteStyle(), VRtoSR_TF, ShellStyles.NormalStyle()), i==1)
            binHisto_VR.Scale(VRtoSR_TF) 
            # Add the normalised histogram to the final Inclusive SR (predicted) histo
            rBkgSum_SR.Add(binHisto_VR, +1)

    #Print("Got Verification Region (VR) shape %s%s%s" % (ShellStyles.NoteStyle(), rFakeB_VR.GetName(), ShellStyles.NormalStyle()), True)

    # Normalise the VR histogram with the Transfer Factor ( BkgSum = VR * (CR1/CR2) )
    #VRtoSR_TF = manager.GetTransferFactor("Inclusive")
    #Print("Applying TF = %s%0.6f%s to VR shape" % (ShellStyles.NoteStyle(), VRtoSR_TF, ShellStyles.NormalStyle()), True)
    #rBkgSum_SR.Scale(VRtoSR_TF) 
    
    # Plot histograms    
    if opts.altPlot:
        # Add the SR EWK Genuine-b to the SR FakeB ( BkgSum = [FakeB] + [GenuineB-MC] = [VR * (CR1/CR2)] + [GenuineB-MC] )
        rBkgSum_SR.Add(rEWKGenuineB_SR, +1) 

        # Change style
        styles.getGenuineBStyle().apply(rBkgSum_SR)

        # Remove unsupported settings of kwargs
        _kwargs["stackMCHistograms"] = False
        _kwargs["addLuminosityText"] = False

        # Create the plot
        p = plots.ComparisonManyPlot(rData_SR, [rBkgSum_SR], saveFormats=[])

        # Set draw / legend style
        p.histoMgr.setHistoDrawStyle("Data-SR-Inclusive", "P")
        p.histoMgr.setHistoLegendStyle("Data-SR-Inclusive" , "LP")
        p.histoMgr.setHistoDrawStyle("BkgSum-SR-Inclusive", "HIST")
        p.histoMgr.setHistoLegendStyle("BkgSum-SR-Inclusive" , "F")

        # Set legend labels
        p.histoMgr.setHistoLegendLabelMany({
                "Data-SR"       : "Data",
                "BkgSum-SR"     : "Fake-b + Gen-b",
                })
    else:
        # Create empty histogram stack list
        myStackList = []
        
        # Signal
        p2 = plots.DataMCPlot(datasetsMgr, "ForTestQGLR/QGLR_SR/QGLR_SRInclusive", saveFormats=[])
        
        hSignal_800 = p2.histoMgr.getHisto('ChargedHiggs_HplusTB_HplusToTB_M_800').getRootHisto()
        hhSignal_800= histograms.Histo(hSignal_800, 'ChargedHiggs_HplusTB_HplusToTB_M_800', "H^{+} m_{H^+}=800 GeV")
        hhSignal_800.setIsDataMC(isData=False, isMC=True)
        myStackList.append(hhSignal_800)
        
        hSignal_250 = p2.histoMgr.getHisto('ChargedHiggs_HplusTB_HplusToTB_M_250').getRootHisto()
        hhSignal_250= histograms.Histo(hSignal_250, 'ChargedHiggs_HplusTB_HplusToTB_M_250', "H^{+} m_{H^+}=250 GeV")#plots._legendLabels['ChargedHiggs_HplusTB_HplusToTB_M_250'])
        hhSignal_250.setIsDataMC(isData=False, isMC=True)
        #myStackList.append(hhSignal_250)
                
        hSignal_500 = p2.histoMgr.getHisto('ChargedHiggs_HplusTB_HplusToTB_M_500').getRootHisto()
        hhSignal_500= histograms.Histo(hSignal_500, 'ChargedHiggs_HplusTB_HplusToTB_M_500', "H^{+} m_{H^+}=500 GeV")#plots._legendLabels['ChargedHiggs_HplusTB_HplusToTB_M_500'])
        hhSignal_500.setIsDataMC(isData=False, isMC=True)
        #myStackList.append(hhSignal_500)

        hSignal_1000 = p2.histoMgr.getHisto('ChargedHiggs_HplusTB_HplusToTB_M_1000').getRootHisto()
        hhSignal_1000= histograms.Histo(hSignal_1000, 'ChargedHiggs_HplusTB_HplusToTB_M_1000', "H^{+} m_{H^+}=1000 GeV")#plots._legendLabels['ChargedHiggs_HplusTB_HplusToTB_M_500'])
        hhSignal_1000.setIsDataMC(isData=False, isMC=True)
        #myStackList.append(hhSignal_1000)
        
        # Add the FakeB data-driven background to the histogram list    
        hFakeB = histograms.Histo(rBkgSum_SR, "FakeB", "Fake-b")
        hFakeB.setIsDataMC(isData=False, isMC=True)
        myStackList.append(hFakeB)
        
        # Add the EWKGenuineB MC background to the histogram list
        hGenuineB = histograms.Histo(rEWKGenuineB_SR, "GenuineB", "EWK Genuine-b")
        hGenuineB.setIsDataMC(isData=False, isMC=True)
        myStackList.append(hGenuineB)

        # Add the collision datato the histogram list        
        hData = histograms.Histo(rData_SR, "Data", "Data")
        hData.setIsDataMC(isData=True, isMC=False)
        myStackList.insert(0, hData)
        
        p = plots.DataMCPlot2( myStackList, saveFormats=[])
        p.setLuminosity(opts.intLumi)
        p.setDefaultStyles()

        
    # Draw the plot and save it
    hName = "test"
    plots.drawPlot(p, hName, **_kwargs)
    SavePlot(p, hName, os.path.join(opts.saveDir, opts.optMode), saveFormats = [".png", ".pdf"])
    
    #==========================================================================================
    # Calculate Cut-Flow Efficiency
    #==========================================================================================
    kwargs = {
        "rebinX" : 1,
        "xlabel" : "QGLR",
        "ylabel" : "Significance / %.02f ",
        "opts"   : {"ymin": 0.0, "ymaxfactor": 1.3},
        "createLegend": {"x1": 0.55, "y1": 0.70, "x2": 0.92, "y2": 0.92},
        #            "cutBox"           : {"cutValue": 0.0, "fillColor" : 16, "box": False, "line": False, "greaterThan": True},
        }
    
    
    efficiencyList = [] 
    xValues = []
    
    yValues_250 = []
    yValues_500 = []
    yValues_800 = []
    yValues_1000 = []
    yValues_Bkg = []


    nBins = hSignal_250.GetNbinsX()+1

    hBkg = p.histoMgr.getHisto("ChargedHiggs_HplusTB_HplusToTB_M_800").getRootHisto().Clone("Bkg")
    hBkg.Reset()

    # Bkg: FakeB + Genuine B
    hBkg.Add(hFakeB.getRootHisto(), +1)
    hBkg.Add(hGenuineB.getRootHisto(), +1)

    for i in range (0, nBins):
        
        # Cut value 
        cut = hSignal_250.GetBinCenter(i)
        
        passed_250  = hSignal_250.Integral(i, hSignal_250.GetXaxis().GetNbins())
        passed_500  = hSignal_500.Integral(i, hSignal_500.GetXaxis().GetNbins())
        passed_800  = hSignal_800.Integral(i, hSignal_800.GetXaxis().GetNbins())
        passed_1000 = hSignal_1000.Integral(i, hSignal_1000.GetXaxis().GetNbins())
        
        passed_Bkg  = hBkg.Integral(i, hBkg.GetXaxis().GetNbins())
        
        total_250   = hSignal_250.Integral()
        total_500   = hSignal_500.Integral()
        total_800   = hSignal_800.Integral()
        total_1000  = hSignal_1000.Integral()
        total_Bkg   = hBkg.Integral()
         
        eff_250  = float(passed_250)/total_250
        eff_500  = float(passed_500)/total_500
        eff_800  = float(passed_800)/total_800
        eff_1000 = float(passed_1000)/total_1000
        
        eff_Bkg = float(passed_Bkg)/total_Bkg
        
        xValues.append(cut)
        yValues_250.append(eff_250)
        yValues_500.append(eff_500)
        yValues_800.append(eff_800)
        yValues_1000.append(eff_1000)
        
        yValues_Bkg.append(eff_Bkg)
        
    # Create the Efficiency Plot
    tGraph_250 = ROOT.TGraph(len(xValues), array.array("d", xValues), array.array("d", yValues_250))
    tGraph_500 = ROOT.TGraph(len(xValues), array.array("d", xValues), array.array("d", yValues_500))
    tGraph_800 = ROOT.TGraph(len(xValues), array.array("d", xValues), array.array("d", yValues_800))
    tGraph_1000 = ROOT.TGraph(len(xValues), array.array("d", xValues), array.array("d", yValues_1000))
    tGraph_Bkg  = ROOT.TGraph(len(xValues), array.array("d", xValues), array.array("d", yValues_Bkg))

    styles.getSignalStyleHToTB_M("200").apply(tGraph_250)
    styles.getSignalStyleHToTB_M("500").apply(tGraph_500)
    styles.getSignalStyleHToTB_M("800").apply(tGraph_800)
    styles.getSignalStyleHToTB_M("1000").apply(tGraph_1000)
    styles.getQCDLineStyle().apply(tGraph_Bkg)
    
    drawStyle = "CPE"
    effGraph_250  = histograms.HistoGraph(tGraph_250, "H^{+} m_{H^{+}} = 250 GeV", "lp", drawStyle)
    effGraph_500  = histograms.HistoGraph(tGraph_500, "H^{+} m_{H^{+}} = 500 GeV", "lp", drawStyle)
    effGraph_800  = histograms.HistoGraph(tGraph_800, "H^{+} m_{H^{+}} = 800 GeV", "lp", drawStyle)
    effGraph_1000 = histograms.HistoGraph(tGraph_1000, "H^{+} m_{H^{+}} = 1000 GeV", "lp", drawStyle)
    effGraph_Bkg  = histograms.HistoGraph(tGraph_Bkg, "Bkg", "lp", drawStyle)
    
    efficiencyList.append(effGraph_250)
    efficiencyList.append(effGraph_500)
    efficiencyList.append(effGraph_800)
    efficiencyList.append(effGraph_1000)
    efficiencyList.append(effGraph_Bkg)
    
    # Efficiency plot
    pE = plots.PlotBase(efficiencyList, saveFormats=["pdf"])
    pE.createFrame("QGLR_Efficiency")
    pE.setEnergy("13")
    pE.getFrame().GetYaxis().SetLabelSize(18)
    pE.getFrame().GetXaxis().SetLabelSize(20)
    
    pE.getFrame().GetYaxis().SetTitle("Efficiency / 0.01")
    pE.getFrame().GetXaxis().SetTitle("QGLR Cut")
    
    # Add Standard Texts to plot
    histograms.addStandardTexts()
    
    # Customise Legend
    moveLegend = {"dx": -0.50, "dy": -0.5, "dh": -0.1}
    pE.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegend))
    
    pE.draw()
#    plots.drawPlot(pE, "QGLR_Efficiency", **kwargs)
    SavePlot(pE, "QGLR_Efficiency", os.path.join(opts.saveDir, opts.optMode), saveFormats=[".png", ".pdf"] )
    


    #==========================================================================================
    # Calculate Significance
    #==========================================================================================
                
    SignalName = "ChargedHiggs_HplusTB_HplusToTB_M_800"
    
    hSignif_250  = p.histoMgr.getHisto(SignalName).getRootHisto().Clone(SignalName)
    hSignif_500  = p.histoMgr.getHisto(SignalName).getRootHisto().Clone(SignalName)
    hSignif_800  = p.histoMgr.getHisto(SignalName).getRootHisto().Clone(SignalName)
    hSignif_1000 = p.histoMgr.getHisto(SignalName).getRootHisto().Clone(SignalName)
    
    hSignif_250.Reset()
    hSignif_500.Reset()
    hSignif_800.Reset()
    hSignif_1000.Reset()
    
    hBkg = p.histoMgr.getHisto(SignalName).getRootHisto().Clone("Bkg")
    hBkg.Reset()
    
    # Bkg: FakeB + Genuine B
    hBkg.Add(hFakeB.getRootHisto(), +1)
    hBkg.Add(hGenuineB.getRootHisto(), +1)
    
    nBins = hSignif_250.GetNbinsX()+1

    # For-loop: All histo bins
    for i in range (1, nBins+1):
    
        sigmaB = ROOT.Double(0)
        
        b = hBkg.IntegralAndError(i, nBins, sigmaB)
        
        s_250 = hSignal_250.Integral(i, nBins)
        s_500 = hSignal_500.Integral(i, nBins)
        s_800 = hSignal_800.Integral(i, nBins)
        s_1000 = hSignal_1000.Integral(i, nBins)
        
        # Calculate significance
        signif_250 = stat.significance(s_250, b, sigmaB, option="Simple")#Asimov")
        signif_500 = stat.significance(s_500, b, sigmaB, option="Simple")#Asimov")
        signif_800 = stat.significance(s_800, b, sigmaB, option="Simple")#Asimov")
        signif_1000 = stat.significance(s_1000, b, sigmaB, option="Simple")#"Asimov")

        # Set signif for this bin
        hSignif_250.SetBinContent(i, signif_250)
        hSignif_500.SetBinContent(i, signif_500)
        hSignif_800.SetBinContent(i, signif_800)
        hSignif_1000.SetBinContent(i, signif_1000)
        
        # Apply style
        s_250 = styles.getSignalStyleHToTB_M("200")
        s_250.apply(hSignif_250)
        
        s_500 = styles.getSignalStyleHToTB_M("500")
        s_500.apply(hSignif_500)
        
        s_800 = styles.getSignalStyleHToTB_M("800")
        s_800.apply(hSignif_800)

        s_1000 = styles.getSignalStyleHToTB_M("1000")
        s_1000.apply(hSignif_1000)
        
        hList = []
        hList.append(hSignif_250)
        hList.append(hSignif_500)
        hList.append(hSignif_800)
        hList.append(hSignif_1000)
        
        hSignif_250.SetName("H^{+} m_{H^{+}} = 250 GeV")
        hSignif_500.SetName("H^{+} m_{H^{+}} = 500 GeV")
        hSignif_800.SetName("H^{+} m_{H^{+}} = 800 GeV")
        hSignif_1000.SetName("H^{+} m_{H^{+}} = 1000 GeV")
        
    pS = plots.PlotBase(hList, saveFormats=["png", "pdf"])
    pS.setLuminosity(opts.intLumi)
        
    # Drawing style
    pS.histoMgr.setHistoDrawStyleAll("HIST")
    pS.histoMgr.setHistoLegendStyleAll("L")
    pS.histoMgr.forEachHisto(lambda h: h.getRootHisto().SetMarkerSize(1.0))
    pS.histoMgr.forEachHisto(lambda h: h.getRootHisto().SetLineStyle(ROOT.kSolid))
    pS.histoMgr.forEachHisto(lambda h: h.getRootHisto().SetMarkerStyle(ROOT.kFullCircle))
    
    # Draw the plot
    name = "QGLR_Signif" + "GE"
    
    plots.drawPlot(pS, name, **kwargs)
    SavePlot(pS, name, os.path.join(opts.saveDir, opts.optMode), saveFormats=[".png", ".pdf"] ) 
    

        


    #=========================================================================================
    # Calculate the Transfer Factor (TF) and save to file
    #=========================================================================================
    Verbose("Write the normalisation factors to a python file", True)
    fileName = os.path.join(opts.mcrab, "FakeBTransferFactors%s.py"% ( getModuleInfoString(opts) ) )
    manager.writeNormFactorFile(fileName, opts)
    return
Exemplo n.º 12
0
def PlotMC(datasetsMgr, datasetsMgr1, histo, intLumi):

    kwargs = {}
    print "here1"
    if opts.normaliseToOne:
        p = plots.MCPlot(datasetsMgr,
                         histo,
                         normalizeToOne=True,
                         saveFormats=[".pdf", "png"],
                         **kwargs)
        p = plots.MCPlot(datasetsMgr1,
                         histo,
                         normalizeToOne=True,
                         saveFormats=[".pdf", ".png"],
                         **kwargs)
    else:
        p = plots.MCPlot(datasetsMgr,
                         histo,
                         normalizeToLumi=intLumi,
                         saveFormats=[".pdf", ".png"],
                         **kwargs)
        p = plots.MCPlot(datasetsMgr1,
                         histo,
                         normalizeToOne=True,
                         saveFormats=[".pdf", "png"],
                         **kwargs)

    # Draw the histograms
    _cutBox = None
    _rebinX = 1
    _format = "%0.0f"
    _xlabel = None
    logY = False
    _opts = {"ymin": 1e-3, "ymaxfactor": 1.0}
    print "here2"
    if "Pt" in histo:
        _rebinX = 2
        xMin = 0.0
        #        rebinX = 2
        xMax = 805.0
        #        xMax = 555.0 # For topPt < 500GeV
        xTitle = "p_{T} (GeV/c)"
        units = "GeV/c"
        _format = "%0.0f" + units
#        yTitle = "Efficiency / "   + str(binwidth) + " "+units
#        yMin = 0.0
#        yMax = 1.1

    else:
        pass

    if logY:
        yMaxFactor = 2.0
    else:
        yMaxFactor = 1.2

    _opts["ymaxfactor"] = yMaxFactor
    if opts.normaliseToOne:
        _opts["ymin"] = 1e-3
        #_opts   = {"ymin": 1e-3, "ymaxfactor": yMaxFactor, "xmax": None}
    else:
        _opts["ymin"] = 1e0
        #_opts["ymaxfactor"] = yMaxFactor
        #_opts   = {"ymin": 1e0, "ymaxfactor": yMaxFactor, "xmax": None}

    # Customise styling
    p.histoMgr.forEachHisto(
        lambda h: h.getRootHisto().SetLineStyle(ROOT.kSolid))

    print "here3"

    histoDef = datasetsMgr.getDataset("TT").getDatasetRootHisto(histo)
    histoDR = datasetsMgr1.getDataset("TT").getDatasetRootHisto(histo)

    histoDef.normalizeToOne()
    histoDR.normalizeToOne()

    hDef = histoDef.getHistogram()
    hDR = histoDR.getHistogram()

    p = plots.ComparisonPlot(histograms.Histo(hDR, "DeltaR08", "l", "L"),
                             histograms.Histo(hDef, "Default", "l", "L"))

    p.histoMgr.setHistoLegendLabelMany({
        "DeltaR08": "#DeltaR(q,q')>0.8",
        "Default": "Default"
    })

    p.histoMgr.forHisto("DeltaR08", styles.getQCDLineStyle())
    p.histoMgr.forEachHisto(
        lambda h: h.getRootHisto().SetLineStyle(ROOT.kSolid))
    p.histoMgr.setHistoDrawStyle("DeltaR08", "HIST")  #HIST
    p.histoMgr.setHistoLegendStyle("DeltaR08", "L")  #F

    p.histoMgr.setHistoDrawStyle("Default", "HIST")  #HIST
    p.histoMgr.setHistoLegendStyle("Default", "L")  #F

    p.histoMgr.forHisto("Default", styles.ttStyle)

    print "here4"

    # Customise style
    signalM = []
    for m in signalMass:
        signalM.append(m.rsplit("M_")[-1])
    for m in signalM:
        p.histoMgr.forHisto("ChargedHiggs_HplusTB_HplusToTB_M_%s" % m,
                            styles.getSignalStyleHToTB_M(m))
    print "here5"
    plots.drawPlot(
        p,
        histo,
        xlabel=_xlabel,
        ylabel="Arbitrary Units / %s" % (_format),
        log=logY,
        rebinX=_rebinX,
        cmsExtraText="Preliminary",
        createLegend={
            "x1": 0.58,
            "y1": 0.75,
            "x2": 0.92,
            "y2": 0.92
        },
        #                   createLegend = {"x1": 0.58, "y1": 0.65, "x2": 0.92, "y2": 0.92},
        opts=_opts,
        opts2={
            "ymin": 0.6,
            "ymax": 1.4
        },
        cutBox=_cutBox,
    )
    print "here6"
    # Save plot in all formats
    saveName = histo.split("/")[-1]
    savePath = os.path.join(opts.saveDir, "HplusMasses",
                            histo.split("/")[0], opts.optMode)
    SavePlot(p, saveName, savePath)
    print "here7"
    return
def PlotHisto(datasetsMgr, histoName):
    Verbose("Plotting histogram %s for Data, EWK, QCD " % (histoName), True)

    # Customize the histograms (BEFORE calculating purity obviously otherwise numbers are nonsense)
    _cutBox = None
    _rebinX = 1
    logY = True
    yMaxF = 1.2
    if logY:
        yMaxF = 10
    _opts = {"ymin": 1e-4, "ymaxfactor": yMaxF}
    _format = "%0.0f"
    _xlabel = None
    _ylabel = "Events / "
    _format = "/ %.0f "
    h = histoName.split("/")[-1]
    if "pt" in h.lower():
        _units = "GeV/c"
        _format = "%0.0f " + _units
        _xlabel = "p_{T} (%s)" % (_units)
        _cutBox = {
            "cutValue": 40.,
            "fillColor": 16,
            "box": False,
            "line": True,
            "greaterThan": True
        }
        _opts["xmax"] = 500.0
    if "eta" in h.lower():
        _rebinX = 1
        _format = "%0.2f"
        _cutBox = {
            "cutValue": 0.0,
            "fillColor": 16,
            "box": False,
            "line": True,
            "greaterThan": True
        }
        _opts["xmin"] = -3.0
        _opts["xmax"] = +3.0
    if "bjetbdisc" in h.lower():
        _format = "%0.2f"
        _opts["xmin"] = 0.0
        _opts["xmax"] = 1.2
        _cutBox = {
            "cutValue": 0.8484,
            "fillColor": 16,
            "box": False,
            "line": True,
            "greaterThan": True
        }
    if "pt_" in h.lower():
        _rebinX = 2

    _ylabel += _format

    # Get histos (Data, EWK) for Inclusive
    p1 = plots.ComparisonPlot(*getHistos(datasetsMgr, histoName))
    p1.histoMgr.normalizeMCToLuminosity(
        datasetsMgr.getDataset("Data").getLuminosity())

    # Clone histograms
    Data = p1.histoMgr.getHisto("Data").getRootHisto().Clone("Data")
    EWK = p1.histoMgr.getHisto("EWK").getRootHisto().Clone("EWK")
    QCD = p1.histoMgr.getHisto("Data").getRootHisto().Clone("QCD")

    # Rebin histograms (Before calculating Purity)
    Data.Rebin(_rebinX)
    EWK.Rebin(_rebinX)
    QCD.Rebin(_rebinX)

    # Get QCD = Data-EWK
    QCD.Add(EWK, -1)

    # Normalize histograms to unit area?
    if opts.normaliseToOne:
        if Data.Integral() > 0:
            Data.Scale(1.0 / Data.Integral())
        if QCD.Integral() > 0:
            QCD.Scale(1.0 / QCD.Integral())
        if EWK.Integral() > 0:
            EWK.Scale(1.0 / EWK.Integral())

    # Make the plots
    if opts.plotEWK:
        p = plots.ComparisonManyPlot(QCD, [EWK], saveFormats=[])
    else:
        p = plots.PlotBase([QCD], saveFormats=[])

    # Apply histo styles
    p.histoMgr.forHisto("QCD", styles.getQCDLineStyle())
    if opts.plotEWK:
        p.histoMgr.forHisto("EWK", styles.getEWKStyle())
        #p.histoMgr.forHisto("EWK", styles.getAltEWKLineStyle() )

    # Set draw style
    p.histoMgr.setHistoDrawStyle("QCD", "P")
    if opts.plotEWK:
        p.histoMgr.setHistoDrawStyle("EWK", "HIST")

    # Set legend style
    p.histoMgr.setHistoLegendStyle("QCD", "P")
    if opts.plotEWK:
        p.histoMgr.setHistoLegendStyle("EWK", "F")

    # Set legend labels
    if opts.plotEWK:
        p.histoMgr.setHistoLegendLabelMany({
            "QCD": "QCD",
            "EWK": "EWK",
        })
    else:
        p.histoMgr.setHistoLegendLabelMany({
            "QCD": "QCD",
        })

    # Do the plot
    plots.drawPlot(
        p,
        histoName,
        xlabel=_xlabel,
        ylabel=_ylabel,
        log=logY,
        rebinX=1,
        cmsExtraText="Preliminary",
        createLegend={
            "x1": 0.76,
            "y1": 0.80,
            "x2": 0.92,
            "y2": 0.92
        },
        opts=_opts,
        opts2={
            "ymin": 1e-5,
            "ymax": 1e0
        },
        ratio=False,
        ratioInvert=False,
        ratioYlabel="Ratio",
        cutBox=_cutBox,
    )

    # Save plot in all formats
    SavePlot(p, histoName,
             os.path.join(opts.saveDir, "FailedBJet",
                          opts.optMode))  #, saveFormats = [".png"] )
    return
Exemplo n.º 14
0
def PlotPurity(datasetsMgr, histoName):
    '''
    Create plots with "FakeB=Data-EWKGenuineB"
    '''
    Verbose("Plotting histogram %s for Data, EWK, QCD " % (histoName), True)

    # Which folder to use (redundant)
    defaultFolder = "FakeBPurity"
    genuineBFolder = defaultFolder + "EWKGenuineB"
    fakeBFolder = defaultFolder + "EWKFakeB"

    # Customize the histograms (BEFORE calculating purity obviously otherwise numbers are nonsense)
    _cutBox = None
    _rebinX = 1
    _opts = {"ymin": 1e-3, "ymax": 1.0}  #"ymaxfactor": 1.2}
    _format = "%0.0f"
    #_opts["xmax"] = xMax
    _xlabel = None
    _ylabel = "Purity / "
    _format = "/ %.0f "

    h = histoName.split("/")[-1]
    if "dijetm" in h.lower():
        _units = "GeV/c^{2}"
        _format = "%0.0f " + _units
        _xlabel = "m_{jj} (%s)" % (_units)
        _cutBox = {
            "cutValue": 80.399,
            "fillColor": 16,
            "box": False,
            "line": True,
            "greaterThan": True
        }
    if "trijetm" in h.lower():
        _rebinX = 2
        _units = "GeV/c^{2}"
        _format = "%0.0f " + _units
        _xlabel = "m_{jjb} (%s)" % _units
        _cutBox = {
            "cutValue": 173.21,
            "fillColor": 16,
            "box": False,
            "line": True,
            "greaterThan": True
        }
        _opts["xmax"] = 1000.0
    if "pt" in h.lower():
        _format = "%0.0f GeV/c"
    if "chisqr" in h.lower():
        _opts["xmax"] = 100.0
        if "allselections" in h.lower():
            _opts["xmax"] = 10.0
    #if histo.lower().endswith("met_et"):
    if h.lower().startswith("ht_"):
        _rebinX = 5
        _units = "GeV/c"
        _format = "%0.0f " + _units
        _xlabel = "H_{T} (%s)" % _units
        _cutBox = {
            "cutValue": 500.0,
            "fillColor": 16,
            "box": False,
            "line": False,
            "greaterThan": True
        }
        _opts["xmin"] = 500.0
        _opts["xmax"] = 3500.0
    if "eta" in h.lower():
        _rebinX = 1
        _format = "%0.2f"
        _cutBox = {
            "cutValue": 0.,
            "fillColor": 16,
            "box": False,
            "line": True,
            "greaterThan": True
        }
        _opts["xmin"] = -3.0
        _opts["xmax"] = +3.0
    if "deltaeta" in h.lower():
        _format = "%0.2f"
        _opts["xmin"] = 0.0
        _opts["xmax"] = 6.0
    if "bdisc" in h.lower():
        _format = "%0.2f"
    if "tetrajetm" in h.lower():
        _rebinX = 4
        _units = "GeV/c^{2}"
        _format = "%0.0f " + _units
        _xlabel = "m_{jjbb} (%s)" % (_units)
        _opts["xmax"] = 2500.0
    if "pt_" in h.lower():
        _rebinX = 2

    _ylabel += _format

    # Get histos (Data, EWK) for Inclusive
    p1 = plots.ComparisonPlot(*getHistos(datasetsMgr, histoName))
    p1.histoMgr.normalizeMCToLuminosity(
        datasetsMgr.getDataset("Data").getLuminosity())

    # Clone histograms
    Data = p1.histoMgr.getHisto("Data").getRootHisto().Clone("Data")
    EWK = p1.histoMgr.getHisto("EWK").getRootHisto().Clone("EWK")
    QCD = p1.histoMgr.getHisto("Data").getRootHisto().Clone("QCD")

    # Rebin histograms (Before calculating Purity)
    Data.Rebin(_rebinX)
    EWK.Rebin(_rebinX)
    QCD.Rebin(_rebinX)

    # Get QCD = Data-EWK
    QCD.Add(EWK, -1)

    # Comparison plot. The first argument is the reference histo. All other histograms are compared with respect to that.
    QCD_Purity, xMin, xMax, binList, valueDict, upDict, downDict = getPurityHisto(
        QCD, Data, inclusiveBins=False, printValues=False)
    EWK_Purity, xMin, xMax, binList, valueDict, upDict, downDict = getPurityHisto(
        EWK, Data, inclusiveBins=False, printValues=False)

    # Create TGraphs
    if 0:
        gQCD_Purity = MakeGraph(ROOT.kFullTriangleUp, ROOT.kOrange, binList,
                                valueDict, upDict, downDict)
        gEWK_Purity = MakeGraph(ROOT.kFullTriangleDown, ROOT.kPurple, binList,
                                valueDict, upDict, downDict)

    # Make the plots
    if opts.plotEWK:
        p = plots.ComparisonManyPlot(QCD_Purity, [EWK_Purity], saveFormats=[])
    else:
        p = plots.PlotBase([QCD_Purity], saveFormats=[])

    # Apply histo styles
    p.histoMgr.forHisto("QCD", styles.getQCDLineStyle())
    if opts.plotEWK:
        p.histoMgr.forHisto("EWK", styles.getAltEWKLineStyle())

    # Set draw style
    p.histoMgr.setHistoDrawStyle("QCD", "P")
    if opts.plotEWK:
        p.histoMgr.setHistoDrawStyle("EWK", "HIST")

    # Set legend style
    p.histoMgr.setHistoLegendStyle("QCD", "P")
    if opts.plotEWK:
        p.histoMgr.setHistoLegendStyle("EWK", "F")

    # Set legend labels
    if opts.plotEWK:
        p.histoMgr.setHistoLegendLabelMany({
            "QCD": "QCD",
            "EWK": "EWK",
        })
    else:
        p.histoMgr.setHistoLegendLabelMany({
            "QCD": "QCD",
        })

    # Do the plot
    plots.drawPlot(
        p,
        histoName,
        xlabel=_xlabel,
        ylabel=_ylabel,
        log=False,
        rebinX=1,  # must be done BEFORE calculating purity
        cmsExtraText="Preliminary",
        createLegend={
            "x1": 0.76,
            "y1": 0.80,
            "x2": 0.92,
            "y2": 0.92
        },
        opts=_opts,
        opts2={
            "ymin": 1e-5,
            "ymax": 1e0
        },
        ratio=False,
        ratioInvert=False,
        ratioYlabel="Ratio",
        cutBox=_cutBox,
    )

    # Save plot in all formats
    SavePlot(p, histoName,
             os.path.join(opts.saveDir, "Purity",
                          opts.optMode))  #, saveFormats = [".png"] )
    return
Exemplo n.º 15
0
def PurityPlots(datasetsMgr, histoName, analysisType="Inverted"):
    '''
    Create plots with "FakeB=Data-EWKGenuineB"
    '''
    Verbose("Plotting histogram %s for Data, EWK, QCD for %s" % (histoName, analysisType), True)

    # Sanity check
    IsBaselineOrInverted(analysisType)

    # Which folder to use (redundant)
    defaultFolder  = "FakeBPurity" 
    genuineBFolder = defaultFolder + "EWKGenuineB"
    fakeBFolder    = defaultFolder + "EWKFakeB"

    # Get histos (Data, EWK) for Inclusive
    p1 = plots.ComparisonPlot(*getHistos(datasetsMgr, histoName, analysisType) )
    p1.histoMgr.normalizeMCToLuminosity(datasetsMgr.getDataset("Data").getLuminosity())

    # Clone histograms 
    Data = p1.histoMgr.getHisto("Data").getRootHisto().Clone(analysisType + "-Data")
    EWK  = p1.histoMgr.getHisto("EWK").getRootHisto().Clone(analysisType  + "-EWK")
    QCD= p1.histoMgr.getHisto("Data").getRootHisto().Clone(analysisType + "-QCD")

    # Get QCD = Data-EWK
    QCD.Add(EWK, -1)

    # Comparison plot. The first argument is the reference histo. All other histograms are compared with respect to that. 
    QCD_Purity, xMin, xMax, binList, valueDict, upDict, downDict = getPurityHisto(QCD, Data, inclusiveBins=False, printValues=False)
    EWK_Purity  , xMin, xMax, binList, valueDict, upDict, downDict = getPurityHisto(EWK  , Data, inclusiveBins=False, printValues=False)

    # Create TGraphs
    if 0:
        gQCD_Purity = MakeGraph(ROOT.kFullTriangleUp, ROOT.kOrange, binList, valueDict, upDict, downDict)
        gEWK_Purity = MakeGraph(ROOT.kFullTriangleDown, ROOT.kPurple, binList, valueDict, upDict, downDict)
        
    # Make the plots
    p = plots.ComparisonManyPlot(QCD_Purity, [EWK_Purity], saveFormats=[])
    if 0:
        p = plots.PlotBase([gQCD_Purity, gEWK_Purity], saveFormats=[]) #legend/styles all wrog

    # Apply histo styles
    p.histoMgr.forHisto(analysisType + "-QCD", styles.getQCDLineStyle() )
    p.histoMgr.forHisto(analysisType + "-EWK"  , styles.getAltEWKLineStyle() )

    # Set draw style
    p.histoMgr.setHistoDrawStyle(analysisType + "-QCD", "P")
    p.histoMgr.setHistoDrawStyle(analysisType + "-EWK", "P")

    # Set legend style
    p.histoMgr.setHistoLegendStyle(analysisType + "-QCD", "P")
    p.histoMgr.setHistoLegendStyle(analysisType + "-EWK", "P")

    # Set legend labels
    p.histoMgr.setHistoLegendLabelMany({
            analysisType + "-QCD" : "QCD",
            analysisType + "-EWK" : "EWK",
            })
    
    # Draw the histograms
    _cutBox = None
    _rebinX = 1
    _opts   = {"ymin": 1e-3, "ymax": 1.0} #"ymaxfactor": 1.2}
    _format = "%0.0f"
    _opts["xmax"] = xMax
    _xlabel = None

    if "dijetm" in histoName.lower():
        _units  = "GeV/c^{2}" 
        _format = "%0.0f " + _units
        _xlabel = "m_{jj} (%s)" % (_units)
        _cutBox = {"cutValue": 80.399, "fillColor": 16, "box": False, "line": True, "greaterThan": True}
    if "trijetm" in histoName.lower():
        _units  = "GeV/c^{2}" 
        _format = "%0.0f " + _units
        _xlabel = "m_{jjb} (%s)" % _units
        _cutBox = {"cutValue": 173.21, "fillColor": 16, "box": False, "line": True, "greaterThan": True}
        _opts["xmax"] = 1000.0
    if "pt" in histoName.lower():
        _format = "%0.0f GeV/c" 
    if "eta" in histoName.lower():
        _format = "%0.2f" 
        _cutBox = {"cutValue": 0., "fillColor": 16, "box": False, "line": True, "greaterThan": True}
        _opts["xmin"] = -3.0
        _opts["xmax"] = +3.0
    if "deltaeta" in histoName.lower():
        _format = "%0.2f" 
        _opts["xmin"] =  0.0
        _opts["xmax"] = 6.0
    if "bdisc" in histoName.lower():
        _format = "%0.2f" 
    if "tetrajetm" in histoName.lower():
        _units  = "GeV/c^{2}" 
        _format = "%0.0f " + _units
        _xlabel = "m_{jjjb} (%s)" % (_units)
        _opts["xmax"] = 2500.0

    # Do the plot
    plots.drawPlot(p, 
                   histoName,  
                   xlabel        = _xlabel,
                   ylabel        = "Purity",
                   log           = False, 
                   cmsExtraText  = "Preliminary", 
                   createLegend  = {"x1": 0.80, "y1": 0.80, "x2": 0.92, "y2": 0.92},
                   opts          = _opts,
                   opts2         = {"ymin": 1e-5, "ymax": 1e0},
                   ratio         = False,
                   ratioInvert   = False, 
                   ratioYlabel   = "Ratio",
                   cutBox        = _cutBox,
                   )
    
    # Save plot in all formats
    SavePlot(p, histoName, os.path.join(opts.saveDir, "Purity", opts.optMode))#, saveFormats = [".png"] )
    return
def main(opts, signalMass):

    # Apply TDR style
    style = tdrstyle.TDRStyle()
    style.setOptStat(True)
    style.setGridX(False)
    style.setGridY(False)

    # If user does not define optimisation mode do all of them
    if opts.optMode == None:
        if len(optList) < 1:
            optList.append("")
        else:
            pass
        optModes = optList
    else:
        optModes = [opts.optMode]

    # For-loop: All optimisation modes
    for opt in optModes:
        opts.optMode = opt

        # Setup & configure the dataset manager
        datasetsMgr = GetDatasetsFromDir(opts)
        datasetsMgr.updateNAllEventsToPUWeighted()
        datasetsMgr.loadLuminosities()  # from lumi.json

        dir = opts.mcrab.replace("_DptOverPt0p32", "")
        print dir

        datasetsMgr_DR = GetDatasetsFromDir_second(opts, dir)
        datasetsMgr_DR.updateNAllEventsToPUWeighted()
        datasetsMgr_DR.loadLuminosities()  # from lumi.json

        if opts.verbose:
            datasetsMgr.PrintCrossSections()
            datasetsMgr.PrintLuminosities()

        # Set/Overwrite cross-sections
        for datasetsMgr_ in [datasetsMgr, datasetsMgr_DR]:
            for d in datasetsMgr_.getAllDatasets():
                if "ChargedHiggs" in d.getName():
                    datasetsMgr_.getDataset(d.getName()).setCrossSection(1.0)

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py)
        #plots.mergeRenameReorderForDataMC(datasetsMgr)

        # Print dataset information before removing anything?
        if 0:
            datasetsMgr.PrintInfo()

        # Determine integrated Lumi before removing data
        if "Data" in datasetsMgr.getAllDatasetNames():
            intLumi = datasetsMgr.getDataset("Data").getLuminosity()
        else:
            intLumi = 35920
        # Remove datasets
        filterKeys = ["Data", "TTZToQQ", "TTWJets", "TTTT"]
        #filterKeys = ["Data", "TTZToQQ", "TTWJets", "TTTT"]
        for key in filterKeys:
            datasetsMgr.remove(
                filter(lambda name: key in name,
                       datasetsMgr.getAllDatasetNames()))
            datasetsMgr_DR.remove(
                filter(lambda name: key in name,
                       datasetsMgr_DR.getAllDatasetNames()))
        # Re-order datasets
        datasetOrder = []
        for d in datasetsMgr.getAllDatasets():
            if "M_" in d.getName():
                if d not in signalMass:
                    continue
            datasetOrder.append(d.getName())

        # Append signal datasets
        for m in signalMass:
            datasetOrder.insert(0, m)
        datasetsMgr.selectAndReorder(datasetOrder)
        datasetsMgr_DR.selectAndReorder(datasetOrder)

        # Print dataset information
        datasetsMgr.PrintInfo()

        # Define the mapping histograms in numerator->denominator pairs

        Numerators = [
            "AllTopQuarkPt_MatchedBDT",
            "AllTopQuarkPt_Matched",
            "EventTrijetPt2T_MatchedBDT",
            "EventTrijetPt2T_MatchedBDT",
            "EventTrijetPt2T_MatchedBDT",
            "AllTopQuarkPt_MatchedBDT",
            "SelectedTrijetsPt_BjetPassCSVdisc_afterCuts",
            "TrijetFakePt_BDT",
        ]
        Denominators = [
            "AllTopQuarkPt_Matched",
            "TopQuarkPt",
            "EventTrijetPt2T_BDT",
            "EventTrijetPt2T_Matched",
            "EventTrijetPt2T",
            "TopQuarkPt",
            "SelectedTrijetsPt_afterCuts",
            "TrijetFakePt",
        ]

        if 0:
            datasetsMgr.merge("QCD", GetListOfQCDatasets())
            datasetsMgr_DR.merge("QCD", GetListOfQCDatasets())

            plots._plotStyles["QCD"] = styles.getQCDLineStyle()
        #Background1_Dataset = datasetsMgr.getDataset("QCD")

        # Merge histograms (see NtupleAnalysis/python/tools/plots.py)
        plots.mergeRenameReorderForDataMC(datasetsMgr)

        # For-loop: All numerator-denominator pairs
        #for i in range(len(Numerators)):
        if 1:
            numerator = os.path.join(opts.folder,
                                     "AfterAllSelections_LeadingTrijet_Pt_SR")
            denominator = os.path.join(
                opts.folder, "AfterStandardSelections_LeadingTrijet_Pt_SR")
            #PlotEfficiency_comparison(datasetsMgr, datasetsMgr_DR, intLumi)
            PlotEfficiency_comparison(datasetsMgr, numerator, denominator,
                                      datasetsMgr_DR, intLumi)
    return
Exemplo n.º 17
0
def PlotMC(datasetsMgr, histo, intLumi):

    kwargs = {}
    if opts.normaliseToOne:
        p = plots.MCPlot(datasetsMgr,
                         histo,
                         normalizeToOne=True,
                         saveFormats=[],
                         **kwargs)
    else:
        p = plots.MCPlot(datasetsMgr,
                         histo,
                         normalizeToLumi=intLumi,
                         saveFormats=[],
                         **kwargs)

    # Draw the histograms
    _cutBox = None
    _rebinX = 1
    _format = "%0.0f"
    _xlabel = None

    _opts = {"ymin": 1e-3, "ymaxfactor": 1.0}

    if "pt" in histo.lower():
        _rebinX = 2
        _units = "GeV/c"
        _format = "%0.0f " + _units
        _xlabel = "p_{T} (%s)" % _units
        _cutBox = {
            "cutValue": 173.21,
            "fillColor": 16,
            "box": False,
            "line": False,
            "greaterThan": True
        }
        #_opts["xmax"] = 505 #1005

    if "eta" in histo.lower():
        _units = ""
        _format = "%0.1f " + _units
        _xlabel = "#eta %s" % _units
        _cutBox = {
            "cutValue": 173.21,
            "fillColor": 16,
            "box": False,
            "line": False,
            "greaterThan": True
        }
        _opts["xmax"] = 2.5
        _opts["xmin"] = -2.5

    if "trijetmass" in histo.lower():
        _rebinX = 2
        _units = "GeV/c^{2}"
        _format = "%0.0f " + _units
        _xlabel = "m_{jjb} (%s)" % _units
        _cutBox = {
            "cutValue": 173.21,
            "fillColor": 16,
            "box": False,
            "line": True,
            "greaterThan": True
        }
        _opts["xmax"] = 505  #1005

    elif "tetrajetmass" in histo.lower():
        _rebinX = 8  #5 #10 #4
        _units = "GeV/c^{2}"
        _format = "%0.0f " + _units
        _xlabel = "m_{jjbb} (%s)" % (_units)
        _format = "%0.0f " + _units
        _opts["xmax"] = 1500  #3500.0

    elif "dijetmass" in histo.lower():
        _rebinX = 1  #5 #10 #4
        _units = "GeV/c^{2}"
        _format = "%0.0f " + _units
        _xlabel = "m_{W} (%s)" % (_units)
        _format = "%0.0f " + _units
        _opts["xmax"] = 800  #3500.0
        _cutBox = {
            "cutValue": 80.4,
            "fillColor": 16,
            "box": False,
            "line": True,
            "greaterThan": True
        }
        _opts["xmax"] = 300

    elif "tetrajetbjetpt" in histo.lower():
        _rebinX = 2
        _units = "GeV/c"
        _format = "%0.0f " + _units
        _xlabel = "p_{T}  (%s)" % (_units)
        _format = "%0.0f " + _units
        _opts["xmax"] = 800

    elif "ldgtrijetpt" in histo.lower():
        _rebinX = 2
        #        logY    = False
        _units = "GeV/c"
        _format = "%0.0f " + _units
        _xlabel = "p_{T}  (%s)" % (_units)
        _format = "%0.0f " + _units
        _opts["xmax"] = 800

    elif "bdt" in histo.lower():
        _format = "%0.1f"

    if "topTagSF" in histo:
        _rebinX = 5
        #kwargs["ylabel"] = "Events / %.3f "
        _format = "%0.2f "
        _opts["xmax"] = +10
        _opts["xmin"] = 0.0
        _xlabel = "topTag SF"

    else:
        pass

    if opts.normaliseToOne:
        logY = False
        Ylabel = "Arbitrary Units / %s" % (_format)
    else:
        logY = True
        Ylabel = "Events / %s" % (_format)

    if logY:
        yMaxFactor = 2.0
    else:
        yMaxFactor = 1.2

    _opts["ymaxfactor"] = yMaxFactor
    if opts.normaliseToOne:
        _opts["ymin"] = 1e-3
        #_opts   = {"ymin": 1e-3, "ymaxfactor": yMaxFactor, "xmax": None}
    else:
        _opts["ymin"] = 1e-3
        #_opts["ymaxfactor"] = yMaxFactor
        #_opts   = {"ymin": 1e0, "ymaxfactor": yMaxFactor, "xmax": None}

    # Customise styling
    p.histoMgr.forEachHisto(
        lambda h: h.getRootHisto().SetLineStyle(ROOT.kSolid))

    if "QCD" in datasetsMgr.getAllDatasetNames():
        p.histoMgr.forHisto("QCD",
                            styles.getQCDLineStyle())  #getQCDFillStyle() )
        p.histoMgr.setHistoDrawStyle("QCD", "HIST")
        p.histoMgr.setHistoLegendStyle("QCD", "F")

    if "TT" in datasetsMgr.getAllDatasetNames():
        TTStyle = styles.StyleCompound([
            styles.StyleFill(fillColor=ROOT.kMagenta - 2),
            styles.StyleLine(lineColor=ROOT.kMagenta - 2,
                             lineStyle=ROOT.kSolid,
                             lineWidth=3),
            styles.StyleMarker(markerSize=1.2,
                               markerColor=ROOT.kMagenta - 2,
                               markerSizes=None,
                               markerStyle=ROOT.kFullTriangleUp)
        ])
        p.histoMgr.forHisto("TT", TTStyle)
        p.histoMgr.setHistoDrawStyle("TT", "HIST")  #AP
        p.histoMgr.setHistoLegendStyle("TT", "F")  #LP

    # Customise style
    signalM = []
    for m in signalMass:
        signalM.append(m.rsplit("M_")[-1])
    for m in signalM:
        p.histoMgr.forHisto("ChargedHiggs_HplusTB_HplusToTB_M_%s" % m,
                            styles.getSignalStyleHToTB_M(m))

    plots.drawPlot(
        p,
        histo,
        xlabel=_xlabel,
        ylabel=Ylabel,
        log=logY,
        rebinX=_rebinX,
        cmsExtraText="Preliminary",
        createLegend={
            "x1": 0.58,
            "y1": 0.65,
            "x2": 0.92,
            "y2": 0.92
        },
        #createLegend = {"x1": 0.73, "y1": 0.85, "x2": 0.97, "y2": 0.77},
        opts=_opts,
        opts2={
            "ymin": 0.6,
            "ymax": 1.4
        },
        cutBox=_cutBox,
    )

    # Save plot in all formats
    saveName = histo.split("/")[-1]
    savePath = os.path.join(opts.saveDir, histo.split("/")[0], opts.optMode)
    '''
    if opts.normaliseToOne:
        save_path = savePath + opts.MVAcut
        if opts.noQCD:
            save_path = savePath + opts.MVAcut + "/noQCD/"
    else:
        save_path = savePath + opts.MVAcut + "/normToLumi/"
        if opts.noQCD:
            save_path = savePath + opts.MVAcut + "/noQCD/"
     '''

    SavePlot(p, saveName, savePath)

    return