Beispiel #1
0
def getPorbeHisto(PlotBaseObj, sample, xVarBase, selection, probeselection,
                  tagselection, isMC):
    logging.subinfo("Making tag histo for sample: {0}".format(sample.name))
    if isMC:
        weight = "{0} * {1}".format(sample.getSampleWeight(), sample.weight)
    else:
        weight = "1"
    logging.subdebug("Probe Selection: {0}".format(tagselection))
    sel = "{0} && {1} && {2}".format(selection, probeselection, tagselection)
    return modules.plotting.getHistoFromTree(sample.tree,
                                             xVarBase,
                                             PlotBaseObj.binning,
                                             sel,
                                             weight=weight)
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")
Beispiel #3
0
def makeSumWPComp(PlotBaseObj,
                  Sample,
                  numSelectionVarIter,
                  nIter,
                  WPs,
                  outname=None,
                  outputformat="pdf",
                  label=None,
                  drawHistos=False,
                  addSel="1",
                  saveGraph=False):
    styleconfig = SafeConfigParser()
    styleconfig.read("config/plotting.cfg")

    binning = PlotBaseObj.binning
    if label is not None and isinstance(label, list):
        for l in label:
            if not isinstance(l, ROOT.TLatex):
                logging.error(
                    "All labels are required to be of type ROOT.TLatex")
                label.remove(l)
    elif label is not None and not isinstance(label, ROOT.TLatex):
        logging.warning("The label param should be of type ROOT.TLatex")
        logging.warning("Ignoring additional label")
        label = None
    elif (label is not None
          and isinstance(label, ROOT.TLatex)) or label is None:
        pass
    else:
        logging.error("Something is happening here... :(")

    graphs = []
    forlegend = []

    colors = [
        ROOT.kRed, ROOT.kBlue, ROOT.kGreen - 2, ROOT.kOrange, ROOT.kPink,
        ROOT.kTeal
    ]

    if len(colors) < len(WPs):
        logging.warning(
            "less colors defined than WP. Will repeat colors for additional WPs"
        )
        while not len(colors) >= len(WPs):
            colors += colors

    for iWP, WP in enumerate(WPs):
        logging.subinfo("Making plot for WP: {0}".format(WP))

        numSelection = "{0} >= {1}".format(numSelectionVarIter, WP)
        logging.debug("Passing numSelection {0} to makeEffSumPlot()".format(
            numSelection))
        graphs.append(
            makeEffSumPlot(PlotBaseObj,
                           Sample,
                           numSelection,
                           nIter,
                           addSel=addSel,
                           forceColor=colors[iWP],
                           drawHistos=True,
                           drawEff=False,
                           outname=outname + "_" + str(WP)))
        forlegend.append((graphs[iWP], "WP: {0}".format(WP), "p"))

    canvas = modules.plotting.getCanvas()
    canvas.cd()

    logging.debug("Drawing graph 0")
    graphs[0].Draw("AP")
    graphs[0].GetHistogram().SetMaximum(
        styleconfig.getfloat("Efficiency", "yMax"))
    graphs[0].GetHistogram().SetMinimum(0.0)
    graphs[0].GetXaxis().SetRangeUser(binning[1], binning[2])
    graphs[0].Draw("AP")

    for i in range(1, len(graphs)):
        logging.debug("Drawing graph {0}".format(i))
        graphs[i].Draw("PSame")

    if label is not None:
        if isinstance(label, list):
            for l in label:
                l.Draw("same")
        else:
            label.Draw("same")
    CMSL1, CMSL2 = modules.utils.getCMStext()
    CMSL1.Draw("same")
    CMSL2.Draw("same")

    logging.debug("Making Legend")
    xstart = PlotBaseObj.LegendPosition[0]
    ystart = PlotBaseObj.LegendPosition[1]
    xend = PlotBaseObj.LegendPosition[2]
    yend = PlotBaseObj.LegendPosition[3]

    leg = modules.utils.getLegend(forlegend,
                                  xstart,
                                  ystart,
                                  xend,
                                  yend,
                                  usingPlotBase=False)
    leg.Draw("")

    if outname is not None:
        modules.utils.savePlot(canvas, outname, outputformat)

    if saveGraph:
        rout = ROOT.TFile(outname + ".root", "RECREATE")
        rout.cd()
        graphs[0].Write()
        rout.Close()
