Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Loading...
Searching...
No Matches
TTreeReaderValue.h
Go to the documentation of this file.
1// @(#)root/tree:$Id$
2// Author: Axel Naumann, 2010-08-02
3// Author: Vincenzo Eduardo Padulano CERN 09/2024
4
5/*************************************************************************
6 * Copyright (C) 1995-2024, Rene Brun and Fons Rademakers. *
7 * All rights reserved. *
8 * *
9 * For the licensing terms see $ROOTSYS/LICENSE. *
10 * For the list of contributors see $ROOTSYS/README/CREDITS. *
11 *************************************************************************/
12
13#ifndef ROOT_TTreeReaderValue
14#define ROOT_TTreeReaderValue
15
16////////////////////////////////////////////////////////////////////////////
17// //
18// TTreeReaderValue //
19// //
20// A simple interface for reading data from trees or chains. //
21// //
22// //
23////////////////////////////////////////////////////////////////////////////
24
25#include "TString.h"
26#include "TDictionary.h"
27#include "TBranchProxy.h"
28
29#include <type_traits>
30#include <vector>
31#include <string>
32
33class TBranch;
34class TBranchElement;
35class TLeaf;
36class TTreeReader;
37
38namespace ROOT {
39namespace Internal {
40
41/** \class TTreeReaderValueBase
42Base class of TTreeReaderValue.
43*/
44
46public:
47 /// Status flags, 0 is good
49 kSetupNotSetup = -7, ///< No initialization has happened yet.
50 kSetupTreeDestructed = -8, ///< The TTreeReader has been destructed / not set.
51 kSetupMakeClassModeMismatch = -9, ///< readers disagree on whether TTree::SetMakeBranch() should be on
52 kSetupMissingCounterBranch = -6, ///< The array cannot find its counter branch: Array[CounterBranch]
53 kSetupMissingBranch = -5, ///< The specified branch cannot be found.
54 kSetupInternalError = -4, ///< Some other error - hopefully the error message helps.
55 kSetupMissingDictionary = -3, ///< To read this branch, we need a dictionary.
56 kSetupMismatch = -2, ///< Mismatch of branch type and reader template type.
57 kSetupNotACollection = -1, ///< The branch class type is not a collection.
59 0, ///< This branch has been set up, branch data type and reader template type match, reading should succeed.
61 7, ///< This branch has been set up, branch data type and reader template type match, reading should succeed.
62 // kSetupMatchConversion = 1, /// This branch has been set up, the branch data type can be converted to the reader
63 // template type, reading should succeed. kSetupMatchConversionCollection = 2, /// This branch has been set up,
64 // the data type of the branch's collection elements can be converted to the reader template type, reading should
65 // succeed. kSetupMakeClass = 3, /// This branch has been set up, enabling MakeClass mode for it, reading should
66 // succeed.
67 // kSetupVoidPtr = 4,
69 kSetupMatchLeaf = 6 ///< This branch (or TLeaf, really) has been set up, reading should succeed.
70 };
72 kReadSuccess = 0, ///< Data read okay
73 kReadNothingYet, ///< Data now yet accessed
74 kReadError ///< Problem reading data
75 };
76
77 EReadStatus ProxyRead() { return (this->*fProxyReadFunc)(); }
78
79 EReadStatus ProxyReadDefaultImpl();
80
82 template <BranchProxyRead_t Func>
84
85 /// Return true if the branch was setup \em and \em read correctly.
86 /// Use GetSetupStatus() to only check the setup status.
87 bool IsValid() const { return fProxy && 0 == (int)fSetupStatus && 0 == (int)fReadStatus; }
88 /// Return this TTreeReaderValue's setup status.
89 /// Use this method to check e.g. whether the TTreeReaderValue is correctly setup and ready for reading.
91 virtual EReadStatus GetReadStatus() const { return fReadStatus; }
92
93 /// If we are reading a leaf, return the corresponding TLeaf.
94 TLeaf *GetLeaf() { return fLeaf; }
95
96 void *GetAddress();
97
98 const char *GetBranchName() const { return fBranchName; }
99
100 virtual ~TTreeReaderValueBase();
101
102protected:
103 TTreeReaderValueBase(TTreeReader *reader, const char *branchname, TDictionary *dict, bool opaqueRead = false);
106
109
111 virtual void CreateProxy();
112 static const char *GetBranchDataType(TBranch *branch, TDictionary *&dict, TDictionary const *curDict);
113
114 virtual const char *GetDerivedTypeName() const = 0;
115
117
123
124 /// Stringify the template argument.
125 static std::string GetElementTypeName(const std::type_info &ti);
126
128
129 bool fHaveLeaf : 1; ///< Whether the data is in a leaf
130 bool fHaveStaticClassOffsets : 1; ///< Whether !fStaticClassOffsets.empty()
131 EReadStatus fReadStatus : 2; ///< Read status of this data access
132 ESetupStatus fSetupStatus = kSetupNotSetup; ///< Setup status of this data access
133 TString fBranchName; ///< Name of the branch to read data from.
135 TTreeReader *fTreeReader; ///< Tree reader we belong to
136 TDictionary *fDict; ///< Type that the branch should contain
137 Detail::TBranchProxy *fProxy = nullptr; ///< Proxy for this branch, owned by TTreeReader
138 TLeaf *fLeaf = nullptr;
139 std::vector<Long64_t> fStaticClassOffsets;
141 Read_t fProxyReadFunc = &TTreeReaderValueBase::ProxyReadDefaultImpl; ///<! Pointer to the Read implementation to use.
142 /**
143 * If true, the reader will not do any type-checking against the actual
144 * type held by the branch. Useful to just check if the current entry can
145 * be read or not without caring about its value.
146 * \note Only used by TTreeReaderOpaqueValue.
147 */
148 bool fOpaqueRead{false};
149
150 // FIXME: re-introduce once we have ClassDefInline!
151 // ClassDefOverride(TTreeReaderValueBase, 0);//Base class for accessors to data via TTreeReader
152
153 friend class ::TTreeReader;
154};
155
156/**
157 * \brief Read a value in a branch without knowledge of its type
158 *
159 * This class is helpful in situations where the actual contents of the branch
160 * at the current entry are not relevant and one only wants to know whether
161 * the entry can be read.
162 */
164public:
166 : TTreeReaderValueBase(&tr, branchname, /*dict*/ nullptr, /*opaqueRead*/ true)
167 {
168 }
169
170protected:
171 const char *GetDerivedTypeName() const { return ""; }
172};
173
175 std::string fElementTypeName;
176
177public:
178 TTreeReaderUntypedValue(TTreeReader &tr, std::string_view branchName, std::string_view typeName)
179 : TTreeReaderValueBase(&tr, branchName.data(), TDictionary::GetDictionary(typeName.data())),
180 fElementTypeName(typeName)
181 {
182 }
183
184 void *Get()
185 {
186 if (!fProxy) {
187 ErrorAboutMissingProxyIfNeeded();
188 return nullptr;
189 }
190 void *address = GetAddress(); // Needed to figure out if it's a pointer
191 return fProxy->IsaPointer() ? *(void **)address : (void *)address;
192 }
193
194protected:
195 const char *GetDerivedTypeName() const final { return fElementTypeName.c_str(); }
196};
197
198} // namespace Internal
199} // namespace ROOT
200
201template <typename T>
203 // R__CLING_PTRCHECK is disabled because pointer / types are checked by CreateProxy().
204
205public:
206 using NonConstT_t = typename std::remove_const<T>::type;
209 : TTreeReaderValueBase(&tr, branchname, TDictionary::GetDictionary(typeid(NonConstT_t)))
210 {
211 }
212
213 /// Return a pointer to the value of the current entry.
214 /// Return a nullptr and print an error if no entry has been loaded yet.
215 /// The returned address is guaranteed to stay constant while a given TTree is being read from a given file,
216 /// unless the branch addresses are manipulated directly (e.g. through TTree::SetBranchAddress()).
217 /// The address might also change when the underlying TTree/TFile is switched, e.g. when a TChain switches files.
218 T *Get()
219 {
220 if (!fProxy) {
221 ErrorAboutMissingProxyIfNeeded();
222 return nullptr;
223 }
224 void *address = GetAddress(); // Needed to figure out if it's a pointer
225 return fProxy->IsaPointer() ? *(T **)address : (T *)address;
226 }
227
228 /// Return a pointer to the value of the current entry.
229 /// Equivalent to Get().
230 T *operator->() { return Get(); }
231
232 /// Return a reference to the value of the current entry.
233 /// Equivalent to dereferencing the pointer returned by Get(). Behavior is undefined if no entry has been loaded yet.
234 /// Most likely a crash will occur.
235 T &operator*() { return *Get(); }
236
237protected:
238 // FIXME: use IsA() instead once we have ClassDefTInline
239 /// Get the template argument as a string.
240 const char *GetDerivedTypeName() const override
241 {
242 static const std::string sElementTypeName = GetElementTypeName(typeid(T));
243 return sElementTypeName.data();
244 }
245
246 // FIXME: re-introduce once we have ClassDefTInline!
247 // ClassDefT(TTreeReaderValue, 0);//Accessor to data via TTreeReader
248};
249
250namespace cling {
251std::string printValue(ROOT::Internal::TTreeReaderValueBase *val);
252template <typename T>
253std::string printValue(TTreeReaderValue<T> *val)
254{
255 return printValue(static_cast<ROOT::Internal::TTreeReaderValueBase *>(val));
256}
257} // namespace cling
258
259#endif // ROOT_TTreeReaderValue
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Base class for all the proxy object.
Read a value in a branch without knowledge of its type.
TTreeReaderOpaqueValue(TTreeReader &tr, const char *branchname)
const char * GetDerivedTypeName() const final
TTreeReaderUntypedValue(TTreeReader &tr, std::string_view branchName, std::string_view typeName)
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
Some other error - hopefully the error message helps.
@ kSetupMissingBranch
The specified branch cannot be found.
@ kSetupNotSetup
No initialization has happened yet.
@ kSetupMakeClassModeMismatch
readers disagree on whether TTree::SetMakeBranch() should be on
@ kSetupNotACollection
The branch class type is not a collection.
@ kSetupMissingDictionary
To read this branch, we need a dictionary.
@ kSetupMissingCounterBranch
The array cannot find its counter branch: Array[CounterBranch].
@ kSetupMismatch
Mismatch of branch type and reader template type.
@ kSetupTreeDestructed
The TTreeReader has been destructed / not set.
@ kSetupMatchLeaf
This branch (or TLeaf, really) has been set up, reading should succeed.
@ kSetupMatch
This branch has been set up, branch data type and reader template type match, reading should succeed.
ESetupStatus GetSetupStatus() const
Return this TTreeReaderValue's setup status.
Read_t fProxyReadFunc
! Pointer to the Read implementation to use.
bool fOpaqueRead
If true, the reader will not do any type-checking against the actual type held by the branch.
EReadStatus(TTreeReaderValueBase::* Read_t)()
void NotifyNewTree(TTree *newTree)
The TTreeReader has switched to a new TTree. Update the leaf.
bool fHaveLeaf
Whether the data is in a leaf.
Detail::TBranchProxy * GetProxy() const
bool fHaveStaticClassOffsets
Whether !fStaticClassOffsets.empty()
void * GetAddress()
Returns the memory address of the object being read.
@ kReadNothingYet
Data now yet accessed.
static std::string GetElementTypeName(const std::type_info &ti)
Stringify the template argument.
ESetupStatus fSetupStatus
Setup status of this data access.
bool IsValid() const
Return true if the branch was setup and read correctly.
virtual const char * GetDerivedTypeName() const =0
TString fBranchName
Name of the branch to read data from.
ROOT::Internal::TTreeReaderValueBase::EReadStatus ProxyReadTemplate()
Try to read the value from the TBranchProxy, returns the status of the read.
TTreeReader * fTreeReader
Tree reader we belong to.
TDictionary * fDict
Type that the branch should contain.
TTreeReaderValueBase & operator=(const TTreeReaderValueBase &)
Copy-assign.
EReadStatus fReadStatus
Read status of this data access.
TLeaf * GetLeaf()
If we are reading a leaf, return the corresponding TLeaf.
virtual ~TTreeReaderValueBase()
Unregister from tree reader, cleanup.
TTreeReaderValueBase(TTreeReader *reader, const char *branchname, TDictionary *dict, bool opaqueRead=false)
Construct a tree value reader and register it with the reader object.
bool(ROOT::Detail::TBranchProxy::* BranchProxyRead_t)()
virtual void CreateProxy()
Create the proxy object for our branch.
virtual EReadStatus GetReadStatus() const
Detail::TBranchProxy * fProxy
Proxy for this branch, owned by TTreeReader.
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:93
This class defines an abstract interface that must be implemented by all classes that contain diction...
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition TLeaf.h:57
Basic string class.
Definition TString.h:139
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.
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
const char * GetDerivedTypeName() const override
Get the template argument as a string.
A simple, robust and fast interface to read values from ROOT columnar datasets such as TTree,...
Definition TTreeReader.h:46
A TTree represents a columnar dataset.
Definition TTree.h:79
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...