Exemplo n.º 1
0
def doPlot(legList, graphList, saveName, **kwargs):

    # Definitions
    hgList = []
    lList = {}

    # For-loop: All TGraphs
    for i, g in enumerate(graphList, 0):
        if opts.boldText:
            gName = legList[i]
        else:
            gName = "#font[42]{%s}" % legList[i]
        hg = histograms.HistoGraph(graphList[i],
                                   gName,
                                   drawStyle="L",
                                   legendStyle="l")
        hgList.append(hg)

    # Create a plot-base object
    Verbose("Creating the plot-base object", True)
    # plot = plots.PlotBase(hgList, saveFormats=[])
    hgList.insert(0, hgList.pop(opts.refIndex))
    plot = plots.ComparisonManyPlot(hgList[0], hgList[1:], saveFormats=[])
    #plot = plots.ComparisonManyPlot(hgList[-1], hgList[:-1], saveFormats=[])

    # Apply histo style
    Verbose("Applying the histogram styles (generator)", True)
    plot.histoMgr.forEachHisto(styles.generator())

    # plot.histoMgr.forEachHisto(lambda h: h.getRootHisto().SetMarkerSize(1.2))
    # plot.histoMgr.forEachHisto(lambda h: h.getRootHisto().SetLineWidth(3))

    def sty(h):
        r = h.getRootHisto()
        r.SetLineWidth(3)
        r.SetMarkerSize(1.2)
        return

    # Apply style and set label
    Verbose("Applying the histogram styles (forEachHisto)", True)
    plot.histoMgr.forEachHisto(sty)
    if opts.plotType != "var":
        plot.setLegendHeader("Sequential Model (Keras)")

    # Draw the plot
    Verbose("Drawing the plot", True)
    plots.drawPlot(plot, saveName, **kwargs)
    if opts.removeLegend:
        plot.removeLegend()

    # Save plots and return
    Verbose("Saving the plot as %s" % (saveName), True)
    SavePlot(plot, opts.saveDir, saveName, opts.saveFormats)

    Verbose(
        "Plots saved under directory %s" %
        (sh_s + aux.convertToURL(opts.saveDir, opts.url) + sh_n), True)
    return
    def doSystematicsPlots(self):
        if self._mySystObject == None:
            return
        # Make sure results have been obtained
        if self._hSystematicsUp == None:
            self.doSystematicsForMetShapeDifference()
        # Create output directory
        if os.path.exists("%s/Systematics_%s"%(self._myDir, self._systName)):
            shutils.rmtree("%s/Systematics_%s"%(self._myDir, self._systName))
        os.mkdir("%s/Systematics_%s"%(self._myDir, self._systName))
        # Make plot for systematics (phase-space bins integrated)
        if self._hFinalShape != None:
            self._hFinalShape.SetLineColor(ROOT.kBlack)
            self._hSystematicsUp.SetLineColor(ROOT.kBlue)
            self._hSystematicsDown.SetLineColor(ROOT.kRed)
            myYmax = 15
            if "2012" in self._moduleInfoString:
                myYmax = 30
            plot = plots.ComparisonManyPlot(histograms.Histo(self._hFinalShape, "Nominal", drawStyle="E"),
                [histograms.Histo(self._hSystematicsUp, "Up", drawStyle="E"), histograms.Histo(self._hSystematicsDown, "Down", drawStyle="E")])
            plot.createFrame("%s/Systematics_%s/QCDInvertedShapeWithMetSyst_final_%s"%(self._myDir, self._systName, self._moduleInfoString), createRatio=True, opts2={"ymin": 0, "ymax": 2}, opts={"addMCUncertainty": True, "ymin": -5, "ymax": myYmax})
            plot.frame.GetXaxis().SetTitle(self._histoSpecs["xtitle"])
            plot.frame.GetYaxis().SetTitle(self._histoSpecs["ytitle"])
            plot.setLegend(histograms.createLegend(0.59, 0.70, 0.87, 0.90))
            plot.legend.SetFillColor(0)
            plot.legend.SetFillStyle(1001)
            styles.mcStyle(plot.histoMgr.getHisto("Up"))
            #plot.histoMgr.getHisto("Up").getRootHisto().SetMarkerSize(0)
            styles.mcStyle2(plot.histoMgr.getHisto("Down"))
            styles.dataStyle(plot.histoMgr.getHisto("Nominal"))
            self._drawPlot(plot)

        # Make plots for systematics (for each phase-space bin separately)
        nSplitBins = self._myCtrlRegion.getNumberOfPhaseSpaceSplitBins()
        myCtrlHistograms = self._mySystObject.getHistogramsForCtrlRegion()
        mySignalHistograms = self._mySystObject.getHistogramsForSignalRegion()
        myMinIndex = 0
        if myCtrlHistograms[0] == None:
            myMinIndex = 1 # Skip empty first bin
        for i in range(myMinIndex, nSplitBins):
            plot = plots.ComparisonPlot(histograms.Histo(mySignalHistograms[i].Clone(), "Signal region", drawStyle="E"),
                                        histograms.Histo(myCtrlHistograms[i].Clone(), "Ctrl. region", drawStyle="E"))
            plot.createFrame("%s/Systematics_%s/QCDInvertedShapeWithMetSyst_%s_%s"%(self._myDir, self._systName, self._myCtrlRegion.getPhaseSpaceBinFileFriendlyTitle(i), self._moduleInfoString), createRatio=True, opts2={"ymin": 0, "ymax": 2}, opts={"addMCUncertainty": True, "ymin": -0.05, "ymax": 0.3})
            plot.frame.GetXaxis().SetTitle(self._histoSpecs["xtitle"])
            plot.frame.GetYaxis().SetTitle("Arb. units. (Normalised to 1)")
            styles.mcStyle(plot.histoMgr.getHisto("Ctrl. region"))
            styles.dataStyle(plot.histoMgr.getHisto("Signal region"))
            plot.setLegend(histograms.createLegend(0.59, 0.77, 0.87, 0.90))
            plot.legend.SetFillColor(0)
            plot.legend.SetFillStyle(1001)
            #plot.histoMgr.getHisto("Up").getRootHisto().SetMarkerSize(0)
            self._drawPlot(plot)
            print "Saved MET shape systematics plot for bin %s"%self._myCtrlRegion.getPhaseSpaceBinFileFriendlyTitle(i)
Exemplo n.º 3
0
def createPlot(histo, myLumi, legendLabel, sLegStyle, sDrawStyle, **kwargs):
    
    style      = tdrstyle.TDRStyle()

    if isinstance(histo, ROOT.TH1):
        args = {"legendStyle": sLegStyle, "drawStyle": sDrawStyle}
        args.update(kwargs)
        histo.GetZaxis().SetTitle("")
        #p = plots.PlotBase([histograms.Histo(histo, legendLabel, **args)])
        p = plots.ComparisonManyPlot(histograms.Histo(histo, legendLabel, **args), [])
        p.setLuminosity(myLumi)
        return p
    else:
        print "*** ERROR: Histogram \"%s\" is not a valid instance of a ROOT.TH1" % (histo)
        sys.exit()
Exemplo n.º 4
0
def PlotTFsCompare(pName, sKeys, gList, isRatio=False):

    # Finally do a all-in=one plot
    plot = plots.ComparisonManyPlot(gList[0], gList[1:])
    for k in sKeys:
        plot.histoMgr.setHistoLegendLabelMany({
            k: k,
        })

    # For-loop: All graphs
    for i, s in enumerate(sKeys, 1):
        # Reference histo is exception (draw just a line)
        if s == opts.refHisto:
            # plot.histoMgr.setHistoDrawStyle(s, "L")
            # plot.histoMgr.setHistoLegendStyle(s, "L")

            # plot.histoMgr.setHistoDrawStyle(s, "C3") # curve && filled area is drawn through end points of errors
            # plot.histoMgr.setHistoLegendStyle(s, "F")

            # plot.histoMgr.setHistoDrawStyle(s, "P2")
            # plot.histoMgr.setHistoLegendStyle(s, "LP")

            plot.histoMgr.setHistoDrawStyle(
                s, "C4"
            )  # curve && smoothed filled area is drawn through end points of errors
            plot.histoMgr.setHistoLegendStyle(s, "LF")
        else:
            plot.histoMgr.setHistoDrawStyle(s, "P")
            plot.histoMgr.setHistoLegendStyle(s, "LP")
            #
            #plot.histoMgr.setHistoDrawStyle(s, "4")   # curve && smoothed filled area is drawn through end points of errors
            #plot.histoMgr.setHistoLegendStyle(s, "F")

    plot.setLegend(getLegend(opts, dx=+0.0, dy=-0.15))
    plot.createFrame(pName, saveFormats=[])
    plot.frame.GetXaxis().SetTitle("")
    plot.frame.GetYaxis().SetTitle("transfer factor")
    plot.draw()
    plot.setLuminosity(opts.intLumi)
    plot.addStandardTexts(addLuminosityText=False, cmsTextPosition="outframe")
    SavePlot(plot, pName, os.path.join(opts.saveDir, ""))
    return
def plotClosure(mt_nom, mts_var, name, optMode):
    #for mt_var in mts_var:
    #    printSummedDifference(mt_nom, mt_var)

    style = tdrstyle.TDRStyle()
    plot = plots.ComparisonManyPlot(mt_nom, mts_var)
    plot.createFrame(optMode.replace("Opt", "mT_Closure_" + name + "_"),
                     createRatio=True)

    moveLegend = {"dx": -0.325, "dy": 0.02, "dw": -0.14, "dh": -0.12}

    plot.setLegend(
        histograms.moveLegend(histograms.createLegend(), **moveLegend))
    histograms.addText(
        0.65, 0.3,
        optMode.replace("OptQCDTailKiller", "R_{BB} ").replace("Plus", ""), 25)

    histograms.addStandardTexts()

    plot.draw()
    plot.save()
Exemplo n.º 6
0
def main(argv):

    multicrabDir = sys.argv[1]
    if not os.path.exists(multicrabDir) or not os.path.isdir(multicrabDir):
        usage()


    # Apply TDR style
    style = tdrstyle.TDRStyle()

    # Construct datasets as stated in the multicrab.cfg of the execution
    # directory. The returned object is of type DatasetManager.

    datasets = dataset.getDatasetsFromMulticrabCfg(directory = multicrabDir)

    datasetlist = datasets.getMCDatasets()
    
    qlist_particle = ["Pt","Phi","Eta"]
    qlist_met = ["Et","Phi"]
    qlist_b = ["Pt","Eta"] 
    if mass < 175:
        objects = {
            "Hplus/associatedB" : qlist_particle, 
            "Hplus/hplus" : qlist_particle, 
            "Hplus/associatedT" : qlist_particle, 
            "Hplus/associatedSecondaryT" : qlist_particle, 
            "Hplus/associatedTau" : qlist_particle, 
            "Hplus/associatedOneProngTau" : qlist_particle, 
            "met" : qlist_met
        }
    else:
        objects = {
            "Hplus/associatedB" : qlist_particle, 
            "Hplus/associatedSecondaryB" : qlist_b, 
            "Hplus/hplus" : qlist_particle, 
            "Hplus/associatedT" : qlist_particle, 
            "Hplus/associatedTau" : qlist_particle, 
            "Hplus/associatedOneProngTau" : qlist_particle, 
            "met" : qlist_met
        } 
    
    colorlist = [1,2,4,5]
    print "These objects are considered: ", objects.keys()

    object_dict={
        "met" : "Gen MET", 
        "associatedB" : "associated b", 
        "associatedT" : "associated t",
        "associatedSecondaryB" : "b (from associated t decay)", 
        "associatedSecondaryT" : "t (from t#bar{t}, t#rightarrow bW)",  
        "hplus" : "H^{#pm}", 
        "associatedTau" : "associated #tau", 
        "associatedOneProngTau" : "associated 1-prong #tau"
    }
    quantity_dict={
        'Et' : '(GeV)', 
        "Pt" : "p_{T} (GeV)", 
        "Phi" : "#phi", 
        "Eta" : "#eta"
    }
    gen_dict = {
        'PYTHIA6' : 'Pythia 6 (LO)', 
        'PYTHIA8' : 'Pythia 8', 
        '2HDMII' : 'MG 2HDM type II', 
        '2HDMtypeII' : 'MG 2HDM type II', 
        'MSSM' :'MG5_aMC@NLO MSSM', 
        'LowTanb' : 'Low tan#beta', 
        '2HDMtypeII_LO' : 'MG5_aMC@NLO (LO)', 
        '2HDMtypeII_NLO' : 'MG5_aMC@NLO (NLO)'
    }


    for obj in objects.keys():
        for quantity in objects[obj]:
            HISTONAME = obj + quantity
            histos = [] 
            for i in range(0,len(datasetlist)):
                name = datasetlist[i].getName()
                histotuple = datasetlist[i].getRootHisto(HISTONAME)
                histo = histotuple[0].Clone()
                if histo.Integral() == 0:
                    print "Warning, histogram",histo.GetName(),"is empty"
                    continue
                if len(datasetlist) > 1:
                    histo.Scale(1/histo.Integral())
                histo.SetLineColor(colorlist[i])
                histo.SetName(gen_dict[name])
                histos.append(histo)

            myParams = {}

            if len(obj.split("/")) > 1:
                obj_name = obj.split("/")[1]
            else:
                obj_name = obj

            xmin = None
            xmax = None
            if quantity == "Phi":
                myParams["moveLegend"] = {"dx": -0.25, "dy": -0.2, "dh": -0.1}
                ymin = 0
                ymax = 0.012
            elif quantity == "Eta":
                myParams["moveLegend"] = {"dx": -0.25, "dy": 0.0, "dh": -0.1}
                myParams["log"] = True
                xmin = -2.5
                xmax = 2.5
                ymin = 0.005
                ymax = 0.05
                if mass > 175:
                    if "associatedB" in obj_name:
                        xmin = -4.0
                        xmax = 4.0
                        ymax = 0.02                        
            else:
                myParams["moveLegend"] = {"dx": -0.25, "dy": 0.0, "dh": -0.1}
                myParams["log"] = True 
                ymin = 0.00005
                ymax = 0.5
                if obj_name == "associatedB":
                    ymin = ymin/5
                if  mass > 175:
                    ymin = ymin*10

            if len(histos) > 1:
                myParams["ratio"] = True
                myParams["opts2"] = {"ymin": 0.0, "ymax":2.0}
                myParams["ylabel"] = "#LT Events / bin #GT, normalized"
                if xmin and xmax:
                    myParams["opts"] = {"xmin": xmin, "xmax": xmax, "ymin": ymin, "ymax": ymax}
                else:
                    myParams["opts"] = {"ymin": ymin, "ymax": ymax}
            else: 
                myParams["ylabel"] = "#LT Events / bin #GT"

            myParams["xlabel"] = object_dict[obj_name]+" "+quantity_dict[quantity]

            if len(histos) <= 1:
                plot = plots.PlotBase(histos)
            else:
                default = histos[0]
                compared = histos[1:]
                plot = plots.ComparisonManyPlot(default, compared)

            plot.saveFormats=formats
            tb = histograms.PlotTextBox(xmin=0.75, ymin=None, xmax=0.85, ymax=0.1, size=25, lineheight=0.035)
            tb.addText("m_{H^+} = %s GeV"%mass)
            plot.appendPlotObject(tb)

            histograms.createLegend.setDefaults(textSize = 0.045)

            def customYTitleOffset(p):
                if quantity != "Pt":
                    scale = 1.2
                else:
                    scale = 1
                yaxis = p.getFrame().GetYaxis()                                          
                yaxis.SetTitleOffset(scale*yaxis.GetTitleOffset())

            if len(histos) > 1:
                myParams["customise"] = customYTitleOffset

            if not os.path.exists(plotDir):
                os.mkdir(plotDir)
            plots.drawPlot(plot, os.path.join(plotDir,obj_name+quantity), **myParams)
Exemplo n.º 7
0
def DoPlots(datasetsMgr, histoName):
    analysisType = "Inverted"

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

    # Get histos
    inverted_Data = p1.histoMgr.getHisto("Data").getRootHisto().Clone(
        "Inverted-Data")
    inverted_EWK = p1.histoMgr.getHisto("EWK").getRootHisto().Clone(
        "Inverted-EWK")
    inverted_QCD = p1.histoMgr.getHisto("Data").getRootHisto().Clone(
        "Inverted-QCD")

    # Normalize histograms to unit area
    inverted_Data.Scale(1.0 / inverted_QCD.Integral())
    inverted_QCD.Scale(1.0 / inverted_QCD.Integral())
    inverted_EWK.Scale(1.0 / inverted_EWK.Integral())

    # Create the final plot object (Cannot subtract EWK from Data since the trigger in the MC samples is NOT prescaled. In data it is)
    p = plots.ComparisonManyPlot(inverted_Data, [inverted_EWK], saveFormats=[])
    #p = plots.PlotBase([inverted_Data], saveFormats=[])

    # Apply styles
    p.histoMgr.forHisto("Inverted-Data", styles.getDataStyle())
    #p.histoMgr.forHisto("Inverted-QCD"  , styles.getInvertedLineStyle() )
    p.histoMgr.forHisto("Inverted-EWK", styles.getAltEWKStyle())

    # Set draw style
    p.histoMgr.setHistoDrawStyle("Inverted-Data", "AP")
    #p.histoMgr.setHistoDrawStyle("Inverted-QCD" , "HIST")
    p.histoMgr.setHistoDrawStyle("Inverted-EWK", "AP")

    # Set legend style
    p.histoMgr.setHistoLegendStyle("Inverted-Data", "P")
    #p.histoMgr.setHistoLegendStyle("Inverted-QCD" , "L")
    p.histoMgr.setHistoLegendStyle("Inverted-EWK", "P")

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

    # Draw the histograms
    _cutBox = None
    _rebinX = 1
    #_opts   = {"ymin": 1e-4, "ymaxfactor": 2.0}
    _opts = {"ymin": 1e-4, "ymax": 1e-1}
    _xlabel = None
    _units = "GeV/c^{2}"
    _xlabel = "m_{jjb} (%s)" % _units
    _ylabel = "Arbitrary Units / %0.0f %s " % (inverted_QCD.GetBinWidth(0),
                                               _units)
    _cutBox = {
        "cutValue": 173.21,
        "fillColor": 16,
        "box": False,
        "line": True,
        "greaterThan": True
    }
    _opts["xmax"] = 1500.0

    # Complains (SysError in <TFile::TFile>: file ...)
    if 0:
        plots.drawPlot(p,
                       histoName,
                       xlabel=_xlabel,
                       ylabel=_ylabel,
                       log=True,
                       rebinX=_rebinX,
                       cmsExtraText="Preliminary",
                       createLegend={
                           "x1": 0.62,
                           "y1": 0.78,
                           "x2": 0.92,
                           "y2": 0.92
                       },
                       opts=_opts)

    if 1:
        plots.drawPlot(
            p,
            histoName,
            xlabel=_xlabel,
            ylabel=_ylabel,
            log=True,
            rebinX=_rebinX,
            cmsExtraText="Preliminary",
            createLegend={
                "x1": 0.62,
                "y1": 0.78,
                "x2": 0.92,
                "y2": 0.92
            },
            opts=_opts,
            opts2={
                "ymin": 0.0,
                "ymax": 2.0
            },
            ratio=True,
            ratioInvert=False,
            ratioYlabel="Ratio",
            cutBox=_cutBox,
        )
    # Save to ROOT file
    inverted_QCD.SaveAs("Inverted_QCD.root")

    # Save plot in all formats
    SavePlot(p, histoName, os.path.join(opts.saveDir, "Test", opts.optMode))
    return
def analyze(analysis=None):

    paths = [sys.argv[1]]

    if not analysis == None:
        datasets = dataset.getDatasetsFromMulticrabDirs(
            paths,
            analysisName=analysis,
            excludeTasks="Silver|GluGluHToTauTau_M125")
    else:
        datasets = dataset.getDatasetsFromMulticrabDirs(
            paths, excludeTasks="Silver|GluGluHToTauTau_M125")
        analysis = datasets.getAllDatasets()[0].getAnalysisName()

#    datasetsDY = None
    datasetsDY = dataset.getDatasetsFromMulticrabDirs(
        paths, analysisName=analysis, includeOnlyTasks="DYJetsToLL")
    #    datasets = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,excludeTasks="GluGluHToTauTau_M125|TTJets")
    datasetsH125 = None
    #    datasetsH125 = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,includeOnlyTasks="GluGluHToTauTau_M125",emptyDatasetsAsNone=True)
    #    datasetsH125 = dataset.getDatasetsFromMulticrabDirs(paths,analysisName=analysis,includeOnlyTasks="GluGluHToTauTau_M125")

    datasets.loadLuminosities()

    style = tdrstyle.TDRStyle()

    dataset1 = datasets.getDataDatasets()
    dataset2 = dataset1
    #    dataset2 = datasets.getMCDatasets()
    if not datasetsDY == None:
        dataset2 = datasetsDY.getMCDatasets()

    eff1 = getEfficiency(dataset1)
    eff2 = getEfficiency(dataset2)
    if isinstance(datasetsH125, dataset.DatasetManager):
        eff3 = getEfficiency(datasetsH125.getMCDatasets())

    styles.dataStyle.apply(eff1)
    styles.mcStyle.apply(eff2)
    eff1.SetMarkerSize(1)
    #    eff2.SetMarkerSize(1.5)
    if isinstance(datasetsH125, dataset.DatasetManager):
        styles.mcStyle.apply(eff3)
        eff3.SetMarkerSize(1.5)
        eff3.SetMarkerColor(4)
        eff3.SetLineColor(4)


