Пример #1
0
def doCounters(datasets, MyCuts, MyCutsName):
    
    ### Define the counters to be used
    #eventCounter = counter.EventCounter(datasets, counters=myAnalysis + myDataEra + "/counters")
    eventCounter = counter.EventCounter(datasets)

    ### Normalise the MC sample to a luminosity before creating a table
    eventCounter.normalizeMCToLuminosity(GetLumi(datasets))
    

    myRowNames = []
    for iCut, iCutName in zip(MyCuts, MyCutsName):
        #print "*** Cut: %s\n*** CutName: %s" % (iCut, iCutName)
        print "*** Processing TCut with:\n    Name = \"%s\" \n    Expr = \"%s\"" % (iCutName, iCut)

        ### Define the event "weight" to be used
        EvtWeight = GetEventWeight(iCut)
    
        ### Define the TTree to be used
        treePath = "tree" # treePath = myAnalysis+"/tree"
        treeDraw = dataset.TreeDraw(treePath, weight=EvtWeight, selection=iCut)

        ### Append custom rows to the event counter. An asterisk denotes that the counter row was added. Informative and makes things easier
        myRowName = iCutName #"*" + iCutName
        myRowNames.append(myRowName)
        eventCounter.getMainCounter().appendRow(myRowName, treeDraw)

    ### Get table with all default rows removed and manage the table format
    myTable = GetCustomTable(eventCounter, myRowNames)

    if getBool("bMergeEwk"):    
        DataMinusEwkMc = counter.subtractColumn("DataMinusEwkMc", myTable.getColumn(name="Data"), myTable.getColumn(name="EWK MC"))
        QcdPurity      = counter.divideColumn("QCD Purity", DataMinusEwkMc, myTable.getColumn(name="Data"))
        myTable.appendColumn(QcdPurity)

    ### See http://docs.python.org/2/library/string.html for string format
    cellTextFormat  = counter.TableFormatText(counter.CellFormatTeX(valueFormat='%.2E', valueOnly=True)) #%.2e
    cellLaTeXFormat = counter.TableFormatLaTeX(counter.CellFormatTeX(valueFormat='%.2E', valueOnly=True)) #%.2e
    purityFormat    = counter.CellFormatTeX(valueFormat='%.2f', valueOnly=True)

    ### Customise the "QCD Purity" column
    cellTextFormat.setColumnFormat(purityFormat, name="QCD Purity") #does nothing
    cellLaTeXFormat.setColumnFormat(purityFormat, name="QCD Purity") #does nothing

    # between construction of table format and table format

    ### Print the final table with the desired format
    print "============================================================"
    print "Data-Era: %s (%s pb-1)" % (myDataEra, GetLumi(datasets))
    print "============================================================"
    print myTable.format(cellTextFormat)

    print "============================================================"
    print "Data-Era: %s (%s pb-1)" % (myDataEra, GetLumi(datasets))
    print "============================================================"
    print myTable.format(cellLaTeXFormat)

    return
def addPurityColumn(table):
    mcSumColumn = table.indexColumn("MCsum")
    purity = counter.divideColumn("TT+W purity", ttwSum(table), table.getColumn(mcSumColumn))
    purity.multiply(100) # -> %
    table.appendColumn(purity)
def addTtwFractionColumn(table):
    ttColumn = table.indexColumn("TTJets")
    fraction = counter.divideColumn("TTJets/(TTJets+WJets)", table.getColumn(ttColumn), ttwSum(table))
    fraction.multiply(100) # -> %
    table.appendColumn(fraction)
Пример #4
0
def addPurityColumn(table):
    mcSumColumn = table.indexColumn("MCsum")
    purity = counter.divideColumn("TT+W purity", ttwSum(table),
                                  table.getColumn(mcSumColumn))
    purity.multiply(100)  # -> %
    table.appendColumn(purity)
Пример #5
0
def addTtwFractionColumn(table):
    ttColumn = table.indexColumn("TTJets")
    fraction = counter.divideColumn("TTJets/(TTJets+WJets)",
                                    table.getColumn(ttColumn), ttwSum(table))
    fraction.multiply(100)  # -> %
    table.appendColumn(fraction)
