CutTreeSelector.cxx
1 #include "CutTreeSelector.h"
2 #include "TProofOutputFile.h"
3 #include "TTreeFormula.h"
4 #include "DrawStrings.h"
5 #include "CutOptimizer.h"
6 #include "TList.h"
7 
8 
15 Acclaim::CutTreeSelector::CutTreeSelector(const char* outFileName, const char* treeName)
16  : fOutTree(NULL), fProofOutFile(NULL), fOutFileName("fOutFileName", outFileName), fTreeName("fTreeName", treeName),
17  fFormulaStrings(new TList), fFormulas(NULL), fIterationFormula()
18 {
19 
20  fFormulaStrings->SetName("fFormulaStrings");
21  fDoDemoHist = false;
22 }
23 
24 
30 {
31 
32  SummarySelector::Notify();
33  TIter next(fFormulas);
34  while(TTreeFormula* form = dynamic_cast<TTreeFormula*>(next())){
35  form->Notify();
36  }
37 
38  return kTRUE;
39 }
40 
41 
49 void Acclaim::CutTreeSelector::setFormulaStrings(const std::vector<const char*>& formulaStrings)
50 {
51 
52  fFormulaStrings->Delete(); // clear and delete?
53 
54  std::vector<const char*>::const_iterator it = formulaStrings.begin();
55  for(; it != formulaStrings.end(); ++it){
56  fFormulaStrings->Add(new TObjString(*it));
57  }
58  fFormulaStrings->SetOwner();
59 }
60 
61 
66  SummarySelector::Begin(tree); // get the selection into memory also!
67  fInput->Add(&fOutFileName);
68  fInput->Add(&fTreeName);
69  fInput->Add(fFormulaStrings);
70 }
71 
72 
77 
78  fOutFileName = *(dynamic_cast<TNamed*>(fInput->FindObject("fOutFileName")));
79  fTreeName = *(dynamic_cast<TNamed*>(fInput->FindObject("fTreeName")));
80  fFormulaStrings = dynamic_cast<TList*>(fInput->FindObject("fFormulaStrings"));
81 
82  // https://root-forum.cern.ch/t/proof-tree-merging/8097/2
83  // https://root.cern.ch/handling-large-outputs-root-files
84  fProofOutFile = new TProofOutputFile(fOutFileName.GetTitle(), TProofOutputFile::kMerge);
85  fOutput->Add(fProofOutFile);
86 
87  fOut = fProofOutFile->OpenFile("recreate");
88  fOutTree = new TTree(fTreeName.GetTitle(), fTreeName.GetTitle());
89 
90  fFormulas = new TList();
91  fFormulas->SetOwner(true);
92 
93  const size_t nForm = fFormulaStrings->GetEntries();
94  fIntVals.resize(nForm, 0);
95  fFloatVals.resize(nForm, 0);
96  fFormulaReturnTypes.resize(nForm, 0);
97 
98  SummarySelector::SlaveBegin(tree); // Optional analysisCutTree needs to be booked after TProofOutputFile
99 }
100 
101 
102 
109 
110  SummarySelector::Init(tree);
111 
112  fFormulas->SetOwner(true);
113  fFormulas->Delete();
114 
115  if(fFormulas->GetEntries()==0){
116 
117  const size_t nForm = fFormulaStrings->GetEntries();
118  TObjArray* fOutBranchList = fOutTree->GetListOfBranches();
119  fIterationFormula.resize(nForm, false);
120 
121  for(UInt_t fInd=0; fInd < nForm; fInd++){
122  const TString& formula = dynamic_cast<TObjString*>(fFormulaStrings->At(fInd))->String();
123 
124  TString formName = TString::Format("form%u", fInd);
125  TTreeFormula* f = new TTreeFormula(formName, formula, tree);
126  fFormulas->Add(f);
127 
128  // std::cout << fInd << "\t" << formula << "\t" << f->GetNdata() << std::endl;
129 
130  if(formula.Contains("Iteration$")){
131  fIterationFormula[fInd] = true;
132  // std::cout << "the iteration formula index is " << fIterationFormula << std::endl;
133  }
134 
135  if(fOutBranchList->GetEntries() < (Int_t)nForm){
136  TString bName = CutOptimizer::branchifyName(formula);
137 
138  fFormulaReturnTypes.at(fInd) = f->IsInteger();
139  if(fFormulaReturnTypes.at(fInd) > 0){
140  fOutTree->Branch(bName, &fIntVals.at(fInd));
141  }
142  else{
143  fOutTree->Branch(bName, &fFloatVals.at(fInd));
144  }
145  }
146  }
147 
148  // if(fIterationFormula==-1){
149  // std::cout << "the iteration formula index is " << fIterationFormula << std::endl;
150  // }
151  }
152  // else{
153  // std::cout << tree->GetName() << std::endl;
154  // tree->Show(0);
155  // TIter next(fFormulas);
156  // while(TTreeFormula* form = dynamic_cast<TTreeFormula*>(next())){
157  // form->SetTree(tree);
158  // // form->UpdateFormulaLeaves();
159  // }
160  // }
161 }
162 
163 
164 
165 
173 Bool_t Acclaim::CutTreeSelector::Process(Long64_t entry){
174 
175  Bool_t matchesSelection = SummarySelector::Process(entry);
176  if(matchesSelection){
177  int fInd=0;
178  TIter next(fFormulas);
179  while(TObject* obj = next()){
180  TTreeFormula* f = dynamic_cast<TTreeFormula*>(obj);
181 
182  fIntVals.at(fInd) = -9999;
183  fFloatVals.at(fInd) = -9999;
184 
185  bool doneAnIteration = false;
186 
187  //***************************************************************
188  // this necessary to force the TTree formula to load the data!!!!
189  f->EvalInstance();
190  //***************************************************************
191 
192  for(int i=0; i < fMaxNdata; i++){
193  if(!doneAnIteration && fCumulativeCutReturns.at(i) > 0){
194 
195  bool requireIteration = fIterationFormula[fInd] || (f->GetNdata() > 1 && i < f->GetNdata());
196  // std::cout << i << "\t" << fInd << "\t" << fCumulativeCutReturns.at(i) << "\t" << f->GetTitle() << "\t";
197 
198  if(fFormulaReturnTypes.at(fInd) > 0){
199  fIntVals.at(fInd) = requireIteration ? f->EvalInstance(i) : f->EvalInstance();
200  // std::cout << "(int) = " << fIntVals.at(fInd) << "\t" << requireIteration << std::endl;
201  }
202  else{
203  fFloatVals.at(fInd) = requireIteration ? f->EvalInstance(i) : f->EvalInstance();
204  // std::cout << "(flt) = " << fFloatVals.at(fInd) << "\t" << requireIteration << std::endl;
205  }
206  doneAnIteration = true;
207  }
208  }
209  // std::cout << fInd << "\t" << f->GetTitle() << "\t" << "the return vals are..." << std::endl;
210  fInd++;
211  }
212  // std::cout << "will fill those values... " << std::endl;
213  // std::cout << std::endl;
214 
215  fOutTree->Fill();
216  }
217 
218  return kTRUE;
219 }
220 
221 
222 
228 
230 
231  // fOutTree->Write();
232  // delete fOutTree;
233  // fOutTree = NULL;
234 
235  fOut->Write();
236  fOut->Close();
237  fOut = NULL;
238 
239  // fInput->Remove(fFormulas);
240  // fFormulas->Delete();
241  // delete fFormulas;
242  // fFormulas = NULL;
243 
244  fFormulaReturnTypes.clear();
245  fFloatVals.clear();
246  fIntVals.clear();
247 }
248 
249 
254 
255  // fInput->Remove(fFormulaStrings);
256  // fFormulaStrings->Delete();
257  // delete fFormulaStrings;
258  // fFormulaStrings = NULL;
259 
260  fProofOutFile = dynamic_cast<TProofOutputFile*>(fOutput->FindObject(fOutFileName.GetTitle()));
261  // std::cout << fProofOutFile << std::endl;
262  if(fProofOutFile){
263  TFile* f = fProofOutFile->OpenFile("read");
264  // std::cout << f << std::endl;
265  if(f){
266  TTree* t = dynamic_cast<TTree*>(f->Get(fTreeName.GetTitle()));
267  // std::cout << t << std::endl;
268  if(t){
269  std::cout << "Created " << t->GetName() << " in file " << f->GetName() << " has "
270  << t->GetEntries() << " entries..." << std::endl;
271  // t->Print();
272  }
273  f->Close();
274  }
275  }
276 }
void setFormulaStrings(const std::vector< const char *> &formulaStrings)
Set the formula strings to evaluate and put into the created output tree.
virtual Bool_t Process(Long64_t entry)
Reads the AnitaEventSummary TTree entry and sets the fSum pointer. Cycles through the fCuts...
CutTreeSelector(const char *outFileName="CutTreeSelector.root", const char *reducedSumTreeName="cutTree")
To force eval instance with an argument.
TList * fFormulaStrings
Where the formula results are written in the case of a int-like variable.
virtual void Begin(TTree *tree)
virtual void SlaveBegin(TTree *tree)
virtual void Begin(TTree *tree)
static TString branchifyName(const char *formStr)
**
virtual void Init(TTree *tree)
virtual void Init(TTree *tree)
virtual Bool_t Process(Long64_t entry)
bool fDoDemoHist
A histogram of peak[1][0].value.
virtual void SlaveBegin(TTree *tree)