Logo coherent WaveBurst  
Config Reference Guide
Logo
CWB_Plugin_UniqueLags_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_UniqueLags.C
8 // 2 - CWB_Plugin_Gating.C
9 // 3 - CWB_Plugin_QLveto.C
10 //
11 // -------------------------------------------------------------------------
12 
13 
14 // -------------------------------------------------------------------------
15 // --> BEGIN CWB_USER PLUGIN CODE 1
16 //
17 // CWB_Plugin_UniqueLags.C
18 // -------------------------------------------------------------------------
19 
20 namespace CWB_UserPluginNamespace_1 {
21 
22 /*
23 # Copyright (C) 2019 Sergey Klimenko
24 #
25 # This program is free software: you can redistribute it and/or modify
26 # it under the terms of the GNU General Public License as published by
27 # the Free Software Foundation, either version 3 of the License, or
28 # (at your option) any later version.
29 #
30 # This program is distributed in the hope that it will be useful,
31 # but WITHOUT ANY WARRANTY; without even the implied warranty of
32 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 # GNU General Public License for more details.
34 #
35 # You should have received a copy of the GNU General Public License
36 # along with this program. If not, see <https://www.gnu.org/licenses/>.
37 */
38 
39 
40 }
41 #define XIFO 4
42 
43 #pragma GCC system_header
44 
45 #include "cwb.hh"
46 #include "cwb2G.hh"
47 #include "config.hh"
48 #include "network.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 // ---------------------------------------------------------------------------------
58 // HOW TO SETUP PLUGIN IN CWB USER CONFIGURATION (EXAMPLE)
59 // WORNING: This plugin works only with 3 detectors !!!
60 // ---------------------------------------------------------------------------------
61 
62 /*
63  TString opt_ulags = ""; // NOTE : add space at the end of each line
64  opt_ulags += "ulags_enabled=true "; // true/false enable/disable unique lags (default=false)
65  opt_ulags += "ulags_lagSize=599 "; // if>0 force lagSize to be fixed
66  strcpy(parPlugin,opt_ulags.Data()); // set plugin parameters
67  lagSize = 0; // mandatory, used to manage command 'cwb_inet #job 0 ced 0 #lag'
68  lagStep = 1.;
69  lagOff = 0;
70 */
71 
72 // ---------------------------------------------------------------------------------
73 // USER UniqueLags PLUGIN OPTIONS
74 // ---------------------------------------------------------------------------------
75 
76 struct uoptions {
77  bool enabled; // true/false enable/disable unique lags (default=false)
78  int lagSize; // if>0 force lagSize to be fixed
79 };
80 
81 uoptions gOPT; // global User Options
82 bool gULAG; // used to manage command 'cwb_inet #job 0 ced 0 #lag'
83 
84 void ResetUserOptions(network* net, CWB::config* cfg);
85 void ReadUserOptions(TString options);
86 void PrintUserOptions(CWB::config* cfg);
87 
88 void
89 CWB_Plugin(TFile* jfile, CWB::config* cfg, network* net, WSeries<double>* x, TString ifo, int type) {
90 //!CONFIG
91 // Plugin to setup dinamically the unique lags for 2/3/4 ifos network
92 
93  if(type==CWB_PLUGIN_CONFIG) {
94  gULAG = (cfg->lagSize) ? true : false; // single lag is select by the command 'cwb_inet #job 0 ced 0 #lag'
95  }
96 
97  if(type==CWB_PLUGIN_NETWORK) {
98  ResetUserOptions(net, cfg); // set default config options
99  ReadUserOptions(cfg->parPlugin); // user config options : read from parPlugin
100  PrintUserOptions(cfg); // print config options
101  }
102 
103  if(type==CWB_PLUGIN_INIT_JOB) {
104 
105  if(!gOPT.enabled) return;
106 
107  cout << endl;
108  cout << "-----> CWB_Plugin_UniqueLags.C ";
109  cout << "type " << type << endl;
110  cout << endl;
111 
112  // get ifo id
113  int id=-1;
114  for(int n=0;n<cfg->nIFO;n++) {
115  if(ifo==net->ifoName[n]) {id=n; break;}
116  }
117  if(id>0) return; // plugin is execute only for the first ifo
118 
119  if(cfg->simulation) {cout << "CWB_Plugin_UniqueLags.C : Error - works only for background" << endl; gSystem->Exit(1);}
120  if(cfg->nIFO<2 || cfg->nIFO>4) {cout << "CWB_Plugin_UniqueLags.C : Error - implemented only for 2/3/4 ifos network" << endl; gSystem->Exit(1);}
121 
122  cwb2G* gCWB2G; IMPORT(cwb2G*,gCWB2G)
123  // get lagBuffer (used to store the contents of the user lagFile)
124  TArrayC* lagBuffer = &(gCWB2G->lagBuffer);
125  // get data range
126  double segLen = gCWB2G->GetSegEnd()-gCWB2G->GetSegBegin();
127  // get laxMax & lagSize
128  int lagMax = int(segLen/cfg->lagStep);
129  if(cfg->nIFO==2) {
130  lagMax = lagMax-lagMax%2; // force to be an even number
131  }
132  if(cfg->nIFO==3) {
133  lagMax = lagMax-lagMax%3;
134  lagMax = lagMax-lagMax%2; // force to be an even number
135  }
136  int lagSize = lagMax-1;
137 
138  if(gOPT.lagSize>0) { // if>0 force lagSize to be fixed
139  lagSize = gOPT.lagSize;
140  lagMax = lagSize+1;
141  }
142 
143  cout << endl << "CWB_Plugin_UniqueLags.C : segLen -> " << segLen << " lagMax " << lagMax << " lagSize " << lagSize << endl << endl;
144 
145  // redirect cout to buffer and return contents
146  std::stringstream buffer;
147  std::streambuf * old = std::cout.rdbuf(buffer.rdbuf());
148 
149  // generate unique lags
150  if(cfg->nIFO==2) {
151  cout << 0 << "\t" << 0 << "\t" << 0 << endl;
152  std::vector<int> v[1];
153  for(int i=1;i<lagSize;i++) {
154  int lag0 = 0;
155  int lag1 = i;
156  cout << i << "\t" << lag0 << "\t" << lag1 << endl;
157  v[0].push_back(lag1-lag0);
158  }
159  for(int k=0;k<1;k++) {
160  sort(v[k].begin(), v[k].end());
161  auto it = std::unique(v[k].begin(), v[k].end());
162  if(!(it == v[k].end())) {cout << "CWB_Plugin_UniqueLags.C : Error - Duplicate Lags" << endl; gSystem->Exit(1);}
163  }
164  }
165  if(cfg->nIFO==3) {
166  cout << 0 << "\t" << 0 << "\t" << 0 << "\t" << 0 << endl;
167  std::vector<int> v[3];
168  for(int i=1;i<lagSize;i++) {
169  int lag0 = 0;
170  int lag1 = i;
171  int lag2 = (i*2)%lagSize;
172  cout << i << "\t" << lag0 << "\t" << lag1 << "\t" << lag2 << endl;
173  v[0].push_back(lag1-lag0);
174  v[1].push_back(lag2-lag0); v[2].push_back(lag2-lag1);
175  }
176  for(int k=0;k<3;k++) {
177  sort(v[k].begin(), v[k].end());
178  auto it = std::unique(v[k].begin(), v[k].end());
179  if(!(it == v[k].end())) {cout << "CWB_Plugin_UniqueLags.C : Error - Duplicate Lags" << endl; gSystem->Exit(1);}
180  }
181  }
182  if(cfg->nIFO==4) {
183  cout << 0 << "\t" << 0 << "\t" << 0 << "\t" << 0 << "\t" << 0 << endl;
184  std::vector<int> v[6];
185  for(int i=1;i<lagSize;i++) {
186  int lag0 = 0;
187  int lag1 = i;
188  int lag2 = (i*2)%lagSize;
189  int lag3 = (i*3)%lagSize;
190  cout << i << "\t" << lag0 << "\t" << lag1 << "\t" << lag2 << "\t" << lag3 << endl;
191  v[0].push_back(lag1-lag0);
192  v[1].push_back(lag2-lag0); v[2].push_back(lag2-lag1);
193  v[3].push_back(lag3-lag0); v[4].push_back(lag3-lag1); v[5].push_back(lag3-lag2);
194  }
195  for(int k=0;k<6;k++) {
196  sort(v[k].begin(), v[k].end());
197  auto it = std::unique(v[k].begin(), v[k].end());
198  if(!(it == v[k].end())) {cout << "CWB_Plugin_UniqueLags.C : Error - Duplicate Lags" << endl; gSystem->Exit(1);}
199  }
200  }
201 
202  // restore cout
203  std::cout.rdbuf(old);
204 
205  // set lagBuffer
206  lagBuffer->Set(buffer.str().size(),buffer.str().c_str());
207  // set end of string
208  lagBuffer->Set(lagBuffer->GetSize()+1);
209  (*lagBuffer)[lagBuffer->GetSize()-1]=0;
210  gCWB2G->lagMode[0]='s';gCWB2G->lagMode[1]=0; // change lagMode : setTimeShifts read lags from string
211  if(!gULAG) cfg->lagSize=lagSize;
212  cfg->lagMax=lagMax;
213  //cout << gCWB2G->lagBuffer.GetArray() << endl;
214  }
215 
216  return;
217 }
218 
219 void ReadUserOptions(TString options) {
220 
221  if(options.CompareTo("")!=0) {
222  cout << options << endl;
223  if(!options.Contains("--")) { // parameters are used only by cwb_inet
224 
225  TObjArray* token = TString(options).Tokenize(TString(' '));
226  for(int j=0;j<token->GetEntries();j++){
227 
228  TObjString* tok = (TObjString*)token->At(j);
229  TString stok = tok->GetString();
230 
231  if(stok.Contains("ulags_enabled=")) {
232  TString ulags_enabled=stok;
233  ulags_enabled.Remove(0,ulags_enabled.Last('=')+1);
234  if(ulags_enabled=="true") gOPT.enabled=true;
235  if(ulags_enabled=="false") gOPT.enabled=false;
236  }
237 
238  if(stok.Contains("ulags_lagSize=")) {
239  TString ulags_lagSize=stok;
240  ulags_lagSize.Remove(0,ulags_lagSize.Last('=')+1);
241  if(ulags_lagSize.IsDigit()) gOPT.lagSize=ulags_lagSize.Atoi();
242  if((gOPT.lagSize%2)==0) {
243  cout << "ReadUserOptions Error : ulags_lagSize = " << gOPT.lagSize << " must be odd" << endl;exit(1);
244  }
245  }
246  }
247  }
248  }
249 }
250 
251 void PrintUserOptions(CWB::config* cfg) {
252 
253  cout << "-----------------------------------------" << endl;
254  cout << "UniqueLags config options " << endl;
255  cout << "-----------------------------------------" << endl << endl;
256  if(gOPT.enabled==false) cout << "ULAGS_ENABLED = false" << endl;
257  if(gOPT.enabled==true) cout << "ULAGS_ENABLED = true " << endl;
258  cout << "ULAGS_LAG_SIZE = " << gOPT.lagSize << endl;
259  cout << endl;
260 }
261 
262 void ResetUserOptions(network* net, CWB::config* cfg) {
263  gOPT.enabled=false;
264  gOPT.lagSize=0;
265 }
266 
267 }
268 
269 // -------------------------------------------------------------------------
270 // --> END CWB_USER PLUGIN CODE 1
271 //
272 // CWB_Plugin_UniqueLags.C
273 // -------------------------------------------------------------------------
274 
275 
276 // -------------------------------------------------------------------------
277 // --> BEGIN CWB_USER PLUGIN CODE 2
278 //
279 // CWB_Plugin_Gating.C
280 // -------------------------------------------------------------------------
281 
282 namespace CWB_UserPluginNamespace_2 {
283 
284 /*
285 # Copyright (C) 2019 Gabriele Vedovato
286 #
287 # This program is free software: you can redistribute it and/or modify
288 # it under the terms of the GNU General Public License as published by
289 # the Free Software Foundation, either version 3 of the License, or
290 # (at your option) any later version.
291 #
292 # This program is distributed in the hope that it will be useful,
293 # but WITHOUT ANY WARRANTY; without even the implied warranty of
294 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
295 # GNU General Public License for more details.
296 #
297 # You should have received a copy of the GNU General Public License
298 # along with this program. If not, see <https://www.gnu.org/licenses/>.
299 */
300 
301 
302 }
303 #define XIFO 4
304 
305 #pragma GCC system_header
306 
307 #include "cwb.hh"
308 #include "cwb2G.hh"
309 #include "config.hh"
310 #include "network.hh"
311 #include "wavearray.hh"
312 #include "TString.h"
313 #include "TObjArray.h"
314 #include "TObjString.h"
315 #include "TRandom.h"
316 #include "Toolbox.hh"
317 
318 namespace CWB_UserPluginNamespace_2 {
319 
320 //!DATA_CONDITIONING
321 
322 // This plugin implements the time gating in the pixel's selection stage.
323 // Gating is a veto of the pixels belonging to a time interval.
324 // This plugin is used to exclude from the analysis the pixels
325 // in a time interval where there is a huge glitch.
326 // Huge glitches are harmful because they affect the correct estimation
327 // of the selection threshold and could mask the nearby events at lower SNR.
328 // Warning : events with high SNR can be rejected by this procedure (see SETHR)
329 
330 }
331 #define SETHR 1000000 // Is the threshold which define the time slices to be cutted
332 namespace CWB_UserPluginNamespace_2 {
333 
334  // Warning : this value depends on the frequency interval [fHigh:fLow]
335 
336 }
337 #define TWIN 0.5 // Is the window (sec) used to integrate the energies in time
338 namespace CWB_UserPluginNamespace_2 {
339 
340  // TWIN must be a multiple of the greatest time resolution used in the analysis
341 
342 }
343 #define TEDG 1.5 // Is the time window (sec) to be cutted when the time integrated energy is > SETHR
344 
345 namespace CWB_UserPluginNamespace_2 {
346 
347 void
348 CWB_Plugin(TFile* jfile, CWB::config* cfg, network* net, WSeries<double>* x, TString ifo, int type) {
349 
350 // this plugin is called in cwb2G.cc to the whitened data at the beginning of the COHERENT stage
351 
352 // cout << endl;
353 // cout << "-----> CWB_Plugin_Gating.C" << endl;
354 // cout << "ifo " << ifo.Data() << endl;
355 // cout << "type " << type << endl;
356 // cout << endl;
357 
358  if(type==CWB_PLUGIN_CONFIG) {
359  cout << endl;
360  cout << "-----> CWB_Plugin_Gating.C" << endl;
361  cout << endl;
362  }
363 
364  if(type==CWB_PLUGIN_ICOHERENCE) {
365 
366  // get data range
367  cwb2G* gCWB2G; IMPORT(cwb2G*,gCWB2G)
368  double Tb = gCWB2G->GetSegBegin()-cfg->segEdge;
369  double Te = gCWB2G->GetSegEnd()+cfg->segEdge;
370 
371 
372  //cout << "CWB_Plugin_Gating.C - " << "Tb : " << int(Tb) << " Te : " << int(Te) << endl;
373 
374  int nIFO = (gCWB2G->IsSingleDetector()) ? 1 : net->ifoListSize(); // number of detectors
375  int size = net->getifo(0)->getHoT()->size(); // number of time bins
376  double rate = net->getifo(0)->getHoT()->rate(); // rate
377  double dt = 1./rate; // time bin resolution (sec)
378 
379  //cout << "-----> CWB_Plugin_Gating.C - size : " << size << "\t rate : " << rate << "\t dt : " << dt << " length(sec) " << size*dt << endl;
380 
381  wavearray<double>* hot[NIFO_MAX];
382  for(int n=0; n<nIFO; n++) hot[n] = net->getifo(n)->getHoT(); // whitened data
383 
384  // A new array 'SE' is obtained from the arrays 'hot whitened data'
385  // It contains the time integrated energies over the time window TWIN
386  // A time sample is marked as to be vetoed when the energy SE>SETHR
387  // When a time sample 'i' is vetoed also the nearby M time size are vetoed [i-M:i+M]
388  // where M is the number of samples contained in TEDG sec
389 
390  int X = int(cfg->segEdge/dt+0.5); // samples in segEdge : this the is scratch time
391  int M = int(TEDG/dt)+1; // samples in TEDG sec
392  int N = int(TWIN/dt); // number of time samples in TWIN
393  if(N<1) N=1;
394  vector<waveSegment> glist; // gating segment list
395  wavearray<double> SE(size);
396  wavearray<int> sVeto(size); // array which contains samples to be vetoed
397 
398  int gsize=0;
399  for(int n=0; n<nIFO; n++) { // loop over detectors
400  SE=0;
401 // for(int i=N-1;i<size;i++) for(int j=0;j<N;j++) SE[i]+=pow(hot[n]->data[i-j],2);
402 
403  for(int j=0;j<N;j++) SE[N-1]+=pow(hot[n]->data[N-1-j],2);
404  for(int i=N;i<size;i++) {
405  SE[i] = SE[i-1];
406  SE[i] -= pow(hot[n]->data[i-N],2);
407  SE[i] += pow(hot[n]->data[i],2);
408  }
409 
410  sVeto=0;
411  for(int i=0;i<size;i++) {
412  if(SE[i]>SETHR) {
413  int is = i-M>X ? i-M : X;
414  int es = i+M<(size-X) ? i+M : size-X;
415  for(int k=is;k<es;k++) sVeto[k]=1;
416  }
417  }
418  // array derivative -> gating_start=1, gating_stop=-1
419  sVeto[0]=0;sVeto[size-1]=0;
420  for(int i=0;i<size-1;i++) sVeto[i]=sVeto[i+1]-sVeto[i];
421 
422  // build the gating segment list
423  waveSegment gseg; // gating segment
424  gseg.index = 0;
425  for(int i=0;i<size;i++) {
426  if(sVeto[i]== 1) gseg.start = Tb+int(i*dt); // round to nearest lower integer
427  if(sVeto[i]==-1) {
428  gseg.stop = Tb+int(i*dt+0.5); // round to nearest higher integer
429  gseg.index++;
430  glist.push_back(gseg);
431  }
432  }
433 
434  if(glist.size()>0) {
435  cout << endl;
436  cout.precision(10);
437  for(int j=gsize;j<glist.size();j++) {
438  cout << j << " -----> CWB_Plugin_Gating.C - " << net->getifo(n)->Name << " gating time : start="
439  << glist[j].start << " stop=" << glist[j].stop << " len=" << glist[j].stop-glist[j].start << endl;
440  }
441  cout << endl;
442  gsize=glist.size();
443  }
444  }
445 
446  double gating_time = 0; // total gating time
447  if(glist.size()) {
448  glist = CWB::Toolbox::unionSegments(glist); // Join & sort a waveSegment list
449  gating_time = CWB::Toolbox::getTimeSegList(glist); // get total gating time
450  glist = CWB::Toolbox::invertSegments(glist); // Invert waveSegment list
451  net->segList = CWB::Toolbox::mergeSegLists(glist,net->segList); // merge gating veto list to the net->segList
452  }
453 
454  cout<< "-----> CWB_Plugin_Gating.C - gating time : " << gating_time << endl << endl;
455 
456  // add infos to history
457  char info[256];
458  sprintf(info,"-GT:%g",gating_time);
459  gCWB2G->PrintAnalysisInfo(CWB_STAGE_COHERENCE,"cwb2G::Coherence",info,false);
460  }
461 
462  return;
463 }
464 }
465 
466 // -------------------------------------------------------------------------
467 // --> END CWB_USER PLUGIN CODE 2
468 //
469 // CWB_Plugin_Gating.C
470 // -------------------------------------------------------------------------
471 
472 
473 // -------------------------------------------------------------------------
474 // --> BEGIN CWB_USER PLUGIN CODE 3
475 //
476 // CWB_Plugin_QLveto.C
477 // -------------------------------------------------------------------------
478 
479 namespace CWB_UserPluginNamespace_3 {
480 
481 /*
482 # Copyright (C) 2019 Gabriele Vedovato
483 #
484 # This program is free software: you can redistribute it and/or modify
485 # it under the terms of the GNU General Public License as published by
486 # the Free Software Foundation, either version 3 of the License, or
487 # (at your option) any later version.
488 #
489 # This program is distributed in the hope that it will be useful,
490 # but WITHOUT ANY WARRANTY; without even the implied warranty of
491 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
492 # GNU General Public License for more details.
493 #
494 # You should have received a copy of the GNU General Public License
495 # along with this program. If not, see <https://www.gnu.org/licenses/>.
496 */
497 
498 
499 }
500 #define XIFO 4
501 
502 #pragma GCC system_header
503 
504 #include "cwb.hh"
505 #include "config.hh"
506 #include "network.hh"
507 #include "wavearray.hh"
508 #include "TString.h"
509 #include "TObjArray.h"
510 #include "TObjString.h"
511 #include "TRandom.h"
512 #include "TComplex.h"
513 #include "TMath.h"
514 #include "mdc.hh"
515 #include "watplot.hh"
516 #include "gwavearray.hh"
517 #include <vector>
518 
519 namespace CWB_UserPluginNamespace_3 {
520 
521 //#define PLOT_LIKELIHOOD
522 //#define PLOT_WHITENED_WAVEFORMS
523 
524 }
525 #define NTHR 1
526 #define ATHR 7.58859
527 
528 namespace CWB_UserPluginNamespace_3 {
529 
530 double GetQfactor(wavearray<double>* wf, double frequency, bool fixAmax);
531 double GetPeakFrequency(wavearray<double>* wf);
532 void GetGaussFitPars(wavearray<double>* wf, double& mean, double& sigma, bool doenv=true);
533 void GetGaussFitPars2(wavearray<double>* wf, double& mean, double& sigma, bool fixAmax);
534 
535 void GetQveto(wavearray<double>* wf, float &Qveto, float &Qfactor);
536 void GetLveto(netcluster* pwc, int cid, int nifo, float* Lveto);
537 void PlotWaveform(TString ifo, wavearray<double>* wfREC,
538  CWB::config* cfg, bool fft=false, bool strain=false);
539 void ClearWaveforms(detector* ifo);
540 
541 std::vector<netpixel> DoPCA(network* NET, CWB::config* cfg, int lag, int id);
542 void ResetPCA(network* NET, CWB::config* cfg, netcluster* pwc, std::vector<netpixel>* vPIX, int ID);
543 
544 
545 void
546 CWB_Plugin(TFile* jfile, CWB::config* cfg, network* NET, WSeries<double>* x, TString ifo, int type) {
547 //!MISCELLANEA
548 // Extract whitened reconstructed waveforms, and compute the Qveto, Lveto parameters
549 
550 // cout << endl;
551 // cout << "-----> CWB_Plugin_QLveto.C" << endl;
552 // cout << "ifo " << ifo.Data() << endl;
553 // cout << "type " << type << endl;
554 // cout << endl;
555 
556  float Qveto[4]; // Qveto
557  float Lveto[3]; // Lveto
558 
559  if(type==CWB_PLUGIN_CONFIG) {
560  cout << endl;
561  cout << "-----> CWB_Plugin_QLveto.C" << endl;
562  cout << endl;
563  cfg->outPlugin=true; // disable built-in output root file
564  }
565 
566  if(type==CWB_PLUGIN_ILIKELIHOOD) {
567  NET->wfsave=true; // enable waveform save
568 
569  // search output root file in the system list
570  TFile* froot = NULL;
571  TList *files = (TList*)gROOT->GetListOfFiles();
572  TString outDump="";
573  netevent* EVT;
574  int nIFO = NET->ifoListSize(); // number of detectors
575  if (files) {
576  TIter next(files);
577  TSystemFile *file;
578  TString fname;
579  bool check=false;
580  while ((file=(TSystemFile*)next())) {
581  fname = file->GetName();
582  // set output root file as the current file
583  if(fname.Contains("wave_")) {
584  froot=(TFile*)file;froot->cd();
585  outDump=fname;
586  outDump.ReplaceAll(".root.tmp",".txt");
587  //cout << "output file name : " << fname << endl;
588  }
589  }
590  if(!froot) {
591  cout << "CWB_Plugin_QLveto.C : Error - output root file not found" << endl;
592  gSystem->Exit(1);
593  }
594  } else {
595  cout << "CWB_Plugin_QLveto.C : Error - output root file not found" << endl;
596  gSystem->Exit(1);
597  }
598 
599  TTree* net_tree = (TTree *) froot->Get("waveburst");
600  if(net_tree==NULL) {
601  EVT = new netevent(nIFO);
602  net_tree = EVT->setTree();
603  net_tree->Branch("Qveto",Qveto,TString::Format("Qveto[%i]/F",4));
604  net_tree->Branch("Lveto",Lveto,TString::Format("Lveto[%i]/F",3));
605  } else {
606  TBranch* branch;
607  bool qveto_exists=false;
608  bool lveto_exists=false;
609  TIter next(net_tree->GetListOfBranches());
610  while ((branch=(TBranch*)next())) {
611  if(TString("Qveto").CompareTo(branch->GetName())==0) qveto_exists=true;
612  if(TString("Lveto").CompareTo(branch->GetName())==0) lveto_exists=true;
613  }
614  next.Reset();
615  if(!qveto_exists) net_tree->Branch("Qveto",Qveto,TString::Format("Qveto[%i]/F",4));
616  if(!lveto_exists) net_tree->Branch("Lveto",Lveto,TString::Format("Lveto[%i]/F",3));
617  }
618  }
619 
620  if(type==CWB_PLUGIN_OLIKELIHOOD) {
621 
622  if(TString(cfg->analysis)!="2G") {
623  cout << "CWB_Plugin_QLveto.C -> "
624  << "CWB_PLUGIN_OLIKELIHOOD implemented only for 2G" << endl;
625  gSystem->Exit(1);
626  }
627 
628  // import ifactor
629  int gIFACTOR=-1; IMPORT(int,gIFACTOR)
630  //cout << "-----> CWB_Plugin_QLveto.C -> "
631  // << " gIFACTOR : " << gIFACTOR << endl;
632 
633  // import slagShift
634  float* gSLAGSHIFT=NULL; IMPORT(float*,gSLAGSHIFT)
635 
636  int nIFO = NET->ifoListSize(); // number of detectors
637  int K = NET->nLag; // number of time lag
638  netevent* EVT;
639  wavearray<double> id;
640  //double factor = cfg->simulation==3||cfg->simulation==4 ? -gIFACTOR : cfg->factors[gIFACTOR];
641  double factor = cfg->factors[gIFACTOR];
642  int rate = 0; // select all resolutions
643 
644  // search output root file in the system list
645  TFile* froot = NULL;
646  TList *files = (TList*)gROOT->GetListOfFiles();
647  TString outDump="";
648  if (files) {
649  TIter next(files);
650  TSystemFile *file;
651  TString fname;
652  bool check=false;
653  while ((file=(TSystemFile*)next())) {
654  fname = file->GetName();
655  // set output root file as the current file
656  if(fname.Contains("wave_")) {
657  froot=(TFile*)file;froot->cd();
658  outDump=fname;
659  outDump.ReplaceAll(".root.tmp",".txt");
660  //cout << "output file name : " << fname << endl;
661  }
662  }
663  if(!froot) {
664  cout << "CWB_Plugin_QLveto.C : Error - output root file not found" << endl;
665  gSystem->Exit(1);
666  }
667  } else {
668  cout << "CWB_Plugin_QLveto.C : Error - output root file not found" << endl;
669  gSystem->Exit(1);
670  }
671 
672  TTree* net_tree = (TTree *) froot->Get("waveburst");
673  if(net_tree!=NULL) {
674  EVT = new netevent(net_tree,nIFO);
675  net_tree->SetBranchAddress("Qveto",Qveto);
676  net_tree->SetBranchAddress("Lveto",Lveto);
677  } else {
678  EVT = new netevent(nIFO);
679  net_tree = EVT->setTree();
680  net_tree->Branch("Qveto",Qveto,TString::Format("Qveto[%i]/F",4));
681  net_tree->Branch("Lveto",Lveto,TString::Format("Lveto[%i]/F",3));
682  }
683  EVT->setSLags(gSLAGSHIFT); // set slags into netevent
684 
685  for(int k=0; k<K; k++) { // loop over the lags
686 
687  id = NET->getwc(k)->get(const_cast<char*>("ID"), 0, 'L', rate);
688 
689  for(int j=0; j<(int)id.size(); j++) { // loop over cluster index
690 
691  int ID = size_t(id.data[j]+0.5);
692 
693  if(NET->getwc(k)->sCuts[ID-1]!=-1) continue; // skip rejected/processed clusters
694 
695  double ofactor=0;
696  if(cfg->simulation==4) ofactor=-factor;
697  else if(cfg->simulation==3) ofactor=-gIFACTOR;
698  else ofactor=factor;
699 
700  EVT->output2G(NULL,NET,ID,k,ofactor); // get reconstructed parameters
701 
702  wavearray<double>** pwfREC = new wavearray<double>*[nIFO];
703  detector* pd = NET->getifo(0);
704  int idSize = pd->RWFID.size();
705 
706  int wfIndex=-1;
707  for (int mm=0; mm<idSize; mm++) if (pd->RWFID[mm]==ID) wfIndex=mm;
708  if(wfIndex==-1) continue;
709 
710  netcluster* pwc = NET->getwc(k);
711  cout << endl << "----------------------------------------------------------------" << endl;
712 
713  // extract whitened reconstructed waveforms
714  Qveto[0]=Qveto[1]=1.e20;
715  double Qfactor1[NIFO_MAX];
716  double Qfactor2[NIFO_MAX];
717  for(int n=0; n<nIFO; n++) {
718 
719  pd = NET->getifo(n);
720 
721  pwfREC[n] = pd->RWFP[wfIndex];
722  wavearray<double>* wfREC = pwfREC[n]; // array of reconstructed waveforms
723 
724 #ifdef PLOT_WHITENED_WAVEFORMS
725  //PlotWaveform(NET->ifoName[n], wfREC, cfg, false, false);
726  PlotWaveform(NET->ifoName[n], wfREC, cfg, true, false);
727 #endif
728  // store in Qveto[0/1] the minimum velue of Qveto/Qfactor between the reconstructed,whitened waveforms in all ifos
729  float qveto,qfactor;
730 
731  // reconstructed whitened waveform
732  NET->getMRAwave(ID,k,'S',0,true);
733  GetQveto(&(pd->waveForm), qveto, qfactor);
734  if(qveto<Qveto[0]) Qveto[0]=qveto;
735  if(qfactor<Qveto[1]) Qveto[1]=qfactor;
736 
737  Qfactor1[n] = qfactor;
738  double fpeak = GetPeakFrequency(&(pd->waveForm));
739  Qfactor2[n] = GetQfactor(&(pd->waveForm), fpeak, true); // get qfactor with amax fixed;
740  cout << endl;
741  cout << "Qfactor1/2 : " << pd->Name << " frequency[0] = " << EVT->frequency[0] << " fpeak = " << fpeak
742  << " -> " << Qfactor1[n] << " / " << Qfactor2[n] << endl;
743  cout << endl;
744 
745  // whitened waveform
746  NET->getMRAwave(ID,k,'W',0,true);
747  GetQveto(&(pd->waveBand), qveto, qfactor);
748  if(qveto<Qveto[0]) Qveto[0]=qveto;
749  if(qfactor<Qveto[1]) Qveto[1]=qfactor;
750 
751  cout << "Qveto : " << pd->Name << " Qveto[0] = " << Qveto[0]
752  << " Qveto[1] = " << Qveto[1] << endl;
753 
754  if(!cfg->simulation) ClearWaveforms(pd); // release waveform memory
755  }
756  delete [] pwfREC;
757 
758  // compute Qveto[2] using the weighted of Qfactor1 values
759  Qveto[2]=0.;
760  for(int n=0; n<nIFO; n++) Qveto[2]+=EVT->sSNR[n]*Qfactor1[n];
761  Qveto[2]/=EVT->likelihood;
762  // compute Qveto[3] using the weighted of Qfactor2 values
763  Qveto[3]=0.;
764  for(int n=0; n<nIFO; n++) Qveto[3]+=EVT->sSNR[n]*Qfactor2[n];
765  Qveto[3]/=EVT->likelihood;
766  cout << "Qveto :" << " Qveto[2] = " << Qveto[2]
767  << " Qveto[3] = " << Qveto[3] << endl;
768 
769  std::vector<netpixel> vPIX;
770  if(cfg->pattern>0) vPIX = DoPCA(NET, cfg, k, ID); // do PCA analysis
771  GetLveto(pwc, ID, nIFO, Lveto);
772  if(cfg->pattern>0) ResetPCA(NET, cfg, pwc, &vPIX, ID); // restore WP pwc
773 
774  cout << endl;
775  cout << "Lveto : " << "fmean : " << Lveto[0] << " frms : " << Lveto[1]
776  << " Energy Ratio : " << Lveto[2] << endl << endl;
777  cout << "----------------------------------------------------------------" << endl;
778 
779  std::vector<int> sCuts = NET->getwc(k)->sCuts; // save cCuts
780  // set sCuts=1 to the events which must be not copied with cps to pwc
781  for(int i=0; i<(int)sCuts.size(); i++) if(i!=ID-1) NET->getwc(k)->sCuts[i]=1;
782 
783  // ID can not be used to get the event, to get event use ID=1 (ex: for watplot)
784  NET->getwc(k)->sCuts = sCuts; // restore cCuts
785 
786  if(cfg->dump) EVT->dopen(outDump.Data(),const_cast<char*>("a"),false);
787  EVT->output2G(net_tree,NET,ID,k,ofactor); // get reconstructed parameters
788  if(cfg->dump) {
789  // add Qveto to dump file
790  fprintf(EVT->fP,"Qveto: ");
791  for(int i=0; i<2*nIFO; i++) fprintf(EVT->fP,"%f ",Qveto[i]);
792  fprintf(EVT->fP,"\n");
793  // add Lveto to dump file
794  fprintf(EVT->fP,"Lveto: ");
795  for(int i=0; i<3; i++) fprintf(EVT->fP,"%f ",Lveto[i]);
796  fprintf(EVT->fP,"\n");
797  }
798  if(cfg->dump) EVT->dclose();
799  if(!cfg->cedDump) NET->getwc(k)->sCuts[ID-1]=1; // mark as processed
800  }
801  }
802 
803  jfile->cd();
804  if(EVT) delete EVT;
805  }
806  return;
807 }
808 
809 void
810 GetQveto(wavearray<double>* wf, float &Qveto, float &Qfactor) {
811 
812  wavearray<double> x = *wf;;
813 
814  // resample data by a factor 4
815  int xsize=x.size();
816  x.FFTW(1);
817  x.resize(4*x.size());
818  x.rate(4*x.rate());
819  for(int j=xsize;j<x.size();j++) x[j]=0;
820  x.FFTW(-1);
821 
822  // extract max/min values and save the absolute values in the array 'a'
823  wavearray<double> a(x.size());
824  int size=0;
825  double dt = 1./x.rate();
826  double prev=x[0];
827  double xmax=0;
828  for (int i=1;i<x.size();i++) {
829  if(fabs(x[i])>xmax) xmax=fabs(x[i]);
830  if(prev*x[i]<0) {
831  a[size]=xmax;
832  size++;
833  xmax=0;
834  }
835  prev=x[i];
836  }
837 
838  // find max value/index ans save on amax/imax
839  int imax=-1;
840  double amax=0;
841  for (int i=1;i<size;i++) {
842  if(a[i]>amax) {amax=a[i];imax=i;}
843  }
844 
845 /*
846  cout << endl;
847  cout << "a[imax-2] " << a[imax-2] << endl;
848  cout << "a[imax-1] " << a[imax-1] << endl;
849  cout << "a[imax] " << a[imax] << endl;
850  cout << "a[imax+1] " << a[imax+1] << endl;
851  cout << "a[imax+2] " << a[imax+2] << endl;
852  cout << endl;
853 */
854 
855  // compute Qveto
856  double ein=0; // energy of max values inside NTHR
857  double eout=0; // energy of max values outside NTHR
858  for (int i=0;i<size;i++) {
859  if(abs(imax-i)<=NTHR) {
860  ein+=a[i]*a[i];
861  //cout << i << " ein " << a[i] << " " << amax << endl;
862  } else {
863  if(a[i]>amax/ATHR) eout+=a[i]*a[i];
864  //if(a[i]>amax/ATHR) cout << i << " eout " << a[i] << " " << amax << endl;
865  }
866  }
867  Qveto = ein>0 ? eout/ein : 0.;
868  //cout << "Qveto : " << Qveto << " ein : " << ein << " eout : " << eout << endl;
869 
870  // compute Qfactor
871  float R = (a[imax-1]+a[imax+1])/a[imax]/2.;
872  Qfactor = sqrt(-pow(TMath::Pi(),2)/log(R)/2.);
873  //cout << "Qfactor : " << Qfactor << endl;
874 
875  return;
876 }
877 
878 void
879 GetLveto(netcluster* pwc, int cid, int nifo, float* Lveto) {
880 //
881 // input
882 // pwc : pointer to netcluster object
883 // cid : cluster id
884 // nifo : number of detectors
885 // output
886 // Lveto[0] : line frequency
887 // Lveto[1] : line bandwitdh
888 // Lveto[2] : line enery ratio (line_energy / total_energy)
889 //
890 
891  Lveto[0] = Lveto[1] = Lveto[2] = 0;
892 
893  std::vector<int>* vint = &(pwc->cList[cid-1]); // pixel list
894  int V = vint->size(); // cluster size
895  if(!V) return;
896 
897  // ------------------------------------------------------------------
898  // Find max pixel parameters
899  // ------------------------------------------------------------------
900 
901  double likeMax=0; // maximum pixel's energy
902  double likeTot=0; // total cluster energy
903  double freqMax; // frequency of the pixel with max energy
904  double dfreqMax; // df of the pixel with max energy
905  for(int n=0; n<V; n++) {
906  netpixel* pix = pwc->getPixel(cid,n);
907  if(pix->layers%2==0) {
908  cout << "CWB_Plugin_QLveto.C - Error : is enabled only for WDM (2G)" << endl;
909  exit(1);
910  }
911  if(!pix->core) continue; // select only the principal components pixels
912 
913  double likePix=0;
914  for(int m=0; m<nifo; m++) {
915  likePix += pow(pix->getdata('S',m),2); // like whitened reconstructed signal 00
916  likePix += pow(pix->getdata('P',m),2); // like whitened reconstructed signal 90
917  }
918 
919  double freq = pix->frequency*pix->rate/2.;
920  double df = pix->rate/2.;
921 
922  likeTot+=likePix;
923  if(likePix>likeMax) {likeMax=likePix;freqMax=freq;dfreqMax=df;}
924  }
925  //cout << "likeMax : " << likeMax << " likeTot : " << likeTot
926  // << " freqMax : " << freqMax << " dfreqMax : " << dfreqMax << endl;
927 
928  // ------------------------------------------------------------------
929  // Compute Lveto parameters
930  // ------------------------------------------------------------------
931 
932  double fmean=0; // line mean frequency
933  double frms=0; // line bandwidth
934  double likeLin=0; // line energy
935  for(int n=0; n<V; n++) {
936  netpixel* pix = pwc->getPixel(cid,n);
937  if(!pix->core) continue; // select only the principal components pixels
938 
939  double likePix=0;
940  for(int m=0; m<nifo; m++) {
941  likePix += pow(pix->getdata('S',m),2); // like whitened reconstructed signal 00
942  likePix += pow(pix->getdata('P',m),2); // like whitened reconstructed signal 90
943  }
944 
945  // the estimation is done for all pixels
946  // with freq in the range [freqMax-dfreqMax, freqMax+dfreqMax]
947  double freq = pix->frequency*pix->rate/2.;
948  if(fabs(freq-freqMax)<=dfreqMax) {
949  likeLin += likePix;
950  fmean += likePix*freq;
951  frms += likePix*freq*freq;
952  }
953  }
954 
955  fmean = fmean/likeLin;
956  frms = frms/likeLin-fmean*fmean;
957  frms = frms>0 ? sqrt(frms) : 0.;
958 
959  if(frms<dfreqMax/2.) frms=dfreqMax/2.;
960 
961  // ------------------------------------------------------------------
962  // Save Lveto parameters
963  // ------------------------------------------------------------------
964 
965  Lveto[0] = fmean; // line mean frequency
966  Lveto[1] = frms; // line bandwidth
967  Lveto[2] = likeTot>0. ? likeLin/likeTot : 0.; // energy ratio energy inside_line/total
968 
969  // ------------------------------------------------------------------
970  // plot time-frequency energy
971  // ------------------------------------------------------------------
972 
973 #if defined PLOT_LIKELIHOOD
974  watplot WTS(const_cast<char*>("wts"));
975  WTS.plot(pwc, cid, nifo, 'L', 0, const_cast<char*>("COLZ"));
976  WTS.canvas->Print("l_tfmap_scalogram.png");
977 #endif
978 
979  return;
980 }
981 
982 void
983 PlotWaveform(TString ifo, wavearray<double>* wfREC,
984  CWB::config* cfg, bool fft, bool strain) {
985 
986  watplot PTS(const_cast<char*>("ptswrc"),200,20,800,500);
987 
988  //cout << "Print " << fname << endl;
989  double tmin = wfREC->start();
990  wfREC->start(wfREC->start()-tmin);
991  if(fft) {
992  PTS.plot(wfREC, const_cast<char*>("ALP"), 1, 0, 0, true, cfg->fLow, cfg->fHigh);
993  } else {
994  PTS.plot(wfREC, const_cast<char*>("ALP"), 1, 0, 0);
995  }
996  PTS.graph[0]->SetLineWidth(1);
997  wfREC->start(wfREC->start()+tmin);
998 
999  char label[64]="";
1000  if(fft) sprintf(label,"%s","fft");
1001  else sprintf(label,"%s","time");
1002  if(strain) sprintf(label,"%s_%s",label,"strain");
1003  else sprintf(label,"%s_%s",label,"white");
1004 
1005  char fname[1024];
1006  //sprintf(fname, "%s_wf_%s_rec_gps_%d.root",ifo.Data(),label,int(tmin));
1007  sprintf(fname, "%s_wf_%s_rec_gps_%d.png",ifo.Data(),label,int(tmin));
1008  PTS.canvas->Print(fname);
1009  cout << "write : " << fname << endl;
1010  //PTS.canvas->Write(REPLACE(fname,dirCED,gtype));
1011 }
1012 
1013 void
1014 ClearWaveforms(detector* ifo) {
1015 
1016  int n;
1017 
1018  n = ifo->IWFP.size();
1019  for (int i=0;i<n;i++) {
1020  wavearray<double>* wf = (wavearray<double>*)ifo->IWFP[i];
1021  delete wf;
1022  }
1023  ifo->IWFP.clear();
1024  ifo->IWFID.clear();
1025 
1026  n = ifo->RWFP.size();
1027  for (int i=0;i<n;i++) {
1028  wavearray<double>* wf = (wavearray<double>*)ifo->RWFP[i];
1029  delete wf;
1030  }
1031  ifo->RWFP.clear();
1032  ifo->RWFID.clear();
1033 }
1034 
1035 std::vector<netpixel> DoPCA(network* NET, CWB::config* cfg, int lag, int id) {
1036 
1037  double ee;
1038 
1039  size_t nIFO = NET->ifoList.size();
1040 
1041  float En = 2*NET->acor*NET->acor*nIFO; // network energy threshold in the sky loop
1042 
1043  int size = NET->a_00.size();
1044  int f_ = NIFO/4;
1045 
1046  netpixel* pix;
1047  netcluster* pwc = NET->getwc(lag);
1048  std::vector<netpixel> vPIX;
1049 
1050  std::vector<int> pI = NET->wdmMRA.getXTalk(pwc, id, false); // buffer for pixel IDs
1051  int V = pI.size();
1052  if(V>cfg->BATCH) return vPIX; // attach TD amp to pixels < V
1053 
1054  wavearray<float> xi(size); xi=0; // PC 00 array
1055  wavearray<float> XI(size); XI=0; // PC 90 array
1056 
1057  __m128* _xi = (__m128*) xi.data; // set pointer to PC 00 array
1058  __m128* _XI = (__m128*) XI.data; // set pointer to PC 90 array
1059 
1060  __m128* _aa = (__m128*) NET->a_00.data; // set pointer to 00 array
1061  __m128* _AA = (__m128*) NET->a_90.data; // set pointer to 90 array
1062 
1063  int nPC = 0;
1064  for(int j=0; j<V; j++) {
1065  int jf = j*f_; // source sse pointer increment
1066  _sse_zero_ps(_xi+jf); // zero MRA amplitudes
1067  _sse_zero_ps(_XI+jf); // zero MRA amplitudes
1068  ee = _sse_abs_ps(_aa+jf,_AA+jf); // total pixel energy / quadrature
1069  if(ee>En) nPC++; else ee=0.; // count core pixels
1070  NET->rNRG.data[j] = ee; // init residual energy array
1071  NET->pNRG.data[j] = NET->rNRG.data[j]; // init residual energy array
1072  }
1073 
1074  nPC = NET->_sse_mra_ps(xi.data,XI.data,En,nPC); // get principal components
1075 
1076  for(int j=0; j<V; j++) { // loop over principal components
1077  pix = pwc->getPixel(id,pI[j]);
1078  vPIX.push_back(*pix); // save original pixels
1079  pix->core = false;
1080  ee = NET->pNRG.data[j]; // total pixel energy
1081  if(ee<En) continue;
1082  pix->core = true;
1083  for(int i=0; i<nIFO; i++) {
1084  pix->setdata(double(xi.data[j*NIFO+i]),'S',i); // store 00 whitened response PC
1085  pix->setdata(double(XI.data[j*NIFO+i]),'P',i); // store 90 whitened response PC
1086  }
1087  }
1088 
1089  return vPIX;
1090 }
1091 
1092 void ResetPCA(network* NET, CWB::config* cfg, netcluster* pwc, std::vector<netpixel>* vPIX, int ID) {
1093 
1094  std::vector<int> pI = NET->wdmMRA.getXTalk(pwc, ID, false); // buffer for pixel IDs
1095  int V = pI.size();
1096  if(V>cfg->BATCH) return; // attach TD amp to pixels < V
1097  for(int j=0; j<V; j++) {
1098  netpixel* pix = pwc->getPixel(ID,pI[j]);
1099  *pix = (*vPIX)[j];
1100  }
1101 
1102  while(!vPIX->empty()) vPIX->pop_back();
1103  vPIX->clear(); std::vector<netpixel>().swap(*vPIX);
1104 }
1105 
1106 double
1107 GetQfactor(wavearray<double>* wf, double frequency, bool fixAmax) {
1108 //
1109 // wf -> input waveform
1110 // frequency -> input peak frequency
1111 // fixAmax -> true/false : fix/not-fix amplitude of the gaussian fit
1112 //
1113 // -> the input waveform wf is resampled 4x
1114 // -> get mean and sigma from a gaussian fit of the waveform envelope
1115 // -> compute the range [xmin,xmax] = [mean-3*sigma,mean-3*sigma]]
1116 // -> compute area of the envelope in the rage [xmin,xmax]
1117 // -> compute qfactor using area, max_amplitude and frequency
1118 // -> return qfactor
1119 //
1120 
1121  wavearray<double> x = *wf;;
1122 
1123  // resample data by a factor 4
1124  int xsize=x.size();
1125 
1126  x.FFTW(1);
1127  x.resize(4*x.size());
1128  x.rate(4*x.rate());
1129  for(int j=xsize;j<x.size();j++) x[j]=0;
1130  x.FFTW(-1);
1131 
1132  // compute range [xmin,xmax] used for final fit using the square of x
1133  double gmean, gsigma;
1134  GetGaussFitPars2(&x, gmean, gsigma, fixAmax);
1135 // cout << "GetQfactor -> gmean " << gmean << " gsigma " << gsigma << endl;
1136 
1137  double xmin = gmean-3.0*gsigma;
1138  double xmax = gmean+3.0*gsigma;
1139 // cout << "GetQfactor -> xmin " << xmin << " xmax " << xmax << endl;
1140 
1141  x = CWB::mdc::GetEnvelope(&x); // get envelope
1142 
1143  // extract max/min values and save the absolute values in the array 'a'
1144  double dt = 1./x.rate();
1145  // find max value/index and save on amax/imax
1146  int imax=-1;
1147  double amax=0;
1148  for (int i=1;i<x.size();i++) {
1149  if(x[i]>amax) {amax=x[i];imax=i;}
1150  }
1151 
1152  double area=0.;
1153  for (int i=1;i<x.size();i++) {
1154  double t=i*dt;
1155  if(t>xmin && t<xmax) area+=x[i]*dt;
1156  }
1157 
1158  double sigma = area/amax/sqrt(2*TMath::Pi());
1159 // cout << "sigma integral is " << sigma << endl;
1160  double qfactor = sigma*(TMath::TwoPi()*frequency); // compute qfactor
1161 
1162  return qfactor;
1163 }
1164 
1165 double
1166 GetPeakFrequency(wavearray<double>* wf) {
1167 //
1168 // wf -> input waveform
1169 //
1170 // -> multiply the input waveform wf by its envelope
1171 // -> perform FFT transform
1172 // -> compute max frequency -> peak
1173 // -> return frequency peak
1174 //
1175 
1176  wavearray<double> x = *wf;
1177  wavearray<double> ex = CWB::mdc::GetEnvelope(&x); // get envelope
1178  int N = x.size();
1179  for(int i=0;i<N;i++) x[i]*=ex[i];
1180 
1181  x.FFTW(1);
1182 
1183  // fill time/amp graph
1184  double df=(x.rate()/(double)N)/2.;
1185  double amax=0.;
1186  double fmax=0.;
1187  for(int i=0;i<N;i+=2) {
1188  double freq=i*df;
1189  double amp=x[i]*x[i]+x[i+1]*x[i+1];
1190  if(amp>amax) {fmax=freq;amax=amp;}
1191  }
1192 
1193  return fmax;
1194 }
1195 
1196 void
1197 GetGaussFitPars(wavearray<double>* wf, double& mean, double& sigma, bool doenv) {
1198 //
1199 // wf -> input waveform
1200 // doenv -> true/false do/not-do waveform envelope
1201 //
1202 // mean, sigma -> output gaussian fit parameters
1203 //
1204 // -> get evelope from the input waveform wf and perform fit with gaussian function
1205 // -> return mean and gigma of gaussian fit
1206 //
1207 
1208  wavearray<double> x = doenv ? CWB::mdc::GetEnvelope(wf) : *wf; // get envelope
1209 
1210  // fill time/amp graph
1211  int N = x.size();
1212  double* time = new double[N];
1213  double* amp = new double[N];
1214  double dt = 1./x.rate();
1215  for(int i=0;i<N;i++) {
1216  time[i]=i*dt;
1217  amp[i]=x[i];
1218  }
1219  TGraph gr(N,time,amp);
1220 
1221  // fit signal envelope with gaussian function
1222  gr.Fit("gaus","q0");
1223  TF1* gaus = (TF1*)gr.GetFunction("gaus");
1224 
1225  mean = gaus->GetParameter("Mean"); // extract sigma from gauss fit
1226  sigma = gaus->GetParameter("Sigma"); // extract sigma from gauss fit
1227 
1228  delete [] time;
1229  delete [] amp;
1230 
1231  return;
1232 }
1233 
1234 void
1235 GetGaussFitPars2(wavearray<double>* wf, double& mean, double& sigma, bool fixAmax) {
1236 //
1237 // wf -> input waveform
1238 // fixAmax -> true/false : fix/not-fix amplitude of the gaussian fit
1239 //
1240 // mean, sigma -> output gaussian fit parameters
1241 //
1242 //
1243 // -> get the square of the evelope from the input waveform wf
1244 // -> perform fit with gaussian function and get mean and gigma of gaussian fit
1245 // -> compute the range [xmin,xmax] = [mean-3*sigma,mean-3*sigma]]
1246 //
1247 // -> get the evelope from the input waveform wf
1248 // -> perform fit with gaussian function in the range [xmin,xmax] fixing the amplitude to max envelope (according to fixAmax value)
1249 // -> return mean and gigma of gaussian fit
1250 //
1251 
1252  wavearray<double> x = CWB::mdc::GetEnvelope(wf); // get envelope
1253 
1254  // compute range [xmin,xmax] used for final fit using the square of x
1255  // x*x highlight the main peak
1256  wavearray<double> x2 = x; // getx*x
1257  for(int i=0;i<x.size();i++) x2[i]=x[i]*x[i];
1258 
1259  double gmean, gsigma;
1260  GetGaussFitPars(&x2, gmean, gsigma, false);
1261 // cout << "GetGaussFitPars2 -> gmean x2 " << gmean << " gsigma x2 " << gsigma << endl;
1262 
1263  double xmin = gmean-3.0*gsigma;
1264  double xmax = gmean+3.0*gsigma;
1265 // cout << "GetGaussFitPars2 -> xmin " << xmin << " xmax " << xmax << endl;
1266 
1267  // fill time/amp graph
1268  int N = x.size();
1269  double* time = new double[N];
1270  double* amp = new double[N];
1271  double dt = 1./x.rate();
1272  double amax=0;
1273  for(int i=0;i<N;i++) {
1274  time[i]=i*dt;
1275  amp[i]=x[i];
1276  if(fabs(x[i])>amax) amax=fabs(x[i]);
1277  }
1278  TGraph gr(N,time,amp);
1279 // cout << "GetGaussFitPars2 -> amax " << amax << endl;
1280 
1281  // fit signal envelope with gaussian function
1282  TF1 *xgaus = new TF1("xgaus","gaus",xmin,xmax);
1283  if(fixAmax) xgaus->SetParLimits(0,amax*0.999,amax*1.001); // fix gauss constant par to amax
1284  gr.Fit("xgaus","RQ");
1285 
1286  mean = xgaus->GetParameter("Mean"); // extract sigma from gauss fit
1287  sigma = xgaus->GetParameter("Sigma"); // extract sigma from gauss fit
1288 
1289  delete xgaus;
1290  delete [] time;
1291  delete [] amp;
1292 
1293  return;
1294 }
1295 
1296 }
1297 
1298 // -------------------------------------------------------------------------
1299 // --> END CWB_USER PLUGIN CODE 3
1300 //
1301 // CWB_Plugin_QLveto.C
1302 // -------------------------------------------------------------------------
1303 
1304 
1305 // -------------------------------------------------------------------------
1306 // --> MAIN CWB_USER PLUGIN CODE
1307 //
1308 // INPUT PLUGINS :
1309 //
1310 // 1 - CWB_Plugin_UniqueLags.C
1311 // 2 - CWB_Plugin_Gating.C
1312 // 3 - CWB_Plugin_QLveto.C
1313 //
1314 // -------------------------------------------------------------------------
1315 
1316 #define XIFO 4
1317 #pragma GCC system_header
1318 #include "cwb.hh"
1319 
1320 
1321 void
1322 CWB_Plugin(TFile* jfile, CWB::config* cfg, network* net, WSeries<double>* x, TString ifo, int type) {
1323 
1324  CWB_UserPluginNamespace_1::CWB_Plugin(jfile, cfg, net, x, ifo, type); // CALL USER PLUGIN CODE 1
1325  CWB_UserPluginNamespace_2::CWB_Plugin(jfile, cfg, net, x, ifo, type); // CALL USER PLUGIN CODE 2
1326  CWB_UserPluginNamespace_3::CWB_Plugin(jfile, cfg, net, x, ifo, type); // CALL USER PLUGIN CODE 3
1327 }
TGraph * gr
Definition: Make_PP_IFAR.C:167
lagMax
segLen
lagSize
nIFO
TString options
void CWB_Plugin(TFile *jfile, CWB::config *cfg, network *net, WSeries< double > *x, TString ifo, int type)
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)
void CWB_Plugin(TFile *jfile, CWB::config *cfg, network *NET, WSeries< double > *x, TString ifo, int type)
std::vector< netpixel > DoPCA(network *NET, CWB::config *cfg, int lag, int id)
void GetLveto(netcluster *pwc, int cid, int nifo, float *Lveto)
void CWB_Plugin(TFile *jfile, CWB::config *cfg, network *NET, WSeries< double > *x, TString ifo, int type)
void GetGaussFitPars2(wavearray< double > *wf, double &mean, double &sigma, bool fixAmax)
double GetQfactor(wavearray< double > *wf, double frequency, bool fixAmax)
void GetGaussFitPars(wavearray< double > *wf, double &mean, double &sigma, bool doenv=true)
void ResetPCA(network *NET, CWB::config *cfg, netcluster *pwc, std::vector< netpixel > *vPIX, int ID)
double GetPeakFrequency(wavearray< double > *wf)
void GetQveto(wavearray< double > *wf, float &Qveto, float &Qfactor)
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