示例#1
0
def performScanForFile(file,
                       type,
                       maxSignalRange=None,
                       ignoreCS=True,
                       noScan=False):
    print "Performing Scan for file", file
    print "Called with ignoreCS", ignoreCS, "noScan", noScan
    scan = []
    basicCond = None
    if type == "2l":
        basicCond = str(
            utils.LUMINOSITY
        ) + " * Weight * ((leptons[1].Pt() <= 3.5 || deltaR < 0.3) && Met >= 200 && invMass < 30 && dilepBDT >= "
    else:
        basicCond = str(
            utils.LUMINOSITY
        ) + " * Weight * (Met >= 200 && invMass < 30 && dilepBDT >= "
    rootFile = TFile(file)
    c = rootFile.Get('tEvent')
    i = 0
    if noScan:
        print "Wanted no scan"
        dilepBDT = -0.3
        cond = basicCond + "{:.2f}".format(dilepBDT) + ")"
        hist = utils.getHistogramFromTree("sig_" + "{:.2f}".format(dilepBDT),
                                          c, observable, binsNumber, minX,
                                          maxX, cond, True)
        if not hist:
            print "WTF"
            exit(0)
        scan.append(hist)
    else:
        for dilepBDT in numpy.arange(-0.3, 0.6, 0.05):
            i += 1
            if maxSignalRange is not None and i > maxSignalRange:
                print "*** breaking scan because of signal at", i, "maxSignalRange=", maxSignalRange
                break
            cond = basicCond + "{:.2f}".format(dilepBDT) + ")"
            if not ignoreCS:
                cond += " * BranchingRatio"
            #print "cond=" + cond
            hist = utils.getHistogramFromTree(
                "sig_" + "{:.2f}".format(dilepBDT), c, observable, binsNumber,
                minX, maxX, cond, True)
            if not hist:
                print "WTF"
                exit(0)
            if hist.Integral() == 0:
                print "Stopping at value", dilepBDT
                break
            scan.append(hist)
    return scan
示例#2
0
def countBgInFiles(type, rootFiles, counts):
    for f in rootFiles:
        print f
        if os.path.basename(f) in ignore_bg_files:
            print "File", f, "in ignore list. Skipping..."
            continue
        rootFile = TFile(f)
        c = rootFile.Get('tEvent')
        #print c
        for bgReTagType in bgReTaggingOrder:
            #print bgReTagType
            drawString = hist_def["cond"] + " * " + bgReTagging[bgReTagType]
            hist = utils.getHistogramFromTree("tmp", c, hist_def["obs"],
                                              hist_def["bins"],
                                              hist_def["minX"],
                                              hist_def["maxX"], drawString,
                                              False)
            #print hist
            #print "{:.2f}".format(hist.Integral())
            if counts[type].get(bgReTagType) is None:
                counts[type][bgReTagType] = 0
            counts[type][bgReTagType] += hist.Integral()

        rootFile.Close()
示例#3
0
def main():
    print "Start: " + datetime.now().strftime('%d-%m-%Y %H:%M:%S')

    c1 = TCanvas("c1", "c1", 800, 800)
    c1.cd()

    signal_histograms = {}
    bg_histograms = {}
    significance = {}

    for filename in glob(bg_dir + "/*"):
        print "Opening", filename

        f = TFile(filename)
        c = f.Get('tEvent')

        # for CorrJetObs in utils.leptonIsolationList:
        #             ptRanges = [""]
        #             if CorrJetObs == "CorrJetIso":
        #                  ptRanges = utils.leptonCorrJetIsoPtRange
        #             for ptRange in ptRanges:
        #                 isoStr = CorrJetObs + str(ptRange)

        isoStr = defaultJetIsoSetting

        for lep in ["Muons", "Electrons"]:

            shortLep = "m"
            if lep == "Electrons":
                shortLep = "e"

            for category in categories:

                catStr = category["name"]
                catCond = category["condition"].replace("$LEPTONS",
                                                        lep).replace(
                                                            "$ISOSTR", isoStr)

                c1.cd()
                basename = os.path.basename(filename).split(".")[0]
                hist = utils.getHistogramFromTree(
                    basename + "exTrack_dilepBDT" + lep + isoStr, c,
                    "exTrack_dilepBDT" + isoStr, 10, -0.6, 0.7,
                    str(utils.LUMINOSITY) +
                    "* passedMhtMet6pack * tEffhMetMhtRealXMht2016 * Weight * (exclusiveTrack"
                    + isoStr +
                    " == 1 && MHT >= 220 && MET >= 200 && exTrack_invMass" +
                    isoStr +
                    " < 30 && BTagsDeepMedium == 0 && exclusiveTrackLeptonFlavour"
                    + isoStr + " == \"" + lep + "\" " + catCond + ")", True)
                hist.Sumw2()

                if bg_histograms.get(isoStr) is None:
                    bg_histograms[isoStr] = {}
                if bg_histograms[isoStr].get(catStr) is None:
                    bg_histograms[isoStr][catStr] = {}

                if bg_histograms[isoStr][catStr].get("t" + shortLep) is None:
                    bg_histograms[isoStr][catStr]["t" + shortLep] = hist
                else:
                    bg_histograms[isoStr][catStr]["t" + shortLep].Add(hist)

                for orth in (True, False):
                    if not orth:
                        hist = utils.getHistogramFromTree(
                            basename + "dilepBDT" + lep + isoStr, c,
                            "dilepBDT" + isoStr, 10, 0, 0.7,
                            str(utils.LUMINOSITY) +
                            "* passedMhtMet6pack * tEffhMetMhtRealXMht2016 * Weight * (twoLeptons"
                            + isoStr +
                            " == 1 && MHT >= 220 &&  MET >= 200 && invMass" +
                            isoStr + " < 12  && invMass" + isoStr +
                            " > 0.4 && !(invMass" + isoStr +
                            " > 3 && invMass" + isoStr +
                            " < 3.2) && !(invMass" + isoStr +
                            " > 0.75 && invMass" + isoStr +
                            " < 0.81) && dilepBDT" + isoStr +
                            " > 0 && BTagsDeepMedium == 0 && @leptons" +
                            isoStr + ".size() == 2 && leptonFlavour" + isoStr +
                            " == \"" + lep + "\" && sameSign" + isoStr +
                            " == 0  " + catCond + ")", True)
                    else:
                        hist = utils.getHistogramFromTree(
                            basename + "dilepBDT" + lep + isoStr, c,
                            "dilepBDT" + isoStr, 10, 0, 0.7,
                            str(utils.LUMINOSITY) +
                            "* passedMhtMet6pack * tEffhMetMhtRealXMht2016 * Weight * (twoLeptons"
                            + isoStr + " == 1 && (leptons" + isoStr +
                            "[1].Pt() <= 3.5 || deltaR" + isoStr +
                            " <= 0.3) && MHT >= 220 &&  MET >= 200 && invMass"
                            + isoStr + " < 12  && invMass" + isoStr +
                            " > 0.4 && !(invMass" + isoStr +
                            " > 3 && invMass" + isoStr +
                            " < 3.2) && !(invMass" + isoStr +
                            " > 0.75 && invMass" + isoStr +
                            " < 0.81) && dilepBDT" + isoStr +
                            " > 0 && BTagsDeepMedium == 0 && @leptons" +
                            isoStr + ".size() == 2 && leptonFlavour" + isoStr +
                            " == \"" + lep + "\" && sameSign" + isoStr +
                            " == 0  " + catCond + ")", True)
                    hist.Sumw2()
                    orthStr = "orth" if orth else "non-orth"
                    if bg_histograms[isoStr][catStr].get(shortLep +
                                                         shortLep) is None:
                        bg_histograms[isoStr][catStr][shortLep + shortLep] = {}
                    if bg_histograms[isoStr][catStr][shortLep + shortLep].get(
                            orthStr) is None:
                        bg_histograms[isoStr][catStr][shortLep +
                                                      shortLep][orthStr] = hist
                    else:
                        bg_histograms[isoStr][catStr][
                            shortLep + shortLep][orthStr].Add(hist)

        f.Close()

    print "Getting signals..."

    for filename in glob(signal_dir + "/*"):
        print "Opening", filename
        deltaM = os.path.basename(filename).split('_')[-1].split(
            'Chi20Chipm')[0].split('dm')[1]
        mu = os.path.basename(filename).split('_')[1].split('mu')[1]
        print "deltaM=" + deltaM
        print "mu=" + mu
        f = TFile(filename)
        c = f.Get('tEvent')

        if signal_histograms.get(mu) is None:
            signal_histograms[mu] = {}
            significance[mu] = {}
        if signal_histograms[mu].get(deltaM) is None:
            signal_histograms[mu][deltaM] = {}
            significance[mu][deltaM] = {}

        # for CorrJetObs in utils.leptonIsolationList:


#             ptRanges = [""]
#             if CorrJetObs == "CorrJetIso":
#                  ptRanges = utils.leptonCorrJetIsoPtRange
#             for ptRange in ptRanges:
#                 isoStr = CorrJetObs + str(ptRange)

        isoStr = defaultJetIsoSetting

        if signal_histograms[mu][deltaM].get(isoStr) is None:
            signal_histograms[mu][deltaM][isoStr] = {}
            significance[mu][deltaM][isoStr] = {}

        for lep in ["Muons", "Electrons"]:
            print mu, deltaM, isoStr, lep

            shortLep = "m"
            if lep == "Electrons":
                shortLep = "e"

            for category in categories:

                catStr = category["name"]
                catCond = category["condition"].replace("$LEPTONS",
                                                        lep).replace(
                                                            "$ISOSTR", isoStr)

                c1.cd()
                hist = utils.getHistogramFromTree(
                    mu + deltaM + isoStr + "_1t_" + lep, c,
                    "exTrack_dilepBDT" + isoStr, 10, -0.6, 0.7,
                    str(utils.LUMINOSITY) +
                    "* passedMhtMet6pack * tEffhMetMhtRealXMht2016 *  Weight * (exclusiveTrack"
                    + isoStr +
                    " == 1 && MHT >= 220 && MET >= 200 && exTrack_invMass" +
                    isoStr +
                    " < 30 && BTagsDeepMedium == 0 && exclusiveTrackLeptonFlavour"
                    + isoStr + " == \"" + lep + "\" " + catCond + ")", True)
                hist.Sumw2()

                if signal_histograms[mu][deltaM][isoStr].get(catStr) is None:
                    signal_histograms[mu][deltaM][isoStr][catStr] = {}
                    significance[mu][deltaM][isoStr][catStr] = {}

                signal_histograms[mu][deltaM][isoStr][catStr]["t" +
                                                              shortLep] = hist
                #sig = utils.calcSignificance(hist, bg_histograms[isoStr][catStr]["t" + shortLep], False)
                sig = utils.calcSignificanceNoAcc(
                    hist, bg_histograms[isoStr][catStr]["t" + shortLep], False)

                significance[mu][deltaM][isoStr][catStr]["t" + shortLep] = sig
                for orth in (True, False):
                    if not orth:
                        hist = utils.getHistogramFromTree(
                            mu + deltaM + isoStr + "_2l_" + lep, c,
                            "dilepBDT" + isoStr, 10, 0, 0.7,
                            str(utils.LUMINOSITY) +
                            "* passedMhtMet6pack * tEffhMetMhtRealXMht2016 * Weight * (twoLeptons"
                            + isoStr +
                            " == 1 && MHT >= 220 &&  MET >= 200 && invMass" +
                            isoStr + " < 12  && invMass" + isoStr +
                            " > 0.4 && !(invMass" + isoStr +
                            " > 3 && invMass" + isoStr +
                            " < 3.2) && !(invMass" + isoStr +
                            " > 0.75 && invMass" + isoStr +
                            " < 0.81) && dilepBDT" + isoStr +
                            " > 0 && BTagsDeepMedium == 0 && @leptons" +
                            isoStr + ".size() == 2 && leptonFlavour" + isoStr +
                            " == \"" + lep + "\" && sameSign" + isoStr +
                            " == 0 " + catCond + ")", True)
                    else:
                        hist = utils.getHistogramFromTree(
                            mu + deltaM + isoStr + "_2l_" + lep, c,
                            "dilepBDT" + isoStr, 10, 0, 0.7,
                            str(utils.LUMINOSITY) +
                            "* passedMhtMet6pack * tEffhMetMhtRealXMht2016 * Weight * (twoLeptons"
                            + isoStr + " == 1 && (leptons" + isoStr +
                            "[1].Pt() <= 3.5 || deltaR" + isoStr +
                            " <= 0.3) && MHT >= 220 &&  MET >= 200 && invMass"
                            + isoStr + " < 12  && invMass" + isoStr +
                            " > 0.4 && !(invMass" + isoStr +
                            " > 3 && invMass" + isoStr +
                            " < 3.2) && !(invMass" + isoStr +
                            " > 0.75 && invMass" + isoStr +
                            " < 0.81) && dilepBDT" + isoStr +
                            " > 0 && BTagsDeepMedium == 0 && @leptons" +
                            isoStr + ".size() == 2 && leptonFlavour" + isoStr +
                            " == \"" + lep + "\" && sameSign" + isoStr +
                            " == 0 " + catCond + ")", True)
                    hist.Sumw2()
                    orthStr = "orth" if orth else "non-orth"

                    if signal_histograms[mu][deltaM][isoStr][catStr].get(
                            shortLep + shortLep) is None:
                        signal_histograms[mu][deltaM][isoStr][catStr][
                            shortLep + shortLep] = {}
                        significance[mu][deltaM][isoStr][catStr][
                            shortLep + shortLep] = {}

                    signal_histograms[mu][deltaM][isoStr][catStr][
                        shortLep + shortLep][orthStr] = hist
                    sig = utils.calcSignificanceNoAcc(
                        hist, bg_histograms[isoStr][catStr][shortLep +
                                                            shortLep][orthStr],
                        False)
                    significance[mu][deltaM][isoStr][catStr][
                        shortLep + shortLep][orthStr] = sig

        f.Close()

    print significance

    categoriesCSV = ",".join(sorted(significance[mu][deltaM][isoStr].keys()))

    print "mu,deltaM,iso,category,ee,orth-ee,mm,orth-mm,te,tm"

    for mu in sorted(significance.keys()):
        for deltaM in sorted(significance[mu].keys()):
            for isoStr in sorted(significance[mu][deltaM].keys()):
                for catStr in sorted(significance[mu][deltaM][isoStr].keys()):
                    print ",".join(
                        (mu, deltaM, isoStr, catStr,
                         "{:.2f}".format(significance[mu][deltaM][isoStr]
                                         [catStr]["ee"]["non-orth"]),
                         "{:.2f}".format(significance[mu][deltaM][isoStr]
                                         [catStr]["ee"]["orth"]),
                         "{:.2f}".format(significance[mu][deltaM][isoStr]
                                         [catStr]["mm"]["non-orth"]),
                         "{:.2f}".format(significance[mu][deltaM][isoStr]
                                         [catStr]["mm"]["orth"]),
                         "{:.2f}".format(
                             significance[mu][deltaM][isoStr][catStr]["te"]),
                         "{:.2f}".format(
                             significance[mu][deltaM][isoStr][catStr]["tm"])))

    print "End: " + datetime.now().strftime('%d-%m-%Y %H:%M:%S')
