Logo coherent WaveBurst  
Library Reference Guide
Logo
CWB_Plugin_Recolor.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 "Toolbox.hh"
32 
34 void PrintUserOptions(CWB::config* cfg, int ifoId);
35 
36 // ---------------------------------------------------------------------------------
37 // plugin parameters
38 // ---------------------------------------------------------------------------------
39 
40 TString frDir[NIFO_MAX]; // output directory for frame files
41 TString frName[NIFO_MAX]; // name to tag the frame
42 TString chName[NIFO_MAX]; // name to tag the frame channel data
43 TString frLabel[NIFO_MAX]; // label used to tag the output frame file name
44  // Ex: frDir/ifo_frLabel-GPS-LENGTH.gwf
45 TString psdFile[NIFO_MAX]; // file used to recolor the data
46 
47 // NOTE : cwb whitened data is 0 for freq<16
48 // for freq<padFreq the amplitude in frequency domain is padded
49 // with a value given by -> (famplitude at padFreq) * padFactor
50 
53 
54 bool gausNoise[NIFO_MAX]; // true/false -> gaussian/real data colored noise
55 
56 /* Example : parPlugin definition in user_parameters.C file for L1,H1 network
57  Note : definitions must be repeated for each detector
58 
59  TString optrc = ""; // NOTE : add space at the end of each line
60  optrc += "frDir=/home/vedovato/WP/RECOLOR/O1_C01_RECOLOR_tst1/recolored/L1 ";
61  optrc += "frDir=/home/vedovato/WP/RECOLOR/O1_C01_RECOLOR_tst1/recolored/H1 ";
62  optrc += "frLabel=HOFT_C01 ";
63  optrc += "frLabel=HOFT_C01 ";
64  optrc += "chName=L1:RECOLOR-CALIB_STRAIN_C01 ";
65  optrc += "chName=H1:RECOLOR-CALIB_STRAIN_C01 ";
66  optrc += "frName=LLO_4k ";
67  optrc += "frName=LHO_4k ";
68  optrc += "psdFile=$HOME_CWB/plugins/strains/advLIGO_NSNS_Opt_8khz_one_side.txt ";
69  optrc += "psdFile=$HOME_CWB/plugins/strains/advLIGO_NSNS_Opt_8khz_one_side.txt ";
70  optrc += "padFreq=20 ";
71  optrc += "padFreq=20 ";
72  optrc += "padFactor=1000 ";
73  optrc += "padFactor=1000 ";
74  optrc += "gausNoise=false ";
75  optrc += "gausNoise=false ";
76 
77  strcpy(parPlugin,optrc.Data()); // set plugin parameters
78 */
79 /* Note : define DQF array with CAT0 DQ files
80 
81  // dq file list
82  // {ifo, dqcat_file, dqcat[0/1/2], shift[sec], inverse[false/true], 4columns[true/false]}
83  nDQF=2;
84  dqfile dqf[nDQF]={
85  {"L1" ,"/home/vedovato/O1/DQvetos/O1_12Sep19Jan_C01/L1Cat0.txt", CWB_CAT0, 0., false, false},
86  {"H1" ,"/home/vedovato/O1/DQvetos/O1_12Sep19Jan_C01/H1Cat0.txt", CWB_CAT0, 0., false, false}
87  };
88  for(int i=0;i<nDQF;i++) DQF[i]=dqf[i];
89 */
90 
91 void
93 //!MISCELLANEA
94 // Plugin to produce recolored frames of whitened data
95 // First data are whitened then are recolored and save to file frames
96 //
97 
98  cout << endl;
99  cout << "-----> CWB_Plugin_Recolor.C" << endl;
100  cout << "ifo " << ifo.Data() << endl;
101  cout << "type " << type << endl;
102  cout << endl;
103 
105 
106  if(type==CWB_PLUGIN_CONFIG) {
107  cfg->levelR=0; // no resampling, data are whitened with yjr original sample rate
108  cfg->simulation = 0; // force non simulation mode
109  cfg->nfactor=1;
110 
111  ReadUserOptions(cfg); // user config options : read from parPlugin
112  }
113 
114  if(type==CWB_PLUGIN_INIT_JOB) {
115  // get ifo id
116  int id=-1;
117  for(int n=0;n<cfg->nIFO;n++) if(ifo==net->ifoName[n]) {id=n;break;}
118  if(id<0) {cout << "Plugin : Error - bad ifo id" << endl; gSystem->Exit(1);}
119 
120  PrintUserOptions(cfg, id); // print config options
121  }
122 
124 
125  // get ifo id
126  int id=-1;
127  for(int n=0;n<cfg->nIFO;n++) if(ifo==net->ifoName[n]) {id=n;break;}
128  if(id<0) {cout << "Plugin : Error - bad ifo id" << endl; gSystem->Exit(1);}
129 
130  TB.mkDir(frDir[id],false,false); // create output frame dir
131 
132  // data recolored
133  int size=x->size();
134  double start=x->start();
135  if(gausNoise[id]) {
136  int seed = 1000+id;
137  TB.getSimNoise(*x, psdFile[id], seed, net->nRun); // gaussian colored noise
138  } else {
139  TB.getSimNoise(*x, psdFile[id], -padFreq[id], padFactor[id]); // real data colored noise
140  }
141 
142  x->resize(size);
143  x->start(start);
144 
145  // save cleaned data into frame
146  wavearray<double> X = *x;
147  // remove scratch
148  int os = cfg->segEdge*x->rate();
149  X.start(x->start()+cfg->segEdge);
150  X.stop(x->stop()-cfg->segEdge);
151  for(int i=0;i<X.size()-2*os;i++) X[i]=X[i+os];
152  X.resize(x->size()-2*os);
153 
154  // create frame file
155  char frFile[1024];
156  sprintf(frFile,"%s/%s_%s-%d-%d.gwf",
157  frDir[id].Data(),ifo.Data(),frLabel[id].Data(),
158  int(X.start()),(int)(X.stop()-X.start()));
159 
160  // open frame file
161  CWB::frame fr(frFile,chName[id],"WRITE");
162 
163  // write frame to file
164  fr.writeFrame(X, frName[id], chName[id]);
165  cout << "CWB_Plugin_Recolor.C : write " << frFile << endl;
166 
167  fr.close();
168 
169  int nIFO=net->ifoListSize();
170  if(TString(ifo).CompareTo(net->ifoName[nIFO-1])==0) gSystem->Exit(0); // last ifo
171  }
172 
173  return;
174 }
175 
177 
178  TString options = cfg->parPlugin;
179 
180  int n_frDir=0;
181  int n_frName=0;
182  int n_frLabel=0;
183  int n_chName=0;
184  int n_psdFile=0;
185  int n_padFreq=0;
186  int n_padFactor=0;
187  int n_gausNoise=0;
188  for(int i=0;i<cfg->nIFO;i++) {
189  frDir[i]="";
190  frName[i]="";
191  frLabel[i]="";
192  chName[i]="";
193  psdFile[i]="";
194  padFreq[i]=2;
195  padFactor[i]=0;
196  gausNoise[i]=false;
197  }
198  if(options.CompareTo("")!=0) {
199  cout << options << endl;
200  if(!options.Contains("--")) { // parameters are used only by cwb_inet
201 
202  TObjArray* token = TString(options).Tokenize(TString(' '));
203  for(int j=0;j<token->GetEntries();j++){
204 
205  TObjString* tok = (TObjString*)token->At(j);
206  TString stok = tok->GetString();
207 
208  if(stok.Contains("frDir=")) {
209  frDir[n_frDir]=stok;
210  frDir[n_frDir].Remove(0,frDir[n_frDir].Last('=')+1);
211  frDir[n_frDir]=gSystem->ExpandPathName(frDir[n_frDir].Data());
212  if(n_frDir<(NIFO_MAX-1)) n_frDir++;
213  }
214 
215  if(stok.Contains("frName=")) {
216  frName[n_frName]=stok;
217  frName[n_frName].Remove(0,frName[n_frName].Last('=')+1);
218  if(n_frName<(NIFO_MAX-1)) n_frName++;
219  }
220 
221  if(stok.Contains("frLabel=")) {
222  frLabel[n_frLabel]=stok;
223  frLabel[n_frLabel].Remove(0,frLabel[n_frLabel].Last('=')+1);
224  if(n_frLabel<(NIFO_MAX-1)) n_frLabel++;
225  }
226 
227  if(stok.Contains("chName=")) {
228  chName[n_chName]=stok;
229  chName[n_chName].Remove(0,chName[n_chName].Last('=')+1);
230  if(n_chName<(NIFO_MAX-1)) n_chName++;
231  }
232 
233  if(stok.Contains("padFreq=")) {
234  TString opt=stok;
235  opt.Remove(0,opt.Last('=')+1);
236  if(opt.IsDigit()) padFreq[n_padFreq]=opt.Atoi();
237  if(padFreq[n_padFreq]<2) padFreq[n_padFreq]=2;
238  if(n_padFreq<(NIFO_MAX-1)) n_padFreq++;
239  }
240 
241  if(stok.Contains("padFactor=")) {
242  TString opt=stok;
243  opt.Remove(0,opt.Last('=')+1);
244  if(opt.IsDigit()) padFactor[n_padFactor]=opt.Atoi();
245  if(padFactor[n_padFactor]<0) padFactor[n_padFactor]=0;
246  if(n_padFactor<(NIFO_MAX-1)) n_padFactor++;
247  }
248 
249  if(stok.Contains("psdFile=")) {
250  psdFile[n_psdFile]=stok;
251  psdFile[n_psdFile].Remove(0,psdFile[n_psdFile].Last('=')+1);
252  psdFile[n_psdFile]=gSystem->ExpandPathName(psdFile[n_psdFile].Data());
253  if(n_psdFile<(NIFO_MAX-1)) n_psdFile++;
254  }
255 
256  if(stok.Contains("gausNoise=")) {
257  TString opt=stok;
258  opt.Remove(0,opt.Last('=')+1);
259  if(opt=="false") gausNoise[n_gausNoise]=false;
260  if(opt=="true") gausNoise[n_gausNoise]=true;
261  if(n_gausNoise<(NIFO_MAX-1)) n_gausNoise++;
262  }
263  }
264  }
265  }
266 
267  for(int i=0;i<cfg->nIFO;i++) {
268  if(frDir[i]=="") {cout << "CWB_Plugin_Recolor : Error - frDir["<<cfg->ifo[i]<<"] not defined" << endl; gSystem->Exit(1);}
269  if(frName[i]=="") {cout << "CWB_Plugin_Recolor : Error - frName["<<cfg->ifo[i]<<"] not defined" << endl; gSystem->Exit(1);}
270  if(chName[i]=="") {cout << "CWB_Plugin_Recolor : Error - chName["<<cfg->ifo[i]<<"] not defined" << endl; gSystem->Exit(1);}
271  if(frLabel[i]=="") {cout << "CWB_Plugin_Recolor : Error - frLabel["<<cfg->ifo[i]<<"] not defined" << endl; gSystem->Exit(1);}
272  if(psdFile[i]=="") {cout << "CWB_Plugin_Recolor : Error - psdFile["<<cfg->ifo[i]<<"] not defined" << endl; gSystem->Exit(1);}
273  if(i>=n_gausNoise) {cout << "CWB_Plugin_Recolor : Error - gausNoise["<<cfg->ifo[i]<<"] not defined" << endl; gSystem->Exit(1);}
274  }
275 }
276 
277 void PrintUserOptions(CWB::config* cfg, int ifoId) {
278 
279  cout << "-----------------------------------------" << endl;
280  cout << "Recolor config options : ifo = " << cfg->ifo[ifoId] << endl;
281  cout << "-----------------------------------------" << endl << endl;
282  cout << endl;
283  cout << "frDir : " << frDir[ifoId] << endl;
284  cout << "chName : " << chName[ifoId] << endl;
285  cout << "frName : " << frName[ifoId] << endl;
286  cout << "frLabel : " << frLabel[ifoId] << endl;
287  cout << "psdFile : " << psdFile[ifoId] << endl;
288  cout << "padFreq : " << padFreq[ifoId] << endl;
289  cout << "padFactor : " << padFactor[ifoId] << endl;
290  cout << "gausNoise : " << gausNoise[ifoId] << endl;
291  cout << endl;
292 }
std::vector< char * > ifoName
Definition: network.hh:609
CWB::config * cfg
virtual void resize(unsigned int)
Definition: wseries.cc:901
int padFactor[NIFO_MAX]
virtual void rate(double r)
Definition: wavearray.hh:141
TString opt
int padFreq[NIFO_MAX]
int n
Definition: cwb_net.C:28
TString("c")
size_t nRun
Definition: network.hh:572
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
void close()
Definition: frame.cc:296
CWB::frame fr(FRLIST_NAME)
CWB::Toolbox TB
void writeFrame(wavearray< double > x, TString frName, TString chName)
Definition: frame.cc:175
Long_t size
virtual void start(double s)
Definition: wavearray.hh:137
int j
Definition: cwb_net.C:28
i drho i
bool gausNoise[NIFO_MAX]
char ifo[NIFO_MAX][8]
network ** net
NOISE_MDC_SIMULATION.
size_t ifoListSize()
Definition: network.hh:431
double segEdge
Definition: config.hh:164
#define nIFO
virtual size_t size() const
Definition: wavearray.hh:145
TString chName[NIFO_MAX]
jfile
Definition: cwb_job_obj.C:43
static void getSimNoise(wavearray< double > &u, TString fName, int seed, int run)
Definition: Toolbox.cc:5593
int simulation
Definition: config.hh:199
i() int(T_cor *100))
const int NIFO_MAX
Definition: wat.hh:22
TString frDir[NIFO_MAX]
char parPlugin[1024]
Definition: config.hh:363
TString psdFile[NIFO_MAX]
int nfactor
Definition: config.hh:201
TObjArray * token
char options[256]
TString frName[NIFO_MAX]
void CWB_Plugin(TFile *jfile, CWB::config *cfg, network *net, WSeries< double > *x, TString ifo, int type)
COHERENCE.
virtual void stop(double s)
Definition: wavearray.hh:139
char ifo[NIFO_MAX][8]
Definition: config.hh:124
static void mkDir(TString dir, bool question=false, bool remove=true)
Definition: Toolbox.cc:4714
void PrintUserOptions(CWB::config *cfg, int ifoId)
sprintf(tfres,"(1/%g)x(%g) (sec)x(Hz)", 2 *df, df)
int nIFO
Definition: config.hh:120
Long_t id
void ReadUserOptions(CWB::config *cfg)
virtual void resize(unsigned int)
Definition: wavearray.cc:463
TString frLabel[NIFO_MAX]
int levelR
Definition: config.hh:152
char os[1024]