CMS 3D CMS Logo

eventloop.py
Go to the documentation of this file.
1 from PhysicsTools.NanoAODTools.postprocessing.framework.datamodel import Event
2 from PhysicsTools.NanoAODTools.postprocessing.framework.treeReaderArrayTools import clearExtraBranches
3 import sys
4 import time
5 import ROOT
6 
7 
8 class Module(object):
9  def __init__(self):
10  self.writeHistFile = False
11 
12  def beginJob(self, histFile=None, histDirName=None):
13  if histFile != None and histDirName != None:
14  self.writeHistFile = True
15  prevdir = ROOT.gDirectory
16  self.histFile = histFile
17  self.histFile.cd()
18  self.dir = self.histFile.mkdir(histDirName, "", True)
19  prevdir.cd()
20  self.objs = []
21 
22  def endJob(self):
23  if hasattr(self, 'objs') and self.objs != None:
24  prevdir = ROOT.gDirectory
25  self.dir.cd()
26  for obj in self.objs:
27  obj.Write()
28  prevdir.cd()
29 
30  def beginFile(self, inputFile, outputFile, inputTree, wrappedOutputTree):
31  pass
32 
33  def endFile(self, inputFile, outputFile, inputTree, wrappedOutputTree):
34  pass
35 
36  def analyze(self, event):
37  """process event, return True (go to next module) or False (fail, go to next event)"""
38  pass
39 
40  def addObject(self, obj):
41  setattr(self, obj.GetName(), obj)
42  self.objs.append(getattr(self, obj.GetName()))
43 
44  def addObjectList(self, names, obj):
45  objlist = []
46  for iname, name in enumerate(names):
47  setattr(self, obj.GetName() + '_' + name,
48  obj.Clone(obj.GetName() + '_' + name))
49  objlist.append(getattr(self, obj.GetName() + '_' + name))
50  self.objs.append(getattr(self, obj.GetName() + '_' + name))
51  setattr(self, obj.GetName(), objlist)
52 
53 
54 def eventLoop(
55  modules, inputFile, outputFile, inputTree, wrappedOutputTree,
56  maxEvents=-1, eventRange=None, progress=(10000, sys.stdout),
57  filterOutput=True
58 ):
59  for m in modules:
60  m.beginFile(inputFile, outputFile, inputTree, wrappedOutputTree)
61 
62  t0 = time.time()
63  tlast = t0
64  doneEvents = 0
65  acceptedEvents = 0
66  entries = inputTree.entries
67  if eventRange:
68  entries = len(eventRange)
69  if maxEvents > 0:
70  entries = min(entries, maxEvents)
71 
72  for ie, i in enumerate(range(entries) if eventRange == None else eventRange):
73  if maxEvents > 0 and ie >= maxEvents:
74  break
75  e = Event(inputTree, i)
76  clearExtraBranches(inputTree)
77  doneEvents += 1
78  ret = True
79  for m in modules:
80  ret = m.analyze(e)
81  if not ret:
82  break
83  if ret:
84  acceptedEvents += 1
85  if (ret or not filterOutput) and wrappedOutputTree != None:
86  wrappedOutputTree.fill()
87  if progress:
88  if ie > 0 and ie % progress[0] == 0:
89  t1 = time.time()
90  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" % (
91  ie, entries, ie / float(0.01 * entries),
92  t1 - t0, (progress[0] / 1000.) / (max(t1 - tlast, 1e-9)),
93  ie / 1000. / (max(t1 - t0, 1e-9)),
94  acceptedEvents, doneEvents,
95  acceptedEvents / (0.01 * doneEvents)))
96  tlast = t1
97  for m in modules:
98  m.endFile(inputFile, outputFile, inputTree, wrappedOutputTree)
99  return (doneEvents, acceptedEvents, time.time() - t0)
def endJob(self)
Definition: eventloop.py:22
def analyze(self, event)
Definition: eventloop.py:36
def beginFile(self, inputFile, outputFile, inputTree, wrappedOutputTree)
Definition: eventloop.py:30
def addObjectList(self, names, obj)
Definition: eventloop.py:44
def __init__(self)
Definition: eventloop.py:9
def addObject(self, obj)
Definition: eventloop.py:40
def mkdir(path)
Definition: eostools.py:251
def eventLoop(modules, inputFile, outputFile, inputTree, wrappedOutputTree, maxEvents=-1, eventRange=None, progress=(10000, sys.stdout), filterOutput=True)
Definition: eventloop.py:58
def beginJob(self, histFile=None, histDirName=None)
Definition: eventloop.py:12
def endFile(self, inputFile, outputFile, inputTree, wrappedOutputTree)
Definition: eventloop.py:33