#    p = plots.ComparisonPlot(histograms.HistoGraph(eff1, "eff1", "p", "P"),
#                             histograms.HistoGraph(eff2, "eff2", "p", "P"))

    if isinstance(datasetsH125, dataset.DatasetManager):
        p = plots.ComparisonManyPlot(
            histograms.HistoGraph(eff1, "eff1", "p", "P"), [
                histograms.HistoGraph(eff2, "eff2", "p", "P"),
                histograms.HistoGraph(eff3, "eff3", "p", "P")
            ])
    elif isinstance(datasetsDY, dataset.DatasetManager):
        p = plots.ComparisonPlot(histograms.HistoGraph(eff1, "eff1", "p", "P"),
                                 histograms.HistoGraph(eff2, "eff2", "p", "P"))
    else:
        p = plots.PlotBase([histograms.HistoGraph(eff1, "eff1", "p", "P")])

    fit("Data", p, eff1, 20, 200)
    fit("MC", p, eff2, 20, 200)
    if isinstance(datasetsH125, dataset.DatasetManager):
        fit("H125", p, eff3, 20, 200)

    opts = {"ymin": 0, "ymax": 1.1}
    opts2 = {"ymin": 0.5, "ymax": 1.5}
    #    moveLegend = {"dx": -0.55, "dy": -0.15, "dh": -0.1}
    moveLegend = {"dx": -0.2, "dy": -0.5, "dh": -0.1}
    name = "TauMET_" + analysis + "_DataVsMC_PFTauPt"

    legend1 = "Data"
    #    legend2 = "MC (DY)"
    legend2 = "Simulation"
    legend3 = "MC (H125)"
    createRatio = False
    p.histoMgr.setHistoLegendLabelMany({"eff1": legend1})
    if isinstance(datasetsDY, dataset.DatasetManager):
        p.histoMgr.setHistoLegendLabelMany({"eff1": legend1, "eff2": legend2})
        createRatio = True
    if isinstance(datasetsH125, dataset.DatasetManager):
        p.histoMgr.setHistoLegendLabelMany({
            "eff1": legend1,
            "eff2": legend2,
            "eff3": legend3
        })

    if createRatio:
        p.createFrame(os.path.join(plotDir, name),
                      createRatio=createRatio,
                      opts=opts,
                      opts2=opts2)
    else:
        p.createFrame(os.path.join(plotDir, name), opts=opts, opts2=opts2)
    p.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegend))

    p.getFrame().GetYaxis().SetTitle("HLT tau efficiency")
    #    p.getFrame().GetXaxis().SetTitle("#tau-jet p_{T} (GeV/c)")
    p.getFrame().GetXaxis().SetTitle("#tau_{h} p_{T} (GeV/c)")
    if createRatio:
        p.getFrame2().GetYaxis().SetTitle("Ratio")
        p.getFrame2().GetYaxis().SetTitleOffset(1.6)

    histograms.addText(0.5, 0.6, "LooseIsoPFTau50_Trk30_eta2p1", 17)
    #    label = analysis.split("_")[len(analysis.split("_")) -1]
    label = "2016"

    histograms.addText(0.5, 0.53, label, 17)
    runRange = datasets.loadRunRange()
    histograms.addText(0.5, 0.46, "Runs " + runRange, 17)

    p.draw()
    lumi = 0.0
    for d in datasets.getDataDatasets():
        print "luminosity", d.getName(), d.getLuminosity()
        lumi += d.getLuminosity()
    print "luminosity, sum", lumi
    histograms.addStandardTexts(lumi=lumi)

    if not os.path.exists(plotDir):
        os.mkdir(plotDir)
    p.save(formats)

    pythonWriter.addParameters(plotDir, label, runRange, lumi, eff1)
    pythonWriter.addMCParameters(label, eff2)

    pythonWriter.writeJSON(
        os.path.join(plotDir, "tauLegTriggerEfficiency_" + label + ".json"))

    #    if not createRatio:
    #        sys.exit()

    #########################################################################

    eff1eta = getEfficiency(dataset1, "NumeratorEta", "DenominatorEta")
    eff2eta = getEfficiency(dataset2, "NumeratorEta", "DenominatorEta")
    if isinstance(datasetsH125, dataset.DatasetManager):
        eff3eta = getEfficiency(datasetsH125.getMCDatasets(), "NumeratorEta",
                                "DenominatorEta")

    styles.dataStyle.apply(eff1eta)
    styles.mcStyle.apply(eff2eta)
    eff1eta.SetMarkerSize(1)

    if isinstance(datasetsH125, dataset.DatasetManager):
        styles.mcStyle.apply(eff3eta)
        eff3eta.SetMarkerSize(1.5)
        eff3eta.SetMarkerColor(4)
        eff3eta.SetLineColor(4)

    if isinstance(datasetsH125, dataset.DatasetManager):
        p_eta = plots.ComparisonManyPlot(
            histograms.HistoGraph(eff1eta, "eff1eta", "p", "P"), [
                histograms.HistoGraph(eff2eta, "eff2eta", "p", "P"),
                histograms.HistoGraph(eff3eta, "eff3eta", "p", "P")
            ])
    elif isinstance(datasetsDY, dataset.DatasetManager):
        p_eta = plots.ComparisonPlot(
            histograms.HistoGraph(eff1eta, "eff1eta", "p", "P"),
            histograms.HistoGraph(eff2eta, "eff2eta", "p", "P"))
    else:
        p_eta = plots.PlotBase(
            [histograms.HistoGraph(eff1eta, "eff1eta", "p", "P")])

    p_eta.histoMgr.setHistoLegendLabelMany({"eff1eta": legend1})
    if isinstance(datasetsDY, dataset.DatasetManager):
        p_eta.histoMgr.setHistoLegendLabelMany({
            "eff1eta": legend1,
            "eff2eta": legend2
        })
    if isinstance(datasetsH125, dataset.DatasetManager):
        p_eta.histoMgr.setHistoLegendLabelMany({
            "eff1eta": legend1,
            "eff2eta": legend2,
            "eff3eta": legend3
        })

    name = "TauMET_" + analysis + "_DataVsMC_PFTauEta"

    if createRatio:
        p_eta.createFrame(os.path.join(plotDir, name),
                          createRatio=createRatio,
                          opts=opts,
                          opts2=opts2)
    else:
        p_eta.createFrame(os.path.join(plotDir, name), opts=opts, opts2=opts2)

    moveLegendEta = {"dx": -0.5, "dy": -0.65, "dh": -0.1}
    p_eta.setLegend(
        histograms.moveLegend(histograms.createLegend(), **moveLegendEta))

    p_eta.getFrame().GetYaxis().SetTitle("HLT tau efficiency")
    p_eta.getFrame().GetXaxis().SetTitle("#tau-jet #eta")
    if createRatio:
        p_eta.getFrame2().GetYaxis().SetTitle("Ratio")
        p_eta.getFrame2().GetYaxis().SetTitleOffset(1.6)

    histograms.addText(0.2, 0.46, "LooseIsoPFTau50_Trk30_eta2p1", 17)
    histograms.addText(0.2, 0.38, label, 17)
    histograms.addText(0.2, 0.31, "Runs " + datasets.loadRunRange(), 17)

    p_eta.draw()
    histograms.addStandardTexts(lumi=lumi)

    p_eta.save(formats)

    #########################################################################

    eff1phi = getEfficiency(dataset1, "NumeratorPhi", "DenominatorPhi")
    eff2phi = getEfficiency(dataset2, "NumeratorPhi", "DenominatorPhi")
    if isinstance(datasetsH125, dataset.DatasetManager):
        eff3phi = getEfficiency(datasetsH125.getMCDatasets(), "NumeratorPhi",
                                "DenominatorPhi")

    styles.dataStyle.apply(eff1phi)
    styles.mcStyle.apply(eff2phi)
    eff1phi.SetMarkerSize(1)

    if isinstance(datasetsH125, dataset.DatasetManager):
        styles.mcStyle.apply(eff3phi)
        eff3phi.SetMarkerSize(1.5)
        eff3phi.SetMarkerColor(4)
        eff3phi.SetLineColor(4)

    if isinstance(datasetsH125, dataset.DatasetManager):
        p_phi = plots.ComparisonManyPlot(
            histograms.HistoGraph(eff1phi, "eff1phi", "p", "P"), [
                histograms.HistoGraph(eff2phi, "eff2phi", "p", "P"),
                histograms.HistoGraph(eff3phi, "eff3phi", "p", "P")
            ])
    elif isinstance(datasetsDY, dataset.DatasetManager):
        p_phi = plots.ComparisonPlot(
            histograms.HistoGraph(eff1phi, "eff1phi", "p", "P"),
            histograms.HistoGraph(eff2phi, "eff2phi", "p", "P"))
    else:
        p_phi = plots.PlotBase(
            [histograms.HistoGraph(eff1phi, "eff1phi", "p", "P")])

    p_phi.histoMgr.setHistoLegendLabelMany({"eff1phi": legend1})
    if isinstance(datasetsDY, dataset.DatasetManager):
        p_phi.histoMgr.setHistoLegendLabelMany({
            "eff1phi": legend1,
            "eff2phi": legend2
        })
    if isinstance(datasetsH125, dataset.DatasetManager):
        p_phi.histoMgr.setHistoLegendLabelMany({
            "eff1phi": legend1,
            "eff2phi": legend2,
            "eff3phi": legend3
        })

    name = "TauMET_" + analysis + "_DataVsMC_PFTauPhi"

    if createRatio:
        p_phi.createFrame(os.path.join(plotDir, name),
                          createRatio=createRatio,
                          opts=opts,
                          opts2=opts2)
    else:
        p_phi.createFrame(os.path.join(plotDir, name), opts=opts, opts2=opts2)

    moveLegendPhi = {"dx": -0.5, "dy": -0.65, "dh": -0.1}
    p_phi.setLegend(
        histograms.moveLegend(histograms.createLegend(), **moveLegendPhi))

    p_phi.getFrame().GetYaxis().SetTitle("HLT tau efficiency")
    p_phi.getFrame().GetXaxis().SetTitle("#tau-jet #phi")
    if createRatio:
        p_phi.getFrame2().GetYaxis().SetTitle("Ratio")
        p_phi.getFrame2().GetYaxis().SetTitleOffset(1.6)

    histograms.addText(0.2, 0.46, "LooseIsoPFTau50_Trk30_eta2p1", 17)
    histograms.addText(0.2, 0.38, label, 17)
    histograms.addText(0.2, 0.31, "Runs " + datasets.loadRunRange(), 17)

    p_phi.draw()
    histograms.addStandardTexts(lumi=lumi)

    p_phi.save(formats)

    #########################################################################

    namePU = "TauMET_" + analysis + "_DataVsMC_nVtx"

    eff1PU = getEfficiency(dataset1, "NumeratorPU", "DenominatorPU")
    eff2PU = getEfficiency(dataset2, "NumeratorPU", "DenominatorPU")

    styles.dataStyle.apply(eff1PU)
    styles.mcStyle.apply(eff2PU)
    eff1PU.SetMarkerSize(1)
    eff2PU.SetMarkerSize(1.5)

    if isinstance(datasetsDY, dataset.DatasetManager):
        pPU = plots.ComparisonManyPlot(
            histograms.HistoGraph(eff1PU, "eff1", "p", "P"),
            [histograms.HistoGraph(eff2PU, "eff2", "p", "P")])
        pPU.histoMgr.setHistoLegendLabelMany({
            "eff1": legend1,
            "eff2": legend2
        })
    else:
        pPU = plots.PlotBase([histograms.HistoGraph(eff1PU, "eff1", "p", "P")])
        pPU.histoMgr.setHistoLegendLabelMany({"eff1": legend1})

    optsPU = {"ymin": 0.01, "ymax": 1.0}
    createRatio = False
    if createRatio:
        pPU.createFrame(os.path.join(plotDir, namePU),
                        createRatio=True,
                        opts=optsPU,
                        opts2=opts2)
    else:
        pPU.createFrame(os.path.join(plotDir, namePU),
                        opts=optsPU,
                        opts2=opts2)

    moveLegend = {"dx": -0.5, "dy": -0.5, "dh": -0.1}
    pPU.setLegend(
        histograms.moveLegend(histograms.createLegend(), **moveLegend))
    #    if createRatio:
    #        pPU.getPad1().SetLogy(True)
    #    else:
    #        pPU.getPad().SetLogy(True)

    pPU.getFrame().GetYaxis().SetTitle("HLT tau efficiency")
    pPU.getFrame().GetXaxis().SetTitle("Number of reco vertices")
    if createRatio:
        pPU.getFrame2().GetYaxis().SetTitle("Ratio")
        pPU.getFrame2().GetYaxis().SetTitleOffset(1.6)

    histograms.addText(0.2, 0.6, "LooseIsoPFTau50_Trk30_eta2p1", 17)
    histograms.addText(0.2, 0.53, label, 17)
    histograms.addText(0.2, 0.46, "Runs " + datasets.loadRunRange(), 17)

    pPU.draw()
    histograms.addStandardTexts(lumi=lumi)

    pPU.save(formats)

    #########################################################################
    """
    hName = "Pull"
#    hName = "Sub"
    namePull = "TauMET_"+analysis+"_DataVsMC_"+hName+"s"

    plots.mergeRenameReorderForDataMC(datasets)
    datasets.merge("MC", ["TT","WJets","DYJetsToLL","SingleTop","QCD"], keepSources=True)

    drh1 = datasets.getDataset("Data").getDatasetRootHisto(hName)
    drh2 = datasets.getDataset("MC").getDatasetRootHisto(hName)
    drh1.normalizeToOne()
    drh2.normalizeToOne()
    pull1 = drh1.getHistogram()
    pull2 = drh2.getHistogram()

    if isinstance(datasetsH125,dataset.DatasetManager):
        plots.mergeRenameReorderForDataMC(datasetsH125)
        drh3 = datasetsH125.getMCDatasets()[0].getDatasetRootHisto(hName)
        drh3.normalizeToOne()
        pull3 = drh3.getHistogram()

    styles.dataStyle.apply(pull1)
    styles.mcStyle.apply(pull2)
    pull1.SetMarkerSize(1)

    if isinstance(datasetsH125,dataset.DatasetManager):
        styles.mcStyle.apply(pull3)
        pull3.SetMarkerSize(1.5)
        pull3.SetMarkerColor(4)
        pull3.SetLineColor(4)

    if isinstance(datasetsH125,dataset.DatasetManager):
        p_pull = plots.ComparisonManyPlot(histograms.Histo(pull1, "pull1", "p", "P"),
                                         [histograms.Histo(pull2, "pull2", "p", "P"),
                                          histograms.Histo(pull3, "pull3", "p", "P")])
    else:
        p_pull = plots.ComparisonPlot(histograms.Histo(pull1, "pull1", "p", "P"),
                                      histograms.Histo(pull2, "pull2", "p", "P"))

    p_pull.histoMgr.setHistoLegendLabelMany({"pull1": legend1, "pull2": legend2})
    if isinstance(datasetsH125,dataset.DatasetManager):
        p_pull.histoMgr.setHistoLegendLabelMany({"pull1": legend1, "pull2": legend2, "pull3": legend3})

    p_pull.createFrame(os.path.join(plotDir, namePull), createRatio=True, opts=opts, opts2=opts2)
    moveLegendPull = {"dx": -0.5, "dy": -0.35, "dh": -0.1}
    p_pull.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegendPull))

    p_pull.getFrame().GetYaxis().SetTitle("Arbitrary units")
#    p_pull.getFrame().GetXaxis().SetTitle("HLT #tau p_{T} - #tau-jet p_{T} (GeV/c)")
    p_pull.getFrame().GetXaxis().SetTitle("HLT #tau p_{T}/ #tau-jet p_{T} - 1")                                                                                                                                     
    p_pull.getFrame2().GetYaxis().SetTitle("Ratio")
    p_pull.getFrame2().GetYaxis().SetTitleOffset(1.6)

    histograms.addText(0.2, 0.75, "LooseIsoPFTau50_Trk30_eta2p1", 17)
    histograms.addText(0.2, 0.68, analysis.split("_")[len(analysis.split("_")) -1], 17)
    histograms.addText(0.2, 0.61, "Runs "+runRange, 17)

    p_pull.draw()

    histograms.addStandardTexts(lumi=lumi)
    p_pull.save(formats)
    """
    #########################################################################
    print "Output written in", plotDir
Exemplo n.º 9
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.º 10
0
def BaselineVsInvertedComparison(datasetsMgr, histoName):
    
    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 Data histos    
    baseline_Data = p1.histoMgr.getHisto("Baseline-Data").getRootHisto().Clone("Baseline-Data")
    inverted_Data = p1.histoMgr.getHisto("Inverted-Data").getRootHisto().Clone("Inverted-Data")

    # Get EWK histos
    baseline_EWK = p2.histoMgr.getHisto("Baseline-EWK").getRootHisto().Clone("Baseline-EWK")
    inverted_EWK = p2.histoMgr.getHisto("Inverted-EWK").getRootHisto().Clone("Inverted-EWK")

    # Create QCD histos: QCD = Data-EWK
    baseline_QCD = p1.histoMgr.getHisto("Baseline-Data").getRootHisto().Clone("Baseline-QCD")
    baseline_QCD.Add(baseline_EWK, -1)
    inverted_QCD = p1.histoMgr.getHisto("Inverted-Data").getRootHisto().Clone("Inverted-QCD")
    inverted_QCD.Add(inverted_EWK, -1)

    # Normalize histograms to unit area
    baseline_QCD.Scale(1.0/baseline_QCD.Integral())
    inverted_QCD.Scale(1.0/inverted_QCD.Integral())

    # Create the final plot object
    p = plots.ComparisonManyPlot(baseline_QCD, [inverted_QCD], saveFormats=[]) #[".C", ".png", ".pdf"])
    p.setLuminosity(GetLumi(datasetsMgr))
        
    # Apply styles
    p.histoMgr.forHisto("Baseline-QCD" , styles.getBaselineStyle() )
    p.histoMgr.forHisto("Inverted-QCD" , styles.getInvertedStyle() )

    # Set draw style
    p.histoMgr.setHistoDrawStyle("Baseline-QCD", "AP")
    p.histoMgr.setHistoLegendStyle("Baseline-QCD", "LP")
    p.histoMgr.setHistoDrawStyle("Inverted-QCD", "HIST")
    p.histoMgr.setHistoLegendStyle("Inverted-QCD", "F")
    # p.histoMgr.setHistoLegendStyleAll("LP")

    # Set legend labels
    p.histoMgr.setHistoLegendLabelMany({
            "Baseline-QCD" : "Baseline (QCD)",
            "Inverted-QCD" : "Inverted (QCD)",
            })

    # Draw the histograms
    _rebinX = 1
    _cutBox = None
    _opts   = {"ymin": 8e-5, "ymaxfactor": 2.0}

    if "Pt_" in histoName:
        _format = "%0.f GeV/c"
        if "tetrajet" in histoName.lower():
            _rebinX = 2
            
    if "ChiSqr" in histoName:
        _format = "%0.1f"
        _rebinX = 10
        if "After" in histoName:
            _rebinX = 1
            _opts["xmax"] = 20.0
    if "Mass" in histoName:
        _format = "%0.0f GeV/c^{2}"
        _rebinX = 2
        if "tetrajet" in histoName.lower():
            _rebinX = 5
            _opts["xmax"] = 3000.0
    if "BDisc" in histoName:
        _format = "%0.2f"
        _rebinX = 2
        _opts["xmax"] = 1.01
    if "Eta" in histoName:
        _format = "%0.2f"
    if "trijet" in histoName.lower():
        _cutBox = {"cutValue": 173.21, "fillColor": 16, "box": False, "line": True, "greaterThan": True}
    if "dijet" in histoName.lower():
        _cutBox = {"cutValue": 80.399, "fillColor": 16, "box": False, "line": True, "greaterThan": True}
    if "TetrajetMass" in histoName:
        _opts   = {"ymin": 8e-5, "ymaxfactor": 2.0, "xmax": 3000.0}
                
    plots.drawPlot(p, histoName,  
                   ylabel       = "Arbitrary Units / %s" % (_format),
                   log          = True, 
                   rebinX       = _rebinX, cmsExtraText = "Preliminary", 
                   createLegend = {"x1": 0.62, "y1": 0.78, "x2": 0.92, "y2": 0.92},
                   opts         = _opts,
                   opts2        = {"ymin": 0.6, "ymax": 1.4},
                   ratio        = True,
                   ratioInvert  = False, 
                   ratioYlabel  = "Ratio",
                   cutBox       = _cutBox,
                   )
    # Save plot in all formats
    SavePlot(p, histoName, os.path.join(opts.saveDir, "BaselineVsInverted") ) 
    return
