Logo coherent WaveBurst  
Config Reference Guide
Logo
CWB_Plugin_Gating_QLWveto.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 - CWB_Plugin_Gating.C
8 // 2 - CWB_Plugin_QLWveto.C
9 //
10 // -------------------------------------------------------------------------
11 
12 
13 // -------------------------------------------------------------------------
14 // --> BEGIN CWB_USER PLUGIN CODE 1
15 //
16 // CWB_Plugin_Gating.C
17 // -------------------------------------------------------------------------
18 
19 #define XIFO 4
20 
21 #pragma GCC system_header
22 
23 #include "cwb.hh"
24 #include "cwb2G.hh"
25 #include "config.hh"
26 #include "network.hh"
27 #include "wavearray.hh"
28 #include "TString.h"
29 #include "TObjArray.h"
30 #include "TObjString.h"
31 #include "TRandom.h"
32 #include "Toolbox.hh"
33 
34 namespace CWB_UserPluginNamespace_1 {
35 
36 //!DATA_CONDITIONING
37 
38 // This plugin implements the time gating in the pixel's selection stage.
39 // Gating is a veto of the pixels belonging to a time interval.
40 // This plugin is used to exclude from the analysis the pixels
41 // in a time interval where there is a huge glitch.
42 // Huge glitches are harmful because they affect the correct estimation
43 // of the selection threshold and could mask the nearby events at lower SNR.
44 // Warning : events with high SNR can be rejected by this procedure (see SETHR)
45 
46 }
47 #define SETHR 1000000 // Is the threshold which define the time slices to be cutted
48 namespace CWB_UserPluginNamespace_1 {
49 
50  // Warning : this value depends on the frequency interval [fHigh:fLow]
51 
52 }
53 #define TWIN 0.5 // Is the window (sec) used to integrate the energies in time
54 namespace CWB_UserPluginNamespace_1 {
55 
56  // TWIN must be a multiple of the greatest time resolution used in the analysis
57 
58 }
59 #define TEDG 1.5 // Is the time window (sec) to be cutted when the time integrated energy is > SETHR
60 
61 namespace CWB_UserPluginNamespace_1 {
62 
63 void
64 CWB_Plugin(TFile* jfile, CWB::config* cfg, network* net, WSeries<double>* x, TString ifo, int type) {
65 
66 // this plugin is called in cwb2G.cc to the whitened data at the beginning of the COHERENT stage
67 
68 // cout << endl;
69 // cout << "-----> CWB_Plugin_Gating.C" << endl;
70 // cout << "ifo " << ifo.Data() << endl;
71 // cout << "type " << type << endl;
72 // cout << endl;
73 
74  if(type==CWB_PLUGIN_CONFIG) {
75  cout << endl;
76  cout << "-----> CWB_Plugin_Gating.C" << endl;
77  cout << endl;
78  }
79 
80  if(type==CWB_PLUGIN_ICOHERENCE) {
81 
82  // get data range
83  cwb2G* gCWB2G; IMPORT(cwb2G*,gCWB2G)
84  double Tb = gCWB2G->GetSegBegin()-cfg->segEdge;
85  double Te = gCWB2G->GetSegEnd()+cfg->segEdge;
86 
87 
88  //cout << "CWB_Plugin_Gating.C - " << "Tb : " << int(Tb) << " Te : " << int(Te) << endl;
89 
90  int nIFO = (gCWB2G->IsSingleDetector()) ? 1 : net->ifoListSize(); // number of detectors
91  int size = net->getifo(0)->getHoT()->size(); // number of time bins
92  double rate = net->getifo(0)->getHoT()->rate(); // rate
93  double dt = 1./rate; // time bin resolution (sec)
94 
95  //cout << "-----> CWB_Plugin_Gating.C - size : " << size << "\t rate : " << rate << "\t dt : " << dt << " length(sec) " << size*dt << endl;
96 
97  wavearray<double>* hot[NIFO_MAX];
98  for(int n=0; n<nIFO; n++) hot[n] = net->getifo(n)->getHoT(); // whitened data
99 
100  // A new array 'SE' is obtained from the arrays 'hot whitened data'
101  // It contains the time integrated energies over the time window TWIN
102  // A time sample is marked as to be vetoed when the energy SE>SETHR
103  // When a time sample 'i' is vetoed also the nearby M time size are vetoed [i-M:i+M]
104  // where M is the number of samples contained in TEDG sec
105 
106  int X = int(cfg->segEdge/dt+0.5); // samples in segEdge : this the is scratch time
107  int M = int(TEDG/dt)+1; // samples in TEDG sec
108  int N = int(TWIN/dt); // number of time samples in TWIN
109  if(N<1) N=1;
110  vector<waveSegment> glist; // gating segment list
111  wavearray<double> SE(size);
112  wavearray<int> sVeto(size); // array which contains samples to be vetoed
113 
114  int gsize=0;
115  for(int n=0; n<nIFO; n++) { // loop over detectors
116  SE=0;
117 // for(int i=N-1;i<size;i++) for(int j=0;j<N;j++) SE[i]+=pow(hot[n]->data[i-j],2);
118 
119  for(int j=0;j<N;j++) SE[N-1]+=pow(hot[n]->data[N-1-j],2);
120  for(int i=N;i<size;i++) {
121  SE[i] = SE[i-1];
122  SE[i] -= pow(hot[n]->data[i-N],2);
123  SE[i] += pow(hot[n]->data[i],2);
124  }
125 
126  sVeto=0;
127  for(int i=0;i<size;i++) {
128  if(SE[i]>SETHR) {
129  int is = i-M>X ? i-M : X;
130  int es = i+M<(size-X) ? i+M : size-X;
131  for(int k=is;k<es;k++) sVeto[k]=1;
132  }
133  }
134  // array derivative -> gating_start=1, gating_stop=-1
135  sVeto[0]=0;sVeto[size-1]=0;
136  for(int i=0;i<size-1;i++) sVeto[i]=sVeto[i+1]-sVeto[i];
137 
138  // build the gating segment list
139  waveSegment gseg; // gating segment
140  gseg.index = 0;
141  for(int i=0;i<size;i++) {
142  if(sVeto[i]== 1) gseg.start = Tb+int(i*dt); // round to nearest lower integer
143  if(sVeto[i]==-1) {
144  gseg.stop = Tb+int(i*dt+0.5); // round to nearest higher integer
145  gseg.index++;
146  glist.push_back(gseg);
147  }
148  }
149 
150  if(glist.size()>0) {
151  cout << endl;
152  cout.precision(10);
153  for(int j=gsize;j<glist.size();j++) {
154  cout << j << " -----> CWB_Plugin_Gating.C - " << net->getifo(n)->Name << " gating time : start="
155  << glist[j].start << " stop=" << glist[j].stop << " len=" << glist[j].stop-glist[j].start << endl;
156  }
157  cout << endl;
158  gsize=glist.size();
159  }
160  }
161 
162  double gating_time = 0; // total gating time
163  if(glist.size()) {
164  glist = CWB::Toolbox::unionSegments(glist); // Join & sort a waveSegment list
165  gating_time = CWB::Toolbox::getTimeSegList(glist); // get total gating time
166  glist = CWB::Toolbox::invertSegments(glist); // Invert waveSegment list
167  net->segList = CWB::Toolbox::mergeSegLists(glist,net->segList); // merge gating veto list to the net->segList
168  }
169 
170  cout<< "-----> CWB_Plugin_Gating.C - gating time : " << gating_time << endl << endl;
171 
172  // add infos to history
173  char info[256];
174  sprintf(info,"-GT:%g",gating_time);
175  gCWB2G->PrintAnalysisInfo(CWB_STAGE_COHERENCE,"cwb2G::Coherence",info,false);
176  }
177 
178  return;
179 }
180 }
181 
182 // -------------------------------------------------------------------------
183 // --> END CWB_USER PLUGIN CODE 1
184 //
185 // CWB_Plugin_Gating.C
186 // -------------------------------------------------------------------------
187 
188 
189 // -------------------------------------------------------------------------
190 // --> BEGIN CWB_USER PLUGIN CODE 2
191 //
192 // CWB_Plugin_QLWveto.C
193 // -------------------------------------------------------------------------
194 
195 #define XIFO 4
196 
197 #pragma GCC system_header
198 
199 #include "cwb.hh"
200 #include "config.hh"
201 #include "network.hh"
202 #include "wavearray.hh"
203 #include "TString.h"
204 #include "TObjArray.h"
205 #include "TObjString.h"
206 #include "TRandom.h"
207 #include "TComplex.h"
208 #include "TMath.h"
209 #include "mdc.hh"
210 #include "watplot.hh"
211 #include "gwavearray.hh"
212 #include <vector>
213 
214 namespace CWB_UserPluginNamespace_2 {
215 
216 //#define PLOT_LIKELIHOOD
217 //#define PLOT_WHITENED_WAVEFORMS
218 
219 }
220 #define NTHR 1
221 #define ATHR 7.58859
222 
223 namespace CWB_UserPluginNamespace_2 {
224 
225 float GetQveto(wavearray<double>* wf);
226 void GetLveto(netcluster* pwc, int cid, int nifo, float* Lveto);
227 void GetWveto(netcluster* pwc, int cid, int nifo, float* Wveto);
228 void PlotWaveform(TString ifo, wavearray<double>* wfREC,
229  CWB::config* cfg, bool fft=false, bool strain=false);
230 void ClearWaveforms(detector* ifo);
231 
232 
233 void
234 CWB_Plugin(TFile* jfile, CWB::config* cfg, network* NET, WSeries<double>* x, TString ifo, int type) {
235 //!MISCELLANEA
236 // Extract whitened reconstructed waveforms, and compute the Qveto, Lveto & Wveto parameters
237 
238  cout << endl;
239  cout << "-----> CWB_Plugin_QLWveto.C" << endl;
240  cout << "ifo " << ifo.Data() << endl;
241  cout << "type " << type << endl;
242  cout << endl;
243 
244  float Qveto[2*NIFO_MAX]; // Qveto
245  float Lveto[3]; // Lveto
246  float Wveto[2]; // Wveto
247 
248  if(type==CWB_PLUGIN_CONFIG) {
249  cfg->outPlugin=true; // disable built-in output root file
250  }
251 
252  if(type==CWB_PLUGIN_ILIKELIHOOD) {
253  NET->wfsave=true; // enable waveform save
254 
255  // search output root file in the system list
256  TFile* froot = NULL;
257  TList *files = (TList*)gROOT->GetListOfFiles();
258  TString outDump="";
259  netevent* EVT;
260  int nIFO = NET->ifoListSize(); // number of detectors
261  if (files) {
262  TIter next(files);
263  TSystemFile *file;
264  TString fname;
265  bool check=false;
266  while ((file=(TSystemFile*)next())) {
267  fname = file->GetName();
268  // set output root file as the current file
269  if(fname.Contains("wave_")) {
270  froot=(TFile*)file;froot->cd();
271  outDump=fname;
272  outDump.ReplaceAll(".root.tmp",".txt");
273  //cout << "output file name : " << fname << endl;
274  }
275  }
276  if(!froot) {
277  cout << "CWB_Plugin_QLWveto.C : Error - output root file not found" << endl;
278  gSystem->Exit(1);
279  }
280  } else {
281  cout << "CWB_Plugin_QLWveto.C : Error - output root file not found" << endl;
282  gSystem->Exit(1);
283  }
284 
285  TTree* net_tree = (TTree *) froot->Get("waveburst");
286  if(net_tree==NULL) {
287  EVT = new netevent(nIFO);
288  net_tree = EVT->setTree();
289  net_tree->Branch("Qveto",Qveto,TString::Format("Qveto[%i]/F",2*cfg->nIFO));
290  net_tree->Branch("Lveto",Lveto,TString::Format("Lveto[%i]/F",3));
291  net_tree->Branch("Wveto",Wveto,TString::Format("Wveto[%i]/F",2));
292  }
293  }
294 
295  if(type==CWB_PLUGIN_OLIKELIHOOD) {
296 
297  if(TString(cfg->analysis)!="2G") {
298  cout << "CWB_Plugin_QLWveto.C -> "
299  << "CWB_PLUGIN_OLIKELIHOOD implemented only for 2G" << endl;
300  gSystem->Exit(1);
301  }
302 
303  // import ifactor
304  int gIFACTOR=-1; IMPORT(int,gIFACTOR)
305  cout << "-----> CWB_Plugin_QLWveto.C -> "
306  << " gIFACTOR : " << gIFACTOR << endl;
307 
308  // import slagShift
309  float* gSLAGSHIFT=NULL; IMPORT(float*,gSLAGSHIFT)
310 
311  int nIFO = NET->ifoListSize(); // number of detectors
312  int K = NET->nLag; // number of time lag
313  netevent* EVT;
314  wavearray<double> id;
315  //double factor = cfg->simulation==3||cfg->simulation==4 ? -gIFACTOR : cfg->factors[gIFACTOR];
316  double factor = cfg->factors[gIFACTOR];
317  int rate = 0; // select all resolutions
318 
319  // search output root file in the system list
320  TFile* froot = NULL;
321  TList *files = (TList*)gROOT->GetListOfFiles();
322  TString outDump="";
323  if (files) {
324  TIter next(files);
325  TSystemFile *file;
326  TString fname;
327  bool check=false;
328  while ((file=(TSystemFile*)next())) {
329  fname = file->GetName();
330  // set output root file as the current file
331  if(fname.Contains("wave_")) {
332  froot=(TFile*)file;froot->cd();
333  outDump=fname;
334  outDump.ReplaceAll(".root.tmp",".txt");
335  //cout << "output file name : " << fname << endl;
336  }
337  }
338  if(!froot) {
339  cout << "CWB_Plugin_QLWveto.C : Error - output root file not found" << endl;
340  gSystem->Exit(1);
341  }
342  } else {
343  cout << "CWB_Plugin_QLWveto.C : Error - output root file not found" << endl;
344  gSystem->Exit(1);
345  }
346 
347  TTree* net_tree = (TTree *) froot->Get("waveburst");
348  if(net_tree!=NULL) {
349  EVT = new netevent(net_tree,nIFO);
350  net_tree->SetBranchAddress("Qveto",Qveto);
351  net_tree->SetBranchAddress("Lveto",Lveto);
352  net_tree->SetBranchAddress("Wveto",Wveto);
353  } else {
354  EVT = new netevent(nIFO);
355  net_tree = EVT->setTree();
356  net_tree->Branch("Qveto",Qveto,TString::Format("Qveto[%i]/F",2*cfg->nIFO));
357  net_tree->Branch("Lveto",Lveto,TString::Format("Lveto[%i]/F",3));
358  net_tree->Branch("Wveto",Wveto,TString::Format("Wveto[%i]/F",2));
359  }
360  EVT->setSLags(gSLAGSHIFT); // set slags into netevent
361 
362  for(int k=0; k<K; k++) { // loop over the lags
363 
364  id = NET->getwc(k)->get(const_cast<char*>("ID"), 0, 'L', rate);
365 
366  for(int j=0; j<(int)id.size(); j++) { // loop over cluster index
367 
368  int ID = size_t(id.data[j]+0.5);
369 
370  if(NET->getwc(k)->sCuts[ID-1]!=-1) continue; // skip rejected/processed clusters
371 
372  double ofactor=0;
373  if(cfg->simulation==4) ofactor=-factor;
374  else if(cfg->simulation==3) ofactor=-gIFACTOR;
375  else ofactor=factor;
376 
377  EVT->output2G(NULL,NET,ID,k,ofactor); // get reconstructed parameters
378 
379  wavearray<double>** pwfREC = new wavearray<double>*[nIFO];
380  detector* pd = NET->getifo(0);
381  int idSize = pd->RWFID.size();
382 
383  int wfIndex=-1;
384  for (int mm=0; mm<idSize; mm++) if (pd->RWFID[mm]==ID) wfIndex=mm;
385  if(wfIndex==-1) continue;
386 
387  netcluster* pwc = NET->getwc(k);
388  cout << endl << "----------------------------------------------------------------" << endl;
389  GetLveto(pwc, ID, nIFO, Lveto);
390  cout << "Lveto : " << "fmean : " << Lveto[0] << " frms : " << Lveto[1]
391  << " Energy Ratio : " << Lveto[2] << endl << endl;
392  GetWveto(pwc, ID, nIFO, Wveto);
393  cout << "Wveto : " << " Slope : " << Wveto[0] << " Correlation : " << Wveto[1] << endl << endl;
394 
395  // extract whitened reconstructed waveforms
396  for(int n=0; n<nIFO; n++) {
397 
398  pd = NET->getifo(n);
399 
400  pwfREC[n] = pd->RWFP[wfIndex];
401  wavearray<double>* wfREC = pwfREC[n]; // array of reconstructed waveforms
402 
403 #ifdef PLOT_WHITENED_WAVEFORMS
404  //PlotWaveform(NET->ifoName[n], wfREC, cfg, false, false);
405  PlotWaveform(NET->ifoName[n], wfREC, cfg, true, false);
406 #endif
407  // reconstructed whitened waveform
408  NET->getMRAwave(ID,k,'S',0,true);
409  Qveto[n] = GetQveto(&(pd->waveForm));
410  // whitened waveform
411  NET->getMRAwave(ID,k,'W',0,true);
412  Qveto[n+nIFO] = GetQveto(&(pd->waveBand));
413 
414  //Qveto[n] = GetQveto(wfREC);
415  cout << "Qveto : " << pd->Name << " Qveto[R] = " << Qveto[n]
416  << " Qveto[W] = " << Qveto[n+nIFO] << endl;
417 
418  if(!cfg->simulation) ClearWaveforms(pd); // release waveform memory
419  }
420  cout << "----------------------------------------------------------------" << endl;
421  delete [] pwfREC;
422 
423  std::vector<int> sCuts = NET->getwc(k)->sCuts; // save cCuts
424  // set sCuts=1 to the events which must be not copied with cps to pwc
425  for(int i=0; i<(int)sCuts.size(); i++) if(i!=ID-1) NET->getwc(k)->sCuts[i]=1;
426 
427  // ID can not be used to get the event, to get event use ID=1 (ex: for watplot)
428  NET->getwc(k)->sCuts = sCuts; // restore cCuts
429 
430  if(cfg->dump) EVT->dopen(outDump.Data(),const_cast<char*>("a"),false);
431  EVT->output2G(net_tree,NET,ID,k,ofactor); // get reconstructed parameters
432  if(cfg->dump) {
433  // add Qveto to dump file
434  fprintf(EVT->fP,"Qveto: ");
435  for(int i=0; i<2*nIFO; i++) fprintf(EVT->fP,"%f ",Qveto[i]);
436  fprintf(EVT->fP,"\n");
437  // add Lveto to dump file
438  fprintf(EVT->fP,"Lveto: ");
439  for(int i=0; i<3; i++) fprintf(EVT->fP,"%f ",Lveto[i]);
440  fprintf(EVT->fP,"\n");
441  // add Wveto to dump file
442  fprintf(EVT->fP,"Wveto: ");
443  for(int i=0; i<2; i++) fprintf(EVT->fP,"%f ",Wveto[i]);
444  fprintf(EVT->fP,"\n");
445  }
446  if(cfg->dump) EVT->dclose();
447  if(!cfg->cedDump) NET->getwc(k)->sCuts[ID-1]=1; // mark as processed
448  }
449  }
450 
451  jfile->cd();
452  if(EVT) delete EVT;
453  }
454  return;
455 }
456 
457 float
458 GetQveto(wavearray<double>* wf) {
459 
460  wavearray<double> x = *wf;;
461 
462  // resample data by a factor 4
463  int xsize=x.size();
464  x.FFTW(1);
465  x.resize(4*x.size());
466  x.rate(4*x.rate());
467  for(int j=xsize;j<x.size();j++) x[j]=0;
468  x.FFTW(-1);
469 
470  // extract max/min values and save the absolute values in the array 'a'
471  wavearray<double> a(x.size());
472  int size=0;
473  double dt = 1./x.rate();
474  double prev=x[0];
475  double xmax=0;
476  for (int i=1;i<x.size();i++) {
477  if(fabs(x[i])>xmax) xmax=fabs(x[i]);
478  if(prev*x[i]<0) {
479  a[size]=xmax;
480  size++;
481  xmax=0;
482  }
483  prev=x[i];
484  }
485 
486  // find max value/index ans save on amax/imax
487  int imax=-1;
488  double amax=0;
489  for (int i=1;i<size;i++) {
490  if(a[i]>amax) {amax=a[i];imax=i;}
491  }
492 
493 /*
494  cout << endl;
495  cout << "a[imax-2] " << a[imax-2] << endl;
496  cout << "a[imax-1] " << a[imax-1] << endl;
497  cout << "a[imax] " << a[imax] << endl;
498  cout << "a[imax+1] " << a[imax+1] << endl;
499  cout << "a[imax+2] " << a[imax+2] << endl;
500  cout << endl;
501 */
502 
503  // compute Qveto
504  double ein=0; // energy of max values inside NTHR
505  double eout=0; // energy of max values outside NTHR
506  for (int i=0;i<size;i++) {
507  if(abs(imax-i)<=NTHR) {
508  ein+=a[i]*a[i];
509  //cout << i << " ein " << a[i] << " " << amax << endl;
510  } else {
511  if(a[i]>amax/ATHR) eout+=a[i]*a[i];
512  //if(a[i]>amax/ATHR) cout << i << " eout " << a[i] << " " << amax << endl;
513  }
514  }
515  float Qveto = ein>0 ? eout/ein : 0.;
516  //cout << "Qveto : " << Qveto << " ein : " << ein << " eout : " << eout << endl;
517 
518  return Qveto;
519 }
520 
521 void
522 GetLveto(netcluster* pwc, int cid, int nifo, float* Lveto) {
523 //
524 // input
525 // pwc : pointer to netcluster object
526 // cid : cluster id
527 // nifo : number of detectors
528 // output
529 // Lveto[0] : line frequency
530 // Lveto[1] : line bandwitdh
531 // Lveto[2] : line enery ratio (line_energy / total_energy)
532 //
533 
534  Lveto[0] = Lveto[1] = Lveto[2] = 0;
535 
536  std::vector<int>* vint = &(pwc->cList[cid-1]); // pixel list
537  int V = vint->size(); // cluster size
538  if(!V) return;
539 
540  // ------------------------------------------------------------------
541  // Find max pixel parameters
542  // ------------------------------------------------------------------
543 
544  double likeMax=0; // maximum pixel's energy
545  double likeTot=0; // total cluster energy
546  double freqMax; // frequency of the pixel with max energy
547  double dfreqMax; // df of the pixel with max energy
548  for(int n=0; n<V; n++) {
549  netpixel* pix = pwc->getPixel(cid,n);
550  if(pix->layers%2==0) {
551  cout << "CWB_Plugin_QLWveto.C - Error : is enabled only for WDM (2G)" << endl;
552  exit(1);
553  }
554  if(!pix->core) continue; // select only the principal components pixels
555 
556  double likePix=0;
557  for(int m=0; m<nifo; m++) {
558  likePix += pow(pix->getdata('S',m),2); // like whitened reconstructed signal 00
559  likePix += pow(pix->getdata('P',m),2); // like whitened reconstructed signal 90
560  }
561 
562  double freq = pix->frequency*pix->rate/2.;
563  double df = pix->rate/2.;
564 
565  likeTot+=likePix;
566  if(likePix>likeMax) {likeMax=likePix;freqMax=freq;dfreqMax=df;}
567  }
568  //cout << "likeMax : " << likeMax << " likeTot : " << likeTot
569  // << " freqMax : " << freqMax << " dfreqMax : " << dfreqMax << endl;
570 
571  // ------------------------------------------------------------------
572  // Compute Lveto parameters
573  // ------------------------------------------------------------------
574 
575  double fmean=0; // line mean frequency
576  double frms=0; // line bandwidth
577  double likeLin=0; // line energy
578  for(int n=0; n<V; n++) {
579  netpixel* pix = pwc->getPixel(cid,n);
580  if(!pix->core) continue; // select only the principal components pixels
581 
582  double likePix=0;
583  for(int m=0; m<nifo; m++) {
584  likePix += pow(pix->getdata('S',m),2); // like whitened reconstructed signal 00
585  likePix += pow(pix->getdata('P',m),2); // like whitened reconstructed signal 90
586  }
587 
588  // the estimation is done for all pixels
589  // with freq in the range [freqMax-dfreqMax, freqMax+dfreqMax]
590  double freq = pix->frequency*pix->rate/2.;
591  if(fabs(freq-freqMax)<=dfreqMax) {
592  likeLin += likePix;
593  fmean += likePix*freq;
594  frms += likePix*freq*freq;
595  }
596  }
597 
598  fmean = fmean/likeLin;
599  frms = frms/likeLin-fmean*fmean;
600  frms = frms>0 ? sqrt(frms) : 0.;
601 
602  if(frms<dfreqMax/2.) frms=dfreqMax/2.;
603 
604  // ------------------------------------------------------------------
605  // Save Lveto parameters
606  // ------------------------------------------------------------------
607 
608  Lveto[0] = fmean; // line mean frequency
609  Lveto[1] = frms; // line bandwidth
610  Lveto[2] = likeTot>0. ? likeLin/likeTot : 0.; // energy ratio energy inside_line/total
611 
612  // ------------------------------------------------------------------
613  // plot time-frequency energy
614  // ------------------------------------------------------------------
615 
616 #if defined PLOT_LIKELIHOOD
617  watplot WTS(const_cast<char*>("wts"));
618  WTS.plot(pwc, cid, nifo, 'L', 0, const_cast<char*>("COLZ"));
619  WTS.canvas->Print("l_tfmap_scalogram.png");
620 #endif
621 
622  return;
623 }
624 
625 void
626 GetWveto(netcluster* pwc, int cid, int nifo, float* Wveto) {
627 //
628 // input
629 // pwc : pointer to netcluster object
630 // cid : cluster id
631 // nifo : number of detectors
632 // output
633 // Wveto[0] : whistle slope
634 // Wveto[1] : whistle correlation
635 //
636 
637  Wveto[0] = Wveto[1] = 0;
638 
639  std::vector<int>* vint = &(pwc->cList[cid-1]); // pixel list
640  int V = vint->size(); // cluster size
641  if(!V) return;
642 
643  std::vector<double> x;
644  std::vector<double> y;
645  std::vector<double> w;
646 
647  // extract pixels
648  for(int j=0; j<V; j++) {
649  netpixel* pix = pwc->getPixel(cid,j);
650  if(pix->layers%2==0) {
651  cout << "CWB_Plugin_QLWveto.C - Error : is enabled only for WDM (2G)" << endl;
652  exit(1);
653  }
654  if(pix->likelihood<1. || pix->frequency==0) continue;
655  //if(!pix->core) continue; // select only the principal components pixels
656 
657  double time = int(double(pix->time)/pix->layers)/pix->rate; // time in seconds from the start
658  double freq = pix->frequency*pix->rate/2.;
659 
660  x.push_back(time);
661  y.push_back(freq);
662  w.push_back(pix->likelihood);
663  }
664  int size = x.size();
665  if(size<5) return;
666 
667  double xcm, ycm, qxx, qyy, qxy, ew;
668  xcm = ycm = qxx = qyy = qxy = ew = 0;
669 
670  for(int i=0; i<size; ++i) {
671  xcm += x[i]*w[i];
672  ycm += y[i]*w[i];
673  ew += w[i];
674  }
675  xcm /= ew;
676  ycm /= ew;
677 
678  for(int i=0; i<size; ++i) {
679  qxx += (x[i] - xcm)*(x[i] - xcm)*w[i];
680  qyy += (y[i] - ycm)*(y[i] - ycm)*w[i];
681  qxy += (x[i] - xcm)*(y[i] - ycm)*w[i];
682  }
683 
684  double beta = qxy/qxx; // slope
685  double alpha = ycm-beta*xcm; // intercept
686  double corr = qxy/sqrt(qxx*qyy); // correlation
687  double duration = sqrt(qxx/ew); // duration
688  double bandwidth = sqrt(qyy/ew); // bandwidth
689 
690  //printf("alpha : %lf , beta : %lf , corr : %lf, dur : %lf , bw : %lf \n",
691  // alpha, beta, corr, duration, bandwidth);
692 
693  Wveto[0] = beta; // slope
694  Wveto[1] = fabs(corr); // correlation
695 
696  return;
697 }
698 
699 void
700 PlotWaveform(TString ifo, wavearray<double>* wfREC,
701  CWB::config* cfg, bool fft, bool strain) {
702 
703  watplot PTS(const_cast<char*>("ptswrc"),200,20,800,500);
704 
705  //cout << "Print " << fname << endl;
706  double tmin = wfREC->start();
707  wfREC->start(wfREC->start()-tmin);
708  if(fft) {
709  PTS.plot(wfREC, const_cast<char*>("ALP"), 1, 0, 0, true, cfg->fLow, cfg->fHigh);
710  } else {
711  PTS.plot(wfREC, const_cast<char*>("ALP"), 1, 0, 0);
712  }
713  PTS.graph[0]->SetLineWidth(1);
714  wfREC->start(wfREC->start()+tmin);
715 
716  char label[64]="";
717  if(fft) sprintf(label,"%s","fft");
718  else sprintf(label,"%s","time");
719  if(strain) sprintf(label,"%s_%s",label,"strain");
720  else sprintf(label,"%s_%s",label,"white");
721 
722  char fname[1024];
723  //sprintf(fname, "%s_wf_%s_rec_gps_%d.root",ifo.Data(),label,int(tmin));
724  sprintf(fname, "%s_wf_%s_rec_gps_%d.png",ifo.Data(),label,int(tmin));
725  PTS.canvas->Print(fname);
726  cout << "write : " << fname << endl;
727  //PTS.canvas->Write(REPLACE(fname,dirCED,gtype));
728 }
729 
730 void
731 ClearWaveforms(detector* ifo) {
732 
733  int n;
734 
735  n = ifo->IWFP.size();
736  for (int i=0;i<n;i++) {
737  wavearray<double>* wf = (wavearray<double>*)ifo->IWFP[i];
738  delete wf;
739  }
740  ifo->IWFP.clear();
741  ifo->IWFID.clear();
742 
743  n = ifo->RWFP.size();
744  for (int i=0;i<n;i++) {
745  wavearray<double>* wf = (wavearray<double>*)ifo->RWFP[i];
746  delete wf;
747  }
748  ifo->RWFP.clear();
749  ifo->RWFID.clear();
750 }
751 
752 }
753 
754 // -------------------------------------------------------------------------
755 // --> END CWB_USER PLUGIN CODE 2
756 //
757 // CWB_Plugin_QLWveto.C
758 // -------------------------------------------------------------------------
759 
760 
761 // -------------------------------------------------------------------------
762 // --> MAIN CWB_USER PLUGIN CODE
763 //
764 // INPUT PLUGINS :
765 //
766 // 1 - CWB_Plugin_Gating.C
767 // 2 - CWB_Plugin_QLWveto.C
768 //
769 // -------------------------------------------------------------------------
770 
771 #define XIFO 4
772 #pragma GCC system_header
773 #include "cwb.hh"
774 
775 
776 void
777 CWB_Plugin(TFile* jfile, CWB::config* cfg, network* net, WSeries<double>* x, TString ifo, int type) {
778 
779  CWB_UserPluginNamespace_1::CWB_Plugin(jfile, cfg, net, x, ifo, type); // CALL USER PLUGIN CODE 1
780  CWB_UserPluginNamespace_2::CWB_Plugin(jfile, cfg, net, x, ifo, type); // CALL USER PLUGIN CODE 2
781 }
nIFO
#define ATHR
#define SETHR
#define NTHR
#define TWIN
#define TEDG
void CWB_Plugin(TFile *jfile, CWB::config *cfg, network *net, WSeries< double > *x, TString ifo, int type)
shift breaksw case n
Definition: cwb_clchunk.csh:75
shift breaksw case M
shift breaksw case w
Definition: cwb_obchunk.csh:97
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 GetWveto(netcluster *pwc, int cid, int nifo, float *Wveto)
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)
string network
Definition: cWB_conf.py:7
string label
Definition: cWB_conf.py:18