Logo ROOT   6.10/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 #include "TString.h"
26 #include "TDictionary.h"
27 #include "TBranchProxy.h"
28 
29 #include <type_traits>
30 
31 class TBranch;
32 class TBranchElement;
33 class TLeaf;
34 class TTreeReader;
35 
36 namespace ROOT {
37 namespace Internal {
38 
40  public:
41 
42  // Status flags, 0 is good
43  enum ESetupStatus {
44  kSetupNotSetup = -7, /// No initialization has happened yet.
45  kSetupTreeDestructed = -8, /// The TTreeReader has been destructed / not set.
46  kSetupMakeClassModeMismatch = -7, // readers disagree on whether TTree::SetMakeBranch() should be on
47  kSetupMissingCounterBranch = -6, /// The array cannot find its counter branch: Array[CounterBranch]
48  kSetupMissingBranch = -5, /// The specified branch cannot be found.
49  kSetupInternalError = -4, /// Some other error - hopefully the error message helps.
50  kSetupMissingDictionary = -3, /// To read this branch, we need a dictionary.
51  kSetupMismatch = -2, /// Mismatch of branch type and reader template type.
52  kSetupNotACollection = -1, /// The branch class type is not a collection.
53  kSetupMatch = 0, /// This branch has been set up, branch data type and reader template type match, reading should succeed.
54  kSetupMatchBranch = 0, /// This branch has been set up, branch data type and reader template type match, reading should succeed.
55  //kSetupMatchConversion = 1, /// This branch has been set up, the branch data type can be converted to the reader template type, reading should succeed.
56  //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.
57  //kSetupMakeClass = 3, /// This branch has been set up, enabling MakeClass mode for it, reading should succeed.
58  // kSetupVoidPtr = 4,
60  kSetupMatchLeaf = 6 /// This branch (or TLeaf, really) has been set up, reading should succeed.
61  };
62  enum EReadStatus {
63  kReadSuccess = 0, // data read okay
64  kReadNothingYet, // data now yet accessed
65  kReadError // problem reading data
66  };
67 
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 = 0, const char* branchname = 0, TDictionary* dict = 0);
87 
89  void NotifyNewTree(TTree* newTree);
90 
91  virtual void CreateProxy();
92  const char* GetBranchDataType(TBranch* branch,
93  TDictionary* &dict) const;
94 
95  virtual const char* GetDerivedTypeName() const = 0;
96 
97  Detail::TBranchProxy* GetProxy() const { return fProxy; }
98 
100 
101  /// Stringify the template argument.
102  static std::string GetElementTypeName(const std::type_info& ti);
103 
104  TString fBranchName; // name of the branch to read data from.
106  TTreeReader* fTreeReader; // tree reader we belong to
107  TDictionary* fDict; // type that the branch should contain
108  Detail::TBranchProxy* fProxy; // proxy for this branch, owned by TTreeReader
110  ESetupStatus fSetupStatus; // setup status of this data access
111  EReadStatus fReadStatus; // read status of this data access
112  std::vector<Long64_t> fStaticClassOffsets;
113 
114  // FIXME: re-introduce once we have ClassDefInline!
115  //ClassDef(TTreeReaderValueBase, 0);//Base class for accessors to data via TTreeReader
116 
117  friend class ::TTreeReader;
118  };
119 
120 } // namespace Internal
121 } // namespace ROOT
122 
123 
124 template <typename T>
126 public:
129  TTreeReaderValue(TTreeReader& tr, const char* branchname):
130  TTreeReaderValueBase(&tr, branchname,
131  TDictionary::GetDictionary(typeid(NonConstT_t))) {}
132 
133  T* Get() {
134  if (!fProxy){
135  Error("Get()", "Value reader not properly initialized, did you remember to call TTreeReader.Set(Next)Entry()?");
136  return 0;
137  }
138  void *address = GetAddress(); // Needed to figure out if it's a pointer
139  return fProxy->IsaPointer() ? *(T**)address : (T*)address; }
140  T* operator->() { return Get(); }
141  T& operator*() { return *Get(); }
142 
143 protected:
144  // FIXME: use IsA() instead once we have ClassDefTInline
145  /// Get the template argument as a string.
146  virtual const char* GetDerivedTypeName() const {
147  static const std::string sElementTypeName = GetElementTypeName(typeid(T));
148  return sElementTypeName.data();
149  }
150 
151  // FIXME: re-introduce once we have ClassDefTInline!
152  //ClassDefT(TTreeReaderValue, 0);//Accessor to data via TTreeReader
153 };
154 
155 #endif // ROOT_TTreeReaderValue
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition: TLeaf.h:32
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:43
Namespace for new ROOT classes and functions.
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:129
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:158
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:78
Base class for all the proxy object.
Definition: TBranchProxy.h:65
A TTree is a list of TBranches.
Definition: TBranch.h:57
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.