Example #1
0
def efficiencies(loglev, run, doMC, doData, doCSV, doDeepCSV, CSVWPs,
                 DeepCSVWPs, ptordered, csvordered, doinclusive,
                 nInclusiveIter, dosplit, nSplitIter, doCross, compWPs,
                 looseSel):
    import ROOT

    import modules.classes
    import modules.effPlots

    setup_logging(loglevel=loglev,
                  logname="efficiencyoutput",
                  errname="efficiencyerror")

    logger = logging.getLogger(__name__)

    logger.info("Starting shape comparison")
    t0 = time.time()

    if not (doMC or doData):
        if __name__ == "__main__":
            logging.warning(
                "At least on of the flags --mc and --data should to be set")
            logging.warning("Falling back the only mc")
        else:
            logging.warning(
                "At least on of the paramters doMC and doData should to be set"
            )
            logging.warning("Falling back the only mc")
        doMC = True

    if not (ptordered or csvordered):
        if __name__ == "__main__":
            logging.warning(
                "At least on of the flags --ptorderd and --csvordered should to be set"
            )
            logging.warning("Falling back to pt ordered")
        else:
            logging.warning(
                "At least on of the paramters ptordered and ptordered should to be set"
            )
            logging.warning("Falling back to pt ordered")
        ptordered = True

    if loglev > 0:
        ROOT.gErrorIgnoreLevel = ROOT.kBreak  # kPrint, kInfo, kWarning, kError, kBreak, kSysError, kFatal;

    if run == "CD":
        DataInput = "/mnt/t3nfs01/data01/shome/koschwei/scratch/HLTBTagging/DiLepton_v8/MuonEG/MuonEG_RunCD_phase1_0p9970p996_mod_mod_mod.root"
        basepaths = "v8nTuples/Efficiencies/RunCD/"
        fileprefix = "RunCD_"
    if run == "E":
        DataInput = "/mnt/t3nfs01/data01/shome/koschwei/scratch/HLTBTagging/DiLepton_v8/MuonEG/RunE/phase1/MuonEG_RunE_phase1_mod_mod_mod.root"
        basepaths = "v8nTuples/Efficiencies/RunE/"
        fileprefix = "RunE_EmilSel_"
    if run == "F":
        DataInput = "/mnt/t3nfs01/data01/shome/koschwei/scratch/HLTBTagging/DiLepton_v8/MuonEG/RunF/phase1/MuonEG_RunF_phase1_mod_mod_mod.root"
        basepaths = "v8nTuples/Efficiencies/RunF/"
        fileprefix = "RunF_"

    if run == "Test":
        DataInput = "/mnt/t3nfs01/data01/shome/koschwei/trigger/onlineBTV/CMSSW_9_2_12_patch1/src/HLTBTagging/nTuples/tree_phase1.root"
        basepaths = "testing/emilComp3K/"
        fileprefix = "Tesing_"

    #MCInput = "/mnt/t3nfs01/data01/shome/koschwei/scratch/HLTBTagging/DiLepton_v3/ttbar/ttbar_v3.root"
    MCInput = "/mnt/t3nfs01/data01/shome/koschwei/scratch/HLTBTagging/DiLepton_v8/ttbar/phase1/ttbar_0p85_mod_mod_mod.root"

    MCSelection = "1"
    VarSelection = "1"

    DeepCSVSelWP = "0.8958"
    if looseSel:
        TriggerSelection = "1"
        LeptonSelection = "1"
        offlineSelection = "1"
        CSVSelection = "1"

    else:
        TriggerSelection = "HLT_Mu12_TrkIsoVVL_Ele23_CaloIdL_TrackIdL_IsoVL_v4 > 0 || HLT_Mu23_TrkIsoVVL_Ele12_CaloIdL_TrackIdL_IsoVL_v4 > 0"
        LeptonSelection = "Sum$((abs(offTightElectrons_superClusterEta) <= 1.4442 || abs(offTightElectrons_superClusterEta) >= 1.5660) && offTightElectrons_pt > 30 && abs(offTightElectrons_eta) < 2.4) > 0 && Sum$(offTightMuons_iso < 0.25 && offTightMuons_pt > 20 && abs(offTightMuons_eta) < 2.4) > 0"
        offlineSelection = "Sum$(cleanJets_pt > 30 && abs(cleanJets_eta) < 2.4) > 2"
        CSVSelection = "Sum$(cleanJets_deepcsv > {0}) >= 1".format(
            DeepCSVSelWP)

    logging.info(
        "Using: doMC: {0} | doData: {1} | doCSV: {2} | doDeepCSV: {3}".format(
            doMC, doData, doCSV, doDeepCSV))

    samples = []
    if doMC:
        eventSelection = "({0}) && ({1}) && ({2}) && ({3})".format(
            VarSelection, TriggerSelection, LeptonSelection, MCSelection)
        samples.append(
            (modules.classes.Sample("ttbar",
                                    MCInput,
                                    eventSelection,
                                    831.76,
                                    4591.622871124,
                                    ROOT.kRed,
                                    9937324,
                                    legendText="Dataset: t#bar{t}"), "MC",
             getLabel("Dataset: t#bar{t}", 0.7)))
    if doData:
        eventSelection = "({0}) && ({1}) && ({2})".format(
            VarSelection, TriggerSelection, LeptonSelection)
        samples.append(
            (modules.classes.Sample("data",
                                    DataInput,
                                    eventSelection,
                                    legendText="Dataset: MuonEG"), "MuonEG",
             getLabel("Dataset: MuonEG", 0.7)))

    CSVSelWP = "0.9535"

    WPColors = [
        ROOT.kRed, ROOT.kBlue, ROOT.kGreen + 2, ROOT.kPink + 7, ROOT.kViolet,
        ROOT.kCyan
    ]

    #CSVSelection = "Sum$(cleanJets_csv > {0}) >= 1".format(CSVSelWP)

    refbyCSVRank = {
        0: "cleanJets_ileadingCSV",
        1: "cleanJets_isecondCSV",
        2: "cleanJets_ithirdCSV",
        3: "cleanJets_ifourthCSV"
    }

    refbyDeepCSVRank = {
        0: "cleanJets_ileadingDeepCSV",
        1: "cleanJets_isecondDeepCSV",
        2: "cleanJets_ithirdDeepCSV",
        3: "cleanJets_ifourthDeepCSV"
    }

    if doCSV:
        logging.info("Processing plots for CSV")
        CSVPlotBaseObjs = {}

        collections = []
        if ptordered:
            #collections.append( ("pT", "cleanJets") )
            collections.append(("pT", "offJets"))
            logging.debug("Adding {1} to collecton as ordered by {0}".format(
                "ptOrdered", "offJets"))
        if csvordered:
            collections.append(("CSV", "cleanCSVJets"))
            logging.debug("Adding {1} to collecton as ordered by {0}".format(
                "csvOrdered", "cleanCSVJets"))

        for WP in CSVWPs:
            WPLabel = getLabel("PF WP: {0}".format(WP), 0.7, "under")
            CaloWPLabel = getLabel("Calo WP: {0}".format(WP), 0.7, "under")
            logging.info("Using WP: {0}".format(WP))
            for collectionName, collection in collections:

                if dosplit:
                    for i in range(nSplitIter):
                        if looseSel:
                            JetSelection = "abs({1}_eta[{0}]) < 2.4 && {1}_pt[{0}] > 30".format(
                                i, collection)
                        else:
                            JetSelection = "abs({1}_eta[{0}]) < 2.4 && {1}_pt[{0}] > 30 && {1}_passesTightLeptVetoID[{0}] > 0".format(
                                i, collection)

                        logging.info(
                            "Making effciency for Jet {0} ordered by {1} w/ CSV as tagger"
                            .format(i, collectionName))
                        logging.subinfo(
                            "Using collection: {0}".format(collection))
                        CSVPlotBaseObjs[i] = modules.classes.PlotBase(
                            "{1}_csv[{0}]".format(i, collection),
                            "{0} && {1} && ({2})".format(
                                CSVSelection, offlineSelection, JetSelection),
                            "1", [20, 0, 1],
                            modules.utils.getAxisTitle("csv", i,
                                                       collectionName.lower()),
                            LegendPosition=[0.1, 0.6, 0.4, 0.76])

                        CSVPlotBaseObjs[i].color = WPColors[i]
                        onlysamples = []

                        for sampleStuff in samples:
                            sample, name, label = sampleStuff
                            onlysamples.append(sample)
                            if not (doMC and doData):
                                modules.effPlots.makeEffPlot(
                                    CSVPlotBaseObjs[i],
                                    sample,
                                    "pfJets_csv[{2}_matchPF[{1}]] >= {0}".
                                    format(WP, i, collection),
                                    basepaths +
                                    "CSV/{3}/{4}Eff_{2}_Jet{0}_pfWP_{1}_CSV_order{3}"
                                    .format(i, WP, name, collectionName,
                                            fileprefix),
                                    addSel="{1}_matchPF[{0}] >= 0".format(
                                        i, collection),
                                    label=[label, WPLabel])

                                modules.effPlots.makeEffPlot(
                                    CSVPlotBaseObjs[i],
                                    sample,
                                    "caloJets_csv[{2}_matchCalo[{1}]] >= {0}".
                                    format(WP, i, collection),
                                    basepaths +
                                    "CSV/{3}/{4}Eff_{2}_Jet{0}_caloWP_{1}_CSV_order{3}"
                                    .format(i, WP, name, collectionName,
                                            fileprefix),
                                    addSel="{1}_matchCalo[{0}] >= 0".format(
                                        i, collection),
                                    label=[label, CaloWPLabel])

                        if doMC and doData:
                            modules.effPlots.makeEffSCompPlot(
                                CSVPlotBaseObjs[i],
                                onlysamples,
                                "pfJets_csv[{2}_matchPF[{1}]] >= {0}".format(
                                    WP, i, collection),
                                basepaths +
                                "CSV/{2}/{3}Eff_DataMC_Jet{0}_pfWP_{1}_CSV_order{2}"
                                .format(i, WP, collectionName, fileprefix),
                                addSel="{1}_matchPF[{0}] >= 0".format(
                                    i, collection),
                                label=WPLabel)
                            modules.effPlots.makeEffSCompPlot(
                                CSVPlotBaseObjs[i],
                                onlysamples,
                                "caloJets_csv[{2}_matchCalo[{1}]] >= {0}".
                                format(WP, i, collection),
                                basepaths +
                                "CSV/{2}/{3}Eff_DataMC_Jet{0}_caloWP_{1}_CSV_order{2}"
                                .format(i, WP, collectionName, fileprefix),
                                addSel="{1}_matchCalo[{0}] >= 0".format(
                                    i, collection),
                                label=CaloWPLabel)
                if doinclusive:
                    JetSelectionIter = "abs({1}_eta[{0}]) < 2.4 && {1}_pt[{0}] > 30 && {1}_passesTightLeptVetoID[{0}] > 0".format(
                        "?", collection)
                    logging.info(
                        "Making inclusive effciency ordered by {0} w/ CSV as tagger"
                        .format(collectionName))
                    logging.subinfo("Using collection: {0}".format(collection))
                    CSVPlotBaseObjIter = modules.classes.PlotBase(
                        "{1}_csv[{0}]".format("?", collection),
                        "{0} && {1} && ({2})".format(CSVSelection,
                                                     offlineSelection,
                                                     JetSelectionIter),
                        "1", [20, 0, 1],
                        modules.utils.getAxisTitle("csv",
                                                   0,
                                                   collectionName.lower(),
                                                   inclusive=True),
                        LegendPosition=[0.1, 0.6, 0.4, 0.76])
                    #CSVPlotBaseObjIter.color = WPColors[0]
                    onlysamples = []

                    for sampleStuff in samples:
                        sample, name, label = sampleStuff
                        onlysamples.append(sample)
                        if not (doMC and doData):
                            modules.effPlots.makeEffSumPlot(
                                CSVPlotBaseObjIter,
                                sample,
                                "pfJets_csv[{2}_matchPF[{1}]] >= {0}".format(
                                    WP, "?", collection),
                                nInclusiveIter,
                                basepaths +
                                "CSV/{3}/{4}Eff_{2}_Jet{0}_pfWP_{1}_CSV_order{3}"
                                .format("incl", WP, name, collectionName,
                                        fileprefix),
                                addSel="{1}_matchPF[{0}] >= 0".format(
                                    "?", collection),
                                label=[label, WPLabel])
                            modules.effPlots.makeEffSumPlot(
                                CSVPlotBaseObjIter,
                                sample,
                                "caloJets_csv[{2}_matchCalo[{1}]] >= {0}".
                                format(WP, "?", collection),
                                nInclusiveIter,
                                basepaths +
                                "CSV/{3}/{4}Eff_{2}_Jet{0}_caloWP_{1}_CSV_order{3}"
                                .format("incl", WP, name, collectionName,
                                        fileprefix),
                                addSel="{1}_matchCalo[{0}] >= 0".format(
                                    "?", collection),
                                label=[label, CaloWPLabel])

                    if doMC and doData:
                        modules.effPlots.makeEffSummSCompPlot(
                            CSVPlotBaseObjIter,
                            onlysamples,
                            "pfJets_csv[{2}_matchPF[{1}]] >= {0}".format(
                                WP, "?", collection),
                            nInclusiveIter,
                            basepaths +
                            "CSV/{2}/{3}Eff_DataMC_Jet{0}_pfWP_{1}_CSV_order{2}"
                            .format("incl", WP, collectionName, fileprefix),
                            addSel="{1}_matchPF[{0}] >= 0".format(
                                "?", collection),
                            label=WPLabel)
                        modules.effPlots.makeEffSummSCompPlot(
                            CSVPlotBaseObjIter,
                            onlysamples,
                            "caloJets_csv[{2}_matchCalo[{1}]] >= {0}".format(
                                WP, "?", collection),
                            nInclusiveIter,
                            basepaths +
                            "CSV/{2}/{3}Eff_DataMC_Jet{0}_caloWP_{1}_CSV_order{2}"
                            .format("incl", WP, collectionName, fileprefix),
                            addSel="{1}_matchCalo[{0}] >= 0".format(
                                "?", collection),
                            label=CaloWPLabel)

                if doCross:
                    if dosplit:
                        pass
                    if doinclusive:
                        if looseSel:
                            JetSelectionIter = "{1}_deepcsv[{0}] >= 0 && abs({1}_eta[{0}]) < 2.4 && {1}_pt[{0}] > 30".format(
                                "?", collection)
                        else:
                            JetSelectionIter = "{1}_deepcsv[{0}] >= 0 && abs({1}_eta[{0}]) < 2.4 && {1}_pt[{0}] > 30 && {1}_passesTightLeptVetoID[{0}] > 0".format(
                                "?", collection)
                        logging.info(
                            "Making inclusive effciency ordered by {0} w/ DeepCSV as tagger"
                            .format(collectionName))
                        logging.subinfo(
                            "Using collection: {0}".format(collection))
                        DeepCSVPlotBaseIter = modules.classes.PlotBase(
                            "{1}_deepcsv[{0}]".format("?", collection),
                            "{0} && {1} && ({2})".format(
                                CSVSelection, offlineSelection,
                                JetSelectionIter),
                            "1", [20, 0, 1],
                            modules.utils.getAxisTitle("deepcsv",
                                                       0,
                                                       collectionName.lower(),
                                                       inclusive=True),
                            LegendPosition=[0.1, 0.6, 0.4, 0.76])
                        onlysamples = []
                        for sampleStuff in samples:
                            sample, name, label = sampleStuff
                            onlysamples.append(sample)

                        if doMC and doData:
                            modules.effPlots.makeEffSummSCompPlot(
                                DeepCSVPlotBaseIter,
                                onlysamples,
                                "pfJets_csv[{2}_matchPF[{1}]] >= {0}".format(
                                    WP, "?", collection),
                                nInclusiveIter,
                                basepaths +
                                "CSV/{2}/{3}Eff_DataMC_Jet{0}_pfWP_{1}_CSV_order{2}"
                                .format("cross", WP, collectionName,
                                        fileprefix),
                                addSel="{1}_matchPF[{0}] >= 0".format(
                                    "?", collection),
                                label=WPLabel)
                            modules.effPlots.makeEffSummSCompPlot(
                                DeepCSVPlotBaseIter,
                                onlysamples,
                                "caloJets_csv[{2}_matchCalo[{1}]] >= {0}".
                                format(WP, "?", collection),
                                nInclusiveIter,
                                basepaths +
                                "CSV/{2}/{3}Eff_DataMC_Jet{0}_caloWP_{1}_CSV_order{2}"
                                .format("cross", WP, collectionName,
                                        fileprefix),
                                addSel="{1}_matchCalo[{0}] >= 0".format(
                                    "?", collection),
                                label=CaloWPLabel)

        if compWPs:
            logging.info("Makeing CSV WP comparison")
            for collectionName, collection in collections:
                #JetSelectionIter = "abs({1}_eta[{0}]) < 2.4 && {1}_pt[{0}] > 30".format("?", collection)
                if looseSel:
                    JetSelectionIter = "abs({1}_eta[{0}]) < 2.4 && {1}_pt[{0}] > 30".format(
                        "?", collection)
                else:
                    JetSelectionIter = "abs({1}_eta[{0}]) < 2.4 && {1}_pt[{0}] > 30 && {1}_passesTightLeptVetoID[{0}] > 0".format(
                        "?", collection)
                logging.info(
                    "Making inclusive  WP comparison ordered by {0} w/ CSV as tagger"
                    .format(collectionName))
                logging.subinfo("Using collection: {0}".format(collection))
                CSVPlotBaseObjIter = modules.classes.PlotBase(
                    "{1}_csv[{0}]".format("?", collection),
                    "{0} && {1} && ({2})".format(CSVSelection,
                                                 offlineSelection,
                                                 JetSelectionIter),
                    "1", [20, 0, 1],
                    modules.utils.getAxisTitle("csv",
                                               0,
                                               collectionName.lower(),
                                               inclusive=True),
                    LegendPosition=[0.1, 0.6, 0.4, 0.76])

                for sampleStuff in samples:
                    sample, name, label = sampleStuff
                    logging.info("Plotting sample {0}".format(name))
                    modules.effPlots.makeSumWPComp(
                        CSVPlotBaseObjIter,
                        sample,
                        "pfJets_csv[{1}_matchPF[{0}]]".format("?", collection),
                        nInclusiveIter,
                        CSVWPs,
                        basepaths +
                        "CSV/{3}/{4}Eff_{2}_Jet{0}_pfWPComp_{1}_CSV_order{3}".
                        format("incl", WP, name, collectionName, fileprefix),
                        addSel="{1}_matchPF[{0}] >= 0".format("?", collection))
                    modules.effPlots.makeSumWPComp(
                        CSVPlotBaseObjIter,
                        sample,
                        "caloJets_csv[{1}_matchCalo[{0}]]".format(
                            "?", collection),
                        nInclusiveIter,
                        CSVWPs,
                        basepaths +
                        "CSV/{3}/{4}Eff_{2}_Jet{0}_caloWPComp_{1}_CSV_order{3}"
                        .format("incl", WP, name, collectionName, fileprefix),
                        addSel="{1}_matchCalo[{0}] >= 0".format(
                            "?", collection))

    if doDeepCSV:
        logging.info("Processing plots for DeepCSV")
        DeepCSVPlotBaseObjs = {}

        collections = []
        if ptordered:
            collections.append(("pT", "offJets"))
            logging.debug("Adding {1} to collecton as ordered by {0}".format(
                "ptOrdered", "cleanJets"))
        if csvordered:
            collections.append(("DeepCSV", "cleanDeepCSVJets"))
            logging.debug("Adding {1} to collecton as ordered by {0}".format(
                "csvOrdered", "cleanDeepCSVJets"))

        for WP in DeepCSVWPs:
            WPLabel = getLabel("PF WP: {0}".format(WP), 0.7, "under")
            CaloWPLabel = getLabel("Calo WP: {0}".format(WP), 0.7, "under")
            logging.info("Using WP: {0}".format(WP))
            for collectionName, collection in collections:

                if dosplit:
                    for i in range(nSplitIter):
                        if looseSel:
                            JetSelection = "{1}_deepcsv[{0}] >= 0 && abs({1}_eta[{0}]) < 2.4 && {1}_pt[{0}] > 30".format(
                                i, collection)
                        else:
                            JetSelection = "{1}_deepcsv[{0}] >= 0 && abs({1}_eta[{0}]) < 2.4 && {1}_pt[{0}] > 30 && {1}_passesTightLeptVetoID[{0}] > 0".format(
                                i, collection)
                        logging.info(
                            "Making effciency for Jet {0} ordered by {1} w/ DeepCSV as tagger"
                            .format(i, collectionName))
                        logging.subinfo(
                            "Using collection: {0}".format(collection))
                        DeepCSVPlotBaseObjs[i] = modules.classes.PlotBase(
                            "{1}_deepcsv[{0}]".format(i, collection),
                            "{0} && {1} && ({2})".format(
                                CSVSelection, offlineSelection, JetSelection),
                            "1", [20, 0, 1],
                            modules.utils.getAxisTitle("deepcsv", i,
                                                       collectionName.lower()),
                            LegendPosition=[0.1, 0.6, 0.4, 0.76])

                        DeepCSVPlotBaseObjs[i].color = WPColors[i]
                        onlysamples = []
                        for sampleStuff in samples:
                            sample, name, label = sampleStuff
                            onlysamples.append(sample)
                            if not (doMC and doData):
                                modules.effPlots.makeEffPlot(
                                    DeepCSVPlotBaseObjs[i],
                                    sample,
                                    "(pfJets_deepcsv[{2}_matchPF[{1}]]) >= {0}"
                                    .format(WP, i, collection),
                                    basepaths +
                                    "DeepCSV/{3}/{4}Eff_{2}_Jet{0}_pfWP_{1}_DeepCSV_order{3}"
                                    .format(i, WP, name, collectionName,
                                            fileprefix),
                                    addSel="{1}_matchPF[{0}] >= 0".format(
                                        i, collection),
                                    label=[label, WPLabel])
                                modules.effPlots.makeEffPlot(
                                    DeepCSVPlotBaseObjs[i],
                                    sample,
                                    "(caloJets_deepcsv[{2}_matchCalo[{1}]]) >= {0}"
                                    .format(WP, i, collection),
                                    basepaths +
                                    "DeepCSV/{3}/{4}Eff_{2}_Jet{0}_caloWP_{1}_DeepCSV_order{3}"
                                    .format(i, WP, name, collectionName,
                                            fileprefix),
                                    addSel="{1}_matchCalo[{0}] >= 0".format(
                                        i, collection),
                                    label=[label, CaloWPLabel])

                        if doMC and doData:
                            modules.effPlots.makeEffSCompPlot(
                                DeepCSVPlotBaseObjs[i],
                                onlysamples,
                                "(pfJets_deepcsv[{2}_matchPF[{1}]]) >= {0}".
                                format(WP, i, collection),
                                basepaths +
                                "DeepCSV/{2}/{3}Eff_DataMC_Jet{0}_pfWP_{1}_DeepCSV_order{2}"
                                .format(i, WP, collectionName, fileprefix),
                                addSel="{1}_matchPF[{0}] >= 0".format(
                                    i, collection),
                                label=WPLabel)
                            modules.effPlots.makeEffSCompPlot(
                                DeepCSVPlotBaseObjs[i],
                                onlysamples,
                                "(caloJets_deepcsv[{2}_matchCalo[{1}]]) >= {0}"
                                .format(WP, i, collection),
                                basepaths +
                                "DeepCSV/{2}/{3}Eff_DataMC_Jet{0}_caloWP_{1}_DeepCSV_order{2}"
                                .format(i, WP, collectionName, fileprefix),
                                addSel="{1}_matchCalo[{0}] >= 0".format(
                                    i, collection),
                                label=CaloWPLabel)
                if doinclusive:
                    if looseSel:
                        JetSelectionIter = "{1}_deepcsv[{0}] >= 0 && abs({1}_eta[{0}]) < 2.4 && {1}_pt[{0}] > 30".format(
                            "?", collection)
                    else:
                        JetSelectionIter = "{1}_deepcsv[{0}] >= 0 && abs({1}_eta[{0}]) < 2.4 && {1}_pt[{0}] > 30 && {1}_passesTightLeptVetoID[{0}] > 0".format(
                            "?", collection)
                    logging.info(
                        "Making inclusive effciency ordered by {0} w/ DeepCSV as tagger"
                        .format(collectionName))
                    logging.subinfo("Using collection: {0}".format(collection))
                    DeepCSVPlotBaseIter = modules.classes.PlotBase(
                        "{1}_deepcsv[{0}]".format("?", collection),
                        "{0} && {1} && ({2})".format(CSVSelection,
                                                     offlineSelection,
                                                     JetSelectionIter),
                        "1", [20, 0, 1],
                        modules.utils.getAxisTitle("deepcsv",
                                                   0,
                                                   collectionName.lower(),
                                                   inclusive=True),
                        LegendPosition=[0.1, 0.6, 0.4, 0.76])

                    onlysamples = []
                    for sampleStuff in samples:
                        sample, name, label = sampleStuff
                        onlysamples.append(sample)
                        if not (doMC and doData):
                            modules.effPlots.makeEffSumPlot(
                                DeepCSVPlotBaseIter,
                                sample,
                                "(pfJets_deepcsv[{2}_matchPF[{1}]]) >= {0}".
                                format(WP, "?", collection),
                                nInclusiveIter,
                                basepaths +
                                "DeepCSV/{3}/{4}Eff_{2}_Jet{0}_pfWP_{1}_DeepCSV_order{3}"
                                .format("incl", WP, name, collectionName,
                                        fileprefix),
                                addSel="{1}_matchPF[{0}] >= 0".format(
                                    "?", collection),
                                label=[label, WPLabel])
                            modules.effPlots.makeEffSumPlot(
                                DeepCSVPlotBaseIter,
                                sample,
                                "(caloJets_deepcsv[{2}_matchCalo[{1}]]) >= {0}"
                                .format(WP, "?", collection),
                                nInclusiveIter,
                                basepaths +
                                "DeepCSV/{3}/{4}Eff_{2}_Jet{0}_caloWP_{1}_DeepCSV_order{3}"
                                .format("incl", WP, name, collectionName,
                                        fileprefix),
                                addSel="{1}_matchCalo[{0}] >= 0".format(
                                    "?", collection),
                                label=[label, CaloWPLabel])

                    if doMC and doData:
                        modules.effPlots.makeEffSummSCompPlot(
                            DeepCSVPlotBaseIter,
                            onlysamples,
                            "(pfJets_deepcsv[{2}_matchPF[{1}]]) >= {0} && {2}_matchPF[{1}] >= 0"
                            .format(WP, "?", collection),
                            nInclusiveIter,
                            basepaths +
                            "DeepCSV/{2}/{3}Eff_DataMC_Jet{0}_pfWP_{1}_DeepCSV_order{2}"
                            .format("incl", WP, collectionName, fileprefix),
                            addSel="{1}_matchPF[{0}] >= 0".format(
                                "?", collection),
                            label=WPLabel)
                        modules.effPlots.makeEffSummSCompPlot(
                            DeepCSVPlotBaseIter,
                            onlysamples,
                            "(caloJets_deepcsv[{2}_matchCalo[{1}]]) >= {0}".
                            format(WP, "?", collection),
                            nInclusiveIter,
                            basepaths +
                            "DeepCSV/{2}/{3}Eff_DataMC_Jet{0}_caloWP_{1}_DeepCSV_order{2}"
                            .format("incl", WP, collectionName, fileprefix),
                            addSel="{1}_matchCalo[{0}] >= 0".format(
                                "?", collection),
                            label=CaloWPLabel)

        if compWPs:
            logging.info("Makeing DeepCSV WP comparison")
            for collectionName, collection in collections:
                if looseSel:
                    #JetSelectionIter = "abs({1}_eta[{0}]) < 2.4 && {1}_pt[{0}] > 30".format("?", collection)
                    JetSelectionIter = "{1}_pt[{0}] > 0".format(
                        "?", collection)
                else:
                    JetSelectionIter = "{1}_deepcsv[{0}] >= 0 && abs({1}_eta[{0}]) < 2.4 && {1}_pt[{0}] > 30 && {1}_passesTightLeptVetoID[{0}] > 0".format(
                        "?", collection)
                logging.info(
                    "Making inclusive  WP comparison ordered by {0} w/ DeepCSV as tagger"
                    .format(collectionName))
                logging.subinfo("Using collection: {0}".format(collection))
                DeepCSVPlotBaseIter = modules.classes.PlotBase(
                    "{1}_deepcsv[{0}]".format("?", collection),
                    "{0} && {1} && ({2})".format(CSVSelection,
                                                 offlineSelection,
                                                 JetSelectionIter),
                    "1", [25, 0, 1],
                    modules.utils.getAxisTitle("deepcsv",
                                               0,
                                               collectionName.lower(),
                                               inclusive=True),
                    LegendPosition=[0.1, 0.6, 0.4, 0.76])

                for sampleStuff in samples:
                    sample, name, label = sampleStuff
                    logging.info("Plotting sample {0}".format(name))
                    modules.effPlots.makeSumWPComp(
                        DeepCSVPlotBaseIter,
                        sample,
                        "pfJets_deepcsv[{1}_matchPF[{0}]]".format(
                            "?", collection),
                        nInclusiveIter,
                        DeepCSVWPs,
                        basepaths +
                        "DeepCSV/{3}/{4}Eff_{2}_Jet{0}_pfWPComp_{1}_DeepCSV_order{3}"
                        .format("incl", WP, name, collectionName, fileprefix),
                        #addSel = "pfJets_deepcsv[{1}_matchPF[{0}]] >= 0 && {1}_matchPF[{0}] >= 0 && pfJets_pt[{1}_matchPF[{0}]] > 30 && abs(pfJets_eta[{1}_matchPF[{0}]]) < 2.4".format("?", collection)
                        addSel="{1}_matchPF[{0}] >= 0 && emilSourceValid == 1".
                        format("?", collection),
                        saveGraph=True)
                    """
                    modules.effPlots.makeSumWPComp(DeepCSVPlotBaseIter, sample,
                                                   "caloJets_deepcsv[{1}_matchCalo[{0}]]".format("?", collection), nInclusiveIter, DeepCSVWPs, 
                                                   basepaths+"DeepCSV/{3}/{4}Eff_{2}_Jet{0}_caloWPComp_{1}_DeepCSV_order{3}".format("incl", WP, name, collectionName, fileprefix),
                                                   addSel = "caloJets_deepcsv[{1}_matchCalo[{0}]] >= 0 && {1}_matchCalo[{0}] >= 0 && pfJets_pt[{1}_matchCalo[{0}]] > 30 && abs(pfJets_eta[{1}_matchCalo[{0}]]) < 2.4".format("?", collection),
                                                   #addSel = "caloJets_deepcsv[{1}_matchCalo[{0}]] >= 0".format("?", collection),
                    )

                    """

    logger.info("Runtime: {0:8f}s".format(time.time() - t0))
    logger.info("Closing efficiency turnon calc")