Exemplo n.º 11
0
def main(argv):

    dirs = []
    if len(sys.argv) < 2:
        usage()

    dirs.append(sys.argv[1])

    dirs_signal = ["../../SignalAnalysis_140605_143702/"]

    QCDInvertedNormalization = sort(
        QCDInvertedNormalizationFactors.QCDInvertedNormalization)
    labels, QCDInvertedNormalizationFilteredEWKFakeTaus = getSortedLabelsAndFactors(
        QCDInvertedNormalizationFactorsFilteredEWKFakeTaus.
        QCDInvertedNormalization)

    analysis_inverted = "signalAnalysisInvertedTau"
    analysis = "signalAnalysis"

    optModes = []
    #optModes.append("OptQCDTailKillerZeroPlus")
    optModes.append("OptQCDTailKillerLoosePlus")
    optModes.append("OptQCDTailKillerMediumPlus")
    optModes.append("OptQCDTailKillerTightPlus")

    varHistoName = "shapeEWKGenuineTausTransverseMass"
    nomHistoName = "shapeTransverseMass"
    signalHistoName = "shapeEWKFakeTausTransverseMass"

    #Optimal: 0.8, 0.82, 0.9

    ###w_list = [0.65, 0.7, 0.76] #old baseline ft
    w_list = [0.66, 0.67, 0.75]
    #w_list = [0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 1]

    #defaultBinning = systematics.getBinningForPlot("shapeTransverseMass")
    defaultBinning = [0, 20, 40, 60, 80, 100, 120, 140, 160, 200, 400]
    defaultBinning_array = array.array("d", defaultBinning)

    diff_opt = []
    for optMode in optModes:
        diff_list = []
        for w in w_list:
            var_values = []
            nom_values = []

            # baseline fake taus
            mt_baseline_faketaus_data = getDataSets(dirs_signal, dataEra,
                                                    searchMode, analysis,
                                                    optMode)
            mtplot_signalfaketaus = plots.DataMCPlot(mt_baseline_faketaus_data,
                                                     signalHistoName)
            mt_signalfaketaus = mtplot_signalfaketaus.histoMgr.getHisto(
                "EWK").getRootHisto().Clone(signalHistoName)

            # inverted fake taus
            mt_inverted_faketaus_data = getDataSets(dirs, dataEra, searchMode,
                                                    analysis_inverted, optMode)

            histonames_var = mt_inverted_faketaus_data.getDataset(
                "Data").getDirectoryContent(varHistoName)
            histonames_nom = mt_inverted_faketaus_data.getDataset(
                "Data").getDirectoryContent(nomHistoName)

            bins_var = getBins(histonames_var, varHistoName)
            bins_nom = getBins(histonames_nom, nomHistoName)

            normalization_var_qg = getNormalization(
                bins_var, w, QCDInvertedNormalizationFilteredEWKFakeTaus, True,
                True)
            normalization_var = getNormalization(
                bins_var, w, QCDInvertedNormalizationFilteredEWKFakeTaus, True,
                False)
            normalization_nom = getNormalization(bins_nom, w,
                                                 QCDInvertedNormalization,
                                                 False, False)

            mt_var_qg = getMt(mt_inverted_faketaus_data, bins_var,
                              varHistoName, normalization_var_qg)
            mt_var = getMt(mt_inverted_faketaus_data, bins_var, varHistoName,
                           normalization_var)
            mt_nom = getMt(mt_inverted_faketaus_data, bins_nom, nomHistoName,
                           normalization_nom)

            mt_nom.Add(mt_signalfaketaus)

            mt_var_qg.SetName(
                "QCD(Data)+EWK+t#bar{t}(Data, mis-ID. #tau), q-g bal.")
            mt_var.SetName("QCD(Data)+EWK+t#bar{t}(Data, mis-ID. #tau)")
            mt_nom.SetName("QCD(Data)+EWK+t#bar{t}(MC, mis-ID. #tau)")

            mt_var_qg.SetLineWidth(4)
            mt_var.SetLineColor(14)
            mt_nom.SetLineColor(2)

            mt_var_qg = mt_var_qg.Rebin(
                len(defaultBinning) - 1, "", defaultBinning_array)
            mt_var = mt_var.Rebin(
                len(defaultBinning) - 1, "", defaultBinning_array)
            mt_nom = mt_nom.Rebin(
                len(defaultBinning) - 1, "", defaultBinning_array)

            for i in range(0, mt_nom.GetSize()):
                var_values.append(mt_var_qg.GetBinContent(i))
                nom_values.append(mt_nom.GetBinContent(i))

            style = tdrstyle.TDRStyle()

            varPlots = [mt_var, mt_var_qg]
            plot = plots.ComparisonManyPlot(mt_nom, varPlots)
            plot.createFrame(optMode.replace(
                "Opt", "Mt_DataDrivenVsMC_" + "w=" + str(w) + "_"),
                             createRatio=True)

            moveLegend = {"dx": -0.35, "dy": 0.05}
            plot.setLegend(
                histograms.moveLegend(histograms.createLegend(), **moveLegend))
            histograms.addText(
                0.65, 0.3,
                optMode.replace("OptQCDTailKiller",
                                "R_{BB} ").replace("Plus", ""), 25)
            histograms.addCmsPreliminaryText()
            histograms.addEnergyText()
            lumi = mt_inverted_faketaus_data.getDataset("Data").getLuminosity()
            histograms.addLuminosityText(x=None, y=None, lumi=lumi)

            plot.draw()
            plot.save()

            mt_var_qg.Delete()
            mt_var.Delete()
            mt_nom.Delete()

            #TFile.CurrentFile().Close("R")
            mt_baseline_faketaus_data.close()
            mt_inverted_faketaus_data.close()
            ROOT.gROOT.CloseFiles()
            ROOT.gROOT.GetListOfCanvases().Delete()
            ROOT.gDirectory.GetList().Delete()

            #print var_values
            #print nom_values

            # difference metrics
            num = 0
            denom = 0
            for i in range(0, len(nom_values)):
                num += var_values[i] * (var_values[i] - nom_values[i])**2
                denom += var_values[i]
            diff = num / denom
            diff_list.append(diff)
        diff_opt.append(diff_list)

    os.system("rm MtOptimal/*")
    os.system("mkdir -p MtOptimal")
    print "\nWeights:\t", w_list, '\n'
    optimalWeights = {}
    for i in range(0, len(diff_opt)):
        print optModes[i]
        print "Differences:\t", diff_opt[i], "- Optimal: w =", w_list[
            diff_opt[i].index(min(diff_opt[i]))]
        optimalWeights[optModes[i]] = w_list[diff_opt[i].index(min(
            diff_opt[i]))]
        command = "cp *" + str(w_list[diff_opt[i].index(min(
            diff_opt[i]))]) + "*" + optModes[i].replace("Opt",
                                                        "") + ".eps MtOptimal"
        os.system(command)
    print optimalWeights
    writeWeightsToFile("OptimalWeights.py", optimalWeights)
    writeNormalizationToFile("QCDPlusEWKFakeTauNormalizationFactors.py",
                             normalization_var_qg, labels)
Exemplo n.º 12
0
def main():

    style = tdrstyle.TDRStyle()

    # Set ROOT batch mode boolean
    ROOT.gROOT.SetBatch(parseOpts.batchMode)
    ROOT.gErrorIgnoreLevel = 3000

    # Get all datasets from the mcrab dir
    datasetsMgr = GetDatasetsFromDir(parseOpts.mcrab, kwargs.get("analysis"))
    # datasetsMgr  = dataset.getDatasetsFromMulticrabDirs([parseOpts.mcrab], analysisName=kwargs.get("analysis"), includeOnlyTasks="ChargedHiggs_HplusTB_HplusToTB_M_200")
    # datasetsMgr  = dataset.getDatasetsFromMulticrabDirs([parseOpts.mcrab], analysisName=kwargs.get("analysis"), excludeTasks="M_200")

    # Determine Integrated Luminosity
    intLumi = GetLumi(datasetsMgr)

    # Update to PU
    datasetsMgr.updateNAllEventsToPUWeighted()

    # Remove datasets
    datasetsMgr.remove(kwargs.get("rmDataset"))
    # datasetsMgr.remove(filter(lambda name: not "QCD" in name, datasetsMgr.getAllDatasetNames()))
    # datasetsMgr.remove(filter(lambda name: "QCD" in name in name, datasetsMgr.getAllDatasetNames()))

    # Set custom XSections
    # datasetsMgr.getDataset("TT_ext3").setCrossSection(831.76)

    # Default merging & ordering: "Data", "QCD", "SingleTop", "Diboson"
    plots.mergeRenameReorderForDataMC(
        datasetsMgr
    )  #WARNING: Merged MC histograms must be normalized to something!

    # Remove datasets (for merged names)
    datasetsMgr.remove(kwargs.get("rmDataset"))
    # datasetsMgr.remove(filter(lambda name: not "QCD" in name, datasetsMgr.getAllDatasetNames()))
    # datasetsMgr.remove(filter(lambda name: "QCD" in name in name, datasetsMgr.getAllDatasetNames()))

    # For-loop: All Histogram names
    for hName in hNames:

        savePath, saveName = GetSavePathAndName(hName, **kwargs)

        # Get efficiency histos
        refEff, otherEff = GetCutEfficiencyTGraphs(datasetsMgr, hName, "kFCP",
                                                   **kwargs)
        # refEff, otherEff = GetCutEfficiencyTGraphs(datasetsMgr, hName, "kFFC", **kwargs)
        # refEff, otherEff = GetCutEfficiencyTGraphs(datasetsMgr, hName, "kBJeffrey", **kwargs)
        # refEff, otherEff = GetCutEfficiencyTGraphs(datasetsMgr, hName, "kFNormal", **kwargs)

        # Plot the efficiencies
        p = plots.ComparisonManyPlot(refEff, otherEff)

        # Create a frame
        opts = {"ymin": 0.0, "ymaxfactor": 1.2}  #"ymax": 5e-1}
        ratioOpts = {"ymin": 0.0, "ymaxfactor": 1.2}
        p.createFrame(saveName,
                      createRatio=kwargs.get("createRatio"),
                      opts=opts,
                      opts2=ratioOpts)

        # Customise Legend
        moveLegend = {"dx": -0.1, "dy": +0.0, "dh": -0.1}
        p.setLegend(
            histograms.moveLegend(histograms.createLegend(), **moveLegend))
        #p.removeLegend()

        # Customise frame
        # p.setEnergy("13")
        if kwargs.get("createRatio"):
            p.getFrame2().GetYaxis().SetTitle("Ratio")
            p.getFrame2().GetYaxis().SetTitleOffset(1.6)

        # SetLog
        SetLogAndGrid(p, **kwargs)

        # Add cut line/box
        _kwargs = {"lessThan": kwargs.get("cutLessThan")}
        p.addCutBoxAndLine(cutValue=kwargs.get("cutValue"),
                           fillColor=kwargs.get("cutFillColour"),
                           box=kwargs.get("cutBox"),
                           line=kwargs.get("cutLine"),
                           **_kwargs)

        # Move the refDataset to first in the draw order (back)
        histoNames = [h.getName() for h in p.histoMgr.getHistos()]
        p.histoMgr.reorder(
            filter(
                lambda n: plots._legendLabels[kwargs.get("refDataset")] not in
                n, histoNames))

        #  Draw plots
        p.draw()

        # Customise text
        histograms.addStandardTexts(lumi=intLumi)
        # histograms.addText(0.4, 0.9, "Alexandros Attikis", 17)
        # histograms.addText(0.4, 0.11, "Runs " + datasetsMgr.loadRunRange(), 17)

        # Save canvas under custom dir
        SaveAs(p, savePath, saveName, kwargs.get("saveFormats"))

    return
Exemplo n.º 13
0
def PlotHistograms(datasetsMgr_Mcrab644_TopPtRew,
                   datasetsMgr_Mcrab644_noTopPtRew,
                   datasetsMgr_Mcrab905_TopPtRew,
                   datasetsMgr_Mcrab905_noTopPtRew):

    # Definitions
    kwargs = {}
    histos = []

    # For-loop: All histos in list
    dName = "TT"

    # Multicrab *644, top pt reweighting:
    p0 = plots.MCPlot(datasetsMgr_Mcrab644_TopPtRew,
                      "Analysis_/TopQuarkPt",
                      normalizeToLumi=opts.intLumi,
                      saveFormats=[],
                      **kwargs)
    h_Mcrab644_TopPtRew = p0.histoMgr.getHisto(dName).getRootHisto().Clone(
        "Mcrab644-ptRew")
    histos.append(h_Mcrab644_TopPtRew)

    # Multicrab *644, no top pt reweighting:
    p1 = plots.MCPlot(datasetsMgr_Mcrab644_noTopPtRew,
                      "Analysis_/TopQuarkPt",
                      normalizeToLumi=opts.intLumi,
                      saveFormats=[],
                      **kwargs)
    h_Mcrab644_noTopPtRew = p1.histoMgr.getHisto(dName).getRootHisto().Clone(
        "Mcrab644-noptRew")
    histos.append(h_Mcrab644_noTopPtRew)

    # Multicrab *905, top pt reweighting:
    p2 = plots.MCPlot(datasetsMgr_Mcrab905_TopPtRew,
                      "Analysis_/TopQuarkPt",
                      normalizeToLumi=opts.intLumi,
                      saveFormats=[],
                      **kwargs)
    h_Mcrab905_TopPtRew = p2.histoMgr.getHisto(dName).getRootHisto().Clone(
        "Mcrab905-ptRew")
    histos.append(h_Mcrab905_TopPtRew)

    # Multicrab *905, no top pt reweighting:
    p3 = plots.MCPlot(datasetsMgr_Mcrab905_noTopPtRew,
                      "Analysis_/TopQuarkPt",
                      normalizeToLumi=opts.intLumi,
                      saveFormats=[],
                      **kwargs)
    h_Mcrab905_noTopPtRew = p3.histoMgr.getHisto(dName).getRootHisto().Clone(
        "Mcrab905-noptRew")
    histos.append(h_Mcrab905_noTopPtRew)

    # Normalise histos
    for h in histos:
        if opts.normaliseToOne:
            h = h.Scale(1 / h.Integral())

    # Make comparison plot
    if opts.eight:
        # p = plots.ComparisonManyPlot(h_Mcrab905_TopPtRew, [ h_Mcrab905_noTopPtRew], saveFormats=[])
        p = plots.ComparisonManyPlot(h_Mcrab905_noTopPtRew,
                                     [h_Mcrab905_TopPtRew],
                                     saveFormats=[])
    else:
        # p = plots.ComparisonManyPlot(h_Mcrab644_TopPtRew, [h_Mcrab644_noTopPtRew], saveFormats=[])
        # p = plots.ComparisonManyPlot(h_Mcrab644_noTopPtRew, [h_Mcrab644_TopPtRew], saveFormats=[])
        p = plots.ComparisonManyPlot(
            h_Mcrab644_noTopPtRew, [h_Mcrab644_TopPtRew, h_Mcrab905_TopPtRew],
            saveFormats=[])
    p.setLuminosity(opts.intLumi)

    # Overwite signal style?
    if opts.eight:
        p.histoMgr.forHisto("Mcrab905-ptRew", styles.getABCDStyle("VR"))
        p.histoMgr.forHisto("Mcrab905-noptRew", styles.getFakeBLineStyle())
    else:
        p.histoMgr.forHisto("Mcrab644-ptRew", styles.getABCDStyle("VR"))
        p.histoMgr.forHisto("Mcrab644-noptRew", styles.getFakeBLineStyle())

    # Set draw style
    if opts.eight:
        p.histoMgr.setHistoDrawStyle("Mcrab905-noptRew", "HIST")
        p.histoMgr.setHistoDrawStyle("Mcrab905-ptRew", "AP")
    else:
        p.histoMgr.setHistoDrawStyle("Mcrab644-noptRew", "HIST")
        p.histoMgr.setHistoDrawStyle("Mcrab644-ptRew", "AP")

    # Set legend style
    if opts.eight:
        p.histoMgr.setHistoLegendStyle("Mcrab905-noptRew", "L")  #"F"
        p.histoMgr.setHistoLegendStyle("Mcrab905-ptRew", "LP")
    else:
        p.histoMgr.setHistoLegendStyle("Mcrab644-noptRew", "L")  #"F"
        p.histoMgr.setHistoLegendStyle("Mcrab644-ptRew", "LP")

    if opts.eight:
        p.histoMgr.setHistoLegendLabelMany({
            "Mcrab905-ptRew": "p_{T} reweight (8 TeV)",
            "Mcrab905-noptRew": "Default",
        })
    else:
        p.histoMgr.setHistoLegendLabelMany({
            "Mcrab644-ptRew": "p_{T} reweight (13 TeV)",
            "Mcrab644-noptRew": "Default",
        })

    # Draw customised plot
    if opts.eight:
        saveName = "TopQuarkPt_PtReweight_8TeV"
    else:
        saveName = "TopQuarkPt_PtReweight_13TeV"
    _units = "GeV/c"
    _leg = {"x1": 0.55, "y1": 0.75, "x2": 0.85, "y2": 0.87}

    if opts.normaliseToOne:
        yLabel = "Arbitrary Units / %0.0f " + _units
    else:
        yLabel = "Events / %0.0f " + _units

    plots.drawPlot(
        p,
        saveName,
        xlabel="generated top p_{T} (%s)" % (_units),
        ylabel=yLabel,
        log=False,
        rebinX=2,
        cmsExtraText="Simulation",  #"Preliminary",
        ratio=True,
        ratioType="errorPropagation",  #"errorScale", "binomial"
        divideByBinWidth=False,
        ratioErrorOptions={
            "numeratorStatSyst": False,
            "denominatorStatSyst": True
        },
        ratioMoveLegend={
            "dx": +0.21,
            "dy": 0.03,
            "dh": -0.08
        },
        ratioYlabel="Ratio ",
        createLegend=_leg,
        opts={
            "xmin": 0.0,
            "xmax": 800.0,
            "ymin": 0.0,
            "ymaxfactor": 1.2
        },
        opts2={
            "ymin": 0.6,
            "ymax": 1.4
        },
        cutBox={
            "cutValue": 0.0,
            "fillColor": 16,
            "box": False,
            "line": False,
            "greaterThan": True
        })

    # Save plot in all formats
    savePath = os.path.join(opts.saveDir, opts.optMode)
    SavePlot(p, saveName, savePath)
    return
Exemplo n.º 14
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.º 15
0
def BaselineVsInvertedPlots(datasetsMgr, histoName, analysisType="Inverted"):
    
    # Sanity check
    IsBaselineOrInverted(analysisType)
    
    # Get the Data (Inclusive)
    p1 = plots.ComparisonPlot(*getHistos(datasetsMgr, "Data",  histoName, analysisType))
    p1.histoMgr.normalizeMCToLuminosity(datasetsMgr.getDataset("Data").getLuminosity())

    # Get the EWK (GenuineB)                              
    defaultFolder  = "ForFakeBMeasurement"
    genuineBFolder = defaultFolder + "EWKGenuineB"
    fakeBFolder    = defaultFolder + "EWKFakeB"
    histoNameNew   = histoName.replace( defaultFolder, genuineBFolder)
    p2 = plots.ComparisonPlot(*getHistos(datasetsMgr, "EWK", histoNameNew, analysisType) )
    p2.histoMgr.normalizeMCToLuminosity(datasetsMgr.getDataset("Data").getLuminosity())

    # Get Data histos    
    baseline_Data = p1.histoMgr.getHisto("Baseline-Data").getRootHisto().Clone("Baseline-Data")
    inverted_Data = p1.histoMgr.getHisto("Inverted-Data").getRootHisto().Clone("Inverted-Data")

    # Get EWK histos
    baseline_EWKGenuineB = p2.histoMgr.getHisto("Baseline-EWK").getRootHisto().Clone("Baseline-EWKGenuineB")
    inverted_EWKGenuineB = p2.histoMgr.getHisto("Inverted-EWK").getRootHisto().Clone("Inverted-EWKGenuineB")

    # Get FakeB (Baseline): FakeB = Data-EWKGenuineB
    baseline_FakeB = p1.histoMgr.getHisto("Baseline-Data").getRootHisto().Clone("Baseline-FakeB")
    baseline_FakeB.Add(baseline_EWKGenuineB, -1)

    # Get FakeB (Inverted): FakeB = Data-EWKGenuineB
    inverted_FakeB = p1.histoMgr.getHisto("Inverted-Data").getRootHisto().Clone("Inverted-FakeB")
    inverted_FakeB.Add(inverted_EWKGenuineB, -1)

    # Normalize histograms to unit area
    baseline_FakeB.Scale(1.0/baseline_FakeB.Integral())
    inverted_FakeB.Scale(1.0/inverted_FakeB.Integral())

    # Create the final plot object
    p = plots.ComparisonManyPlot(baseline_FakeB, [inverted_FakeB], saveFormats=[]) #[".C", ".png", ".pdf"])
    p.setLuminosity(GetLumi(datasetsMgr))
        
    # Apply styles
    p.histoMgr.forHisto("Baseline-FakeB" , styles.getBaselineStyle() )
    p.histoMgr.forHisto("Inverted-FakeB" , styles.getInvertedStyle() )

    # Set draw style
    p.histoMgr.setHistoDrawStyle("Baseline-FakeB", "AP")
    p.histoMgr.setHistoDrawStyle("Inverted-FakeB", "HIST")

    # Set legend style
    p.histoMgr.setHistoLegendStyle("Baseline-FakeB", "LP")
    p.histoMgr.setHistoLegendStyle("Inverted-FakeB", "F")
    # p.histoMgr.setHistoLegendStyleAll("LP")

    # Set legend labels
    p.histoMgr.setHistoLegendLabelMany({
            "Baseline-FakeB" : "FakeB (Baseline)",
            "Inverted-FakeB" : "FakeB (Inverted)",
            })

    # Draw the histograms
    _cutBox = None
    _rebinX = 1
    _opts   = {"ymin": 1e-4, "ymaxfactor": 2.0}
    _format = "%0.0f"
    _xlabel = None

    if "dijetm" in histoName.lower():
        _rebinX = 2
        _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}
        _opts["xmax"] = 400.0
    if "trijetm" in histoName.lower():
        _rebinX = 5
        _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"] = 1500.0
    if "pt" in histoName.lower():
        _rebinX = 2
        _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():
        _rebinX = 10
        _units  = "GeV/c^{2}"
        _format = "%0.0f " + _units
        _xlabel = "m_{jjjb} (%s)" % (_units)
        _opts["xmax"] = 3500.0

    plots.drawPlot(p, histoName,  
                   xlabel       = _xlabel,
                   ylabel       = "Arbitrary Units / %s" % (_format),
                   log          = True, 
                   rebinX       = _rebinX, cmsExtraText = "Preliminary", 
                   createLegend = {"x1": 0.62, "y1": 0.78, "x2": 0.92, "y2": 0.92},
                   opts         = _opts,
                   opts2        = {"ymin": 0.6, "ymax": 1.4},
                   ratio        = True,
                   ratioInvert  = False, 
                   ratioYlabel  = "Ratio",
                   cutBox       = _cutBox,
                   )
    # Save plot in all formats
    SavePlot(p, histoName, os.path.join(opts.saveDir, "BaselineVsInverted", opts.optMode) ) 
    return
