Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TTreeReaderArray.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_TTreeReaderArray
13#define ROOT_TTreeReaderArray
14
15#include "TTreeReaderValue.h"
16#include "TTreeReaderUtils.h"
17#include <type_traits>
18
19namespace ROOT {
20namespace Internal {
21
22/** \class TTreeReaderArrayBase
23Base class of TTreeReaderArray.
24*/
25
27public:
32
33 std::size_t GetSize() const { return fImpl ? fImpl->GetSize(GetProxy()) : 0; }
34 bool IsEmpty() const { return !GetSize(); }
35
36 EReadStatus GetReadStatus() const override { return fImpl ? fImpl->fReadStatus : kReadError; }
37
38protected:
39 void *UntypedAt(std::size_t idx) const { return fImpl->At(GetProxy(), idx); }
40 void CreateProxy() override;
45
46 std::unique_ptr<TVirtualCollectionReader> fImpl; // Common interface to collections
47
48 // FIXME: re-introduce once we have ClassDefInline!
49 // ClassDefOverride(TTreeReaderArrayBase, 0);//Accessor to member of an object stored in a collection
50};
51
52} // namespace Internal
53} // namespace ROOT
54
55// clang-format off
56/**
57 * \class TTreeReaderArray
58 * \ingroup treeplayer
59 * \brief An interface for reading collections stored in ROOT columnar datasets
60 *
61 * The TTreeReaderArray is a type-safe tool to be used in association with a TTreeReader
62 * to access the collections stored in TTree, TNtuple and TChain datasets.
63 * In order to access values which are not collections, the TTreeReaderValue class can
64 * be used.
65 *
66 * See the documentation of TTreeReader for more details and examples.
67*/
68// clang-format on
69
70template <typename T>
72 // R__CLING_PTRCHECK is disabled because pointer / types are checked by CreateProxy().
73
74public:
75 /// Random access iterator to the elements of a TTreeReaderArray.
76 // The template parameter is there to allow distinguishing between the `const` and `non-const` cases.
77 template <typename ReaderArrayType>
78 class Iterator_t {
79 public:
80 // iterators must define the following types
81 using iterator_category = std::random_access_iterator_tag;
82 using value_type = T;
83 using difference_type = std::ptrdiff_t;
84 using pointer = std::conditional_t<std::is_const<ReaderArrayType>::value, const T *, T *>;
85 using reference = std::conditional_t<std::is_const<ReaderArrayType>::value, const T &, T &>;
86
87 private:
88 TTreeReaderArray *fArray; ///< The array iterated over; nullptr if invalid/past-the-end.
89 std::size_t fIndex; ///< Current index in the array.
90 std::size_t fSize; ///< Size of the TTreeReaderArray
91 public:
92 /// Default ctor: constructs a past-the-end iterator
93 Iterator_t() : fArray(nullptr), fIndex(0u), fSize(0u) {}
94
95 /// Construct iterator
96 Iterator_t(std::size_t index, TTreeReaderArray *array)
97 : fArray(array), fIndex(index), fSize(fArray ? fArray->GetSize() : 0u)
98 {
99 if (fIndex >= fSize)
100 fArray = nullptr; // invalidate iterator
101 }
102
103 /// Construct iterator from a const TTreeReaderArray
104 Iterator_t(std::size_t index, const TTreeReaderArray *array)
106 {
107 }
108
109 Iterator_t(Iterator_t &&) = default;
110 Iterator_t(const Iterator_t &) = default;
112 Iterator_t &operator=(const Iterator_t &) = default;
113
115 {
116 R__ASSERT(fArray && "invalid iterator!");
117 return fArray->At(fIndex);
118 }
119
120 pointer operator->() const { return IsValid() ? &fArray->At(fIndex) : nullptr; }
121
122 bool operator==(const Iterator_t &other) const
123 {
124 // Follow C++14 requiring two past-the-end iterators to be equal.
125 if (!IsValid() && !other.IsValid())
126 return true;
127 return fArray == other.fArray && fIndex == other.fIndex;
128 }
129
130 bool operator!=(const Iterator_t &other) const { return !(*this == other); }
131
132 /// Pre-increment operator
134 {
135 if (IsValid())
136 ++fIndex;
137 if (fIndex >= fSize)
138 fArray = nullptr; // invalidate iterator
139 return *this;
140 }
141
142 /// Post-increment operator
144 {
145 auto ret = *this;
146 this->operator++();
147 return ret;
148 }
149
150 /// Pre-decrement operator
152 {
153 if (fIndex == 0u)
154 fArray = nullptr; // invalidate iterator
155 else
156 --fIndex;
157 return *this;
158 }
159
160 /// Post-decrement operator
162 {
163 auto ret = *this;
164 this->operator--();
165 return ret;
166 }
167
168 Iterator_t operator+(std::ptrdiff_t n) const { return Iterator_t(fIndex + n, fArray); }
169 friend auto operator+(std::ptrdiff_t n, const Iterator_t &it) -> decltype(it + n) { return it + n; }
170
171 Iterator_t operator-(std::ptrdiff_t n) const
172 {
173 const auto index = std::ptrdiff_t(fIndex);
174 const auto newIndex = index >= n ? index - n : std::numeric_limits<decltype(fIndex)>::max();
175 return Iterator_t(newIndex, fArray);
176 }
177
178 std::ptrdiff_t operator-(const Iterator_t &other) const { return fIndex - other.fIndex; }
179
180 Iterator_t &operator+=(std::ptrdiff_t n) { return (*this = *this + n); }
181
182 Iterator_t &operator-=(std::ptrdiff_t n) { return (*this = *this - n); }
183
184 bool operator<(const Iterator_t &other) const { return fIndex < other.fIndex; }
185 bool operator>(const Iterator_t &other) const { return fIndex > other.fIndex; }
186 bool operator<=(const Iterator_t &other) const { return !(*this > other); }
187 bool operator>=(const Iterator_t &other) const { return !(*this < other); }
188
189 reference operator[](std::size_t index) const { return *(*this + index); }
190
191 operator pointer() { return &fArray->At(fIndex); }
192
193 bool IsValid() const { return fArray != nullptr; }
194 };
195
198
199 /// Create an array reader of branch "branchname" for TTreeReader "tr".
201 : TTreeReaderArrayBase(&tr, branchname, TDictionary::GetDictionary(typeid(T)))
202 {
203 }
204
205 T &At(std::size_t idx) { return *static_cast<T *>(UntypedAt(idx)); }
206 const T &At(std::size_t idx) const { return *static_cast<T *>(UntypedAt(idx)); }
207 T &operator[](std::size_t idx) { return At(idx); }
208 const T &operator[](std::size_t idx) const { return At(idx); }
209
210 iterator begin() { return iterator(0u, this); }
211 iterator end() { return iterator(GetSize(), this); }
212 const_iterator begin() const { return cbegin(); }
213 const_iterator end() const { return cend(); }
214 const_iterator cbegin() const { return const_iterator(0u, this); }
215 const_iterator cend() const { return const_iterator(GetSize(), this); }
216
217protected:
218#define R__TTreeReaderArray_TypeString(T) #T
219 const char *GetDerivedTypeName() const override { return R__TTreeReaderArray_TypeString(T); }
220#undef R__TTreeReaderArray_TypeString
221 // FIXME: re-introduce once we have ClassDefTInline!
222 // ClassDefT(TTreeReaderArray, 0);//Accessor to member of an object stored in a collection
223};
224
225namespace cling {
226template <typename T>
227std::string printValue(TTreeReaderArray<T> *val)
228{
229 return printValue(static_cast<ROOT::Internal::TTreeReaderValueBase *>(val));
230}
231} // namespace cling
232
233#endif // ROOT_TTreeReaderArray
dim_t fSize
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
Definition TError.h:125
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
#define R__TTreeReaderArray_TypeString(T)
Base class of TTreeReaderArray.
void CreateProxy() override
Create the proxy object for our branch.
std::unique_ptr< TVirtualCollectionReader > fImpl
bool GetBranchAndLeaf(TBranch *&branch, TLeaf *&myLeaf, TDictionary *&branchActualType, bool suppressErrorsForMissingBranch=false)
Determine the branch / leaf and its type; reset fProxy / fSetupStatus on error.
TTreeReaderArrayBase(TTreeReader *reader, const char *branchname, TDictionary *dict)
void * UntypedAt(std::size_t idx) const
EReadStatus GetReadStatus() const override
void SetImpl(TBranch *branch, TLeaf *myLeaf)
Create the TVirtualCollectionReader object for our branch.
const char * GetBranchContentDataType(TBranch *branch, TString &contentTypeName, TDictionary *&dict)
Access a branch's collection content (not the collection itself) through a proxy.
Base class of TTreeReaderValue.
Detail::TBranchProxy * GetProxy() const
A TTree is a list of TBranches.
Definition TBranch.h:93
This class defines an abstract interface that must be implemented by all classes that contain diction...
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition TLeaf.h:57
Basic string class.
Definition TString.h:139
Random access iterator to the elements of a TTreeReaderArray.
Iterator_t()
Default ctor: constructs a past-the-end iterator.
std::size_t fIndex
Current index in the array.
bool operator>(const Iterator_t &other) const
reference operator[](std::size_t index) const
Iterator_t & operator--()
Pre-decrement operator.
bool operator!=(const Iterator_t &other) const
Iterator_t operator++(int)
Post-increment operator.
bool operator>=(const Iterator_t &other) const
Iterator_t(const Iterator_t &)=default
Iterator_t & operator++()
Pre-increment operator.
Iterator_t operator--(int)
Post-decrement operator.
std::size_t fSize
Size of the TTreeReaderArray.
Iterator_t & operator-=(std::ptrdiff_t n)
Iterator_t & operator=(Iterator_t &&)=default
std::conditional_t< std::is_const< ReaderArrayType >::value, const T *, T * > pointer
bool operator<(const Iterator_t &other) const
std::ptrdiff_t operator-(const Iterator_t &other) const
TTreeReaderArray * fArray
The array iterated over; nullptr if invalid/past-the-end.
Iterator_t & operator=(const Iterator_t &)=default
std::conditional_t< std::is_const< ReaderArrayType >::value, const T &, T & > reference
Iterator_t & operator+=(std::ptrdiff_t n)
Iterator_t(Iterator_t &&)=default
bool operator<=(const Iterator_t &other) const
Iterator_t operator-(std::ptrdiff_t n) const
bool operator==(const Iterator_t &other) const
friend auto operator+(std::ptrdiff_t n, const Iterator_t &it) -> decltype(it+n)
Iterator_t operator+(std::ptrdiff_t n) const
std::random_access_iterator_tag iterator_category
Iterator_t(std::size_t index, TTreeReaderArray *array)
Construct iterator.
Iterator_t(std::size_t index, const TTreeReaderArray *array)
Construct iterator from a const TTreeReaderArray.
An interface for reading collections stored in ROOT columnar datasets.
const char * GetDerivedTypeName() const override
T & At(std::size_t idx)
TTreeReaderArray(TTreeReader &tr, const char *branchname)
Create an array reader of branch "branchname" for TTreeReader "tr".
const_iterator cend() const
const_iterator cbegin() const
const T & At(std::size_t idx) const
const_iterator begin() const
const T & operator[](std::size_t idx) const
T & operator[](std::size_t idx)
const_iterator end() const
A simple, robust and fast interface to read values from ROOT columnar datasets such as TTree,...
Definition TTreeReader.h:45
const Int_t n
Definition legend1.C:16
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...