Пример #6
0
def doCounters(datasets, MyCuts, MyCutsName):

    ### Define the counters to be used
    #eventCounter = counter.EventCounter(datasets, counters=myAnalysis + myDataEra + "/counters")
    eventCounter = counter.EventCounter(datasets)

    ### Normalise the MC sample to a luminosity before creating a table
    eventCounter.normalizeMCToLuminosity(GetLumi(datasets))

    myRowNames = []
    for iCut, iCutName in zip(MyCuts, MyCutsName):
        #print "*** Cut: %s\n*** CutName: %s" % (iCut, iCutName)
        print "*** Processing TCut with:\n    Name = \"%s\" \n    Expr = \"%s\"" % (
            iCutName, iCut)

        ### Define the event "weight" to be used
        EvtWeight = GetEventWeight(iCut)

        ### Define the TTree to be used
        treePath = "tree"  # treePath = myAnalysis+"/tree"
        treeDraw = dataset.TreeDraw(treePath, weight=EvtWeight, selection=iCut)

        ### Append custom rows to the event counter. An asterisk denotes that the counter row was added. Informative and makes things easier
        myRowName = iCutName  #"*" + iCutName
        myRowNames.append(myRowName)
        eventCounter.getMainCounter().appendRow(myRowName, treeDraw)

    ### Get table with all default rows removed and manage the table format
    myTable = GetCustomTable(eventCounter, myRowNames)

    if getBool("bMergeEwk"):
        DataMinusEwkMc = counter.subtractColumn(
            "DataMinusEwkMc", myTable.getColumn(name="Data"),
            myTable.getColumn(name="EWK MC"))
        QcdPurity = counter.divideColumn("QCD Purity", DataMinusEwkMc,
                                         myTable.getColumn(name="Data"))
        myTable.appendColumn(QcdPurity)

    ### See http://docs.python.org/2/library/string.html for string format
    cellTextFormat = counter.TableFormatText(
        counter.CellFormatTeX(valueFormat='%.2E', valueOnly=True))  #%.2e
    cellLaTeXFormat = counter.TableFormatLaTeX(
        counter.CellFormatTeX(valueFormat='%.2E', valueOnly=True))  #%.2e
    purityFormat = counter.CellFormatTeX(valueFormat='%.2f', valueOnly=True)

    ### Customise the "QCD Purity" column
    cellTextFormat.setColumnFormat(purityFormat,
                                   name="QCD Purity")  #does nothing
    cellLaTeXFormat.setColumnFormat(purityFormat,
                                    name="QCD Purity")  #does nothing

    # between construction of table format and table format

    ### Print the final table with the desired format
    print "============================================================"
    print "Data-Era: %s (%s pb-1)" % (myDataEra, GetLumi(datasets))
    print "============================================================"
    print myTable.format(cellTextFormat)

    print "============================================================"
    print "Data-Era: %s (%s pb-1)" % (myDataEra, GetLumi(datasets))
    print "============================================================"
    print myTable.format(cellLaTeXFormat)

    return
Пример #7
0
def addDataMcRatioColumn(table):
    dataColumn = table.indexColumn("Data")
    mcSumColumn = table.indexColumn("MCsum")
    ratio = counter.divideColumn("Data/MCsum", table.getColumn(dataColumn), table.getColumn(mcSumColumn))
    table.appendColumn(ratio)
Пример #8
0
def addDyFractionColumn(table):
    dyColumn = table.indexColumn("DYJetsToLL")
    mcSumColumn = table.indexColumn("MCsum")
    fraction = counter.divideColumn("DY/MCsum", table.getColumn(dyColumn), table.getColumn(mcSumColumn))
    fraction.multiply(100) # -> %
    table.appendColumn(fraction)
Пример #9
0
def addQcdFractionColumn(table):
    qcdColumn = table.indexColumn("QCD_Pt20_MuEnriched")
    mcSumColumn = table.indexColumn("MCsum")
    fraction = counter.divideColumn("QCD/MCsum", table.getColumn(qcdColumn), table.getColumn(mcSumColumn))
    fraction.multiply(100) # -> %
    table.appendColumn(fraction)
