Logo coherent WaveBurst  
Library Reference Guide
Logo
CWB_Plugin_WF.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 #define _USE_HEALPIX
24 
25 #include "cwb.hh"
26 #include "config.hh"
27 #include "network.hh"
28 #include "wavearray.hh"
29 #include "TString.h"
30 #include "TObjArray.h"
31 #include "TObjString.h"
32 #include "TRandom.h"
33 #include "TComplex.h"
34 #include "TGraphAsymmErrors.h"
35 #include "TMath.h"
36 #include "mdc.hh"
37 #include "cwb2G.hh"
38 #include "watplot.hh"
39 #include "gwavearray.hh"
40 #include "network.hh"
41 #include "gskymap.hh"
42 #include <fstream>
43 #include <vector>
44 
45 // ---------------------------------------------------------------------------------
46 // HOW TO SETUP PLUGIN WF IN CWB USER CONFIGURATION (EXAMPLE)
47 // ---------------------------------------------------------------------------------
48 
49 /*
50  TString optwf = ""; // NOTE : add space at the end of each line
51 
52  optwf += "wf_output_disable=root "; // disable output root file (to be used when QLveto is enabled)
53  optwf += "wf_output_disable=inj "; // disable save injection to the output root file
54  optwf += "wf_output_enable=rec "; // enable save reconstructed waveform to the output root file
55  optwf += "wf_output_disable=wht "; // disable save whitened data to the output root file
56  optwf += "wf_output_disable=dat "; // disable save rec+null data to the output root file
57  optwf += "wf_output_disable=nul "; // disable save null data to the output root file
58  optwf += "wf_inj_tstep=150.000 "; // is the the injection step time
59 
60  strcpy(parPlugin,optwf.Data()); // set WF plugin parameters
61 */
62 
63 // ---------------------------------------------------------------------------------
64 // DEFINES
65 // ---------------------------------------------------------------------------------
66 
67 #define WF_VERSION 1.2
68 
69 #define WF_OUTPUT_ROOT false
70 
71 #define WF_OUTPUT_INJ false // save injected into the output root file
72 #define WF_OUTPUT_REC true // save reconstructed into the output root file
73 #define WF_OUTPUT_WHT false // save whitened data into the output root file
74 #define WF_OUTPUT_DAT false // save rec+null data into the output root file
75 #define WF_OUTPUT_NUL false // save null data into the output root file
76 
77 #define WF_OUTPUT_STRAIN false // save full strain data buffer into the output root file
78 #define WF_OUTPUT_MDC false // save full strain mdc injections data buffer into the output root file
79 #define WF_OUTPUT_CSTRAIN false // save full cstrain data buffer into the output root file
80 
81 #define WF_OUTPUT_CLUSTER false // save event crate/cluster
82 #define WF_OUTPUT_POL false // save event polarization components (dual stream polarization frame)
83 
84 #define WF_FITS_PE ""
85 #define WF_GPS_PE 0
86 
87 #define WF_FITS_OS ""
88 #define WF_GPS_OS 0
89 
90 #define WF_INJ_TSTEP 0 // is the injection step time
91 #define WF_INJ_TOFF 0 // is time offset (sec) of periodic time step injections
92 
93 //#define PLOT_INJ
94 //#define TEST1
95 //#define TEST2
96 //#define TEST3
97 //#define DUMP_FITS
98 
99 #define FRACTION 0.01
100 
101 // ---------------------------------------------------------------------------------
102 // USER SGW PLUGIN OPTIONS
103 // ---------------------------------------------------------------------------------
104 
105 struct uoptions {
106  bool output_root; // (def=false) set to false if this plugin is used with QLveto (QLveto plugin output par to output root file)
107  // NOTE: QLveto plugin must be declared as the last one in cwb_mplugin
108  bool output_inj; // save injected into the output root file
109  bool output_rec; // (def=true) save reconstructed into the output root file
110  bool output_wht; // save whitened data into the output root file
111  bool output_dat; // save rec+null data into the output root file
112  bool output_nul; // save null data into the output root file
113 
114  bool output_strain; // save full strain data buffer into the output root file
115  bool output_mdc; // save full strain mdc injections data buffer into the output root file
116  bool output_cstrain;// save full cstrain data buffer into the output root file
117 
118  bool output_cluster;// save event crate/cluster into the output root file
119  bool output_pol; // save event polarization components into the output root file
120 
121  TString fits_pe; // fits file name -> reference skymap: Parameter Estimation
122  double gps_pe; // gps Parameter Estimation gps time
123 
124  TString fits_os; // fits file name -> reference skymap: cWB On Source
125  double gps_os; // gps On Source gps time
126 
127  double inj_tstep; // is the injection time step
128  double inj_toff; // is time offset (sec) of periodic time step injections
129 };
130 
131 // ----------------------------------------------------
132 // ROOT Output WF Parameters
133 // ----------------------------------------------------
134 
135 struct WF { // structure for output root parameters
136 
137  wavearray<double>* wINJ[NIFO_MAX]; // whiten injected waveforms
138  wavearray<double>* wAUX[NIFO_MAX]; // whiten auxiliary injected waveforms
139  wavearray<double>* cINJ[NIFO_MAX]; // injected (cutted) on TF sub-space of the reconstructed waveform vREC
140  wavearray<double>* wREC[NIFO_MAX]; // whiten reconstructed waveforms
141  wavearray<double>* wWHT[NIFO_MAX]; // whiten data (in the waveform time range)
142  wavearray<double>* wDAT[NIFO_MAX]; // whiten rec+noise data (in the waveform time range)
143  wavearray<double>* wNUL[NIFO_MAX]; // whiten null data (in the waveform time range)
144 
145  std::vector<netpixel>* pCLUSTER; // event netcluster pointer
146 
147  wavearray<double> rPOL00; // polarization of 00 amplitudes -> radial
148  wavearray<double> aPOL00; // polarization of 00 amplitudes -> angle
149  wavearray<double> rPOL90; // polarization of 90 amplitudes -> radial
150  wavearray<double> aPOL90; // polarization of 90 amplitudes -> angle
151 };
152 
153 // ---------------------------------------------------------------------------------
154 // Global Variables
155 // ---------------------------------------------------------------------------------
156 
157 uoptions gOPT; // global User Options
158 TTree* gTREE; // output tree file name
159 TString gOUTPUT; // output root file name
160 WF gWF; // global ROOT Output WF Parameters
161 double gSEGGPS; // segment start time
162 skymap gSM_PE; // reference skymap: Parameter Estimation
163 skymap gSM_OS; // reference skymap: cWB On Source
164 float gSM_PE_OF; // skymap PE overlap factor
165 float gSM_OS_OF; // skymap OS overlap factor
166 bool gCEDDUMP; // save user_parameters cedDump
167 float gFF[3]; // Fitting Factor W1,W2 0/1/2 -> full/left/right
168 float gOF[3]; // Overlap Factor W1,W2 0/1/2 -> full/left/right
169 float gRE[3]; // residual energy W1-W2 0/1/2 -> full/left/right
170 
171 wavearray<double> gSTRAIN[NIFO_MAX]; // HoT time series used to save strain data
172 wavearray<double> gMDC[NIFO_MAX]; // HoT time series used to save mdc strain data
173 wavearray<double> gCSTRAIN[NIFO_MAX]; // HoT time series used to save whitened data
174 
175 wavearray<double> gHOT[NIFO_MAX]; // HoT time series used to save whitened data
176 
177 double gTCOA; // Coalescence Time
178 std::vector<double> gvTCOA; // vector of gTCOA: contains the gTCOA of all injections in the job
179 string gLOG; // List of input parameters used to create the injected waveform
180 std::vector<string> gvLOG; // vector of gLOG: contains the gLOG of all injections in the job
181 
182 float gCRATE; // event netcluster::rate
183 std::vector<netpixel> gCLUSTER; // event netcluster
184 
185 // ---------------------------------------------------------------------------------
186 // waveforms (vectors index is ifos)
187 // ---------------------------------------------------------------------------------
188 
189 std::vector<wavearray<double> > vINJ; // injected (full)
190 std::vector<wavearray<double> > vAUX; // auxiliary injected (full)
191 std::vector<wavearray<double> > cINJ; // injected (cutted) on TF sub-space of the reconstructed waveform vREC
192 std::vector<wavearray<double> > vREC; // signal reconstructed by wave packet
193 std::vector<wavearray<double> > vWHT; // whitened data (in the vREC time range)
194 std::vector<wavearray<double> > vDAT; // noise+signal reconstructed by wave packet
195 std::vector<wavearray<double> > vNUL; // vDAT-vREC
196 std::vector<wavearray<double> > vRES; // vREC-cINJ
197 
198 // ---------------------------------------------------------------------------------
199 // sparse maps
200 // ---------------------------------------------------------------------------------
201 
202 std::vector<SSeries<double> > vSS[NIFO_MAX]; // original sparse maps
203 std::vector<SSeries<double> > jSS[NIFO_MAX]; // injected (only TF sub-space of the reconstructed waveform vREC)
204 std::vector<SSeries<double> > rSS[NIFO_MAX]; // reconstructed
205 std::vector<SSeries<double> > dSS[NIFO_MAX]; // noise+signal reconstructed by wave packet
206 std::vector<SSeries<double> > xSS[NIFO_MAX]; // sparse map -> vRES=vREC-cINJ
207 
208 // ---------------------------------------------------------------------------------
209 // FUNCTIONS
210 // ---------------------------------------------------------------------------------
211 
212 void ResetUserOptions();
215 
217 void ClearVectors();
218 
219 void SetOutputFile(network* NET, netevent* &EVT, CWB::config* cfg, bool dump_wf);
220 void DumpOutputFile(network* NET, netevent* &EVT, CWB::config* cfg, int ID, int k, int factor);
221 
222 std::vector<netpixel> GetCluster(network* NET, int lag, int id);
223 
224 std::vector<wavearray<double> > GetWaveform(network* NET, int lag, int id, char type, bool shift=true);
225 std::vector<wavearray<double> > GetWhitenedData(network* NET, CWB::config* cfg);
226 std::vector<wavearray<double> > GetInjWaveform(network* NET, netevent* EVT, int id, double factor);
227 std::vector<wavearray<double> > GetAuxWaveform(network* NET, netevent* EVT, int id, double factor);
228 std::vector<wavearray<double> > GetRecWaveform(network* NET, netevent* EVT, int id);
229 
230 std::vector<wavearray<double> > GetSigWaveform(network* NET, CWB::config* cfg, int lag, int id);
231 void Wave2Sparse(network* NET, CWB::config* cfg, char type);
232 double GetSparseMapData(SSeries<double>* SS, bool phase, int index);
233 
234 void DumpSkymap(network* NET, int lag, netevent* EVT, int ID);
235 
238  bool fft=false, TString pdir="", double P=0.99, EColor col1=kRed, EColor col2=kBlue, EColor col3=(EColor)418);
239 
240 skymap ReadSkyMap(TString fitsFile, CWB::config* cfg, double gps);
241 double ComputeSkyMapOF(skymap* sm1, skymap* sm2);
243 skymap GetSkyMap(network* NET, int lag, netevent* EVT, int ID);
245 
246 std::vector<netpixel> SavePixels(network* NET, CWB::config* cfg, int lag, int id);
247 void RestorePixels(network* NET, CWB::config* cfg, netcluster* pwc, std::vector<netpixel>* vPIX, int id);
248 
249 double GetInjTcoa(double geocentric_tcoa, network* NET, TString ifo, double theta, double phi);
250 
251 void
253 //!MISCELLANEA
254 // Plugin used to save inj/rec/wht waveforms to output ROOT file
255 
256  if(type==CWB_PLUGIN_CONFIG) {
257 
258  ResetUserOptions(); // set default config options
259  ReadUserOptions(cfg->parPlugin); // user config options : read from parPlugin
260 
261  cfg->outPlugin=true; // disable built-in output root file
262 
263  gCEDDUMP=cfg->cedDump; // save user_parameter cedDump
264 
265  if(gOPT.fits_pe!="" && gOPT.gps_pe<=0) {
266  cout<< "CWB_Plugin_xWF - Error : plugin parameter wf_gps_pe not defined" << endl;exit(1);
267  }
268  if(gOPT.fits_os!="" && gOPT.gps_os<=0) {
269  cout<< "CWB_Plugin_xWF - Error : plugin parameter wf_gps_os not defined" << endl;exit(1);
270  }
271  if(gOPT.inj_tstep<=0) {
272  cout<< "CWB_Plugin_xWF - Error : plugin parameter inj_tstep not defined" << endl;exit(1);
273  }
274 
275  if(gOPT.fits_pe!="") {CWB::Toolbox::checkFile(gOPT.fits_pe); gSM_PE = ReadSkyMap(gOPT.fits_pe, cfg, gOPT.gps_pe);}
276  if(gOPT.fits_os!="") {CWB::Toolbox::checkFile(gOPT.fits_os); gSM_OS = ReadSkyMap(gOPT.fits_os, cfg, gOPT.gps_os);}
277  }
278 
279  if(type==CWB_PLUGIN_NETWORK) {
280  PrintUserOptions(cfg); // print config options
281  }
282 
283  if(type==CWB_PLUGIN_STRAIN) {
284  // save strain
285  int ifoID =0; for(int n=0;n<cfg->nIFO;n++) if(ifo==NET->getifo(n)->Name) {ifoID=n;break;}
286  if(gOPT.output_strain) {
287  gSTRAIN[ifoID] = *x;
288  if(cfg->fResample>0) gSTRAIN[ifoID].Resample(cfg->fResample); // RESAMPLING
289  gSTRAIN[ifoID].Resample(gSTRAIN[ifoID].rate()/(1<<cfg->levelR)); // resampling
290  }
291  }
292 
293  if(type==CWB_PLUGIN_MDC) {
294  // save mdc strain data
295  int ifoID =0; for(int n=0;n<cfg->nIFO;n++) if(ifo==NET->getifo(n)->Name) {ifoID=n;break;}
296  if(gOPT.output_mdc) {
297  gMDC[ifoID] = *x;
298  if(cfg->fResample>0) gMDC[ifoID].Resample(cfg->fResample); // RESAMPLING
299  gMDC[ifoID].Resample(gMDC[ifoID].rate()/(1<<cfg->levelR)); // resampling
300  }
301  }
302 
304  // save whitened HoT
305  int ifoID =0; for(int n=0;n<cfg->nIFO;n++) if(ifo==NET->getifo(n)->Name) {ifoID=n;break;}
306  if(gOPT.output_wht) gHOT[ifoID] = *x;
307  if(gOPT.output_cstrain) gCSTRAIN[ifoID] = *x;
308  }
309 
310  if(type==CWB_PLUGIN_MDC && cfg->simulation) {
311  // save coalescence times
312  for(int k=0;k<(int)NET->mdcTime.size();k++) gvTCOA.push_back(NET->mdcTime[k]);
313  // save logs
314  for(int k=0;k<(int)NET->mdcList.size();k++) {
315  gvLOG.push_back(NET->mdcList[k]);
316  gvLOG[k].erase(std::remove(gvLOG[k].begin(), gvLOG[k].end(), '\n'), gvLOG[k].end()); // remove new line
317  }
318  }
319 
320  if(type==CWB_PLUGIN_XLIKELIHOOD) { // BEFORE EVENT RECONSTRUCTION
321  cfg->cedDump = true; // enable skymaps filling
322  }
323 
324  if(type==CWB_PLUGIN_ILIKELIHOOD) { // DEFINE WAVE TREE - FIX CASE WHEN NO EVENTS HAVE BEEN FOUND !!!
325  netevent* EVT;
326  SetOutputFile(NET, EVT, cfg, false); // set default output waveburst tree in root file
327  }
328 
329  if(type==CWB_PLUGIN_OLIKELIHOOD) { // AFTER EVENT RECONSTRUCTION
330 
331  cfg->cedDump=gCEDDUMP; // restore user_parameter cedDump
332 
333  cout << "CWB_Plugin_WF.C " << endl;
334 
335  ClearVectors();
336 
337  // import ifactor
338  int gIFACTOR=-1; IMPORT(int,gIFACTOR)
339  double factor = cfg->simulation==3||cfg->simulation==4 ? -gIFACTOR : cfg->factors[gIFACTOR];
340  double ofactor=0;
341  if(cfg->simulation==4) ofactor=-gIFACTOR;
342  else if(cfg->simulation==3) ofactor=-gIFACTOR;
343  else ofactor=factor;
344 
345  int nIFO = NET->ifoListSize(); // number of detectors
346  int K = NET->nLag; // number of time lag
347  int rate = 0; // select all resolutions
348  netevent* EVT;
350 
351  SetOutputFile(NET, EVT, cfg, false); // set output root file
352 
353  injection INJ(nIFO);
354 
355  for(int k=0; k<K; k++) { // loop over the lags
356 
357  id = NET->getwc(k)->get(const_cast<char*>("ID"), 0, 'L', rate);
358 
359  for(int j=0; j<(int)id.size(); j++) { // loop over cluster index
360 
361  int ID = size_t(id.data[j]+0.5);
362 
363  if(NET->getwc(k)->sCuts[ID-1]!=-1) continue; // skip rejected/processed clusters
364 
365  EVT->output2G(NULL,NET,ID,k,ofactor); // get reconstructed parameters
366 
367  // get coalescence time
368  if(cfg->simulation) {
369  double minDT = 1.e12;
370  int injID = -1;
371  int M = NET->mdc__IDSize();
372  for(int m=0; m<M; m++) {
373  int mdcID = NET->getmdc__ID(m);
374  double DT = fabs(EVT->time[0] - NET->getmdcTime(mdcID));
375  if(DT<minDT && INJ.fill_in(NET,mdcID)) {
376  minDT = DT;
377  injID = mdcID;
378  }
379  }
380  gTCOA = gvTCOA[injID];
381  gLOG = gvLOG[injID];
382  }
383 
384  // print event parameters
385  cout << endl;
386  cout << "CWB_Plugin_WF - event parameters : ID -> " << ID << endl;
387  for(int n=0;n<nIFO;n++) printf("rec_time %s : %.4f\n",NET->ifoName[n], EVT->time[n]);
388  cout << "rec_theta : " << EVT->theta[0] << " rec_phi : " << EVT->phi[0] << endl;
389  cout << "SNRnet : " << sqrt(EVT->likelihood) << " netcc[0] : " << EVT->netcc[0]
390  << " rho[0] : " << EVT->rho[0] << " size : " << EVT->size[0] << endl;
391 
392  // save event parameters
393  gSEGGPS = EVT->gps[0]; // save segment start time (sec)
394 
395  skymap skyprob = GetSkyMap(NET, k, EVT, ID);
396  skyprob = EarthCoordinatesSM(&skyprob, EVT->time[0]);
397  if(gOPT.fits_pe!="") {
398  gSM_PE_OF = ComputeSkyMapOF(&skyprob, &gSM_PE);
399  cout << "SKYMAP PE Overlap Factor = " << gSM_PE_OF << endl;
400  }
401  if(gOPT.fits_os!="") {
402  gSM_OS_OF = ComputeSkyMapOF(&skyprob, &gSM_OS);
403  cout << "SKYMAP OS Overlap Factor = " << gSM_OS_OF << endl;
404  }
405 
406  cout << "MAX SKYMAP Correlation " << NET->nCorrelation.max() << endl;
407  double maxcc = ComputeMaxSkyMapProduct(&NET->nCorrelation, &NET->nProbability);
408  cout << "MAX SKYMAP Correlation*CumProbability " << maxcc << endl;
409  if(gOPT.fits_pe!="") {
410  double maxcc_pe = ComputeMaxSkyMapProduct(&NET->nCorrelation, &gSM_PE);
411  cout << "MAX SKYMAP Correlation*CumProbability PE " << maxcc_pe << endl;
412  }
413  if(gOPT.fits_os!="") {
414  double maxcc_os = ComputeMaxSkyMapProduct(&NET->nCorrelation, &gSM_OS);
415  cout << "MAX SKYMAP Correlation*CumProbability OS " << maxcc_os << endl;
416  }
417 
418 #ifdef DUMP_FITS
419  DumpSkymap(NET, k, EVT, ID); // save skymap to fits file
420 #endif
421 
422  // get waveforms
423  if(cfg->simulation) {
424  vINJ = GetInjWaveform(NET, EVT, ID, factor); // get injected waveform
425  if(vINJ.size()!=nIFO) {
426  cout << "CWB_Plugin_WF Error : Injection Waveform Not Found !!!" << endl;
427  exit(1);
428  }
429  vAUX = GetAuxWaveform(NET, EVT, ID, factor); // get injected waveform
430  }
431  vREC = GetWaveform(NET, k, ID, 'S'); // get reconstructed waveform
432  vWHT = GetWhitenedData(NET, cfg); // get whitened data (in the vREC time range)
433  vDAT = GetWaveform(NET, k, ID, 'W'); // get reconstructed+noise waveform
434  vNUL = GetWaveform(NET, k, ID, 'N'); // get noise-reconstructed waveform
435  for(int i=0; i<nIFO; i++) vDAT[i] = CWB::mdc::GetAligned(&vDAT[i], &vREC[i]);
436 
437 #ifdef TEST1
438  double sync_time;
439  vREC[1]*=-1.0;
440  double sync_xcor = CWB::mdc::TimeSync(vREC[0], vREC[1], sync_time);
441  cout << ID << " vREC[0]xvREC[1] --------------------------------> sync_time " << sync_time << " sync_xcor : " << sync_xcor << endl;
442 #endif
443 
444  Wave2Sparse(NET,cfg,'v'); // save original sparse map to vSS
445  Wave2Sparse(NET,cfg,'r'); // rec to rSS
446  Wave2Sparse(NET,cfg,'d'); // dat to dSS
447  if(cfg->simulation) Wave2Sparse(NET,cfg,'j'); // inj to jSS
448 
449  // get injected waveform on TF sub-space of the reconstructed waveform vREC
450  if(cfg->simulation) {
451  cINJ = GetSigWaveform(NET, cfg, k, ID);
452  for(int i=0; i<nIFO; i++) vINJ[i] = CWB::mdc::GetAligned(&vINJ[i], &vREC[i]);
453  for(int i=0; i<nIFO; i++) cINJ[i] = CWB::mdc::GetAligned(&cINJ[i], &vREC[i]);
454  for(int i=0; i<nIFO; i++) vRES.push_back(CWB::mdc::GetDiff(&vREC[i], &cINJ[i])); // vRES=vREC-cINJ
455  Wave2Sparse(NET,cfg,'x'); // res to xSS
456  }
457 
458  if(cfg->simulation) {
459  double FF,OF,RE;
460  vector<double> tstart;
461  vector<double> tstop;
462 
463  // get FF/OF/RE for the full time range
464  gFF[0] = CWB::mdc::GetMatchFactor("ff",vREC,vINJ,tstart,tstop);
465  gOF[0] = CWB::mdc::GetMatchFactor("of",vREC,vINJ,tstart,tstop);
466  gRE[0] = CWB::mdc::GetMatchFactor("re",vREC,vINJ,tstart,tstop);
467 
468  // get FF/OF/RE for the left time range (before the coalescence time)
469  tstart.resize(0);
470  tstop.resize(nIFO);
471  for(int n=0;n<nIFO;n++) {
472  tstop[n] = GetInjTcoa(gTCOA, NET, NET->ifoName[n], EVT->theta[1], EVT->phi[1]);
473  }
474  gFF[1] = CWB::mdc::GetMatchFactor("ff",vREC,vINJ,tstart,tstop);
475  gOF[1] = CWB::mdc::GetMatchFactor("of",vREC,vINJ,tstart,tstop);
476  gRE[1] = CWB::mdc::GetMatchFactor("re",vREC,vINJ,tstart,tstop);
477 
478  // get FF/OF/RE for the right time range (after the coalescence time)
479  tstart.resize(nIFO);
480  tstop.resize(0);
481  for(int n=0;n<nIFO;n++) {
482  tstart[n] = GetInjTcoa(gTCOA, NET, NET->ifoName[n], EVT->theta[1], EVT->phi[1]);
483  }
484  gFF[2] = CWB::mdc::GetMatchFactor("ff",vREC,vINJ,tstart,tstop);
485  gOF[2] = CWB::mdc::GetMatchFactor("of",vREC,vINJ,tstart,tstop);
486  gRE[2] = CWB::mdc::GetMatchFactor("re",vREC,vINJ,tstart,tstop);
487  }
488 
489  gCRATE = NET->getwc(k)->rate; // get event cluster rate
490  gCLUSTER = GetCluster(NET, k, ID); // get event netcluster
491 
492  if(gOPT.output_pol) {
493  gWF.rPOL00 = NET->r00_POL[0]; // polarization of 00 amplitudes -> radial
494  gWF.aPOL00 = NET->r00_POL[1]; // polarization of 00 amplitudes -> angle
495  gWF.rPOL90 = NET->r90_POL[0]; // polarization of 90 amplitudes -> radial
496  gWF.aPOL90 = NET->r90_POL[1]; // polarization of 90 amplitudes -> angle
497  }
498 
499 #ifdef TEST2
501  for(int i=0; i<nIFO; i++) vDIF[i] = CWB::mdc::GetDiff(&vREC[i], &cINJ[i]);
502  double sync_time;
503  vDIF[1]*=-1.0;
504  double sync_xcor = CWB::mdc::TimeSync(vDIF[0], vDIF[1], sync_time);
505  cout << ID << " vDIF[0]xvDIF[1] --------------------------------> sync_time " << sync_time << " sync_xcor : " << sync_xcor << endl;
506 #endif
507 
508 #ifdef TEST3
509  char title[256];
510  char ofname[256];
511  for(int i=0; i<nIFO; i++) {
512  // PLOT -> vRES
513  sprintf(title,"%s (TIME) : vREC(red) - cINJ(blue)",NET->ifoName[i]);
514  sprintf(ofname,"%s_vREC_cINJ%d.%s",NET->ifoName[i],ID,"root");
515  PlotWaveform(ofname, title, cfg, &vREC[i], &cINJ[i], NULL, &vREC[i], false);
516  }
517 #endif
518 
519  SetOutputFile(NET, EVT, cfg, true); // set output root file
520 
521  if(gOPT.output_root) {
522  DumpOutputFile(NET, EVT, cfg, ID, k, ofactor); // dump event to output root file
523 
524  for(int n=0;n<nIFO;n++) {
525  detector* pD = NET->getifo(n);
526  if(!cfg->simulation) ClearWaveforms(pD); // release waveform memory
527  }
528  }
529  }
530  }
531 
532  jfile->cd();
533  if(EVT) delete EVT;
534  }
535 
536  return;
537 }
538 
539 std::vector<wavearray<double> > GetWaveform(network* NET, int lag, int id, char type, bool shift) {
540 
541  if(type!='S' && type!='s' && type!='W' && type!='w' && type!='N' && type!='n') {
542  cout << "GetWaveform : not valid type : Abort" << endl; exit(1);
543  }
544 
545  std::vector<wavearray<double> > vWAV; // reconstructed
546 
547  int nIFO = NET->ifoListSize(); // number of detectors
548  netcluster* pwc = NET->getwc(lag);
549 
550  if(type=='S' || type=='s') {
551  NET->getMRAwave(id,lag,'S',0,shift); // reconstruct whitened shifted pd->waveForm
552  detector* pd;
553  for(int i=0; i<nIFO; i++) { // loop over detectors
554  pd = NET->getifo(i);
555  wavearray<double>* wf = &pd->waveForm;
556  wf->start(pwc->start+pd->waveForm.start());
557  vWAV.push_back(*wf);
558  }
559  }
560 
561  if(type=='W' || type=='w') {
562  NET->getMRAwave(id,lag,type,0,shift); // reconstruct+noise whitened shifted pd->waveBand
563  detector* pd;
564  for(int i=0; i<nIFO; i++) { // loop over detectors
565  pd = NET->getifo(i);
566  wavearray<double>* wf = &pd->waveBand;
567  wf->start(pwc->start+pd->waveBand.start());
568  vWAV.push_back(*wf);
569  }
570  }
571 
572  if(type=='N' || type=='n') {
573  NET->getMRAwave(id,lag,'W',0,shift); // reconstruct+noise whitened shifted pd->waveBand
574  NET->getMRAwave(id,lag,'S',0,shift); // reconstruct whitened shifted pd->waveForm
575  detector* pd;
576  for(int i=0; i<nIFO; i++) { // loop over detectors
577  pd = NET->getifo(i);
578  pd->waveNull = pd->waveBand;
579  pd->waveNull-= pd->waveForm;
580  wavearray<double>* wf = &pd->waveNull;
581  wf->start(pwc->start+pd->waveNull.start());
582  vWAV.push_back(*wf);
583  }
584  }
585 
586  return vWAV;
587 }
588 
589 void SetOutputFile(network* NET, netevent* &EVT, CWB::config* cfg, bool dump_wf) {
590 
591  // import slagShift
592  float* gSLAGSHIFT=NULL; IMPORT(float*,gSLAGSHIFT)
593 
594  int nIFO = NET->ifoListSize(); // number of detectors
595 
596  // search output root file in the system list
597  TFile* froot = NULL;
598  TList *files = (TList*)gROOT->GetListOfFiles();
599  gOUTPUT="";
600  if (files) {
601  TIter next(files);
602  TSystemFile *file;
603  TString fname;
604  bool check=false;
605  while ((file=(TSystemFile*)next())) {
606  fname = file->GetName();
607  // set output root file as the current file
608  if(fname.Contains("wave_")) {
609  froot=(TFile*)file;froot->cd();
610  gOUTPUT=fname;
611  gOUTPUT.ReplaceAll(".root.tmp",".txt");
612  //cout << "output file name : " << fname << endl;
613  }
614  }
615  if(!froot) {
616  cout << "CWB_Plugin_WF.C : Error - output root file not found" << endl;
617  gSystem->Exit(1);
618  }
619  } else {
620  cout << "CWB_Plugin_WF.C : Error - output root file not found" << endl;
621  gSystem->Exit(1);
622  }
623 
624  if(dump_wf) {
625  if(gOPT.output_inj) if(cfg->simulation) for(int n=0;n<nIFO;n++) gWF.wINJ[n] = &vINJ[n];
626  if(gOPT.output_inj) if(cfg->simulation && vAUX.size()==nIFO) for(int n=0;n<nIFO;n++) gWF.wAUX[n] = &vAUX[n];
627  if(gOPT.output_inj) if(cfg->simulation) for(int n=0;n<nIFO;n++) gWF.cINJ[n] = &cINJ[n];
628  if(gOPT.output_rec) for(int n=0;n<nIFO;n++) gWF.wREC[n] = &vREC[n];
629  if(gOPT.output_wht) for(int n=0;n<nIFO;n++) gWF.wWHT[n] = &vWHT[n];
630  if(gOPT.output_dat) for(int n=0;n<nIFO;n++) gWF.wDAT[n] = &vDAT[n];
631  if(gOPT.output_nul) for(int n=0;n<nIFO;n++) gWF.wNUL[n] = &vNUL[n];
632  if(gOPT.output_cluster) gWF.pCLUSTER = &gCLUSTER;
633  }
634 
635  gTREE = (TTree *) froot->Get("waveburst");
636  if(gTREE!=NULL) {
637  EVT = new netevent(gTREE,nIFO);
638  if(dump_wf) {
639  if(gOPT.fits_pe!="") gTREE->Branch("sm_pe_of",&gSM_PE_OF,"sm_pe_of/F");
640  if(gOPT.fits_os!="") gTREE->Branch("sm_os_of",&gSM_OS_OF,"sm_os_of/F");
641  if(cfg->simulation) gTREE->Branch("wf_ff", gFF, TString::Format("wf_ff[%i]/F",3));
642  if(cfg->simulation) gTREE->Branch("wf_of", gOF, TString::Format("wf_of[%i]/F",3));
643  if(cfg->simulation) gTREE->Branch("wf_re", gRE, TString::Format("wf_re[%i]/F",3));
644  if(cfg->simulation) gTREE->Branch("wf_tcoa", &gTCOA, "wf_tcoa/D");
645  for(int n=0;n<nIFO;n++) {
646  if(gOPT.output_inj) if(cfg->simulation) gTREE->Branch(TString::Format("wINJ_%d",n).Data(),"wavearray<double>",&gWF.wINJ[n],32000,0);
647  if(gOPT.output_inj) if(cfg->simulation && vAUX.size()==nIFO) gTREE->Branch(TString::Format("wAUX_%d",n).Data(),"wavearray<double>",&gWF.wAUX[n],32000,0);
648  if(gOPT.output_inj) if(cfg->simulation) gTREE->Branch(TString::Format("cINJ_%d",n).Data(),"wavearray<double>",&gWF.cINJ[n],32000,0);
649  if(gOPT.output_rec) gTREE->Branch(TString::Format("wREC_%d",n).Data(),"wavearray<double>",&gWF.wREC[n],32000,0);
650  if(gOPT.output_wht) gTREE->Branch(TString::Format("wWHT_%d",n).Data(),"wavearray<double>",&gWF.wWHT[n],32000,0);
651  if(gOPT.output_dat) gTREE->Branch(TString::Format("wDAT_%d",n).Data(),"wavearray<double>",&gWF.wDAT[n],32000,0);
652  if(gOPT.output_nul) gTREE->Branch(TString::Format("wNUL_%d",n).Data(),"wavearray<double>",&gWF.wNUL[n],32000,0);
653  if(gOPT.output_strain) gTREE->Branch(TString::Format("wSTRAIN_%d",n).Data(),"wavearray<double>",&gSTRAIN[n],32000,0);
654  if(gOPT.output_mdc) gTREE->Branch(TString::Format("wMDC_%d",n).Data(),"wavearray<double>",&gMDC[n],32000,0);
655  if(gOPT.output_cstrain) gTREE->Branch(TString::Format("wCSTRAIN_%d",n).Data(),"wavearray<double>",&gCSTRAIN[n],32000,0);
656  }
657  if(gOPT.output_cluster) {
658  gTREE->Branch("seggps",&gSEGGPS,"seggps/D");
659  gTREE->Branch("crate",&gCRATE,"crate/F");
660  gTREE->Branch("cluster","vector<netpixel>",&gWF.pCLUSTER,32000,0);
661  }
662  if(gOPT.output_pol) {
663  gTREE->Branch("rPOL00","wavearray<double>",&gWF.rPOL00,32000,0);
664  gTREE->Branch("aPOL00","wavearray<double>",&gWF.aPOL00,32000,0);
665  gTREE->Branch("rPOL90","wavearray<double>",&gWF.rPOL90,32000,0);
666  gTREE->Branch("aPOL90","wavearray<double>",&gWF.aPOL90,32000,0);
667  }
668  }
669  } else {
670  EVT = new netevent(nIFO);
671  gTREE = EVT->setTree();
672  if(gOPT.fits_pe!="") gTREE->SetBranchAddress("sm_pe_of",&gSM_PE_OF);
673  if(gOPT.fits_os!="") gTREE->SetBranchAddress("sm_os_of",&gSM_OS_OF);
674  }
675  EVT->setSLags(gSLAGSHIFT); // set slags into netevent
676  EVT->Psave=cfg->Psave;
677 }
678 
679 void DumpOutputFile(network* NET, netevent* &EVT, CWB::config* cfg, int ID, int k, int factor) {
680 
681  if(cfg->dump) EVT->dopen(gOUTPUT.Data(),const_cast<char*>("a"),false);
682  EVT->output2G(gTREE,NET,ID,k,factor); // get reconstructed parameters
683  if(cfg->dump) EVT->dclose();
684  if(!cfg->cedDump) NET->getwc(k)->sCuts[ID-1]=1; // mark as processed
685 }
686 
687 void
689 
690  int n;
691 
692  n = ifo->IWFP.size();
693  for (int i=0;i<n;i++) {
695  delete wf;
696  }
697  ifo->IWFP.clear();
698  ifo->IWFID.clear();
699 
700  n = ifo->RWFP.size();
701  for (int i=0;i<n;i++) {
703  delete wf;
704  }
705  ifo->RWFP.clear();
706  ifo->RWFID.clear();
707 }
708 
710 
711  // get plugin options
712 
713  if(TString(options)!="") {
714 
715  //cout << "WF options : " << options << endl;
716  TObjArray* token = TString(options).Tokenize(TString(' '));
717  for(int j=0;j<token->GetEntries();j++) {
718 
719  TObjString* tok = (TObjString*)token->At(j);
720  TString stok = tok->GetString();
721 
722  if(stok.Contains("wf_output_enable=")) {
723  TString wf_output_enable=stok;
724  wf_output_enable.Remove(0,wf_output_enable.Last('=')+1);
725  if(wf_output_enable=="root") gOPT.output_root=true;
726  if(wf_output_enable=="inj") gOPT.output_inj=true;
727  if(wf_output_enable=="rec") gOPT.output_rec=true;
728  if(wf_output_enable=="wht") gOPT.output_wht=true;
729  if(wf_output_enable=="dat") gOPT.output_dat=true;
730  if(wf_output_enable=="nul") gOPT.output_nul=true;
731  if(wf_output_enable=="strain") gOPT.output_strain=true;
732  if(wf_output_enable=="mdc") gOPT.output_mdc=true;
733  if(wf_output_enable=="cstrain") gOPT.output_cstrain=true;
734  if(wf_output_enable=="cluster") gOPT.output_cluster=true;
735  if(wf_output_enable=="pol") gOPT.output_pol=true;
736  }
737 
738  if(stok.Contains("wf_output_disable=")) {
739  TString wf_output_disable=stok;
740  wf_output_disable.Remove(0,wf_output_disable.Last('=')+1);
741  if(wf_output_disable=="root") gOPT.output_root=false;
742  if(wf_output_disable=="inj") gOPT.output_inj=false;
743  if(wf_output_disable=="rec") gOPT.output_rec=false;
744  if(wf_output_disable=="wht") gOPT.output_wht=false;
745  if(wf_output_disable=="dat") gOPT.output_dat=false;
746  if(wf_output_disable=="nul") gOPT.output_nul=false;
747  if(wf_output_disable=="strain") gOPT.output_strain=false;
748  if(wf_output_disable=="mdc") gOPT.output_mdc=false;
749  if(wf_output_disable=="cstrain") gOPT.output_cstrain=false;
750  if(wf_output_disable=="cluster") gOPT.output_cluster=false;
751  if(wf_output_disable=="pol") gOPT.output_pol=false;
752  }
753 
754  if(stok.Contains("wf_fits_pe=")) {
755  TString wf_fits_pe=stok;
756  wf_fits_pe.Remove(0,wf_fits_pe.Last('=')+1);
757  gOPT.fits_pe=wf_fits_pe;
758  }
759  if(stok.Contains("wf_gps_pe=")) {
760  TString wf_gps_pe=stok;
761  wf_gps_pe.Remove(0,wf_gps_pe.Last('=')+1);
762  if(wf_gps_pe.IsFloat()) gOPT.gps_pe=wf_gps_pe.Atof();
763  }
764 
765  if(stok.Contains("wf_fits_os=")) {
766  TString wf_fits_os=stok;
767  wf_fits_os.Remove(0,wf_fits_os.Last('=')+1);
768  gOPT.fits_os=wf_fits_os;
769  }
770  if(stok.Contains("wf_gps_os=")) {
771  TString wf_gps_os=stok;
772  wf_gps_os.Remove(0,wf_gps_os.Last('=')+1);
773  if(wf_gps_os.IsFloat()) gOPT.gps_os=wf_gps_os.Atof();
774  }
775  if(stok.Contains("wf_inj_tstep=")) {
776  TString wf_inj_tstep=stok;
777  wf_inj_tstep.Remove(0,wf_inj_tstep.Last('=')+1);
778  if(wf_inj_tstep.IsFloat()) gOPT.inj_tstep=wf_inj_tstep.Atof();
779  }
780  if(stok.Contains("wf_inj_toff=")) {
781  TString wf_inj_toff=stok;
782  wf_inj_toff.Remove(0,wf_inj_toff.Last('=')+1);
783  if(wf_inj_toff.IsFloat()) gOPT.inj_toff=wf_inj_toff.Atof();
784  }
785  }
786  }
787 }
788 
790 
791  gOPT.output_root = WF_OUTPUT_ROOT;
792  gOPT.output_inj = WF_OUTPUT_INJ;
793  gOPT.output_rec = WF_OUTPUT_REC;
794  gOPT.output_wht = WF_OUTPUT_WHT;
795  gOPT.output_dat = WF_OUTPUT_DAT;
796  gOPT.output_nul = WF_OUTPUT_NUL;
797  gOPT.output_strain = WF_OUTPUT_STRAIN;
798  gOPT.output_mdc = WF_OUTPUT_MDC;
799  gOPT.output_cstrain = WF_OUTPUT_CSTRAIN;
800  gOPT.output_cluster = WF_OUTPUT_CLUSTER;
801  gOPT.output_pol = WF_OUTPUT_POL;
802  gOPT.fits_pe = WF_FITS_PE;
803  gOPT.gps_pe = WF_GPS_PE;
804  gOPT.fits_os = WF_FITS_OS;
805  gOPT.gps_os = WF_GPS_OS;
806  gOPT.inj_tstep = WF_INJ_TSTEP;
807  gOPT.inj_toff = WF_INJ_TOFF;
808 }
809 
811 
812  cout << "-----------------------------------------" << endl;
813  cout << "WF config options " << endl;
814  cout << "-----------------------------------------" << endl << endl;
815 
816  cout << "WF_OUTPUT_ROOT " << gOPT.output_root << endl;
817 
818  cout << "WF_OUTPUT_INJ " << gOPT.output_inj << endl;
819  cout << "WF_OUTPUT_REC " << gOPT.output_rec << endl;
820  cout << "WF_OUTPUT_WHT " << gOPT.output_wht << endl;
821  cout << "WF_OUTPUT_DAT " << gOPT.output_dat << endl;
822  cout << "WF_OUTPUT_NUL " << gOPT.output_nul << endl;
823 
824  cout << "WF_OUTPUT_STRAIN " << gOPT.output_strain << endl;
825  cout << "WF_OUTPUT_MDC " << gOPT.output_mdc << endl;
826  cout << "WF_OUTPUT_CSTRAIN " << gOPT.output_cstrain << endl;
827 
828  cout << "WF_OUTPUT_CLUSTER " << gOPT.output_cluster << endl;
829  cout << "WF_OUTPUT_POL " << gOPT.output_pol << endl;
830 
831  cout << "WF_FITS_PE " << gOPT.fits_pe << endl;
832  cout << "WF_GPS_PE " << gOPT.gps_pe << endl;
833  cout << "WF_FITS_OS " << gOPT.fits_os << endl;
834  cout << "WF_GPS_OS " << gOPT.gps_os << endl;
835 
836  cout << "WF_INJ_TSTEP " << gOPT.inj_tstep << endl;
837  cout << "WF_INJ_TOFF " << gOPT.inj_toff << endl;
838 
839  cout << endl;
840 }
841 
842 std::vector<wavearray<double> > GetWhitenedData(network* NET, CWB::config* cfg) {
843  // get whitened data (in the vREC time range)
844 
845  std::vector<wavearray<double> > xWHT; // temporary stuff
846 
847  int nIFO = NET->ifoListSize(); // number of detectors
848 
849  for(int n=0; n<nIFO; n++) {
850  xWHT.push_back(CWB::mdc::GetAligned(&gHOT[n], &vREC[n]));
851  }
852  return xWHT;
853 }
854 
855 std::vector<wavearray<double> > GetInjWaveform(network* NET, netevent* EVT, int ID, double factor) {
856 
857  std::vector<wavearray<double> > xINJ; // injected
858 
859  int nIFO = NET->ifoListSize(); // number of detectors
860 
861  double recTime = EVT->time[0]; // rec event time det=0
862 
863  injection INJ(nIFO);
864  // get inj ID
865  int M = NET->mdc__IDSize();
866  double injTime = 1.e12;
867  int injID = -1;
868  for(int m=0; m<M; m++) {
869  int mdcID = NET->getmdc__ID(m);
870  double T = fabs(recTime - NET->getmdcTime(mdcID));
871  if(T<injTime && INJ.fill_in(NET,mdcID)) {
872  injTime = T;
873  injID = mdcID;
874  }
875  }
876 
877  if(INJ.fill_in(NET,injID)) { // get inj parameters
878 
879  wavearray<double>** pwfINJ = new wavearray<double>*[nIFO];
880 
881  // extract whitened injected & reconstructed waveforms
882  for(int n=0; n<nIFO; n++) {
883 
884  detector* pd = NET->getifo(n);
885 
886  pwfINJ[n] = INJ.pwf[n];
887  if (pwfINJ[n]==NULL) {
888  cout << "CWB_Plugin_WF.C : Error : Injected waveform not saved !!! : detector "
889  << NET->ifoName[n] << endl;
890  continue;
891  }
892 
893  double rFactor = 1.;
894  rFactor *= factor>0 ? factor : 1;
895  wavearray<double>* wf = pwfINJ[n];
896  *wf*=rFactor; // injected wf is multiplied by the custom factor
897  xINJ.push_back(*wf);
898  *wf*=1./rFactor; // restore injected amplitude
899  }
900  delete [] pwfINJ;
901  }
902 
903  return xINJ;
904 }
905 
906 std::vector<wavearray<double> > GetAuxWaveform(network* NET, netevent* EVT, int ID, double factor) {
907 
908  std::vector<wavearray<double> > xAUX; // auxiliary injected
909 
910  int nIFO = NET->ifoListSize(); // number of detectors
911 
912  double recTime = EVT->time[0]; // rec event time det=0
913 
914  injection INJ(nIFO);
915  // get inj ID
916  int M = NET->mdc__IDSize();
917  double injTime = 1.e12;
918  int injID = -1;
919  for(int m=0; m<M; m++) {
920  int mdcID = NET->getmdc__ID(m);
921  double T = fabs(recTime - NET->getmdcTime(mdcID));
922  if(T<injTime && INJ.fill_in(NET,mdcID)) {
923  injTime = T;
924  injID = mdcID;
925  }
926  }
927 
928  // search the auxiliary injection, same injection time
929  detector* pD = NET->getifo(0);
930  int J = pD->IWFP.size();
931  cout.precision(14);
932  int auxID = -1;
933  for(int j=0;j<J;j++) {
934  int mdcID = pD->IWFID[j];
935  if(mdcID<0 || mdcID==injID) continue; // skip primary and strain injections
937  double start = wf->start();
938  double stop = start+wf->size()/wf->rate();
939  if(recTime>start && recTime<stop) auxID = j;
940  }
941 
942  if(auxID>0) {
943  for(int n=0; n<nIFO; n++) {
944  pD = NET->getifo(n);
945  cout.precision(14);
946  wavearray<double>* wf = (wavearray<double>*)pD->IWFP[auxID];
947  xAUX.push_back(*wf);
948  }
949  }
950 
951  return xAUX;
952 }
953 
954 std::vector<wavearray<double> > GetRecWaveform(network* NET, netevent* EVT, int ID) {
955 
956  std::vector<wavearray<double> > xREC; // reconstructed
957 
958  int nIFO = NET->ifoListSize(); // number of detectors
959 
960  wavearray<double>** pwfREC = new wavearray<double>*[nIFO];
961  for(int n=0; n<nIFO; n++) {
962 
963  detector* pd = NET->getifo(n);
964  int idSize = pd->RWFID.size();
965  int wfIndex=-1;
966  for(int mm=0; mm<idSize; mm++) if (pd->RWFID[mm]==ID) wfIndex=mm;
967 
968  if(wfIndex<0) {
969  cout << "CWB_Plugin_WF.C : Error : Reconstructed waveform not saved !!! : ID -> "
970  << ID << " : detector " << NET->ifoName[n] << endl;
972  xREC.push_back(wf);
973  continue;
974  }
975  if(wfIndex>=0) pwfREC[n] = pd->RWFP[wfIndex];
976 
977  wavearray<double>* wf = pwfREC[n];
978  xREC.push_back(*wf);
979  }
980  delete [] pwfREC;
981 
982  return xREC;
983 }
984 
985 void ClearVectors() {
986 
987  while(!vINJ.empty()) vINJ.pop_back();
988  vINJ.clear(); std::vector<wavearray<double> >().swap(vINJ);
989  while(!cINJ.empty()) cINJ.pop_back();
990  cINJ.clear(); std::vector<wavearray<double> >().swap(cINJ);
991  while(!vREC.empty()) vREC.pop_back();
992  vREC.clear(); std::vector<wavearray<double> >().swap(vREC);
993  while(!vWHT.empty()) vWHT.pop_back();
994  vWHT.clear(); std::vector<wavearray<double> >().swap(vWHT);
995  while(!vDAT.empty()) vDAT.pop_back();
996  vDAT.clear(); std::vector<wavearray<double> >().swap(vDAT);
997  while(!vNUL.empty()) vNUL.pop_back();
998  vNUL.clear(); std::vector<wavearray<double> >().swap(vNUL);
999  while(!vRES.empty()) vRES.pop_back();
1000  vRES.clear(); std::vector<wavearray<double> >().swap(vRES);
1001 }
1002 
1003 std::vector<wavearray<double> > GetSigWaveform(network* NET, CWB::config* cfg, int lag, int id) {
1004 // get injected waveform on TF sub-space of the reconstructed waveform vREC
1005 
1006  std::vector<wavearray<double> > vSIG; // reconstructed
1007 
1008  int nIFO = NET->ifoListSize(); // number of detectors
1009  netcluster* pwc = NET->getwc(lag);
1010 
1011  std::vector<int> pI = NET->wdmMRA.getXTalk(pwc, id, false); // buffer for pixel IDs
1012 
1013  netpixel* pix = pwc->getPixel(id,pI[0]);
1014 
1015  int tsize=1;
1016 
1017  int V = pI.size();
1018  if(!V) return vSIG;
1019  int V4 = V + (V%4 ? 4 - V%4 : 0);
1020  int V44 = V4 + 4;
1021 
1022  float En = 2*NET->acor*NET->acor*nIFO; // network energy threshold in the sky loop
1023 
1024  float *pd[NIFO], *pD[NIFO];
1025  float *v00[NIFO],*v90[NIFO];
1026  float *pa[NIFO], *pA[NIFO];
1027 
1028  float* ptmp;
1029  int i,j;
1030 
1031  std::vector<float*> _DAT; // vectors for packet amplitudes
1032  std::vector<float*> _vtd; // vectors of TD amplitudes
1033  std::vector<float*> _vTD; // vectors of TD amplitudes
1034 
1035  if(_DAT.size()) _avx_free_ps(_DAT); // container for data packet amplitudes
1036  if(_vtd.size()) _avx_free_ps(_vtd); // array for 00 amplitudes
1037  if(_vTD.size()) _avx_free_ps(_vTD); // array for 90 amplitudes
1038  for(i=0; i<NIFO; i++) {
1039  ptmp = (float*)_mm_malloc((V4*3+8)*sizeof(float),32);
1040  for(j=0; j<(V4*3+8); j++) ptmp[j]=0; _DAT.push_back(ptmp); // concatenated arrays {amp}{AMP}{norm}{n,N,c,s}
1041  ptmp = (float*)_mm_malloc(tsize*V4*sizeof(float),32);
1042  for(j=0; j<tsize*V4; j++) ptmp[j]=0; _vtd.push_back(ptmp); // array of aligned vectors
1043  ptmp = (float*)_mm_malloc(tsize*V4*sizeof(float),32);
1044  for(j=0; j<tsize*V4; j++) ptmp[j]=0; _vTD.push_back(ptmp); // array of aligned vectors
1045  }
1046 
1047  std::vector<float*> _AVX; // vectors for network pixel statistics
1048  if(_AVX.size()) _avx_free_ps(_AVX);
1049  float* p_et = (float*)_mm_malloc(V4*sizeof(float),32); // 0
1050  for(j=0; j<V4; j++) p_et[j]=0; _AVX.push_back(p_et);
1051  float* pMSK = (float*)_mm_malloc(V44*sizeof(float),32); // 1 - pixel mask
1052  for(j=0; j<V44; j++) pMSK[j]=0; _AVX.push_back(pMSK); pMSK[V4]=nIFO;
1053  float* p_fp = (float*)_mm_malloc(V44*sizeof(float),32); // 2- |f+|^2 (0:V4), +norm (V4:V4+4)
1054  for(j=0; j<V44; j++) p_fp[j]=0; _AVX.push_back(p_fp);
1055  float* p_fx = (float*)_mm_malloc(V44*sizeof(float),32); // 3- |fx|^2 (0:V4), xnorm (V4:V4+4)
1056  for(j=0; j<V44; j++) p_fx[j]=0; _AVX.push_back(p_fx);
1057  float* p_si = (float*)_mm_malloc(V4*sizeof(float),32); // 4
1058  for(j=0; j<V4; j++) p_si[j]=0; _AVX.push_back(p_si);
1059  float* p_co = (float*)_mm_malloc(V4*sizeof(float),32); // 5
1060  for(j=0; j<V4; j++) p_co[j]=0; _AVX.push_back(p_co);
1061  float* p_uu = (float*)_mm_malloc((V4+16)*sizeof(float),32); // 6 - 00+ unit vector(0:V4), norm(V4), cos(V4+4)
1062  for(j=0; j<V4+16; j++) p_uu[j]=0; _AVX.push_back(p_uu);
1063  float* p_UU = (float*)_mm_malloc((V4+16)*sizeof(float),32); // 7 - 90+ unit vector(0:V4), norm(V4), sin(V4+4)
1064  for(j=0; j<V4+16; j++) p_UU[j]=0; _AVX.push_back(p_UU);
1065  float* p_vv = (float*)_mm_malloc((V4+16)*sizeof(float),32); // 8- 00x unit vector(0:V4), norm(V4), cos(V4+4)
1066  for(j=0; j<V4+16; j++) p_vv[j]=0; _AVX.push_back(p_vv);
1067  float* p_VV = (float*)_mm_malloc((V4+16)*sizeof(float),32); // 9- 90x unit vector(0:V4), norm(V4), sin(V4+4)
1068  for(j=0; j<V4+16; j++) p_VV[j]=0; _AVX.push_back(p_VV);
1069  float* p_au = (float*)_mm_malloc(V4*sizeof(float),32); // 10
1070  for(j=0; j<V4; j++) p_au[j]=0; _AVX.push_back(p_au);
1071  float* p_AU = (float*)_mm_malloc(V4*sizeof(float),32); // 11
1072  for(j=0; j<V4; j++) p_AU[j]=0; _AVX.push_back(p_AU);
1073  float* p_av = (float*)_mm_malloc(V4*sizeof(float),32); // 12
1074  for(j=0; j<V4; j++) p_av[j]=0; _AVX.push_back(p_av);
1075  float* p_AV = (float*)_mm_malloc(V4*sizeof(float),32); // 13
1076  for(j=0; j<V4; j++) p_AV[j]=0; _AVX.push_back(p_AV);
1077  float* p_uv = (float*)_mm_malloc(V4*4*sizeof(float),32); // 14 special array for GW norm calculation
1078  for(j=0; j<V4*4; j++) p_uv[j]=0; _AVX.push_back(p_uv);
1079  float* p_ee = (float*)_mm_malloc(V4*sizeof(float),32); // 15 + energy array
1080  for(j=0; j<V4; j++) p_ee[j]=0; _AVX.push_back(p_ee);
1081  float* p_EE = (float*)_mm_malloc(V4*sizeof(float),32); // 16 x energy array
1082  for(j=0; j<V4; j++) p_EE[j]=0; _AVX.push_back(p_EE);
1083  float* pTMP=(float*)_mm_malloc(V4*4*NIFO*sizeof(float),32); // 17 temporary array for _avx_norm_ps()
1084  for(j=0; j<V4*4*NIFO; j++) pTMP[j]=0; _AVX.push_back(pTMP);
1085  float* p_ni = (float*)_mm_malloc(V4*sizeof(float),32); // 18 + network index
1086  for(j=0; j<V4; j++) p_ni[j]=0; _AVX.push_back(p_ni);
1087  float* p_ec = (float*)_mm_malloc(V4*sizeof(float),32); // 19 + coherent energy
1088  for(j=0; j<V4; j++) p_ec[j]=0; _AVX.push_back(p_ec);
1089  float* p_gn = (float*)_mm_malloc(V4*sizeof(float),32); // 20 + Gaussian noise correction
1090  for(j=0; j<V4; j++) p_gn[j]=0; _AVX.push_back(p_gn);
1091  float* p_ed = (float*)_mm_malloc(V4*sizeof(float),32); // 21 + energy disbalance
1092  for(j=0; j<V4; j++) p_ed[j]=0; _AVX.push_back(p_ed);
1093  float* p_rn = (float*)_mm_malloc(V4*sizeof(float),32); // 22 + sattelite noise in TF domain
1094  for(j=0; j<V4; j++) p_rn[j]=0; _AVX.push_back(p_rn);
1095 
1096  double R = NET->getifo(0)->TFmap.rate();
1097  for(int j=0; j<V; j++) {
1098  netpixel* pix = pwc->getPixel(id,pI[j]);
1099  if(!pix->core) continue; // select only core pixels
1100  for(int n=0; n<nIFO; n++) {
1101  int index = pix->data[n].index;
1102  int ires = int(TMath::Log2(R/pix->rate))-cfg->l_low;
1103  double aa = GetSparseMapData(&jSS[n][ires], true, index);
1104  double AA = GetSparseMapData(&jSS[n][ires], false , index);
1105  //aa = GetSparseMapData(&rSS[n][ires], true, index);
1106  //AA = GetSparseMapData(&rSS[n][ires], false , index);
1107  _vtd[n][j] = aa; // copy 00 data
1108  _vTD[n][j] = AA; // copy 90 data
1109  }
1110  }
1111 
1112  for(int i=0; i<NIFO; i++) { // set up zero delay and packet pointers
1113  pd[i] = _DAT[i];
1114  pD[i] = _DAT[i]+V4;
1115  pa[i] = _vtd[i];
1116  pA[i] = _vTD[i];
1117  v00[i] = pa[i];
1118  v90[i] = pA[i];
1119  }
1120 
1121  En=0.0001;
1122  int M;
1123  double Eo=0;
1124  wavearray<float> D_snr;
1125  Eo = _avx_loadata_ps(v00,v90,pd,pD,En,_AVX,V4); // calculate data stats and store in _AVX
1126  Eo = _avx_packet_ps(pd,pD,_AVX,V4); // get data packet
1127  D_snr = NET->_avx_norm_ps(pd,pD,_AVX,V4); // data packet energy snr
1128  M = _avx_setAMP_ps(pd,pD,_AVX,V4); // set data packet amplitudes for data
1129 
1130  std::vector<netpixel> vPIX = SavePixels(NET, cfg, lag, id); // save original pixels
1131 
1132  for(int j=0; j<V; j++) { // loop over principle components
1133  pix = pwc->getPixel(id,pI[j]);
1134  pix->likelihood = (pMSK[j]>0) ? (p_ee[j]+p_EE[j])/2 : 0; // total pixel energy
1135  if(pMSK[j]>0) pix->core = true;
1136  //pix->core = true;
1137  for(int i=0; i<nIFO; i++) {
1138  pix->setdata(double(pd[i][j]),'S',i); // 00 whitened
1139  pix->setdata(double(pD[i][j]),'P',i); // 90 whitened
1140  }
1141  }
1142  vSIG = GetWaveform(NET, lag, id, 'S', false); // get reconstructed+noise waveform
1143 
1144  RestorePixels(NET, cfg, pwc, &vPIX, id); // restore original pixels
1145 
1146 #ifdef PLOT_INJ
1148  for(int i=0; i<nIFO; i++) {
1149  char title[256];
1150  char ofname[256];
1151 
1152  sprintf(title,"%s (TIME) : vREC(red) - vSIG(blue)",NET->ifoName[i]);
1153  sprintf(ofname,"%s_vREC_vSIG_time_id_%d.%s",NET->ifoName[i],id,"root");
1154  PlotWaveform(ofname, title, cfg, &vREC[i], &vSIG[i], NULL, &vREC[i], false); // time
1155 
1156  sprintf(title,"%s (TIME) : vINJ(red) - vSIG(blue)",NET->ifoName[i]);
1157  sprintf(ofname,"%s_vINJ_vSIG_time_id_%d.%s",NET->ifoName[i],id,"root");
1158  PlotWaveform(ofname, title, cfg, &vINJ[i], &vSIG[i], NULL, &vREC[i], false); // time
1159 
1160  // PLOT -> vREC : vREC-vSIG
1161  wDIF[i] = CWB::mdc::GetDiff(&vREC[i], &vSIG[i]);
1162  sprintf(title,"%s (TIME) : vREC(red) - cREC-vSIG(blue)",NET->ifoName[i]);
1163  sprintf(ofname,"%s_cINJ_vREvSIG_id_%d.%s",NET->ifoName[i],id,"root");
1164  PlotWaveform(ofname, title, cfg, &vREC[i], &wDIF[i], NULL, &vREC[i], false);
1165  }
1166 #endif
1167 
1168  if(_AVX.size()) _avx_free_ps(_AVX);
1169  if(_DAT.size()) _avx_free_ps(_DAT); // container for data packet amplitudes
1170  if(_vtd.size()) _avx_free_ps(_vtd); // array for 00 amplitudes
1171  if(_vTD.size()) _avx_free_ps(_vTD); // array for 90 amplitudes
1172 
1173  return vSIG;
1174 }
1175 
1176 void Wave2Sparse(network* NET, CWB::config* cfg, char type) {
1177 
1178  if(type!='j' && type!='r' && type!='v' && type!='d' && type!='x') {
1179  cout << "Wave2Sparse - Error : wrong wave type" << endl; exit(1);
1180  }
1181 
1182  // init waveform sparse maps
1183  int nIFO = NET->ifoListSize(); // number of detectors
1184  for(int n=0;n<nIFO;n++) {
1185  detector* pD = NET->getifo(n);
1186  if(type=='v') vSS[n]=pD->vSS;
1187  if(type=='r') rSS[n]=pD->vSS;
1188  if(type=='j') jSS[n]=pD->vSS;
1189  if(type=='d') dSS[n]=pD->vSS;
1190  if(type=='x') xSS[n]=pD->vSS;
1191  }
1192  if(type=='v') return;
1193 
1194  int nRES = cfg->l_high-cfg->l_low+1; // number of frequency resolution levels
1195 
1196  // build waveform array
1198  for(int n=0; n<nIFO; n++) {
1199  WF[n].resize(vSS[n][0].wdm_nSTS);
1200  WF[n].rate(vSS[n][0].wdm_rate);
1201  WF[n].start(vSS[n][0].wdm_start);
1202  WF[n]=0;
1203  if(type=='j') {
1204  int wos = double(vINJ[n].start()-WF[n].start())*WF[n].rate();
1205  for (int i=0;i<vINJ[n].size();i++) WF[n][wos+i] = vINJ[n][i];
1206  }
1207  if(type=='r') {
1208  int wos = double(vREC[n].start()-WF[n].start())*WF[n].rate();
1209  for (int i=0;i<vREC[n].size();i++) WF[n][wos+i] = vREC[n][i];
1210  }
1211  if(type=='d') {
1212  int wos = double(vDAT[n].start()-WF[n].start())*WF[n].rate();
1213  for (int i=0;i<vDAT[n].size();i++) WF[n][wos+i] = vDAT[n][i];
1214  }
1215  if(type=='x') {
1216  int wos = double(vDAT[n].start()-WF[n].start())*WF[n].rate();
1217  for (int i=0;i<vRES[n].size();i++) WF[n][wos+i] = vRES[n][i];
1218  }
1219 
1220 #ifdef SAVE_WAVE2SPARSE_PLOT
1221  gwavearray<double> gw(&WF[n]);
1222  gw.Draw(GWAT_TIME);
1223  watplot* plot = gw.GetWATPLOT();
1224  TString gfile;
1225  if(type=='r') gfile=TString::Format("%s/WAVE2SPARSE_REC_%s.root",".",NET->ifoName[n]);
1226  if(type=='j') gfile=TString::Format("%s/WAVE2SPARSE_INJ_%s.root",".",NET->ifoName[n]);
1227  if(type=='d') gfile=TString::Format("%s/WAVE2SPARSE_DAT_%s.root",".",NET->ifoName[n]);
1228  if(type=='x') gfile=TString::Format("%s/WAVE2SPARSE_RES_%s.root",".",NET->ifoName[n]);
1229  (*plot) >> gfile;
1230 #endif
1231  }
1232 
1233  // fill waveform sparse maps
1234  WSeries<double> w;
1235  WDM<double>* pwdm = NULL;
1236  double r00,r90;
1237  for(int n=0; n<nIFO; n++) {
1238  for(int i=0; i<nRES; i++) {
1239  w.Forward(WF[n],*(NET->wdmList[i]));
1240  int k = NET->getifo(n)->getSTFind(w.wrate()); // pointer to sparse TF array
1241 
1242  int size;
1243  if(type=='r') size = rSS[n][k].sparseMap00.size();
1244  if(type=='j') size = jSS[n][k].sparseMap00.size();
1245  if(type=='d') size = dSS[n][k].sparseMap00.size();
1246  if(type=='x') size = xSS[n][k].sparseMap00.size();
1247 
1248  for(int j=0; j<size; j++) {
1249  int index;
1250  if(type=='r') index = rSS[n][k].sparseIndex[j];
1251  if(type=='j') index = jSS[n][k].sparseIndex[j];
1252  if(type=='d') index = dSS[n][k].sparseIndex[j];
1253  if(type=='x') index = xSS[n][k].sparseIndex[j];
1254  double v00 = w.pWavelet->pWWS[index];
1255  double v90 = w.pWavelet->pWWS[index+w.maxIndex()+1];
1256  if(type=='r') {
1257  rSS[n][k].sparseMap00[j]=v00;
1258  rSS[n][k].sparseMap90[j]=v90;
1259  }
1260  if(type=='j') {
1261  jSS[n][k].sparseMap00[j]=v00;
1262  jSS[n][k].sparseMap90[j]=v90;
1263  }
1264  if(type=='d') {
1265  dSS[n][k].sparseMap00[j]=v00;
1266  dSS[n][k].sparseMap90[j]=v90;
1267  }
1268  if(type=='x') {
1269  xSS[n][k].sparseMap00[j]=v00;
1270  xSS[n][k].sparseMap90[j]=v90;
1271  }
1272  }
1273  }
1274  }
1275 }
1276 
1278 
1279  int layer = SS->GetLayer(index);
1280  int start = SS->sparseLookup[layer]; // sparse table layer offset
1281  int end = SS->sparseLookup[layer+1]-1; // sparse table layer+1 offset
1282 
1283  int i = SS->binarySearch(SS->sparseIndex.data,start,end,index);
1284 
1285  if(i<0) return 0;
1286 
1287  return phase ? SS->sparseMap00[i] : SS->sparseMap90[i];
1288 }
1289 
1292  bool fft, TString pdir, double P, EColor col1, EColor col2, EColor col3) {
1293 
1294  watplot PTS(const_cast<char*>("ptspe"),200,20,800,500);
1295 
1296  //cout << "Print " << ofname << endl;
1297  double tmin=1.e20;
1298  if(wref==NULL) return;
1299  if(wf1==NULL) return;
1300  else tmin=wf1->start();
1301  if(wf2!=NULL) if(wf2->start()<tmin) tmin=wf2->start();
1302  if(wf3!=NULL) if(wf3->start()<tmin) tmin=wf3->start();
1303 
1304  tmin=gSEGGPS;
1305 
1306  wf1->start(wf1->start()-tmin);
1307  if(wf2!=NULL) if(wf2!=wf1) wf2->start(wf2->start()-tmin);
1308  if(wf3!=NULL) if(wf3!=wf1 && wf3!=wf2) wf3->start(wf3->start()-tmin);
1309  if(wref!=wf1 && wref!=wf2 && wref!=wf3) wref->start(wref->start()-tmin);
1310 
1311  double bT, eT;
1312  CWB::mdc::GetTimeBoundaries(*wref, P, bT, eT);
1313 
1314  if(fft) {
1315  wavearray<double> wf=*wf1; int k=0;
1316  for(int i=0;i<wf1->size();i++) {
1317  double time=i/wf1->rate()+wf1->start();
1318  if(time>bT && time<eT) wf[k++]=wf1->data[i];
1319  }
1320  wf.resize(k);
1321  PTS.plot(&wf, const_cast<char*>("AL"), col1, 0, 0, true, cfg->fLow, cfg->fHigh);
1322  } else {
1323  PTS.plot(wf1, const_cast<char*>("AL"), col1, bT, eT);
1324  PTS.graph[0]->GetXaxis()->SetRangeUser(bT, eT);
1325  }
1326  PTS.graph[0]->SetLineWidth(1);
1327  PTS.graph[0]->SetTitle(title);
1328 
1329  TString xtitle = TString::Format("Time (sec) : GPS OFFSET = %.3f",tmin);
1330  PTS.graph[0]->GetXaxis()->SetTitle(xtitle);
1331 
1332  if(wf2!=NULL) {
1333  if(fft) {
1334  if(wf2!=NULL) {
1335  wavearray<double> wf=*wf2; int k=0;
1336  for(int i=0;i<wf2->size();i++) {
1337  double time=i/wf2->rate()+wf2->start();
1338  if(time>bT && time<eT) wf[k++]=wf2->data[i];
1339  }
1340  wf.resize(k);
1341  PTS.plot(&wf, const_cast<char*>("SAME"), col2, 0, 0, true, cfg->fLow, cfg->fHigh);
1342  }
1343  } else {
1344  if(wf2!=NULL) PTS.plot(wf2, const_cast<char*>("SAME"), col2, 0, 0);
1345  }
1346  if(wf2!=NULL) PTS.graph[1]->SetLineWidth(2);
1347  }
1348 
1349  if(wf3!=NULL) {
1350  if(fft) {
1351  if(wf3!=NULL) {
1352  wavearray<double> wf=*wf3; int k=0;
1353  for(int i=0;i<wf3->size();i++) {
1354  double time=i/wf3->rate()+wf3->start();
1355  if(time>bT && time<eT) wf[k++]=wf3->data[i];
1356  }
1357  wf.resize(k);
1358  PTS.plot(&wf, const_cast<char*>("SAME"), col3, 0, 0, true, cfg->fLow, cfg->fHigh);
1359  }
1360  } else {
1361  if(wf3!=NULL) PTS.plot(wf3, const_cast<char*>("SAME"), col3, 0, 0);
1362  }
1363  if(wf3!=NULL) PTS.graph[2]->SetLineWidth(1);
1364  }
1365 
1366  wf1->start(wf1->start()+tmin);
1367  if(wf2!=NULL) if(wf2!=wf1) wf2->start(wf2->start()+tmin);
1368  if(wf3!=NULL) if(wf3!=wf1 && wf3!=wf2) wf3->start(wf3->start()+tmin);
1369  if(wref!=wf1 && wref!=wf2 && wref!=wf3) wref->start(wref->start()+tmin);
1370 
1371  if(fft) {
1372  PTS.canvas->SetLogx();
1373  PTS.canvas->SetLogy();
1374  }
1375 
1376  if(ofname!="") {
1377  if(pdir!="") ofname = TString(pdir)+TString("/")+ofname;
1378  PTS.canvas->Print(ofname);
1379  cout << "write : " << ofname << endl;
1380  }
1381 }
1382 
1383 void
1384 DumpSkymap(network* NET, int lag, netevent* EVT, int ID) {
1385 
1386  // Dump2fits probability skymap (healpix)
1387  skymap skyprobcc = NET->getifo(0)->tau;
1388  skyprobcc=0.;
1389  skymap skyprob = skyprobcc;
1390  skyprob=1.e-12;
1391 
1392  std::vector<float>* vP;
1393  std::vector<int>* vI;
1394 
1395  vP = &(NET->wc_List[lag].p_Map[ID-1]);
1396  vI = &(NET->wc_List[lag].p_Ind[ID-1]);
1397  double th,ph,ra;
1398  int k;
1399  for(int j=0; j<int(vP->size()); j++) {
1400  int i = (*vI)[j];
1401  th = skyprob.getTheta(i);
1402  ph = skyprob.getPhi(i);
1403 
1404  k=skyprob.getSkyIndex(th, ph);
1405  skyprob.set(k,(*vP)[j]);
1406 
1407  ra = skyprob.getRA(i);
1408  k=skyprob.getSkyIndex(th, ra);
1409  skyprobcc.set(k,(*vP)[j]);
1410  }
1411 
1412  char fname[1024];
1413  sprintf(fname, "skyprobcc_%d.%s", ID, "fits");
1414  skyprobcc.Dump2fits(const_cast<char*>(fname),EVT->time[0],const_cast<char*>(""),const_cast<char*>("PROB"),const_cast<char*>("pix-1"),'C');
1415 
1416  return;
1417 }
1418 
1419 skymap
1420 GetSkyMap(network* NET, int lag, netevent* EVT, int ID) {
1421 
1422  skymap skyprobcc = NET->getifo(0)->tau;
1423  skyprobcc=0.;
1424  skymap skyprob = skyprobcc;
1425  skyprob=1.e-12;
1426 
1427  std::vector<float>* vP;
1428  std::vector<int>* vI;
1429 
1430  vP = &(NET->wc_List[lag].p_Map[ID-1]);
1431  vI = &(NET->wc_List[lag].p_Ind[ID-1]);
1432  double th,ph,ra;
1433  int k;
1434  for(int j=0; j<int(vP->size()); j++) {
1435  int i = (*vI)[j];
1436  th = skyprob.getTheta(i);
1437  ph = skyprob.getPhi(i);
1438 
1439  k=skyprob.getSkyIndex(th, ph);
1440  skyprob.set(k,(*vP)[j]);
1441 
1442  ra = skyprob.getRA(i);
1443  k=skyprob.getSkyIndex(th, ra);
1444  skyprobcc.set(k,(*vP)[j]);
1445  }
1446 
1447  return skyprobcc;
1448 }
1449 
1450 double
1452 
1453  skymap csm = *sm2; // cumulative probability
1454 
1455  double integral=0;
1456  int L=csm.size();
1457  int* index = new int[L]; // sorted index
1458  double* prob = new double[L]; // probability array
1459  for(int l=0;l<L;l++) { // loop over the sky grid
1460  prob[l] = csm.get(l); // get skymap prob
1461  integral += prob[l];
1462  }
1463 
1464  TMath::Sort(L,prob,index,false); // sort prob
1465  double cumul=0.0;
1466  for(int l=0;l<L;l++) { // loop over the sky grid
1467  int m=index[l]; // sorted index
1468  cumul+=prob[m];
1469  csm.set(m,cumul/integral); // set norlmalized cumulative probabilty
1470  }
1471 
1472  double max=-1.e+20;
1473  for(int l=0;l<L;l++) { // loop over the sky grid
1474  double p1=sm1->get(l);
1475  double p2=csm.get(l);
1476  if(p1*p2>max) max=p1*p2;
1477  }
1478 
1479  return max;
1480 }
1481 
1482 double
1484 
1485  int L = sm1->size(); // number of pixels in the sphere
1486 
1487  double pp12=0.;
1488  double pp11=0.;
1489  double pp22=0.;
1490  for(int l=0;l<L;l++) { // loop over the sky grid
1491  double p1=sm1->get(l);
1492  double p2=sm2->get(l);
1493  pp12 += p1*p2;
1494  pp11 += p1*p1;
1495  pp22 += p2*p2;
1496  }
1497 
1498  double OF = pp12/sqrt(pp11*pp22);
1499 
1500  return OF;
1501 }
1502 
1503 skymap
1504 ReadSkyMap(TString fitsFile, CWB::config* cfg, double gps) {
1505 
1506  gskymap sm((char*)fitsFile.Data());
1507  sm.resample(cfg->healpix);
1508 
1509  return EarthCoordinatesSM(&sm, gps);
1510 }
1511 
1512 skymap
1514 
1515  gskymap xsm = *sm;
1516 
1517  int L = sm->size(); // number of pixels in the sphere
1518  for(int l=0;l<L;l++) { // loop over the sky grid
1519  double p = sm->get(l);
1520  double phi = sm->getPhi(l);
1521  double theta = sm->getTheta(l);
1522  double xphi = sm->RA2phi(phi, gps);
1523  int k = sm->getSkyIndex(theta, xphi);
1524  xsm.set(k,p);
1525  }
1526 
1527  return xsm;
1528 }
1529 
1530 std::vector<netpixel>
1531 SavePixels(network* NET, CWB::config* cfg, int lag, int id) {
1532 
1533  netpixel* pix;
1534  netcluster* pwc = NET->getwc(lag);
1535  std::vector<netpixel> vPIX;
1536 
1537  std::vector<int> pI = NET->wdmMRA.getXTalk(pwc, id, false); // buffer for pixel IDs
1538  int V = pI.size();
1539  if(V>cfg->BATCH) return vPIX; // attach TD amp to pixels < V
1540 
1541  for(int j=0; j<V; j++) { // loop over principal components
1542  pix = pwc->getPixel(id,pI[j]);
1543  vPIX.push_back(*pix); // save original pixels
1544  }
1545 
1546  return vPIX;
1547 }
1548 
1549 void
1550 RestorePixels(network* NET, CWB::config* cfg, netcluster* pwc, std::vector<netpixel>* vPIX, int id) {
1551 
1552  std::vector<int> pI = NET->wdmMRA.getXTalk(pwc, id, false); // buffer for pixel IDs
1553  int V = pI.size();
1554  if(V>cfg->BATCH) return; // attach TD amp to pixels < V
1555  for(int j=0; j<V; j++) {
1556  netpixel* pix = pwc->getPixel(id,pI[j]);
1557  *pix = (*vPIX)[j];
1558  }
1559 
1560  while(!vPIX->empty()) vPIX->pop_back();
1561  vPIX->clear(); std::vector<netpixel>().swap(*vPIX);
1562 }
1563 
1564 double
1565 GetInjTcoa(double geocentric_tcoa, network* NET, TString ifo, double theta, double phi) {
1566 // compute coalescence time
1567 
1568  // Add Time Delay respect to geocenter
1569  CWB::mdc MDC(NET);
1570  double tdelay = MDC.GetDelay(ifo,"",phi,theta);
1571  double ifo_tcoa = geocentric_tcoa+tdelay;
1572 
1573  return ifo_tcoa;
1574 }
1575 
1576 std::vector<netpixel> GetCluster(network* NET, int lag, int id) {
1577 
1578  netpixel* pix;
1579  netcluster* pwc = NET->getwc(lag);
1580  std::vector<netpixel> vPIX;
1581 
1582  std::vector<int> pI = NET->wdmMRA.getXTalk(pwc, id, false); // buffer for pixel IDs
1583 
1584  int V = pI.size();
1585  for(int j=0; j<V; j++) { // loop over principal components
1586  pix = pwc->getPixel(id,pI[j]);
1587  vPIX.push_back(*pix); // save original pixels
1588  }
1589 
1590  return vPIX;
1591 }
1592 
monster wdmMRA
list of pixel pointers for MRA
Definition: network.hh:651
std::vector< char * > ifoName
Definition: network.hh:609
detector * getifo(size_t n)
param: detector index
Definition: network.hh:436
CWB::config * cfg
double GetSparseMapData(SSeries< double > *SS, bool phase, int index)
skymap EarthCoordinatesSM(skymap *sm, double gps)
#define WF_OUTPUT_MDC
Definition: CWB_Plugin_WF.C:78
#define NIFO
Definition: wat.hh:74
uoptions gOPT
size_t nLag
Definition: network.hh:573
#define WF_OUTPUT_CLUSTER
Definition: CWB_Plugin_WF.C:81
Float_t * rho
biased null statistics
Definition: netevent.hh:112
std::vector< wavearray< double > > vWHT
char xtitle[1024]
double M
Definition: DrawEBHH.C:13
string gLOG
std::vector< wavearray< double > > wREC[MAX_TRIALS]
std::vector< netcluster > wc_List
Definition: network.hh:610
#define WF_OUTPUT_POL
Definition: CWB_Plugin_WF.C:82
void setSLags(float *slag)
Definition: netevent.cc:426
skymap gSM_PE
std::vector< wavearray< double > > GetInjWaveform(network *NET, netevent *EVT, int id, double factor)
std::vector< double > * getmdcTime()
Definition: network.hh:422
std::vector< wavearray< double > > vDAT
std::vector< wavearray< double > > wNUL[PE_MAX_EVENTS]
Definition: cwb_pereport.C:323
std::vector< wavearray< double > > GetSigWaveform(network *NET, CWB::config *cfg, int lag, int id)
#define WF_OUTPUT_CSTRAIN
Definition: CWB_Plugin_WF.C:79
virtual void rate(double r)
Definition: wavearray.hh:141
skymap * sm2
float factor
wavearray< double > gCSTRAIN[NIFO_MAX]
float likelihood
Definition: netpixel.hh:114
int n
Definition: cwb_net.C:28
wavearray< float > _avx_norm_ps(float **, float **, std::vector< float *> &, int)
Definition: network.hh:1365
float gSM_PE_OF
std::vector< netpixel > SavePixels(network *NET, CWB::config *cfg, int lag, int id)
std::vector< SSeries< double > > dSS[NIFO_MAX]
float gFF[3]
TTree * setTree()
Definition: netevent.cc:434
std::vector< SSeries< double > > jSS[NIFO_MAX]
TString("c")
void set(size_t i, double a)
Definition: gskymap.hh:128
int ID
Definition: TestMDC.C:70
Int_t * size
cluster volume
Definition: netevent.hh:80
std::vector< wavearray< double > > cINJ
size_t maxIndex()
Definition: wseries.hh:149
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
std::vector< pixdata > data
Definition: netpixel.hh:122
double phase
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
std::vector< wavearray< double > * > RWFP
Definition: detector.hh:382
#define WF_INJ_TSTEP
Definition: CWB_Plugin_WF.C:90
double fLow
Definition: config.hh:140
float theta
TString gOUTPUT
netpixel pix(nifo)
netcluster * pwc
Definition: cwb_job_obj.C:38
#define WF_OUTPUT_ROOT
Definition: CWB_Plugin_WF.C:69
TH2F * ph
skymap ReadSkyMap(TString fitsFile, CWB::config *cfg, double gps)
void PlotWaveform(TString ofname, TString title, CWB::config *cfg, wavearray< double > *wf1, wavearray< double > *wf2, wavearray< double > *wf3, wavearray< double > *wref, bool fft=false, TString pdir="", double P=0.99, EColor col1=kRed, EColor col2=kBlue, EColor col3=(EColor) 418)
int GetLayer(int index)
Definition: sseries.hh:122
std::vector< TGraph * > graph
Definition: watplot.hh:194
bool cedDump
Definition: config.hh:297
void RestorePixels(network *NET, CWB::config *cfg, netcluster *pwc, std::vector< netpixel > *vPIX, int id)
CWB::mdc * MDC
double getTheta(size_t i)
Definition: skymap.hh:224
#define WF_OUTPUT_DAT
Definition: CWB_Plugin_WF.C:74
std::vector< double > gvTCOA
waveform wf
Long_t size
WSeries< double > waveBand
Definition: detector.hh:356
int m
Definition: cwb_net.C:28
DataType_t * pWWS
Definition: WaveDWT.hh:141
virtual void start(double s)
Definition: wavearray.hh:137
double max()
Definition: skymap.cc:438
int j
Definition: cwb_net.C:28
bool getMRAwave(size_t ID, size_t lag, char atype='S', int mode=0, bool tof=false)
Definition: network.cc:3666
i drho i
double rate
Definition: netcluster.hh:378
std::vector< double > mdcTime
Definition: network.hh:614
skymap tau
Definition: detector.hh:346
static bool checkFile(TString fName, bool question=false, TString message="")
Definition: Toolbox.cc:4670
void plot(wavearray< double > &, char *=NULL, int=1, double=0., double=0., bool=false, float=0., float=0., bool=false, float=0., bool=false)
Definition: watplot.cc:150
wavearray< int > sparseIndex
Definition: sseries.hh:180
double GetDelay(TString ifo1, TString ifo2, double phi, double theta)
Definition: mdc.cc:4523
#define WF_OUTPUT_STRAIN
Definition: CWB_Plugin_WF.C:77
bool outPlugin
Definition: config.hh:369
bool core
Definition: netpixel.hh:120
char ifo[NIFO_MAX][8]
void dopen(const char *fname, char *mode, bool header=true)
Definition: netevent.hh:312
size_t ifoListSize()
Definition: network.hh:431
float gSM_OS_OF
std::vector< wavearray< double > > vNUL
int Psave
Definition: config.hh:193
watplot p2(const_cast< char *>("TFMap2"))
wavearray< double > w
Definition: Test1.C:27
#define nIFO
void wrate(double r)
Definition: wseries.hh:120
size_t getSkyIndex(double th, double ph)
param: theta param: phi
Definition: skymap.cc:720
double tstart
std::vector< wavearray< double > > vREC
virtual size_t size() const
Definition: wavearray.hh:145
TCanvas * canvas
Definition: watplot.hh:192
float phi
gWSeries< double > gw(w)
double ra
Definition: ConvertGWGC.C:46
std::vector< SSeries< double > > xSS[NIFO_MAX]
size_t fResample
Definition: config.hh:142
x plot
wavearray< double > gHOT[NIFO_MAX]
jfile
Definition: cwb_job_obj.C:43
void output2G(TTree *, network *, size_t, int, double)
Definition: netevent.cc:711
static float _avx_packet_ps(float **p, float **q, std::vector< float *> &pAVX, int I)
Definition: watavx.hh:754
float gOF[3]
Definition: mdc.hh:248
static double GetMatchFactor(TString match, vector< wavearray< double > > &w1, vector< wavearray< double > > &w2, vector< double > tstart=DEFAULT_VECtOR_DOUBLE, vector< double > tstop=DEFAULT_VECtOR_DOUBLE)
Definition: mdc.cc:7737
Int_t Psave
number of detectors
Definition: netevent.hh:66
int simulation
Definition: config.hh:199
int l_high
Definition: config.hh:156
double gTCOA
skymap GetSkyMap(network *NET, int lag, netevent *EVT, int ID)
TTree * gTREE
#define IMPORT(TYPE, VAR)
Definition: cwb.hh:69
#define WF_OUTPUT_WHT
Definition: CWB_Plugin_WF.C:73
xtalk getXTalk(int nLay1, size_t indx1, int nLay2, size_t indx2)
param: numbers of layers identifying the resolution of the first pixel param: TF map index of the fir...
Definition: monster.cc:351
Double_t * gps
average center_of_gravity time
Definition: netevent.hh:95
double getRA(size_t i)
Definition: skymap.hh:215
wavearray< int > sparseLookup
Definition: sseries.hh:178
i() int(T_cor *100))
std::vector< SSeries< double > > vSS[NIFO_MAX]
network NET
Definition: cwb_dump_inj.C:30
wavearray< double > gMDC[NIFO_MAX]
watplot p1(const_cast< char *>("TFMap1"))
static wavearray< double > GetAligned(wavearray< double > *w1, wavearray< double > *w2)
Definition: mdc.cc:7302
std::vector< std::string > mdcList
Definition: network.hh:612
const int NIFO_MAX
Definition: wat.hh:22
void Resample(const wavearray< DataType_t > &, double, int=6)
Definition: wavearray.cc:640
int BATCH
Definition: config.hh:245
char parPlugin[1024]
Definition: config.hh:363
static void _avx_free_ps(std::vector< float *> &v)
Definition: watavx.hh:40
std::vector< WDM< double > * > wdmList
Definition: network.hh:617
#define WF_FITS_PE
Definition: CWB_Plugin_WF.C:84
double start
Definition: netcluster.hh:379
printf("total live time: non-zero lags = %10.1f \, liveTot)
int l
TIter next(twave->GetListOfBranches())
char fname[1024]
std::vector< int > RWFID
Definition: detector.hh:381
WF gWF
size_t mdc__IDSize()
Definition: network.hh:414
skymap gSM_OS
std::vector< wavearray< double > > vAUX
#define WF_OUTPUT_REC
Definition: CWB_Plugin_WF.C:72
void DumpSkymap(network *NET, int lag, netevent *EVT, int ID)
int k
std::vector< string > gvLOG
double tstop
Float_t * netcc
effective correlated SNR
Definition: netevent.hh:113
void DumpOutputFile(network *NET, netevent *&EVT, CWB::config *cfg, int ID, int k, int factor)
std::vector< wavearray< double > * > IWFP
Definition: detector.hh:379
size_t healpix
Definition: config.hh:282
std::vector< wavearray< double > > GetWhitenedData(network *NET, CWB::config *cfg)
double RA2phi(double ph, double gps)
Definition: skymap.hh:213
TObjArray * token
double acor
Definition: network.hh:585
float gRE[3]
Definition: skymap.hh:63
void ClearWaveforms(detector *ifo)
std::vector< int > IWFID
Definition: detector.hh:378
void ReadUserOptions(TString options)
std::vector< netpixel > GetCluster(network *NET, int lag, int id)
static double TimeSync(wavearray< double > &w1, wavearray< double > &w2, double &sync_time)
Definition: mdc.cc:7562
static double GetTimeBoundaries(wavearray< double > x, double P, double &bT, double &eT, double T=-1., double Q=-1.)
Definition: mdc.cc:7419
double ComputeMaxSkyMapProduct(skymap *sm1, skymap *sm2)
TFile * froot
std::vector< wavearray< double > > wAUX[PE_MAX_EVENTS]
Definition: cwb_pereport.C:325
netpixel * getPixel(size_t n, size_t i)
Definition: netcluster.hh:413
int l_low
Definition: config.hh:155
char options[256]
#define WF_GPS_PE
Definition: CWB_Plugin_WF.C:85
double getPhi(size_t i)
Definition: skymap.hh:164
WSeries< double > TFmap
Definition: detector.hh:354
void Wave2Sparse(network *NET, CWB::config *cfg, char type)
#define WF_INJ_TOFF
Definition: CWB_Plugin_WF.C:91
char title[256]
Definition: SSeriesExample.C:1
Float_t * phi
sqrt(h+*h+ + hx*hx)
Definition: netevent.hh:87
double gps
void PrintUserOptions(CWB::config *cfg)
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 T
Definition: testWDM_4.C:11
double GetInjTcoa(double geocentric_tcoa, network *NET, TString ifo, double theta, double phi)
static float _avx_setAMP_ps(float **p, float **q, std::vector< float *> &pAVX, int I)
Definition: watavx.hh:956
std::vector< wavearray< double > > GetRecWaveform(network *NET, netevent *EVT, int id)
void CWB_Plugin(TFile *jfile, CWB::config *cfg, network *NET, WSeries< double > *x, TString ifo, int type)
COHERENCE.
skymap sm1
static wavearray< double > GetDiff(wavearray< double > *w1, wavearray< double > *w2)
Definition: mdc.cc:7374
std::vector< int > sCuts
Definition: netcluster.hh:392
WSeries< double > waveForm
Definition: detector.hh:355
void dclose()
Definition: netevent.hh:321
double fHigh
Definition: config.hh:141
float gCRATE
wavearray< int > index
char Name[16]
Definition: detector.hh:327
double fabs(const Complex &x)
Definition: numpy.cc:55
wavearray< float > sparseMap00
Definition: sseries.hh:181
wavearray< float > sparseMap90
Definition: sseries.hh:182
int gIFACTOR
#define WF_GPS_OS
Definition: CWB_Plugin_WF.C:88
std::vector< netpixel > gCLUSTER
void ClearVectors()
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
std::vector< SSeries< double > > rSS[NIFO_MAX]
sprintf(tfres,"(1/%g)x(%g) (sec)x(Hz)", 2 *df, df)
static float _avx_loadata_ps(float **p, float **q, float **u, float **v, float En, std::vector< float *> &pAVX, int I)
Definition: watavx.hh:634
TString gfile
void set(size_t i, double a)
param: sky index param: value to set
Definition: skymap.hh:122
int nIFO
Definition: config.hh:120
watplot * GetWATPLOT()
Definition: gwavearray.hh:88
DataType_t * data
Definition: wavearray.hh:319
std::vector< wavearray< double > > GetWaveform(network *NET, int lag, int id, char type, bool shift=true)
const int nRES
Definition: revMonster.cc:7
Long_t id
double factors[FACTORS_MAX]
Definition: config.hh:202
double get(size_t i)
param: sky index
Definition: skymap.cc:699
WaveDWT< DataType_t > * pWavelet
Definition: wseries.hh:456
wavearray< double > gSTRAIN[NIFO_MAX]
wavearray< double > wINJ[NIFO_MAX]
void ResetUserOptions()
skymap nCorrelation
Definition: network.hh:621
#define WF_FITS_OS
Definition: CWB_Plugin_WF.C:87
Bool_t fill_in(network *, int, bool=true)
Definition: injection.cc:349
double ComputeSkyMapOF(skymap *sm1, skymap *sm2)
float rate
Definition: netpixel.hh:113
std::vector< SSeries< double > > vSS
Definition: detector.hh:352
double gSEGGPS
size_t size()
Definition: skymap.hh:136
WSeries< double > waveNull
Definition: detector.hh:357
#define WF_OUTPUT_INJ
Definition: CWB_Plugin_WF.C:71
size_t getmdc__ID(size_t n)
Definition: network.hh:426
double shift[NIFO_MAX]
bool gCEDDUMP
wavearray< double > ** pwf
[x1,y1,z1,x2,y2,z2] components of spin vector
Definition: injection.hh:101
virtual void resize(unsigned int)
Definition: wavearray.cc:463
int check
int getSTFind(double r)
Definition: detector.hh:182
wavearray< double > r90_POL[2]
buffer for standard response 00 ampl
Definition: network.hh:662
skymap nProbability
Definition: network.hh:631
int levelR
Definition: config.hh:152
std::vector< wavearray< double > > vINJ
void Draw(GWAT_DRAW type=GWAT_TIME, TString options="ALP", Color_t color=kBlack)
Definition: gwavearray.cc:89
detector ** pD
std::vector< wavearray< double > > GetAuxWaveform(network *NET, netevent *EVT, int id, double factor)
int binarySearch(int array[], int start, int end, int key)
Definition: sseries.cc:461
exit(0)
bool setdata(double a, char type='R', size_t n=0)
Definition: netpixel.hh:58
void SetOutputFile(network *NET, netevent *&EVT, CWB::config *cfg, bool dump_wf)
wavearray< double > r00_POL[2]
buffer for projection on network plane 90 ampl
Definition: network.hh:661
#define WF_OUTPUT_NUL
Definition: CWB_Plugin_WF.C:75
std::vector< wavearray< double > > vRES
bool dump
Definition: config.hh:295