Baseline.cc
1 #include "Baseline.h"
2 #include "TGraph.h"
3 #include <cstdio>
4 #include "FilteredAnitaEvent.h"
5 #include "TSystem.h"
6 #include "FilterStrategy.h"
7 #include "AnitaDataset.h"
8 #include "UsefulAnitaEvent.h"
9 #include "FFTtools.h"
10 #include "RawAnitaHeader.h"
11 #include "UCFlags.h"
12 #include "TFile.h"
13 #include "FFTWComplex.h"
14 
15 #define NSAMPLES 256
16 #define NOMINAL_DT 1./2.6
17 
18 
19 static int makeBaselines(int run, TGraph ** hpol, TGraph ** vpol, int N = 5000)
20 {
21  char * datadir = getenv("ANITA_ROOT_DATA");
22  if (!datadir)
23  {
24  fprintf(stderr,"The environmental variable ANITA_ROOT_DATA must be defined! Aborting.");
25  exit(1);
26  }
27 
28 
29  AnitaDataset d(run);
30  FilterStrategy empty;
31 
32  int nevents = 0;
33  int i = 10;
34 
35 
36  while (nevents < N && i < d.N())
37  {
38  d.getEntry(i++);
39 
40  // skip non-RF triggers and a small fraction of times? is this really right? This is what Abby has though
41  if (d.header()-> trigType != 1 && d.header()->triggerTimeNs <= 1e6)
42  continue;
43 
44  //skip saturated events
45 
46  FilteredAnitaEvent fae(d.useful(), &empty, d.gps(), d.header());
47 
48  if (fae.checkSaturation())
49  continue;
50 
51  for (int ant = 0; ant < NUM_SEAVEYS; ant++)
52  {
53  if (nevents == 0)
54  {
55  hpol[ant] = new TGraph(NSAMPLES/2+1);
56  vpol[ant] = new TGraph(NSAMPLES/2+1);
57  }
58 
59  TGraph * gh = d.useful()->getGraph(ant, AnitaPol::kHorizontal);
60  TGraph * gv = d.useful()->getGraph(ant, AnitaPol::kVertical);
61 
62  TGraph * igh = FFTtools::getInterpolatedGraph(gh, NOMINAL_DT);
63  TGraph * igv = FFTtools::getInterpolatedGraph(gv, NOMINAL_DT);
64 
65  igh->Set(NSAMPLES);
66  igv->Set(NSAMPLES);
67 
68  FFTWComplex * fft_h = FFTtools::doFFT(igh->GetN(), igh->GetY());
69  FFTWComplex * fft_v = FFTtools::doFFT(igv->GetN(), igv->GetY());
70 
71  for (int j = 0; j < NSAMPLES/2+1; j++)
72  {
73  if (nevents == 0)
74  {
75  hpol[ant]->GetX()[j] = j / (NOMINAL_DT * NSAMPLES); //GHz
76  vpol[ant]->GetX()[j] = j / (NOMINAL_DT * NSAMPLES); //GHz
77  }
78 
79  hpol[ant]->GetY()[j] += fft_h[j].getAbs() / (NSAMPLES/2.+1) * (j == 0 || j == NSAMPLES/2 ? 1 : 2);
80  vpol[ant]->GetY()[j] += fft_v[j].getAbs() / (NSAMPLES/2.+1) * (j == 0 || j == NSAMPLES/2 ? 1 : 2);
81 
82  }
83 
84  delete [] fft_h;
85  delete [] fft_v;
86  delete gh;
87  delete gv;
88  delete igh;
89  delete igv;
90 
91  }
92 
93  nevents++;
94  }
95 
96  for (int ant = 0; ant < NUM_SEAVEYS; ant++)
97  {
98  for (int i = 0; i < NSAMPLES / 2 + 1; i++)
99  {
100  hpol[ant]->GetY()[i] /= nevents;
101  vpol[ant]->GetY()[i] /= nevents;
102  }
103  }
104 
105 
106  return nevents;
107 }
108 
109 
110 void UCorrelator::Baseline::saveToDir(const char * dir)
111 {
112 
113  gSystem->mkdir(dir,true);
114  TFile f(TString::Format("%s/%d_%d.root", dir, run,navg),"RECREATE");
115  for (int ant = 0; ant < NUM_SEAVEYS; ant++)
116  {
117  hpol[ant]->Write(TString::Format("h%d",ant));
118  vpol[ant]->Write(TString::Format("v%d",ant));
119  }
120 }
121 
122 UCorrelator::Baseline::Baseline(int run, int navg, const char * persistdir)
123  : navg(navg) , run(run)
124 {
125  bool foundit = false;
126  if (persistdir)
127  {
128  TFile f(TString::Format("%s/%d_%d.root", persistdir, run,navg));
129  if (f.IsOpen())
130  {
131  for (int ant = 0; ant < NUM_SEAVEYS; ant++)
132  {
133  TGraph * found_hpol = (TGraph*) f.Get(TString::Format("h%d",ant));
134  hpol[ant] = new TGraph(*found_hpol);
135 
136  TGraph * found_vpol = (TGraph*) f.Get(TString::Format("v%d",ant));
137  vpol[ant] = new TGraph(*found_vpol);
138  }
139  foundit = true;
140  }
141  }
142 
143  if (!foundit)
144  {
145  printf("Didn't find baselines... creating!\n");
146  if (!persistdir)
147  {
148  printf("Define UCORRELATOR_BASELINE_DIR to persist somewhere.\n");
149  }
150  makeBaselines(run, hpol, vpol, navg);
151  }
152 
153 
154  hpol_avg = new TGraph(NSAMPLES/2+1);
155  vpol_avg = new TGraph(NSAMPLES/2+1);
156 
157  memcpy(hpol_avg->GetX(), hpol[0]->GetX(), sizeof(double) * hpol_avg->GetN());
158  memcpy(vpol_avg->GetX(), vpol[0]->GetX(), sizeof(double) * vpol_avg->GetN());
159 
160  for (int ant = 0; ant < NUM_SEAVEYS; ant++)
161  {
162  for (int i =0; i < NSAMPLES/2 +1; i++)
163  {
164  hpol_avg->GetY()[i] += hpol[ant]->GetY()[i] / NUM_SEAVEYS;
165  vpol_avg->GetY()[i] += vpol[ant]->GetY()[i] / NUM_SEAVEYS;
166  }
167  }
168 
169  if (persistdir) saveToDir(persistdir);
170 }
171 
Baseline(int run, int navg=5000, const char *persistdir="")
Definition: Baseline.cc:122
FFTWComplex * doFFT(int length, const double *theInput)
Computes an FFT of an array of real numbers.
Definition: FFTtools.cxx:436
A filter strategy defines the sets of filters that are used and provides some introspection abilities...
TGraph * getInterpolatedGraph(const TGraph *grIn, Double_t deltaT)
Interpolation Routines that use ROOT::Math::Interpolator.
Definition: FFTtools.cxx:32
void saveToDir(const char *dir)
Definition: Baseline.cc:110
This is a wrapper class for a complex number.
Definition: FFTWComplex.h:12
Vertical Polarisation.
Horizontal Polarisation.
This class is intended to store all the necessary data about an ANITA event for filtering and analysi...