1 #include "AveragePowerSpectrum.h" 17 for(Int_t freqInd=0; freqInd<NUM_FREQS; freqInd++){
37 deltaFMHz = 1e3/(NOMINAL_SAMPLING_DELTAT*NUM_SAMPLES);
47 for(Int_t freqInd=0; freqInd<NUM_FREQS; freqInd++){
51 TString histName = name + TString::Format(
"_%d", freqInd);
52 TString histTitle = title + TString::Format(
" Rayleigh Distribution for %4.2lf MHz bin",
54 histTitle +=
"; Amplitude (mV/MHz); Events/bin";
55 TH1D* hTemp =
new TH1D(histName, histTitle,
57 INITIAL_MIN_AMPLITUDE,
58 INITIAL_MAX_AMPLITUDE);
59 hTemp->SetDirectory(0);
67 hTemp->SetCanExtend(TH1::kAllAxes);
69 hTemp->SetBit(TH1::kCanRebin);
87 for(
int i=0; i<MAX_NUM_OUTLIERS; i++){
114 return "([0]*x/([1]*[1]))*exp(-x*x/(2*[1]*[1]))";
147 NOMINAL_SAMPLING_DELTAT, FancyFFTs::kSum);
148 for(Int_t freqInd=0; freqInd < NUM_FREQS; freqInd++){
150 Double_t sqrtPSD = TMath::Sqrt(ps[freqInd])/(
deltaFMHz);
179 for(Int_t freqInd=0; freqInd < NUM_FREQS; freqInd++){
184 Double_t histMaxVal = h->GetXaxis()->GetBinLowEdge(h->GetNbinsX()+1);
185 if(sqrtPSD < histMaxVal){
190 numOutliers[freqInd]++;
193 if(numOutliers[freqInd]>=MAX_NUM_OUTLIERS){
195 while(numOutliers[freqInd] >= MAX_NUM_OUTLIERS){
198 Double_t outliersTemp[MAX_NUM_OUTLIERS] = {0};
199 Int_t numOutliersTemp = 0;
201 for(
int i=0; i<MAX_NUM_OUTLIERS; i++){
202 if(
outliers[freqInd][i] < histMaxVal){
206 outliersTemp[numOutliersTemp] =
outliers[freqInd][i];
211 numOutliers[freqInd] = 0;
212 for(
int i=0; i < numOutliersTemp; i++){
213 outliers[freqInd][i] = outliersTemp[i];
214 numOutliers[freqInd]++;
216 for(
int i=numOutliersTemp; i < MAX_NUM_OUTLIERS; i++){
224 for(Int_t freqInd=0; freqInd < NUM_FREQS; freqInd++){
243 for(Int_t freqInd=0; freqInd < NUM_FREQS; freqInd++){
245 h->Rebin(rebinFactor);
263 Double_t bestFreqDiff = DBL_MAX;
264 for(Int_t freqInd=0; freqInd < NUM_FREQS-1; freqInd++){
265 Double_t freqDiff = TMath::Abs(freqInd*
deltaFMHz - freqMHz);
266 if(freqDiff < bestFreqDiff){
267 bestFreqInd = freqInd;
268 bestFreqDiff = freqDiff;
302 TString funcName = TString::Format(
"fit_%d", freqInd);
303 TF1* f = (TF1*) h->FindObject(funcName);
321 for(Int_t freqInd=0; freqInd < NUM_FREQS; freqInd++){
323 Double_t xMax = h->GetXaxis()->GetBinLowEdge(h->GetNbinsX()+1);
330 TString name = TString::Format(
"h2D_%s", GetName());
331 TString title = TString::Format(
"%s Rayleigh Distribution Summary", GetTitle());
333 TH2D* h2 =
new TH2D(name, title, NUM_FREQS, 0, NUM_FREQS*
deltaFMHz,
334 NUM_AMPLITUDE_BINS, 0, maxVal);
336 h2->GetXaxis()->SetTitle(
"Frequency (MHz)");
337 h2->GetXaxis()->SetNoExponent(1);
338 h2->GetYaxis()->SetTitle(
"Amplitude (mV/MHz)");
339 h2->GetYaxis()->SetNoExponent(1);
342 for(Int_t freqInd=0; freqInd < NUM_FREQS; freqInd++){
344 for(Int_t binx=1; binx<=h->GetNbinsX(); binx++){
346 Double_t amplitude = h->GetBinCenter(binx);
347 Double_t weight = h->GetBinContent(binx);
349 h2->Fill(freqMHz, amplitude, weight);
366 for(Int_t freqInd=0; freqInd < NUM_FREQS; freqInd++){
381 for(Int_t freqInd=0; freqInd < NUM_FREQS; freqInd++){
395 for(Int_t freqInd=0; freqInd < NUM_FREQS; freqInd++){
416 TString fitName = TString::Format(
"fit_%d_%lf", freqInd, amplitude);
418 fit->SetParameter(1, amplitude);
419 fit->SetParameter(0, h->GetBinLowEdge(2)*h->Integral());
491 xHigh[freqInd] = h->GetBinLowEdge(h->GetNbinsX()+1);
541 Double_t xHighTemp = -1;
543 Double_t peakVal = h->GetBinContent(peakBin);
545 for(Int_t binx=peakBin; binx<=h->GetNbinsX(); binx++){
546 Double_t binVal = h->GetBinContent(binx);
547 if(binVal < peakVal*0.5){
548 xHighTemp = h->GetBinCenter(binx);
582 Double_t* rAmplitudes,
583 Double_t* rChiSquares,
585 Double_t* rChiSquaresFullRange,
586 Int_t* rNdfFullRange){
591 if(h->Integral() > 0){
593 TString fitName = TString::Format(
"fit_%d", freqInd);
596 Double_t mean = h->GetMean();
598 Double_t sigGuess = mean / TMath::Sqrt(0.5*TMath::Pi());
600 fit->SetParameter(1, sigGuess);
606 Double_t histArea = h->Integral() * h->GetBinLowEdge(2);
607 fit->FixParameter(0, histArea);
612 rAmplitudes[freqInd] = h->GetFunction(fit->GetName())->GetParameter(1);
613 rChiSquares[freqInd] = h->GetFunction(fit->GetName())->GetChisquare();
614 rNdf[freqInd] = h->GetFunction(fit->GetName())->GetNDF();
616 rChiSquaresFullRange[freqInd] = 0;
617 rNdfFullRange[freqInd] = 0;
618 for(
int binx=1; binx<=h->GetNbinsX(); binx++){
619 Double_t binVal = h->GetBinContent(binx);
620 Double_t binError = h->GetBinError(binx);
621 Double_t fitVal = fit->Eval(h->GetBinCenter(binx));
623 Double_t chi = (binVal - fitVal)/binError;
624 rChiSquaresFullRange[freqInd] += chi*chi;
625 rNdfFullRange[freqInd]++;
642 for(
int freqInd=0; freqInd < NUM_FREQS; freqInd++){
660 TString name = TString::Format(
"gr_%s", GetName());
661 TString title = TString::Format(
"%s", GetTitle());
663 std::vector<Double_t> avePowSpec(NUM_FREQS);
665 for(Int_t freqInd=0; freqInd < NUM_FREQS; freqInd++){
672 for(Int_t freqInd=0; freqInd < NUM_FREQS; freqInd++){
676 Double_t freqArray[NUM_FREQS];
677 for(
int freqInd=0; freqInd < NUM_FREQS; freqInd++){
681 TGraph* gr =
new TGraph(NUM_FREQS, freqArray, &avePowSpec[0]);
699 TString name = TString::Format(
"%s_dB", gr->GetName());
701 for(Int_t freqInd=0; freqInd < NUM_FREQS; freqInd++){
702 Double_t y = gr->GetY()[freqInd];
704 gr->GetY()[freqInd] = 10*TMath::Log10(y);
TH1D * getRayleighHistogramFromFrequencyMHz(Double_t freqMHz)
Get pointer to rayleigh distribution of frequency closest to a particular frequency.
void fitAllRayleighHistograms()
Fits all the Rayeligh distributions.
static TString getRayleighFunctionText()
Generates the text that defines the Rayleigh function for TF1.
Int_t count
Number of waveforms that have been added to the AveragePowerSpectrum.
TGraph * makeAvePowSpecTGraph()
Creates a TGraph of the average power spectrum.
TGraph * makeAvePowSpecTGraph_dB()
Creates a TGraph of the average power spectrum with a dB scale.
Double_t rayleighAmplitudesRisingEdge[NUM_FREQS]
Amplitudes from fits to the leading edge of the Rayleigh distributions.
Double_t rayleighFitChiSquares[NUM_FREQS]
Chi squares of the Rayleigh fits.
TF1 * constructFitFromAmplitude(Int_t freqInd, Double_t amplitude)
Create a Rayleigh fit with a particular Rayleigh amplitude.
void rebinAllRayleighHistograms(Int_t rebinFactor)
Rebins all the stored Rayleigh distributions.
Double_t rayleighFitChiSquaresRisingEdge[NUM_FREQS]
Chi squares of the fit to the rising egde of the Rayleigh distributions.
Int_t rayleighNdfFullRange[NUM_FREQS]
NDFs from Rayleigh fits extended over the full range.
static TF1 * makeRayleighFunction(TString name, Double_t xMin, Double_t xMax)
Creates a TF1 with to fit a Rayeligh distribution.
Double_t rayleighAmplitudes[NUM_FREQS]
Fitted Rayleigh amplitudes.
Int_t rayleighNdfRisingEdgeAndHalfFalling[NUM_FREQS]
NDFs from fits to the leading and half the falling edge of the Rayleigh distributions.
double * getPowerSpectrum(int len, double *input, double dt, conventionFlag normFlag, int threadInd=0)
Creates the power spectrum in an array of doubles of length (len/2 + 1).
Double_t summedPowSpec[NUM_FREQS]
Sum of all power spectrum.
TF1 * getRayleighHistogramFit(Int_t freqInd)
Get Rayleigh fit to Rayleigh distribution from frequency index.
Double_t eventPowSpec[NUM_FREQS]
Power spectra for the last added event.
size_t add(TGraph *gr)
Function for the user to add the voltage/time waveform to all the stored averages.
void fitRayleighHistogramRisingEdge(Int_t freqInd)
Fit a Rayleigh histogram, up to the peak bin.
Double_t rayleighAmplitudesRisingEdgeAndHalfFalling[NUM_FREQS]
Amplitudes from fits to the leading and half the falling edge of the Rayleigh distributions.
Double_t deltaFMHz
Difference between frequency bins in the power spectrums.
~AveragePowerSpectrum()
Destructor.
void fitAllRayleighHistogramsRisingEdgeAndHalfFallingEdge()
Fits all the Rayeligh distributions, as far as the bin which is half the value of the peak bin...
Double_t xHigh[NUM_FREQS]
High end of fitted range of Rayleigh histograms.
void deleteRayleighDistributions()
Deletes all the Rayleigh histograms.
TH1D * getRayleighHistogram(Int_t freqInd)
Get Rayleigh distribution from frequency index.
void fitRayleighHistogramOverRange(Int_t freqInd, Double_t xLowVal, Double_t xHighVal, Double_t *rAmplitudes, Double_t *rChiSquares, Int_t *rNdf, Double_t *rChiSquaresFullRange, Int_t *rNdfFullRange)
Worker function called by all the other fitting functions.
TF1 * constructFitFromRayleighAmplitudeRisingEdge(Int_t freqInd)
Create a Rayleigh fit with stored Rayleigh amplitude, fit up to the peak bin.
TF1 * constructFitFromRayleighAmplitudeRisingEdgeAndHalfFalling(Int_t freqInd)
Create a Rayleigh fit with stored Rayleigh amplitude, fit past the peak to half the maximum value...
AveragePowerSpectrum()
Constructor.
Int_t rayleighNdfRisingEdge[NUM_FREQS]
NDFs from fits to the leading edge of the Rayleigh distributions.
void fitRayleighHistogramRisingEdgeAndHalfFallingEdge(Int_t freqInd)
Fit a Rayleigh histogram, past the peak up to half the maximum value.
TF1 * constructFitFromRayleighAmplitude(Int_t freqInd)
Create a Rayleigh fit with stored Rayleigh amplitude.
Double_t outliers[NUM_FREQS][MAX_NUM_OUTLIERS]
Used to store values that are much greater than the current Rayliegh histogram content.
Double_t xHighRisingEdgeAndHalfFalling[NUM_FREQS]
High end of fitted range of Rayleigh histograms up to half the maximum value past the peak bin...
void fitRayleighHistogram(Int_t freqInd)
Fit a Rayleigh histogram.
Double_t xHighRisingEdge[NUM_FREQS]
High end of fitted range of Rayleigh histograms up to peak bin.
TH2D * makeRayleigh2DHistogram()
Combine all the 1D rayleigh distribution histograms to a 2D histogram.
Double_t eventRayleighAmplitudes[NUM_FREQS]
Rayleigh amplitudes for the last added event.
Double_t rayleighFitChiSquaresRisingEdgeAndHalfFallingFullRange[NUM_FREQS]
Amplitudes from fits to the leading and half the falling edge of the Rayleigh distributions extended ...
Takes in waveforms and averages averages their power spectra.
Int_t rayleighNdfRisingEdgeAndHalfFallingFullRange[NUM_FREQS]
NDFs from fits to the leading and half the falling edge of the Rayleigh distributions extended over t...
Double_t rayleighFitChiSquaresRisingEdgeFullRange[NUM_FREQS]
Chi squares of the fit to the rising egde of the Rayleigh distributions extended over the full range...
Double_t rayleighFitChiSquaresFullRange[NUM_FREQS]
Chi squares of the Rayleigh fits extended over the full range.
void getEventRayleighAmplitudes(TGraph *gr)
Converts the stored power spectrum for the events and stores it in internal memory.
Double_t rayleighFitChiSquaresRisingEdgeAndHalfFalling[NUM_FREQS]
Chi squares of the fit to the leading and half the falling edge of the Rayleigh distributions.
Int_t rayleighNdfRisingEdgeFullRange[NUM_FREQS]
NDFs from fits to the leading edge of the Rayleigh distributions extended over the full range...
Int_t rayleighNdf[NUM_FREQS]
NDFs from Rayleigh fits.
void fitAllRayleighHistogramsRisingEdge()
Fits all the Rayeligh distributions, but only up to the peak bin.
Int_t numOutliers[NUM_FREQS]
Count of number of outliers in outliers array.
TH1D * hRayleighs[NUM_FREQS]
Histograms for Rayleigh distributions.