Logo ROOT   6.10/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 #include "TTreeReaderValue.h"
26 #include "TTreeReaderUtils.h"
27 
28 namespace ROOT {
29 namespace Internal {
30 
32  public:
33  TTreeReaderArrayBase(TTreeReader* reader, const char* branchname,
34  TDictionary* dict):
35  TTreeReaderValueBase(reader, branchname, dict), fImpl(0) {}
36 
37  size_t GetSize() const { return fImpl->GetSize(GetProxy()); }
38  Bool_t IsEmpty() const { return !GetSize(); }
39 
40  virtual EReadStatus GetReadStatus() const { return fImpl ? fImpl->fReadStatus : kReadError; }
41 
42  protected:
43  void* UntypedAt(size_t idx) const { return fImpl->At(GetProxy(), idx); }
44  virtual void CreateProxy();
45  bool GetBranchAndLeaf(TBranch* &branch, TLeaf* &myLeaf,
46  TDictionary* &branchActualType);
47  void SetImpl(TBranch* branch, TLeaf* myLeaf);
48  const char* GetBranchContentDataType(TBranch* branch,
49  TString& contentTypeName,
50  TDictionary* &dict);
51 
52  TVirtualCollectionReader* fImpl; // Common interface to collections
53 
54  // FIXME: re-introduce once we have ClassDefInline!
55  //ClassDef(TTreeReaderArrayBase, 0);//Accessor to member of an object stored in a collection
56  };
57 
58 } // namespace Internal
59 } // namespace ROOT
60 
61 template <typename T>
63 public:
64 
65  // Iterator through the indices of a TTreeReaderArray.
66  struct Iterator_t:
67  public std::iterator<std::input_iterator_tag, T, long> {
68  // Default initialized, past-end iterator.
70  fIndex(0), fArray(0) {}
71 
72  // Initialize with an array and index.
73  Iterator_t(size_t idx, TTreeReaderArray* array) :
74  fIndex(idx), fArray(array) {}
75 
76  size_t fIndex; // Current index in the array.
77  TTreeReaderArray* fArray; // The array iterated over; 0 if invalid / end.
78 
79  bool IsValid() const { return fArray; }
80 
81  bool operator==(const Iterator_t& lhs) const {
82  // Compare two iterators as equal; follow C++14 requiring two past-end
83  // iterators to be equal.
84  if (!IsValid() && !lhs.IsValid())
85  return true;
86  return fIndex == lhs.fIndex && fArray == lhs.fArray;
87  }
88 
89  bool operator!=(const Iterator_t& lhs) const {
90  // Compare not equal.
91  return !(*this == lhs);
92  }
93 
95  // Post-increment (it++).
96  Iterator_t ret = *this;
97  this->operator++();
98  return ret;
99  }
100 
102  // Pre-increment (++it).
103  if (IsValid()) {
104  ++fIndex;
105  if (fIndex >= fArray->GetSize()) {
106  // Remember that it's past-end.
107  fArray = 0;
108  }
109  }
110  return *this;
111  }
112 
113  T& operator*() const {
114  // Get the referenced element.
115  R__ASSERT(fArray && "invalid iterator!");
116  return fArray->At(fIndex);
117  }
118 
119  operator const T*() const { return &fArray->At(fIndex); }
120  };
121 
122  typedef Iterator_t iterator;
123 
124  TTreeReaderArray(TTreeReader& tr, const char* branchname):
125  TTreeReaderArrayBase(&tr, branchname, TDictionary::GetDictionary(typeid(T)))
126  {
127  // Create an array reader of branch "branchname" for TTreeReader "tr".
128  }
129 
130  T& At(size_t idx) { return *(T*)UntypedAt(idx); }
131  T& operator[](size_t idx) { return At(idx); }
132 
133  Iterator_t begin() {
134  // Return an iterator to the 0th TTree entry or an empty iterator if the
135  // array is empty.
136  return IsEmpty() ? Iterator_t() : Iterator_t(0, this);
137  }
138  Iterator_t end() const { return Iterator_t(); }
139 
140 protected:
141 #define R__TTreeReaderArray_TypeString(T) #T
142  virtual const char* GetDerivedTypeName() const { return R__TTreeReaderArray_TypeString(T); }
143 #undef R__TTreeReaderArray_TypeString
144  // FIXME: re-introduce once we have ClassDefTInline!
145  //ClassDefT(TTreeReaderArray, 0);//Accessor to member of an object stored in a collection
146 };
147 
148 #endif // ROOT_TTreeReaderArray
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition: TLeaf.h:32
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:43
Namespace for new ROOT classes and functions.
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:96
T & At(size_t idx)
Basic string class.
Definition: TString.h:129
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:158
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:57
Iterator_t begin()
void SetImpl(TBranch *branch, TLeaf *myLeaf)
Create the TVirtualCollectionReader object for our branch.
virtual size_t GetSize(Detail::TBranchProxy *)=0