ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 class TBranch;
36 class TBranchElement;
37 class TLeaf;
38 class TTreeReader;
39 
40 namespace ROOT {
41 namespace Internal {
42 
44  public:
45 
46  // Status flags, 0 is good
47  enum ESetupStatus {
50  kSetupMakeClassModeMismatch = -7, // readers disagree on whether TTree::SetMakeBranch() should be on
65  };
66  enum EReadStatus {
67  kReadSuccess = 0, // data read okay
68  kReadNothingYet, // data now yet accessed
69  kReadError // problem reading data
70  };
71 
73 
74  Bool_t IsValid() const { return fProxy && 0 == (int)fSetupStatus && 0 == (int)fReadStatus; }
76  virtual EReadStatus GetReadStatus() const { return fReadStatus; }
77 
78  TLeaf* GetLeaf();
79 
80  void* GetAddress();
81 
82  const char* GetBranchName() const { return fBranchName; }
83 
84  protected:
85  TTreeReaderValueBase(TTreeReader* reader = 0, const char* branchname = 0, TDictionary* dict = 0);
86 
87  virtual ~TTreeReaderValueBase();
88 
89  virtual void CreateProxy();
90  const char* GetBranchDataType(TBranch* branch,
91  TDictionary* &dict) const;
92 
93  virtual const char* GetDerivedTypeName() const = 0;
94 
95  Detail::TBranchProxy* GetProxy() const { return fProxy; }
96 
98 
99  TString fBranchName; // name of the branch to read data from.
101  TTreeReader* fTreeReader; // tree reader we belong to
102  TDictionary* fDict; // type that the branch should contain
103  Detail::TBranchProxy* fProxy; // proxy for this branch, owned by TTreeReader
106  ESetupStatus fSetupStatus; // setup status of this data access
107  EReadStatus fReadStatus; // read status of this data access
108  std::vector<Long64_t> fStaticClassOffsets;
109 
110  // FIXME: re-introduce once we have ClassDefInline!
111  //ClassDef(TTreeReaderValueBase, 0);//Base class for accessors to data via TTreeReader
112 
113  friend class ::TTreeReader;
114  };
115 
116 } // namespace Internal
117 } // namespace ROOT
118 
119 
120 template <typename T>
122 public:
124  TTreeReaderValue(TTreeReader& tr, const char* branchname):
125  TTreeReaderValueBase(&tr, branchname, TDictionary::GetDictionary(typeid(T))) {}
126 
127  T* Get() {
128  if (!fProxy){
129  Error("Get()", "Value reader not properly initialized, did you remember to call TTreeReader.Set(Next)Entry()?");
130  return 0;
131  }
132  void *address = GetAddress(); // Needed to figure out if it's a pointer
133  return fProxy->IsaPointer() ? *(T**)address : (T*)address; }
134  T* operator->() { return Get(); }
135  T& operator*() { return *Get(); }
136 
137 protected:
138  // FIXME: use IsA() instead once we have ClassDefTInline
139 #define R__TTreeReaderValue_TypeString(T) #T
140  virtual const char* GetDerivedTypeName() const { return R__TTreeReaderValue_TypeString(T); }
141 #undef R__TTreeReaderValue_TypeString
142 
143  // FIXME: re-introduce once we have ClassDefTInline!
144  //ClassDefT(TTreeReaderValue, 0);//Accessor to data via TTreeReader
145 };
146 
147 #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
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...
virtual ~TTreeReaderValueBase()
Unregister from tree reader, cleanup.
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
TTree * T
TLeaf * GetLeaf()
If we are reading a leaf, return the corresponding TLeaf.
virtual const char * GetDerivedTypeName() const
void Error(const char *location, const char *msgfmt,...)
Extracts data from a TTree.
Detail::TBranchProxy * GetProxy() const
#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.
TEllipse dict(9, 20, 3, 1.5)
Bool_t IsaPointer() const
Definition: TBranchProxy.h:135
virtual EReadStatus GetReadStatus() const
virtual const char * GetDerivedTypeName() const =0
TTreeReaderValueBase(TTreeReader *reader=0, const char *branchname=0, TDictionary *dict=0)
A TTree is a list of TBranches.
Definition: TBranch.h:58