示例#4
0
def main():
    print "Start: " + datetime.now().strftime('%d-%m-%Y %H:%M:%S')

    fnew = TFile(output_file, 'recreate')
    c1 = TCanvas("c1", "c1", 800, 800)
    c1.cd()

    i = 0

    print "Getting signals..."

    for signal_file in signals:
        filename = signal_dir + "/higgsino_" + signal_file + "Chi20Chipm.root"
        print "=====================================\n\n\n\n\n\n\n\n\n\n\n"
        print "Opening", filename
        if sam:
            deltaM = utils.getPointFromSamFileName(filename)
        else:
            print filename
            deltaM = utils.getPointFromFileName(filename)

        print "deltaM=" + deltaM
        f = TFile(filename)
        c = f.Get('tEvent')

        for lep in ["Muons", "Electrons"]:

            if lep == "Electrons":
                continue

            for iso in utils.leptonIsolationList:
                for cat in utils.leptonIsolationCategories:
                    ptRanges = [""]
                    drCuts = [""]
                    if iso == "CorrJetIso":
                        ptRanges = utils.leptonCorrJetIsoPtRange
                        drCuts = utils.leptonCorrJetIsoDrCuts
                    for ptRange in ptRanges:
                        for drCut in drCuts:
                            cuts = ""
                            if len(str(ptRange)) > 0:
                                cuts = str(ptRange) + "Dr" + str(drCut)
                            jetiso = iso + cuts + cat
                            c1.cd()

                            #1t category
                            #hist = utils.getHistogramFromTree(deltaM + "_1t_" + lep + "_" + jetiso, c, "exTrack_dilepBDT" + jetiso, bins, -1, 1, str(utils.LUMINOSITY) + "* passedMhtMet6pack * tEffhMetMhtRealXMht2016 *  Weight * BranchingRatio * (exclusiveTrack" + jetiso + " == 1 && MinDeltaPhiMetJets > 0.4 && MET >= 140 && MHT >= 220 && exTrack_invMass" + jetiso + " < 12 && BTagsDeepMedium == 0 && exclusiveTrackLeptonFlavour" + jetiso + " == \"" + lep + "\")", True)

                            # hist.Sumw2()
                            #                             fnew.cd()
                            #                             hist.Write()

                            orth_cond = " && (leptons" + jetiso + "[1].Pt() <= 3.5 || deltaR" + jetiso + " <= 0.3)"
                            orthOpt = [True, False
                                       ] if lep == "Muons" else [False]
                            isoCrs = [True, False
                                      ] if iso == "CorrJetIso" else [False]
                            for orth in orthOpt:
                                for isoCr in isoCrs:
                                    c1.cd()
                                    hist = utils.getHistogramFromTree(
                                        deltaM + "_2l_" +
                                        ("orth_" if orth else "") + lep + "_" +
                                        jetiso + ("_isoCr" if isoCr else ""),
                                        c, "dilepBDT" + jetiso, bins, -1, 1,
                                        str(utils.LUMINOSITY) +
                                        "* passedMhtMet6pack * tEffhMetMhtRealXMht2016 * Weight * BranchingRatio * (twoLeptons"
                                        + jetiso + " == 1 " +
                                        (orth_cond if orth else "") +
                                        " && MinDeltaPhiMetJets > 0.4 && MET >= 140 && MHT >= 220 && invMass"
                                        + jetiso + " < 12  && invMass" +
                                        jetiso + " > 0.4 && !(invMass" +
                                        jetiso + " > 3 && invMass" + jetiso +
                                        " < 3.2) && !(invMass" + jetiso +
                                        " > 0.75 && invMass" + jetiso +
                                        " < 0.81) && BTagsDeepMedium == 0 && vetoElectronsPassIso == 0 && vetoMuonsPassIso == 0 && leptonFlavour"
                                        + jetiso + " == \"" + lep +
                                        "\" && sameSign" + jetiso + " == 0 " +
                                        (("&& isoCr" + jetiso +
                                          (" >= 1" if isoCr else " == 0"))
                                         if len(cuts) > 0 else "") + ")", True)
                                    hist.Sumw2()
                                    fnew.cd()
                                    hist.Write()

        f.Close()

    bg_1t_hist = {}
    bg_2l_hist = {}

    print "Getting BG..."
    bg_files = [bg_slim_file]
    for filename in bg_files:  #glob(bg_dir + "/*"):
        print "=====================================\n\n\n\n\n\n\n\n\n\n\n"
        print "Opening", filename
        f = TFile(filename)
        c = f.Get('tEvent')

        for lep in ["Muons", "Electrons"]:

            if lep == "Electrons":
                continue

            for iso in utils.leptonIsolationList:
                for cat in utils.leptonIsolationCategories:
                    ptRanges = [""]
                    drCuts = [""]
                    if iso == "CorrJetIso":
                        ptRanges = utils.leptonCorrJetIsoPtRange
                        drCuts = utils.leptonCorrJetIsoDrCuts
                    for ptRange in ptRanges:
                        for drCut in drCuts:
                            cuts = ""
                            if len(str(ptRange)) > 0:
                                cuts = str(ptRange) + "Dr" + str(drCut)
                            jetiso = iso + cuts + cat

                            c1.cd()
                            basename = os.path.basename(filename).split(".")[0]
                            # prev
                            #hist = utils.getHistogramFromTree(basename, c, "exTrack_dilepBDT", 30, -0.6, 0.6, str(utils.LUMINOSITY) + "* passedMhtMet6pack * tEffhMetMhtRealXMht2016 * Weight * (exclusiveTrack == 1 && MHT >= 220 && MET >= 200 && exTrack_invMass < 30 && BTagsDeepMedium == 0  && exTrack_dilepBDT >= 0 && exclusiveTrackLeptonFlavour == \"" + lep + "\")", True)
                            # Making new version without trackBDT precut
                            # 1t category
                            # hist = utils.getHistogramFromTree("bg_1t_" + lep + "_" + jetiso, c, "exTrack_dilepBDT" + jetiso, bins, -1, 1, str(utils.LUMINOSITY) + "* passedMhtMet6pack * tEffhMetMhtRealXMht2016 *  Weight * BranchingRatio * (exclusiveTrack" + jetiso + " == 1 && MinDeltaPhiMetJets > 0.4 && MET >= 140 && MHT >= 220 && exTrack_invMass" + jetiso + " < 12 && BTagsDeepMedium == 0 && exclusiveTrackLeptonFlavour" + jetiso + " == \"" + lep + "\")", True)
                            #
                            #                             hist.Sumw2()
                            #                             if bg_1t_hist.get(lep) is None:
                            #                                 bg_1t_hist[lep + "_" + jetiso] = hist
                            #                             else:
                            #                                 bg_1t_hist[lep + "_" + jetiso].Add(hist)
                            #
                            orthOpt = [True, False
                                       ] if lep == "Muons" else [False]
                            orth_cond = " && (leptons" + jetiso + "[1].Pt() <= 3.5 || deltaR" + jetiso + " <= 0.3)"
                            isoCrs = [True, False
                                      ] if iso == "CorrJetIso" else [False]
                            print "isoCrs", isoCrs
                            for orth in orthOpt:
                                for isoCr in isoCrs:
                                    c1.cd()

                                    print("2l", lep, orth, jetiso, isoCr)
                                    hist = utils.getHistogramFromTree(
                                        "bg_2l_" + lep +
                                        ("_orth" if orth else "") + "_" +
                                        jetiso + ("_isoCr" if isoCr else ""),
                                        c, "dilepBDT" + jetiso, bins, -1, 1,
                                        str(utils.LUMINOSITY) +
                                        "* passedMhtMet6pack * tEffhMetMhtRealXMht2016 * Weight * BranchingRatio * (twoLeptons"
                                        + jetiso + " == 1 " +
                                        (orth_cond if orth else "") +
                                        " && MinDeltaPhiMetJets > 0.4 && MET >= 140 && MHT >= 220 && invMass"
                                        + jetiso + " < 12  && invMass" +
                                        jetiso + " > 0.4 && !(invMass" +
                                        jetiso + " > 3 && invMass" + jetiso +
                                        " < 3.2) && !(invMass" + jetiso +
                                        " > 0.75 && invMass" + jetiso +
                                        " < 0.81) && BTagsDeepMedium == 0 && vetoElectronsPassIso == 0 && vetoMuonsPassIso == 0 && leptonFlavour"
                                        + jetiso + " == \"" + lep +
                                        "\" && sameSign" + jetiso + " == 0 " +
                                        (("&& isoCr" + jetiso +
                                          (" >= 1" if isoCr else " == 0"))
                                         if len(cuts) > 0 else "") + ")", True)
                                    hist.Sumw2()
                                    if bg_2l_hist.get(lep + (
                                            "_orth" if orth else "")) is None:
                                        bg_2l_hist[
                                            lep + ("_orth" if orth else "") +
                                            "_" + jetiso +
                                            ("_isoCr" if isoCr else "")] = hist
                                    else:
                                        bg_2l_hist[lep +
                                                   ("_orth" if orth else "") +
                                                   "_" + jetiso +
                                                   ("_isoCr" if isoCr else
                                                    "")].Add(hist)
        f.Close()

    fnew.cd()
    for lep in ["Muons", "Electrons"]:
        if lep == "Electrons":
            continue
        for iso in utils.leptonIsolationList:
            for cat in utils.leptonIsolationCategories:
                ptRanges = [""]
                drCuts = [""]
                if iso == "CorrJetIso":
                    ptRanges = utils.leptonCorrJetIsoPtRange
                    drCuts = utils.leptonCorrJetIsoDrCuts
                for ptRange in ptRanges:
                    for drCut in drCuts:
                        cuts = ""
                        if len(str(ptRange)) > 0:
                            cuts = str(ptRange) + "Dr" + str(drCut)
                        jetiso = iso + cuts + cat
                        #bg_1t_hist[lep + "_" + jetiso].Write("bg_1t_" + lep + "_" + jetiso)
                        orthOpt = [True, False] if lep == "Muons" else [False]
                        isoCrs = [True, False
                                  ] if iso == "CorrJetIso" else [False]
                        for orth in orthOpt:
                            for isoCr in isoCrs:
                                bg_2l_hist[lep + ("_orth" if orth else "") +
                                           "_" + jetiso +
                                           ("_isoCr" if isoCr else "")].Write(
                                               "bg_2l_" +
                                               ("orth_" if orth else "") +
                                               lep + "_" + jetiso +
                                               ("_isoCr" if isoCr else ""))
    fnew.Close()

    exit(0)
