# Tai Sakuma <tai.sakuma@cern.ch>

##____________________________________________________________________________||
class Events(object):
    """An iterative object for events.


    Examples
    --------

    inputFile = ROOT.TFile.Open(inputPath)
    tree = inputFile.Get(treeName)
    events = Events(tree)
    for event in events:
        event.jet_pt # a branch can be accessed as an attribute

    """

    def __init__(self, tree, maxEvents = -1):
        self.file = tree.GetDirectory() # so a file won't close
        self.tree = tree
        self.nEvents = min(self.tree.GetEntries(), maxEvents) if (maxEvents > -1) else self.tree.GetEntries()
        self.iEvent = -1

    def __getitem__(self, i):
        if i >= self.nEvents:
            self.iEvent = -1
            raise IndexError("the index is out of range: " + str(i))
        self.iEvent = i
        self.tree.GetEntry(self.iEvent)
        return self

    def __iter__(self):
        for self.iEvent in xrange(self.nEvents):
            self.tree.GetEntry(self.iEvent)
            yield self
        self.iEvent = -1

    def __getattr__(self, name):
        return getattr(self.tree, name)

##____________________________________________________________________________||

@@ -175,36 +175,201 @@
 s.%0A%0A
-%0A    Examples%0A    --------%0A%0A
+    You can use this class to iterate over entries in a ROOT TTree.%0A%0A    You can instantiate this class with a TTree object and an%0A    optionally a maximum number of entries to loop over.%0A%0A    
     
@@ -407,16 +407,20 @@
 utPath)%0A
+    
     tree
@@ -446,24 +446,28 @@
 eeName)%0A    
+ 
+   
 events = Eve
@@ -476,16 +476,75 @@
 s(tree)%0A
+%0A    Then, the %22for%22 loop for the tree entries can be%0A%0A    
     for 
@@ -564,30 +564,183 @@
 ts:%0A
+%0A
     
-    event.jet_pt #
+Note: %22event%22 and %22events%22 are the same object. In each iteration,%0A    %22event%22 (and %22events%22) is loaded with the next entry in the tree.%0A%0A    A content of the tree, e.g.,
  a b
@@ -744,16 +744,17 @@
 a branch
+,
  can be 
@@ -767,16 +767,20 @@
 ed as an
+%0A   
  attribu
@@ -781,16 +781,285 @@
 ttribute
+ of %22event%22.%0A%0A          event.jet_pt%0A%0A    In order to access to a particular entry, you can use an index.%0A    For example, to get 11th entry (the index for the first entry is%0A    0),%0A%0A        event = events%5B10%5D%0A%0A    Note: Again %22event%22 and %22events%22 are the same object.
 %0A%0A    %22%22

update docstrings in Events