Logo ROOT   6.07/09
Reference Guide
TTreeReader.h
Go to the documentation of this file.
1 // @(#)root/tree:$Id$
2 // Author: Axel Naumann, 2010-08-02
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2013, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TTreeReader
13 #define ROOT_TTreeReader
14 
15 
16 ////////////////////////////////////////////////////////////////////////////
17 // //
18 // TTreeReader //
19 // //
20 // A simple interface for reading trees or chains. //
21 // //
22 // //
23 ////////////////////////////////////////////////////////////////////////////
24 
25 #ifndef ROOT_THashTable
26 #include "THashTable.h"
27 #endif
28 #ifndef ROOT_TTree
29 #include "TTree.h"
30 #endif
31 #ifndef ROOT_TTreeReaderUtils
32 #include "TTreeReaderUtils.h"
33 #endif
34 
35 #include <deque>
36 #include <iterator>
37 
38 class TDictionary;
39 class TDirectory;
40 class TFileCollection;
41 
42 namespace ROOT {
43 namespace Internal {
44  class TBranchProxyDirector;
45 }
46 }
47 
48 class TTreeReader: public TObject {
49 public:
50 
51  ///\class TTreeReader::Iterator_t
52  /// Iterate through the entries of a TTree.
53  ///
54  /// This iterator drives the associated TTreeReader; its
55  /// dereferencing (and actually even the iteration) will
56  /// set the entry number represented by this iterator.
57  /// It does not really represent a data element; it simply
58  /// returns the entry number (or -1 once the end of the tree
59  /// is reached).
60  class Iterator_t:
61  public std::iterator<std::input_iterator_tag, const Long64_t, Long64_t> {
62  private:
63  Long64_t fEntry; ///< Entry number of the tree referenced by this iterator; -1 is invalid.
64  TTreeReader* fReader; ///< The reader we select the entries on.
65 
66  /// Whether the iterator points to a valid entry.
67  bool IsValid() const { return fEntry >= 0; }
68 
69  public:
70  /// Default-initialize the iterator as "past the end".
71  Iterator_t(): fEntry(-1), fReader() {}
72 
73  /// Initialize the iterator with the reader it steers and a
74  /// tree entry number; -1 is invalid.
75  Iterator_t(TTreeReader& reader, Long64_t entry):
76  fEntry(entry), fReader(&reader) {}
77 
78  /// Compare two iterators for equality.
79  bool operator==(const Iterator_t& lhs) const {
80  // From C++14: value initialized (past-end) it compare equal.
81  if (!IsValid() && !lhs.IsValid()) return true;
82  return fEntry == lhs.fEntry && fReader == lhs.fReader;
83  }
84 
85  /// Compare two iterators for inequality.
86  bool operator!=(const Iterator_t& lhs) const {
87  return !(*this == lhs);
88  }
89 
90  /// Increment the iterator (postfix i++).
92  Iterator_t ret = *this;
93  this->operator++();
94  return ret;
95  }
96 
97  /// Increment the iterator (prefix ++i).
99  if (IsValid()) {
100  ++fEntry;
101  // Force validity check of new fEntry.
102  this->operator*();
103  // Don't set the old entry: op* will if needed, and
104  // in most cases it just adds a lot of spinning back
105  // and forth: in most cases teh sequence is ++i; *i.
106  }
107  return *this;
108  }
109 
110  /// Set the entry number in the reader and return it.
111  const Long64_t& operator*() {
112  if (IsValid()) {
113  // If we cannot access that entry, mark the iterator invalid.
114  if (fReader->SetEntry(fEntry) != kEntryValid) {
115  fEntry = -1;
116  }
117  }
118  // There really is no data in this iterator; return the number.
119  return fEntry;
120  }
121 
122  const Long64_t& operator*() const {
123  return **const_cast<Iterator_t*>(this);
124  }
125  };
126 
128 
130  kEntryValid = 0, ///< data read okay
131  kEntryNotLoaded, ///< no entry has been loaded yet
132  kEntryNoTree, ///< the tree does not exist
133  kEntryNotFound, ///< the tree entry number does not exist
134  kEntryChainSetupError, ///< problem in accessing a chain element, e.g. file without the tree
135  kEntryChainFileError, ///< problem in opening a chain's file
136  kEntryDictionaryError, ///< problem reading dictionary info from tree
137  kEntryLast, ///< last entry was reached
138  };
139 
141  fTree(0),
142  fDirectory(0),
143  fEntryStatus(kEntryNoTree),
144  fDirector(0),
145  fLastEntry(-1),
146  fProxiesSet(false)
147  {}
148 
150  TTreeReader(const char* keyname, TDirectory* dir = NULL );
151  TTreeReader(const char* /*keyname*/, TFileCollection* /*files*/) { Error("TTreeReader()", "Not Implemented!");};
152 
153  ~TTreeReader();
154 
155  void SetTree(TTree* tree);
156  void SetTree(const char* /*keyname*/, TDirectory* /*dir = NULL*/ ) { Error("SetTree()", "Not Implemented!");};
157  void SetChain(const char* /*keyname*/, TFileCollection* /*files*/ ) { Error("SetChain()", "Not Implemented!");};
158 
159  Bool_t IsChain() const { return TestBit(kBitIsChain); }
160 
161  Bool_t Next() { return SetEntry(GetCurrentEntry() + 1) == kEntryValid; }
162  EEntryStatus SetEntry(Long64_t entry) { return SetEntryBase(entry, kFALSE); }
163  EEntryStatus SetLocalEntry(Long64_t entry) { return SetEntryBase(entry, kTRUE); }
164  void SetLastEntry(Long64_t entry) { fLastEntry = entry; }
165  EEntryStatus SetEntriesRange(Long64_t first, Long64_t last);
166 
167  EEntryStatus GetEntryStatus() const { return fEntryStatus; }
168 
169  TTree* GetTree() const { return fTree; }
170  Long64_t GetEntries(Bool_t force) const { return fTree ? (force ? fTree->GetEntries() : fTree->GetEntriesFast() ) : -1; }
171  Long64_t GetCurrentEntry() const;
172 
173  /// Return an iterator to the 0th TTree entry.
175  return Iterator_t(*this, 0);
176  }
177  Iterator_t end() const { return Iterator_t(); }
178 
179 protected:
180  void Initialize();
181  ROOT::Internal::TNamedBranchProxy* FindProxy(const char* branchname) const {
182  return (ROOT::Internal::TNamedBranchProxy*) fProxies.FindObject(branchname); }
183  TCollection* GetProxies() { return &fProxies; }
184 
185  void RegisterValueReader(ROOT::Internal::TTreeReaderValueBase* reader);
186  void DeregisterValueReader(ROOT::Internal::TTreeReaderValueBase* reader);
187 
188  EEntryStatus SetEntryBase(Long64_t entry, Bool_t local);
189 
190 private:
191 
193  kBitIsChain = BIT(14) ///< our tree is a chain
194  };
195 
196  TTree* fTree; ///< tree that's read
197  TDirectory* fDirectory; ///< directory (or current file for chains)
198  EEntryStatus fEntryStatus; ///< status of most recent read request
199  ROOT::Internal::TBranchProxyDirector* fDirector; ///< proxying director, owned
200  std::deque<ROOT::Internal::TTreeReaderValueBase*> fValues; ///< readers that use our director
201  THashTable fProxies; ///< attached ROOT::TNamedBranchProxies; owned
202 
203  /// The last entry to be processed. When set (i.e. >= 0), it provides a way
204  /// to stop looping over the TTree when we reach a certain entry: Next()
205  /// returns kEntryLast when GetCurrentEntry() reaches fLastEntry
207  Bool_t fProxiesSet; ///< True if the proxies have been set, false otherwise
208 
211 
212  ClassDef(TTreeReader, 0); // A simple interface to read trees
213 };
214 
215 #endif // defined TTreeReader
the tree does not exist
Definition: TTreeReader.h:132
const Long64_t & operator*() const
Definition: TTreeReader.h:122
Iterate through the entries of a TTree.
Definition: TTreeReader.h:60
long long Long64_t
Definition: RtypesCore.h:69
TCollection * GetProxies()
Definition: TTreeReader.h:183
TTreeReader is a simple, robust and fast interface to read values from a TTree, TChain or TNtuple...
Definition: TTreeReader.h:48
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
Definition: StringConv.hxx:21
last entry was reached
Definition: TTreeReader.h:137
bool IsValid() const
Whether the iterator points to a valid entry.
Definition: TTreeReader.h:67
Iterator_t end() const
Definition: TTreeReader.h:177
TTree * GetTree() const
Definition: TTreeReader.h:169
std::deque< ROOT::Internal::TTreeReaderValueBase * > fValues
readers that use our director
Definition: TTreeReader.h:200
ROOT::Internal::TNamedBranchProxy * FindProxy(const char *branchname) const
Definition: TTreeReader.h:181
void SetTree(const char *, TDirectory *)
Definition: TTreeReader.h:156
bool operator==(const Iterator_t &lhs) const
Compare two iterators for equality.
Definition: TTreeReader.h:79
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
Long64_t fEntry
Entry number of the tree referenced by this iterator; -1 is invalid.
Definition: TTreeReader.h:63
THashTable fProxies
attached ROOT::TNamedBranchProxies; owned
Definition: TTreeReader.h:201
TTreeReader(const char *, TFileCollection *)
Definition: TTreeReader.h:151
THashTable implements a hash table to store TObject&#39;s.
Definition: THashTable.h:39
#define ClassDef(name, id)
Definition: Rtypes.h:254
the tree entry number does not exist
Definition: TTreeReader.h:133
Iterator_t begin()
Return an iterator to the 0th TTree entry.
Definition: TTreeReader.h:174
TDirectory * fDirectory
directory (or current file for chains)
Definition: TTreeReader.h:197
Long64_t GetEntries(Bool_t force) const
Definition: TTreeReader.h:170
EEntryStatus fEntryStatus
status of most recent read request
Definition: TTreeReader.h:198
const Long64_t & operator*()
Set the entry number in the reader and return it.
Definition: TTreeReader.h:111
Iterator_t()
Default-initialize the iterator as "past the end".
Definition: TTreeReader.h:71
Long64_t fLastEntry
The last entry to be processed.
Definition: TTreeReader.h:206
ROOT::Internal::TBranchProxyDirector * fDirector
proxying director, owned
Definition: TTreeReader.h:199
Bool_t IsChain() const
Definition: TTreeReader.h:159
void Initialize(Bool_t useTMVAStyle=kTRUE)
Definition: tmvaglob.cxx:176
This class defines an abstract interface that must be implemented by all classes that contain diction...
Definition: TDictionary.h:162
TTime operator*(const TTime &t1, const TTime &t2)
Definition: TTime.h:87
Bool_t fProxiesSet
True if the proxies have been set, false otherwise.
Definition: TTreeReader.h:207
Collection abstract base class.
Definition: TCollection.h:48
Iterator_t iterator
Definition: TTreeReader.h:127
EEntryStatus SetEntry(Long64_t entry)
Definition: TTreeReader.h:162
no entry has been loaded yet
Definition: TTreeReader.h:131
problem in opening a chain&#39;s file
Definition: TTreeReader.h:135
problem in accessing a chain element, e.g. file without the tree
Definition: TTreeReader.h:134
void SetChain(const char *, TFileCollection *)
Definition: TTreeReader.h:157
Iterator_t operator++(int)
Increment the iterator (postfix i++).
Definition: TTreeReader.h:91
EEntryStatus SetLocalEntry(Long64_t entry)
Definition: TTreeReader.h:163
Describe directory structure in memory.
Definition: TDirectory.h:44
TTree * fTree
tree that&#39;s read
Definition: TTreeReader.h:196
EEntryStatus GetEntryStatus() const
Definition: TTreeReader.h:167
problem reading dictionary info from tree
Definition: TTreeReader.h:136
#define BIT(n)
Definition: Rtypes.h:120
Iterator_t & operator++()
Increment the iterator (prefix ++i).
Definition: TTreeReader.h:98
Mother of all ROOT objects.
Definition: TObject.h:44
bool operator!=(const Iterator_t &lhs) const
Compare two iterators for inequality.
Definition: TTreeReader.h:86
Bool_t Next()
Definition: TTreeReader.h:161
Class that contains a list of TFileInfo&#39;s and accumulated meta data information about its entries...
#define NULL
Definition: Rtypes.h:82
Definition: tree.py:1
A TTree object has a header with a name and a title.
Definition: TTree.h:98
Definition: first.py:1
TTreeReader * fReader
The reader we select the entries on.
Definition: TTreeReader.h:64
void SetLastEntry(Long64_t entry)
Definition: TTreeReader.h:164
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual TObject * FindObject(const char *name) const
Must be redefined in derived classes.
Definition: TObject.cxx:380
void Error(ErrorHandler_t func, int code, const char *va_(fmt),...)
Write error message and call a handler, if required.
Iterator_t(TTreeReader &reader, Long64_t entry)
Initialize the iterator with the reader it steers and a tree entry number; -1 is invalid.
Definition: TTreeReader.h:75