def writeOutput(self): rootFile = radmonLumiFilePattern.replace('__XXX__', str(self.fill)) try: fout = TFile(rootFile,'RECREATE') except IOError: print "Cannot open file for output", rootFile exit t = TTree('t', 'Combined Lumi and Radmon data') fillData = FillData() lumiData = LumiData() radmonData = RadmonData() t.Branch('fillBranch', fillData, 'fill/I:fillStart/I:fillStable/I:fillEnd/I:durationStable/I:run/I:lsNo/I:tstamp/I:msecs/I:bunchSpacing/I') t.Branch('fillBranchD', AddressOf(fillData, 'bField'), 'bField/D:beamEnergy/D') t.Branch('lumiBranch', lumiData, 'bestLumi/D:bestLumiErr/D:hfLumi/D:hfLumiErr/D:pltLumi/D:pltLumiErr/D:pltZeroLumi/D:pltZeroLumiErr/D:bcmfLumi/D:bcmfLumiErr/D') t.Branch('radmonBranch', radmonData, 'rates[16]/D:ratesErr[16]/D') href = self.pbest # Reference histogram #self.pbcmf.Draw() #raw_input("Press enter to continue") print "Starting filling tree, file ", radmonLumiFilePattern.replace('__XXX__', str(self.fill)) for i in range(len(self.xx) - 1): fillData.fill = self.fillDataList[i].fill fillData.fillStart = self.fillDataList[i].fillStart fillData.fillStable = self.fillDataList[i].fillStable fillData.fillEnd = self.fillDataList[i].fillEnd fillData.durationStable = self.fillDataList[i].durationStable fillData.run = self.fillDataList[i].run fillData.lsNo = self.fillDataList[i].lsNo fillData.bunchSpacing = self.fillDataList[i].bunchSpacing fillData.bField = self.fillDataList[i].bField fillData.beamEnergy = self.fillDataList[i].beamEnergy fillData.tstamp = int(href.GetXaxis().GetBinCenter(i)) fillData.msecs = int(href.GetXaxis().GetBinCenter(i) - fillData.tstamp) lumiData.bestLumi = self.pbest.GetBinContent(i) lumiData.bestLumiErr = self.pbest.GetBinError(i) lumiData.hfLumi = self.phf.GetBinContent(i) lumiData.hfLumiErr = self.phf.GetBinError(i) lumiData.pltLumi = self.pplt.GetBinContent(i) lumiData.pltLumiErr = self.pplt.GetBinError(i) lumiData.pltZeroLumi = self.ppltZero.GetBinContent(i) lumiData.pltZeroLumiErr = self.ppltZero.GetBinError(i) lumiData.bcmfLumi = self.pbcmf.GetBinContent(i) lumiData.bcmfLumiErr = self.pbcmf.GetBinError(i) for j in range(len(self.pradmon)): radmonData.rates[j] = self.pradmon[j].GetBinContent(i) radmonData.ratesErr[j] = self.pradmon[j].GetBinError(i) t.Fill() print "Writing", rootFile fout.Write() fout.Close() del fout
def radmon2fill(): #Get fills already processed filesDone = glob.glob(radmonFillPattern) (toReplace, dummy) = radmonFillPattern.split('.') print filesDone fillsDone = [] for file in filesDone: name, ext = file.split('.') fillsDone.append(int(name.replace(toReplace[0:-1], ""))) if len(fillsDone) > 0: print "Fills already done", fillsDone fillReport = FillReport(fillReportName) #Start/End of each fill in fillReport fillStarted = fillReport.getFillCreationTime() fillDumped = fillReport.getFillEndTime() for fill in sorted(fillStarted.keys()): if int(fill) in fillsDone: print "Fill", fill, "is already processed. Skipping..." continue print "Processing fill", fill, "........." fillData = FillData() radmonData = RadmonData() #runningSums = RunningSums() #rsumsList = [] for i in range(0, 16): #rsumsList.insert(i, [0.]*1800) radmonData.rates[i] = 0. radmonData.status[i] = 0. #Output rootFile = radmonFillPattern.replace("*", str(fill)) try: fout = TFile(rootFile,'RECREATE') except IOError: print "Cannot open file for output", rootFile exit t = TTree('t', 'Radmon rates') t.Branch('fillBranchI', fillData, 'fill/I:fillStart/I:fillStable/I:fillEnd/I:durationStable/I') t.Branch('fillBranchD', AddressOf(fillData, 'bField'), 'bField/D:beamEnergy/D') t.Branch('radmonBranchI', radmonData, 'tstamp/I:status[16]/I') t.Branch('radmonBranchD', AddressOf(radmonData, 'rates'), 'rates[16]/D') #t.Branch('radmonSums', runningSums, 'rsums[28800]/D') tsStart = fillStarted[fill] tsEnd = fillDumped[fill] tsStable = fillReport.getFillStableTime(int(fill)) duration = fillReport.getFillDuration(int(fill)) bField = fillReport.getFillField(int(fill)) beamEnergy = fillReport.getFillBeamEnergy(int(fill)) fromto = [tsStart-hrs*3600, tsEnd+hrs*3600] filelist = get_filelist(RadmonDataDir, radmonFilePattern, fromto) print "Files used:" chain = TChain("Rate") for file in filelist: print file chain.Add(file) fillData.fillStart = tsStart fillData.fillStable = tsStable fillData.fillEnd = tsEnd fillData.durationStable = duration fillData.bField = bField fillData.beamEnergy = beamEnergy for i in range(chain.GetEntries()): chain.GetEntry(i) #if tsStart - chain.tstamp < 1800: if chain.tstamp > tsStart and chain.tstamp < tsEnd: radmonData.tstamp = chain.tstamp for j in range(0, 16): #rsumsList[j].append(chain.rates[j]) #rsumsList[j].pop(0) radmonData.rates[j] = chain.rates[j] radmonData.status[j] = chain.status[j] #runningSums.rsums = np.asarray(rsumsList).ravel() #nparray = np.asarray(rsumsList).ravel() #for j in range(0, 16*1800): #runningSums.rsums[j] = nparray[j] #nump2d = np.reshape(nump, (-1,1800)) (To get back to 2D) #if chain.tstamp > tsStable - deltaMinutes*60 and chain.tstamp < tsEnd + deltaMinutes*60: t.Fill() fout.Write() fout.Close() del chain del fillData del radmonData #del runningSums del t del fout
def radmon2fill(): #Get fills already processed filesDone = glob.glob(radmonFillPattern) (toReplace, dummy) = radmonFillPattern.split('.') print filesDone fillsDone = [] for file in filesDone: name, ext = file.split('.') fillsDone.append(int(name.replace(toReplace[0:-1], ""))) if len(fillsDone) > 0: print "Fills already done", fillsDone fillReport = FillReport(fillReportName) #Start/End of each fill in fillReport fillStarted = fillReport.getFillCreationTime() fillDumped = fillReport.getFillEndTime() for fill in sorted(fillStarted.keys()): if int(fill) in fillsDone: print "Fill", fill, "is already processed. Skipping..." continue print "Processing fill", fill, "........." fillData = FillData() radmonData = RadmonData() #runningSums = RunningSums() #rsumsList = [] for i in range(0, 16): #rsumsList.insert(i, [0.]*1800) radmonData.rates[i] = 0. radmonData.status[i] = 0. #Output rootFile = radmonFillPattern.replace("*", str(fill)) try: fout = TFile(rootFile, 'RECREATE') except IOError: print "Cannot open file for output", rootFile exit t = TTree('t', 'Radmon rates') t.Branch('fillBranchI', fillData, 'fill/I:fillStart/I:fillStable/I:fillEnd/I:durationStable/I') t.Branch('fillBranchD', AddressOf(fillData, 'bField'), 'bField/D:beamEnergy/D') t.Branch('radmonBranchI', radmonData, 'tstamp/I:status[16]/I') t.Branch('radmonBranchD', AddressOf(radmonData, 'rates'), 'rates[16]/D') #t.Branch('radmonSums', runningSums, 'rsums[28800]/D') tsStart = fillStarted[fill] tsEnd = fillDumped[fill] tsStable = fillReport.getFillStableTime(int(fill)) duration = fillReport.getFillDuration(int(fill)) bField = fillReport.getFillField(int(fill)) beamEnergy = fillReport.getFillBeamEnergy(int(fill)) fromto = [tsStart - hrs * 3600, tsEnd + hrs * 3600] filelist = get_filelist(RadmonDataDir, radmonFilePattern, fromto) print "Files used:" chain = TChain("Rate") for file in filelist: print file chain.Add(file) fillData.fillStart = tsStart fillData.fillStable = tsStable fillData.fillEnd = tsEnd fillData.durationStable = duration fillData.bField = bField fillData.beamEnergy = beamEnergy for i in range(chain.GetEntries()): chain.GetEntry(i) #if tsStart - chain.tstamp < 1800: if chain.tstamp > tsStart and chain.tstamp < tsEnd: radmonData.tstamp = chain.tstamp for j in range(0, 16): #rsumsList[j].append(chain.rates[j]) #rsumsList[j].pop(0) radmonData.rates[j] = chain.rates[j] radmonData.status[j] = chain.status[j] #runningSums.rsums = np.asarray(rsumsList).ravel() #nparray = np.asarray(rsumsList).ravel() #for j in range(0, 16*1800): #runningSums.rsums[j] = nparray[j] #nump2d = np.reshape(nump, (-1,1800)) (To get back to 2D) #if chain.tstamp > tsStable - deltaMinutes*60 and chain.tstamp < tsEnd + deltaMinutes*60: t.Fill() fout.Write() fout.Close() del chain del fillData del radmonData #del runningSums del t del fout
def addRunningSum(): for fill in fills: inputFile = inputPattern.replace('__XXX__', str(fill)) try: fin = ROOT.TFile(inputFile,'READ') print "Processing fill", fill except IOError: print "Cannot open file", inputFile continue inputTree = fin.Get('t') outfile = outputPattern.replace('__XXX__', str(fill)) try: fout = ROOT.TFile(outfile,'RECREATE') except IOError: print "Cannot open file for output", outfile continue #Output tree fillData = FillData() radmonData = RadmonData() t = ROOT.TTree('t', "Radmon rates + runnning sums ") t.Branch('fillBranchI', fillData, 'fill/I:fillStart/I:fillColl/I:fillStable/I:fillEnd/I:durationStable/I') t.Branch('fillBranchD', ROOT.AddressOf(fillData, 'bField'), 'bField/D:beamEnergy/D') t.Branch('radmonBranchI', radmonData, 'tstamp/I:status[16]/I') t.Branch('radmonRates', ROOT.AddressOf(radmonData, 'rates'), 'rates[16]/D:rsums15[16]/D:rsums30[16]/D:rsums60[16]/D') rsums15 = [0.]*16 rsumsPrev15 = [0.]*16 rsums30 = [0.]*16 rsumsPrev30 = [0.]*16 rsums60 = [0.]*16 rsumsPrev60 = [0.]*16 # First pass is to find beginning of the collisions tsColl = -100000 for i in range(0, inputTree.GetEntries()): nb = inputTree.GetEntry(i) if nb < 0: continue if inputTree.rates[refIndx] > rateCut: tsColl = inputTree.tstamp break for i in range(0, inputTree.GetEntries()): nb = inputTree.GetEntry(i) if nb < 0: continue #Copy data to a new tree fillData.fill = inputTree.fill fillData.fillStart = inputTree.fillStart fillData.fillColl = tsColl fillData.fillStable = inputTree.fillStable fillData.fillEnd = inputTree.fillEnd fillData.durationStable = inputTree.durationStable fillData.bField = inputTree.bField fillData.beamEnergy = inputTree.beamEnergy radmonData.tstamp = inputTree.tstamp for j in range(0, 16): radmonData.status[j] = inputTree.status[j] radmonData.rates[j] = inputTree.rates[j] #Running sums (c) AK # 15 minutes if i+1 < 15*60: rsums15[j] += inputTree.rates[j] radmonData.rsums15[j] = rsums15[j] rsumsPrev15[j] = rsums15[j] else: rsums15[j] = ((15*60 - 1)*rsumsPrev15[j] + inputTree.rates[j])/15/60 radmonData.rsums15[j] = rsums15[j] rsumsPrev15[j] = rsums15[j] # 30 minutes if i+1 < 30*60: rsums30[j] += inputTree.rates[j] radmonData.rsums30[j] = rsums30[j] rsumsPrev30[j] = rsums30[j] else: rsums30[j] = ((30*60 - 1)*rsumsPrev30[j] + inputTree.rates[j])/30/60 radmonData.rsums30[j] = rsums30[j] rsumsPrev30[j] = rsums30[j] # 60 minutes if i+1 < 60*60: rsums60[j] += inputTree.rates[j] radmonData.rsums60[j] = rsums60[j] rsumsPrev60[j] = rsums60[j] else: rsums60[j] = ((60*60 - 1)*rsumsPrev60[j] + inputTree.rates[j])/60/60 radmonData.rsums60[j] = rsums60[j] rsumsPrev60[j] = rsums60[j] t.Fill() fout.Write() fout.Close() del fillData del radmonData del t del fout del inputTree del inputFile
def setupRanges(self): """ read file with lumi data setup low edges of histograms corresponding to the beginning of lumisections fill array of fill data (fill, ls, run) corresponding to the given bin """ lsection = -1 fillData = FillData() fillReport = FillReport(fillReportName) #Start/End/Stable tsStart = fillReport.getFillCreationTime(self.fill) tsEnd = fillReport.getFillEndTime(self.fill) tsStable = fillReport.getFillStableTime(self.fill) duration = fillReport.getFillDuration(self.fill) bField = fillReport.getFillField(self.fill) beamEnergy = fillReport.getFillBeamEnergy(self.fill) #Time range to look for radmon files self.fromto[0] = tsStart self.fromto[1] = tsEnd f = TFile(lumiFilePattern.replace('__XXX__', str(self.fill))) tree = f.Get("t") for i in range(0, tree.GetEntries()) : nb = tree.GetEntry(i) if nb < 0: continue #Y range for histograms if tree.bestLumi > self.yMax: self.yMax = tree.bestLumi if tree.bestLumi < self.yMin: self.yMin = tree.bestLumi #Low edges for histos (start of LS) if tree.lsNo != lsection: self.xx.append(tree.lsStart + tree.lsStartMs/1000000.) lsection = tree.lsNo # New lumisection - Add fill data fillData.fill = tree.fill fillData.fillStart = tsStart fillData.fillStable = tsStable fillData.fillEnd = tsEnd fillData.durationStable = duration fillData.bField = bField fillData.beamEnergy = beamEnergy fillData.bunchSpacing = tree.bunchSpacing fillData.run = tree.run fillData.lsNo = tree.lsNo self.fillDataList.append(fillData) print "Done setting up ranges for fill", self.fill, "\tNo of bins", len(self.xx) -1 del tree del f del fillReport return len(self.xx) -1
def writeOutput(self): rootFile = radmonLumiFilePattern.replace('__XXX__', str(self.fill)) try: fout = TFile(rootFile, 'RECREATE') except IOError: print "Cannot open file for output", rootFile exit t = TTree('t', 'Combined Lumi and Radmon data') fillData = FillData() lumiData = LumiData() radmonData = RadmonData() t.Branch( 'fillBranch', fillData, 'fill/I:fillStart/I:fillStable/I:fillEnd/I:durationStable/I:run/I:lsNo/I:tstamp/I:msecs/I:bunchSpacing/I' ) t.Branch('fillBranchD', AddressOf(fillData, 'bField'), 'bField/D:beamEnergy/D') t.Branch( 'lumiBranch', lumiData, 'bestLumi/D:bestLumiErr/D:hfLumi/D:hfLumiErr/D:pltLumi/D:pltLumiErr/D:pltZeroLumi/D:pltZeroLumiErr/D:bcmfLumi/D:bcmfLumiErr/D' ) t.Branch('radmonBranch', radmonData, 'rates[16]/D:ratesErr[16]/D') href = self.pbest # Reference histogram #self.pbcmf.Draw() #raw_input("Press enter to continue") print "Starting filling tree, file ", radmonLumiFilePattern.replace( '__XXX__', str(self.fill)) for i in range(len(self.xx) - 1): fillData.fill = self.fillDataList[i].fill fillData.fillStart = self.fillDataList[i].fillStart fillData.fillStable = self.fillDataList[i].fillStable fillData.fillEnd = self.fillDataList[i].fillEnd fillData.durationStable = self.fillDataList[i].durationStable fillData.run = self.fillDataList[i].run fillData.lsNo = self.fillDataList[i].lsNo fillData.bunchSpacing = self.fillDataList[i].bunchSpacing fillData.bField = self.fillDataList[i].bField fillData.beamEnergy = self.fillDataList[i].beamEnergy fillData.tstamp = int(href.GetXaxis().GetBinCenter(i)) fillData.msecs = int(href.GetXaxis().GetBinCenter(i) - fillData.tstamp) lumiData.bestLumi = self.pbest.GetBinContent(i) lumiData.bestLumiErr = self.pbest.GetBinError(i) lumiData.hfLumi = self.phf.GetBinContent(i) lumiData.hfLumiErr = self.phf.GetBinError(i) lumiData.pltLumi = self.pplt.GetBinContent(i) lumiData.pltLumiErr = self.pplt.GetBinError(i) lumiData.pltZeroLumi = self.ppltZero.GetBinContent(i) lumiData.pltZeroLumiErr = self.ppltZero.GetBinError(i) lumiData.bcmfLumi = self.pbcmf.GetBinContent(i) lumiData.bcmfLumiErr = self.pbcmf.GetBinError(i) for j in range(len(self.pradmon)): radmonData.rates[j] = self.pradmon[j].GetBinContent(i) radmonData.ratesErr[j] = self.pradmon[j].GetBinError(i) t.Fill() print "Writing", rootFile fout.Write() fout.Close() del fout
def setupRanges(self): """ read file with lumi data setup low edges of histograms corresponding to the beginning of lumisections fill array of fill data (fill, ls, run) corresponding to the given bin """ lsection = -1 fillData = FillData() fillReport = FillReport(fillReportName) #Start/End/Stable tsStart = fillReport.getFillCreationTime(self.fill) tsEnd = fillReport.getFillEndTime(self.fill) tsStable = fillReport.getFillStableTime(self.fill) duration = fillReport.getFillDuration(self.fill) bField = fillReport.getFillField(self.fill) beamEnergy = fillReport.getFillBeamEnergy(self.fill) #Time range to look for radmon files self.fromto[0] = tsStart self.fromto[1] = tsEnd f = TFile(lumiFilePattern.replace('__XXX__', str(self.fill))) tree = f.Get("t") for i in range(0, tree.GetEntries()): nb = tree.GetEntry(i) if nb < 0: continue #Y range for histograms if tree.bestLumi > self.yMax: self.yMax = tree.bestLumi if tree.bestLumi < self.yMin: self.yMin = tree.bestLumi #Low edges for histos (start of LS) if tree.lsNo != lsection: self.xx.append(tree.lsStart + tree.lsStartMs / 1000000.) lsection = tree.lsNo # New lumisection - Add fill data fillData.fill = tree.fill fillData.fillStart = tsStart fillData.fillStable = tsStable fillData.fillEnd = tsEnd fillData.durationStable = duration fillData.bField = bField fillData.beamEnergy = beamEnergy fillData.bunchSpacing = tree.bunchSpacing fillData.run = tree.run fillData.lsNo = tree.lsNo self.fillDataList.append(fillData) print "Done setting up ranges for fill", self.fill, "\tNo of bins", len( self.xx) - 1 del tree del f del fillReport return len(self.xx) - 1