Tools.cc
1 #include "Tools.h"
2 #include <iostream>
3 #include <cmath>
4 #include "TSpline.h"
5 #include "TH2F.h"
6 #include <fstream>
7 #include "TTree.h"
8 #include "TGraph.h"
9 #include "TCanvas.h"
10 #include "Constants.h"
11 #include "icemc_random.h"
12 
13 
14 //using std::cout;
15 using namespace std;
16 
17 
18 void Tools::MakeGraph(int index, const int n,double *time,double *volts,TGraph *&mygraph,TH2F *&h2, double scalex,double scaley,string xaxistitle,string yaxistitle) {
19 
20  double maxtime=-1.E20;
21  double maxv=-1.E20;
22  double mintime=1E20;
23  double minv=1.E20;
24 
25  double timecopy[n];
26  double voltscopy[n];
27 
28  for (int i=0;i<n;i++) {
29  timecopy[i]=time[i];
30  voltscopy[i]=volts[i];
31  timecopy[i]*=scalex;
32  voltscopy[i]*=scaley;
33  }
34  for (int i=0;i<n;i++) {
35  if (timecopy[i]>maxtime)
36  maxtime=timecopy[i];
37  if (timecopy[i]<mintime)
38  mintime=timecopy[i];
39  if (voltscopy[i]>maxv)
40  maxv=voltscopy[i];
41  if (voltscopy[i]<minv)
42  minv=voltscopy[i];
43  }
44 
45  mygraph=new TGraph(n,timecopy,voltscopy);
46 
47  h2=new TH2F(Form("h2%d",index),"",10*n,mintime*1.1,maxtime*1.1,100,minv*1.1,maxv*1.1);
48  h2->SetLineWidth(3);
49  h2->SetXTitle(xaxistitle.c_str());
50  h2->SetYTitle(yaxistitle.c_str());
51 }
52 
53 
54 int Tools::iSum(int* thisarray,int n) {
55 
56  int sum=0;
57  for (int i=0;i<n;i++) {
58  sum+=thisarray[i];
59  } //for
60  return sum;
61 } //iSum
62 
63 
64 double Tools::getMaxMagnitude(vector<double> v) {
65  double mag=0.;
66  for (int i=0;i<(int)v.size();i++) {
67  if (v[i]>mag)
68  mag=v[i];
69  }
70  return mag;
71 }
72 
73 
74 void Tools::ShiftLeft(double *x,const int n,int ishift) {
75 
76  double x_temp[n];
77  // shift the x array to the left by ishift bins and fill the gap with zeroes
78  for (int i=0;i<n;i++) {
79  x_temp[i]=x[i];
80  }
81  for (int i=0;i<n-ishift;i++) {
82  x[i]=x_temp[i+ishift];
83  }
84  for (int i=n-ishift;i<n;i++) {
85  x[i]=0.;
86  }
87 }
88 
89 
90 void Tools::ShiftRight(double *x,const int n,int ishift) {
91 
92  double x_temp[n];
93  // shift the x array to the right by ishift bins and fill the gap with zeroes
94  for (int i=0;i<n;i++) {
95  x_temp[i]=x[i];
96  }
97  for (int i=ishift;i<n;i++) {
98  x[i]=x_temp[i-ishift];
99  }
100  for (int i=0;i<ishift;i++) {
101  x[i]=0.;
102  }
103 }
104 
105 
106 void Tools::realft(double *data, const int isign, int nsize){
107  int i, i1, i2, i3, i4;
108  double c1=0.5,c2,h1r,h1i,h2r,h2i,wr,wi,wpr,wpi,wtemp,theta;
109  theta=3.141592653589793238/(nsize>>1);
110  if (isign == 1) {
111  c2 = -0.5;
112  four1(data,1,nsize);
113  } else {
114  c2=0.5;
115  theta = -theta;
116  }
117  wtemp=sin(0.5*theta);
118  wpr = -2.0*wtemp*wtemp;
119  wpi=sin(theta);
120  wr=1.0+wpr;
121  wi=wpi;
122  for (i=1;i<(nsize>>2);i++) {
123  i2=1+(i1=i+i);
124  i4=1+(i3=nsize-i1);
125  h1r=c1*(data[i1]+data[i3]);
126  h1i=c1*(data[i2]-data[i4]);
127  h2r= -c2*(data[i2]+data[i4]);
128  h2i=c2*(data[i1]-data[i3]);
129  data[i1]=h1r+wr*h2r-wi*h2i;
130  data[i2]=h1i+wr*h2i+wi*h2r;
131  data[i3]=h1r-wr*h2r+wi*h2i;
132  data[i4]= -h1i+wr*h2i+wi*h2r;
133  wr=(wtemp=wr)*wpr-wi*wpi+wr;
134  wi=wi*wpr+wtemp*wpi+wi;
135  }
136  if (isign == 1) {
137  data[0] = (h1r=data[0])+data[1];
138  data[1] = h1r-data[1];
139  } else {
140  data[0]=c1*((h1r=data[0])+data[1]);
141  data[1]=c1*(h1r-data[1]);
142  four1(data,-1,nsize);
143  }
144 }
145 
146 
147 void Tools::SWAP(double &a, double &b){
148  double dum = a;
149  a = b;
150  b = dum;
151 }
152 
153 
154 void Tools::four1(double *data, const int isign,int nsize) {
155  int n,mmax,m,j,istep,i;
156  double wtemp,wr,wpr,wpi,wi,theta,tempr,tempi;
157 
158  int nn=nsize/2;
159  n=nn << 1;
160  j=1;
161  for (i=1;i<n;i+=2) {
162  if (j > i) {
163  SWAP(data[j-1],data[i-1]);
164  SWAP(data[j],data[i]);
165  }
166  m=nn;
167  while (m >= 2 && j > m) {
168  j -= m;
169  m >>= 1;
170  }
171  j += m;
172  }
173  mmax=2;
174  while (n > mmax) {
175  istep=mmax << 1;
176  theta=isign*(6.28318530717959/mmax);
177  wtemp=sin(0.5*theta);
178  wpr = -2.0*wtemp*wtemp;
179  wpi=sin(theta);
180  wr=1.0;
181  wi=0.0;
182  for (m=1;m<mmax;m+=2) {
183  for (i=m;i<=n;i+=istep) {
184  j=i+mmax;
185  tempr=wr*data[j-1]-wi*data[j];
186  tempi=wr*data[j]+wi*data[j-1];
187  data[j-1]=data[i-1]-tempr;
188  data[j]=data[i]-tempi;
189  data[i-1] += tempr;
190  data[i] += tempi;
191  }
192  wr=(wtemp=wr)*wpr-wi*wpi+wr;
193  wi=wi*wpr+wtemp*wpi+wi;
194  }
195  mmax=istep;
196  }
197 }
198 
199 
200 double Tools::dSquare(double *p) {
201  return p[0]*p[0]+p[1]*p[1]+p[2]*p[2];
202 } //dSquare
203 
204 
205 int Tools::WhichIsMin(double *x,int n) {
206  double min=1.E22;
207  int imin=0;
208  for (int i=0;i<n;i++) {
209  if (x[i]<min) {
210  min=x[i];
211  imin=i;
212  }
213  } //for
214  return imin;
215 } //WhichIsMin
216 
217 
218 void Tools::Print(double *p,int i) {
219  for (int j=0;j<i;j++) {
220  cout << p[j] << " ";
221  } //for
222  cout << "\n";
223 } //Print (double*,int)
224 
225 
226 void Tools::Print(int *p,int i) {
227  for (int j=0;j<i;j++) {
228  cout << p[j] << " ";
229  } //for
230  cout << "\n";
231 } //Print (double*,int)
232 
233 
234 void Tools::GetNextNumberAsString(ifstream& fin, ofstream& fout, string& number) {
235  string temp;
236  getline(fin,temp); // get next line of the input file
237 
238  fout << temp << "\n"; // output this line to the summary file
239 
240  int place=0;
241  place=temp.find_first_of(" \t"); // find where the first space or tab is
242 
243  number=temp.substr(0,place); // everything up until the first space is what we're looking for
244 } //GetNextNumberAsString
245 
246 
247 void Tools::GetNumbersAsStringArray(ifstream& fin, ofstream& fout,vector<string>& vnumbers, int nelements) {
248  string temp;
249  //getline(fin,temp);
250 
251  //fout << temp << "\n";
252 
253  // int place_previous=0;
254  //int place_next;
255  vnumbers.clear();
256  string s;
257  for (int n=0;n<nelements;n++) {
258  fin >> s;
259  fout << s << "\t"; // output this line to the summary file
260  vnumbers.push_back(s);
261  // place_next=temp.find_first_of("\t",place_previous+1); // find where first tab is
262  //vnumbers.push_back(temp.substr(place_previous,place_next-place_previous));
263  //place_previous=place_next;
264  }
265  getline(fin,temp);
266  fout << temp << "\n";
267  // cout << "temp is " << temp << "\n";
268 }
269 
270 
271 void Tools::GetNext2NumbersAsString(ifstream& fin,ofstream& fout,string& number1,string& number2, string& stherest) {
272 
273  string temp;
274  getline(fin,temp); // get next line of the input file
275 
276  fout << temp << "\n"; // output this line to the summary file
277 
278  int place=0;
279  place=temp.find_first_of(" \t"); // find where the first space is
280 
281  number1=temp.substr(0,place); // everything up until the first space is what we're looking for
282 
283  temp=temp.substr(place+1,temp.size());
284 
285  number2=temp.substr(0,temp.find_first_of(" "));
286 
287  stherest=temp.substr(2,temp.size());
288 } //GetNext2NumbersAsString
289 
290 
291 double Tools::GetFWHM(TH1 *h1) {
292  int imax=h1->GetMaximumBin();
293  double max=h1->GetMaximum();
294 
295  // cout << "imax, max are " << imax << " " << max << "\n";
296  int ibin_plus=0;
297  int ibin_minus=0;
298  // now step to the right until it's half
299  for (int ibin=imax;ibin<=h1->GetNbinsX();ibin++) {
300  if (h1->GetBinContent(ibin)<max/2.) {
301  ibin_plus=ibin;
302  ibin=h1->GetNbinsX()+1;
303  // cout << "ibin_plus is " << ibin_plus << "\n";
304  }
305  }
306  // now step to the left
307  for (int ibin=imax;ibin>=1;ibin--) {
308  if (h1->GetBinContent(ibin)<max/2.) {
309  ibin_minus=ibin;
310  ibin=0;
311  // cout << "ibin_minus is " << ibin_minus << "\n";
312  }
313  }
314 
315  if (ibin_plus>0 && ibin_minus==0) {
316  ibin_minus=1;
317  //cout << "bin_minus is " << ibin_minus << "\n";
318  }
319 
320  if (ibin_plus==0 && ibin_minus==0) {
321  cout << "Found 0 FWHM.\n";
322  return 0.;
323  }
324 
325  return (h1->GetBinCenter(ibin_plus)-h1->GetBinCenter(ibin_minus))/2.;
326 }
327 
328 
329 int Tools::NonZero(double *anarray,int n) { // how many bins are nonzero
330  int count=0;
331  for (int i=0;i<n;i++) {
332  cout << "i, anarray is " << i << "\t" << anarray[i] << "\n";
333  if (anarray[i]!=0)
334  count++;
335  }
336  return count;
337 }
338 
339 
340 void Tools::Zero(int *anarray,int n) {
341  for (int i=0;i<n;i++) {
342  anarray[i]=0;
343  } //for
344 } //Zero (int*,int)
345 
346 
347 void Tools::Zero(double *anarray,int n) {
348  for (int i=0;i<n;i++) {
349  anarray[i]=0.;
350  } //for
351 } //Zero (int*,int)
352 double Tools::dSum(double *anarray,int n) {
353  double sum=0.;
354  for (int i=0;i<n;i++) {
355  sum+=anarray[i];
356  } //for
357  return sum;
358 } //Zero (int*,int)
359 
360 
361 double Tools::dMinNotZero(const double *x,int n) {
362  double min=dMax(x,n);
363  if (min==0)
364  cout << "max is 0.\n";
365  for (int k=1;k<n;k++) {
366  if (x[k]<min && x[k]!=0)
367  min=x[k];
368  }
369  return min;
370 } //dMinNotZero(double*, int)
371 
372 
373 double Tools::dMin(const double *x,int n) {
374  double min=x[0];
375  for (int k=1;k<n;k++) {
376  if (x[k]<min)
377  min=x[k];
378  }
379  return min;
380 } //dMin(double*, int)
381 
382 
383 double Tools::dMin(double x,double y) {
384  double min=1.E22;
385  if (x<y)
386  min=x;
387  else
388  min=y;
389 
390  return min;
391 } //dMin(double,double)
392 
393 
394 double Tools::dMax(const double *x,int n) {
395 
396  double max=x[0];
397  for (int k=1;k<n;k++) {
398  if (x[k]>max)
399  max=x[k];
400  }
401  return max;
402 } //dMax(double*, int)
403 
404 
405 double Tools::dvMax(const vector<double> x) {
406 
407  double max=x[0];
408  for (int k=1;k<(int)x.size();k++) {
409  if (x[k]>max)
410  max=x[k];
411  }
412  return max;
413 } //dMax(double*, int)
414 
415 
416 double Tools::dsMax(TSpline5 *sp) {
417  vector<double> y;
418  double maxn;
419  double blah1,blah2;
420  for (int i=0;i<sp->GetNp();i++) {
421  sp->GetKnot(i,blah1,blah2);
422  y.push_back(blah2);
423  }
424  maxn=Tools::dvMax(y);
425  return maxn;
426 }
427 
428 
429 double Tools::dMax(double a,double b) {
430  if (a>b)
431  return a;
432  else if (a<b)
433  return b;
434  else if (a==b)
435  return a;
436  return 0;
437 } //dMax(double,double
438 
439 
440 int Tools::Getifreq(double freq,double freq_low,double freq_high,int n) {
441 
442  if (freq>=freq_high)
443  return -1;
444  if (freq<freq_low)
445  return -1;
446 
447  return (int)((freq-freq_low)/(freq_high-freq_low)*(double)n);
448 } //Getifreq
449 
450 
451 void Tools::InterpolateReal(double* array, const unsigned n){
452  // to get rid of the zero bins
453  double previous_nonzero = 0.;
454  double next_nonzero = 0.;
455  double check;
456  unsigned ifirstnonzero = 0;
457  unsigned ilastnonzero = 0;
458  unsigned k;
459  unsigned m = 0;
460  unsigned count_nonzero = 0;
461 
462  // find the first nonzero even element
463  while (!array[m]){
464  m++;
465  }
466 
467  ifirstnonzero = m;
468 
469 
470  // count the nonzero elements
471  for (unsigned i = 0; i < n; i++) {
472  if (array[i] != 0)
473  count_nonzero++;
474  }
475 
476  if (count_nonzero) {
477  // loop through the elements of the array and replace the zeros with interpolated values
478  for (unsigned i = ifirstnonzero; i < n; i++) {
479  if (array[i]) {
480  // set the lower nonzero value that we are interpolating from
481  previous_nonzero = array[i];
482  }
483  else{
484  check = 0.;
485  k = i;
486  while (check == 0. && k < n) {
487  check = array[k];
488  k++;
489  }
490  if (k < n){
491  next_nonzero = check;
492  for (unsigned j = i; j < k; j++) {
493  array[j] = previous_nonzero + (next_nonzero - previous_nonzero) * double(j - (i - 1)) / double(k - (i - 1));
494  }
495  i = k - 1;
496  previous_nonzero = next_nonzero;
497  }
498  else {
499  ilastnonzero = i - 1;
500  i = n;
501  }
502  } // end if array=0
503  } // end loop over i
504  for (unsigned j = 0; j < n; j++){
505  array[2 * j] *= sqrt(double(count_nonzero) / double(ilastnonzero - ifirstnonzero));
506  }
507  }
508 }
509 
510 
511 void Tools::InterpolateComplex(double *array, const unsigned n) {
512  // to get rid of the zero bins
513  double previous_nonzero = 0.;
514  double next_nonzero = 0.;
515  double check;
516  unsigned ifirstnonzero = 0;
517  unsigned ilastnonzero = 0;
518  unsigned k;
519  unsigned m = 0;
520  unsigned count_nonzero = 0;
521 
522  // find the first nonzero even element
523  while (array[2 * m] == 0){
524  m++;
525  }
526 
527  ifirstnonzero = m;
528 
529  // count the nonzero elements
530  for (unsigned i = 0; i < n; i++) {
531  if (array[2 * i] != 0)
532  count_nonzero++;
533  }
534 
535  if (count_nonzero) {
536  // loop through the elements of the array and replace the zeros with interpolated values
537  for (unsigned i = ifirstnonzero; i < n; i++) {
538  if (array[2 * i] != 0.) {
539  // set the lower nonzero value that we are interpolating from
540  previous_nonzero = array[2 * i];
541  }
542  else{
543  check = 0.;
544  k = i;
545  while (check == 0. && k < n) {
546  check = array[2 * k];
547  k++;
548  }
549  if (k < n){
550  next_nonzero = check;
551  for (unsigned j = i; j < k; j++) {
552  array[2 * j] = previous_nonzero + (next_nonzero - previous_nonzero) * (double)(j - (i - 1)) / (double)(k - (i - 1));
553  array[2 * j + 1] = array[2 * j];
554  }
555  i = k - 1;
556  previous_nonzero = next_nonzero;
557  }
558  else {
559  ilastnonzero = i - 1;
560  i = n;
561  }
562  } // end if array=0
563  } // end loop over i
564  for (unsigned j = 0; j < n; j++){
565  array[2 * j] *= sqrt((double)count_nonzero / (double)(ilastnonzero - ifirstnonzero));
566  array[2 * j + 1] *= sqrt((double)count_nonzero / (double)(ilastnonzero - ifirstnonzero));
567  }
568  }
569 }
570 
571 
572 void Tools::NormalTimeOrdering(const int n, double* volts) {
573  double volts_temp[n];
574  for (int i=0;i<n/2;i++) {
575  volts_temp[i]=volts[i+n/2];
576  volts_temp[i+n/2]=volts[i];
577  }
578  for (int i=0;i<n;i++) {
579  volts[i]=volts_temp[i];
580  }
581 }
582 
583 void Tools::reverseTimeOrdering(const int n, double* bitsin,double *bitsout) {
584  double bits_temp[n];
585  for (int i=0;i<n;i++) {
586  bits_temp[i]=bitsin[n-i-1];
587  }
588  for (int i=0;i<n;i++) {
589  bitsout[i]=bitsin[i];
590  }
591 }
592 void Tools::reverseTimeOrdering(const int n, int* bitsin,int *bitsout) {
593  int bits_temp[n];
594  for (int i=0;i<n;i++) {
595  bits_temp[i]=bitsin[n-i-1];
596  }
597  for (int i=0;i<n;i++) {
598  bitsout[i]=bitsin[i];
599  }
600 }
601 
602 
603 int Tools::findIndex(double *freqlab,double freq,int npoints,double min,double max) {
604 
605  // cout << "inside findIndex, freq, min are " << freq << " " << min << "\n";
606 
607  if (freq<min)
608  return -1;
609  if (freq>=min && freq<freqlab[1])
610  return 0;
611  if (freq>=freqlab[npoints-1] && freq<max)
612  return npoints-1;
613  return ((int)((freq-freqlab[0])/(freqlab[1]-freqlab[0])))+1;
614 
615  return -1;
616 }
617 
618 
619 void Tools::get_random_rician(double signal_amplitude, double signal_phase, double sigma, double &amplitude, double &phase){
620  double rand_gauss_a, rand_gauss_b;
621  get_circular_bivariate_normal_random_variable(rand_gauss_a, rand_gauss_b);
622 
623  // Gives the gaussian-distributed random variables a standard deviation of sigma
624  rand_gauss_a *= sigma;
625  rand_gauss_b *= sigma;
626 
627  // Gives the gaussian-distributed random variables a mean of (v*cos(theta), v*sin(theta)) when v is the mean of the desired rician distribution
628  rand_gauss_a += signal_amplitude * cos(signal_phase);
629  rand_gauss_b += signal_amplitude * sin(signal_phase);
630 
631  // The Rician Distribution produces the probability of the the absolute value (radius) of a circular bivariate normal random variable:
632  amplitude = sqrt(rand_gauss_a * rand_gauss_a + rand_gauss_b * rand_gauss_b);
633  // Thus, the descriptor other than amplitude for the circular bivariate is given by a phase:
634  phase = atan2(rand_gauss_b, rand_gauss_a);
635  return;
636 }
637 
638 
639 void Tools::get_circular_bivariate_normal_random_variable(double& rand_gauss_a, double& rand_gauss_b){
640  TRandom * rng =getRNG(RNG_RICIAN);
641 
642  double rand_uni_a = rng->Rndm(); //gRandom->Rndm() produces uniformly-distributed floating points in ]0,1]
643  double rand_uni_b = rng->Rndm();
644  double first_term = sqrt(-2. * log(rand_uni_a));
645  // Box-Muller transform from a bivariate uniform distribution from 0 to 1 to a gaussian with mean = 0 and sigma = 1
646  rand_gauss_a = first_term * cos(2. * M_PI * rand_uni_b);
647  rand_gauss_b = first_term * sin(2. * M_PI * rand_uni_b);
648  return;
649 }
650 
651 
652 int Tools::round(double number){
653  return (number > 0.0) ? floor(number + 0.5) : ceil(number - 0.5);
654 }
655 
656 
657 double Tools::AbbyPhiCalc(double x_abby, double y_abby){
658  double abbyanglephi = 0;
659 
660  if(x_abby>=0 && y_abby>=0) //first quadrant
661  abbyanglephi= atan(y_abby/x_abby)*DEGRAD;
662  if(x_abby<0 && y_abby>=0) //second quadrant
663  abbyanglephi= atan(y_abby/x_abby)*DEGRAD+180.;
664  if(x_abby<0 && y_abby<0) //third quadrant
665  abbyanglephi= atan(y_abby/x_abby)*DEGRAD+180.;
666  if(x_abby>=0 && y_abby<0) //fourth quadrant
667  abbyanglephi= atan(y_abby/x_abby)*DEGRAD+360.;
668  //else abbyanglephi=0.;
669  if(x_abby==0 && y_abby>=0)
670  abbyanglephi=90.;
671  if(x_abby==0 && y_abby<=0)
672  abbyanglephi=270.;
673  //cout<<"abby's phi is "<< abbyanglephi<<endl;
674  return abbyanglephi;
675 }
676 
677 TGraph *Tools::getInterpolatedGraph(TGraph *grIn, Double_t deltaT)
678 {
679  //Will use the ROOT::Math::Interpolator function to do this.
680  std::vector<double> tVec;
681  std::vector<double> vVec;
682 
683  Int_t numIn=grIn->GetN();
684  Double_t tIn,vIn;
685 
686  Double_t startTime=0;
687  Double_t lastTime=0;
688  for (int samp=0;samp<numIn;samp++) {
689  grIn->GetPoint(samp,tIn,vIn);
690  tVec.push_back(tIn);
691  vVec.push_back(vIn);
692  //std::cout << "samp " << samp << " t " << tIn << " v " << vIn << " this-last " << tIn-tVec[tVec.size()-2] << std::endl;
693  if(samp==0)
694  startTime=tIn;
695  lastTime=tIn;
696  }
697  if(tVec.size()<1) {
698  std::cout << "Insufficent points for interpolation\n";
699  return NULL;
700  }
701 
702  //Bastards
703  ROOT::Math::Interpolator chanInterp(tVec,vVec,ROOT::Math::Interpolation::kAKIMA);
704 
705  Int_t roughPoints=Int_t((lastTime-startTime)/deltaT);
706 
707 
708  Double_t *newTimes = new Double_t[roughPoints+100]; //Will change this at some point, but for now
709  Double_t *newVolts = new Double_t[roughPoints+100]; //Will change this at some point, but for now
710  Int_t numPoints=0;
711  for(Double_t time=startTime;time<=lastTime;time+=deltaT) {
712  newTimes[numPoints]=time;
713  newVolts[numPoints]=chanInterp.Eval(time);
714  // std::cout << numPoints << "\t" << newTimes[numPoints]
715  // << "\t" << newVolts[numPoints] << std::endl;
716 
717  numPoints++;
718  }
719 
720  TGraph *grInt = new TGraph(numPoints,newTimes,newVolts);
721  delete [] newTimes;
722  delete [] newVolts;
723  return grInt;
724 
725 }
726 
727 
728 double Tools::calculateSNR(double justSig[512], double justNoise[512]){
729 
730  double p2p = Tools::dMax(justSig, 512) - Tools::dMin(justSig, 512) ;
731  double rms = 0;
732 
733  for (int i=0; i<256; i++){
734  rms += justNoise[i]*justNoise[i];
735  }
736 
737  rms/=256.;
738  rms=TMath::Sqrt(rms);
739 
740  return p2p/(2*rms);
741 
742 }
743 
744 
745 
746 void Tools::ConvertHVtoLRTimedomain(const int nfour,double *vvolts,
747  double *hvolts,
748  double *left,double *right) {
749 
750  // nfour is the real and complex values from -F to F
751 
752  // first perform fft on each of h and v
753  // find l, r polarizations
754  // take fft back
755 
756  double hvolts_f[nfour/2];
757  double vvolts_f[nfour/2];
758  for (int i=0;i<nfour/2;i++) {
759  hvolts_f[i]=hvolts[i];
760  vvolts_f[i]=vvolts[i];
761  }
762 
763  realft(hvolts_f,1,nfour/2);
764  realft(vvolts_f,1,nfour/2);
765 
766  for (int i=0;i<nfour/4;i++) {
767  right[2*i]=1/sqrt(2.)*(vvolts_f[2*i]-hvolts_f[2*i+1]);
768  left[2*i]=1/sqrt(2.)*(hvolts_f[2*i]-vvolts_f[2*i+1]);
769 
770  right[2*i+1]=1/sqrt(2.)*(vvolts_f[2*i+1]+hvolts_f[2*i]);
771  left[2*i+1]=1/sqrt(2.)*(hvolts_f[2*i+1]+vvolts_f[2*i]);
772 
773 
774  left[2*i]=left[2*i]*2./((double)nfour/2.);
775  left[2*i+1]=left[2*i+1]*2./((double)nfour/2.);
776 
777  right[2*i]=right[2*i]*2./((double)nfour/2.);
778  right[2*i+1]=right[2*i+1]*2./((double)nfour/2.);
779  }
780 
781  realft(left,-1,nfour/2);
782  realft(right,-1,nfour/2);
783 
784  // now take fft back
785 
786 }
STL namespace.
void ConvertHVtoLRTimedomain(const int nfour, double *vvolts, double *hvolts, double *left, double *right)
Definition: Tools.cc:746