Logo ROOT   6.18/05
Reference Guide
TTreeReaderFast.cxx
Go to the documentation of this file.
1// Author: Brian Bockelman, 2017-03-21
2
3/*************************************************************************
4 * Copyright (C) 1995-2013, Rene Brun and Fons Rademakers and al. *
5 * All rights reserved. *
6 * *
7 * For the licensing terms see $ROOTSYS/LICENSE. *
8 * For the list of contributors see $ROOTSYS/README/CREDITS. *
9 *************************************************************************/
10
11#include <climits>
12
13#include "TTreeReader.h"
15
16#include "TChain.h"
17#include "TDirectory.h"
19
20using namespace ROOT::Experimental;
21
22TTreeReaderFast::TTreeReaderFast(TTree* tree):
23 fTree(tree)
24{
25 if (!fTree) {
26 ::Error("TTreeReaderFast::TTreeReaderFast", "TTree is NULL!");
27 } else {
28 Initialize();
29 }
30}
31
32TTreeReaderFast::TTreeReaderFast(const char* keyname, TDirectory* dir /*= NULL*/):
33 fDirectory(dir)
34{
36 fDirectory->GetObject(keyname, fTree);
37 Initialize();
38}
39
40////////////////////////////////////////////////////////////////////////////////
41/// Tell all value readers that the tree reader does not exist anymore.
42
44{
45 for (auto &reader : fValues) {
46 reader->MarkTreeReaderUnavailable();
47 }
48}
49
50////////////////////////////////////////////////////////////////////////////////
51/// Initialization of the director.
52
54{
55 if (!fTree) {
56 MakeZombie();
58 } else {
60 }
61
62 bool IsOK = true;
63 // Tell readers we now have a tree
64 for (auto &reader : fValues) {
65 reader->CreateProxy();
66 if (reader->GetSetupStatus() != ROOT::Internal::TTreeReaderValueBase::kSetupMatch) {
67 //printf("Reader setup failed. Status: %d\n", reader->GetSetupStatus());
68 IsOK = false;
69 }
70 }
71 if (!IsOK) {
72 //printf("Failed to initialize the reader.\n");
74 }
75}
76
77////////////////////////////////////////////////////////////////////////////////
78/// Set an entry to be read.
79
82{
83 if (!fTree) {
85 return fEntryStatus;
86 }
87
88 TTree* prevTree = fDirector->GetTree();
89
90 Int_t treeNumInChainBeforeLoad = fTree->GetTreeNumber();
91
92 TTree* treeToCallLoadOn = fTree->GetTree();
93 Long64_t loadResult = treeToCallLoadOn->LoadTree(entry);
94
95 if (loadResult == -2) {
97 return fEntryStatus;
98 }
99
100 if (treeNumInChainBeforeLoad != fTree->GetTreeNumber()) {
102 }
103
104 if (!prevTree || fDirector->GetReadEntry() == -1)
105 {
106 bool IsOK = true;
107 // Tell readers we now have a tree
108 for (auto &reader : fValues) {
109 reader->CreateProxy();
110 if (reader->GetSetupStatus() != ROOT::Internal::TTreeReaderValueBase::kSetupMatch) IsOK = false;
111 }
113 }
114
115 return fEntryStatus;
116}
117
118////////////////////////////////////////////////////////////////////////////////
119/// Add a value reader for this tree.
120
122{
123 fValues.push_back(reader);
124}
125
126////////////////////////////////////////////////////////////////////////////////
127/// Remove a value reader for this tree.
128
130{
131 auto iReader = std::find(fValues.begin(), fValues.end(), reader);
132 if (iReader == fValues.end()) {
133 Error("DeregisterValueReader", "Cannot find reader of type %s for branch %s", reader->GetTypeName(), reader->fBranchName.c_str());
134 return;
135 }
136 fValues.erase(iReader);
137}
138
139////////////////////////////////////////////////////////////////////////////////
140/// Advance to the next range in the file; returns the number of events in the range.
141///
142/// Returned number is the number of events we can process before one of the Value
143/// objects will hit the end of its buffer.
144
145Int_t
147{
148 Int_t remaining = INT_MAX;
149 for (auto &value : fValues) {
150 Int_t valueRemaining = value->GetEvents(eventNum);
151 if (valueRemaining < remaining) {
152 remaining = valueRemaining;
153 }
154 }
155 //printf("TTreeReaderFast::GetNextRange: Starting at %d, remaining events %d.\n", eventNum, remaining);
156 return remaining;
157}
158
int Int_t
Definition: RtypesCore.h:41
long long Long64_t
Definition: RtypesCore.h:69
#define gDirectory
Definition: TDirectory.h:218
Int_t GetNextRange(Int_t)
Advance to the next range in the file; returns the number of events in the range.
void Initialize()
Initialization of the director.
std::deque< ROOT::Experimental::Internal::TTreeReaderValueFastBase * > fValues
readers that use our director
TDirectory * fDirectory
directory (or current file for chains)
~TTreeReaderFast()
Tell all value readers that the tree reader does not exist anymore.
ROOT::Internal::TBranchProxyDirector * fDirector
proxying director, owned
TTreeReader::EEntryStatus fEntryStatus
status of most recent read request
TTreeReader::EEntryStatus SetEntry(Long64_t)
Set an entry to be read.
void DeregisterValueReader(ROOT::Experimental::Internal::TTreeReaderValueFastBase *reader)
Remove a value reader for this tree.
void RegisterValueReader(ROOT::Experimental::Internal::TTreeReaderValueFastBase *reader)
Add a value reader for this tree.
Long64_t GetReadEntry() const
Return the current 'local' entry number; i.e.
@ kSetupMatch
The branch class type is not a collection.
Describe directory structure in memory.
Definition: TDirectory.h:34
void GetObject(const char *namecycle, T *&ptr)
Definition: TDirectory.h:144
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:880
void MakeZombie()
Definition: TObject.h:49
@ kEntryNotFound
the tree entry number does not exist
Definition: TTreeReader.h:129
@ kEntryNoTree
the tree does not exist
Definition: TTreeReader.h:128
@ kEntryBadReader
One of the readers was not successfully initialized.
Definition: TTreeReader.h:134
@ kEntryValid
data read okay
Definition: TTreeReader.h:126
A TTree represents a columnar dataset.
Definition: TTree.h:71
virtual TTree * GetTree() const
Definition: TTree.h:456
virtual Long64_t LoadTree(Long64_t entry)
Set current entry.
Definition: TTree.cxx:6251
virtual Int_t GetTreeNumber() const
Definition: TTree.h:458
Definition: tree.py:1