Пример #10
0
def main():
    datasets = dataset.getDatasetsFromMulticrabCfg(counters=baseline+"Counters")

    datasets.remove([
#            "SingleMu_Mu_160431-163261_May10",
#            "SingleMu_Mu_163270-163869_May10",
#            "SingleMu_Mu_165088-166150_Prompt",
#            "SingleMu_Mu_166161-166164_Prompt",
#            "SingleMu_Mu_166346-166346_Prompt",
#            "SingleMu_Mu_166374-167043_Prompt",
#            "SingleMu_Mu_167078-167913_Prompt",

#            "SingleMu_Mu_170722-172619_Aug05",
#            "SingleMu_Mu_172620-173198_Prompt",
#            "SingleMu_Mu_173236-173692_Prompt",
        ])
    datasets.loadLuminosities()
    plots.mergeRenameReorderForDataMC(datasets)

    values = {}

    analyses = [
        ("Baseline", baseline),
        ("Plus", plus),
        ("Minus", minus)
        ]

    mainTable = counter.CounterTable()
    tauTable = counter.CounterTable()
    cntr = "Counters"
    if normalize:
        cntr += "/weighted"
    for name, analysis in analyses:
        eventCounter = counter.EventCounter(datasets, counters=analysis+cntr)

        eventCounter.normalizeMCByLuminosity()
        if normalize:
            tauEmbedding.scaleNormalization(eventCounter)
        col = eventCounter.getMainCounterTable().getColumn(name="Data")
        col.setName(name)
        mainTable.appendColumn(col)
        effcol = counter.efficiencyColumn(col.getName()+" eff", col)
        mainTable.appendColumn(effcol)
        values[name+"Main"] = effcol

        value = col.getCount(col.getRowNames().index(count)).value()
        values[name] = value

        col = eventCounter.getSubCounterTable("TauIDPassedEvt::tauID_HPSTight").getColumn(name="Data")
        col.setName(name)
        tauTable.appendColumn(col)
        effcol = counter.efficiencyColumn(col.getName()+" eff", col)
        tauTable.appendColumn(effcol)
        values[name+"Tau"] = effcol
    
    mainTable.appendColumn(counter.divideColumn("Plus eff/Baseline", values["PlusMain"], values["BaselineMain"]))
    mainTable.appendColumn(counter.divideColumn("Minus eff/Baseline", values["MinusMain"], values["BaselineMain"]))
    tauTable.appendColumn(counter.divideColumn("Plus eff/Baseline", values["PlusTau"], values["BaselineTau"]))
    tauTable.appendColumn(counter.divideColumn("Minus eff/Baseline", values["MinusTau"], values["BaselineTau"]))

    print mainTable.format()
    print tauTable.format()

    plusDiff = abs(values["Baseline"] - values["Plus"])
    minusDiff = abs(values["Baseline"] - values["Minus"])
    maxDiff = max(plusDiff, minusDiff)
    rel = maxDiff / values["Baseline"]

    print "Count %s, baseline %.3f, plus %.3f, minus %.3f" % (count, values["Baseline"], values["Plus"], values["Minus"])
    print "Plus diff %.3f, minus diff %.3f" % (plusDiff, minusDiff)
    print "Relative uncertainty from tau energy scale %.6f" % (rel)


    style = tdrstyle.TDRStyle()
    histograms.createLegend.moveDefaults(dx=-0.32, dh=-0.15)

    metCut = "(met_p4.Et() > 50)"
    bTaggingCut = "passedBTagging"
    tdMt = dataset.TreeDraw("dummy",
                            weight="weightPileup*weightTrigger*weightBTagging",
                            selection=metCut+"&&"+bTaggingCut,
                            varexp="sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))) >>tmp(40,0,400)"
                            )

    output = ROOT.TFile.Open("mt_variated_ewk.root", "RECREATE")

    #doPlot(datasets, analyses, tdMt, "mt_variated")
    doPlot(datasets, analyses, "transverseMass", "mt_variated_btagging", output, "Dphi180")
    doPlot(datasets, analyses, "transverseMassAfterDeltaPhi160", "mt_variated_deltaPhi160", output, "Dphi160")
    doPlot(datasets, analyses, "transverseMassAfterDeltaPhi130", "mt_variated_deltaPhi130", output, "Dphi130")
    doPlot(datasets, analyses, "transverseMassAfterDeltaPhi90", "mt_variated_deltaPhi90", output, "Dphi90")

    output.Close()
