Logo ROOT   6.08/07
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  bool GetBranchAndLeaf(TBranch* &branch, TLeaf* &myLeaf,
50  TDictionary* &branchActualType);
51  void SetImpl(TBranch* branch, TLeaf* myLeaf);
52  const char* GetBranchContentDataType(TBranch* branch,
53  TString& contentTypeName,
54  TDictionary* &dict);
55 
56  TVirtualCollectionReader* fImpl; // Common interface to collections
57 
58  // FIXME: re-introduce once we have ClassDefInline!
59  //ClassDef(TTreeReaderArrayBase, 0);//Accessor to member of an object stored in a collection
60  };
61 
62 } // namespace Internal
63 } // namespace ROOT
64 
65 template <typename T>
67 public:
68 
69  // Iterator through the indices of a TTreeReaderArray.
70  struct Iterator_t:
71  public std::iterator<std::input_iterator_tag, T, long> {
72  // Default initialized, past-end iterator.
74  fIndex(0), fArray(0) {}
75 
76  // Initialize with an array and index.
77  Iterator_t(size_t idx, TTreeReaderArray* array) :
78  fIndex(idx), fArray(array) {}
79 
80  size_t fIndex; // Current index in the array.
81  TTreeReaderArray* fArray; // The array iterated over; 0 if invalid / end.
82 
83  bool IsValid() const { return fArray; }
84 
85  bool operator==(const Iterator_t& lhs) const {
86  // Compare two iterators as equal; follow C++14 requiring two past-end
87  // iterators to be equal.
88  if (!IsValid() && !lhs.IsValid())
89  return true;
90  return fIndex == lhs.fIndex && fArray == lhs.fArray;
91  }
92 
93  bool operator!=(const Iterator_t& lhs) const {
94  // Compare not equal.
95  return !(*this == lhs);
96  }
97 
99  // Post-increment (it++).
100  Iterator_t ret = *this;
101  this->operator++();
102  return ret;
103  }
104 
106  // Pre-increment (++it).
107  if (IsValid()) {
108  ++fIndex;
109  if (fIndex >= fArray->GetSize()) {
110  // Remember that it's past-end.
111  fArray = 0;
112  }
113  }
114  return *this;
115  }
116 
117  T& operator*() const {
118  // Get the referenced element.
119  R__ASSERT(fArray && "invalid iterator!");
120  return fArray->At(fIndex);
121  }
122  };
123 
124  typedef Iterator_t iterator;
125 
126  TTreeReaderArray(TTreeReader& tr, const char* branchname):
127  TTreeReaderArrayBase(&tr, branchname, TDictionary::GetDictionary(typeid(T)))
128  {
129  // Create an array reader of branch "branchname" for TTreeReader "tr".
130  }
131 
132  T& At(size_t idx) { return *(T*)UntypedAt(idx); }
133  T& operator[](size_t idx) { return At(idx); }
134 
135  Iterator_t begin() {
136  // Return an iterator to the 0th TTree entry or an empty iterator if the
137  // array is empty.
138  return IsEmpty() ? Iterator_t() : Iterator_t(0, this);
139  }
140  Iterator_t end() const { return Iterator_t(); }
141 
142 protected:
143 #define R__TTreeReaderArray_TypeString(T) #T
144  virtual const char* GetDerivedTypeName() const { return R__TTreeReaderArray_TypeString(T); }
145 #undef R__TTreeReaderArray_TypeString
146  // FIXME: re-introduce once we have ClassDefTInline!
147  //ClassDefT(TTreeReaderArray, 0);//Accessor to member of an object stored in a collection
148 };
149 
150 #endif // ROOT_TTreeReaderArray
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition: TLeaf.h:37
TTreeReaderArray(TTreeReader &tr, const char *branchname)
const char * GetBranchContentDataType(TBranch *branch, TString &contentTypeName, TDictionary *&dict)
Access a branch&#39;s collection content (not the collection itself) through a proxy. ...
TTreeReader is a simple, robust and fast interface to read values from a TTree, TChain or TNtuple...
Definition: TTreeReader.h:48
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
Definition: StringConv.hxx:21
double T(double x)
Definition: ChebyshevPol.h:34
bool operator!=(const Iterator_t &lhs) const
virtual void CreateProxy()
Create the proxy object for our branch.
#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
Detail::TBranchProxy * GetProxy() const
TTreeReaderArrayBase(TTreeReader *reader, const char *branchname, TDictionary *dict)
Iterator_t end() const
bool operator==(const Iterator_t &lhs) const
Iterator_t(size_t idx, TTreeReaderArray *array)
virtual const char * GetDerivedTypeName() const
TTreeReaderValueBase::EReadStatus fReadStatus
bool GetBranchAndLeaf(TBranch *&branch, TLeaf *&myLeaf, TDictionary *&branchActualType)
Determine the branch / leaf and its type; reset fProxy / fSetupStatus on error.
This class defines an abstract interface that must be implemented by all classes that contain diction...
Definition: TDictionary.h:162
virtual EReadStatus GetReadStatus() const
Extracts array data from a TTree.
void * UntypedAt(size_t idx) const
T & operator[](size_t idx)
#define R__TTreeReaderArray_TypeString(T)
TVirtualCollectionReader * fImpl
virtual void * At(Detail::TBranchProxy *, size_t)=0
A TTree is a list of TBranches.
Definition: TBranch.h:58
Iterator_t begin()
void SetImpl(TBranch *branch, TLeaf *myLeaf)
Create the TVirtualCollectionReader object for our branch.
virtual size_t GetSize(Detail::TBranchProxy *)=0