예제 #1
0
def testDB():
    """ ./lat2.py -test
    Do database stuff. """
    # ds.getDBKeys()
    # ds.getDBRecord("ds1_idx0")
    # ds.delDBRecord("ds1_idx0")

    cal = ds.CalInfo()
예제 #2
0
파일: cs1.py 프로젝트: rafaellopesdesa/LAT
def testLivetimeOutput():

    dsNum, modNum, dPath = 4, 2, "../data"
    expDict = ds.getExposureDict(dsNum, modNum, dPath)

    expTot = 0.
    for key in expDict:
        expTot += sum(expDict[key])
        # print key, expDict[key]

    print "Total exposure:", expTot
예제 #3
0
def main(argv):
    """ Calculates channel-by-channel cut values for LAT parameters.
        Cuts are calculated independent of other cut values.
    """
    # -- ROOT options --
    gROOT.ProcessLine(".x ~/env/MJDClintPlotStyle.C")
    gROOT.ProcessLine("gErrorIgnoreLevel = 3001;")  # suppress ROOT messages

    # -- Get user args --
    dsNum = int(argv[0])
    fastMode = False
    f = dict.fromkeys(shlex.split('a b c d e'), False)  # make a bunch of bools
    for i, opt in enumerate(argv):
        if opt == "-f": fastMode = True
        if opt == "-bcMax": f['a'] = True
        if opt == "-noiseWeight": f['b'] = True
        if opt == "-bcTime": f['c'] = True
        if opt == "-tailSlope": f['d'] = True
        if opt == "-fitSlo": f['e'] = True
        if opt == "-burst":
            burstCut(dsNum)
            return

    # -- Load chains for this DS --
    homePath = os.path.expanduser('~')  # glob doesn't know how to expand this
    inPath = homePath + "/project/lat/latSkimDS%d*.root" % dsNum
    fileList = glob.glob(inPath)
    cutFile = TFile(fileList[0])
    theCut = cutFile.Get("theCut").GetTitle()

    cal = TChain("skimTree")
    cal.Add("~/project/cal-lat/latSkimDS%d*.root" % dsNum)

    # -- Load channel list --
    chList = ds.GetGoodChanList(dsNum)
    if dsNum == 5:  # remove 692 and 1232
        chList = [
            584, 592, 598, 608, 610, 614, 624, 626, 628, 632, 640, 648, 658,
            660, 662, 672, 678, 680, 688, 690, 694, 1106, 1110, 1120, 1124,
            1128, 1170, 1172, 1174, 1176, 1204, 1208, 1298, 1302, 1330, 1332
        ]
    # chList = [578]
    print chList

    # -- Run routines --
    if f['a']: bcMax(dsNum, theCut, cal, chList, fastMode)
    if f['b']: noiseWeight(dsNum, theCut, cal, chList, fastMode)
    if f['c']: bcTime(dsNum, theCut, cal, chList, fastMode)
    if f['d']: tailSlope(dsNum, theCut, cal, chList, fastMode)
    if f['e']: fitSlo(dsNum, theCut, cal, chList, fastMode)
예제 #4
0
def MovingAve(dsNum=1, dType='isNat'):
    inDir = '/Users/brianzhu/macros/code/LAT/plots/AThresh'

    chList = ds.GetGoodChanList(dsNum, dType[2:])
    if dsNum == 5:  # remove 692 and 1232 (both beges, so who cares)
        if 692 in chList: chList.remove(692)
        if 1232 in chList: chList.remove(1232)

    f1 = ROOT.TFile("{}/Bkg_{}_DS{}.root".format(inDir, dType, dsNum))
    bkgDict = {}
    bkgTot = []
    for key in f1.GetListOfKeys():
        histName = key.GetName()
        if "UnCorr" in histName:
            for xbin in range(f1.Get(histName).GetNbinsX()):
                bkgTot.append(f1.Get(histName).GetBinContent(xbin))
        if 'ChTot' not in histName: continue
        name = ''.join(c for c in histName.split('_')[2] if c.isdigit())
        ch = int(name)
        if ch not in bkgDict.keys(): bkgDict[ch] = []
        for xbin in range(f1.Get(histName).GetNbinsX()):
            bkgDict[ch].append(f1.Get(histName).GetBinContent(xbin))

    # Moving average over 1 keV
    fig, ax = plt.subplots(figsize=(10, 7))
    for ch in bkgDict.keys():
        ax.cla()
        test = np.cumsum(bkgDict[ch][:200])
        moveAve = (test[5:] - test[:-5])
        moveAve2 = (test[3:] - test[:-3])
        moveAve3 = (test[7:] - test[:-7])
        moveAve4 = (test[10:] - test[:-10])
        ax.plot(np.linspace(0,
                            len(bkgDict[ch][:200]) / 10.,
                            len(bkgDict[ch][:200])),
                np.array(bkgDict[ch][:200]),
                ls='steps',
                label='Spectrum')
        ax.plot(np.linspace(0.3,
                            len(moveAve2) / 10., len(moveAve2)),
                np.array(moveAve2),
                label='Moving Average (0.3 keV ave)',
                linestyle="-")
        ax.plot(np.linspace(0.5,
                            len(moveAve) / 10., len(moveAve)),
                np.array(moveAve),
                label='Moving Average (0.5 keV ave)',
                linestyle="-.")
        ax.plot(np.linspace(0.7,
                            len(moveAve3) / 10., len(moveAve3)),
                np.array(moveAve3),
                label='Moving Average (0.7 keV ave)',
                linestyle="--")
        ax.legend()
        fig.savefig("{}/MovingAve_DS{}_{}_Ch{}.png".format(
            inDir, dsNum, dType[2:], ch))

    ax.cla()
    test = np.cumsum(bkgTot[:200])
    moveAve = (test[5:] - test[:-5])
    moveAve2 = (test[3:] - test[:-3])
    moveAve3 = (test[7:] - test[:-7])
    ax.plot(np.linspace(0,
                        len(bkgTot[:200]) / 10., len(bkgTot[:200])),
            np.array(bkgTot[:200]),
            ls='steps',
            label='Spectrum')
    ax.plot(np.linspace(0.3,
                        len(moveAve2) / 10., len(moveAve2)),
            np.array(moveAve2),
            label='Moving Average (0.3 keV ave)',
            linestyle="-")
    ax.plot(np.linspace(0.5,
                        len(moveAve) / 10., len(moveAve)),
            np.array(moveAve),
            label='Moving Average (0.5 keV ave)',
            linestyle="-.")
    ax.plot(np.linspace(0.7,
                        len(moveAve3) / 10., len(moveAve3)),
            np.array(moveAve3),
            label='Moving Average (0.7 keV ave)',
            linestyle="--")
    ax.legend()
    fig.savefig("{}/MovingAve_DS{}_{}_Tot.png".format(inDir, dsNum, dType[2:]))