Exemplo n.º 16
0
def PlotHistos(d_noSF, d_withSF, num_histoList, den_histoList, opts):

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

    # Get the root histos for all datasets and Control Regions (CRs)
    regions = ["SR", "VR", "CR1", "CR2"]
    labels = ["Genuine", "Fake", "Inclusive"]

    #==========================================================================================
    # Get Dictionaries
    #==========================================================================================
    rhDict_den_noSF = GetRootHistos(d_noSF, den_histoList, regions)
    rhDict_num_noSF = GetRootHistos(d_noSF, num_histoList, regions)
    rhDict_num_withSF = GetRootHistos(d_withSF, num_histoList, regions)

    # =========================================================================================
    # Normalization Factors (see: getNormalization.py)
    # =========================================================================================
    #f1=0.626877; f2=0.880767;
    f1 = 0.625454
    f2 = 0.836566
    #noTopPtRew
    #f1 = 0.626893; f2 = 0.880846; #noDeltaRqq
    #f1 = 0.625454; f2 = 0.836566; #noDeltaRqq, noTopPtRew

    # =========================================================================================
    # (A) Apply Normalization Factors (see: getNormalizations.py)
    # =========================================================================================

    # Normalize all histograms (QCD and TT) to normalization factors
    for re in regions:

        rhDict_den_noSF["NormQCD-" + re + "-Inclusive"] = rhDict_den_noSF[
            "QCD-" + re + "-Inclusive"].Clone("NormQCD-" + re + "-Inclusive")
        rhDict_den_noSF["NormQCD-" + re + "-Inclusive"].Scale(f1)

        rhDict_num_noSF["NormQCD-" + re + "-Inclusive"] = rhDict_num_noSF[
            "QCD-" + re + "-Inclusive"].Clone("NormQCD-" + re + "-Inclusive")
        rhDict_num_noSF["NormQCD-" + re + "-Inclusive"].Scale(f1)

        rhDict_num_withSF["NormQCD-" + re + "-Inclusive"] = rhDict_num_withSF[
            "QCD-" + re + "-Inclusive"].Clone("NormQCD-" + re + "-Inclusive")
        rhDict_num_withSF["NormQCD-" + re + "-Inclusive"].Scale(f1)

        for la in labels:

            rhDict_den_noSF["NormTT-" + re + "-" +
                            la] = rhDict_den_noSF["TT-" + re + "-" +
                                                  la].Clone("NormTT-" + re +
                                                            "-" + la)
            rhDict_den_noSF["NormTT-" + re + "-" + la].Scale(f2)

            rhDict_num_noSF["NormTT-" + re + "-" +
                            la] = rhDict_num_noSF["TT-" + re + "-" +
                                                  la].Clone("NormTT-" + re +
                                                            "-" + la)
            rhDict_num_noSF["NormTT-" + re + "-" + la].Scale(f2)

            rhDict_num_withSF["NormTT-" + re + "-" +
                              la] = rhDict_num_withSF["TT-" + re + "-" +
                                                      la].Clone("NormTT-" +
                                                                re + "-" + la)
            rhDict_num_withSF["NormTT-" + re + "-" + la].Scale(f2)

    # =========================================================================================
    # (B) Estimate Inclusive TT in SR
    # =========================================================================================

    # (B1) Inclusive TT in Data (Denominator)  =  Data - F1*QCD - EWK - ST
    rhDict_den_noSF["TTinData-SR-Inclusive"] = rhDict_den_noSF[
        "Data-SR-Inclusive"].Clone("Inclusive t#bar{t} (Data)")
    rhDict_den_noSF["TTinData-SR-Inclusive"].Add(
        rhDict_den_noSF["NormQCD-SR-Inclusive"], -1)
    rhDict_den_noSF["TTinData-SR-Inclusive"].Add(
        rhDict_den_noSF["EWK-SR-Inclusive"], -1)
    rhDict_den_noSF["TTinData-SR-Inclusive"].Add(
        rhDict_den_noSF["SingleTop-SR-Inclusive"], -1)

    # (B2) Inclusive TT in Data (Numerator)    =  Data - (F1*QCD -EWK - ST)*SF
    rhDict_num_noSF["TTinData-SR-Inclusive"] = rhDict_num_noSF[
        "Data-SR-Inclusive"].Clone("Inclusive t#bar{t} (Data)")
    rhDict_num_noSF["TTinData-SR-Inclusive"].Add(
        rhDict_num_withSF["NormQCD-SR-Inclusive"], -1)
    rhDict_num_noSF["TTinData-SR-Inclusive"].Add(
        rhDict_num_withSF["EWK-SR-Inclusive"], -1)
    rhDict_num_noSF["TTinData-SR-Inclusive"].Add(
        rhDict_num_withSF["SingleTop-SR-Inclusive"], -1)

    # ==========================================================================================
    # (C) Plot Inclusive Efficiency (Data Vs MC)
    # ==========================================================================================
    _kwargs["opts"] = {"xmax": 800, "ymaxfactor": 2.0}
    _kwargs["ratioYlabel"] = "Data/MC"
    _kwargs["ratio"] = True
    _kwargs["stackMCHistograms"] = False
    _kwargs["createLegend"] = {"x1": 0.60, "y1": 0.75, "x2": 0.95, "y2": 0.92}
    _kwargs["ratioInvert"] = True

    num_data = rhDict_num_noSF["TTinData-SR-Inclusive"].Clone(
        "t#bar{t}_{SR} (Data)")
    den_data = rhDict_den_noSF["TTinData-SR-Inclusive"].Clone(
        "t#bar{t}_{SR} (Data)")

    num_mc = rhDict_num_noSF["TT-SR-Inclusive"].Clone("t#bar{t}_{SR} (MC)")
    den_mc = rhDict_den_noSF["TT-SR-Inclusive"].Clone("t#bar{t}_{SR} (MC)")

    num_data.Rebin(2)
    num_mc.Rebin(2)
    den_data.Rebin(2)
    den_mc.Rebin(2)

    styles.getABCDStyle("CR4").apply(num_mc)
    styles.getABCDStyle("CR4").apply(den_mc)

    # Denominator
    hName = "InclusiveTT_SR_Denominator"
    hData_Den = histograms.Histo(den_data,
                                 "t#bar{t}_{SR} (Data)",
                                 "Data",
                                 drawStyle="AP")
    hData_Den.setIsDataMC(isData=True, isMC=False)
    hMC_Den = histograms.Histo(den_mc,
                               "t#bar{t}_{SR} (MC)",
                               "MC",
                               drawStyle="HIST")
    hMC_Den.setIsDataMC(isData=False, isMC=True)

    pDen = plots.ComparisonManyPlot(hData_Den, [hMC_Den], saveFormats=[])
    pDen.setLuminosity(opts.intLumi)
    pDen.setDefaultStyles()
    plots.drawPlot(pDen, hName, **_kwargs)
    SavePlot(pDen,
             hName,
             os.path.join(opts.saveDir, opts.optMode),
             saveFormats=[".png"])

    # Numerator
    hName = "InclusiveTT_SR_Numerator"
    hData_Num = histograms.Histo(num_data,
                                 "t#bar{t}_{SR} (Data)",
                                 "Data",
                                 drawStyle="AP")
    hData_Num.setIsDataMC(isData=True, isMC=False)
    hMC_Num = histograms.Histo(num_mc,
                               "t#bar{t}_{SR} (MC)",
                               "MC",
                               drawStyle="HIST")
    hMC_Num.setIsDataMC(isData=False, isMC=True)

    pNum = plots.ComparisonManyPlot(hData_Num, [hMC_Num], saveFormats=[])
    pNum.setLuminosity(opts.intLumi)
    pNum.setDefaultStyles()
    plots.drawPlot(pNum, hName, **_kwargs)
    SavePlot(pNum,
             hName,
             os.path.join(opts.saveDir, opts.optMode),
             saveFormats=[".png"])

    # =========================
    # Inclusive Efficiency
    # =========================
    _kwargs = {
        "xlabel": "p_{T} (GeV/c)",
        "ylabel": "Efficiency",
        "ratioYlabel": "Data/MC",
        "ratio": True,
        "ratioInvert": False,
        "ratioType": None,
        "ratioCreateLegend": True,
        "ratioMoveLegend": {
            "dx": -0.51,
            "dy": 0.03,
            "dh": -0.08
        },
        "ratioErrorOptions": {
            "numeratorStatSyst": False,
            "denominatorStatSyst": False
        },
        "errorBarsX": True,
        "stackMCHistograms": False,
        "addMCUncertainty": False,
        "addLuminosityText": False,
        "addCmsText": True,
        "cmsExtraText": "Preliminary",
        "opts": {
            "ymin": 0.0,
            "ymaxfactor": 1.5,
            "xmax": 600
        },
        "opts2": {
            "ymin": 0.6,
            "ymax": 1.5
        },
        "log": False,
        "createLegend": {
            "x1": 0.64,
            "y1": 0.80,
            "x2": 0.95,
            "y2": 0.92
        },
    }
    _kwargs["cutBoxY"] = {
        "cutValue": 1.10,
        "fillColor": ROOT.kGray + 1,
        "fillStyle": 3001,
        "box": False,
        "line": True,
        "greaterThan": True,
        "mainCanvas": False,
        "ratioCanvas": True,
        "mirror": True
    }

    bins = [0, 100, 200, 300, 400, 500, 600]
    xBins = array.array('d', bins)
    nx = len(xBins) - 1

    # Data
    h0_data_den = rhDict_den_noSF["TTinData-SR-Inclusive"].Clone(
        "t#bar{t}_{SR} (Data)")
    h0_data_num = rhDict_num_noSF["TTinData-SR-Inclusive"].Clone(
        "t#bar{t}_{SR} (Data)")

    # MC
    h0_mc_den = rhDict_den_noSF["TT-SR-Inclusive"].Clone("t#bar{t}_{SR} (MC)")
    h0_mc_num = rhDict_num_noSF["TT-SR-Inclusive"].Clone("t#bar{t}_{SR} (MC)")

    h0_data_den = h0_data_den.Rebin(nx, "", xBins)
    h0_data_num = h0_data_num.Rebin(nx, "", xBins)

    h0_mc_den = h0_mc_den.Rebin(nx, "", xBins)
    h0_mc_num = h0_mc_num.Rebin(nx, "", xBins)

    hNumerator_Data, hDenominator_Data = GetHistosForEfficiency(
        h0_data_num, h0_data_den)
    hNumerator_MC, hDenominator_MC = GetHistosForEfficiency(
        h0_mc_num, h0_mc_den)

    eff_data = ROOT.TEfficiency(hNumerator_Data, hDenominator_Data)
    eff_mc = ROOT.TEfficiency(hNumerator_MC, hDenominator_MC)

    eff_data.SetStatisticOption(ROOT.TEfficiency.kFCP)
    eff_mc.SetStatisticOption(ROOT.TEfficiency.kFCP)

    geff_data = convert2TGraph(eff_data)
    geff_mc = convert2TGraph(eff_mc)

    styles.dataStyle.apply(geff_data)
    styles.ttStyle.apply(geff_mc)

    Graph_Data = histograms.HistoGraph(geff_data, "t#bar{t}_{SR} (Data) ", "p",
                                       "P")
    Graph_MC = histograms.HistoGraph(geff_mc, "t#bar{t}_{SR} (MC)", "p", "P")

    p = plots.ComparisonManyPlot(Graph_MC, [Graph_Data], saveFormats=[])
    saveName = "Efficiency_InclusiveTT_SR"
    savePath = os.path.join(opts.saveDir, opts.optMode)
    plots.drawPlot(p, savePath, **_kwargs)
    SavePlot(p,
             saveName,
             os.path.join(opts.saveDir, opts.optMode),
             saveFormats=[".png", ".pdf", ".C"])

    # Save results in JSON
    if (opts.inclusiveEff):
        name = opts.noSFcrab.split("_")[-4]
        name = name.replace(opts.analysisName, "")
        print "name = ", name
        jsonName = "toptagEff_BDT" + name + "_InclusiveTT_TopMassCut400.json"
        runRange = "273150-284044"
        analysis = opts.analysisName
        label = "2016"
        plotDir = os.path.join(opts.folder, jsonName)
        pythonWriter.addParameters(plotDir, label, runRange, opts.intLumi,
                                   geff_data)
        pythonWriter.addMCParameters(label, geff_mc)
        fileName_json = jsonName
        pythonWriter.writeJSON(fileName_json)

    # ==========================================================================================
    # (D) Estimate Genuine TT in SR
    # ==========================================================================================

    # (D1) Genuine TT in Data - Denominator = Data - F1*QCD - F2*FakeTT - EWK - ST
    rhDict_den_noSF["TTinData-SR-Genuine"] = rhDict_den_noSF[
        "Data-SR-Inclusive"].Clone("genuine t#bar{t} (Data)")
    rhDict_den_noSF["TTinData-SR-Genuine"].Add(
        rhDict_den_noSF["NormQCD-SR-Inclusive"], -1)
    rhDict_den_noSF["TTinData-SR-Genuine"].Add(
        rhDict_den_noSF["NormTT-SR-Fake"], -1)
    rhDict_den_noSF["TTinData-SR-Genuine"].Add(
        rhDict_den_noSF["EWK-SR-Inclusive"], -1)
    rhDict_den_noSF["TTinData-SR-Genuine"].Add(
        rhDict_den_noSF["SingleTop-SR-Inclusive"], -1)

    # (D2) Genuine TT in Data - Numerator = Data - [F1*QCD - F2*FakeTT - ST - EWK] * SF
    rhDict_num_noSF["TTinData-SR-Genuine"] = rhDict_num_noSF[
        "Data-SR-Inclusive"].Clone("genuine t#bar{t} (Data)")
    rhDict_num_noSF["TTinData-SR-Genuine"].Add(
        rhDict_num_withSF["NormQCD-SR-Inclusive"], -1)
    rhDict_num_noSF["TTinData-SR-Genuine"].Add(
        rhDict_num_withSF["NormTT-SR-Fake"], -1)
    rhDict_num_noSF["TTinData-SR-Genuine"].Add(
        rhDict_num_withSF["SingleTop-SR-Inclusive"], -1)
    rhDict_num_noSF["TTinData-SR-Genuine"].Add(
        rhDict_num_withSF["EWK-SR-Inclusive"], -1)

    # ========================================================================================
    # (E) Plot Numerator and Denominator (Data Vs MC)
    # ========================================================================================
    _kwargs = GetHistoKwargs(num_histoList[0])
    _kwargs["opts"] = {"xmax": 800, "ymaxfactor": 2.0}
    _kwargs["ratioYlabel"] = "Data/MC"
    _kwargs["ratio"] = True
    _kwargs["stackMCHistograms"] = False
    _kwargs["createLegend"] = {"x1": 0.60, "y1": 0.75, "x2": 0.95, "y2": 0.92}
    _kwargs["ratioInvert"] = True

    num_data.Reset()
    den_data.Reset()
    num_mc.Reset()
    den_mc.Reset()

    num_data = rhDict_num_noSF["TTinData-SR-Genuine"].Clone(
        "genuine t#bar{t}_{SR} (Data)")
    den_data = rhDict_den_noSF["TTinData-SR-Genuine"].Clone(
        "genuine t#bar{t}_{SR} (Data)")

    num_mc = rhDict_num_noSF["TT-SR-Genuine"].Clone(
        "genuine t#bar{t}_{SR} (MC)")
    den_mc = rhDict_den_noSF["TT-SR-Genuine"].Clone(
        "genuine t#bar{t}_{SR} (MC)")

    num_data.Rebin(2)
    num_mc.Rebin(2)
    den_data.Rebin(2)
    den_mc.Rebin(2)

    styles.getABCDStyle("CR4").apply(num_mc)
    styles.getABCDStyle("CR4").apply(den_mc)

    # Denominator
    hName = "GenuineTT_SR_Denominator"
    hData_Den = histograms.Histo(den_data,
                                 "genuine t#bar{t}_{SR} (Data)",
                                 "Data",
                                 drawStyle="AP")
    hData_Den.setIsDataMC(isData=True, isMC=False)
    hMC_Den = histograms.Histo(den_mc,
                               "genuine t#bar{t}_{SR} (MC)",
                               "MC",
                               drawStyle="HIST")
    hMC_Den.setIsDataMC(isData=False, isMC=True)

    pDenumerator = plots.ComparisonManyPlot(hData_Den, [hMC_Den],
                                            saveFormats=[])
    pDenumerator.setLuminosity(opts.intLumi)
    pDenumerator.setDefaultStyles()
    plots.drawPlot(pDenumerator, hName, **_kwargs)
    SavePlot(pDenumerator,
             hName,
             os.path.join(opts.saveDir, opts.optMode),
             saveFormats=[".png"])

    # Numerator
    hName = "GenuineTT_SR_Numerator"
    hData_Num = histograms.Histo(num_data,
                                 "genuine t#bar{t}_{SR} (Data)",
                                 "Data",
                                 drawStyle="AP")
    hData_Num.setIsDataMC(isData=True, isMC=False)
    hMC_Num = histograms.Histo(num_mc,
                               "genuine t#bar{t}_{SR} (MC)",
                               "MC",
                               drawStyle="HIST")
    hMC_Num.setIsDataMC(isData=False, isMC=True)

    pNumerator = plots.ComparisonManyPlot(hData_Num, [hMC_Num], saveFormats=[])
    pNumerator.setLuminosity(opts.intLumi)
    pNumerator.setDefaultStyles()
    plots.drawPlot(pNumerator, hName, **_kwargs)
    SavePlot(pNumerator,
             hName,
             os.path.join(opts.saveDir, opts.optMode),
             saveFormats=[".png"])

    # ========================================================================================
    # (F) Plot Genuine TT Efficiency
    # ========================================================================================
    _kwargs = {
        "xlabel": "p_{T} (GeV/c)",
        "ylabel": "Efficiency",
        "ratioYlabel": "Data/MC",
        "ratio": True,
        "ratioInvert": False,
        "ratioType": None,  #"errorScale",
        "ratioCreateLegend": True,
        "ratioMoveLegend": {
            "dx": -0.51,
            "dy": 0.03,
            "dh": -0.08
        },
        "ratioErrorOptions": {
            "numeratorStatSyst": False,
            "denominatorStatSyst": False
        },
        "errorBarsX": True,
        "stackMCHistograms": False,
        "addMCUncertainty": False,
        "addLuminosityText": False,
        "addCmsText": True,
        "cmsExtraText": "Preliminary",
        "opts": {
            "ymin": 0.0,
            "ymaxfactor": 1.25,
            "xmax": 600
        },
        "opts2": {
            "ymin": 0.55,
            "ymax": 1.55
        },
        "log": False,
        "createLegend": {
            "x1": 0.54,
            "y1": 0.80,
            "x2": 0.95,
            "y2": 0.92
        },
    }
    _kwargs["cutBoxY"] = {
        "cutValue": 1.10,
        "fillColor": ROOT.kGray + 1,
        "fillStyle": 3001,
        "box": False,
        "line": True,
        "greaterThan": True,
        "mainCanvas": False,
        "ratioCanvas": True,
        "mirror": True
    }

    bins = [0, 100, 200, 300, 400, 500, 600]
    xBins = array.array('d', bins)
    nx = len(xBins) - 1

    # Data
    h1_data_den = rhDict_den_noSF["TTinData-SR-Genuine"].Clone(
        "genuine t#bar{t}_{SR} (Data)")
    h1_data_num = rhDict_num_noSF["TTinData-SR-Genuine"].Clone(
        "genuine t#bar{t}_{SR} (Data)")

    # MC
    h1_mc_den = rhDict_den_noSF["TT-SR-Genuine"].Clone(
        "genuine t#bar{t}_{SR} (MC)")
    h1_mc_num = rhDict_num_noSF["TT-SR-Genuine"].Clone(
        "genuine t#bar{t}_{SR} (MC)")

    h1_data_den = h1_data_den.Rebin(nx, "", xBins)
    h1_data_num = h1_data_num.Rebin(nx, "", xBins)

    h1_mc_den = h1_mc_den.Rebin(nx, "", xBins)
    h1_mc_num = h1_mc_num.Rebin(nx, "", xBins)

    h_Numerator_Data, h_Denominator_Data = GetHistosForEfficiency(
        h1_data_num, h1_data_den)
    h_Numerator_MC, h_Denominator_MC = GetHistosForEfficiency(
        h1_mc_num, h1_mc_den)

    effi_data = ROOT.TEfficiency(h_Numerator_Data, h_Denominator_Data)
    effi_mc = ROOT.TEfficiency(h_Numerator_MC, h_Denominator_MC)

    effi_data.SetStatisticOption(ROOT.TEfficiency.kFCP)
    effi_mc.SetStatisticOption(ROOT.TEfficiency.kFCP)

    geffi_data = convert2TGraph(effi_data)
    geffi_mc = convert2TGraph(effi_mc)

    styles.dataStyle.apply(geffi_data)
    styles.ttStyle.apply(geffi_mc)

    Graph_Data = histograms.HistoGraph(geffi_data,
                                       "genuine t#bar{t}_{SR} (Data) ", "p",
                                       "P")
    Graph_MC = histograms.HistoGraph(geffi_mc, "genuine t#bar{t}_{SR} (MC)",
                                     "p", "P")

    pp = plots.ComparisonManyPlot(Graph_MC, [Graph_Data], saveFormats=[])
    saveName = "Efficiency_GenuineTT_SR"
    savePath = os.path.join(opts.saveDir, opts.optMode)
    plots.drawPlot(pp, savePath, **_kwargs)
    SavePlot(pp,
             saveName,
             os.path.join(opts.saveDir, opts.optMode),
             saveFormats=[".png", ".pdf", ".C"])

    # Save results in JSON
    if (not opts.inclusiveEff):
        name = opts.noSFcrab.split("_")[-4]
        name = name.replace(opts.analysisName, "")
        jsonName = "toptagEff_BDT" + name + "_GenuineTT_TopMassCut400.json"
        runRange = "273150-284044"
        analysis = opts.analysisName
        label = "2016"
        plotDir = os.path.join(opts.folder, jsonName)
        pythonWriter.addParameters(plotDir, label, runRange, opts.intLumi,
                                   geffi_data)
        pythonWriter.addMCParameters(label, geffi_mc)
        fileName_json = jsonName
        pythonWriter.writeJSON(fileName_json)

    return