示例#5
0
def main():
    print "Start: " + datetime.now().strftime('%d-%m-%Y %H:%M:%S')

    significance = {}
    for type in ["1t1l", "2l"]:
        bgHistograms = {}
        signalHistograms = {}
        bgHistograms[type] = {}
        signalHistograms[type] = {}
        significance[type] = {}
        trainGroupsToCheck = utils.trainGroupsOrder + ["all", "1t1l", "2l"]
        if sam:
            trainGroupsToCheck = ["all", "1t1l", "2l"]
        for trainGroup in trainGroupsToCheck:
            print "Checking train group " + trainGroup

            signalHistograms[type][trainGroup] = {}

            maxSignalRange = 0

            groups = None
            if trainGroup in ["all", "1t1l", "2l"]:
                groups = [trainGroup]
            else:
                groups = utils.trainGroups[trainGroup]

            for group in groups:
                print "Checking group", group
                signalFilesPath = None
                if group in ["all", "1t1l", "2l"]:
                    if not sam:
                        signalFilesPath = paths[
                            type] + "/signal/skim_dilepton_signal_bdt_all/single/*"
                    else:
                        signalFilesPath = paths[
                            type] + "/signal/skim_sam_dilepton_signal_bdt_all/single/*"
                else:
                    signalFilesPath = paths[
                        type] + "/signal/skim_dilepton_signal_bdt/single/*" + group + "*"
                #signalFiles = glob(signalFilesPath + "mChipm100GeV_dm2p26GeV_.root")
                signalFiles = glob(signalFilesPath)
                for signalFile in signalFiles:
                    signalFileName = os.path.basename(signalFile).split(".")[0]
                    print "Scanning file", signalFileName
                    if group in ["1t1l", "2l"]:
                        if group != type:
                            continue
                        cond = str(
                            utils.LUMINOSITY
                        ) + " * Weight * (Met >= 200 && invMass < 30 && " + cuts[
                            group] + ")"
                        if sam:
                            cond += " * BranchingRatio"
                        print "opening file", signalFile, "with cond ", cond
                        rootFile = TFile(signalFile)
                        c = rootFile.Get('tEvent')
                        hist = utils.getHistogramFromTree(
                            "sig_" + group, c, observable, binsNumber, minX,
                            maxX, cond, True)
                        signalHistograms[type][trainGroup][
                            signalFileName] = hist
                    else:
                        ignoreCS = True
                        noScan = False
                        if sam:
                            ignoreCS = False
                        if observable == "dilepBDT":
                            noScan = True
                        print "calling scan with ignoreCS", ignoreCS, " noScan", noScan
                        signalHistograms[type][trainGroup][
                            signalFileName] = performScanForFile(
                                signalFile, type, None, ignoreCS, noScan)
                        if len(signalHistograms[type][trainGroup]
                               [signalFileName]) > maxSignalRange:
                            maxSignalRange = len(signalHistograms[type]
                                                 [trainGroup][signalFileName])

            print "Maximum signal range for", trainGroup, " is", maxSignalRange
            bgFilesPath = None
            if trainGroup in ["1t1l", "2l"]:
                bgFilesPath = paths[
                    type] + "/bg/skim_dilepton_signal_bdt/all/single/*"
            else:
                bgFilesPath = paths[
                    type] + "/bg/skim_dilepton_signal_bdt/" + trainGroup + "/single/*"
            #print "Checking files", bgFilesPath
            print "bgFilesPath", bgFilesPath
            bgFiles = glob(bgFilesPath)

            for bgFile in bgFiles:
                print "Scanning", bgFile
                if trainGroup in ["1t1l", "2l"]:
                    print ">>>>>>> trainGroup", trainGroup
                    if trainGroup != type:
                        print "Skipping trainGroup", trainGroup, "for type", type
                        continue
                    print "******* BG trainGroup", trainGroup
                    cond = str(
                        utils.LUMINOSITY
                    ) + " * Weight * (Met >= 200 && invMass < 30 && " + cuts[
                        group] + ")"
                    rootFile = TFile(bgFile)
                    c = rootFile.Get('tEvent')
                    hist = utils.getHistogramFromTree("bg_" + group, c,
                                                      observable, binsNumber,
                                                      minX, maxX, cond, True)
                    if bgHistograms[type].get(trainGroup) is None:
                        bgHistograms[type][trainGroup] = hist
                    else:
                        bgHistograms[type][trainGroup].Add(hist)
                else:
                    print "maxSignalRange", maxSignalRange
                    scan = performScanForFile(bgFile, type, maxSignalRange)
                    if bgHistograms[type].get(trainGroup) is None:
                        bgHistograms[type][trainGroup] = scan
                    else:
                        for i in range(len(scan)):
                            if i >= len(bgHistograms[type][trainGroup]):
                                print "Expending scan"
                                bgHistograms[type][trainGroup].append(scan[i])
                            else:
                                bgHistograms[type][trainGroup][i].Add(scan[i])
            if bgHistograms[type].get(trainGroup) is None:
                print "bgHistograms not ready for", trainGroup
                continue
            bgScan = bgHistograms[type][trainGroup]
            ignoreCS = False
            if sam:
                ignoreCS = True

            for signalFileName in signalHistograms[type][trainGroup]:
                if trainGroup in ["1t1l", "2l"]:
                    if trainGroup != type:
                        continue
                    sigHist = signalHistograms[type][trainGroup][
                        signalFileName]
                    bgHist = bgHistograms[type][trainGroup]
                    sig = 0
                    if sig_method == "rsb":
                        sig = utils.calcSignificance(sigHist, bgHist, ignoreCS)
                    else:
                        sig = utils.calcSignificanceLlhdSingleCount(
                            sigHist, bgHist)

                    #if sig > 0:
                    #    print "Greater!", sig, "for rect", signalFileName
                    if significance[type].get(trainGroup) is None:
                        significance[type][trainGroup] = {}
                    print "Adding to significance file", signalFileName
                    significance[type][trainGroup][signalFileName] = sig
                    #print "After addition"
                    #print significance
                else:
                    #print "Calculating Significance for", signalFileName
                    sigScan = []
                    dilepBDT = -0.3
                    i = 0
                    for sigHist in signalHistograms[type][trainGroup][
                            signalFileName]:
                        #print "dilepBDT=" + str(dilepBDT)
                        i += 1
                        dilepBDT += 0.05
                        if i > len(bgHistograms[type][trainGroup]):
                            print "Breaking scan at ", dilepBDT
                            break
                        bgHist = bgHistograms[type][trainGroup][i - 1]
                        sig = 0
                        if sig_method == "rsb":
                            sig = utils.calcSignificance(
                                sigHist, bgHist, ignoreCS)
                        else:
                            sig = utils.calcSignificanceLlhdSingleCount(
                                sigHist, bgHist)
                        #if sig > 0:
                        #    print "Greater!", sig, "for", type, signalFileName
                        sigScan.append(sig)

                    print "sigScan for", trainGroup, signalFileName, sigScan  #, "min", min(sigScan)
                    if significance[type].get(trainGroup) is None:
                        significance[type][trainGroup] = {}
                    if len(sigScan) == 0:
                        significance[type][trainGroup][signalFileName] = 0
                    else:
                        if sig_method == "rsb":
                            significance[type][trainGroup][
                                signalFileName] = max(sigScan)
                        else:
                            significance[type][trainGroup][
                                signalFileName] = min(sigScan)

    #significance = {'2l': {'2l': {'mChipm160GeV_dm0p44GeV_': 0.0, 'mChipm180GeV_dm5p29GeV_': 0.07551929728621258, 'mChipm200GeV_dm4p3GeV_': 0.02253163914333506, 'mChipm500GeV_dm0p53GeV_': 0.0, 'mChipm115GeV_dm6p27GeV_': 0.0, 'mChipm140GeV_dm4p28GeV_': 0.1261404896783124, 'mChipm200GeV_dm2p3GeV_': 0.028682721618256173, 'mChipm500GeV_dm2p33GeV_': 0.004089909531025812, 'mChipm115GeV_dm0p77GeV_': 0.23881328391373574, 'mChipm225GeV_dm0p8GeV_': 0.0, 'mChipm180GeV_dm6p29GeV_': 0.0, 'mChipm180GeV_dm0p59GeV_': 0.0, 'mChipm500GeV_dm5p33GeV_': 0.01577980402980185, 'mChipm160GeV_dm5p29GeV_': 0.36226096466070085, 'mChipm225GeV_dm2p3GeV_': 0.049369290394491554, 'mChipm100GeV_dm0p56GeV_': 0.0, 'mChipm180GeV_dm3p29GeV_': 0.8072665615521555, 'mChipm200GeV_dm6p3GeV_': 0.0, 'mChipm160GeV_dm3p29GeV_': 0.7631769821910923, 'mChipm180GeV_dm4p29GeV_': 0.19636477138395744, 'mChipm500GeV_dm1p03GeV_': 0.0, 'mChipm100GeV_dm0p96GeV_': 3.7680687765478655, 'mChipm500GeV_dm0p43GeV_': 0.0, 'mChipm250GeV_dm2p31GeV_': 0.04189351264856475, 'mChipm250GeV_dm0p61GeV_': 0.0, 'mChipm140GeV_dm1p78GeV_': 1.2165583079457931, 'mChipm115GeV_dm3p27GeV_': 2.3973607707258218, 'mChipm160GeV_dm1p29GeV_': 0.037799969783616535, 'mChipm200GeV_dm5p3GeV_': 0.03292756643109106, 'mChipm100GeV_dm0p41GeV_': 0.28336538493352775, 'mChipm115GeV_dm0p97GeV_': 0.6086150101226576, 'mChipm200GeV_dm1p0GeV_': 0.14582837352966277, 'mChipm140GeV_dm2p28GeV_': 0.7068266418892982, 'mChipm100GeV_dm2p26GeV_': 4.340224000828833, 'mChipm500GeV_dm4p33GeV_': 0.0032268627781113198, 'mChipm180GeV_dm0p49GeV_': 0.0, 'mChipm100GeV_dm6p26GeV_': 0.0, 'mChipm300GeV_dm0p46GeV_': 0.0, 'mChipm300GeV_dm1p31GeV_': 0.0, 'mChipm100GeV_dm4p26GeV_': 0.3806607613236031, 'mChipm160GeV_dm0p49GeV_': 0.07600709055800008, 'mChipm180GeV_dm1p79GeV_': 0.225089079018234, 'mChipm160GeV_dm2p29GeV_': 0.21201043661030672, 'mChipm140GeV_dm0p78GeV_': 0.0, 'mChipm400GeV_dm0p224GeV_': 0.004597262189298272, 'mChipm160GeV_dm1p79GeV_': 0.37047103377666624, 'mChipm200GeV_dm1p3GeV_': 0.3375344434279763, 'mChipm250GeV_dm4p31GeV_': 0.013176525166881524, 'mChipm100GeV_dm0p26GeV_': 0.0, 'mChipm300GeV_dm0p21GeV_': 0.0, 'mChipm250GeV_dm5p31GeV_': 0.045647177611062684, 'mChipm180GeV_dm2p29GeV_': 0.11234394971217414, 'mChipm180GeV_dm1p29GeV_': 0.42372219067547034, 'mChipm140GeV_dm5p28GeV_': 0.10032151486684084, 'mChipm115GeV_dm0p17GeV_': 0.0, 'mChipm500GeV_dm1p83GeV_': 0.0026669096581372034, 'mChipm115GeV_dm0p57GeV_': 0.0, 'mChipm115GeV_dm5p27GeV_': 0.09688162261455202, 'mChipm100GeV_dm0p76GeV_': 0.19418615944436338, 'mChipm115GeV_dm4p27GeV_': 0.5238589751879157, 'mChipm275GeV_dm0p312GeV_': 0.0, 'mChipm225GeV_dm0p45GeV_': 0.0, 'mChipm500GeV_dm1p33GeV_': 0.0, 'mChipm500GeV_dm3p33GeV_': 0.001357813409102185, 'mChipm500GeV_dm0p23GeV_': 0.0, 'mChipm100GeV_dm5p26GeV_': 4.060382235597963, 'mChipm140GeV_dm3p28GeV_': 1.213870824763569, 'mChipm250GeV_dm1p81GeV_': 0.00926684327213774, 'mChipm225GeV_dm0p2GeV_': 0.0, 'mChipm115GeV_dm2p27GeV_': 2.7408137466856104, 'mChipm300GeV_dm4p31GeV_': 0.011351442815145597, 'mChipm100GeV_dm1p76GeV_': 1.2753790791651751, 'mChipm100GeV_dm3p26GeV_': 1.1815185419462095, 'mChipm115GeV_dm1p27GeV_': 0.23823609659168726, 'mChipm275GeV_dm0p412GeV_': 0.0, 'mChipm225GeV_dm3p3GeV_': 0.1940271508125967, 'mChipm140GeV_dm0p98GeV_': 0.09956232656197977, 'mChipm160GeV_dm4p29GeV_': 0.3816257955321174, 'mChipm300GeV_dm3p31GeV_': 0.02548883786126789, 'mChipm250GeV_dm3p31GeV_': 0.0385846046701078, 'mChipm140GeV_dm0p28GeV_': 0.0, 'mChipm140GeV_dm0p38GeV_': 0.0, 'mChipm200GeV_dm3p3GeV_': 0.38279455194688944, 'mChipm300GeV_dm1p81GeV_': 0.014281247025868842, 'mChipm250GeV_dm1p01GeV_': 0.0, 'mChipm275GeV_dm0p212GeV_': 0.0, 'mChipm160GeV_dm0p99GeV_': 0.0, 'mChipm225GeV_dm1p8GeV_': 0.02195257572389453, 'mChipm500GeV_dm0p48GeV_': 0.0, 'mChipm300GeV_dm2p31GeV_': 0.07633037842893708, 'mChipm200GeV_dm1p8GeV_': 0.08427907461400076, 'mChipm225GeV_dm0p3GeV_': 0.0, 'mChipm115GeV_dm1p77GeV_': 0.6218820227342045, 'mChipm200GeV_dm0p8GeV_': 0.0, 'mChipm100GeV_dm1p26GeV_': 3.769446906945395, 'mChipm225GeV_dm1p0GeV_': 0.014243411827769453}, 'all': {'mChipm160GeV_dm0p44GeV_': 0, 'mChipm180GeV_dm5p29GeV_': 0, 'mChipm200GeV_dm4p3GeV_': 0, 'mChipm500GeV_dm0p53GeV_': 0, 'mChipm115GeV_dm6p27GeV_': 0, 'mChipm140GeV_dm4p28GeV_': 0, 'mChipm200GeV_dm2p3GeV_': 0, 'mChipm500GeV_dm2p33GeV_': 0, 'mChipm115GeV_dm0p77GeV_': 0, 'mChipm225GeV_dm0p8GeV_': 0, 'mChipm180GeV_dm6p29GeV_': 0, 'mChipm180GeV_dm0p59GeV_': 0, 'mChipm500GeV_dm5p33GeV_': 0, 'mChipm160GeV_dm5p29GeV_': 0, 'mChipm225GeV_dm2p3GeV_': 0, 'mChipm100GeV_dm0p56GeV_': 0, 'mChipm180GeV_dm3p29GeV_': 0, 'mChipm200GeV_dm6p3GeV_': 0, 'mChipm160GeV_dm3p29GeV_': 0, 'mChipm180GeV_dm4p29GeV_': 0, 'mChipm500GeV_dm1p03GeV_': 0, 'mChipm100GeV_dm0p96GeV_': 0, 'mChipm500GeV_dm0p43GeV_': 0, 'mChipm250GeV_dm2p31GeV_': 0, 'mChipm250GeV_dm0p61GeV_': 0, 'mChipm140GeV_dm1p78GeV_': 0, 'mChipm115GeV_dm3p27GeV_': 0, 'mChipm160GeV_dm1p29GeV_': 0, 'mChipm200GeV_dm5p3GeV_': 0, 'mChipm100GeV_dm0p41GeV_': 0, 'mChipm115GeV_dm0p97GeV_': 0, 'mChipm200GeV_dm1p0GeV_': 0, 'mChipm140GeV_dm2p28GeV_': 0, 'mChipm100GeV_dm2p26GeV_': 0, 'mChipm500GeV_dm4p33GeV_': 0, 'mChipm180GeV_dm0p49GeV_': 0, 'mChipm100GeV_dm6p26GeV_': 0, 'mChipm300GeV_dm0p46GeV_': 0, 'mChipm300GeV_dm1p31GeV_': 0, 'mChipm100GeV_dm4p26GeV_': 0, 'mChipm160GeV_dm0p49GeV_': 0, 'mChipm180GeV_dm1p79GeV_': 0, 'mChipm160GeV_dm2p29GeV_': 0, 'mChipm140GeV_dm0p78GeV_': 0, 'mChipm400GeV_dm0p224GeV_': 0, 'mChipm160GeV_dm1p79GeV_': 0, 'mChipm200GeV_dm1p3GeV_': 0, 'mChipm250GeV_dm4p31GeV_': 0, 'mChipm100GeV_dm0p26GeV_': 0, 'mChipm300GeV_dm0p21GeV_': 0, 'mChipm250GeV_dm5p31GeV_': 0, 'mChipm180GeV_dm2p29GeV_': 0, 'mChipm180GeV_dm1p29GeV_': 0, 'mChipm140GeV_dm5p28GeV_': 0, 'mChipm115GeV_dm0p17GeV_': 0, 'mChipm500GeV_dm1p83GeV_': 0, 'mChipm115GeV_dm0p57GeV_': 0, 'mChipm115GeV_dm5p27GeV_': 0, 'mChipm100GeV_dm0p76GeV_': 0, 'mChipm115GeV_dm4p27GeV_': 0, 'mChipm275GeV_dm0p312GeV_': 0, 'mChipm225GeV_dm0p45GeV_': 0, 'mChipm500GeV_dm1p33GeV_': 0, 'mChipm500GeV_dm3p33GeV_': 0, 'mChipm500GeV_dm0p23GeV_': 0, 'mChipm100GeV_dm5p26GeV_': 0, 'mChipm140GeV_dm3p28GeV_': 0, 'mChipm250GeV_dm1p81GeV_': 0, 'mChipm225GeV_dm0p2GeV_': 0, 'mChipm115GeV_dm2p27GeV_': 0, 'mChipm300GeV_dm4p31GeV_': 0, 'mChipm100GeV_dm1p76GeV_': 0, 'mChipm100GeV_dm3p26GeV_': 0, 'mChipm115GeV_dm1p27GeV_': 0, 'mChipm275GeV_dm0p412GeV_': 0, 'mChipm225GeV_dm3p3GeV_': 0, 'mChipm140GeV_dm0p98GeV_': 0, 'mChipm160GeV_dm4p29GeV_': 0, 'mChipm300GeV_dm3p31GeV_': 0, 'mChipm250GeV_dm3p31GeV_': 0, 'mChipm140GeV_dm0p28GeV_': 0, 'mChipm140GeV_dm0p38GeV_': 0, 'mChipm200GeV_dm3p3GeV_': 0, 'mChipm300GeV_dm1p81GeV_': 0, 'mChipm250GeV_dm1p01GeV_': 0, 'mChipm275GeV_dm0p212GeV_': 0, 'mChipm160GeV_dm0p99GeV_': 0, 'mChipm225GeV_dm1p8GeV_': 0, 'mChipm500GeV_dm0p48GeV_': 0, 'mChipm300GeV_dm2p31GeV_': 0, 'mChipm200GeV_dm1p8GeV_': 0, 'mChipm225GeV_dm0p3GeV_': 0, 'mChipm115GeV_dm1p77GeV_': 0, 'mChipm200GeV_dm0p8GeV_': 0, 'mChipm100GeV_dm1p26GeV_': 0, 'mChipm225GeV_dm1p0GeV_': 0}}, '1t1l': {'all': {'mChipm160GeV_dm0p44GeV_': 0, 'mChipm180GeV_dm5p29GeV_': 0, 'mChipm200GeV_dm4p3GeV_': 0, 'mChipm115GeV_dm0p42GeV_': 0, 'mChipm500GeV_dm0p53GeV_': 0, 'mChipm115GeV_dm6p27GeV_': 0, 'mChipm140GeV_dm4p28GeV_': 0, 'mChipm180GeV_dm0p44GeV_': 0, 'mChipm200GeV_dm2p3GeV_': 0, 'mChipm400GeV_dm0p174GeV_': 0, 'mChipm300GeV_dm0p31GeV_': 0, 'mChipm115GeV_dm0p27GeV_': 0, 'mChipm500GeV_dm2p33GeV_': 0, 'mChipm250GeV_dm0p81GeV_': 0, 'mChipm115GeV_dm0p77GeV_': 0, 'mChipm225GeV_dm0p8GeV_': 0, 'mChipm180GeV_dm6p29GeV_': 0, 'mChipm180GeV_dm0p59GeV_': 0, 'mChipm200GeV_dm0p3GeV_': 0, 'mChipm500GeV_dm0p48GeV_': 0, 'mChipm160GeV_dm5p29GeV_': 0, 'mChipm225GeV_dm2p3GeV_': 0, 'mChipm100GeV_dm0p56GeV_': 0, 'mChipm400GeV_dm0p424GeV_': 0, 'mChipm225GeV_dm0p5GeV_': 0, 'mChipm200GeV_dm0p4GeV_': 0, 'mChipm160GeV_dm3p29GeV_': 0, 'mChipm200GeV_dm6p3GeV_': 0, 'mChipm100GeV_dm0p46GeV_': 0, 'mChipm140GeV_dm0p78GeV_': 0, 'mChipm500GeV_dm1p03GeV_': 0, 'mChipm100GeV_dm0p96GeV_': 0, 'mChipm500GeV_dm0p43GeV_': 0, 'mChipm250GeV_dm2p31GeV_': 0, 'mChipm250GeV_dm0p61GeV_': 0, 'mChipm200GeV_dm0p6GeV_': 0, 'mChipm300GeV_dm0p41GeV_': 0, 'mChipm100GeV_dm6p26GeV_': 0, 'mChipm100GeV_dm0p36GeV_': 0, 'mChipm180GeV_dm0p19GeV_': 0, 'mChipm115GeV_dm3p27GeV_': 0, 'mChipm160GeV_dm1p29GeV_': 0, 'mChipm180GeV_dm0p39GeV_': 0, 'mChipm200GeV_dm5p3GeV_': 0, 'mChipm100GeV_dm0p41GeV_': 0, 'mChipm180GeV_dm4p29GeV_': 0, 'mChipm200GeV_dm1p0GeV_': 0, 'mChipm160GeV_dm0p29GeV_': 0, 'mChipm140GeV_dm2p28GeV_': 0, 'mChipm225GeV_dm0p4GeV_': 0, 'mChipm100GeV_dm2p26GeV_': 0, 'mChipm500GeV_dm4p33GeV_': 0, 'mChipm180GeV_dm0p49GeV_': 0, 'mChipm200GeV_dm0p5GeV_': 0, 'mChipm140GeV_dm1p78GeV_': 0, 'mChipm300GeV_dm0p46GeV_': 0, 'mChipm300GeV_dm1p31GeV_': 0, 'mChipm100GeV_dm4p26GeV_': 0, 'mChipm160GeV_dm0p49GeV_': 0, 'mChipm160GeV_dm0p79GeV_': 0, 'mChipm140GeV_dm1p28GeV_': 0, 'mChipm180GeV_dm1p79GeV_': 0, 'mChipm160GeV_dm2p29GeV_': 0, 'mChipm500GeV_dm0p63GeV_': 0, 'mChipm200GeV_dm0p45GeV_': 0, 'mChipm100GeV_dm0p16GeV_': 0, 'mChipm400GeV_dm0p224GeV_': 0, 'mChipm160GeV_dm1p79GeV_': 0, 'mChipm200GeV_dm1p3GeV_': 0, 'mChipm250GeV_dm4p31GeV_': 0, 'mChipm160GeV_dm0p39GeV_': 0, 'mChipm100GeV_dm0p26GeV_': 0, 'mChipm300GeV_dm0p21GeV_': 0, 'mChipm250GeV_dm5p31GeV_': 0, 'mChipm500GeV_dm1p83GeV_': 0, 'mChipm180GeV_dm2p29GeV_': 0, 'mChipm180GeV_dm1p29GeV_': 0, 'mChipm300GeV_dm0p81GeV_': 0, 'mChipm140GeV_dm5p28GeV_': 0, 'mChipm115GeV_dm0p17GeV_': 0, 'mChipm140GeV_dm0p18GeV_': 0, 'mChipm160GeV_dm0p19GeV_': 0, 'mChipm250GeV_dm1p31GeV_': 0, 'mChipm300GeV_dm1p01GeV_': 0, 'mChipm115GeV_dm0p57GeV_': 0, 'mChipm225GeV_dm1p3GeV_': 0, 'mChipm115GeV_dm5p27GeV_': 0, 'mChipm100GeV_dm0p76GeV_': 0, 'mChipm180GeV_dm0p29GeV_': 0, 'mChipm115GeV_dm4p27GeV_': 0, 'mChipm275GeV_dm0p312GeV_': 0, 'mChipm225GeV_dm0p45GeV_': 0, 'mChipm500GeV_dm1p33GeV_': 0, 'mChipm140GeV_dm0p48GeV_': 0, 'mChipm500GeV_dm3p33GeV_': 0, 'mChipm180GeV_dm0p79GeV_': 0, 'mChipm500GeV_dm0p23GeV_': 0, 'mChipm140GeV_dm0p58GeV_': 0, 'mChipm250GeV_dm0p31GeV_': 0, 'mChipm140GeV_dm3p28GeV_': 0, 'mChipm250GeV_dm1p81GeV_': 0, 'mChipm300GeV_dm0p61GeV_': 0, 'mChipm140GeV_dm0p43GeV_': 0, 'mChipm225GeV_dm0p2GeV_': 0, 'mChipm500GeV_dm0p83GeV_': 0, 'mChipm115GeV_dm2p27GeV_': 0, 'mChipm300GeV_dm4p31GeV_': 0, 'mChipm100GeV_dm1p76GeV_': 0, 'mChipm100GeV_dm3p26GeV_': 0, 'mChipm115GeV_dm1p27GeV_': 0, 'mChipm250GeV_dm0p51GeV_': 0, 'mChipm275GeV_dm0p412GeV_': 0, 'mChipm225GeV_dm3p3GeV_': 0, 'mChipm140GeV_dm0p98GeV_': 0, 'mChipm160GeV_dm4p29GeV_': 0, 'mChipm300GeV_dm3p31GeV_': 0, 'mChipm250GeV_dm3p31GeV_': 0, 'mChipm140GeV_dm0p28GeV_': 0, 'mChipm140GeV_dm0p38GeV_': 0, 'mChipm200GeV_dm3p3GeV_': 0, 'mChipm225GeV_dm0p6GeV_': 0, 'mChipm300GeV_dm1p81GeV_': 0, 'mChipm200GeV_dm1p8GeV_': 0, 'mChipm115GeV_dm0p47GeV_': 0, 'mChipm250GeV_dm1p01GeV_': 0, 'mChipm275GeV_dm0p212GeV_': 0, 'mChipm160GeV_dm0p99GeV_': 0, 'mChipm225GeV_dm1p8GeV_': 0, 'mChipm200GeV_dm0p2GeV_': 0, 'mChipm250GeV_dm0p46GeV_': 0, 'mChipm300GeV_dm2p31GeV_': 0, 'mChipm160GeV_dm0p59GeV_': 0, 'mChipm100GeV_dm5p26GeV_': 0, 'mChipm225GeV_dm0p3GeV_': 0, 'mChipm115GeV_dm0p97GeV_': 0, 'mChipm115GeV_dm1p77GeV_': 0, 'mChipm400GeV_dm0p324GeV_': 0, 'mChipm250GeV_dm0p21GeV_': 0, 'mChipm275GeV_dm0p512GeV_': 0, 'mChipm115GeV_dm0p37GeV_': 0, 'mChipm200GeV_dm0p8GeV_': 0, 'mChipm250GeV_dm0p41GeV_': 0, 'mChipm180GeV_dm0p99GeV_': 0, 'mChipm100GeV_dm1p26GeV_': 0, 'mChipm180GeV_dm3p29GeV_': 0, 'mChipm500GeV_dm5p33GeV_': 0, 'mChipm300GeV_dm0p51GeV_': 0, 'mChipm225GeV_dm1p0GeV_': 0}, '1t1l': {'mChipm160GeV_dm0p44GeV_': 0.00689757023462127, 'mChipm180GeV_dm5p29GeV_': 0.2837101250699646, 'mChipm200GeV_dm4p3GeV_': 0.21310144060427808, 'mChipm115GeV_dm0p42GeV_': 0.024554306591202706, 'mChipm500GeV_dm0p53GeV_': 0.0, 'mChipm115GeV_dm6p27GeV_': 0.610358958538046, 'mChipm140GeV_dm4p28GeV_': 0.5880484712140863, 'mChipm180GeV_dm0p44GeV_': 0.04973370834660533, 'mChipm200GeV_dm2p3GeV_': 0.162799422460962, 'mChipm400GeV_dm0p174GeV_': 0.0005336768787590724, 'mChipm300GeV_dm0p31GeV_': 0.0, 'mChipm115GeV_dm0p27GeV_': 0.0, 'mChipm500GeV_dm2p33GeV_': 0.0029452433331537365, 'mChipm250GeV_dm0p81GeV_': 0.0016680055927431505, 'mChipm115GeV_dm0p77GeV_': 0.3391636657443909, 'mChipm225GeV_dm0p8GeV_': 0.00126772470194455, 'mChipm180GeV_dm6p29GeV_': 0.2518175568964451, 'mChipm180GeV_dm0p59GeV_': 0.0, 'mChipm200GeV_dm0p3GeV_': 0.002125153632053275, 'mChipm500GeV_dm0p48GeV_': 0.0009737148788201116, 'mChipm160GeV_dm5p29GeV_': 0.3999095381718706, 'mChipm225GeV_dm2p3GeV_': 0.08175157463760614, 'mChipm100GeV_dm0p56GeV_': 0.055026624466963846, 'mChipm400GeV_dm0p424GeV_': 0.0009098416019210147, 'mChipm225GeV_dm0p5GeV_': 0.0, 'mChipm200GeV_dm0p4GeV_': 0.0, 'mChipm160GeV_dm3p29GeV_': 0.35495048236846305, 'mChipm200GeV_dm6p3GeV_': 0.7140755849536564, 'mChipm100GeV_dm0p46GeV_': 0.027888666159752867, 'mChipm140GeV_dm0p78GeV_': 0.04445395591428412, 'mChipm500GeV_dm1p03GeV_': 0.0006060033909069968, 'mChipm100GeV_dm0p96GeV_': 0.06970351865541863, 'mChipm500GeV_dm0p43GeV_': 0.0006300043816300224, 'mChipm250GeV_dm2p31GeV_': 0.0633367864035943, 'mChipm250GeV_dm0p61GeV_': 0.008892649217381213, 'mChipm200GeV_dm0p6GeV_': 0.0030635761869421177, 'mChipm300GeV_dm0p41GeV_': 0.0, 'mChipm100GeV_dm6p26GeV_': 1.4317321858786187, 'mChipm100GeV_dm0p36GeV_': 0.10103394693147388, 'mChipm180GeV_dm0p19GeV_': 0.015157961118278707, 'mChipm115GeV_dm3p27GeV_': 1.0142200482821984, 'mChipm160GeV_dm1p29GeV_': 0.1894590369225231, 'mChipm180GeV_dm0p39GeV_': 0.0032324413514466027, 'mChipm200GeV_dm5p3GeV_': 0.19249654066028407, 'mChipm100GeV_dm0p41GeV_': 0.1595761724129806, 'mChipm180GeV_dm4p29GeV_': 0.3481060072443589, 'mChipm200GeV_dm1p0GeV_': 0.020033248688189155, 'mChipm160GeV_dm0p29GeV_': 0.0, 'mChipm140GeV_dm2p28GeV_': 0.37717236914250946, 'mChipm225GeV_dm0p4GeV_': 0.0, 'mChipm100GeV_dm2p26GeV_': 1.2167551793742268, 'mChipm500GeV_dm4p33GeV_': 0.008454900693100429, 'mChipm180GeV_dm0p49GeV_': 0.0, 'mChipm200GeV_dm0p5GeV_': 0.04754653512072733, 'mChipm140GeV_dm1p78GeV_': 0.2918415789502378, 'mChipm300GeV_dm0p46GeV_': 0.0, 'mChipm300GeV_dm1p31GeV_': 0.004692227375344473, 'mChipm100GeV_dm4p26GeV_': 1.6495460195488063, 'mChipm160GeV_dm0p49GeV_': 0.0, 'mChipm160GeV_dm0p79GeV_': 0.0, 'mChipm140GeV_dm1p28GeV_': 0.12018200036858953, 'mChipm180GeV_dm1p79GeV_': 0.1228160803691891, 'mChipm160GeV_dm2p29GeV_': 0.3501282997932912, 'mChipm500GeV_dm0p63GeV_': 0.0007180772149953693, 'mChipm200GeV_dm0p45GeV_': 0.010876557167119862, 'mChipm100GeV_dm0p16GeV_': 0.2843975913620334, 'mChipm400GeV_dm0p224GeV_': 0.00023245178810405904, 'mChipm160GeV_dm1p79GeV_': 0.1899874946064316, 'mChipm200GeV_dm1p3GeV_': 0.04618254732663303, 'mChipm250GeV_dm4p31GeV_': 0.1160603406043768, 'mChipm160GeV_dm0p39GeV_': 0.0, 'mChipm100GeV_dm0p26GeV_': 0.0, 'mChipm300GeV_dm0p21GeV_': 0.007295490527942999, 'mChipm250GeV_dm5p31GeV_': 0.0967719332439017, 'mChipm500GeV_dm1p83GeV_': 0.0014869976443930707, 'mChipm180GeV_dm2p29GeV_': 0.20443686778205689, 'mChipm180GeV_dm1p29GeV_': 0.055952349229885456, 'mChipm300GeV_dm0p81GeV_': 0.0, 'mChipm140GeV_dm5p28GeV_': 0.6596841399933816, 'mChipm115GeV_dm0p17GeV_': 0.24499326278453798, 'mChipm140GeV_dm0p18GeV_': 0.0, 'mChipm160GeV_dm0p19GeV_': 0.0, 'mChipm250GeV_dm1p31GeV_': 0.015853232182233126, 'mChipm300GeV_dm1p01GeV_': 0.0041915689423126365, 'mChipm115GeV_dm0p57GeV_': 0.03722525022287641, 'mChipm225GeV_dm1p3GeV_': 0.0255843883720325, 'mChipm115GeV_dm5p27GeV_': 0.8024951654117263, 'mChipm100GeV_dm0p76GeV_': 0.05090245822367015, 'mChipm180GeV_dm0p29GeV_': 0.022736942520432502, 'mChipm115GeV_dm4p27GeV_': 0.9513375930353846, 'mChipm275GeV_dm0p312GeV_': 0.0, 'mChipm225GeV_dm0p45GeV_': 0.018272745793591467, 'mChipm500GeV_dm1p33GeV_': 0.0009574207088834994, 'mChipm140GeV_dm0p48GeV_': 0.0, 'mChipm500GeV_dm3p33GeV_': 0.006584109481913894, 'mChipm180GeV_dm0p79GeV_': 0.011937751653069456, 'mChipm500GeV_dm0p23GeV_': 0.003437623253865544, 'mChipm140GeV_dm0p58GeV_': 0.0, 'mChipm250GeV_dm0p31GeV_': 0.013248720171257077, 'mChipm140GeV_dm3p28GeV_': 0.6889671298609716, 'mChipm250GeV_dm1p81GeV_': 0.047989456450687876, 'mChipm300GeV_dm0p61GeV_': 0.0006985947814219381, 'mChipm140GeV_dm0p43GeV_': 0.0, 'mChipm225GeV_dm0p2GeV_': 0.0, 'mChipm500GeV_dm0p83GeV_': 0.00017603846417799167, 'mChipm115GeV_dm2p27GeV_': 0.7912670519126788, 'mChipm300GeV_dm4p31GeV_': 0.05604614685420679, 'mChipm100GeV_dm1p76GeV_': 0.892124083370378, 'mChipm100GeV_dm3p26GeV_': 1.4780178492596412, 'mChipm115GeV_dm1p27GeV_': 0.4245890550598446, 'mChipm250GeV_dm0p51GeV_': 0.006177013682524181, 'mChipm275GeV_dm0p412GeV_': 0.0, 'mChipm225GeV_dm3p3GeV_': 0.12581635826569101, 'mChipm140GeV_dm0p98GeV_': 0.04625133487556135, 'mChipm160GeV_dm4p29GeV_': 0.45568183971904785, 'mChipm300GeV_dm3p31GeV_': 0.04336339787981361, 'mChipm250GeV_dm3p31GeV_': 0.08782077427436072, 'mChipm140GeV_dm0p28GeV_': 0.0, 'mChipm140GeV_dm0p38GeV_': 0.008756618065151201, 'mChipm200GeV_dm3p3GeV_': 0.2497739909649137, 'mChipm225GeV_dm0p6GeV_': 0.001980616957871743, 'mChipm300GeV_dm1p81GeV_': 0.014640761178061342, 'mChipm200GeV_dm1p8GeV_': 0.07774894786469205, 'mChipm115GeV_dm0p47GeV_': 0.24096605922274159, 'mChipm250GeV_dm1p01GeV_': 0.0034448377443317157, 'mChipm275GeV_dm0p212GeV_': 0.0064236655326765195, 'mChipm160GeV_dm0p99GeV_': 0.09562270100666934, 'mChipm225GeV_dm1p8GeV_': 0.06189328571357645, 'mChipm200GeV_dm0p2GeV_': 0.010708103588948869, 'mChipm250GeV_dm0p46GeV_': 0.0, 'mChipm300GeV_dm2p31GeV_': 0.0394059971002393, 'mChipm160GeV_dm0p59GeV_': 0.02795619578109419, 'mChipm100GeV_dm5p26GeV_': 1.0384989223731345, 'mChipm225GeV_dm0p3GeV_': 0.0, 'mChipm115GeV_dm0p97GeV_': 0.23038821451912864, 'mChipm115GeV_dm1p77GeV_': 0.739966030349064, 'mChipm400GeV_dm0p324GeV_': 0.0008132286344158288, 'mChipm250GeV_dm0p21GeV_': 0.0, 'mChipm275GeV_dm0p512GeV_': 0.0006795995780545999, 'mChipm115GeV_dm0p37GeV_': 0.0, 'mChipm200GeV_dm0p8GeV_': 0.08767552989928645, 'mChipm250GeV_dm0p41GeV_': 0.006457787007036022, 'mChipm180GeV_dm0p99GeV_': 0.012685200276902895, 'mChipm100GeV_dm1p26GeV_': 0.32056337343048613, 'mChipm180GeV_dm3p29GeV_': 0.32567967521915997, 'mChipm500GeV_dm5p33GeV_': 0.009159146797849446, 'mChipm300GeV_dm0p51GeV_': 0.0, 'mChipm225GeV_dm1p0GeV_': 0.005959051266021664}}}
    #print "\n\n\n\n"
    #print significance["1t1l"]["all"]

    print "=============="
    print "\n\n\n\n"
    print significance
    print " "
    print " "
    print " "
    if sam:
        for signalFileName in sorted(significance["1t1l"]["all"]):
            fileNameParts = signalFileName.split("_")
            mu = "mu" + fileNameParts[0].split("mChipm")[1].split("GeV")[0]
            dm = fileNameParts[1].split("GeV")[0]
            if significance["1t1l"]["all"].get(signalFileName) is None:
                significance["1t1l"]["all"][signalFileName] = 0
            if significance["1t1l"]["1t1l"].get(signalFileName) is None:
                significance["1t1l"]["1t1l"][signalFileName] = 0
            if significance["2l"]["all"].get(signalFileName) is None:
                significance["2l"]["all"][signalFileName] = 0
            if significance["2l"]["2l"].get(signalFileName) is None:
                significance["2l"]["2l"][signalFileName] = 0
            print mu, dm, "{:.2f}".format(0.0), "{:.2f}".format(
                significance["1t1l"]["all"][signalFileName]), "{:.2f}".format(
                    significance["1t1l"]["1t1l"][signalFileName]
                ), "{:.2f}".format(0.0), "{:.2f}".format(
                    significance["2l"]["all"][signalFileName]
                ), "{:.2f}".format(
                    significance["2l"]["2l"][signalFileName]
                ), "{:.2f}".format(0.0), "{:.2f}".format(
                    math.sqrt((significance["2l"]["all"][signalFileName])**2 +
                              (significance["1t1l"]["all"][signalFileName])**2)
                ), "{:.2f}".format(
                    math.sqrt((significance["2l"]["2l"][signalFileName])**2 + (
                        significance["1t1l"]["1t1l"][signalFileName])**2))
    else:
        for trainGroup in significance["1t1l"]:
            if trainGroup in ["all", "1t1l", "2l"]:
                continue
            for signalFileName in sorted(significance["1t1l"][trainGroup]):
                fileNameParts = signalFileName.split("_")
                mu = fileNameParts[1]
                dm = fileNameParts[2].split("Chi")[0]
                print mu, dm, "{:.2f}".format(
                    significance["1t1l"][trainGroup][signalFileName]
                ), "{:.2f}".format(
                    significance["1t1l"]["all"][signalFileName]
                ), "{:.2f}".format(
                    significance["1t1l"]["1t1l"][signalFileName]
                ), "{:.2f}".format(
                    significance["2l"][trainGroup][signalFileName]
                ), "{:.2f}".format(
                    significance["2l"]["all"][signalFileName]
                ), "{:.2f}".format(
                    significance["2l"]["2l"][signalFileName]
                ), "{:.2f}".format(
                    math.sqrt(
                        (significance["1t1l"][trainGroup][signalFileName])**2 +
                        (significance["2l"][trainGroup][signalFileName])**2)
                ), "{:.2f}".format(
                    math.sqrt((significance["2l"]["all"][signalFileName])**2 +
                              (significance["1t1l"]["all"][signalFileName])**2)
                ), "{:.2f}".format(
                    math.sqrt((significance["2l"]["2l"][signalFileName])**2 + (
                        significance["1t1l"]["1t1l"][signalFileName])**2))

    # c1 = TCanvas("c1", "c1", 800, 800)


