Logo ROOT  
Reference Guide
 
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
46 public:
47
48 /// Status flags, 0 is good
50 kSetupNotSetup = -7, ///< No initialization has happened yet.
51 kSetupTreeDestructed = -8, ///< The TTreeReader has been destructed / not set.
52 kSetupMakeClassModeMismatch = -9, ///< readers disagree on whether TTree::SetMakeBranch() should be on
53 kSetupMissingCounterBranch = -6, ///< The array cannot find its counter branch: Array[CounterBranch]
54 kSetupMissingBranch = -5, ///< The specified branch cannot be found.
55 kSetupInternalError = -4, ///< Some other error - hopefully the error message helps.
56 kSetupMissingDictionary = -3, ///< To read this branch, we need a dictionary.
57 kSetupMismatch = -2, ///< Mismatch of branch type and reader template type.
58 kSetupNotACollection = -1, ///< The branch class type is not a collection.
59 kSetupMatch = 0, ///< This branch has been set up, branch data type and reader template type match, reading should succeed.
60 kSetupMatchBranch = 7, ///< This branch has been set up, branch data type and reader template type match, reading should succeed.
61 //kSetupMatchConversion = 1, /// This branch has been set up, the branch data type can be converted to the reader template type, reading should succeed.
62 //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.
63 //kSetupMakeClass = 3, /// This branch has been set up, enabling MakeClass mode for it, reading should succeed.
64 // kSetupVoidPtr = 4,
66 kSetupMatchLeaf = 6 ///< This branch (or TLeaf, really) has been set up, reading should succeed.
67 };
69 kReadSuccess = 0, ///< Data read okay
70 kReadNothingYet, ///< Data now yet accessed
71 kReadError ///< Problem reading data
72 };
73
74 EReadStatus ProxyRead() { return (this->*fProxyReadFunc)(); }
75
77
79 template <BranchProxyRead_t Func>
81
82 /// Return true if the branch was setup \em and \em read correctly.
83 /// Use GetSetupStatus() to only check the setup status.
84 bool IsValid() const { return fProxy && 0 == (int)fSetupStatus && 0 == (int)fReadStatus; }
85 /// Return this TTreeReaderValue's setup status.
86 /// Use this method to check e.g. whether the TTreeReaderValue is correctly setup and ready for reading.
88 virtual EReadStatus GetReadStatus() const { return fReadStatus; }
89
90 /// If we are reading a leaf, return the corresponding TLeaf.
91 TLeaf* GetLeaf() { return fLeaf; }
92
93 void* GetAddress();
94
95 const char* GetBranchName() const { return fBranchName; }
96
97 virtual ~TTreeReaderValueBase();
98
99 protected:
100 TTreeReaderValueBase(TTreeReader *reader, const char *branchname, TDictionary *dict, bool opaqueRead = false);
103
105 void NotifyNewTree(TTree* newTree);
106
107 TBranch* SearchBranchWithCompositeName(TLeaf *&myleaf, TDictionary *&branchActualType, std::string &err);
108 virtual void CreateProxy();
109 static const char* GetBranchDataType(TBranch* branch,
110 TDictionary* &dict,
111 TDictionary const *curDict);
112
113 virtual const char* GetDerivedTypeName() const = 0;
114
116
118
119 /// Stringify the template argument.
120 static std::string GetElementTypeName(const std::type_info& ti);
121
123
124 bool fHaveLeaf : 1; ///< Whether the data is in a leaf
125 bool fHaveStaticClassOffsets : 1; ///< Whether !fStaticClassOffsets.empty()
126 EReadStatus fReadStatus : 2; ///< Read status of this data access
127 ESetupStatus fSetupStatus = kSetupNotSetup; ///< Setup status of this data access
128 TString fBranchName; ///< Name of the branch to read data from.
130 TTreeReader* fTreeReader; ///< Tree reader we belong to
131 TDictionary* fDict; ///< Type that the branch should contain
132 Detail::TBranchProxy* fProxy = nullptr; ///< Proxy for this branch, owned by TTreeReader
133 TLeaf* fLeaf = nullptr;
134 std::vector<Long64_t> fStaticClassOffsets;
136 Read_t fProxyReadFunc = &TTreeReaderValueBase::ProxyReadDefaultImpl; ///<! Pointer to the Read implementation to use.
137 /**
138 * If true, the reader will not do any type-checking against the actual
139 * type held by the branch. Useful to just check if the current entry can
140 * be read or not without caring about its value.
141 * \note Only used by TTreeReaderOpaqueValue.
142 */
143 bool fOpaqueRead{false};
144
145 // FIXME: re-introduce once we have ClassDefInline!
146 //ClassDefOverride(TTreeReaderValueBase, 0);//Base class for accessors to data via TTreeReader
147
148 friend class ::TTreeReader;
149 };
150
151 /**
152 * \brief Read a value in a branch without knowledge of its type
153 *
154 * This class is helpful in situations where the actual contents of the branch
155 * at the current entry are not relevant and one only wants to know whether
156 * the entry can be read.
157 */
158 class R__CLING_PTRCHECK(off) TTreeReaderOpaqueValue final : public ROOT::Internal::TTreeReaderValueBase {
159 public:
160 TTreeReaderOpaqueValue(TTreeReader &tr, const char *branchname)
161 : TTreeReaderValueBase(&tr, branchname, /*dict*/ nullptr, /*opaqueRead*/ true)
162 {
163 }
164
165 protected:
166 const char *GetDerivedTypeName() const { return ""; }
167 };
168
169} // namespace Internal
170} // namespace ROOT
171
172
173template <typename T>
174class R__CLING_PTRCHECK(off) TTreeReaderValue final: public ROOT::Internal::TTreeReaderValueBase {
175// R__CLING_PTRCHECK is disabled because pointer / types are checked by CreateProxy().
176
177public:
178 using NonConstT_t = typename std::remove_const<T>::type;
180 TTreeReaderValue(TTreeReader& tr, const char* branchname):
181 TTreeReaderValueBase(&tr, branchname,
182 TDictionary::GetDictionary(typeid(NonConstT_t))) {}
183
184 /// Return a pointer to the value of the current entry.
185 /// Return a nullptr and print an error if no entry has been loaded yet.
186 /// The returned address is guaranteed to stay constant while a given TTree is being read from a given file,
187 /// unless the branch addresses are manipulated directly (e.g. through TTree::SetBranchAddress()).
188 /// The address might also change when the underlying TTree/TFile is switched, e.g. when a TChain switches files.
189 T *Get()
190 {
191 if (!fProxy) {
192 ErrorAboutMissingProxyIfNeeded();
193 return nullptr;
194 }
195 void *address = GetAddress(); // Needed to figure out if it's a pointer
196 return fProxy->IsaPointer() ? *(T **)address : (T *)address;
197 }
198
199 /// Return a pointer to the value of the current entry.
200 /// Equivalent to Get().
201 T* operator->() { return Get(); }
202
203 /// Return a reference to the value of the current entry.
204 /// Equivalent to dereferencing the pointer returned by Get(). Behavior is undefined if no entry has been loaded yet.
205 /// Most likely a crash will occur.
206 T& operator*() { return *Get(); }
207
208protected:
209 // FIXME: use IsA() instead once we have ClassDefTInline
210 /// Get the template argument as a string.
211 const char* GetDerivedTypeName() const override {
212 static const std::string sElementTypeName = GetElementTypeName(typeid(T));
213 return sElementTypeName.data();
214 }
215
216 // FIXME: re-introduce once we have ClassDefTInline!
217 //ClassDefT(TTreeReaderValue, 0);//Accessor to data via TTreeReader
218};
219
220namespace cling {
221std::string printValue(ROOT::Internal::TTreeReaderValueBase *val);
222template <typename T>
223std::string printValue(TTreeReaderValue<T> *val)
224{
225 return printValue(static_cast<ROOT::Internal::TTreeReaderValueBase *>(val));
226}
227} // namespace cling
228
229#endif // ROOT_TTreeReaderValue
Base class for all the proxy object.
Read a value in a branch without knowledge of its type.
TTreeReaderOpaqueValue(TTreeReader &tr, const char *branchname)
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.
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...