Example #1
0
def addConfigInfo(of, dataset):
    d = of.mkdir("configInfo")
    d.cd()

    # configinfo histogram
    configinfo = ROOT.TH1F("configinfo", "configinfo", 3, 0, 3)
    axis = configinfo.GetXaxis()

    def setValue(bin, name, value):
        axis.SetBinLabel(bin, name)
        configinfo.SetBinContent(bin, value)

    setValue(1, "control", 1)
    if dataset.isData():
        setValue(2, "luminosity", dataset.getLuminosity())
        setValue(3, "isData", 1)
    elif dataset.isMC():
        setValue(2, "crossSection", dataset.getCrossSection())
        setValue(3, "isData", 0)

    configinfo.Write()
    configinfo.Delete()

    # dataVersion
    ds = dataset
    if dataset.isData():
        ds = dataset.datasets[0]

    dataVersion = ROOT.TNamed("dataVersion", ds.dataVersion)
    dataVersion.Write()
    dataVersion.Delete()

    # codeVersion
    codeVersion = ROOT.TNamed("codeVersion", git.getCommitId())
    codeVersion.Write()
    codeVersion.Delete()

    of.cd()
def addConfigInfo(of, dataset):
    d = of.mkdir("configInfo")
    d.cd()

    # configinfo histogram
    configinfo = ROOT.TH1F("configinfo", "configinfo", 3, 0, 3)
    axis = configinfo.GetXaxis()

    def setValue(bin, name, value):
        axis.SetBinLabel(bin, name)
        configinfo.SetBinContent(bin, value)

    setValue(1, "control", 1)
    if dataset.isData():
        setValue(2, "luminosity", dataset.getLuminosity())
        setValue(3, "isData", 1)
    elif dataset.isMC():
        setValue(2, "crossSection", dataset.getCrossSection())
        setValue(3, "isData", 0)

    configinfo.Write()
    configinfo.Delete()

    # dataVersion
    ds = dataset
    if dataset.isData():
        ds = dataset.datasets[0]

    dataVersion = ROOT.TNamed("dataVersion", ds.dataVersion)
    dataVersion.Write()
    dataVersion.Delete()

    # codeVersion
    codeVersion = ROOT.TNamed("codeVersion", git.getCommitId())
    codeVersion.Write()
    codeVersion.Delete()

    of.cd()
def plotTurnOn(datasets,
               pathAll,
               pathPassed,
               commonText,
               dataText=None,
               mcText=None,
               rebin=1,
               ratio=False):
    dataLabel = "Data"
    mcLabel = "Simulation"
    if dataText != None and mcText == None:
        raise Exception("mcText must not be None when dataText is not")
    if dataText == None and mcText != None:
        raise Exception("dataText must not be None when mcText is not")