Пример #11
0
def doCounters(datasets, mcLumi=None):
    createPlot = lambda name: createPlotCommon(name, datasets, mcLumi)
    eventCounter = counter.EventCounter(datasets, counters=countersWeighted)

    sels = [
        #        "(sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))) < 20)",
        #        "(20 < sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))))", "(sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))) < 80)",
        #        "(80 < sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))))", "(sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))) < 120)",
        #        "(120 < sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))))",
    ]
    tdCount = treeDraw.clone(weight=weightBTagging)
    tdCountMET = tdCount.clone(weight=weight,
                               selection="&&".join(sels + [metCut]))
    tdCountBTagging = tdCount.clone(selection="&&".join(sels +
                                                        [metCut, bTaggingCut]))
    tdCountDeltaPhi160 = tdCount.clone(
        selection="&&".join(sels + [metCut, bTaggingCut, deltaPhi160Cut]))
    tdCountDeltaPhi130 = tdCount.clone(
        selection="&&".join(sels + [metCut, bTaggingCut, deltaPhi130Cut]))
    tdCountDeltaPhi90 = tdCount.clone(
        selection="&&".join(sels + [metCut, bTaggingCut, deltaPhi90Cut]))
    #    eventCounter.getMainCounter().appendRow("JetsForEffs", tdCount.clone(weight=weight, selection="&&".join(sels)))
    #    eventCounter.getMainCounter().appendRow("METForEffs", tdCountMET)
    #    eventCounter.getMainCounter().appendRow("BTagging", tdCountBTagging)
    #    eventCounter.getMainCounter().appendRow("DeltaPhi < 160", tdCountDeltaPhi160)
    #    eventCounter.getMainCounter().appendRow("DeltaPhi < 130", tdCountDeltaPhi130)
    #    eventCounter.getMainCounter().appendRow("DeltaPhi < 90", tdCountDeltaPhi90)

    td1 = tdCount.clone(selection=metCut + "&&" + bTaggingCut +
                        "&& (tecalometNoHF_p4.Pt() > 60)")
    td2 = tdCount.clone(selection=metCut + "&&" + bTaggingCut +
                        "&& (tecalomet_p4.Pt() > 60)")
    td3 = dataset.TreeDrawCompound(
        td1, {
            "SingleMu_Mu_170722-172619_Aug05": td2,
            "SingleMu_Mu_172620-173198_Prompt": td2,
            "SingleMu_Mu_173236-173692_Prompt": td2,
        })
    #    eventCounter.getMainCounter().appendRow("BTagging+CaloMetNoHF", td1)
    #    eventCounter.getMainCounter().appendRow("BTagging+CaloMet", td2)
    #    eventCounter.getMainCounter().appendRow("BTagging+CaloMet(NoHF)", td3)

    if mcLumi != None:
        eventCounter.normalizeMCToLuminosity(mcLumi)
    else:
        eventCounter.normalizeMCByLuminosity()
    tauEmbedding.scaleNormalization(eventCounter)

    ewkDatasets = ["WJets", "TTJets", "DYJetsToLL", "SingleTop", "Diboson"]

    table = eventCounter.getMainCounterTable()
    mainTable = table
    #muonAnalysis.addSumColumn(table)
    #mainTable.insertColumn(2, counter.sumColumn("EWKMCsum", [mainTable.getColumn(name=name) for name in ewkDatasets]))
    #muonAnalysis.addDataMcRatioColumn(table)
    if datasets.hasDataset("EWKSignal"):
        mainTable.insertColumn(
            7,
            counter.divideColumn(
                "SignalFraction",
                mainTable.getColumn(name="TTToHplus_" + keepSignal),
                mainTable.getColumn(name="EWKSignal")))

    datasets.printInfo()
    print "============================================================"
    print "Main counter (%s)" % eventCounter.getNormalizationString()
    cellFormat = counter.TableFormatText(
        counter.CellFormatTeX(valueFormat='%.3f'))
    print table.format(cellFormat)

    tauTable = eventCounter.getSubCounterTable(
        "TauIDPassedEvt::TauSelection_HPS")
    #muonAnalysis.addSumColumn(tauTable)
    tauTable.insertColumn(
        2,
        counter.sumColumn(
            "EWKMCsum",
            [tauTable.getColumn(name=name) for name in ewkDatasets]))
    print tauTable.format(cellFormat)

    #    print eventCounter.getSubCounterTable("TauIDPassedJets::tauID_HPSTight").format()
    #    table = eventCounter.getSubCounterTable("Trigger")
    #    muonAnalysis.addSumColumn(table)
    #    print table.format(cellFormat)

    mainTable.keepOnlyRows([
        "All events",
        "Trigger and HLT_MET cut",
        "taus == 1",
        #            "trigger scale factor",
        "electron veto",
        "muon veto",
        "MET",
        "njets",
        "btagging",
        "btagging scale factor",
        "JetsForEffs",
        "METForEffs",
        "BTagging",
        "DeltaPhi < 160",
        "DeltaPhi < 130"
    ])
    tauTable.keepOnlyRows([
        "AllTauCandidates",
        "DecayModeFinding",
        "TauJetPt",
        "TauJetEta",
        "TauLdgTrackExists",
        "TauLdgTrackPtCut",
        "TauECALFiducialCutsCracksAndGap",
        "TauAgainstElectronCut",
        "TauAgainstMuonCut",
        #"EMFractionCut",
        "HPS",
        "TauOneProngCut",
        "TauRtauCut",
    ])

    #effFormat = counter.TableFormatText(counter.CellFormatText(valueFormat='%.4f'))
    effFormat = counter.TableFormatText(
        counter.CellFormatTeX(valueFormat='%.4f'))
    #effFormat = counter.TableFormatConTeXtTABLE(counter.CellFormatTeX(valueFormat='%.4f'))
    for name, table in [("Main", mainTable), ("Tau ID", tauTable)]:
        effTable = counter.CounterTable()
        col = table.getColumn(name="Data")
        effTable.appendColumn(col)
        effTable.appendColumn(
            counter.efficiencyColumn(col.getName() + " eff", col))
        col = table.getColumn(name="EWKMCsum")
        effTable.appendColumn(col)
        effTable.appendColumn(
            counter.efficiencyColumn(col.getName() + " eff", col))
        print "%s counter efficiencies" % name
        print effTable.format(effFormat)

    print "Trigger uncertainties"
    bins = [40, 50, 60, 80]
    tauPtPrototype = ROOT.TH1F("tauPtTrigger", "Tau pt",
                               len(bins) - 1, array.array("d", bins))
    runs = [
        "(160431 <= run && run <= 167913)",
        "(170722 <= run && run <= 173198)",
        "(173236 <= run && run <= 173692)",
        #"(160431 <= run && run <= 173692)",
    ]
    for name, td in [("BTagging", tdCountBTagging),
                     ("DeltaPhi160", tdCountDeltaPhi160),
                     ("DeltaPhi130", tdCountDeltaPhi130),
                     ("DeltaPhi90", tdCountDeltaPhi90)]:
        t = td.clone(varexp="tau_p4.Pt() >>tauPtTrigger")

        NallSum = 0
        NSum = 0
        absUncSquareSum = 0

        for runRegion in runs:
            #neventsPlot = createPlot(dataset.treeDrawToNumEntries(t.clone(weight="weightTrigger")))
            #uncertaintyPlot = createPlot(dataset.treeDrawToNumEntries(t.clone(weight="weightTriggerAbsUnc*weightTriggerAbsUnc/(weightTrigger*weightTrigger)")))
            tmp = t.clone(selection=t.selection + "&&" + runRegion)
            nallPlot = createPlot(tmp.clone(weight=""))
            neventsPlot = createPlot(tmp.clone(weight="weightTrigger"))
            uncertaintyPlot = createPlot(
                tmp.clone(weight="weightTriggerAbsUnc"))
            th1all = nallPlot.histoMgr.getHisto("Data").getRootHisto()
            th1 = neventsPlot.histoMgr.getHisto("Data").getRootHisto()
            th12 = uncertaintyPlot.histoMgr.getHisto("Data").getRootHisto()

            Nall = th1all.Integral(0, th1all.GetNbinsX() + 1)
            N = th1.Integral(0, th1.GetNbinsX() + 1)
            #absSum2 = th12.Integral(0, th12.GetNbinsX()+1)
            #absUnc = math.sqrt(absSum2)
            #absUnc = th12.Integral(0, 2)
            NallSum += Nall
            NSum += N
            absUnc = tauEmbedding.squareSum(th12)
            absUncSquareSum += absUnc
            absUnc = math.sqrt(absUnc)
            relUnc = 0
            if N > 0:
                relUnc = absUnc / N

            print "%-15s for runs %s Nall = %.2f, N = %.2f, absolute uncertainty %.2f, relative uncertainty %.4f" % (
                name, runRegion, Nall, N, absUnc, relUnc)

        absUnc = math.sqrt(absUncSquareSum)
        relUnc = absUnc / NSum

        print "%-15s Nall = %.2f, N = %.2f, absolute uncertainty %.2f, relative uncertainty %.4f" % (
            name, NallSum, NSum, absUnc, relUnc)
        print
