Crombie Tools
TreeContainer.cc
Go to the documentation of this file.
1 /**
2  @file TreeContainer.cc
3  Defines the function members for the TreeContainer class.
4  @author Daniel Abercrombie <dabercro@mit.edu>
5 */
6 
7 #include <iostream>
8 
9 #include "TROOT.h"
10 #include "TSystem.h"
11 #include "TList.h"
12 #include "TSystemDirectory.h"
13 #include "TreeContainer.h"
14 
16 
17 //--------------------------------------------------------------------
18 TreeContainer::TreeContainer(TString fileName)
19 {
20  if (fileName != "") {
21  if (fileName.Contains(".root"))
22  AddFile(fileName);
23  else
24  AddDirectory(fileName);
25  }
26 }
27 
28 //--------------------------------------------------------------------
30 {
31  if (fTree)
32  delete fTree;
33 
34  for (UInt_t iFile = 0; iFile != fFileList.size(); ++iFile) {
35  if (fFileList[iFile]->IsOpen())
36  fFileList[iFile]->Close();
37  }
38 }
39 
40 //--------------------------------------------------------------------
41 void TreeContainer::AddFile(TString fileName)
42 {
43  tempFile = TFile::Open(fileName);
44  if (fPrinting)
45  std::cout << "File: " << tempFile << std::endl;
46 
47  if (tempFile != NULL) {
48  fFileList.push_back(tempFile);
49  fFileNames.push_back(fileName);
50  }
51 }
52 
53 //--------------------------------------------------------------------
54 void TreeContainer::AddDirectory(TString directoryName,TString searchFor)
55 {
56  TString tempName;
57  TSystemDirectory *dir = new TSystemDirectory(directoryName,directoryName);
58  TList *fileNameList = dir->GetListOfFiles();
59 
60  for (Int_t iFile = 0; iFile != fileNameList->GetEntries(); ++iFile) {
61  TNamed *tempMember = (TNamed*) fileNameList->At(iFile);
62  TString tempName = TString(tempMember->GetName());
63  if (tempName.Contains(searchFor)) {
64  if (fPrinting)
65  std::cout << "Opening " << tempName << std::endl;
66 
67  tempFile = TFile::Open(directoryName+"/"+tempName);
68  if (tempFile != NULL) {
69  fFileList.push_back(tempFile);
70  fFileNames.push_back(tempName); // Note this is different from AddFile in that it doesn't keep full filename
71  }
72  }
73  }
74  delete dir;
75 }
76 
77 //--------------------------------------------------------------------
78 TTree* TreeContainer::SkimTree(TTree *tree, TFile *inFile)
79 {
80  if (fKeepBranches.size() > 0) {
81  tree->SetBranchStatus("*",0);
82  for (UInt_t iBranch = 0; iBranch != fKeepBranches.size(); iBranch++)
83  tree->SetBranchStatus(fKeepBranches[iBranch],1);
84  }
85 
86  if (inFile)
87  inFile->cd();
88  else
89  gROOT->cd();
90 
91  TTree *outTree = 0;
92  if (fSkimmingCut == "")
93  outTree = tree->CloneTree(-1,"fast");
94  else
95  outTree = tree->CopyTree(fSkimmingCut);
96 
97  return outTree;
98 }
99 
100 //--------------------------------------------------------------------
101 TTree* TreeContainer::ReturnTree(TString Name, TFile* inFile)
102 {
103  if (fTree)
104  return fTree;
105 
106  if (Name != "")
107  SetTreeName(Name);
108 
109  TList *treeList = new TList;
110  ReturnTreeList();
111  for (UInt_t i0 = 0; i0 != fTreeList.size(); ++i0)
112  treeList->Add(fTreeList[i0]);
113 
114  if (inFile)
115  inFile->cd();
116  else
117  gROOT->cd();
118 
119  if (treeList->GetEntries() > 1)
120  fTree = TTree::MergeTrees(treeList,"fast");
121  else if (treeList->GetEntries() == 1)
122  fTree = tempTree;
123  else
124  fTree = NULL;
125 
126  delete treeList;
127  return fTree;
128 }
129 
130 //--------------------------------------------------------------------
131 std::vector<TTree*> TreeContainer::ReturnTreeList(TString Name)
132 {
133  if (fTreeList.size() != 0)
134  return fTreeList;
135 
136  if (Name != "")
137  SetTreeName(Name);
138 
139  for (UInt_t i0 = 0; i0 < fFileList.size(); i0++) {
140  if (fTreeName.Contains("/"))
141  tempTree = (TTree*) fFileList[i0]->Get(fTreeName);
142  else
143  tempTree = (TTree*) fFileList[i0]->FindObjectAny(fTreeName);
144 
145  if(fPrinting)
146  std::cout << "Getting " << fTreeName << " from " << fFileList[i0]->GetName() << std::endl;
147 
148  for (UInt_t i1 = 0; i1 < fFriendNames.size(); i1++) {
149  if (fFriendNames[i1].Contains("/"))
150  tempFriend = (TTree*) fFileList[i0]->Get(fFriendNames[i1]);
151  else
152  tempFriend = (TTree*) fFileList[i0]->FindObjectAny(fFriendNames[i1]);
153 
154  tempTree->AddFriend(tempFriend);
155  }
156 
157  if ((fSkimmingCut != "") || (fKeepBranches.size() != 0))
159 
160  fTreeList.push_back(tempTree);
161  }
162  return fTreeList;
163 }
164 
165 //--------------------------------------------------------------------
166 void TreeContainer::MakeFile(TString fileName, TString treeName)
167 {
168  if (fileName != "")
169  SetOutputFileName(fileName);
170 
171  if (treeName != "")
172  SetTreeName(treeName);
173 
174  TFile *outFile = new TFile(fOutputFileName,"RECREATE");
175  if (!fTree)
176  ReturnTree(fTreeName,outFile);
177 
178  outFile->cd();
179  fTree->Write();
180  outFile->Close();
181  delete outFile;
182 }