Logo ROOT   6.21/01
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"
14 #include "ROOT/TTreeReaderFast.hxx"
15 
16 #include "TChain.h"
17 #include "TDirectory.h"
19 
20 using 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 
32 TTreeReaderFast::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 
145 Int_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 
the tree does not exist
Definition: TTreeReader.h:127
~TTreeReaderFast()
Tell all value readers that the tree reader does not exist anymore.
Int_t GetNextRange(Int_t)
Advance to the next range in the file; returns the number of events in the range. ...
long long Long64_t
Definition: RtypesCore.h:69
The branch class type is not a collection.
void GetObject(const char *namecycle, T *&ptr)
Definition: TDirectory.h:149
TDirectory * fDirectory
directory (or current file for chains)
void RegisterValueReader(ROOT::Experimental::Internal::TTreeReaderValueFastBase *reader)
Add a value reader for this tree.
int Int_t
Definition: RtypesCore.h:41
TTreeReader::EEntryStatus fEntryStatus
status of most recent read request
void DeregisterValueReader(ROOT::Experimental::Internal::TTreeReaderValueFastBase *reader)
Remove a value reader for this tree.
TTree * fTree
tree that&#39;s read
virtual Long64_t LoadTree(Long64_t entry)
Set current entry.
Definition: TTree.cxx:6272
the tree entry number does not exist
Definition: TTreeReader.h:128
virtual TTree * GetTree() const
Definition: TTree.h:496
virtual Int_t GetTreeNumber() const
Definition: TTree.h:498
ROOT::Internal::TBranchProxyDirector * fDirector
proxying director, owned
std::deque< ROOT::Experimental::Internal::TTreeReaderValueFastBase * > fValues
readers that use our director
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:880
void Initialize()
Initialization of the director.
One of the readers was not successfully initialized.
Definition: TTreeReader.h:133
Long64_t GetReadEntry() const
Return the current &#39;local&#39; entry number; i.e.
Describe directory structure in memory.
Definition: TDirectory.h:34
void MakeZombie()
Definition: TObject.h:49
Definition: tree.py:1
A TTree represents a columnar dataset.
Definition: TTree.h:72
#define gDirectory
Definition: TDirectory.h:223
TTreeReader::EEntryStatus SetEntry(Long64_t)
Set an entry to be read.