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