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 double GetQfactor(wavearray<double>* wf, double frequency, bool fixAmax);
268 double GetPeakFrequency(wavearray<double>* wf);
269 void GetGaussFitPars(wavearray<double>* wf, double& mean, double& sigma, bool doenv=true);
270 void GetGaussFitPars2(wavearray<double>* wf, double& mean, double& sigma, bool fixAmax);
271 
272 void GetQveto(wavearray<double>* wf, float &Qveto, float &Qfactor);
273 void GetLveto(netcluster* pwc, int cid, int nifo, float* Lveto);
274 void PlotWaveform(TString ifo, wavearray<double>* wfREC,
275  CWB::config* cfg, bool fft=false, bool strain=false);
276 void ClearWaveforms(detector* ifo);
277 
278 std::vector<netpixel> DoPCA(network* NET, CWB::config* cfg, int lag, int id);
279 void ResetPCA(network* NET, CWB::config* cfg, netcluster* pwc, std::vector<netpixel>* vPIX, int ID);
280 
281 
282 void
283 CWB_Plugin(TFile* jfile, CWB::config* cfg, network* NET, WSeries<double>* x, TString ifo, int type) {
284 //!MISCELLANEA
285 // Extract whitened reconstructed waveforms, and compute the Qveto, Lveto parameters
286 
287 // cout << endl;
288 // cout << "-----> CWB_Plugin_QLveto.C" << endl;
289 // cout << "ifo " << ifo.Data() << endl;
290 // cout << "type " << type << endl;
291 // cout << endl;
292 
293  float Qveto[4]; // Qveto
294  float Lveto[3]; // Lveto
295 
296  if(type==CWB_PLUGIN_CONFIG) {
297  cout << endl;
298  cout << "-----> CWB_Plugin_QLveto.C" << endl;
299  cout << endl;
300  cfg->outPlugin=true; // disable built-in output root file
301  }
302 
303  if(type==CWB_PLUGIN_ILIKELIHOOD) {
304  NET->wfsave=true; // enable waveform save
305 
306  // search output root file in the system list
307  TFile* froot = NULL;
308  TList *files = (TList*)gROOT->GetListOfFiles();
309  TString outDump="";
310  netevent* EVT;
311  int nIFO = NET->ifoListSize(); // number of detectors
312  if (files) {
313  TIter next(files);
314  TSystemFile *file;
315  TString fname;
316  bool check=false;
317  while ((file=(TSystemFile*)next())) {
318  fname = file->GetName();
319  // set output root file as the current file
320  if(fname.Contains("wave_")) {
321  froot=(TFile*)file;froot->cd();
322  outDump=fname;
323  outDump.ReplaceAll(".root.tmp",".txt");
324  //cout << "output file name : " << fname << endl;
325  }
326  }
327  if(!froot) {
328  cout << "CWB_Plugin_QLveto.C : Error - output root file not found" << endl;
329  gSystem->Exit(1);
330  }
331  } else {
332  cout << "CWB_Plugin_QLveto.C : Error - output root file not found" << endl;
333  gSystem->Exit(1);
334  }
335 
336  TTree* net_tree = (TTree *) froot->Get("waveburst");
337  if(net_tree==NULL) {
338  EVT = new netevent(nIFO);
339  net_tree = EVT->setTree();
340  net_tree->Branch("Qveto",Qveto,TString::Format("Qveto[%i]/F",4));
341  net_tree->Branch("Lveto",Lveto,TString::Format("Lveto[%i]/F",3));
342  } else {
343  TBranch* branch;
344  bool qveto_exists=false;
345  bool lveto_exists=false;
346  TIter next(net_tree->GetListOfBranches());
347  while ((branch=(TBranch*)next())) {
348  if(TString("Qveto").CompareTo(branch->GetName())==0) qveto_exists=true;
349  if(TString("Lveto").CompareTo(branch->GetName())==0) lveto_exists=true;
350  }
351  next.Reset();
352  if(!qveto_exists) net_tree->Branch("Qveto",Qveto,TString::Format("Qveto[%i]/F",4));
353  if(!lveto_exists) net_tree->Branch("Lveto",Lveto,TString::Format("Lveto[%i]/F",3));
354  }
355  }
356 
357  if(type==CWB_PLUGIN_OLIKELIHOOD) {
358 
359  if(TString(cfg->analysis)!="2G") {
360  cout << "CWB_Plugin_QLveto.C -> "
361  << "CWB_PLUGIN_OLIKELIHOOD implemented only for 2G" << endl;
362  gSystem->Exit(1);
363  }
364 
365  // import ifactor
366  int gIFACTOR=-1; IMPORT(int,gIFACTOR)
367  //cout << "-----> CWB_Plugin_QLveto.C -> "
368  // << " gIFACTOR : " << gIFACTOR << endl;
369 
370  // import slagShift
371  float* gSLAGSHIFT=NULL; IMPORT(float*,gSLAGSHIFT)
372 
373  int nIFO = NET->ifoListSize(); // number of detectors
374  int K = NET->nLag; // number of time lag
375  netevent* EVT;
376  wavearray<double> id;
377  //double factor = cfg->simulation==3||cfg->simulation==4 ? -gIFACTOR : cfg->factors[gIFACTOR];
378  double factor = cfg->factors[gIFACTOR];
379  int rate = 0; // select all resolutions
380 
381  // search output root file in the system list
382  TFile* froot = NULL;
383  TList *files = (TList*)gROOT->GetListOfFiles();
384  TString outDump="";
385  if (files) {
386  TIter next(files);
387  TSystemFile *file;
388  TString fname;
389  bool check=false;
390  while ((file=(TSystemFile*)next())) {
391  fname = file->GetName();
392  // set output root file as the current file
393  if(fname.Contains("wave_")) {
394  froot=(TFile*)file;froot->cd();
395  outDump=fname;
396  outDump.ReplaceAll(".root.tmp",".txt");
397  //cout << "output file name : " << fname << endl;
398  }
399  }
400  if(!froot) {
401  cout << "CWB_Plugin_QLveto.C : Error - output root file not found" << endl;
402  gSystem->Exit(1);
403  }
404  } else {
405  cout << "CWB_Plugin_QLveto.C : Error - output root file not found" << endl;
406  gSystem->Exit(1);
407  }
408 
409  TTree* net_tree = (TTree *) froot->Get("waveburst");
410  if(net_tree!=NULL) {
411  EVT = new netevent(net_tree,nIFO);
412  net_tree->SetBranchAddress("Qveto",Qveto);
413  net_tree->SetBranchAddress("Lveto",Lveto);
414  } else {
415  EVT = new netevent(nIFO);
416  net_tree = EVT->setTree();
417  net_tree->Branch("Qveto",Qveto,TString::Format("Qveto[%i]/F",4));
418  net_tree->Branch("Lveto",Lveto,TString::Format("Lveto[%i]/F",3));
419  }
420  EVT->setSLags(gSLAGSHIFT); // set slags into netevent
421 
422  for(int k=0; k<K; k++) { // loop over the lags
423 
424  id = NET->getwc(k)->get(const_cast<char*>("ID"), 0, 'L', rate);
425 
426  for(int j=0; j<(int)id.size(); j++) { // loop over cluster index
427 
428  int ID = size_t(id.data[j]+0.5);
429 
430  if(NET->getwc(k)->sCuts[ID-1]!=-1) continue; // skip rejected/processed clusters
431 
432  double ofactor=0;
433  if(cfg->simulation==4) ofactor=-factor;
434  else if(cfg->simulation==3) ofactor=-gIFACTOR;
435  else ofactor=factor;
436 
437  EVT->output2G(NULL,NET,ID,k,ofactor); // get reconstructed parameters
438 
439  wavearray<double>** pwfREC = new wavearray<double>*[nIFO];
440  detector* pd = NET->getifo(0);
441  int idSize = pd->RWFID.size();
442 
443  int wfIndex=-1;
444  for (int mm=0; mm<idSize; mm++) if (pd->RWFID[mm]==ID) wfIndex=mm;
445  if(wfIndex==-1) continue;
446 
447  netcluster* pwc = NET->getwc(k);
448  cout << endl << "----------------------------------------------------------------" << endl;
449 
450  // extract whitened reconstructed waveforms
451  Qveto[0]=Qveto[1]=1.e20;
452  double Qfactor1[NIFO_MAX];
453  double Qfactor2[NIFO_MAX];
454  for(int n=0; n<nIFO; n++) {
455 
456  pd = NET->getifo(n);
457 
458  pwfREC[n] = pd->RWFP[wfIndex];
459  wavearray<double>* wfREC = pwfREC[n]; // array of reconstructed waveforms
460 
461 #ifdef PLOT_WHITENED_WAVEFORMS
462  //PlotWaveform(NET->ifoName[n], wfREC, cfg, false, false);
463  PlotWaveform(NET->ifoName[n], wfREC, cfg, true, false);
464 #endif
465  // store in Qveto[0/1] the minimum velue of Qveto/Qfactor between the reconstructed,whitened waveforms in all ifos
466  float qveto,qfactor;
467 
468  // reconstructed whitened waveform
469  NET->getMRAwave(ID,k,'S',0,true);
470  GetQveto(&(pd->waveForm), qveto, qfactor);
471  if(qveto<Qveto[0]) Qveto[0]=qveto;
472  if(qfactor<Qveto[1]) Qveto[1]=qfactor;
473 
474  Qfactor1[n] = qfactor;
475  double fpeak = GetPeakFrequency(&(pd->waveForm));
476  Qfactor2[n] = GetQfactor(&(pd->waveForm), fpeak, true); // get qfactor with amax fixed;
477  cout << endl;
478  cout << "Qfactor1/2 : " << pd->Name << " frequency[0] = " << EVT->frequency[0] << " fpeak = " << fpeak
479  << " -> " << Qfactor1[n] << " / " << Qfactor2[n] << endl;
480  cout << endl;
481 
482  // whitened waveform
483  NET->getMRAwave(ID,k,'W',0,true);
484  GetQveto(&(pd->waveBand), qveto, qfactor);
485  if(qveto<Qveto[0]) Qveto[0]=qveto;
486  if(qfactor<Qveto[1]) Qveto[1]=qfactor;
487 
488  cout << "Qveto : " << pd->Name << " Qveto[0] = " << Qveto[0]
489  << " Qveto[1] = " << Qveto[1] << endl;
490 
491  if(!cfg->simulation) ClearWaveforms(pd); // release waveform memory
492  }
493  delete [] pwfREC;
494 
495  // compute Qveto[2] using the weighted of Qfactor1 values
496  Qveto[2]=0.;
497  for(int n=0; n<nIFO; n++) Qveto[2]+=EVT->sSNR[n]*Qfactor1[n];
498  Qveto[2]/=EVT->likelihood;
499  // compute Qveto[3] using the weighted of Qfactor2 values
500  Qveto[3]=0.;
501  for(int n=0; n<nIFO; n++) Qveto[3]+=EVT->sSNR[n]*Qfactor2[n];
502  Qveto[3]/=EVT->likelihood;
503  cout << "Qveto :" << " Qveto[2] = " << Qveto[2]
504  << " Qveto[3] = " << Qveto[3] << endl;
505 
506  std::vector<netpixel> vPIX;
507  if(cfg->pattern>0) vPIX = DoPCA(NET, cfg, k, ID); // do PCA analysis
508  GetLveto(pwc, ID, nIFO, Lveto);
509  if(cfg->pattern>0) ResetPCA(NET, cfg, pwc, &vPIX, ID); // restore WP pwc
510 
511  cout << endl;
512  cout << "Lveto : " << "fmean : " << Lveto[0] << " frms : " << Lveto[1]
513  << " Energy Ratio : " << Lveto[2] << endl << endl;
514  cout << "----------------------------------------------------------------" << endl;
515 
516  std::vector<int> sCuts = NET->getwc(k)->sCuts; // save cCuts
517  // set sCuts=1 to the events which must be not copied with cps to pwc
518  for(int i=0; i<(int)sCuts.size(); i++) if(i!=ID-1) NET->getwc(k)->sCuts[i]=1;
519 
520  // ID can not be used to get the event, to get event use ID=1 (ex: for watplot)
521  NET->getwc(k)->sCuts = sCuts; // restore cCuts
522 
523  if(cfg->dump) EVT->dopen(outDump.Data(),const_cast<char*>("a"),false);
524  EVT->output2G(net_tree,NET,ID,k,ofactor); // get reconstructed parameters
525  if(cfg->dump) {
526  // add Qveto to dump file
527  fprintf(EVT->fP,"Qveto: ");
528  for(int i=0; i<2*nIFO; i++) fprintf(EVT->fP,"%f ",Qveto[i]);
529  fprintf(EVT->fP,"\n");
530  // add Lveto to dump file
531  fprintf(EVT->fP,"Lveto: ");
532  for(int i=0; i<3; i++) fprintf(EVT->fP,"%f ",Lveto[i]);
533  fprintf(EVT->fP,"\n");
534  }
535  if(cfg->dump) EVT->dclose();
536  if(!cfg->cedDump) NET->getwc(k)->sCuts[ID-1]=1; // mark as processed
537  }
538  }
539 
540  jfile->cd();
541  if(EVT) delete EVT;
542  }
543  return;
544 }
545 
546 void
547 GetQveto(wavearray<double>* wf, float &Qveto, float &Qfactor) {
548 
549  wavearray<double> x = *wf;;
550 
551  // resample data by a factor 4
552  int xsize=x.size();
553  x.FFTW(1);
554  x.resize(4*x.size());
555  x.rate(4*x.rate());
556  for(int j=xsize;j<x.size();j++) x[j]=0;
557  x.FFTW(-1);
558 
559  // extract max/min values and save the absolute values in the array 'a'
560  wavearray<double> a(x.size());
561  int size=0;
562  double dt = 1./x.rate();
563  double prev=x[0];
564  double xmax=0;
565  for (int i=1;i<x.size();i++) {
566  if(fabs(x[i])>xmax) xmax=fabs(x[i]);
567  if(prev*x[i]<0) {
568  a[size]=xmax;
569  size++;
570  xmax=0;
571  }
572  prev=x[i];
573  }
574 
575  // find max value/index ans save on amax/imax
576  int imax=-1;
577  double amax=0;
578  for (int i=1;i<size;i++) {
579  if(a[i]>amax) {amax=a[i];imax=i;}
580  }
581 
582 /*
583  cout << endl;
584  cout << "a[imax-2] " << a[imax-2] << endl;
585  cout << "a[imax-1] " << a[imax-1] << endl;
586  cout << "a[imax] " << a[imax] << endl;
587  cout << "a[imax+1] " << a[imax+1] << endl;
588  cout << "a[imax+2] " << a[imax+2] << endl;
589  cout << endl;
590 */
591 
592  // compute Qveto
593  double ein=0; // energy of max values inside NTHR
594  double eout=0; // energy of max values outside NTHR
595  for (int i=0;i<size;i++) {
596  if(abs(imax-i)<=NTHR) {
597  ein+=a[i]*a[i];
598  //cout << i << " ein " << a[i] << " " << amax << endl;
599  } else {
600  if(a[i]>amax/ATHR) eout+=a[i]*a[i];
601  //if(a[i]>amax/ATHR) cout << i << " eout " << a[i] << " " << amax << endl;
602  }
603  }
604  Qveto = ein>0 ? eout/ein : 0.;
605  //cout << "Qveto : " << Qveto << " ein : " << ein << " eout : " << eout << endl;
606 
607  // compute Qfactor
608  float R = (a[imax-1]+a[imax+1])/a[imax]/2.;
609  Qfactor = sqrt(-pow(TMath::Pi(),2)/log(R)/2.);
610  //cout << "Qfactor : " << Qfactor << endl;
611 
612  return;
613 }
614 
615 void
616 GetLveto(netcluster* pwc, int cid, int nifo, float* Lveto) {
617 //
618 // input
619 // pwc : pointer to netcluster object
620 // cid : cluster id
621 // nifo : number of detectors
622 // output
623 // Lveto[0] : line frequency
624 // Lveto[1] : line bandwitdh
625 // Lveto[2] : line enery ratio (line_energy / total_energy)
626 //
627 
628  Lveto[0] = Lveto[1] = Lveto[2] = 0;
629 
630  std::vector<int>* vint = &(pwc->cList[cid-1]); // pixel list
631  int V = vint->size(); // cluster size
632  if(!V) return;
633 
634  // ------------------------------------------------------------------
635  // Find max pixel parameters
636  // ------------------------------------------------------------------
637 
638  double likeMax=0; // maximum pixel's energy
639  double likeTot=0; // total cluster energy
640  double freqMax; // frequency of the pixel with max energy
641  double dfreqMax; // df of the pixel with max energy
642  for(int n=0; n<V; n++) {
643  netpixel* pix = pwc->getPixel(cid,n);
644  if(pix->layers%2==0) {
645  cout << "CWB_Plugin_QLveto.C - Error : is enabled only for WDM (2G)" << endl;
646  exit(1);
647  }
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  double freq = pix->frequency*pix->rate/2.;
657  double df = pix->rate/2.;
658 
659  likeTot+=likePix;
660  if(likePix>likeMax) {likeMax=likePix;freqMax=freq;dfreqMax=df;}
661  }
662  //cout << "likeMax : " << likeMax << " likeTot : " << likeTot
663  // << " freqMax : " << freqMax << " dfreqMax : " << dfreqMax << endl;
664 
665  // ------------------------------------------------------------------
666  // Compute Lveto parameters
667  // ------------------------------------------------------------------
668 
669  double fmean=0; // line mean frequency
670  double frms=0; // line bandwidth
671  double likeLin=0; // line energy
672  for(int n=0; n<V; n++) {
673  netpixel* pix = pwc->getPixel(cid,n);
674  if(!pix->core) continue; // select only the principal components pixels
675 
676  double likePix=0;
677  for(int m=0; m<nifo; m++) {
678  likePix += pow(pix->getdata('S',m),2); // like whitened reconstructed signal 00
679  likePix += pow(pix->getdata('P',m),2); // like whitened reconstructed signal 90
680  }
681 
682  // the estimation is done for all pixels
683  // with freq in the range [freqMax-dfreqMax, freqMax+dfreqMax]
684  double freq = pix->frequency*pix->rate/2.;
685  if(fabs(freq-freqMax)<=dfreqMax) {
686  likeLin += likePix;
687  fmean += likePix*freq;
688  frms += likePix*freq*freq;
689  }
690  }
691 
692  fmean = fmean/likeLin;
693  frms = frms/likeLin-fmean*fmean;
694  frms = frms>0 ? sqrt(frms) : 0.;
695 
696  if(frms<dfreqMax/2.) frms=dfreqMax/2.;
697 
698  // ------------------------------------------------------------------
699  // Save Lveto parameters
700  // ------------------------------------------------------------------
701 
702  Lveto[0] = fmean; // line mean frequency
703  Lveto[1] = frms; // line bandwidth
704  Lveto[2] = likeTot>0. ? likeLin/likeTot : 0.; // energy ratio energy inside_line/total
705 
706  // ------------------------------------------------------------------
707  // plot time-frequency energy
708  // ------------------------------------------------------------------
709 
710 #if defined PLOT_LIKELIHOOD
711  watplot WTS(const_cast<char*>("wts"));
712  WTS.plot(pwc, cid, nifo, 'L', 0, const_cast<char*>("COLZ"));
713  WTS.canvas->Print("l_tfmap_scalogram.png");
714 #endif
715 
716  return;
717 }
718 
719 void
720 PlotWaveform(TString ifo, wavearray<double>* wfREC,
721  CWB::config* cfg, bool fft, bool strain) {
722 
723  watplot PTS(const_cast<char*>("ptswrc"),200,20,800,500);
724 
725  //cout << "Print " << fname << endl;
726  double tmin = wfREC->start();
727  wfREC->start(wfREC->start()-tmin);
728  if(fft) {
729  PTS.plot(wfREC, const_cast<char*>("ALP"), 1, 0, 0, true, cfg->fLow, cfg->fHigh);
730  } else {
731  PTS.plot(wfREC, const_cast<char*>("ALP"), 1, 0, 0);
732  }
733  PTS.graph[0]->SetLineWidth(1);
734  wfREC->start(wfREC->start()+tmin);
735 
736  char label[64]="";
737  if(fft) sprintf(label,"%s","fft");
738  else sprintf(label,"%s","time");
739  if(strain) sprintf(label,"%s_%s",label,"strain");
740  else sprintf(label,"%s_%s",label,"white");
741 
742  char fname[1024];
743  //sprintf(fname, "%s_wf_%s_rec_gps_%d.root",ifo.Data(),label,int(tmin));
744  sprintf(fname, "%s_wf_%s_rec_gps_%d.png",ifo.Data(),label,int(tmin));
745  PTS.canvas->Print(fname);
746  cout << "write : " << fname << endl;
747  //PTS.canvas->Write(REPLACE(fname,dirCED,gtype));
748 }
749 
750 void
751 ClearWaveforms(detector* ifo) {
752 
753  int n;
754 
755  n = ifo->IWFP.size();
756  for (int i=0;i<n;i++) {
757  wavearray<double>* wf = (wavearray<double>*)ifo->IWFP[i];
758  delete wf;
759  }
760  ifo->IWFP.clear();
761  ifo->IWFID.clear();
762 
763  n = ifo->RWFP.size();
764  for (int i=0;i<n;i++) {
765  wavearray<double>* wf = (wavearray<double>*)ifo->RWFP[i];
766  delete wf;
767  }
768  ifo->RWFP.clear();
769  ifo->RWFID.clear();
770 }
771 
772 std::vector<netpixel> DoPCA(network* NET, CWB::config* cfg, int lag, int id) {
773 
774  double ee;
775 
776  size_t nIFO = NET->ifoList.size();
777 
778  float En = 2*NET->acor*NET->acor*nIFO; // network energy threshold in the sky loop
779 
780  int size = NET->a_00.size();
781  int f_ = NIFO/4;
782 
783  netpixel* pix;
784  netcluster* pwc = NET->getwc(lag);
785  std::vector<netpixel> vPIX;
786 
787  std::vector<int> pI = NET->wdmMRA.getXTalk(pwc, id, false); // buffer for pixel IDs
788  int V = pI.size();
789  if(V>cfg->BATCH) return vPIX; // attach TD amp to pixels < V
790 
791  wavearray<float> xi(size); xi=0; // PC 00 array
792  wavearray<float> XI(size); XI=0; // PC 90 array
793 
794  __m128* _xi = (__m128*) xi.data; // set pointer to PC 00 array
795  __m128* _XI = (__m128*) XI.data; // set pointer to PC 90 array
796 
797  __m128* _aa = (__m128*) NET->a_00.data; // set pointer to 00 array
798  __m128* _AA = (__m128*) NET->a_90.data; // set pointer to 90 array
799 
800  int nPC = 0;
801  for(int j=0; j<V; j++) {
802  int jf = j*f_; // source sse pointer increment
803  _sse_zero_ps(_xi+jf); // zero MRA amplitudes
804  _sse_zero_ps(_XI+jf); // zero MRA amplitudes
805  ee = _sse_abs_ps(_aa+jf,_AA+jf); // total pixel energy / quadrature
806  if(ee>En) nPC++; else ee=0.; // count core pixels
807  NET->rNRG.data[j] = ee; // init residual energy array
808  NET->pNRG.data[j] = NET->rNRG.data[j]; // init residual energy array
809  }
810 
811  nPC = NET->_sse_mra_ps(xi.data,XI.data,En,nPC); // get principal components
812 
813  for(int j=0; j<V; j++) { // loop over principal components
814  pix = pwc->getPixel(id,pI[j]);
815  vPIX.push_back(*pix); // save original pixels
816  pix->core = false;
817  ee = NET->pNRG.data[j]; // total pixel energy
818  if(ee<En) continue;
819  pix->core = true;
820  for(int i=0; i<nIFO; i++) {
821  pix->setdata(double(xi.data[j*NIFO+i]),'S',i); // store 00 whitened response PC
822  pix->setdata(double(XI.data[j*NIFO+i]),'P',i); // store 90 whitened response PC
823  }
824  }
825 
826  return vPIX;
827 }
828 
829 void ResetPCA(network* NET, CWB::config* cfg, netcluster* pwc, std::vector<netpixel>* vPIX, int ID) {
830 
831  std::vector<int> pI = NET->wdmMRA.getXTalk(pwc, ID, false); // buffer for pixel IDs
832  int V = pI.size();
833  if(V>cfg->BATCH) return; // attach TD amp to pixels < V
834  for(int j=0; j<V; j++) {
835  netpixel* pix = pwc->getPixel(ID,pI[j]);
836  *pix = (*vPIX)[j];
837  }
838 
839  while(!vPIX->empty()) vPIX->pop_back();
840  vPIX->clear(); std::vector<netpixel>().swap(*vPIX);
841 }
842 
843 double
844 GetQfactor(wavearray<double>* wf, double frequency, bool fixAmax) {
845 //
846 // wf -> input waveform
847 // frequency -> input peak frequency
848 // fixAmax -> true/false : fix/not-fix amplitude of the gaussian fit
849 //
850 // -> the input waveform wf is resampled 4x
851 // -> get mean and sigma from a gaussian fit of the waveform envelope
852 // -> compute the range [xmin,xmax] = [mean-3*sigma,mean-3*sigma]]
853 // -> compute area of the envelope in the rage [xmin,xmax]
854 // -> compute qfactor using area, max_amplitude and frequency
855 // -> return qfactor
856 //
857 
858  wavearray<double> x = *wf;;
859 
860  // resample data by a factor 4
861  int xsize=x.size();
862 
863  x.FFTW(1);
864  x.resize(4*x.size());
865  x.rate(4*x.rate());
866  for(int j=xsize;j<x.size();j++) x[j]=0;
867  x.FFTW(-1);
868 
869  // compute range [xmin,xmax] used for final fit using the square of x
870  double gmean, gsigma;
871  GetGaussFitPars2(&x, gmean, gsigma, fixAmax);
872 // cout << "GetQfactor -> gmean " << gmean << " gsigma " << gsigma << endl;
873 
874  double xmin = gmean-3.0*gsigma;
875  double xmax = gmean+3.0*gsigma;
876 // cout << "GetQfactor -> xmin " << xmin << " xmax " << xmax << endl;
877 
878  x = CWB::mdc::GetEnvelope(&x); // get envelope
879 
880  // extract max/min values and save the absolute values in the array 'a'
881  double dt = 1./x.rate();
882  // find max value/index and save on amax/imax
883  int imax=-1;
884  double amax=0;
885  for (int i=1;i<x.size();i++) {
886  if(x[i]>amax) {amax=x[i];imax=i;}
887  }
888 
889  double area=0.;
890  for (int i=1;i<x.size();i++) {
891  double t=i*dt;
892  if(t>xmin && t<xmax) area+=x[i]*dt;
893  }
894 
895  double sigma = area/amax/sqrt(2*TMath::Pi());
896 // cout << "sigma integral is " << sigma << endl;
897  double qfactor = sigma*(TMath::TwoPi()*frequency); // compute qfactor
898 
899  return qfactor;
900 }
901 
902 double
903 GetPeakFrequency(wavearray<double>* wf) {
904 //
905 // wf -> input waveform
906 //
907 // -> multiply the input waveform wf by its envelope
908 // -> perform FFT transform
909 // -> compute max frequency -> peak
910 // -> return frequency peak
911 //
912 
913  wavearray<double> x = *wf;
914  wavearray<double> ex = CWB::mdc::GetEnvelope(&x); // get envelope
915  int N = x.size();
916  for(int i=0;i<N;i++) x[i]*=ex[i];
917 
918  x.FFTW(1);
919 
920  // fill time/amp graph
921  double df=(x.rate()/(double)N)/2.;
922  double amax=0.;
923  double fmax=0.;
924  for(int i=0;i<N;i+=2) {
925  double freq=i*df;
926  double amp=x[i]*x[i]+x[i+1]*x[i+1];
927  if(amp>amax) {fmax=freq;amax=amp;}
928  }
929 
930  return fmax;
931 }
932 
933 void
934 GetGaussFitPars(wavearray<double>* wf, double& mean, double& sigma, bool doenv) {
935 //
936 // wf -> input waveform
937 // doenv -> true/false do/not-do waveform envelope
938 //
939 // mean, sigma -> output gaussian fit parameters
940 //
941 // -> get evelope from the input waveform wf and perform fit with gaussian function
942 // -> return mean and gigma of gaussian fit
943 //
944 
945  wavearray<double> x = doenv ? CWB::mdc::GetEnvelope(wf) : *wf; // get envelope
946 
947  // fill time/amp graph
948  int N = x.size();
949  double* time = new double[N];
950  double* amp = new double[N];
951  double dt = 1./x.rate();
952  for(int i=0;i<N;i++) {
953  time[i]=i*dt;
954  amp[i]=x[i];
955  }
956  TGraph gr(N,time,amp);
957 
958  // fit signal envelope with gaussian function
959  gr.Fit("gaus","q0");
960  TF1* gaus = (TF1*)gr.GetFunction("gaus");
961 
962  mean = gaus->GetParameter("Mean"); // extract sigma from gauss fit
963  sigma = gaus->GetParameter("Sigma"); // extract sigma from gauss fit
964 
965  delete [] time;
966  delete [] amp;
967 
968  return;
969 }
970 
971 void
972 GetGaussFitPars2(wavearray<double>* wf, double& mean, double& sigma, bool fixAmax) {
973 //
974 // wf -> input waveform
975 // fixAmax -> true/false : fix/not-fix amplitude of the gaussian fit
976 //
977 // mean, sigma -> output gaussian fit parameters
978 //
979 //
980 // -> get the square of the evelope from the input waveform wf
981 // -> perform fit with gaussian function and get mean and gigma of gaussian fit
982 // -> compute the range [xmin,xmax] = [mean-3*sigma,mean-3*sigma]]
983 //
984 // -> get the evelope from the input waveform wf
985 // -> perform fit with gaussian function in the range [xmin,xmax] fixing the amplitude to max envelope (according to fixAmax value)
986 // -> return mean and gigma of gaussian fit
987 //
988 
989  wavearray<double> x = CWB::mdc::GetEnvelope(wf); // get envelope
990 
991  // compute range [xmin,xmax] used for final fit using the square of x
992  // x*x highlight the main peak
993  wavearray<double> x2 = x; // getx*x
994  for(int i=0;i<x.size();i++) x2[i]=x[i]*x[i];
995 
996  double gmean, gsigma;
997  GetGaussFitPars(&x2, gmean, gsigma, false);
998 // cout << "GetGaussFitPars2 -> gmean x2 " << gmean << " gsigma x2 " << gsigma << endl;
999 
1000  double xmin = gmean-3.0*gsigma;
1001  double xmax = gmean+3.0*gsigma;
1002 // cout << "GetGaussFitPars2 -> xmin " << xmin << " xmax " << xmax << endl;
1003 
1004  // fill time/amp graph
1005  int N = x.size();
1006  double* time = new double[N];
1007  double* amp = new double[N];
1008  double dt = 1./x.rate();
1009  double amax=0;
1010  for(int i=0;i<N;i++) {
1011  time[i]=i*dt;
1012  amp[i]=x[i];
1013  if(fabs(x[i])>amax) amax=fabs(x[i]);
1014  }
1015  TGraph gr(N,time,amp);
1016 // cout << "GetGaussFitPars2 -> amax " << amax << endl;
1017 
1018  // fit signal envelope with gaussian function
1019  TF1 *xgaus = new TF1("xgaus","gaus",xmin,xmax);
1020  if(fixAmax) xgaus->SetParLimits(0,amax*0.999,amax*1.001); // fix gauss constant par to amax
1021  gr.Fit("xgaus","RQ");
1022 
1023  mean = xgaus->GetParameter("Mean"); // extract sigma from gauss fit
1024  sigma = xgaus->GetParameter("Sigma"); // extract sigma from gauss fit
1025 
1026  delete xgaus;
1027  delete [] time;
1028  delete [] amp;
1029 
1030  return;
1031 }
1032 
1033 }
1034 
1035 // -------------------------------------------------------------------------
1036 // --> END CWB_USER PLUGIN CODE 2
1037 //
1038 // CWB_Plugin_QLveto.C
1039 // -------------------------------------------------------------------------
1040 
1041 
1042 // -------------------------------------------------------------------------
1043 // --> MAIN CWB_USER PLUGIN CODE
1044 //
1045 // INPUT PLUGINS :
1046 //
1047 // 1 - CWB_Plugin_Gating.C
1048 // 2 - CWB_Plugin_QLveto.C
1049 //
1050 // -------------------------------------------------------------------------
1051 
1052 #define XIFO 4
1053 #pragma GCC system_header
1054 #include "cwb.hh"
1055 
1056 
1057 void
1058 CWB_Plugin(TFile* jfile, CWB::config* cfg, network* net, WSeries<double>* x, TString ifo, int type) {
1059 
1060  CWB_UserPluginNamespace_1::CWB_Plugin(jfile, cfg, net, x, ifo, type); // CALL USER PLUGIN CODE 1
1061  CWB_UserPluginNamespace_2::CWB_Plugin(jfile, cfg, net, x, ifo, type); // CALL USER PLUGIN CODE 2
1062 }
TGraph * gr
Definition: Make_PP_IFAR.C:167
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 t
Definition: cwb_clchunk.csh:85
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)
double GetQfactor(wavearray< double > *wf, double frequency, bool fixAmax)
void ResetPCA(network *NET, CWB::config *cfg, netcluster *pwc, std::vector< netpixel > *vPIX, int ID)
double GetPeakFrequency(wavearray< double > *wf)
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 GetGaussFitPars(wavearray< double > *wf, double &mean, double &sigma, bool doenv=true)
void GetGaussFitPars2(wavearray< double > *wf, double &mean, double &sigma, bool fixAmax)
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