Logo ROOT   master
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 
30 class TBranch;
31 class TBranchElement;
32 class TLeaf;
33 class TTreeReader;
34 
35 namespace ROOT {
36 namespace Internal {
37 
38 /** \class TTreeReaderValueBase
39 Base class of TTreeReaderValue.
40 */
41 
43  public:
44 
45  // Status flags, 0 is good
46  enum ESetupStatus {
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  };
65  enum EReadStatus {
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 
101  void RegisterWithTreeReader();
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 
112  Detail::TBranchProxy* GetProxy() const { return fProxy; }
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 
143 template <typename T>
144 class R__CLING_PTRCHECK(off) TTreeReaderValue final: public ROOT::Internal::TTreeReaderValueBase {
145 // R__CLING_PTRCHECK is disabled because pointer / types are checked by CreateProxy().
146 
147 public:
149  TTreeReaderValue() = delete;
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 
174 protected:
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
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition: TLeaf.h:55
Bool_t IsValid() const
Return true if the branch was setup and read correctly.
ROOT::Internal::TTreeReaderValueBase::EReadStatus ProxyReadTemplate()
Try to read the value from the TBranchProxy, returns the status of the read.
virtual const char * GetDerivedTypeName() const
Get the template argument as a string.
To read this branch, we need a dictionary.
A simple, robust and fast interface to read values from ROOT columnar datasets such as TTree...
Definition: TTreeReader.h:43
Returns the available number of logical cores.
Definition: RNumpyDS.hxx:30
The branch class type is not a collection.
EReadStatus(TTreeReaderValueBase::* Read_t)()
double T(double x)
Definition: ChebyshevPol.h:34
virtual ~TTreeReaderValueBase()
Unregister from tree reader, cleanup.
Basic string class.
Definition: TString.h:131
bool Bool_t
Definition: RtypesCore.h:61
TTreeReaderValue(TTreeReader &tr, const char *branchname)
std::vector< Long64_t > fStaticClassOffsets
This branch has been set up, branch data type and reader template type match, reading should succeed...
The array cannot find its counter branch: Array[CounterBranch].
Mismatch of branch type and reader template type.
T * Get()
Return a pointer to the value of the current entry.
Detail::TBranchProxy * GetProxy() const
virtual EReadStatus GetReadStatus() const
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...
TTreeReaderValueBase & operator=(const TTreeReaderValueBase &)
Copy-assign.
void Error(const char *location, const char *msgfmt,...)
An interface for reading values stored in ROOT columnar datasets.
typename std::remove_const< Float_t >::type NonConstT_t
This branch has been set up, branch data type and reader template type match, reading should succeed...
void RegisterWithTreeReader()
Register with tree reader.
TTreeReaderValueBase(TTreeReader *reader, const char *branchname, TDictionary *dict)
Construct a tree value reader and register it with the reader object.
T & operator*()
Return a reference to the value of the current entry.
This class defines an abstract interface that must be implemented by all classes that contain diction...
Definition: TDictionary.h:162
Read_t fProxyReadFunc
! Pointer to the Read implementation to use.
Bool_t(ROOT::Detail::TBranchProxy::* BranchProxyRead_t)()
virtual void CreateProxy()
Create the proxy object for our branch.
Some other error - hopefully the error message helps.
Base class of TTreeReaderValue.
void * GetAddress()
Returns the memory address of the object being read.
A Branch for the case of an object.
static std::string GetElementTypeName(const std::type_info &ti)
Stringify the template argument.
int type
Definition: TGX11.cxx:120
TBranch * SearchBranchWithCompositeName(TLeaf *&myleaf, TDictionary *&branchActualType, std::string &err)
Search a branch the name of which contains a ".".
TLeaf * GetLeaf()
If we are reading a leaf, return the corresponding TLeaf.
The TTreeReader has been destructed / not set.
virtual const char * GetDerivedTypeName() const =0
void NotifyNewTree(TTree *newTree)
The TTreeReader has switched to a new TTree. Update the leaf.
A TTree represents a columnar dataset.
Definition: TTree.h:78
Base class for all the proxy object.
Definition: TBranchProxy.h:68
A TTree is a list of TBranches.
Definition: TBranch.h:88
ESetupStatus GetSetupStatus() const
Return this TTreeReaderValue&#39;s setup status.
T * operator->()
Return a pointer to the value of the current entry.