Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TTreeReaderValueFast.hxx
Go to the documentation of this file.
1// @(#)root/tree:$Id$
2// Author: Brian Bockelman, 2017-06-13
3
4/*************************************************************************
5 * Copyright (C) 1995-2017, 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_TTreeReaderValueFast
13#define ROOT_TTreeReaderValueFast
14
15
16////////////////////////////////////////////////////////////////////////////
17// //
18// TTreeReaderValueFast //
19// //
20// A simple interface for reading data from trees or chains. //
21// //
22// //
23////////////////////////////////////////////////////////////////////////////
24
26
27#include "TBranch.h"
28#include "TBufferFile.h"
29
30#include <type_traits>
31
32class TBranch;
33
34namespace ROOT {
35namespace Experimental {
36namespace Internal {
37
38/* All the common code shared by the fast reader templates.
39 */
41 public:
43
46
47 //////////////////////////////////////////////////////////////////////////////
48 /// Construct a tree value reader and register it with the reader object.
49 TTreeReaderValueFastBase(TTreeReaderFast* reader, const std::string &branchName) :
50 fBranchName(branchName),
51 fLeafName(branchName), // TODO: only support single-leaf branches for now.
53 fBuffer(TBuffer::kWrite, 32*1024),
54 fEvtIndex(reader->GetIndexRef())
55 {
57 }
58
60 //printf("Getting events starting at %lld. Current remaining is %d events with base %lld.\n", eventNum, fRemaining, fEventBase);
61 if (fEventBase >= 0 && (fRemaining + fEventBase > eventNum)) {
63 if (R__unlikely(Adjust(adjust) < 0)) {
64 //printf("Failed to adjust offset to mid-buffer.\n");
65 return -1;
66 }
68 } else {
69 if (!fBranch) {
71 // printf("Failed to retrieve branch.\n");
72 return -1;
73 }
75 if (R__unlikely(fRemaining < 0)) {
77 //printf("Failed to retrieve entries from the branch.\n");
78 return -1;
79 }
80 }
82 //printf("After getting events, the base is %lld with %d remaining.\n", fEventBase, fRemaining);
84 return fRemaining;
85 }
86
87 virtual const char *GetTypeName() {return "{UNDETERMINED}";}
88
89
90 protected:
91
92 // Adjust the current buffer offset forward N events.
98 virtual UInt_t GetSize() = 0;
99
101 fTreeReader = nullptr;
102 }
103
104 // Create the linkage between the TTreeReader's current tree and this ReaderValue
105 // object. After CreateProxy() is invoked, if fSetupStatus doesn't indicate an
106 // error, then we are pointing toward a valid TLeaf in the current tree
107 void CreateProxy();
108
110
111 // Returns the name of the branch type; will be used when the TBranch version to
112 // detect between the compile-time and runtime type names.
113 virtual const char *BranchTypeName() = 0;
114
115 std::string fBranchName; // Name of the branch we should read from.
116 std::string fLeafName; // The branch's leaf we should read from. NOTE: currently only support single-leaf branches.
117 TTreeReaderFast *fTreeReader{nullptr}; // Reader we belong to
118 TBranch * fBranch{nullptr}; // Actual branch object we are reading.
119 TLeaf * fLeaf{nullptr}; // Actual leaf we are reading.
120 TBufferFile fBuffer; // Buffer object holding the current events.
121 Int_t fRemaining{0}; // Number of events remaining in the buffer.
122 Int_t &fEvtIndex; // Current event index.
123 Long64_t fLastChainOffset{-1}; // Current chain in the TTree we are pointed at.
124 Long64_t fEventBase{-1}; // Event number of the current buffer position.
125
128
130};
131
132} // Internal
133
134template <typename T>
136
137 public:
139
140 T* Get() {
141 return Deserialize(reinterpret_cast<char *>(reinterpret_cast<T*>(fBuffer.GetCurrent()) + fEvtIndex));
142 }
143 T* operator->() { return Get(); }
144 T& operator*() { return *Get(); }
145
146 protected:
147 T* Deserialize(char *) {return nullptr;}
148
149 const char *GetTypeName() override {return "{INCOMPLETE}";}
150 UInt_t GetSize() override {return sizeof(T);}
151};
152
153template<>
155
156 public:
157
160
161 float* Get() {
162 return Deserialize(reinterpret_cast<char *>(reinterpret_cast<float*>(fBuffer.GetCurrent()) + fEvtIndex));
163 }
164 float* operator->() { return Get(); }
165 float& operator*() { return *Get(); }
166
167 protected:
168 const char *GetTypeName() override {return "float";}
169 const char *BranchTypeName() override {return "float";}
170 UInt_t GetSize() override {return sizeof(float);}
171 float * Deserialize(char *input) {frombuf(input, &fTmp); return &fTmp;}
172
173 float fTmp;
174};
175
176template <>
178
179 public:
180
183
184 // TODO: why isn't template specialization working here?
185 double* Get() {
186 //printf("Double: Attempting to deserialize buffer %p from index %d.\n", fBuffer.GetCurrent(), fEvtIndex);
187 return Deserialize(reinterpret_cast<char *>(reinterpret_cast<double*>(fBuffer.GetCurrent()) + fEvtIndex));
188 }
189 double* operator->() { return Get(); }
190 double& operator*() { return *Get(); }
191
192 protected:
193 const char *GetTypeName() override {return "double";}
194 const char *BranchTypeName() override {return "double";}
195 UInt_t GetSize() override {return sizeof(double);}
196 double* Deserialize(char *input) {frombuf(input, &fTmp); return &fTmp;}
197
198 double fTmp;
199};
200
201template <>
203
204 public:
205
208
210 return Deserialize(reinterpret_cast<char *>(reinterpret_cast<Int_t*>(fBuffer.GetCurrent()) + fEvtIndex));
211 }
212 Int_t* operator->() { return Get(); }
213 Int_t& operator*() { return *Get(); }
214
215 protected:
216 const char *GetTypeName() override {return "integer";}
217 const char *BranchTypeName() override {return "integer";}
218 UInt_t GetSize() override {return sizeof(Int_t);}
219 Int_t* Deserialize(char *input) {frombuf(input, &fTmp); return &fTmp;}
220
222};
223
224template <>
226
227 public:
228
231
233 return Deserialize(reinterpret_cast<char *>(reinterpret_cast<UInt_t*>(fBuffer.GetCurrent()) + fEvtIndex));
234 }
235 UInt_t* operator->() { return Get(); }
236 UInt_t& operator*() { return *Get(); }
237
238 protected:
239 const char *GetTypeName() override {return "unsigned integer";}
240 const char *BranchTypeName() override {return "unsigned integer";}
241 UInt_t GetSize() override {return sizeof(UInt_t);}
242 UInt_t* Deserialize(char *input) {frombuf(input, &fTmp); return &fTmp;}
243
245};
246
247template <>
249
250 public:
251
254
255 bool* Get() {
256 return Deserialize(reinterpret_cast<char *>(reinterpret_cast<bool*>(fBuffer.GetCurrent()) + fEvtIndex));
257 }
258 bool* operator->() { return Get(); }
259 bool& operator*() { return *Get(); }
260
261 protected:
262 const char *GetTypeName() override {return "unsigned integer";}
263 const char *BranchTypeName() override {return "unsigned integer";}
264 UInt_t GetSize() override {return sizeof(bool);}
265 bool* Deserialize(char *input) {frombuf(input, &fTmp); return &fTmp;}
266
267 bool fTmp;
268};
269
270} // Experimental
271} // ROOT
272
273#endif // ROOT_TTreeReaderValueFast
void frombuf(char *&buf, Bool_t *x)
Definition Bytes.h:278
#define R__unlikely(expr)
Definition RConfig.hxx:594
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int)
Definition RtypesCore.h:60
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
Int_t GetEntriesSerialized(Long64_t evt, TBuffer &user_buf)
See TBranch::GetEntriesSerialized(Long64_t evt, TBuffer &user_buf);.
Definition TBranch.h:322
void CreateProxy()
Attach this value to the appropriate branch on the tree.
ROOT::Internal::TTreeReaderValueBase::ESetupStatus fSetupStatus
TTreeReaderValueFastBase(const TTreeReaderValueFastBase &)=delete
TTreeReaderValueFastBase(TTreeReaderFast *reader, const std::string &branchName)
Construct a tree value reader and register it with the reader object.
ROOT::Internal::TTreeReaderValueBase::ESetupStatus GetSetupStatus() const
virtual ~TTreeReaderValueFastBase()
Unregister from tree reader, cleanup.
ROOT::Internal::TTreeReaderValueBase::EReadStatus fReadStatus
virtual ROOT::Internal::TTreeReaderValueBase::EReadStatus GetReadStatus() const
void RegisterValueReader(ROOT::Experimental::Internal::TTreeReaderValueFastBase *reader)
Add a value reader for this tree.
TTreeReaderValueFast(TTreeReaderFast &tr, const std::string &branchname)
TTreeReaderValueFast(TTreeReaderFast &tr, const std::string &branchname)
TTreeReaderValueFast(TTreeReaderFast &tr, const std::string &branchname)
TTreeReaderValueFast(TTreeReaderFast &tr, const std::string &branchname)
TTreeReaderValueFast(TTreeReaderFast &tr, const std::string &branchname)
TTreeReaderValueFast(TTreeReaderFast *reader, const std::string &branchname)
@ kSetupNotSetup
No initialization has happened yet.
@ kReadNothingYet
Data now yet accessed.
A TTree is a list of TBranches.
Definition TBranch.h:93
ROOT::Experimental::Internal::TBulkBranchRead & GetBulkRead()
Definition TBranch.h:219
The concrete implementation of TBuffer for writing/reading to/from a ROOT file or socket.
Definition TBufferFile.h:47
Buffer base class used for serializing objects.
Definition TBuffer.h:43
char * GetCurrent() const
Definition TBuffer.h:97
void SetBufferOffset(Int_t offset=0)
Definition TBuffer.h:93
Int_t Length() const
Definition TBuffer.h:100
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition TLeaf.h:57