ATLAS Offline Software
Loading...
Searching...
No Matches
TreeReader.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4
5#include "TTreeFormula.h"
6#include "TTreeFormulaManager.h"
7#include "TString.h"
8#include <iostream>
9#include <TChain.h>
10#include <vector>
12
14//
15// Class TreeReader
16// TreeReader.cpp
17//
19
20//______________________________________________________________________________
21/*
22 Class for Tree reading through TFormula.
23 ______________________________________________________________________________*/
24
25
27{
28 // Default constructor.
29 m_isChain = false;
30 m_currentTree = -1;
31 m_tree = nullptr;
32 m_currentEntry = -1;
33 m_entries = -1;
34}
35
37{
38 m_formulae.clear();
39}
40
41
42//============================================================
44//============================================================
45{
46 // Constructor.
47 m_tree = nullptr;
48 m_entries = -1;
49 SetTree(n);
50}
51
52//============================================================
53void TreeReader::SetTree(TTree* n)
54//============================================================
55{
56 // check for null pointer BEFORE trying to use it
57 if(!n) return;
58 // Set tree.
59 m_tree = n;
60 m_currentEntry = -1;
61 m_formulae.clear();
62 m_formulae["__DUMMY__"] = new TTreeFormula("__DUMMY__","0",m_tree);
63 m_isChain = (n->IsA() == TClass::GetClass("TChain"));
64 m_currentTree = 0;
65 m_entries = (int) m_tree->GetEntries();
66}
67
68//=============================================================
69double TreeReader::GetVariable(const char* c, int entry)
70//============================================================
71{
72 // Get vaviable.
73 // Return variable for a given entry (<0 -> current entry).
74 if(entry>=0 && entry!=m_currentEntry) this->GetEntry(entry);
75 std::string s = c;
76 TTreeFormula *f = m_formulae[s];
77 if(!f)
78 {
79 f = new TTreeFormula(c,c,m_tree);
80 f->SetQuickLoad(kTRUE);
81 // fManager->Add(f);
82 // fManager->Sync();
83 if(f->GetNdim()!=1) //invalid fomula
84 {
85 delete f;
86 f = m_formulae["__DUMMY__"];
87 std::cout << "in [TreeReader] : " << s << " is not valid -> return 0" << std::endl;
88 }
89 // else {f->Notify();}
90 m_formulae[s] = f;
91 }
92 if(f == m_formulae["__DUMMY__"]) return 0;
93 int valid = f->GetNdata() ;
94 if(!valid) return 0;
95 // std::cout << "Evaluating formula : " << s << std::flush;
96 // std::cout << " " << f->EvalInstance(0) << std::endl;
97 return f->EvalInstance(0);
98}
99
100
101//============================================================
103//============================================================
104{
105 // Read a given entry in the buffer (-1 -> next entry).
106 // Return kFALSE if not found.
107 // entry += 1;
108 if(m_entries==0) return 0;
109 if(entry==-1) entry = m_currentEntry+1;
110 if(entry<m_entries)
111 {
112 int entryNumber = m_tree->GetEntryNumber(entry);
113 if (entryNumber < 0) return 0;
114 Long64_t localEntry = m_tree->LoadTree(entryNumber);
115 if (localEntry < 0) return 0;
116 m_currentEntry = entry;
117 if(m_isChain) // check file change in chain
118 {
119 int I = static_cast<TChain*>(m_tree)->GetTreeNumber();
120 if(I!=m_currentTree)
121 {
123 //fManager->Clear();
124 std::map<std::string, TTreeFormula*>::iterator itr = m_formulae.begin();
125 std::map<std::string, TTreeFormula*>::iterator itrE= m_formulae.end();
126 TTreeFormula* dummy = m_formulae["__DUMMY__"];
127 for(;itr!=itrE;++itr)
128 {
129 if(itr->second!=dummy) itr->second->Notify(); //itr->second->UpdateFormulaLeaves();
130 }
131 }
132 }
133 return 1;
134 }
135 return 0;
136}
137
138
139
140//ClassImp(TreeReader) // Integrate this class into ROOT
141
#define I(x, y, z)
Definition MD5.cxx:116
double GetVariable(const char *c, int entry=-2)
virtual ~TreeReader()
TTree * m_tree
Definition TreeReader.h:33
int m_currentTree
Definition TreeReader.h:37
std::map< std::string, TTreeFormula * > m_formulae
Definition TreeReader.h:38
void SetTree(TTree *n)
int GetEntry(int entry=-1)
int m_entries
Definition TreeReader.h:35
bool m_isChain
Definition TreeReader.h:36
int m_currentEntry
Definition TreeReader.h:34