Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
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
long long Long64_t
Definition RtypesCore.h:80
#define gDirectory
Definition TDirectory.h:384
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Int_t GetNextRange(Int_t)
Advance to the next range in the file; returns the number of events in the range.
~TTreeReaderFast() override
Tell all value readers that the tree reader does not exist anymore.
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)
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.
TTree * SetTree(TTree *newtree)
Set the BranchProxy to be looking at a new tree.
@ kSetupMatch
This branch has been set up, branch data type and reader template type match, reading should succeed.
Describe directory structure in memory.
Definition TDirectory.h:45
void GetObject(const char *namecycle, T *&ptr)
Get an object with proper type checking.
Definition TDirectory.h:212
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:987
void MakeZombie()
Definition TObject.h:53
@ kEntryNotFound
the tree entry number does not exist
@ kEntryNoTree
the tree does not exist
@ kEntryBadReader
One of the readers was not successfully initialized.
@ kEntryValid
data read okay
A TTree represents a columnar dataset.
Definition TTree.h:79
virtual TTree * GetTree() const
Definition TTree.h:517
virtual Long64_t LoadTree(Long64_t entry)
Set current entry.
Definition TTree.cxx:6470
virtual Int_t GetTreeNumber() const
Definition TTree.h:519
Definition tree.py:1