Diana Software
QChain.cc
Go to the documentation of this file.
1 #include "QChain.hh"
2 #include "QFileList.hh"
3 #include <iostream>
4 #include "QTree.hh"
5 #include <TFile.h>
6 #include <TChainElement.h>
7 #include <TFriendElement.h>
8 #include <TBranch.h>
9 #include <TLeaf.h>
10 #include <TList.h>
11 
13 
14 QChain::QChain() : TChain(QTREE_NAME)
15 {
16  fSoftwareRevision = "";
17  fVersionTag = "";
18  fAllLeaves = 0;
19  fAllBranches = 0;
20  fAllAliases = 0;
21 }
22 
23 QChain::QChain(const char* name) : TChain(name)
24 {
25  fSoftwareRevision = "";
26  fVersionTag = "";
27  fAllLeaves = 0;
28  fAllBranches = 0;
29  fAllAliases = 0;
30 }
31 
33 {
34  for(size_t f = 0; f < fFriends.size(); f++) {
35  delete fFriends[f];
36  }
37  ResetLists();
38 }
39 
41 {
42  if(fAllLeaves) delete fAllLeaves;
43  if(fAllBranches) delete fAllBranches;
44  if(fAllAliases) delete fAllAliases;
45  fAllLeaves = 0;
46  fAllBranches = 0;
47  fAllAliases = 0;
48 }
49 
50 Int_t QChain::Add(const char* name, Long64_t nentries)
51 {
52 
53  int res = 0;
54  std::string sname(name);
55 
56  bool firstCall = false;
57  if(GetNtrees() == 0) firstCall = true;
58 
59  std::string fileExtension = "";
60  std::string inFile = name;
61  size_t dotpos = inFile.find_last_of(".");
62  if(dotpos != std::string::npos)
63  fileExtension = inFile.substr( dotpos+1 );
64  std::list<std::string> list;
65  if(fileExtension == "list") {
66  list = QFileList::Read(name,"");
67  std::list<std::string>::const_iterator iter = list.begin();
68  while(iter != list.end()) {
69  res = TChain::Add(iter->c_str(),kBigNumber);
70  iter++;
71  }
72  } else {
73  res = TChain::Add(name,kBigNumber);
74  }
75 
76  if(firstCall && res) {
77  // get the tree independently otherwise tchain crashes if
78  // we get entries on main chain before adding friends
79  std::string filename=
80  ((TChainElement *)GetListOfFiles()->First())->GetTitle();
81 
82  TFile* file0 = new TFile(filename.c_str());
83  if(!file0 || file0->IsZombie()) return res;
84 
85  QTree *tree=(QTree *)(file0->GetObjectChecked(GetName(),"QTree"));
86 
87  if(!tree) return res;
89  fVersionTag = tree->GetVersionTag();
90 
91  const char* pulserPath = tree->GetAlias("IsHeater");
92  // change alias IsHeater into IsPulser
93  if(pulserPath) {
94  this->SetAlias("IsPulser",pulserPath);
95  }
96 
97  TList* friends = tree->GetListOfFriends();
98  if(friends) {
99  TIter nextf(friends);
100  while(TFriendElement* afriend = (TFriendElement*)nextf()) {
101  QTree* qfriend = dynamic_cast<QTree*>(afriend->GetTree());
102  QChain* fchain = new QChain(qfriend->GetName());
103  fchain->Add(name);
104  fchain->fSoftwareRevision = qfriend->GetSoftwareRevision();
105  fchain->fVersionTag = qfriend->GetVersionTag();
106  fFriends.push_back(fchain);
107  this->AddFriend(qfriend->GetName());
108  }
109  }
110  delete file0;
111 
112  } else if(!fFriends.empty()) {
113  // add new friend trees to friend chains
114  for(size_t f = 0; f < fFriends.size(); f++) {
115  fFriends[f]->Add(name);
116  }
117  }
118  ResetLists();
119  return res;
120 }
121 
123 {
124  // if(fAllLeaves) return fAllLeaves;
125  if(fAllLeaves) delete fAllLeaves;
126  if(!this->GetListOfLeaves()) return 0;
127 
128  fAllLeaves = new TObjArray();
129  fAllLeaves->SetOwner(0);
130 
131  TIter nexta(this->GetListOfLeaves());
132  while(TObject* leaf = nexta()) {
133  fAllLeaves->AddLast(leaf);
134  }
135 
136  for(size_t f = 0; f < fFriends.size(); f++) {
137  TObjArray* fleaves = fFriends[f]->GetListOfLeaves();
138  if(!fleaves) continue;
139  TIter nextb(fleaves);
140  while(TLeaf* fleaf = (TLeaf*)nextb()) {
141  fAllLeaves->AddLast(fleaf);
142  }
143  }
144  return fAllLeaves;
145 }
146 
147 
149 {
150  if(fAllAliases) return fAllAliases;
151 
152  if(!this->GetTree()) this->LoadTree(0);
153  if(!this->GetTree()) return 0;
154 
155  TList* aliases = this->GetTree()->GetListOfAliases();
156  if(!aliases) return 0;
157 
158  fAllAliases = new TList;
159  fAllAliases->SetOwner(1);
160  TIter nexta(aliases);
161  while(TObject* alias = nexta()) {
162  fAllAliases->Add(alias->Clone());
163 
164  }
165  for(size_t f = 0; f < fFriends.size(); f++) {
166  if(!fFriends[f]->GetTree()) fFriends[f]->LoadTree(0);
167  if(!fFriends[f]->GetTree()) continue;
168  TList* faliases = fFriends[f]->GetTree()->GetListOfAliases();
169  if(!faliases) continue;
170  TIter nextb(faliases);
171  while(TObject* falias = nextb()) {
172  fAllAliases->Add(falias->Clone());
173  }
174  }
175  return fAllAliases;
176 }
177 
179 {
180  //if(fAllBranches) return fAllBranches;
181  if(fAllBranches) delete fAllBranches;
182  if(!this->GetListOfBranches()) return 0;
183 
184  fAllBranches = new TObjArray();
185  fAllBranches->SetOwner(0);
186 
187  TIter nexta(this->GetListOfBranches());
188  while(TObject* branch = nexta()) {
189  fAllBranches->AddLast(branch);
190  }
191  for(size_t f = 0; f < fFriends.size(); f++) {
192  TObjArray* fbranches = fFriends[f]->GetListOfBranches();
193  if(!fbranches) continue;
194  TIter nextb(fbranches);
195  while(TBranch* fbranch = (TBranch*)nextb()) {
196  fAllBranches->AddLast(fbranch);
197  }
198  }
199  return fAllBranches;
200 }
201 
202 
#define QTREE_NAME
Definition: QBaseTree.hh:18
ClassImp(QChain) QChain
Definition: QChain.cc:12
const std::string & GetVersionTag()
Definition: QBaseTree.hh:45
const std::string & GetSoftwareRevision()
Definition: QBaseTree.hh:43
TChain used in diana.
Definition: QChain.hh:23
std::string fSoftwareRevision
software version of first qtree
Definition: QChain.hh:59
TObjArray * GetListOfAllBranches()
Definition: QChain.cc:178
TObjArray * fAllBranches
list of all branches
Definition: QChain.hh:69
TList * fAllAliases
list of all aliases
Definition: QChain.hh:72
virtual ~QChain()
destructor
Definition: QChain.cc:32
TList * GetListOfAllAliases()
Definition: QChain.cc:148
void ResetLists()
Definition: QChain.cc:40
std::vector< QChain * > fFriends
friend QChains
Definition: QChain.hh:63
Int_t Add(const char *filename, Long64_t nentries=kBigNumber)
Definition: QChain.cc:50
std::string fVersionTag
Definition: QChain.hh:60
TObjArray * fAllLeaves
list of all leaves
Definition: QChain.hh:66
QChain()
constructor
TObjArray * GetListOfAllLeaves()
Definition: QChain.cc:122
void Read(const std::string &fileListPath)
Definition: QFileList.cc:67
TTree used in diana.
Definition: QTree.hh:19