Logo ROOT   6.07/09
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 // //
18 // TTreeReaderValue //
19 // //
20 // A simple interface for reading data from trees or chains. //
21 // //
22 // //
23 ////////////////////////////////////////////////////////////////////////////
24 
25 #ifndef ROOT_TString
26 #include "TString.h"
27 #endif
28 #ifndef ROOT_TDictionary
29 #include "TDictionary.h"
30 #endif
31 #ifndef ROOT_TBranchProxy
32 #include "TBranchProxy.h"
33 #endif
34 
35 #include <type_traits>
36 
37 class TBranch;
38 class TBranchElement;
39 class TLeaf;
40 class TTreeReader;
41 
42 namespace ROOT {
43 namespace Internal {
44 
46  public:
48 
49  // Status flags, 0 is good
50  enum ESetupStatus {
53  kSetupMakeClassModeMismatch = -7, // readers disagree on whether TTree::SetMakeBranch() should be on
68  };
69  enum EReadStatus {
70  kReadSuccess = 0, // data read okay
71  kReadNothingYet, // data now yet accessed
72  kReadError // problem reading data
73  };
74 
76 
77  Bool_t IsValid() const { return fProxy && 0 == (int)fSetupStatus && 0 == (int)fReadStatus; }
79  virtual EReadStatus GetReadStatus() const { return fReadStatus; }
80 
81  TLeaf* GetLeaf();
82 
83  void* GetAddress();
84 
85  const char* GetBranchName() const { return fBranchName; }
86 
87  protected:
88  TTreeReaderValueBase(TTreeReader* reader = 0, const char* branchname = 0, TDictionary* dict = 0);
89 
90  virtual ~TTreeReaderValueBase();
91 
92  virtual void CreateProxy();
93  const char* GetBranchDataType(TBranch* branch,
94  TDictionary* &dict) const;
95 
96  virtual const char* GetDerivedTypeName() const = 0;
97 
98  Detail::TBranchProxy* GetProxy() const { return fProxy; }
99 
101 
102  TString fBranchName; // name of the branch to read data from.
104  TTreeReader* fTreeReader; // tree reader we belong to
105  TDictionary* fDict; // type that the branch should contain
106  Detail::TBranchProxy* fProxy; // proxy for this branch, owned by TTreeReader
109  ESetupStatus fSetupStatus; // setup status of this data access
110  EReadStatus fReadStatus; // read status of this data access
111  std::vector<Long64_t> fStaticClassOffsets;
112 
113  // FIXME: re-introduce once we have ClassDefInline!
114  //ClassDef(TTreeReaderValueBase, 0);//Base class for accessors to data via TTreeReader
115 
116  friend class ::TTreeReader;
117  };
118 
119 } // namespace Internal
120 } // namespace ROOT
121 
122 
123 template <typename T>
125 public:
128  TTreeReaderValue(TTreeReader& tr, const char* branchname):
129  TTreeReaderValueBase(&tr, branchname,
130  TDictionary::GetDictionary(typeid(NonConstT_t))) {}
131 
132  T* Get() {
133  if (!fProxy){
134  Error("Get()", "Value reader not properly initialized, did you remember to call TTreeReader.Set(Next)Entry()?");
135  return 0;
136  }
137  void *address = GetAddress(); // Needed to figure out if it's a pointer
138  return fProxy->IsaPointer() ? *(T**)address : (T*)address; }
139  T* operator->() { return Get(); }
140  T& operator*() { return *Get(); }
141 
142 protected:
143  // FIXME: use IsA() instead once we have ClassDefTInline
144 #define R__TTreeReaderValue_TypeString(T) #T
145  virtual const char* GetDerivedTypeName() const { return R__TTreeReaderValue_TypeString(T); }
146 #undef R__TTreeReaderValue_TypeString
147 
148  // FIXME: re-introduce once we have ClassDefTInline!
149  //ClassDefT(TTreeReaderValue, 0);//Accessor to data via TTreeReader
150 };
151 
152 #endif // ROOT_TTreeReaderValue
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition: TLeaf.h:37
long long Long64_t
Definition: RtypesCore.h:69
TTreeReader is a simple, robust and fast interface to read values from a TTree, TChain or TNtuple...
Definition: TTreeReader.h:48
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
Definition: StringConv.hxx:21
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...
double T(double x)
Definition: ChebyshevPol.h:34
virtual ~TTreeReaderValueBase()
Unregister from tree reader, cleanup.
TTreeReaderValueBase(const TTreeReaderValueBase &)=delete
Basic string class.
Definition: TString.h:137
EReadStatus ProxyRead()
Try to read the value from the TBranchProxy, returns the status of the read.
bool Bool_t
Definition: RtypesCore.h:59
TTreeReaderValue(TTreeReader &tr, const char *branchname)
std::vector< Long64_t > fStaticClassOffsets
virtual const char * GetDerivedTypeName() const =0
TLeaf * GetLeaf()
If we are reading a leaf, return the corresponding TLeaf.
virtual const char * GetDerivedTypeName() const
Extracts data from a TTree.
Detail::TBranchProxy * GetProxy() const
typename std::remove_const< Int_t >::type NonConstT_t
#define R__TTreeReaderValue_TypeString(T)
This class defines an abstract interface that must be implemented by all classes that contain diction...
Definition: TDictionary.h:162
virtual void CreateProxy()
Create the proxy object for our branch.
void * GetAddress()
Returns the memory address of the object being read.
A Branch for the case of an object.
int type
Definition: TGX11.cxx:120
Bool_t IsaPointer() const
Definition: TBranchProxy.h:135
virtual EReadStatus GetReadStatus() const
Base class for all the proxy object.
Definition: TBranchProxy.h:81
A TTree is a list of TBranches.
Definition: TBranch.h:58
void Error(ErrorHandler_t func, int code, const char *va_(fmt),...)
Write error message and call a handler, if required.