Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 {
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.
45 class Iterator_t {
46
47 Int_t *fIdx{nullptr}; ///< Current offset inside this cluster.
48 Int_t fCount{0}; ///< Number of entries inside this cluster.
49 Long64_t fEntry{-1}; ///< Entry number of the tree referenced by this iterator; -1 is invalid.
50 TTreeReaderFast *fReader{nullptr}; ///< The reader we select the entries on.
51
52 /// Whether the iterator points to a valid entry.
53 bool IsValid() const { return fEntry >= 0; }
54
55 public:
56 using iterator_category = std::input_iterator_tag;
57 using value_type = const Long64_t;
59 using pointer = const Long64_t *;
60 using const_pointer = const Long64_t *;
61 using reference = const Long64_t &;
62
63 /// Default-initialize the iterator as "past the end".
65
66 /// Initialize the iterator with the reader it steers and a
67 /// tree entry number; -1 is invalid.
69 fIdx(&(reader.fEvtIndex)), fEntry(first), fReader(&reader)
70 {
71 //printf("Initializing new iterator; start of %lld, proceed for %lld events.\n", first, count);
73 *fIdx = 0;
74 }
75
76 /// Compare two iterators for equality.
77 bool operator==(const Iterator_t& lhs) const {
78 // From C++14: value initialized (past-end) it compare equal.
79 if (R__unlikely(!IsValid() && !lhs.IsValid())) {return true;}
80 return R__unlikely(fEntry == lhs.fEntry && fReader == lhs.fReader);
81 }
82
83 /// Compare two iterators for inequality.
84 bool operator!=(const Iterator_t& lhs) const {
85 return !(*this == lhs);
86 }
87
88 /// Increment the iterator (postfix i++).
90 Iterator_t ret = *this;
91 this->operator++();
92 return ret;
93 }
94
95 /// Increment the iterator (prefix ++i).
97 (*fIdx)++;
98 if (R__unlikely(*fIdx == fCount)) {
99 //printf("Hit end-of-basket of %d events. Get next entry.\n", fCount);
100 fEntry += fCount;
101 *fIdx = 0;
103 //printf("This chunk has %d events.\n", fCount);
104 if (R__unlikely(!fCount || (fCount < 0))) {
105 fEntry = -1;
107 }
108 }
109 return *this;
110 }
111
112 /// Set the entry number in the reader and return it.
114 return fEntry + *fIdx;
115 }
116
118 return **const_cast<Iterator_t*>(this);
119 }
120 };
121
123
125 fTree(nullptr),
126 fEntryStatus(TTreeReader::kEntryNoTree),
127 fLastEntry(-1)
128 {}
129
131 TTreeReaderFast(const char* keyname, TDirectory* dir = NULL );
132
134
136
138
139 TTree* GetTree() const { return fTree; }
140
142
143 /// Return an iterator to the 0th TTree entry.
145 return Iterator_t(*this, 0);
146 }
147 Iterator_t end() const { return Iterator_t(); }
148
149protected:
150
151 // Returns a reference to the current event index in the various value buffers.
153
156
157private:
158
160 void Initialize();
161
162 TTree* fTree{nullptr}; ///< tree that's read
163 TDirectory* fDirectory{nullptr}; ///< directory (or current file for chains)
164 ROOT::Internal::TBranchProxyDirector* fDirector{nullptr}; ///< proxying director, owned
166 std::deque<ROOT::Experimental::Internal::TTreeReaderValueFastBase*> fValues; ///< readers that use our director
167
171
173
174 ClassDef(TTreeReaderFast, 0); // A simple interface to read trees via bulk I/O
175};
176
177} // Experimental
178} // ROOT
179#endif // defined TTreeReaderFast
#define R__unlikely(expr)
Definition RConfig.hxx:598
long Long_t
Definition RtypesCore.h:54
long long Long64_t
Definition RtypesCore.h:80
#define ClassDef(name, id)
Definition Rtypes.h:325
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:45
Mother of all ROOT objects.
Definition TObject.h:41
Extracts data from a TTree.
A simple, robust and fast interface to read values from ROOT columnar datasets such as TTree,...
Definition TTreeReader.h:44
@ kEntryNotLoaded
no entry has been loaded yet
@ kEntryBadReader
One of the readers was not successfully initialized.
A TTree represents a columnar dataset.
Definition TTree.h:79
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
Definition first.py:1
Definition tree.py:1