FourierBuffer.h
1 /* -*- C++ -*-.***************************************************************************************************
2  Author: Ben Strutt
3  Email: strutt@physics.ucla.edu
4 
5  Description:
6  A FIFO queue for frequency domain information
7 *************************************************************************************************************** */
8 
9 #ifndef FOURIERBUFFER_H
10 #define FOURIERBUFFER_H
11 
12 #include "AnalysisWaveform.h"
13 
14 #include "TH1D.h"
15 #include <complex>
16 #include <deque>
17 #include "AnitaConventions.h"
18 #include "TObject.h"
19 
20 class TSpectrum;
21 class FilteredAnitaEvent;
22 class TPad;
23 class UsefulAnitaEvent;
24 
25 
26 namespace Acclaim
27 {
28 
29  class RayleighHist;
30  class TGraphFB;
31 
32 
42  class FourierBuffer {
43 
44  friend class RayleighHist;
45  public:
46 
47  enum SummaryOption_t{
48  None,
49  Chisquare,
50  ReducedChisquare,
51  NDF,
52  RayleighAmplitude,
53  Prob
54  };
55 
56 
57  virtual ~FourierBuffer();
58  explicit FourierBuffer(Int_t theBufferSize=1000);
59 
60  size_t add(const FilteredAnitaEvent* fEv);
61 
62  Double_t getProb(AnitaPol::AnitaPol_t pol, Int_t ant, Int_t freqBin) const{
63  return probs[pol][ant][freqBin];
64  }
65  Double_t getBackgroundSpectrumAmp(AnitaPol::AnitaPol_t pol, Int_t ant, Int_t freqBin) const{
66  return spectrumAmplitudes[pol][ant][freqBin];
67  }
68 
69  // void getChanChiSquareAndNDF(AnitaPol::AnitaPol_t pol, Int_t ant,
70  // double& chiSquare, int& ndf) const{
71  // chiSquare = chanChisquare[pol][ant];
72  // ndf = chanNdf[pol][ant];
73  // }
74  const std::vector<double>& getPowerRingBufferBack(AnitaPol::AnitaPol_t pol, int ant){
75  return powerRingBuffers[pol][ant].back();
76  }
77 
78  const RayleighHist* getRayleighDistribution(Int_t ant, AnitaPol::AnitaPol_t pol,
79  Int_t freqBin) const {
80  return hRays[pol][ant].at(freqBin);
81  }
82 
83  unsigned getN(int ant, AnitaPol::AnitaPol_t pol) const {
84  return sumPowers[pol][ant].size();
85  }
86 
87  TGraphFB* getAvePowSpec_dB(Int_t ant, AnitaPol::AnitaPol_t pol, int lastNEvents = -1) const;
88  TGraphFB* getAvePowSpec(Int_t ant, AnitaPol::AnitaPol_t pol, int lastNEvents = -1) const;
89  TGraphFB* getBackground_dB(Int_t ant, AnitaPol::AnitaPol_t pol, int lastNEvents = -1) const;
90  TGraphFB* getBackground(Int_t ant, AnitaPol::AnitaPol_t pol, int lastNEvents = -1) const;
91  TGraphFB* getReducedChiSquaresOfRayelighDistributions(Int_t ant, AnitaPol::AnitaPol_t pol) const;
92  void drawSummary(TPad* pad, SummaryOption_t) const;
93  unsigned getCurrentBufferSize() const;
94 
95  const std::vector<double>& getChiSquares(int ant,
96  AnitaPol::AnitaPol_t pol) const {
97  return chiSquares[pol][ant];
98  }
99  const std::vector<double>& getChiSquaresRelativeToSpectrum(int ant,
100  AnitaPol::AnitaPol_t pol) const {
101  return chiSquaresRelativeToSpectrum[pol][ant];
102  }
103  const std::vector<int>& getNDFs(int ant,
104  AnitaPol::AnitaPol_t pol) const{
105  return ndfs[pol][ant];
106  }
107  const std::vector<double>& getRayleighAmplitudes(int ant,
108  AnitaPol::AnitaPol_t pol) const {
109  return fitAmplitudes[pol][ant];
110  }
111  const std::vector<double>& getBackgroundSpectrumAmplitudes(int ant,
112  AnitaPol::AnitaPol_t pol) const {
113  return spectrumAmplitudes[pol][ant];
114  }
115  const std::vector<double>& getProbabilities(int ant, AnitaPol::AnitaPol_t pol) const {
116  return probs[pol][ant];
117  }
118 
119  const std::vector<double>& getChanChiSquares(int ant, AnitaPol::AnitaPol_t pol) const{
120  return chanChiSquares[pol][ant];
121  }
122 
123  TH1D* makeChanChisquareHist(AnitaPol::AnitaPol_t pol, Int_t ant, TPad* pad, const char* drawOpt) const; // for summary
124 
125  void getMeanVarChanChiSquares(int ant, AnitaPol::AnitaPol_t pol, double mean, double var) const{
126  mean = meanChanChiSquare[pol][ant];
127  var = varChanChiSquare[pol][ant];
128  }
129 
130  int getNumEventsInBuffer() const {
131  return eventsInBuffer;
132  }
133  void setForceLoadHistory(bool f) const {
134  fForceLoadHistory=f;
135  }
136 
137  bool isAlfaBandpassed(int ant, AnitaPol::AnitaPol_t pol) const {
138  return (ant == 4 && pol == AnitaPol::kHorizontal) || (ant == 12 && pol == AnitaPol::kHorizontal);
139  }
140 
141  protected:
142  Int_t bufferSize;
143  Int_t removeOld();
144  void initVectors(int n, double df);
145 
146  // list of events
147  std::deque<UInt_t> eventNumbers;
148  std::deque<Int_t> runs;
149 
150  std::deque<std::vector<double> > powerRingBuffers[AnitaPol::kNotAPol][NUM_SEAVEYS];
151 
152  // utility function to sanitize vector/graph initialization
153  void initGraphAndVector(std::vector<double> vec[][NUM_SEAVEYS],
154  std::vector<TGraphFB>* gr,
155  int n, double df, double defaultVal);
156 
157  TGraphFB* getSelectedGraphForSummary(SummaryOption_t choice, int ant, AnitaPol::AnitaPol_t pol) const;
158 
159 
160  // vectors of frequency bins
161  std::vector<double> sumPowers[AnitaPol::kNotAPol][NUM_SEAVEYS];
162  std::vector<RayleighHist*> hRays[AnitaPol::kNotAPol][NUM_SEAVEYS];
163 
164  std::vector<double> chiSquares[AnitaPol::kNotAPol][NUM_SEAVEYS];
165  std::vector<double> chiSquaresRelativeToSpectrum[AnitaPol::kNotAPol][NUM_SEAVEYS];
166  std::vector<int> ndfs[AnitaPol::kNotAPol][NUM_SEAVEYS];
167  std::vector<double> fitAmplitudes[AnitaPol::kNotAPol][NUM_SEAVEYS];
168  std::vector<double> spectrumAmplitudes[AnitaPol::kNotAPol][NUM_SEAVEYS];
169  std::vector<double> lastAmps[AnitaPol::kNotAPol][NUM_SEAVEYS];
170  std::vector<double> probs[AnitaPol::kNotAPol][NUM_SEAVEYS];
171 
172 
173  std::vector<double> chanChiSquares[AnitaPol::kNotAPol][NUM_SEAVEYS];
174  double meanChanChiSquare[AnitaPol::kNotAPol][NUM_SEAVEYS];
175  double varChanChiSquare[AnitaPol::kNotAPol][NUM_SEAVEYS];
176 
177 
178  // it turns out that initialising a TF1 is very slow,
179  // so I initialize a master here (owned by FourierBuffer) and clone others from this one.
180  TF1* fRay;
181 
182  Int_t fNumSkipped;
184 
185 
187  bool fCurrentlyLoadingHistory;
188  mutable bool fForceLoadHistory;
189 
190  int eventsInBuffer;
191 
192  double df; // frequency bin width (from AnalysisWaveform so probably in GHz)
193  double fMinFitFreq;
194  double fMaxFitFreq;
195  mutable TSpectrum* fSpectrum; // to estimate the background
196  double fMinSpecFreq;
197  double fMaxSpecFreq;
198  void getBackgroundSpectrum(double* y, int n) const;
199 
200  // double eventPower[AnitaPol::kNotAPol][NUM_SEAVEYS];
201  // double expectedThermalPower[AnitaPol::kNotAPol][NUM_SEAVEYS];
202 
203  mutable TPad* summaryPads[NUM_SEAVEYS]; // for drawSummary
204  mutable std::vector<TGraphFB> grReducedChiSquares[AnitaPol::kNotAPol]; // for drawSummary
205  mutable std::vector<TGraphFB> grChiSquares[AnitaPol::kNotAPol]; // for drawSummary
206  mutable std::vector<TGraphFB> grChiSquaresRelativeToSpectrum[AnitaPol::kNotAPol]; // for drawSummary
207  mutable std::vector<TGraphFB> grReducedChiSquaresRelativeToSpectrum[AnitaPol::kNotAPol]; // for drawSummary
208  mutable std::vector<TGraphFB> grNDFs[AnitaPol::kNotAPol]; // for drawSummary
209  mutable std::vector<TGraphFB> grSpectrumAmplitudes[AnitaPol::kNotAPol]; // for drawSummary
210  mutable std::vector<TGraphFB> grAmplitudes[AnitaPol::kNotAPol]; // for drawSummary
211  mutable std::vector<TGraphFB> grLastAmps[AnitaPol::kNotAPol]; // for drawSummary
212  mutable std::vector<TGraphFB> grProbs[AnitaPol::kNotAPol]; // for drawSummary
213 
214 
215  };
216 
217 
222  class TGraphFB : public TGraphAligned {
223  friend class FourierBuffer;
224  public:
225 
226  // Utility function to set fDerives from and fDerivatives (i.e. the drawing ownership)
227  static void setDrawingDependencies(const std::vector<Acclaim::TGraphFB*> grs);
228 
229  enum EDoubleClickOption{
230  kDrawRayleigh,
231  kDrawCopy
232  };
233 
242  TGraphFB(const FourierBuffer* theFb=NULL, Int_t theAnt=-1, AnitaPol::AnitaPol_t thePol=AnitaPol::kNotAPol,
243  int n=0) : TGraphAligned(n), fDoubleClickOption(kDrawCopy), fDerivedFrom(NULL)
244  {
245  SetTitle("");
246  fb = theFb;
247  ant = theAnt;
248  pol = thePol;
249  }
250 
256  explicit TGraphFB(const TGraphFB* gr) : TGraphAligned(gr->GetN(), gr->GetX(), gr->GetY()), fDoubleClickOption(gr->fDoubleClickOption), fDerivatives(gr->fDerivatives)
257  {
258  fb = gr->fb;
259  ant = gr->ant;
260  pol = gr->pol;
261  SetLineColor(gr->GetLineColor());
262  SetLineStyle(gr->GetLineStyle());
263  fDerivedFrom = gr->fDerivedFrom;
264  }
265  virtual ~TGraphFB(){;}
266  virtual void ExecuteEvent(Int_t event, Int_t x, Int_t y);
267  void drawCopy() const;
268  void drawRayleighHistNearMouse(int x, int y) const;
269  private:
270  const FourierBuffer* fb; // pointer to parent, don't delete
271  Int_t ant;
273  EDoubleClickOption fDoubleClickOption;
274 
275  // just raw pointers, no one "owns" anyone, rely on FourierBuffer and ROOT's garbage collection as appropriate
276  const TGraphFB* fDerivedFrom; // e.g. spectrum is derived from amplitudes, want to draw them together
277  std::vector<TGraphFB*> fDerivatives; // amplitudes point to spectrum
278  ClassDef(Acclaim::TGraphFB, 0);
279  };
280 }
281 #endif //FOURIERBUFFER_H
void initGraphAndVector(std::vector< double > vec[][NUM_SEAVEYS], std::vector< TGraphFB > *gr, int n, double df, double defaultVal)
Initialise an internal vector and TGraphFB with with an appropriate set of default values...
A a glorified ring buffer of frequency amplitudes with a TSpectrum to look for CW spikes...
Definition: FourierBuffer.h:42
Int_t fNumSkipped
Incremented when skipping a payload blast or SURF saturation event, currently only for debugging...
void automagicallyLoadHistory(const FilteredAnitaEvent *fEv)
Function which loops through old events so that the FourierBuffer is filled.
TGraphFB(const TGraphFB *gr)
Creates a copy of the passed TGraphFB.
A little class for some GUI magic (for MagicDisplay)
bool doneVectorInit
Do we need to allocate a bunch of memory? (Do this dynamically to avoid MagicDisplay being slow) ...
Namespace which wraps everything in the library.
USeful in for loops.
size_t add(const FilteredAnitaEvent *fEv)
Adds an event to the FourierBuffer.
UsefulAnitaEvent – The Calibrated Useful Anita Event object.
virtual ~FourierBuffer()
Destructor.
Int_t removeOld()
Remove data from the dequeues while their size is greater than the buffer size.
void initVectors(int n, double df)
Initialise all internal vectors.
FourierBuffer(Int_t theBufferSize=1000)
Constructor for FourierBuffer.
Horizontal Polarisation.
This class is intended to store all the necessary data about an ANITA event for filtering and analysi...
TH1D * makeChanChisquareHist(AnitaPol::AnitaPol_t pol, Int_t ant, TPad *pad, const char *drawOpt) const
Histogram the event frequency bin amplitudes squared, divided by the rayleigh distribution amplitude ...
enum AnitaPol::EAnitaPol AnitaPol_t
Polarisation enumeration.
TGraphFB(const FourierBuffer *theFb=NULL, Int_t theAnt=-1, AnitaPol::AnitaPol_t thePol=AnitaPol::kNotAPol, int n=0)
Constructor.