RayleighHist.h
1 #ifndef RAYLEIGH_HIST_H
2 #define RAYLEIGH_HIST_H
3 
4 #include "TH1D.h"
5 #include "RingBuffer.h"
6 #include <Math/Minimizer.h>
7 #include <Math/Factory.h>
8 #include <Math/Functor.h>
9 #include "TF1.h"
10 
11 
12 class TGraph;
13 
14 namespace Acclaim{
15  class FourierBuffer;
16 
20  class RayleighHist : public TH1D {
21 
22  friend class FourierBuffer;
23 
24  public:
25 
26  typedef enum {
27  kTF1, // slow
28  kMinuit, // less slow
29  kScan, // probably the fastest useful option, won't do errors
30  kJustEvalGuess, // fastest but obviously the least accurate
31  kAdaptive, // Tries just evaluating the guess, if that's good enough (chiSquare < 2), stops there, otherwise does a scan
32  kDefault = kAdaptive
33  // Default = JustEvalGuess
34  } FitMethod;
35 
36  typedef enum {
37  kPoisson,
38  kPearson
39  } ChiSquareErrorMethod;
40 
41  RayleighHist(FourierBuffer* fb=NULL, const char* name = "", const char* title = "");
42  virtual ~RayleighHist();
43 
44  virtual void Draw(Option_t* opt="");
45  virtual bool add(double newAmp);
46  void getRayleighFitParams(double& rayAmp, double& chiSquare, int& ndf);
47 
48  void fitRayleigh(bool forGuiUpdateTF1=true); // *MENU* Fit the Rayleigh distribution using the selected fit method
49 
50  // Mostly for validating fit sanity with a GUI
51  void fitRayleighAdaptive(bool forGuiUpdateTF1=true); // *MENU* Tries to evaluate the guess, if it's good enough move on, otherwise scan.
52  void fitRayleighScan(bool forGuiUpdateTF1=true); // *MENU* Fit the Rayleigh distribution scanning through amplitude values and choosing the lowest chiSquare residual
53  void fitRayleighJustEvalGuess(bool forGuiUpdateTF1=true); // *MENU* "Fit" the rayleigh distribution just using the guess from the mean/integral of the histogram
54  void fitRayleighTF1(); // *MENU* Fit using the TF1
55  void fitRayleighMinuit(bool forGuiUpdateTF1=true); // *MENU* Fit using Minuit
56 
57  static void guessMaxBinLimitAndSigmaFromMean(double meanAmp, double& maxAmp, double& sigmaGuess, double fracOfEventsInsideMaxAmp);
58 
59 
60  inline double getOneMinusCDF(double amp, double distAmp = -1) const {
61  distAmp = distAmp < 0 ? fRayleighAmplitude : distAmp; // use this histograms rayleigh distribution amplitude if one wasn't specified
62  return exp((-0.5*amp*amp)/(distAmp*distAmp));
63  }
64 
65  inline double getCDF(double amp, double distAmp = -1) const{ // This is the fraction of amplitudes lower than amp
66  distAmp = distAmp < 0 ? fRayleighAmplitude : distAmp; // Use this histograms rayleigh distribution amplitude if one wasn't specified
67  return 1 - getOneMinusCDF(amp, distAmp);
68  }
69  inline double getAmplitude() const {return fRayleighAmplitude;}
70 
71  protected:
73 
74  virtual int Fill(double amp, double sign=1);
76  Int_t fNx;
77  Int_t fNumEvents;
78  std::vector<double> binCentres;
79  std::vector<double> squaredBinCentres;
80  std::vector<int> binValues; // cache histogram bin contents, should be integers
81  std::vector<double> squaredBinErrors;
82 
83 
84 
85 
86  // virtual int Fill(double amp, double sign=1); //!< Fill the histogram, this is called by add(double)
87  bool axisRangeOK() const;
88  void rebinAndRefill(double meanAmp);
89 
91  TF1* fRay;
92 
93  Int_t fNumFitParams;
94  ROOT::Math::Minimizer* fMinimizer;
95  ROOT::Math::Functor fChiSquaredFunc;
96 
97  double getRayleighChiSquare(const double* params); // for the minuit fitter
98  std::vector<double> theFitParams; // for minuit fitter interface
99  std::vector<double> theFitParamsSteps; // for minuit fitter interface (I think minuit ignores this, but oh well)
100 
103  double freqMHz;
104 
105  TGraph* grLastAddedAmp;
106 
107  Int_t fNDF;
108  Double_t fChiSquare;
109  Double_t fRayleighAmplitude;
110 
111  void updateParamsOfTF1();
112  std::vector<Double_t> fParamsTF1;
113 
114  FitMethod fitMethod;
115  ChiSquareErrorMethod chiSquareErrorMethod;
116  const Int_t fFitEveryNAdds;
117  Int_t fNumAddsMod10;
118 
119  // caching for fit functions
120  double fBinWidth;
121  double fRayleighAmpGuess;
122  double fRayleighNorm;
123  // Int_t fNx;
124  // std::vector<double> binCentres;
125  // std::vector<double> squaredBinCentres;
126  // std::vector<int> binValues; // cache histogram bin contents, should be integers
127  // std::vector<double> squaredBinErrors;
128 
129  ClassDef(RayleighHist, 0);
130  };
131 
132 
133 
134 }
135 #endif
double getOneMinusCDF(double amp, double distAmp=-1) const
Definition: RayleighHist.h:60
void getRayleighFitParams(double &rayAmp, double &chiSquare, int &ndf)
Output Rayleigh distribution parameters.
Int_t fNx
The number of bins (faster than GetNbinsX())
Definition: RayleighHist.h:76
A a glorified ring buffer of frequency amplitudes with a TSpectrum to look for CW spikes...
Definition: FourierBuffer.h:42
virtual bool add(double newAmp)
Input amplitudes events.
TGraph * grLastAddedAmp
A pretty visual representation of the last added amplitude.
Definition: RayleighHist.h:105
Int_t fNumFitParams
Will be equal to one as we only try and fit the amplitude (normalization is fixed by fNumEvents and b...
Definition: RayleighHist.h:93
TF1 * fRay
Pointer to the Rayeligh TF1 cloned from parent FourierBuffer.
Definition: RayleighHist.h:91
double fracOfEventsWanted
Fraction of events to be in the histogram bin limits using the guessed amplitude (don&#39;t set to 1 as t...
Definition: RayleighHist.h:101
Namespace which wraps everything in the library.
virtual int Fill(double amp, double sign=1)
Fill the histogram, this is called by add(double)
RingBuffer amplitudes
Tracks all the amplitudes.
Definition: RayleighHist.h:72
Int_t fNumNonEmptyBins
Cache number of non empty bins.
Definition: RayleighHist.h:75
void rebinAndRefill(double meanAmp)
Dynamically rebin and refill histogram with contents of RingBuffer of amplitudes. ...
ROOT::Math::Minimizer * fMinimizer
The minuit minimizer object.
Definition: RayleighHist.h:94
double freqMHz
The frequency (MHz) of this Rayleigh distribution.
Definition: RayleighHist.h:103
ROOT::Math::Functor fChiSquaredFunc
For minuit interface, will point to getRayelighChiSquare(const double*)
Definition: RayleighHist.h:95
Int_t risingEdgeBins
Number of bins between 0 and where we guess the histogram peak is, for dynamic rebinning.
Definition: RayleighHist.h:102
FourierBuffer * fParent
Daddy.
Definition: RayleighHist.h:90
Int_t fNumEvents
Tracks the number of events in the RingBuffer/histogram (faster than integral)
Definition: RayleighHist.h:77
bool axisRangeOK() const
Checks current axis range is reasonable.