Exemplo n.º 17
0
def main(opts):

    style = tdrstyle.TDRStyle()

    # Set ROOT batch mode boolean
    ROOT.gROOT.SetBatch(opts.batchMode)

    # Get all datasets from the mcrab dir
    # def GetDatasetsFromDir(mcrab, opts, **kwargs): #iro
    datasetsMgr = GetDatasetsFromDir(opts.mcrab, opts,
                                     **kwargs)  #kwargs.get("analysis"))
    datasetsMgr_fixed = GetDatasetsFromDir(opts.mcrab, opts, **kwargs_fixed)

    # Determine Integrated Luminosity (If Data datasets present)
    intLumi = GetLumi(datasetsMgr)

    # Update to PU
    datasetsMgr.updateNAllEventsToPUWeighted()

    # Remove datasets
    datasetsMgr.remove(kwargs.get("rmDataset"))
    datasetsMgr_fixed.remove(kwargs_fixed.get("rmDataset"))
    # datasetsMgr.remove(filter(lambda name: not "QCD" in name, datasetsMgr.getAllDatasetNames()))
    # datasetsMgr.remove(filter(lambda name: "QCD" in name in name, datasetsMgr.getAllDatasetNames()))

    # Set custom XSections
    #datasetsMgr.getDataset("QCD_bEnriched_HT1000to1500").setCrossSection(1.0)
    #datasetsMgr.getDataset("QCD_bEnriched_HT1500to2000").setCrossSection(1.0)
    #datasetsMgr.getDataset("QCD_bEnriched_HT2000toInf").setCrossSection(1.0)
    #datasetsMgr.getDataset("QCD_bEnriched_HT300to500").setCrossSection(1.0)
    #datasetsMgr.getDataset("QCD_bEnriched_HT500to700").setCrossSection(1.0)
    #datasetsMgr.getDataset("QCD_bEnriched_HT700to1000").setCrossSection(1.0)

    # Default merging & ordering: "Data", "QCD", "SingleTop", "Diboson"
    plots.mergeRenameReorderForDataMC(datasetsMgr)
    plots.mergeRenameReorderForDataMC(datasetsMgr_fixed)

    # Remove datasets (for merged names)
    datasetsMgr.remove(kwargs.get("rmDataset"))
    datasetsMgr_fixed.remove(kwargs_fixed.get("rmDataset"))

    # Print the cross
    datasetsMgr.PrintCrossSections()

    for h_prefix in ["reco", "gen"]:
        # Get ref histo (?) here and the fixed histos (TT, QCD, QCD-b
        inclusiveHisto, fixedHistos = GetHistosForPlotter(
            datasetsMgr_fixed, h_prefix + "MHT", **kwargs_fixed)
        inclusiveHisto.setName(inclusiveHisto.getName() + " (inclusive)")
        #for hi in fixedHistos:
        #    print(type(hi), hi.getName())
        #return
        # For-loop: All Histogram names
        for counter, hName in enumerate(hNames):

            # Get the save path and name
            savePath, saveName = GetSavePathAndName(h_prefix + hName, **kwargs)

            # Get Histos for Plotter
            refHisto_, otherHistos_ = GetHistosForPlotter(
                datasetsMgr, h_prefix + hName, **kwargs)
            refHisto = refHisto_
            otherHistos = otherHistos_ + fixedHistos
            #for hi in otherHistos:
            #     print hi.getName()
            #return

            # Create a comparison plot
            p = plots.ComparisonManyPlot(refHisto, otherHistos)

            # Remove negative contributions
            #RemoveNegativeBins(datasetsMgr, hName, p)

            # Create a frame
            if kwargs.get("logY") == True:
                opts = {"ymin": 8e-5, "ymax": 2}
                #opts = {"ymin": 1e-3, "ymax": 1}
            else:
                opts = {"ymin": 8.e-5, "ymax": 2}
            ratioOpts = {"ymin": 0.0, "ymax": 2.0}
            p.createFrame(saveName,
                          createRatio=kwargs.get("createRatio"),
                          opts=opts,
                          opts2=ratioOpts)

            # Customise Legend
            moveLegend = {"dx": -0.2, "dy": +0.0, "dh": -0.1}
            p.setLegend(
                histograms.moveLegend(histograms.createLegend(), **moveLegend))
            #p.removeLegend()

            # Customise frame
            p.getFrame().GetYaxis().SetTitle(getTitleY(refHisto, **kwargs))
            #p.setEnergy("13")
            if kwargs.get("createRatio"):
                p.getFrame2().GetYaxis().SetTitle("Ratio")
                p.getFrame2().GetYaxis().SetTitleOffset(1.6)

            # SetLog
            SetLogAndGrid(p, **kwargs)

            # Add cut line/box
            _kwargs = {"lessThan": kwargs.get("cutLessThan")}
            p.addCutBoxAndLine(cutValue=kwargs.get("cutValue"),
                               fillColor=kwargs.get("cutFillColour"),
                               box=kwargs.get("cutBox"),
                               line=kwargs.get("cutLine"),
                               **_kwargs)

            # Move the refDataset to first in the draw order (back)
            histoNames = [h.getName() for h in p.histoMgr.getHistos()]
            p.histoMgr.reorder(
                filter(
                    lambda n: plots._legendLabels[kwargs.get("refDataset")]
                    not in n, histoNames))

            #  Draw plots
            p.draw()

            # Customise text
            histograms.addStandardTexts(lumi=intLumi)
            # histograms.addText(0.4, 0.9, "Alexandros Attikis", 17)
            # histograms.addText(0.4, 0.11, "Runs " + datasetsMgr.loadRunRange(), 17)

            # Save canvas under custom dir
            if not os.path.exists(savePath):
                os.mkdir(savePath)
            SaveAs(p, savePath, saveName, kwargs.get("saveFormats"))

    return
def PlotComparison(datasetsMgr1, datasetsMgr2, datasetsMgr3, hBaseline,
                   hInverted, ext):

    # Create corresponding paths for GenuineB and FakeB histograms (not only Inclusive)
    hBaseline_Inclusive = hBaseline  #no extra string
    hInverted_Inclusive = hInverted  #no extra string
    hBaseline_GenuineB = hBaseline_Inclusive.replace(
        opts.folder, opts.folder + "EWKGenuineB")
    hInverted_GenuineB = hInverted_Inclusive.replace(
        opts.folder, opts.folder + "EWKGenuineB")
    hBaseline_FakeB = hBaseline_Inclusive.replace(opts.folder,
                                                  opts.folder + "EWKFakeB")
    hInverted_FakeB = hInverted_Inclusive.replace(opts.folder,
                                                  opts.folder + "EWKFakeB")

    # Create the histograms in the Baseline (SR) and Inverted (CR) regions
    pBaseline_Inclusive1 = plots.DataMCPlot(datasetsMgr1, hBaseline_Inclusive)
    pBaseline_GenuineB1 = plots.DataMCPlot(datasetsMgr1, hBaseline_GenuineB)
    pBaseline_FakeB1 = plots.DataMCPlot(datasetsMgr1, hBaseline_FakeB)
    pInverted_Inclusive1 = plots.DataMCPlot(datasetsMgr1, hInverted_Inclusive)
    pInverted_GenuineB1 = plots.DataMCPlot(datasetsMgr1, hInverted_GenuineB)
    pInverted_FakeB1 = plots.DataMCPlot(datasetsMgr1, hInverted_FakeB)

    pBaseline_Inclusive2 = plots.DataMCPlot(datasetsMgr2, hBaseline_Inclusive)
    pBaseline_GenuineB2 = plots.DataMCPlot(datasetsMgr2, hBaseline_GenuineB)
    pBaseline_FakeB2 = plots.DataMCPlot(datasetsMgr2, hBaseline_FakeB)
    pInverted_Inclusive2 = plots.DataMCPlot(datasetsMgr2, hInverted_Inclusive)
    pInverted_GenuineB2 = plots.DataMCPlot(datasetsMgr2, hInverted_GenuineB)
    pInverted_FakeB2 = plots.DataMCPlot(datasetsMgr2, hInverted_FakeB)

    pBaseline_Inclusive3 = plots.DataMCPlot(datasetsMgr3, hBaseline_Inclusive)
    pBaseline_GenuineB3 = plots.DataMCPlot(datasetsMgr3, hBaseline_GenuineB)
    pBaseline_FakeB3 = plots.DataMCPlot(datasetsMgr3, hBaseline_FakeB)
    pInverted_Inclusive3 = plots.DataMCPlot(datasetsMgr3, hInverted_Inclusive)
    pInverted_GenuineB3 = plots.DataMCPlot(datasetsMgr3, hInverted_GenuineB)
    pInverted_FakeB3 = plots.DataMCPlot(datasetsMgr3, hInverted_FakeB)

    # Extract the correct SR and CR histograms
    baseline_Data1 = pBaseline_Inclusive1.histoMgr.getHisto(
        "Data").getRootHisto().Clone("Baseline-Data1")
    baseline_EWKGenuineB1 = pBaseline_GenuineB1.histoMgr.getHisto(
        "EWK").getRootHisto().Clone("Baseline-EWKGenuineB1")
    baseline_EWKFakeB1 = pBaseline_FakeB1.histoMgr.getHisto(
        "EWK").getRootHisto().Clone("Baseline-EWKFakeB1")
    inverted_Data1 = pInverted_Inclusive1.histoMgr.getHisto(
        "Data").getRootHisto().Clone("Inverted-Data1")
    inverted_EWKGenuineB1 = pInverted_GenuineB1.histoMgr.getHisto(
        "EWK").getRootHisto().Clone("Inverted-EWKGenuineB1")
    inverted_EWKFakeB1 = pInverted_FakeB1.histoMgr.getHisto(
        "EWK").getRootHisto().Clone("Inverted-EWKFakeB1")

    baseline_Data2 = pBaseline_Inclusive2.histoMgr.getHisto(
        "Data").getRootHisto().Clone("Baseline-Data2")
    baseline_EWKGenuineB2 = pBaseline_GenuineB2.histoMgr.getHisto(
        "EWK").getRootHisto().Clone("Baseline-EWKGenuineB2")
    baseline_EWKFakeB2 = pBaseline_FakeB2.histoMgr.getHisto(
        "EWK").getRootHisto().Clone("Baseline-EWKFakeB2")
    inverted_Data2 = pInverted_Inclusive2.histoMgr.getHisto(
        "Data").getRootHisto().Clone("Inverted-Data2")
    inverted_EWKGenuineB2 = pInverted_GenuineB2.histoMgr.getHisto(
        "EWK").getRootHisto().Clone("Inverted-EWKGenuineB2")
    inverted_EWKFakeB2 = pInverted_FakeB2.histoMgr.getHisto(
        "EWK").getRootHisto().Clone("Inverted-EWKFakeB2")

    baseline_Data3 = pBaseline_Inclusive3.histoMgr.getHisto(
        "Data").getRootHisto().Clone("Baseline-Data3")
    baseline_EWKGenuineB3 = pBaseline_GenuineB3.histoMgr.getHisto(
        "EWK").getRootHisto().Clone("Baseline-EWKGenuineB3")
    baseline_EWKFakeB3 = pBaseline_FakeB3.histoMgr.getHisto(
        "EWK").getRootHisto().Clone("Baseline-EWKFakeB3")
    inverted_Data3 = pInverted_Inclusive3.histoMgr.getHisto(
        "Data").getRootHisto().Clone("Inverted-Data3")
    inverted_EWKGenuineB3 = pInverted_GenuineB3.histoMgr.getHisto(
        "EWK").getRootHisto().Clone("Inverted-EWKGenuineB3")
    inverted_EWKFakeB3 = pInverted_FakeB3.histoMgr.getHisto(
        "EWK").getRootHisto().Clone("Inverted-EWKFakeB3")

    # FakeB = Data -EWKGenuineB
    baseline_FakeB1 = baseline_Data1.Clone("Baseline-FakeB1")
    inverted_FakeB1 = inverted_Data1.Clone("Inverted-FakeB1")

    baseline_FakeB2 = baseline_Data2.Clone("Baseline-FakeB2")
    inverted_FakeB2 = inverted_Data2.Clone("Inverted-FakeB2")

    baseline_FakeB3 = baseline_Data3.Clone("Baseline-FakeB3")
    inverted_FakeB3 = inverted_Data3.Clone("Inverted-FakeB3")

    # Subtract the EWK GenuineB
    baseline_FakeB1.Add(baseline_EWKGenuineB1, -1)
    inverted_FakeB1.Add(inverted_EWKGenuineB1, -1)

    baseline_FakeB2.Add(baseline_EWKGenuineB2, -1)
    inverted_FakeB2.Add(inverted_EWKGenuineB2, -1)

    baseline_FakeB3.Add(baseline_EWKGenuineB3, -1)
    inverted_FakeB3.Add(inverted_EWKGenuineB3, -1)

    # Normalize histograms to unit area
    if opts.normaliseToOne:
        baseline_FakeB1.Scale(1.0 / baseline_FakeB1.Integral())
        inverted_FakeB1.Scale(1.0 / inverted_FakeB1.Integral())

        baseline_FakeB2.Scale(1.0 / baseline_FakeB2.Integral())
        inverted_FakeB2.Scale(1.0 / inverted_FakeB2.Integral())

        baseline_FakeB3.Scale(1.0 / baseline_FakeB3.Integral())
        inverted_FakeB3.Scale(1.0 / inverted_FakeB3.Integral())

    # Create the final plot object
    if ext == "CR1" or ext == "SR":
        p = plots.ComparisonManyPlot(baseline_FakeB1,
                                     [baseline_FakeB2, baseline_FakeB3],
                                     saveFormats=[])
    elif ext == "CR2" or ext == "VR":
        p = plots.ComparisonManyPlot(inverted_FakeB1,
                                     [inverted_FakeB2, inverted_FakeB3],
                                     saveFormats=[])
    else:
        raise Exception("Unexpected extension %s" % (ext))
    p.setLuminosity(opts.intLumi)

    # Get the BDT cut values from the multicrab name
    BDT1 = find_between(opts.mcrab1, "MVAm1p00to", "_").replace("p", ".")
    BDT2 = find_between(opts.mcrab2, "MVAm1p00to", "_").replace("p", ".")
    BDT3 = find_between(opts.mcrab3, "MVAm1p00to", "_").replace("p", ".")

    # Apply histogram styles
    if ext == "CR1" or ext == "SR":
        p.histoMgr.forHisto("Baseline-FakeB1", styles.getFakeBLineStyle()
                            )  # getABCDStyle("SR") , getABCDStyle("VR")
        p.histoMgr.forHisto("Baseline-FakeB2", styles.genuineBAltStyle)
        p.histoMgr.forHisto("Baseline-FakeB3", styles.getABCDStyle("SR"))

        # Set drawing style
        p.histoMgr.setHistoDrawStyle("Baseline-FakeB1", "HIST")
        p.histoMgr.setHistoDrawStyle("Baseline-FakeB2", "AP")
        p.histoMgr.setHistoDrawStyle("Baseline-FakeB3", "AP")

        # Set legend styles
        p.histoMgr.setHistoLegendStyle("Baseline-FakeB1", "L")
        p.histoMgr.setHistoLegendStyle("Baseline-FakeB2", "LP")
        p.histoMgr.setHistoLegendStyle("Baseline-FakeB3", "LP")

        # Set legend labels
        p.histoMgr.setHistoLegendLabelMany({
            "Baseline-FakeB1":
            "%s (BDT < %s)" % (ext, BDT1),
            "Baseline-FakeB2":
            "%s (BDT < %s)" % (ext, BDT2),
            "Baseline-FakeB3":
            "%s (BDT < %s)" % (ext, BDT3),
        })
    else:
        p.histoMgr.forHisto("Inverted-FakeB1", styles.getFakeBLineStyle())
        p.histoMgr.forHisto("Inverted-FakeB2", styles.genuineBAltStyle)
        p.histoMgr.forHisto("Inverted-FakeB3", styles.getABCDStyle("SR"))
        # Set drawing styles
        p.histoMgr.setHistoDrawStyle("Inverted-FakeB1", "HIST")
        p.histoMgr.setHistoDrawStyle("Inverted-FakeB2", "AP")
        p.histoMgr.setHistoDrawStyle("Inverted-FakeB3", "AP")

        # Set legend styles
        p.histoMgr.setHistoLegendStyle("Inverted-FakeB1", "L")
        p.histoMgr.setHistoLegendStyle("Inverted-FakeB2", "LP")
        p.histoMgr.setHistoLegendStyle("Inverted-FakeB3", "LP")
        # Set legend labels
        p.histoMgr.setHistoLegendLabelMany({
            "Inverted-FakeB1":
            "%s (BDT < %s)" % (ext, BDT1),
            "Inverted-FakeB2":
            "%s (BDT < %s)" % (ext, BDT2),
            "Inverted-FakeB3":
            "%s (BDT < %s)" % (ext, BDT3),
        })

    # Get histogram keyword arguments
    kwargs_ = GetHistoKwargs(hBaseline_Inclusive, ext, opts)

    # Draw the histograms
    plots.drawPlot(p, hBaseline_Inclusive, **kwargs_)  #iro

    # Save plot in all formats
    saveName = hBaseline_Inclusive.split("/")[-1]
    saveName = saveName.replace("Baseline_", "")
    saveName = saveName.replace("Inverted_", "")
    saveName = saveName.replace("_AfterAllSelections", "_" + ext)
    saveName = saveName.replace("_AfterCRSelections", "_" + ext)
    savePath = os.path.join(opts.saveDir, opts.optMode)
    SavePlot(p, saveName, savePath, saveFormats=[".png", ".pdf"])
    return
