def eventLoop(modules, inputFile, outputFile, inputTree, wrappedOutputTree, outputFileSmear=None, outputTreeSmear=None, typeofprocess=None, maxEvents=-1, eventRange=None, progress=(10000, sys.stdout), filterOutput=True): for m in modules: if typeofprocess == "smear": m.beginFile(inputFile, outputFile, inputTree, wrappedOutputTree, outputFileSmear, outputTreeSmear) else: m.beginFile(inputFile, outputFile, inputTree, wrappedOutputTree) t0 = time.clock() tlast = t0 doneEvents = 0 acceptedEvents = 0 entries = inputTree.entries for i in xrange(entries) if eventRange == None else eventRange: if maxEvents > 0 and i >= maxEvents - 1: break e = Event(inputTree, i) clearExtraBranches(inputTree) doneEvents += 1 ret = True for m in modules: ret = m.analyze(e) if not ret: break if ret: acceptedEvents += 1 if ( ret or not filterOutput ) and wrappedOutputTree != None and typeofprocess != "resp" and typeofprocess != "tau" and typeofprocess != "smear": wrappedOutputTree.fill() if progress: if i > 0 and i % progress[0] == 0: t1 = time.clock() progress[1].write( "Processed %8d/%8d entries (elapsed time %7.1fs, curr speed %8.3f kHz, avg speed %8.3f kHz), accepted %8d/%8d events (%5.2f%%)\n" % (i, entries, t1 - t0, (progress[0] / 1000.) / (max(t1 - tlast, 1e-9)), i / 1000. / (max(t1 - t0, 1e-9)), acceptedEvents, doneEvents, acceptedEvents / (0.01 * doneEvents))) tlast = t1 for m in modules: m.endFile(inputFile, outputFile, inputTree, wrappedOutputTree) return (doneEvents, acceptedEvents, time.clock() - t0)
def eventLoop(modules, inputFile, outputFile, inputTree, wrappedOutputTree, maxEvents=-1, eventRange=None, progress=(10000, sys.stdout), filterOutput=True): for m in modules: m.beginFile(inputFile, outputFile, inputTree, wrappedOutputTree) t0 = time.time() tlast = t0 doneEvents = 0 acceptedEvents = 0 entries = inputTree.entries if eventRange: entries = len(eventRange) if maxEvents > 0: entries = min(entries, maxEvents) for ie, i in enumerate( range(entries) if eventRange == None else eventRange): if maxEvents > 0 and ie >= maxEvents: break e = Event(inputTree, i) clearExtraBranches(inputTree) doneEvents += 1 ret = True for m in modules: ret = m.analyze(e) if not ret: break if ret: acceptedEvents += 1 if (ret or not filterOutput) and wrappedOutputTree != None: wrappedOutputTree.fill() if progress: if ie > 0 and ie % progress[0] == 0: t1 = time.time() progress[1].write( "Processed %8d/%8d entries, %5.2f%% (elapsed time %7.1fs, curr speed %8.3f kHz, avg speed %8.3f kHz), accepted %8d/%8d events (%5.2f%%)\n" % (ie, entries, ie / float(0.01 * entries), t1 - t0, (progress[0] / 1000.) / (max(t1 - tlast, 1e-9)), ie / 1000. / (max(t1 - t0, 1e-9)), acceptedEvents, doneEvents, acceptedEvents / (0.01 * doneEvents))) tlast = t1 for m in modules: m.endFile(inputFile, outputFile, inputTree, wrappedOutputTree) return (doneEvents, acceptedEvents, time.time() - t0)
def run(self): outpostfix = self.postfix if self.postfix != None else ( "_Friend" if self.friend else "_Skim") fullClone = False outFileNames = [] totEntriesRead = 0 t0 = time.time() for fileName in self.inputFiles: # open file print("Opening file %s" % fileName) inFile = ROOT.TFile.Open(fileName) if (not inFile): #check for null pointer print("Unable to open file %s, exting \n" % fileName) return 1 # get input tree inTree = inFile.Get("Events") nEntries = min(inTree.GetEntries() - self.firstEntry, self.maxEntries) totEntriesRead += nEntries # pre-skimming elist, jsonFilter = preSkim(inTree, self.json, self.cut, maxEntries=self.maxEntries, firstEntry=self.firstEntry) # number of events to be processed nTotal = elist.GetN() if elist else nEntries print('Pre-select %d entries out of %s ' % (nTotal, nEntries)) inTree = InputTree(inTree, elist) # output outFileName = os.path.join( self.outputDir, os.path.basename(fileName).replace(".root", outpostfix + ".root")) #compressionAlgo = ROOT.ROOT.kLZMA #compressionLevel = int(9) compressionAlgo = ROOT.ROOT.kLZ4 compressionLevel = int(4) outFile = ROOT.TFile.Open(outFileName, "RECREATE", "", compressionLevel) outFileNames.append(outFileName) outFile.SetCompressionAlgorithm(compressionAlgo) maxEntries = self.maxEntries if self.perJet: #save two first jets maxEntries = self.maxEntries * 2 outTree = FullOutput(inFile, inTree, outFile, branchSelection=self.branchsel, outputbranchSelection=self.outputbranchsel, fullClone=fullClone, maxEntries=maxEntries, firstEntry=self.firstEntry, jsonFilter=jsonFilter, provenance=self.provenance) t0 = time.time() tlast = t0 doneEvents = 0 acceptedEvents = 0 if elist: eventRange = [(elist.GetEntry(0) if i == 0 else elist.Next()) for i in range(elist.GetN())] else: eventRange = range(self.firstEntry, self.firstEntry + nEntries) if nEntries > 0 else None entries = inTree.entries if eventRange: entries = len(eventRange) maxEvents = self.maxEntries if maxEvents > 0: entries = min(entries, self.maxEntries) entriesRange = range(entries) if eventRange == None else eventRange for m in self.modules: m.beginFile(inFile, outFile, inTree, outTree, entriesRange) for ie, i in enumerate(entriesRange): if maxEvents > 0 and ie >= maxEvents: break e = Event(inTree, ie) ret = True if self.perJet: #print('ie ',ie) for m in self.modules: ret = m.analyze(e, ie) if not ret: break else: clearExtraBranches(inTree) m.fill(e, ie) else: clearExtraBranches(inTree) for m in self.modules: ret = m.analyze(e, ie) if not ret: break if ret and outTree is not None: outTree.fill() if ret: acceptedEvents += 1 for m in self.modules: m.endFile(inFile, outFile, inTree, outTree) outTree.write() outFile.Close() print("Done %s" % outFileName) for m in self.modules: m.endJob() print("Total time %.1f sec. to process %i events. Rate = %.1f Hz." % ((time.time() - t0), totEntriesRead, totEntriesRead / (time.time() - t0)))
def eventLoop(modules, inputFile, outputFile, inputTree, wrappedOutputTree, maxEvents=-1, eventRange=None, progress=(10000, sys.stdout), filterOutput=True): for m in modules: m.beginFile(inputFile, outputFile, inputTree, wrappedOutputTree) h_nevents = ROOT.TH1F('nEvents', 'nEvents', 4, 0, 4) h_nevents.Sumw2() h_nevents.GetXaxis().SetBinLabel(1, "total") h_nevents.GetXaxis().SetBinLabel(2, "pos") h_nevents.GetXaxis().SetBinLabel(3, "neg") h_nevents.GetXaxis().SetBinLabel(4, "pass") t0 = time.clock() tlast = t0 doneEvents = 0 acceptedEvents = 0 entries = inputTree.entries if eventRange: entries = len(eventRange) if maxEvents > 0: entries = min(entries, maxEvents) for ie, i in enumerate( xrange(entries) if eventRange == None else eventRange): if maxEvents > 0 and ie >= maxEvents: break e = Event(inputTree, i) weight = 1. if hasattr(e, "genWeight"): genWeight = getattr(e, "genWeight", None) if genWeight: weight = genWeight h_nevents.Fill(0.5, weight) if weight >= 0: h_nevents.Fill(1.5, weight) else: h_nevents.Fill(2.5, weight) clearExtraBranches(inputTree) doneEvents += 1 ret = True for m in modules: ret = m.analyze(e) if not ret: break if ret: acceptedEvents += 1 if (ret or not filterOutput) and wrappedOutputTree != None: h_nevents.Fill(3.5, weight) wrappedOutputTree.fill() if progress: if ie > 0 and ie % progress[0] == 0: t1 = time.clock() progress[1].write( "Processed %8d/%8d entries, %5.2f%% (elapsed time %7.1fs, curr speed %8.3f kHz, avg speed %8.3f kHz), accepted %8d/%8d events (%5.2f%%)\n" % (ie, entries, ie / float(0.01 * entries), t1 - t0, (progress[0] / 1000.) / (max(t1 - tlast, 1e-9)), ie / 1000. / (max(t1 - t0, 1e-9)), acceptedEvents, doneEvents, acceptedEvents / (0.01 * doneEvents))) tlast = t1 for m in modules: m.endFile(inputFile, outputFile, inputTree, wrappedOutputTree) prevdir = ROOT.gDirectory outputFile.cd() h_nevents.Write() prevdir.cd() return (doneEvents, acceptedEvents, time.clock() - t0)