Beispiel #4
0
def LeadingProbe(PlotBaseObj,
                 Samples2Stack,
                 probeSelection,
                 tagSelection,
                 plottag=False,
                 convertIterSelection=True,
                 probeIndex=0,
                 tagIndex=1,
                 data=None,
                 normalized=False,
                 drawRatio=True,
                 outname=None,
                 outputformat="pdf",
                 label=None,
                 skipPlots=False):
    """
    Function for plotting a distribution of a porbe jet depending on a probe jet selection. Calls
    modules.DataMC.StackDMCPlotBase for plotting. 

    Parameters
    ----------
    PlotBaseObj : 
    Samples2Stack : list modules.classes.sample objects
    probeSelection : string
        Selection of the probe jets. I index of jet not fixed, use 
        convertIterSelection = True and the probeIndex to replace ?
        with the index
    tagSelection : string
        Selection of the tag jets. I index of jet not fixed, use 
        convertIterSelection = True and the tagIndex to replace ?
        with the index
    convertIterSelection : bool
        If true, selections are expected to have [?] instead of [index]
        which will be replaced with probeIndex or tagIndex.
    probeIndex : int
        Index of the probe Jet in the jet array
    tagIndex
        Index of the tag Jet in the jet array
    data : modules.classes.sample object
        If None is provide the sum of all stacked histograms (passed in
        Samples2Stack will be used as "data"
    """
    StackHistos = []
    StackSum = None

    if convertIterSelection:
        probeSelection = probeSelection.replace("?", str(probeIndex))
        tagSelection = tagSelection.replace("?", str(tagIndex))
        if plottag:
            xVarBase = PlotBaseObj.variable.replace("?", str(tagIndex))

        else:
            xVarBase = PlotBaseObj.variable.replace("?", str(probeIndex))
    else:
        xVarBase = PlotBaseObj.variable
    for isample, sample in enumerate(Samples2Stack):

        selection = "{0} && {1}".format(PlotBaseObj.selection,
                                        sample.selection).replace(
                                            "?", str(probeIndex))

        StackHistos.append(
            getPorbeHisto(PlotBaseObj,
                          sample,
                          xVarBase,
                          selection,
                          probeSelection,
                          tagSelection,
                          isMC=True))
        if isample == 0:
            logging.debug("Creating stacksum" + " with name " +
                          str(sample.name))
            StackSum = StackHistos[0].Clone()
            StackSum.SetName("Stacksum_" + StackSum.GetName())
            StackSum.SetTitle("Stacksum_" + StackSum.GetTitle())
        else:
            logging.debug("Adding histo to stacksum " + str(isample) +
                          " with name " + str(sample.name))
            StackSum.Add(StackHistos[isample])

    logging.subinfo("Stack histos finished")
    if data is None:
        logging.warning("No data set -> Setting data to Stacksum")
        hData = StackSum.Clone()
        hData.SetName("Data_" + StackSum.GetName())
    else:
        selection = "{0} && {1}".format(PlotBaseObj.selection,
                                        data.selection).replace(
                                            "?", str(probeIndex))
        hData = getPorbeHisto(PlotBaseObj,
                              data,
                              xVarBase,
                              selection,
                              probeSelection,
                              tagSelection,
                              isMC=False)

    if not skipPlots:
        modules.DataMC.StackDMCPlotBase(StackSum, StackHistos, hData,
                                        PlotBaseObj, Samples2Stack, data,
                                        normalized, drawRatio, outname,
                                        outputformat, label)

    return StackSum, StackHistos, hData