Пример #12
0
def main():
    datasets = dataset.getDatasetsFromMulticrabCfg(counters=baseline +
                                                   "Counters",
                                                   weightedCounters=False)

    datasets.remove([
        #            "SingleMu_Mu_160431-163261_May10",
        #            "SingleMu_Mu_163270-163869_May10",
        #            "SingleMu_Mu_165088-166150_Prompt",
        #            "SingleMu_Mu_166161-166164_Prompt",
        #            "SingleMu_Mu_166346-166346_Prompt",
        #            "SingleMu_Mu_166374-167043_Prompt",
        #            "SingleMu_Mu_167078-167913_Prompt",

        #            "SingleMu_Mu_170722-172619_Aug05",
        #            "SingleMu_Mu_172620-173198_Prompt",
        #            "SingleMu_Mu_173236-173692_Prompt",
    ])
    datasets.loadLuminosities()
    plots.mergeRenameReorderForDataMC(datasets)

    values = {}

    analyses = [("Baseline", baseline), ("Plus", plus), ("Minus", minus)]

    mainTable = counter.CounterTable()
    tauTable = counter.CounterTable()
    cntr = "Counters"
    if normalize:
        cntr += "/weighted"
    for name, analysis in analyses:
        eventCounter = counter.EventCounter(datasets, counters=analysis + cntr)

        eventCounter.normalizeMCByLuminosity()
        if normalize:
            tauEmbedding.scaleNormalization(eventCounter)
        col = eventCounter.getMainCounterTable().getColumn(name="Data")
        col.setName(name)
        mainTable.appendColumn(col)
        effcol = counter.efficiencyColumn(col.getName() + " eff", col)
        mainTable.appendColumn(effcol)
        values[name + "Main"] = effcol

        value = col.getCount(col.getRowNames().index(count)).value()
        values[name] = value

        col = eventCounter.getSubCounterTable(
            "TauIDPassedEvt::tauID_HPSTight").getColumn(name="Data")
        col.setName(name)
        tauTable.appendColumn(col)
        effcol = counter.efficiencyColumn(col.getName() + " eff", col)
        tauTable.appendColumn(effcol)
        values[name + "Tau"] = effcol

    mainTable.appendColumn(
        counter.divideColumn("Plus eff/Baseline", values["PlusMain"],
                             values["BaselineMain"]))
    mainTable.appendColumn(
        counter.divideColumn("Minus eff/Baseline", values["MinusMain"],
                             values["BaselineMain"]))
    tauTable.appendColumn(
        counter.divideColumn("Plus eff/Baseline", values["PlusTau"],
                             values["BaselineTau"]))
    tauTable.appendColumn(
        counter.divideColumn("Minus eff/Baseline", values["MinusTau"],
                             values["BaselineTau"]))

    print mainTable.format()
    print tauTable.format()

    plusDiff = abs(values["Baseline"] - values["Plus"])
    minusDiff = abs(values["Baseline"] - values["Minus"])
    maxDiff = max(plusDiff, minusDiff)
    rel = maxDiff / values["Baseline"]

    print "Count %s, baseline %.3f, plus %.3f, minus %.3f" % (
        count, values["Baseline"], values["Plus"], values["Minus"])
    print "Plus diff %.3f, minus diff %.3f" % (plusDiff, minusDiff)
    print "Relative uncertainty from tau energy scale %.6f" % (rel)

    style = tdrstyle.TDRStyle()
    histograms.createLegend.moveDefaults(dx=-0.32, dh=-0.15)

    metCut = "(met_p4.Et() > 50)"
    bTaggingCut = "passedBTagging"
    tdMt = dataset.TreeDraw(
        "dummy",
        weight="weightPileup*weightTrigger*weightBTagging",
        selection=metCut + "&&" + bTaggingCut,
        varexp=
        "sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))) >>tmp(40,0,400)"
    )

    output = ROOT.TFile.Open("mt_variated_ewk.root", "RECREATE")

    #doPlot(datasets, analyses, tdMt, "mt_variated")
    doPlot(datasets, analyses, "transverseMass", "mt_variated_btagging",
           output, "Dphi180")
    doPlot(datasets, analyses, "transverseMassAfterDeltaPhi160",
           "mt_variated_deltaPhi160", output, "Dphi160")
    doPlot(datasets, analyses, "transverseMassAfterDeltaPhi130",
           "mt_variated_deltaPhi130", output, "Dphi130")
    doPlot(datasets, analyses, "transverseMassAfterDeltaPhi90",
           "mt_variated_deltaPhi90", output, "Dphi90")

    output.Close()
