ROOT  6.06/09
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 // TTreeReaderArray //
19 // //
20 // A simple interface for reading data from trees or chains. //
21 // //
22 // //
23 ////////////////////////////////////////////////////////////////////////////
24 
25 #ifndef ROOT_TTreeReaderValue
26 #include "TTreeReaderValue.h"
27 #endif
28 #ifndef ROOT_TTreeReaderUtils
29 #include "TTreeReaderUtils.h"
30 #endif
31 
32 namespace ROOT {
33 namespace Internal {
34 
36  public:
37  TTreeReaderArrayBase(TTreeReader* reader, const char* branchname,
38  TDictionary* dict):
39  TTreeReaderValueBase(reader, branchname, dict), fImpl(0) {}
40 
41  size_t GetSize() const { return fImpl->GetSize(GetProxy()); }
42  Bool_t IsEmpty() const { return !GetSize(); }
43 
44  virtual EReadStatus GetReadStatus() const { return fImpl ? fImpl->fReadStatus : kReadError; }
45 
46  protected:
47  void* UntypedAt(size_t idx) const { return fImpl->At(GetProxy(), idx); }
48  virtual void CreateProxy();
49  const char* GetBranchContentDataType(TBranch* branch,
50  TString& contentTypeName,
51  TDictionary* &dict) const;
52 
53  TVirtualCollectionReader* fImpl; // Common interface to collections
54 
55  // FIXME: re-introduce once we have ClassDefInline!
56  //ClassDef(TTreeReaderArrayBase, 0);//Accessor to member of an object stored in a collection
57  };
58 
59 } // namespace Internal
60 } // namespace ROOT
61 
62 template <typename T>
64 public:
65 
66  // Iterator through the indices of a TTreeReaderArray.
67  struct Iterator_t:
68  public std::iterator<std::input_iterator_tag, T, long> {
69  // Default initialized, past-end iterator.
71  fIndex(0), fArray(0) {}
72 
73  // Initialize with an array and index.
74  Iterator_t(size_t idx, TTreeReaderArray* array) :
75  fIndex(idx), fArray(array) {}
76 
77  size_t fIndex; // Current index in the array.
78  TTreeReaderArray* fArray; // The array iterated over; 0 if invalid / end.
79 
80  bool IsValid() const { return fArray; }
81 
82  bool operator==(const Iterator_t& lhs) const {
83  // Compare two iterators as equal; follow C++14 requiring two past-end
84  // iterators to be equal.
85  if (!IsValid() && !lhs.IsValid())
86  return true;
87  return fIndex == lhs.fIndex && fArray == lhs.fArray;
88  }
89 
90  bool operator!=(const Iterator_t& lhs) const {
91  // Compare not equal.
92  return !(*this == lhs);
93  }
94 
96  // Post-increment (it++).
97  Iterator_t ret = *this;
98  this->operator++();
99  return ret;
100  }
101 
103  // Pre-increment (++it).
104  if (IsValid()) {
105  ++fIndex;
106  if (fIndex >= fArray->GetSize()) {
107  // Remember that it's past-end.
108  fArray = 0;
109  }
110  }
111  return *this;
112  }
113 
114  T& operator*() const {
115  // Get the referenced element.
116  R__ASSERT(fArray && "invalid iterator!");
117  return fArray->At(fIndex);
118  }
119  };
120 
122 
123  TTreeReaderArray(TTreeReader& tr, const char* branchname):
124  TTreeReaderArrayBase(&tr, branchname, TDictionary::GetDictionary(typeid(T)))
125  {
126  // Create an array reader of branch "branchname" for TTreeReader "tr".
127  }
128 
129  T& At(size_t idx) { return *(T*)UntypedAt(idx); }
130  T& operator[](size_t idx) { return At(idx); }
131 
133  // Return an iterator to the 0th TTree entry or an empty iterator if the
134  // array is empty.
135  return IsEmpty() ? Iterator_t() : Iterator_t(0, this);
136  }
137  Iterator_t end() const { return Iterator_t(); }
138 
139 protected:
140 #define R__TTreeReaderArray_TypeString(T) #T
141  virtual const char* GetDerivedTypeName() const { return R__TTreeReaderArray_TypeString(T); }
142 #undef R__TTreeReaderArray_TypeString
143  // FIXME: re-introduce once we have ClassDefTInline!
144  //ClassDefT(TTreeReaderArray, 0);//Accessor to member of an object stored in a collection
145 };
146 
147 #endif // ROOT_TTreeReaderArray
TTreeReaderArray(TTreeReader &tr, const char *branchname)
bool operator!=(const Iterator_t &lhs) const
TTreeReader is a simple, robust and fast interface to read values from a TTree, TChain or TNtuple...
Definition: TTreeReader.h:48
Namespace for new ROOT classes and functions.
Definition: ROOT.py:1
double T(double x)
Definition: ChebyshevPol.h:34
virtual EReadStatus GetReadStatus() const
#define R__ASSERT(e)
Definition: TError.h:98
T & At(size_t idx)
Basic string class.
Definition: TString.h:137
bool Bool_t
Definition: RtypesCore.h:59
TTreeReaderArrayBase(TTreeReader *reader, const char *branchname, TDictionary *dict)
bool operator==(const Iterator_t &lhs) const
Iterator_t(size_t idx, TTreeReaderArray *array)
Detail::TBranchProxy * GetProxy() const
TTreeReaderValueBase::EReadStatus fReadStatus
Iterator_t end() const
This class defines an abstract interface that must be implemented by all classes that contain diction...
Definition: TDictionary.h:162
Extracts array data from a TTree.
virtual const char * GetDerivedTypeName() const
T & operator[](size_t idx)
#define R__TTreeReaderArray_TypeString(T)
TVirtualCollectionReader * fImpl
const char * GetBranchContentDataType(TBranch *branch, TString &contentTypeName, TDictionary *&dict) const
Access a branch's collection content (not the collection itself) through a proxy. ...
virtual void * At(Detail::TBranchProxy *, size_t)=0
A TTree is a list of TBranches.
Definition: TBranch.h:58
virtual void CreateProxy()
Create the proxy object for our branch.
Iterator_t begin()
void * UntypedAt(size_t idx) const
virtual size_t GetSize(Detail::TBranchProxy *)=0