Logo ROOT   6.12/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 #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, const char* branchname, TDictionary* dict);
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  int fHaveLeaf : 1; // Whether the data is in a leaf
105  int fHaveStaticClassOffsets : 1; // Whether !fStaticClassOffsets.empty()
106  EReadStatus fReadStatus : 2; // read status of this data access
107  ESetupStatus fSetupStatus = kSetupNotSetup; // setup status of this data access
108  TString fBranchName; // name of the branch to read data from.
110  TTreeReader* fTreeReader; // tree reader we belong to
111  TDictionary* fDict; // type that the branch should contain
112  Detail::TBranchProxy* fProxy = nullptr; // proxy for this branch, owned by TTreeReader
113  TLeaf* fLeaf = nullptr;
114  std::vector<Long64_t> fStaticClassOffsets;
115 
116  // FIXME: re-introduce once we have ClassDefInline!
117  //ClassDef(TTreeReaderValueBase, 0);//Base class for accessors to data via TTreeReader
118 
119  friend class ::TTreeReader;
120  };
121 
122 } // namespace Internal
123 } // namespace ROOT
124 
125 
126 template <typename T>
128 public:
130  TTreeReaderValue() = delete;
131  TTreeReaderValue(TTreeReader& tr, const char* branchname):
132  TTreeReaderValueBase(&tr, branchname,
133  TDictionary::GetDictionary(typeid(NonConstT_t))) {}
134 
135  T* Get() {
136  if (!fProxy){
137  Error("Get()", "Value reader not properly initialized, did you remember to call TTreeReader.Set(Next)Entry()?");
138  return 0;
139  }
140  void *address = GetAddress(); // Needed to figure out if it's a pointer
141  return fProxy->IsaPointer() ? *(T**)address : (T*)address; }
142  T* operator->() { return Get(); }
143  T& operator*() { return *Get(); }
144 
145 protected:
146  // FIXME: use IsA() instead once we have ClassDefTInline
147  /// Get the template argument as a string.
148  virtual const char* GetDerivedTypeName() const {
149  static const std::string sElementTypeName = GetElementTypeName(typeid(T));
150  return sElementTypeName.data();
151  }
152 
153  // FIXME: re-introduce once we have ClassDefTInline!
154  //ClassDefT(TTreeReaderValue, 0);//Accessor to data via TTreeReader
155 };
156 
157 #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:125
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.
TTreeReaderValueBase(TTreeReader *reader, const char *branchname, TDictionary *dict)
Construct a tree value reader and register it with the reader object.
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
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:70
Base class for all the proxy object.
Definition: TBranchProxy.h:66
A TTree is a list of TBranches.
Definition: TBranch.h:59
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.