Exemplo n.º 19
0
def PlotAndFitTemplates(datasetsMgr, histoName, addQcdBaseline, opts):
    Verbose("PlotAndFitTemplates()")

    # Create comparison plot
    p1 = plots.ComparisonPlot(
        getHisto(datasetsMgr, "Data", "topSelection_Baseline/%s" % histoName, "Baseline"),
        getHisto(datasetsMgr, "EWK" , "topSelection_Baseline/%s" % histoName, "Baseline")
        )
    p1.histoMgr.normalizeMCToLuminosity(datasetsMgr.getDataset("Data").getLuminosity())

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


    # Get Baseline histos
    Data_baseline = p1.histoMgr.getHisto("Baseline-Data").getRootHisto().Clone("Baseline_Data")
    EWK_baseline  = p1.histoMgr.getHisto("Baseline-EWK").getRootHisto().Clone("Baseline_EWK")
    QCD_baseline  = p1.histoMgr.getHisto("Baseline-Data").getRootHisto().Clone("Baseline_QCD")

    # Get Inverted histos
    Data_inverted = p2.histoMgr.getHisto("Inverted-Data").getRootHisto().Clone("Inverted_Data")
    EWK_inverted  = p2.histoMgr.getHisto("Inverted-EWK").getRootHisto().Clone("Inverted_EWK")
    QCD_inverted  = p2.histoMgr.getHisto("Inverted-Data").getRootHisto().Clone("Inverted_QCD")

    # Create QCD histos: QCD = Data-EWK
    QCD_baseline.Add(EWK_baseline, -1)
    QCD_inverted.Add(EWK_inverted, -1)

    # Normalize histograms to unit area
    if 0:
        EWK_baseline.Scale(1.0/EWK_baseline.Integral())    
        EWK_inverted.Scale(1.0/EWK_inverted.Integral())
        QCD_baseline.Scale(1.0/QCD_baseline.Integral())
        QCD_inverted.Scale(1.0/QCD_inverted.Integral())
        
    # Create the final plot object
    if addQcdBaseline:
        compareHistos = [EWK_baseline, QCD_baseline]
    else:
        compareHistos = [EWK_baseline]
    p = plots.ComparisonManyPlot(QCD_inverted, compareHistos, saveFormats=[])
    p.setLuminosity(GetLumi(datasetsMgr))


    #=========================================================================================
    # Start Fit process
    #=========================================================================================
    binLabels = ["Inclusive"]
    moduleInfoString = opts.optMode
    FITMIN = 0
    FITMAX = 1500

    manager = QCDNormalization.QCDNormalizationManagerDefault(binLabels, opts.mcrab, moduleInfoString)

    #=========================================================================================
    # Create templates (EWK fakes, EWK genuine, QCD; data template is created by manager)
    #=========================================================================================
    template_EWKFakeB_Baseline     = manager.createTemplate("EWKFakeB_Baseline")
    template_EWKFakeB_Inverted     = manager.createTemplate("EWKFakeB_Inverted")
    template_EWKInclusive_Baseline = manager.createTemplate("EWKInclusive_Baseline")
    template_EWKInclusive_Inverted = manager.createTemplate("EWKInclusive_Inverted")
    template_QCD_Baseline          = manager.createTemplate("QCD_Baseline")
    template_QCD_Inverted          = manager.createTemplate("QCD_Inverted")
        
    #=========================================================================================
    # Inclusive EWK
    #=========================================================================================
    # par[0]*ROOT.Math.crystalball_function(x[0], par[1], par[2], par[3], par[4]) +
    # par[5]*ROOT.TMath.BreitWigner(x[0], par[6], par[7]) +
    # (1-par[0]-par[5])*ROOT.TMath.Landau(x[0], par[8], par[9])

    #template_EWKInclusive_Baseline.setFitter(QCDNormalization.FitFunction("EWKFunction", boundary=200, norm=1, rejectPoints=0), FITMIN, FITMAX)
    template_EWKInclusive_Baseline.setFitter(QCDNormalization.FitFunction("EWKFunction", boundary=200, norm=1, rejectPoints=0), FITMIN, 800)
    template_EWKInclusive_Baseline.setDefaultFitParam(defaultInitialValue=None,
                                                      #                   p0   p1     p2     p3     p4   p5    p6    p7      p8      p9
                                                      #defaultLowerLimit=[0.0, 150.0,  0.0, -5.0, 0.0, 0.0, 150.0,   0.0, 100.0,    0.0],
                                                      #defaultUpperLimit=[1.0, 300.0, 50.0,  0.0, 1.0, 1.0, 200.0, 100.0, 200.0,  100.0])
                                                      defaultLowerLimit=[0.0, 160.0,   0.0, -1.0, 0.0],
                                                      defaultUpperLimit=[1.0, 180.0,  60.0,  0.0, 1e6])

    #=========================================================================================
    # Note that the same function is used for QCD only and QCD+EWK fakes
    #=========================================================================================
    #par[0]*ROOT.Math.lognormal_pdf(x[0], par[1], par[2]) + par[3]*ROOT.TMath.Exp(-x[0]*par[4]) + (1-par[0]-par[3])*ROOT.TMath.Gaus(x[0], par[5], part[6])
    
    # 0 =   0.8489  for coeff of lognorm
    # 1 =   1.42978 for logshape
    # 2 = 238.546   for logmean
    #
    # 3 =   0.047   for coeff of exp
    # 4 =  -0.0048  for exp
    #
    # 5 =  45.486   for gaus sigma
    # 6 = 204.6     for gaus mean

    #my lognormal is of the form:
    #lognorm(x,m0,k) = exp{-[log(x/m0)]^2/[2*log(k)]^2}/sqrt[2pi*log(k)*x]
    #and differs than the Math::lognormal_pdf parametrization which takes as arguments lognormal(x,m,s,x0)
    #where m = log(m0) and s=log(k)
    #TMath::LogNormal  return Math::lognormal_pdf
    #my exponential is   exp(x*a)  so the sign comes from the fit
    #and my gaussian is exp[-0.5*mean*mean/(sigma*sigma)]

    # Double_t LogNormal(x, sigma, theta = 0, m = 1)

    #template_QCD_Inverted.setFitter(QCDNormalization.FitFunction("QCDFunction", boundary=350, norm=1), FITMIN, FITMAX)
    template_QCD_Inverted.setFitter(QCDNormalization.FitFunction("QCDFunction", boundary=350, norm=1), 100, 800)
    template_QCD_Inverted.setDefaultFitParam(defaultInitialValue=None,
                                             defaultLowerLimit=[0.0, 0.0, 200.0, 0.0, -0.1, 200.0,  0.0],
                                             defaultUpperLimit=[1.0, 2.0, 400.0, 0.1, +0.0, 300.0, 60.0])


    #=========================================================================================
    # Set histograms to the templates
    #=========================================================================================
    template_EWKFakeB_Baseline.setHistogram(EWK_baseline, "Inclusive")
    template_EWKFakeB_Inverted.setHistogram(EWK_inverted, "Inclusive")
    template_EWKInclusive_Baseline.setHistogram(EWK_baseline, "Inclusive")
    template_EWKInclusive_Inverted.setHistogram(EWK_inverted, "Inclusive")
    template_QCD_Baseline.setHistogram(QCD_baseline, "Inclusive")
    template_QCD_Inverted.setHistogram(QCD_inverted, "Inclusive")

    #=========================================================================================
    # Make plots of templates
    #=========================================================================================
    manager.plotTemplates()
    
    #=========================================================================================
    # Fit individual templates to histogram "data_baseline", with custom fit options
    #=========================================================================================
    fitOptions = "R B L W M Q"
    manager.calculateNormalizationCoefficients(Data_baseline, fitOptions, FITMIN, FITMAX)
            
    # Append analysisType to histogram name
    saveName = histoName

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

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

    # Save plot in all formats
    #SavePlot(p, saveName, os.path.join(opts.saveDir, "Templates") ) 
    return
def PlotHistos(noSF_datasetsMgr, withCR2SF_datasetsMgr, num_histoList, den_histoList,  opts):    
    
    # Get the histogram customisations (keyword arguments)
    _kwargs = GetHistoKwargs(num_histoList[0])
    
    # Get the root histos for all datasets and Control Regions (CRs)
    regions = ["SR", "VR", "CR1", "CR2"]
    labels  = ["Genuine", "Fake", "Inclusive"]
    
    #==========================================================================================
    # Get Dictionaries
    #==========================================================================================
    rhDict_den_noSF      = GetRootHistos(noSF_datasetsMgr,      den_histoList, regions)
    rhDict_num_noSF      = GetRootHistos(noSF_datasetsMgr,      num_histoList, regions)
    rhDict_num_withCR2SF = GetRootHistos(withCR2SF_datasetsMgr, num_histoList, regions) # Scale Factors from CR2 are only applied in the Numerator on EWK+QCD+ST
    
    #=========================================================================================
    # Normalization Factors (see: getNormalization.py)
    #=========================================================================================
    # Marina
    f1=0.619886; f2=0.904877;

    # =========================================================================================
    # (A) Apply Normalization Factors (see: getNormalizations.py)
    # =========================================================================================
    
    # Normalize all histograms (QCD and TT) to normalization factors
    for re in regions:
        
        rhDict_den_noSF["NormQCD-"+re+"-Inclusive"] = rhDict_den_noSF["QCD-"+re+"-Inclusive"].Clone("NormQCD-"+re+"-Inclusive")
        rhDict_den_noSF["NormQCD-"+re+"-Inclusive"].Scale(f1)

        rhDict_num_noSF["NormQCD-"+re+"-Inclusive"] =rhDict_num_noSF["QCD-"+re+"-Inclusive"].Clone("NormQCD-"+re+"-Inclusive")
        rhDict_num_noSF["NormQCD-"+re+"-Inclusive"].Scale(f1)

        rhDict_num_withCR2SF["NormQCD-"+re+"-Inclusive"] = rhDict_num_withCR2SF["QCD-"+re+"-Inclusive"].Clone("NormQCD-"+re+"-Inclusive")
        rhDict_num_withCR2SF["NormQCD-"+re+"-Inclusive"].Scale(f1)
        
        for la in labels:
            
            rhDict_den_noSF["NormTT-"+re+"-"+la] = rhDict_den_noSF["TT-"+re+"-"+la].Clone("NormTT-"+re+"-"+la)
            rhDict_den_noSF["NormTT-"+re+"-"+la].Scale(f2)
            
            rhDict_num_noSF["NormTT-"+re+"-"+la] = rhDict_num_noSF["TT-"+re+"-"+la].Clone("NormTT-"+re+"-"+la)
            rhDict_num_noSF["NormTT-"+re+"-"+la].Scale(f2)
                        
    # ==========================================================================================
    # (B) Estimate Inclusive TT in SR
    # ==========================================================================================
            
    # (B1) Inclusive TT in Data - Denominator = Data - F1*QCD - - EWK - ST
    rhDict_den_noSF["TTinData-SR-Inclusive"] = rhDict_den_noSF["Data-SR-Inclusive"].Clone("t#bar{t} (Data)") 
    rhDict_den_noSF["TTinData-SR-Inclusive"].Add(rhDict_den_noSF["NormQCD-SR-Inclusive"],   -1)
    rhDict_den_noSF["TTinData-SR-Inclusive"].Add(rhDict_den_noSF["EWK-SR-Inclusive"],       -1)
    rhDict_den_noSF["TTinData-SR-Inclusive"].Add(rhDict_den_noSF["SingleTop-SR-Inclusive"], -1)

    # (B2) Inclusive TT in Data - Numerator = Data - F1*QCD*SF_{CR2} - ST*SF_{CR2} - EWK*SF_{CR2}
    rhDict_num_noSF["TTinData-SR-Inclusive"] = rhDict_num_noSF["Data-SR-Inclusive"].Clone("t#bar{t} (Data)")
    rhDict_num_noSF["TTinData-SR-Inclusive"].Add(rhDict_num_withCR2SF["NormQCD-SR-Inclusive"],   -1)
    rhDict_num_noSF["TTinData-SR-Inclusive"].Add(rhDict_num_withCR2SF["SingleTop-SR-Inclusive"], -1)
    rhDict_num_noSF["TTinData-SR-Inclusive"].Add(rhDict_num_withCR2SF["EWK-SR-Inclusive"],       -1)
    
    # (B3) Inclusive TT in MC - Denominator:  rhDict_den_noSF["TT-SR-Inclusive"]
    # (B4) Inclusive TT in MC - Numerator:    rhDict_num_noSF["TT-SR-Inclusive"]
    
    
    # ========================================================================================
    # (C) Plot Numerator and Denominator (Data Vs MC)
    # ========================================================================================
    _kwargs["opts"] = {"xmax" : 800, "ymaxfactor" : 2.0}
    _kwargs["ratioYlabel"]  = "Data/MC"
    _kwargs["ratio"]        = True
    _kwargs["stackMCHistograms"] = False
    _kwargs["createLegend"]      = {"x1": 0.68, "y1": 0.75, "x2": 0.95, "y2": 0.92}
    _kwargs["ratioInvert"]  = True

    num_data = rhDict_num_noSF["TTinData-SR-Inclusive"].Clone("t#bar{t} (Data)")
    den_data = rhDict_den_noSF["TTinData-SR-Inclusive"].Clone("t#bar{t} (Data)")
    
    num_mc = rhDict_num_noSF["TT-SR-Inclusive"].Clone("t#bar{t} (MC)")
    den_mc = rhDict_den_noSF["TT-SR-Inclusive"].Clone("t#bar{t} (MC)")
    
    num_data.Rebin(2)
    num_mc.Rebin(2)
    den_data.Rebin(2)
    den_mc.Rebin(2)               

    styles.getABCDStyle("CR4").apply(num_mc)
    styles.getABCDStyle("CR4").apply(den_mc)
    
    # Denominator
    hName = "InclusiveTT_SR_Denominator_LeadingJet"
    hData_Den = histograms.Histo( den_data, "t#bar{t} (Data)", "Data", drawStyle="AP"); hData_Den.setIsDataMC(isData=True, isMC=False)
    hMC_Den   = histograms.Histo( den_mc,   "t#bar{t} (MC)", "MC", drawStyle="HIST");   hMC_Den.setIsDataMC(isData=False, isMC=True)

    pDen = plots.ComparisonManyPlot(hData_Den, [hMC_Den], saveFormats=[])
    pDen.setLuminosity(opts.intLumi)
    pDen.setDefaultStyles()
    plots.drawPlot(pDen, hName, **_kwargs)
    SavePlot(pDen, hName, os.path.join(opts.saveDir, opts.optMode), saveFormats = [".png", ".C", ".pdf"])
    
    # Numerator
    hName = "InclusiveTT_SR_Numerator_LeadingJet"
    hData_Num = histograms.Histo( num_data, "t#bar{t} (Data)", "Data", drawStyle="AP"); hData_Num.setIsDataMC(isData=True, isMC=False)
    hMC_Num   = histograms.Histo( num_mc,   "t#bar{t} (MC)", "MC", drawStyle="HIST");   hMC_Num.setIsDataMC(isData=False, isMC=True)

    pNum = plots.ComparisonManyPlot(hData_Num, [hMC_Num], saveFormats=[])
    pNum.setLuminosity(opts.intLumi)
    pNum.setDefaultStyles()
    plots.drawPlot(pNum, hName, **_kwargs)
    SavePlot(pNum, hName, os.path.join(opts.saveDir, opts.optMode), saveFormats = [".png", ".C", ".pdf"])
    
    # ========================================================================================
    # (D) Plot Genuine TT Efficiency
    # ========================================================================================
    _kwargs = {
        "xlabel"           : "p_{T} (GeV/c)",
        "ylabel"           : "Efficiency",
        "ratioYlabel"      : "Data/MC",
        "ratio"            : True,
        "ratioInvert"      : False,
        "ratioType"        : "errorScale",
        "ratioCreateLegend": True,
        "ratioMoveLegend"  : {"dx": -0.51, "dy": 0.03, "dh": -0.08},
        "ratioErrorOptions": {"numeratorStatSyst": False, "denominatorStatSyst": False},
        "errorBarsX"       : True,
        "stackMCHistograms": False,
        "addMCUncertainty" : False,
        "addLuminosityText": False,
        "addCmsText"       : True,
        "cmsExtraText"     : "Preliminary",
        "opts"             : {"ymin": 0.0, "ymaxfactor": 1.28, "xmax" : 600},
        "opts2"            : {"ymin": 0.72, "ymax": 1.28},
        "log"              : False,
        "createLegend"     : {"x1": 0.68, "y1": 0.80, "x2": 0.95, "y2": 0.92},
        }

    bins = [0, 100, 200, 300, 400, 500, 600]
    xBins = array.array('d', bins)
    nx    = len(xBins)-1

    # Data 
    h0_data_den = rhDict_den_noSF["TTinData-SR-Inclusive"].Clone("t#bar{t} (Data)")
    h0_data_num = rhDict_num_noSF["TTinData-SR-Inclusive"].Clone("t#bar{t} (Data)")
    
    # MC
    h0_mc_den = rhDict_den_noSF["TT-SR-Inclusive"].Clone("t#bar{t} (MC)")
    h0_mc_num = rhDict_num_noSF["TT-SR-Inclusive"].Clone("t#bar{t} (MC)")
    
    h0_data_den = h0_data_den.Rebin(nx, "", xBins)
    h0_data_num = h0_data_num.Rebin(nx, "", xBins)
    
    h0_mc_den = h0_mc_den.Rebin(nx, "", xBins)
    h0_mc_num = h0_mc_num.Rebin(nx, "", xBins)
    
    hNumerator_Data, hDenominator_Data = GetHistosForEfficiency(h0_data_num, h0_data_den)
    hNumerator_MC,   hDenominator_MC   = GetHistosForEfficiency(h0_mc_num, h0_mc_den)
    
    eff_data = ROOT.TEfficiency(hNumerator_Data, hDenominator_Data)
    eff_mc   = ROOT.TEfficiency(hNumerator_MC,   hDenominator_MC)

    eff_data.SetStatisticOption(ROOT.TEfficiency.kFCP)
    eff_mc.SetStatisticOption(ROOT.TEfficiency.kFCP)

    geff_data = convert2TGraph(eff_data)
    geff_mc   = convert2TGraph(eff_mc)

    styles.dataStyle.apply(geff_data)
    styles.ttStyle.apply(geff_mc)

    Graph_Data = histograms.HistoGraph(geff_data, "t#bar{t} (Data) ", "p", "P")
    Graph_MC   = histograms.HistoGraph(geff_mc, "t#bar{t} (MC)", "p", "P")
    
    p = plots.ComparisonManyPlot(Graph_MC, [Graph_Data], saveFormats=[])
    saveName = "Efficiency_InclusiveTT_SR_LeadingJet"
    savePath = os.path.join(opts.saveDir, opts.optMode)
    plots.drawPlot(p, savePath, **_kwargs)
    SavePlot(p, saveName, os.path.join(opts.saveDir, opts.optMode), saveFormats = [".png", ".pdf", ".C"])

    # =============================
    #    Export JSON file
    # =============================
    #jsonName = "Efficiency_InclusiveTT_SR_MET50_MuIso0p1_InvMET20_InvMuIso0p1.json"
    #runRange = "273150-284044"
    #analysis = opts.analysisName
    #label = "2016"
    #plotDir =  os.path.join(opts.folder, jsonName)
    #pythonWriter.addParameters(plotDir, label, runRange, opts.intLumi, geff_data)
    #pythonWriter.addMCParameters(label, geff_mc)
    #pythonWriter.writeJSON(jsonName)
    
    
    return
