Logo coherent WaveBurst  
Library Reference Guide
Logo
cwb_report_skymap.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 // produce skymap html report : use -> https://github.com/reedessick/skymap_statistics
20 
21 {
22  #include <vector>
23 
25 
26  TB.checkFile(gSystem->Getenv("CWB_ROOTLOGON_FILE"));
27  TB.checkFile(gSystem->Getenv("CWB_PARAMETERS_FILE"));
28  TB.checkFile(gSystem->Getenv("CWB_UPARAMETERS_FILE"));
29 
31  if(gSystem->Getenv("HOME_SKYMAP_LIB")==NULL) {
32  cout << "cwb_report_skymap.C - Error : environment HOME_SKYMAP_LIB is not defined!!!" << endl;exit(1);
33  } else {
34  home_skymap_lib=TString(gSystem->Getenv("HOME_SKYMAP_LIB"));
35  }
36 
38  if(gSystem->Getenv("CWB_USER_URL")==NULL) {
39  cout << "cwb_report_skymap.C - Error : environment CWB_USER_URL is not defined!!!" << endl;exit(1);
40  } else {
41  cwb_user_url=TString(gSystem->Getenv("CWB_USER_URL"));
42  }
43 
45  if(gSystem->Getenv("CWB_REP_URL")!=NULL) {
46  cwb_rep_url=TString(gSystem->Getenv("CWB_REP_URL"));
47  }
48 
50  if(gSystem->Getenv("CWB_SKYMAP_FILE")==NULL) {
51  cout << "cwb_report_skymap.C - Error : environment CWB_SKYMAP_FILE is not defined!!!" << endl;exit(1);
52  } else {
53  cwb_skymap_file=TString(gSystem->Getenv("CWB_SKYMAP_FILE"));
54  }
55 
56  // check skymap extension
57  if(cwb_skymap_file.EndsWith(".fits")) {
58  } else if(cwb_skymap_file.EndsWith(".fits.gz")) {
59  } else {
60  cout << "cwb_report_skymap.C - Error : file name must ends with .fits/.fits.gz !!!" << endl;exit(1);
61  }
62 
63  TB.checkFile(cwb_skymap_file);
64 
65  TString cwb_skymap_dir = cwb_skymap_file; // default output dir
66 
67  TString cwb_report_options = TString(gSystem->Getenv("CWB_REPORT_OPTIONS"));
68 
69  // check if options contains the output dir & if contains multiple skymaps
70  bool singleSkymap = true;
71  if(cwb_report_options!="") {
72  cout << cwb_report_options << endl;
73 
74  TObjArray* token = TString(cwb_report_options).Tokenize(TString(' '));
75  for(int j=0;j<token->GetEntries();j++){
76 
77  TObjString* tok = (TObjString*)token->At(j);
78  TString stok = tok->GetString();
79 
80  if(stok=="-o") {
81  if(j<token->GetEntries()-1) {
82  TObjString* otoken = (TObjString*)token->At(j+1);
83  cwb_skymap_dir = otoken->GetString();
84  }
85  }
86  if(stok=="--output-dir") {
87  if(j<token->GetEntries()-1) {
88  TObjString* otoken = (TObjString*)token->At(j+1);
89  cwb_skymap_dir = otoken->GetString();
90  }
91  }
92 
93  if(stok=="-O") {
94  if(j<token->GetEntries()-1) {
95  TObjString* otoken = (TObjString*)token->At(j+1);
96  cwb_rep_url = otoken->GetString();
97  }
98  }
99  if(stok=="--output-url") {
100  if(j<token->GetEntries()-1) {
101  TObjString* otoken = (TObjString*)token->At(j+1);
102  cwb_rep_url = otoken->GetString();
103  }
104  }
105  if(stok=="--fits") {
106  if(j<token->GetEntries()-1) {
107  TObjString* otoken = (TObjString*)token->At(j+1);
108  TString cwb_skymap_comp = otoken->GetString();
109  // check skymap extension
110  if(cwb_skymap_comp.EndsWith(".fits")) {
111  } else if(cwb_skymap_comp.EndsWith(".fits.gz")) {
112  } else {
113  cout << "cwb_report_skymap.C - Error : file name of comparison skymap must ends with .fits/.fits.gz !!!" << endl;exit(1);
114  }
115  // remove --fits string
116  cwb_report_options.ReplaceAll("--fits","");
117  singleSkymap = false;
118  }
119  }
120  }
121  }
122 
123  if(cwb_rep_url=="") {
124  cout << "cwb_report_skymap.C - Error : --output-url not defined !!!" << endl;exit(1);
125  }
126 
127  TString cwb_skymap_name = gSystem->BaseName(cwb_skymap_file);
128  // for multiple skymaps add 'c' in front of of fits file directory
129  if(!singleSkymap) cwb_skymap_dir.ReplaceAll(cwb_skymap_name,TString("c")+cwb_skymap_name);
130  cwb_skymap_name.ReplaceAll(".gz","");
131  cwb_skymap_name.ReplaceAll(".fits","");
132 
133  if(cwb_skymap_dir.EndsWith(".fits.gz")) cwb_skymap_dir.ReplaceAll(".fits.gz","");
134  if(cwb_skymap_dir.EndsWith(".fits")) cwb_skymap_dir.ReplaceAll(".fits","");
135 
136  // creates dir for skymap report
137  bool overwrite = TB.checkFile(cwb_skymap_dir,true,"skymap statistic directory already exist");
138  if(!overwrite) {cout << "cwb_report_skymap.C terminated !!!" << endl<<endl;gSystem->Exit(1);}
139 
140  TB.mkDir(cwb_skymap_dir,false,true); // remove old directory
141 
142  char ifostr[64]="";
143  for(int n=0; n<nIFO; n++) sprintf(ifostr,"%s -i %c",ifostr,ifo[n][0]);
144  cout << "Network : " << ifostr << endl;
145 
146  unsigned int Pid = gSystem->GetPid(); // used to tag in a unique way the temporary files
147 
148  // create temporary skymap script file
149  char skymap_script_file[1024];
150  sprintf(skymap_script_file,"%s/skymap_%d.csh",tmp_dir,Pid);
151 
152  TString rel_skymap_dir = cwb_skymap_dir;
153  // strip report_dir from rel_skymap_dir
154  if(rel_skymap_dir.BeginsWith(report_dir)) if(rel_skymap_dir.First("/")>0) rel_skymap_dir.Remove(0,rel_skymap_dir.First("/")+1);
155  char ourl[1024];
156  sprintf(ourl,"%s/%s/%s",cwb_user_url.Data(),data_label,rel_skymap_dir.Data());
158 
159  TString pyCmd = singleSkymap ? "snglFITShtml.py" : "multFITShtml.py";
160 
161  // write output skymap script file
162  ofstream out;
163  out.open(skymap_script_file,ios::out);
164  char ostring[1024];
165 
166  sprintf(ostring,"bash -c \\");
167  out << ostring << endl;
168  sprintf(ostring,"' \\");
169  out << ostring << endl;
170  sprintf(ostring,"USER_DIR=${PWD}; \\");
171  out << ostring << endl;
172  sprintf(ostring,"cd %s; \\",home_skymap_lib.Data());
173  out << ostring << endl;
174  sprintf(ostring,". setup.sh; \\");
175  out << ostring << endl;
176  sprintf(ostring,"cd $USER_DIR; \\");
177  out << ostring << endl;
178  sprintf(ostring,"fits=%s; \\",cwb_skymap_file.Data());
179  out << ostring << endl;
180  sprintf(ostring,"time %s -v %s --dT-nside 128 --dT-Nsamp 500 -o %s -t cwb -O %s --no-margticks $fits %s \\",
181  pyCmd.Data(),ifostr,odir.Data(),ourl,cwb_report_options.Data());
182  out << ostring << endl;
183  sprintf(ostring,"'");
184  out << ostring << endl;
185 
186  out.close();
187 
188  // execute skymap script file -> generate skymap statistics + skyprobcc/skyprobcc-skymapSummary_cwb.html
189  char cmd[1024];
190  sprintf(cmd,". %s",skymap_script_file);
191  cout << cmd << endl;
192  gSystem->Exec(cmd);
193  sprintf(cmd,"rm %s",skymap_script_file);
194  gSystem->Exec(cmd);
195 
196  TString cwb_skymap_parent = gSystem->DirName(cwb_skymap_dir);
197 
198  // if symbolic link exist -> copy link to index.html
200  char index_html_file[1024];
201  Long_t id,size,flags,mt;
202  sprintf(index_html_file,"%s/index.html",cwb_skymap_parent.Data());
203  symLink = CWB::Toolbox::getFileName(index_html_file);
204  if(symLink!="") {
205  int estat = gSystem->GetPathInfo(symLink.Data(),&id,&size,&flags,&mt);
206  if(estat==0) {
207  sprintf(cmd,"rm %s;cp %s %s",index_html_file,symLink.Data(),index_html_file);
208  cout << cmd << endl;
209  gSystem->Exec(cmd);
210  }
211  }
212 
213  int estat = gSystem->GetPathInfo(index_html_file,&id,&size,&flags,&mt);
214  if(estat!=0) gSystem->Exit(0);
215 
216  // add link "skyprobcc/skyprobcc-skymapSummary_cwb.html" to index.html file
217 
218  TString cwb_skymap_html = cwb_skymap_name+"/"+cwb_skymap_name+"-skymapSummary_cwb.html";
219  if(!singleSkymap) cwb_skymap_html = TString("c")+cwb_skymap_name+"/"+"multFITS-skymapComparison_cwb.html";
220 
221  ifstream in;
222  in.open(index_html_file,ios::in);
223  if(!in.good()) {cout << "cwb_report_skymap.C - Error Opening File : " << index_html_file << endl;exit(1);}
224 
225  ofstream out2;
227  sprintf(index_html_file_tmp,"%s.tmp", index_html_file);
228  cout << index_html_file_tmp << endl;
229  out2.open(index_html_file_tmp,ios::out);
230  if (!out2.good()) {cout << "cwb_report_skymap.C - Error Opening File : " << index_html_file_tmp << endl;exit(1);}
231 
232  bool found=false;
233  char istr[1024];
234  while(1) {
235  in.getline(istr,1024);
236  if (!in.good()) break;
237  TString ostr(istr);
238  if(ostr.Contains(cwb_skymap_html)) found=true;
239  if(!found && ostr.Contains("cwb_parameters.C.html")) {
240  out2 << "<li> <a href=\"" << cwb_skymap_html << "\" target=\"_blank\">Skymap Statistics</a>";
241  if(cwb_skymap_html=="skyprobcc/skyprobcc-skymapSummary_cwb.html") out2 << " ( point estimate )" << endl;
242  else if(cwb_skymap_html=="mskyprobcc/mskyprobcc-skymapSummary_cwb.html") out2 << " ( median )" << endl;
243  else if(cwb_skymap_html=="cskyprobcc/multFITS-skymapComparison_cwb.html") out2 << " ( comparison )" << endl;
244  else out2 << endl;
245  found=false;
246  }
247  out2 << ostr.Data() << endl;
248  }
249  in.close();
250  out2.close();
251 
252  // copy index_html_file_tmp -> index_html_file
253  sprintf(cmd,"rm %s;mv %s %s",index_html_file,index_html_file_tmp,index_html_file);
254  cout << cmd << endl;
255  gSystem->Exec(cmd);
256 
257  gSystem->Exit(0);
258 }
TString cwb_rep_url
TString symLink
bool found
ofstream out2
Long_t mt
int n
Definition: cwb_net.C:28
TString("c")
Long_t flags
ofstream out
Definition: cwb_merge.C:214
char odir[1024]
CWB::Toolbox TB
TString home_skymap_lib
char ifostr[64]
int j
Definition: cwb_net.C:28
char report_dir[512]
Definition: test_config1.C:149
static bool checkFile(TString fName, bool question=false, TString message="")
Definition: Toolbox.cc:4670
char ifo[NIFO_MAX][8]
TString cwb_skymap_name
#define nIFO
TString cwb_report_options
char data_label[512]
Definition: test_config1.C:160
Long_t id
TString cwb_skymap_file
TString cwb_user_url
char tmp_dir[512]
Definition: test_config1.C:153
TString cwb_skymap_dir
char index_html_file_tmp[1024]
TObjArray * token
int estat
bool singleSkymap
char index_html_file[1024]
ifstream in
TString cwb_skymap_parent
static void mkDir(TString dir, bool question=false, bool remove=true)
Definition: Toolbox.cc:4714
char cmd[1024]
static TString getFileName(FILE *fp)
Definition: Toolbox.cc:6780
bool overwrite
Definition: cwb_dump_inj.C:100
Long_t size
sprintf(cmd,"rm %s", skymap_script_file)
exit(1)