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