Logo ROOT  
Reference Guide
TTreeReaderValue.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_TTreeReaderValue
13#define ROOT_TTreeReaderValue
14
15////////////////////////////////////////////////////////////////////////////
16// //
17// TTreeReaderValue //
18// //
19// A simple interface for reading data from trees or chains. //
20// //
21// //
22////////////////////////////////////////////////////////////////////////////
23
24#include "TString.h"
25#include "TDictionary.h"
26#include "TBranchProxy.h"
27
28#include <type_traits>
29
30class TBranch;
31class TBranchElement;
32class TLeaf;
33class TTreeReader;
34
35namespace ROOT {
36namespace Internal {
37
38/** \class TTreeReaderValueBase
39Base class of TTreeReaderValue.
40*/
41
43 public:
44
45 // Status flags, 0 is good
47 kSetupNotSetup = -7, /// No initialization has happened yet.
48 kSetupTreeDestructed = -8, /// The TTreeReader has been destructed / not set.
49 kSetupMakeClassModeMismatch = -7, // readers disagree on whether TTree::SetMakeBranch() should be on
50 kSetupMissingCounterBranch = -6, /// The array cannot find its counter branch: Array[CounterBranch]
51 kSetupMissingBranch = -5, /// The specified branch cannot be found.
52 kSetupInternalError = -4, /// Some other error - hopefully the error message helps.
53 kSetupMissingDictionary = -3, /// To read this branch, we need a dictionary.
54 kSetupMismatch = -2, /// Mismatch of branch type and reader template type.
55 kSetupNotACollection = -1, /// The branch class type is not a collection.
56 kSetupMatch = 0, /// This branch has been set up, branch data type and reader template type match, reading should succeed.
57 kSetupMatchBranch = 0, /// This branch has been set up, branch data type and reader template type match, reading should succeed.
58 //kSetupMatchConversion = 1, /// This branch has been set up, the branch data type can be converted to the reader template type, reading should succeed.
59 //kSetupMatchConversionCollection = 2, /// This branch has been set up, the data type of the branch's collection elements can be converted to the reader template type, reading should succeed.
60 //kSetupMakeClass = 3, /// This branch has been set up, enabling MakeClass mode for it, reading should succeed.
61 // kSetupVoidPtr = 4,
63 kSetupMatchLeaf = 6 /// This branch (or TLeaf, really) has been set up, reading should succeed.
64 };
66 kReadSuccess = 0, // data read okay
67 kReadNothingYet, // data now yet accessed
68 kReadError // problem reading data
69 };
70
71 EReadStatus ProxyRead() { return (this->*fProxyReadFunc)(); }
72
74
76 template <BranchProxyRead_t Func>
78
79 Bool_t IsValid() const { return fProxy && 0 == (int)fSetupStatus && 0 == (int)fReadStatus; }
81 virtual EReadStatus GetReadStatus() const { return fReadStatus; }
82
83 /// If we are reading a leaf, return the corresponding TLeaf.
84 TLeaf* GetLeaf() { return fLeaf; }
85
86 void* GetAddress();
87
88 const char* GetBranchName() const { return fBranchName; }
89
90 virtual ~TTreeReaderValueBase();
91
92 protected:
93 TTreeReaderValueBase(TTreeReader* reader, const char* branchname, TDictionary* dict);
96
98 void NotifyNewTree(TTree* newTree);
99
100 TBranch* SearchBranchWithCompositeName(TLeaf *&myleaf, TDictionary *&branchActualType, std::string &err);
101 virtual void CreateProxy();
102 static const char* GetBranchDataType(TBranch* branch,
103 TDictionary* &dict,
104 TDictionary const *curDict);
105
106 virtual const char* GetDerivedTypeName() const = 0;
107
109
111
112 /// Stringify the template argument.
113 static std::string GetElementTypeName(const std::type_info& ti);
114
115 int fHaveLeaf : 1; // Whether the data is in a leaf
116 int fHaveStaticClassOffsets : 1; // Whether !fStaticClassOffsets.empty()
117 EReadStatus fReadStatus : 2; // read status of this data access
118 ESetupStatus fSetupStatus = kSetupNotSetup; // setup status of this data access
119 TString fBranchName; // name of the branch to read data from.
121 TTreeReader* fTreeReader; // tree reader we belong to
122 TDictionary* fDict; // type that the branch should contain
123 Detail::TBranchProxy* fProxy = nullptr; // proxy for this branch, owned by TTreeReader
124 TLeaf* fLeaf = nullptr;
125 std::vector<Long64_t> fStaticClassOffsets;
127 Read_t fProxyReadFunc = &TTreeReaderValueBase::ProxyReadDefaultImpl; ///<! Pointer to the Read implementation to use.
128
129 // FIXME: re-introduce once we have ClassDefInline!
130 //ClassDef(TTreeReaderValueBase, 0);//Base class for accessors to data via TTreeReader
131
132 friend class ::TTreeReader;
133 };
134
135} // namespace Internal
136} // namespace ROOT
137
138
139template <typename T>
140class R__CLING_PTRCHECK(off) TTreeReaderValue final: public ROOT::Internal::TTreeReaderValueBase {
141// R__CLING_PTRCHECK is disabled because pointer / types are checked by CreateProxy().
142
143public:
146 TTreeReaderValue(TTreeReader& tr, const char* branchname):
147 TTreeReaderValueBase(&tr, branchname,
148 TDictionary::GetDictionary(typeid(NonConstT_t))) {}
149
150 /// Return a pointer to the value of the current entry.
151 /// Return a nullptr and print an error if no entry has been loaded yet.
152 /// The returned address is guaranteed to stay constant while a given TTree is being read from a given file,
153 /// unless the branch addresses are manipulated directly (e.g. through TTree::SetBranchAddress()).
154 /// The address might also change when the underlying TTree/TFile is switched, e.g. when a TChain switches files.
155 T* Get() {
156 if (!fProxy){
157 Error("TTreeReaderValue::Get()", "Value reader not properly initialized, did you remember to call TTreeReader.Set(Next)Entry()?");
158 return nullptr;
159 }
160 void *address = GetAddress(); // Needed to figure out if it's a pointer
161 return fProxy->IsaPointer() ? *(T**)address : (T*)address; }
162 /// Return a pointer to the value of the current entry.
163 /// Equivalent to Get().
164 T* operator->() { return Get(); }
165 /// Return a reference to the value of the current entry.
166 /// Equivalent to dereferencing the pointer returned by Get(). Behavior is undefined if no entry has been loaded yet.
167 /// Most likely a crash will occur.
168 T& operator*() { return *Get(); }
169
170protected:
171 // FIXME: use IsA() instead once we have ClassDefTInline
172 /// Get the template argument as a string.
173 virtual const char* GetDerivedTypeName() const {
174 static const std::string sElementTypeName = GetElementTypeName(typeid(T));
175 return sElementTypeName.data();
176 }
177
178 // FIXME: re-introduce once we have ClassDefTInline!
179 //ClassDefT(TTreeReaderValue, 0);//Accessor to data via TTreeReader
180};
181
182#endif // ROOT_TTreeReaderValue
bool Bool_t
Definition: RtypesCore.h:59
void Error(const char *location, const char *msgfmt,...)
int type
Definition: TGX11.cxx:120
Base class for all the proxy object.
Definition: TBranchProxy.h:69
Base class of TTreeReaderValue.
void RegisterWithTreeReader()
Register with tree reader.
@ kSetupMatchBranch
This branch has been set up, branch data type and reader template type match, reading should succeed.
@ kSetupInternalError
The specified branch cannot be found.
@ kSetupMissingBranch
The array cannot find its counter branch: Array[CounterBranch].
@ kSetupMakeClassModeMismatch
The TTreeReader has been destructed / not set.
@ kSetupNotACollection
Mismatch of branch type and reader template type.
@ kSetupMissingDictionary
Some other error - hopefully the error message helps.
@ kSetupMismatch
To read this branch, we need a dictionary.
@ kSetupTreeDestructed
No initialization has happened yet.
@ kSetupMatch
The branch class type is not a collection.
@ kSetupNoCheck
This branch has been set up, branch data type and reader template type match, reading should succeed.
Read_t fProxyReadFunc
! Pointer to the Read implementation to use.
EReadStatus(TTreeReaderValueBase::* Read_t)()
void NotifyNewTree(TTree *newTree)
The TTreeReader has switched to a new TTree. Update the leaf.
Detail::TBranchProxy * GetProxy() const
void * GetAddress()
Returns the memory address of the object being read.
static std::string GetElementTypeName(const std::type_info &ti)
Stringify the template argument.
virtual const char * GetDerivedTypeName() const =0
TTreeReaderValueBase(TTreeReader *reader, const char *branchname, TDictionary *dict)
Construct a tree value reader and register it with the reader object.
Bool_t(ROOT::Detail::TBranchProxy::* BranchProxyRead_t)()
ROOT::Internal::TTreeReaderValueBase::EReadStatus ProxyReadTemplate()
Try to read the value from the TBranchProxy, returns the status of the read.
TTreeReaderValueBase & operator=(const TTreeReaderValueBase &)
Copy-assign.
TLeaf * GetLeaf()
If we are reading a leaf, return the corresponding TLeaf.
virtual ~TTreeReaderValueBase()
Unregister from tree reader, cleanup.
virtual void CreateProxy()
Create the proxy object for our branch.
virtual EReadStatus GetReadStatus() const
std::vector< Long64_t > fStaticClassOffsets
TBranch * SearchBranchWithCompositeName(TLeaf *&myleaf, TDictionary *&branchActualType, std::string &err)
Search a branch the name of which contains a ".".
static const char * GetBranchDataType(TBranch *branch, TDictionary *&dict, TDictionary const *curDict)
Retrieve the type of data stored by branch; put its dictionary into dict, return its type name.
A Branch for the case of an object.
A TTree is a list of TBranches.
Definition: TBranch.h:91
This class defines an abstract interface that must be implemented by all classes that contain diction...
Definition: TDictionary.h:162
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition: TLeaf.h:49
Basic string class.
Definition: TString.h:131
An interface for reading values stored in ROOT columnar datasets.
T * operator->()
Return a pointer to the value of the current entry.
TTreeReaderValue()=delete
T * Get()
Return a pointer to the value of the current entry.
virtual const char * GetDerivedTypeName() const
Get the template argument as a string.
TTreeReaderValue(TTreeReader &tr, const char *branchname)
T & operator*()
Return a reference to the value of the current entry.
typename std::remove_const< T >::type NonConstT_t
A simple, robust and fast interface to read values from ROOT columnar datasets such as TTree,...
Definition: TTreeReader.h:43
A TTree represents a columnar dataset.
Definition: TTree.h:72
double T(double x)
Definition: ChebyshevPol.h:34
VSD Structures.
Definition: StringConv.hxx:21