Logo coherent WaveBurst  
Library Reference Guide
Logo
cwb_petools.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 // this macro uses the following json package to read json file
20 // https://github.com/nlohmann/json
21 // git clone https://github.com/nlohmann/json.git
22 
23 R__ADD_INCLUDE_PATH(/home/waveburst/git/json/include)
24 #include <nlohmann/json.hpp>
26 
27 #include "MACROS/ReadChunkList.C"
28 
29 #define GW_MAX_SIZE 1000
30 #define CHUNK_FILE_LIST "Chunk_List.txt"
31 
32 #define PE_GIT_DIR "/home/waveburst/git/pe"
33 
34 struct evtparameters {
35  TString name;
36  double time;
37  int chunk;
38  int start;
39  int stop;
41  TString calib;
42  TString run;
43  TString tag;
44 };
45 
46 struct peparameters {
47  evtparameters evt;
48  TString jsonFile;
49  TString run;
50  TString approx;
51  float fref;
52  float flow;
53  TString webdir;
54  TString pspath;
55 };
56 
57 vector<evtparameters> ReadInputEventsFile(TString ifile);
58 vector<evtparameters> GetEventsParameters(TString ifile, TString run );
59 vector<TString> GetPreferredPE(vector<evtparameters> evtparms, TString pedir, TString run);
60 vector<peparameters> GetPreferredParmsPE(vector<evtparameters> evtparms, TString pedir, TString run, bool posteriors=false);
61 
62 void DumpUpdateGIT(vector<evtparameters> evtparms, TString ofile="");
63 void DumpMkDirPE(TString ofile, vector<peparameters> vpeparms, TString run);
64 void DumpConfigPE(TString ofile, vector<peparameters> vpeparms, TString run);
65 void DumpXML(vector<peparameters> vpeparms, TString xmldir, bool execute=false);
66 void DumpMacroPosterior2XML(TString sample, peparameters peparms, TString odir);
67 
68 void cwb_petools(TString ifile, TString option="", TString opath="", TString pe_git_dir=PE_GIT_DIR, TString run="O3") {
69 //
70 // this macro provides a set of methods used for the PE analysis
71 //
72 
73  // cwb_petools options
74  if(ifile=="") {
75  cout << endl;
76  cout << "cwb_petools parameters" << endl;
77  cout << endl;
78  cout << " COMMAND: cwb_petools ifile option opath" << endl;
79  cout << endl;
80  cout << " IFILE: " << endl;
81  cout << endl;
82  cout << " event list file" << endl;
83  cout << endl;
84  cout << " file format: GW-NAME GW-TIME SEARCH CALIB PE-RUN TAG " << endl;
85  cout << " S190408an 1238782700.29 BBH C00 NONE dev1 " << endl;
86  cout << endl;
87  cout << " OPTIONS: " << endl;
88  cout << endl;
89  cout << " 1 - dump_update_git_pe : Generates a script used to update the local git repository (remote: https://git.ligo.org/pe/O3)" << endl;
90  cout << " 2 - dump_peparms : Dump to file the PE parameters of the preferred posterior samples" << endl;
91  cout << " 3 - dump_pe_xml_files : Generates XML files for map, maxl & offsource samples" << endl;
92  cout << " 4 - dump_pe_mkdirs : Dump to file the instructions used to create the working directories" << endl;
93  cout << " 5 - dump_cwb_pereport_config : Write the config used as imput for Makefile.pe" << endl;
94  cout << endl;
95  cout << " OPATH: " << endl;
96  cout << endl;
97  cout << " output file/dir" << endl;
98  cout << endl;
99  exit(0);
100  }
101 
102  vector<evtparameters> evtparms = GetEventsParameters(ifile, run);
103 
104  vector<TString> jsonFile = GetPreferredPE(evtparms, pe_git_dir, run);
105  for(int n=0;n<jsonFile.size();n++) cout << n << "\t" << jsonFile[n] << endl; cout << endl;
106 
107  if(option!="dump_pe_xml_files" && option!="dump_peparms" && option!="dump_pe_mkdirs" &&
108  option!="dump_cwb_pereport_config" && option!="dump_update_git_pe") exit(0);
109 
110  TSystemFile odir(opath.Data(),"");
111  if(option=="dump_pe_xml_files") {
112  if(!odir.IsDirectory()) {cout << "cwb_petools - Error: \"" << opath << "\" is not a directory" << endl << endl; exit(1);}
113  } else {
114  if(opath!="") {
115  bool overwrite = CWB::Toolbox::checkFile(opath,true);
116  if(!overwrite) exit(1);
117  } else {
118  cout << "cwb_petools - Error : output file not declared" << endl << endl;exit(1);
119  }
120  }
121 
122  if(option=="dump_update_git_pe") {
123  DumpUpdateGIT(evtparms, opath);
124  cout << endl << "Output file: " << opath << endl << endl;
125  exit(0);
126  }
127 
128  cout << endl;
129  vector<peparameters> vpeparms = GetPreferredParmsPE(evtparms, pe_git_dir, run, false);
130  if(option=="dump_peparms") {
131  ofstream outpe;
132  outpe.open(opath.Data());
133  if (!outpe.good()) {cout << "Error Opening File : " << opath << endl;exit(1);}
134  for(int n=0;n<vpeparms.size();n++) {
135  outpe << endl;
136  outpe << n+1 << "/" << vpeparms.size() << "\t" << vpeparms[n].evt.name << endl;
137  outpe << endl;
138  outpe << "jsonFile = " << vpeparms[n].jsonFile << endl;
139  outpe << "run = " << vpeparms[n].run << endl;
140  outpe << "approx = " << vpeparms[n].approx << endl;
141  outpe << "fref = " << vpeparms[n].fref << endl;
142  outpe << "flow = " << vpeparms[n].flow << endl;
143  outpe << "webdir = " << vpeparms[n].webdir << endl;
144  outpe << "pspath = " << vpeparms[n].pspath << endl;
145  outpe << endl;
146  }
147  cout << endl << "Output file: " << opath << endl << endl;
148  }
149 
150  if(option=="dump_cwb_pereport_config") {
151  DumpConfigPE(opath, vpeparms, run);
152  cout << endl << "Output file: " << opath << endl << endl;
153  }
154 
155  if(option=="dump_pe_mkdirs") {
156  DumpMkDirPE(opath, vpeparms, run);
157  cout << endl << "Output file: " << opath << endl << endl;
158  }
159 
160  if(option=="dump_pe_xml_files") {
161  DumpXML(vpeparms, odir.GetName(),true);
162  }
163 
164  exit(0);
165 }
166 
167 vector<evtparameters>
169 //
170 // merge the user input list of event parameters with chunk parameters
171 //
172 // Input:
173 // ifile - user input file: list of event parameters
174 // run - O3 - Implemented only for O3
175 //
176 // Returns the vector of event parameters + chunk parameters
177 //
178 
179  // get CWB_CONFIG
180  char cwb_config_env[1024] = "";
181  if(gSystem->Getenv("CWB_CONFIG")!=NULL) {
182  strcpy(cwb_config_env,TString(gSystem->Getenv("CWB_CONFIG")).Data());
183  }
184 
185  TString search="";
186  char chunk_file_list[1024];
187  sprintf(chunk_file_list,"%s/%s/CHUNKS/%s/%s",cwb_config_env,run.Data(),search.Data(),CHUNK_FILE_LIST);
188  cout << chunk_file_list << endl;
189 
190  int chunk[CHUNK_MAX_SIZE];
191  double start[CHUNK_MAX_SIZE];
192  double stop[CHUNK_MAX_SIZE];
193  int nChunks = ReadChunkList(chunk_file_list,chunk,start,stop);
194 
195  vector<evtparameters> evtparms = ReadInputEventsFile(ifile);
196 
197  for(int n=0;n<evtparms.size();n++) {
198  bool found=false;
199  for(int i=0;i<nChunks;i++) {
200  if(evtparms[n].time>start[i] && evtparms[n].time<=stop[i]) {
201  printf("\t%d\t%s\t%.2f\tK%02d\n",n+1,evtparms[n].name.Data(),evtparms[n].time,chunk[i]);
202  evtparms[n].chunk=chunk[i];
203  evtparms[n].start=start[i];
204  evtparms[n].stop=stop[i];
205  found=true;
206  }
207  }
208  if(!found) {
209  evtparms[n].chunk=-1;
210  evtparms[n].start=-1;
211  evtparms[n].stop=-1;
212  }
213  }
214  cout << endl;
215 
216  return evtparms;
217 }
218 
219 void
220 DumpUpdateGIT(vector<evtparameters> evtparms, TString ofile) {
221 //
222 // Generates a script used to update the local git repository (remote: https://git.ligo.org/pe/O3)
223 //
224 // Input:
225 // evtparms - the vector of event parameters
226 // ofile - output file name
227 //
228 // output file format :
229 //
230 // if [ ! -d S190408an ]; then
231 // git clone git@git.ligo.org:pe/O3/S190408an.git
232 // fi
233 // git lfs install --skip-smudge
234 // cd S190408an/Preferred/PESummary_metafile
235 // git lfs pull -I posterior_samples.json
236 // cd -
237 //
238 
239  ofstream outpe;
240  outpe.open(ofile.Data());
241  if (!outpe.good()) {cout << "Error Opening File : " << ofile << endl;exit(1);}
242  outpe << "if [ ! -d O3 ]; then" << endl;
243  outpe << " mkdir O3" << endl;
244  outpe << "fi" << endl;
245  outpe << "cd O3" << endl;
246  outpe << "" << endl;
247  for(int k=0;k<evtparms.size();k++) {
248  outpe << "if [ ! -d " << evtparms[k].name << " ]; then" << endl;
249  outpe << " git clone git@git.ligo.org:pe/O3/"<< evtparms[k].name << ".git" << endl;
250  outpe << "fi" << endl;
251  outpe << "git lfs install --skip-smudge" << endl;
252  outpe << "cd " << evtparms[k].name << "/Preferred/PESummary_metafile" << endl;
253  outpe << "git lfs pull -I posterior_samples.json" << endl;
254  outpe << "cd -" << endl;
255  outpe << "" << endl;
256  }
257  outpe.close();
258 }
259 
260 vector<peparameters>
261 GetPreferredParmsPE(vector<evtparameters> vevtparms, TString pedir, TString run, bool posteriors) {
262 //
263 // extract the PE parameters (fref, flow, pe-run, approximant) from json files
264 //
265 // Input:
266 //
267 // evtparms - the vector of event parameters
268 // pedir - the local PE git directory (/home/waveburst/git/pe)
269 // run - O3 - Implemented only for O3
270 // posteriors - true/false, if true it retrive the path of posterior_samples.dat file
271 //
272 // Return the vector of pe parameters
273 //
274 
275  vector<peparameters> vpeparms(vevtparms.size());
276 
277  for(int k=0;k<vevtparms.size();k++) {
278 
279  cout << "\t" << k+1 << "/" << vevtparms.size() << "\t" << "GetPreferredParmsPE\t" << vevtparms[k].name << endl;
280 
281  vpeparms[k].evt=vevtparms[k];
282 
283  // Get json file name
284  TString jsonFile = TString::Format("%s/%s/%s/Preferred/PESummary_metafile/posterior_samples.json",
285  pedir.Data(),run.Data(),vevtparms[k].name.Data());
286 
287  Long_t id,size=0,flags,mt;
288  int estat = gSystem->GetPathInfo(jsonFile.Data(),&id,&size,&flags,&mt);
289  if (estat!=0) continue;
290 
291  vpeparms[k].jsonFile=jsonFile;
292 
293  // fix json file -> remove NaN, Infinity
294  unsigned int Pid = gSystem->GetPid(); // used to tag in a unique way the temporary files
295  TString jsonFileTmp = TString::Format("/tmp/%d_%s",Pid,gSystem->BaseName(jsonFile));
296 
297  char cmd[1024];
298 
299  // copy json file to temporary dir
300  sprintf(cmd,"cp %s %s",jsonFile.Data(),jsonFileTmp.Data());
301  //cout << cmd << endl;
302  gSystem->Exec(cmd);
303 
304  // replace NaN with 0, (avoid parser crash)
305  sprintf(cmd,"sed -i 's/NaN/0/g' %s",jsonFileTmp.Data());
306  //cout << cmd << endl;
307  gSystem->Exec(cmd);
308 
309  // replace Infinity with 0, (avoid parser crash)
310  sprintf(cmd,"sed -i 's/Infinity/0/g' %s",jsonFileTmp.Data());
311  //cout << cmd << endl;
312  gSystem->Exec(cmd);
313 
314  // open temp json file
315  std::ifstream ifile(jsonFileTmp.Data());
316  json jroot;
317  ifile >> jroot;
318 
319  json japprox = jroot["approximant"];
320 
321  // extract run (Ex: EXP2)
322  TString run="";
323  for (auto& el : japprox.items()) {
324  //cout << el.key() << " : " << el.value() << "\n";
325  string key = el.key();
326  string value = el.value();
327  if(TString(value.c_str()).Contains("IMRPhenomPv2")) run = key.c_str();
328  }
329 
330  // substitute non alphanumeric characters with 'x'
331  vpeparms[k].run = run;
332  for(int i=0;i<run.Sizeof()-1;i++) if(!TString(run[i]).IsAlnum()) vpeparms[k].run[i]='x';
333  // overwrite with user defined run (if != NONE)
334  if(vevtparms[k].run!="NONE") vpeparms[k].run=vevtparms[k].run;
335 
336  // extract approximant
337  try {
338  string capprox = jroot["config_file"][run.Data()]["engine"]["approx"];
339  TString sapprox = capprox.c_str();
340  vpeparms[k].approx=sapprox;
341  } catch (...) {
342  cout << "extract approximant exception ...." << endl;
343  vpeparms[k].approx="";
344  }
345 
346  // extract fref
347  try {
348  string cfref = jroot["config_file"][run.Data()]["engine"]["fref"];
349  vpeparms[k].fref = TString(cfref.c_str()).Atof();
350  } catch (...) {
351  cout << "extract fref exception ...." << endl;
352  vpeparms[k].fref=-1;
353  }
354 
355  // extract flow
356  try {
357  string cflow = jroot["config_file"][run.Data()]["lalinference"]["flow"];
358  TString sflow = cflow.c_str();
359  sflow = sflow(sflow.Last(':')+1,sflow.Last('}')-sflow.Last(':')-1);
360  vpeparms[k].flow = sflow.Atof();
361  } catch (...) {
362  cout << "extract flow exception ...." << endl;
363  vpeparms[k].flow=-1;
364  }
365 
366  // extract webdir
367  try {
368  string cwebdir = jroot["config_file"][run.Data()]["paths"]["webdir"];
369  vpeparms[k].webdir = cwebdir.c_str();
370  } catch (...) {
371  cout << "extract webdir exception ...." << endl;
372  vpeparms[k].webdir="";
373  }
374 
375  // search posterior_samples.dat file in webdir
376  if(posteriors && vpeparms[k].webdir!="") {
377  sprintf(cmd,"find %s -name posterior_samples.dat",vpeparms[k].webdir.Data());
378  TString ocmd = gSystem->GetFromPipe(cmd);
379  TObjArray* token = ocmd.Tokenize(TString('\n'));
380  TString pspath="";
381  for(int j=0;j<token->GetEntries();j++){
382  TObjString* tok = (TObjString*)token->At(j);
383  TString stok = tok->GetString();
384  if(stok.Contains("/L1/")) continue;
385  if(stok.Contains("/H1/")) continue;
386  if(stok.Contains("/V1/")) continue;
387  if(stok.Contains("/K1/")) continue;
388  pspath=stok;
389  //cout << j << " " << stok << endl;
390  }
391  if(pspath=="") pspath=vpeparms[k].webdir;
392  vpeparms[k].pspath=pspath;
393  } else { // use converted json file
394  //vpeparms[k].pspath=TString::Format("pesummary_%s.dat",vpeparms[k].run.Data());
395  // save the non alpha numeric run
396  vpeparms[k].pspath=TString::Format("pesummary_%s.dat",run.Data());
397  }
398 
399  // remove temporary json file
400  sprintf(cmd,"rm %s",jsonFileTmp.Data());
401  //cout << cmd << endl;
402  gSystem->Exec(cmd);
403  }
404 
405  return vpeparms;
406 }
407 
408 void
409 DumpMkDirPE(TString ofile, vector<peparameters> vpeparms, TString run) {
410 //
411 // Dump to file the instructions used to create the working directories
412 //
413 // Input:
414 //
415 // ofile - output file name
416 // peparms - the vector of pe parameters
417 // run - O3 - Implemented only for O3
418 //
419 
420  ofstream out;
421  out.open(ofile.Data());
422  if (!out.good()) {cout << "DumpMkDirPE: Error Opening File : " << ofile << endl;exit(1);}
423 
424  for(int k=0;k<vpeparms.size();k++) {
425 
426  cout << "\t" << k+1 << "/" << vpeparms.size() << "\t" << vpeparms[k].evt.name << endl << endl;
427  out << "\t" << k+1 << "/" << vpeparms.size() << "\t" << vpeparms[k].evt.name << endl << endl;
428 
429  cout << endl;
430  out << endl;
431 
432  TString cmd1 = TString::Format("cwb_mkchunk --run %s --chunk %02d --cal %s --net LH --search %s --type SIM/ONSPE --tag %s_%s_maxl_%s",
433  run.Data(), vpeparms[k].evt.chunk, vpeparms[k].evt.calib.Data(), vpeparms[k].evt.search.Data(),
434  vpeparms[k].evt.name.Data(), vpeparms[k].run.Data(), vpeparms[k].evt.tag.Data());
435  cout << cmd1 << endl;
436  out << cmd1 << endl;
437 
438  TString cmd2 = TString::Format("cwb_inet @ %.2f ced",vpeparms[k].evt.time);
439  cout.precision(12);
440  cout << cmd2 << endl << endl;
441  out.precision(12);
442  out << cmd2 << endl << endl;
443 
444  TString cmd3 = TString::Format("cwb_mkchunk --run %s --chunk %02d --cal %s --net LH --search %s --type SIM/OFSPE --tag %s_%s_%s",
445  run.Data(), vpeparms[k].evt.chunk, vpeparms[k].evt.calib.Data(), vpeparms[k].evt.search.Data(),
446  vpeparms[k].evt.name.Data(), vpeparms[k].run.Data(), vpeparms[k].evt.tag.Data());
447  cout << cmd3 << endl;
448  out << cmd3 << endl;
449 
450  TString cmd4 = TString::Format("cwb_ppchunk --run %s --chunk %02d --cal %s --net LH --search %s --type SIM/OFSPE --tag %s_%s_%s --opt=all",
451  run.Data(), vpeparms[k].evt.chunk, vpeparms[k].evt.calib.Data(), vpeparms[k].evt.search.Data(),
452  vpeparms[k].evt.name.Data(), vpeparms[k].run.Data(), vpeparms[k].evt.tag.Data());
453  cout << cmd4 << endl << endl;
454  out << cmd4 << endl << endl;
455 
456  TString cmd5 = TString::Format("cwb_mkchunk --run %s --chunk %02d --cal %s --net LH --search %s --type SIM/ONSPE --tag %s_%s_pe_%s",
457  run.Data(), vpeparms[k].evt.chunk, vpeparms[k].evt.calib.Data(), vpeparms[k].evt.search.Data(),
458  vpeparms[k].evt.name.Data(), vpeparms[k].run.Data(), vpeparms[k].evt.tag.Data());
459  cout << cmd5 << endl;
460  out << cmd5 << endl;
461 
462  TString cmd6 = TString::Format("cwb_ppchunk --run %s --chunk %02d --cal %s --net LH --search %s --type SIM/ONSPE --tag %s_%s_pe_%s --opt=all",
463  run.Data(), vpeparms[k].evt.chunk, vpeparms[k].evt.calib.Data(), vpeparms[k].evt.search.Data(),
464  vpeparms[k].evt.name.Data(), vpeparms[k].run.Data(), vpeparms[k].evt.tag.Data());
465  cout << cmd6 << endl << endl;
466  out << cmd6 << endl << endl;
467  }
468 
469  out.close();
470 }
471 
472 void
473 DumpConfigPE(TString ofile, vector<peparameters> vpeparms, TString run) {
474 //
475 // Write the config used as imput for Makefile.AnalizePE
476 //
477 // Input:
478 //
479 // ofile - output file name
480 // peparms - the vector of pe parameters
481 // run - O3 - Implemented only for O3
482 //
483 
484  ofstream out;
485  out.open(ofile.Data());
486  if (!out.good()) {cout << "DumpConfigPE: Error Opening File : " << ofile << endl;exit(1);}
487 
488  out << endl << "STATUS=FALSE" << endl << endl;
489 
490  for(int k=0;k<vpeparms.size();k++) {
491 
492  TString onpath = TString::Format("%s/%s_K%02d_%s_LH_%s_SIM_ONSPE_%s_%s_maxl_%s",
493  vpeparms[k].evt.name.Data(), run.Data(), vpeparms[k].evt.chunk, vpeparms[k].evt.calib.Data(),
494  vpeparms[k].evt.search.Data(), vpeparms[k].evt.name.Data(), vpeparms[k].run.Data(), vpeparms[k].evt.tag.Data());
495 
496  TString offpath = TString::Format("%s/%s_K%02d_%s_LH_%s_SIM_OFSPE_%s_%s_%s",
497  vpeparms[k].evt.name.Data(), run.Data(), vpeparms[k].evt.chunk, vpeparms[k].evt.calib.Data(),
498  vpeparms[k].evt.search.Data(), vpeparms[k].evt.name.Data(), vpeparms[k].run.Data(), vpeparms[k].evt.tag.Data());
499 
500  out << "ifeq ($(GW_NAME)," << vpeparms[k].evt.name << ")" << endl;
501  out << "GW_GPS = " << vpeparms[k].evt.name << endl;
502  out << "GW_TSTEP = 150." << endl;
503  out << "GW_ONPATH = " << onpath << endl;
504  out << "GW_OFFPATH = " << offpath << endl;
505  out << "GW_RDIR = pestat_maxl_" << vpeparms[k].evt.tag << endl;
506  out << "STATUS = TRUE" << endl;
507  out << "endif" << endl;
508  out << endl;
509  }
510 
511  out << "GW_WDIR = $(shell basename $(GW_OFFPATH))" << endl;
512  out << "GW_MTAG = M1.C_U.S_bin1_cut" << endl;
513  out << endl;
514  out << "GW_DTAG = $(GW_WDIR).$(GW_MTAG)" << endl;
515  out << endl;
516  out << "WWW_DIR = " << endl;
517  out << endl;
518 
519  out.close();
520 }
521 
522 void
523 DumpXML(vector<peparameters> vpeparms, TString xmldir, bool execute) {
524 //
525 // Generates XML files for map, maxl & offsource samples
526 //
527 // Input:
528 //
529 // peparms - the vector of pe parameters
530 // xmldir - output xml directory
531 // execute - false -> creates directories & Posteriors2XML macros
532 // true -> creates & execute Posteriors2XML macros
533 //
534 
535  // write Posterior2XML macros
536  for(int k=0;k<vpeparms.size();k++) {
537  if(vpeparms[k].jsonFile=="" || vpeparms[k].run=="") continue;
538  TString odir = xmldir+"/"+vpeparms[k].evt.name+"/PE/"+vpeparms[k].run;
539  char cmd[1024];
540 
541  // create output directory
542  sprintf(cmd,"mkdir -p %s",odir.Data());
543  cout << cmd << endl;
544  gSystem->Exec(cmd);
545 
546  // copy json file
547  sprintf(cmd,"cp %s %s",vpeparms[k].jsonFile.Data(),odir.Data());
548  cout << cmd << endl;
549  gSystem->Exec(cmd);
550 
551  //convert json file to dat
552  TString json2dat = TString("python ")+gSystem->ExpandPathName("$CWB_SCRIPTS/cwb_json2dat.py");
553  TString setup = "source /home/vedovato/virtualenv/pycwb/bin/activate";
554  sprintf(cmd,"cd %s;%s;%s %s",odir.Data(),setup.Data(),json2dat.Data(),vpeparms[k].jsonFile.Data());
555  cout << cmd << endl;
556  gSystem->Exec(cmd);
557 
558  // rename non alphanumeric pspath to alphanumeric
559  TString pspath = TString::Format("pesummary_%s.dat",vpeparms[k].run.Data());
560  sprintf(cmd,"cd %s;mv \"%s\" %s",odir.Data(),vpeparms[k].pspath.Data(),pspath.Data());
561  cout << cmd << endl;
562  gSystem->Exec(cmd);
563  vpeparms[k].pspath=pspath;
564 
565  // create Posterior2XML macros
566  DumpMacroPosterior2XML("map", vpeparms[k], odir);
567  DumpMacroPosterior2XML("maxl", vpeparms[k], odir);
568  DumpMacroPosterior2XML("pe", vpeparms[k], odir);
569  DumpMacroPosterior2XML("offsource", vpeparms[k], odir);
570 
571  // excecute Posterior2XML macros
572  odir = odir+"/xml";
573  sprintf(cmd,"mkdir -p %s",odir.Data());
574  gSystem->Exec(cmd);
575  if(execute) { // generate XML files
576  sprintf(cmd,"cd %s; root -l -b ../Posterior2XML_MAP.C",odir.Data()); gSystem->Exec(cmd);
577  sprintf(cmd,"cd %s; root -l -b ../Posterior2XML_MAXL.C",odir.Data()); gSystem->Exec(cmd);
578  //sprintf(cmd,"cd %s; root -l -b ../Posterior2XML_ONSOURCE.C",odir.Data()); gSystem->Exec(cmd);
579  sprintf(cmd,"cd %s; root -l -b ../Posterior2XML_OFFSOURCE.C",odir.Data()); gSystem->Exec(cmd);
580  }
581  cout << endl << "output xml directory: " << odir << endl << endl;
582  }
583 }
584 
585 vector<TString>
586 GetPreferredPE(vector<evtparameters> evtparms, TString pedir, TString run) {
587 //
588 // extract the json files names
589 //
590 // Input:
591 //
592 // evtparms - the vector of event parameters
593 // pedir - the local PE git directory (/home/waveburst/git/pe)
594 // run - O3 - Implemented only for O3
595 //
596 // Return the vector of json files
597 //
598 
599  vector<TString> jsonFile;
600  for(int k=0;k<evtparms.size();k++) {
601  TString json_file_path = TString::Format("%s/%s/%s/Preferred/PESummary_metafile/posterior_samples.json",pedir.Data(),run.Data(),evtparms[k].name.Data());
602  jsonFile.push_back(json_file_path);
603  }
604  return jsonFile;
605 }
606 
607 void
608 DumpMacroPosterior2XML(TString sample, peparameters peparms, TString odir) {
609 //
610 // creates Posteriors2XML macros
611 //
612 // Input:
613 // sample - map/maxl/onsource/offsource
614 // peparms - the vector of pe parameters
615 // odir - output directory
616 //
617 // Output:
618 // Posterior2XML_MAP.C
619 // Posterior2XML_MAXL.C
620 // Posterior2XML_ONSOURCE.C
621 // Posterior2XML_OFFSOURCE.C
622 //
623 
624  TString ofile;
626  TString clbFile;
627 
628  if(sample=="map") {
629  xmlFile = TString::Format("posterior_samples_%s_%s_map.xml",peparms.evt.name.Data(),peparms.run.Data());
630  ofile = odir+"/Posterior2XML_MAP.C";
631  }
632  if(sample=="maxl") {
633  xmlFile = TString::Format("posterior_samples_%s_%s_maxl.xml",peparms.evt.name.Data(),peparms.run.Data());
634  ofile = odir+"/Posterior2XML_MAXL.C";
635  }
636  if(sample=="pe") {
637  xmlFile = TString::Format("posterior_samples_%s_%s_pe.xml",peparms.evt.name.Data(),peparms.run.Data());
638  ofile = odir+"/Posterior2XML_ONSOURCE.C";
639  }
640  if(sample=="offsource") {
641  xmlFile = TString::Format("posterior_samples_%s_%s_offsource.xml",peparms.evt.name.Data(),peparms.run.Data());
642  ofile = odir+"/Posterior2XML_OFFSOURCE.C";
643  }
644  clbFile = xmlFile; clbFile.ReplaceAll(".xml",".clb");
645 
646  ofstream out;
647  out.open(ofile.Data());
648  if (!out.good()) {cout << "DumpMacroPosterior2XML- Error Opening File : " << ofile << endl;exit(1);}
649 
650  out << "{" << endl;
651  out << " TString options = \"\";" << endl;
652  out << endl;
653  out << " TString posteriorFile = \"" << "../" << peparms.pspath << "\";" << endl;
654  out << endl;
655  if(sample=="map") {
656  out << " options += \"--sample map \";" << endl;
657  out << " options += \"--ninjections 1 \";" << endl;
658  }
659  if(sample=="maxl") {
660  out << " options += \"--sample maxl \";" << endl;
661  out << " options += \"--ninjections 1 \";" << endl;
662  }
663  if(sample=="offsource") {
664  out << " options += \"--gps_start_time " << TString::Format("%d",peparms.evt.start) << " \";" << endl;
665  out << " options += \"--gps_stop_time " << TString::Format("%d",peparms.evt.stop) << " \";" << endl;
666  out << endl;
667  out << " options += \"--time_step 150 \";" << endl;
668  out << " options += \"--seed 1 \";" << endl;
669  }
670  if(sample=="pe") {
671  out << " // extract from posteriorFile the number of entries " << endl;
672  out << " char cmd[1024]; " << endl;
673  out << " sprintf(cmd,\"wc %s\",posteriorFile.Data()); " << endl;
674  out << " TString ocmd = gSystem->GetFromPipe(cmd); " << endl;
675  out << " TObjArray* token = ocmd.Tokenize(TString(' ')); " << endl;
676  out << " TObjString* tok = (TObjString*)token->At(0); " << endl;
677  out << " TString stok = tok->GetString(); " << endl;
678  out << endl;
679  out << " cout << \"ocmd \" << ocmd << endl; " << endl;
680  out << " int max_sampleID = stok.Atoi()-1; " << endl;
681  out << " cout << \"max_sampleID \" << max_sampleID << endl; " << endl;
682  out << endl;
683  out << " gRandom->SetSeed(150914); " << endl;
684  out << endl;
685  out << " CWB::Toolbox::mkDir(\"pe\",true); " << endl;
686  out << endl;
687  out << " for(int i=0;i<1000;i++) { " << endl;
688  out << endl;
689  out << " int sampleID = gRandom->Uniform(0,max_sampleID); " << endl;
690  out << endl;
691  out << " cout << endl; " << endl;
692  out << " cout << i << \" -> sampleID = \" << sampleID << endl << endl; " << endl;
693  out << endl;
694  out << " options = \"\"; " << endl;
695  out << endl;
696  out << " options += TString::Format(\"--sample %d \",sampleID); " << endl;
697  out << " options += \"--ninjections 1 \"; " << endl;
698  out << endl;
699  out << " options += \"--source " << peparms.evt.name << " \";" << endl;
700  out << " options += \"--waveform " << peparms.approx << " \";" << endl;
701  out << " options += \"--f_ref " << peparms.fref << " \";" << endl;
702  out << " options += \"--f_lower " << peparms.flow << " \";" << endl;
703  out << endl;
704  out << " TString xmlFile = \"" << "pe/" << xmlFile << "\";" << endl;
705  out << " TString clbFile = \"" << "pe/" << clbFile << "\";" << endl;
706  out << " TString xml = TString::Format(\"_%d.xml\",i+1); " << endl;
707  out << " xmlFile.ReplaceAll(\".xml\",xml); " << endl;
708  out << " TString clb = TString::Format(\"_%d.clb\",i+1); " << endl;
709  out << " clbFile.ReplaceAll(\".clb\",clb); " << endl;
710  out << " options += \"--clb_file \"+clbFile+\" \"; " << endl;
711  out << endl;
712  out << " CWB::mdc::Posterior2XML(posteriorFile, xmlFile, options); " << endl;
713  out << " } " << endl;
714  out << endl;
715  } else {
716  out << endl;
717  out << " options += \"--source " << peparms.evt.name << " \";" << endl;
718  out << " options += \"--waveform " << peparms.approx << " \";" << endl;
719  out << " options += \"--f_ref " << peparms.fref << " \";" << endl;
720  out << " options += \"--f_lower " << peparms.flow << " \";" << endl;
721  out << endl;
722  out << " TString xmlFile = \"" << xmlFile << "\";" << endl;
723  out << " options += \"--clb_file " << clbFile << " \";" << endl;
724  out << endl;
725  out << " CWB::mdc::Posterior2XML(posteriorFile, xmlFile, options);" << endl;
726  out << endl;
727  }
728  out << " exit(0);" << endl;
729  out << endl;
730  out << "}" << endl;
731 
732  out.close();
733 }
734 
735 vector<evtparameters>
737 //
738 // read the user input file: list of event parameters
739 //
740 // file format: GW-NAME GW-TIME SEARCH CALIB PE-RUN TAG
741 // S190408an 1238782700.29 BBH C00 NONE dev1
742 //
743 // Input:
744 // ifile - user input file: list of event parameters
745 // run - O3 - Implemented only for O3
746 //
747 // Returns the vector of event parameters
748 //
749 
751 
752  ifstream in;
753  in.open(ifile.Data(),ios::in);
754  if (!in.good()) {cout << "cwb_petools: Error Opening File : " << ifile << endl;exit(1);}
755 
756  int isize=0;
757  char str[1024];
758  int fpos=0;
759  while(true) {
760  in.getline(str,1024);
761  if (!in.good()) break;
762  if(str[0] != '#') isize++;
763  }
764  in.clear(ios::goodbit);
765  in.seekg(0, ios::beg);
766  if(isize==0) {cout << "cwb_petools: Error : File " << ifile << " is empty" << endl;exit(1);}
767  if(isize>GW_MAX_SIZE) {cout << "cwb_petools: Error : File " << ifile << " > " << GW_MAX_SIZE << endl;exit(1);}
768 
769  char name[256];
770  double time;
771  char search[256];
772  char calib[256];
773  char run[256];
774  char tag[256];
775 
776  cout.precision(10);
777 
778  cout << endl;
779  int k=0;
780  vector<evtparameters> evtparms(isize);
781  while (1) {
782  fpos=in.tellg();
783  in.getline(str,1024);
784  if(str[0] == '#') continue;
785  in.seekg(fpos, ios::beg);
786 
787  in >> name >> time >> search >> calib >> run >> tag;
788  if (!in.good()) break;
789  fpos=in.tellg();
790  in.seekg(fpos+1, ios::beg);
791 
792  evtparms[k].name=name;
793  evtparms[k].time=time;
794  evtparms[k].search=search;
795  evtparms[k].calib=calib;
796  evtparms[k].run=run;
797  evtparms[k].tag=tag;
798  k++;
799  }
800  evtparms.resize(k);
801  in.close();
802 
803  return evtparms;
804 }
805 
char ofile[1024]
nlohmann::json json
Definition: cwb_petools.C:25
bool found
par [0] value
#define CHUNK_FILE_LIST
Definition: cwb_petools.C:30
CWB run(runID)
vector< evtparameters > ReadInputEventsFile(TString ifile)
Definition: cwb_petools.C:736
vector< evtparameters > GetEventsParameters(TString ifile, TString run)
Definition: cwb_petools.C:168
TString home
par [0] name
int n
Definition: cwb_net.C:28
TString("c")
void DumpXML(vector< peparameters > vpeparms, TString xmldir, bool execute=false)
Definition: cwb_petools.C:523
ofstream out
Definition: cwb_merge.C:214
void DumpMacroPosterior2XML(TString sample, peparameters peparms, TString odir)
Definition: cwb_petools.C:608
char odir[1024]
TString xmlFile
Definition: Posterior2XML.C:34
Long_t flags
string search
Definition: cWB_conf.py:110
Long_t size
int j
Definition: cwb_net.C:28
i drho i
static bool checkFile(TString fName, bool question=false, TString message="")
Definition: Toolbox.cc:4670
int isize
void DumpMkDirPE(TString ofile, vector< peparameters > vpeparms, TString run)
Definition: cwb_petools.C:409
char str[1024]
void cwb_petools(TString ifile, TString option="", TString opath="", TString pe_git_dir=PE_GIT_DIR, TString run="O3")
Definition: cwb_petools.C:68
vector< TString > GetPreferredPE(vector< evtparameters > evtparms, TString pedir, TString run)
Definition: cwb_petools.C:586
printf("total live time: non-zero lags = %10.1f \, liveTot)
int k
void DumpConfigPE(TString ofile, vector< peparameters > vpeparms, TString run)
Definition: cwb_petools.C:473
TObjArray * token
TFile * ifile
char tag[256]
Definition: cwb_merge.C:92
double flow
vector< peparameters > GetPreferredParmsPE(vector< evtparameters > evtparms, TString pedir, TString run, bool posteriors=false)
Definition: cwb_petools.C:261
ifstream in
#define GW_MAX_SIZE
Definition: cwb_petools.C:29
char cmd[1024]
int estat
strcpy(RunLabel, RUN_LABEL)
sprintf(tfres,"(1/%g)x(%g) (sec)x(Hz)", 2 *df, df)
Long_t mt
bool overwrite
Definition: cwb_dump_inj.C:100
Long_t id
#define PE_GIT_DIR
Definition: cwb_petools.C:32
void DumpUpdateGIT(vector< evtparameters > evtparms, TString ofile="")
Definition: cwb_petools.C:220
exit(0)