Example #2
0
def getBEfficiency(PlotBaseObj,
                   Samples2Stack,
                   probeSelection,
                   tagSelection,
                   addNumeratorSel,
                   numWPs,
                   bindex,
                   convertIterSelection=True,
                   probeIndex=0,
                   tagIndex=1,
                   data=None,
                   normalized=False,
                   drawRatio=True,
                   outname=None,
                   outputformat="pdf",
                   label=None,
                   savetable=True):

    results = {}

    modules.utils.savePlot(None, outname, None, True)

    outfile = ROOT.TFile(outname + "_histos.root", "RECREATE")
    for WP in numWPs:
        results[WP] = {}
        logging.info("Processing WP: {0}".format(WP))
        #Getting denominator
        numWP = "{0} > {1}".format(addNumeratorSel, WP)
        StackSum_denom, StackHistos_denom, hData_denom = LeadingProbe(
            PlotBaseObj,
            Samples2Stack,
            probeSelection,
            tagSelection,
            False,
            convertIterSelection,
            probeIndex,
            tagIndex,
            data,
            skipPlots=True)
        StackSum_num, StackHistos_num, hData_num = LeadingProbe(
            PlotBaseObj,
            Samples2Stack,
            "{0} && {1}".format(probeSelection, numWP),
            tagSelection,
            False,
            convertIterSelection,
            probeIndex,
            tagIndex,
            data,
            skipPlots=True)
        outfile.cd()
        ##Normalization to data
        try:
            hData_denom.Integral() / StackSum_denom.Integral()
        except ZeroDivisionError:
            MCscale_denom = 0
            logging.warning("ZeroDivision error. Setting MCscale_denom to 0")
        else:
            MCscale_denom = hData_denom.Integral() / StackSum_denom.Integral()

        NormLabel = getLabel("MC normalized to Data", 0.6, scale=0.8)
        if label is None:
            label = [NormLabel]
        if label is isinstance(label, ROOT.TLatex):
            label = [label + NormLabel]
        else:
            label.append(NormLabel)

        logging.info("Scaling denom by {0}".format(MCscale_denom))
        StackSum_denom.Scale(MCscale_denom)
        for ihisto, histo in enumerate(StackHistos_denom):
            histo.Scale(MCscale_denom)

        #MCscale_num = hData_num.Integral()/StackSum_num.Integral()
        #logging.info("Scaling num by {0}".format(MCscale_num))
        StackSum_num.Scale(MCscale_denom)
        for ihisto, histo in enumerate(StackHistos_num):
            histo.Scale(MCscale_denom)

        calcEfficiencies(results, StackSum_denom, StackHistos_denom,
                         hData_denom, StackSum_num, StackHistos_num, hData_num,
                         PlotBaseObj, Samples2Stack, WP, bindex, data,
                         normalized, drawRatio, outname, outputformat, label,
                         savetable)

    if savetable:
        saveEffTable(numWPs, results, outname)
    outfile.Close()
