Logo ROOT   6.08/07
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.
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  fMostRecentTreeNumber(-1),
145  fDirector(0),
146  fLastEntry(-1),
147  fProxiesSet(false)
148  {}
149 
151  TTreeReader(const char* keyname, TDirectory* dir = NULL );
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  /// \deprecated This is a very misleading interface, please read carefully:
165  ///
166  /// SetLastEntry() really sets the entry that `Next()` will not read, i.e. the
167  /// first entry number for which `Next()` will return kFALSE. It is equivalant
168  /// to end() for iterators.
169  /// This interface has been deprecated in ROOT 6.10 and will be removed in
170  /// ROOT 6.12. Please use SetEntriesRange() instead.
171  void SetLastEntry(Long64_t entry) { fLastEntry = entry; }
172  EEntryStatus SetEntriesRange(Long64_t beginEntry, Long64_t endEntry);
173  void Restart();
174 
175  EEntryStatus GetEntryStatus() const { return fEntryStatus; }
176 
177  TTree* GetTree() const { return fTree; }
178  Long64_t GetEntries(Bool_t force) const { return fTree ? (force ? fTree->GetEntries() : fTree->GetEntriesFast() ) : -1; }
179  Long64_t GetCurrentEntry() const;
180 
181  /// Return an iterator to the 0th TTree entry.
183  return Iterator_t(*this, 0);
184  }
185  Iterator_t end() const { return Iterator_t(); }
186 
187 protected:
188  void Initialize();
189  ROOT::Internal::TNamedBranchProxy* FindProxy(const char* branchname) const {
190  return (ROOT::Internal::TNamedBranchProxy*) fProxies.FindObject(branchname); }
191  TCollection* GetProxies() { return &fProxies; }
192 
193  Bool_t RegisterValueReader(ROOT::Internal::TTreeReaderValueBase* reader);
194  void DeregisterValueReader(ROOT::Internal::TTreeReaderValueBase* reader);
195 
196  EEntryStatus SetEntryBase(Long64_t entry, Bool_t local);
197 
198 private:
199 
201  kBitIsChain = BIT(14) ///< our tree is a chain
202  };
203 
204  TTree* fTree; ///< tree that's read
205  TDirectory* fDirectory; ///< directory (or current file for chains)
206  EEntryStatus fEntryStatus; ///< status of most recent read request
207  Int_t fMostRecentTreeNumber; ///< TTree::GetTreeNumber() of the most recent tree
208  ROOT::Internal::TBranchProxyDirector* fDirector; ///< proxying director, owned
209  std::deque<ROOT::Internal::TTreeReaderValueBase*> fValues; ///< readers that use our director
210  THashTable fProxies; ///< attached ROOT::TNamedBranchProxies; owned
211 
212  /// The last entry to be processed. When set (i.e. >= 0), it provides a way
213  /// to stop looping over the TTree when we reach a certain entry: Next()
214  /// returns kEntryLast when GetCurrentEntry() reaches fLastEntry
216  Bool_t fProxiesSet; ///< True if the proxies have been set, false otherwise
217 
220 
221  ClassDef(TTreeReader, 0); // A simple interface to read trees
222 };
223 
224 #endif // defined TTreeReader
ROOT::Internal::TNamedBranchProxy * FindProxy(const char *branchname) const
Definition: TTreeReader.h:189
the tree does not exist
Definition: TTreeReader.h:132
Iterate through the entries of a TTree.
Definition: TTreeReader.h:60
bool operator!=(const Iterator_t &lhs) const
Compare two iterators for inequality.
Definition: TTreeReader.h:86
long long Long64_t
Definition: RtypesCore.h:69
TCollection * GetProxies()
Definition: TTreeReader.h:191
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
#define BIT(n)
Definition: Rtypes.h:120
std::deque< ROOT::Internal::TTreeReaderValueBase * > fValues
readers that use our director
Definition: TTreeReader.h:209
void SetTree(const char *, TDirectory *)
Definition: TTreeReader.h:156
int Int_t
Definition: RtypesCore.h:41
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
bool IsValid() const
Whether the iterator points to a valid entry.
Definition: TTreeReader.h:67
THashTable fProxies
attached ROOT::TNamedBranchProxies; owned
Definition: TTreeReader.h:210
TTree * GetTree() const
Definition: TTreeReader.h:177
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:182
TDirectory * fDirectory
directory (or current file for chains)
Definition: TTreeReader.h:205
EEntryStatus fEntryStatus
status of most recent read request
Definition: TTreeReader.h:206
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:215
Iterator_t end() const
Definition: TTreeReader.h:185
ROOT::Internal::TBranchProxyDirector * fDirector
proxying director, owned
Definition: TTreeReader.h:208
virtual TObject * FindObject(const char *name) const
Must be redefined in derived classes.
Definition: TObject.cxx:380
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:216
Collection abstract base class.
Definition: TCollection.h:48
Long64_t GetEntries(Bool_t force) const
Definition: TTreeReader.h:178
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
const Long64_t & operator*() const
Definition: TTreeReader.h:122
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:204
problem reading dictionary info from tree
Definition: TTreeReader.h:136
EEntryStatus GetEntryStatus() const
Definition: TTreeReader.h:175
Bool_t IsChain() const
Definition: TTreeReader.h:159
Iterator_t & operator++()
Increment the iterator (prefix ++i).
Definition: TTreeReader.h:98
Mother of all ROOT objects.
Definition: TObject.h:37
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
bool operator==(const Iterator_t &lhs) const
Compare two iterators for equality.
Definition: TTreeReader.h:79
A TTree object has a header with a name and a title.
Definition: TTree.h:98
TTreeReader * fReader
The reader we select the entries on.
Definition: TTreeReader.h:64
Int_t fMostRecentTreeNumber
TTree::GetTreeNumber() of the most recent tree.
Definition: TTreeReader.h:207
void SetLastEntry(Long64_t entry)
Definition: TTreeReader.h:171
const Bool_t kTRUE
Definition: Rtypes.h:91
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