SpectrumParameters.cc
1 #include "SpectrumParameters.h"
2 #include "TGraph.h"
3 #include "TLinearFitter.h"
4 #include "TMutex.h"
5 #include "AnalysisConfig.h"
6 #include <cfloat>
7 #include "TMath.h"
8 #ifdef UCORRELATOR_OPENMP
9 #include <omp.h>
10 #endif
11 
12 
13 
20 static TLinearFitter ** fitters;
21 
22 static void setupFitters() __attribute__((constructor));
23 
24 void setupFitters()
25 {
26  int nthreads = 1;
27 
28 #ifdef UCORRELATOR_OPENMP
29  nthreads = omp_get_max_threads();
30 #endif
31 
32 // printf("set up %d fitters\n", nthreads);
33  fitters = new TLinearFitter * [nthreads];
34  for (int i = 0; i < nthreads; i++)
35  {
36  fitters[i] = new TLinearFitter(1,"1++x","");
37  double dummy_x[5] = {0};
38  double dummy_y[5] = {0};
39  /* !@#!DSAFSD #!@$ !@#! %!@$ */
40  fitters[i]->AssignData(5,1,dummy_x,dummy_y);
41 
42  }
43 }
44 
45 void UCorrelator::spectrum::fillSpectrumParameters(const TGraph * spectrum, const TGraph * average,
47  const AnalysisConfig * config)
48 {
49  TLinearFitter* fitter;
50 
51 #ifdef UCORRELATOR_OPENMP
52  fitter = fitters[omp_get_thread_num()];
53 #else
54  fitter = fitters[0];
55 #endif
56 
57 
58  //TODO don't hardcode these
59  double lower_limit = config->spectral_fit_start;
60  double upper_limit = config->spectral_fit_stop;
61 
62  double df = spectrum->GetX()[1]-spectrum->GetX()[0];
63  double f0 = spectrum->GetX()[0];
64 
65  int low = (lower_limit - f0) / df;
66  int high = (upper_limit - f0) / df + 0.5;
67  if (high > spectrum->GetN()) high = spectrum->GetN();
68 
69  int N = high - low +1;
70 
71 
72  std::vector<double> x(N);
73  std::vector<double> y(N);
74 
75  const double * xx = spectrum->GetX() + low;
76  const double * yy = spectrum->GetY() + low;
77 
78  memcpy(&x[0], xx, N * sizeof(double));
79  memcpy(&y[0], yy, N * sizeof(double));
80 
81 
82  //subtract off average spectrum
83  for (size_t i = 0; i < x.size(); i++)
84  {
85  y[i] -= average->Eval(x[i]);
86  }
87 
88  double m = 0;
89  double b = 0;
90 
91  std::vector<bool> used(x.size(), false);
92 
93  for (int i = 0; i < AnitaEventSummary::peaksPerSpectrum; i++)
94  {
95  double max_val = -DBL_MAX;
96  int max_j = -1;
97 
98  for (unsigned j = 0; j < x.size(); j++)
99  {
100  if (used[j]) continue;
101  double val = y[j];// - (m * x[j] + b);
102 // printf("%d | %f, %f\n", i, x[j], val);
103  if (val > max_val)
104  {
105  max_j = j;
106  max_val = val;
107  }
108  }
109 
110 // printf("\t max_i: %f\n", xx[max_i]);
111 
112  int index_bounds[2] = {max_j,max_j};
113  double power = TMath::Power(10,max_val/10);
114 
115  for (int sign = -1; sign <=1; sign+=2)
116  {
117  int how_far = 1;
118  while(true)
119  {
120  int jj = max_j + how_far * sign;
121  double val = jj>=0 && jj < N ? y[jj] : 0;
122 // printf("%f\n",max_val - val);
123  if (jj <= 0 || jj >=N-1 || max_val - val > config->bw_ndb || used[jj])
124  {
125  index_bounds[(sign+1)/2] = jj;
126  break;
127  }
128  power += TMath::Power(10,val/10);
129  how_far++;
130  }
131  }
132 
133 
134  int start = index_bounds[0];
135  int end = index_bounds[1];
136  if (start < 0) start = 0;
137  if (end >= (int) x.size()) end = x.size()-1;
138  int difference = end - start - 1;
139  if (difference < 1){
140  difference = 1;
141  }
142  winfo->peakPower[i] = 10 * log10(power/difference);
143 
144  winfo->bandwidth[i] = x[end] - x[start] - 0.01;
145  if (winfo->bandwidth[i]< 0.01){
146  winfo->bandwidth[i] = 0.01;
147  }
148  winfo->peakFrequency[i] = (x[start] + x[end])/2;
149 
150 
151 // printf("%d %d\n",start,end);
152 // printf("%f %f\n", power, x[max_j]);
153 // printf("%f %f\n", x[end]-x[start],winfo->peakFrequency[i] );
154 
155 
156  //now remove adjacents up to ndb below
157 
158  for (int u= start; u < end; u++) used[u] = true;
159 
160  }
161 
162 
163  // fit for slope
164  fitter->ClearPoints();
165 
166  //TODO do I need to mutex this?!?
167  fitter->AssignData(x.size(),1,&x[0] ,&y[0]);
168 
169  fitter->Eval();
170  m = fitter->GetParameter(1);
171  b = fitter->GetParameter(0);
172 
173  winfo->spectrumSlope = m;
174  winfo->spectrumIntercept = b;
175 
176 }
struct __attribute__((packed))
Debugging use only TURF scaler data.
static const Int_t peaksPerSpectrum
The maximum number of hypotheses storable per polarization */.
Double_t bandwidth[peaksPerSpectrum]
Total power in xPol waveform.
Stores information about a coherently summed waveform (filtered/unfiltered/deconvolved) The coherent ...