#     c1.SetBottomMargin(0.16)
#     c1.SetLeftMargin(0.18)
#     c1.cd()
#     histPad = c1
#     histPad.Draw()
#     c1.Print("output.pdf[");
#     sigHist = signalHistograms["1t1l"]["low"]["higgsino_mu100_dm4p30Chi20Chipm"][10]
#     bgHist = bgHistograms["1t1l"]["low"][10]
#     utils.formatHist(bgHist, utils.signalCp[1], 0.8)
#     bgHist.Draw("hist")
#     sigHist.Draw("hist same")
#     c1.Print("output.pdf");
#     c1.Print("output.pdf]");
#     sig = utils.calcSignificance(sigHist, [bgHist])
#     print "sig=", sig

#     trainGroups = {
#     "dm0" : ["dm0p"],
#     "dm1" : ["dm1p"],
#     "low" : ["dm2p", "dm3p", "dm4p"],
#     "dm7" : ["dm7p"],
#     "dm9" : ["dm9p"],
#     "high" : ["dm12p", "dm13p"]
# }
#
# trainGroupsOrder = ["dm0", "dm1", "low", "dm7", "dm9", "high"]

    print "End: " + datetime.now().strftime('%d-%m-%Y %H:%M:%S')
    exit(0)
def main():
    print "Start: " + datetime.now().strftime('%d-%m-%Y %H:%M:%S')

    c1 = TCanvas("c1", "c1", 800, 800)
    c1.cd()

    signal_histograms = {}
    bg_histograms = {}
    significance = {}

    for filename in glob(bg_dir + "/*"):
        print "Opening", filename

        f = TFile(filename)
        c = f.Get('tEvent')

        for CorrJetObs in utils.leptonIsolationList:
            ptRanges = [""]
            if CorrJetObs == "CorrJetIso":
                ptRanges = utils.leptonCorrJetIsoPtRange
            for ptRange in ptRanges:
                isoStr = CorrJetObs + str(ptRange)

                for lep in ["Muons", "Electrons"]:

                    shortLep = "m"
                    if lep == "Electrons":
                        shortLep = "e"

                    for veto in (True, False):

                        vetoStr = "veto" if veto else "no-veto"
                        vetoCond = "" if not veto else "&& veto" + lep + isoStr + " == 0"

                        c1.cd()
                        basename = os.path.basename(filename).split(".")[0]
                        hist = utils.getHistogramFromTree(
                            basename + "exTrack_dilepBDT" + lep + isoStr, c,
                            "exTrack_dilepBDT" + isoStr, 10, -0.6, 0.7,
                            str(utils.LUMINOSITY) +
                            "* passedMhtMet6pack * tEffhMetMhtRealXMht2016 * Weight * (exclusiveTrack"
                            + isoStr +
                            " == 1 && MHT >= 220 && MET >= 200 && exTrack_invMass"
                            + isoStr +
                            " < 30 && BTagsDeepMedium == 0 && exclusiveTrackLeptonFlavour"
                            + isoStr + " == \"" + lep + "\" " + vetoCond + ")",
                            True)
                        hist.Sumw2()

                        if bg_histograms.get(isoStr) is None:
                            bg_histograms[isoStr] = {}
                        if bg_histograms[isoStr].get(vetoStr) is None:
                            bg_histograms[isoStr][vetoStr] = {}

                        if bg_histograms[isoStr][vetoStr].get(
                                "t" + shortLep) is None:
                            bg_histograms[isoStr][vetoStr]["t" +
                                                           shortLep] = hist
                        else:
                            bg_histograms[isoStr][vetoStr]["t" +
                                                           shortLep].Add(hist)

                        for orth in (True, False):
                            if not orth:
                                hist = utils.getHistogramFromTree(
                                    basename + "dilepBDT" + lep + isoStr, c,
                                    "dilepBDT" + isoStr, 10, 0, 0.7,
                                    str(utils.LUMINOSITY) +
                                    "* passedMhtMet6pack * tEffhMetMhtRealXMht2016 * Weight * (twoLeptons"
                                    + isoStr +
                                    " == 1 && MHT >= 220 &&  MET >= 200 && invMass"
                                    + isoStr + " < 12  && invMass" + isoStr +
                                    " > 0.4 && !(invMass" + isoStr +
                                    " > 3 && invMass" + isoStr +
                                    " < 3.2) && !(invMass" + isoStr +
                                    " > 0.75 && invMass" + isoStr +
                                    " < 0.81) && dilepBDT" + isoStr +
                                    " > 0 && BTagsDeepMedium == 0 && @leptons"
                                    + isoStr +
                                    ".size() == 2 && leptonFlavour" + isoStr +
                                    " == \"" + lep + "\" && sameSign" +
                                    isoStr + " == 0  " + vetoCond + ")", True)
                            else:
                                hist = utils.getHistogramFromTree(
                                    basename + "dilepBDT" + lep + isoStr, c,
                                    "dilepBDT" + isoStr, 10, 0, 0.7,
                                    str(utils.LUMINOSITY) +
                                    "* passedMhtMet6pack * tEffhMetMhtRealXMht2016 * Weight * (twoLeptons"
                                    + isoStr + " == 1 && (leptons" + isoStr +
                                    "[1].Pt() <= 3.5 || deltaR" + isoStr +
                                    " <= 0.3) && MHT >= 220 &&  MET >= 200 && invMass"
                                    + isoStr + " < 12  && invMass" + isoStr +
                                    " > 0.4 && !(invMass" + isoStr +
                                    " > 3 && invMass" + isoStr +
                                    " < 3.2) && !(invMass" + isoStr +
                                    " > 0.75 && invMass" + isoStr +
                                    " < 0.81) && dilepBDT" + isoStr +
                                    " > 0 && BTagsDeepMedium == 0 && @leptons"
                                    + isoStr +
                                    ".size() == 2 && leptonFlavour" + isoStr +
                                    " == \"" + lep + "\" && sameSign" +
                                    isoStr + " == 0  " + vetoCond + ")", True)
                            hist.Sumw2()
                            orthStr = "orth" if orth else "non-orth"
                            if bg_histograms[isoStr][vetoStr].get(
                                    shortLep + shortLep) is None:
                                bg_histograms[isoStr][vetoStr][shortLep +
                                                               shortLep] = {}
                            if bg_histograms[isoStr][vetoStr][
                                    shortLep + shortLep].get(orthStr) is None:
                                bg_histograms[isoStr][vetoStr][
                                    shortLep + shortLep][orthStr] = hist
                            else:
                                bg_histograms[isoStr][vetoStr][
                                    shortLep + shortLep][orthStr].Add(hist)

        f.Close()

    print "Getting signals..."

    for filename in glob(signal_dir + "/*"):
        print "Opening", filename
        deltaM = os.path.basename(filename).split('_')[-1].split(
            'Chi20Chipm')[0].split('dm')[1]
        mu = os.path.basename(filename).split('_')[1].split('mu')[1]
        print "deltaM=" + deltaM
        print "mu=" + mu
        f = TFile(filename)
        c = f.Get('tEvent')

        if signal_histograms.get(mu) is None:
            signal_histograms[mu] = {}
            significance[mu] = {}
        if signal_histograms[mu].get(deltaM) is None:
            signal_histograms[mu][deltaM] = {}
            significance[mu][deltaM] = {}

        for CorrJetObs in utils.leptonIsolationList:
            ptRanges = [""]
            if CorrJetObs == "CorrJetIso":
                ptRanges = utils.leptonCorrJetIsoPtRange
            for ptRange in ptRanges:
                isoStr = CorrJetObs + str(ptRange)
                if signal_histograms[mu][deltaM].get(isoStr) is None:
                    signal_histograms[mu][deltaM][isoStr] = {}
                    significance[mu][deltaM][isoStr] = {}

                for lep in ["Muons", "Electrons"]:
                    print mu, deltaM, isoStr, lep

                    shortLep = "m"
                    if lep == "Electrons":
                        shortLep = "e"

                    for veto in (True, False):

                        vetoStr = "veto" if veto else "no-veto"
                        vetoCond = "" if not veto else "&& veto" + lep + isoStr + " == 0"

                        c1.cd()
                        hist = utils.getHistogramFromTree(
                            mu + deltaM + isoStr + "_1t_" + lep, c,
                            "exTrack_dilepBDT" + isoStr, 10, -0.6, 0.7,
                            str(utils.LUMINOSITY) +
                            "* passedMhtMet6pack * tEffhMetMhtRealXMht2016 *  Weight * (exclusiveTrack"
                            + isoStr +
                            " == 1 && MHT >= 220 && MET >= 200 && exTrack_invMass"
                            + isoStr +
                            " < 30 && BTagsDeepMedium == 0 && exclusiveTrackLeptonFlavour"
                            + isoStr + " == \"" + lep + "\" " + vetoCond + ")",
                            True)
                        hist.Sumw2()

                        if signal_histograms[mu][deltaM][isoStr].get(
                                vetoStr) is None:
                            signal_histograms[mu][deltaM][isoStr][vetoStr] = {}
                            significance[mu][deltaM][isoStr][vetoStr] = {}

                        signal_histograms[mu][deltaM][isoStr][vetoStr][
                            "t" + shortLep] = hist
                        #sig = utils.calcSignificance(hist, bg_histograms[isoStr][vetoStr]["t" + shortLep], False)
                        sig = utils.calcSignificanceNoAcc(
                            hist,
                            bg_histograms[isoStr][vetoStr]["t" + shortLep],
                            False)

                        significance[mu][deltaM][isoStr][vetoStr][
                            "t" + shortLep] = sig
                        for orth in (True, False):
                            if not orth:
                                hist = utils.getHistogramFromTree(
                                    mu + deltaM + isoStr + "_2l_" + lep, c,
                                    "dilepBDT" + isoStr, 10, 0, 0.7,
                                    str(utils.LUMINOSITY) +
                                    "* passedMhtMet6pack * tEffhMetMhtRealXMht2016 * Weight * (twoLeptons"
                                    + isoStr +
                                    " == 1 && MHT >= 220 &&  MET >= 200 && invMass"
                                    + isoStr + " < 12  && invMass" + isoStr +
                                    " > 0.4 && !(invMass" + isoStr +
                                    " > 3 && invMass" + isoStr +
                                    " < 3.2) && !(invMass" + isoStr +
                                    " > 0.75 && invMass" + isoStr +
                                    " < 0.81) && dilepBDT" + isoStr +
                                    " > 0 && BTagsDeepMedium == 0 && @leptons"
                                    + isoStr +
                                    ".size() == 2 && leptonFlavour" + isoStr +
                                    " == \"" + lep + "\" && sameSign" +
                                    isoStr + " == 0 " + vetoCond + ")", True)
                            else:
                                hist = utils.getHistogramFromTree(
                                    mu + deltaM + isoStr + "_2l_" + lep, c,
                                    "dilepBDT" + isoStr, 10, 0, 0.7,
                                    str(utils.LUMINOSITY) +
                                    "* passedMhtMet6pack * tEffhMetMhtRealXMht2016 * Weight * (twoLeptons"
                                    + isoStr + " == 1 && (leptons" + isoStr +
                                    "[1].Pt() <= 3.5 || deltaR" + isoStr +
                                    " <= 0.3) && MHT >= 220 &&  MET >= 200 && invMass"
                                    + isoStr + " < 12  && invMass" + isoStr +
                                    " > 0.4 && !(invMass" + isoStr +
                                    " > 3 && invMass" + isoStr +
                                    " < 3.2) && !(invMass" + isoStr +
                                    " > 0.75 && invMass" + isoStr +
                                    " < 0.81) && dilepBDT" + isoStr +
                                    " > 0 && BTagsDeepMedium == 0 && @leptons"
                                    + isoStr +
                                    ".size() == 2 && leptonFlavour" + isoStr +
                                    " == \"" + lep + "\" && sameSign" +
                                    isoStr + " == 0 " + vetoCond + ")", True)
                            hist.Sumw2()
                            orthStr = "orth" if orth else "non-orth"

                            if signal_histograms[mu][deltaM][isoStr][
                                    vetoStr].get(shortLep + shortLep) is None:
                                signal_histograms[mu][deltaM][isoStr][vetoStr][
                                    shortLep + shortLep] = {}
                                significance[mu][deltaM][isoStr][vetoStr][
                                    shortLep + shortLep] = {}

                            signal_histograms[mu][deltaM][isoStr][vetoStr][
                                shortLep + shortLep][orthStr] = hist
                            sig = utils.calcSignificanceNoAcc(
                                hist, bg_histograms[isoStr][vetoStr][
                                    shortLep + shortLep][orthStr], False)
                            significance[mu][deltaM][isoStr][vetoStr][
                                shortLep + shortLep][orthStr] = sig

        f.Close()

    print significance

    print "End: " + datetime.now().strftime('%d-%m-%Y %H:%M:%S')
