def fitBCMax(): tree = TChain("skimTree") tree.Add("~/project/cal-lat/latSkimDS3*") cutFile = TFile("~/project/lat/latSkimDS3_0_0.root") theCut = cutFile.Get("theCut").GetTitle() # scatter plot h0 = wl.H2D(tree,"h0",100,0,20,500,parLim[0],parLim[1],thisVar+":trapENFCal",theCut) # fitModel10 = "[0] + [1]*x + [2]*x**2 + [3] * exp(-1.0 * ((x-[4])**2 / (2 * [5]**2)) )" h1 = wl.H1D(tree,"h1",20,9.5,11.5,"trapENFCal",theCut) h2 = wl.H1D(tree,"h2",20,9.5,11.5,"trapENFCal",theCut+checkCut) f0 = TF1("f0","gaus",10,11) h1.Fit("f0","Lq") [norm,mu,sig] = wl.GetPars(f0) f1 = TF1("f1", fitModel10, 9.5,11) wl.SetPars(f1,[-209, 41, -2, norm, mu, sig]) f1.SetLineColor(4) h1.Fit("f1","Lq") [p0,p1,p2,norm1,mu,sig] = wl.GetPars(f1) f2 = TF1("f2", fitModel10, 9.5,11) wl.SetPars(f2,[-209, 41, -2, norm1, mu, sig]) f2.SetLineColor(2) h2.Fit("f2","Lq") [p0,p1,p2,norm2,mu,sig] = wl.GetPars(f2) retention = 100*norm2/norm1 print "Retention:",retention," Cut used:",checkCut
def MakeCutPlot(c,cal,var,eb,elo,ehi,vb,vlo,vhi,d2Cut,d1Cut,outPlot,fastMode): """ Creates a channel-specific energy calibration plot. """ # Calculate cut vals (assumes plot range is correct) h1 = wl.H1D(cal,vb,vlo,vhi,var,d1Cut) h1Sum = h1.Integral() if h1Sum == 0: print "Error: Failed %s, histogram sum is 0 so cannot normalize, setting to [0,0,0,0,0]"%(var) return 0,0,0,0,0 h1.Scale(1/h1Sum) try: cut99,cut95,cut01,cut05,cut90 = wl.GetIntegralPoints(h1) except: print "Error: Failed %s using cut %s, setting to [0,0,0,0,0]"%(var,d1Cut) return 0,0,0,0,0 if fastMode: print "Returning fastMode output: ", cut99,cut95,cut01,cut05,cut90 return cut99,cut95,cut01,cut05,cut90 # Generate the plot for inspection. c.cd(2) gPad.SetLogy(0) h1.GetXaxis().SetRangeUser(cut01-abs(0.25*cut01), cut99 + abs(0.25*cut99) ) h1.SetTitle("") h1.GetXaxis().SetTitle(var) h1.Draw("hist") c.cd(1) gPad.SetLogy(0) cal.Draw("%s:trapENFCal>>b(%d,%d,%d,%d,%.3E,%.3E)"%(var,eb+10,elo-5,ehi+5,vb,cut01-abs(0.25*cut01),cut99+abs(0.25*cut99)) ,d2Cut) l1, l2, l3 = TLine(), TLine(), TLine() l1.SetLineColor(ROOT.kGreen) l2.SetLineColor(ROOT.kRed) l3.SetLineColor(ROOT.kMagenta) l1.DrawLine(elo-5, cut99, ehi+5, cut99) l2.DrawLine(elo-5, cut95, ehi+5, cut95) l2.DrawLine(elo-5, cut05, ehi+5, cut05) l1.DrawLine(elo-5, cut01, ehi+5, cut01) c.cd(3) x_h1, y_h1 = wl.npTH1D(h1) int_h1 = wl.integFunc(y_h1) g2 = TGraph(len(x_h1), x_h1, int_h1) g2.GetXaxis().SetRangeUser(cut01-abs(0.3*cut01), cut99 + abs(0.3*cut99) ) g2.SetTitle("") g2.GetXaxis().SetTitle(var) g2.GetYaxis().SetTitle("Percentile") g2.Draw("ACP") l1.DrawLine(cut99, 0, cut99, 1) l2.DrawLine(cut95, 0, cut95, 1) l1.DrawLine(cut01, 0, cut01, 1) l2.DrawLine(cut05, 0, cut05, 1) c.Print(outPlot) return cut99,cut95,cut01,cut05,cut90
def MakeCutPlot(c, cal, var, eb, elo, ehi, vb, vlo, vhi, d2Draw, d2Cut, d1Cut, outPlot, fastMode): """ Repeated code is the DEVIL. Even if you have to pass in 1,000,000 arguments. """ # Calculate cut vals (assumes plot range is correct) h1 = wl.H1D(cal, vb, vlo, vhi, var, d1Cut) h1Sum = h1.Integral() if h1Sum == 0: return 0, 0, 0, 0, 0 h1.Scale(1 / h1Sum) cut99, cut95, cut01, cut05, cut90 = wl.GetIntegralPoints(h1) if fastMode: return cut99, cut95, cut01, cut05, cut90 # Generate the plot for inspection. c.cd(2) gPad.SetLogy(0) h1.Draw("hist") c.cd(1) gPad.SetLogy(0) cal.Draw(d2Draw, d2Cut) l1, l2 = TLine(), TLine() l1.SetLineColor(ROOT.kGreen) l2.SetLineColor(ROOT.kRed) l1.DrawLine(elo, cut99, ehi, cut99) l2.DrawLine(elo, cut95, ehi, cut95) l2.DrawLine(elo, cut05, ehi, cut05) l1.DrawLine(elo, cut01, ehi, cut01) c.cd(3) x_h1, y_h1 = wl.npTH1D(h1) int_h1 = wl.integFunc(y_h1) g2 = TGraph(len(x_h1), x_h1, int_h1) g2.Draw("ACP") l1.DrawLine(cut99, 0, cut99, 1) l2.DrawLine(cut95, 0, cut95, 1) l1.DrawLine(cut01, 0, cut01, 1) l2.DrawLine(cut05, 0, cut05, 1) c.Print(outPlot) return cut99, cut95, cut01, cut05, cut90
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()
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)
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
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()
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
def getEff(): """ ./job-panda.py -getEff METHOD: open up the latskim file for each channel. loop over the good run ranges. for each good range, make an energy histogram. then calculate the efficiency curve based on the sigma value and convolve it with the histogram points. """ import numpy as np import waveLibs as wl import scipy.special as spec import matplotlib.pyplot as plt from ROOT import TFile, TTree, TH1D, TF1, TCanvas, gROOT import ROOT, random gROOT.ProcessLine(".x ~/env/MJDClintPlotStyle.C") # gROOT.ProcessLine("gErrorIgnoreLevel = 3001;") # suppress ROOT messages bins, xlo, xhi = 50, 0, 15 # set it just high enough that the first bin center isn't negative hSumCorr = TH1D("hSumCorr", "hSumCorr", bins, xlo, xhi) hSumUncr = TH1D("hSumUncr", "hSumUncr", bins, xlo, xhi) dsNum = 1 # ch = 578 for ch in ds.GetGoodChanList(dsNum): inFile = TFile(homePath + "/project/latskim/latSkimDS%d_ch%d.root" % (dsNum, ch)) tree = inFile.Get("skimTree") fileCut = inFile.Get("theCut").GetTitle() _, _, goodRunErfs = ds.GetThreshDicts(dsNum) hUnc = wl.H1D(tree, bins, xlo, xhi, "trapENFCal", fileCut) hSumUncr.Add(hUnc) for erfs in goodRunErfs[ch]: runCut = " && run >= %d && run <= %d" % (erfs[0], erfs[1]) theCut = fileCut + runCut h1 = wl.H1D(tree, bins, xlo, xhi, "trapENFCal", theCut) h1x, h1y = wl.npTH1D(h1) # calculate efficiency curve thisErf = TF1("thisErf", "0.5*(1+TMath::Erf((x-[0])/(TMath::Sqrt(2)*[1]) ))") thisErf.SetParameter(0, erfs[2]) # mu thisErf.SetParameter(1, erfs[3]) # sigma h1.Divide(thisErf) # thisErf = 0.5 * (1 + spec.erf( (h1x - mu) / (np.sqrt(2) * sig) )) # h1yScaled = h1y / thisErf # nameStr = str(random.uniform(1.,2.)) # h2 = TH1D(nameStr,nameStr,bins,xlo,xhi) # for i in range(bins): # h2.SetBinContent(i,h1yScaled[i]) hSumCorr.Add(h1) # eff-corrected spectrum. c = TCanvas("c", "c", 800, 600) c.SetLogy(1) hSumCorr.SetLineColor(ROOT.kBlue) hSumCorr.Draw("hist") hSumUncr.SetLineColor(ROOT.kRed) hSumUncr.Draw("hist same") # l1 = TLegend c.Print("./plots/effWeight/eff_DS%d.pdf" % dsNum)
def fitSlo238(tree,theCut): # for calibration data, keep 99% of the fast events in the 238 peak. # this is practice for doing this with the 10 kev peak in natural bg data. # scatter plot h1 = wl.H2D(tree,"h1",500,0,250,500,0,200,"fitSlo:trapENFCal",theCut) cts1 = h1.Integral( *wl.Get2DBins(h1,0,250,0,200) ) # -- 238 peak -- h2 = wl.H1D(tree,"h2",50,237,240,"trapENFCal",theCut) cts2 = h2.Integral( *wl.Get1DBins(h2,237,240) ) h2.SetLineColor(4) f0 = TF1("f0","gaus",237,240) h2.Fit("f0","Lq") # use to set initial guesses f1 = TF1("f1", "[0] * x + [1] * exp(-1.0 * (TMath::Power((x-[2]),2) / (2 * TMath::Power([3],2)) ))", 237, 240) wl.SetPars(f1,[1,418,238,0.381]) f1.SetLineColor(4) h2.Fit("f1","q") [flat, norm1, mu, sig] = wl.GetPars(f1) h3 = wl.H1D(tree,"h3",50,237,240,"trapENFCal",theCut+" && fitSlo < 20") cts3 = h3.Integral( *wl.Get1DBins(h3,237,240) ) f2 = TF1("f2", "[0] * x + [1] * exp(-1.0 * (TMath::Power((x-[2]),2) / (2 * TMath::Power([3],2)) ))", 237, 240) wl.SetPars(f2,[1,418,238,0.381]) f2.SetLineColor(2) h3.Fit("f2","q") [flat, norm2, mu, sig] = wl.GetPars(f2) retention = 100*norm2/norm1 print "norm1 %.2f norm2 %.2f retention: %.2f" % (norm1, norm2, retention) # -- Plots -- c1 = TCanvas("c1","Bob Ross's Canvas",1200,600) c1.Divide(2,1) # x, y c1.cd(1) gPad.SetLogz() h1.Draw("COLZ") c1.cd(2) h2.Draw() h3.SetLineColor(2) # red h3.Draw("same") l1 = TLegend(0.6,0.7,0.87,0.92) l1.AddEntry(h2,"basic","l") l1.AddEntry(f1,"basic fit:","l") # l1.AddEntry(f1,"flat %.2f norm %.2f" % (flat,norm1) ,"") # l1.AddEntry(f1,"mu %.2f sig %.2f" % (mu,sig) ,"") l1.AddEntry(h3,"+fitSlo < 20","l") l1.AddEntry(h3,"Retention: %.3f" % retention,"") l1.Draw("same") c1.Update() c1.Print("./plots/cal-fitSlo.pdf")
def fitArb10(tree,theCut): # fit an arbitrary low-energy cut print theCut theCut += " && !isEnr" checkCut = "" thisVar = "bandTime" fileName = "./plots/lat-max.pdf" parLim = [0,20000] # scatter plot h0 = wl.H2D(tree,"h0",100,0,20,500,parLim[0],parLim[1],thisVar+":trapENFCal",theCut) # -- 10 peak -- fitModel10 = "[0] + [1]*x + [2]*x**2 + [3] * exp(-1.0 * ((x-[4])**2 / (2 * [5]**2)) )" h1 = wl.H1D(tree,"h1",20,9.5,11.5,"trapENFCal",theCut) h2 = wl.H1D(tree,"h2",20,9.5,11.5,"trapENFCal",theCut+checkCut) f0 = TF1("f0","gaus",10,11) h1.Fit("f0","Lq") [norm,mu,sig] = wl.GetPars(f0) f1 = TF1("f1", fitModel10, 9.5,11) wl.SetPars(f1,[-209, 41, -2, norm, mu, sig]) f1.SetLineColor(4) h1.Fit("f1","Lq") [p0,p1,p2,norm1,mu,sig] = wl.GetPars(f1) f2 = TF1("f2", fitModel10, 9.5,11) wl.SetPars(f2,[-209, 41, -2, norm1, mu, sig]) f2.SetLineColor(2) h2.Fit("f2","Lq") [p0,p1,p2,norm2,mu,sig] = wl.GetPars(f2) retention = 100*norm2/norm1 print "Retention:",retention," Cut used:",checkCut # -- Energy Spectrum -- h3 = wl.H1D(tree,"h3",250,0,50,"trapENFCal",theCut) h4 = wl.H1D(tree,"h4",250,0,50,"trapENFCal",theCut+checkCut) cts3 = h3.Integral( *wl.Get1DBins(h3,0,20)) cts4 = h4.Integral( *wl.Get1DBins(h4,0,20)) cts5 = h3.Integral( *wl.Get1DBins(h3,20,40)) cts6 = h4.Integral( *wl.Get1DBins(h4,20,40)) print "Cts. 0-20 %d before %d after. 20-40 %d before %d after" % (cts3,cts4,cts5,cts6) h3.GetXaxis().SetRangeUser(0,20) h4.GetXaxis().SetRangeUser(0,20) # -- Plots -- c1 = TCanvas("c1","Bob Ross's Canvas",1800,600) c1.Divide(3,1,0.00001) # TPad::Divide c1.cd(1) gPad.SetLogz() h0.SetMinimum(1) h0.Draw("COLZ") c1.cd(2) gPad.SetLogy() h3.SetLineColor(4) h3.Draw() h4.SetLineColor(2) h4.Draw("same") l1 = TLegend(0.4,0.7,0.87,0.92) l1.AddEntry(h1,"basic","l") l1.AddEntry(h2,thisVar,"l") l1.Draw("same") c1.cd(3) h1.SetLineColor(4) # blue h1.Draw() h2.SetLineColor(2) # red h2.Draw("same") l2 = TLegend(0.6,0.6,0.87,0.92) l2.AddEntry(h1,"basic","l") l2.AddEntry(h2,thisVar,"l") l2.AddEntry(h2,"Retention: %.3f" % retention,"") l2.Draw("same") c1.Print(fileName)
def fitSlo10(tree,theCut): theCut += " && !isEnr" # scatter plot h0 = wl.H2D(tree,"h0",100,0,20,500,0,200,"fitSlo:trapENFCal",theCut) # -- 10 peak -- fitModel10 = "[0] + [1]*x + [2]*x**2 + [3] * exp(-1.0 * ((x-[4])**2 / (2 * [5]**2)) )" h1 = wl.H1D(tree,"h1",20,9.5,11.5,"trapENFCal",theCut) h1.SetLineColor(4) # f1 = TF1("f1","gaus",10,11) # h1.Fit("f1","L") # use to set initial guesses f1 = TF1("f1", fitModel10, 9.5,11) wl.SetPars(f1,[-209, 41, -2, 28, 10.3, 0.135]) f1.SetLineColor(4) h1.Fit("f1","Lq") [p0,p1,p2,norm1,mu,sig] = wl.GetPars(f1) # print wl.GetPars(f1) h2 = wl.H1D(tree,"h2",20,9.5,11.5,"trapENFCal",theCut+" && fitSlo < 18") f2 = TF1("f2", fitModel10, 9.5,11) wl.SetPars(f2,[-209, 41, -2, 28, 10.3, 0.135]) f2.SetLineColor(2) h2.Fit("f2","Lq") [p0,p1,p2,norm2,mu,sig] = wl.GetPars(f2) retention1 = 100*norm2/norm1 # -- 6.54 (Fe55) peak -- # TODO: This doesn't work well. wait till you're using RooFit. fitModel6 = "[0] * x + [1] * exp( -1.0 * ((x - [2])**2 / (2 * [3]**2)) )" # fitModel6 = "[0] * exp( -1.0 * ((x - [1])**2 / (2 * [2]**2)) )" h3 = wl.H1D(tree,"h3",20,5.5,7.5,"trapENFCal",theCut) # f3 = TF1("f3","gaus",6,7) # h3.Fit("f3","L") # use to set initial guesses # f3 = TF1("f3", fitModel6, 6,7) # wl.SetPars(f3,[-1, 19.4, 6.47, 0.631]) # f3.SetLineColor(4) # h3.Fit("f3","Lq") # [lin,norm3,mu,sig] = wl.GetPars(f3) # # h4 = wl.H1D(tree,"h4",20,5.5,7.5,"trapENFCal",theCut+" && fitSlo < 18") # f4 = TF1("f4", fitModel6, 6,7) # wl.SetPars(f4,[-1, 19.4, 6.47, 0.631]) # f4.SetLineColor(2) # h4.Fit("f4","Lq") # [lin,norm4,mu,sig] = wl.GetPars(f4) # retention2 = 100*norm4/norm3 # print "norm3 %.2f norm4 %.2f retention2: %.2f" % (norm3, norm4, retention2) # -- Plots -- c1 = TCanvas("c1","Bob Ross's Canvas",1200,600) # c1.Divide(3,1,0.00001) # TPad::Divide c1.Divide(2,1,0.00001) c1.cd(1) gPad.SetLogz() h0.SetMinimum(1) h0.Draw("COLZ") c1.cd(2) h1.SetLineColor(4) # blue h1.Draw() h2.SetLineColor(2) # red h2.Draw("same") l1 = TLegend(0.6,0.6,0.87,0.92) l1.AddEntry(h1,"basic","l") l1.AddEntry(h2,"+fitSlo < 20","l") l1.AddEntry(h2,"Retention: %.3f" % retention1,"") l1.Draw("same") # c1.cd(3) # h3.Draw() # h4.SetLineColor(2) # red # h4.Draw("same") # # l2 = TLegend(0.6,0.6,0.87,0.92) # l2.AddEntry(h3,"basic","l") # l2.AddEntry(h4,"+fitSlo < 20","l") # l2.AddEntry(h4,"Retention: %.3f" % retention2,"") # l2.Draw("same") c1.Print("./plots/ds1-fitSlo.pdf")