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 = -9, // 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 = 7, /// 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 /// Return true if the branch was setup \em and \em read correctly.
80 /// Use GetSetupStatus() to only check the setup status.
81 Bool_t IsValid() const { return fProxy && 0 == (int)fSetupStatus && 0 == (int)fReadStatus; }
82 /// Return this TTreeReaderValue's setup status.
83 /// Use this method to check e.g. whether the TTreeReaderValue is correctly setup and ready for reading.
85 virtual EReadStatus GetReadStatus() const { return fReadStatus; }
86
87 /// If we are reading a leaf, return the corresponding TLeaf.
88 TLeaf* GetLeaf() { return fLeaf; }
89
90 void* GetAddress();
91
92 const char* GetBranchName() const { return fBranchName; }
93
94 virtual ~TTreeReaderValueBase();
95
96 protected:
97 TTreeReaderValueBase(TTreeReader* reader, const char* branchname, TDictionary* dict);
100
102 void NotifyNewTree(TTree* newTree);
103
104 TBranch* SearchBranchWithCompositeName(TLeaf *&myleaf, TDictionary *&branchActualType, std::string &err);
105 virtual void CreateProxy();
106 static const char* GetBranchDataType(TBranch* branch,
107 TDictionary* &dict,
108 TDictionary const *curDict);
109
110 virtual const char* GetDerivedTypeName() const = 0;
111
113
115
116 /// Stringify the template argument.
117 static std::string GetElementTypeName(const std::type_info& ti);
118
119 int fHaveLeaf : 1; // Whether the data is in a leaf
120 int fHaveStaticClassOffsets : 1; // Whether !fStaticClassOffsets.empty()
121 EReadStatus fReadStatus : 2; // read status of this data access
122 ESetupStatus fSetupStatus = kSetupNotSetup; // setup status of this data access
123 TString fBranchName; // name of the branch to read data from.
125 TTreeReader* fTreeReader; // tree reader we belong to
126 TDictionary* fDict; // type that the branch should contain
127 Detail::TBranchProxy* fProxy = nullptr; // proxy for this branch, owned by TTreeReader
128 TLeaf* fLeaf = nullptr;
129 std::vector<Long64_t> fStaticClassOffsets;
131 Read_t fProxyReadFunc = &TTreeReaderValueBase::ProxyReadDefaultImpl; ///<! Pointer to the Read implementation to use.
132
133 // FIXME: re-introduce once we have ClassDefInline!
134 //ClassDef(TTreeReaderValueBase, 0);//Base class for accessors to data via TTreeReader
135
136 friend class ::TTreeReader;
137 };
138
139} // namespace Internal
140} // namespace ROOT
141
142
143template <typename T>
144class R__CLING_PTRCHECK(off) TTreeReaderValue final: public ROOT::Internal::TTreeReaderValueBase {
145// R__CLING_PTRCHECK is disabled because pointer / types are checked by CreateProxy().
146
147public:
150 TTreeReaderValue(TTreeReader& tr, const char* branchname):
151 TTreeReaderValueBase(&tr, branchname,
152 TDictionary::GetDictionary(typeid(NonConstT_t))) {}
153
154 /// Return a pointer to the value of the current entry.
155 /// Return a nullptr and print an error if no entry has been loaded yet.
156 /// The returned address is guaranteed to stay constant while a given TTree is being read from a given file,
157 /// unless the branch addresses are manipulated directly (e.g. through TTree::SetBranchAddress()).
158 /// The address might also change when the underlying TTree/TFile is switched, e.g. when a TChain switches files.
159 T* Get() {
160 if (!fProxy){
161 Error("TTreeReaderValue::Get()", "Value reader not properly initialized, did you remember to call TTreeReader.Set(Next)Entry()?");
162 return nullptr;
163 }
164 void *address = GetAddress(); // Needed to figure out if it's a pointer
165 return fProxy->IsaPointer() ? *(T**)address : (T*)address; }
166 /// Return a pointer to the value of the current entry.
167 /// Equivalent to Get().
168 T* operator->() { return Get(); }
169 /// Return a reference to the value of the current entry.
170 /// Equivalent to dereferencing the pointer returned by Get(). Behavior is undefined if no entry has been loaded yet.
171 /// Most likely a crash will occur.
172 T& operator*() { return *Get(); }
173
174protected:
175 // FIXME: use IsA() instead once we have ClassDefTInline
176 /// Get the template argument as a string.
177 virtual const char* GetDerivedTypeName() const {
178 static const std::string sElementTypeName = GetElementTypeName(typeid(T));
179 return sElementTypeName.data();
180 }
181
182 // FIXME: re-introduce once we have ClassDefTInline!
183 //ClassDefT(TTreeReaderValue, 0);//Accessor to data via TTreeReader
184};
185
186#endif // ROOT_TTreeReaderValue
bool Bool_t
Definition: RtypesCore.h:61
void Error(const char *location, const char *msgfmt,...)
int type
Definition: TGX11.cxx:120
Base class for all the proxy object.
Definition: TBranchProxy.h:68
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.
Bool_t IsValid() const
Return true if the branch was setup and read correctly.
ESetupStatus GetSetupStatus() const
Return this TTreeReaderValue's setup status.
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:166
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:78
double T(double x)
Definition: ChebyshevPol.h:34
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
Definition: StringConv.hxx:21