def main():
    print "Start: " + datetime.now().strftime('%d-%m-%Y %H:%M:%S')
    print "End: " + datetime.now().strftime('%d-%m-%Y %H:%M:%S')

    c1 = TCanvas("c1", "c1", 800, 800)
    c1.SetLeftMargin(0.13)
    #c1.SetRightMargin(0.02)
    c1.SetGridx()
    c1.SetGridy()

    hist = None

    samples = "sim"
    #samples = "data"

    select_jpsi = True

    if select_jpsi and samples == "sim":
        #basicCond = "tagJpsi == 1 && probeJpsi == 1 && tracks[probeTrack].Pt()>3 && abs(tracks[probeTrack].Eta()) <= 1.2  && Muons[tagMuon].Pt() > 25"
        basicCond = "tagJpsi == 1 && probeJpsi == 1 && tracks[probeTrack].Pt()>2 && Muons[tagMuon].Pt() > 5"
    else:
        #basicCond = "tracks[probeTrack].Pt()>3 && abs(tracks[probeTrack].Eta()) <= 1.2  && Muons[tagMuon].Pt() > 5"
        basicCond = "tracks[probeTrack].Pt()>2 &&  Muons[tagMuon].Pt() > 5 && invMass < 3.2 && invMass > 3.0"

    idMuons = True
    if idMuons:
        basicCond += " && tracks_mi[probeTrack] > -1 && Muons_mediumID[tracks_mi[probeTrack]] == 1"

    if samples == "sim":
        fileNames = glob(
            "/afs/desy.de/user/n/nissanuv/nfs/x1x2x1/bg/skim_jpsi_single_electron/sum/type_sum/*"
        )
    else:
        fileNames = glob(
            "/afs/desy.de/user/n/nissanuv/nfs/x1x2x1/data/skim_jpsi_single_electron/sum/*"
        )

    for fileName in fileNames:
        print fileName
        rootFile = TFile(fileName)
        c = rootFile.Get('tEvent')

        tmpHist = utils.getHistogramFromTree("hist", c,
                                             "tracks[probeTrack].Pt():deltaR",
                                             20, 0, 1, basicCond, False,
                                             "hsqrt", False, True, 20, 2, 20)

        if hist is None:
            hist = tmpHist.Clone("2dhist")
            hist.SetDirectory(0)
        else:
            hist.Add(tmpHist)

        rootFile.Close()
        #break

    # for f in rootfiles:


