TimeDependentAverage.cc
1 #include "TimeDependentAverage.h"
2 #include "AnitaVersion.h"
3 #include "TFile.h"
4 #include "UCImageTools.h"
5 #include "TSystem.h"
6 #include "AnalysisWaveform.h"
7 #include "FilterStrategy.h"
8 #include "TH2.h"
9 #include "RawAnitaHeader.h"
10 #include "AnitaDataset.h"
11 #include "TCut.h"
12 #include "FilteredAnitaEvent.h"
13 #include "TMutex.h"
14 #include "BasicFilters.h"
15 #include <math.h>// for isnan
16 
17 static ALFAFilter alfa;
18 
19 
20 #define DEBUG_SPEC_AVG
21 
22 
23 UCorrelator::TimeDependentAverage::~TimeDependentAverage()
24 {
25 
26  for (int i = 0; i < NUM_SEAVEYS; i++)
27  {
28  for (int j = 0; j < 2; j++)
29  {
30  delete avgs[i][j];
31  delete avgs_minbias[i][j];
32  delete rms[i][j];
33  if (peakiness[i][j]) delete peakiness[i][j];
34  if (peakiness_minbias[i][j]) delete peakiness_minbias[i][j];
35 
36  }
37  }
38 
39  delete nblasts;
40  delete norms;
41  delete norms_minbias;
42 }
43 
44 int UCorrelator::TimeDependentAverage::computeAverage(double max_r, int min_norm, double max_power)
45 {
46  AnitaDataset d(run);
47 
48 
49 
50 
51  //figure out how long it is.
52 
53  d.getEntry(0);
54  double startTime = d.header()->triggerTime;
55  d.getEntry(d.N()-1);
56  double endTime = d.header()->triggerTime+1;
57 
58  int nbins = (endTime-startTime) / nsecs;
59 
60  TString name;
61  TString title;
62  name.Form("nblasts%d_%d",run,nsecs);
63  title.Form("N Blast Candidates run %d, nsec=%d", run,nsecs);
64  nblasts = new TH1I(name, title, nbins, startTime,endTime) ;
65  nblasts->SetDirectory(0);
66  nblasts->GetYaxis()->SetTitle("NBlasts");
67  nblasts->GetXaxis()->SetTitle("Time");
68  nblasts->GetXaxis()->SetTimeDisplay(true);
69  nblasts->GetXaxis()->SetTimeOffset(0);
70 
71  name.Form("norm_%d_%d",run,nsecs);
72  title.Form("RF Normalization run %d, nsec=%d", run,nsecs);
73 
74  norms = new TH1I(name, title,nbins,startTime,endTime);
75  norms->SetDirectory(0);
76  norms->GetYaxis()->SetTitle("NEvents");
77  norms->GetXaxis()->SetTitle("Time");
78  norms->GetXaxis()->SetTimeDisplay(true);
79  norms->GetXaxis()->SetTimeOffset(0);
80 
81  name.Form("norm_minbias_%d_%d",run,nsecs);
82  title.Form("MinBias Normalization run %d, nsec=%d", run,nsecs);
83 
84  norms_minbias = new TH1I(name, title,nbins,startTime,endTime);
85  norms_minbias->SetDirectory(0);
86  norms_minbias->GetYaxis()->SetTitle("NEvents");
87  norms_minbias->GetXaxis()->SetTitle("Time");
88  norms_minbias->GetXaxis()->SetTimeDisplay(true);
89  norms_minbias->GetXaxis()->SetTimeOffset(0);
90 
91 
92 // printf("%d\n", nbins);
93 
94  for (int ant = 0; ant < NUM_SEAVEYS; ant++)
95  {
96  for (int pol = 0; pol < 2; pol++)
97  {
98 
99  name.Form("specavg_%d_%d_%d_%d",run,nsecs,ant,pol);
100  title.Form("RF Spectrum Average ant=%d, pol=%d, run %d, nsec=%d", ant,pol,run,nsecs);
101 
102 
103  avgs[ant][pol] = new TH2F(name, title,
104  131,0,1.31,//TODO don't hardcode here
105  nbins, startTime,endTime) ;
106  avgs[ant][pol]->SetDirectory(0);
107  avgs[ant][pol]->GetYaxis()->SetTitle("Time");
108  avgs[ant][pol]->GetYaxis()->SetTimeDisplay(true);
109  avgs[ant][pol]->GetYaxis()->SetTimeOffset(0);
110  avgs[ant][pol]->GetXaxis()->SetTitle("Frequency");
111 
112 
113  name.Form("specavg_minbias_%d_%d_%d_%d",run,nsecs,ant,pol);
114  title.Form("MinBias Spectrum Average ant=%d, pol=%d, run %d, nsec=%d", ant,pol,run,nsecs);
115 
116  avgs_minbias[ant][pol] = new TH2F(name, title,
117  131,0,1.31,//TODO don't hardcode here
118  nbins, startTime,endTime) ;
119  avgs_minbias[ant][pol]->SetDirectory(0);
120  avgs_minbias[ant][pol]->GetYaxis()->SetTitle("Time");
121  avgs_minbias[ant][pol]->GetYaxis()->SetTimeDisplay(true);
122  avgs_minbias[ant][pol]->GetYaxis()->SetTimeOffset(0);
123  avgs_minbias[ant][pol]->GetXaxis()->SetTitle("Frequency");
124 
125  name.Form("rms_%d_%d_%d_%d",run,nsecs,ant,pol);
126  title.Form("MinBias RMS ant=%d, pol=%d, run %d, nsec=%d", ant,pol,run,nsecs);
127  rms[ant][pol] = new TH1D(name, title, nbins, startTime,endTime) ;
128  rms[ant][pol]->SetDirectory(0);
129  rms[ant][pol]->GetYaxis()->SetTitle("RMS (mVish)");
130  rms[ant][pol]->GetXaxis()->SetTitle("Time");
131  rms[ant][pol]->GetXaxis()->SetTimeDisplay(true);
132  rms[ant][pol]->GetXaxis()->SetTimeOffset(0);
133 
134 
135 
136 
137  }
138  }
139 
140 
141 
142 
143  FilterStrategy str;
144  if (AnitaVersion::get() == 3) str.addOperation(&alfa);
145 
146 
147  int N = d.N();
148 
149  N = d.N(); //For all the events.
150  for (int i = 0; i < N; i++)
151  {
152  d.getEntry(i);
153  FilteredAnitaEvent ev(d.useful(), &str, d.gps(), d.header());
154 
155  double t= d.header()->triggerTime;
156 
157  //cut out possible blasts. This is perhaps a bit aggressive, but it's worth it
158  double max_ratio_hpol, max_ratio_vpol;
159  ev.getMinMaxRatio(AnitaPol::kHorizontal, &max_ratio_hpol,0,0,0);
160  if (max_ratio_hpol > max_r || ev.getAveragePower(AnitaPol::kHorizontal) > max_power)
161  {
162  nblasts->Fill(t);
163  continue;
164  }
165  ev.getMinMaxRatio(AnitaPol::kVertical, &max_ratio_vpol,0,0,0);
166  if (max_ratio_vpol > max_r || ev.getAveragePower(AnitaPol::kVertical) > max_power)
167  {
168  nblasts->Fill(t);
169  continue;
170  }
171 
172  bool isRF = d.header()->trigType & 1;
173 
174  (isRF ? norms : norms_minbias)->Fill(t);
175  int tbin = norms->FindFixBin(t);
176 
177 #ifdef UCORRELATOR_OPENMP
178 #pragma omp parallel for
179 #endif
180  for (int j = 0; j < NUM_SEAVEYS * 2; j++)
181  {
182  int ant = j /2;
183  int pol = j %2;
184  const AnalysisWaveform * wf = ev.getFilteredGraph(ant,AnitaPol::AnitaPol_t(pol));
185 
186 
187  if (!isRF)
188  {
189  rms[ant][pol]->SetBinContent(tbin, rms[ant][pol]->GetBinContent(tbin) + wf->even()->GetRMS(2));
190  }
191 
192  const TGraphAligned * g = wf->power();
193  for (int k = 0; k<avgs[ant][pol]->GetNbinsX() ; k++)
194  {
195  TH2F * h = isRF ? avgs[ant][pol] : avgs_minbias[ant][pol];
196  h->SetBinContent(k,tbin,h->GetBinContent(k,tbin) + g->GetY()[k]);
197  }
198  }
199 
200  if (i % 50 == 0)
201  {
202  printf("\r%d/%d",i,N);
203  }
204  else {
205  printf(".");
206  fflush(stdout);
207  }
208 
209  }
210  printf("..Done\n");
211 
212 
213  for (int isRF = 0; isRF <=1; isRF++)
214  {
215  for (int j = 0; j < NUM_SEAVEYS * 2; j++)
216  {
217  int ant = j /2;
218  int pol = j %2;
219 
220  TH2F * h = isRF ? avgs[ant][pol] : avgs_minbias[ant][pol];
221  TH1I * hnorm = isRF ? norms : norms_minbias;
222 
223 
224  for (int ybin = 1; ybin <= h->GetNbinsY(); ybin++)
225  {
226  if (hnorm->GetBinContent(ybin) < min_norm)
227  {
228  continue;
229  }
230 
231  for (int k = 0; k<h->GetNbinsX() ; k++)
232  {
233  h->SetBinContent(k,ybin, h->GetBinContent(k,ybin) / hnorm->GetBinContent(ybin));
234  }
235 
236  if (!isRF)
237  {
238  rms[ant][pol]->SetBinContent(ybin, rms[ant][pol]->GetBinContent(ybin) / hnorm->GetBinContent(ybin));
239  }
240 
241  }
242 
243  //now go through and fill in the rows with no content with an average of ones that do
244  for (int ybin = 1; ybin <= h->GetNbinsY(); ybin++)
245  {
246 
247  if (hnorm->GetBinContent(ybin) < min_norm)
248  {
249  int last_bin = ybin-1;
250  while(hnorm->GetBinContent(last_bin) < min_norm && last_bin > 0) last_bin--;
251 
252  int next_bin = ybin+1;
253  while (!hnorm->GetBinContent(next_bin) && next_bin <= h->GetNbinsY()) next_bin++;
254 
255  //lost cause, no good bins
256  if (last_bin == 0 && next_bin > h->GetNbinsY()) break;
257 
258  if (last_bin == 0)
259  {
260  for (int k = 0; k<h->GetNbinsX() ; k++)
261  {
262  h->SetBinContent(k,ybin, h->GetBinContent(k,next_bin));
263  if (!isRF) rms[ant][pol]->SetBinContent(ybin, rms[ant][pol]->GetBinContent(next_bin));
264  }
265  }
266  else if (next_bin > h->GetNbinsY())
267  {
268  for (int k = 0; k<h->GetNbinsX() ; k++)
269  {
270  h->SetBinContent(k,ybin, h->GetBinContent(k,last_bin));
271  if (!isRF) rms[ant][pol]->SetBinContent(ybin, rms[ant][pol]->GetBinContent(last_bin));
272  }
273  }
274  else
275  {
276  double last_frac = double(ybin - last_bin) / (next_bin-last_bin);
277 
278  for (int k = 0; k<h->GetNbinsX() ; k++)
279  {
280  h->SetBinContent(k,ybin, last_frac * h->GetBinContent(k,last_bin) + (1-last_frac) * h->GetBinContent(k,next_bin));
281  if (!isRF) rms[ant][pol]->SetBinContent(ybin, last_frac * rms[ant][pol]->GetBinContent(last_bin) + (1-last_frac) * rms[ant][pol]->GetBinContent(next_bin));
282  }
283  }
284  }
285  }
286  }
287 
288  }
289 
290  return 0;
291 }
292 
293 
294 
296 {
297 
298  gSystem->mkdir(dir,true);
299 
300  TString fstr;
301  fstr.Form("%s/%d_%d.root", dir, run,nsecs);
302  TFile f(fstr.Data(),"RECREATE");
303  f.cd();
304 
305  //make sure averages and RMS are loaded
306  //getSpectrogram(AnitaPol::kHorizontal,0);
307  //getRMS(AnitaPol::kHorizontal,0);
308 
309  f.mkdir("specavg");
310  f.cd("specavg");
311 
312  for (int ant = 0; ant < NUM_SEAVEYS; ant++)
313  {
314  avgs[ant][0]->Write(TString::Format("h%d",ant));
315  avgs[ant][1]->Write(TString::Format("v%d",ant));
316  }
317 
318  f.cd();
319 
320  f.mkdir("specavg_minbias");
321  f.cd("specavg_minbias");
322  for (int ant = 0; ant < NUM_SEAVEYS; ant++)
323  {
324  avgs_minbias[ant][0]->Write(TString::Format("h%d",ant));
325  avgs_minbias[ant][1]->Write(TString::Format("v%d",ant));
326  }
327  f.cd();
328 
329  f.mkdir("rms");
330  f.cd("rms");
331  for (int ant = 0; ant < NUM_SEAVEYS; ant++)
332  {
333  rms[ant][0]->Write(TString::Format("h%d",ant));
334  rms[ant][1]->Write(TString::Format("v%d",ant));
335  }
336  f.cd();
337 
338  nblasts->Write("nblasts");
339  norms->Write("norms");
340  norms_minbias->Write("norms_minbias");
341 
342 
343 }
344 
345 const TH2F * UCorrelator::TimeDependentAverage::getSpectrogram(AnitaPol::AnitaPol_t pol, int ant, bool minbias) const
346 {
347 
348 
349  __sync_synchronize(); //memory barrier
350 
351  if (!avgs_loaded)
352  {
353  m.Lock();
354  if (!avgs_loaded)
355  {
356  TFile f(fname);
357  f.cd("specavg");
358  for (int ant = 0; ant < NUM_SEAVEYS; ant++)
359  {
360  TH2F * found_hpol = (TH2F*) gDirectory->Get(TString::Format("h%d",ant));
361  avgs[ant][0] = new TH2F(*found_hpol);
362  avgs[ant][0]->SetDirectory(0);
363 
364  TH2F * found_vpol = (TH2F*) gDirectory->Get(TString::Format("v%d",ant));
365  avgs[ant][1] = new TH2F(*found_vpol);
366  avgs[ant][1]->SetDirectory(0);
367  }
368 
369  f.cd("specavg_minbias");
370 
371  for (int ant = 0; ant < NUM_SEAVEYS; ant++)
372  {
373 
374  TH2F * found_hpol = (TH2F*) gDirectory->Get(TString::Format("h%d",ant));
375  avgs_minbias[ant][0] = new TH2F(*found_hpol);
376  avgs_minbias[ant][0]->SetDirectory(0);
377 
378  TH2F * found_vpol = (TH2F*) gDirectory->Get(TString::Format("v%d",ant));
379  avgs_minbias[ant][1] = new TH2F(*found_vpol);
380  avgs_minbias[ant][1]->SetDirectory(0);
381  }
382  __sync_synchronize(); //memory barrier
383  avgs_loaded = true;
384  }
385  m.UnLock();
386  }
387 
388 
389  return minbias ? avgs_minbias[ant][pol] : avgs[ant][pol];
390 }
391 
392 
393 const TH1D * UCorrelator::TimeDependentAverage::getRMS(AnitaPol::AnitaPol_t pol, int ant) const
394 {
395  __sync_synchronize(); //memory barrier
396  if (!rms_loaded)
397  {
398  m.Lock();
399 
400  if (!rms_loaded)
401  {
402  TFile f(fname);
403  f.cd("rms");
404 
405  for (int ant = 0; ant < NUM_SEAVEYS; ant++)
406  {
407 
408  TH1D * found_hpol = (TH1D*) gDirectory->Get(TString::Format("h%d",ant));
409  rms[ant][0] = new TH1D(*found_hpol);
410  rms[ant][0]->SetDirectory(0);
411 
412  TH1D * found_vpol = (TH1D*) gDirectory->Get(TString::Format("v%d",ant));
413  rms[ant][1] = new TH1D(*found_vpol);
414  rms[ant][1]->SetDirectory(0);
415  }
416  __sync_synchronize(); //memory barrier
417  rms_loaded = true;
418  }
419  m.UnLock();
420  }
421 
422  return rms[ant][pol];
423 
424 }
425 
426 UCorrelator::TimeDependentAverage::TimeDependentAverage(int run, int nsecs, const char * persistdir,
427  double max_bottom_top_ratio, int min_norm, double max_power)
428  : nsecs(nsecs) , run(run)
429 {
430  bool foundit = false;
431  rms_loaded = false;
432  peakiness_loaded = false;
433  avgs_loaded = false;
434 
435  const char * check_dir = persistdir ?: getenv("UCORRELATOR_TIMEAVG_DIR") ?: getenv("UCORRELATOR_SPECAVG_DIR") ?: 0;
436 
437 
438  if (check_dir)
439  {
440  fname.Form("%s/%d_%d.root", check_dir, run,nsecs);
441  TFile f(fname);
442  if (f.IsOpen())
443  {
444  f.cd();
445 
446  if (gDirectory->Get("norms") && gDirectory->Get("norms_minbias") && gDirectory->Get("nblasts"))
447  {
448  TH1I * found= (TH1I*) gDirectory->Get("norms");
449  norms = new TH1I(*found);
450  norms->SetDirectory(0);
451 
452  found= (TH1I*) gDirectory->Get("norms_minbias");
453  norms_minbias = new TH1I(*found);
454  norms_minbias->SetDirectory(0);
455 
456  found= (TH1I*) gDirectory->Get("nblasts");
457  nblasts = new TH1I(*found);
458  nblasts->SetDirectory(0);
459 
460  foundit = true;
461  }
462  else
463  {
464  fprintf(stderr,"Could not find one or more of norms, norms_minbias or nblasts inside %s/%d_%d.root. Are your spectrum averages out of date? ", check_dir,run,nsecs);
465 
466  }
467  }
468  else
469  {
470  printf("Could not open %s/%d_%d.root", check_dir, run, nsecs);
471  }
472  memset(rms,0,sizeof(rms));
473 
474  memset(avgs,0,sizeof(avgs));
475  memset(avgs_minbias,0,sizeof(avgs_minbias));
476  }
477 
478  if (!foundit)
479  {
480  printf("Didn't find spectrum_averages... creating!\n");
481  if (!check_dir)
482  {
483  printf("Define UCORRELATOR_TIMEAVG_DIR to persist somewhere.\n");
484  }
485  computeAverage( max_bottom_top_ratio, min_norm, max_power);
486  if (check_dir) saveToDir(check_dir);
487  }
488 
489  memset(peakiness,0,sizeof(peakiness));
490  memset(peakiness_minbias,0,sizeof(peakiness_minbias));
491 }
492 
493 
494 
495 TH1* UCorrelator::TimeDependentAverage::getSpectrumAverage(AnitaPol::AnitaPol_t pol, int ant, double t, bool db, bool minbias) const
496 {
497 
498  const TH2 * h =getSpectrogram(pol,ant,minbias);
499  //figure out which bin we are in
500  int bin = h->GetYaxis()->FindFixBin(t);
501 
502  if (bin == 0 || bin == h->GetNbinsY()+1) return 0 ;
503 
504  TString title;
505  title.Form("Ant%d %c-Pol Average at t=%g", ant, pol == AnitaPol::kHorizontal ? 'H' : 'V', t);
506  TString name;
507  name.Form("specavg_%d_%d_%g",ant,pol,t);
508  TH1D * answer = new TH1D(name,title, 131,0,1.31); //todo don't hardcode
509 
510  for (int i = 1; i <= answer->GetNbinsX(); i++)
511  {
512  answer->SetBinContent(i, h->GetBinContent(i,bin));
513  }
514 
515  answer->GetXaxis()->SetTitle("Frequency");
516  answer->GetYaxis()->SetTitle(db ? "Power (dBish)" :"Power (linear)");
517 
518 
519  if (db)
520  {
521  for (int i = 1; i <= answer->GetNbinsX(); i++)
522  {
523  answer->SetBinContent(i, 10 * TMath::Log10(answer->GetBinContent(i)));
524  }
525  }
526 
527 
528  return answer;
529 
530 
531 }
532 
533 TH1 *UCorrelator::TimeDependentAverage::getSpectrumPercentile(AnitaPol::AnitaPol_t pol, int ant, double pct , bool db, bool minbias ) const
534 {
535 
536  TH1 * answer = UCorrelator::image::getPctileProjection( getSpectrogram(pol,ant,minbias), 1, pct, true, getNorms(minbias));
537 
538  answer->GetXaxis()->SetTitle("Frequency");
539  answer->GetYaxis()->SetTitle(db ? "Pctile Power (dBish)" :"Pctile Power (linear)");
540 
541  answer->SetTitle(TString::Format("Ant%d %c-Pol %gth Pctile", ant, pol == AnitaPol::kHorizontal ? 'H' : 'V', pct*100));
542 
543  if (db)
544  {
545  for (int i = 1; i <= answer->GetNbinsX(); i++)
546  {
547  answer->SetBinContent(i, 10 * TMath::Log10(answer->GetBinContent(i)));
548  }
549  }
550 
551 
552  return answer;
553 
554 }
555 
556 
557 double UCorrelator::TimeDependentAverage::getStartTime() const
558 {
559 
560  return norms ? norms->GetXaxis()->GetXmin() : -1;
561 }
562 
563 double UCorrelator::TimeDependentAverage::getEndTime() const
564 {
565  return norms ? norms->GetXaxis()->GetXmax() : -1;
566 }
567 
568 
569 // to take advantage of magic statics
571 {
574  {
575  if (AnitaVersion::get() == 4)
576  {
577  fprintf(stderr,"warning: using default terminated thermal spectrum for A3 for peakiness\n");
578  }
579 
580  TString dir;
581  dir.Form("%s/share/UCorrelator/terminated_noise/", getenv("ANITA_UTIL_INSTALL_DIR"));
582 
583  avg = new UCorrelator::TimeDependentAverage(11382,60, dir.Data());
584  }
585 
586  static ThermalAverageWrapper & get()
587  {
588  static ThermalAverageWrapper t;
589  return t;
590  }
591 
592 };
593 
594 const UCorrelator::TimeDependentAverage* UCorrelator::TimeDependentAverage::defaultThermal()
595 {
596  return ThermalAverageWrapper::get().avg;
597 }
598 
599 
600 
601 void UCorrelator::TimeDependentAverage::computePeakiness(const TimeDependentAverage * thermalSpec, double fractionForNormalization) const
602 {
603 
604  if (!thermalSpec) thermalSpec = defaultThermal();
605 
606 
607 //#ifdef UCORRELATOR_OPENMP
608 //#pragma omp parallel for
609 //#endif
610  for (int ant = 0; ant < NUM_SEAVEYS; ant++)
611  {
612 // printf("%d\n",ant);
613  for (int ipol = 0; ipol < 2; ipol++)
614  {
615 
616  for (int minbias = 0; minbias < 2; minbias++)
617  {
618 // printf("%d %d\n",ant,ipol);
619 
620  //get median spectrum
621  TH1 * median = getSpectrumPercentile(AnitaPol::AnitaPol_t(ipol), ant, 0.5,false,minbias);
622  TH1 * thermal = thermalSpec->getSpectrumPercentile(AnitaPol::AnitaPol_t(ipol),ant,0.5,false,minbias);
623 
624  // now we have to figure out scale. To do this, we compute the mean of the middle frac of points.
625  int index_spec[median->GetNbinsX()];
626  int index_therm[thermal->GetNbinsX()];
627 
628  TMath::Sort(thermal->GetNbinsX(),((TH1F*) thermal)->GetArray()+1, index_therm);
629  TMath::Sort(median->GetNbinsX(), ((TH1F*)median)->GetArray()+1, index_spec);
630 
631  double sum_spec = 0;
632  double sum_therm =0;
633 
634  for (int i = 0; i <= thermal->GetNbinsX()*fractionForNormalization; i++)
635  {
636  sum_therm+=thermal->GetBinContent(index_therm[ (int) (i + (thermal->GetNbinsX() * (0.5-fractionForNormalization))) ]);
637  }
638 
639  for (int i = 0; i <= median->GetNbinsX()*fractionForNormalization; i++)
640  {
641  sum_spec+=median->GetBinContent(index_spec[ (int) (i + median->GetNbinsX() * (0.5-fractionForNormalization))]);
642  }
643 
644  sum_therm /= thermal->GetNbinsX();
645  sum_spec /= median->GetNbinsX();
646  double ratio = sum_spec / sum_therm;
647 
648  TString name;
649  name.Form("peakiness_%d_%d_%s\n", ant,ipol, minbias ? "minbias" : "rf");
650  TString title;
651  title.Form("%s peakiness ant=%d pol=%d\n", minbias ? "minbias" : "RF" , ant,ipol);
652 
653  const TH2 * avg = getSpectrogram(AnitaPol::AnitaPol_t(ipol),ant,minbias);
654  TH2D * peaky = new TH2D(name,title,
655  avg->GetNbinsX(), avg->GetXaxis()->GetXmin(), avg->GetXaxis()->GetXmax(),
656  avg->GetNbinsY(), avg->GetYaxis()->GetXmin(), avg->GetYaxis()->GetXmax());
657 
658 
659  peaky->SetDirectory(0);
660 
661  for (int ii = 1; ii < peaky->GetNbinsX(); ii++)
662  {
663  for (int jj = 1; jj < peaky->GetNbinsY(); jj++)
664  {
665  peaky->SetBinContent(ii,jj, avg->GetBinContent(ii,jj) / ( ratio * thermal->GetBinContent(ii)));
666  if (std::isnan(peaky->GetBinContent(ii,jj)))
667  peaky->SetBinContent(ii,jj,0);
668  }
669  }
670  (minbias ? peakiness_minbias[ant][ipol] : peakiness[ant][ipol]) = peaky;
671  delete median;
672  delete thermal;
673  }
674 
675  }
676  }
677 
678 }
679 
680 UCorrelator:: TimeDependentAverageLoader::TimeDependentAverageLoader(const char * the_dir, int secs)
681  : dir(the_dir), nsecs(secs)
682 {
683  tavg = 0;
684 }
685 
686 
687 static TMutex mut;
688 const UCorrelator::TimeDependentAverage* UCorrelator::TimeDependentAverageLoader::avg(double t) const
689 {
690  TLockGuard l(&mut);
691 
692 // printf("%g\n",t);
693 
694  if (tavg && t >= tavg->getStartTime()-5 && t <= tavg->getEndTime()+5 )
695  {
696  return tavg;
697  }
698 
699  int run = AnitaDataset::getRunAtTime(t);
700 // printf("loading average from run %d\n",run);
701 
702  if (tavg) delete tavg;
703  tavg = new TimeDependentAverage(run,nsecs, dir);
704 
705  return tavg;
706 
707 }
708 
709 double UCorrelator::TimeDependentAverage::getBlastFraction(double t) const
710 {
711  //Estimate by estimating nblasts and n, taking their ratio
712  int bin = nblasts->GetXaxis()->FindFixBin(t);
713 
714  //the other closest bin
715  int other_bin = t < nblasts->GetXaxis()->GetBinCenter(bin) ? bin-1 : bin+1;
716 
717 
718  //the fraction of blasts in the closest bin
719  double blast_frac_bin = nblasts->GetBinContent(bin) / (nblasts->GetBinContent(bin) + norms->GetBinContent(bin));
720 
721  //f is the weight of the main bin.
722  double f = norms->GetBinContent(other_bin) == 0 ? 1 : 1-fabs(t-nblasts->GetXaxis()->GetBinCenter(bin)) / nsecs;
723  if (f < 1 )
724  {
725  double blast_frac_other_bin = nblasts->GetBinContent(other_bin) / (nblasts->GetBinContent(other_bin) + norms->GetBinContent(other_bin));
726  return f * blast_frac_bin + (1-f) * blast_frac_other_bin;
727  }
728 
729  return blast_frac_bin;
730 }
731 
732 
733 TMutex loader_map_lock;
734 std::map<int,UCorrelator::TimeDependentAverageLoader*> loader_map;
735 
736 static UCorrelator::TimeDependentAverageLoader * getLoader(int nsecs)
737 {
738  TLockGuard l(&loader_map_lock);
739 
740  if (loader_map.count(nsecs))
741  {
742  return loader_map[nsecs];
743  }
744 
746  loader_map[nsecs] = ldr;
747  return ldr;
748 }
749 
750 double UCorrelator::TimeDependentAverage::getRMS(AnitaPol::AnitaPol_t pol, int ant, double t) const
751 {
752  return ((TH1*) getRMS(pol,ant))->Interpolate(t);
753 }
754 
755 
756 
757 double UCorrelator::TimeDependentAverageLoader::getRMS(double t, int ipol, int ant, int nsecs)
758 {
759  return getLoader(nsecs)->avg(t)->getRMS(AnitaPol::AnitaPol_t(ipol),ant,t);
760 }
761 
762 double UCorrelator::TimeDependentAverageLoader::getPayloadBlastFraction(double t, int nsecs)
763 {
764  return getLoader(nsecs)->avg(t)->getBlastFraction(t);
765 }
766 
767 
768 const TH2D * UCorrelator::TimeDependentAverage::getPeakiness(AnitaPol::AnitaPol_t pol, int ant, bool minbias) const
769 {
770 
771  //make sure we have averages loaded
772  getSpectrogram(AnitaPol::kHorizontal,0);
773 
774  __sync_synchronize(); //memory barrier
775  if (!peakiness_loaded)
776  {
777  m.Lock();
778  if (!peakiness_loaded)
779  {
780  computePeakiness();
781  __sync_synchronize(); //memory barrier
782  peakiness_loaded = true;
783  }
784  m.UnLock();
785  }
786 
787  return minbias ? peakiness_minbias[ant][pol] : peakiness[ant][pol];
788 }
static int getRunAtTime(double t)
const TGraphAligned * power() const
A filter strategy defines the sets of filters that are used and provides some introspection abilities...
TimeDependentAverage(int run, int nsecs=10, const char *persistdir=0, double max_bottom_top_ratio=4.0, int min_norm=5, double max_power=1e6)
void computePeakiness(const TimeDependentAverage *thermal=0, double fractionForNormalization=0.5) const
Vertical Polarisation.
const TGraphAligned * even() const
Horizontal Polarisation.
This class is intended to store all the necessary data about an ANITA event for filtering and analysi...
enum AnitaPol::EAnitaPol AnitaPol_t
Polarisation enumeration.
void addOperation(FilterOperation *f, bool enable_output=false)
This class is intended to be the main storage vessel for ANITA waveforms. It is similar in principle ...
static double getRMS(double t, int ipol, int ant, int nsecs=10)
UInt_t triggerTime
Trigger time from TURF converted to unixTime.