TreeManager.cxx
Go to the documentation of this file.
1 /*
2  * TreeManager.cxx
3  *
4  * Created on: Feb 9, 2021
5  * Author: chilgenb
6  */
7 
9 #include <iostream>
10 
11 using namespace garana;
12 using namespace std;
13 
14 TreeManager::TreeManager(const string& infilename) : //, bool onGen=1, bool onG4=1, bool onDet=0, bool onReco=1, bool onDis=0) :
15  fInFileName(infilename),
16  fOnGen(true),
17  fOnG4(true),
18  fOnDet(false),
19  fOnRec(true),
20  fOnDis(false)
21  {
22 
23  //fInFileName = infilename;
24  Init();
25  CheckEntries();
26 
27 }
28 
30 
31  //open file for reading
32  try {
33  fInFile = new TFile(fInFileName.c_str(),"READ");
34  if(!fInFile)
35  throw fInFileName;
36  }
37  catch(std::string& fname){
38  std::cerr << "TreeManager::Init: unable to find the file, "
39  << fInFileName << '\n';
40  }
41 
42  // headerTree
43  TTree* header = (TTree*)fInFile->FindObjectAny("headerTree");
44  try {
45  if(!header)
46  throw header;
47  }
48  catch(TTree* t){
49  std::cerr << "TreeManager::Init: "
50  << "No HeaderTree found in input file, " << fInFile
51  << '\n';
52  }
53 
54  cout << "found header Tree" << endl;
55 
56  fHeaderTree = new HeaderTree(header);
58 
59  /// genTree
60  TTree* gen = nullptr;
61  try{
62  gen = (TTree*)fInFile->FindObjectAny(genName.c_str());
63  if(!gen)
64  throw gen;
65  }
66  catch(TTree* t){
67  std::cerr << "TreeManager::Init: "
68  << "No GenTree found in input file, " << fInFile
69  << '\n';
70  }
71 
72  if(fTreeType == "structured"){ //structured tree
74  }
75  else { //flat tree
76  fFlatGenTree = new FlatGenTree(gen);
77  }
78 
79  cout << "found genTree" << endl;
80 
81  /// g4Tree ///
82  TTree* g4 = nullptr;
83  try{
84  g4 = (TTree*)fInFile->FindObjectAny(g4Name.c_str());
85  if(!g4)
86  throw g4;
87  }
88  catch(TTree* t){
89  std::cerr << "TreeManager::Init: "
90  << "No G4Tree found in input file, " << fInFile
91  << '\n';
92  }
93 
94  if(fTreeType == "structured"){ //structured tree
96  }
97  else { //flat tree
98  fFlatG4Tree = new FlatG4Tree(g4);
99  }
100  cout << "found g4Tree" << endl;
101 
102  /// recoTree ///
103  TTree* reco = nullptr;
104  try{
105  reco = (TTree*)fInFile->FindObjectAny(recoName.c_str());
106  if(!reco)
107  throw reco;
108  }
109  catch(TTree* t){
110  std::cerr << "TreeManager::Init: "
111  << "No RecoTree found in input file, " << fInFile
112  << '\n';
113  }
114 
115  if(fTreeType == "structured"){ //structured tree
117  }
118  else { //flat tree
119  fFlatRecoTree = new FlatRecoTree(reco);
120  }
121 
122  cout << "found recoTree" << endl;
123 
124  cout << "at end of Init()" << endl;
125 
126 }// Init()
127 
129 
130  return fHeaderTree;
131 
132 }
133 
135 
136  try{
138  throw fStructGenTree;
139  if(!fStructGenTree){
140  return fFlatGenTree;
141  }
142  else{
143  return fStructGenTree;
144  }
145  }
146  catch(StructuredGenTree* e){
147  std::cerr << "TreeManager::GenTree: no structured genTree or flat genTree found!" << std::endl;
148  }
149 
150  return nullptr;
151 
152 }//GetGenTree()
153 
155 
156  try{
157  if(!fStructG4Tree && !fFlatG4Tree)
158  throw fStructG4Tree;
159  if(!fStructG4Tree){
160  return fFlatG4Tree;
161  }
162  else{
163  return fStructG4Tree;
164  }
165  }
166  catch(StructuredGenTree* e){
167  std::cerr << "TreeManager::G4Tree: no structured g4Tree or flat g4Tree found!" << std::endl;
168  }
169 
170  return nullptr;
171 
172 }//GetGenTree()
173 
175 
176  try{
178  throw fStructDetTree;
179  if(!fStructDetTree){
180  return fFlatDetTree;
181  }
182  else{
183  return fStructDetTree;
184  }
185  }
186  catch(StructuredDetTree* e){
187  std::cerr << "TreeManager::DetTree: no structured detTree or flat detTree found!" << std::endl;
188  }
189 
190  return nullptr;
191 
192 }//GetGenTree()
193 
195 
196  try{
198  throw fStructRecoTree;
199  if(!fStructRecoTree){
200  return fFlatRecoTree;
201  }
202  else{
203  return fStructRecoTree;
204  }
205  }
206  catch(StructuredRecoTree* e){
207  cerr << "TreeManager::RecoTree: no structured recoTree or flat recoTree found!" << endl;
208  }
209 
210  return nullptr;
211 
212 }//GetGenTree()
213 
215 
216  try{
218  throw fStructDisplayTree;
219  if(!fStructDisplayTree){
220  return fFlatDisplayTree;
221  }
222  else{
223  return fStructDisplayTree;
224  }
225  }
226  catch(StructuredDisplayTree* e){
227  cerr << "TreeManager::DisplayTree: no structured displayTree or flat displayTree found!" << endl;
228  }
229 
230  return nullptr;
231 
232 }//GetGenTree()
233 
234 
235 void TreeManager::GetEntry(const UInt_t& ientry){
236 
237  if(fOnGen) {
239  else if(fFlatGenTree) fFlatGenTree->GetEntry(ientry);
240  }
241  if(fOnG4) {
242  if(fStructG4Tree) fStructG4Tree->GetEntry(ientry);
243  else if(fFlatG4Tree) fFlatG4Tree->GetEntry(ientry);
244  }
245  if(fOnDet) {
247  else if(fFlatDetTree) fFlatDetTree->GetEntry(ientry);
248  }
249  if(fOnRec) {
251  else if(fFlatRecoTree) fFlatRecoTree->GetEntry(ientry);
252  }
253 
254  // probably don't want to include display tree
255 }
256 
258 
259  vector<UInt_t> ns;
260  if(fOnGen) {
261  if(fStructGenTree) ns.push_back(fStructGenTree->NEntries());
262  else if(fFlatGenTree) ns.push_back(fFlatGenTree->NEntries());
263  }
264  if(fOnG4) {
265  if(fStructG4Tree) ns.push_back(fStructG4Tree->NEntries());
266  else if(fFlatG4Tree) ns.push_back(fFlatG4Tree->NEntries());
267  }
268  if(fOnDet) {
269  if(fStructDetTree) ns.push_back(fStructDetTree->NEntries());
270  else if(fFlatDetTree) ns.push_back(fFlatDetTree->NEntries());
271  }
272  if(fOnRec) {
273  if(fStructRecoTree) ns.push_back(fStructRecoTree->NEntries());
274  else if(fFlatRecoTree) ns.push_back(fFlatRecoTree->NEntries());
275  }
276 
277  try {
278  for(size_t i=1; i<ns.size(); i++){
279  if(ns[0]!=ns[i])
280  throw ns[0];
281  }
282  }
283  catch(Int_t n){
284  cerr << "number of entries differs between trees!" << endl;
285  return false;
286  }
287 
288  fNEntries = ns[0];
289  return true;
290 }
DisplayTree * GetDisplayTree() const
StructuredRecoTree * fStructRecoTree
Definition: TreeManager.h:95
FlatGenTree * fFlatGenTree
Definition: TreeManager.h:98
std::string string
Definition: nybbler.cc:12
STL namespace.
FlatDisplayTree * fFlatDisplayTree
Definition: TreeManager.h:102
StructuredDisplayTree * fStructDisplayTree
Definition: TreeManager.h:96
StructuredGenTree * fStructGenTree
Definition: TreeManager.h:92
StructuredDetTree * fStructDetTree
Definition: TreeManager.h:94
const double e
const std::string *const TreeType() const
Definition: HeaderTree.cxx:32
std::void_t< T > n
void GetEntry(const UInt_t &ientry) override
Definition: FlatGenTree.cxx:39
gen
Definition: demo.py:24
FlatG4Tree * fFlatG4Tree
Definition: TreeManager.h:99
FlatRecoTree * fFlatRecoTree
Definition: TreeManager.h:101
FlatDetTree * fFlatDetTree
Definition: TreeManager.h:100
HeaderTree * GetHeaderTree() const
DetTree * GetDetTree() const
StructuredG4Tree * fStructG4Tree
Definition: TreeManager.h:93
void GetEntry(const UInt_t &ientry) override
Definition: FlatG4Tree.cxx:38
G4Tree * GetG4Tree() const
const string recoName
Definition: TreeManager.h:71
RecoTree * GetRecoTree() const
const string genName
Definition: TreeManager.h:68
HeaderTree * fHeaderTree
Definition: TreeManager.h:90
virtual void GetEntry(const UInt_t &ientry)
Definition: TreeReader.cxx:39
GenTree * GetGenTree() const
const string g4Name
Definition: TreeManager.h:69
QAsciiDict< Entry > ns
void GetEntry(const UInt_t &ientry)
size_t NEntries() const
Definition: TreeReader.cxx:35
QTextStream & endl(QTextStream &s)
g4
Definition: tracks.py:87