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