Logo coherent WaveBurst  
Config Reference Guide
Logo
CWB_Plugin_QLveto_Gating_EBBH.C
Go to the documentation of this file.
1 // -------------------------------------------------------------------------
2 // WARNING, NOT EDIT : This is a multi plugin generated with cwb_mplugin !!!
3 // NOTE : The main is listed on the bottom
4 //
5 // INPUT PLUGINS :
6 //
7 // 1 - macro/CWB_Plugin_QLveto_Gating.C
8 // 2 - macro/CWB_Plugin_EBBH.C
9 //
10 // -------------------------------------------------------------------------
11 
12 
13 // -------------------------------------------------------------------------
14 // --> BEGIN CWB_USER PLUGIN CODE 1
15 //
16 // macro/CWB_Plugin_QLveto_Gating.C
17 // -------------------------------------------------------------------------
18 
19 namespace CWB_UserPluginNamespace_1 {
20 
21 // -------------------------------------------------------------------------
22 // WARNING, NOT EDIT : This is a multi plugin generated with cwb_mplugin !!!
23 // NOTE : The main is listed on the bottom
24 //
25 // INPUT PLUGINS :
26 //
27 // 1 - /home/vedovato/Y2/coherent/SVN/watrepo/wat/trunk_wp_merge/trunk_4886_clean/tools/cwb/plugins/CWB_Plugin_QLveto.C
28 // 2 - /home/vedovato/Y2/coherent/SVN/watrepo/wat/trunk_wp_merge/trunk_4886_clean/tools/cwb/plugins/CWB_Plugin_Gating.C
29 //
30 // -------------------------------------------------------------------------
31 
32 
33 // -------------------------------------------------------------------------
34 // --> BEGIN CWB_USER PLUGIN CODE 1
35 //
36 // /home/vedovato/Y2/coherent/SVN/watrepo/wat/trunk_wp_merge/trunk_4886_clean/tools/cwb/plugins/CWB_Plugin_QLveto.C
37 // -------------------------------------------------------------------------
38 
39 }
40 #define XIFO 4
41 
42 #pragma GCC system_header
43 
44 #include "cwb.hh"
45 #include "config.hh"
46 #include "network.hh"
47 #include "wavearray.hh"
48 #include "TString.h"
49 #include "TObjArray.h"
50 #include "TObjString.h"
51 #include "TRandom.h"
52 #include "TComplex.h"
53 #include "TMath.h"
54 #include "mdc.hh"
55 #include "watplot.hh"
56 #include "gwavearray.hh"
57 #include <vector>
58 
59 namespace CWB_UserPluginNamespace_1 {
60 
61 namespace CWB_UserPluginNamespace_1 {
62 
63 //#define PLOT_LIKELIHOOD
64 //#define PLOT_WHITENED_WAVEFORMS
65 
66 }
67 }
68 #define NTHR 1
69 #define ATHR 7.58859
70 
71 namespace CWB_UserPluginNamespace_1 {
72 
73 namespace CWB_UserPluginNamespace_1 {
74 
75 float GetQveto(wavearray<double>* wf);
76 void GetLveto(netcluster* pwc, int cid, int nifo, float* Lveto);
77 void PlotWaveform(TString ifo, wavearray<double>* wfREC,
78  CWB::config* cfg, bool fft=false, bool strain=false);
79 void ClearWaveforms(detector* ifo);
80 
81 std::vector<netpixel> DoPCA(network* NET, CWB::config* cfg, int lag, int id);
82 void ResetPCA(network* NET, CWB::config* cfg, netcluster* pwc, std::vector<netpixel>* vPIX, int ID);
83 
84 
85 void
86 CWB_Plugin(TFile* jfile, CWB::config* cfg, network* NET, WSeries<double>* x, TString ifo, int type) {
87 //!MISCELLANEA
88 // Extract whitened reconstructed waveforms, and compute the Qveto, Lveto parameters
89 
90  cout << endl;
91  cout << "-----> CWB_Plugin_QLveto.C" << endl;
92  cout << "ifo " << ifo.Data() << endl;
93  cout << "type " << type << endl;
94  cout << endl;
95 
96  float Qveto[2*NIFO_MAX]; // Qveto
97  float Lveto[3]; // Lveto
98 
99  if(type==CWB_PLUGIN_CONFIG) {
100  cfg->outPlugin=true; // disable built-in output root file
101  }
102 
103  if(type==CWB_PLUGIN_ILIKELIHOOD) {
104  NET->wfsave=true; // enable waveform save
105 
106  // search output root file in the system list
107  TFile* froot = NULL;
108  TList *files = (TList*)gROOT->GetListOfFiles();
109  TString outDump="";
110  netevent* EVT;
111  int nIFO = NET->ifoListSize(); // number of detectors
112  if (files) {
113  TIter next(files);
114  TSystemFile *file;
115  TString fname;
116  bool check=false;
117  while ((file=(TSystemFile*)next())) {
118  fname = file->GetName();
119  // set output root file as the current file
120  if(fname.Contains("wave_")) {
121  froot=(TFile*)file;froot->cd();
122  outDump=fname;
123  outDump.ReplaceAll(".root.tmp",".txt");
124  //cout << "output file name : " << fname << endl;
125  }
126  }
127  if(!froot) {
128  cout << "CWB_Plugin_QLveto.C : Error - output root file not found" << endl;
129  gSystem->Exit(1);
130  }
131  } else {
132  cout << "CWB_Plugin_QLveto.C : Error - output root file not found" << endl;
133  gSystem->Exit(1);
134  }
135 
136  TTree* net_tree = (TTree *) froot->Get("waveburst");
137  if(net_tree==NULL) {
138  EVT = new netevent(nIFO);
139  net_tree = EVT->setTree();
140  net_tree->Branch("Qveto",Qveto,TString::Format("Qveto[%i]/F",2*cfg->nIFO));
141  net_tree->Branch("Lveto",Lveto,TString::Format("Lveto[%i]/F",3));
142  }
143  }
144 
145  if(type==CWB_PLUGIN_OLIKELIHOOD) {
146 
147  if(TString(cfg->analysis)!="2G") {
148  cout << "CWB_Plugin_QLveto.C -> "
149  << "CWB_PLUGIN_OLIKELIHOOD implemented only for 2G" << endl;
150  gSystem->Exit(1);
151  }
152 
153  // import ifactor
154  int gIFACTOR=-1; IMPORT(int,gIFACTOR)
155  cout << "-----> CWB_Plugin_QLveto.C -> "
156  << " gIFACTOR : " << gIFACTOR << endl;
157 
158  // import slagShift
159  float* gSLAGSHIFT=NULL; IMPORT(float*,gSLAGSHIFT)
160 
161  int nIFO = NET->ifoListSize(); // number of detectors
162  int K = NET->nLag; // number of time lag
163  netevent* EVT;
164  wavearray<double> id;
165  //double factor = cfg->simulation==3||cfg->simulation==4 ? -gIFACTOR : cfg->factors[gIFACTOR];
166  double factor = cfg->factors[gIFACTOR];
167  int rate = 0; // select all resolutions
168 
169  // search output root file in the system list
170  TFile* froot = NULL;
171  TList *files = (TList*)gROOT->GetListOfFiles();
172  TString outDump="";
173  if (files) {
174  TIter next(files);
175  TSystemFile *file;
176  TString fname;
177  bool check=false;
178  while ((file=(TSystemFile*)next())) {
179  fname = file->GetName();
180  // set output root file as the current file
181  if(fname.Contains("wave_")) {
182  froot=(TFile*)file;froot->cd();
183  outDump=fname;
184  outDump.ReplaceAll(".root.tmp",".txt");
185  //cout << "output file name : " << fname << endl;
186  }
187  }
188  if(!froot) {
189  cout << "CWB_Plugin_QLveto.C : Error - output root file not found" << endl;
190  gSystem->Exit(1);
191  }
192  } else {
193  cout << "CWB_Plugin_QLveto.C : Error - output root file not found" << endl;
194  gSystem->Exit(1);
195  }
196 
197  TTree* net_tree = (TTree *) froot->Get("waveburst");
198  if(net_tree!=NULL) {
199  EVT = new netevent(net_tree,nIFO);
200  net_tree->SetBranchAddress("Qveto",Qveto);
201  net_tree->SetBranchAddress("Lveto",Lveto);
202  } else {
203  EVT = new netevent(nIFO);
204  net_tree = EVT->setTree();
205  net_tree->Branch("Qveto",Qveto,TString::Format("Qveto[%i]/F",2*cfg->nIFO));
206  net_tree->Branch("Lveto",Lveto,TString::Format("Lveto[%i]/F",3));
207  }
208  EVT->setSLags(gSLAGSHIFT); // set slags into netevent
209 
210  for(int k=0; k<K; k++) { // loop over the lags
211 
212  id = NET->getwc(k)->get(const_cast<char*>("ID"), 0, 'L', rate);
213 
214  for(int j=0; j<(int)id.size(); j++) { // loop over cluster index
215 
216  int ID = size_t(id.data[j]+0.5);
217 
218  if(NET->getwc(k)->sCuts[ID-1]!=-1) continue; // skip rejected/processed clusters
219 
220  double ofactor=0;
221  if(cfg->simulation==4) ofactor=-factor;
222  else if(cfg->simulation==3) ofactor=-gIFACTOR;
223  else ofactor=factor;
224 
225  EVT->output2G(NULL,NET,ID,k,ofactor); // get reconstructed parameters
226 
227  wavearray<double>** pwfREC = new wavearray<double>*[nIFO];
228  detector* pd = NET->getifo(0);
229  int idSize = pd->RWFID.size();
230 
231  int wfIndex=-1;
232  for (int mm=0; mm<idSize; mm++) if (pd->RWFID[mm]==ID) wfIndex=mm;
233  if(wfIndex==-1) continue;
234 
235  netcluster* pwc = NET->getwc(k);
236  cout << endl << "----------------------------------------------------------------" << endl;
237 
238  // extract whitened reconstructed waveforms
239  for(int n=0; n<nIFO; n++) {
240 
241  pd = NET->getifo(n);
242 
243  pwfREC[n] = pd->RWFP[wfIndex];
244  wavearray<double>* wfREC = pwfREC[n]; // array of reconstructed waveforms
245 
246 #ifdef PLOT_WHITENED_WAVEFORMS
247  //PlotWaveform(NET->ifoName[n], wfREC, cfg, false, false);
248  PlotWaveform(NET->ifoName[n], wfREC, cfg, true, false);
249 #endif
250  // reconstructed whitened waveform
251  NET->getMRAwave(ID,k,'S',0,true);
252  Qveto[n] = GetQveto(&(pd->waveForm));
253  // whitened waveform
254  NET->getMRAwave(ID,k,'W',0,true);
255  Qveto[n+nIFO] = GetQveto(&(pd->waveBand));
256 
257  //Qveto[n] = GetQveto(wfREC);
258  cout << "Qveto : " << pd->Name << " Qveto[R] = " << Qveto[n]
259  << " Qveto[W] = " << Qveto[n+nIFO] << endl;
260 
261  if(!cfg->simulation) ClearWaveforms(pd); // release waveform memory
262  }
263  delete [] pwfREC;
264 
265  std::vector<netpixel> vPIX;
266  if(cfg->pattern>0) vPIX = DoPCA(NET, cfg, k, ID); // do PCA analysis
267  GetLveto(pwc, ID, nIFO, Lveto);
268  if(cfg->pattern>0) ResetPCA(NET, cfg, pwc, &vPIX, ID); // restore WP pwc
269 
270  cout << endl;
271  cout << "Lveto : " << "fmean : " << Lveto[0] << " frms : " << Lveto[1]
272  << " Energy Ratio : " << Lveto[2] << endl << endl;
273  cout << "----------------------------------------------------------------" << endl;
274 
275  std::vector<int> sCuts = NET->getwc(k)->sCuts; // save cCuts
276  // set sCuts=1 to the events which must be not copied with cps to pwc
277  for(int i=0; i<(int)sCuts.size(); i++) if(i!=ID-1) NET->getwc(k)->sCuts[i]=1;
278 
279  // ID can not be used to get the event, to get event use ID=1 (ex: for watplot)
280  NET->getwc(k)->sCuts = sCuts; // restore cCuts
281 
282  if(cfg->dump) EVT->dopen(outDump.Data(),const_cast<char*>("a"),false);
283  EVT->output2G(net_tree,NET,ID,k,ofactor); // get reconstructed parameters
284  if(cfg->dump) {
285  // add Qveto to dump file
286  fprintf(EVT->fP,"Qveto: ");
287  for(int i=0; i<2*nIFO; i++) fprintf(EVT->fP,"%f ",Qveto[i]);
288  fprintf(EVT->fP,"\n");
289  // add Lveto to dump file
290  fprintf(EVT->fP,"Lveto: ");
291  for(int i=0; i<3; i++) fprintf(EVT->fP,"%f ",Lveto[i]);
292  fprintf(EVT->fP,"\n");
293  }
294  if(cfg->dump) EVT->dclose();
295  if(!cfg->cedDump) NET->getwc(k)->sCuts[ID-1]=1; // mark as processed
296  }
297  }
298 
299  jfile->cd();
300  if(EVT) delete EVT;
301  }
302  return;
303 }
304 
305 float
306 GetQveto(wavearray<double>* wf) {
307 
308  wavearray<double> x = *wf;;
309 
310  // resample data by a factor 4
311  int xsize=x.size();
312  x.FFTW(1);
313  x.resize(4*x.size());
314  x.rate(4*x.rate());
315  for(int j=xsize;j<x.size();j++) x[j]=0;
316  x.FFTW(-1);
317 
318  // extract max/min values and save the absolute values in the array 'a'
319  wavearray<double> a(x.size());
320  int size=0;
321  double dt = 1./x.rate();
322  double prev=x[0];
323  double xmax=0;
324  for (int i=1;i<x.size();i++) {
325  if(fabs(x[i])>xmax) xmax=fabs(x[i]);
326  if(prev*x[i]<0) {
327  a[size]=xmax;
328  size++;
329  xmax=0;
330  }
331  prev=x[i];
332  }
333 
334  // find max value/index ans save on amax/imax
335  int imax=-1;
336  double amax=0;
337  for (int i=1;i<size;i++) {
338  if(a[i]>amax) {amax=a[i];imax=i;}
339  }
340 
341 /*
342  cout << endl;
343  cout << "a[imax-2] " << a[imax-2] << endl;
344  cout << "a[imax-1] " << a[imax-1] << endl;
345  cout << "a[imax] " << a[imax] << endl;
346  cout << "a[imax+1] " << a[imax+1] << endl;
347  cout << "a[imax+2] " << a[imax+2] << endl;
348  cout << endl;
349 */
350 
351  // compute Qveto
352  double ein=0; // energy of max values inside NTHR
353  double eout=0; // energy of max values outside NTHR
354  for (int i=0;i<size;i++) {
355  if(abs(imax-i)<=NTHR) {
356  ein+=a[i]*a[i];
357  //cout << i << " ein " << a[i] << " " << amax << endl;
358  } else {
359  if(a[i]>amax/ATHR) eout+=a[i]*a[i];
360  //if(a[i]>amax/ATHR) cout << i << " eout " << a[i] << " " << amax << endl;
361  }
362  }
363  float Qveto = ein>0 ? eout/ein : 0.;
364  //cout << "Qveto : " << Qveto << " ein : " << ein << " eout : " << eout << endl;
365 
366  return Qveto;
367 }
368 
369 void
370 GetLveto(netcluster* pwc, int cid, int nifo, float* Lveto) {
371 //
372 // input
373 // pwc : pointer to netcluster object
374 // cid : cluster id
375 // nifo : number of detectors
376 // output
377 // Lveto[0] : line frequency
378 // Lveto[1] : line bandwitdh
379 // Lveto[2] : line enery ratio (line_energy / total_energy)
380 //
381 
382  Lveto[0] = Lveto[1] = Lveto[2] = 0;
383 
384  std::vector<int>* vint = &(pwc->cList[cid-1]); // pixel list
385  int V = vint->size(); // cluster size
386  if(!V) return;
387 
388  // ------------------------------------------------------------------
389  // Find max pixel parameters
390  // ------------------------------------------------------------------
391 
392  double likeMax=0; // maximum pixel's energy
393  double likeTot=0; // total cluster energy
394  double freqMax; // frequency of the pixel with max energy
395  double dfreqMax; // df of the pixel with max energy
396  for(int n=0; n<V; n++) {
397  netpixel* pix = pwc->getPixel(cid,n);
398  if(pix->layers%2==0) {
399  cout << "CWB_Plugin_QLveto.C - Error : is enabled only for WDM (2G)" << endl;
400  exit(1);
401  }
402  if(!pix->core) continue; // select only the principal components pixels
403 
404  double likePix=0;
405  for(int m=0; m<nifo; m++) {
406  likePix += pow(pix->getdata('S',m),2); // like whitened reconstructed signal 00
407  likePix += pow(pix->getdata('P',m),2); // like whitened reconstructed signal 90
408  }
409 
410  double freq = pix->frequency*pix->rate/2.;
411  double df = pix->rate/2.;
412 
413  likeTot+=likePix;
414  if(likePix>likeMax) {likeMax=likePix;freqMax=freq;dfreqMax=df;}
415  }
416  //cout << "likeMax : " << likeMax << " likeTot : " << likeTot
417  // << " freqMax : " << freqMax << " dfreqMax : " << dfreqMax << endl;
418 
419  // ------------------------------------------------------------------
420  // Compute Lveto parameters
421  // ------------------------------------------------------------------
422 
423  double fmean=0; // line mean frequency
424  double frms=0; // line bandwidth
425  double likeLin=0; // line energy
426  for(int n=0; n<V; n++) {
427  netpixel* pix = pwc->getPixel(cid,n);
428  if(!pix->core) continue; // select only the principal components pixels
429 
430  double likePix=0;
431  for(int m=0; m<nifo; m++) {
432  likePix += pow(pix->getdata('S',m),2); // like whitened reconstructed signal 00
433  likePix += pow(pix->getdata('P',m),2); // like whitened reconstructed signal 90
434  }
435 
436  // the estimation is done for all pixels
437  // with freq in the range [freqMax-dfreqMax, freqMax+dfreqMax]
438  double freq = pix->frequency*pix->rate/2.;
439  if(fabs(freq-freqMax)<=dfreqMax) {
440  likeLin += likePix;
441  fmean += likePix*freq;
442  frms += likePix*freq*freq;
443  }
444  }
445 
446  fmean = fmean/likeLin;
447  frms = frms/likeLin-fmean*fmean;
448  frms = frms>0 ? sqrt(frms) : 0.;
449 
450  if(frms<dfreqMax/2.) frms=dfreqMax/2.;
451 
452  // ------------------------------------------------------------------
453  // Save Lveto parameters
454  // ------------------------------------------------------------------
455 
456  Lveto[0] = fmean; // line mean frequency
457  Lveto[1] = frms; // line bandwidth
458  Lveto[2] = likeTot>0. ? likeLin/likeTot : 0.; // energy ratio energy inside_line/total
459 
460  // ------------------------------------------------------------------
461  // plot time-frequency energy
462  // ------------------------------------------------------------------
463 
464 #if defined PLOT_LIKELIHOOD
465  watplot WTS(const_cast<char*>("wts"));
466  WTS.plot(pwc, cid, nifo, 'L', 0, const_cast<char*>("COLZ"));
467  WTS.canvas->Print("l_tfmap_scalogram.png");
468 #endif
469 
470  return;
471 }
472 
473 void
474 PlotWaveform(TString ifo, wavearray<double>* wfREC,
475  CWB::config* cfg, bool fft, bool strain) {
476 
477  watplot PTS(const_cast<char*>("ptswrc"),200,20,800,500);
478 
479  //cout << "Print " << fname << endl;
480  double tmin = wfREC->start();
481  wfREC->start(wfREC->start()-tmin);
482  if(fft) {
483  PTS.plot(wfREC, const_cast<char*>("ALP"), 1, 0, 0, true, cfg->fLow, cfg->fHigh);
484  } else {
485  PTS.plot(wfREC, const_cast<char*>("ALP"), 1, 0, 0);
486  }
487  PTS.graph[0]->SetLineWidth(1);
488  wfREC->start(wfREC->start()+tmin);
489 
490  char label[64]="";
491  if(fft) sprintf(label,"%s","fft");
492  else sprintf(label,"%s","time");
493  if(strain) sprintf(label,"%s_%s",label,"strain");
494  else sprintf(label,"%s_%s",label,"white");
495 
496  char fname[1024];
497  //sprintf(fname, "%s_wf_%s_rec_gps_%d.root",ifo.Data(),label,int(tmin));
498  sprintf(fname, "%s_wf_%s_rec_gps_%d.png",ifo.Data(),label,int(tmin));
499  PTS.canvas->Print(fname);
500  cout << "write : " << fname << endl;
501  //PTS.canvas->Write(REPLACE(fname,dirCED,gtype));
502 }
503 
504 void
505 ClearWaveforms(detector* ifo) {
506 
507  int n;
508 
509  n = ifo->IWFP.size();
510  for (int i=0;i<n;i++) {
511  wavearray<double>* wf = (wavearray<double>*)ifo->IWFP[i];
512  delete wf;
513  }
514  ifo->IWFP.clear();
515  ifo->IWFID.clear();
516 
517  n = ifo->RWFP.size();
518  for (int i=0;i<n;i++) {
519  wavearray<double>* wf = (wavearray<double>*)ifo->RWFP[i];
520  delete wf;
521  }
522  ifo->RWFP.clear();
523  ifo->RWFID.clear();
524 }
525 
526 std::vector<netpixel> DoPCA(network* NET, CWB::config* cfg, int lag, int id) {
527 
528  double ee;
529 
530  size_t nIFO = NET->ifoList.size();
531 
532  float En = 2*NET->acor*NET->acor*nIFO; // network energy threshold in the sky loop
533 
534  int size = NET->a_00.size();
535  int f_ = NIFO/4;
536 
537  netpixel* pix;
538  netcluster* pwc = NET->getwc(lag);
539  std::vector<netpixel> vPIX;
540 
541  std::vector<int> pI = NET->wdmMRA.getXTalk(pwc, id, false); // buffer for pixel IDs
542  int V = pI.size();
543  if(V>cfg->BATCH) return vPIX; // attach TD amp to pixels < V
544 
545  wavearray<float> xi(size); xi=0; // PC 00 array
546  wavearray<float> XI(size); XI=0; // PC 90 array
547 
548  __m128* _xi = (__m128*) xi.data; // set pointer to PC 00 array
549  __m128* _XI = (__m128*) XI.data; // set pointer to PC 90 array
550 
551  __m128* _aa = (__m128*) NET->a_00.data; // set pointer to 00 array
552  __m128* _AA = (__m128*) NET->a_90.data; // set pointer to 90 array
553 
554  int nPC = 0;
555  for(int j=0; j<V; j++) {
556  int jf = j*f_; // source sse pointer increment
557  _sse_zero_ps(_xi+jf); // zero MRA amplitudes
558  _sse_zero_ps(_XI+jf); // zero MRA amplitudes
559  ee = _sse_abs_ps(_aa+jf,_AA+jf); // total pixel energy / quadrature
560  if(ee>En) nPC++; else ee=0.; // count core pixels
561  NET->rNRG.data[j] = ee; // init residual energy array
562  NET->pNRG.data[j] = NET->rNRG.data[j]; // init residual energy array
563  }
564 
565  nPC = NET->_sse_mra_ps(xi.data,XI.data,En,nPC); // get principal components
566 
567  for(int j=0; j<V; j++) { // loop over principal components
568  pix = pwc->getPixel(id,pI[j]);
569  vPIX.push_back(*pix); // save original pixels
570  pix->core = false;
571  ee = NET->pNRG.data[j]; // total pixel energy
572  if(ee<En) continue;
573  pix->core = true;
574  for(int i=0; i<nIFO; i++) {
575  pix->setdata(double(xi.data[j*NIFO+i]),'S',i); // store 00 whitened response PC
576  pix->setdata(double(XI.data[j*NIFO+i]),'P',i); // store 90 whitened response PC
577  }
578  }
579 
580  return vPIX;
581 }
582 
583 void ResetPCA(network* NET, CWB::config* cfg, netcluster* pwc, std::vector<netpixel>* vPIX, int ID) {
584 
585  std::vector<int> pI = NET->wdmMRA.getXTalk(pwc, ID, false); // buffer for pixel IDs
586  int V = pI.size();
587  if(V>cfg->BATCH) return; // attach TD amp to pixels < V
588  for(int j=0; j<V; j++) {
589  netpixel* pix = pwc->getPixel(ID,pI[j]);
590  *pix = (*vPIX)[j];
591  }
592 
593  while(!vPIX->empty()) vPIX->pop_back();
594  vPIX->clear(); std::vector<netpixel>().swap(*vPIX);
595 }
596 }
597 
598 // -------------------------------------------------------------------------
599 // --> END CWB_USER PLUGIN CODE 1
600 //
601 // /home/vedovato/Y2/coherent/SVN/watrepo/wat/trunk_wp_merge/trunk_4886_clean/tools/cwb/plugins/CWB_Plugin_QLveto.C
602 // -------------------------------------------------------------------------
603 
604 
605 // -------------------------------------------------------------------------
606 // --> BEGIN CWB_USER PLUGIN CODE 2
607 //
608 // /home/vedovato/Y2/coherent/SVN/watrepo/wat/trunk_wp_merge/trunk_4886_clean/tools/cwb/plugins/CWB_Plugin_Gating.C
609 // -------------------------------------------------------------------------
610 
611 }
612 #define XIFO 4
613 
614 #pragma GCC system_header
615 
616 #include "cwb.hh"
617 #include "cwb2G.hh"
618 #include "config.hh"
619 #include "network.hh"
620 #include "wavearray.hh"
621 #include "TString.h"
622 #include "TObjArray.h"
623 #include "TObjString.h"
624 #include "TRandom.h"
625 #include "Toolbox.hh"
626 
627 namespace CWB_UserPluginNamespace_1 {
628 
629 namespace CWB_UserPluginNamespace_2 {
630 
631 //!DATA_CONDITIONING
632 
633 // This plugin implements the time gating in the pixel's selection stage.
634 // Gating is a veto of the pixels belonging to a time interval.
635 // This plugin is used to exclude from the analysis the pixels
636 // in a time interval where there is a huge glitch.
637 // Huge glitches are harmful because they affect the correct estimation
638 // of the selection threshold and could mask the nearby events at lower SNR.
639 // Warning : events with high SNR can be rejected by this procedure (see SETHR)
640 
641 }
642 }
643 #define SETHR 1000000 // Is the threshold which define the time slices to be cutted
644 namespace CWB_UserPluginNamespace_1 {
645 
646 namespace CWB_UserPluginNamespace_2 {
647 
648  // Warning : this value depends on the frequency interval [fHigh:fLow]
649 
650 }
651 }
652 #define TWIN 0.5 // Is the window (sec) used to integrate the energies in time
653 namespace CWB_UserPluginNamespace_1 {
654 
655 namespace CWB_UserPluginNamespace_2 {
656 
657  // TWIN must be a multiple of the greatest time resolution used in the analysis
658 
659 }
660 }
661 #define TEDG 1.5 // Is the time window (sec) to be cutted when the time integrated energy is > SETHR
662 
663 namespace CWB_UserPluginNamespace_1 {
664 
665 namespace CWB_UserPluginNamespace_2 {
666 
667 void
668 CWB_Plugin(TFile* jfile, CWB::config* cfg, network* net, WSeries<double>* x, TString ifo, int type) {
669 
670 // this plugin is called in cwb2G.cc after the production of the TF maps with max over the sky energy (TFmax)
671 // The procedure is applied for each detector and for each resolution level using the TFmax.
672 
673  cout << endl;
674  cout << "-----> CWB_Plugin_Gating.C" << endl;
675  cout << "ifo " << ifo.Data() << endl;
676  cout << "type " << type << endl;
677  cout << endl;
678 
679  if(type==CWB_PLUGIN_ECOHERENCE) {
680 
681  // WSeries contains energy (it is stored in the 0 phase amplitudes)
682 
683  // import resolution level
684  size_t gILEVEL=-1; IMPORT(size_t,gILEVEL)
685  char slevel[256];sprintf(slevel,",%d,",gILEVEL);
686 
687  int nIFO = net->ifoListSize();
688  detector* pD[NIFO_MAX]; // pointers to detectors
689  for(int n=0;n<nIFO;n++) pD[n] = net->getifo(n);
690  WSeries<double>* WS[NIFO_MAX];
691  for(int n=0; n<nIFO; n++) WS[n] = pD[n]->getTFmap();
692 
693  int layers = WS[0]->maxLayer()+1; // numbers of frequency bins (first & last bins have df/2)
694  int slices = WS[0]->sizeZero(); // number of time bins
695 
696  double df = WS[0]->resolution(); // frequency bin resolution (hz)
697  double dt = 1./(2*df); // time bin resolution (sec)
698 
699  int rate = int(1./dt);
700 
701  cout << "layers : " << layers << "\t slices : " << slices << "\t rate : " << rate
702  << "\t dt : " << dt << "\t df : " << df << endl;
703 
704  double rTWIN = fabs(TWIN/dt-TMath::Nint(TWIN/dt));
705  if(TWIN<dt || rTWIN>1e-7) {
706  cout << "-----> CWB_Plugin_Gating.C : Error-> " << " TWIN=" << TWIN
707  << " is not a multiple of dt=" << dt << endl;
708  gSystem->Exit(1);
709  }
710 
711  double rTEDG = fabs(TEDG/dt-TMath::Nint(TEDG/dt));
712  if(TEDG<dt || rTEDG>1e-7) {
713  cout << "-----> CWB_Plugin_Gating.C : Error-> " << " TEDG=" << TEDG
714  << " is not a multiple of dt=" << dt << endl;
715  gSystem->Exit(1);
716  }
717 
718  // For each time slice (time index) we compute the sum of the pixel
719  // energy over all frequency layers (freq index)
720  // The sum is stored in the array 'se[NIFO_MAX]'
721  // se = projection of TF map energy on the time axis
722  wavearray<double> se[NIFO_MAX];
723  for(int n=0; n<nIFO; n++) {
724  se[n].resize(slices); se[n]=0;
725  for(int i=0;i<slices;i++) {
726  for(int j=0;j<layers;j++) se[n][i] += WS[n]->getSample(i,j);
727  }
728  }
729 
730  // A new array 'SE[NIFO_MAX]' is obtained from the arrays 'se[NIFO_MAX]'
731  // It contains the time integrated energies over the time window TWIN
732  // NOTE : this procedure makes the energies independents from the resolution level
733  int N = int(TWIN/dt); // number of time samples in TWIN
734  if(N<1) N=1;
735  wavearray<double> SE[NIFO_MAX];
736  for(int n=0; n<nIFO; n++) {
737  SE[n].resize(slices); SE[n]=0;
738  for(int i=N-1;i<slices;i++) for(int j=0;j<N;j++) SE[n][i]+=se[n][i-j];
739  }
740 
741  // find the time slices to be excluded :
742  // 1) from the computation of the pixel's selection threshold
743  // 2) from the pixel's selection
744  // A time slice is marked as to be cutted when the energy SE>SETHR
745  // When a time slice is cutted also the nearby M time slices are cutted [slice-M:slice+M]
746  // where M is the number of samples contained in TEDG sec
747  int X = int(cfg->segEdge/dt+0.5); // samples in segEdge : this the is scratch time
748  int M = int(TEDG/dt)+1; // samples in TEDG sec
749  wavearray<int> sCut[NIFO_MAX]; // array which contains the slices to be cut
750  for(int n=0; n<nIFO; n++) {
751  sCut[n].resize(slices); sCut[n]=0;
752  for(int i=0;i<slices;i++) {
753  if(SE[n][i]>SETHR) {
754  int is = i-M>X ? i-M : X;
755  int es = i+M<(slices-X) ? i+M : slices-X;
756  for(int ii=is;ii<es;ii++) sCut[n][ii]=1;
757  }
758  //cout << i << " " << SE[n][i] << endl;
759  }
760  }
761 
762  // All pixels in the layers which time slice is marked as cutted are filled with a negative energy = -1e20
763  // -> these pixels do not partecipate to the computation of the pixel's selection thereshold
764  // and moreover these pixels are excluded from the selection
765  double gating_time=0.; // total time vetoed by gating
766  for(int n=0; n<nIFO; n++) {
767  for(int i=0;i<slices;i++) { // set to -1e20 the energy
768  if(sCut[n][i]) {
769  gating_time+=dt;
770  for(int j=0;j<layers;j++) WS[n]->putSample(-1e20,i,j);
771  }
772  }
773  }
774 
775  // add infos to history
776  char info[256];
777  sprintf(info,"-RES:%d-GT:%g",cfg->l_high-gILEVEL,gating_time);
778  cwb2G* gCWB2G; IMPORT(cwb2G*,gCWB2G)
779  gCWB2G->PrintAnalysisInfo(CWB_STAGE_COHERENCE,"cwb2G::Coherence",info,false);
780  }
781 
782  return;
783 }
784 }
785 
786 // -------------------------------------------------------------------------
787 // --> END CWB_USER PLUGIN CODE 2
788 //
789 // /home/vedovato/Y2/coherent/SVN/watrepo/wat/trunk_wp_merge/trunk_4886_clean/tools/cwb/plugins/CWB_Plugin_Gating.C
790 // -------------------------------------------------------------------------
791 
792 
793 // -------------------------------------------------------------------------
794 // --> MAIN CWB_USER PLUGIN CODE
795 //
796 // INPUT PLUGINS :
797 //
798 // 1 - /home/vedovato/Y2/coherent/SVN/watrepo/wat/trunk_wp_merge/trunk_4886_clean/tools/cwb/plugins/CWB_Plugin_QLveto.C
799 // 2 - /home/vedovato/Y2/coherent/SVN/watrepo/wat/trunk_wp_merge/trunk_4886_clean/tools/cwb/plugins/CWB_Plugin_Gating.C
800 //
801 // -------------------------------------------------------------------------
802 
803 }
804 #define XIFO 4
805 #pragma GCC system_header
806 #include "cwb.hh"
807 
808 
809 namespace CWB_UserPluginNamespace_1 {
810 
811 void
812 CWB_Plugin(TFile* jfile, CWB::config* cfg, network* net, WSeries<double>* x, TString ifo, int type) {
813 
814  CWB_UserPluginNamespace_1::CWB_Plugin(jfile, cfg, net, x, ifo, type); // CALL USER PLUGIN CODE 1
815  CWB_UserPluginNamespace_2::CWB_Plugin(jfile, cfg, net, x, ifo, type); // CALL USER PLUGIN CODE 2
816 }
817 }
818 
819 // -------------------------------------------------------------------------
820 // --> END CWB_USER PLUGIN CODE 1
821 //
822 // macro/CWB_Plugin_QLveto_Gating.C
823 // -------------------------------------------------------------------------
824 
825 
826 // -------------------------------------------------------------------------
827 // --> BEGIN CWB_USER PLUGIN CODE 2
828 //
829 // macro/CWB_Plugin_EBBH.C
830 // -------------------------------------------------------------------------
831 
832 #define XIFO 4
833 
834 #pragma GCC system_header
835 
836 #include "cwb.hh"
837 #include "config.hh"
838 #include "network.hh"
839 #include "wavearray.hh"
840 #include "TString.h"
841 #include "TObjArray.h"
842 #include "TObjString.h"
843 #include "TRandom.h"
844 #include "TComplex.h"
845 #include "TMath.h"
846 #include "mdc.hh"
847 #include <vector>
848 
849 namespace CWB_UserPluginNamespace_2 {
850 
851 void
852 CWB_Plugin(TFile* jfile, CWB::config* cfg, network* net, WSeries<double>* x, TString ifo, int type) {
853 
854 // Plugin to generate simulated gaussian noise and injected 'on the fly' EBBH
855 
856  cout << endl;
857  cout << "-----> plugins/CWB_Plugin_EBBH.C" << endl;
858  cout << "ifo " << ifo.Data() << endl;
859  cout << "type " << type << endl;
860  cout << endl;
861 
862  if(type==CWB_PLUGIN_CONFIG) {
863 // cfg->dataPlugin=true; // disable read data from frames
864  cfg->mdcPlugin=true; // disable read mdc from frames
865  }
866 
867  if(type==CWB_PLUGIN_MDC) {
868 
869  char cmd[128];
870  sprintf(cmd,"network* net = (network*)%p;",net);
871  gROOT->ProcessLine(cmd);
872  sprintf(cmd,"CWB::config* cfg = (CWB::config*)%p;",cfg);
873  gROOT->ProcessLine(cmd);
874 
875  CWB::mdc MDC(net);
876 
877  // ---------------------------------
878  // read plugin config
879  // ---------------------------------
880 
881  cfg->configPlugin.Exec();
882 
883  // ---------------------------------
884  // set list of mdc waveforms
885  // ---------------------------------
886 
887  IMPORT(CWB::mdc,MDC)
888  MDC.Print();
889 
890  // ---------------------------------
891  // get mdc data
892  // ---------------------------------
893 
894  MDC.Get(*x,ifo);
895 
896  // ---------------------------------
897  // set mdc list in the network class
898  // ---------------------------------
899 
900  //if(type==CWB_PLUGIN_INIT_JOB)
901  {
902  if(ifo.CompareTo(net->ifoName[0])==0) {
903  net->mdcList.clear();
904  net->mdcType.clear();
905  net->mdcTime.clear();
906  net->mdcList=MDC.mdcList;
907  net->mdcType=MDC.mdcType;
908  net->mdcTime=MDC.mdcTime;
909 
910  double Tb = x->start();
911  double dT = x->size()/x->rate();
912  char log_label[512];
913  char tmpFile[1024];
914  sprintf(log_label,"%d_%d_%s_job%d",int(Tb),int(dT),cfg->data_label,net->nRun);
915  sprintf(tmpFile,"%s/%s-LogTMP.txt",cfg->log_dir,log_label);
916  cout << "Write MDC Log : " << tmpFile << endl;
917  MDC.DumpLog(tmpFile);
918 
919  }
920  }
921 
922 
923  cout.precision(14);
924  for(int k=0;k<(int)net->mdcList.size();k++) cout << k << " mdcList " << MDC.mdcList[k] << endl;
925  for(int k=0;k<(int)net->mdcTime.size();k++) cout << k << " mdcTime " << MDC.mdcTime[k] << endl;
926  for(int k=0;k<(int)net->mdcType.size();k++) cout << k << " mdcType " << MDC.mdcType[k] << endl;
927  }
928 /*
929  if(type==CWB_PLUGIN_CLOSE_JOB) {
930  if(ifo.CompareTo(net->ifoName[0])==0) {
931  double Tb = x->start();
932  double dT = x->size()/x->rate();
933  char log_label[512];
934  char tmpFile[1024];
935  char outFile[1024];
936  sprintf(log_label,"%d_%d_%s_job%d",int(Tb),int(dT),cfg->data_label,net->nRun);
937  sprintf(tmpFile,"%s/%s-LogTMP.txt",cfg->log_dir,log_label);
938  sprintf(outFile,"%s/%s-Log.txt",cfg->log_dir,log_label);
939  char command[1024];
940  sprintf(command,"/bin/mv %s %s", tmpFile, outFile);
941  gSystem->Exec(command);
942  }
943  }
944 */
945  return;
946 }
947 }
948 
949 // -------------------------------------------------------------------------
950 // --> END CWB_USER PLUGIN CODE 2
951 //
952 // macro/CWB_Plugin_EBBH.C
953 // -------------------------------------------------------------------------
954 
955 
956 // -------------------------------------------------------------------------
957 // --> MAIN CWB_USER PLUGIN CODE
958 //
959 // INPUT PLUGINS :
960 //
961 // 1 - macro/CWB_Plugin_QLveto_Gating.C
962 // 2 - macro/CWB_Plugin_EBBH.C
963 //
964 // -------------------------------------------------------------------------
965 
966 #define XIFO 4
967 #pragma GCC system_header
968 #include "cwb.hh"
969 
970 
971 void
972 CWB_Plugin(TFile* jfile, CWB::config* cfg, network* net, WSeries<double>* x, TString ifo, int type) {
973 
974  CWB_UserPluginNamespace_1::CWB_Plugin(jfile, cfg, net, x, ifo, type); // CALL USER PLUGIN CODE 1
975  CWB_UserPluginNamespace_2::CWB_Plugin(jfile, cfg, net, x, ifo, type); // CALL USER PLUGIN CODE 2
976 }
nIFO
CWB::mdc MDC(net)
shift breaksw case n
Definition: cwb_clchunk.csh:75
shift breaksw case M
shift breaksw case m
waveform wf
char fname[256]
void CWB_Plugin(TFile *jfile, CWB::config *cfg, network *net, WSeries< double > *x, TString ifo, int type)
void PlotWaveform(TString ifo, wavearray< double > *wfREC, CWB::config *cfg, bool fft=false, bool strain=false)
void ResetPCA(network *NET, CWB::config *cfg, netcluster *pwc, std::vector< netpixel > *vPIX, int ID)
void GetLveto(netcluster *pwc, int cid, int nifo, float *Lveto)
std::vector< netpixel > DoPCA(network *NET, CWB::config *cfg, int lag, int id)
void CWB_Plugin(TFile *jfile, CWB::config *cfg, network *net, WSeries< double > *x, TString ifo, int type)
void CWB_Plugin(TFile *jfile, CWB::config *cfg, network *net, WSeries< double > *x, TString ifo, int type)
void CWB_Plugin(TFile *jfile, CWB::config *cfg, network *NET, WSeries< double > *x, TString ifo, int type)
string network
Definition: cWB_conf.py:7
string label
Definition: cWB_conf.py:18