Logo coherent WaveBurst  
Library Reference Guide
Logo
PCA_Benchmark.C
Go to the documentation of this file.
1 #include <vector>
2 
3 // --------------------------------------------------------
4 // PRINCIPAL COMPONENT ANALYSIS
5 // --------------------------------------------------------
6 
7 #define PCA_NRES 7 // number of resolutions
8 #define PCA_IRES 4 // initial resolution
9 
10 #define PCA_MAX 60 // number of max PC extractions
11 #define PCA_PREC 0.01 // precision = Eresidual/Esignal
12 
13 #define ACORE 1.7 // set selection threshold : Ethr = 2*ACORE*ACORE
14 
15 //#define APPLY_TSHIFT // enable/disable (uncomment/comment) find best wavelet time shift
16 
17 //#define OUTPUT_FILE "pca_benchmark.txt"
18  // enable/disable (uncomment/comment) write precision vs nPC to ascii file
19 
20 // --------------------------------------------------------
21 // SIGNAL
22 // --------------------------------------------------------
23 
24 #define WAVE_TYPE "INSPIRAL" // inspiral
25 //#define WAVE_TYPE "SGE" // sine gaussian
26 //#define WAVE_TYPE "WNB" // white noise burst
27 //#define WAVE_TYPE "GA" // gaussian
28 
29 #define WAVE_LENGTH 6 // Is effective only for WAVE_TYPE = SGE,WNB,GA (def=1sec)
30 
31 #define WAVE_POL "hp" // select hp,hx component
32 
33 #define WAVE_SNR 30 // signal SNR
34 
35 // --------------------------------------------------------
36 // WDM
37 // --------------------------------------------------------
38 
39 #define WDM_NU 6 // WDM mayer order
40  // NOTE : wp_xtalk coefficients are defined only for WDM_NU=2,4,6
41 #define WDM_PREC 10 // WDM precision
42 #define WDM_TDSIZE 12 // Time Delay filter size
43 //#define WDM_SCRATCH 14.0 // WDM TF segment scratch (sec)
44 #define WDM_SCRATCH 27.0 // WDM TF segment scratch (sec)
45 //#define WDM_SCRATCH 54.0 // WDM TF segment scratch (sec)
46 
47 //#define LOAD_XTALK_CATALOG // enable/disable (uncomment/comment) xcatalog
48 //#define XTALK_CATALOG "wdmXTalk/OverlapCatalog16-1024.bin" // WDM_NU = 4
49 #define XTALK_CATALOG "wdmXTalk/OverlapCatalog-ilLev4-hLev10-iNu6-P10.bin" // WDM_NU = 6
50 
51 // --------------------------------------------------------
52 // WAVELET PACKET
53 // --------------------------------------------------------
54 
55 #define NWP 4 // number of WP types used in the PCA
56 
57 #define WP_SINGLE // enable/disable (uncomment/comment) single wavelet
58 //#define WP_DIAG // enable/disable (uncomment/comment) diagonal WP
59 //#define WP_HORIZ // enable/disable (uncomment/comment) horizontal WP
60 //#define WP_VERT // enable/disable (uncomment/comment) vertical WP
61 //#define WP_BDIAG // enable/disable (uncomment/comment) back-diagonal WP
62 
63  // enable/disable (uncomment/comment) wavelet packet analysis
64  // NOTE : for more infos see InitXTALK function
65  // ----------------------------------------------------------
66  // Phase 00 Phase 90
67  // - - c*w - - C*W
68  // - b*v - - B*V -
69  // a*u - - A*U - -
70  //
71  // WAVELET PACKET BASE
72  // parity = odd -> z=U+v+W , Z=u-V+w
73  // parity = even -> z=U-v+W , Z=u+V+w
74  // (v,U) = (v,W) = -(V,u) = -(V,w) = WP_XTALK
75  // (W,u) = (U,w) = 0
76  // (z,z) = (Z,Z) = 3+4*WP_XTALK
77  // (z,Z) = 0
78  //
79  // DATA
80  // x=a*u+b*v+c*w
81  // A00 = (x,z)/(z,z) // A00 is the amplitude of x respect to z
82  // A90 = (x,Z)/(Z,Z) // A90 is the amplitude of x respect to Z
83  // parity = odd
84  // A00 = (x,z) = (x,U+v+W) = A+b+C
85  // A90 = (x,Z) = (x,u-V+w) = a-B+c
86  // parity = even
87  // A00 = (x,z) = (x,U-v+W) = A-b+C
88  // A90 = (x,Z) = (x,u+V+w) = a+B+c
89  // ----------------------------------------------------------
90 
91 // --------------------------------------------------------
92 // PLOTS
93 // --------------------------------------------------------
94 
95 //#define PLOT_MONSTER // enable/disable (uncomment/comment) show likelihood TFmap
96 #define PLOT_REC_VS_INJ // enable/disable (uncomment/comment) show time rec vs inj waveform
97 
98 //#define PLOT_TFMAP // enable/disable (uncomment/comment) show TF map @ PLOT_TFRES resolution
99 #define PLOT_TFRES 6 // TF frequency resolution showed
100 #define PLOT_MINFREQ 0 // min freq shoed in the TF plots
101 #define PLOT_MAXFREQ 256 // max freq shoed in the TF plots
102 #define PLOT_TFTYPE 2
103  // 0 : sqrt((E00+E90)/2)
104  // 1 : (E00+E90)/2
105  // 2 : sqrt((E00+E90)/2)
106  // 3 : amplitude:00
107  // 4 : energy:00
108  // 5 : |amplitude:00|
109  // 6 : amplitude:90
110  // 7 : energy:90
111  // 8 : |amplitude:90|
112 
113 // --------------------------------------------------------
114 // DECLARATIONS
115 // --------------------------------------------------------
116 
117 WDM<double>* wdm[PCA_NRES]; // define a WDM transform containers
118 WSeries<double> wsig[PCA_NRES]; // waveform TF map containers
120 gwavearray<double>* grec; // used to plot rec
121 gwavearray<double>* gsig; // used to plot sig
122 watplot *WTS[3]; // plot objects
123 double wp_xtalk[4];
124 bool wp_mask[5];
125 
126 #ifdef LOAD_XTALK_CATALOG
127 monster wdmMRA; // wdm multi-resolution analysis
128 void CheckXTalkCatalog(int l_low, int l_high);
129 double GetPrincipalFactor(int nmax, int mmax, int rmax);
130 #endif
131 
132 int GetPrincipalComponent(double &amax, int &nmax, int &mmax, int &rmax);
133 
134 void InitXTALK();
135 
137 
138  //
139  // Principal Component Analysis : Test Macro
140  // Extraction of Pincipal Components from multiresolution TF maps
141  // Author : Gabriele Vedovato
142 
143  if(TString("hp")!=WAVE_POL && TString("hx")!=WAVE_POL) {
144  cout << "Error - Bad WAVE_POL value : must be hp or hx" << endl;
145  exit(1);
146  }
147 #ifdef PLOT_TFMAP
148  if(PLOT_TFRES<0 || PLOT_TFRES>=PCA_NRES) {
149  cout << "Error - Bad PLOT_TFRES value : must be >=0 && < " << PCA_NRES << endl;
150  exit(1);
151  }
152 #endif
153 #ifdef OUTPUT_FILE
154  ofstream out;
155  out.open(OUTPUT_FILE,ios::out);
156 #endif
157 
158  for(int l=0;l<=NWP;l++) wp_mask[l] = false;
159 #ifdef WP_SINGLE
160  wp_mask[0] = true;
161 #endif
162 #ifdef WP_DIAG
163  wp_mask[1] = true;
164 #endif
165 #ifdef WP_HORIZ
166  wp_mask[2] = true;
167 #endif
168 #ifdef WP_VERT
169  wp_mask[3] = true;
170 #endif
171 #ifdef WP_BDIAG
172  wp_mask[4] = true;
173 #endif
174 
175  InitXTALK(); // init wp_xtalk coefficients
176 
177  CWB::mdc MDC;
179 
180  if(WAVE_TYPE == "INSPIRAL") {
181  // ---------------------------------
182  // set inspiral parameters
183  // ---------------------------------
184  TString inspOptions="";
185  inspOptions = "--time-step 60.0 --time-interval 0 ";
186  inspOptions+= "--l-distr random ";
187  inspOptions+= "--gps-start-time 931072160 --gps-end-time 931072235 ";
188  inspOptions+= "--d-distr volume --m-distr totalMassRatio --i-distr uniform ";
189 
190 // inspOptions+= "--f-lower 80.000000 "; // 0.5 sec
191 // inspOptions+= "--f-lower 50.000000 "; // 1.5 sec
192 // inspOptions+= "--f-lower 40.000000 "; // 3 sec
193  inspOptions+= "--f-lower 30.000000 "; // 6.5 sec
194 // inspOptions+= "--f-lower 20.000000 "; // 19 sec
195 // inspOptions+= "--f-lower 10.000000 "; // 122 sec
196  inspOptions+= "--min-mass1 5.0 --max-mass1 5.0 ";
197  inspOptions+= "--min-mass2 5.0 --max-mass2 5.0 ";
198  inspOptions+= "--min-mtotal 10.0 --max-mtotal 10.0 ";
199 
200 /*
201  inspOptions+= "--f-lower 40.000000 ";
202  inspOptions+= "--min-mass1 10.0 --max-mass1 10.0 ";
203  inspOptions+= "--min-mass2 10.0 --max-mass2 10.0 ";
204  inspOptions+= "--min-mtotal 20.0 --max-mtotal 20.0 ";
205 */
206 /*
207  inspOptions+= "--f-lower 24.000000 ";
208  inspOptions+= "--min-mass1 42.1 --max-mass1 42.1 ";
209  inspOptions+= "--min-mass2 29.6 --max-mass2 29.6 ";
210  inspOptions+= "--min-mtotal 71.7 --max-mtotal 71.7 ";
211 */
212 /*
213  inspOptions+= "--f-lower 10.000000 ";
214  inspOptions+= "--min-mass1 75.000000 --max-mass1 75.000000 ";
215  inspOptions+= "--min-mass2 75.000000 --max-mass2 75.000000 ";
216  inspOptions+= "--min-mtotal 150.000000 --max-mtotal 150.000000 ";
217 */
218 
219  inspOptions+= "--m-distr componentMass ";
220  inspOptions+= "--min-distance 1000000.0 --max-distance 1500000.0 ";
221  inspOptions+= "--approximant EOBNRv2pseudoFourPN --disable-spin ";
222  inspOptions+= "--taper-injection start --seed 123456789 ";
223  inspOptions+= "--dir ./ ";
224  inspOptions+= "--output inspirals.xml "; // set output xml file
225 
226  MDC.SetInspiral("EOBNRv2",inspOptions);
227 
228  // Get the first waveform hp,hx components starting from gps = 931072160
229  sig = MDC.GetInspiral(WAVE_POL,931072160,931072235);
230  }
231 
232  if(WAVE_TYPE == "SGE") {
233  vector<mdcpar> par(2);
234  par[0].name="frequency"; par[0].value=100.;
235  par[1].name="Q"; par[1].value=8.9;
236  MDC.AddWaveform(MDC_SGE, par);
237  MDC.Print();
238  waveform wf = MDC.GetWaveform(0,0);
239  if(TString(WAVE_POL)=="hp") sig = wf.hp;
240  if(TString(WAVE_POL)=="hx") sig = wf.hx;
241  }
242 
243  if(WAVE_TYPE == "WNB") {
244  vector<mdcpar> par(6);
245  par[0].name="frequency"; par[0].value=250;
246  par[1].name="bandwidth"; par[1].value=100;
247  par[2].name="duration"; par[2].value=0.1;
248  par[3].name="pseed"; par[3].value=111;
249  par[4].name="xseed"; par[4].value=222;
250  par[5].name="mode"; par[5].value=1; // simmetric
251  MDC.AddWaveform(MDC_WNB, par);
252  MDC.Print();
253  waveform wf = MDC.GetWaveform(0,0);
254  if(TString(WAVE_POL)=="hp") sig = wf.hp;
255  if(TString(WAVE_POL)=="hx") sig = wf.hx;
256  }
257 
258  if(WAVE_TYPE == "GA") {
259  vector<mdcpar> par(1);
260  par[0].name="duration"; par[0].value=0.004;
261  MDC.AddWaveform(MDC_GA, par);
262  MDC.Print();
263  waveform wf = MDC.GetWaveform(0,0);
264  if(TString(WAVE_POL)=="hp") sig = wf.hp;
265  if(TString(WAVE_POL)=="hx") {
266  cout << "Error - WAVE=GA -> hx=0" << endl;
267  exit(1);
268  }
269  }
270 
271  cout << endl << "sig size : " << sig.size() << " sig rate : "
272  << sig.rate() << " sig start : " << (int)sig.start()
273  << " sig length : " << sig.size()/sig.rate() << " sec" << endl;
274  sig.start(0); // set start to 0 (needed by draw method)
275 
276  // add scratch array
277  int iscratch = WDM_SCRATCH*sig.rate();
278  wavearray<double> X(sig.size()+2*iscratch);
279  X.rate(sig.rate());
280  X=0.; for(int i=0;i<sig.size();i++) X[i+iscratch]=sig[i];
281  // apply time shift
282  //int jshift = 0.05*sig.rate();
283  //for(int i=jshift;i<sig.size();i++) X[i+iscratch]=sig[i-jshift];
284  sig=X;
285 
286  // resample data 16384 Hz -> 2048 Hz
287  sig.FFTW(1);
288  sig.resize(sig.size()/8);
289  sig.FFTW(-1);
290  sig.rate(sig.rate()/8.);
291 
292  // normalize sig energy to WAVE_SNR^2
293  double Esig=0.; for(int i=0;i<sig.size();i++) Esig+=sig[i]*sig[i];
294  for(int i=0;i<sig.size();i++) sig[i]/=sqrt(Esig)/sqrt(WAVE_SNR*WAVE_SNR);
295  Esig=WAVE_SNR*WAVE_SNR;
296 
297  // print pixel resolutions
298  cout << endl;
299  for(int level=PCA_NRES+PCA_IRES-1; level>=PCA_IRES; level--) {
300  int rateANA = int(sig.rate());
301  int layers = level>0 ? 1<<level : 0;
302  int rate = rateANA>>level;
303  cout << "level : " << level << "\t rate(hz) : " << rate << "\t layers : " << layers
304  << "\t df(hz) : " << rateANA/2./double(1<<level)
305  << "\t dt(ms) : " << 1000./rate << endl;
306  }
307  cout << endl;
308 
309 #ifdef APPLY_TSHIFT
310  cout << endl << "find best wavelet time shift ENABLED" << endl << endl;
311 #else
312  cout << endl << "find best wavelet time shift DISABLED" << endl << endl;
313 #endif
314  bool wpEnabled=false;
315  for(int l=0;l<=NWP;l++) if(wp_mask[l]) wpEnabled=true;
316  if(wpEnabled) {
317  cout << endl;
318  if(wp_mask[0]) cout << "wavelet packet single ENABLED" << endl;
319  if(wp_mask[1]) cout << "wavelet packet diagonal ENABLED" << endl;
320  if(wp_mask[2]) cout << "wavelet packet horizontal ENABLED" << endl;
321  if(wp_mask[3]) cout << "wavelet packet vertical ENABLED" << endl;
322  if(wp_mask[4]) cout << "wavelet packet back-diagonal ENABLED" << endl;
323  cout << endl;
324  } else {
325  cout << endl << "Error : all WP options are disabled !!!" << endl << endl;
326  exit(1);
327  }
328 
329  // compute WDM with PCA_NRES TF resolutions
330  cout << "Init WDM ..." << endl;
331  for(int i=PCA_NRES-1;i>=0;i--) {
332  int j=i+PCA_IRES;
333  wdm[i] = new WDM<double>(1<<j, 1<<j, WDM_NU, WDM_PREC); // define a WDM transform
334 
335  // check if filter lenght is less than cwb scratch length
336  double wdmFLen = double(wdm[i]->m_H)/sig.rate(); // sec
337  if(wdmFLen > WDM_SCRATCH) {
338  cout << endl;
339  cout << "Error - filter length must be <= WDM_SCRATCH !!!" << " RES = " << j << endl;
340  cout << "filter length : " << wdmFLen << " sec" << endl;
341  cout << "cwb scratch : " << WDM_SCRATCH << " sec" << endl;
342  exit(1);
343  }
344  }
345 
346 #ifdef LOAD_XTALK_CATALOG
347 
348  char filter_dir[1024];
349  if(gSystem->Getenv("HOME_WAT_FILTERS")==NULL) {
350  cout << "Error : environment HOME_WAT_FILTERS is not defined!!!" << endl;exit(1);
351  } else {
352  strcpy(filter_dir,TString(gSystem->Getenv("HOME_WAT_FILTERS")).Data());
353  }
354 
355  char MRAcatalog[1024];
356  sprintf(MRAcatalog,"%s/%s",filter_dir,XTALK_CATALOG);
357  cout << "cwb2G::Init - Loading catalog of WDM cross-talk coefficients ... " << endl;
358  cout << MRAcatalog << endl;
359  CWB::Toolbox::checkFile(MRAcatalog);
360 
361  wdmMRA.read(MRAcatalog);
362 
363  // check if analysis layers are contained in the MRAcatalog
364  // level : is the decomposition level
365  // layes : are the number of layers along the frequency axis rateANA/(rateANA>>level)
366 
367  int l_low = PCA_IRES;
368  int l_high = PCA_IRES+PCA_NRES-1;
369 
370  int check_layers=0;
371  for(int level=l_high; level>=l_low; level--) {
372  int layers = level>0 ? 1<<level : 0;
373  for(int j=0;j<wdmMRA.nRes;j++) if(layers==wdmMRA.layers[j]) check_layers++;
374  }
375 
376  if(check_layers!=PCA_NRES) {
377  cout << "Error : analysis layers do not match the MRA catalog" << endl;
378  cout << endl << "analysis layers : " << endl;
379  for(int level=l_high; level>=l_low; level--) {
380  int layers = level>0 ? 1<<level : 0;
381  cout << "level : " << level << " layers : " << layers << endl;
382  }
383  cout << endl << "MRA catalog layers : " << endl;
384  for(int i=0;i<wdmMRA.nRes;i++)
385  cout << "layers : " << wdmMRA.layers[i] << endl;
386  exit(1);
387  }
388 
389  //CheckXTalkCatalog(l_low, l_high);
390 #endif
391 
392  // compute TF of sig signal
393  cout << "Apply WDM Transform to signal ..." << endl;
394  for(int i=0;i<PCA_NRES;i++) {
395  wsig[i].Forward(sig, *wdm[i]); // apply the WDM to the time series
396  }
397 
398  // display TF sig
399 #ifdef PLOT_TFMAP
400  double dt0 = 1./wsig[PLOT_TFRES].rate();
401  double tbeg0 = WDM_SCRATCH-2*dt0;
402  double tend0 = dt0*wsig[PLOT_TFRES].size()/2-WDM_SCRATCH;
403  WTS[0] = new watplot(const_cast<char*>("wts0"));
404  WTS[0]->plot(&wsig[PLOT_TFRES], PLOT_TFTYPE, tbeg0, tend0, const_cast<char*>("COLZ"));
405  WTS[0]->hist2D->GetYaxis()->SetRangeUser(PLOT_MINFREQ, PLOT_MAXFREQ);
406  double hist2D_MAX = WTS[0]->hist2D->GetMaximum();
407 #endif
408 
409  // define time array of the PC reconstructed waveform
410  wavearray<double> rec(sig.size());
411  rec.rate(sig.rate());
412  rec=0;
413  double Erec=0.;
414 
415  // define netcluster for monster display
416  netcluster* pwc = new netcluster;
417  netpixel* pix = new netpixel;
418  pixdata pdata;
419  float crate = sig.rate();
420  float ctime;
421  float cfreq;
422  std::vector<int> list; // cluster list
423  std::vector<int> vtof(1); // time configuration array
424  std::vector<int> vtmp; // sky index array
425  std::vector<float> varea; // sky error regions array
426  std::vector<float> vpmap; // sky pixel map array
427  clusterdata cd; // dummy cluster data
428 
429  // start PC extraction
430  cout << "Start PC extraction ..." << endl;
431  double Ethr = 2*ACORE*ACORE; // selection pixel threshold
432  double precision=1;
433  int npix=0;
434  int nn,mm;
435  int nWP[NWP+1]; for(int l=0;l<=NWP;l++) nWP[l]=0;
436  int nPC=0;
437  for(int k=0;k<PCA_MAX;k++) {
438 
439  //if(fabs(Esig-Erec)/Esig<PCA_PREC) break;
440  if(precision<PCA_PREC) break;
441 
442  // find pixel with max energy -> principal component
443  double amax; // max pixel amplitude
444  int nmax; // time index @ amax
445  int mmax; // freq index @ amax
446  int rmax; // resolution @ amax
447  int isWP=GetPrincipalComponent(amax, nmax, mmax, rmax);
448  if(amax*amax<Ethr) break; // break loop if the PC energy is below the threshold
449  double factor = 1.;
450 #ifdef LOAD_XTALK_CATALOG
451  factor = GetPrincipalFactor(nmax, mmax, rmax);
452 #endif
453  nPC++; nWP[isWP]++;
454 
455  cout << endl << "--------------------------------------------------------" << endl;
456  cout << "Principal Component # " << k << endl;
457  cout << "--------------------------------------------------------" << endl;
458 
459  if(isWP) cout << "---> WAVELET PACKET : " << isWP << endl;
460  else cout << "---> wavelet pixel" << endl;
461  cout << " amax : " << amax << " nmax : " << nmax
462  << " mmax : " << mmax << " rmax : " << rmax << endl;
463 
464  double a00[3];
465  double a90[3];
466  a00[0] = wsig[rmax].getSample(nmax,mmax);
467  a90[0] = wsig[rmax].getSample(nmax,-mmax);
468 
469  if(isWP) { // wavelet packet : select 2 more pixels
470 
471  if(isWP==1) {nn=nmax+1; mm=mmax+1;}
472  if(isWP==2) {nn=nmax-1; mm=mmax; }
473  if(isWP==3) {nn=nmax; mm=mmax+1;}
474  if(isWP==4) {nn=nmax-1; mm=mmax+1;}
475 
476  a00[1] = wsig[rmax].getSample(nn,mm);
477  a90[1] = wsig[rmax].getSample(nn,-mm);
478 
479  if(isWP==1) {nn=nmax-1; mm=mmax-1;}
480  if(isWP==2) {nn=nmax+1; mm=mmax; }
481  if(isWP==3) {nn=nmax; mm=mmax-1;}
482  if(isWP==4) {nn=nmax+1; mm=mmax-1;}
483 
484  a00[2] = wsig[rmax].getSample(nn,mm);
485  a90[2] = wsig[rmax].getSample(nn,-mm);
486  }
487 
488  double ishift=0;
489 #ifdef APPLY_TSHIFT
490  // get time delayed amplitutes A00,A90
491  WDM<double>* pWDM = (WDM<double>*)wsig[rmax].pWavelet;
492  pWDM->setTDFilter(WDM_TDSIZE); // computes TD filters
493  int nsmp = (1<<rmax+PCA_IRES)/2;
494  double Emax=a00[0]*a00[0]+a90[0]*a90[0];
495  if(isWP) { // wavelet packet
496  Emax+=(a00[1]*a00[1]+a90[1]*a90[1]);
497  Emax+=(a00[2]*a00[2]+a90[2]*a90[2]);
498  }
499  double A00[3];
500  double A90[3];
501  A00[0]=a00[0]; A90[0]=a90[0];
502  if(isWP) {
503  A00[1]=a00[1]; A90[1]=a90[1];
504  A00[2]=a00[2]; A90[2]=a90[2];
505  }
506  double d00[3];
507  double d90[3];
508  double EMAX=0;
509  for(int i=-nsmp;i<=nsmp;i++) {
510  d00[0] = (double)pWDM->getPixelAmplitude(mmax, nmax, i, false); // delay a00 by i samples
511  d90[0] = (double)pWDM->getPixelAmplitude(mmax, nmax, i, true); // delay a90 by i samples
512  EMAX = d00[0]*d00[0]+d90[0]*d90[0];
513  if(EMAX>Emax) {Emax=EMAX;ishift=i;A00[0]=d00[0];A90[0]=d90[0];}
514 
515  if(isWP) { // wavelet packet : select 2 more pixels
516 
517  if(isWP==1) {nn=nmax+1; mm=mmax+1;}
518  if(isWP==2) {nn=nmax-1; mm=mmax; }
519  if(isWP==3) {nn=nmax; mm=mmax+1;}
520  if(isWP==4) {nn=nmax-1; mm=mmax+1;}
521 
522  d00[1] = (double)pWDM->getPixelAmplitude(mm, nn, i, false); // delay a00 by i samples
523  d90[1] = (double)pWDM->getPixelAmplitude(mm, nn, i, true); // delay a90 by i samples
524 
525  if(isWP==1) {nn=nmax-1; mm=mmax-1;}
526  if(isWP==2) {nn=nmax+1; mm=mmax; }
527  if(isWP==3) {nn=nmax; mm=mmax-1;}
528  if(isWP==4) {nn=nmax+1; mm=mmax-1;}
529 
530  d00[2] = (double)pWDM->getPixelAmplitude(mm, nn, i, false); // delay a00 by i samples
531  d90[2] = (double)pWDM->getPixelAmplitude(mm, nn, i, true); // delay a90 by i samples
532 
533  // parity
534  int sign;
535  if(isWP==1) sign = mmax%2 ? 1 :-1;
536  if(isWP==2) sign = mmax%2 ? -1 : 1;
537  if(isWP==3) sign = mmax%2 ? -1 : 1;
538  if(isWP==4) sign = mmax%2 ? -1 : 1;
539 
540  // get D00,D90 with respect to z,Z
541  // x = a90[1]*U +/- a00[0]*v + a90[2]*W
542  // X = a00[1]*u -/+ a90[0]*V + a00[2]*w
543  double D00 = 0;
544  double D90 = 0;
545  if(sign==1) {
546  D00 = a00[0]+a90[1]+a90[2]; // D00 = (x,z)
547  D90 = -a90[0]+a00[1]+a00[2]; // D00 = (x,z)
548  } else {
549  D00 = -a00[0]+a90[1]+a90[2]; // D00 = (x,z)
550  D90 = a90[0]+a00[1]+a00[2]; // D00 = (x,z)
551  }
552 
553  // D00 = (x,z)/(z,z)
554  // D90 = (X,Z)/(Z,Z)
555  // (z,z) = (Z,Z) = 3+4*WP_XTALK
556  D00/=(3+4*wp_xtalk[isWP-1]);
557  D90/=(3+4*wp_xtalk[isWP-1]);
558 
559  // z = z/sqrt(3+4*WP_XTALK)
560  // Z = Z/sqrt(3+4*WP_XTALK)
561  // (z,z) = (Z,Z) = 1
562  D00*=sqrt(3+4*wp_xtalk[isWP-1]);
563  D90*=sqrt(3+4*wp_xtalk[isWP-1]);
564 
565  EMAX = (D00*D00+D90*D90);
566 
567  if(EMAX>Emax) {
568  Emax=EMAX;ishift=i;
569  A00[0]=d00[0];A90[0]=d90[0];
570  A00[1]=d00[1];A90[1]=d90[1];
571  A00[2]=d00[2];A90[2]=d90[2];
572  }
573  }
574  }
575  cout << 0 << " a00[0] : " << a00[0] << " a90[0] : " << a90[0] << endl;
576  cout << ishift << " A00[0] : " << A00[0] << " A90[0] : " << A90[0] << endl;
577  cout << "tshift : " << 1000*ishift/wsig[rmax].rate() << " msec" << endl;
578  cout << "tmax : " << 1000*nsmp/wsig[rmax].rate() << " msec" << endl;
579  a00[0]=A00[0];a90[0]=A90[0];
580  if(isWP) {
581  a00[1]=A00[1];a90[1]=A90[1];
582  a00[2]=A00[2];a90[2]=A90[2];
583  }
584 #endif
585 
586  // get max pixels time representation
587 
588  int layers = wsig[rmax].maxLayer()+1; // numbers of frequency bins (first & last bins have df/2)
589  int slices = wsig[rmax].sizeZero(); // number of time bins
590  //cout << "layers " << layers << " slices " << slices << endl;
591 
592  int itime = nmax*layers + mmax;
593 
594  wavearray<double> w00[3]; //time series container
595  wavearray<double> w90[3]; //time series container
596  int j00[3];
597  int j90[3];
598 
599  j00[0] = wdm[rmax]->getBaseWave(itime,w00[0],false)-ishift;
600  j90[0] = wdm[rmax]->getBaseWave(itime,w90[0],true)-ishift;
601 
602  wavearray<double> x(sig.size()); // PC time series container
603  x.rate(sig.rate());
604  x=0;
605 
606  double D00 = 0;
607  double D90 = 0;
608  if(isWP) { // wavelet packet : select 2 more pixels
609 
610  if(isWP==1) {nn=nmax+1; mm=mmax+1;}
611  if(isWP==2) {nn=nmax-1; mm=mmax; }
612  if(isWP==3) {nn=nmax; mm=mmax+1;}
613  if(isWP==4) {nn=nmax-1; mm=mmax+1;}
614 
615  itime = nn*layers + mm;
616  j00[1] = wdm[rmax]->getBaseWave(itime,w00[1],false)-ishift;
617  j90[1] = wdm[rmax]->getBaseWave(itime,w90[1],true)-ishift;
618 
619  if(isWP==1) {nn=nmax-1; mm=mmax-1;}
620  if(isWP==2) {nn=nmax+1; mm=mmax; }
621  if(isWP==3) {nn=nmax; mm=mmax-1;}
622  if(isWP==4) {nn=nmax+1; mm=mmax-1;}
623 
624  itime = nn*layers + mm;
625  j00[2] = wdm[rmax]->getBaseWave(itime,w00[2],false)-ishift;
626  j90[2] = wdm[rmax]->getBaseWave(itime,w90[2],true)-ishift;
627 
628  // parity
629  // sign= 1 -> z=U+v+W , Z=u-V+w
630  // sign=-1 -> z=U-v+W , Z=u+V+w
631  // v=w00[0], u=w00[1], w=w00[2]
632  // V=w90[0], U=w90[1], W=w90[2]
633  // (z,z) = (Z,Z) = 3+4*WP_XTALK
634  // (z,Z) = 0
635  int sign;
636  if(isWP==1) sign = mmax%2 ? 1 :-1;
637  if(isWP==2) sign = mmax%2 ? -1 : 1;
638  if(isWP==3) sign = mmax%2 ? -1 : 1;
639  if(isWP==4) sign = mmax%2 ? -1 : 1;
640  cout << " parity : " << sign << endl;
641 
642  // get D00,D90 with respect to z,Z
643  // x = a90[1]*U +/- a00[0]*v + a90[2]*W
644  // X = a00[1]*u -/+ a90[0]*V + a00[2]*w
645  // (x,X)=0
646  if(sign==1) {
647  D00 = a00[0]+a90[1]+a90[2]; // D00 = (x,z)
648  D90 = -a90[0]+a00[1]+a00[2]; // D00 = (x,z)
649  } else {
650  D00 = -a00[0]+a90[1]+a90[2]; // D00 = (x,z)
651  D90 = a90[0]+a00[1]+a00[2]; // D00 = (x,z)
652  }
653  D00/=(3+4*wp_xtalk[isWP-1]); // D00 = (x,z)/(z,z)
654  D90/=(3+4*wp_xtalk[isWP-1]); // D90 = (x,Z)/(Z,Z)
655 
656  // D00*z
657  // sign= 1 -> z=U+v+W
658  // sign=-1 -> z=U-v+W
659  for(int i=0; i<w90[1].size(); i++){ // U=w90[1]
660  if(j90[1]+i<0 || j90[1]+i>=x.size()) continue;
661  x.data[j90[1]+i] += D00*w90[1][i];
662  }
663  for(int i=0; i<w00[0].size(); i++){ // v=w00[0]
664  if(j00[0]+i<0 || j00[0]+i>=x.size()) continue;
665  x.data[j00[0]+i] += sign*D00*w00[0][i];
666  }
667  for(int i=0; i<w90[2].size(); i++){ // v=w00[0]
668  if(j90[2]+i<0 || j90[2]+i>=x.size()) continue;
669  x.data[j90[2]+i] += D00*w90[2][i];
670  }
671 
672  // D90*Z
673  // sign= 1 -> Z=u-V+w
674  // sign=-1 -> Z=u+V+w
675  for(int i=0; i<w00[1].size(); i++){ // u=w00[1]
676  if(j00[1]+i<0 || j00[1]+i>=x.size()) continue;
677  x.data[j00[1]+i] += D90*w00[1][i];
678  }
679  for(int i=0; i<w90[0].size(); i++){ // V=w90[0]
680  if(j90[0]+i<0 || j90[0]+i>=x.size()) continue;
681  x.data[j90[0]+i] -= sign*D90*w90[0][i];
682  }
683  for(int i=0; i<w00[2].size(); i++){ // w=w00[2]
684  if(j00[2]+i<0 || j00[2]+i>=x.size()) continue;
685  x.data[j00[2]+i] += D90*w00[2][i];
686  }
687 
688  } else { // single pixel
689 
690  for(int i=0; i<w00[0].size(); i++){
691  if(j00[0]+i<0 || j00[0]+i>=x.size()) continue;
692  x.data[j00[0]+i] += factor*a00[0]*w00[0][i];
693  }
694  for(int i=0; i<w90[0].size(); i++){
695  if(j90[0]+i<0 || j90[0]+i>=x.size()) continue;
696  x.data[j90[0]+i] += factor*a90[0]*w90[0][i];
697  }
698 
699  D00 = a00[0];
700  D90 = a90[0];
701  }
702 
703  // fill pixel infos for monster display
704  npix++;
705  pix->core = 1;
706  pix->clusterID = 0; // setup new ID
707  pix->rate = x.rate()/(1<<(rmax+PCA_IRES));
708  pix->layers = layers;
709  pix->time = itime;
710  pix->frequency = mmax;
711  pix->data.clear();
712  pdata.asnr = D00;
713  pdata.a_90 = D90;
714  pix->data.push_back(pdata);
715  pwc->pList.push_back(*pix); // update pList and counter
716 
717  // PC waveform
718  for(int i=0; i<x.size(); i++) rec.data[i] += x[i];
719  Erec=0.; for(int i=0; i<rec.size(); i++) Erec+=rec[i]*rec[i];
720 
721  // subtract max pixels from TF maps
722  WSeries<double> wx; // TF map container
723  for(int i=0;i<PCA_NRES;i++) {
724  wx.Forward(x, *wdm[i]); // apply the WDM to the max pixel time series
725  wsig[i]-=wx; // subtract max pixels from TF maps
726  }
727 
728  // compute precision = Eresidual/Esignal
729  wavearray<double> y = rec;
730  y-=sig; y*=y;
731  wavearray<double> y2 = sig;
732  y2*=y2;
733  precision = y.mean()/y2.mean();
734  cout << " Erec " << Erec << " Esig " << Esig
735  << " fabs(Esig-Erec)/Esig : " << fabs(Esig-Erec)/Esig
736  << " precision " << precision << endl;
737 #ifdef OUTPUT_FILE
738  out << Erec << " " << Esig << " " << fabs(Esig-Erec)/Esig << " " << precision << endl;
739 #endif
740  }
741 #ifdef OUTPUT_FILE
742  out.close();
743 #endif
744 
745  cout << endl;
746  cout << "-----------------------------------------------------" << endl;
747  cout << "number of SW/nPC used in the PCA : " << nWP[0] << "/" << nPC << endl;
748  cout << "number of diagonal_WP/nPC used in the PCA : " << nWP[1] << "/" << nPC << endl;
749  cout << "number of horizontal_WP/nPC used in the PCA : " << nWP[2] << "/" << nPC << endl;
750  cout << "number of vertical_WP/nPC used in the PCA : " << nWP[3] << "/" << nPC << endl;
751  cout << "number of back-diagonal_WP/nPC used in the PCA : " << nWP[4] << "/" << nPC << endl;
752 #ifdef OUTPUT_FILE
753  cout << "output file : " << OUTPUT_FILE << endl;
754 #endif
755  cout << "-----------------------------------------------------" << endl;
756  cout << endl;
757 
758  // display tf (residuals)
759 #ifdef PLOT_TFMAP
760  double dt1 = 1./wsig[PLOT_TFRES].rate();
761  double tbeg1 = WDM_SCRATCH-2*dt1;
762  double tend1 = dt1*wsig[PLOT_TFRES].size()/2-WDM_SCRATCH;
763  WTS[1] = new watplot(const_cast<char*>("wts1"));
764  WTS[1]->plot(&wsig[PLOT_TFRES], PLOT_TFTYPE, tbeg1, tend1, const_cast<char*>("COLZ"));
765  WTS[1]->hist2D->GetYaxis()->SetRangeUser(PLOT_MINFREQ, PLOT_MAXFREQ);
766  WTS[1]->hist2D->GetZaxis()->SetRangeUser(0, hist2D_MAX);
767 #endif
768 
769 #ifdef PLOT_MONSTER
770  // monster display
771  for(int i=0;i<npix;i++) list.push_back(i);
772  // fill pixel infos for monster display
773  pwc->rate = sig.rate();
774  pwc->cList.push_back(list);
775  pwc->cRate.push_back(crate);
776  pwc->cTime.push_back(ctime);
777  pwc->cFreq.push_back(cfreq);
778  pwc->sArea.push_back(varea); // recreate sky error regions array
779  pwc->p_Map.push_back(vpmap); // recreate sky pixel map array
780  pwc->nTofF.push_back(vtof); // recreate time configuration array
781  pwc->p_Ind.push_back(vtmp); // recreate sky index array
782  pwc->cData.push_back(cd); // recreate dummy cluster data
783 
784  WTS[2] = new watplot(const_cast<char*>("wts2"));
785  WTS[2]->plot(pwc, 1, 1, 'L', 0, const_cast<char*>("COLZ"));
786 #endif
787 
788 #ifdef PLOT_REC_VS_INJ
789  // display rec vs sig waveform in time domain
790  grec = new gwavearray<double>;
791  *grec = rec;
792  gsig = new gwavearray<double>;
793  *gsig = sig;
794  gsig->SetTimeRange(WDM_SCRATCH, gsig->size()/gsig->rate()-WDM_SCRATCH);
795  gsig->Draw(GWAT_TIME,"ALPCUSTOM");
796  gsig->Draw(grec,GWAT_TIME,"SAME",kRed);
797 #endif
798 }
799 
800 int GetPrincipalComponent(double &amax, int &nmax, int &mmax, int &rmax) {
801 
802  rmax=0;
803  nmax=0;
804  mmax=0;
805  amax=0;
806 
807  int Rmax=0;
808  double Nmax=0;
809  double Mmax=0;
810  double Amax=0;
811 
812  double Ethr = 2*ACORE*ACORE; // selection pixel threshold
813  int isWP=0;
814  int nn,mm;
815  double a00[3];
816  double a90[3];
817  double A00 = 0;
818  double A90 = 0;
819 
820  for(int i=0;i<PCA_NRES;i++) {
821 
822  int layers = wsig[i].maxLayer()+1; // numbers of frequency bins (first & last bins have df/2)
823  int slices = wsig[i].sizeZero(); // number of time bins
824  //cout << "layers " << layers << " slices " << slices << endl;
825 
826  int iscratch = WDM_SCRATCH*(wsig[i].rate()/(1<<(i+PCA_IRES)));
827  for(int n=iscratch;n<slices-iscratch;n++) { // exclude scratch data
828  for(int m=2;m<layers-2;m++) {
829  a00[0] = wsig[i].getSample(n,m);
830  a90[0] = wsig[i].getSample(n,-m);
831 
832  double E = (a00[0]*a00[0]+a90[0]*a90[0]);
833 
834  double A = sqrt(E);
835  if(wp_mask[0]) {
836  if(A>amax) {amax=A; nmax=n; mmax=m; rmax=i; isWP=0;}
837  if(A>Amax) {Amax=A; Nmax=n; Mmax=m; Rmax=i; A00=a00[0]; A90=a90[0];}
838  }
839 
840  for(int l=0;l<NWP;l++) {
841 
842  if(!wp_mask[l+1]) continue;
843 
844  if(l==0) {nn=n+1; mm=m+1;} // diagonal WP
845  if(l==1) {nn=n-1; mm=m; } // horizontal WP
846  if(l==2) {nn=n; mm=m+1;} // vertical WP
847  if(l==3) {nn=n-1; mm=m+1;} // back-diagonal WP
848 
849  a00[1] = wsig[i].getSample(nn,mm);
850  a90[1] = wsig[i].getSample(nn,-mm);
851 
852  if(l==0) {nn=n-1; mm=m-1;} // diagonal WP
853  if(l==1) {nn=n+1; mm=m; } // horizontal WP
854  if(l==2) {nn=n; mm=m-1;} // vertical WP
855  if(l==3) {nn=n+1; mm=m-1;} // back-diagonal WP
856 
857  a00[2] = wsig[i].getSample(nn,mm);
858  a90[2] = wsig[i].getSample(nn,-mm);
859 
860  // parity
861  int sign;
862  if(l==0) sign = m%2 ? 1 :-1;
863  if(l==1) sign = m%2 ? -1 : 1;
864  if(l==2) sign = m%2 ? -1 : 1;
865  if(l==3) sign = m%2 ? -1 : 1;
866 
867  // sign= 1 -> z=U+v+W , Z=u-V+w
868  // sign=-1 -> z=U-v+W , Z=u+V+w
869  // v=w00[0], u=w00[1], w=w00[2]
870  // V=w90[0], U=w90[1], W=w90[2]
871  // (v,U) = (v,W) = -(V,u) = -(V,w) = WP_XTALK
872  // (z,Z) = 0
873 
874  // get D00,D90 with respect to z,Z
875  // x = a90[1]*U +/- a00[0]*v + a90[2]*W
876  // X = a00[1]*u -/+ a90[0]*V + a00[2]*w
877  double D00 = 0;
878  double D90 = 0;
879  if(sign==1) {
880  D00 = a00[0]+a90[1]+a90[2]; // D00 = (x,z)
881  D90 = -a90[0]+a00[1]+a00[2]; // D00 = (x,z)
882  } else {
883  D00 = -a00[0]+a90[1]+a90[2]; // D00 = (x,z)
884  D90 = a90[0]+a00[1]+a00[2]; // D00 = (x,z)
885  }
886  // (z,z) = (Z,Z) = 3+4*WP_XTALK
887  D00/=(3+4*wp_xtalk[l]);
888  D90/=(3+4*wp_xtalk[l]);
889 
890  // get D00,D90 with respect to the normalized z,Z
891  // z = z/sqrt(3+4*WP_XTALK)
892  // Z = Z/sqrt(3+4*WP_XTALK)
893  // (z,z) = (Z,Z) = 1
894  D00*=sqrt(3+4*wp_xtalk[l]);
895  D90*=sqrt(3+4*wp_xtalk[l]);
896 
897  double E = (D00*D00+D90*D90);
898 
899  A = sqrt(E);
900 
901  if(A>amax) {amax=A; nmax=n; mmax=m; rmax=i; isWP=l+1;}
902  }
903  // set isWP=0 if the difference between 1 pixels and WP is below the noise threshold
904  // 2*Ethr is the noise of the 2 WP extra pixels
905 // if(fabs(amax*amax-Amax*Amax)<2*Ethr) {amax=Amax; nmax=Nmax; mmax=Mmax; rmax=Rmax; isWP=0;}
906  }
907  }
908  }
909  return isWP;
910 }
911 
912 #ifdef LOAD_XTALK_CATALOG
913 void CheckXTalkCatalog(int l_low, int l_high) {
914 
915  for(int level=l_high; level>=l_low; level--) {
916  int layers = level>0 ? (1<<level)+1 : 0;
917  cout << "layers : " << layers << endl;
918 
919  // v,V
920  int _n = 100;
921  int _m = 9; // parity = m%2 ? even : odd; m=9 -> parity=even
922  // w,W
923  int _nn=_n+1;
924  int _mm=_m+1;
925 
926 /*
927  for(int ii=-1;ii<=1;ii++) {
928  for(int jj=-1;jj<=1;jj++) {
929  // CC[0] CC[2]
930  // CC[1] CC[3]
931  _nn=_n+ii;
932  _mm=_m+jj;
933  struct xtalk xt = wdmMRA.getXTalk(layers, _n*layers+_m, layers, _nn*layers+_mm);
934  cout << ii << " " << jj << " (v,w) : " << xt.CC[0] << " (v,W) : " << xt.CC[1]
935  << " (V,w) : " << xt.CC[2] << " (V,W) : " << xt.CC[3] << endl;
936  }
937  }
938 */
939 
940  struct xtalk xt = wdmMRA.getXTalk(layers, _n*layers+_m, layers, _nn*layers+_mm);
941  cout << "xtalk catalog : " << " (v,w) : " << xt.CC[0] << " (v,W) : " << xt.CC[1]
942  << " (V,w) : " << xt.CC[2] << " (V,W) : " << xt.CC[3] << endl;
943 
944  int j00[2];
945  int j90[2];
946  wavearray<double> w00[2]; //time series container
947  wavearray<double> w90[2]; //time series container
948 
949  j00[0] = wdm[level-l_low]->getBaseWave( _n*layers+_m, w00[0],false); // v
950  j90[0] = wdm[level-l_low]->getBaseWave( _n*layers+_m, w90[0],true); // V
951  j00[1] = wdm[level-l_low]->getBaseWave(_nn*layers+_mm,w00[1],false); // w
952  j90[1] = wdm[level-l_low]->getBaseWave(_nn*layers+_mm,w90[1],true); // W
953 
954  wavearray<double> W00[2]; //time series container
955  wavearray<double> W90[2]; //time series container
956  for(int k=0;k<2;k++) {
957  W00[k]=sig; W00[k]=0;
958  for(int i=0;i<w00[k].size();i++) {
959  if(i+j00[k]<0 || i+j00[k]>W00[k].size()) continue;
960  W00[k][i+j00[k]] = w00[k][i];
961  }
962  W90[k]=sig; W90[k]=0;
963  for(int i=0;i<w90[k].size();i++) {
964  if(i+j90[k]<0 || i+j90[k]>W90[k].size()) continue;
965  W90[k][i+j90[k]] = w90[k][i];
966  }
967  }
968 
969  double vw=0.;
970  double vW=0.;
971  double Vw=0.;
972  double VW=0.;
973  for(int i=0;i<W90[0].size();i++) {
974  vw+=W00[0][i]*W00[1][i]; // (v,w)
975  vW+=W00[0][i]*W90[1][i]; // (v,W)
976  Vw+=W90[0][i]*W00[1][i]; // (V,w)
977  VW+=W90[0][i]*W90[1][i]; // (V,W)
978  }
979  // NOTE : for more infos see InitXTALK function
980  cout << "xtalk base waves : " << " (v,w) : " << vw << " (v,W) : " << vW
981  << " (V,w) : " << Vw << " (V,W) : " << VW << endl << endl;
982  cout << "wp_xtalk : " << " (v,w) : " << 0 << " (v,W) : " << wp_xtalk[0]
983  << " (V,w) : " << -wp_xtalk[0] << " (V,W) : " << 0 << endl << endl;
984  }
985 }
986 #endif
987 
988 double GetPrincipalFactor(int nmax, int mmax, int rmax) {
989 
990  double Ethr = 2*ACORE*ACORE; // selection pixel threshold
991  int nn,mm;
992  vector<double> vb;
993  vector<double> vB;
994  vector<int> vN;
995  vector<int> vM;
996  vector<int> vL;
997 
998  struct xtalk xt;
999 
1000  for(int i=0;i<PCA_NRES;i++) {
1001 
1002  int layers = wsig[i].maxLayer()+1; // numbers of frequency bins (first & last bins have df/2)
1003  int slices = wsig[i].sizeZero(); // number of time bins
1004  //cout << "layers " << layers << " slices " << slices << endl;
1005 
1006  int iscratch = WDM_SCRATCH*(wsig[i].rate()/(1<<(i+PCA_IRES)));
1007  for(int n=iscratch;n<slices-iscratch;n++) { // exclude scratch data
1008  for(int m=2;m<layers-2;m++) {
1009  double a00 = wsig[i].getSample(n,m);
1010  double a90 = wsig[i].getSample(n,-m);
1011 
1012  double E = (a00*a00+a90*a90);
1013 
1014  if(E<Ethr) continue;
1015 
1016  //cout << "r:n:m " << i << ":" << n << ":" << m << " E : " << E << endl;
1017  //cout << "lmax:nmax:mmax " << lmax << ":" << nmax << ":" << mmax << endl;
1018 
1019  vb.push_back(a00);
1020  vB.push_back(a90);
1021  vN.push_back(n);
1022  vM.push_back(m);
1023  vL.push_back(layers);
1024  }
1025  }
1026  }
1027 
1028  // PC is X = a*Y00 + A*Y90
1029  // x - q*X
1030  // sum_i((x,Yi)-q*(X,Yi))^2 = sum_i(bi-q*(X,Yi))^2 + sum_i(Bi-q*(X,Yi))^2
1031  // q = (sum_i(bi*(X,Yi)) + sum_i(Bi*(X,Yi))) / (sum_i((X,Yi)^2) + sum_i((X,Yi)^2))
1032 
1033  int lmax = wsig[rmax].maxLayer()+1;
1034 
1035  double a = wsig[rmax].getSample(nmax,mmax);
1036  double A = wsig[rmax].getSample(nmax,-mmax);
1037 
1038  double num=0;
1039  double den=0;
1040  for(int k=0;k<vb.size();k++) {
1041  double b = vb[k];
1042  double B = vB[k];
1043  int n = vN[k];
1044  int m = vM[k];
1045  int l = vL[k];
1046  xt = wdmMRA.getXTalk(lmax, nmax*lmax+mmax, l, n*l+m);
1047  if(xt.CC[0]>2) continue;
1048  //cout << "r:n:m " << l << ":" << n << ":" << m << " b : " << b << " B : " << B << endl;
1049  //cout << "lmax:nmax:mmax " << lmax << ":" << nmax << ":" << mmax << endl;
1050  //cout << k << " (u,w) : " << xt.CC[0] << " (u,W) : " << xt.CC[1]
1051  // << " (U,w) : " << xt.CC[2] << " (U,W) : " << xt.CC[3] << endl;
1052 
1053  num+=b*(xt.CC[0]*a+xt.CC[2]*A)+B*(xt.CC[1]*a+xt.CC[3]*A);
1054  den+=pow(xt.CC[0]*a+xt.CC[2]*A,2)+pow(xt.CC[1]*a+xt.CC[3]*A,2);
1055  }
1056 
1057  double q = num/den;
1058 
1059  cout << "q = " << q << endl;
1060 
1061  return q;
1062 }
1063 
1064 void InitXTALK() {
1065 
1066 // -------------------------------------------------------------------------
1067 // XTALK DEFINITIONS
1068 // NOTE : All xtalk values are computed with 'v,V' parity = even (see below)
1069 // -------------------------------------------------------------------------
1070 //
1071 // WAVELET PACKET BASE
1072 // parity = m%2 ? even : odd; (n,m) are the 'v,V' time,frequency index
1073 // parity = odd -> z=U+v+W , Z=u-V+w
1074 // parity = even -> z=U-v+W , Z=u+V+w
1075 // (z,z) = (Z,Z) = 3+4*WP_XTALK
1076 // (z,Z) = 0
1077 //
1078 // diagonal WP
1079 // - - w - - W
1080 // - v - - V -
1081 // u - - U - -
1082 //
1083 // horizontal WP
1084 // - - - - - -
1085 // u v w U V W
1086 // - - - - - -
1087 //
1088 // vertical WP
1089 // - w - - W -
1090 // - v - - V -
1091 // - u - - U -
1092 //
1093 // back-diagonal WP
1094 // w - - W - -
1095 // - v - - V -
1096 // - - u - - U
1097 //
1098 // wp_xtalk = (v,W) = (W,v) = -(V,w) = -(w,V)
1099 // wp_xtalk = (v,U) = (U,v) = -(V,u) = -(u,V)
1100 //
1101 
1102 #if WDM_NU == 2
1103 
1104  wp_xtalk[0] = 0.207345; // diagonal WP (v,W)=(n:m, n+1:m+1) (v,U)=(n:m, n-1:m-1)
1105  wp_xtalk[1] = 0.561945; // horizontal WP (v,U)=(n:m, n-1:m ) (v,W)=(n:m, n+1:m )
1106  wp_xtalk[2] = 0.243038; // vertical WP (v,W)=(n:m, n :m+1) (v,U)=(n:m, n :m-1)
1107  wp_xtalk[3] = -0.207345; // back-diagonal WP (v,W)=(n:m, n-1:m+1) (v,U)=(n:m, n+1:m-1)
1108 
1109 #elif WDM_NU == 4
1110 
1111  wp_xtalk[0] = 0.162725; // diagonal WP (v,W)=(n:m, n+1:m+1) (v,U)=(n:m, n-1:m-1)
1112  wp_xtalk[1] = 0.597494; // horizontal WP (v,U)=(n:m, n-1:m ) (v,W)=(n:m, n+1:m )
1113  wp_xtalk[2] = 0.178856; // vertical WP (v,W)=(n:m, n :m+1) (v,U)=(n:m, n :m-1)
1114  wp_xtalk[3] = -0.162725; // back-diagonal WP (v,W)=(n:m, n-1:m+1) (v,U)=(n:m, n+1:m-1)
1115 
1116 #elif WDM_NU == 6
1117 
1118  wp_xtalk[0] = 0.138369; // diagonal WP (v,W)=(n:m, n+1:m+1) (v,U)=(n:m, n-1:m-1)
1119  wp_xtalk[1] = 0.610111; // horizontal WP (v,U)=(n:m, n-1:m ) (v,W)=(n:m, n+1:m )
1120  wp_xtalk[2] = 0.148011; // vertical WP (v,W)=(n:m, n :m+1) (v,U)=(n:m, n :m-1)
1121  wp_xtalk[3] = -0.138369; // back-diagonal WP (v,W)=(n:m, n-1:m+1) (v,U)=(n:m, n+1:m-1)
1122 
1123 #else
1124 
1125  bool wpCheck=false;
1126  for(int l=1;l<=NWP;l++) if(wp_mask[l]) wpCheck=true;
1127  if(wpCheck) {
1128  if(WDM_NU!=2) {
1129  cout << endl << "Error : wp_xtalk coefficients are defined only for WDM_NU=2,4,6 !!!" << endl << endl;
1130  exit(1);
1131  }
1132  }
1133 
1134 #endif
1135 
1136 }
1137 
#define WAVE_TYPE
Definition: PCA_Benchmark.C:24
int getBaseWave(int m, int n, SymmArray< double > &w)
Definition: WDM.cc:323
gwavearray< double > * gsig
std::vector< vector_int > cRate
Definition: netcluster.hh:398
int slices
char filter_dir[1024]
Definition: mdc.hh:219
int j00
std::vector< WSeries< double > > vN[NIFO_MAX]
#define PCA_NRES
Definition: PCA_Benchmark.C:7
size_t clusterID
Definition: netpixel.hh:109
virtual void rate(double r)
Definition: wavearray.hh:141
float factor
#define PCA_IRES
Definition: PCA_Benchmark.C:8
wavearray< double > a(hp.size())
size_t frequency
Definition: netpixel.hh:111
Definition: mdc.hh:158
#define B
int n
Definition: cwb_net.C:28
#define XTALK_CATALOG
Definition: PCA_Benchmark.C:49
TString("c")
ofstream out
Definition: cwb_merge.C:214
cout<< endl;cout<< "ts size = "<< ts.size()<< " ts rate = "<< ts.rate()<< endl;tf.Forward(ts, wdm);int levels=tf.getLevel();cout<< "tf size = "<< tf.size()<< endl;double dF=tf.resolution();double dT=1./(2 *dF);cout<< "rate(hz) : "<< RATE<< "\ layers : "<< nLAYERS<< "\ dF(hz) : "<< dF<< "\ dT(ms) : "<< dT *1000.<< endl;int itime=TIME_PIXEL_INDEX;int ifreq=FREQ_PIXEL_INDEX;int index=(levels+1) *itime+ifreq;double time=itime *dT;double freq=(ifreq >0) ? ifreq *dF :dF/4;cout<< endl;cout<< "PIXEL TIME = "<< time<< " sec "<< endl;cout<< "PIXEL FREQ = "<< freq<< " Hz "<< endl;cout<< endl;wavearray< double > x
std::vector< pixdata > data
Definition: netpixel.hh:122
std::vector< vector_float > sArea
Definition: netcluster.hh:401
netpixel pix(nifo)
netcluster * pwc
Definition: cwb_job_obj.C:38
void SetInjLength(double inj_length=MDC_INJ_LENGTH)
Definition: mdc.hh:304
int layers
wavearray< double > hp
Definition: mdc.hh:226
#define WAVE_POL
Definition: PCA_Benchmark.C:31
CWB::mdc * MDC
#define WDM_TDSIZE
Definition: PCA_Benchmark.C:42
waveform wf
void PCA_Benchmark()
Long_t size
size_t layers
Definition: netpixel.hh:112
int m
Definition: cwb_net.C:28
std::vector< vector_int > cList
Definition: netcluster.hh:397
virtual void start(double s)
Definition: wavearray.hh:137
int j
Definition: cwb_net.C:28
canvas cd(1)
watplot * WTS[3]
i drho i
int l_low
Definition: test_config1.C:40
double rate
Definition: netcluster.hh:378
#define NWP
Definition: PCA_Benchmark.C:55
void setTDFilter(int nCoeffs, int L=1)
Definition: WDM.cc:639
#define ACORE
Definition: PCA_Benchmark.C:13
TList * list
#define PCA_PREC
Definition: PCA_Benchmark.C:11
#define PLOT_MINFREQ
static bool checkFile(TString fName, bool question=false, TString message="")
Definition: Toolbox.cc:4670
void plot(wavearray< double > &, char *=NULL, int=1, double=0., double=0., bool=false, float=0., float=0., bool=false, float=0., bool=false)
Definition: watplot.cc:150
double asnr
Definition: netpixel.hh:38
void InitXTALK()
mdcid AddWaveform(MDC_TYPE mdc_type, vector< mdcpar > par, TString uname="")
Definition: mdc.cc:472
#define PLOT_TFRES
Definition: PCA_Benchmark.C:99
void Print(int level=0)
Definition: mdc.cc:2736
bool core
Definition: netpixel.hh:120
double getPixelAmplitude(int, int, int, bool=false)
Definition: WDM.cc:748
std::vector< vector_float > p_Map
Definition: netcluster.hh:402
TH2F * hist2D
Definition: watplot.hh:193
#define WDM_SCRATCH
Definition: PCA_Benchmark.C:44
virtual size_t size() const
Definition: wavearray.hh:145
#define PCA_MAX
Definition: PCA_Benchmark.C:10
Definition: mdc.hh:248
int GetPrincipalComponent(double &amax, int &nmax, int &mmax, int &rmax)
Definition: monster.hh:30
xtalk getXTalk(int nLay1, size_t indx1, int nLay2, size_t indx2)
param: numbers of layers identifying the resolution of the first pixel param: TF map index of the fir...
Definition: monster.cc:351
i() int(T_cor *100))
std::vector< netpixel > pList
Definition: netcluster.hh:390
double wp_xtalk[4]
int l
#define PLOT_MAXFREQ
std::vector< vector_int > p_Ind
Definition: netcluster.hh:403
double GetPrincipalFactor(int nmax, int mmax, int rmax)
std::vector< float > cFreq
Definition: netcluster.hh:400
void read(char *filename)
param: file name
Definition: monster.cc:244
double precision
TString inspOptions
int k
Definition: mdc.hh:152
static double A
Definition: geodesics.cc:26
#define WAVE_SNR
Definition: PCA_Benchmark.C:33
void SetTimeRange(double tMin, double tMax)
Definition: gwavearray.hh:75
size_t time
Definition: netpixel.hh:110
vector< mdcpar > par
WSeries< double > wsig[PCA_NRES]
size_t rateANA
Definition: test_config1.C:21
int npix
#define WDM_NU
Definition: PCA_Benchmark.C:39
virtual void FFTW(int=1)
Definition: wavearray.cc:896
WDM< double > * wdm[PCA_NRES]
wavearray< double > hx
Definition: mdc.hh:227
#define PLOT_TFTYPE
std::vector< float > cTime
Definition: netcluster.hh:399
virtual double mean() const
Definition: wavearray.cc:1071
WDM< double > * pWDM
Definition: testWDM_5.C:28
double fabs(const Complex &x)
Definition: numpy.cc:55
bool wp_mask[5]
int * layers
Definition: monster.hh:121
waveform GetWaveform(int ID, int id=0)
Definition: mdc.cc:2541
void Forward(int n=-1)
param: wavelet - n is number of steps (-1 means full decomposition)
Definition: wseries.cc:246
strcpy(RunLabel, RUN_LABEL)
long int num
sprintf(tfres,"(1/%g)x(%g) (sec)x(Hz)", 2 *df, df)
int nRes
Definition: monster.hh:117
std::vector< clusterdata > cData
Definition: netcluster.hh:391
float CC[4]
Definition: monster.hh:30
double mmax
int l_high
Definition: test_config1.C:41
double * itime
#define WDM_PREC
Definition: PCA_Benchmark.C:41
DataType_t getSample(int n, double m)
Definition: wseries.hh:185
double ctime
float rate
Definition: netpixel.hh:113
virtual void resize(unsigned int)
Definition: wavearray.cc:463
Definition: mdc.hh:157
size_t sizeZero()
Definition: wseries.hh:144
wavearray< double > y
Definition: Test10.C:31
gwavearray< double > * grec
wavearray< double > sig
int maxLayer()
Definition: wseries.hh:139
void Draw(GWAT_DRAW type=GWAT_TIME, TString options="ALP", Color_t color=kBlack)
Definition: gwavearray.cc:89
double a_90
Definition: netpixel.hh:39
std::vector< vector_int > nTofF
Definition: netcluster.hh:404
exit(0)
#define WAVE_LENGTH
Definition: PCA_Benchmark.C:29