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)
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  if hasattr(self, 'histFile') and self.histFile != None:
30  self.histFile.Close()
31 
32  def beginFile(self, inputFile, outputFile, inputTree, wrappedOutputTree):
33  pass
34 
35  def endFile(self, inputFile, outputFile, inputTree, wrappedOutputTree):
36  pass
37 
38  def analyze(self, event):
39  """process event, return True (go to next module) or False (fail, go to next event)"""
40  pass
41 
42  def addObject(self, obj):
43  setattr(self, obj.GetName(), obj)
44  self.objs.append(getattr(self, obj.GetName()))
45 
46  def addObjectList(self, names, obj):
47  objlist = []
48  for iname, name in enumerate(names):
49  setattr(self, obj.GetName() + '_' + name,
50  obj.Clone(obj.GetName() + '_' + name))
51  objlist.append(getattr(self, obj.GetName() + '_' + name))
52  self.objs.append(getattr(self, obj.GetName() + '_' + name))
53  setattr(self, obj.GetName(), objlist)
54 
55 
56 def eventLoop(
57  modules, inputFile, outputFile, inputTree, wrappedOutputTree,
58  maxEvents=-1, eventRange=None, progress=(10000, sys.stdout),
59  filterOutput=True
60 ):
61  for m in modules:
62  m.beginFile(inputFile, outputFile, inputTree, wrappedOutputTree)
63 
64  t0 = time.time()
65  tlast = t0
66  doneEvents = 0
67  acceptedEvents = 0
68  entries = inputTree.entries
69  if eventRange:
70  entries = len(eventRange)
71  if maxEvents > 0:
72  entries = min(entries, maxEvents)
73 
74  for ie, i in enumerate(range(entries) if eventRange == None else eventRange):
75  if maxEvents > 0 and ie >= maxEvents:
76  break
77  e = Event(inputTree, i)
78  clearExtraBranches(inputTree)
79  doneEvents += 1
80  ret = True
81  for m in modules:
82  ret = m.analyze(e)
83  if not ret:
84  break
85  if ret:
86  acceptedEvents += 1
87  if (ret or not filterOutput) and wrappedOutputTree != None:
88  wrappedOutputTree.fill()
89  if progress:
90  if ie > 0 and ie % progress[0] == 0:
91  t1 = time.time()
92  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" % (
93  ie, entries, ie / float(0.01 * entries),
94  t1 - t0, (progress[0] / 1000.) / (max(t1 - tlast, 1e-9)),
95  ie / 1000. / (max(t1 - t0, 1e-9)),
96  acceptedEvents, doneEvents,
97  acceptedEvents / (0.01 * doneEvents)))
98  tlast = t1
99  for m in modules:
100  m.endFile(inputFile, outputFile, inputTree, wrappedOutputTree)
101  return (doneEvents, acceptedEvents, time.time() - t0)
def endJob(self)
Definition: eventloop.py:22
def analyze(self, event)
Definition: eventloop.py:38
def beginFile(self, inputFile, outputFile, inputTree, wrappedOutputTree)
Definition: eventloop.py:32
def addObjectList(self, names, obj)
Definition: eventloop.py:46
def __init__(self)
Definition: eventloop.py:9
def addObject(self, obj)
Definition: eventloop.py:42
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:60
def beginJob(self, histFile=None, histDirName=None)
Definition: eventloop.py:12
def endFile(self, inputFile, outputFile, inputTree, wrappedOutputTree)
Definition: eventloop.py:35