#         if os.path.basename(f) in plot_par.ignore_bg_files:
#             print "File", f, "in ignore list. Skipping..."
#             continue
#         rootFile = TFile(f)
#         if not_full and i > 0:
#             break
#         i += 1
#         print f
#         c = rootFile.Get('tEvent')

#hist = TH1F("ptdrhist", "ptdrhist", 50, 0, 1)
#hist.SetMinimum(1)
#hist.SetMaximum(20)
    hist.SetTitle("simulation" if samples == "sim" else "data")
    hist.GetYaxis().SetTitle("probe p_{T} [GeV]")
    hist.GetXaxis().SetTitle("#Delta_{}R")

    hist.Draw("colz")

    legend = TLegend(.60, .40, .89, .60)
    legend.SetNColumns(1)
    legend.SetBorderSize(1)
    legend.SetFillStyle(0)

    fFullModel5 = TF1("ptdr5", ptdr, 0, 1, 2)
    fFullModel5.SetNpx(500)
    fFullModel5.SetParameter(0, 5.0)
    fFullModel5.SetParameter(1, 0)
    fFullModel5.SetLineWidth(2)
    fFullModel5.SetLineColor(kYellow)

    fFullModel75 = TF1("ptdr75", ptdr, 0, 1, 2)
    fFullModel75.SetNpx(500)
    fFullModel75.SetParameter(0, 7.5)
    fFullModel75.SetParameter(1, 0)
    fFullModel75.SetLineWidth(2)
    fFullModel75.SetLineColor(kGray)

    fFullModel15 = TF1("ptdr15", ptdr, 0, 1, 2)
    fFullModel15.SetNpx(500)
    fFullModel15.SetParameter(0, 15)
    fFullModel15.SetParameter(1, 0)
    fFullModel15.SetLineWidth(2)
    fFullModel15.SetLineColor(kGreen)

    fFullModel25 = TF1("ptdr25", ptdr, 0, 1, 2)
    fFullModel25.SetNpx(500)
    fFullModel25.SetParameter(0, 25)
    fFullModel25.SetParameter(1, 0)
    fFullModel25.SetLineWidth(2)
    fFullModel25.SetLineColor(kRed)

    fFullModel35 = TF1("ptdr2", ptdr, 0, 1, 2)
    fFullModel35.SetNpx(500)
    fFullModel35.SetParameter(0, 35)
    fFullModel35.SetParameter(1, 0)
    fFullModel35.SetLineWidth(2)
    fFullModel35.SetLineColor(kBlue)

    fFullModel55 = TF1("ptdr55", ptdr, 0, 1, 2)
    fFullModel55.SetNpx(500)
    fFullModel55.SetParameter(0, 55)
    fFullModel55.SetParameter(1, 0)
    fFullModel55.SetLineWidth(2)
    fFullModel55.SetLineColor(kBlack)

    fFullModel85 = TF1("ptdr85", ptdr, 0, 1, 2)
    fFullModel85.SetNpx(500)
    fFullModel85.SetParameter(0, 85)
    fFullModel85.SetParameter(1, 0)
    fFullModel85.SetLineWidth(2)
    fFullModel85.SetLineColor(kMagenta)

    fFullModel120 = TF1("ptdr120", ptdr, 0, 1, 2)
    fFullModel120.SetNpx(500)
    fFullModel120.SetParameter(0, 150)
    fFullModel120.SetParameter(1, 0)
    fFullModel120.SetLineWidth(2)
    fFullModel120.SetLineColor(kCyan)

    fFullModel5.Draw("SAME")
    fFullModel75.Draw("SAME")
    fFullModel15.Draw("SAME")
    fFullModel25.Draw("SAME")
    fFullModel35.Draw("SAME")
    fFullModel55.Draw("SAME")
    fFullModel85.Draw("SAME")
    fFullModel120.Draw("SAME")

    legend.AddEntry(fFullModel5, "#Delta#eta=0 tag p_{T} 5.0 [GeV]", 'l')
    legend.AddEntry(fFullModel75, "#Delta#eta=0 tag p_{T} 7.5 [GeV]", 'l')
    legend.AddEntry(fFullModel15, "#Delta#eta=0 tag p_{T} 15 [GeV]", 'l')
    legend.AddEntry(fFullModel25, "#Delta#eta=0 tag p_{T} 25 [GeV]", 'l')
    legend.AddEntry(fFullModel35, "#Delta#eta=0 tag p_{T} 35 [GeV]", 'l')
    legend.AddEntry(fFullModel55, "#Delta#eta=0 tag p_{T} 55 [GeV]", 'l')
    legend.AddEntry(fFullModel85, "#Delta#eta=0 tag p_{T} 85 [GeV]", 'l')
    legend.AddEntry(fFullModel120, "#Delta#eta=0 tag p_{T} 150 [GeV]", 'l')

    legend.Draw("SAME")
    baseName = "ptdr_barrel"
    if samples == "sim":
        baseName += "_sim"
        if select_jpsi:
            baseName += "_jpsi"
        if idMuons:
            baseName += "_id"
    else:
        baseName += "_data"

    c1.SaveAs(baseName + ".pdf")