def doCounters(datasets, mcLumi=None):
    createPlot = lambda name: createPlotCommon(name, datasets, mcLumi)
    eventCounter = counter.EventCounter(datasets, counters=countersWeighted)
   

    sels = [
#        "(sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))) < 20)",
#        "(20 < sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))))", "(sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))) < 80)",
#        "(80 < sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))))", "(sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))) < 120)",
#        "(120 < sqrt(2 * tau_p4.Pt() * met_p4.Et() * (1-cos(tau_p4.Phi()-met_p4.Phi()))))",
        ]
    tdCount = treeDraw.clone(weight=weightBTagging)
    tdCountMET = tdCount.clone(weight=weight, selection="&&".join(sels+[metCut]))
    tdCountBTagging = tdCount.clone(selection="&&".join(sels+[metCut, bTaggingCut]))
    tdCountDeltaPhi160 = tdCount.clone(selection="&&".join(sels+[metCut, bTaggingCut, deltaPhi160Cut]))
    tdCountDeltaPhi130 = tdCount.clone(selection="&&".join(sels+[metCut, bTaggingCut, deltaPhi130Cut]))
    tdCountDeltaPhi90 = tdCount.clone(selection="&&".join(sels+[metCut, bTaggingCut, deltaPhi90Cut]))
    eventCounter.getMainCounter().appendRow("JetsForEffs", tdCount.clone(weight=weight, selection="&&".join(sels)))
    eventCounter.getMainCounter().appendRow("METForEffs", tdCountMET)
    eventCounter.getMainCounter().appendRow("BTagging", tdCountBTagging)
    eventCounter.getMainCounter().appendRow("DeltaPhi < 160", tdCountDeltaPhi160)
    eventCounter.getMainCounter().appendRow("DeltaPhi < 130", tdCountDeltaPhi130)
    eventCounter.getMainCounter().appendRow("DeltaPhi < 90", tdCountDeltaPhi90)

    td1 = tdCount.clone(selection=metCut+"&&"+bTaggingCut+"&& (tecalometNoHF_p4.Pt() > 60)")
    td2 = tdCount.clone(selection=metCut+"&&"+bTaggingCut+"&& (tecalomet_p4.Pt() > 60)")
    td3 = dataset.TreeDrawCompound(td1, {
            "SingleMu_Mu_170722-172619_Aug05": td2,
            "SingleMu_Mu_172620-173198_Prompt": td2,
            "SingleMu_Mu_173236-173692_Prompt": td2,
            })
    eventCounter.getMainCounter().appendRow("BTagging+CaloMetNoHF", td1)
    eventCounter.getMainCounter().appendRow("BTagging+CaloMet", td2)
    eventCounter.getMainCounter().appendRow("BTagging+CaloMet(NoHF)", td3)

    if mcLumi != None:
        eventCounter.normalizeMCToLuminosity(mcLumi)
    else:
        eventCounter.normalizeMCByLuminosity()
    tauEmbedding.scaleNormalization(eventCounter)

    ewkDatasets = [
        "WJets", "TTJets",
        "DYJetsToLL", "SingleTop", "Diboson"
        ]

    table = eventCounter.getMainCounterTable()
    mainTable = table
    muonAnalysis.addSumColumn(table)
    mainTable.insertColumn(2, counter.sumColumn("EWKMCsum", [mainTable.getColumn(name=name) for name in ewkDatasets]))
