ROOT  6.06/08
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  // Iterate through the entries of a TTree.
52  //
53  // This iterator drives the associated TTreeReader; its
54  // dereferencing (and actually even the iteration) will
55  // set the entry number represented by this iterator.
56  // It does not really represent a data element; it simply
57  // returns the entry number (or -1 once the end of the tree
58  // is reached).
59  class Iterator_t:
60  public std::iterator<std::input_iterator_tag, const Long64_t, Long64_t> {
61  private:
62  Long64_t fEntry; // Entry number of the tree referenced by this iterator; -1 is invalid.
63  TTreeReader* fReader; // The reader we select the entries on.
64 
65  // Whether the iterator points to a valid entry.
66  bool IsValid() const { return fEntry >= 0; }
67 
68  public:
69  // Default-initialize the iterator as "past the end".
70  Iterator_t(): fEntry(-1), fReader() {}
71 
72  // Initialize the iterator with the reader it steers and a
73  // tree entry number; -1 is invalid.
74  Iterator_t(TTreeReader& reader, Long64_t entry):
75  fEntry(entry), fReader(&reader) {}
76 
77  bool operator==(const Iterator_t& lhs) const {
78  // Compare two iterators for equality.
79  // From C++14: value initialized (past-end) it compare equal.
80  if (!IsValid() && !lhs.IsValid()) return true;
81  return fEntry == lhs.fEntry && fReader == lhs.fReader;
82  }
83 
84  bool operator!=(const Iterator_t& lhs) const {
85  // Compare two iterators for inequality.
86  return !(*this == lhs);
87  }
88 
90  // Increment the iterator (postfix i++).
91  Iterator_t ret = *this;
92  this->operator++();
93  return ret;
94  }
95 
97  // Increment the iterator (prefix ++i).
98  if (IsValid()) {
99  ++fEntry;
100  // Force validity check of new fEntry.
101  this->operator*();
102  // Don't set the old entry: op* will if needed, and
103  // in most cases it just adds a lot of spinning back
104  // and forth: in most cases teh sequence is ++i; *i.
105  }
106  return *this;
107  }
108 
109  const Long64_t& operator*() {
110  // Set the entry number in the reader and return it.
111  if (IsValid()) {
112  // If we cannot access that entry, mark the iterator invalid.
113  if (fReader->SetEntry(fEntry) != kEntryValid) {
114  fEntry = -1;
115  }
116  }
117  // There really is no data in this iterator; return the number.
118  return fEntry;
119  }
120 
121  const Long64_t& operator*() const {
122  return **const_cast<Iterator_t*>(this);
123  }
124  };
125 
127 
129  kEntryValid = 0, // data read okay
130  kEntryNotLoaded, // no entry has been loaded yet
131  kEntryNoTree, // the tree does not exist
132  kEntryNotFound, // the tree entry number does not exist
133  kEntryChainSetupError, // problem in accessing a chain element, e.g. file without the tree
134  kEntryChainFileError, // problem in opening a chain's file
135  kEntryDictionaryError, // problem reading dictionary info from tree
136  kEntryLast, // last entry was reached
137  };
138 
140  fDirectory(0),
141  fEntryStatus(kEntryNoTree),
142  fDirector(0),
143  fLastEntry(-1)
144  {}
145 
146  TTreeReader(TTree* tree);
147  TTreeReader(const char* keyname, TDirectory* dir = NULL );
148  TTreeReader(const char* /*keyname*/, TFileCollection* /*files*/) { Error("TTreeReader()", "Not Implemented!");};
149 
150  ~TTreeReader();
151 
152  void SetTree(TTree* tree);
153  void SetTree(const char* /*keyname*/, TDirectory* /*dir = NULL*/ ) { Error("SetTree()", "Not Implemented!");};
154  void SetChain(const char* /*keyname*/, TFileCollection* /*files*/ ) { Error("SetChain()", "Not Implemented!");};
155 
156  Bool_t IsChain() const { return TestBit(kBitIsChain); }
157 
158  Bool_t Next() { return SetEntry(GetCurrentEntry() + 1) == kEntryValid; }
159  EEntryStatus SetEntry(Long64_t entry) { return SetEntryBase(entry, kFALSE); }
160  EEntryStatus SetLocalEntry(Long64_t entry) { return SetEntryBase(entry, kTRUE); }
161  void SetLastEntry(Long64_t entry) { fLastEntry = entry; }
162  EEntryStatus SetEntriesRange(Long64_t first, Long64_t last);
163 
164  EEntryStatus GetEntryStatus() const { return fEntryStatus; }
165 
166  TTree* GetTree() const { return fTree; }
167  Long64_t GetEntries(Bool_t force) const { return fTree ? (force ? fTree->GetEntries() : fTree->GetEntriesFast() ) : -1; }
168  Long64_t GetCurrentEntry() const;
169 
171  // Return an iterator to the 0th TTree entry.
172  return Iterator_t(*this, 0);
173  }
174  Iterator_t end() const { return Iterator_t(); }
175 
176 protected:
177  void Initialize();
178  ROOT::Internal::TNamedBranchProxy* FindProxy(const char* branchname) const {
179  return (ROOT::Internal::TNamedBranchProxy*) fProxies.FindObject(branchname); }
180  TCollection* GetProxies() { return &fProxies; }
181 
182  void RegisterValueReader(ROOT::Internal::TTreeReaderValueBase* reader);
183  void DeregisterValueReader(ROOT::Internal::TTreeReaderValueBase* reader);
184 
185  EEntryStatus SetEntryBase(Long64_t entry, Bool_t local);
186 
187 private:
188 
190  kBitIsChain = BIT(14) // our tree is a chain
191  };
192 
193  TTree* fTree; // tree that's read
194  TDirectory* fDirectory; // directory (or current file for chains)
195  EEntryStatus fEntryStatus; // status of most recent read request
196  ROOT::Internal::TBranchProxyDirector* fDirector; // proxying director, owned
197  std::deque<ROOT::Internal::TTreeReaderValueBase*> fValues; // readers that use our director
198  THashTable fProxies; //attached ROOT::TNamedBranchProxies; owned
199  Long64_t fLastEntry; //< The last entry to be processed. When set (i.e. >= 0), it provides a way to stop looping over the TTree when we reach a certain entry: Next() returns kEntryLast when GetCurrentEntry() reaches fLastEntry
200  Bool_t fProxiesSet; //< True if the proxies have been set, false otherwise
201 
204 
205  ClassDef(TTreeReader, 0); // A simple interface to read trees
206 };
207 
208 #endif // defined TTreeReader
ROOT::Internal::TNamedBranchProxy * FindProxy(const char *branchname) const
Definition: TTreeReader.h:178
bool operator!=(const Iterator_t &lhs) const
Definition: TTreeReader.h:84
long long Long64_t
Definition: RtypesCore.h:69
TCollection * GetProxies()
Definition: TTreeReader.h:180
TTreeReader is a simple, robust and fast interface to read values from a TTree, TChain or TNtuple...
Definition: TTreeReader.h:48
Namespace for new ROOT classes and functions.
Definition: ROOT.py:1
#define BIT(n)
Definition: Rtypes.h:120
std::deque< ROOT::Internal::TTreeReaderValueBase * > fValues
Definition: TTreeReader.h:197
void SetTree(const char *, TDirectory *)
Definition: TTreeReader.h:153
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
bool IsValid() const
Definition: TTreeReader.h:66
THashTable fProxies
Definition: TTreeReader.h:198
TTreeReader(const char *, TFileCollection *)
Definition: TTreeReader.h:148
TTree * GetTree() const
Definition: TTreeReader.h:166
THashTable implements a hash table to store TObject&#39;s.
Definition: THashTable.h:39
#define ClassDef(name, id)
Definition: Rtypes.h:254
Iterator_t begin()
Definition: TTreeReader.h:170
TDirectory * fDirectory
Definition: TTreeReader.h:194
EEntryStatus fEntryStatus
Definition: TTreeReader.h:195
const Long64_t & operator*()
Definition: TTreeReader.h:109
Long64_t fLastEntry
Definition: TTreeReader.h:199
Iterator_t end() const
Definition: TTreeReader.h:174
ROOT::Internal::TBranchProxyDirector * fDirector
Definition: TTreeReader.h:196
virtual TObject * FindObject(const char *name) const
Must be redefined in derived classes.
Definition: TObject.cxx:379
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
Definition: TTreeReader.h:200
Collection abstract base class.
Definition: TCollection.h:48
Long64_t GetEntries(Bool_t force) const
Definition: TTreeReader.h:167
Iterator_t iterator
Definition: TTreeReader.h:126
EEntryStatus SetEntry(Long64_t entry)
Definition: TTreeReader.h:159
const Long64_t & operator*() const
Definition: TTreeReader.h:121
void SetChain(const char *, TFileCollection *)
Definition: TTreeReader.h:154
Iterator_t operator++(int)
Definition: TTreeReader.h:89
EEntryStatus SetLocalEntry(Long64_t entry)
Definition: TTreeReader.h:160
Describe directory structure in memory.
Definition: TDirectory.h:41
TTree * fTree
Definition: TTreeReader.h:193
EEntryStatus GetEntryStatus() const
Definition: TTreeReader.h:164
Bool_t IsChain() const
Definition: TTreeReader.h:156
Iterator_t & operator++()
Definition: TTreeReader.h:96
Mother of all ROOT objects.
Definition: TObject.h:58
Bool_t Next()
Definition: TTreeReader.h:158
Class that contains a list of TFileInfo&#39;s and accumulated meta data information about its entries...
#define NULL
Definition: Rtypes.h:82
bool operator==(const Iterator_t &lhs) const
Definition: TTreeReader.h:77
A TTree object has a header with a name and a title.
Definition: TTree.h:94
TTreeReader * fReader
Definition: TTreeReader.h:63
void SetLastEntry(Long64_t entry)
Definition: TTreeReader.h:161
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)
Definition: TTreeReader.h:74