ATLAS Offline Software
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 //============================================================
53 void 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 //=============================================================
69 double 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;
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  {
122  m_currentTree = I;
123  //fManager->Clear();
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 
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
TreeReader::m_entries
int m_entries
Definition: TreeReader.h:35
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
TreeReader::~TreeReader
virtual ~TreeReader()
Definition: TreeReader.cxx:36
TreeReader::SetTree
void SetTree(TTree *n)
Definition: TreeReader.cxx:53
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
TreeReader::GetEntry
int GetEntry(int entry=-1)
Definition: TreeReader.cxx:102
calibdata.valid
list valid
Definition: calibdata.py:45
beamspotman.n
n
Definition: beamspotman.py:731
TreeReader.h
TreeReader::m_formulae
std::map< std::string, TTreeFormula * > m_formulae
Definition: TreeReader.h:38
TreeReader::TreeReader
TreeReader()
Definition: TreeReader.cxx:26
python.xAODType.dummy
dummy
Definition: xAODType.py:4
hist_file_dump.f
f
Definition: hist_file_dump.py:135
TreeReader::m_currentEntry
int m_currentEntry
Definition: TreeReader.h:34
TreeReader::m_tree
TTree * m_tree
Definition: TreeReader.h:33
GetAllXsec.entry
list entry
Definition: GetAllXsec.py:132
TreeReader::m_isChain
bool m_isChain
Definition: TreeReader.h:36
TreeReader::GetVariable
double GetVariable(const char *c, int entry=-2)
Definition: TreeReader.cxx:69
TreeReader::m_currentTree
int m_currentTree
Definition: TreeReader.h:37
I
#define I(x, y, z)
Definition: MD5.cxx:116
python.compressB64.c
def c
Definition: compressB64.py:93