SummarySelector.cxx
1 #include "SummarySelector.h"
2 
3 #include <TH2.h>
4 #include <TStyle.h>
5 #include "TCanvas.h"
6 #include "DrawStrings.h"
7 #include "TTreeFormula.h"
8 #include "TMath.h"
9 #include "TTreeFormulaManager.h"
10 
11 ClassImp(Acclaim::SummarySelector);
12 
13 
21  : TSelector(), fChain(NULL),
22  fCuts(new TList), fCutFormulas(NULL),
23  // fCutTreeName("cutResultTree"), fAnalysisCutTree(NULL),
24  // fCutReturns(), fDoAnalysisCutTree(true),
25  fDemoForm(NULL),
26  fDemoHist(NULL), fDoDemoHist(false)
27  // fEventNumber(0), fRun(0), fWeight(0)
28 {
29  fInput = new TList;
30  fCuts->SetName("fCuts");
31 }
32 
33 
34 
39 {
40 
41 }
42 
43 
44 
50 void Acclaim::SummarySelector::addCut(const TCut* analysisCut)
51 {
52  fCuts->Add(const_cast<TCut*>(analysisCut));
53 }
54 
55 
56 
57 
69 {
70 
71  fTree = tree;
72 
73  if(!fCutFormulas){
74  fCutFormulas = new TList;
75  }
76  fCutFormulas->SetOwner(true);
77  fCutFormulas->Delete();
78 
79  if(fCutFormulas->GetEntries()==0){
80  const int nForm = fCuts->GetEntries();
81  fCutReturns.resize(nForm, std::vector<Int_t>());
82 
83  fMaxNdata = 1;
84 
85  for(UInt_t fInd=0; fInd < nForm; fInd++){
86  const TCut* eventSelection = dynamic_cast<const TCut*>(fCuts->At(fInd));
87 
88  TString formName = TString::Format("cutForm_%s", eventSelection->GetName());
89  TTreeFormula* f = new TTreeFormula(formName, eventSelection->GetTitle(), fTree);
90  fCutFormulas->Add(f);
91 
92  if(f->GetNdata() > fMaxNdata){
93  fMaxNdata = f->GetNdata();
94  }
95  // fManager->Add(f);
96  }
97 
98  for(UInt_t fInd=0; fInd < fCutReturns.size(); fInd++){
99  fCutReturns.at(fInd).resize(fMaxNdata, false);
100  }
101  fCumulativeCutReturns.resize(fMaxNdata, false);
102 
103 
104  if(fDoDemoHist){
105  fDemoForm = new TTreeFormula("demo_form", "run", fTree); // only has 1 data...
106  }
107  }
108  // else{
109  // // std::cout << tree->GetName() << std::endl;
110  // // tree->Show(0);
111 
112  // TIter next(fCutFormulas);
113  // while(TTreeFormula* form = dynamic_cast<TTreeFormula*>(next())){
114  // form->SetTree(tree);
115  // // form->UpdateFormulaLeaves();
116  // }
117 
118  // if(fDemoForm){
119  // fDemoForm->SetTree(tree);
120  // // fDemoForm->UpdateFormulaLeaves();
121  // }
122  // }
123 }
124 
125 
126 
127 
128 
129 
130 
131 
132 Bool_t Acclaim::SummarySelector::Notify()
133 {
134  TIter next(fCutFormulas);
135  while(TTreeFormula* form = dynamic_cast<TTreeFormula*>(next())){
136  form->Notify();
137  }
138 
139  return kTRUE;
140 }
141 
142 
148 void Acclaim::SummarySelector::Begin(TTree * /*tree*/)
149 {
150  // TString option = GetOption();
151 
152  fInput->Add(fCuts);
153 
154  if(fDoDemoHist){
155  fInput->Add(new TNamed("fDoDemo", "hDemo"));
156  }
157 
158 
159 }
160 
161 
168 {
169  fCuts = dynamic_cast<TList*>(fInput->FindObject("fCuts"));
170 
171  TNamed* n = dynamic_cast<TNamed*>(fInput->FindObject("fDoDemo"));
172  fDoDemoHist = n ? true : false;
173  if(fDoDemoHist){
174  fDemoHist = new TH1D("hDemo", "Demo histogram - run; run; number of events", 310, 130, 440);
175  fOutput->Add(fDemoHist);
176  }
177 
178 
179 }
180 
181 
204 Bool_t Acclaim::SummarySelector::Process(Long64_t entry)
205 {
206 
207  // Long64_t readEntry = fTree->LoadTree(entry);
208  // std::cout << "On entry " << entry << ", got readEntry " << readEntry << std::endl;
209 
210  fTree->LoadTree(entry);
211 
212  Bool_t matchesSelection = true;
213  for(int i=0; i < fMaxNdata; i++){
214  fCumulativeCutReturns[i] = true;
215  }
216 
217 
218 
219  TIter next(fCutFormulas);
220  int fInd=0;
221  while (TObject* obj = next()){
222  TTreeFormula* form = dynamic_cast<TTreeFormula*>(obj);
223 
224  bool anyIterationsPass = false;
225  for(int i=0; i < fMaxNdata; i++){
226  Float_t cutVal = form->GetNdata() > 1 && i < form->GetNdata() ? form->EvalInstance(i) : form->EvalInstance();
227  fCutReturns.at(fInd).at(i) = cutVal;
228 
229  bool thisPass = cutVal > 0;
230 
231  fCumulativeCutReturns.at(i) = bool(fCumulativeCutReturns.at(i)) && thisPass;
232 
233  anyIterationsPass = anyIterationsPass || fCumulativeCutReturns.at(i);
234  }
235 
236  // std::cout << fInd << "\t" << form->GetTitle() << std::endl;
237  // for(int i=0; i < fMaxNdata; i++){
238  // // std::cout << fInd << "\t" << i << std::endl;
239  // std::cout << "(" << i << " : " << fCutReturns.at(fInd).at(i) << "), ";
240  // }
241  // std::cout << std::endl;
242  // for(int i=0; i < fMaxNdata; i++){
243  // std::cout << i << std::endl;
244  // std::cout << fCumulativeCutReturns.at(i) << "\t";
245  // }
246 
247  // std::cout << "\n" << std::endl;
248  // std::cout << anyIterationsPass << "\t" << matchesSelection << std::endl;
249 
250  matchesSelection = matchesSelection && anyIterationsPass;
251 
252  // we can break out of the loop early if we're not storing the results
253  // of all the cuts, and the selection doesn't match all the cuts
254  // if(!fDoAnalysisCutTree && !matchesSelection){
255 
256  fInd++;
257  if(!matchesSelection){
258  break;
259  }
260  }
261 
262  // if doing the demo hist, then fill event passes the cut
263  if(matchesSelection && fDemoHist){
264  fDemoHist->Fill(fDemoForm->EvalInstance());
265  }
266 
267  // // If storing the analysis cut result, then update
268  // if(fDoAnalysisCutTree && fAnalysisCutTree){
269  // fEventNumber = fSum->eventNumber;
270  // fRun = fSum->run;
271  // fWeight = fSum->weight();
272  // fAnalysisCutTree->Fill();
273  // }
274 
275  return matchesSelection;
276 }
277 
278 
285 {
286  // fInput->Remove(fCuts);
287 
288  // fCuts->Clear(); // don't delete globals
289  // delete fCuts;
290  // fCuts = NULL;
291 }
292 
293 
294 
301 {
302 
303  if(fDoDemoHist){
304  fDemoHist = dynamic_cast<TH1D *>(fOutput->FindObject("hDemo"));
305  if (fDemoHist) {
306  TCanvas *c1 = new TCanvas();
307  fDemoHist->Draw("hist");
308 
309  // Final update
310  c1->cd();
311  c1->Update();
312  }
313  else {
314  Warning("Terminate", "histogram not found");
315  }
316  }
317 }
virtual Bool_t Process(Long64_t entry)
Reads the AnitaEventSummary TTree entry and sets the fSum pointer. Cycles through the fCuts...
SummarySelector(const char *sumBranchName="sum")
Set this to true to generate and fill fDemoHist.
virtual void Begin(TTree *tree)
TList * fCuts
Which TTree formula determines the peak direction?
Template TSelector to be inherited from, designed for use with SummarySet.
virtual void Init(TTree *tree)
void addCut(const TCut *analysisCut)
virtual void SlaveBegin(TTree *tree)