Example #3
0
def flavourComposition(loglev,
                       run,
                       doData,
                       doCSV,
                       doDeepCSV,
                       plotinclusive,
                       plotTagAndProbe,
                       calcEfficiency,
                       TagWPCSV=0.8484,
                       TagWPDeepCSV=0.8001,
                       test=False):
    import ROOT

    import modules.classes
    import modules.DataMC
    import modules.TagNProbe

    styleconfig = SafeConfigParser()
    #logging.debug("Loading style config")
    styleconfig.read("config/plotting.cfg")

    setup_logging(loglevel=loglev, logname="shapeoutput", errname="shapeerror")

    logger = logging.getLogger(__name__)
    #logging.getLogger(__name__).setLevel("SUBDEBUG")

    logger.info("Starting flavour composition analysis")
    t0 = time.time()

    if not (doCSV or doDeepCSV):
        if __name__ == "__main__":
            logging.warning(
                "At least on of the flags --CSV and --deepCSV should to be set"
            )
        else:
            logging.warning(
                "At least on of the paramters doCSV and doDeepCSV should to be set"
            )
        logging.warning("Falling back the only CSV")
        doCSV = True

    if run not in ["C", "CD", "E", "F", "CDF"]:
        logging.error("Run not supported!")
        exit()
    ##############################################################################################################
    ############################################## Plotting Code #################################################
    ##############################################################################################################

    if loglev > 0:
        ROOT.gErrorIgnoreLevel = ROOT.kError  # kPrint, kInfo, kWarning, kError, kBreak, kSysError, kFatal;

    MCInput = "/mnt/t3nfs01/data01/shome/koschwei/scratch/HLTBTagging/DiLepton_v10/ttbar/ttbar_98p0_mod_mod_mod_mod_mod_mod.root"
    MC2Input = "/mnt/t3nfs01/data01/shome/koschwei/scratch/HLTBTagging/DiLepton_v10/ST/ST_tW_part_mod_mod_mod.root"
    MC3Input = "/mnt/t3nfs01/data01/shome/koschwei/scratch/HLTBTagging/DiLepton_v10/SantiT/ST_antitW_mod_mod_mod.root"
    if run == "C":
        logging.info("Setting file, name and basepath for Run C")
        DataInput = "/mnt/t3nfs01/data01/shome/koschwei/scratch/HLTBTagging/DiLepton_v10_2/RunC/RunCFull.root"
        puweight = "get_puWeight_C_ReReco(pu)"
        globalPrefix = "ProdGTData_RunC_TESTTESTFull_XS"
        basepath = "v10_2nTuples_Final/FlavourSplitting/RunC/"

    #Run C-D
    if run == "CD":
        logging.info("Setting file, pu weight, name and basepath for Run CD")
        DataInput = "/mnt/t3nfs01/data01/shome/koschwei/scratch/HLTBTagging/DiLepton_v10_2/RunCD.root"
        puweight = "get_puWeight_CD(pu)"
        globalPrefix = "ProdGTData_RunCD_TESTTESTFull_XS"
        basepath = "v10_2nTuples_Final/FlavourSplitting/RunCD/"

    if run == "CDF":
        logging.info("Setting file, pu weight, name and basepath for Run CDF")
        DataInput = "/mnt/t3nfs01/data01/shome/koschwei/scratch/HLTBTagging/DiLepton_v10_2/RunCDF.root"
        #puweight = "get_puWeight_CDF(pu)"
        #globalPrefix = "ProdGTData_RunCDF_TESTTESTFull_XS_MWP"
        #puweight = "get_puWeight_CDF(pu)*wDeepCSV"
        #globalPrefix = "ProdGTData_RunCDF_TESTTESTFull_XS_mod_deep_MWP"
        puweight = "get_puWeight_CDF(pu) * offTightElectrons_SF[0] * offTightMuons_SF[0]"
        globalPrefix = "ProdGTData_RunCDF_LeptonSF_XS_mod_v2_MWP"
        basepath = "v10_2nTuples_Finalv2/FlavourSplitting/RunCDF/jetW_finebinning/"

    #Run F
    if run == "F":
        logging.info("Setting file, pu weight, name and basepath for Run F")
        DataInput = "/mnt/t3nfs01/data01/shome/koschwei/scratch/HLTBTagging/DiLepton_v8/MuonEG/RunF/phase1/MuonEG_RunF_phase1_mod_mod_mod.root"
        puweight = "wPURunF"
        globalPrefix = "DeepCSVMPresel_phase1_RunF"
        basepath = "v8nTuples/FlavourSplitting/RunF/"

    #Run E
    if run == "E":
        logging.info("Setting file, pu weight, name and basepath for Run E")
        DataInput = "/mnt/t3nfs01/data01/shome/koschwei/scratch/HLTBTagging/DiLepton_v8/MuonEG/RunE/phase1/MuonEG_RunE_phase1_mod_mod_mod.root"
        puweight = "wPURunE"
        globalPrefix = "DeepCSVMPresel_phase1_RunE"
        basepath = "v8nTuples/FlavourSplitting/RunE/"

    MCSelection = "1"
    DataSelection = "1"
    VarSelection = "Sum$(offCleanJets_deepcsv > 0.8001 && offCleanJets_pt > 30 && abs(offCleanJets_eta) < 2.4) >= 1 && Sum$(offCleanJets_pt > 30 && abs(offCleanJets_eta) < 2.4) >= 2"
    TriggerSelection = "HLT_Mu12_TrkIsoVVL_Ele23_CaloIdL_TrackIdL_IsoVL_v4 > 0 || HLT_Mu23_TrkIsoVVL_Ele12_CaloIdL_TrackIdL_IsoVL_v4 > 0"
    LeptonSelection = "Sum$((abs(offTightElectrons_superClusterEta) <= 1.4442 || abs(offTightElectrons_superClusterEta) >= 1.5660) && offTightElectrons_pt > 30 && abs(offTightElectrons_eta) < 2.4) == 1 && Sum$(offTightMuons_iso < 0.25 && offTightMuons_pt > 20 && abs(offTightMuons_eta) < 2.4) == 1"

    offlineSelection = "abs(offCleanJets_eta) < 2.4 && offCleanJets_pt > 30 && offCleanJets_passesTightLeptVetoID > 0"
    offlineSelectionIter = "abs(offCleanJets_eta[?]) < 2.4 && offCleanJets_pt[?] > 30 && offCleanJets_passesTightLeptVetoID[?] > 0"

    MCsamples = []
    MCsamplesIter = []

    datalumi = 14000

    eventSelection = "({0}) && ({1}) && ({2}) && ({3})".format(
        VarSelection, TriggerSelection, LeptonSelection, MCSelection)
    ttbarOnly = False
    if ttbarOnly:
        MCsamples.append(
            modules.classes.Sample(
                "ttbar_udsgJets",
                MCInput,
                "{0} && offCleanJets_hadronFlavour == 0".format(
                    eventSelection),
                88.341903326,
                datalumi,
                ROOT.kBlue,
                941634,
                weight=puweight,
                legendText="light jets (t#bar{t})"))
        MCsamples.append(
            modules.classes.Sample(
                "ttbar_cJets",
                MCInput,
                "{0} && offCleanJets_hadronFlavour == 4".format(
                    eventSelection),
                88.341903326,
                datalumi,
                ROOT.kGreen + 2,
                941634,
                weight=puweight,
                legendText="c jets (t#bar{t})"))
        MCsamples.append(
            modules.classes.Sample(
                "ttbar_bJets",
                MCInput,
                "{0} && offCleanJets_hadronFlavour == 5".format(
                    eventSelection),
                88.341903326,
                datalumi,
                ROOT.kRed,
                941634,
                weight=puweight,
                legendText="b jets (t#bar{t})"))

        MCsamplesIter.append(
            modules.classes.Sample(
                "ttbar_udsgJets",
                MCInput,
                "{0} && offCleanJets_hadronFlavour[?] == 0".format(
                    eventSelection),
                88.341903326,
                datalumi,
                ROOT.kBlue,
                941634,
                weight=puweight,
                legendText="light jets (t#bar{t})"))
        MCsamplesIter.append(
            modules.classes.Sample(
                "ttbar_cJets",
                MCInput,
                "{0} && offCleanJets_hadronFlavour[?] == 4".format(
                    eventSelection),
                88.341903326,
                datalumi,
                ROOT.kGreen + 2,
                941634,
                weight=puweight,
                legendText="c jets (t#bar{t})"))
        MCsamplesIter.append(
            modules.classes.Sample(
                "ttbar_bJets",
                MCInput,
                "{0} && offCleanJets_hadronFlavour[?] == 5".format(
                    eventSelection),
                88.341903326,
                datalumi,
                ROOT.kRed,
                941634,
                weight=puweight,
                legendText="b jets (t#bar{t})"))

    else:
        """ ttbar """
        MCsamples.append(
            modules.classes.Sample(
                "ttbar_udsgJets",
                MCInput,
                "{0} && offCleanJets_hadronFlavour == 0".format(
                    eventSelection),
                88.34,
                datalumi,
                ROOT.kBlue,
                941634,
                weight=puweight,
                legendText="light jets"))
        MCsamples.append(
            modules.classes.Sample(
                "ttbar_cJets",
                MCInput,
                "{0} && offCleanJets_hadronFlavour == 4".format(
                    eventSelection),
                88.34,
                datalumi,
                ROOT.kOrange + 2,
                941634,
                weight=puweight,
                legendText="c jets "))
        MCsamples.append(
            modules.classes.Sample(
                "ttbar_bJets",
                MCInput,
                "{0} && offCleanJets_hadronFlavour == 5".format(
                    eventSelection),
                88.34,
                datalumi,
                ROOT.kRed,
                941634,
                weight=puweight,
                legendText="b jets"))

        MCsamplesIter.append(
            modules.classes.Sample(
                "ttbar_udsgJets",
                MCInput,
                "{0} && offCleanJets_hadronFlavour[?] == 0".format(
                    eventSelection),
                88.34,
                datalumi,
                ROOT.kBlue,
                941634,
                weight=puweight,
                legendText="light jets "))
        MCsamplesIter.append(
            modules.classes.Sample(
                "ttbar_cJets",
                MCInput,
                "{0} && offCleanJets_hadronFlavour[?] == 4".format(
                    eventSelection),
                88.34,
                datalumi,
                ROOT.kGreen + 2,
                941634,
                weight=puweight,
                legendText="c jets "))
        MCsamplesIter.append(
            modules.classes.Sample(
                "ttbar_bJets",
                MCInput,
                "{0} && offCleanJets_hadronFlavour[?] == 5".format(
                    eventSelection),
                88.34,
                datalumi,
                ROOT.kRed,
                941634,
                weight=puweight,
                legendText="b jets"))
        """ ST """
        MCsamples.append(
            modules.classes.Sample(
                "st_udsgJets",
                MC2Input,
                "{0} && offCleanJets_hadronFlavour == 0".format(
                    eventSelection),
                35.85,
                datalumi,
                ROOT.kBlue + 3,
                727212,
                weight=puweight,
                legendText="light jets (single t)"))
        MCsamples.append(
            modules.classes.Sample(
                "st_cJets",
                MC2Input,
                "{0} && offCleanJets_hadronFlavour == 4".format(
                    eventSelection),
                35.85,
                datalumi,
                ROOT.kGreen + 4,
                727212,
                weight=puweight,
                legendText="c jets (single t)"))
        MCsamples.append(
            modules.classes.Sample(
                "st_bJets",
                MC2Input,
                "{0} && offCleanJets_hadronFlavour == 5".format(
                    eventSelection),
                35.85,
                datalumi,
                ROOT.kRed + 3,
                727212,
                weight=puweight,
                legendText="b jets (single t)"))

        MCsamplesIter.append(
            modules.classes.Sample(
                "st_udsgJets",
                MC2Input,
                "{0} && offCleanJets_hadronFlavour[?] == 0".format(
                    eventSelection),
                35.85,
                datalumi,
                ROOT.kBlue + 3,
                727212,
                weight=puweight,
                legendText="light jets (single t)"))
        MCsamplesIter.append(
            modules.classes.Sample(
                "st_cJets",
                MC2Input,
                "{0} && offCleanJets_hadronFlavour[?] == 4".format(
                    eventSelection),
                35.85,
                datalumi,
                ROOT.kGreen + 4,
                727212,
                weight=puweight,
                legendText="c jets (single t)"))
        MCsamplesIter.append(
            modules.classes.Sample(
                "st_bJets",
                MC2Input,
                "{0} && offCleanJets_hadronFlavour[?] == 5".format(
                    eventSelection),
                35.85,
                datalumi,
                ROOT.kRed + 3,
                727212,
                weight=puweight,
                legendText="b jets (single t)"))
        """ SAntiT """
        MCsamples.append(
            modules.classes.Sample(
                "santit_udsgJets",
                MC3Input,
                "{0} && offCleanJets_hadronFlavour == 0".format(
                    eventSelection),
                35.85,
                datalumi,
                ROOT.kBlue - 9,
                5603226,
                weight=puweight,
                legendText="light jets (single #bar{t})"))
        MCsamples.append(
            modules.classes.Sample(
                "santit_cJets",
                MC3Input,
                "{0} && offCleanJets_hadronFlavour == 4".format(
                    eventSelection),
                35.85,
                datalumi,
                ROOT.kGreen - 3,
                5603226,
                weight=puweight,
                legendText="c jets (single #bar{t})"))
        MCsamples.append(
            modules.classes.Sample(
                "santit_bJets",
                MC3Input,
                "{0} && offCleanJets_hadronFlavour == 5".format(
                    eventSelection),
                35.85,
                datalumi,
                ROOT.kRed - 9,
                5603226,
                weight=puweight,
                legendText="b jets (single #bar{t})"))

        MCsamplesIter.append(
            modules.classes.Sample(
                "santit_udsgJets",
                MC3Input,
                "{0} && offCleanJets_hadronFlavour[?] == 0".format(
                    eventSelection),
                35.85,
                datalumi,
                ROOT.kBlue - 9,
                5603226,
                weight=puweight,
                legendText="light jets (single #bar{t})"))
        MCsamplesIter.append(
            modules.classes.Sample(
                "santit_cJets",
                MC3Input,
                "{0} && offCleanJets_hadronFlavour[?] == 4".format(
                    eventSelection),
                35.85,
                datalumi,
                ROOT.kGreen - 3,
                5603226,
                weight=puweight,
                legendText="c jets (single #bar{t})"))
        MCsamplesIter.append(
            modules.classes.Sample(
                "santit_bJets",
                MC3Input,
                "{0} && offCleanJets_hadronFlavour[?] == 5".format(
                    eventSelection),
                35.85,
                datalumi,
                ROOT.kRed - 9,
                5603226,
                weight=puweight,
                legendText="b jets (single #bar{t})"))
        """
        MCsamples.append( modules.classes.Sample("ttbar", MCInput, "{0}".format(eventSelection),
                                                88.341903326, datalumi, ROOT.kBlue, 941634, weight = puweight, legendText = "t#bar{t}") )
        MCsamples.append( modules.classes.Sample("STtW", MC2Input, "{0}".format(eventSelection),
                                                 1, datalumi, ROOT.kRed, 727212, weight = puweight, legendText = "ST tW") )
        
        MCsamplesIter.append( modules.classes.Sample("ttbar", MCInput, "{0}".format(eventSelection),
                                                    88.341903326, datalumi, ROOT.kBlue, 941634, weight = puweight, legendText = "t#bar{t}") )
        MCsamplesIter.append( modules.classes.Sample("STtW", MC2Input, "{0}".format(eventSelection),
                                                     1, datalumi, ROOT.kBlue, 727212, weight = puweight, legendText = "ST tW") )
        """
    if doData:
        eventSelection = "({0}) && ({1}) && ({2})".format(
            VarSelection, TriggerSelection, LeptonSelection)
        dataSample = modules.classes.Sample("data",
                                            DataInput,
                                            eventSelection,
                                            color=ROOT.kBlue,
                                            legendText="Data")
    else:
        dataSample = None

    if plotinclusive:
        logging.info("Making inclusive stack plots")
        if doCSV:
            CSVOffline = modules.classes.PlotBase("offCleanJets_csv",
                                                  offlineSelection, "1",
                                                  [100, 0, 1],
                                                  "Offline jet csv value")
            CSVOfflineIter = modules.classes.PlotBase("offCleanJets_csv", "1",
                                                      "1", [100, 0, 1],
                                                      "Offline jet csv value")
            CSVPF = modules.classes.PlotBase(
                "pfJets_csv[offCleanJets_matchPF]",
                "{0} && {1}".format(offlineSelection,
                                    "1"), "1", [100, 0, 1], "PF jet csv value")
            CSVPFIter = modules.classes.PlotBase("offCleanJets_csv", "1", "1",
                                                 [100, 0, 1],
                                                 "PF jet csv value")
            CSVCaloIter = modules.classes.PlotBase("offCleanJets_csv", "1",
                                                   "1", [100, 0, 1],
                                                   "Calo jet csv value")
            #modules.DataMC.makeStackDMCPlot(CSVOffline, MCsamples, dataSample, drawRatio = True, outname = basepath+globalPrefix+"_off_csv", normalized = True)
            #modules.DataMC.makeStackDMCPlot(CSVPF, MCsamples, dataSample, drawRatio = True, outname = basepath+globalPrefix+"_pf_csv", normalized = True)
            modules.DataMC.makeSumDMCPlot(CSVOfflineIter,
                                          MCsamplesIter,
                                          "offCleanJets_csv[?]",
                                          15,
                                          offlineSelectionIter,
                                          dataSample,
                                          drawRatio=True,
                                          outname=basepath + globalPrefix +
                                          "_off_sum_csv",
                                          normalized=True)
            modules.DataMC.makeSumDMCPlot(
                CSVPFIter,
                MCsamplesIter,
                "pfJets_csv[offCleanJets_matchPF[?]]",
                15,
                "{0} && {1}".format(offlineSelectionIter,
                                    "offCleanJets_matchPF[?] >= 0"),
                dataSample,
                drawRatio=True,
                outname=basepath + globalPrefix + "_pf_inclusive_csv",
                normalized=True)
            modules.DataMC.makeSumDMCPlot(
                CSVCaloIter,
                MCsamplesIter,
                "caloJets_csv[offCleanJets_matchCalo[?]]",
                15,
                "{0} && {1}".format(offlineSelectionIter,
                                    "offCleanJets_matchCalo[?] >= 0"),
                dataSample,
                drawRatio=True,
                outname=basepath + globalPrefix + "_calo_inclusive_csv",
                normalized=True)
        if doDeepCSV:
            DeepCSVOfflineIter = modules.classes.PlotBase(
                "offCleanJets_deepcsv", "1", "1", [100, 0, 1],
                "Offline jet DeepCSV value")
            DeepCSVPFIter = modules.classes.PlotBase("offCleanJets_deepcsv",
                                                     "1", "1", [100, 0, 1],
                                                     "PF jet DeepCSV value")
            DeepCSVCaloIter = modules.classes.PlotBase(
                "offCleanJets_deepcsv", "1", "1", [100, 0, 1],
                "Calo jet DeepCSV value")
            #modules.DataMC.makeStackDMCPlot(CSVOffline, MCsamples, dataSample, drawRatio = True, outname = basepath+globalPrefix+"_off_csv", normalized = True)
            #modules.DataMC.makeStackDMCPlot(CSVPF, MCsamples, dataSample, drawRatio = True, outname = basepath+globalPrefix+"_pf_csv", normalized = True)
            modules.DataMC.makeSumDMCPlot(DeepCSVOfflineIter,
                                          MCsamplesIter,
                                          "offCleanJets_deepcsv[?]",
                                          15,
                                          offlineSelectionIter,
                                          dataSample,
                                          drawRatio=True,
                                          outname=basepath + globalPrefix +
                                          "_off_sum_csv",
                                          normalized=True)
            modules.DataMC.makeSumDMCPlot(
                DeepCSVPFIter,
                MCsamplesIter,
                "pfJets_deepcsv[offCleanJets_matchPF[?]]",
                15,
                "{0} && {1}".format(offlineSelectionIter,
                                    "offCleanJets_matchPF[?] >= 0"),
                dataSample,
                drawRatio=True,
                outname=basepath + globalPrefix + "_pf_inclusive_deepcsv",
                normalized=True)
            modules.DataMC.makeSumDMCPlot(
                DeepCSVCaloIter,
                MCsamplesIter,
                "caloJets_deepcsv[offCleanJets_matchCalo[?]]",
                15,
                "{0} && {1}".format(offlineSelectionIter,
                                    "offCleanJets_matchCalo[?] >= 0"),
                dataSample,
                drawRatio=True,
                outname=basepath + globalPrefix + "_calo_inclusive_deepcsv",
                normalized=True)

    ##############################################################################################################
    ##############################################################################################################
    ########################################### Tag&Probe plots ##################################################
    ##############################################################################################################
    ##############################################################################################################

    if plotTagAndProbe:
        lepoverfix = False
        logging.info("Making Tag&Probe plots")
        probeSel = offlineSelectionIter
        if doCSV:
            logging.info("Processing CSV plots")
            OffCSVnthJet = modules.classes.PlotBase(
                "offCleanJets_csv[?]", "1", "1", [100, 0, 1],
                "Probe offline jet CSV value")
            PFCSVnthJet = modules.classes.PlotBase(
                "pfJets_csv[offCleanJets_matchPF[?]]", "1", "1", [100, 0, 1],
                "PF jet matched to probe CSV value")
            CaloCSVnthJet = modules.classes.PlotBase(
                "caloJets_csv[offCleanJets_matchCalo[?]]", "1", "1",
                [100, 0, 1], "Calo jet matched to probe CSV value")

            thisProbeWeight = "offCleanJets_csvv2SF[?]"
            thisTagWeight = "offCleanJets_deepcsvSF[?]"

            #tagSel = "{0} && {1}".format(offlineSelectionIter, "offCleanJets_csv[?] >= {0}".format(TagWPCSV))
            tagSel = "{0} && {1}".format(
                offlineSelectionIter,
                "offCleanJets_deepcsv[?] >= {0}".format(TagWPDeepCSV))
            WPlabel = getLabel("Tag DeepCSV WP: {0}".format(TagWPDeepCSV),
                               styleconfig.getfloat("CMSLabel", "xStart"),
                               pos="topSup",
                               scale=0.8)
            #tagSel = "1"
            if calcEfficiency:
                logging.error("New reweighting missing !")
                exit()
                logging.info("Calculating PF Efficiency")
                if not test:
                    pfCSVWPs = [0.405, 0.840, 0.975]
                else:
                    logging.warning("Test flag set! Only using one WP")
                    pfCSVWPs = [0.840]
                if not lepoverfix:
                    modules.TagNProbe.getBEfficiency(
                        PFCSVnthJet,
                        MCsamplesIter,
                        "{0} && {1}".format(probeSel,
                                            "offCleanJets_matchPF[?] >= 0"),
                        tagSel,
                        "pfJets_csv[offCleanJets_matchPF[?]]",
                        pfCSVWPs,
                        2,
                        data=dataSample,
                        normalized=True,
                        outname=basepath + "PFCSV/" + globalPrefix +
                        "_TnP_leading_pf_csv",
                        label=[WPlabel])
                else:
                    logging.info("Plotting with tmp. lepton overlap removal")
                    modules.TagNProbe.getBEfficiencyHack(
                        PFCSVnthJet,
                        MCsamplesIter,
                        "{0} && {1}".format(probeSel,
                                            "offCleanJets_matchPF[?] >= 0"),
                        tagSel,
                        "pfJets_csv[offCleanJets_matchPF[?]]",
                        pfCSVWPs,
                        2,
                        lepveto,
                        probeIndices=[0, 1, 2],
                        tagIndices=[[1, 2, 3], [2, 3, 4], [3]],
                        data=dataSample,
                        normalized=True,
                        outname=basepath + "PFCSV/" + globalPrefix +
                        "_TnP_leading_pf_csv",
                        label=[WPlabel])

                if not test:
                    logging.info("Calculating Calo Efficiency")
                    if not lepoverfix:
                        modules.TagNProbe.getBEfficiency(
                            CaloCSVnthJet,
                            MCsamplesIter,
                            "{0} && {1}".format(
                                probeSel, "offCleanJets_matchCalo[?] >= 0"),
                            tagSel,
                            "caloJets_csv[offCleanJets_matchCalo[?]]",
                            [0.435, 0.840, 0.97],
                            2,
                            data=dataSample,
                            normalized=True,
                            outname=basepath + "CaloCSV/" + globalPrefix +
                            "_TnP_leading_calo_csv",
                            label=[WPlabel])
                    else:
                        logging.info(
                            "Plotting with tmp. lepton overlap removal")
                        modules.TagNProbe.getBEfficiencyHack(
                            CaloCSVnthJet,
                            MCsamplesIter,
                            "{0} && {1}".format(
                                probeSel, "offCleanJets_matchCalo[?] >= 0"),
                            tagSel,
                            "caloJets_csv[offCleanJets_matchCalo[?]]",
                            [0.435, 0.840, 0.97],
                            2,
                            lepveto,
                            probeIndices=[0, 1, 2],
                            tagIndices=[[1, 2, 3], [2, 3], [3]],
                            data=dataSample,
                            normalized=True,
                            outname=basepath + "CaloCSV/" + globalPrefix +
                            "_TnP_leading_calo_csv",
                            label=[WPlabel])
                else:
                    logging.warning("Test flag set! Skipping calo efficiancy")

            else:
                modules.TagNProbe.LeadingProbe(
                    OffCSVnthJet,
                    MCsamplesIter,
                    probeSel,
                    tagSel,
                    data=dataSample,
                    convertIterSelection=True,
                    outname=basepath + globalPrefix + "_TnP_leadingoff_csv",
                    normalized=True,
                    label=[WPlabel],
                    probeWeight=thisProbeWeight,
                    tagWeight=thisTagWeight)
                modules.TagNProbe.LeadingProbe(
                    PFCSVnthJet,
                    MCsamplesIter,
                    "{0} && {1}".format(probeSel,
                                        "offCleanJets_matchPF[?] >= 0"),
                    tagSel,
                    data=dataSample,
                    convertIterSelection=True,
                    outname=basepath + globalPrefix + "_TnP_leading_pf_csv",
                    normalized=True,
                    label=[WPlabel],
                    probeWeight=thisProbeWeight,
                    tagWeight=thisTagWeight)
                modules.TagNProbe.LeadingProbe(
                    CaloCSVnthJet,
                    MCsamplesIter,
                    "{0} && {1}".format(probeSel,
                                        "offCleanJets_matchCalo[?] >= 0"),
                    tagSel,
                    data=dataSample,
                    convertIterSelection=True,
                    outname=basepath + globalPrefix + "_TnP_leading_calo_csv",
                    normalized=True,
                    label=[WPlabel],
                    probeWeight=thisProbeWeight,
                    tagWeight=thisTagWeight)
        if doDeepCSV:
            logging.info("Processing DeepCSV plots")
            WPlabel = getLabel("Tag DeepCSV WP: {0}".format(TagWPDeepCSV),
                               styleconfig.getfloat("CMSLabel", "xStart"),
                               pos="topSup",
                               scale=0.8)
            OffDeepCSVnthJet = modules.classes.PlotBase(
                "offCleanJets_deepcsv[?]", "1", "1", [100, 0, 1],
                "Probe offline jet DeepCSV value")
            PFDeepCSVnthJet = modules.classes.PlotBase(
                "pfJets_deepcsv[offCleanJets_matchPF[?]]", "1", "1",
                [100, 0, 1], "PF jet matched to probe DeepCSV value")
            CaloDeepCSVnthJet = modules.classes.PlotBase(
                "caloJets_deepcsv[offCleanJets_matchCalo[?]]", "1", "1",
                [100, 0, 1], "Calo jet matched to probe DeepCSV value")

            thisProbeWeight = "offCleanJets_deepcsvSF[?]"
            thisTagWeight = "offCleanJets_deepcsvSF[?]"

            tagSel = "{0} && {1}".format(
                offlineSelectionIter,
                "offCleanJets_deepcsv[?] >= {0}".format(TagWPDeepCSV))
            #tagSel = "1"
            if calcEfficiency:
                logging.error("New reweighting missing !")
                exit()
                logging.info("Calculating PF Efficiency")
                if not lepoverfix:
                    modules.TagNProbe.getBEfficiency(
                        PFDeepCSVnthJet,
                        MCsamplesIter,
                        "{0} && {1}".format(probeSel,
                                            "offCleanJets_matchPF[?] >= 0"),
                        tagSel,
                        "pfJets_deepcsv[offCleanJets_matchPF[?]]",
                        [0.2, 0.67, 0.955],
                        2,
                        data=dataSample,
                        normalized=False,
                        outname=basepath + "PFDeepCSV/" + globalPrefix +
                        "_TnP_leading_pf_deepcsv",
                        label=[WPlabel])
                else:
                    modules.TagNProbe.getBEfficiencyHack(
                        PFDeepCSVnthJet,
                        MCsamplesIter,
                        "{0} && {1}".format(probeSel,
                                            "offCleanJets_matchPF[?] >= 0"),
                        tagSel,
                        "pfJets_deepcsv[offCleanJets_matchPF[?]]",
                        [0.2, 0.67, 0.955],
                        2,
                        lepveto,
                        probeIndices=[0, 1, 2],
                        tagIndices=[[1, 2, 3], [2, 3], [3]],
                        data=dataSample,
                        normalized=True,
                        outname=basepath + "PFDeepCSV/" + globalPrefix +
                        "_TnP_leading_pf_deepcsv",
                        label=[WPlabel])
                logging.info("Calculating Calo Efficiency")
                if not lepoverfix:
                    modules.TagNProbe.getBEfficiency(
                        CaloDeepCSVnthJet,
                        MCsamplesIter,
                        "{0} && {1}".format(probeSel,
                                            "offCleanJets_matchCalo[?] >= 0"),
                        tagSel,
                        "caloJets_deepcsv[offCleanJets_matchCalo[?]]",
                        [0.205, 0.675, 0.95],
                        2,
                        data=dataSample,
                        normalized=False,
                        outname=basepath + "CaloDeepCSV/" + globalPrefix +
                        "_TnP_leading_calo_deepcsv",
                        label=[WPlabel])
                else:
                    modules.TagNProbe.getBEfficiencyHack(
                        CaloDeepCSVnthJet,
                        MCsamplesIter,
                        "{0} && {1}".format(probeSel,
                                            "offCleanJets_matchCalo[?] >= 0"),
                        tagSel,
                        "caloJets_deepcsv[offCleanJets_matchCalo[?]]",
                        [0.205, 0.675, 0.95],
                        2,
                        lepveto,
                        probeIndices=[0, 1, 2],
                        tagIndices=[[1, 2, 3], [2, 3], [3]],
                        data=dataSample,
                        normalized=True,
                        outname=basepath + "CaloDeepCSV/" + globalPrefix +
                        "_TnP_leading_calo_deepcsv",
                        label=[WPlabel])
            else:
                modules.TagNProbe.LeadingProbe(OffDeepCSVnthJet,
                                               MCsamplesIter,
                                               probeSel,
                                               tagSel,
                                               data=dataSample,
                                               convertIterSelection=True,
                                               outname=basepath +
                                               globalPrefix +
                                               "_TnP_leadingoff_deepcsv",
                                               normalized=True,
                                               label=[WPlabel],
                                               probeWeight=thisProbeWeight,
                                               tagWeight=thisTagWeight)
                modules.TagNProbe.LeadingProbe(
                    PFDeepCSVnthJet,
                    MCsamplesIter,
                    "{0} && {1}".format(probeSel,
                                        "offCleanJets_matchPF[?] >= 0"),
                    tagSel,
                    data=dataSample,
                    convertIterSelection=True,
                    outname=basepath + globalPrefix +
                    "_TnP_leading_pf_deepcsv",
                    normalized=True,
                    label=[WPlabel],
                    probeWeight=thisProbeWeight,
                    tagWeight=thisTagWeight)
                modules.TagNProbe.LeadingProbe(
                    CaloDeepCSVnthJet,
                    MCsamplesIter,
                    "{0} && {1}".format(probeSel,
                                        "offCleanJets_matchCalo[?] >= 0"),
                    tagSel,
                    data=dataSample,
                    convertIterSelection=True,
                    outname=basepath + globalPrefix +
                    "_TnP_leading_calo_deepcsv",
                    normalized=True,
                    label=[WPlabel],
                    probeWeight=thisProbeWeight,
                    tagWeight=thisTagWeight)

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

    logger.info("Runtime: {0:8f}s".format(time.time() - t0))
    logger.info("Closing falvour compostion analysis")