def DoPlots(datasetsMgr, histoName, analysisType="Inverted", bType="GenuineB"):

    # Sanity checks
    IsBaselineOrInverted(analysisType)
    IsGenuineOrFake(bType)

    # Definitions
    defaultFolder = ""
    if "FakeBPurity" in histoName:
        defaultFolder = "FakeBPurity"
    elif "ForFakeBMeasurement" + histoName:
        defaultFolder = "ForFakeBMeasurement"
    else:
        raise Exception("This should never happen")

    # Define folders for inclusive/genuine/fakes
    inclusiveFolder = defaultFolder
    genuineFolder = defaultFolder + "EWKGenuineB"
    fakeFolder = defaultFolder + "EWKFakeB"
    inclusiveHisto = histoName.replace(defaultFolder, inclusiveFolder)
    genuineHisto = histoName.replace(defaultFolder, genuineFolder)
    fakeHisto = histoName.replace(defaultFolder, fakeFolder)

    # Get the inclusive histograms
    p0 = plots.DataMCPlot(datasetsMgr, inclusiveHisto)
    Data = p0.histoMgr.getHisto("Data").getRootHisto().Clone("Data")

    # Get the genuine-b histograms
    p1 = plots.DataMCPlot(datasetsMgr, genuineHisto)
    EWKGenuineB = p1.histoMgr.getHisto("EWK").getRootHisto().Clone(
        "EWKGenuineB")
    QCDGenuineB = p1.histoMgr.getHisto("QCD").getRootHisto().Clone(
        "QCDGenuineB")

    # Get the fake-b histograms
    p2 = plots.DataMCPlot(datasetsMgr, fakeHisto)
    EWKFakeB = p2.histoMgr.getHisto("EWK").getRootHisto().Clone("EWKFakeB")
    QCDFakeB = p2.histoMgr.getHisto("QCD").getRootHisto().Clone("QCDFakeB")

    # Normalize histograms to unit area
    if 0:
        Data.Scale(1.0 / Data.Integral())
        EWKGenuineB.Scale(1.0 / EWKGenuineB.Integral())
        EWKFakeB.Scale(1.0 / EWKFakeB.Integral())
        QCDGenuineB.Scale(1.0 / QCDGenuineB.Integral())
        QCDFakeB.Scale(1.0 / EWKFakeB.Integral())

    # Create the final plot object
    comparisonList = [EWKGenuineB, QCDGenuineB, EWKFakeB, QCDFakeB]
    p = plots.ComparisonManyPlot(Data, comparisonList, saveFormats=[])
    p.setLuminosity(GetLumi(datasetsMgr))

    # Apply styles
    p.histoMgr.forHisto("Data", styles.getDataStyle())
    p.histoMgr.forHisto("EWKGenuineB",
                        styles.getAltEWKStyle())  #GenuineBStyle()
    p.histoMgr.forHisto("QCDGenuineB", styles.getQCDStyle())
    p.histoMgr.forHisto("EWKFakeB", styles.getGenuineBStyle())
    p.histoMgr.forHisto("QCDFakeB", styles.getFakeBStyle())

    # Set draw style
    p.histoMgr.setHistoDrawStyle("Data", "AP")
    p.histoMgr.setHistoDrawStyle("EWKGenuineB", "AP")
    p.histoMgr.setHistoDrawStyle("QCDGenuineB", "AP")
    p.histoMgr.setHistoDrawStyle("EWKFakeB", "AP")
    p.histoMgr.setHistoDrawStyle("QCDFakeB", "AP")

    # Set legend style
    p.histoMgr.setHistoLegendStyle("Data", "P")
    p.histoMgr.setHistoLegendStyle("EWKGenuineB", "P")
    p.histoMgr.setHistoLegendStyle("QCDGenuineB", "P")
    p.histoMgr.setHistoLegendStyle("EWKFakeB", "P")
    p.histoMgr.setHistoLegendStyle("QCDFakeB", "P")
    # p.histoMgr.setHistoLegendStyleAll("LP")

    # Set legend labels
    p.histoMgr.setHistoLegendLabelMany({
        "Data": "Data",
        "EWKGenuineB": "EWK-GenuineB",
        "QCDGenuineB": "QCD-GenuineB",
        "EWKFakeB": "EWK-FakeB",
        "QCDFakeB": "QCD-FakeB",
        #"Data"       : "Data (%s)"         % (analysisType),
        #"EWKGenuineB": "EWK-GenuineB (%s)" % (analysisType),
        #"QCDGenuineB": "QCD-GenuineB (%s)" % (analysisType),
        #"EWKFakeB"   : "EWK-FakeB (%s)"    % (analysisType),
        #"QCDFakeB"   : "QCD-FakeB (%s)"    % (analysisType),
    })

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

    if "dijetm" in histoName.lower():
        _rebinX = 2
        _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
        }
        _opts["xmax"] = 400.0
    if "trijetm" in histoName.lower():
        _rebinX = 5
        _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"] = 1500.0
    if "pt" in histoName.lower():
        _rebinX = 2
        _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():
        _rebinX = 10
        _units = "GeV/c^{2}"
        _format = "%0.0f " + _units
        _xlabel = "m_{jjjb} (%s)" % (_units)
        _opts["xmax"] = 3500.0
    if "ancestry" in histoName.lower():
        _rebinX = 1
        _units = ""
        _format = "%0.0f " + _units
        _opts["xmax"] = 32.0
    plots.drawPlot(
        p,
        histoName,
        xlabel=_xlabel,
        ylabel="Arbitrary Units / %s" % (_format),
        log=True,
        rebinX=_rebinX,
        cmsExtraText="Preliminary",
        createLegend={
            "x1": 0.62,
            "y1": 0.72,
            "x2": 0.92,
            "y2": 0.92
        },
        opts=_opts,
        opts2={
            "ymin": 0.0,
            "ymax": 1.5
        },  #{"ymin": 0.6, "ymax": 1.4},
        ratio=True,
        ratioInvert=False,
        ratioYlabel="Ratio",
        cutBox=_cutBox,
    )
    # Save plot in all formats
    SavePlot(p, histoName,
             os.path.join(opts.saveDir, "GenuineVsFake", opts.optMode))
    return
Exemplo n.º 22
0
def PlotHistograms(datasetsMgr, histoName):

    # Get Histogram name and its kwargs
    rootHistos = []
    bdiscList  = ["Loose", "Medium"]
    saveName   = histoName.rsplit("/")[-1]
    kwargs     = GetHistoKwargs(saveName, opts)
    myBdiscs   = []
    histoName  = histoName.replace(opts.folder, "")
    stylesList = [styles.FakeBStyle1, styles.FakeBStyle3, styles.FakeBStyle2, styles.FakeBStyle4] 
    #stylesList = [styles.fakeBLineStyle1, styles.FakeBStyle2, styles.FakeBStyle3, styles.FakeBStyle4] 

    # For-loop: All histograms in all regions
    for bdisc in bdiscList:
        
        hName_ = histoName.replace(opts.refBdisc, bdisc)
        hName = opts.folder + "/" + hName_

        if "Data" in datasetsMgr.getAllDatasetNames():
            p = plots.DataMCPlot(datasetsMgr, hName, saveFormats=[])
        else:
            p = plots.MCPlot(datasetsMgr, hName, normalizeToLumi=opts.intLumi, saveFormats=[])
            
        # Append (non-empty) dataset ROOT histos to a list for plotting
        h = p.histoMgr.getHisto(opts.dataset).getRootHisto()
        if h.GetEntries() > 0:
            h.SetName(bdisc)
            rootHistos.append(h)
            myBdiscs.append(bdisc)

    # Create a comparison plot for a given dataset in all CRs
    if len(rootHistos) < 2:
        Print("Cannot plot comparison due to too few non-empty histograms present (=%i)" % (len(rootHistos)), False)
        return

    # Move ref histo to top of rootHisto list
    for rh in rootHistos:
        if opts.refBdisc in rh.GetName():
            s = rootHistos.pop( rootHistos.index(rh) )
            #rootHistos.insert(0, s)
            rootHistos.insert(len(rootHistos), s)

    # Draw the comparison plot (first argument the reference histo for ratio purposes)
    p = plots.ComparisonManyPlot(rootHistos[-1], rootHistos[:-1], saveFormats=[])
    # p = plots.ComparisonManyPlot(rootHistos[0], rootHistos[1:], saveFormats=[])
    p.setLuminosity(opts.intLumi)

    if opts.normalizeToOne:
        p.histoMgr.forEachHisto(lambda h: h.getRootHisto().Scale(1.0/h.getRootHisto().Integral()))

    # Apply drawing/legend styles
    for i, bdisc in enumerate(myBdiscs, 0):
        stylesList[i].apply(p.histoMgr.getHisto(bdisc).getRootHisto())
        if bdisc == opts.refBdisc:
            p.histoMgr.setHistoDrawStyle(bdisc, "AP")
            p.histoMgr.setHistoLegendStyle(bdisc,"LP")
        else:
            p.histoMgr.setHistoDrawStyle(bdisc, "AP")
            p.histoMgr.setHistoLegendStyle(bdisc, "LP")
            #p.histoMgr.setHistoDrawStyle(bdisc, "HIST")
            #p.histoMgr.setHistoLegendStyle(bdisc, "F")
            
    # Add dataset name on canvas
    p.appendPlotObject(histograms.PlotText(0.18, 0.88, plots._legendLabels[opts.dataset], bold=True, size=22))
    #p.appendPlotObject(histograms.PlotText(0.18, 0.88, plots._legendLabels[opts.dataset + " (%s)" % ], bold=True, size=22))

    # Set legend labels
    p.histoMgr.setHistoLegendLabelMany({
            "Loose" : "Loose (%s)"  % (GetCRLabel(histoName)),
            "Medium": "Medium (%s)" % (GetCRLabel(histoName)),
            })
    
    # Draw the plot
    plots.drawPlot(p, saveName, **kwargs) #the "**" unpacks the kwargs_ dictionary

    # Save the plots in custom list of saveFormats
    SavePlot(p, saveName, os.path.join(opts.saveDir), [".png", ".pdf"] )
    return
def PlotEfficiency_comparison(datasetsMgr, numPath, denPath, datasetsMgr_DR,
                              intLumi):

    # Definitions
    myList = []
    index = 0
    _kwargs = GetHistoKwargs(numPath, opts)

    # For-loop: All datasets
    #for dataset in datasetsMgr.getAllDatasets():
    if 1:
        #if "Fake" in numPath:
        #    return
        dataset = datasetsMgr.getDataset("TT")
        dataset_DR = datasetsMgr_DR.getDataset("TT")

        #if "Fake" in numPath:
        #    dataset = datasetsMgr.getDataset("QCD")
        #    dataset_DR = datasetsMgr_DR.getDataset("QCD")

        legend = "t#bar{t} (#Delta R < 0.30, #Delta p_{T} / p_{T} < 0.32)"
        legend_DR = "t#bar{t} (#Delta R < 0.30)"

        styleDef = styles.ttStyle
        style_DR = styles.signalStyleHToTB500  #800

        n = dataset_DR.getDatasetRootHisto(numPath)
        n.normalizeToLuminosity(intLumi)
        num_DR = n.getHistogram()
        d = dataset_DR.getDatasetRootHisto(denPath)
        d.normalizeToLuminosity(intLumi)
        den_DR = d.getHistogram()
        #=========================================
        n = dataset.getDatasetRootHisto(numPath)
        n.normalizeToLuminosity(intLumi)
        num = n.getHistogram()
        d = dataset.getDatasetRootHisto(denPath)
        d.normalizeToLuminosity(intLumi)
        den = d.getHistogram()
        #=========================================

        if "binList" in _kwargs:
            xBins = _kwargs["binList"]
            nx = len(xBins) - 1
            num = num.Rebin(nx, "", xBins)
            den = den.Rebin(nx, "", xBins)
            num_DR = num_DR.Rebin(nx, "", xBins)
            den_DR = den_DR.Rebin(nx, "", xBins)

        for i in range(1, num.GetNbinsX() + 1):
            nbin = num.GetBinContent(i)
            dbin = den.GetBinContent(i)
            nbin_DR = num_DR.GetBinContent(i)
            dbin_DR = den_DR.GetBinContent(i)
            eps = nbin / dbin
            eps_DR = nbin_DR / dbin_DR
            ratio_DR = eps / eps_DR
            if ratio_DR > 1:
                ratio_DR = 1 / ratio_DR
            print "bin: ", i, "eps: ", round(eps,
                                             5), "eps_DR: ", round(eps_DR, 5)
            print "bin: ", i, "eps/eps_DR: ", (1.0 - round(ratio_DR, 3)) * 100

        # Sanity checks
        if den.GetEntries() == 0 or num.GetEntries() == 0:
            #    continue
            return
        if num.GetEntries() > den.GetEntries():
            #    continue
            return

        # Sanity check (Histograms are valid and consistent) - Always false!
        # if not ROOT.TEfficiency.CheckConsistency(num, den):
        #    continue

        # Create Efficiency plots with Clopper-Pearson stats
        eff = ROOT.TEfficiency(num, den)
        eff.SetStatisticOption(ROOT.TEfficiency.kFCP)

        eff_DR = ROOT.TEfficiency(num_DR, den_DR)
        eff_DR.SetStatisticOption(ROOT.TEfficiency.kFCP)

        eff = convert2TGraph(eff)
        eff_DR = convert2TGraph(eff_DR)

        # Apply default style (according to dataset name)
        #plots._plotStyles[dataset.getName()].apply(eff)

        #styleDef.apply(eff)
        #style_DR.apply(eff_DR)

        styles.markerStyles[0].apply(eff)
        styles.markerStyles[1].apply(eff_DR)
        '''
        eff_DR.SetLineStyle(ROOT.kSolid)
        eff.SetLineWidth(2)
        eff_DR.SetLineWidth(2)

        eff.SetLineStyle(ROOT.kSolid)
        eff_DR.SetLineStyle(ROOT.kSolid)

        eff.SetMarkerStyle(ROOT.kFullTriangleUp)
        eff_DR.SetMarkerStyle(ROOT.kFullTriangleUp)
        
        eff.SetMarkerSize(1.2)
        eff_DR.SetMarkerSize(1.2)
        '''
        # Append in list
        myList.append(histograms.HistoGraph(eff, legend, "p", "P"))
        myList.append(histograms.HistoGraph(eff_DR, legend_DR, "p", "P"))

    # Save plot in all formats
    #savePath = os.path.join(opts.saveDir, opts.optMode)
    #plots.drawPlot(p, savePath, **_kwargs)
    #SavePlot(p, saveName, savePath, saveFormats = [".png", ".pdf"])

    # Define stuff
    numPath = numPath.replace("AfterAllSelections_", "")
    numPath = numPath.replace("LeadingTrijet_Pt", "LdgTopPt")
    if "Genuine" in numPath:
        TTcateg = "_TTgenuine"
    elif "Fake" in numPath:
        TTcateg = "_TTfake"
    else:
        TTcateg = "_TTinclusive"

    if "SR" in numPath:
        region = "_SR"
    if "VR" in numPath:
        region = "_VR"
    if "CR1" in numPath:
        region = "_CR1"
    if "CR2" in numPath:
        region = "_CR2"

    saveName = "Eff_" + numPath.split("/")[-1] + TTcateg + "_BDTtraining"

    # Plot the efficiency
    p = plots.ComparisonManyPlot(
        histograms.HistoGraph(eff, legend, "p", "P"),
        [histograms.HistoGraph(eff_DR, legend_DR, "p", "P")],
        saveFormats=[".pdf", ".png", ".C"])

    leg = ROOT.TLegend(0.2, 0.8, 0.81, 0.87)
    leg.SetFillStyle(0)
    leg.SetFillColor(0)
    leg.SetBorderSize(0)
    #{"dx": -0.55, "dy": -0.55, "dh": -0.08}
    leg.SetHeader("t#bar{t}")
    #if "Fake" in numPath:
    #    leg.SetHeader("QCD")

    leg.Draw()
    #moveLegend       =  {"dx": -0.0, "dy": +0.0, "dh": +0.1}
    #moveLegend       = {"dx": -0.1, "dy": +0.0, "dh": +0.1}
    #p.setLegend(histograms.moveLegend(histograms.createLegend(), **moveLegend))

    # Save plot in all formats
    savePath = os.path.join(SAVEDIR, "HplusMasses",
                            numPath.split("/")[0], opts.optMode)
    #savePath = os.path.join(opts.saveDir, numPath.split("/")[0], opts.optMode)
    plots.drawPlot(p, SAVEDIR, **_kwargs)
    #leg.Draw()
    SavePlot(p, saveName, SAVEDIR, saveFormats=[".png", ".pdf", ".C"])
    return
def PlotHistoGraphs(hGraphList, _kwargs):

    # Create & draw the plot
    p = plots.ComparisonManyPlot(hGraphList[0], hGraphList[1:], saveFormats=[])
    p.setLuminosity(opts.intLumi)

    if 0:
        p.histoMgr.setHistoLegendStyleAll("P")
        p.histoMgr.setHistoDrawStyleAll("LP")
        p.histoMgr.setHistoDrawStyle(hGraphList[0].getRootHisto().GetName(),
                                     "HIST")

    if opts.saveName == None:
        opts.saveName = hGraphList[0].getRootHisto().GetName().split(
            "_")[0] + "VsCutflow"

    # Draw the plot
    p.histoMgr.forEachHisto(
        lambda h: h.getRootHisto().SetLineStyle(ROOT.kSolid))
    p.histoMgr.forEachHisto(lambda h: h.getRootHisto().SetLineWidth(3))
    p.histoMgr.forEachHisto(
        lambda h: h.getRootHisto().SetMarkerSize(1.3))  #1.2
    if opts.acceptance:
        p.histoMgr.forEachHisto(
            lambda h: h.getRootHisto().SetMarkerStyle(ROOT.kFullCircle))
    plots.drawPlot(p, opts.saveName, **_kwargs)

    if not opts.acceptance:
        p.getPad2().SetLogy(True)

    # Add some text
    if opts.refCounter == "passed PV":
        xPos = [0.15, 0.24, 0.37, 0.50, 0.65, 0.77,
                0.90]  #does not work for every reference counter!
    else:
        xPos = [
            0.15 + float(i) / (float(len(opts.binLabels)))
            for i in range(0, len(opts.binLabels))
        ]

    # For-loop: All bin labels
    for i, b in enumerate(opts.binLabels, 0):
        if opts.acceptance:
            break

        # Calculate coordinats
        dx = 0.10 * (i)
        dy = 0.0
        if _kwargs["ratio"]:
            dy = -0.4

        # Basic text replacemenets to get compact bin text
        bin = b.replace("passed ", "").replace(")", "").replace(
            "selection", "").replace("(", "").replace("Selection",
                                                      "").replace("PV", "pv")
        label = bin.replace("mu", "#mu").replace("Passed", "").replace(
            "tau", "#tau").replace("  Veto",
                                   "-veto").replace("Selected Events",
                                                    "selected")
        label = label.replace("jet", "jets").replace("-", " ")

        if 1:
            #histograms.addText(0.16+dx, 0.08+dy, label, 19)
            histograms.addText(xPos[i], 0.08 + dy, label, 19)
        else:
            # Automatic assignment (not perfect, yet!)
            x = opts.xValues[i] / opts.xValues[-1]
            xlow = opts.xErrLow[i] / opts.xErrLow[-1]
            xhigh = opts.xErrHigh[i] / opts.xErrHigh[-1]
            if i == 0:
                x = 0.15
            if i > 2:
                x -= 0.05
            if i == len(opts.binLabels) - 1:
                x = 0.92
            if not opts.acceptance:
                histograms.addText(x, 0.08 + dy, label, 19)

    # Save the plot
    SavePlot(p,
             opts.saveName,
             os.path.join(opts.saveDir, opts.optMode),
             saveFormats=[".C", ".png", ".pdf"])
    return
