Logo ROOT   6.08/07
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:
47 
48  // Status flags, 0 is good
49  enum ESetupStatus {
50  kSetupNotSetup = -7, /// No initialization has happened yet.
51  kSetupTreeDestructed = -8, /// The TTreeReader has been destructed / not set.
52  kSetupMakeClassModeMismatch = -7, // 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 = 0, /// 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  };
68  enum EReadStatus {
69  kReadSuccess = 0, // data read okay
70  kReadNothingYet, // data now yet accessed
71  kReadError // problem reading data
72  };
73 
75 
76  Bool_t IsValid() const { return fProxy && 0 == (int)fSetupStatus && 0 == (int)fReadStatus; }
78  virtual EReadStatus GetReadStatus() const { return fReadStatus; }
79 
80  /// If we are reading a leaf, return the corresponding TLeaf.
81  TLeaf* GetLeaf() { return fLeaf; }
82 
83  void* GetAddress();
84 
85  const char* GetBranchName() const { return fBranchName; }
86 
87  virtual ~TTreeReaderValueBase();
88 
89  protected:
90  TTreeReaderValueBase(TTreeReader* reader = 0, const char* branchname = 0, TDictionary* dict = 0);
93 
95  void NotifyNewTree(TTree* newTree);
96 
97  virtual void CreateProxy();
98  const char* GetBranchDataType(TBranch* branch,
99  TDictionary* &dict) const;
100 
101  virtual const char* GetDerivedTypeName() const = 0;
102 
103  Detail::TBranchProxy* GetProxy() const { return fProxy; }
104 
106 
107  /// Stringify the template argument.
108  static std::string GetElementTypeName(const std::type_info& ti);
109 
110  TString fBranchName; // name of the branch to read data from.
112  TTreeReader* fTreeReader; // tree reader we belong to
113  TDictionary* fDict; // type that the branch should contain
114  Detail::TBranchProxy* fProxy; // proxy for this branch, owned by TTreeReader
116  ESetupStatus fSetupStatus; // setup status of this data access
117  EReadStatus fReadStatus; // read status of this data access
118  std::vector<Long64_t> fStaticClassOffsets;
119 
120  // FIXME: re-introduce once we have ClassDefInline!
121  //ClassDef(TTreeReaderValueBase, 0);//Base class for accessors to data via TTreeReader
122 
123  friend class ::TTreeReader;
124  };
125 
126 } // namespace Internal
127 } // namespace ROOT
128 
129 
130 template <typename T>
132 public:
135  TTreeReaderValue(TTreeReader& tr, const char* branchname):
136  TTreeReaderValueBase(&tr, branchname,
137  TDictionary::GetDictionary(typeid(NonConstT_t))) {}
138 
139  T* Get() {
140  if (!fProxy){
141  Error("Get()", "Value reader not properly initialized, did you remember to call TTreeReader.Set(Next)Entry()?");
142  return 0;
143  }
144  void *address = GetAddress(); // Needed to figure out if it's a pointer
145  return fProxy->IsaPointer() ? *(T**)address : (T*)address; }
146  T* operator->() { return Get(); }
147  T& operator*() { return *Get(); }
148 
149 protected:
150  // FIXME: use IsA() instead once we have ClassDefTInline
151  /// Get the template argument as a string.
152  virtual const char* GetDerivedTypeName() const {
153  static const std::string sElementTypeName = GetElementTypeName(typeid(T));
154  return sElementTypeName.data();
155  }
156 
157  // FIXME: re-introduce once we have ClassDefTInline!
158  //ClassDefT(TTreeReaderValue, 0);//Accessor to data via TTreeReader
159 };
160 
161 #endif // ROOT_TTreeReaderValue
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition: TLeaf.h:37
virtual const char * GetDerivedTypeName() const
Get the template argument as a string.
To read this branch, we need a dictionary.
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
The branch class type is not a collection.
double T(double x)
Definition: ChebyshevPol.h:34
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
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.
Detail::TBranchProxy * GetProxy() const
virtual EReadStatus GetReadStatus() const
TTreeReaderValueBase & operator=(const TTreeReaderValueBase &)
Copy-assign.
Extracts data from a TTree.
typename std::remove_const< Int_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.
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.
Some other error - hopefully the error message helps.
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
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
TTreeReaderValueBase(TTreeReader *reader=0, const char *branchname=0, TDictionary *dict=0)
Construct a tree value reader and register it with the reader object.
void NotifyNewTree(TTree *newTree)
The TTreeReader has switched to a new TTree. Update the leaf.
A TTree object has a header with a name and a title.
Definition: TTree.h:98
Base class for all the proxy object.
Definition: TBranchProxy.h:81
A TTree is a list of TBranches.
Definition: TBranch.h:58
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...
void Error(ErrorHandler_t func, int code, const char *va_(fmt),...)
Write error message and call a handler, if required.