ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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.
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),
142  fDirector(0),
143  fLastEntry(-1)
144  {}
145 
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 
163 
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); }
181 
184 
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
const Long64_t & operator*() const
Definition: TTreeReader.h:121
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
bool IsValid() const
Definition: TTreeReader.h:66
Iterator_t end() const
Definition: TTreeReader.h:174
TTree * GetTree() const
Definition: TTreeReader.h:166
#define BIT(n)
Definition: Rtypes.h:120
std::deque< ROOT::Internal::TTreeReaderValueBase * > fValues
Definition: TTreeReader.h:197
ROOT::Internal::TNamedBranchProxy * FindProxy(const char *branchname) const
Definition: TTreeReader.h:178
void SetTree(const char *, TDirectory *)
Definition: TTreeReader.h:153
bool operator==(const Iterator_t &lhs) const
Definition: TTreeReader.h:77
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
THashTable fProxies
Definition: TTreeReader.h:198
TTreeReader(const char *, TFileCollection *)
Definition: TTreeReader.h:148
EEntryStatus SetEntryBase(Long64_t entry, Bool_t local)
Load an entry into the tree, return the status of the read.
THashTable implements a hash table to store TObject's.
Definition: THashTable.h:39
Long64_t GetCurrentEntry() const
Returns the index of the current entry being read.
Iterator_t begin()
Definition: TTreeReader.h:170
TDirectory * fDirectory
Definition: TTreeReader.h:194
Long64_t GetEntries(Bool_t force) const
Definition: TTreeReader.h:167
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:918
EEntryStatus fEntryStatus
Definition: TTreeReader.h:195
const Long64_t & operator*()
Definition: TTreeReader.h:109
ClassDef(TTreeReader, 0)
Long64_t fLastEntry
Definition: TTreeReader.h:199
void RegisterValueReader(ROOT::Internal::TTreeReaderValueBase *reader)
Add a value reader for this tree.
ROOT::Internal::TBranchProxyDirector * fDirector
Definition: TTreeReader.h:196
Bool_t IsChain() const
Definition: TTreeReader.h:156
This class defines an abstract interface that must be implemented by all classes that contain diction...
Definition: TDictionary.h:162
Bool_t fProxiesSet
Definition: TTreeReader.h:200
Collection abstract base class.
Definition: TCollection.h:48
Iterator_t iterator
Definition: TTreeReader.h:126
Bool_t TestBit(UInt_t f) const
Definition: TObject.h:173
~TTreeReader()
Tell all value readers that the tree reader does not exist anymore.
EEntryStatus SetEntry(Long64_t entry)
Definition: TTreeReader.h:159
bool first
Definition: line3Dfit.C:48
Long64_t entry
void SetChain(const char *, TFileCollection *)
Definition: TTreeReader.h:154
tuple tree
Definition: tree.py:24
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:44
TTree * fTree
Definition: TTreeReader.h:193
virtual Long64_t GetEntriesFast() const
Definition: TTree.h:388
void dir(char *path=0)
Definition: rootalias.C:30
void Initialize()
Initialization of the director.
void DeregisterValueReader(ROOT::Internal::TTreeReaderValueBase *reader)
Remove a value reader for this tree.
EEntryStatus GetEntryStatus() const
Definition: TTreeReader.h:164
Iterator_t & operator++()
Definition: TTreeReader.h:96
Mother of all ROOT objects.
Definition: TObject.h:58
bool operator!=(const Iterator_t &lhs) const
Definition: TTreeReader.h:84
Bool_t Next()
Definition: TTreeReader.h:158
Class that contains a list of TFileInfo's and accumulated meta data information about its entries...
TObject * FindObject(const char *name) const
Find object using its name.
Definition: THashTable.cxx:210
#define NULL
Definition: Rtypes.h:82
virtual Long64_t GetEntries() const
Definition: TTree.h:386
A TTree object has a header with a name and a title.
Definition: TTree.h:98
void SetTree(TTree *tree)
Set (or update) the which tree to reader from.
TTreeReader * fReader
Definition: TTreeReader.h:63
void SetLastEntry(Long64_t entry)
Definition: TTreeReader.h:161
const Bool_t kTRUE
Definition: Rtypes.h:91
EEntryStatus SetEntriesRange(Long64_t first, Long64_t last)
Set the range of entries to be processed.
Iterator_t(TTreeReader &reader, Long64_t entry)
Definition: TTreeReader.h:74