Exemplo n.º 25
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)

    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())

    # Get histos (Data, EWK) for GenuineB
    p2 = plots.ComparisonPlot(
        *getHistos(datasetsMgr, histoName.replace(
            defaultFolder, genuineBFolder), analysisType))
    p2.histoMgr.normalizeMCToLuminosity(
        datasetsMgr.getDataset("Data").getLuminosity())

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

    # Subtract EWKGEnuineB from Data to get FakeB
    FakeB.Add(EWKGenuineB, -1)
    #FakeB.Add(EWK, -1)

    # Dos not work
    # p1.histoMgr.forEachHisto(lambda h: h.getRootHisto().Rebin(2))
    # p2.histoMgr.forEachHisto(lambda h: h.getRootHisto().Rebin(2))

    # Comparison plot. The first argument is the reference histo. All other histograms are compared with respect to that.
    FakeB_Purity, xMin, xMax = getPurityHisto(FakeB,
                                              Data,
                                              inclusiveBins=False,
                                              printValues=False)
    EWKGenuineB_Purity, xMin, xMax = getPurityHisto(EWKGenuineB,
                                                    Data,
                                                    inclusiveBins=False,
                                                    printValues=False)
    p = plots.ComparisonManyPlot(FakeB_Purity, [EWKGenuineB_Purity],
                                 saveFormats=[])

    # Apply histo styles
    p.histoMgr.forHisto(
        analysisType + "-FakeB",
        styles.getInvertedLineStyle())  #styles.getFakeBLineStyle() )
    p.histoMgr.forHisto(
        analysisType + "-EWK",
        styles.getAltEWKLineStyle())  #styles.getAltEWKStyle() )

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

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

    # p.histoMgr.setHistoLegendStyleAll("LP")

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

    # Draw the histograms
    _cutBox = None
    _rebinX = 1
    _opts = {"ymin": 1e-3, "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"] = 1500.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"] = 3500.0

    # Do the plot
    plots.drawPlot(
        p,
        histoName,
        xlabel=_xlabel,
        ylabel="Purity / {0}".format(_format),
        log=False,
        rebinX=_rebinX,  # Can only Rebin BEFORE calculating purity
        cmsExtraText="Preliminary",
        createLegend={
            "x1": 0.60,
            "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.º 26
0
 def doPlot(self, opts, myAllShapeNuisances, f, mass, luminosity, signalTable, rebinList=None):
     def rebin(h, rebinList):
         if rebinList == None or h == None:
             return h
         myArray = array.array("d",rebinList)
         hnew = h.Rebin(len(rebinList)-1,h.GetTitle(),myArray)
         h.Delete()
         return hnew
     
     print "Doing plots for:",self._name
     hNominal = f.Get(self._cardReader.getHistoNameForColumn(self._name))
     hNominal = rebin(hNominal, rebinList)
     hNominal.SetFillStyle(0)
     hNominalFine = f.Get(self._cardReader.getHistoNameForColumn(self._name)+"_fineBinning")
     hNominalFine = rebin(hNominalFine, rebinList)
     hNominalHisto = histograms.Histo(hNominal, self._name, drawStyle="HIST")
     # Determine label
     mySignalLabels = ["HH","HW","Hp"]
     mySignalStatus = False
     for l in mySignalLabels:
         if l in self._name:
             mySignalStatus = True
     myMCLabels = ["HH","HW","Hp","MC"]
     myMCStatus = False
     
     for mclab in myMCLabels:
         if mclab in self._name:
             myMCStatus = True
     histograms.cmsTextMode = histograms.CMSMode.PRELIMINARY
     if myMCStatus:
         histograms.cmsTextMode = histograms.CMSMode.SIMULATION_PRELIMINARY
     x = 0.6
     size = 20
     myRatioContainer = RatioPlotContainer(self._label)
     for uncName in self._uncertaintyShapes:
         myShortName = uncName
         print "... uncertainty:",myShortName
         myLongName = self._cardReader.getHistoNameForNuisance(self._name, uncName)
         hup = f.Get("%sUp"%(myLongName))
         hup = rebin(hup, rebinList)
         hup.SetFillStyle(0)
         up = dataset.RootHistoWithUncertainties(hup)
         upFine = f.Get("%sUp_fineBinning"%(myLongName))
         upFine = rebin(upFine, rebinList)
         nom = dataset.RootHistoWithUncertainties(hNominal.Clone())
         nom.makeFlowBinsVisible()
         hdown = f.Get("%sDown"%(myLongName))
         hdown = rebin(hdown, rebinList)
         hdown.SetFillStyle(0)
         down = dataset.RootHistoWithUncertainties(hdown)
         downFine = f.Get("%sDown_fineBinning"%(myLongName))
         downFine = rebin(downFine, rebinList)
         up.getRootHisto().SetLineColor(ROOT.kRed)
         nom.getRootHisto().SetLineColor(ROOT.kBlack)
         down.getRootHisto().SetLineColor(ROOT.kBlue)
         upHisto = histograms.Histo(up, "Up %.1f"%_integral(up.getRootHisto()), drawStyle="HIST", legendStyle="l")
         downHisto = histograms.Histo(down, "Down %.1f"%_integral(down.getRootHisto()), drawStyle="HIST", legendStyle="l")
         nomHisto = histograms.Histo(nom, "Nominal %.1f"%_integral(nom.getRootHisto()), drawStyle="HIST", legendStyle="l")
         # Add fit uncert. as stat uncert.
         for i in range(0,9):
             tailFitUp = f.Get("%s_TailFit_par%dUp"%(myLongName,i))
             tailFitDown = f.Get("%s_TailFit_par%dDown"%(myLongName,i))
             if tailFitUp != None and tailFitDown != None:
                 nom.addShapeUncertaintyFromVariation("%s_TailFit_par%d"%(self._name,i),tailFitUp,tailFitDown)
                 tailFitUp.Delete()
                 tailFitDown.Delete()
         tailfitNames = filter(lambda n: "_TailFit_" in n, nom.getShapeUncertaintyNames())
         nom.setShapeUncertaintiesAsStatistical(tailfitNames)
         # Do plot
         plot = plots.ComparisonManyPlot(nomHisto, [upHisto, downHisto])
         plot.setLuminosity(luminosity)
         plot.histoMgr.forEachHisto(lambda h: h.getRootHisto().SetLineWidth(3))
         myPlotName = "%s/shapeSyst_%s_syst%s" % (_dirname, self._label, uncName.replace("Up",""))
         myParams = {}
         myParams["ylabel"] = "Events"
         myParams["log"] = False
         #myParams["opts2"] = {"ymin": 0.0, "ymax":2.0}
         myParams["opts2"] = {"ymin": 0.7, "ymax":1.3}
         myParams["opts"] = {"ymin": 0.0}
         myParams["ratio"] = True
         myParams["ratioType"] = "errorScale"
         myParams["ratioYlabel"] = "Var./Nom."
         myParams["addLuminosityText"] = True
         myParams["customizeBeforeDraw"] = customizeMarker
         #myParams["ratioErrorOptions"] = {"numeratorStatSyst": False}
         myParams["addMCUncertainty"] = True
         
         plots.drawPlot(plot, myPlotName, **myParams)
         myRatioContainer.addRatioPlot(plot, myShortName)
         # Analyse up and down variation
         if mySignalStatus and upFine != None and downFine != None:
             a = abs(upFine.Integral()/hNominalFine.Integral() - 1.0)
             b = abs(1.0 - downFine.Integral()/hNominalFine.Integral())
             r = a
             if b > a:
                 r = b
             if uncName in signalTable.keys():
                 if r < signalTable[uncName]["min"]:
                     signalTable[uncName]["min"] = r
                 if r > signalTable[uncName]["max"]:
                     signalTable[uncName]["max"] = r
             else:
                 signalTable[uncName] = {}
                 signalTable[uncName]["min"] = r
                 signalTable[uncName]["max"] = r
         
     # Create plots with only the ratio plot
     if opts.individual:
         myRatioContainer.drawIndividually()
     else:
         myRatioContainer.drawAllInOne(myAllShapeNuisances, luminosity)
Exemplo n.º 27
0
def PlotEfficiency(datasetsMgr, numPath, denPath):
    # Definitions
    myList = []
    _kwargs = GetHistoKwargs(numPath, opts)
    nx = 0
    if len(_kwargs["binList"]) > 0:
        xBins = _kwargs["binList"]
        nx = len(xBins) - 1
    counter = 0

    # For-loop: All datasets
    for dataset in datasetsMgr.getAllDatasets():

        if dataset.isMC():
            n = dataset.getDatasetRootHisto(numPath)
            d = dataset.getDatasetRootHisto(denPath)
            num = n.getHistogram()
            den = d.getHistogram()

            if nx > 0:
                num = num.Rebin(nx, "", xBins)
                den = den.Rebin(nx, "", xBins)
        else:
            num = dataset.getDatasetRootHisto(numPath).getHistogram()
            den = dataset.getDatasetRootHisto(denPath).getHistogram()
            if nx > 0:
                num = num.Rebin(nx, "", xBins)
                den = den.Rebin(nx, "", xBins)

        # Calculations
        total = den.Integral(0, den.GetXaxis().GetNbins() + 1)
        selected = num.Integral(0, num.GetXaxis().GetNbins() + 1)

        if 0:
            print "Numerical Efficiency", numPath, dataset.getName(
            ), ":", round(selected / total, 3)

        # Sanity checks
        if den.GetEntries() == 0 or num.GetEntries() == 0:
            continue
        if num.GetEntries() > den.GetEntries():
            continue

        # Create Efficiency plots with Clopper-Pearson stats
        eff = ROOT.TEfficiency(num, den)
        eff.SetStatisticOption(ROOT.TEfficiency.kFCP)  #FCP

        datasetTT = datasetsMgr.getDataset("TT")
        # Get the histograms
        numTT = datasetTT.getDatasetRootHisto(numPath).getHistogram()
        denTT = datasetTT.getDatasetRootHisto(denPath).getHistogram()
        if nx > 0:
            numTT = numTT.Rebin(nx, "", xBins)  #num.Rebin(nx, "", xBins)
            denTT = denTT.Rebin(nx, "", xBins)  #den.Rebin(nx, "", xBins)
        '''
        for i in range(1, num.GetNbinsX()+1):
            nbin = num.GetBinContent(i)
            dbin = den.GetBinContent(i)
            nbinTT = numTT.GetBinContent(i)
            dbinTT = denTT.GetBinContent(i)
            eps = nbin/dbin
            epsTT = nbinTT/dbinTT
            ratioTT = eps/epsTT
            if ratioTT > 1:
                ratioTT = 1/ratioTT
            #print "bin: ", i, "eps: ", round(eps,5) , "epsTT: ", round(epsTT,5)
            #print "bin: ", i, "eps/epsTT: ", (1.0 - round(ratioTT, 3))*100
        '''
        eff_ref = ROOT.TEfficiency(numTT, denTT)
        eff_ref.SetStatisticOption(ROOT.TEfficiency.kFCP)  #FCP

        # Convert to TGraph
        gEff = convert2TGraph(eff)
        gEffRef = convert2TGraph(eff_ref)

        # Style definitions
        stylesDef = styles.ttStyle
        styles0 = styles.signalStyleHToTB300
        styles1 = styles.signalStyleHToTB500
        styles2 = styles.signalStyleHToTB800
        styles3 = styles.signalStyleHToTB500
        styles4 = styles.signalStyleHToTB1000
        styles5 = styles.signalStyleHToTB2000
        styles6 = styles.signalStyleHToTB180
        styles7 = styles.signalStyleHToTB3000
        styles8 = styles.signalStyleHToTB200

        if dataset.getName() == "TT":
            styles.ttStyle.apply(gEffRef)
            legend_ref = "t#bar{t}"
            if opts.type == "partonShower":
                legend_ref = "t#bar{t} (Pythia8)"
            elif opts.type == "evtGen":
                legend_ref = "t#bar{t} (Powheg)"
            refGraph = histograms.HistoGraph(gEffRef, legend_ref, "p", "P")
        else:
            styles.markerStyles[counter].apply(gEff)
            legend = dataset.getName().replace("TT_", "t#bar{t} (").replace(
                "isr", "ISR ").replace("fsr", "FSR ")
            legend = legend.replace("hdamp", "hdamp ").replace("DOWN",
                                                               "down").replace(
                                                                   "UP", "up")
            legend = legend.replace("mtop1665", "m_{t} = 166.5 GeV")
            legend = legend.replace("mtop1695", "m_{t} = 169.5 GeV")
            legend = legend.replace("mtop1715", "m_{t} = 171.5 GeV")
            legend = legend.replace("mtop1735", "m_{t} = 173.5 GeV")
            legend = legend.replace("mtop1755", "m_{t} = 175.5 GeV")
            legend = legend.replace("mtop1785", "m_{t} = 178.5 GeV")
            legend = legend.replace("TuneEE5C", "Herwig++")
            legend += ")"
            counter += 1
            #myList.append(histograms.HistoGraph(gEff, legend, "lp", "P"))
            myList.append(histograms.HistoGraph(gEff, legend, "p", "P"))

    # Define stuff
    numPath = numPath.replace("AfterAllSelections_", "")
    saveName = "Efficiency_%s_%s" % (opts.folder, opts.type)
    saveName = saveName.replace("__", "_Inclusive_")

    # Plot the efficiency
    p = plots.ComparisonManyPlot(refGraph, myList, saveFormats=[])
    savePath = os.path.join(opts.saveDir, opts.optMode)
    plots.drawPlot(p, savePath, **_kwargs)

    # Save plot in all formats
    SavePlot(p, saveName, savePath, saveFormats=[".png", ".pdf", ".C"])
    return
def PlotEfficiency(datasetsMgr, datasetsMgr40, intLumi):

    # Definitions
    myList = []

    dataset_TT = datasetsMgr.getDataset("TT")
    dataset40_TT = datasetsMgr40.getDataset("TT")
    dataset_QCD = datasetsMgr.getDataset("QCD")
    dataset40_QCD = datasetsMgr40.getDataset("QCD")

    # Efficiency
    hNumerator = os.path.join(opts.folder, "AllTopQuarkPt_MatchedBDT")
    hDenominator = os.path.join(opts.folder,
                                "TopQuarkPt")  #AllTopQuarkPt_Matched
    n_TT = dataset40_TT.getDatasetRootHisto(hNumerator)
    n_TT.normalizeToLuminosity(intLumi)
    num40_TT = n_TT.getHistogram()

    d_TT = dataset40_TT.getDatasetRootHisto(hDenominator)
    d_TT.normalizeToLuminosity(intLumi)
    den40_TT = d_TT.getHistogram()

    # Misidentification rate
    hNumerator = os.path.join(opts.folder, "TrijetFakePt_BDT")
    hDenominator = os.path.join(opts.folder, "TrijetFakePt")
    n_QCD = dataset40_QCD.getDatasetRootHisto(hNumerator)
    n_QCD.normalizeToLuminosity(intLumi)
    num40_QCD = n_QCD.getHistogram()

    d_QCD = dataset40_QCD.getDatasetRootHisto(hDenominator)
    d_QCD.normalizeToLuminosity(intLumi)
    den40_QCD = d_QCD.getHistogram()

    # Customise binning
    _kwargs = GetHistoKwargs(opts)
    if "binList" in _kwargs:
        xBins = _kwargs["binList"]
        nx = len(xBins) - 1
        num40_TT = num40_TT.Rebin(nx, "", xBins)
        den40_TT = den40_TT.Rebin(nx, "", xBins)
        num40_QCD = num40_QCD.Rebin(nx, "", xBins)
        den40_QCD = den40_QCD.Rebin(nx, "", xBins)

    # Calculate efficiency
    eff40_TT = ROOT.TEfficiency(num40_TT, den40_TT)
    eff40_TT.SetStatisticOption(ROOT.TEfficiency.kFCP)

    eff40_QCD = ROOT.TEfficiency(num40_QCD, den40_QCD)
    eff40_QCD.SetStatisticOption(ROOT.TEfficiency.kFCP)  #

    eff40_TT = convert2TGraph(eff40_TT)
    eff40_QCD = convert2TGraph(eff40_QCD)

    styles.ttStyle.apply(eff40_TT)
    styles.qcdStyle.apply(eff40_QCD)

    # Append in list
    gEff40_TT = histograms.HistoGraph(eff40_TT, "t#bar{t}", "lp", "P")
    #gEff40_QCD = histograms.HistoGraph(eff40_QCD, "QCD multijet", "lp", "P")
    gEff40_QCD = histograms.HistoGraph(eff40_QCD, "QCD", "lp", "P")
    myList.append(gEff40_TT)
    myList.append(gEff40_QCD)

    # Define save name
    saveName = "TopTagEfficiency"

    # Plot the efficiency
    #p = plots.PlotBase(datasetRootHistos=myList, saveFormats=[])
    p = plots.ComparisonManyPlot(gEff40_TT, [gEff40_QCD], saveFormats=[])
    #p = plots.ComparisonPlot(gEff40_TT, gEff40_QCD, saveFormats=[])
    plots.drawPlot(p, saveName, **_kwargs)

    # Save plot in all formats
    savePath = os.path.join(opts.saveDir, opts.optMode)
    save_path = os.path.join(savePath, "BDT0p40")
    SavePlot(p, saveName, save_path, saveFormats=[".png", ".C", ".pdf"])
    return
Exemplo n.º 29
0
def PlotTemplates(datasetsMgr, histoName, analysisType="Inverted"):                  
    Verbose("Plotting EWK Vs QCD unity-normalised histograms")

    # Create comparison plot
    p1 = plots.ComparisonPlot(
        getHisto(datasetsMgr, "Data", histoName, "Baseline"),
        getHisto(datasetsMgr, "Data", histoName, "Inverted")
        )
    p1.histoMgr.normalizeMCToLuminosity(datasetsMgr.getDataset("Data").getLuminosity())
    
    defaultFolder  = "ForFakeBMeasurement"
    genuineBFolder = defaultFolder + "EWKGenuineB"
    fakeBFolder    = defaultFolder + "EWKFakeB"
    histoNameNew   = histoName.replace( defaultFolder, genuineBFolder)
    p2 = plots.ComparisonPlot(
        getHisto(datasetsMgr, "EWK", histoNameNew, "Baseline"),
        getHisto(datasetsMgr, "EWK", histoNameNew, "Inverted")
        )
    p2.histoMgr.normalizeMCToLuminosity(datasetsMgr.getDataset("Data").getLuminosity())

    # Get EWKGenuineB histos
    EWKGenuineB_baseline = p2.histoMgr.getHisto("Baseline-EWK").getRootHisto().Clone("Baseline-EWKGenuineB")
    EWKGenuineB_inverted = p2.histoMgr.getHisto("Inverted-EWK").getRootHisto().Clone("Inverted-EWKGenuineB")

    # Get FakeB histos
    FakeB_baseline = p1.histoMgr.getHisto("Baseline-Data").getRootHisto().Clone("Baseline-FakeB")
    FakeB_baseline.Add(EWKGenuineB_baseline, -1)
    FakeB_inverted = p1.histoMgr.getHisto("Inverted-Data").getRootHisto().Clone("Inverted-FakeB")
    FakeB_inverted.Add(EWKGenuineB_inverted, -1)

    # Normalize histograms to unit area
    EWKGenuineB_baseline.Scale(1.0/EWKGenuineB_baseline.Integral())    
    EWKGenuineB_inverted.Scale(1.0/EWKGenuineB_inverted.Integral())
    FakeB_baseline.Scale(1.0/FakeB_baseline.Integral())
    FakeB_inverted.Scale(1.0/FakeB_inverted.Integral())
    
    # Create the final plot object
    comparisonList = [EWKGenuineB_baseline]
    p = plots.ComparisonManyPlot(FakeB_inverted, comparisonList, saveFormats=[])
    p.setLuminosity(GetLumi(datasetsMgr))
        
    # Apply styles
    p.histoMgr.forHisto("Baseline-EWKGenuineB", styles.getBaselineStyle() )
    p.histoMgr.forHisto("Inverted-FakeB"      , styles.getInvertedStyle() )

    # Set draw style
    p.histoMgr.setHistoDrawStyle("Baseline-EWKGenuineB", "AP")
    p.histoMgr.setHistoDrawStyle("Inverted-FakeB"      , "HIST")

    # Set legend style
    p.histoMgr.setHistoLegendStyle("Baseline-EWKGenuineB", "LP")
    p.histoMgr.setHistoLegendStyle("Inverted-FakeB"        , "LP")

    # Set legend labels
    p.histoMgr.setHistoLegendLabelMany({
            "Baseline-EWKGenuineB": "GenuineB (EWK)", # (Baseline)
            "Inverted-FakeB"      : "FakeB", # (Inverted)
            })

    # Append analysisType to histogram name
    saveName = histoName

    # 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)
    
    # Add text
    text = opts.optMode.replace("OptChiSqrCutValue", "#chi^{2} #leq ")
    histograms.addText(0.21, 0.85, text)

    # Save plot in all formats
    saveDir = os.path.join(opts.saveDir, "Templates", opts.optMode)
    SavePlot(p, saveName, saveDir, saveFormats = [".C", ".png", ".pdf"])
    return
Exemplo n.º 30
0
def PlotHistograms(datasetsMgr, histoList, binLabels, opts):
    '''
    histoList contains all histograms for all bins for CR1 and CR2 
    
    '''
    # Get the root histos for all datasets and Control Regions (CRs)
    rhDict = GetRootHistos(datasetsMgr, histoList)

    # For-loop: All root-histo keys (All fake-B bins, all CRs, (CR1, CR2, ..)  and all folders (Data, EWKFakeB, EWKGenuineB)
    for key1 in rhDict:
        # Definitions
        region1 = "CRone"
        region2 = "CRtwo"
        key2 = key1.replace(region1, region2)
        dataset = key1.split("-")[0]
        region = key1.split("-")[1]
        bin = key1.split("-")[2]
        hName1 = rhDict[key1].GetName()
        hName2 = rhDict[key2].GetName()
        bInclusive = "Inclusive" in key1

        # Dataset and Region filter
        if dataset != "FakeB":
            continue
        # This gives CR1. can get CR2 by replacing CR1 with CR2 (histo-names are identical otherwise)
        if region != region1:
            continue

        Verbose("Accessing histogram %s" % key1, False)
        if bInclusive:
            Verbose(
                "The histo key is \"%s\" and its name is \"%s\"" %
                (key1, rhDict[key1].GetName()), True)

            rFakeB_CRone = rhDict[key1].Clone()
            rFakeB_CRone.Reset("ICES")

            rFakeB_CRtwo = rhDict[key2].Clone()
            rFakeB_CRtwo.Reset("ICES")

            # For-loop: All fakeB bins (to add-up all binned histos)
            for i, b in enumerate(binLabels, 1):
                if "Inclusive" in b:
                    Verbose("Skipping bin-label %s" % (b), False)
                    continue
                else:
                    Verbose("Adding bin %s" % (b), False)

                # Determine keys
                k1 = key1.replace("Inclusive", b)
                k2 = key2.replace("Inclusive", b)

                # Normalise bin histo to one (before adding to inclusive histo)
                Verbose("Cloning histogram %s" % (rhDict[k1].GetName()), False)
                h1 = rhDict[k1].Clone()
                h2 = rhDict[k2].Clone()

                # First normalise the histos
                h1.Scale(1.0 / h1.Integral())
                h2.Scale(1.0 / h2.Integral())

                # Add-up individual bins
                rFakeB_CRone.Add(h1, +1)
                rFakeB_CRtwo.Add(h2, +1)
        else:
            # Get the histos
            rFakeB_CRone = rhDict[key1]
            rFakeB_CRtwo = rhDict[key2]

        # Normalise the histos?
        if opts.normaliseToOne:
            rFakeB_CRone.Scale(1.0 / rFakeB_CRone.Integral())
            rFakeB_CRtwo.Scale(1.0 / rFakeB_CRtwo.Integral())

        # Apply histogram styles
        styles.getABCDStyle("CRone").apply(rFakeB_CRone)
        styles.getABCDStyle("CRtwo").apply(rFakeB_CRtwo)

        # Create the plot
        p = plots.ComparisonManyPlot(rFakeB_CRone, [rFakeB_CRtwo],
                                     saveFormats=[])
        p.setLuminosity(opts.intLumi)

        # Set draw/legend style
        p.histoMgr.setHistoDrawStyle(hName1, "AP")
        p.histoMgr.setHistoLegendStyle(hName1, "LP")

        p.histoMgr.setHistoDrawStyle(hName2, "HIST")
        p.histoMgr.setHistoLegendStyle(hName2, "F")

        # Set legend labels
        p.histoMgr.setHistoLegendLabelMany({
            hName1: "CR1",
            hName2: "CR2",
        })

        # Draw the plot and save it
        if bin == "Inclusive":
            histoName = histoList[0] + "_CR1vCR2_combined"
        else:
            histoName = histoList[0] + "_CR1vCR2_bin%s" % (bin)
        saveName = histoName.split("/")[-1].replace("CRone0_",
                                                    "").replace("CRtwo0_", "")

        # Get the histogram customisations (keyword arguments)
        p.appendPlotObject(
            histograms.PlotText(0.20,
                                0.88,
                                GetBinText(bin),
                                bold=True,
                                size=22))
        plots.drawPlot(p, saveName, **GetHistoKwargs(saveName))
        SavePlot(p,
                 saveName,
                 os.path.join(opts.saveDir, opts.optMode),
                 saveFormats=[".png"])  #, ".pdf"])
    return