Example #4
0
def getBEfficiencyHack(PlotBaseObj,
                       Samples2Stack,
                       probeSelection,
                       tagSelection,
                       addNumeratorSel,
                       numWPs,
                       bindex,
                       probeSelVetoIter,
                       convertIterSelection=True,
                       probeIndices=[0],
                       tagIndices=[[1]],
                       data=None,
                       normalized=False,
                       drawRatio=True,
                       outname=None,
                       outputformat="pdf",
                       label=None,
                       savetable=True):
    import itertools

    results = {}

    outfile = ROOT.TFile(outname + "_histos.root", "RECREATE")
    initTagSelection = tagSelection
    initProbeSelection = probeSelection
    for WP in numWPs:
        results[WP] = {}
        logging.info("Processing WP: {0}".format(WP))
        #Getting denominator
        numWP = "{0} > {1}".format(addNumeratorSel, WP)

        StackSum_denom, StackHistos_denom, hData_denom = None, None, None
        StackSum_num, StackHistos_num, hData_num = None, None, None
        histosSet = False

        probeSelection = initProbeSelection

        prevProbes = []
        for iprobeIndex, probIndexElem in enumerate(probeIndices):
            logging.info(
                "Making combinations for Tag/Probe selection w/ ProbeIndex {0} and TagIndex {1}"
                .format(probIndexElem, tagIndices[iprobeIndex]))
            tagSelection = initTagSelection
            if len(prevProbes) > 0:
                probeSelection = probeSelection + " && (" + probeSelVetoIter.replace(
                    "?", str(prevProbes[-1])) + " != 0)"
                print "---------", probeSelVetoIter, prevProbes[-1]
            prevProbes.append(probIndexElem)
            for probeIndex, tagIndex in list(
                    itertools.product([probIndexElem],
                                      tagIndices[iprobeIndex])):
                logging.info(
                    "Making histos w/ ProbeIndex {0} & TagIndex {1}".format(
                        probeIndex, tagIndex))
                if not histosSet:
                    logging.debug("Setting histograms")
                    histosSet = True

                    StackSum_denom, StackHistos_denom, hData_denom = LeadingProbe(
                        PlotBaseObj,
                        Samples2Stack,
                        probeSelection,
                        tagSelection,
                        False,
                        convertIterSelection,
                        probeIndex,
                        tagIndex,
                        data,
                        skipPlots=True)
                    StackSum_num, StackHistos_num, hData_num = LeadingProbe(
                        PlotBaseObj,
                        Samples2Stack,
                        "{0} && {1}".format(probeSelection, numWP),
                        tagSelection,
                        False,
                        convertIterSelection,
                        probeIndex,
                        tagIndex,
                        data,
                        skipPlots=True)
                else:
                    logging.debug("Adding histograms")
                    StackSum_denom_temp, StackHistos_denom_temp, hData_denom_temp = LeadingProbe(
                        PlotBaseObj,
                        Samples2Stack,
                        probeSelection,
                        tagSelection,
                        False,
                        convertIterSelection,
                        probeIndex,
                        tagIndex,
                        data,
                        skipPlots=True)
                    StackSum_num_temp, StackHistos_num_temp, hData_num_temp = LeadingProbe(
                        PlotBaseObj,
                        Samples2Stack,
                        "{0} && {1}".format(probeSelection, numWP),
                        tagSelection,
                        False,
                        convertIterSelection,
                        probeIndex,
                        tagIndex,
                        data,
                        skipPlots=True)

                    StackSum_denom.Add(StackSum_denom_temp)
                    addHistoList(StackHistos_denom, StackHistos_denom_temp)
                    hData_denom.Add(hData_denom_temp)
                    StackSum_num.Add(StackSum_num_temp)
                    addHistoList(StackHistos_num, StackHistos_num_temp)
                    hData_num.Add(hData_num_temp)

                    del StackSum_denom_temp, StackHistos_denom_temp, hData_denom_temp, StackSum_num_temp, StackHistos_num_temp, hData_num_temp
                tagSelection = tagSelection + " && (" + tagSelection.replace(
                    "?", str(tagIndex)) + " != 0)"
                logging.debug("Adding:  && (" +
                              tagSelection.replace("?", str(tagIndex)) +
                              " != 0)")
        outfile.cd()
        ##Normalization to data
        MCscale_denom = hData_denom.Integral() / StackSum_denom.Integral()

        NormLabel = getLabel("MC normalized to Data", 0.6, scale=0.8)
        if label is None:
            label = [NormLabel]
        if label is isinstance(label, ROOT.TLatex):
            label = [label + NormLabel]
        else:
            label.append(NormLabel)

        logging.info("Scaling denom by {0}".format(MCscale_denom))
        StackSum_denom.Scale(MCscale_denom)
        for ihisto, histo in enumerate(StackHistos_denom):
            histo.Scale(MCscale_denom)

        #MCscale_num = hData_num.Integral()/StackSum_num.Integral()
        #logging.info("Scaling num by {0}".format(MCscale_num))
        StackSum_num.Scale(MCscale_denom)
        for ihisto, histo in enumerate(StackHistos_num):
            histo.Scale(MCscale_denom)

        calcEfficiencies(results, StackSum_denom, StackHistos_denom,
                         hData_denom, StackSum_num, StackHistos_num, hData_num,
                         PlotBaseObj, Samples2Stack, WP, bindex, data,
                         normalized, drawRatio, outname, outputformat, label,
                         savetable)

    if savetable:
        saveEffTable(numWPs, results, outname)
    outfile.Close()