Logo coherent WaveBurst  
Library Reference Guide
Logo
CWB_Plugin_WavePeaks.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 "config.hh"
25 #include "network.hh"
26 #include "wavearray.hh"
27 #include "TString.h"
28 #include "TObjArray.h"
29 #include "TObjString.h"
30 #include "TRandom.h"
31 #include "TComplex.h"
32 #include "TMath.h"
33 #include "mdc.hh"
34 #include "watplot.hh"
35 #include "gwavearray.hh"
36 #include <vector>
37 
38 //#define SAVE_WHT_PLOT // enable event WHITE plots
39 
40 #define nPEAK 10 // number of peaks to be extracted from the reconstructed waveform
41 
42 void GetGlitchParams(wavearray<double>* wf, int ifoID, float PF[][nPEAK],
43  float PB[][nPEAK], float PA[][nPEAK], float PE[][nPEAK]);
45  CWB::config* cfg, bool fft=false, bool strain=false);
46 
47 void
49 //!MISCELLANEA
50 // Extract whitened reconstructed waveforms, and compute the first nPEAK parameters
51 // Save peak parameters to the output wave root file
52 
53  cout << endl;
54  cout << "-----> CWB_Plugin_WavePeaks.C" << endl;
55  cout << "ifo " << ifo.Data() << endl;
56  cout << "type " << type << endl;
57  cout << endl;
58 
59  int nP = nPEAK; // number of peaks
60  float PF[NIFO_MAX][nPEAK]; // peak frequency
61  float PB[NIFO_MAX][nPEAK]; // peak bandwidth
62  float PA[NIFO_MAX][nPEAK]; // peak amplitude/(max signal amplitude)
63  float PE[NIFO_MAX][nPEAK]; // peak energy/(signal energy)
64 
65  if(type==CWB_PLUGIN_CONFIG) {
66  cfg->outPlugin=true; // disable built-in output root file
67  }
68 
69  if(type==CWB_PLUGIN_ILIKELIHOOD) {
70  NET->wfsave=true; // enable waveform save
71  }
72 
73  if(type==CWB_PLUGIN_OLIKELIHOOD) {
74 
75  if(TString(cfg->analysis)!="2G") {
76  cout << "CWB_Plugin_WavePeaks.C -> "
77  << "CWB_PLUGIN_OLIKELIHOOD implemented only for 2G" << endl;
78  gSystem->Exit(1);
79  }
80 
81  // import ifactor
82  int gIFACTOR=-1; IMPORT(int,gIFACTOR)
83  cout << "-----> CWB_Plugin_WavePeaks.C -> "
84  << " gIFACTOR : " << gIFACTOR << endl;
85 
86  // import slagShift
87  float* gSLAGSHIFT=NULL; IMPORT(float*,gSLAGSHIFT)
88 
89  int nIFO = NET->ifoListSize(); // number of detectors
90  int K = NET->nLag; // number of time lag
91  netevent* EVT;
93  double factor = cfg->simulation==3||cfg->simulation==4 ? -gIFACTOR : cfg->factors[gIFACTOR];
94  int rate = 0; // select all resolutions
95 
96  // search output root file in the system list
97  TFile* froot = NULL;
98  TList *files = (TList*)gROOT->GetListOfFiles();
99  TString outDump="";
100  if (files) {
101  TIter next(files);
102  TSystemFile *file;
103  TString fname;
104  bool check=false;
105  while ((file=(TSystemFile*)next())) {
106  fname = file->GetName();
107  // set output root file as the current file
108  if(fname.Contains("wave_")) {
109  froot=(TFile*)file;froot->cd();
110  outDump=fname;
111  outDump.ReplaceAll(".root.tmp",".txt");
112  //cout << "output file name : " << fname << endl;
113  }
114  }
115  if(!froot) {
116  cout << "CWB_Plugin_WavePeaks.C : Error - output root file not found" << endl;
117  gSystem->Exit(1);
118  }
119  } else {
120  cout << "CWB_Plugin_WavePeaks.C : Error - output root file not found" << endl;
121  gSystem->Exit(1);
122  }
123 
124  TTree* net_tree = (TTree *) froot->Get("waveburst");
125  if(net_tree!=NULL) {
126  EVT = new netevent(net_tree,nIFO);
127  net_tree->SetBranchAddress("nP",&nP);
128  net_tree->SetBranchAddress("PF",PF);
129  net_tree->SetBranchAddress("PB",PB);
130  net_tree->SetBranchAddress("PA",PA);
131  net_tree->SetBranchAddress("PE",PE);
132  } else {
133  EVT = new netevent(nIFO);
134  net_tree = EVT->setTree();
135  net_tree->Branch("nP",&nP,"nP/I");
136  net_tree->Branch("PF",PF,TString::Format("PF[%i][%i]/F",cfg->nIFO,nPEAK));
137  net_tree->Branch("PB",PB,TString::Format("PB[%i][%i]/F",cfg->nIFO,nPEAK));
138  net_tree->Branch("PA",PA,TString::Format("PA[%i][%i]/F",cfg->nIFO,nPEAK));
139  net_tree->Branch("PE",PE,TString::Format("PE[%i][%i]/F",cfg->nIFO,nPEAK));
140  }
141  EVT->setSLags(gSLAGSHIFT); // set slags into netevent
142 
143  for(int k=0; k<K; k++) { // loop over the lags
144 
145  id = NET->getwc(k)->get(const_cast<char*>("ID"), 0, 'L', rate);
146 
147  for(int j=0; j<(int)id.size(); j++) { // loop over cluster index
148 
149  int ID = size_t(id.data[j]+0.5);
150 
151  if(NET->getwc(k)->sCuts[ID-1]!=-1) continue; // skip rejected/processed clusters
152 
153  double ofactor=0;
154  if(cfg->simulation==4) ofactor=-factor;
155  else if(cfg->simulation==3) ofactor=-gIFACTOR;
156  else ofactor=factor;
157 
158  EVT->output2G(NULL,NET,ID,k,ofactor); // get reconstructed parameters
159 
160  wavearray<double>** pwfREC = new wavearray<double>*[nIFO];
161  detector* pd = NET->getifo(0);
162  int idSize = pd->RWFID.size();
163 
164  int wfIndex=-1;
165  for (int mm=0; mm<idSize; mm++) if (pd->RWFID[mm]==ID) wfIndex=mm;
166  if(wfIndex==-1) continue;
167 
168  // extract whitened reconstructed waveforms
169  for(int n=0; n<nIFO; n++) {
170 
171  pd = NET->getifo(n);
172 
173  pwfREC[n] = pd->RWFP[wfIndex];
174  wavearray<double>* wfREC = pwfREC[n]; // array of reconstructed waveforms
175 
176 #ifdef SAVE_WHT_PLOT
177  //PlotWaveform(NET->ifoName[n], wfREC, cfg, false, false);
178  PlotWaveform(NET->ifoName[n], wfREC, cfg, true, false);
179 #endif
180 
181  for(int i=0;i<nPEAK;i++) {
182  GetGlitchParams(wfREC, n, PF, PB, PA, PE);
183 // printf("%d -> \t %5.1f \t %5.1f \t %3.2f \t %3.2f\n",
184 // i,PF[n][i],PB[n][i],PA[n][i],PE[n][i]);
185  }
186  }
187  delete [] pwfREC;
188 
189  std::vector<int> sCuts = NET->getwc(k)->sCuts; // save cCuts
190  // set sCuts=1 to the events which must be not copied with cps to pwc
191  for(int i=0; i<(int)sCuts.size(); i++) if(i!=ID-1) NET->getwc(k)->sCuts[i]=1;
192 
193  // ID can not be used to get the event, to get event use ID=1 (ex: for watplot)
194  NET->getwc(k)->sCuts = sCuts; // restore cCuts
195 
196  if(cfg->dump) EVT->dopen(outDump.Data(),const_cast<char*>("a"),false);
197  EVT->output2G(net_tree,NET,ID,k,ofactor); // get reconstructed parameters
198  if(cfg->dump) EVT->dclose();
199  if(!cfg->cedDump) NET->getwc(k)->sCuts[ID-1]=1; // mark as processed
200  }
201  }
202 
203  jfile->cd();
204  if(EVT) delete EVT;
205  }
206  return;
207 }
208 
209 void
211  float PB[][nPEAK], float PA[][nPEAK], float PE[][nPEAK]) {
212 
213  int size = wf->size()/2;
214  double Fs=((double)wf->rate()/(double)(2*size));
215 
216  wavearray<float> ener(size);
217 
218  wf->FFTW(1);
219  for(int j=0;j<size;j++) ener[j]=pow(wf->data[2*j],2)+pow(wf->data[2*j+1],2);
220  wf->FFTW(-1);
221  wf->resetFFTW();
222 
223  // compute total signal energy
224  double ET=0;
225  for(int j=0;j<size;j++) ET+=ener[j];
226  // compute max signal amplitude
227  double MA=0;
228  for(int j=0;j<size;j++) if(ener[j]>MA) MA=ener[j];
229  MA=sqrt(MA);
230 
231  for(int k=0;k<nPEAK;k++) {
232 
233  // find frequency @ max energy
234  float ME=0; // peak max energy
235  int jME=0; // peak max energy index
236  for(int j=0;j<size;j++) if(ener[j]>ME) {ME=ener[j];jME=j;}
237 
238  // compute peak energy
239  float fmean=ME*Fs*jME;
240  float frms=ME*pow(Fs*jME,2);
241  double EP=ME;
242  double ge=ME;
243  ener[jME]=0;
244  float finf=0;
245  for(int j=jME-1;j>=0;j--) {
246  float E = ener[j];
247  float F = Fs*j;
248  if(E<ge) {EP+=E;finf=F;fmean+=E*F;frms+=E*F*F;ener[j]=0;} else break;
249  ge=E;
250  }
251  ge = ME;
252  float fsup=size*Fs;
253  for(int j=jME+1;j<size;j++) {
254  float E = ener[j];
255  float F = Fs*j;
256  if(E<ge) {EP+=E;fsup=F;fmean+=E*F;frms+=E*F*F;ener[j]=0;} else break;
257  ge=E;
258  }
259 
260  fmean = fmean/EP;
261  frms = frms/EP-fmean*fmean;
262  frms = frms>0 ? sqrt(frms) : 0;
263 
264  PF[ifoID][k] = fmean;
265  PA[ifoID][k] = sqrt(ME)/MA;
266  PB[ifoID][k] = frms;
267  PE[ifoID][k] = ET>0 ? EP/ET : 0;
268  }
269 }
270 
271 void
273  CWB::config* cfg, bool fft, bool strain) {
274 
275  watplot PTS(const_cast<char*>("ptswrc"),200,20,800,500);
276 
277  //cout << "Print " << fname << endl;
278  double tmin = wfREC->start();
279  wfREC->start(wfREC->start()-tmin);
280  if(fft) {
281  PTS.plot(wfREC, const_cast<char*>("ALP"), 1, 0, 0, true, cfg->fLow, cfg->fHigh);
282  } else {
283  PTS.plot(wfREC, const_cast<char*>("ALP"), 1, 0, 0);
284  }
285  PTS.graph[0]->SetLineWidth(1);
286  wfREC->start(wfREC->start()+tmin);
287 
288  char label[64]="";
289  if(fft) sprintf(label,"%s","fft");
290  else sprintf(label,"%s","time");
291  if(strain) sprintf(label,"%s_%s",label,"strain");
292  else sprintf(label,"%s_%s",label,"white");
293 
294  char fname[1024];
295  sprintf(fname, "%s_wf_%s_rec_gps_%d.root",ifo.Data(),label,int(tmin));
296  PTS.canvas->Print(fname);
297  cout << "write : " << fname << endl;
298  //PTS.canvas->Write(REPLACE(fname,dirCED,gtype));
299 }
std::vector< char * > ifoName
Definition: network.hh:609
detector * getifo(size_t n)
param: detector index
Definition: network.hh:436
CWB::config * cfg
char analysis[8]
Definition: config.hh:117
TString outDump
size_t nLag
Definition: network.hh:573
void setSLags(float *slag)
Definition: netevent.cc:426
#define nPEAK
virtual void rate(double r)
Definition: wavearray.hh:141
float factor
void PlotWaveform(TString ifo, wavearray< double > *wfREC, CWB::config *cfg, bool fft=false, bool strain=false)
int n
Definition: cwb_net.C:28
TTree * setTree()
Definition: netevent.cc:434
TString("c")
int ID
Definition: TestMDC.C:70
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
std::vector< wavearray< double > * > RWFP
Definition: detector.hh:382
double fLow
Definition: config.hh:140
std::vector< TGraph * > graph
Definition: watplot.hh:194
bool cedDump
Definition: config.hh:297
waveform wf
Long_t size
virtual void start(double s)
Definition: wavearray.hh:137
int j
Definition: cwb_net.C:28
i drho i
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
bool outPlugin
Definition: config.hh:369
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
#define nIFO
virtual size_t size() const
Definition: wavearray.hh:145
TCanvas * canvas
Definition: watplot.hh:192
jfile
Definition: cwb_job_obj.C:43
void GetGlitchParams(wavearray< double > *wf, int ifoID, float PF[][nPEAK], float PB[][nPEAK], float PA[][nPEAK], float PE[][nPEAK])
void output2G(TTree *, network *, size_t, int, double)
Definition: netevent.cc:711
TTree * net_tree
int simulation
Definition: config.hh:199
#define IMPORT(TYPE, VAR)
Definition: cwb.hh:69
i() int(T_cor *100))
network NET
Definition: cwb_dump_inj.C:30
TString label
Definition: MergeTrees.C:21
const int NIFO_MAX
Definition: wat.hh:22
TIter next(twave->GetListOfBranches())
char fname[1024]
std::vector< int > RWFID
Definition: detector.hh:381
int k
virtual void resetFFTW()
Definition: wavearray.cc:977
double F
TFile * froot
virtual void FFTW(int=1)
Definition: wavearray.cc:896
bool wfsave
Definition: network.hh:600
netevent EVT(itree, nifo)
std::vector< int > sCuts
Definition: netcluster.hh:392
void dclose()
Definition: netevent.hh:321
double fHigh
Definition: config.hh:141
int gIFACTOR
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 nIFO
Definition: config.hh:120
DataType_t * data
Definition: wavearray.hh:319
Long_t id
double factors[FACTORS_MAX]
Definition: config.hh:202
int check
void CWB_Plugin(TFile *jfile, CWB::config *cfg, network *NET, WSeries< double > *x, TString ifo, int type)
COHERENCE.
bool dump
Definition: config.hh:295