Logo coherent WaveBurst  
Library Reference Guide
Logo
CWB_Plugin_TukeyWindow.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 "TGraphAsymmErrors.h"
33 #include "TMath.h"
34 #include "mdc.hh"
35 #include "cwb2G.hh"
36 #include "watplot.hh"
37 #include "gwavearray.hh"
38 #include "network.hh"
39 #include <fstream>
40 #include <vector>
41 
42 
43 // ---------------------------------------------------------------------------------
44 // HOW TO SETUP PLUGIN WF IN CWB USER CONFIGURATION (EXAMPLE)
45 // ---------------------------------------------------------------------------------
46 
47 /*
48  // Example for the L1 Glitch in GW170817
49 
50  TString opttw = ""; // NOTE : add space at the end of each line
51 
52  opttw += "tw_gps_time=1187008881.38 "; // TW central GPS time
53  opttw += "tw_width=0.34 "; // TW width
54  opttw += "tw_range=0.14 "; // TW range
55  opttw += "tw_ced_spectrogram_zmax=1e-3 "; // TW set zmax in CED spectrograms
56  opttw += "tw_dump=false "; // TW dump ascii file
57 
58  strcpy(parPlugin,opttw.Data()); // set TW plugin parameters
59  strcpy(comment,"TW");
60 
61 */
62 
63 // ---------------------------------------------------------------------------------
64 // TUKEY WINDOW DEFINES
65 //
66 // GPS_TIME
67 // |
68 // ^
69 // -----> WIDTH <------
70 // \ /
71 // \ /
72 // --------> RANGE <--------
73 //
74 // ---------------------------------------------------------------------------------
75 
76 #define TW_GPS_TIME 0.0
77 #define TW_WIDTH 0.0
78 #define TW_RANGE 0.0
79 #define TW_CED_SPECTROGRAM_ZMAX 0.0
80 #define TW_DUMP false // true -> window is dumped to ascii file
81 
82 // ---------------------------------------------------------------------------------
83 // USER TW PLUGIN OPTIONS
84 // ---------------------------------------------------------------------------------
85 
86 struct uoptions {
87  double gps_time;
88  double width;
89  double range;
90  double ced_spectrogram_zmax;
91  bool dump;
92 };
93 
94 // ---------------------------------------------------------------------------------
95 // FUNCTIONS
96 // ---------------------------------------------------------------------------------
97 
98 void ResetUserOptions();
100 void PrintUserOptions();
101 
102 double TukeyWin(double x, double W, double R);
103 void DumpData(wavearray<double>* x , TString ofname);
104 
105 // ---------------------------------------------------------------------------------
106 // Global Variables
107 // ---------------------------------------------------------------------------------
108 
109 uoptions gOPT; // global User Options
110 
111 
112 
113 void
115 //!MISCELLANEA
116 // Plugin used to remove the L1 GW170817 glitch
117 
118  char ofname[256];
119 
120  if(type==CWB_PLUGIN_CONFIG) {
121 
122  ResetUserOptions(); // set default config options
123  ReadUserOptions(cfg->parPlugin); // user config options : read from parPlugin
124  PrintUserOptions(); // print config options
125 
126  if(gOPT.gps_time==0) {
127  cout << "CWB_Plugin_TukeyWindow Error : tw_gps_time not defined !!!" << endl;
128  exit(1);
129  }
130 
131  if(gOPT.width==0) {
132  cout << "CWB_Plugin_TukeyWindow Error : tw_width not defined !!!" << endl;
133  exit(1);
134  }
135 
136  if(gOPT.range==0) {
137  cout << "CWB_Plugin_TukeyWindow Error : tw_range not defined !!!" << endl;
138  exit(1);
139  }
140  }
141 
142  if(type==CWB_PLUGIN_STRAIN) {
143  double start = x->start();
144  double stop = x->start()+x->size()/x->rate();
145  if(!(gOPT.gps_time>start && gOPT.gps_time<stop)) return;
146  if(ifo=="L1") {
147  cout << endl << "CWB_Plugin_TukeyWindow.C : Apply Tukey Window to GW170817 L1 Glitch !!!" << endl << endl;
148 
149  if(gOPT.dump) {
150  sprintf(ofname,"%s_TukeyWindow.dat",ifo.Data());
151  DumpData(x, ofname);
152  }
153 
154  wavearray<double> TW = *x;
155  double dt=1./x->rate();
156  for(int i=0;i<x->size();i++) {
157  double t = start+i*dt-(gOPT.gps_time-gOPT.width/2);
158  double w = 1-TukeyWin(t,gOPT.width,gOPT.range);
159  x->data[i]*=w;
160  TW[i]=w;
161  }
162 
163  if(gOPT.dump) {
164  sprintf(ofname,"%s_TukeyWindow.dat",ifo.Data());
165  DumpData(&TW, ofname);
166 
167  sprintf(ofname,"%s_TukeyWindow.dat",ifo.Data());
168  DumpData(x, ofname);
169  }
170  }
171  }
172 
173  if(type==CWB_PLUGIN_CED) {
174  // get object CED pointer
175  cwb2G* gCWB2G; IMPORT(cwb2G*,gCWB2G)
176  CWB::ced* ced = gCWB2G->GetCED();
177  char ced_options[64];
178  sprintf(ced_options,"ced_spectrogram_zmax=%f",gOPT.ced_spectrogram_zmax);
179  if(ced!=NULL) ced->SetOptions(ced_options);
180  }
181 }
182 
183 double TukeyWin(double x, double W, double R) {
184 
185  double y=0;
186 
187  if(x<0 || x>W) return y;
188 
189  double Pi = TMath::Pi();
190 
191  if(x>=0 && x<R/2 ) y = 0.5*(1+cos(2*Pi/R*(x-R/2)));
192  if(x>= R/2 && x<W-R/2) y = 1;
193  if(x>=(W-R/2) && x<=W ) y = 0.5*(1+cos(2*Pi/R*(x-W+R/2)));
194 
195  return y;
196 }
197 
198 void DumpData(wavearray<double>* x , TString ofname) {
199 
200  ofstream out;
201  out.open(ofname.Data(),ios::out);
202  if (!out.good()) {cout << "CWB_Plugin_TukeyWindow.C : Error Opening Output File : " << ofname << endl;exit(1);}
203  cout << "Create Output File : " << ofname << endl;
204  out.precision(19);
205 
206  // write data
207  int size = x->size();
208  double dt=1./x->rate();
209  for (int i=0; i<size; i++) {
210  double start = gOPT.gps_time-x->start()-1;
211  double stop = start+2;
212  double time = i*dt;
213  if(time>start && time<stop) out << time << " " << x->data[i] << endl;
214  }
215 
216  out.close();
217 }
218 
220 
221  // get plugin options
222 
223  if(TString(options)!="") {
224 
225  //cout << "SGW options : " << options << endl;
226  TObjArray* token = TString(options).Tokenize(TString(' '));
227  for(int j=0;j<token->GetEntries();j++) {
228 
229  TObjString* tok = (TObjString*)token->At(j);
230  TString stok = tok->GetString();
231 
232  if(stok.Contains("tw_dump=")) {
233  TString tw_dump=stok;
234  tw_dump.Remove(0,tw_dump.Last('=')+1);
235  if(tw_dump=="root") gOPT.dump=true;
236  }
237 
238  if(stok.Contains("tw_gps_time=")) {
239  TString tw_gps_time=stok;
240  tw_gps_time.Remove(0,tw_gps_time.Last('=')+1);
241  if(tw_gps_time.IsFloat()) gOPT.gps_time=tw_gps_time.Atof();
242  }
243 
244  if(stok.Contains("tw_width=")) {
245  TString tw_width=stok;
246  tw_width.Remove(0,tw_width.Last('=')+1);
247  if(tw_width.IsFloat()) gOPT.width=tw_width.Atof();
248  }
249 
250  if(stok.Contains("tw_range=")) {
251  TString tw_range=stok;
252  tw_range.Remove(0,tw_range.Last('=')+1);
253  if(tw_range.IsFloat()) gOPT.range=tw_range.Atof();
254  }
255 
256  if(stok.Contains("tw_ced_spectrogram_zmax=")) {
257  TString tw_ced_spectrogram_zmax=stok;
258  tw_ced_spectrogram_zmax.Remove(0,tw_ced_spectrogram_zmax.Last('=')+1);
259  if(tw_ced_spectrogram_zmax.IsFloat()) gOPT.ced_spectrogram_zmax=tw_ced_spectrogram_zmax.Atof();
260  }
261  }
262  }
263 }
264 
266 
267  gOPT.gps_time = TW_GPS_TIME;
268  gOPT.width = TW_WIDTH;
269  gOPT.range = TW_RANGE;
270  gOPT.range = TW_CED_SPECTROGRAM_ZMAX;
271  gOPT.dump = TW_DUMP;
272 }
273 
274 
276 
277  cout.precision(14);
278  cout << "-----------------------------------------" << endl;
279  cout << "TW config options " << endl;
280  cout << "-----------------------------------------" << endl << endl;
281  cout << "TW_GPS_TIME " << gOPT.gps_time << endl;
282  cout << "TW_WIDTH " << gOPT.width << endl;
283  cout << "TW_RANGE " << gOPT.range << endl;
284  cout << "TW_CED_SPECTROGRAM_ZMAX " << gOPT.ced_spectrogram_zmax << endl;
285  cout << "TW_DUMP " << gOPT.dump << endl;
286  cout << endl;
287 }
288 
wavearray< double > t(hp.size())
#define TW_GPS_TIME
void SetOptions(int simulation, double rho, double inRate, bool useSparse=false, char *gtype=const_cast< char *>("png"), int paletteId=0)
Definition: ced.hh:88
CWB::config * cfg
#define TW_RANGE
virtual void rate(double r)
Definition: wavearray.hh:141
TString("c")
#define TW_WIDTH
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
Long_t size
virtual void start(double s)
Definition: wavearray.hh:137
int j
Definition: cwb_net.C:28
i drho i
void PrintUserOptions()
char ifo[NIFO_MAX][8]
Definition: cwb2G.hh:33
wavearray< double > w
Definition: Test1.C:27
virtual size_t size() const
Definition: wavearray.hh:145
CWB::ced * GetCED()
Definition: cwb2G.hh:56
void CWB_Plugin(TFile *jfile, CWB::config *cfg, network *NET, WSeries< double > *x, TString ifo, int type)
COHERENCE.
jfile
Definition: cwb_job_obj.C:43
#define IMPORT(TYPE, VAR)
Definition: cwb.hh:69
network NET
Definition: cwb_dump_inj.C:30
double Pi
double TukeyWin(double x, double W, double R)
char parPlugin[1024]
Definition: config.hh:363
uoptions gOPT
#define TW_CED_SPECTROGRAM_ZMAX
void ReadUserOptions(TString options)
TObjArray * token
void ResetUserOptions()
double dt
char options[256]
sprintf(tfres,"(1/%g)x(%g) (sec)x(Hz)", 2 *df, df)
#define TW_DUMP
bool dump
DataType_t * data
Definition: wavearray.hh:319
Definition: ced.hh:44
wavearray< double > y
Definition: Test10.C:31
exit(0)
void DumpData(wavearray< double > *x, TString ofname)