Logo coherent WaveBurst  
Library Reference Guide
Logo
CWB_Plugin_Periodic_Frames.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 //!STRAIN_DATA_MANIPULATION_
20 // Plugin used to replicate periodicaly the input strain data
21 
22 #define XIFO 4
23 
24 #pragma GCC system_header
25 
26 #include "cwb.hh"
27 #include "config.hh"
28 #include "network.hh"
29 #include "wavearray.hh"
30 #include "TString.h"
31 #include "TObjArray.h"
32 #include "TObjString.h"
33 #include "TRandom.h"
34 #include "TComplex.h"
35 #include "TMath.h"
36 #include "cwb2G.hh"
37 #include <vector>
38 
39 
40 // ---------------------------------------------------------------------------------
41 // HOW TO SETUP PLUGIN STRAIN IN CWB USER CONFIGURATION (EXAMPLE)
42 // ---------------------------------------------------------------------------------
43 
44 // Set veto in the range [strain_veto_time-strain_veto_window/2, strain_veto_time+strain_veto_window/2]
45 
46 /*
47  // Example for the GW150914 event
48 
49  TString optstrain = ""; // NOTE : add space at the end of each line
50 
51  optstrain += "strain_veto_time=1126259462.404 "; // Veto GPS time
52  optstrain += "strain_veto_window=10 "; // Veto window time
53 
54  strcpy(parPlugin,optstrain.Data()); // set STRAIN plugin parameters
55  strcpy(comment,"STRAIN"); // optional
56 
57 */
58 
59 #define STRAIN_VETO_TIME 0.0
60 #define STRAIN_VETO_WINDOW 0.0
61 
62 // ---------------------------------------------------------------------------------
63 // USER STRAIN PLUGIN OPTIONS
64 // ---------------------------------------------------------------------------------
65 
66 struct uoptions {
67  double veto_time;
68  double veto_window;
69 };
70 
71 // ---------------------------------------------------------------------------------
72 // FUNCTIONS
73 // ---------------------------------------------------------------------------------
74 
75 void SetCAT2(TString ifo, CWB::config* cfg, double gps, double window=0.0);
76 void SetTimeShiftCAT2(TString ifo, CWB::config* cfg, double shift);
77 
78 void ResetUserOptions();
80 void PrintUserOptions();
81 
82 
83 // ---------------------------------------------------------------------------------
84 // Global Variables
85 // ---------------------------------------------------------------------------------
86 
87 uoptions gOPT; // global User Options
88 double strainShift;
89 
90 
91 void
93 
94  // this plugin permits to replicate periodicaly in time the same set of data declared in input frame list files
95  //
96  // the GPS range P=[frRange.start, frRange.stop] defines the Start data period to be used
97  //
98  // frRange.start frRange.stop
99  // ^ ^
100  // ...................|xxxxxx P xxxxxx|............................
101  //
102  // the period P is replicated back and forth starting from startFramesGPS
103  //
104  // frRange.start
105  // ^
106  // xxx|xxxxxx P xxxxxx|xxxxxx P xxxxxx|xxxxxx P xxxxxx|xxxxxx P xxx
107  //
108  // = J ==|== J ==|== J ==|== J ==|== J ==|== J ==|== J ==|== J ==|=
109  //
110  // N Y N Y N Y N Y
111  //
112  // WARNING: The jobs (J) with range overlapping 2 periods do not work with this method
113  // It is necessary to use a procedure which select only the working jobs
114  //
115 
116 
117  if(type==CWB_PLUGIN_CONFIG) {
118  cfg->dataPlugin=true; // disable read strain from main pipeline
119 
120  ResetUserOptions(); // set default config options
121  ReadUserOptions(cfg->parPlugin); // user config options : read from parPlugin
122  PrintUserOptions(); // print config options
123  }
124 
125  if(type==CWB_PLUGIN_INIT_JOB) {
126 
128 
129  int ifoID=0; for(int n=0;n<cfg->nIFO;n++) if(ifo==net->getifo(n)->Name) {ifoID=n;break;}
130 
131  int segEdge = int(cfg->segEdge);
132 
133  // get data range
134  cwb2G* gCWB2G; IMPORT(cwb2G*,gCWB2G)
135  double xstart = gCWB2G->Tb-segEdge;
136  double xstop = gCWB2G->Te+segEdge;
137  cout << "CWB_Plugin_Periodic_Frames.C - " << "xstart : " << int(xstart) << " xstop : " << int(xstop) << endl;
138 
139  if(TString(cfg->frFiles[ifoID])=="") {
140  cout << "CWB_Plugin_Periodic_Frames.C - Error : strain frame files list name is not defined" << endl;
141  exit(1);
142  }
143 
144  // open frame file
145  CWB::frame fr;
146  fr.open(cfg->frFiles[ifoID]);
147  fr.setVerbose();
148  fr.setRetryTime(cfg->frRetryTime);
149  fr.setSRIndex(TMath::Nint(TMath::Log2(cfg->inRate))); // force data to be resampled to inRate
150  int nfrFiles = fr.getNfiles();
151  cout << "CWB_Plugin_Periodic_Frames.C - Strain " << " -> nfrFiles : " << nfrFiles << endl;
152 
153  // read strain range from strain frl files
154  waveSegment frRange = fr.getFrRange();
155  cout << "CWB_Plugin_Periodic_Frames.C - frRange : " << frRange.start << " " << frRange.stop << endl;
156 
157  // compute strain shift time
158  double mlength = frRange.stop-frRange.start; // frame files time range
159  double toffset = xstart-frRange.start;
160  int nShift = toffset>=0 ? int(toffset/mlength) : int(toffset/mlength)-1;
161  strainShift = mlength*nShift;
162  cout << "strainShift : " << strainShift << endl;
163 
164  // ------------------------------
165  // init cat2 segments
166  // ------------------------------
167 
168  if(gOPT.veto_time>0) SetCAT2(ifo, cfg, gOPT.veto_time); // set veto CAT2 defined by the user
169  SetTimeShiftCAT2(ifo, cfg, strainShift); // apply strainShift to CAT2
170 
171  // set veto CAT2 with 2 sec window to the edges of frames
172  SetCAT2(ifo, cfg, frRange.start, 2);
173  SetCAT2(ifo, cfg, frRange.stop, 2);
174 
175  // apply CAT2
176  if(TString(ifo).CompareTo(net->ifoName[cfg->nIFO-1])==0) { // last ifo
177 
178  // get shifted merged dq cat0+cat1+cat2 list
179  vector<waveSegment> cat2List=TB.readSegList(cfg->nDQF, cfg->DQF, CWB_CAT2);
180  // store cat2List into network class
181  net->segList=cat2List;
182 
183  // check if seg+cat2 data length is >= segTHR
184  vector<waveSegment> detSegs(1);
185  detSegs[0].start = xstart;
186  detSegs[0].stop = xstop;
187  vector<waveSegment> detSegs_dq2;
188  detSegs_dq2.push_back(detSegs[0]);
189  detSegs_dq2 = TB.mergeSegLists(detSegs_dq2,cat2List);
190  for(int i=0;i<(int)detSegs_dq2.size();i++) {
191  cout << "detSegs_dq2[" << i << "] GPS range : "
192  << detSegs_dq2[i].start << "-" << detSegs_dq2[i].stop << endl;
193  }
194  cout << endl;
195  double detSegs_ctime = TB.getTimeSegList(detSegs_dq2);
196  cout << "live time after cat 2 : " << detSegs_ctime << " sec" << endl;
197  if(detSegs_ctime<cfg->segTHR) {
198  cout << "CWB_Plugin_Periodic_Frames.C : job segment live time after cat2 < segTHR="
199  << cfg->segTHR << " sec, job terminated !!!" << endl;
200  cout << endl << "To remove this check set segTHR=0 in the parameter file" << endl << endl;
201  exit(1);
202  }
203  }
204  }
205 
206  if(type==CWB_PLUGIN_STRAIN) {
207 
208  cout << "CWB_Plugin_Periodic_Frames.C : Read Strain Frames ..." << endl;
209 
210  int ifoID=0; for(int n=0;n<cfg->nIFO;n++) if(ifo==net->getifo(n)->Name) {ifoID=n;break;}
211 
212  int xstart = (int)x->start();
213  int xstop = (int)x->stop();
214  int segEdge = int(cfg->segEdge);
215 
216  // open frame file
217  CWB::frame fr;
218  fr.open(cfg->frFiles[ifoID]);
219  fr.setVerbose();
220  fr.setRetryTime(cfg->frRetryTime);
221  fr.setSRIndex(TMath::Nint(TMath::Log2(cfg->inRate))); // force data to be resampled to inRate
222  int nfrFiles = fr.getNfiles();
223  cout << "CWB_Plugin_Periodic_Frames.C - Strain " << " -> nfrFiles : " << nfrFiles << endl;
224 
225  // read frame file
226  frfile FRF = fr.getFrList(xstart-strainShift+segEdge, xstop-strainShift-segEdge, segEdge);
227  fr.readFrames(FRF,const_cast<char*>(cfg->channelNamesRaw[ifoID]),*x);
228  x->start(x->start()+strainShift);
229  if(x->rate()!=cfg->inRate)
230  {cout << "CWB_Plugin_Periodic_Frames.C - input rate from frame " << x->rate()
231  << " do not match the one defined in config : " << cfg->inRate << endl;gSystem->Exit(1);}
232  }
233 
234  return;
235 }
236 
237 void SetCAT2(TString ifo, CWB::config* cfg, double gps, double window) {
238 
239  if(gps<0) return;
240 
241  if(window<=0) window = cfg->iwindow/2.;
242 
243  // dq file list
244  // {ifo, dqcat_file, dqcat[0/1/2], shift[sec], inverse[false/true], 4columns[true/false]}
245 
246  for(int n=0; n<cfg->nIFO; n++) {
247 
248  if(ifo == cfg->DQF[n].ifo) {
249 
250  strcpy(cfg->DQF[cfg->nDQF].ifo, cfg->ifo[n]);
251  sprintf(cfg->DQF[cfg->nDQF].file, "%s/%s_%s.gps_%d",cfg->tmp_dir,cfg->ifo[n],cfg->data_label,int(gps));
252  cfg->DQF[cfg->nDQF].cat = CWB_CAT2;
253  cfg->DQF[cfg->nDQF].shift = 0.;
254  cfg->DQF[cfg->nDQF].invert = true;
255  cfg->DQF[cfg->nDQF].c4 = true;
256  cfg->nDQF++;
257 
258  cout << cfg->DQF[cfg->nDQF-1].file << endl;
259 
260  ofstream out;
261  out.open(cfg->DQF[cfg->nDQF-1].file,ios::out);
262  cout << "Write file : " << cfg->DQF[cfg->nDQF-1].file << endl;
263  if (!out.good()) {cout << "Error Opening File : " << cfg->DQF[cfg->nDQF-1].file << endl;exit(1);}
264  out.precision(14);
265  int istart = int(gps)-window;
266  int istop = int(gps)+window;
267  out << "1 " << istart << " " << istop << " " << 2*window << endl;
268  out.close();
269  }
270  }
271 }
272 
274 
275  // dq file list
276  // {ifo, dqcat_file, dqcat[0/1/2], shift[sec], inverse[false/true], 4columns[true/false]}
277 
278  for(int n=0; n<(cfg->nDQF); n++) {
279  if(ifo == cfg->DQF[n].ifo) cfg->DQF[n].shift += shift;
280  }
281 }
282 
284 
285  // get plugin options
286 
287  if(TString(options)!="") {
288 
289  //cout << "STRAIN options : " << options << endl;
290  TObjArray* token = TString(options).Tokenize(TString(' '));
291  for(int j=0;j<token->GetEntries();j++) {
292 
293  TObjString* tok = (TObjString*)token->At(j);
294  TString stok = tok->GetString();
295 
296  if(stok.Contains("strain_veto_time=")) {
297  TString strain_veto_time=stok;
298  strain_veto_time.Remove(0,strain_veto_time.Last('=')+1);
299  if(strain_veto_time.IsFloat()) gOPT.veto_time=strain_veto_time.Atof();
300  }
301 
302  if(stok.Contains("strain_veto_window=")) {
303  TString strain_veto_window=stok;
304  strain_veto_window.Remove(0,strain_veto_window.Last('=')+1);
305  if(strain_veto_window.IsFloat()) gOPT.veto_window=strain_veto_window.Atof();
306  }
307  }
308  }
309 }
310 
312 
313  gOPT.veto_time = STRAIN_VETO_TIME;
314  gOPT.veto_window = STRAIN_VETO_WINDOW;
315 }
316 
318 
319  cout.precision(14);
320  cout << "-----------------------------------------" << endl;
321  cout << "STRAIN config options " << endl;
322  cout << "-----------------------------------------" << endl << endl;
323  cout << "STRAIN_VETO_TIME " << gOPT.veto_time << endl;
324  cout << "STRAIN_VETO_WINDOW " << gOPT.veto_window << endl;
325  cout << endl;
326 }
327 
std::vector< char * > ifoName
Definition: network.hh:609
detector * getifo(size_t n)
param: detector index
Definition: network.hh:436
CWB::config * cfg
double Te
Definition: cwb.hh:282
double iwindow
Definition: config.hh:200
double start
Definition: network.hh:55
void PrintUserOptions()
virtual void rate(double r)
Definition: wavearray.hh:141
void CWB_Plugin(TFile *jfile, CWB::config *cfg, network *net, WSeries< double > *x, TString ifo, int type)
COHERENCE.
bool dataPlugin
Definition: config.hh:364
int n
Definition: cwb_net.C:28
bool invert
Definition: Toolbox.hh:88
TString("c")
char ifo[32]
Definition: Toolbox.hh:84
double Tb
Definition: cwb.hh:281
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
bool c4
Definition: Toolbox.hh:89
CWB::frame fr(FRLIST_NAME)
CWB::Toolbox TB
dqfile DQF[DQF_MAX]
Definition: config.hh:349
static double getTimeSegList(vector< waveSegment > list)
Definition: Toolbox.cc:611
virtual void start(double s)
Definition: wavearray.hh:137
vector< waveSegment > detSegs_dq2
Definition: cwb_net.C:297
double segEdge
Definition: test_config1.C:49
int j
Definition: cwb_net.C:28
i drho i
double segTHR
Definition: config.hh:163
double strainShift
char ifo[NIFO_MAX][8]
Definition: cwb2G.hh:33
network ** net
NOISE_MDC_SIMULATION.
waveSegment getFrRange()
Definition: frame.hh:107
double segTHR
Definition: test_config1.C:48
double segEdge
Definition: config.hh:164
CWB_CAT cat
Definition: Toolbox.hh:86
char file[1024]
Definition: Toolbox.hh:85
jfile
Definition: cwb_job_obj.C:43
void ReadUserOptions(TString options)
#define STRAIN_VETO_WINDOW
int nDQF
Definition: config.hh:348
#define IMPORT(TYPE, VAR)
Definition: cwb.hh:69
void SetTimeShiftCAT2(TString ifo, CWB::config *cfg, double shift)
i() int(T_cor *100))
char tmp_dir[1024]
Definition: config.hh:325
char parPlugin[1024]
Definition: config.hh:363
void setVerbose(bool verbose=true)
Definition: frame.hh:137
double shift
Definition: Toolbox.hh:87
char channelNamesRaw[NIFO_MAX][50]
Definition: config.hh:310
frfile FRF[nIFO+1]
Definition: cwb_net.C:269
int frRetryTime
Definition: config.hh:344
int getNfiles()
Definition: frame.hh:110
std::vector< waveSegment > segList
Definition: network.hh:616
int nfrFiles[nIFO+1]
Definition: cwb_net.C:192
TObjArray * token
void ResetUserOptions()
void setSRIndex(int srIndex)
Definition: frame.hh:114
int * xstart
vector< waveSegment > detSegs
Definition: cwb_net.C:193
char options[256]
static vector< waveSegment > readSegList(dqfile DQF)
Definition: Toolbox.cc:409
double gps
char Name[16]
Definition: detector.hh:327
virtual void stop(double s)
Definition: wavearray.hh:139
char ifo[NIFO_MAX][8]
Definition: config.hh:124
void readFrames(char *filename, char *channel, wavearray< double > &w)
Definition: frame.cc:828
strcpy(RunLabel, RUN_LABEL)
cout<< "total cat1 livetime : "<< int(cat1_time)<< " sec "<< cat1_time/3600.<< " h "<< cat1_time/86400.<< " day"<< endl;cout<< endl;vector< waveSegment > cat2List
Definition: cwb_dump_job.C:40
sprintf(tfres,"(1/%g)x(%g) (sec)x(Hz)", 2 *df, df)
frfile getFrList(int istart, int istop, int segEdge)
Definition: frame.cc:527
static vector< waveSegment > mergeSegLists(vector< waveSegment > &ilist1, vector< waveSegment > &ilist2)
Definition: Toolbox.cc:350
uoptions gOPT
int nIFO
Definition: config.hh:120
double detSegs_ctime
Definition: cwb_net.C:303
char data_label[1024]
Definition: config.hh:332
void setRetryTime(int frRetryTime=60)
Definition: frame.hh:141
void open(TString ioFile, TString chName="", Option_t *option="", bool onDisk=false, TString label=".gwf", unsigned int mode=0)
Definition: frame.cc:230
double shift[NIFO_MAX]
double stop
Definition: network.hh:56
size_t inRate
Definition: config.hh:132
char frFiles[2 *NIFO_MAX][1024]
Definition: config.hh:342
exit(0)
#define STRAIN_VETO_TIME
void SetCAT2(TString ifo, CWB::config *cfg, double gps, double window=0.0)