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