#    table = eventCounter.getSubCounterTable("Trigger")
    #    muonAnalysis.reorderCounterTable(table)
    muonAnalysis.addDataMcRatioColumn(table)
    if datasets.hasDataset("EWKSignal"):
        mainTable.insertColumn(7, counter.divideColumn("SignalFraction", mainTable.getColumn(name="TTToHplus_"+keepSignal), mainTable.getColumn(name="EWKSignal")))

    datasets.printInfo()
    print "============================================================"
    print "Main counter (%s)" % eventCounter.getNormalizationString()
    cellFormat = counter.TableFormatText(counter.CellFormatTeX(valueFormat='%.3f'))
    print table.format(cellFormat)

    tauTable = eventCounter.getSubCounterTable("TauIDPassedEvt::tauID_HPSTight")
    #muonAnalysis.addSumColumn(tauTable)
    tauTable.insertColumn(2, counter.sumColumn("EWKMCsum", [tauTable.getColumn(name=name) for name in ewkDatasets]))
    print tauTable.format(cellFormat)

#    print eventCounter.getSubCounterTable("TauIDPassedJets::tauID_HPSTight").format()
#    table = eventCounter.getSubCounterTable("Trigger")
#    muonAnalysis.addSumColumn(table)
#    print table.format(cellFormat)

    mainTable.keepOnlyRows([
            "All events",
            "Trigger and HLT_MET cut",
            "taus == 1",
#            "trigger scale factor",
            "electron veto",
            "muon veto",
            "MET",
            "njets",
            "btagging",
            "btagging scale factor",
            "JetsForEffs",
            "METForEffs",
            "BTagging",
            "DeltaPhi < 160",
            "DeltaPhi < 130"
            ])
    tauTable.keepOnlyRows([
            "AllTauCandidates",
            "DecayModeFinding",
            "TauJetPt",
            "TauJetEta",
            "TauLdgTrackExists",
            "TauLdgTrackPtCut",
            "TauECALFiducialCutsCracksAndGap",
            "TauAgainstElectronCut",
            "TauAgainstMuonCut",
            #"EMFractionCut",
            "HPS",
            "TauOneProngCut",
            "TauRtauCut",
            ])

    #effFormat = counter.TableFormatText(counter.CellFormatText(valueFormat='%.4f'))
    effFormat = counter.TableFormatText(counter.CellFormatTeX(valueFormat='%.4f'))
    #effFormat = counter.TableFormatConTeXtTABLE(counter.CellFormatTeX(valueFormat='%.4f'))
    for name, table in [("Main", mainTable), ("Tau ID", tauTable)]:
        effTable = counter.CounterTable()
        col = table.getColumn(name="Data")
        effTable.appendColumn(col)
        effTable.appendColumn(counter.efficiencyColumn(col.getName()+" eff", col))
        col = table.getColumn(name="EWKMCsum")
        effTable.appendColumn(col)
        effTable.appendColumn(counter.efficiencyColumn(col.getName()+" eff", col))
        print "%s counter efficiencies" % name
        print effTable.format(effFormat)


    print "Trigger uncertainties"
    bins = [40, 50, 60, 80]
    tauPtPrototype = ROOT.TH1F("tauPtTrigger", "Tau pt", len(bins)-1, array.array("d", bins))
    runs = [
        "(160431 <= run && run <= 167913)",
        "(170722 <= run && run <= 173198)",
        "(173236 <= run && run <= 173692)",
        #"(160431 <= run && run <= 173692)",
        ]
    for name, td in [
        ("BTagging", tdCountBTagging),
        ("DeltaPhi160", tdCountDeltaPhi160),
        ("DeltaPhi130", tdCountDeltaPhi130),
        ("DeltaPhi90", tdCountDeltaPhi90)
        ]:
        t = td.clone(varexp="tau_p4.Pt() >>tauPtTrigger")
        
        NallSum = 0
        NSum = 0
        absUncSquareSum = 0

        for runRegion in runs:
            #neventsPlot = createPlot(dataset.treeDrawToNumEntries(t.clone(weight="weightTrigger")))
            #uncertaintyPlot = createPlot(dataset.treeDrawToNumEntries(t.clone(weight="weightTriggerAbsUnc*weightTriggerAbsUnc/(weightTrigger*weightTrigger)")))
            tmp = t.clone(selection=t.selection+"&&"+runRegion)
            nallPlot = createPlot(tmp.clone(weight=""))
            neventsPlot = createPlot(tmp.clone(weight="weightTrigger"))
            uncertaintyPlot = createPlot(tmp.clone(weight="weightTriggerAbsUnc"))
            th1all = nallPlot.histoMgr.getHisto("Data").getRootHisto()
            th1 = neventsPlot.histoMgr.getHisto("Data").getRootHisto()
            th12 = uncertaintyPlot.histoMgr.getHisto("Data").getRootHisto()

            Nall = th1all.Integral(0, th1all.GetNbinsX()+1)
            N = th1.Integral(0, th1.GetNbinsX()+1)
            #absSum2 = th12.Integral(0, th12.GetNbinsX()+1)
            #absUnc = math.sqrt(absSum2)
            #absUnc = th12.Integral(0, 2)
            NallSum += Nall
            NSum += N
            absUnc = squareSum(th12)
            absUncSquareSum += absUnc
            absUnc = math.sqrt(absUnc)
            relUnc = 0
            if N > 0:
                relUnc = absUnc/N

            print "%-15s for runs %s Nall = %.2f, N = %.2f, absolute uncertainty %.2f, relative uncertainty %.4f" % (name, runRegion, Nall, N, absUnc, relUnc)


        absUnc = math.sqrt(absUncSquareSum)
        relUnc = absUnc/NSum

        print "%-15s Nall = %.2f, N = %.2f, absolute uncertainty %.2f, relative uncertainty %.4f" % (name, NallSum, NSum, absUnc, relUnc)
        print