예제 #5
0
    if os.path.exists(outDir):
        print "Failed: Output directory %s already exits" % ('"'+outDir+'"')
        exit(0)
    else:
        os.makedirs(outDir)
    jettypes = ["pt30_1l"]
    mvaBin = [ "D1", "D2","D3","D4"]
    systypes = ["", "_JECUp", "_JECDown", "_JERUp", "_JERDown", "_btgUp", "_btgDown", "_lepUp", "_lepDown",
                "_isrUp", "_isrDown", "_fsrUp", "_fsrDown", "_isr2Up", "_isr2Down", "_fsr2Up", "_fsr2Down",
                "_pdfUp", "_pdfDown", "_htUp", "_htDown", "_puUp", "_puDown", "_sclUp", "_sclDown"]
    outputfile = ROOT.TFile.Open(outDir + "/" + options.rootFile,"RECREATE")
    outputDataCard = options.dataCard

    # I hadd my MakeN files into TT.root, TTX, and OTHER, then take QCD from control region
    bgData = {
        "TT"    : info.DataSetInfo(basedir=basedir, fileName=options.year+"_TT.root",      label="TT",    processName="bkg_tt",    process="1", rate=False, lumiSys="-", scale=options.scaleB),
        "QCD"   : info.DataSetInfo(basedir=basedir, fileName=options.year+"_QCD.root",     label="QCD",   processName="bkg_qcd",   process="2", rate=False, lumiSys="-", scale=options.scaleB),
        "TTX"   : info.DataSetInfo(basedir=basedir, fileName=options.year+"_TTX.root",     label="TTX",   processName="bkg_ttx",   process="3", rate=False, lumiSys="-", scale=options.scaleB),
        "OTHER" : info.DataSetInfo(basedir=basedir, fileName=options.year+"_BG_OTHER.root",label="OTHER", processName="bkg_other", process="4", rate=True,  lumiSys="-", scale=options.scaleB),
    }

    if options.year == "2016":
        sgData = {
            "SYY_300" : info.DataSetInfo(basedir=basedir, fileName="2016_StealthSYY_2t6j_mStop-300.root", label="SYY_300", processName="signal", process="0", rate=True, lumiSys="1.05", scale=options.scaleS*1.17424),
            "SYY_350" : info.DataSetInfo(basedir=basedir, fileName="2016_StealthSYY_2t6j_mStop-350.root", label="SYY_350", processName="signal", process="0", rate=True, lumiSys="1.05", scale=options.scaleS*1.16727),
            "SYY_400" : info.DataSetInfo(basedir=basedir, fileName="2016_StealthSYY_2t6j_mStop-400.root", label="SYY_400", processName="signal", process="0", rate=True, lumiSys="1.05", scale=options.scaleS*1.17143),
            "SYY_450" : info.DataSetInfo(basedir=basedir, fileName="2016_StealthSYY_2t6j_mStop-450.root", label="SYY_450", processName="signal", process="0", rate=True, lumiSys="1.05", scale=options.scaleS*1.17047),
            "SYY_500" : info.DataSetInfo(basedir=basedir, fileName="2016_StealthSYY_2t6j_mStop-500.root", label="SYY_500", processName="signal", process="0", rate=True, lumiSys="1.05", scale=options.scaleS*1.17459),
            "SYY_550" : info.DataSetInfo(basedir=basedir, fileName="2016_StealthSYY_2t6j_mStop-550.root", label="SYY_550", processName="signal", process="0", rate=True, lumiSys="1.05", scale=options.scaleS*1.17179),
            "SYY_600" : info.DataSetInfo(basedir=basedir, fileName="2016_StealthSYY_2t6j_mStop-600.root", label="SYY_600", processName="signal", process="0", rate=True, lumiSys="1.05", scale=options.scaleS*1.17412),
            "SYY_650" : info.DataSetInfo(basedir=basedir, fileName="2016_StealthSYY_2t6j_mStop-650.root", label="SYY_650", processName="signal", process="0", rate=True, lumiSys="1.05", scale=options.scaleS*1.16773),
예제 #6
0
def main(argv):

    print "========================================"
    print "LAT2 started:", time.strftime('%X %x %Z')
    startT = time.clock()
    gROOT.ProcessLine(
        "gErrorIgnoreLevel = 3001;")  # suppress ROOT error messages

    dsNum, subNum, runNum = None, None, None
    fCal, fUpd, fBat, fFor, fPlt, fRaw, fCalPath = 0, 0, 0, 0, 0, 0, 0
    fPaths = [".", "."]
    if len(argv) == 0: return
    for i, opt in enumerate(argv):
        if opt == "-cal":
            fCal = True
            print "Calibration mode."
        if opt == "-upd":
            fUpd = True
            print "File update mode."
        if opt == "-p":
            fPlt = True
            print "Writing plots mode."
        if opt == "-debug":
            debugFiles()
            stopT = time.clock()
            print "Stopped:", time.strftime(
                '%X %x %Z'), "\nProcess time (min):", (stopT - startT) / 60
            exit()
        if opt == "-fitRN":
            fitDBRiseNoise()
            # stopT = time.clock()
            # print "Stopped:",time.strftime('%X %x %Z'),"\nProcess time (min):",(stopT - startT)/60
            exit()
        if opt == "-force":
            fFor = True
            print "Force DB update mode."
        if opt == "-raw":
            fRaw = True
            print "Printing raw peakfinder plots."
        if opt == "-d":
            dsNum = int(argv[i + 1])
            print "Scanning DS-%d" % (dsNum)
        if opt == "-c":
            fCalPath = True
            print "pointing to cal path"
        if opt == "-f":
            print "Scanning DS-%d, run %d" % (dsNum, runNum)
        if opt == "-s":
            dsNum, subNum = int(argv[i + 1]), int(argv[i + 2])
            print "Scanning DS-%d sub-range %d" % (dsNum, subNum)
        if opt == "-p":
            fPaths = [argv[i + 1], argv[i + 2]]
            print "Manual I/O paths set."
        if opt == "-test":
            testDB()
        if opt == "-b":
            fBat = True
            import matplotlib
            if os.environ.get('DISPLAY', '') == '':
                print "No display found. Using non-interactive Agg backend"
                matplotlib.use('Agg')
            print "Batch mode selected."
    global plt
    import matplotlib.pyplot as plt

    if fCal:
        rec = calibrateRuns(dsNum, subNum, fPaths, fBat, fPlt, fRaw)
        ds.setDBRecord(rec, fFor)

    if fUpd:
        updateFile(dsNum, fCalPath)

    stopT = time.clock()
    print "Stopped:", time.strftime(
        '%X %x %Z'), "\nProcess time (min):", (stopT - startT) / 60
예제 #7
0
def SaveCalHistograms():

    outDir = '/projecta/projectdirs/majorana/users/bxyzhu/LATv2/plots/spectra'
    calDir = '/projecta/projectdirs/majorana/users/wisecg/cal-lat'
    cutDir = '/projecta/projectdirs/majorana/users/bxyzhu/cuts'
    cInfo = ds.CalInfo()
    bins,lower,upper = 250,0,250
    # Basic cut
    mNum = 2
    cuts = "gain==0 && mHL==%d && isGood && !muVeto && !(C==1&&isLNFill1) && !(C==2&&isLNFill2) && C!=0&&P!=0&&D!=0"%(mNum)
    skimTree = ROOT.TChain("skimTree")
    skimCut = ROOT.TChain("skimTree")
    dsList = [0, 1, 2, 3, 4, 5]
    # dsList = [1]
    outFile = ROOT.TFile(outDir + "/CalAcceptance_wfstd_mHL%d.root"%(mNum), "RECREATE")
    outFile.cd()
    # Total histogram (all datasets)

    hCutTotal = ROOT.TH1D("hCutTotal", "", bins,lower,upper)
    hFullTotal = ROOT.TH1D("hFullTotal", "", bins,lower,upper)
    # Total histogram for Dataset
    hDSTotal = []
    hDSFullTotal = []
    dMissingCh = [0, 1, 2, 3, 4, 5]
    dThreshCutCh = [0, 1, 2, 3, 4, 5]
    for iDS, dsNum in enumerate(dsList):
        hDSTotal.append(ROOT.TH1D())
        hDSTotal[iDS] = ROOT.TH1D("hDS%d"%(dsNum), "", bins,lower,upper)
        hDSFullTotal.append(ROOT.TH1D())
        hDSFullTotal[iDS] = ROOT.TH1D("hDS%d_Full"%(dsNum), "", bins,lower,upper)
        nMods = [1]
        if dsNum == 4: nMods = [2]
        if dsNum == 5: nMods = [1, 2]
        for modNum in nMods:
            chList = ds.GetGoodChanList(dsNum)
            if dsNum==5 and modNum==1: # remove 692 and 1232 (both beges, so who cares)
                chList = [584, 592, 598, 608, 610, 614, 624, 626, 628, 632, 640, 648, 658, 660, 662, 672, 678, 680, 688, 690, 694]
            elif dsNum==5 and modNum==2:
                chList = [1106, 1110, 1120, 1124, 1128, 1170, 1172, 1174, 1176, 1204, 1208, 1298, 1302, 1330, 1332]
            # Total channel histograms, split by dataset
            hChTotalList = []
            hChFullTotalList = []
            for idx, ch in enumerate(chList):
                hChTotalList.append(ROOT.TH1D())
                hChTotalList[idx] = ROOT.TH1D("hDS%d_Ch%d"%(dsNum, ch), "", bins,lower,upper)
                hChFullTotalList.append(ROOT.TH1D())
                hChFullTotalList[idx] = ROOT.TH1D("hDS%d_Ch%d_Full"%(dsNum, ch), "", bins,lower,upper)

            nRanges = [0, len(cInfo.master['ds%d_m%d'%(dsNum, modNum)])]
            for calidx in range(nRanges[0], nRanges[1]):
                print "Drawing DS%d calidx%d mod%d"%(dsNum, calidx, modNum)
                hCutList, hFullList = [], []
                skimTree.Reset()
                calList = cInfo.GetCalList("ds%d_m%d" % (dsNum, modNum), calidx, runLimit=10)
                for run in calList:
                    skimTree.Add("%s/latSkimDS%d_run%d_*.root"%(calDir, dsNum, run))

                for idx, ch in enumerate(chList):
                    # Reset Tree every calidx + ch
                    skimCut.Reset()
                    skimCut.Add("%s/calwf/calwfstd-DS%d-%d-ch%d.root"%(cutDir, dsNum, calidx, ch))
                    if skimCut.GetEntries() == 0:
                        hCutList.append(ROOT.TH1D())
                        hFullList.append(ROOT.TH1D())
                        print "Channel %d, idx %d has no entries, skipping"
                        continue

                    hCutList.append(ROOT.TH1D())
                    hFullList.append(ROOT.TH1D())
                    # Add additional cut here for channel
                    hCutList[idx] = wl.H1D(skimCut,bins,lower,upper, "trapENFCal", cuts+"&& channel==%d"%(ch), Title="hDS%d_Ch%d_%d"%(dsNum,ch,calidx), Name="hDS%d_Ch%d_%d"%(dsNum, ch,calidx))
                    hFullList[idx] = wl.H1D(skimTree,bins,lower,upper, "trapENFCal", cuts+"&&channel==%d"%(ch),Title="hFullDS_%d_Ch%d_%d"%(dsNum,ch,calidx), Name="hFullDS%d_Ch%d_%d"%(dsNum,ch,calidx))

                    # Only write channel specific if there are counts
                    if hCutList[idx].Integral() > 0:
                        # Add ch+calidx histograms to ch total histogram if
                        hCutList[idx].Write()
                        hFullList[idx].Write()
                        hChTotalList[idx].Add(hCutList[idx])
                        hChFullTotalList[idx].Add(hFullList[idx])

                        # Add individual ch+calidx histograms to total histogram and total DS histogram
                        hCutTotal.Add(hCutList[idx])
                        hFullTotal.Add(hFullList[idx])
                        hDSTotal[iDS].Add(hCutList[idx])
                        hDSFullTotal[iDS].Add(hFullList[idx])

        # Write Total channel histograms
        for idx, ch in enumerate(chList):
            if hChTotalList[idx].Integral() > 0:
                hChTotalList[idx].Write()
                hChFullTotalList[idx].Write()
            else:
                print "Channel %d has no entries!"%(ch)

        # Write total DS histograms
        hDSTotal[iDS].Write()
        hDSFullTotal[iDS].Write()

    # Write total histogram and close
    hFullTotal.Write()
    hCutTotal.Write()
    outFile.Close()
    return 0
예제 #8
0
파일: cs1.py 프로젝트: rafaellopesdesa/LAT
def parseLivetimeOutput():

    # for dSet in [(0,1),(1,1),(2,1),(3,1),(4,2),(5,1),(5,2)]:
    for dSet in [(5, 2)]:

        dsNum, modNum = dSet[0], dSet[1]

        chList = ds.GetGoodChanList(dsNum)
        if dsNum == 5 and modNum == 1:
            chList = [ch for ch in chList if ch < 1000 and ch != 692]
        if dsNum == 5 and modNum == 2:
            chList = [ch for ch in chList if ch > 1000 and ch != 1232]

        expDict = {ch: [] for ch in chList}
        tmpDict, bkgIdx, prevBkgIdx = {}, -1, -1

        with open("../data/expos_ds%d.txt" % dsNum, "r") as f:
            table = f.readlines()

        for idx, line in enumerate(table):
            tmp = (line.rstrip()).split(" ")
            if len(tmp) == 0: continue

            if bkgIdx != prevBkgIdx:
                for ch in chList:
                    if ch in tmpDict.keys():
                        expDict[ch].append(tmpDict[ch])
                    else:
                        expDict[ch].append(0.)
                tmpDict = {}
                prevBkgIdx = bkgIdx

            if tmp[0] == "bkgIdx":
                bkgIdx = tmp[1]

            if len(tmp) > 1 and tmp[1] == ":" and tmp[0].isdigit() and int(
                    tmp[0]) in chList:
                ch, exp = int(tmp[0]), float(tmp[2])
                tmpDict[ch] = exp

            if line == "All-channel summary: \n":
                summaryIdx = idx

        # get last bkgIdx
        for ch in chList:
            if ch in tmpDict.keys():
                expDict[ch].append(tmpDict[ch])
            else:
                expDict[ch].append(0.)

        # knock off the first element (it's 0).  Now expDict is done
        for ch in expDict:
            if expDict[ch][0] > 0:
                print "ERROR, WTF"
                exit(1)
            expDict[ch].pop(0)

        # now get the all-channel summary for HG channels
        summaryDict = {ch: [] for ch in chList}
        for line in table[summaryIdx + 2:]:
            tmp = (line.rstrip()).split()
            ch, detID, aMass, runTime, expo = int(tmp[0]), int(tmp[1]), float(
                tmp[2]), float(tmp[3]), float(tmp[4])
            summaryDict[ch] = [expo, aMass]

        # now a final cross check
        print "DS%d, M%d" % (dsNum, modNum)
        for ch in chList:

            if sum(expDict[ch]) > 0 and len(summaryDict[ch]) == 0:
                print "That ain't shoulda happened"
                exit(1)
            elif len(summaryDict[ch]) == 0:
                continue

            mySum, ltResult, aMass = sum(
                expDict[ch]), summaryDict[ch][0], summaryDict[ch][1]
            diff = ((ltResult - mySum) / aMass) * 86400

            print "%d   %.4f   %-8.4f    %-8.4f    %-8.4f" % (ch, aMass, mySum,
                                                              ltResult, diff)

        print " "
예제 #9
0
def main(argv):

    inDir, cutDir, outDir = ".", ".", "./plots"
    specMode = False
    dsNum, modNum, chNum = -1, -1, -1
    skimTree = ROOT.TChain("skimTree")
    bins, lower, upper = {1250}, 0, 250
    outFile = ROOT.TFile()
    for i, opt in enumerate(argv):
        if opt == "-spec":
            specMode = True
            print "Spectrum Mode"
        if opt == "-d":
            inDir, outDir = argv[i + 1], argv[i + 2]
        if opt == "-ch":
            chNum = int(argv[i + 1])
            print("Drawing specific channel %d" % (chNum))
        if opt == "-s":
            dsNum, modNum = int(argv[i + 1]), int(argv[i + 2])
            print("Drawing DS-%d Module-%d" % (dsNum, modNum))
        if opt == "-cal":
            calMode = True
            print("Drawing Calibration runs")

    cInfo = ds.CalInfo()
    EnergyList = [[1., 5.], [2., 4.], [4., 9.], [9., 12.], [12., 40.],
                  [40., 50.], [50., 100.]]

    # -- Load channel list --
    if chNum == -1:
        chList = ds.GetGoodChanList(dsNum)
        if dsNum == 5 and modNum == 1:  # remove 692 and 1232
            chList = [
                584, 592, 598, 608, 610, 614, 624, 626, 628, 632, 640, 648,
                658, 660, 662, 672, 678, 680, 688, 690, 694
            ]
        if dsNum == 5 and modNum == 2:
            # chList = [1106, 1110, 1120, 1124, 1128, 1170, 1172, 1174, 1176, 1204, 1208, 1298, 1302, 1330, 1332]
            # Removed ch 1110, 1208, and 1332
            chList = [
                1106, 1120, 1124, 1128, 1170, 1172, 1174, 1176, 1204, 1298,
                1302, 1330
            ]
    else:
        chList = [chNum]

    # -- Load calibration files --
    if dsNum == -1 or modNum == -1:
        print "DS, subDS, or module number not set properly, exiting"
        return
    else:
        # Limit to 10 calibration runs because that's all Clint processed!
        for subNum in cInfo.master["ds%d_m%d" % (dsNum, modNum)].keys():
            calList = cInfo.GetCalList("ds%d_m%d" % (dsNum, modNum),
                                       subNum,
                                       runLimit=10)
            for i in calList:
                skimTree.Add("%s/latSkimDS%d_run%d_*" % (inDir, dsNum, i))

    # List of histograms (for summing together) and Dictionary of histograms (for different cuts)
    hList, hDict = [], {}

    # Create a list of DataFrames for each channel to concatenate at the end
    # cutNames = ["BasicCut", "+tailSlope", "+riseNoise", "+fitSlo"]
    cutNames = ["BasicCut", "+riseNoise", "+fitSlo"]
    # cutNames = ["BasicCut", "+tailSlope", "+bcMax", "+fitSlo"]

    if specMode:
        outFile = ROOT.TFile("%s/CalibHistograms_DS%d.root" % (outDir, dsNum),
                             "RECREATE")

    for subNum in cInfo.master["ds%d_m%d" % (dsNum, modNum)].keys():
        runCut = "&&run>=%d&&run<=%d" % (
            cInfo.master["ds%d_m%d" % (dsNum, modNum)][subNum][1],
            cInfo.master["ds%d_m%d" % (dsNum, modNum)][subNum][2])

        # DB style
        fsD = ds.getDBRecord("fitSlo_ds%d_idx%d_m%d_Peak" %
                             (dsNum, subNum, modNum))
        rnD = ds.getDBRecord("riseNoise_ds%d_idx%d_m%d_Peak" %
                             (dsNum, subNum, modNum))
        bcD = ds.getDBRecord("bcMax_ds%d_idx%d_m%d_Peak" %
                             (dsNum, subNum, modNum))

        # Get threshold info
        # goodRuns,badRuns,goodRunSigmas = ds.GetThreshDicts(dsNum)
        # threshrunCut = "&&("
        # for idx2,runRange in enumerate(goodRuns[ch]):
        #     threshrunCut += "(run>=%d&&run<=%d)||" % (runRange[0],runRange[1])
        #     # Strip all spaces to save space
        #     totalCut = megaCut.replace(" ", "") + threshrunCut[:-2] + ")"

        # Create new key for dictionaries according to subNum
        hDict[subNum] = []

        for idx, ch in enumerate(chList):
            # Append empty list for every subNum to store channel-based
            hDict[subNum].append([])

            # Set high gain only!
            channelCut = "channel==%d&&gain==0" % (ch)
            # Create new array for each subDS
            riseNoiseCut, fitSloCut = "", ""
            if rnD[ch][2] == 0 or fsD[ch][2] == 0:
                continue
            else:
                riseNoiseCut = '&&riseNoise<%.2f' % (rnD[ch][2])
                fitSloCut = '&&fitSlo<%.2f' % (fsD[ch][2])

            # Set cuts here
            PSA1 = channelCut + runCut + riseNoiseCut
            PSA2 = channelCut + runCut + riseNoiseCut + fitSloCut

            # Save all cuts into a list to iterate through
            cutList = [channelCut + runCut, PSA1, PSA2]

            for idx2, cuts in enumerate(cutList):
                if specMode:
                    hDict[subNum][idx].append(ROOT.TH1D())
                    hDict[subNum][idx][idx2] = wl.H1D(skimTree,
                                                      bins,
                                                      lower,
                                                      upper,
                                                      "trapENFCal",
                                                      cuts,
                                                      Title="h0_%d_Ch%d_%d" %
                                                      (subNum, ch, idx2))
                    print("Drawn: h0_%d_Ch%d_%d" % (subNum, ch, idx2))

    # Merge histograms into a list of histograms per cut
    if specMode:
        outFile.cd()
        ROOT.gStyle.SetOptStat(0)
        c1 = ROOT.TCanvas("c1", "c1", 1100, 800)
        c1.SetLogy()
        leg1 = ROOT.TLegend(0.35, 0.8, 0.65, 0.89)
        leg1.SetBorderSize(0)
        for idx2, cuts in enumerate(cutList):
            hList.append(ROOT.TH1D())
            hList[idx2] = hDict[0][0][idx2]
            for subNum in cInfo.master["ds%d_m%d" % (dsNum, modNum)].keys():
                for idx, ch in enumerate(chList[1:]):
                    hDict[subNum][idx][idx2].Write()
                    hList[idx2].Add(hDict[subNum][idx][idx2])

            hList[idx2].SetTitle("")
            hList[idx2].GetXaxis().SetTitle("Energy (keV)")
            hList[idx2].GetYaxis().SetTitle("Counts/ %.1f keV" % (float(
                (upper - lower) / bins)))
            # hList[idx2].SetMinimum(0.1) # Arbitrary unit right now...
            hList[idx2].SetLineColor(idx2 + 1)
            hList[idx2].Draw("SAME")
            leg1.AddEntry(hList[idx2], "%s" % cutNames[idx2], "l")
        leg1.Draw()
        c1.SaveAs("%s/Spec_ds%d_m%d.pdf" % (outDir, dsNum, modNum))
        c1.SaveAs("%s/Spec_ds%d_m%d.C" % (outDir, dsNum, modNum))

        outFile.Close()
예제 #10
0
파일: cs1.py 프로젝트: rafaellopesdesa/LAT
def channelSelection():

    calDB = db.TinyDB('../calDB.json')
    pars = db.Query()

    np.set_printoptions(threshold=np.inf)  # print full numpy array

    for dSet in [(0, 1), (1, 1), (2, 1), (3, 1), (4, 2), (5, 1), (5, 2)]:
        # for dSet in [(4,2)]:
        dsNum, modNum = dSet[0], dSet[1]
        print "DS-%d, M-%d" % (dsNum, modNum)

        expDict = ds.getExposureDict(dsNum, modNum, "../data")

        chList = ds.GetGoodChanList(dsNum)
        if dsNum == 5 and modNum == 1:
            chList = [ch for ch in chList if ch < 1000 and ch != 692]
        if dsNum == 5 and modNum == 2:
            chList = [ch for ch in chList if ch > 1000 and ch != 1232]

        chStats = {ch: [] for ch in chList}
        xTicks = []

        nBkg = ds.dsMap[dsNum]
        bkgRuns = ds.bkgRunsDS[dsNum]
        nCal = ds.getNCalIdxs(dsNum, modNum)
        calInfo = ds.CalInfo()
        xThresh = np.arange(0, 5, 0.01)
        yThreshTot = np.zeros(len(xThresh))

        for bkgIdx in range(nBkg + 1):

            # get the exposure and efficiency
            thD = ds.getDBRecord("thresh_ds%d_bkgidx%d" % (dsNum, bkgIdx),
                                 False, calDB, pars)

            chThreshList = (ch for ch in chList
                            if ch in thD.keys())  # python generator expression
            for ch in chThreshList:

                mu, sig = thD[ch][0], thD[ch][1]
                if mu > 5 or mu == 0:
                    print "Threshold for ch%d bkgidx%d zero or too high, skipping" % (
                        ch, bkgIdx)
                    continue
                # print bkgIdx, ch, mu, sig

                # threshold efficiency
                yThresh = 0.5 * (1 + sp.erf(
                    (xThresh - mu) / (2**(.5) * abs(sig))))

                # scale by exposure
                yThresh *= expDict[ch][bkgIdx]

                yThreshTot += yThresh

                # # Scale here
                # h3.Multiply(threshFnc)
                # if ch in threshDict.keys():
                #     threshDict[ch].Add(h3)
                # else:
                #     threshDict[ch] = h3.Clone("Efficiency_ch%d"%(ch))

            # get the cut coverage
            runLo = bkgRuns[bkgIdx][0]
            runHi = bkgRuns[bkgIdx][-1]
            calIdxLo = calInfo.GetCalIdx("ds%d_m%d" % (dsNum, modNum), runLo)
            calIdxHi = calInfo.GetCalIdx("ds%d_m%d" % (dsNum, modNum), runHi)
            # if calIdxHi != calIdxLo:
            # print "DS-%d  bkgIdx %d  Multiple calIdxs: %d - %d" % (dsNum, bkgIdx, calIdxLo, calIdxHi)
            for calIdx in range(calIdxLo, calIdxHi + 1):

                xTicks.append(bkgIdx + 0.1 * (calIdx - calIdxLo))

                calRunLo = calInfo.master["ds%d_m%d" %
                                          (dsNum, modNum)][calIdx][1]
                calRunHi = calInfo.master["ds%d_m%d" %
                                          (dsNum, modNum)][calIdx][2]

                fsD = ds.getDBRecord(
                    "fitSlo_ds%d_idx%d_m%d_Peak" % (dsNum, calIdx, modNum),
                    False, calDB, pars)
                rnD = ds.getDBRecord(
                    "riseNoise_ds%d_idx%d_m%d_SoftPlus" %
                    (dsNum, calIdx, modNum), False, calDB, pars)
                wfD = ds.getDBRecord(
                    "wfstd_ds%d_idx%d_mod%d" % (dsNum, calIdx, modNum), False,
                    calDB, pars)

                goodFS = True if fsD != 0 else False
                goodRN = True if rnD != 0 else False
                goodWF = True if wfD != 0 else False

                print "DS%d  bkgIdx %d  calIdx %d  FS %d  RN %d  WF %d" % (
                    dsNum, bkgIdx, calIdx, int(goodFS), int(goodRN),
                    int(goodWF))

                for ch in chList:

                    goodChanFS = True if goodFS and fsD[ch][2] > 0 else False
                    goodChanRN = True if goodRN and rnD[ch][3] > 0 else False
                    goodChanWF = True if goodWF and ch in wfD.keys(
                    ) and wfD[ch][0] == u'y' else False

                    goodList = [goodChanFS, goodChanRN, goodChanWF]
                    code = getErrorCode(goodList)
                    chStats[ch].append(code)

        # -- Create efficiency plot --
        f1 = plt.figure(figsize=(9, 6), facecolor='w')
        ax = plt.subplot(111)
        ax.plot(xThresh, yThreshTot, "r")
        ax.set_xlabel('Energy (keV)', x=1., ha='right')
        ax.set_ylabel('Exposure (kg-d)')
        ax.set_title("DS-%d, Module %d" % (dsNum, modNum))
        plt.savefig("../plots/expo-ds%d-m%d.pdf" % (dsNum, modNum))

        # -- Create bkgIdx vs. channel plot --

        chData = np.asarray([chStats[ch] for ch in chStats])
        xTicks = [int(t) if float(t).is_integer() else t for t in xTicks]
        # yTicks = [ch for ch in chStats]
        yTicks = [
            "P%sD%s" % (str(ds.CPD[dsNum][ch])[1], str(ds.CPD[dsNum][ch])[2])
            for ch in chStats
        ]

        a, b = int(len(xTicks) / 3.), int(len(yTicks) / 2.)
        if a < 12: a = 12
        if b < 8: b = 8
        print "a", a, "b", b

        f2 = plt.figure(figsize=(a, b), facecolor='w')
        ax = plt.subplot(111)

        im = plt.imshow(np.asarray(chData), interpolation='nearest')
        ax.set_xlabel('bkgIdx', x=1., ha='right')
        ax.set_ylabel('Detector', rotation=0)
        ax.yaxis.set_label_coords(-0.01, 1.01)
        ax.set_title("DS-%d, Module %d" % (dsNum, modNum))

        # Set the major ticks at the centers and minor tick at the edges
        xlocs = np.arange(0, len(xTicks), 1)
        ax.xaxis.set_ticks(xlocs + 0.5, minor=True)
        ax.xaxis.set(ticks=xlocs, ticklabels=xTicks)
        ax.tick_params(axis='x', labelsize=8)
        ylocs = np.arange(0, len(yTicks), 1)
        ax.yaxis.set_ticks(ylocs + 0.5, minor=True)
        ax.yaxis.set(ticks=ylocs, ticklabels=yTicks)
        ax.grid(True, which='minor')

        values = np.unique(chData.ravel())
        colors = [im.cmap(im.norm(value)) for value in values]

        patches = []
        for idx, val in enumerate(values):
            tmp = unpackErrorCode(val)
            lab = "FS %d RN %d WF %d" % (int(tmp[0]), int(tmp[1]), int(tmp[2]))
            patches.append(mpatches.Patch(color=colors[idx], label=lab))

        box = ax.get_position()
        ax.set_position([box.x0, box.y0, box.width * 0.85, box.height])
        ax.legend(handles=patches,
                  bbox_to_anchor=(1, 1),
                  loc=2,
                  borderaxespad=0.)

        plt.savefig("../plots/dc-results-DS%d-M%d.pdf" % (dsNum, modNum))
예제 #11
0
파일: fitRatio.py 프로젝트: semrat/Analyzer
def main():
    path = "condor/Analyze1Lep_Kerasv1.2.4_HTSF/"
    histoNames = [
        #"blind_ht_1l_4j_ge1b", "blind_ht_1l_5j_ge1b", "blind_ht_1l_6j_ge1b", "blind_ht_1l_ge7j_ge1b",
        "h_ht_1l_5j_ge1b",
        "h_ht_1l_6j_ge1b",
        "h_ht_1l_7j_ge1b",
        "h_ht_1l_8j_ge1b"
        #"blind_deepESM_1l_4j_ge1b", "blind_deepESM_1l_5j_ge1b", "blind_deepESM_1l_6j_ge1b", "blind_deepESM_1l_ge7j_ge1b",
    ]
    nJets = []
    norm = []
    expo = []

    nJet = 5
    for histoName in histoNames:
        c = ROOT.TCanvas("c", "c", 0, 0, 800, 800)
        ROOT.gPad.SetLeftMargin(0.12)
        ROOT.gPad.SetRightMargin(0.15)
        ROOT.gPad.SetTopMargin(0.08)
        ROOT.gPad.SetBottomMargin(0.12)
        ROOT.gPad.SetTicks(1, 1)
        ROOT.TH1.SetDefaultSumw2()
        ROOT.gStyle.SetOptFit()

        data = info.DataSetInfo(basedir="condor/Analyze1Lep_Kerasv1.2.5_HTSF/",
                                fileName="2016_Data.root",
                                label="Data")
        bg = info.DataSetInfo(basedir=path,
                              fileName="2016_AllBG.root",
                              label="Background")
        dHist = data.getHisto(histoName)
        bHist = bg.getHisto(histoName)
        dHist.Rebin(10)
        bHist.Rebin(10)
        dHist.Scale(1.0 / dHist.Integral())
        bHist.Scale(1.0 / bHist.Integral())

        ratio = dHist.Clone("Ratio")
        ratio.Divide(bHist)
        ratio.SetMaximum(2.0)
        ratio.SetMinimum(0.0)
        ratio.SetLineColor(ROOT.kBlack)
        ratio.SetMarkerColor(ROOT.kBlack)
        ratio.SetMarkerStyle(21)
        ratio.SetTitle(histoName + " Ratio")
        ratio.GetYaxis().SetTitle("Data/BG")
        ratio.Draw("ep")

        line = ROOT.TF1("1", "1", -2000, 20000)
        line.SetLineColor(ROOT.kBlack)
        line.Draw("same")

        ratio.GetXaxis().SetTitle("H_{T} [GeV]")
        fit = ROOT.TF1("fit", "[0]*exp([1]*x)", 300.0, 3000.0, 2)
        #fit.SetParLimits(0,  -1.0, 1.0)
        #fit.SetParLimits(1,  -2.0, 2.0)
        #fit.SetParameter(0, -1.0)
        #fit.SetParameter(1,  0.0)
        fit.SetLineColor(ROOT.kRed)
        ratio.Fit(fit, "", "", 300.0, 3000.0)
        fit.Draw("same")

        #ratio.GetXaxis().SetTitle("DeepESM Score")
        #fit = ROOT.TF1("fit", "[0]*x + [1]", 0.0, 1.0, 2)
        #fit.SetParLimits(0,  -1.0, 1.0)
        #fit.SetParLimits(1,  -2.0, 2.0)
        #fit.SetParameter(0, -1.0)
        #fit.SetParameter(1,  0.0)
        #fit.SetLineColor(ROOT.kRed)
        #ratio.Fit(fit, "", "", 0.0, 1.0)
        #fit.Draw("same")

        nJets.append(nJet)
        norm.append(fit.GetParameter(0))
        expo.append(fit.GetParameter(1))

        c.SaveAs(histoName + "_ratio.pdf")
        del c
        nJet += 1

    ####################################################
    #Fit results of all the fits
    ####################################################
    nJets = np.array(nJets)
    norm = np.array(norm)
    expo = 1000 * np.array(expo)
    print nJets
    print norm
    print expo

    s = ROOT.TGraph(nJets.size, nJets.astype(np.double),
                    norm.astype(np.double))
    y = ROOT.TGraph(nJets.size, nJets.astype(np.double),
                    expo.astype(np.double))

    List = [
        ("Norm Term", s, 0.0, 2.0, -5, 5, -1),
        #("expo",y, -0.0005, 0.0, -0.002, 0.0, -0.0001)]
        ("Exp Term", y, -1, 0.0, -5, 5, -1)
    ]
    for l in List:
        print np.max(l[2])
        c1 = ROOT.TCanvas("c1", "c1", 0, 0, 800, 800)
        ROOT.gPad.SetLeftMargin(0.12)
        ROOT.gPad.SetRightMargin(0.15)
        ROOT.gPad.SetTopMargin(0.08)
        ROOT.gPad.SetBottomMargin(0.12)
        ROOT.gPad.SetTicks(1, 1)
        ROOT.TH1.SetDefaultSumw2()
        ROOT.gStyle.SetOptFit()

        l[1].SetMaximum(l[3])
        l[1].SetMinimum(l[2])
        l[1].SetLineColor(ROOT.kBlack)
        l[1].SetMarkerColor(ROOT.kBlack)
        l[1].SetMarkerStyle(21)
        l[1].SetTitle(l[0] + " Fits")
        l[1].GetXaxis().SetTitle("NJet")
        l[1].GetYaxis().SetTitle(l[0])
        l[1].Draw("AP")

        fit = ROOT.TF1("fit", "[0]*x + [1]", 0.0, 10.0, 3)
        fit.SetParLimits(0, l[4], l[5])
        fit.SetParLimits(1, -5.0, 5.0)
        fit.SetParameter(0, l[6])
        fit.SetParameter(1, 0.0)
        fit.SetLineColor(ROOT.kRed)
        l[1].Fit(fit, "M", "", 0.0, 10.0)
        fit.Draw("same")

        c1.SaveAs(l[0] + "_fits.pdf")
        del c1

    ####################################################
    #Plot Scale Factor per nJet
    ####################################################
    c = ROOT.TCanvas("c", "c", 0, 0, 800, 800)
    ROOT.gPad.SetLeftMargin(0.12)
    ROOT.gPad.SetRightMargin(0.15)
    ROOT.gPad.SetTopMargin(0.08)
    ROOT.gPad.SetBottomMargin(0.12)
    ROOT.gPad.SetTicks(1, 1)
    ROOT.TH1.SetDefaultSumw2()
    ROOT.gStyle.SetOptStat(0)

    leg = ROOT.TLegend(0.6, 0.65, 0.9, 0.88)
    leg.SetFillStyle(0)
    leg.SetBorderSize(0)
    leg.SetLineWidth(1)
    leg.SetNColumns(1)
    leg.SetTextFont(42)

    h = ROOT.TH1F("Ht Scale Factor", "Ht Scale Factor", 1, 0, 5000)
    h.GetXaxis().SetTitle("Ht [GeV]")
    h.GetYaxis().SetTitle("Scale Factor")
    h.SetMaximum(1.6)
    h.Draw()

    dummyList = []
    for nJet in [(ROOT.kCyan + 1, "7"), (ROOT.kCyan + 2, "8"),
                 (ROOT.kCyan + 3, "9"), (ROOT.kCyan + 4, "10"),
                 (ROOT.kRed, "11"), (ROOT.kRed + 1, "12"),
                 (ROOT.kRed + 2, "13"), (ROOT.kRed + 3, "14")]:
        sf = ROOT.TF1(
            "sf" + nJet[1], "(0.06146*" + nJet[1] + "+0.7908)*exp((-0.06063*" +
            nJet[1] + "+0.1018)*(x/1000))", 0.0, 5000)
        sf.SetLineColor(nJet[0])
        #sf.SetMarkerColor(nJet[0])
        sf.DrawCopy("l same")

        hd = ROOT.TH1F("Ht Scale Factor" + nJet[1],
                       "Ht Scale Factor" + nJet[1], 1, 0, 5000)
        hd.Draw("P same")
        hd.SetLineColor(nJet[0])
        hd.SetLineWidth(3)
        leg.AddEntry(hd, "NJet " + nJet[1], "l")
        dummyList.append(hd)

    leg.Draw()
    c.SaveAs("sf.pdf")

    ####################################################
    #Plot Scale Factor per nJet
    ####################################################
    cc = ROOT.TCanvas("cc", "cc", 0, 0, 800, 800)
    ROOT.gPad.SetLeftMargin(0.12)
    ROOT.gPad.SetRightMargin(0.15)
    ROOT.gPad.SetTopMargin(0.08)
    ROOT.gPad.SetBottomMargin(0.12)
    ROOT.gPad.SetTicks(1, 1)
    ROOT.TH1.SetDefaultSumw2()
    ROOT.gStyle.SetOptStat(0)
    #ROOT.gPad.SetLogy()

    leg = ROOT.TLegend(0.2, 0.7, 0.85, 0.88)
    leg.SetFillStyle(0)
    leg.SetBorderSize(0)
    leg.SetLineWidth(1)
    leg.SetNColumns(1)
    leg.SetTextFont(42)

    h = ROOT.TH1F("Ht Scale Factor: 8 Jet Bin", "Ht Scale Factor: 8 Jet Bin",
                  1, 0, 5000)
    h.GetXaxis().SetTitle("Ht [GeV]")
    h.GetYaxis().SetTitle("Scale Factor")
    h.SetMaximum(1.6)
    h.Draw()

    nJet = (ROOT.kCyan + 2, "8")
    sf = ROOT.TF1(
        "a", "(0.06146*" + nJet[1] + "+0.7908)*exp((-0.06063*" + nJet[1] +
        "+0.1018)*(x/1000))", 0.0, 5000)
    sf.SetLineColor(ROOT.kRed)
    #sf.SetMarkerColor(ROOT.kRed)
    leg.AddEntry(sf, "Extrapolation: 8 Jet bin", "l")
    sf.DrawCopy("l same")

    sffit = ROOT.TF1("b", "1.353*exp(-0.0003955*x)", 0.0, 5000)
    sffit.SetLineColor(ROOT.kBlack)
    leg.AddEntry(sffit, "Fit: 8 Jet bin", "l")
    sffit.DrawCopy("l same")

    #sfnew = ROOT.TF1("sf extrapolation: 8 Jet bin", "(0.07474*8+0.7368)*exp((-0.04826*8-0.004357)*(x/1000))", 0.0, 5000)
    #sfnew.SetLineColor(ROOT.kRed)
    #leg.AddEntry(sfnew, "sf extrapolation: 8 Jet bin", "l")
    #sfnew.DrawCopy("l same")

    leg.Draw()
    cc.SaveAs("sf_8Jetbin.pdf")
    del cc

    ####################################################
    #Plot Scale Factor Up and Down variation
    ####################################################
    cc = ROOT.TCanvas("cc", "cc", 0, 0, 800, 800)
    ROOT.gPad.SetLeftMargin(0.12)
    ROOT.gPad.SetRightMargin(0.15)
    ROOT.gPad.SetTopMargin(0.08)
    ROOT.gPad.SetBottomMargin(0.12)
    ROOT.gPad.SetTicks(1, 1)
    ROOT.TH1.SetDefaultSumw2()
    ROOT.gStyle.SetOptStat(0)
    #ROOT.gPad.SetLogy()

    leg = ROOT.TLegend(0.5, 0.8, 0.85, 0.88)
    leg.SetFillStyle(0)
    leg.SetBorderSize(0)
    leg.SetLineWidth(1)
    leg.SetNColumns(1)
    leg.SetTextFont(42)

    h = ROOT.TH1F("Dummy", "HT Scale Factor Variation", 1, 0, 5000)
    h.GetXaxis().SetTitle("Ht [GeV]")
    h.GetYaxis().SetTitle("A.U.")
    h.SetMinimum(0.9)
    h.SetMaximum(1.1)
    h.Draw()

    ratio = ROOT.TF1("asf", "b/a", 0.0, 5000)
    ratio.DrawCopy("l same")
    leg.AddEntry(ratio, "Variation Up", "l")

    ratio2 = ROOT.TF1("yep", "a/b", 0.0, 5000)
    ratio2.SetLineColor(ROOT.kBlack)
    ratio2.DrawCopy("l same")
    leg.AddEntry(ratio2, "Variation Down", "l")

    leg.Draw()
    cc.SaveAs("sf_8Jetbin_Up_Down.pdf")
    del cc
예제 #12
0
def burstCut(dsNum):
    """ ./tuneCuts.py [dsNum] -burst """

    # rates = {0:(30,5), 1:(20,5), 3:(50,5), 4:(20,3), 5:(150.,5.)} # v1 - before fitSLo
    rates = {
        0: (20, 5),
        1: (20, 5),
        3: (20, 5),
        4: (20, 5),
        5: (40., 5)
    }  # v2 - after fitSlo

    chList = ds.GetGoodChanList(dsNum)
    nDets = len(chList)
    maxRate = rates[dsNum][0]
    maxChanRate = rates[dsNum][0] * rates[dsNum][1] / float(nDets)

    print "maxRate %d  nDets %d  factor %d  maxChanRate %.2f" % (
        rates[dsNum][0], nDets, rates[dsNum][1], maxChanRate)
    energyCut = "trapENFCal >= 1"

    ignoreList = {0: [656], 3: [592, 692], 4: [1332], 5: [692, 1232, 1124]}
    bkg = ROOT.TChain("skimTree")
    for ch in chList:
        if ch not in ignoreList[dsNum]:
            f = "~/project/latskim/latSkimDS%d_ch%d.root" % (dsNum, ch)
            print "Added", f
            bkg.Add(f)

    # append to the text file (input to ds_livetime.cc)
    ds_livetimeList = open("burstCut_v1.txt", 'a')
    for key in ignoreList:
        killChs = ""
        for val in ignoreList[key]:
            killChs += " %d " % val
        ds_livetimeList.write("%s %s \n" % (key, killChs))

    c0 = ROOT.TCanvas("c0", "c0", 1000, 600)

    rlo, rhi = ds.dsRanges[dsNum][0], ds.dsRanges[dsNum][1]

    clo, chi = 570, 700
    if dsNum == 4: clo, chi = 1100, 1400
    if dsNum == 5: clo, chi = 570, 1400

    h0 = wl.H2D(bkg, rhi - rlo, rlo, rhi, chi - clo, clo, chi, "channel:run",
                energyCut, "Run Number", "Channel")
    h0.Draw("colz")
    c0.SetLogz(1)

    c0.Print("./plots/burst/channelRateDS%d.pdf" % dsNum)

    c1 = ROOT.TCanvas("c", "c", 1600, 600)
    c1.Divide(2, 1, 0)

    c1.cd(1)
    ROOT.gPad.SetLogy(1)

    h1 = wl.H1D(bkg, rhi - rlo, rlo, rhi, "run", energyCut, "Run Number",
                "Counts")
    h1.SetLineColor(ROOT.kRed)
    h1.Draw()

    # Run & channel-based burst cut.

    runs, rates = wl.npTH1D(h1, "i")
    idx = np.where(rates > maxRate)
    print "Noisy runs:", runs[idx]

    burstCut, invBurstCut = energyCut + " && ", energyCut + " && ("

    print "maxChanRate:", maxChanRate
    for i, run in enumerate(runs[idx]):

        runCut = " && run==%d" % run
        # h2.append(ROOT.TH1D())

        h2 = wl.H1D(bkg, chi - clo, clo, chi, "channel", energyCut + runCut,
                    "channel", "Counts")

        if h2.GetEntries() == 0:
            continue

        chans, chRates = wl.npTH1D(h2, "i")
        idx2 = np.where(chRates > maxChanRate)
        print "run", int(run), "noisy chans", chans[idx2], "rates", chRates[
            idx2], "total entries", h2.GetEntries(
            ), "runCut:", energyCut + runCut

        # Write run + channel groups to the file (input to ds_livetime.cc)
        noisyChans = ""
        for ch in chans[idx2]:
            noisyChans += "%d " % ch
        ds_livetimeList.write("%d %s \n" % (run, noisyChans))

        # Make the TCut
        runBurst = "&& !(run==%d && (" % run
        invBurst = "|| (run==%d && (" % run
        if i == 0:
            runBurst = runBurst[3:]
            invBurst = invBurst[3:]

        for ch in chans[idx2]:
            runBurst += "channel==%d|| " % ch
            invBurst += "channel==%d|| " % ch
        runBurst = runBurst[:-3] + ")) "
        invBurst = invBurst[:-3] + ")) "
        burstCut += runBurst
        invBurstCut += invBurst

    invBurstCut += ")"

    ds_livetimeList.close()

    if len(runs[idx]) == 0:
        burstCut, invBurstCut = energyCut, energyCut

    # add the dead channels back in
    for ch in ignoreList[dsNum]:
        burstCut += " && channel!=%d" % ch

    print "\nBURST CUT:"
    print burstCut

    print "\nINVERSE BURST CUT:"
    print invBurstCut
    print ""

    h1a = wl.H1D(bkg, rhi - rlo, rlo, rhi, "run", burstCut)
    h1a.SetLineColor(ROOT.kBlue)
    h1a.Draw("same")

    c1.cd(2)
    ROOT.gPad.SetLogy(1)

    h1.Draw("hist")

    h1b = wl.H1D(bkg, rhi - rlo, rlo, rhi, "run", invBurstCut)
    h1b.SetLineColor(ROOT.kBlue)
    h1b.Draw("hist same")

    c1.Print("./plots/burst/burstCutDS%d.pdf" % dsNum)

    c2 = TCanvas("c2", "c2", 1000, 600)
    c2.SetLogy(1)

    eb, elo, ehi = 150, 0, 30  # 5 bins/kev

    h2 = wl.H1D(bkg, eb, elo, ehi, "trapENFCal", "", "Energy", "Counts")
    h2.SetLineColor(ROOT.kRed)

    h3 = wl.H1D(bkg, eb, elo, ehi, "trapENFCal", burstCut)
    h3.SetLineColor(ROOT.kBlue)

    h2.Draw("hist")
    h3.Draw("hist same")

    c2.Print("./plots/burst/energySpecDS%d.pdf" % dsNum)
예제 #13
0
def SaveHistogramsIDX(dType='Bkg', binsize=0.1, lower=0, upper=250):
    """
        Saves background or calibration data before and after cuts into histograms
    """

    outDir = '/projecta/projectdirs/majorana/users/bxyzhu/LATv2/plots/spectra'
    calDir = '/projecta/projectdirs/majorana/users/wisecg/cal-lat'
    bkgDir = '/projecta/projectdirs/majorana/users/wisecg/bg-lat'
    bkgcutDir = '/projecta/projectdirs/majorana/users/wisecg/cuts'
    calcutDir = '/projecta/projectdirs/majorana/users/bxyzhu/cuts'
    cInfo = ds.CalInfo()
    bins = int((upper - lower) / binsize)
    # Basic cut
    mNum = 1
    cuts = "gain==0 && mHL=={} && isGood && !muVeto && !(C==1&&isLNFill1) && !(C==2&&isLNFill2) && C!=0&&P!=0&&D!=0" % (
        mNum)
    skimTreeCal = ROOT.TChain("skimTree")
    skimTreeBkg = ROOT.TChain("skimTree")
    skimCutCal = ROOT.TChain("skimTree")
    skimCutBkg = ROOT.TChain("skimTree")
    dsList = [0, 1, 2, 3, 4, 5]
    outFile = ROOT.TFile(outDir + "/AThresh_mHL{}.root" % (mNum), "RECREATE")
    outFile.cd()

    for iDS, dsNum in enumerate(dsList):
        nMods = [1]
        if dsNum == 4: nMods = [2]
        if dsNum == 5: nMods = [1, 2]
        for modNum in nMods:
            chList = ds.GetGoodChanList(dsNum)
            if dsNum == 5 and modNum == 1:  # remove 692 and 1232 (both beges, so who cares)
                chList = [
                    584, 592, 598, 608, 610, 614, 624, 626, 628, 632, 640, 648,
                    658, 660, 662, 672, 678, 680, 688, 690, 694
                ]
            elif dsNum == 5 and modNum == 2:
                chList = [
                    1106, 1110, 1120, 1124, 1128, 1170, 1172, 1174, 1176, 1204,
                    1208, 1298, 1302, 1330, 1332
                ]
            # Total channel histograms, split by dataset
            nRangesCal = [
                0, len(cInfo.master['ds{}_m{}'.format(dsNum, modNum)])
            ]
            nRangesBkg = [0, ds.dsMap[dsNum]]

            if dType == 'Cal':
                for calidx in range(nRangesCal[0], nRangesCal[1]):
                    print("Drawing DS{} calidx{} mod{}".format(
                        dsNum, calidx, modNum))
                    hCutList, hFullList = [], []
                    skimTreeCal.Reset()
                    calList = cInfo.GetCalList("ds{}_m{}".format(
                        dsNum, modNum),
                                               calidx,
                                               runLimit=10)
                    for run in calList:
                        skimTreeCal.Add("{}/latSkimDS{}_run{}_*.root".format(
                            calDir, dsNum, run))

                    for idx, ch in enumerate(chList):
                        # Reset Tree every calidx + ch
                        skimCutCal.Reset()
                        if not os.path.exists(
                                "{}/calfs_rn/calfs_rn-DS{}-{}-ch{}.root".
                                format(calcutDir, dsNum, calidx, ch)):
                            hCutList.append(ROOT.TH1D())
                            hFullList.append(ROOT.TH1D())
                            print(
                                "Channel {}, calidx {} doesn't exist, skipping"
                                .format(ch, calidx))
                            continue
                        skimCutCal.Add(
                            "{}/calfs_rn/calfs_rn-DS{}-{}-ch{}.root".format(
                                calcutDir, dsNum, calidx, ch))
                        hCutList.append(ROOT.TH1D())
                        hFullList.append(ROOT.TH1D())

                        # Add additional cut here for channel
                        hCutList[idx] = wl.H1D(
                            skimCutCal,
                            bins,
                            lower,
                            upper,
                            "trapENFCal",
                            cuts + "&& channel=={}".format(ch),
                            Title="hCalDS{}_Ch{}_CalIdx{}".format(
                                dsNum, ch, calidx),
                            Name="hDS{}_Ch{}_CalIdx{}".format(
                                dsNum, ch, calidx))
                        hFullList[idx] = wl.H1D(
                            skimTreeCal,
                            bins,
                            lower,
                            upper,
                            "trapENFCal",
                            cuts + "&&channel=={}".format(ch),
                            Title="hFullDS_{}_Ch{}_CalIdx{}".format(
                                dsNum, ch, calidx),
                            Name="hCalFullDS{}_Ch{}_CalIdx{}".format(
                                dsNum, ch, calidx))

                        # Write all histograms (even if they're empty -- for debugging purposes)
                        hCutList[idx].Write()
                        hFullList[idx].Write()
            if dType == 'Bkg':
                for bkgidx in range(nRangesBkg[0], nRangesBkg[1] + 1):
                    print("Drawing DS{} bkgidx{} mod{}".format(
                        dsNum, bkgidx, modNum))
                    hCutList, hFullList = [], []
                    skimTreeBkg.Reset()
                    skimTreeBkg.Add("{}/latSkimDS{}_{}_*.root".format(
                        bkgDir, dsNum, bkgidx))

                    for idx, ch in enumerate(chList):
                        # Reset Tree every bkgidx + ch
                        skimCutBkg.Reset()
                        if not os.path.exists(
                                "{}/fs_rn/fs_rn-DS{}-{}-ch{}.root".format(
                                    bkgcutDir, dsNum, bkgidx, ch)):
                            hCutList.append(ROOT.TH1D())
                            hFullList.append(ROOT.TH1D())
                            print(
                                "Channel {}, bkgidx {} has no entries, skipping"
                                .format(ch, bkgidx))
                            continue
                        skimCutBkg.Add(
                            "{}/fs_rn/fs_rn-DS{}-{}-ch{}.root".format(
                                bkgcutDir, dsNum, bkgidx, ch))

                        hCutList.append(ROOT.TH1D())
                        hFullList.append(ROOT.TH1D())

                        # Add additional cut here for channel
                        hCutList[idx] = wl.H1D(
                            skimCutBkg,
                            bins,
                            lower,
                            upper,
                            "trapENFCal",
                            cuts + "&& channel=={}".format(ch),
                            Title="hBkgDS{}_Ch{}_BkgIdx{}".format(
                                dsNum, ch, bkgidx),
                            Name="hDS{}_Ch{}_BkgIdx{}".format(
                                dsNum, ch, bkgidx))
                        hFullList[idx] = wl.H1D(
                            skimTreeBkg,
                            bins,
                            lower,
                            upper,
                            "trapENFCal",
                            cuts + "&&channel=={}".format(ch),
                            Title="hFullDS_{}_Ch{}_BkgIdx{}".format(
                                dsNum, ch, bkgidx),
                            Name="hBkgFullDS{}_Ch{}_BkgIdx{}".format(
                                dsNum, ch, bkgidx))

                        # Write all histograms -- for debugging
                        hCutList[idx].Write()
                        hFullList[idx].Write()

    # Write total histogram and close
    outFile.Close()
    return 0
예제 #14
0
def fitDBRiseNoise():
    """ ./lat2.py -fitRN

    For each dataset, for every channel, for every calIdx:
    pull the 95% riseNoise value for 50-90, 90-130, 130-170, 170-210.
    Then do a simple linear fit and save the channel value.
    """

    dsNum = 1

    # parse database
    # { recordName : { chan : 95% value } }  - recordName is idxN-e50-90, etc

    dbDict = {}
    calDB = db.TinyDB('calDB.json')
    for item in calDB:
        d = dict(item)
        key = d["key"]
        vals = d["vals"]
        tmp = key.split("_")
        tmp = [str(t) for t in tmp]

        # ex - ['riseNoise', 'ds1', 'idx12', 'm1', '130', '170']
        if tmp[0] == "riseNoise" and tmp[1] == "ds%d" % dsNum and tmp[
                4] != "Peak" and tmp[4] != "Continuum":
            calIdx = int(tmp[2][3:])
            eLo, eHi = int(tmp[4]), int(tmp[5])
            recName = "idx%s-%d-%d" % (calIdx, eLo, eHi)
            recDict = {}
            for ch in vals:
                chan = int(ch)
                val95 = vals[ch][3]  # ex - [1%, 5%, 90%, 95%, 99%]
                recDict[chan] = val95
            dbDict[recName] = recDict

    dsNum, module = 1, 1
    if dsNum == 4: module = 2

    nIdx = ds.getNCalIdxs(dsNum, module)

    # for plots
    # { chan : { calIdx : [vals] } }
    # plotDict = {}

    # for ch in ds.GetGoodChanList(dsNum):
    ch = 578

    calIdx = 0

    chEne = [70, 110, 150, 190]

    chVals = []
    chVals.append(dbDict["idx%d-%d-%d" % (calIdx, 50, 90)][ch])
    chVals.append(dbDict["idx%d-%d-%d" % (calIdx, 90, 130)][ch])
    chVals.append(dbDict["idx%d-%d-%d" % (calIdx, 130, 170)][ch])
    chVals.append(dbDict["idx%d-%d-%d" % (calIdx, 170, 210)][ch])

    chEne, chVals = np.array(chEne), np.array(chVals)

    slope, yint, r_val, p_val, std_err = linregress(chEne, chVals)

    # plt.figure()
    # plt.plot(chEne, chVals, 'o', label='riseNoise vals')
    # plt.plot(chEne, yint + slope*chEne, 'r', label='fit')
    # plt.legend(loc="best")
    # plt.xlabel("Energy (keV)")
    # plt.ylabel("riseNoise 95% val")
    # plt.show()
    # plt.savefig("./plots/linFit.png")

    print "ch", ch, "vals:", chVals, slope, yint, r_val, p_val, std_err

    # build a db record
    # key: [Name]_ds[i]_idx[j]_module[k]_[descriptor]
    # vals: {[chan] : [slope, yint]} --> need a value for every channel.
    # fk.  the linear fit is going to overcut. i don't even want to use it.

    rnKey = "riseNoise_ds%d_idx%d_m%d_idx%d-ch%d"

    return
예제 #15
0
def GenerateCorrectedSpectra(dsNum=1,
                             dType='isNat',
                             binsize=0.1,
                             binsize2=0.001,
                             lower=0,
                             upper=250):
    """
        Calculates analysis threshold and applies analysis threshold to exposure calculation
        Saves histograms into ROOT file
    """

    ROOT.gStyle.SetOptStat(0)
    bgDir, outDir = '/Users/brianzhu/project/cuts/fs_rn', '/Users/brianzhu/macros/code/LAT/plots/AThresh'
    bins = int((upper - lower) / binsize)
    bins2 = int((upper - lower) / binsize2)
    cuts = "{} && gain==0 && mHL==1 && isGood && !muVeto && !(C==1&&isLNFill1) && !(C==2&&isLNFill2) && C!=0&&P!=0&&D!=0".format(
        dType)

    chList = ds.GetGoodChanList(dsNum, dType[2:])
    if dsNum == 5:  # remove 692 and 1232 (both beges, so who cares)
        if 692 in chList: chList.remove(692)
        if 1232 in chList: chList.remove(1232)

    nRanges = [0, ds.dsMap[dsNum]]
    if dsNum == 5: nRanges[0] = 80  # exclude DS-5A

    athresh = GetAnaylsisThreshold(dsNum, True)
    threshDict = {}
    threshDictSave = {}
    specIDXDict = {}
    specDict = {}
    outFile = ROOT.TFile(
        outDir + '/Bkg_{}_DS{}_Test.root'.format(dType, dsNum), "RECREATE")
    cutTree = ROOT.TChain("skimTree")
    TotalSpec = ROOT.TH1D('DS{}_{}_Corr'.format(dsNum, dType), '', bins, lower,
                          upper)
    UncorrTotalSpec = ROOT.TH1D('DS{}_{}_UnCorr'.format(dsNum, dType), '',
                                bins, lower, upper)
    for bkgidx in range(nRanges[0], nRanges[1] + 1):
        # Get Threshold dictionary
        tD = ds.getDBRecord("thresh_ds{}_bkgidx{}".format(dsNum, bkgidx))

        # Get Analysis Threshold Dictionary and Exposure Dictionary here
        for idx, ch in enumerate(chList):
            if ch in tD:
                # Reset Tree
                cutTree.Reset()

                # Check dictionaries to see if variables exist
                if ch not in athresh[bkgidx]:
                    print(
                        "Warning: Analysis threshold doesn't exist for ch{} bkgidx{}"
                        .format(ch, bkgidx))
                    continue

                if ch not in ex.Exposure[dsNum][bkgidx]:
                    print("Warning: Exposure doesn't exist for ch{} bkgidx{}".
                          format(ch, bkgidx))
                    continue

                if not os.path.exists(
                        bgDir +
                        "/fs_rn-DS{}-{}-ch{}.root".format(dsNum, bkgidx, ch)):
                    print(
                        "Warning: Background data doesn't exist for ch{} bkgidx{}"
                        .format(ch, bkgidx))
                    continue

                # Load Background Data with cuts applied
                cutTree.Add(
                    bgDir +
                    "/fs_rn-DS{}-{}-ch{}.root".format(dsNum, bkgidx, ch))

                # Create Trigger Efficiency function
                mu, sigma = tD[ch][0], tD[ch][1]
                threshFnc = ROOT.TF1(
                    "fEff_{}_{}_{}".format(dsNum, ch, bkgidx),
                    "0.5*(1+TMath::Erf((x-[0])/(TMath::Sqrt(2)*[1])))", 0, 250)
                threshFnc.SetParameters(mu, abs(sigma))

                # Create Analysis Threshold + Exposure function
                dExp, dAThresh = ex.Exposure[dsNum][bkgidx][ch], athresh[
                    bkgidx][ch]
                expFnc = ROOT.TF1("fExp_{}_{}_{}".format(dsNum, ch, bkgidx),
                                  "[0]*(x>[1])", 0, 250)
                expFnc.SetParameters(dExp, dAThresh)

                # Print out info
                print(
                    "DS{} Ch{} BkgIdx{}  Exposure {}  Thresh {}  Analysis Threshold {} "
                    .format(dsNum, ch, bkgidx, dExp, mu, dAThresh))

                specIDXDict.setdefault(bkgidx, {}).setdefault(ch, ROOT.TH1D())
                specIDXDict[bkgidx][ch] = wl.H1D(
                    cutTree,
                    bins,
                    lower,
                    upper,
                    "trapENFCal",
                    cuts +
                    "&& trapENFCal>{:.2f} && channel=={}".format(dAThresh, ch),
                    Title="hBkg_Ch{}_Bkgidx{}".format(ch, bkgidx),
                    Name="hBkg_Ch{}_Bkgidx{}".format(ch, bkgidx))

                # Exposure function for scaling
                h3 = ROOT.TH1D("hCh{}_Bkgidx{}_Scale".format(ch, bkgidx), "",
                               bins, lower, upper)
                for i in range(h3.GetNbinsX() + 1):
                    if i < dAThresh * 10:
                        continue  # Round up to set analysis threshold
                    h3.SetBinContent(i, dExp)
                # Scale here -- trying to make it legible instead of messy
                h3.Multiply(threshFnc)
                threshDict.setdefault(ch,
                                      h3.Clone("Efficiency_ch{}".format(ch)))
                threshDict[ch].Add(h3)

                # Save exposure function to histogram
                h4 = ROOT.TH1D("hCh{}_Bkgidx{}".format(ch, bkgidx), "", bins2,
                               lower, upper)
                for i in range(h4.GetNbinsX() + 1):
                    if i < dAThresh * 1000:
                        continue  # Round up to set analysis threshold
                    h4.SetBinContent(i, dExp)
                # Scale here
                h4.Multiply(threshFnc)
                threshDictSave.setdefault(
                    ch, h4.Clone("hEff_DS{}_ch{}".format(dsNum, ch)))
                threshDictSave[ch].Add(h4)

                if specIDXDict[bkgidx][ch].Integral() == 0:
                    print("Ch {} has 0 counts, not saving".format(ch))
                    continue

                specDict.setdefault(
                    ch, specIDXDict[bkgidx][ch].Clone('hBkg_Ch{}'.format(ch)))
                specDict[ch].Add(specIDXDict[bkgidx][ch])
                specIDXDict[bkgidx][ch].Write()

    EffTot = ROOT.TH1D("DS{}_EffTot_Divide".format(dsNum),
                       "DS{} {}".format(dsNum, dType), bins, lower, upper)
    EffTotSave = ROOT.TH1D("DS{}_{}_EffTot".format(dsNum, dType),
                           "DS{} {}".format(dsNum, dType), bins2, lower, upper)

    for ch in specDict:
        if ch not in threshDict:
            print("Ch {} not in threshDict... you should've fixed this!".
                  format(ch))
            continue
        TotalSpec.Add(specDict[ch])
        UncorrTotalSpec.Add(specDict[ch])
        EffTot.Add(threshDict[ch])
        EffTotSave.Add(threshDictSave[ch])

        # Save Channel specific
        h1 = specDict[ch].Clone('hDS{}_ChTot_Ch{}'.format(dsNum, ch))
        h1.SetTitle('hDS{}_ChTot_Ch{}'.format(dsNum, ch))
        threshDictSave[ch].Write()
        h1.Write()

    print('Channels in DS{} -- {}'.format(dsNum, dType), specDict.keys())

    # Save all histograms with fancy names and axes
    EffTotSave.GetYaxis().SetTitle('Exposure (kg-day)')
    EffTotSave.GetXaxis().SetTitle('Energy (keV)')
    EffTotSave.Write()
    UncorrTotalSpec.GetXaxis().SetTitle('Energy (keV)')
    UncorrTotalSpec.GetYaxis().SetTitle('Counts')
    UncorrTotalSpec.Write()
    TotalSpec.Divide(EffTot)
    TotalSpec.GetXaxis().SetTitle('Energy (keV)')
    TotalSpec.GetYaxis().SetTitle('Counts/(0.1 keV)/kg/day')
    TotalSpec.Write()
    outFile.Close()
예제 #16
0
def GetAnaylsisThreshold(dsNum=2, savePlot=False):
    """
        Returns a dictionary for analysis threshold, organized by: athreshDict[bkgidx][ch]
    """
    inDir = '/Users/brianzhu/macros/code/LAT/plots/spectra'
    f1 = ROOT.TFile("{}/AThresh_mHL1.root".format(inDir))

    if savePlot: fig, ax = plt.subplots(figsize=(10, 6))

    bkgDict, bkgFullDict, athreshDict = {}, {}, {}
    for key in f1.GetListOfKeys():
        histName = key.GetName()
        if 'DS{}'.format(dsNum) not in histName: continue
        name = ''.join(c for c in histName.split('_')[1] if c.isdigit())
        idx = ''.join(c for c in histName.split('_')[2] if c.isdigit())
        ch = int(name)
        bkgidx = int(idx)
        if 'BkgIdx' in histName and 'BkgFull' not in histName:
            for xbin in range(f1.Get(histName).GetNbinsX()):
                # If greater than 50 keV -- skip
                if f1.Get(histName).GetBinCenter(xbin) > 50.: continue
                bkgFullDict.setdefault(bkgidx, {}).setdefault(ch, []).append(
                    f1.Get(histName).GetBinContent(xbin))
                # Skip first 7 bins, -0.05 to 0.65 keV
                if f1.Get(histName).GetBinCenter(xbin) < 0.70: continue
                bkgDict.setdefault(bkgidx, {}).setdefault(ch, []).append(
                    f1.Get(histName).GetBinContent(xbin))

    for bkgidx in bkgDict:
        print("Accessing thresh_ds{}_bkgidx{}".format(dsNum, bkgidx))
        tD = ds.getDBRecord("thresh_ds{}_bkgidx{}".format(dsNum, bkgidx))
        print tD
        for ch in bkgDict[bkgidx]:
            if ch not in tD:
                print("Ch{} bkgidx{} not in thresholds database".format(
                    ch, bkgidx))
                continue
            x = np.array(bkgDict[bkgidx][ch])
            xFull = np.array(bkgFullDict[bkgidx][ch])
            noWall = True
            if np.cumsum(x[:50])[-1] >= 4: noWall = False
            # Method 1: Find first 0 walking up in energy from threshold
            # Works if no noise wall
            thresh1 = math.ceil(
                tD[ch][0] * 10) / 10  # Round up to the 1st decimal
            sigma = tD[ch][1]
            aThresh = 0.
            if noWall:
                thresh2 = 0.1 * np.where(
                    x == 0)[0][0] + 0.7  # Add 0.65 or 0.7 here?
                aThresh = max(thresh1 + 3 * sigma, thresh2)
                athreshDict.setdefault(bkgidx, {}).setdefault(ch, aThresh)
                # print "Ch%d, idx%d -- Thresh: %.1f -- AThresh: %.1f"%(ch, bkgidx, thresh1+3*sigma, thresh2)
                print(
                    "Ch{}, idx{} -- Thresh: {:.1f} -- AThresh: {:.1f}".format(
                        ch, bkgidx, thresh1 + 3 * sigma, thresh2))

                if savePlot:
                    ax.cla()
                    xbins = np.linspace(0, len(xFull) / 10., len(xFull))
                    ax.plot(
                        xbins[:100], xFull[:100],
                        ls="steps")  # vals, bins instead of x-value, y-value
                    ax.plot([aThresh, aThresh], [0, np.amax(x[:100])],
                            'k-',
                            color='r')  # Draw Line at analysis thresh
                    fig.savefig(
                        '/Users/brianzhu/macros/code/LAT/plots/AThresh/DS{}/NoWall_DS{}_ch{}_idx{}.png'
                        .format(dsNum, dsNum, ch, bkgidx))

            # Method 2: If there's a noise wall, find the maximum index and then start the walk from there
            elif not noWall:
                amax = np.argmax(x[:50])
                thresh3 = 0.1 * (np.where(x[amax:] == 0)[0][0] + amax) + 0.7
                aThresh = max(thresh1, thresh3)
                athreshDict.setdefault(bkgidx, {}).setdefault(ch, aThresh)
                print("Noise Wall Exists -- Ch{}, idx{} -- AThresh: {:.1f}".
                      format(ch, bkgidx, thresh3))

                if savePlot:
                    ax.cla()
                    xbins = np.linspace(0, len(xFull) / 10., len(xFull))
                    ax.plot(
                        xbins[:100], xFull[:100],
                        ls="steps")  # vals, bins instead of x-value, y-value
                    ax.plot([aThresh, aThresh], [0, np.amax(x[:100])],
                            'k-',
                            color='r')  # Draw Line at analysis thresh
                    fig.savefig(
                        '/Users/brianzhu/macros/code/LAT/plots/AThresh/DS{}/Wall_DS{}_ch{}_idx{}.png'
                        .format(dsNum, dsNum, ch, bkgidx))

    return athreshDict
예제 #17
0
파일: lat3.py 프로젝트: gothman5/LAT
def ApplyChannelCuts(dsNum, cutType, dType):
    """ ./lat3.py -cut [dsNum] [cutType]
    This runs over whole datasets.
    cutTypes:
        fs, rn, wf, fs+rn, fs+wf, rn+wf, fs+rn+wf

    dTypes:
        bkg, cal
    """
    # load the database
    calDB = db.TinyDB('../calDB.json')
    pars = db.Query()

    # setup a loop over modules and dataset ranges
    gROOT.ProcessLine("gErrorIgnoreLevel = 3001;")
    cInfo = ds.CalInfo()
    nMods = [1]
    if dsNum == 4: nMods = [2]
    if dsNum == 5: nMods = [1,2]
    # Dummy string for file writing -- adds nothing to the directories if background
    dString = ""
    if dType == "cal": dString = "cal"

    for modNum in nMods:
        # Changed so range of idx are set here to take advantage of module number
        nRanges = []
        if dType == "bkg":
            nRanges = [0, ds.dsMap[dsNum]]
            # if dsNum==5: nRanges[0] = 80 # exclude DS-5A
        elif dType == "cal":
            nRanges = [0, len(cInfo.master['ds%d_m%d'%(dsNum, modNum)])-1]
        else:
            print "cal or bkg not set, returning"
            return 0

        # Loop over bkgIdx, even though for calibration runs this will represent calIdx
        for bkgIdx in range(nRanges[0], nRanges[1]+1):
            # load the chains and find the right calIdx's.
            skimTree = TChain("skimTree")

            # build the file list
            fRegex = ""
            fList = []
            if dType == "bkg":
                fRegex = "/global/homes/w/wisecg/project/bg-lat/latSkimDS%d_%d_*.root" % (dsNum, bkgIdx)
                fList = glob.glob(fRegex)
                skimTree.Add(fRegex)
            elif dType == "cal":
                calList = cInfo.GetCalList("ds%d_m%d" % (dsNum, modNum), bkgIdx, runLimit=10)
                for i in calList:
                    fList += glob.glob("/global/homes/w/wisecg/project/cal-lat/latSkimDS%d_run%d_*.root"%(dsNum,i))
                    skimTree.Add("/global/homes/w/wisecg/project/cal-lat/latSkimDS%d_run%d_*.root" % (dsNum, i))
            file0 = fList[0]
            print "DS-%d subset %d, Mod-%d.  N_files: %d" % (dsNum, bkgIdx, modNum, len(fList))

            # Print some basic info about files
            f = TFile(file0)
            firstRun, lastRun, calIdxLo, calIdxHi = 0,0,0,0
            theCut = f.Get("theCut").GetTitle()
            if dType == "bkg":
                skimTree.GetEntry(0)
                firstRun = skimTree.run
                skimTree.GetEntry(skimTree.GetEntries()-1)
                lastRun = skimTree.run
                calIdxLo = cInfo.GetCalIdx("ds%d_m%d" % (dsNum, modNum), firstRun)
                calIdxHi = cInfo.GetCalIdx("ds%d_m%d" % (dsNum, modNum), lastRun)
            elif dType == "cal":
                # All the idx are the same for calibration!
                calIdxLo = calIdxHi = bkgIdx
                firstRun, lastRun = calList[0], calList[-1]

            print "    Entries %d  firstRun %d  lastRun %d  calIdxLo %d  calIdxHi %d" % (skimTree.GetEntries(),firstRun,lastRun,calIdxLo,calIdxHi)

            # build the channel list  (remove 692 and 1232 from DS5 for now.)
            chList = ds.GetGoodChanList(dsNum)
            if dsNum==5 and modNum==1:
                chList = [ch for ch in chList if ch < 1000 and ch!=692]
            if dsNum==5 and modNum==2:
                chList = [ch for ch in chList if ch > 1000 and ch!=1232]

            # -- create a dict of cuts for each channel, covering each calIdx. --
            cutDict = {}
            for calIdx in range(calIdxLo, calIdxHi+1):

                runCovMin = cInfo.master["ds%d_m%d" % (dsNum, modNum)][calIdx][1]
                runCovMax = cInfo.master["ds%d_m%d" % (dsNum, modNum)][calIdx][2]
                runCut = "run>=%d && run<=%d" % (runCovMin, runCovMax)

                fsD = ds.getDBRecord("fitSlo_ds%d_idx%d_m%d_Peak" % (dsNum, calIdx, modNum), False, calDB, pars)
                rnSD = ds.getDBRecord("riseNoise_ds%d_idx%d_m%d_SoftPlus" % (dsNum, calIdx, modNum), False, calDB, pars)
                rnCD = ds.getDBRecord("riseNoise_ds%d_idx%d_m%d_Continuum" % (dsNum, calIdx, modNum), False, calDB, pars)
                wfD = ds.getDBRecord("wfstd_ds%d_idx%d_mod%d" % (dsNum, calIdx, modNum), False, calDB, pars)

                for ch in chList:

                    fsCut, rnCut, wfCut, chanCut = None, None, None, None

                    # print ch,":",fsD[ch][2]

                    # fitSlo: check the 90% value is positive
                    if fsD[ch][2] > 0:
                        fsCut = "fitSlo<%.2f" % fsD[ch][2]

                    # riseNoise: check the softplus curvature is positive
                    if rnSD[ch][3] > 0:
                        rnCut = "riseNoise<(%.3f+%.5f*TMath::Log(1+TMath::Exp((trapENFCal-(%.3f))/%.3f)))" % (max(rnSD[ch][0],rnCD[ch][4]), rnSD[ch][1], rnSD[ch][2], rnSD[ch][3])

                    # wfStd: check if ralph says this is ok to use
                    if wfD!=0 and ch in wfD.keys() and wfD[ch][0]==u'y':
                        wfCut = "abs(wfstd - sqrt((%.4e + %.4e*trapENFCal + %.4e*trapENFCal**2 + %.2e*pow(trapENFCal,3) + %.2e*pow(trapENFCal,4))**2 + %.4f)) < (%.2f+%.3f*trapENFCal)" % (wfD[ch][3], wfD[ch][4], wfD[ch][5], wfD[ch][6], wfD[ch][7], wfD[ch][8], wfD[ch][9], wfD[ch][10])

                    # set the combination channel cut
                    if cutType == "fs" and fsCut!=None:
                        chanCut = "(%s && %s)" % (runCut, fsCut)

                    if cutType == "rn" and rnCut!=None:
                        chanCut = "(%s && %s)" % (runCut, rnCut)

                    if cutType == "wf" and wfCut!=None:
                        chanCut = "(%s && %s)" % (runCut, wfCut)

                    if cutType == "fs+rn" and fsCut!=None and rnCut!=None:
                        chanCut = "(%s && %s && %s)" % (runCut, fsCut, rnCut)

                    if cutType == "fs+wf" and fsCut!=None and wfCut!=None:
                        chanCut = "(%s && %s && %s)" % (runCut, fsCut, wfCut)

                    if cutType == "rn+wf" and rnCut!=None and wfCut!=None:
                        chanCut = "(%s && %s && %s)" % (runCut, rnCut, wfCut)

                    if cutType == "fs+rn+wf" and fsCut!=None and rnCut!=None and wfCut!=None:
                        chanCut = "(%s && %s && %s && %s)" % (runCut, fsCut, rnCut, wfCut)

                    # create dict entry for this channel or append to existing, taking care of parentheses and OR's.
                    if ch in cutDict.keys() and chanCut!=None:
                        cutDict[ch] += " || %s" % chanCut
                    elif ch not in cutDict.keys() and chanCut!=None:
                        cutDict[ch] = "(%s" % chanCut

            # close the parens for each channel entry
            for key in cutDict:
                cutDict[key] += ")"

            # -- finally, loop over each channel we have an entry for, get its cut, and create an output file. --
            for ch in cutDict:
                # TODO: threshold cut (or at least save the value for each bkgIdx)

                chanCut = theCut + "&& gain==0 && channel==%d" % ch

                if cutType == "fs":
                    outFile = "~/project/cuts/%sfs/%sfitSlo-DS%d-%d-ch%d.root" % (dString, dString, dsNum, bkgIdx, ch)
                    chanCut += "&& fitSlo>0 && %s" % cutDict[ch]

                if cutType == "rn":
                    outFile = "~/project/cuts/%srn/%sriseNoise-DS%d-%d-ch%d.root" % (dString, dString, dsNum, bkgIdx, ch)
                    chanCut += "&& %s" % cutDict[ch]

                if cutType == "wf":
                    outFile = "~/project/cuts/%swf/%swfstd-DS%d-%d-ch%d.root" % (dString, dString, dsNum, bkgIdx, ch)
                    chanCut += "&& %s" % cutDict[ch]

                if cutType == "fs+rn":
                    outFile = "~/project/cuts/%sfs_rn/%sfs_rn-DS%d-%d-ch%d.root" % (dString, dString, dsNum, bkgIdx, ch)
                    chanCut += "&& fitSlo>0 && %s" % cutDict[ch]

                if cutType == "fs+wf":
                    outFile = "~/project/cuts/%sfs_wf/%sfs_wf-DS%d-%d-ch%d.root" % (dString, dString, dsNum, bkgIdx, ch)
                    chanCut += "&& fitSlo>0 && %s" % cutDict[ch]

                if cutType == "rn+wf":
                    outFile = "~/project/cuts/%srn_wf/%srn_wf-DS%d-%d-ch%d.root" % (dString, dString, dsNum, bkgIdx, ch)
                    chanCut += "&& %s" % cutDict[ch]

                if cutType == "fs+rn+wf":
                    outFile = "~/project/cuts/%sfs_rn_wf/%sfs_rn_wf-DS%d-%d-ch%d.root" % (dString, dString, dsNum, bkgIdx, ch)
                    chanCut += "&& fitSlo>0 && %s" % cutDict[ch]

                print "    Writing to:",outFile
                print "    Cut used:",chanCut,"\n"
                outFile = TFile(outFile,"RECREATE")
                outTree = TTree()
                outTree = skimTree.CopyTree(chanCut)
                outTree.Write()
                cutUsed = TNamed("chanCut",chanCut)
                cutUsed.Write()
                print "Wrote",outTree.GetEntries(),"entries."
                outFile.Close()
예제 #18
0
파일: lat3.py 프로젝트: gothman5/LAT
def main(argv):

    print "========================================"
    print "LAT3 started:",time.strftime('%X %x %Z')
    startT = time.clock()

    cInfo = CalInfo()
    pathToInput, pathToOutput = ".", "."
    dsNum, subNumm, modNum, chNum = -1, -1, -1, -1
    skimTree = ROOT.TChain("skimTree")
    customPar = ""
    tuneNames, calList, parList, parNameList, chList = [], [], [], [], []
    fTune, fFor, fastMode, fDB, fCSV = False, False, False, False, False

    if len(argv) == 0:
        return
    for i, opt in enumerate(argv):

        # -- Cut tuning options --
        if opt == "-all":
            parList.append('bcMax'), parNameList.append('bcMax')
            parList.append('fitSlo'), parNameList.append('fitSlo')
            parList.append('riseNoise'), parNameList.append('riseNoise')
            print "Tuning all cuts"
        if opt == "-bcMax":
            parList.append('bcMax'), parNameList.append('bcMax')
            print "Tuning bcMax"
        if opt == "-noiseWeight":
            parList.append('(waveS4-waveS1)/bcMax/trapENFCal'), parNameList.append('noiseWeight')
            print "Tuning noiseWeight ((waveS4-waveS1)/bcMax/trapENFCal)"
        if opt == "-bcTime":
            parList.append('(bandTime-tOffset-1100)/(matchTime-tOffset)'), parNameList.append('bcTime')
            print "Tuning bcTime"
        if opt == "-tailSlope":
            parList.append('pol2'), parNameList.append('pol2')
            parList.append('pol3'), parNameList.append('pol3')
            print "Tuning tailSlope"
        if opt == "-fitSlo":
            parList.append('fitSlo'), parNameList.append('fitSlo')
            print "Tuning fitSlo"
        if opt == "-riseNoise":
            parList.append('riseNoise'), parNameList.append('riseNoise')
            print "Tuning riseNoise"
        if opt == "-Custom":
            customPar = str(argv[i+1])
            parList.append(customPar), parNameList.append('customPar')
            print "Tuning custom cut parameter: ", customPar

        # Tune specific range -- input as string with comma separation:
        if opt == "-Range":
            rangeName = str(argv[i+1])
            tuneNames = rangeName.split(',')
            print "Tuning ranges: ", tuneNames

        # -- Input/output options --
        if opt == "-s":
            dsNum, subNum, modNum = int(argv[i+1]), int(argv[i+2]), int(argv[i+3])
            print "Processing DS-%d subDS-%d Module-%d" % (dsNum, subNum, modNum)
        if opt == "-d":
            pathToInput, pathToOutput = argv[i+1], argv[i+2]
            print "Custom paths: Input %s" % pathToInput
        if opt == "-ch":
            chNum = int(argv[i+1])
            print "Tuning specific channel %d" % (chNum)
        if opt == "-fast":
            fastMode = True
            print "Tuning cuts with fastMode ON, diagnostic plots will not be generated!"
        if opt == "-pd":
            import pandas as pd
            dfList, fCSV = [], True
            print "Saving CSV file in ./output"
        if opt == "-db":
            fDB = True
            print "DB mode"

        # -- Database options --
        if opt == "-force":
            fFor = True
            print "Force DB update mode."
        if opt == "-tune":
            fTune = True
            pathToInput = argv[i+1]
            print ("Cut tune mode. Input path for cal files: ", pathToInput)

        # -- Apply channel cuts (exits after running this) --
        if opt == "-cut":
            dsNum = int(argv[i+1])
            cutType = argv[i+2]
            dType = argv[i+3]
            print "Applying %s cut to DS-%d (%s)..." % (cutType, dsNum, dType)
            ApplyChannelCuts(dsNum, cutType, dType)
            stopT = time.clock()
            print "Stopped:",time.strftime('%X %x %Z'),"\nProcess time (min):",(stopT - startT)/60
            return

    # -- Load calibration files --
    if dsNum == -1 or subNum == -1 or modNum == -1:
        print "DS, subDS, or module number not set properly, exiting"
        return
    elif fTune:
        # Limit to 10 calibration runs because that's all Clint processed!  What a jerk.
        calList = cInfo.GetCalList("ds%d_m%d" % (dsNum, modNum), subNum, runLimit=10)
        for i in calList: skimTree.Add("%s/latSkimDS%d_run%d_*" % (pathToInput, dsNum, i))
    else:
        print "Tune or Cut option not set"
        return

    # -- Load chains for this DS --
    inPath = pathToInput + "/latSkimDS%d*.root" % dsNum
    fileList = glob.glob(inPath)
    cutFile = TFile(fileList[0])
    theCut = cutFile.Get("theCut").GetTitle()

    # -- Load channel list --
    if chNum == -1:
        chList = ds.GetGoodChanList(dsNum)
        if dsNum==5 and modNum == 1: # remove 692 and 1232 (both beges, so who cares)
            chList = [584, 592, 598, 608, 610, 614, 624, 626, 628, 632, 640, 648, 658, 660, 662, 672, 678, 680, 688, 690, 694]
        if dsNum==5 and modNum == 2:
            chList = [1106, 1110, 1120, 1124, 1128, 1170, 1172, 1174, 1176, 1204, 1208, 1298, 1302, 1330, 1332]
    else:
        chList = [chNum]

    print "Processing channels: ", chList
    print "Processing runs: ", calList

    # -- Tune cuts --
    if fTune:
        tunedPars = {}
        # Default is peak only
        if not tuneNames: tuneNames.append("Peak")
        for par, parName in zip(parList, parNameList):
            for idx, tName in enumerate(tuneNames):
                tRange = []
                if tName == "Continuum": tRange = [5, 50]
                elif tName == "Peak": tRange = [236, 240]
                elif tName == "SoftPlus": tRange = [] # Maybe should be another boolean
                else: tRange = [int(tName.split("_")[0]), int(tName.split("_")[1])]

                key = "%s_ds%d_idx%d_m%d_%s"%(parName,dsNum,subNum,modNum,tName)
                if not tRange:
                    cutDict = TuneSoftPlus(dsNum, subNum, tName, skimTree, chList, par, parName, theCut, fastMode)
                else:
                    cutDict = TuneCut(dsNum, subNum, tRange[0], tRange[1], tName, skimTree, chList, par, parName, theCut, fastMode)

                if fDB: ds.setDBRecord({"key":key,"vals":cutDict}, forceUpdate=fFor)
                if fCSV:
                    dummyDict = {"DS":[dsNum]*5, "SubDS":[subNum]*5, "Module":[modNum]*5, "Cut":[parName]*5, "Range":[tName]*5, "Percentage":[1, 5, 90, 95, 99]}
                    dummyDict2 = dict(dummyDict.items() + cutDict.items())
                    dfList.append(pd.DataFrame(dummyDict2))
        if fCSV:
            dfTot = pd.concat(dfList)
            dfTot.to_csv("./output/Cuts_ds%d_idx%d_m%d.csv"%(dsNum,subNum,modNum))

    stopT = time.clock()
    print "Stopped:",time.strftime('%X %x %Z'),"\nProcess time (min):",(stopT - startT)/60
예제 #19
0
def main():
    gROOT.SetBatch(True)
    path = "condor/hadd_2016_MC_AllUpdates.04.09.2019/"
    histoName = "h_njets_dR_bjet1_bjet2_0l_HT500_ge2b_ge2t"  # 2D histo from analyzer / x axes is deltaR, y axes is njets
    rebinVal = 20  # 20

    bgData = {
        "TT":
        info.DataSetInfo(basedir=path,
                         fileName="2016_TT.root",
                         sys=0.2,
                         label="Background"),
        #"WJetsToLNu"      : info.DataSetInfo(basedir=path, fileName="2016_WJetsToLNu.root",      sys=0.2, label="Background"),
        #"DYJetsToLL_M-50" : info.DataSetInfo(basedir=path, fileName="2016_DYJetsToLL_M-50.root", sys=0.2, label="Background"),
        "QCD":
        info.DataSetInfo(basedir=path,
                         fileName="2016_QCD.root",
                         sys=0.5,
                         label="Background"),  # changed as 0.5 
        #"ST"              : info.DataSetInfo(basedir=path, fileName="2016_ST.root" ,             sys=0.2, label="Background"),
        #"Diboson_nonIncl" : info.DataSetInfo(basedir=path, fileName="2016_Diboson_nonIncl.root", sys=0.2, label="Background"),
        #"Diboson"         : info.DataSetInfo(basedir=path, fileName="2016_Diboson.root" ,        sys=0.2, label="Background"),
        #"TTX"             : info.DataSetInfo(basedir=path, fileName="2016_TTX.root" ,            sys=0.2, label="Background"),
        #"Triboson"        : info.DataSetInfo(basedir=path, fileName="2016_Triboson.root" ,       sys=0.2, label="Background"),
        "BG_Others":
        info.DataSetInfo(basedir=path,
                         fileName="2016_BG_OTHER.root",
                         sys=0.2,
                         label="Background"),
    }

    # add uncertainty also for signal for the fake signal that big peak
    sgData = {
        "2016_rpv_stop_300":
        info.DataSetInfo(basedir=path,
                         fileName="2016_RPV_2t6j_mStop-300.root",
                         sys=0.2,
                         label="Signal"),
        "2016_rpv_stop_350":
        info.DataSetInfo(basedir=path,
                         fileName="2016_RPV_2t6j_mStop-350.root",
                         sys=0.2,
                         label="Signal"),
        "2016_rpv_stop_400":
        info.DataSetInfo(basedir=path,
                         fileName="2016_RPV_2t6j_mStop-400.root",
                         sys=0.2,
                         label="Signal"),
        "2016_rpv_stop_450":
        info.DataSetInfo(basedir=path,
                         fileName="2016_RPV_2t6j_mStop-450.root",
                         sys=0.2,
                         label="Signal"),
        "2016_rpv_stop_500":
        info.DataSetInfo(basedir=path,
                         fileName="2016_RPV_2t6j_mStop-500.root",
                         sys=0.2,
                         label="Signal"),
        "2016_rpv_stop_550":
        info.DataSetInfo(basedir=path,
                         fileName="2016_RPV_2t6j_mStop-550.root",
                         sys=0.2,
                         label="Signal"),
        "2016_rpv_stop_600":
        info.DataSetInfo(basedir=path,
                         fileName="2016_RPV_2t6j_mStop-600.root",
                         sys=0.2,
                         label="Signal"),
        "2016_rpv_stop_650":
        info.DataSetInfo(basedir=path,
                         fileName="2016_RPV_2t6j_mStop-650.root",
                         sys=0.2,
                         label="Signal"),
        "2016_rpv_stop_700":
        info.DataSetInfo(basedir=path,
                         fileName="2016_RPV_2t6j_mStop-700.root",
                         sys=0.2,
                         label="Signal"),
        "2016_rpv_stop_750":
        info.DataSetInfo(basedir=path,
                         fileName="2016_RPV_2t6j_mStop-750.root",
                         sys=0.2,
                         label="Signal"),
        "2016_rpv_stop_800":
        info.DataSetInfo(basedir=path,
                         fileName="2016_RPV_2t6j_mStop-800.root",
                         sys=0.2,
                         label="Signal"),
        "2016_rpv_stop_850":
        info.DataSetInfo(basedir=path,
                         fileName="2016_RPV_2t6j_mStop-850.root",
                         sys=0.2,
                         label="Signal"),
        "2016_rpv_stop_900":
        info.DataSetInfo(basedir=path,
                         fileName="2016_RPV_2t6j_mStop-900.root",
                         sys=0.2,
                         label="Signal"),
        "2016_stealth_stop_300_SYY":
        info.DataSetInfo(basedir=path,
                         fileName="2016_StealthSYY_2t6j_mStop-300.root",
                         sys=0.2,
                         label="Signal"),
        "2016_stealth_stop_350_SYY":
        info.DataSetInfo(basedir=path,
                         fileName="2016_StealthSYY_2t6j_mStop-350.root",
                         sys=0.2,
                         label="Signal"),
        "2016_stealth_stop_400_SYY":
        info.DataSetInfo(basedir=path,
                         fileName="2016_StealthSYY_2t6j_mStop-400.root",
                         sys=0.2,
                         label="Signal"),
        "2016_stealth_stop_450_SYY":
        info.DataSetInfo(basedir=path,
                         fileName="2016_StealthSYY_2t6j_mStop-450.root",
                         sys=0.2,
                         label="Signal"),
        "2016_stealth_stop_500_SYY":
        info.DataSetInfo(basedir=path,
                         fileName="2016_StealthSYY_2t6j_mStop-500.root",
                         sys=0.2,
                         label="Signal"),
        "2016_stealth_stop_550_SYY":
        info.DataSetInfo(basedir=path,
                         fileName="2016_StealthSYY_2t6j_mStop-550.root",
                         sys=0.2,
                         label="Signal"),
        "2016_stealth_stop_600_SYY":
        info.DataSetInfo(basedir=path,
                         fileName="2016_StealthSYY_2t6j_mStop-600.root",
                         sys=0.2,
                         label="Signal"),
        "2016_stealth_stop_650_SYY":
        info.DataSetInfo(basedir=path,
                         fileName="2016_StealthSYY_2t6j_mStop-650.root",
                         sys=0.2,
                         label="Signal"),
        "2016_stealth_stop_700_SYY":
        info.DataSetInfo(basedir=path,
                         fileName="2016_StealthSYY_2t6j_mStop-700.root",
                         sys=0.2,
                         label="Signal"),
        "2016_stealth_stop_750_SYY":
        info.DataSetInfo(basedir=path,
                         fileName="2016_StealthSYY_2t6j_mStop-750.root",
                         sys=0.2,
                         label="Signal"),
        "2016_stealth_stop_800_SYY":
        info.DataSetInfo(basedir=path,
                         fileName="2016_StealthSYY_2t6j_mStop-800.root",
                         sys=0.2,
                         label="Signal"),
        "2016_stealth_stop_850_SYY":
        info.DataSetInfo(basedir=path,
                         fileName="2016_StealthSYY_2t6j_mStop-850.root",
                         sys=0.2,
                         label="Signal"),
        "2016_stealth_stop_900_SYY":
        info.DataSetInfo(basedir=path,
                         fileName="2016_StealthSYY_2t6j_mStop-900.root",
                         sys=0.2,
                         label="Signal"),
    }

    # Loop over all background and get their histograms
    bgHistos = {}
    for key in bgData:
        h = bgData[key].getHisto(histoName)
        h.RebinX(rebinVal)
        bgHistos[key] = {"hist": h}

    # Loop over all signal models and calculate sig. for each one
    for key in sgData:
        hSg = sgData[key].getHisto(histoName)
        hSg.RebinX(rebinVal)
        nBins = hSg.GetNbinsX()
        low = hSg.GetXaxis().GetXmin()
        high = hSg.GetXaxis().GetXmax()
        binToGeV = (high - low) / nBins
        sigDic = {}

        # Find all possible MLB cut values
        c1 = ROOT.TCanvas("c", "c", 0, 0, 800, 800)
        ROOT.gStyle.SetStatY(0.5)
        ROOT.gStyle.SetStatX(0.85)
        ROOT.gStyle.SetPalette(ROOT.kRainBow)
        ROOT.gPad.SetLeftMargin(0.12)
        ROOT.gPad.SetRightMargin(0.15)
        ROOT.gPad.SetTopMargin(0.08)
        ROOT.gPad.SetBottomMargin(0.12)
        ROOT.gPad.SetTicks(1, 1)
        sig1DHisto = ROOT.TH1D(key + "hist", key + "hist", nBins, low, high)

        for lowBin in range(1, nBins + 1):
            sigTot2 = 0.0
            for nJetBin in range(0, hSg.GetNbinsY() + 1):
                highBin = hSg.GetNbinsX()
                nSg = hSg.Integral(
                    lowBin, highBin, nJetBin,
                    nJetBin)  # x axes is deltaR, y axes is njets

                # Loop over all background histos and calculate sigma
                sigma2 = 0.0
                for k in bgHistos:
                    n = bgHistos[k]["hist"].Integral(lowBin, highBin, nJetBin,
                                                     nJetBin)
                    m = nSg
                    sys1 = ((bgData[k].sys) * n)**2
                    sys2 = ((sgData[key].sys) * m)**2
                    sigma2 += n + m + sys1 + sys2

                # Calculate and fill significance
                sig = 0.0
                if (sigma2 > 0.0):
                    sig = nSg / math.sqrt(sigma2)
                sigTot2 += sig**2

            sig1DHisto.SetBinContent(lowBin, math.sqrt(sigTot2))

        # Make the plot nice
        sig1DHisto.SetTitle(key + " Significance")
        sig1DHisto.GetXaxis().SetTitle("#DeltaR_{bj1-bj2}")
        sig1DHisto.GetYaxis().SetTitle("Significance")
        #sig1DHisto.SetAxisRange(0.0, 1.8) # cut off at 2
        sig1DHisto.GetBinContent(h.GetMaximumBin())
        sig1DHisto.Draw("")
        c1.SaveAs(key + "_2016_significance.pdf")
        del c1
예제 #20
0
def getData(path, scale=1.0, year="2018"):
    bgData = [
        info.DataSetInfo(basedir=path,
                         fileName=year + "_Triboson.root",
                         sys=0.3,
                         label="Triboson",
                         scale=scale),
        info.DataSetInfo(basedir=path,
                         fileName=year + "_Diboson.root",
                         sys=0.3,
                         label="Diboson",
                         scale=scale),
        info.DataSetInfo(basedir=path,
                         fileName=year + "_DYJetsToLL_M-50.root",
                         sys=0.3,
                         label="DYJetsToLL_M-50",
                         scale=scale),
        info.DataSetInfo(basedir=path,
                         fileName=year + "_TTX.root",
                         sys=0.3,
                         label="TTX",
                         scale=scale),
        info.DataSetInfo(basedir=path,
                         fileName=year + "_WJets.root",
                         sys=0.3,
                         label="WJets",
                         scale=scale),
        info.DataSetInfo(basedir=path,
                         fileName=year + "_ST.root",
                         sys=0.3,
                         label="ST",
                         scale=scale),
        info.DataSetInfo(basedir=path,
                         fileName=year + "_QCD.root",
                         sys=0.5,
                         label="QCD",
                         scale=scale),
        info.DataSetInfo(basedir=path,
                         fileName=year + "_TT.root",
                         sys=0.2,
                         label="T#bar{T}",
                         scale=scale),
    ]

    sgData = [
        info.DataSetInfo(basedir=path,
                         fileName=year + "_RPV_2t6j_mStop-300.root",
                         sys=-1.0,
                         label="RPV 300",
                         scale=scale),
        info.DataSetInfo(basedir=path,
                         fileName=year + "_RPV_2t6j_mStop-350.root",
                         sys=-1.0,
                         label="RPV 350",
                         scale=scale),
        info.DataSetInfo(basedir=path,
                         fileName=year + "_RPV_2t6j_mStop-400.root",
                         sys=-1.0,
                         label="RPV 400",
                         scale=scale),
        info.DataSetInfo(basedir=path,
                         fileName=year + "_RPV_2t6j_mStop-450.root",
                         sys=-1.0,
                         label="RPV 450",
                         scale=scale),
        info.DataSetInfo(basedir=path,
                         fileName=year + "_RPV_2t6j_mStop-500.root",
                         sys=-1.0,
                         label="RPV 500",
                         scale=scale),
        info.DataSetInfo(basedir=path,
                         fileName=year + "_RPV_2t6j_mStop-550.root",
                         sys=-1.0,
                         label="RPV 550",
                         scale=scale),
        info.DataSetInfo(basedir=path,
                         fileName=year + "_RPV_2t6j_mStop-600.root",
                         sys=-1.0,
                         label="RPV 600",
                         scale=scale),
        info.DataSetInfo(basedir=path,
                         fileName=year + "_RPV_2t6j_mStop-650.root",
                         sys=-1.0,
                         label="RPV 650",
                         scale=scale),
        info.DataSetInfo(basedir=path,
                         fileName=year + "_RPV_2t6j_mStop-700.root",
                         sys=-1.0,
                         label="RPV 700",
                         scale=scale),
        info.DataSetInfo(basedir=path,
                         fileName=year + "_RPV_2t6j_mStop-750.root",
                         sys=-1.0,
                         label="RPV 750",
                         scale=scale),
        info.DataSetInfo(basedir=path,
                         fileName=year + "_RPV_2t6j_mStop-800.root",
                         sys=-1.0,
                         label="RPV 800",
                         scale=scale),
        info.DataSetInfo(basedir=path,
                         fileName=year + "_RPV_2t6j_mStop-850.root",
                         sys=-1.0,
                         label="RPV 850",
                         scale=scale),
        info.DataSetInfo(basedir=path,
                         fileName=year + "_RPV_2t6j_mStop-900.root",
                         sys=-1.0,
                         label="RPV 900",
                         scale=scale),
    ]
    return sgData, bgData
예제 #21
0
def main():
    path = "condor/Analyze1Lep_Kerasv1.2.0_MCTrigger_bTag_leptonWeight_ht300/"
    #path = "condor/Analyze1Lep_Kerasv1.2.0_MCTrigger_bTag_leptonWeight/"
    #path = "condor/Analyze1Lep_Kerasv1.2.0_noMCTrigger_PU_bTag_leptonWeight/"
    #histoName = "h_mbl_1l_ge7j_ge1b_noMbl"
    histoName = "h_njets_mbl_1l_ge7j_ge1b_noMbl"
    rebinVal = 5

    bgData = {
        "TT"              : info.DataSetInfo(basedir=path, fileName="TT.root",              sys=0.1, label="Background"),
        #"TTJets"          : info.DataSetInfo(basedir=path, fileName="TTJets.root",          sys=0.3, label="Background"),
        "QCD"             : info.DataSetInfo(basedir=path, fileName="QCD.root",             sys=0.5, label="Background"),
        #"DYJetsToLL_M-50" : info.DataSetInfo(basedir=path, fileName="DYJetsToLL_M-50.root", sys=1.0, label="Background"),
        #"Rare"            : info.DataSetInfo(basedir=path, fileName="Rare.root",            sys=1.0, label="Background"),
        #"Diboson"         : info.DataSetInfo(basedir=path, fileName="Diboson.root",         sys=1.0, label="Background"),
        #"ST"              : info.DataSetInfo(basedir=path, fileName="ST.root",              sys=1.0, label="Background"),
        #"WJetsToLNu"      : info.DataSetInfo(basedir=path, fileName="WJetsToLNu.root",      sys=1.0, label="Background"),
    }

    sgData = {
        "rpv_stop_350" : info.DataSetInfo(basedir=path, fileName="rpv_stop_350.root", sys=-1.0, label="Signal"),
        "rpv_stop_450" : info.DataSetInfo(basedir=path, fileName="rpv_stop_450.root", sys=-1.0, label="Signal"),
        "rpv_stop_550" : info.DataSetInfo(basedir=path, fileName="rpv_stop_550.root", sys=-1.0, label="Signal"),
        "rpv_stop_650" : info.DataSetInfo(basedir=path, fileName="rpv_stop_650.root", sys=-1.0, label="Signal"),
        "rpv_stop_750" : info.DataSetInfo(basedir=path, fileName="rpv_stop_750.root", sys=-1.0, label="Signal"),
        "rpv_stop_850" : info.DataSetInfo(basedir=path, fileName="rpv_stop_850.root", sys=-1.0, label="Signal"),

        "stealth_stop_350_SYY" : info.DataSetInfo(basedir=path, fileName="stealth_stop_350_SYY.root", sys=-1.0, label="Signal"),
        "stealth_stop_450_SYY" : info.DataSetInfo(basedir=path, fileName="stealth_stop_450_SYY.root", sys=-1.0, label="Signal"),
        "stealth_stop_550_SYY" : info.DataSetInfo(basedir=path, fileName="stealth_stop_550_SYY.root", sys=-1.0, label="Signal"),
        "stealth_stop_650_SYY" : info.DataSetInfo(basedir=path, fileName="stealth_stop_650_SYY.root", sys=-1.0, label="Signal"),
        "stealth_stop_750_SYY" : info.DataSetInfo(basedir=path, fileName="stealth_stop_750_SYY.root", sys=-1.0, label="Signal"),
        "stealth_stop_850_SYY" : info.DataSetInfo(basedir=path, fileName="stealth_stop_850_SYY.root", sys=-1.0, label="Signal"),
    }

    # Loop over all background and get their histograms
    bgHistos = {}
    for key in bgData:
        h = bgData[key].getHisto(histoName)
        h.RebinY(rebinVal)
        bgHistos[key] = {"hist" : h}

    # Loop over all signal models and calculate sig. for each one 
    for key in sgData:
        hSg = sgData[key].getHisto(histoName)
        hSg.RebinY(rebinVal)
        nBins = hSg.GetNbinsY()
        low = hSg.GetYaxis().GetXmin()
        high = hSg.GetYaxis().GetXmax()
        binToGeV = (high - low)/nBins
        sigDic = {}
            
        # Find all possible MLB cut values
        c1 = ROOT.TCanvas( "c", "c", 0, 0, 800, 800)
        ROOT.gStyle.SetStatY(0.5)
        ROOT.gStyle.SetStatX(0.85)
        ROOT.gStyle.SetPalette(ROOT.kRainBow)
        ROOT.gPad.SetLeftMargin(0.12)
        ROOT.gPad.SetRightMargin(0.15)
        ROOT.gPad.SetTopMargin(0.08)
        ROOT.gPad.SetBottomMargin(0.12)
        ROOT.gPad.SetTicks(1,1)
        sig2DHisto = ROOT.TH2D(key+"hist",key+"hist", nBins, low, high, nBins, low, high)        
        for lowBin in range(1, nBins+1):       
            for highBin in range(nBins+1):
                if(lowBin < highBin):
                    sigTot2 = 0.0
                    for nJetBin in range(6, hSg.GetNbinsX()+1):            
                        nSg = hSg.Integral(nJetBin, nJetBin, lowBin, highBin)
                        
                        # Loop over all background histos and calculate sigma
                        sigma2 = 0.0
                        for k in bgHistos:
                            n = bgHistos[k]["hist"].Integral(nJetBin, nJetBin, lowBin, highBin)
                            sys = ((bgData[k].sys)*n)**2
                            sigma2 += n + sys
            
                        # Calculate and fill sig.
                        sig = 0.0
                        if(sigma2 != 0): sig = nSg/math.sqrt(sigma2)
                        sigTot2 += sig**2

                    sig2DHisto.SetBinContent(lowBin, highBin, math.sqrt(sigTot2))

        # Make the plot nice
        sig2DHisto.SetTitle(key+" Significance")
        sig2DHisto.GetXaxis().SetTitle("Lower Cut [GeV]")
        sig2DHisto.GetYaxis().SetTitle("Higher Cut [GeV]")
        sig2DHisto.Draw("colz")
        c1.SaveAs(key+"_sig.png")
        del c1