HighLAND
TreeManager.cxx
1 #include "TreeManager.hxx"
2 #include <TDirectory.h>
3 #include <TList.h>
4 #include <sstream>
5 #include <stdlib.h>
6 #include <algorithm>
7 
8 //********************************************************************
9 TreeManager::TreeManager(){
10 //********************************************************************
11 
12  _current_tree=-1;
13  _file=NULL;
14 
15  // Put all trees to NULL
16  _trees.resize(NMAXTREES);
17  for (UInt_t i=0;i<_trees.size();i++)
18  _trees[i]= NULL;
19 
20  _trees_indices.clear();
21 }
22 
23 //********************************************************************
24 TreeManager::~TreeManager() {
25 //********************************************************************
26 
27  if (_file) delete _file;
28 }
29 
30 //**************************************************
31 TTree* TreeManager::GetTree(const std::string& name){
32 //**************************************************
33 
34  Int_t index = GetTreeIndex(name);
35  if (index!=-1) return GetTree(index);
36  else return NULL;
37 }
38 
39 //**************************************************
40 Int_t TreeManager::GetTreeIndex(const std::string& name){
41 //**************************************************
42 
43  for (UInt_t i=0;i<_trees_indices.size();i++){
44  if (_trees[_trees_indices[i]]->GetName() == name) return _trees_indices[i];
45  }
46 
47  return -1;
48 }
49 
50 //**************************************************
51 std::string TreeManager::GetTreeName(Int_t index){
52 //**************************************************
53 
54  if (!HasTree(index)) return "";
55  return _trees[index]->GetName();
56 }
57 
58 //**************************************************
59 bool TreeManager::HasTree(Int_t index){
60 //**************************************************
61 
62  if (index<0 || index>(Int_t)_trees.size()-1) return false;
63  return (_trees[index]!=NULL);
64 }
65 
66 //**************************************************
67 bool TreeManager::HasTree(const std::string& name){
68 //**************************************************
69 
70  return (GetTreeIndex(name)!=-1);
71 }
72 
73 //********************************************************************
74 void TreeManager::ReadTree(const std::string& file, Int_t index){
75 //********************************************************************
76 
77  // Load a tree from a file
78  if (_file) delete _file;
79  _file = new TFile(file.c_str(),"READ");
80 
81  std::string tree_name = GetTreeName(index);
82 
83  TTree *t = (TTree*)(_file->Get(tree_name.c_str()));
84  Int_t tree_index = std::atoi(t->GetTitle());
85  _trees[tree_index]= t;
86 
87  _trees_indices.push_back(tree_index);
88  _trees_nonull.push_back(_trees[tree_index]);
89 
90  // Set as current tree
91  SetCurrentTree(index);
92 }
93 
94 
95 //********************************************************************
96 void TreeManager::ReadTree(const std::string& file, const std::string& tree_name){
97 //********************************************************************
98 
99  Int_t index = GetTreeIndex(tree_name);
100  if (index!=-1) ReadTree(file,index);
101 }
102 
103 //********************************************************************
104 void TreeManager::ReadFile(const std::string& file){
105 //********************************************************************
106 
107  for (UInt_t i=0;i<_trees.size();i++)
108  _trees[i]= NULL;
109 
110  _trees_indices.clear();
111 
112 
113  // Load all trees from a file
114  _file = new TFile(file.c_str(),"READ");
115 
116  TList *keys = _file->GetListOfKeys();
117  for (int i=0;i<keys->GetEntries();i++){
118  std::string tree_name = keys->At(i)->GetName();
119  TTree *t = (TTree*)(_file->Get(tree_name.c_str()));
120  Int_t tree_index = std::atoi(t->GetTitle());
121  if(_trees[tree_index]) continue;
122  _trees[tree_index]= t;
123 
124  _trees_indices.push_back(tree_index);
125  _trees_nonull.push_back(_trees[tree_index]);
126  }
127 
128  // Set as current tree the "default"
129  SetCurrentTree("default");
130 }
131 
132 
133 //**************************************************
135 //**************************************************
136 
137  if (GetCurrentTree()==-1) return "";
138  return GetTreeName(GetCurrentTree());
139 }
140 
141 
142 //**************************************************
143 void TreeManager::SetCurrentTree(const std::string& tree_name){
144 //**************************************************
145 
146  SetCurrentTree(GetTreeIndex(tree_name));
147 }
std::vector< Int_t > _trees_indices
Vector of indices for the non NULL TTree in previous vector.
Definition: TreeManager.hxx:81
Int_t GetCurrentTree() const
Retuns the current tree index.
Definition: TreeManager.hxx:52
std::string GetTreeName(Int_t index)
Retuns the tree name provided the index.
Definition: TreeManager.cxx:51
std::string GetCurrentTreeName()
Retuns the current tree name.
void ReadFile(const std::string &file)
Read all trees from a file.
void SetCurrentTree(Int_t index)
Sets the current tree provided the index.
Definition: TreeManager.hxx:58
Int_t GetTreeIndex(const std::string &tree_name)
Retuns the tree index provided the name.
Definition: TreeManager.cxx:40
bool HasTree(Int_t index)
Check the existence of a tree provided the index.
Definition: TreeManager.cxx:59
std::vector< TTree *> _trees
Vector of trees.
Definition: TreeManager.hxx:75
void ReadTree(const std::string &file, Int_t index)
Read a tree from a file provided the index.
Definition: TreeManager.cxx:74
TFile * _file
Root input or output file.
Definition: TreeManager.hxx:72
std::vector< TTree *> _trees_nonull
Vector of non NULL trees.
Definition: TreeManager.hxx:78
TTree * GetTree()
Returns the a tree set as current.
Definition: TreeManager.hxx:34
Int_t _current_tree
The current tree.
Definition: TreeManager.hxx:84