23 R__ADD_INCLUDE_PATH(/
home/waveburst/git/
json/include)
24 #include <nlohmann/json.hpp> 27 #include "MACROS/ReadChunkList.C" 29 #define GW_MAX_SIZE 1000 30 #define CHUNK_FILE_LIST "Chunk_List.txt" 32 #define PE_GIT_DIR "/home/waveburst/git/pe" 34 struct evtparameters {
65 void DumpXML(vector<peparameters> vpeparms,
TString xmldir,
bool execute=
false);
76 cout <<
"cwb_petools parameters" << endl;
78 cout <<
" COMMAND: cwb_petools ifile option opath" << endl;
80 cout <<
" IFILE: " << endl;
82 cout <<
" event list file" << 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;
87 cout <<
" OPTIONS: " << 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;
95 cout <<
" OPATH: " << endl;
97 cout <<
" output file/dir" << endl;
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;
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);
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);}
116 if(!overwrite)
exit(1);
118 cout <<
"cwb_petools - Error : output file not declared" << endl << endl;
exit(1);
122 if(option==
"dump_update_git_pe") {
124 cout << endl <<
"Output file: " << opath << endl << endl;
130 if(option==
"dump_peparms") {
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++) {
136 outpe <<
n+1 <<
"/" << vpeparms.size() <<
"\t" << vpeparms[
n].evt.name << 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;
147 cout << endl <<
"Output file: " << opath << endl << endl;
150 if(option==
"dump_cwb_pereport_config") {
152 cout << endl <<
"Output file: " << opath << endl << endl;
155 if(option==
"dump_pe_mkdirs") {
157 cout << endl <<
"Output file: " << opath << endl << endl;
160 if(option==
"dump_pe_xml_files") {
161 DumpXML(vpeparms, odir.GetName(),
true);
167 vector<evtparameters>
180 char cwb_config_env[1024] =
"";
181 if(gSystem->Getenv(
"CWB_CONFIG")!=NULL) {
182 strcpy(cwb_config_env,
TString(gSystem->Getenv(
"CWB_CONFIG")).Data());
186 char chunk_file_list[1024];
188 cout << chunk_file_list << endl;
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);
197 for(
int n=0;
n<evtparms.size();
n++) {
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];
209 evtparms[
n].chunk=-1;
210 evtparms[
n].start=-1;
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;
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;
275 vector<peparameters> vpeparms(vevtparms.size());
277 for(
int k=0;
k<vevtparms.size();
k++) {
279 cout <<
"\t" <<
k+1 <<
"/" << vevtparms.size() <<
"\t" <<
"GetPreferredParmsPE\t" << vevtparms[
k].name << endl;
281 vpeparms[
k].evt=vevtparms[
k];
284 TString jsonFile = TString::Format(
"%s/%s/%s/Preferred/PESummary_metafile/posterior_samples.json",
285 pedir.Data(),run.Data(),vevtparms[
k].name.Data());
289 if (estat!=0)
continue;
291 vpeparms[
k].jsonFile=jsonFile;
294 unsigned int Pid = gSystem->GetPid();
295 TString jsonFileTmp = TString::Format(
"/tmp/%d_%s",Pid,gSystem->BaseName(jsonFile));
300 sprintf(cmd,
"cp %s %s",jsonFile.Data(),jsonFileTmp.Data());
305 sprintf(cmd,
"sed -i 's/NaN/0/g' %s",jsonFileTmp.Data());
310 sprintf(cmd,
"sed -i 's/Infinity/0/g' %s",jsonFileTmp.Data());
315 std::ifstream
ifile(jsonFileTmp.Data());
319 json japprox = jroot[
"approximant"];
323 for (
auto& el : japprox.items()) {
325 string key = el.key();
326 string value = el.value();
327 if(
TString(value.c_str()).Contains(
"IMRPhenomPv2")) run = key.c_str();
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';
334 if(vevtparms[
k].run!=
"NONE") vpeparms[
k].run=vevtparms[
k].run;
338 string capprox = jroot[
"config_file"][run.Data()][
"engine"][
"approx"];
339 TString sapprox = capprox.c_str();
340 vpeparms[
k].approx=sapprox;
342 cout <<
"extract approximant exception ...." << endl;
343 vpeparms[
k].approx=
"";
348 string cfref = jroot[
"config_file"][run.Data()][
"engine"][
"fref"];
349 vpeparms[
k].fref =
TString(cfref.c_str()).Atof();
351 cout <<
"extract fref exception ...." << endl;
357 string cflow = jroot[
"config_file"][run.Data()][
"lalinference"][
"flow"];
359 sflow = sflow(sflow.Last(
':')+1,sflow.Last(
'}')-sflow.Last(
':')-1);
360 vpeparms[
k].flow = sflow.Atof();
362 cout <<
"extract flow exception ...." << endl;
368 string cwebdir = jroot[
"config_file"][run.Data()][
"paths"][
"webdir"];
369 vpeparms[
k].webdir = cwebdir.c_str();
371 cout <<
"extract webdir exception ...." << endl;
372 vpeparms[
k].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);
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;
391 if(pspath==
"") pspath=vpeparms[
k].webdir;
392 vpeparms[
k].pspath=pspath;
396 vpeparms[
k].pspath=TString::Format(
"pesummary_%s.dat",run.Data());
400 sprintf(cmd,
"rm %s",jsonFileTmp.Data());
421 out.open(ofile.Data());
422 if (!out.good()) {cout <<
"DumpMkDirPE: Error Opening File : " << ofile << endl;
exit(1);}
424 for(
int k=0;
k<vpeparms.size();
k++) {
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;
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;
438 TString cmd2 = TString::Format(
"cwb_inet @ %.2f ced",vpeparms[
k].evt.time);
440 cout << cmd2 << endl << endl;
442 out << cmd2 << endl << endl;
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;
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;
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;
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;
485 out.open(ofile.Data());
486 if (!out.good()) {cout <<
"DumpConfigPE: Error Opening File : " << ofile << endl;
exit(1);}
488 out << endl <<
"STATUS=FALSE" << endl << endl;
490 for(
int k=0;
k<vpeparms.size();
k++) {
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());
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());
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;
511 out <<
"GW_WDIR = $(shell basename $(GW_OFFPATH))" << endl;
512 out <<
"GW_MTAG = M1.C_U.S_bin1_cut" << endl;
514 out <<
"GW_DTAG = $(GW_WDIR).$(GW_MTAG)" << endl;
516 out <<
"WWW_DIR = " << endl;
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;
542 sprintf(cmd,
"mkdir -p %s",odir.Data());
547 sprintf(cmd,
"cp %s %s",vpeparms[
k].jsonFile.Data(),odir.Data());
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());
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());
563 vpeparms[
k].pspath=pspath;
573 sprintf(cmd,
"mkdir -p %s",odir.Data());
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);
579 sprintf(cmd,
"cd %s; root -l -b ../Posterior2XML_OFFSOURCE.C",odir.Data()); gSystem->Exec(cmd);
581 cout << endl <<
"output xml directory: " << odir << endl << endl;
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);
629 xmlFile = TString::Format(
"posterior_samples_%s_%s_map.xml",peparms.evt.name.Data(),peparms.run.Data());
630 ofile = odir+
"/Posterior2XML_MAP.C";
633 xmlFile = TString::Format(
"posterior_samples_%s_%s_maxl.xml",peparms.evt.name.Data(),peparms.run.Data());
634 ofile = odir+
"/Posterior2XML_MAXL.C";
637 xmlFile = TString::Format(
"posterior_samples_%s_%s_pe.xml",peparms.evt.name.Data(),peparms.run.Data());
638 ofile = odir+
"/Posterior2XML_ONSOURCE.C";
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";
644 clbFile =
xmlFile; clbFile.ReplaceAll(
".xml",
".clb");
647 out.open(ofile.Data());
648 if (!out.good()) {cout <<
"DumpMacroPosterior2XML- Error Opening File : " << ofile << endl;
exit(1);}
651 out <<
" TString options = \"\";" << endl;
653 out <<
" TString posteriorFile = \"" <<
"../" << peparms.pspath <<
"\";" << endl;
656 out <<
" options += \"--sample map \";" << endl;
657 out <<
" options += \"--ninjections 1 \";" << endl;
660 out <<
" options += \"--sample maxl \";" << endl;
661 out <<
" options += \"--ninjections 1 \";" << endl;
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;
667 out <<
" options += \"--time_step 150 \";" << endl;
668 out <<
" options += \"--seed 1 \";" << endl;
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;
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;
683 out <<
" gRandom->SetSeed(150914); " << endl;
685 out <<
" CWB::Toolbox::mkDir(\"pe\",true); " << endl;
687 out <<
" for(int i=0;i<1000;i++) { " << endl;
689 out <<
" int sampleID = gRandom->Uniform(0,max_sampleID); " << endl;
691 out <<
" cout << endl; " << endl;
692 out <<
" cout << i << \" -> sampleID = \" << sampleID << endl << endl; " << endl;
694 out <<
" options = \"\"; " << endl;
696 out <<
" options += TString::Format(\"--sample %d \",sampleID); " << endl;
697 out <<
" options += \"--ninjections 1 \"; " << 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;
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;
712 out <<
" CWB::mdc::Posterior2XML(posteriorFile, xmlFile, options); " << endl;
713 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;
722 out <<
" TString xmlFile = \"" << xmlFile <<
"\";" << endl;
723 out <<
" options += \"--clb_file " << clbFile <<
" \";" << endl;
725 out <<
" CWB::mdc::Posterior2XML(posteriorFile, xmlFile, options);" << endl;
728 out <<
" exit(0);" << endl;
735 vector<evtparameters>
754 if (!in.good()) {cout <<
"cwb_petools: Error Opening File : " << ifile << endl;
exit(1);}
760 in.getline(str,1024);
761 if (!in.good())
break;
762 if(str[0] !=
'#') isize++;
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);}
780 vector<evtparameters> evtparms(isize);
783 in.getline(str,1024);
784 if(str[0] ==
'#')
continue;
785 in.seekg(fpos, ios::beg);
787 in >> name >> time >> search >> calib >> run >>
tag;
788 if (!in.good())
break;
790 in.seekg(fpos+1, ios::beg);
792 evtparms[
k].name=
name;
793 evtparms[
k].time=time;
795 evtparms[
k].calib=calib;
printf("total live time: non-zero lags = %10.1f \, liveTot)
strcpy(RunLabel, RUN_LABEL)
sprintf(tfres,"(1/%g)x(%g) (sec)x(Hz)", 2 *df, df)