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