#    if dataText != None:
#        dataLabel += ": "+dataText
#        mcLabel += ": "+mcText

    mc_effs = []
    data_eff_gr = None
    binWidth = None
    luminosity = 0
    for dataset in datasets.getAllDatasets():
        all = dataset.getDatasetRootHisto(pathAll).getHistogram()
        passed = dataset.getDatasetRootHisto(pathPassed).getHistogram()

        if rebin > 1:
            all.Rebin(rebin)
            passed.Rebin(rebin)
        binWidth = all.GetBinWidth(1)

        if dataset.isMC() and not "TTTo" in dataset.getName():
            #if dataset.isMC() and not "TTTo" in dataset.getName() and not "QCD" in dataset.getName():
            #if dataset.isMC() and "QCD" in dataset.getName():
            mc_effs.append(HistoEff(all, passed, dataset))

        elif dataset.isData():
            data_eff_gr = ROOT.TGraphAsymmErrors(passed, all,
                                                 "cp")  # 0.683 cl is default
            luminosity += dataset.getLuminosity()

    mc_eff_gr = combineHistoEffs(mc_effs)

    p = plots.ComparisonPlot(
        histograms.HistoGraph(data_eff_gr, "Data", "p", "P"),
        histograms.HistoGraph(mc_eff_gr, "Simulation", "p", "P"))
    p.histoMgr.forEachHisto(
        styles.generator2(styles.StyleMarker(markerSizes=[1.0, 1.5]),
                          plotStyles))
    p.histoMgr.setHistoLegendLabelMany({
        "Data": dataLabel,
        "Simulation": mcLabel,
    })
    p.setLuminosity(luminosity)
    #p.addGraph(data_eff_gr, "Data")
    #p.addGraph(mc_eff_gr, "Simulation")
    #p.finalize()

    opts = {"ymin": 0.0, "ymax": 1.1}
    #opts2 = {"ymin": 0.5, "ymax": 1.5}
    opts2 = {"ymin": 0.7, "ymax": 1.3}

    name = "calomet_turnon_" + runs
    if not mcDataDefinition:
        name += "_McSummer11"
    if caloMetNoHF:
        name += "_caloMetNoHF"

    p.createFrame(name, createRatio=ratio, opts=opts, opts2=opts2)
    if ratio:
        p.getFrame2().GetYaxis().SetTitle("Ratio")
        p.cf.canvas.cd(2)
        p.line1 = plots._createRatioLine(p.cf.frame.getXmin(),
                                         p.cf.frame.getXmax(), 0.9)
        p.line2 = plots._createRatioLine(p.cf.frame.getXmin(),
                                         p.cf.frame.getXmax(), 1.1)
        p.line1.SetLineColor(ROOT.kBlue)
        p.line2.SetLineColor(ROOT.kBlue)
        p.line1.Draw("L")
        p.line2.Draw("L")
        p.cf.canvas.cd(1)

    p.setLegend(
        histograms.moveLegend(
            histograms.createLegend(y1=0.95, y2=0.85),
            #dx=-0.55, dy=-0.05
            #dx=-0.44, dy=-0.58
            dx=-0.3,
            dy=-0.4))

    x = 0.25
    y = 0.3
    dy = 0.035
    mcColor = mc_eff_gr.GetMarkerColor()
    size = 17
    p.appendPlotObject(
        histograms.PlotText(x, y, "Data (runs %s):" % runs, size=size))
    y -= dy
    p.appendPlotObject(histograms.PlotText(x, y, dataText, size=size))
    y -= dy

    y -= 0.01
    p.appendPlotObject(
        histograms.PlotText(x, y, "Simulation:", size=size, color=mcColor))
    y -= dy
    p.appendPlotObject(
        histograms.PlotText(x, y, mcText, size=size, color=mcColor))
    y -= dy

    def text():
        l = ROOT.TLatex()
        l.SetNDC()
        #        l.SetTextFont(l.GetTextFont()-20) # bold -> normal
        l.SetTextSize(l.GetTextSize() * 0.65)
        #l.DrawLatex(0.35, 0.4, commonText)
        l.DrawLatex(0.48, 0.32, commonText)

    textFunction = text
    if dataText != None:
        textFunction = None
    #common(p, "PF E_{T}^{miss} (GeV)", "Efficiency / %.0f GeV"%binWidth, afterDraw=textFunction)
    common(p,
           "Uncorrected PF E_{T}^{miss} (GeV)",
           "Efficiency",
           afterDraw=textFunction)
def printEfficiency(datasets, bin, function):
    backup = ROOT.gErrorIgnoreLevel
    ROOT.gErrorIgnoreLevel = ROOT.kError

    mc_all = 0
    mc_passed = 0
    data_all = 0
    data_passed = 0
    #    mc_eff = ROOT.TEfficiency("mc", "mc", 1, 0, 1)
    #    mc_eff.SetStatisticOption(ROOT.TEfficiency.kFCP)
    mc_effs = []
    for dataset in datasets.getAllDatasets():
        #        print histo.getRootHisto().GetNbinsX(), histo.getXmin(), histo.getXmax()
        #        hpass = histograms.dist2pass(histo.getRootHisto(), greaterThan=True)

        #        hpass = histograms.dist2pass(dataset.getDatasetRootHisto(path).getHistogram(), greaterThan=True)
        #        all = hpass.GetBinContent(1)
        #        passed = hpass.GetBinContent(bin)
        #        cutvalue = hpass.GetBinCenter(bin)
        (all, passed, cutvalue) = function(dataset, bin)
        eff = Eff(all, passed, dataset)
        print "Dataset %-35s, eff %f + %f - %f" % (dataset.getName(), eff.eff,
                                                   eff.eff_up, eff.eff_down)
        #        print all, passed, cutvalue

        #        e = ROOT.TEfficiency("mc"+dataset.getName(), "mc_"+dataset.getName(), 1, 0, 1)
        #        e.SetStatisticOption(ROOT.TEfficiency.kFCP)

        if dataset.isMC() and not "TTTo" in dataset.getName():
            mc_all += all
            mc_passed += passed
            mc_effs.append(eff)
        elif dataset.isData():
            data_all += all
            data_passed += passed

    #cl = 0.95
    cl = 0.683

    mc_eff = mc_passed / mc_all
    mc_eff_up = ROOT.TEfficiency.ClopperPearson(int(mc_all), int(mc_passed),
                                                cl, True)
    mc_eff_down = ROOT.TEfficiency.ClopperPearson(int(mc_all), int(mc_passed),
                                                  cl, False)
    mc_eff_up = mc_eff_up - mc_eff
    mc_eff_down = mc_eff - mc_eff_down

    (cmc_eff, cmc_eff_up, cmc_eff_down) = combineEffs(mc_effs)
    cmc_eff_err = max(cmc_eff_up, cmc_eff_down)

    data_eff = data_passed / data_all
    data_eff_up = ROOT.TEfficiency.ClopperPearson(int(data_all),
                                                  int(data_passed), cl, True)
    data_eff_down = ROOT.TEfficiency.ClopperPearson(int(data_all),
                                                    int(data_passed), cl,
                                                    False)
    data_eff_up = data_eff_up - data_eff
    data_eff_down = data_eff - data_eff_down
    data_eff_err = max(data_eff_up, data_eff_down)

    rho = data_eff / cmc_eff
    rho_err = math.sqrt((data_eff_err / cmc_eff)**2 + (data_eff * cmc_eff_err /
                                                       (cmc_eff**2))**2)

    print "Cut value %f, bin %d:" % (cutvalue, bin)
    #    print "  MC %f/%f = %f + %f - %f" % (mc_all, mc_passed, mc_eff, mc_eff_up, mc_eff_down)
    print "  Comb. MC %f + %f - %f" % (cmc_eff, cmc_eff_up, cmc_eff_down)
    print "  data %f/%f = %f + %f - %f" % (data_all, data_passed, data_eff,
                                           data_eff_up, data_eff_down)
    print "  scale factor = %f \\pm %f" % (rho, rho_err)

    ROOT.gErrorIgnoreLevel = backup