def main():
    print "Start: " + datetime.now().strftime('%d-%m-%Y %H:%M:%S')

    fnew = TFile(output_file, 'recreate')
    c1 = TCanvas("c1", "c1", 800, 800)
    c1.cd()

    print "Getting signals..."

    for filename in glob(signal_dir + "/*"):
        print "Opening", filename
        if sam:
            deltaM = utils.getPointFromSamFileName(filename)
        else:
            deltaM = utils.getPointFromFileName(filename)

        print "deltaM=" + deltaM
        f = TFile(filename)
        c = f.Get('tEvent')

        for lep in ["Muons", "Electrons"]:
            c1.cd()
            # prev
            #hist = utils.getHistogramFromTree(deltaM + "_1t_" + lep, c, "exTrack_dilepBDT", 30, -0.6, 0.6, str(utils.LUMINOSITY) + "* passedMhtMet6pack * tEffhMetMhtRealXMht2016 *  Weight * BranchingRatio * (exclusiveTrack == 1 && MHT >= 220 && MET >= 200 && exTrack_invMass < 30 && BTagsDeepMedium == 0 && exTrack_dilepBDT >= 0 && exclusiveTrackLeptonFlavour == \"" + lep + "\")", True)
            # Making new version without trackBDT precut
            hist = utils.getHistogramFromTree(
                deltaM + "_1t_" + lep, c, "exTrack_dilepBDT" + jetiso, bins,
                -1, 1,
                str(utils.LUMINOSITY) +
                "* passedMhtMet6pack * tEffhMetMhtRealXMht2016 *  Weight * BranchingRatio * (exclusiveTrack"
                + jetiso +
                " == 1 && MET >= 140 && MHT >= 220 && exTrack_invMass" +
                jetiso +
                " < 12 && BTagsDeepMedium == 0 && exclusiveTrackLeptonFlavour"
                + jetiso + " == \"" + lep + "\")", True)
            #hist = utils.getHistogramFromTree(deltaM + "_1t_" + lep, c, "MET", bins, 0, 500, str(utils.LUMINOSITY) + "* passedMhtMet6pack * tEffhMetMhtRealXMht2016 *  Weight * BranchingRatio * (exclusiveTrack" + jetiso + " == 1 && MHT >= 220 && exTrack_invMass" + jetiso + " < 12 && BTagsDeepMedium == 0 && exclusiveTrackLeptonFlavour" + jetiso + " == \"" + lep + "\")", True)
            hist.Sumw2()
            fnew.cd()
            hist.Write()
            orthOpt = [True, False] if lep == "Muons" else [False]
            for orth in orthOpt:
                c1.cd()
                hist = utils.getHistogramFromTree(
                    deltaM + "_2l_" + ("orth_" if orth else "") + lep, c,
                    "dilepBDT" + jetiso, bins, -1, 1,
                    str(utils.LUMINOSITY) +
                    "* passedMhtMet6pack * tEffhMetMhtRealXMht2016 * Weight * BranchingRatio * (twoLeptons"
                    + jetiso + " == 1 " + (orth_cond if orth else "") +
                    " && MET >= 140 && MHT >= 220 && invMass" + jetiso +
                    " < 12  && invMass" + jetiso + " > 0.4 && !(invMass" +
                    jetiso + " > 3 && invMass" + jetiso +
                    " < 3.2) && !(invMass" + jetiso + " > 0.75 && invMass" +
                    jetiso +
                    " < 0.81) && BTagsDeepMedium == 0 && vetoElectronsPassIso == 0 && vetoMuonsPassIso == 0 && @leptons"
                    + jetiso + ".size() == 2 && leptonFlavour" + jetiso +
                    " == \"" + lep + "\" && sameSign" + jetiso +
                    " == 0 && isoCr" + jetiso + " == 0)", True)
                #hist = utils.getHistogramFromTree(deltaM + "_2l_" + ("orth_" if orth else "") + lep, c, "MET", bins, 0, 500, str(utils.LUMINOSITY) + "* passedMhtMet6pack * tEffhMetMhtRealXMht2016 * Weight * BranchingRatio * (twoLeptons" + jetiso + " == 1 " + (orth_cond if orth else "") + " && MHT >= 220 && invMass" + jetiso + " < 12  && invMass" + jetiso + " > 0.4 && !(invMass" + jetiso + " > 3 && invMass" + jetiso + " < 3.2) && !(invMass" + jetiso + " > 0.75 && invMass" + jetiso + " < 0.81) && BTagsDeepMedium == 0 && vetoElectronsPassIso == 0 && vetoMuonsPassIso == 0 && @leptons" + jetiso + ".size() == 2 && leptonFlavour" + jetiso + " == \"" + lep + "\" && sameSign" + jetiso + " == 0 && isoCr" + jetiso + " == 0)", True)
                #non-orth
                #hist = utils.getHistogramFromTree(deltaM + "_2l", c, "dilepBDT", 30, -0.6, 0.6, str(utils.LUMINOSITY) + "* passedMhtMet6pack * tEffhMetMhtRealXMht2016 * Weight * BranchingRatio * (MHT >= 220 &&  MET >= 200 && invMass < 12  && invMass > 0.4 && !(invMass > 3 && invMass < 3.2) && !(invMass > 0.75 && invMass < 0.81) && dilepBDT > 0.1)", True)
                hist.Sumw2()
                fnew.cd()
                hist.Write()

        f.Close()

    bg_1t_hist = {}
    bg_2l_hist = {}

    print "Getting BG..."

    for filename in glob(bg_dir + "/*"):
        print "Opening", filename
        f = TFile(filename)
        c = f.Get('tEvent')

        for lep in ["Muons", "Electrons"]:

            c1.cd()
            basename = os.path.basename(filename).split(".")[0]
            # prev
            #hist = utils.getHistogramFromTree(basename, c, "exTrack_dilepBDT", 30, -0.6, 0.6, str(utils.LUMINOSITY) + "* passedMhtMet6pack * tEffhMetMhtRealXMht2016 * Weight * (exclusiveTrack == 1 && MHT >= 220 && MET >= 200 && exTrack_invMass < 30 && BTagsDeepMedium == 0  && exTrack_dilepBDT >= 0 && exclusiveTrackLeptonFlavour == \"" + lep + "\")", True)
            # Making new version without trackBDT precut
            hist = utils.getHistogramFromTree(
                "bg_1t_" + lep, c, "exTrack_dilepBDT" + jetiso, bins, -1, 1,
                str(utils.LUMINOSITY) +
                "* passedMhtMet6pack * tEffhMetMhtRealXMht2016 *  Weight * BranchingRatio * (exclusiveTrack"
                + jetiso +
                " == 1 && MET >= 140 && MHT >= 220 && exTrack_invMass" +
                jetiso +
                " < 12 && BTagsDeepMedium == 0 && exclusiveTrackLeptonFlavour"
                + jetiso + " == \"" + lep + "\")", True)
            #hist = utils.getHistogramFromTree(basename, c, "MET", bins, 0, 500, str(utils.LUMINOSITY) + "* passedMhtMet6pack * tEffhMetMhtRealXMht2016 *  Weight * BranchingRatio * (exclusiveTrack" + jetiso + " == 1 && MHT >= 220 && exTrack_invMass" + jetiso + " < 12 && BTagsDeepMedium == 0 && exclusiveTrackLeptonFlavour" + jetiso + " == \"" + lep + "\")", True)
            hist.Sumw2()
            if bg_1t_hist.get(lep) is None:
                bg_1t_hist[lep] = hist
            else:
                bg_1t_hist[lep].Add(hist)

            orthOpt = [True, False] if lep == "Muons" else [False]
            for orth in orthOpt:
                c1.cd()
                print("2l", lep, orth)
                hist = utils.getHistogramFromTree(
                    "bg_2l_" + lep + ("_orth" if orth else ""), c,
                    "dilepBDT" + jetiso, bins, -1, 1,
                    str(utils.LUMINOSITY) +
                    "* passedMhtMet6pack * tEffhMetMhtRealXMht2016 * Weight * BranchingRatio * (twoLeptons"
                    + jetiso + " == 1 " + (orth_cond if orth else "") +
                    " && MET >= 140 && MHT >= 220 && invMass" + jetiso +
                    " < 12  && invMass" + jetiso + " > 0.4 && !(invMass" +
                    jetiso + " > 3 && invMass" + jetiso +
                    " < 3.2) && !(invMass" + jetiso + " > 0.75 && invMass" +
                    jetiso +
                    " < 0.81) && BTagsDeepMedium == 0 && vetoElectronsPassIso == 0 && vetoMuonsPassIso == 0 && @leptons"
                    + jetiso + ".size() == 2 && leptonFlavour" + jetiso +
                    " == \"" + lep + "\" && sameSign" + jetiso +
                    " == 0 && isoCr" + jetiso + " == 0)", True)
                #hist = utils.getHistogramFromTree(basename, c, "MET", bins, 0, 500, str(utils.LUMINOSITY) + "* passedMhtMet6pack * tEffhMetMhtRealXMht2016 * Weight * BranchingRatio * (twoLeptons" + jetiso + " == 1 "  + (orth_cond if orth else "") +  " && MHT >= 220 && invMass" + jetiso + " < 12  && invMass" + jetiso + " > 0.4 && !(invMass" + jetiso + " > 3 && invMass" + jetiso + " < 3.2) && !(invMass" + jetiso + " > 0.75 && invMass" + jetiso + " < 0.81) && BTagsDeepMedium == 0 && vetoElectronsPassIso == 0 && vetoMuonsPassIso == 0 && @leptons" + jetiso + ".size() == 2 && leptonFlavour" + jetiso + " == \"" + lep + "\" && sameSign" + jetiso + " == 0 && isoCr" + jetiso + " == 0)", True)
                #non-orth
                #hist = utils.getHistogramFromTree(basename, c, "dilepBDT", 30, -0.6, 0.6, str(utils.LUMINOSITY) + "* passedMhtMet6pack * tEffhMetMhtRealXMht2016 * Weight * (MHT >= 220 &&  MET >= 200 && invMass < 12  && invMass > 0.4 && !(invMass > 3 && invMass < 3.2) && !(invMass > 0.75 && invMass < 0.81) && dilepBDT > 0.1)", True)
                hist.Sumw2()
                if bg_2l_hist.get(lep + ("_orth" if orth else "")) is None:
                    bg_2l_hist[lep + ("_orth" if orth else "")] = hist
                else:
                    bg_2l_hist[lep + ("_orth" if orth else "")].Add(hist)

        f.Close()

    fnew.cd()
    for lep in ["Muons", "Electrons"]:
        bg_1t_hist[lep].Write("bg_1t_" + lep)
        orthOpt = [True, False] if lep == "Muons" else [False]
        for orth in orthOpt:
            bg_2l_hist[lep + ("_orth" if orth else "")].Write(
                "bg_2l_" + ("orth_" if orth else "") + lep)
    fnew.Close()

    exit(0)