Beispiel #5
0
def LeadingProbe(PlotBaseObj,
                 Samples2Stack,
                 probeSelection,
                 tagSelection,
                 plottag=False,
                 convertIterSelection=True,
                 probeIndex=0,
                 tagIndex=1,
                 probeWeight="1",
                 tagWeight="1",
                 data=None,
                 normalized=False,
                 drawRatio=True,
                 outname=None,
                 outputformat="pdf",
                 label=None,
                 skipPlots=False):
    """
    Function for plotting a distribution of a porbe jet depending on a probe jet selection. Calls
    modules.DataMC.StackDMCPlotBase for plotting. 

    Parameters
    ----------
    PlotBaseObj : 
    Samples2Stack : list modules.classes.sample objects
    probeSelection : string
        Selection of the probe jets. I index of jet not fixed, use 
        convertIterSelection = True and the probeIndex to replace ?
        with the index
    tagSelection : string
        Selection of the tag jets. I index of jet not fixed, use 
        convertIterSelection = True and the tagIndex to replace ?
        with the index
    convertIterSelection : bool
        If true, selections are expected to have [?] instead of [index]
        which will be replaced with probeIndex or tagIndex.
    probeIndex : int
        Index of the probe Jet in the jet array
    tagIndex
        Index of the tag Jet in the jet array
    probeWeight : str
        Special weight applyied for the probe Jet. If not [?] in string, 
        "1" will be uesd. Genera lweights are expected to be set in the 
        sample definition
    tagWeight : str
        Special weight applyied for the tag Jet. If not [?] in string, 
        "1" will be uesd. Genera lweights are expected to be set in the 
        sample definition
    data : modules.classes.sample object
        If None is provide the sum of all stacked histograms (passed in
        Samples2Stack will be used as "data"
    """
    StackHistos = []
    StackSum = None

    if convertIterSelection:
        probeSelection = probeSelection.replace("?", str(probeIndex))
        tagSelection = tagSelection.replace("?", str(tagIndex))
        if plottag:
            xVarBase = PlotBaseObj.variable.replace("?", str(tagIndex))

        else:
            xVarBase = PlotBaseObj.variable.replace("?", str(probeIndex))
    else:
        xVarBase = PlotBaseObj.variable

    if "[?]" in probeWeight:
        probeWeight = probeWeight.replace("?", str(probeIndex))
    else:
        probeWeight = "1"
    if "[?]" in tagWeight:
        tagWeight = tagWeight.replace("?", str(tagIndex))
    else:
        tagWeight = "1"

    logging.info("Will use {0} as tag and {1} as probe weight".format(
        tagWeight, probeWeight))

    for isample, sample in enumerate(Samples2Stack):

        selection = "{0} && {1}".format(PlotBaseObj.selection,
                                        sample.selection).replace(
                                            "?", str(probeIndex))

        StackHistos.append(
            getPorbeHisto(PlotBaseObj,
                          sample,
                          xVarBase,
                          selection,
                          probeSelection,
                          tagSelection,
                          isMC=True,
                          addWeight="({0} * {1})".format(
                              probeWeight, tagWeight)))

    #for isample, sample in enumerate(Samples2Stack):
    #    print sample.name, StackHistos[isample], StackHistos[isample].Integral()#

    #raw_input(".........................")
    """ Merge all sample that end with the same string after _  """
    #print Samples2Stack
    mergedStackHistos = []
    samples2Merge = OrderedDict(
    )  # Dict with sample name postfix as key and list of indices as
    Samples2StackResorted = []
    for isample, sample in enumerate(Samples2Stack):
        print sample, sample.name, sample.color
        postfix = sample.name.split("_")[-1]
        if not postfix in samples2Merge:
            samples2Merge[postfix] = []
        samples2Merge[postfix].append(isample)
        Samples2StackResorted.append(sample)
    logging.debug("Merging sampes with same postfix")
    for merge in samples2Merge:  #Loop over gourps
        #print "-----------------"
        for iIndex, index in enumerate(samples2Merge[merge]):
            #iIndex: Number of histos in the current merge
            #index: index in the total list of samples
            #print merge, StackHistos[index].Integral()
            if iIndex == 0:
                logging.info("Creating merged histos" + " with postfix " +
                             str(merge))
                mergedHisto = StackHistos[index].Clone()
                mergedHisto.SetName("Merge_" + str(merge))
                mergedHisto.SetTitle("Merge_" + str(merge))
            else:
                logging.debug("Adding sample to merge")
                mergedHisto.Add(StackHistos[index])
        #print "--------",mergedHisto.Integral()
        mergedStackHistos.append(mergedHisto)
    # mergedStackHistos : Has len() of different sample name postfixes
    #print mergedStackHistos
    StackSum = None
    for imergedHisto, mergedHisto in enumerate(mergedStackHistos):
        if imergedHisto == 0:
            logging.debug("Creating stacksum" + " with name " +
                          str(sample.name))
            StackSum = mergedHisto.Clone()
            StackSum.SetName("Stacksum_" + StackSum.GetName())
            StackSum.SetTitle("Stacksum_" + StackSum.GetTitle())
        else:
            logging.debug("Adding histo to stacksum " + str(isample) +
                          " with name " + str(sample.name))
            StackSum.Add(mergedHisto)
        #print "++++++++++++++++****",StackSum.Integral()
    logging.subinfo("Stack histos finished")
    #print "StackSum:",StackSum.Integral()
    if data is None:
        logging.warning("No data set -> Setting data to Stacksum")
        hData = StackSum.Clone()
        hData.SetName("Data_" + StackSum.GetName())
    else:
        selection = "{0} && {1}".format(PlotBaseObj.selection,
                                        data.selection).replace(
                                            "?", str(probeIndex))
        hData = getPorbeHisto(PlotBaseObj,
                              data,
                              xVarBase,
                              selection,
                              probeSelection,
                              tagSelection,
                              isMC=False)
    print "StackSum:", StackSum.Integral()
    print "hData:", hData.Integral()

    if not skipPlots:
        logging.info("Will Start printing here")
        modules.DataMC.StackDMCPlotBase(StackSum, mergedStackHistos, hData,
                                        PlotBaseObj, Samples2StackResorted,
                                        data, normalized, drawRatio, outname,
                                        outputformat, label)

    print "StackSum:", StackSum.Integral()
    #raw_input(".........................")
    return StackSum, mergedStackHistos, hData