def plotTurnOn(datasets, pathAll, pathPassed, commonText, dataText=None, mcText=None, rebin=1, ratio=False):
    dataLabel = "Data"
    mcLabel = "Simulation"
    if dataText != None and mcText == None:
        raise Exception("mcText must not be None when dataText is not")
    if dataText == None and mcText != None:
        raise Exception("dataText must not be None when mcText is not")
#    if dataText != None:
#        dataLabel += ": "+dataText
#        mcLabel += ": "+mcText

    mc_effs = []
    data_eff_gr = None
    binWidth = None
    luminosity = 0
    for dataset in datasets.getAllDatasets():
        all = dataset.getDatasetRootHisto(pathAll).getHistogram()
        passed = dataset.getDatasetRootHisto(pathPassed).getHistogram()

        if rebin > 1:
            all.Rebin(rebin)
            passed.Rebin(rebin)
        binWidth = all.GetBinWidth(1)

        if dataset.isMC() and not "TTTo" in dataset.getName():
        #if dataset.isMC() and not "TTTo" in dataset.getName() and not "QCD" in dataset.getName():
        #if dataset.isMC() and "QCD" in dataset.getName():
            mc_effs.append(HistoEff(all, passed, dataset))

        elif dataset.isData():
            data_eff_gr = ROOT.TGraphAsymmErrors(passed, all, "cp") # 0.683 cl is default
            luminosity += dataset.getLuminosity()

    mc_eff_gr = combineHistoEffs(mc_effs)

    p = plots.ComparisonPlot(
        histograms.HistoGraph(data_eff_gr, "Data", "p", "P"),
        histograms.HistoGraph(mc_eff_gr, "Simulation", "p", "P")
        )
    p.histoMgr.forEachHisto(styles.generator2(styles.StyleMarker(markerSizes=[1.0, 1.5]), plotStyles))
    p.histoMgr.setHistoLegendLabelMany({
            "Data": dataLabel,
            "Simulation": mcLabel,
            })
    p.setLuminosity(luminosity)
    #p.addGraph(data_eff_gr, "Data")
    #p.addGraph(mc_eff_gr, "Simulation")
    #p.finalize()

    opts = {"ymin": 0.0, "ymax": 1.1}
    #opts2 = {"ymin": 0.5, "ymax": 1.5}
    opts2 = {"ymin": 0.7, "ymax": 1.3}

    name = "calomet_turnon_"+runs
    if not mcDataDefinition:
        name += "_McSummer11"
    if caloMetNoHF:
        name += "_caloMetNoHF"
    
    p.createFrame(name, createRatio=ratio, opts=opts, opts2=opts2)
    if ratio:
        p.getFrame2().GetYaxis().SetTitle("Data / MC")
        p.cf.canvas.cd(2)
        p.line1 = plots._createRatioLine(p.cf.frame.getXmin(), p.cf.frame.getXmax(), 0.9)
        p.line2 = plots._createRatioLine(p.cf.frame.getXmin(), p.cf.frame.getXmax(), 1.1)
        p.line1.SetLineColor(ROOT.kBlue)
        p.line2.SetLineColor(ROOT.kBlue)
        p.line1.Draw("L")
        p.line2.Draw("L")
        p.cf.canvas.cd(1)

    p.setLegend(histograms.moveLegend(histograms.createLegend(y1=0.95, y2=0.85),
                                      #dx=-0.55, dy=-0.05
                                      #dx=-0.44, dy=-0.58
                                      dx=-0.3, dy=-0.4
                                      ))

    x = 0.25
    y = 0.3
    dy = 0.035
    mcColor = mc_eff_gr.GetMarkerColor()
    size = 17
    p.appendPlotObject(histograms.PlotText(x, y, "Data (runs %s):"%runs, size=size)); y -= dy
    p.appendPlotObject(histograms.PlotText(x, y, dataText, size=size)); y -= dy

    y -= 0.01
    p.appendPlotObject(histograms.PlotText(x, y, "Simulation:", size=size, color=mcColor)); y -= dy
    p.appendPlotObject(histograms.PlotText(x, y, mcText, size=size, color=mcColor)); y -= dy
                       


    def text():
        l = ROOT.TLatex()
        l.SetNDC()
