CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
eventstfile.py
Go to the documentation of this file.
1 # Copyright (C) 2014 Colin Bernet
2 # https://github.com/cbernet/heppy/blob/master/LICENSE
3 
4 from ROOT import TFile
5 
6 class Events(object):
7  '''Event list from a tree in a root file.
8  '''
9  def __init__(self, filename, treename, options=None):
10  self.file = TFile(filename)
11  if self.file.IsZombie():
12  raise ValueError('file {fnam} does not exist'.format(fnam=filename))
13  self.tree = self.file.Get(treename)
14  if self.tree == None: # is None would not work
15  raise ValueError('tree {tree} does not exist in file {fnam}'.format(
16  tree = treename,
17  fnam = filename
18  ))
19 
20  def size(self):
21  return self.tree.GetEntries()
22 
23  def to(self, iEv):
24  '''navigate to event iEv.'''
25  self.tree.GetEntry(iEv)
26  return self.tree
27 
28  def __iter__(self):
29  return iter(self.tree)
30 
31 if __name__ == '__main__':
32 
33  import sys
34  events = Events(sys.argv[1], 'test_tree')
35  print '\naccessing one event directly'
36  event = events.to(2)
37  print event.var1
38  print '\nlooping on all events:'
39  for iev, ev in enumerate(events):
40  if iev==9:
41  print '...'
42  if iev>=9 and iev<990:
43  continue
44  else:
45  print ev.var1
list object
Definition: dbtoconf.py:77