SummarySet.cxx
1 #include "SummarySet.h"
2 
3 #include <stdlib.h>
4 #include <iostream>
5 
6 #include "TChain.h"
7 #include "TH2D.h"
8 #include "TFile.h"
9 #include "TProof.h"
10 #include "TCanvas.h"
11 #include "TSeqCollection.h"
12 #include "TChainElement.h"
13 #include "TROOT.h"
14 
15 #include "AnitaEventSummary.h"
16 
17 #include "SummarySelector.h"
18 #include "ProgressBar.h"
19 #include "TH2DAntarctica.h"
20 #include "TGraphAntarctica.h"
21 #include "RootTools.h"
22 
23 
24 
25 Acclaim::SummarySet::SummarySet(const char* pathToSummaryFiles, const char* treeName, const char* summaryBranchName, bool useProof)
26  : fPathToSummaryFiles(pathToSummaryFiles), fTreeName(treeName), fSummaryBranchName(summaryBranchName),
27  fChain(NULL), fSum(NULL), fFirstTime(0), fFirstEventNumber(0), fLastTime(0), fLastEventNumber(0),
28  fUseProof(useProof), fProof(NULL), fBuiltIndex(false), fFlagChain(NULL), fFlags(NULL), fFlagEventNumber(0),
29  fDrawOutput(NULL)
30 {
31 
32  init();
33 }
34 
35 
36 
37 Acclaim::SummarySet::~SummarySet(){
38 
39  if(fProof){
40  delete fProof;
41  fProof = NULL;
42  }
43 
44  delete fChain;
45  fChain = NULL;
46 
47  if(fSum){
48  delete fSum;
49  }
50  fSum = NULL;
51 
52  if(gFile && gFile->IsWritable()){
53  TString name = "";
54  TString title = "Acclaim::SummarySet ss(\"" + fPathToSummaryFiles + "\", \"" + fTreeName + "\", \"" + fSummaryBranchName + "\")";
55  TNamed note(name, title);
56  note.Write();
57  }
58 }
59 
60 
62  if(gProof){
63  std::cerr << "Warning in " << __PRETTY_FUNCTION__
64  << " won't start new PROOF session if one is already running!"
65  << std::endl;
66  return gProof;
67  }
68 
69  gProof = TProof::Open("");
70  const char* anitaUtilInstallDir = getenv("ANITA_UTIL_INSTALL_DIR");
71  TString loadAnita = TString::Format("%s/share/Acclaim/loadAnita.C", anitaUtilInstallDir);
72  gProof->Load(loadAnita);
73  std::cout << "Info in " << __PRETTY_FUNCTION__ << ", started PROOF!" << std::endl;
74  return gProof;
75 }
76 
77 
82 
83  if(fUseProof && !fProof){
84  fProof = startProof();
85  }
86  if(fChain){
87  fChain->SetProof(fUseProof);
88  }
89 }
90 
91 
92 Long64_t Acclaim::SummarySet::Process(TSelector* selector, Option_t* option, Long64_t nentries, Long64_t firstentry){
93  Long64_t retVal = -1;
94  if(fChain){
95  initProof();
96  retVal = fChain->Process(selector, option, nentries, firstentry);
97  }
98  return retVal;
99 }
100 
101 
102 void Acclaim::SummarySet::addFlagChain(const char* flagFileGlob, const char* flagTreeName){
103 
104  if(flagFileGlob){
105  if(fFlagChain){
106  delete fFlagChain;
107  fFlagChain = NULL;
108  }
109 
110  fFlagChain = new TChain(flagTreeName);
111  fFlagChain->Add(flagFileGlob);
112 
113  fFlagChain->SetBranchAddress("eventNumber", &fFlagEventNumber);
114  fFlagChain->SetBranchAddress("flags", &fFlags);
115  }
116 }
117 
118 
119 void Acclaim::SummarySet::init(){
120 
121 
122  fChain = new TChain(fTreeName);
123  fChain->Add(fPathToSummaryFiles);
124  fN = fChain->GetEntries();
125  fChain->SetBranchAddress(fSummaryBranchName, &fSum);
126 
127  if(fN == 0){
128  std::cerr << "Warning in " << __PRETTY_FUNCTION__ << ", no entries in TChain of AnitaEventSummary" << std::endl;
129  }
130  else{
131  last();
132  fLastTime = fSum->realTime;
133  fLastEventNumber = fSum->eventNumber;
134 
135  first();
136  fFirstTime = fSum->realTime;
137  fFirstEventNumber = fSum->eventNumber;
138 
139  // std::cerr << "Just called " << __PRETTY_FUNCTION__ << ", time range is " << fFirstTime << " to " << fLastTime << "\t eventNumber range is " << fFirstEventNumber << " to " << fLastEventNumber << std::endl;
140  }
141 }
142 
143 
144 
145 
146 
153 
154  Double_t totalSize = 0;
155 
156  if(fChain){
157  TObjArray* fileElements = fChain->GetListOfFiles();
158 
159  TIter next(fileElements);
160  TChainElement* chEl = NULL;
161 
162  while ((chEl = (TChainElement*) next())){
163  TFile f(chEl->GetTitle());
164  totalSize += f.GetSize();
165  }
166  }
167  return totalSize;
168 }
169 
170 
171 
179 Long64_t Acclaim::SummarySet::getEntry(Long64_t entry){
180  Long64_t nb = fChain->GetEntry(entry);
181 
182  if(fFlagChain){
183  fFlagChain->GetEntry(entry);
184 
185  if(fFlagEventNumber!=fSum->eventNumber){
186  std::cerr << "Error in " << __PRETTY_FUNCTION__ << ", fSum->eventNumber = " << fSum->eventNumber
187  << ", but fFlagEventNumber = " << fFlagEventNumber << "!" << std::endl;
188  }
189 
190  // std::cout << fFlags->topPower[0] << "\t" << fSum->flags.topPower[0] << "\t" << std::endl;
191 
192  fSum->flags = *fFlags;
193  }
194 
195 
196  return nb;
197 }
198 
199 
200 
208 Long64_t Acclaim::SummarySet::getEvent(UInt_t eventNumber){
209  if(!fBuiltIndex){
210  fChain->BuildIndex("eventNumber");
211  fBuiltIndex = true;
212  }
213 
214  Long64_t entry = fChain->GetEntryNumberWithIndex(eventNumber);
215  return getEntry(entry);
216 }
217 
218 
219 
220 
221 
222 
223 
224 TProfile2DAntarctica* Acclaim::SummarySet::makeAntarcticaProf(AnitaPol::AnitaPol_t pol, const char* name, const char* title, Int_t nx, Int_t ny){
225 
226  TProfile2DAntarctica* prof = new TProfile2DAntarctica(name, title, nx, ny);
227 
228  Bool_t doPol[AnitaPol::kNotAPol] = {pol == AnitaPol::kHorizontal || pol == AnitaPol::kNotAPol,
229  pol == AnitaPol::kVertical || pol == AnitaPol::kNotAPol};
230 
231  ProgressBar p(N());
232  for(Long64_t entry=0; entry < N(); entry++){
233  getEntry(entry);
234  AnitaEventSummary* sum = summary();
235 
236  for(int polInd=0; polInd < AnitaPol::kNotAPol; polInd++){
237  if(doPol[polInd]){
238  // for(int peakInd=0; peakInd < sum->nPeaks[polInd]; peakInd++){
239  for(int peakInd=0; peakInd < sum->nPeaks[polInd]; peakInd++){
240 
241  // TODO consider removing theta < 0 once traceBackTocontinent is fixed.
242  if(sum->peak[polInd][peakInd].theta < 0 && sum->peak[polInd][peakInd].longitude > -9999 && sum->peak[polInd][peakInd].latitude > -9999){
243  // std::cerr << entry << "\t" << polInd << "\t" << peakInd << "\t"
244  // << sum->peak[polInd][peakInd].longitude << "\t"
245  // << sum->peak[polInd][peakInd].latitude << "\t"
246  // << sum->peak[polInd][peakInd].value << std::endl;
247  prof->Fill(sum->peak[polInd][peakInd].longitude,
248  sum->peak[polInd][peakInd].latitude,
249  sum->peak[polInd][peakInd].value);
250 
251  }
252  }
253  }
254  }
255  p.inc(entry, N());
256  }
257  return prof;
258 }
259 
260 
261 
262 TH2DAntarctica* Acclaim::SummarySet::makeAntarcticaHist(AnitaPol::AnitaPol_t pol, const char* name, const char* title, Int_t nx, Int_t ny){
263 
264  TH2DAntarctica* hist = new TH2DAntarctica(name, title, nx, ny);
265 
266  Bool_t doPol[AnitaPol::kNotAPol] = {pol == AnitaPol::kHorizontal || pol == AnitaPol::kNotAPol,
267  pol == AnitaPol::kVertical || pol == AnitaPol::kNotAPol};
268 
269  ProgressBar p(N());
270  for(Long64_t entry=0; entry < N(); entry++){
271  getEntry(entry);
272  AnitaEventSummary* sum = summary();
273 
274  for(int polInd=0; polInd < AnitaPol::kNotAPol; polInd++){
275  if(doPol[polInd]){
276  for(int peakInd=0; peakInd < sum->nPeaks[polInd]; peakInd++){
277 
278  // TODO consider removing theta < 0 once traceBackTocontinent is fixed.
279  if(sum->peak[polInd][peakInd].theta < 0 && sum->peak[polInd][peakInd].longitude > -9999 && sum->peak[polInd][peakInd].latitude > -9999){
280  // std::cerr << entry << "\t" << polInd << "\t" << peakInd << "\t"
281  // << sum->peak[polInd][peakInd].longitude << "\t"
282  // << sum->peak[polInd][peakInd].latitude << "\t"
283  // << sum->peak[polInd][peakInd].value << std::endl;
284  hist->Fill(sum->peak[polInd][peakInd].longitude,
285  sum->peak[polInd][peakInd].latitude,
286  sum->peak[polInd][peakInd].value);
287 
288  }
289  }
290  }
291  }
292  p.inc(entry, N());
293  }
294  return hist;
295 }
296 
297 
298 
299 
300 
306 void Acclaim::SummarySet::findHist(const char* varexp){
307 
308  std::vector<TString> tokens;
309  RootTools::tokenize(tokens, varexp, ">>");
310 
311 
312  TString histName = "htemp";
313 
314  if(tokens.size()>0){
315  UInt_t i = tokens.size() > 1 ? 1 : 0;
316  std::vector<TString> tokens2;
317  RootTools::tokenize(tokens2, tokens[i], "(");
318 
319  histName = tokens2.at(0);
320  // std::cout << histName << std::endl;
321  }
322 
323  TObject* obj = gROOT->FindObject(histName);
324  if(obj){
325  if(fDrawOutput){
326  delete fDrawOutput;
327  fDrawOutput = NULL;
328  }
329  fDrawOutput = obj->Clone();
330  }
331 }
332 
333 
334 
335 
341 void Acclaim::SummarySet::renameProofCanvas(const char* varexp){
342 
343  if(gPad){
344  TCanvas* c = gPad->GetCanvas();
345  TString canName = c->GetName();
346  TString command = varexp;
347  TString histName = "htemp";
348  if(command.Contains(">>")){ // then we have a histogram name, let's go get it
349  std::vector<TString> tokens;
350  RootTools::tokenize(tokens, varexp, ">>");
351 
352  if(tokens.size()>0){
353  UInt_t i = tokens.size() > 1 ? 1 : 0;
354  std::vector<TString> tokens2;
355  RootTools::tokenize(tokens2, tokens[i], "(");
356 
357  if(tokens2.size() > 0){
358  histName = tokens2[0];
359  }
360  }
361  }
362 
363  // for some reason, proof does this...
364  // and if I want the histogram on the command line, I need to rename the canvas
365  if(canName==histName){
366  TString newCanName = RootTools::nextCanvasName();
367  c->SetName(newCanName);
368  c->SetTitle(newCanName);
369  }
370  }
371 }
372 
373 
374 Long64_t Acclaim::SummarySet::Draw(const char* varexp, const TCut &selection, Option_t *option, Long64_t nentries, Long64_t firstentry){
375  initProof();
376 
377  ProgressBar p(1);
378  Long64_t retVal = fChain->Draw(varexp, selection, option, nentries, firstentry);
379 
380  if(fUseProof){
381  renameProofCanvas(varexp);
382  }
383  findHist(varexp);
384 
385  p++;
386  return retVal;
387 
388 }
389 
390 Long64_t Acclaim::SummarySet::Draw(const char* varexp, const char* selection, Option_t* option, Long64_t nentries, Long64_t firstentry){
391  TCut cut = selection;
392  return Draw(varexp, cut, option, nentries, firstentry);
393 }
394 
395 
396 
397 
398 TH2D* Acclaim::SummarySet::bookTimeHistogram(const char* name, const char* title, int nx, int ny, double yMin, double yMax){
399  TH2D* h = new TH2D(name, title, nx, getFirstTime(), getLastTime(), ny, yMin, yMax);
400  return h;
401 }
402 
403 
404 TH2D* Acclaim::SummarySet::bookEventNumberHistogram(const char* name, const char* title, int nx, int ny, double yMin, double yMax){
405  TH2D* h = new TH2D(name, title, nx, getFirstEventNumber(), getLastEventNumber(), ny, yMin, yMax);
406  return h;
407 }
408 
409 
410 
419 
421  Long64_t n = N();
422  for(Long64_t entry=0; entry < n; entry+=stride){
423  getEntry(entry);
424  AnitaEventSummary* sum = summary();
425  gr->SetPoint(gr->GetN(), sum->anitaLocation.longitude, sum->anitaLocation.latitude);
426  }
427  return gr;
428 }
429 
static TProof * startProof()
You take it, you own it, you delete it.
Definition: SummarySet.cxx:61
void renameProofCanvas(const char *varexp)
Definition: SummarySet.cxx:341
USeful in for loops.
Long64_t getEntry(Long64_t entry)
Definition: SummarySet.cxx:179
Prints a progress bar and timer to stderr.
Definition: ProgressBar.h:25
Long64_t getEvent(UInt_t eventNumber)
Definition: SummarySet.cxx:208
TGraphAntarctica * makePayloadLocationGraph(int stride=TGraphAntarctica::defaultGpsTreeStride)
Definition: SummarySet.cxx:418
Vertical Polarisation.
Common analysis format between UCorrelator and Acclaim.
Horizontal Polarisation.
void findHist(const char *varexp)
Definition: SummarySet.cxx:306
virtual void SetPoint(Int_t i, Double_t lon, Double_t lat)
enum AnitaPol::EAnitaPol AnitaPol_t
Polarisation enumeration.
Double_t getTotalSize() const
Definition: SummarySet.cxx:152