#        l.SetTextFont(l.GetTextFont()-20) # bold -> normal
        l.SetTextSize(l.GetTextSize()*0.65)
        #l.DrawLatex(0.35, 0.4, commonText)
        l.DrawLatex(0.48, 0.32, commonText)
    textFunction = text
    if dataText != None:
        textFunction = None
    #common(p, "PF E_{T}^{miss} (GeV)", "Efficiency / %.0f GeV"%binWidth, afterDraw=textFunction)
    common(p, "Uncorrected PF E_{T}^{miss} (GeV)", "Efficiency", afterDraw=textFunction)
def printEfficiency(datasets, bin, function):
    backup = ROOT.gErrorIgnoreLevel
    ROOT.gErrorIgnoreLevel = ROOT.kError

    mc_all = 0
    mc_passed = 0
    data_all = 0
    data_passed = 0
#    mc_eff = ROOT.TEfficiency("mc", "mc", 1, 0, 1)
#    mc_eff.SetStatisticOption(ROOT.TEfficiency.kFCP)
    mc_effs = []
    for dataset in datasets.getAllDatasets():
#        print histo.getRootHisto().GetNbinsX(), histo.getXmin(), histo.getXmax()
#        hpass = histograms.dist2pass(histo.getRootHisto(), greaterThan=True)

#        hpass = histograms.dist2pass(dataset.getDatasetRootHisto(path).getHistogram(), greaterThan=True)
#        all = hpass.GetBinContent(1)
#        passed = hpass.GetBinContent(bin)
#        cutvalue = hpass.GetBinCenter(bin)
        (all, passed, cutvalue) = function(dataset, bin)
        eff = Eff(all, passed, dataset)
        print "Dataset %-35s, eff %f + %f - %f" % (dataset.getName(), eff.eff, eff.eff_up, eff.eff_down)
#        print all, passed, cutvalue

#        e = ROOT.TEfficiency("mc"+dataset.getName(), "mc_"+dataset.getName(), 1, 0, 1)
#        e.SetStatisticOption(ROOT.TEfficiency.kFCP)

        if dataset.isMC() and not "TTTo" in dataset.getName():
            mc_all += all
            mc_passed += passed
            mc_effs.append(eff)
        elif dataset.isData():
            data_all += all
            data_passed += passed

    #cl = 0.95
    cl = 0.683

    mc_eff = mc_passed/mc_all
    mc_eff_up = ROOT.TEfficiency.ClopperPearson(int(mc_all), int(mc_passed), cl, True)
    mc_eff_down = ROOT.TEfficiency.ClopperPearson(int(mc_all), int(mc_passed), cl, False)
    mc_eff_up = mc_eff_up-mc_eff
    mc_eff_down = mc_eff-mc_eff_down

    (cmc_eff, cmc_eff_up, cmc_eff_down) = combineEffs(mc_effs)
    cmc_eff_err = max(cmc_eff_up, cmc_eff_down)
    
    data_eff = data_passed/data_all
    data_eff_up = ROOT.TEfficiency.ClopperPearson(int(data_all), int(data_passed), cl, True)
    data_eff_down = ROOT.TEfficiency.ClopperPearson(int(data_all), int(data_passed), cl, False)
    data_eff_up = data_eff_up-data_eff
    data_eff_down = data_eff-data_eff_down
    data_eff_err = max(data_eff_up, data_eff_down)

    rho = data_eff / cmc_eff
    rho_err = math.sqrt( (data_eff_err/cmc_eff)**2 + (data_eff*cmc_eff_err/(cmc_eff**2))**2)

    print "Cut value %f, bin %d:" % (cutvalue, bin)
#    print "  MC %f/%f = %f + %f - %f" % (mc_all, mc_passed, mc_eff, mc_eff_up, mc_eff_down)
    print "  Comb. MC %f + %f - %f" % (cmc_eff, cmc_eff_up, cmc_eff_down)
    print "  data %f/%f = %f + %f - %f" % (data_all, data_passed, data_eff, data_eff_up, data_eff_down)
    print "  scale factor = %f \\pm %f" % (rho, rho_err)

    ROOT.gErrorIgnoreLevel = backup