Logo ROOT  
Reference Guide
TTreeReaderFast.hxx
Go to the documentation of this file.
1// @(#)root/tree:$Id$
2// Author: Brian Bockelman, 2017-06-13
3
4/*************************************************************************
5 * Copyright (C) 1995-2017, 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_TTreeReaderFast
13#define ROOT_TTreeReaderFast
14
15
16////////////////////////////////////////////////////////////////////////////
17// //
18// TTreeReader //
19// //
20// A simple interface for reading trees or chains. //
21// //
22// //
23////////////////////////////////////////////////////////////////////////////
24
25#include "TTree.h"
26#include "TTreeReader.h"
27
28#include <deque>
29
30// Forward decl's
31namespace ROOT {
32namespace Experimental {
33namespace Internal {
34class TTreeReaderValueFast;
35class TTreeReaderValueFastBase;
36} // Internal
37
38class TTreeReaderFast: public TObject {
39public:
40
41 // A simple iterator based on TTreeReader::Iterator_t; allows use of the
42 // TTreeReaderFast.
43 //
44 // NOTE that an increment may invalidate previous copies of the iterator.
46 public std::iterator<std::input_iterator_tag, const Long64_t, Long64_t> {
47 private:
48 Int_t *fIdx{nullptr}; ///< Current offset inside this cluster.
49 Int_t fCount{0}; ///< Number of entries inside this cluster.
50 Long64_t fEntry{-1}; ///< Entry number of the tree referenced by this iterator; -1 is invalid.
51 TTreeReaderFast *fReader{nullptr}; ///< The reader we select the entries on.
52
53 /// Whether the iterator points to a valid entry.
54 bool IsValid() const { return fEntry >= 0; }
55
56 public:
57 /// Default-initialize the iterator as "past the end".
59
60 /// Initialize the iterator with the reader it steers and a
61 /// tree entry number; -1 is invalid.
63 fIdx(&(reader.fEvtIndex)), fEntry(first), fReader(&reader)
64 {
65 //printf("Initializing new iterator; start of %lld, proceed for %lld events.\n", first, count);
67 *fIdx = 0;
68 }
69
70 /// Compare two iterators for equality.
71 bool operator==(const Iterator_t& lhs) const {
72 // From C++14: value initialized (past-end) it compare equal.
73 if (R__unlikely(!IsValid() && !lhs.IsValid())) {return true;}
74 return R__unlikely(fEntry == lhs.fEntry && fReader == lhs.fReader);
75 }
76
77 /// Compare two iterators for inequality.
78 bool operator!=(const Iterator_t& lhs) const {
79 return !(*this == lhs);
80 }
81
82 /// Increment the iterator (postfix i++).
84 Iterator_t ret = *this;
85 this->operator++();
86 return ret;
87 }
88
89 /// Increment the iterator (prefix ++i).
91 (*fIdx)++;
92 if (R__unlikely(*fIdx == fCount)) {
93 //printf("Hit end-of-basket of %d events. Get next entry.\n", fCount);
94 fEntry += fCount;
95 *fIdx = 0;
97 //printf("This chunk has %d events.\n", fCount);
98 if (R__unlikely(!fCount || (fCount < 0))) {
99 fEntry = -1;
101 }
102 }
103 return *this;
104 }
105
106 /// Set the entry number in the reader and return it.
108 return fEntry + *fIdx;
109 }
110
112 return **const_cast<Iterator_t*>(this);
113 }
114 };
115
117
119 fTree(nullptr),
120 fEntryStatus(TTreeReader::kEntryNoTree),
121 fLastEntry(-1)
122 {}
123
125 TTreeReaderFast(const char* keyname, TDirectory* dir = NULL );
126
128
130
132
133 TTree* GetTree() const { return fTree; }
134
136
137 /// Return an iterator to the 0th TTree entry.
139 return Iterator_t(*this, 0);
140 }
141 Iterator_t end() const { return Iterator_t(); }
142
143protected:
144
145 // Returns a reference to the current event index in the various value buffers.
147
150
151private:
152
154 void Initialize();
155
156 TTree* fTree{nullptr}; ///< tree that's read
157 TDirectory* fDirectory{nullptr}; ///< directory (or current file for chains)
158 ROOT::Internal::TBranchProxyDirector* fDirector{nullptr}; ///< proxying director, owned
160 std::deque<ROOT::Experimental::Internal::TTreeReaderValueFastBase*> fValues; ///< readers that use our director
161
165
167
168 ClassDef(TTreeReaderFast, 0); // A simple interface to read trees via bulk I/O
169};
170
171} // Experimental
172} // ROOT
173#endif // defined TTreeReaderFast
#define R__unlikely(expr)
Definition: RConfig.hxx:604
int Int_t
Definition: RtypesCore.h:43
long long Long64_t
Definition: RtypesCore.h:71
#define ClassDef(name, id)
Definition: Rtypes.h:322
bool operator==(const Iterator_t &lhs) const
Compare two iterators for equality.
Iterator_t(TTreeReaderFast &reader, Long64_t first)
Initialize the iterator with the reader it steers and a tree entry number; -1 is invalid.
Int_t fCount
Number of entries inside this cluster.
Long64_t fEntry
Entry number of the tree referenced by this iterator; -1 is invalid.
Iterator_t & operator++()
Increment the iterator (prefix ++i).
Int_t * fIdx
Current offset inside this cluster.
Iterator_t operator++(int)
Increment the iterator (postfix i++).
bool operator!=(const Iterator_t &lhs) const
Compare two iterators for inequality.
bool IsValid() const
Whether the iterator points to a valid entry.
Long64_t operator*()
Set the entry number in the reader and return it.
TTreeReaderFast * fReader
The reader we select the entries on.
Iterator_t()
Default-initialize the iterator as "past the end".
Int_t GetNextRange(Int_t)
Advance to the next range in the file; returns the number of events in the range.
void Initialize()
Initialization of the director.
std::deque< ROOT::Experimental::Internal::TTreeReaderValueFastBase * > fValues
readers that use our director
TTreeReader::EEntryStatus SetEntriesRange(Long64_t first, Long64_t last)
TDirectory * fDirectory
directory (or current file for chains)
~TTreeReaderFast()
Tell all value readers that the tree reader does not exist anymore.
ROOT::Internal::TBranchProxyDirector * fDirector
proxying director, owned
Iterator_t begin()
Return an iterator to the 0th TTree entry.
TTreeReader::EEntryStatus GetEntryStatus() const
TTreeReader::EEntryStatus fEntryStatus
status of most recent read request
TTreeReader::EEntryStatus SetEntry(Long64_t)
Set an entry to be read.
void DeregisterValueReader(ROOT::Experimental::Internal::TTreeReaderValueFastBase *reader)
Remove a value reader for this tree.
void RegisterValueReader(ROOT::Experimental::Internal::TTreeReaderValueFastBase *reader)
Add a value reader for this tree.
Describe directory structure in memory.
Definition: TDirectory.h:40
Mother of all ROOT objects.
Definition: TObject.h:37
A simple, robust and fast interface to read values from ROOT columnar datasets such as TTree,...
Definition: TTreeReader.h:43
@ kEntryNotLoaded
no entry has been loaded yet
Definition: TTreeReader.h:126
@ kEntryBadReader
One of the readers was not successfully initialized.
Definition: TTreeReader.h:133
A TTree represents a columnar dataset.
Definition: TTree.h:78
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
Definition: StringConv.hxx:21
Definition: first.py:1
Definition: tree.py:1