Logo Coherent WaveBurst  
Reference Guide
Logo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CWB_Plugin_Linear_Bilinear_Regression_Rank.C
Go to the documentation of this file.
1 #define XIFO 4
2 
3 #pragma GCC system_header
4 
5 #include "cwb.hh"
6 #include "config.hh"
7 #include "network.hh"
8 #include "wavearray.hh"
9 #include "TString.h"
10 #include "TObjArray.h"
11 #include "TObjString.h"
12 #include "TRandom.h"
13 #include "TComplex.h"
14 #include "TMath.h"
15 #include "TSystem.h"
16 #include "mdc.hh"
17 #include "WDM.hh"
18 #include "regression.hh"
19 #include "frame.hh"
20 #include "watplot.hh"
21 #include "Biorthogonal.hh"
22 #include <fstream>
23 #include <vector>
24 
25 #define NCH 1000
26 
27 using namespace CWB;
28 
29 void
31 //!REGRESSION
32 // Plugin for linear/bilinear ranking analysis for one detector
33 
34  cout << endl;
35  cout << "-----> CWB_Plugin_Linear_Bilinear_Regression_Rank.C : " << ifo.Data() << endl;
36  cout << endl;
37 
38  //---------------------------------------------------------------------
39  // MAIN REGRESSION RANK
40  //---------------------------------------------------------------------
41 
42  bool SAVE_TREE = false; // if true then results are saved to tree
43  bool SAVE_ASCII = false; // if true then results are saved to ascii file
44 
45  TString REGRESSION_RANK = "LINEAR"; // regression rank [LINEAR/BILINEAR]
46  int CUT_LOW_FREQ = 32; // if > 0 the data in [0:CUT_LOW_FREQ] are set to 0
47 
48  //---------------------------------------------------------------------
49  // REGRESSION PARAMETERS
50  //---------------------------------------------------------------------
51 
52  TString FRLIST_WITNESS = ""; // list of witness frame files
53  TString CHLIST_LINEAR = ""; // list of channel names used for linear regression
54  TString CHLIST_WITNESS = ""; // list of channel names used for regression rank
55 
56  TString CHNAME_LINEAR = ""; // channel name used to made the bilinear channels
57 
58  int RESAMPLING_INDEX = 11; // resample target/witness channels to pow(2,RESAMPLING_INDEX)
59 
60  double WFLOW = 0; // lower frequency used to made the bilinear channels
61  double WFHIGH = 10; // high frequency used to made the bilinear channels
62 
63  //---------------------------------------------------------------------
64  // REGRESSION PARAMETERS
65  //---------------------------------------------------------------------
66 
67  double LAYER_WIDTH = 1.0; // frequency layer resolution used in the regression analysis
68  double fPOWERLINE = 60.0; // powerline frequency (Hz)
69  int lPOWERLINE = 1; // low power line harmonic (lPOWERLINE*fPOWERLINE)
70  int hPOWERLINE = 3; // high power line harmonic (hPOWERLINE*fPOWERLINE)
71  double FWIDTH = 5; // frequency width used by regression
72 
73  // PARAMETERS FOR LINEAR REGRESSION
74 
75  int L_NFILTER = 5; // half-size length of a unit filter (setFilter)
76  double L_APPLY_THRESHOLD = 0.2; // threshold used in apply
77  double L_SOLVE_THRESHOLD = 0.0; // eigenvalue threshold (solve)
78  double L_SOLVE_NEIGEN_PER_LAYER = 0; // number of selected eigenvalues (solve)
79  char L_SOLVE_REGULATOR = 'h'; // regulator (solve)
80 
81  // PARAMETERS FOR BILINEAR REGRESSION
82 
83  int B_NFILTER = 5; // half-size length of a unit filter (setFilter)
84  double B_APPLY_THRESHOLD = 0.2; // threshold used in apply
85  double B_SOLVE_THRESHOLD = 0.0; // eigenvalue threshold (solve)
86  double B_SOLVE_NEIGEN_PER_LAYER = 0; // number of selected eigenvalues (solve)
87  char B_SOLVE_REGULATOR = 'h'; // regulator (solve)
88 
89  // ---------------------------------
90  // read plugin config
91  // ---------------------------------
92 
93  cfg->configPlugin.Exec();
94 
95  IMPORT(bool,SAVE_TREE)
96  IMPORT(bool,SAVE_ASCII)
97 
98  IMPORT(TString,REGRESSION_RANK)
99  IMPORT(int,CUT_LOW_FREQ)
100  IMPORT(int,RESAMPLING_INDEX)
101  IMPORT(double,WFLOW)
102  IMPORT(double,WFHIGH)
103 
104  IMPORT(TString,FRLIST_WITNESS)
105  IMPORT(TString,CHLIST_LINEAR)
106  IMPORT(TString,CHLIST_WITNESS)
107  IMPORT(TString,CHNAME_LINEAR)
108 
109  if(FRLIST_WITNESS=="") {cout << "Error : witness frames list not defined" << endl;gSystem->Exit(1);}
110  if(CHLIST_LINEAR=="") {cout << "Error : linear witness channels list not defined" << endl;gSystem->Exit(1);}
111  if(CHLIST_WITNESS=="") {cout << "Error : witness channels list not defined" << endl;gSystem->Exit(1);}
112  if(CHNAME_LINEAR=="") {cout << "Error : main linear witness channel list not defined" << endl;gSystem->Exit(1);}
113 
114  IMPORT(double,LAYER_WIDTH)
115  IMPORT(double,fPOWERLINE)
116  IMPORT(int,lPOWERLINE)
117  IMPORT(int,hPOWERLINE)
118  IMPORT(double,FWIDTH)
119 
120  IMPORT(int,L_NFILTER)
121  IMPORT(double,L_APPLY_THRESHOLD)
122  IMPORT(double,L_SOLVE_THRESHOLD)
123  IMPORT(double,L_SOLVE_NEIGEN_PER_LAYER)
124  IMPORT(char,L_SOLVE_REGULATOR)
125 
126  IMPORT(int,B_NFILTER)
127  IMPORT(double,B_APPLY_THRESHOLD)
128  IMPORT(double,B_SOLVE_THRESHOLD)
129  IMPORT(double,B_SOLVE_NEIGEN_PER_LAYER)
130  IMPORT(char,B_SOLVE_REGULATOR)
131 
132  REGRESSION_RANK.ToUpper();
133  if(REGRESSION_RANK!="LINEAR" && REGRESSION_RANK!="BILINEAR") {
134  cout << "Error : REGRESSION_RANK not defined [LINEAR/BILINEAR]" << endl;
135  gSystem->Exit(1);
136  }
137 
138  if(type==CWB_PLUGIN_DATA_MDC) {
139 
140  int rank[4] = {50,30,10,1};
141 
142  int nCH=0;
143  std::vector<std::string> CHN;
144  std::vector<std::string>* pCHN=&CHN;
145  double CHC[4][NCH];
146 
147  // get ifo index
148  int xIFO =0;
149  for(int n=0;n<cfg->nIFO;n++) if(ifo==cfg->ifo[n]) {xIFO=n;break;}
150 
151  int runID = net->nRun;
152  char flabel[512];
153  int Tb=x->start();
154  int dT=x->size()/x->rate();
155  sprintf(flabel,"%d_%d_%s_job%d",int(Tb),int(dT),cfg->data_label,runID);
156 
157  Biorthogonal<double> Bio(512);
158  WSeries<double> wB(Bio);
159  WSeries<double> wT;
161  int size=0;
162 
163  // target channel
164  int level=x->getLevel();
165  x->Inverse(-1);
166  wavearray<double> xx(x->size());
167  xx.start(x->start());
168  xx.rate(x->rate());
169  for(int i=0;i<x->size();i++) xx.data[i]=x->data[i];
170 
171  if(CUT_LOW_FREQ>0) {
172  // set range [0-CUT_LOW_FREQ]Hz to 0 (remove large dynamics at low frequency)
173  int SR = int(xx.rate());
174  int mm = 0;
175  while (((SR % 2) == 0) && SR > 2*CUT_LOW_FREQ) {SR /= 2;mm++;}
176  wB.Forward(xx,mm);
177  for(int i=0;i<1;i++) {wB.getLayer(xx,i);xx=0;wB.putLayer(xx,i);}
178  wB.Inverse();
179  wB.getLayer(xx,0);
180  cout << "Set Frequency Range [0:" << SR/2 << "] = 0" << endl;
181  }
182 
183  // resample target to pow(2,RESAMPLING_INDEX) Hz
184  int sr = int(xx.rate());
185  int nn = 0;
186  while (((sr % 2) == 0) && sr > (1<<RESAMPLING_INDEX)) {sr /= 2;nn++;}
187  wB.Forward(xx,nn);
188  wB.getLayer(xx,0);
189 
190  int rrlevel=xx.rate()/(2*LAYER_WIDTH);
191  WDM<double> WD(rrlevel, 2*rrlevel, 6, 12);
192  double scratch = WD.m_H/xx.rate();
193  if(scratch>cfg->segEdge+0.001) {
194  cout << endl;
195  cout << "Regression Plugin : Error - filter scratch must be <= cwb scratch!!!" << endl;
196  cout << "filter scratch : " << scratch << " sec" << endl;
197  cout << "cwb scratch : " << cfg->segEdge << " sec" << endl;
198  gSystem->Exit(1);
199  }
200 
201  regression rr;
202 
203  // --------------------------------------------------------------
204  // LINEAR RANKING
205  // --------------------------------------------------------------
206 
207  cout << "CWB_PLUGIN_DATA_MDC : Apply Linear Regression ..." << endl;
208 
209  frame frl(FRLIST_WITNESS,"","README",true);
211  wl.start(x->start()); wl.stop(x->stop());
212  wT.Forward(xx,WD);
213 
214  if(REGRESSION_RANK=="LINEAR") {
215  cout << "CWB_PLUGIN_DATA_MDC : Apply Linear Regression Rank ..." << endl;
216 
217  // open witness channel list
218  ifstream ifl;
219  ifl.open(CHLIST_WITNESS.Data(),ios::in);
220  if (!ifl.good()) {cout << "Error Opening File : " << CHLIST_WITNESS.Data() << endl;gSystem->Exit(1);}
221 
222  char witness[1024];
223  cout << endl;
224 
225  char ofname[256];
226  sprintf(ofname,"%s/linear_regression_rank_%s_%s.txt",cfg->output_dir,ifo.Data(),flabel);
227  cout << "write results to " << ofname << endl;
228  ofstream out;
229  if(SAVE_ASCII) {
230  out.open(ofname);
231  if (!out.good()) {cout << "Error Opening File : " << ofname << endl;gSystem->Exit(1);}
232  }
233  nCH=0;
234  char fstring[512];
235  while(true) {
236  ifl >> witness;
237  if (!ifl.good()) break;
238  if(witness[0]=='#') continue;
239  //cout << "read witness channel : \t" << witness << endl;
240  frl.setChName(witness);
241  frl.setSRIndex(RESAMPLING_INDEX);
242  frl >> wl;
243  size = rr.add(wT,"target");
244  if(size==0) {cout << "Regression Plugin - empty target channel" << endl;gSystem->Exit(1);}
245  rr.mask(0,0.,xx.rate()/2.);
246  for(int n=lPOWERLINE;n<=hPOWERLINE;n++) {double f=n*fPOWERLINE;rr.unmask(0,f-FWIDTH,f+FWIDTH);}
247  size = rr.add(wl,witness);
248  if(size>0) {
249  rr.setFilter(L_NFILTER);
250  rr.setMatrix(cfg->segEdge,1.);
251  rr.solve(L_SOLVE_THRESHOLD,L_SOLVE_NEIGEN_PER_LAYER,L_SOLVE_REGULATOR);
252  rr.apply(L_APPLY_THRESHOLD);
253 
254  for(int n=0;n<4;n++) {
255  e[n] = rr.rank(rank[n],0);
256  CHC[n][nCH]=e[n][0];
257  }
258  } else {
259  for(int n=0;n<4;n++) {
260  CHC[n][nCH]=-1;
261  }
262  }
263  CHN.push_back(witness);
264  sprintf(fstring,"%3d %35s %10.5f %9.4f %8.3f %7.2f",
265  nCH,witness,CHC[0][nCH],CHC[1][nCH],CHC[2][nCH],CHC[3][nCH]);
266  cout << fstring << endl;
267  if(SAVE_ASCII) out << fstring << endl;out.flush();
268  nCH++;
269  }
270  cout << endl;
271  ifl.close();
272  if(SAVE_ASCII) out.close();
273  } else {
274 
275  // open linear channel list
276  ifstream ifl;
277  ifl.open(CHLIST_LINEAR.Data(),ios::in);
278  if (!ifl.good()) {cout << "Error Opening File : " << CHLIST_LINEAR.Data() << endl;gSystem->Exit(1);}
279 
280  char linear[1024];
281  cout << endl;
282 
283  size = rr.add(wT,"target");
284  if(size==0) {cout << "Regression Plugin - empty target channel" << endl;gSystem->Exit(1);}
285  rr.mask(0,0.,xx.rate()/2.);
286  for(int n=lPOWERLINE;n<=hPOWERLINE;n++) {double f=n*fPOWERLINE;rr.unmask(0,f-FWIDTH,f+FWIDTH);}
287  while(true) {
288  ifl >> linear;
289  if (!ifl.good()) break;
290  if(linear[0]=='#') continue;
291  cout << "read linear channel : \t" << linear << endl;
292  frl.setChName(linear);
293  frl.setSRIndex(RESAMPLING_INDEX);
294  frl >> wl;
295  rr.add(wl,linear);
296  }
297  cout << endl;
298  ifl.close();
299  rr.setFilter(L_NFILTER);
300  rr.setMatrix(cfg->segEdge,1.);
301  rr.solve(L_SOLVE_THRESHOLD,L_SOLVE_NEIGEN_PER_LAYER,L_SOLVE_REGULATOR);
302  rr.apply(L_APPLY_THRESHOLD);
303  }
304  wl.resize(0);
305 
306  // --------------------------------------------------------------
307  // BILINEAR RANKING
308  // --------------------------------------------------------------
309 
310  if(REGRESSION_RANK=="BILINEAR") {
311 
312  cout << "CWB_PLUGIN_DATA_MDC : Apply Bilinear Regression Rank ..." << endl;
313 
314  char ofname[256];
315  sprintf(ofname,"%s/bilinear_regression_rank_%s_%s.txt",cfg->output_dir,ifo.Data(),flabel);
316  cout << "write results to " << ofname << endl;
317  ofstream out;
318  if(SAVE_ASCII) {
319  out.open(ofname);
320  if (!out.good()) {cout << "Error Opening File : " << ofname << endl;gSystem->Exit(1);}
321  }
323  wT.Forward(yy,WD);
324 
325  // open main linear frame list
327  ml.start(x->start()); ml.stop(x->stop());
328  frame frb(FRLIST_WITNESS,"","README",true);
329 
330  // open witness channel list
331  ifstream iwb;
332  iwb.open(CHLIST_WITNESS.Data(),ios::in);
333  if (!iwb.good()) {cout << "Error Opening File : " << CHLIST_WITNESS.Data() << endl;gSystem->Exit(2);}
334 
335  char fstring[512];
336  char witness[1024];
338  wb.start(x->start()); wb.stop(x->stop());
339  cout << endl;
340  nCH=0;
341  while(true) {
342  iwb >> witness;
343  if (!iwb.good()) break;
344  //cout << "read witness channel : \t" << witness << endl;
345  if(witness[0]=='#') continue;
346  frb.setChName(witness);
347  frb.setSRIndex(RESAMPLING_INDEX);
348  frb >> wb;
349 
350  rr.clear();
351  size=rr.add(wT,"target");
352  if(size==0) {cout << "Regression Plugin - empty target channel" << endl;gSystem->Exit(1);}
353  rr.mask(0,0.,yy.rate()/2.);
354  for(int n=lPOWERLINE;n<=hPOWERLINE;n++) {double f=n*fPOWERLINE;rr.unmask(0,f-FWIDTH,f+FWIDTH);}
355 
356  // add main linear channel for bicoherence removal
357  //cout << endl;
358  //cout << "read main linear channel : \t" << CHNAME_LINEAR.Data() << endl << endl;
359  frb.setChName(CHNAME_LINEAR);
360  frb.setSRIndex(RESAMPLING_INDEX);
361  frb >> ml;
362  size=rr.add(ml,const_cast<char*>(CHNAME_LINEAR.Data()));
363  if(size==0) {cout << "Regression Plugin - empty main linear channel" << endl;gSystem->Exit(1);}
364 
365  if(rr.add(wb,witness,WFLOW,WFHIGH)) {
366  rr.add(1,2,"biwitness");
367  rr.mask(1); rr.mask(2);
368 
369  rr.setFilter(B_NFILTER);
370  rr.setMatrix(cfg->segEdge,1.);
371  rr.solve(B_SOLVE_THRESHOLD,B_SOLVE_NEIGEN_PER_LAYER,B_SOLVE_REGULATOR);
372  rr.apply(B_APPLY_THRESHOLD);
373 
374  for(int n=0;n<4;n++) {
375  e[n] = rr.rank(rank[n],0);
376  CHC[n][nCH]=e[n][0];
377  }
378  } else {
379  for(int n=0;n<4;n++) {
380  CHC[n][nCH]=-1;
381  }
382  }
383  CHN.push_back(witness);
384  nCH++;
385  sprintf(fstring,"%3d %35s %10.5f %9.4f %8.3f %7.2f",
386  nCH,witness,CHC[0][nCH],CHC[1][nCH],CHC[2][nCH],CHC[3][nCH]);
387  cout << fstring << endl;
388  if(SAVE_ASCII) out << fstring << endl;out.flush();
389  }
390  iwb.close();
391  if(SAVE_ASCII) out.close();
392  cout << endl;
393 
394  ml.resize(0);
395  wb.resize(0);
396  }
397 
398  if(SAVE_TREE) {
399  // save data to root
400  char outFile[512];
401  if(REGRESSION_RANK=="LINEAR") {
402  sprintf(outFile,"%s/linear_regression_rank_%s_%s.root",cfg->output_dir,ifo.Data(),flabel);
403  } else {
404  sprintf(outFile,"%s/bilinear_regression_rank_%s_%s.root",cfg->output_dir,ifo.Data(),flabel);
405  }
406  cout << "write results to " << outFile << endl;
407 
408  TFile ofile = TFile(outFile,"RECREATE");
409  cfg->Write("config"); // write config object
410  //cfg->configPlugin.Write("configPlugin"); // write configPlugin object
411 
412  TTree rrtree("regression","regression");
413  double igps=x->start();
414  double start=x->start();
415  double stop=x->stop();
416  char sifo[256];sprintf(sifo,"%s",ifo.Data());
417  rrtree.Branch("run",&runID,"run/I");
418  rrtree.Branch("gps",&igps,"gps/D");
419  rrtree.Branch("start",&start,"start/D");
420  rrtree.Branch("stop",&stop,"stop/D");
421  rrtree.Branch("sifo",sifo,"sifo/C");
422  rrtree.Branch("ifo",&xIFO,"ifo/I");
423  rrtree.Branch("nch",&nCH,"nch/I");
424  rrtree.Branch("chc50",CHC[0],"chc[nch]/D");
425  rrtree.Branch("chc30",CHC[1],"chc[nch]/D");
426  rrtree.Branch("chc10",CHC[2],"chc[nch]/D");
427  rrtree.Branch("chc01",CHC[3],"chc[nch]/D");
428  rrtree.Branch("chn",&pCHN);
429 
430  rrtree.Fill();
431  rrtree.Write();
432  ofile.Close();
433  }
434 
435  // end job - clean temporary files
436  if(ifo==cfg->ifo[xIFO]) {
437  cout << "remove temporary file ..." << endl;
438  TString jname = jfile->GetPath();
439  jname.ReplaceAll(":/","");
440  cout << jname.Data() << endl;
441  gSystem->Exec(TString("rm "+jname).Data());
442  cout << "end job" << endl;
443  gSystem->Exit(0);
444  }
445  }
446 
447  return;
448 }
CWB::config * cfg
Definition: TestCWB_Plugin.C:5
virtual size_t size() const
Definition: wavearray.hh:127
void setMatrix(double edge=0., double f=1.)
Definition: regression.cc:407
tuple f
Definition: cwb_online.py:91
TMacro configPlugin
Definition: config.hh:344
Definition: ced.hh:24
virtual void rate(double r)
Definition: wavearray.hh:123
float * rank
int n
Definition: cwb_net.C:10
cout<< "skymap size : "<< L<< endl;for(int l=0;l< L;l++) sm.set(l, l);sm > const_cast< char * >("skymap.dat")
TString("c")
size_t nRun
Definition: network.hh:554
cout<< endl;cout<< "ts size = "<< ts.size()<< " ts rate = "<< ts.rate()<< endl;tf.Forward(ts, wdm);int levels=tf.getLevel();cout<< "tf size = "<< tf.size()<< endl;double dF=tf.resolution();double dT=1./(2 *dF);cout<< "rate(hz) : "<< RATE<< "\t layers : "<< nLAYERS<< "\t dF(hz) : "<< dF<< "\t dT(ms) : "<< dT *1000.<< endl;int itime=TIME_PIXEL_INDEX;int ifreq=FREQ_PIXEL_INDEX;int index=(levels+1)*itime+ifreq;double time=itime *dT;double freq=(ifreq >0)?ifreq *dF:dF/4;cout<< endl;cout<< "PIXEL TIME = "<< time<< " sec "<< endl;cout<< "PIXEL FREQ = "<< freq<< " Hz "<< endl;cout<< endl;wavearray< double > x
Long_t size
size_t add(WSeries< double > &target, char *name, double fL=0., double fH=0.)
Definition: regression.cc:73
virtual void start(double s)
Definition: wavearray.hh:119
char ofile[512]
i drho i
char ifo[NIFO_MAX][8]
network ** net
NOISE_MDC_SIMULATION.
void setChName(TString chName)
Definition: frame.hh:102
double segEdge
Definition: config.hh:146
ofstream out
Definition: cwb_merge.C:196
wavearray< double > rank(int nbins=0, double fL=0., double fH=0.)
Definition: regression.cc:807
int getLevel()
Definition: wseries.hh:91
jfile
Definition: cwb_job_obj.C:25
wavearray< double > xx
Definition: TestFrame1.C:11
#define IMPORT(TYPE, VAR)
Definition: cwb.hh:51
i() int(T_cor *100))
char output_dir[1024]
Definition: config.hh:300
void apply(double threshold=0., char c='a')
Definition: regression.cc:691
int getLayer(wavearray< DataType_t > &w, double n)
param: n - layer number
Definition: wseries.cc:175
void CWB_Plugin(TFile *jfile, CWB::config *cfg, network *net, WSeries< double > *x, TString ifo, int type)
COHERENCE.
void solve(double th, int nE=0, char c='s')
Definition: regression.cc:592
size_t setFilter(size_t)
Definition: regression.cc:258
void setSRIndex(int srIndex)
Definition: frame.hh:96
wavearray< double > getClean()
Definition: regression.hh:117
double e
wavearray< double > yy
Definition: TestFrame5.C:12
void clear()
Definition: regression.hh:142
double Tb
ifstream in
virtual void stop(double s)
Definition: wavearray.hh:121
char ifo[NIFO_MAX][8]
Definition: config.hh:106
void Forward(int n=-1)
param: wavelet - n is number of steps (-1 means full decomposition)
Definition: wseries.cc:228
void unmask(int n, double flow=0., double fhigh=0.)
Definition: regression.cc:321
#define FRLIST_WITNESS
Definition: RegressionL1.C:2
void mask(int n, double flow=0., double fhigh=0.)
Definition: regression.cc:303
sprintf(tfres,"(1/%g)x(%g) (sec)x(Hz)", 2 *df, df)
int nIFO
Definition: config.hh:102
DataType_t * data
Definition: wavearray.hh:301
TString jname
double dT
Definition: testWDM_5.C:12
char data_label[1024]
Definition: config.hh:314
virtual void resize(unsigned int)
Definition: wavearray.cc:445
void Inverse(int n=-1)
param: n - number of steps (-1 means full reconstruction)
Definition: wseries.cc:273
void putLayer(wavearray< DataType_t > &, double n)
param: n - layer number
Definition: wseries.cc:201
int m_H
Definition: Wavelet.hh:103