Logo coherent WaveBurst  
Library Reference Guide
Logo
CWB_Plugin_MDC_PE.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 //!NOISE_MDC_SIMULATION
20 // Plugin to injected MDC 'on the fly'
21 
22 #define XIFO 4
23 
24 #pragma GCC system_header
25 
26 #include "cwb.hh"
27 #include "cwb2G.hh"
28 #include "config.hh"
29 #include "network.hh"
30 #include "wavearray.hh"
31 #include "TString.h"
32 #include "TObjArray.h"
33 #include "TObjString.h"
34 #include "TRandom.h"
35 #include "TComplex.h"
36 #include "TMath.h"
37 #include "mdc.hh"
38 #include "frame.hh"
39 #include <vector>
40 
41 //#define DUMP_LOG // uncomment to enable dump log
42 
43 // ---------------------------------------------------------------------------------
44 // HOW TO SETUP PLUGIN MDC_PE IN CWB USER CONFIGURATION (EXAMPLE)
45 // ---------------------------------------------------------------------------------
46 
47 /*
48 
49 Example1:
50 
51  TString optmdc_pe = ""; // NOTE : add space at the end of each line
52  optmdc_pe += "mdc_pe_dummy=true "; // enable dummy signal injections
53  optmdc_pe += "mdc_pe_xml_file=xml_fname.xml "; // xml_file used for dummy injections
54  strcpy(parPlugin,optmdc_pe.Data()); // set MDC_PE plugin parameters
55 
56 Example2:
57 
58  TString optmdc_pe = ""; // NOTE : add space at the end of each line
59  optmdc_pe += "mdc_pe_xml_file=xml_fname1.xml "; // xml_file used for dummy injections
60  optmdc_pe += "mdc_pe_xml_file=xml_fname2.xml "; // xml_file used for real injections
61  strcpy(parPlugin,optmdc_pe.Data()); // set MDC_PE plugin parameters
62 
63 */
64 
65 // ---------------------------------------------------------------------------------
66 // DEFINES
67 // ---------------------------------------------------------------------------------
68 
69 #define MDC_PE_SIM true
70 #define MDC_PE_DUMMY false
71 #define MDC_PE_INVERT true
72 
73 // ---------------------------------------------------------------------------------
74 // USER SGW PLUGIN OPTIONS
75 // ---------------------------------------------------------------------------------
76 
77 struct uoptions {
78  bool sim; // (def=true) - if false the plugin is disabled
79  bool dummy; // (def=false) - if enabled the plugin is used for dummy signal injections.
80  // the signal is whitened but not injected into the data stream.
81  // This feature can be used in CED report for signal comparison or
82  // to report the whitened signal into the output root file the whitened signal
83  // if dummy=true then xml_file is read from xml_file[0] or from plugin config CWB_Plugin_MDC_PE_Config.C
84  bool invert; // (def=true) - if enabled & if two xml_file are defined then wf1 is wf2 are switched
85 
86  vector<TString> xml_file; // xml file is provided in user plugin option
87  // if not setup then xml file is read from plugin config CWB_Plugin_MDC_PE_Config.C
88 };
89 
90 // ---------------------------------------------------------------------------------
91 // Global Variables
92 // ---------------------------------------------------------------------------------
93 
94 uoptions gOPT; // global User Options
95 int gXMLID; // index used to select the xml file from the array gOPT.xml_file
96 
97 // ---------------------------------------------------------------------------------
98 // FUNCTIONS
99 // ---------------------------------------------------------------------------------
100 
101 void ResetUserOptions();
104 
105 using namespace CWB;
106 
107 void
109 
110  if(type==CWB_PLUGIN_CONFIG) {
111 
112  ResetUserOptions(); // set default config options
113  ReadUserOptions(cfg->parPlugin); // user config options : read from parPlugin
114 
115  if(!gOPT.sim) { // if gOPT.sim=false -> disaple plugin
116  cfg->nfactor = 1;
117  cfg->simulation = 0;
118  cfg->mdcPlugin=false;
119  gOPT.dummy=false;
120  gOPT.xml_file.resize(0);
121  return;
122  }
123 
124  // check if xml files exist
125  for(int n=0;n<gOPT.xml_file.size();n++) CWB::Toolbox::checkFile(gOPT.xml_file[n]);
126 
127  cfg->mdcPlugin=true; // disable read mdc from frames
128 
129  gXMLID=0; // first time -> read mdc using gOPT.xml_file[0]
130  }
131 
132  if(type==CWB_PLUGIN_NETWORK) {
133  PrintUserOptions(cfg); // print config options
134  }
135 
136  if(!gOPT.sim) return; // skip plugin if gOPT.sim=false
137 
138  if(type==CWB_PLUGIN_MDC) {
139 
140  cout << "Execute CWB_Plugin_MDC_PE.C : Inject On The Fly MDC ..." << endl;
141 
142  // ---------------------------------
143  // Declare mdc class
144  // On The Fly MDC Injections
145  // ---------------------------------
146 
147  CWB::mdc MDC(net);
148  CWB_PLUGIN_EXPORT(MDC)
149 
150  // export global variables to the config plugin
151  int gIFACTOR=-1; IMPORT(int,gIFACTOR)
152  int xstart = (int)x->start();
153  int xstop = (int)x->stop();
154  CWB_PLUGIN_EXPORT(net)
155  CWB_PLUGIN_EXPORT(cfg)
156  CWB_PLUGIN_EXPORT(xstart)
157  CWB_PLUGIN_EXPORT(xstop)
158  CWB_PLUGIN_EXPORT(gIFACTOR)
159  int gIFOID=0; for(int n=0;n<cfg->nIFO;n++) if(ifo==net->getifo(n)->Name) {gIFOID=n;break;}
160  CWB_PLUGIN_EXPORT(gIFOID)
161 
162  if(gOPT.xml_file.size()==0) { // read plugin config CWB_Plugin_MDC_PE_Config.C
163  int error=0;
164  // execute config plugin
165  cfg->configPlugin.Exec(NULL,&error);
166  if(error) {
167  cout << "Error executing macro : " << cfg->configPlugin.GetTitle() << endl;
168  cfg->configPlugin.Print();
169  gSystem->Exit(1);
170  }
171  } else { // xml file is provided in user plugin option
172  char symlink[1024];
173  sprintf(symlink,gOPT.xml_file[gXMLID]);
174  TString xmlFile = CWB::Toolbox::getFileName(symlink); // get path from symbolic link
175  if(xmlFile!="") { // it is a symbolic link
176  if(xmlFile[0]=='.') { // if path is relative, start with '../' then '../' is removed
177  xmlFile = xmlFile(xmlFile.Index('/')+1, xmlFile.Sizeof()-xmlFile.Index('/')-2);
178  }
179  cout << endl;
180  cout << "---------> CWB_Plugin_MDC_PE: injections.xml = " << xmlFile << endl;
181  } else {
182  xmlFile=symlink; // not a symbolic link
183  }
184 
185  vector<TString> fileList;
186  void *dir = gSystem->OpenDirectory(xmlFile);
187  if(dir) { // is directory
188  // get number of files
189  fileList = CWB::Toolbox::getFileListFromDir(xmlFile, ".xml", "", "");
190  if(fileList.size()<cfg->nfactor) cfg->nfactor=fileList.size();
191  // get file with index gIFACTOR
192  char containString[1024];
193  sprintf(containString,"_%d.xml",gIFACTOR);
194  fileList = CWB::Toolbox::getFileListFromDir(xmlFile, ".xml", "", containString);
195  xmlFile=fileList[0];
196  } else { // is file
197  cfg->nfactor=1; // set nfactor=1
198  }
199  cout << "---------> CWB_Plugin_MDC_PE: xmlFile = " << xmlFile << " nfactor = " << cfg->nfactor << endl;
200  cout << endl;
201  CWB::Toolbox::checkFile(xmlFile); // check if file exist
202  TString clbFile = xmlFile; // set clb file
203  clbFile.ReplaceAll(".xml",".clb");
204 
205  TString inspOptions="";
206  inspOptions+= "--dir "+TString(cfg->nodedir)+" ";
207  inspOptions+= "--xml "+xmlFile;
208  MDC.SetInspiral("PE",inspOptions);
209  }
210 
211  // print list waveforms declared in the config plugin
212  MDC.Print();
213 
214  // ---------------------------------
215  // get mdc data
216  // fill x array with MDC injections
217  // ---------------------------------
218 
219  MDC.Get(*x,ifo);
220 
221  // ---------------------------------
222  // set mdc list in the network class
223  // ---------------------------------
224 
225  if(ifo.CompareTo(net->ifoName[0])==0) {
226  net->mdcList.clear();
227  net->mdcType.clear();
228  net->mdcTime.clear();
229  net->mdcList=MDC.mdcList;
230  net->mdcType=MDC.mdcType;
231  net->mdcTime=MDC.mdcTime;
232  }
233 
234  // ---------------------------------
235  // write MDC log file
236  // if enabled (uncomment #define DUMP_LOG) then the txt log file is created under the output dir
237  // the cwb_merge collect all log job files on a single file under the merge directory
238  // ---------------------------------
239 
240 #ifdef DUMP_LOG
241  char logFile[512];
242  int runID = net->nRun;
243  int Tb=x->start()+cfg->segEdge;
244  int dT=x->size()/x->rate()-2*cfg->segEdge;
245  sprintf(logFile,"%s/log_%d_%d_%s_job%d.txt",cfg->output_dir,int(Tb),int(dT),cfg->data_label,runID);
246  cout << "Dump : " << logFile << endl;
247  if(ifo==cfg->ifo[0]) MDC.DumpLog(logFile);
248 #endif
249 
250  // ---------------------------------
251  // print MDC injections list
252  // ---------------------------------
253 
254  cout.precision(14);
255  if(ifo.CompareTo(net->ifoName[0])==0) {
256  for(int k=0;k<(int)net->mdcList.size();k++) cout << k << " mdcList " << MDC.mdcList[k] << endl;
257  for(int k=0;k<(int)net->mdcTime.size();k++) cout << k << " mdcTime " << MDC.mdcTime[k] << endl;
258  for(int k=0;k<(int)net->mdcType.size();k++) cout << k << " mdcType " << MDC.mdcType[k] << endl;
259  }
260  }
261 
262  if(type==CWB_PLUGIN_STRAIN_AND_MDC) {
263 
264  if(!gOPT.dummy) return;
265 
266  // if dummy=true -> restore the original strain data -> read data again from frames
267 
268  // get ifo index
269  int xIFO =0;
270  for(int n=0;n<cfg->nIFO;n++) if(ifo==cfg->ifo[n]) {xIFO=n;break;}
271 
272  int runID = net->nRun;
273  char flabel[512];
274  int Tb=x->start();
275  int dT=x->size()/x->rate();
276  sprintf(flabel,"%d_%d_%s_job%d",int(Tb),int(dT),cfg->data_label,runID);
277 
278  int size=0;
279 
280  // in channel
281  wavearray<double> xx(x->size());
282  xx.rate(x->rate());
283  xx.start(x->start());
284  xx.stop(x->stop());
285 
286  // read strain again and write to x array
287  frame frl(cfg->frFiles[xIFO],"READ");
288  frl.setChName(cfg->channelNamesRaw[xIFO]);
289  frl >> xx;
290  xx.Resample(xx.rate()/(1<<cfg->levelR)); // resampling
291  xx*=sqrt(1<<cfg->levelR); // rescaling
292  for(int i=0;i<(int)x->size();i++) x->data[i] = xx[i];
293  }
294 
295  if(type==CWB_PLUGIN_BCOHERENCE) {
296 
297  if(gOPT.xml_file.size()<2) return;
298 
299  int gIFACTOR=-1; IMPORT(int,gIFACTOR)
300  cwb2G* gCWB2G; IMPORT(cwb2G*,gCWB2G)
301 
302  vector<TString> delObjList;
303  // supercluster clusters and parse maps are removed
304  delObjList.push_back("strain");
305  delObjList.push_back("mdc");
306  delObjList.push_back("cstrain");
307  delObjList.push_back("rms");
308  delObjList.push_back("waveform");
309  delObjList.push_back("csparse");
310  TString jname = jfile->GetPath();
311  jname.ReplaceAll(":/","");
312  jfile->Close();
313  gCWB2G->FileGarbageCollector(jname,"",delObjList);
314 
315  gXMLID=1; // read again strain data and mdc using gOPT.xml_file[1]
316 
317  // perform read data and data conditioning stages
318  // the injected signals (second set) are stored after the previous in the pD->IWFP / pD->IWFID arrays
319  // warning the ID are stored with the same values used for the first set, see below
320  gCWB2G->ReadData(0, gIFACTOR);
321  gCWB2G->DataConditioning(gIFACTOR);
322 
323  // the first inj set is stored in the first pD->IWFP.size()/2 positions
324  // the second inj set is stored from pD->IWFP.size()/2 to pD->IWFP.size()-1 positions
325  // we use the second inj set to store the difference between the second and the first set
326  int nIFO = net->ifoListSize(); // number of detectors
327  for(int i=0; i<nIFO; i++) {
328  detector* pD = net->getifo(i);
329  int J = pD->IWFP.size()/2;
330  for (int j=0;j<J;j++) {
331  // the index id must be modified to be unique
332  if(pD->IWFID[j+J]<0) pD->IWFID[j+J]-=J/2; else pD->IWFID[j+J]+=J/2;
333  // compute the difference
335  wavearray<double>* wf2 = (wavearray<double>*)pD->IWFP[j+J];
336  wavearray<double> WF2 = *wf2;
337  *wf2 = CWB::mdc::GetDiff(wf2,wf1);
338  // move wf2 to wf1
339  if(gOPT.invert) *wf1 = WF2;
340  }
341  }
342  }
343 
344  return;
345 }
346 
348 
349  // get plugin options
350 
351  if(TString(options)!="") {
352 
353  //cout << "WF options : " << options << endl;
354  TObjArray* token = TString(options).Tokenize(TString(' '));
355  for(int j=0;j<token->GetEntries();j++) {
356 
357  TObjString* tok = (TObjString*)token->At(j);
358  TString stok = tok->GetString();
359 
360  if(stok.Contains("mdc_pe_sim=")) {
361  TString mdc_pe_sim=stok;
362  mdc_pe_sim.Remove(0,mdc_pe_sim.Last('=')+1);
363  if(mdc_pe_sim=="true") gOPT.sim=true;
364  if(mdc_pe_sim=="false") gOPT.sim=false;
365  }
366 
367  if(stok.Contains("mdc_pe_dummy=")) {
368  TString mdc_pe_dummy=stok;
369  mdc_pe_dummy.Remove(0,mdc_pe_dummy.Last('=')+1);
370  if(mdc_pe_dummy=="true") gOPT.dummy=true;
371  if(mdc_pe_dummy=="false") gOPT.dummy=false;
372  }
373 
374  if(stok.Contains("mdc_pe_invert=")) {
375  TString mdc_pe_invert=stok;
376  mdc_pe_invert.Remove(0,mdc_pe_invert.Last('=')+1);
377  if(mdc_pe_invert=="true") gOPT.invert=true;
378  if(mdc_pe_invert=="false") gOPT.invert=false;
379  }
380 
381  if(stok.Contains("mdc_pe_xml_file=")) {
382  TString mdc_pe_xml_file=stok;
383  mdc_pe_xml_file.Remove(0,mdc_pe_xml_file.Last('=')+1);
384  if(gOPT.xml_file.size()<2) gOPT.xml_file.push_back(mdc_pe_xml_file);
385  }
386  }
387  }
388 }
389 
391 
392  gOPT.sim = MDC_PE_SIM;
393  gOPT.dummy = MDC_PE_DUMMY;
394  gOPT.invert = MDC_PE_INVERT;
395 }
396 
398 
399  cout << "-----------------------------------------" << endl;
400  cout << "MDC_PE config options " << endl;
401  cout << "-----------------------------------------" << endl << endl;
402 
403  cout << "MDC_PE_SIM " << gOPT.sim << endl;
404  cout << "MDC_PE_DUMMY " << gOPT.dummy << endl;
405  cout << "MDC_PE_INVERT " << gOPT.invert << endl;
406 
407  for(int n=0;n<gOPT.xml_file.size();n++) {
408  cout << "MDC_PE_XML_FILE " << n << "\t" << gOPT.xml_file[n] << endl;
409  }
410 
411  cout << endl;
412 }
413 
std::vector< char * > ifoName
Definition: network.hh:609
detector * getifo(size_t n)
param: detector index
Definition: network.hh:436
CWB::config * cfg
TChain sim("waveburst")
static vector< TString > getFileListFromDir(TString dir_name, TString endString="", TString beginString="", TString containString="", bool fast=false)
Definition: Toolbox.cc:5108
void FileGarbageCollector(TString ifName, TString ofName="", vector< TString > delObjList=vector< TString >())
Definition: cwb.cc:2338
TMacro configPlugin
Definition: config.hh:362
TString Get(wavearray< double > &x, TString ifo)
Definition: mdc.cc:1529
bool mdcPlugin
Definition: config.hh:365
Definition: ced.hh:42
std::vector< std::string > mdcList
Definition: mdc.hh:389
virtual void rate(double r)
Definition: wavearray.hh:141
char logFile[1024]
Definition: cwb_merge_log.C:31
int error
Definition: cwb_compile.C:43
int n
Definition: cwb_net.C:28
void PrintUserOptions(CWB::config *cfg)
TString("c")
std::vector< std::string > mdcType
Definition: mdc.hh:390
size_t nRun
Definition: network.hh:572
void ResetUserOptions()
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
TString xmlFile
Definition: Posterior2XML.C:34
void ReadUserOptions(TString options)
std::vector< std::string > mdcType
Definition: network.hh:613
CWB::mdc * MDC
Long_t size
virtual void start(double s)
Definition: wavearray.hh:137
int j
Definition: cwb_net.C:28
i drho i
std::vector< double > mdcTime
Definition: network.hh:614
char nodedir[1024]
Definition: config.hh:352
uoptions gOPT
static bool checkFile(TString fName, bool question=false, TString message="")
Definition: Toolbox.cc:4670
void Print(int level=0)
Definition: mdc.cc:2736
char ifo[NIFO_MAX][8]
Definition: cwb2G.hh:33
network ** net
NOISE_MDC_SIMULATION.
size_t ifoListSize()
Definition: network.hh:431
void setChName(TString chName)
Definition: frame.hh:120
double segEdge
Definition: config.hh:164
#define nIFO
virtual size_t size() const
Definition: wavearray.hh:145
jfile
Definition: cwb_job_obj.C:43
wavearray< double > xx
Definition: TestFrame1.C:11
#define CWB_PLUGIN_EXPORT(VAR)
Definition: CWB_Plugin.h:18
Definition: mdc.hh:248
int simulation
Definition: config.hh:199
#define IMPORT(TYPE, VAR)
Definition: cwb.hh:69
i() int(T_cor *100))
char output_dir[1024]
Definition: config.hh:318
std::vector< std::string > mdcList
Definition: network.hh:612
void Resample(const wavearray< DataType_t > &, double, int=6)
Definition: wavearray.cc:640
char parPlugin[1024]
Definition: config.hh:363
char channelNamesRaw[NIFO_MAX][50]
Definition: config.hh:310
TString inspOptions
int k
int nfactor
Definition: config.hh:201
std::vector< wavearray< double > * > IWFP
Definition: detector.hh:379
TObjArray * token
std::vector< int > IWFID
Definition: detector.hh:378
#define MDC_PE_SIM
void CWB_Plugin(TFile *jfile, CWB::config *cfg, network *net, WSeries< double > *x, TString ifo, int type)
COHERENCE.
int * xstart
#define MDC_PE_INVERT
char options[256]
double Tb
static wavearray< double > GetDiff(wavearray< double > *w1, wavearray< double > *w2)
Definition: mdc.cc:7374
cout<< "Starting reading output directory ..."<< endl;vector< TString > fileList
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
int gIFACTOR
sprintf(tfres,"(1/%g)x(%g) (sec)x(Hz)", 2 *df, df)
static TString getFileName(FILE *fp)
Definition: Toolbox.cc:6780
int nIFO
Definition: config.hh:120
DataType_t * data
Definition: wavearray.hh:319
void DumpLog(TString fName, TString label="", bool append=false)
Definition: mdc.cc:5068
TString jname
#define MDC_PE_DUMMY
void DataConditioning(int ifactor)
Definition: cwb2G.cc:450
double dT
Definition: testWDM_5.C:12
std::vector< double > mdcTime
Definition: mdc.hh:392
char data_label[1024]
Definition: config.hh:332
double ReadData(double mdcShift, int ifactor)
Definition: cwb2G.cc:202
int gXMLID
int levelR
Definition: config.hh:152
detector ** pD
char frFiles[2 *NIFO_MAX][1024]
Definition: config.hh:342