Logo coherent WaveBurst  
Library Reference Guide
Logo
CWB_Plugin_LowLatency.C
Go to the documentation of this file.
1 /*
2 # Copyright (C) 2019 Gabriele Vedovato
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17 
18 
19 #define XIFO 4
20 
21 #pragma GCC system_header
22 
23 #include "cwb.hh"
24 #include "cwb2G.hh"
25 #include "config.hh"
26 #include "network.hh"
27 #include "wavearray.hh"
28 #include "TString.h"
29 #include "TObjArray.h"
30 #include "TObjString.h"
31 #include "TRandom.h"
32 #include "TComplex.h"
33 #include "TMath.h"
34 #include "mdc.hh"
35 #include "regression.hh"
36 #include <vector>
37 
38 
39 // regression parameters
40 #define REGRESSION_FILTER_LENGTH 8
41 #define REGRESSION_MATRIX_FRACTION 0.95
42 #define REGRESSION_SOLVE_EIGEN_THR 0.
43 #define REGRESSION_SOLVE_EIGEN_NUM 10
44 #define REGRESSION_SOLVE_REGULATOR 'h'
45 #define REGRESSION_APPLY_THR 0.8
46 
47 //#define SAVE_RMS
48 
49 // ---------------------------------------------------------------------------------
50 // WHAT IS?
51 // this plugin can be used for low latency analysis
52 // HOW TO CONFIGURE THE Low Latency PLUGIN
53 // the following is an example : must be included in the config/user_parameters.C file
54 // see the DumpUserOptions function for the full description of parameters
55 // ---------------------------------------------------------------------------------
56 /*
57  plugin = TMacro(gSystem->ExpandPathName("$HOME_CWB/plugins/CWB_Plugin_LowLatency.C")); // Macro source
58 
59  TString optll = ""; // NOTE : add space at the end of each line
60  optll += "ll_rmsfile=rmsfile.root "; // is the root file which contains the noise rms used for whitening data
61  optll += "ll_grms=true "; // if grms==true && rmsfile!="" -> RMS whitening is provided by the user by the rmsfile parameter
62  optll += "ll_fitsfile=fitsfile.fits "; // is the name of file where the fits skymap is saved -> fitsfile.fits
63 
64  strcpy(parPlugin,optll.Data()); // set LL plugin parameters
65  strcpy(comment,"LL configuration example");
66 
67 */
68 
69 // ---------------------------------------------------------------------------------
70 // DEFINES
71 // ---------------------------------------------------------------------------------
72 
73 #define LL_RMSFILE "" // is the root file which contains the noise rms used for whitening data
74  //
75 #define LL_FITSFILE "" // if fitsfile!="" the cWB fits skymap is saved into fitsfile.fits
76  //
77 #define LL_GRMS false // if grms==true && rmsfile!="" -> RMS whitening is provided by the user by the rmsfile parameter
78  // if grms==false && rmsfile!="" -> RMS whitening is saved into rmsfile
79 
80 // ---------------------------------------------------------------------------------
81 // USER CONFIG OPTIONS
82 // ---------------------------------------------------------------------------------
83 
84 struct uoptions {
85  TString rmsfile;
86  bool grms;
87  TString fitsfile;
88 };
89 
90 void ResetUserOptions();
94 
95 // ---------------------------------------------------------------------------------
96 // Global Variables
97 // ---------------------------------------------------------------------------------
98 
99 uoptions gOPT; // global User Options
100 
101 // ---------------------------------------------------------------------------------
102 // FUNCTIONS
103 // ---------------------------------------------------------------------------------
104 
105 void DumpSkymap(network* NET, int lag, netevent* EVT, int ID);
106 
107 
108 void
110 //!DATA_CONDITIONING
111 // This plugin shows how to implement the low latency pipeline
112 
113  cout << endl;
114  cout << "-----> CWB_Plugin_LowLatency.C" << endl;
115  cout << "ifo " << ifo.Data() << endl;
116  cout << "type " << type << endl;
117  cout << endl;
118 
119  if(type==CWB_PLUGIN_CONFIG) {
120 
121  ResetUserOptions(); // set default config options
122  ReadUserOptions(cfg->parPlugin); // user config options : read from parPlugin
123 
124  cfg->dcPlugin=true; // disable built in dc
125  }
126 
127  if(type==CWB_PLUGIN_NETWORK) {
128  PrintUserOptions(cfg); // print config options
129  }
130 
131  if(type==CWB_PLUGIN_DATA_CONDITIONING) {
132 
133  if(TString(cfg->analysis)!="2G") {
134  cout << "CWB_Plugin_LowLatency.C -> implemented only for 2G" << endl;
135  gSystem->Exit(1);
136  }
137 
138  // get data range
139  cwb2G* gCWB2G; IMPORT(cwb2G*,gCWB2G)
140 
143  // import global variables
144  size_t gRATEANA; IMPORT(size_t,gRATEANA)
145 
146  // get ifo id
147  int id=-1;
148  for(int n=0;n<cfg->nIFO;n++) if(ifo==net->ifoName[n]) {id=n;break;}
149  if(id<0) {cout << "CWB_Plugin_LowLatency.C : Error - bad ifo id" << endl; gSystem->Exit(1);}
150 
151  detector* pD = net->getifo(id);
152  WSeries<double>* pTF = pD->getTFmap();
154 
155  int layers = 0;
156  for(int level=cfg->l_high; level>=cfg->l_low; level--) {
157  for(int j=0;j<net->wdmMRA.nRes;j++)
158  if(layers<net->wdmMRA.layers[j]) layers=net->wdmMRA.layers[j];
159  }
160  WDM<double> WDMwhite(layers,layers,6,10); // set whitening WDM
161  layers = gRATEANA/8;
162  WDM<double> WDMlpr(layers,layers,6,10); // set LPE filter WDM
163 
164  // regression
165  pTF->Forward(*hot,WDMlpr);
166  regression rr(*pTF,const_cast<char*>("target"),1.,cfg->fHigh);
167  rr.add(*hot,const_cast<char*>("target"));
172  *hot = rr.getClean();
173 
174  // whitening
175  pTF->Forward(*hot,WDMwhite);
176  pTF->setlow(cfg->fLow);
177  pTF->sethigh(cfg->fHigh);
178 
179  if(gOPT.grms) {
180 
181  if(gOPT.rmsfile!="") { // RMS whitening is loaded from root file
182 
183  cout << "CWB_Plugin_LowLatency.C -> LOAD WHITENING FILE: " << gOPT.rmsfile << endl;
184 
185  TFile *frms = new TFile(gOPT.rmsfile);
186  if(frms==NULL) {
187  cout << "CWB_Plugin_LowLatency.C -> Failed to open RMS whitening file " << gOPT.rmsfile << endl;
188  gSystem->Exit(1);
189  }
190 
191  frms->ls();
192 
193  WSeries<double>* wrms = (WSeries<double>*)frms->Get(ifo);
194  if(wrms==NULL) {
195  cout << "CWB_Plugin_LowLatency.C -> Object RMS doesn't exist !!! " << endl;
196  gSystem->Exit(1);
197  }
198  pD->nRMS = *wrms;
199 
200  int levels = pD->nRMS.getLevel()+1; // number of levels
201  int slices = pD->nRMS.size()/levels; // number of nRMS samples
202  double length = slices*cfg->whiteStride; // nRMS len in sec
203 
204  double tcenter = (x->start()+x->stop())/2.;
205  double rms_start = tcenter - length/2.;
206 
207  pD->nRMS.start(tcenter);
208 // pD->nRMS.start(rms_start);
209  delete wrms;
210  }
211 
212  } else {
213 
214  pD->white(cfg->whiteWindow,0,cfg->segEdge,cfg->whiteStride); // calculate noise rms
215  pD->nRMS.bandpass(16.,0.,1); // high pass filtering at 16Hz
216 
217  if(gOPT.rmsfile!="") { // RMS whitening is saved to root file
218 
219  cout << "CWB_Plugin_LowLatency.C -> SAVE WHITENING FILE: " << gOPT.rmsfile << endl;
220 
221  // save nRMS
222  TFile *frms = id==0 ? new TFile(gOPT.rmsfile, "RECREATE") : new TFile(gOPT.rmsfile, "UPDATE");
223  if(frms==NULL) {
224  cout << "CWB_Plugin_LowLatency.C -> Failed to create file !!! " << endl;
225  gSystem->Exit(1);
226  }
227  pD->nRMS.Write(ifo);
228  frms->Close();
229  }
230  }
231 
232  pTF->white(pD->nRMS,1); // whiten 0 phase WSeries
233  pTF->white(pD->nRMS,-1); // whiten 90 phase WSeries
234 
235  WSeries<double> wtmp = *pTF;
236  pTF->Inverse();
237  wtmp.Inverse(-2);
238  *hot = *pTF;
239  *hot += wtmp;
240  *hot *= 0.5;
241  // add infos to history
242  char info[256];
243  sprintf(info,"-IFO:%d-RMS:%g",id,hot->rms());
244  gCWB2G->PrintAnalysisInfo(CWB_STAGE_CSTRAIN,"cwb2G::DataConditioning",info,false);
245  }
246 
247  if(type==CWB_PLUGIN_OLIKELIHOOD) { // AFTER EVENT RECONSTRUCTION
248 
249  // import ifactor
250  int gIFACTOR=-1; IMPORT(int,gIFACTOR)
251  double factor = cfg->simulation==3||cfg->simulation==4 ? -gIFACTOR : cfg->factors[gIFACTOR];
252  double ofactor=0;
253  if(cfg->simulation==4) ofactor=-gIFACTOR;
254  else if(cfg->simulation==3) ofactor=-gIFACTOR;
255  else ofactor=factor;
256 
257  int nIFO = net->ifoListSize(); // number of detectors
258  int K = net->nLag; // number of time lag
259  int rate = 0; // select all resolutions
260  netevent* EVT = new netevent(nIFO);;
262 
263  for(int k=0; k<K; k++) { // loop over the lags
264 
265  id = net->getwc(k)->get(const_cast<char*>("ID"), 0, 'L', rate);
266 
267  for(int j=0; j<(int)id.size(); j++) { // loop over cluster index
268 
269  int ID = size_t(id.data[j]+0.5);
270 
271  if(net->getwc(k)->sCuts[ID-1]!=-1) continue; // skip rejected/processed clusters
272 
273  EVT->output2G(NULL,net,ID,k,ofactor); // get reconstructed parameters
274 
275  // print event parameters
276  cout << endl;
277  cout << "CWB_Plugin_WF - event parameters : ID -> " << ID << endl;
278  for(int n=0;n<nIFO;n++) printf("rec_time %s : %.4f\n",net->ifoName[n], EVT->time[n]);
279  cout << "rec_theta : " << EVT->theta[0] << " rec_phi : " << EVT->phi[0] << endl;
280  cout << "SNRnet : " << sqrt(EVT->likelihood) << " netcc[0] : " << EVT->netcc[0]
281  << " rho[0] : " << EVT->rho[0] << " size : " << EVT->size[0] << endl;
282 
283  if(gOPT.fitsfile!="") DumpSkymap(net, k, EVT, ID); // save skymap to fits file
284  }
285  }
286  }
287 
288  return;
289 }
290 
292 
293  cout << "-----------------------------------------" << endl;
294  cout << "LowLatency config options " << endl;
295  cout << "-----------------------------------------" << endl << endl;
296  cout << "LL_RMSFILE " << gOPT.rmsfile << endl;
297  cout << "LL_GRMS " << gOPT.grms << endl;
298  cout << "LL_FITSFILE " << gOPT.fitsfile << endl;
299 
300  cout << endl;
301 }
302 
304 
305  TString ofName = odir+"/ll_config.txt";
306 
307  ofstream out;
308  out.open(ofName,ios::out);
309  if(!out.good()) {cout << "DumpUserOptions : Error Opening File : " << ofName << endl;exit(1);}
310 
311  TString info="";
312  TString tabs="\t\t\t\t";
313 
314  char version[128];
315  sprintf(version,"WAT Version : %s - SVN Revision : %s - Tag/Branch : %s",watversion('f'),watversion('r'),watversion('b'));
316 
317  out << endl;
318  out << "--------------------------------" << endl;
319  out << "LL config options " << endl;
320  out << "--------------------------------" << endl;
321  out << endl;
322 
323  out << "ll_grms " << gOPT.grms << endl;
324  out << tabs << info << endl;
325 
326  out << "ll_rmsfile " << gOPT.rmsfile << endl;
327  out << tabs << info << endl;
328 
329  out << "ll_fitsfile " << gOPT.fitsfile << endl;
330  out << tabs << info << endl;
331 
332  out.close();
333 }
334 
336 
337  gOPT.rmsfile = LL_RMSFILE;
338  gOPT.grms = LL_GRMS;
339  gOPT.fitsfile = LL_FITSFILE;
340 }
341 
343 
344  if(options.CompareTo("")!=0) {
345  cout << options << endl;
346  if(!options.Contains("--")) { // parameters are used only by cwb_inet
347 
348  TObjArray* token = TString(options).Tokenize(TString(' '));
349  for(int j=0;j<token->GetEntries();j++){
350 
351  TObjString* tok = (TObjString*)token->At(j);
352  TString stok = tok->GetString();
353 
354  if(stok.Contains("ll_rmsfile=")) {
355  TString rmsfile=stok;
356  rmsfile.Remove(0,rmsfile.Last('=')+1);
357  gOPT.rmsfile=rmsfile;
358  }
359 
360  if(stok.Contains("ll_grms=")) {
361  TString grms=stok;
362  grms.Remove(0,grms.Last('=')+1);
363  gOPT.grms=grms;
364  }
365 
366  if(stok.Contains("ll_fitsfile=")) {
367  TString fitsfile=stok;
368  fitsfile.Remove(0,fitsfile.Last('=')+1);
369  gOPT.fitsfile=fitsfile;
370  }
371  }
372  }
373  }
374 }
375 
376 void
377 DumpSkymap(network* NET, int lag, netevent* EVT, int ID) {
378 
379  // Dump2fits probability skymap (healpix)
380  skymap skyprobcc = NET->getifo(0)->tau;
381  skyprobcc=0.;
382  skymap skyprob = skyprobcc;
383  skyprob=1.e-12;
384 
385  std::vector<float>* vP;
386  std::vector<int>* vI;
387 
388  vP = &(NET->wc_List[lag].p_Map[ID-1]);
389  vI = &(NET->wc_List[lag].p_Ind[ID-1]);
390  double th,ph,ra;
391  int k;
392  for(int j=0; j<int(vP->size()); j++) {
393  int i = (*vI)[j];
394  th = skyprob.getTheta(i);
395  ph = skyprob.getPhi(i);
396 
397  k=skyprob.getSkyIndex(th, ph);
398  skyprob.set(k,(*vP)[j]);
399 
400  ra = skyprob.getRA(i);
401  k=skyprob.getSkyIndex(th, ra);
402  skyprobcc.set(k,(*vP)[j]);
403  }
404 
405  skyprobcc.Dump2fits(const_cast<char*>(gOPT.fitsfile.Data()),EVT->time[0],const_cast<char*>(""),const_cast<char*>("PROB"),const_cast<char*>("pix-1"),'C');
406 
407  return;
408 }
409 
monster wdmMRA
list of pixel pointers for MRA
Definition: network.hh:651
std::vector< char * > ifoName
Definition: network.hh:609
void sethigh(double f)
Definition: wseries.hh:132
detector * getifo(size_t n)
param: detector index
Definition: network.hh:436
CWB::config * cfg
char analysis[8]
Definition: config.hh:117
size_t nLag
Definition: network.hh:573
void white(double dT=0, int wtype=1, double offset=0., double stride=0.)
what it does: see algorithm description in wseries.hh
Definition: detector.hh:258
Float_t * rho
biased null statistics
Definition: netevent.hh:112
TString ofName
int slices
void setMatrix(double edge=0., double f=1.)
Definition: regression.cc:425
std::vector< netcluster > wc_List
Definition: network.hh:610
void PrintUserOptions(CWB::config *cfg)
float factor
#define REGRESSION_APPLY_THR
int n
Definition: cwb_net.C:28
char * watversion(char c='s')
Definition: watversion.hh:21
TString("c")
void bandpass(wavearray< DataType_t > &ts, double flow, double fhigh, int n=-1)
Definition: wseries.cc:313
int ID
Definition: TestMDC.C:70
Int_t * size
cluster volume
Definition: netevent.hh:80
uoptions gOPT
ofstream out
Definition: cwb_merge.C:214
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<< "\ layers : "<< nLAYERS<< "\ dF(hz) : "<< dF<< "\ 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
wavearray< double > get(char *name, size_t index=0, char atype='R', int type=1, bool=true)
param: string with parameter name param: index in the amplitude array, which define detector param: c...
Definition: netcluster.cc:2207
double whiteWindow
Definition: config.hh:189
double fLow
Definition: config.hh:140
char odir[1024]
TH2F * ph
bool dcPlugin
Definition: config.hh:366
void setlow(double f)
Definition: wseries.hh:125
int layers
void ReadUserOptions(TString options)
double getTheta(size_t i)
Definition: skymap.hh:224
Long_t size
size_t add(WSeries< double > &target, char *name, double fL=0., double fH=0.)
Definition: regression.cc:91
virtual void start(double s)
Definition: wavearray.hh:137
int j
Definition: cwb_net.C:28
i drho i
skymap tau
Definition: detector.hh:346
#define REGRESSION_SOLVE_EIGEN_NUM
#define REGRESSION_MATRIX_FRACTION
char ifo[NIFO_MAX][8]
double Edge
Definition: network.hh:578
Definition: cwb2G.hh:33
network ** net
NOISE_MDC_SIMULATION.
size_t ifoListSize()
Definition: network.hh:431
virtual double rms()
Definition: wavearray.cc:1206
double segEdge
Definition: config.hh:164
#define nIFO
#define REGRESSION_SOLVE_REGULATOR
size_t getSkyIndex(double th, double ph)
param: theta param: phi
Definition: skymap.cc:720
virtual size_t size() const
Definition: wavearray.hh:145
int getLevel()
Definition: wseries.hh:109
double ra
Definition: ConvertGWGC.C:46
jfile
Definition: cwb_job_obj.C:43
void output2G(TTree *, network *, size_t, int, double)
Definition: netevent.cc:711
int simulation
Definition: config.hh:199
int l_high
Definition: config.hh:156
void PrintAnalysisInfo(CWB_STAGE stage, TString comment, TString info, bool out=true, bool log=true)
Definition: cwb.cc:2129
wavearray< double > hot[2]
#define IMPORT(TYPE, VAR)
Definition: cwb.hh:69
WSeries< double > nRMS
Definition: detector.hh:358
double getRA(size_t i)
Definition: skymap.hh:215
i() int(T_cor *100))
network NET
Definition: cwb_dump_inj.C:30
void apply(double threshold=0., char c='a')
Definition: regression.cc:709
#define LL_FITSFILE
char parPlugin[1024]
Definition: config.hh:363
WSeries< double > pTF[nRES]
Definition: revMonster.cc:8
printf("total live time: non-zero lags = %10.1f \, liveTot)
int gRATEANA
int k
void solve(double th, int nE=0, char c='s')
Definition: regression.cc:610
Float_t * netcc
effective correlated SNR
Definition: netevent.hh:113
TObjArray * token
void DumpUserOptions(TString odir, CWB::config *cfg)
size_t setFilter(size_t)
Definition: regression.cc:276
Definition: skymap.hh:63
wavearray< double > getClean()
Definition: regression.hh:135
WSeries< double > wM
Definition: cwb_job_obj.C:41
void CWB_Plugin(TFile *jfile, CWB::config *cfg, network *net, WSeries< double > *x, TString ifo, int type)
COHERENCE.
void DumpSkymap(network *NET, int lag, netevent *EVT, int ID)
int l_low
Definition: config.hh:155
#define LL_RMSFILE
char options[256]
double getPhi(size_t i)
Definition: skymap.hh:164
Float_t * phi
sqrt(h+*h+ + hx*hx)
Definition: netevent.hh:87
netevent EVT(itree, nifo)
Float_t * theta
[0]-reconstructed, [1]-injected phi angle, [2]-RA
Definition: netevent.hh:88
Double_t * time
beam pattern coefficients for hx
Definition: netevent.hh:94
double whiteStride
Definition: config.hh:190
void ResetUserOptions()
std::vector< int > sCuts
Definition: netcluster.hh:392
double fHigh
Definition: config.hh:141
WSeries< double > * getTFmap()
param: no parameters
Definition: detector.hh:179
virtual void stop(double s)
Definition: wavearray.hh:139
int * layers
Definition: monster.hh:121
int gIFACTOR
void Forward(int n=-1)
param: wavelet - n is number of steps (-1 means full decomposition)
Definition: wseries.cc:246
Float_t likelihood
Definition: netevent.hh:124
netcluster * getwc(size_t n)
param: delay index
Definition: network.hh:439
sprintf(tfres,"(1/%g)x(%g) (sec)x(Hz)", 2 *df, df)
int nRes
Definition: monster.hh:117
void set(size_t i, double a)
param: sky index param: value to set
Definition: skymap.hh:122
int nIFO
Definition: config.hh:120
Long_t id
double factors[FACTORS_MAX]
Definition: config.hh:202
#define REGRESSION_SOLVE_EIGEN_THR
#define LL_GRMS
string version
Definition: cWB_conf.py:108
double length
Definition: TestBandPass.C:18
void Inverse(int n=-1)
param: n - number of steps (-1 means full reconstruction)
Definition: wseries.cc:291
wavearray< double > y
Definition: Test10.C:31
detector ** pD
exit(0)
virtual WSeries< double > white(double, int, double=0., double=0.)
what it does: each wavelet layer is devided into k intervals.
Definition: wseries.cc:1146
#define REGRESSION_FILTER_LENGTH