Logo coherent WaveBurst  
Library Reference Guide
Logo
DrawMedianPRCvsSNR.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 //
20 // Draw median50/90 sky localization error region angle vs SNR
21 // Note : this macro is used to generate the PRC report (cwb_report merge_label prc)
22 // Author : Gabriele Vedovato
23 //
24 // input lst file names format :
25 // output root merge file_name mdc_name mdc_type line_color line_style
26 // wave_ADV_SIM_BRST_L1H1V1_run1.M1.root WNB250_100_0d100 1 6 0
27 // note : mdc_type = type[1]+1, where type[1] is the parameter declared in the waveburst tree
28 //
29 // command : root -l 'root 'macro/DrawMedianPRCvsSNR.C("file.lst","plot title","MEDIAN50","output_plot.png")'
30 //
31 
32 #define APPLY_FIT
33 
34 #define MAX_FILES 20 // num max of file in the input file list
35 
36 //------------------------------------------------------
37 // Thresholds Settings
38 //------------------------------------------------------
39 
40 #define TREE_NAME "waveburst"
41 
42 #define MIN_SNR_NET 5 // SNRnet inf
43 #define MAX_SNR_NET 100 // SNRnet sup
44 #define NSNR 20 // number of snr bins
45 
46 #define MIN_NSNR 20 // minimum number of entries in the SNR bin
47 #define MAX_MEDIAN50 50 // max median50 to be included in the fit
48 #define MAX_MEDIAN90 100 // max median90 to be included in the fit
49 
50 #define LINX // set Logx to false
51 
52 Double_t
53 medianfit(Double_t *x, Double_t *par); // fit function
54 int
55 GetMedian(TString ifName, int type, TString pType, wavearray<double>& snr,
57  float T_win, int pp_inetcc, float T_cor, int pp_irho, float T_cut, float T_vED, float T_pen, float T_ifar);
58 
59 void
61  float T_win=0, int pp_inetcc=0, float T_cor=0, int pp_irho=0,
62  float T_cut=0, float T_vED=0, float T_pen=0, float T_ifar=0) {
63 
64  if((ofName!="")&&(!ofName.Contains(".png"))) {
65  cout << "Error Output File : " << ilist_file.Data() << endl;
66  cout << "Must have .png extension" << endl;
67  exit(1);
68  } else {
69  ofName.ReplaceAll(".png",".gif");
70  }
71 
72  TString ptitle="Sky Localization : median error area vs SNR";
73 
74  if((pType!="MEDIAN50")&&(pType!="MEDIAN90")&&(pType!="")) {
75  ptitle=pType;
76  } else {
77  if(pType=="") pType="MEDIAN50";
78  if(pType=="MEDIAN50") ptitle=ptitle+"50% error region";
79  if(pType=="MEDIAN90") ptitle=ptitle+"90% error region";
80  }
81 
84  int type[MAX_FILES];
85  int colors[MAX_FILES];
86  int lstyle[MAX_FILES];
87 
88  // Open list
89  ifstream in;
90  in.open(ilist_file.Data(),ios::in);
91  if (!in.good()) {cout << "Error Opening File : " << ilist_file.Data() << endl;exit(1);}
92 
93  int size=0;
94  char str[1024];
95  int fpos=0;
96  while(true) {
97  in.getline(str,1024);
98  if (!in.good()) break;
99  if(str[0] != '#') size++;
100  }
101  cout << "size " << size << endl;
102  in.clear(ios::goodbit);
103  in.seekg(0, ios::beg);
104  if (size==0) {cout << "Error : File " << ilist_file.Data() << " is empty" << endl;exit(1);}
105 
106  char sfile[256];
107  char sname[256];
108  int stype;
109  int scolors;
110  int sltyle;
111  int NFILE=0;
112  while(true) {
113  in >> sfile >> sname >> stype >> scolors >> sltyle;
114  if(!in.good()) break;
115  if(sfile[0]=='#') continue;
116  ifile[NFILE]=sfile;
117  name[NFILE]=sname;
118  type[NFILE]=stype;
119  colors[NFILE]=scolors;
120  lstyle[NFILE]=sltyle;
121  cout << NFILE+1 << " " << ifile[NFILE] << " " << name[NFILE] << " "
122  << type[NFILE] << " " << colors[NFILE] << " " << lstyle[NFILE] << endl;
123  NFILE++;
124  }
125  in.close();
126 
127 #ifdef APPLY_FIT
128  TF1* f1 = NULL;
129  f1=new TF1("medianfit",medianfit,0.5,4,3);
130  f1->SetParNames("A","B","C");
131  f1->SetLineWidth(2);
132  f1->SetNpx(10000);
133 #endif
134 
135  // get medians
136  double max_median=0;
137  int nevents[MAX_FILES];
141  for(int n=0;n<NFILE;n++) {
142  cout << "Process File : " << ifile[n].Data() << endl;
143  nevents[n]=GetMedian(ifile[n], type[n], pType, snr[n], median[n], entries[n],
145  // find max median
146  for(int i=0;i<snr[n].size();i++) {
147  if(median[n][i]>max_median) max_median=median[n][i];
148  //cout << n << " " << i << "\t snr " << snr[n][i] << "\t median " << median[n][i] << endl;
149  }
150  //cout << n << " max_median " << max_median << endl;
151  }
152 
153  // create plots
154  gStyle->SetFrameBorderMode(0); // remove the red box around canvas
155  gROOT->ForceStyle();
156 
157  gStyle->SetTitleFont(72);
158  gStyle->SetMarkerColor(50);
159  gStyle->SetLineColor(kWhite);
160  gStyle->SetTitleW(0.98);
161  gStyle->SetTitleH(0.05);
162  gStyle->SetTitleY(0.98);
163  gStyle->SetFillColor(kWhite);
164  gStyle->SetLineColor(kWhite);
165  gStyle->SetTitleFont(12,"D");
166 
167  TCanvas *canvas = new TCanvas("median", "median", 300,40, 600, 600);
168  canvas->Clear();
169  canvas->ToggleEventStatus();
170 #ifdef LINX
171  canvas->SetLogx(false);
172 #else
173  canvas->SetLogx(true);
174 #endif
175  //canvas->SetLogy();
176  canvas->SetGridx();
177  canvas->SetGridy();
178  canvas->SetFillColor(kWhite);
179 
180  double A[MAX_FILES];
181  double B[MAX_FILES];
182  double C[MAX_FILES];
183  double chi2[MAX_FILES];
184  //TGraph* gr[MAX_FILES];
185  //for(int n=0;n<NFILE;n++) gr[n] = new TGraph(snr[n].size(),snr[n].data,median[n].data);
186  TGraphErrors* gr[MAX_FILES];
187  for(int n=0;n<NFILE;n++) {
188  wavearray<double> esnr=snr[n];esnr=0;
189  wavearray<double> emedian=median[n];emedian=0;
190  //for(int i=0;i<emedian.size();i++) emedian[i]=1./sqrt(entries[n][i]);
191  gr[n] = new TGraphErrors(snr[n].size(),snr[n].data,median[n].data,esnr.data,emedian.data);
192  }
193  for(int n=0;n<NFILE;n++) {
194  gr[n]->SetLineWidth(0);
195  gr[n]->SetLineColor(colors[n]);
196  gr[n]->SetLineStyle(1);
197  gr[n]->SetMarkerColor(colors[n]);
198  gr[n]->SetMarkerStyle(20);
199  gr[n]->SetMarkerSize(1);
200 
201 #ifdef APPLY_FIT
202  f1->SetLineColor(colors[n]);
203  f1->SetLineStyle(lstyle[n]);
204 // f1->SetLineWidth(1);
205  f1->SetNpx(100);
206  gr[n]->Fit("medianfit");
207  chi2[n]=f1->GetChisquare();
208  A[n]=f1->GetParameter(0);
209  B[n]=f1->GetParameter(1);
210  C[n]=f1->GetParameter(2);
211 #endif
212  }
213 
214  TMultiGraph* mg = new TMultiGraph();
215  TString gTitle = "Sky Localization "+pType+" vs SNR";
216  if(pTitle!="") gTitle = gTitle+" : "+pTitle;
217  mg->SetTitle(gTitle.Data());
218  for(int n=0;n<NFILE;n++) mg->Add(gr[n]);
219  mg->Paint("AP");
220 
221  mg->GetHistogram()->GetXaxis()->SetRangeUser(MIN_SNR_NET,MAX_SNR_NET);
222 // mg->GetHistogram()->GetYaxis()->SetRangeUser(0,max_median);
223 
224  mg->GetHistogram()->GetXaxis()->SetTitle("Injected SNRnet");
225  if(pType=="MEDIAN50") mg->GetHistogram()->GetYaxis()->SetTitle("Median50 error angle (degrees)");
226  else mg->GetHistogram()->GetYaxis()->SetTitle("Median90 error angle (degrees)");
227 
228  mg->GetHistogram()->GetXaxis()->SetLabelSize(0.04);
229  mg->GetHistogram()->GetYaxis()->SetLabelSize(0.04);
230  mg->GetHistogram()->GetXaxis()->SetTitleSize(0.04);
231  mg->GetHistogram()->GetYaxis()->SetTitleSize(0.04);
232  mg->GetHistogram()->GetXaxis()->SetTitleOffset(1.2);
233  mg->GetHistogram()->GetYaxis()->SetTitleOffset(1.2);
234  mg->GetHistogram()->GetXaxis()->CenterTitle(true);
235  mg->GetHistogram()->GetYaxis()->CenterTitle(true);
236  mg->GetHistogram()->GetXaxis()->SetLabelFont(42);
237  mg->GetHistogram()->GetXaxis()->SetTitleFont(42);
238  mg->GetHistogram()->GetYaxis()->SetLabelFont(42);
239  mg->GetHistogram()->GetYaxis()->SetTitleFont(42);
240  mg->Draw("AP");
241 
242  // draw the legend
243 
244  TLegend* leg;
245  double hleg = 0.8-NFILE*0.05;
246  leg = new TLegend(0.5793173,hleg,0.9915385,0.8721805,NULL,"brNDC");
247 
248  leg->SetBorderSize(1);
249  leg->SetTextAlign(22);
250  leg->SetTextFont(12);
251  leg->SetLineColor(1);
252  leg->SetLineStyle(1);
253  leg->SetLineWidth(1);
254  leg->SetFillColor(0);
255  leg->SetFillStyle(1001);
256  leg->SetTextSize(0.05);
257  leg->SetLineColor(kBlack);
258  leg->SetFillColor(kWhite);
259 
260  for(int n=0;n<NFILE;n++) {
261  double median_snr15 = A[n]+pow(fabs(B[n])/15.,1)+pow(C[n]/15.,2);
262  char legLabel[256];
263  //sprintf(legLabel,"%s : %2.1f (%g)",name[n].Data(),median_snr10,nevents[n]);
264  sprintf(legLabel,"SNR(15) = %2.1f",median_snr15);
265  leg->AddEntry(gr[n],legLabel,"lp");
266  }
267  leg->Draw();
268 
269  // save plot
270 
271  if(ofName!="") {
272  TString gfileName=ofName;
273  canvas->Print(gfileName);
274  TString pfileName=gfileName;
275  pfileName.ReplaceAll(".gif",".png");
276  char cmd[1024];
277  sprintf(cmd,"convert %s %s",gfileName.Data(),pfileName.Data());
278  cout << cmd << endl;
279  gSystem->Exec(cmd);
280  sprintf(cmd,"rm %s",gfileName.Data());
281  cout << cmd << endl;
282  gSystem->Exec(cmd);
283  //gSystem->Exit(0);
284  }
285 }
286 
287 Double_t
288 medianfit(Double_t *x, Double_t *par) {
289  // constrain par[1] to be >=0
290  double y = par[0]+pow(fabs(par[1])/x[0],1)+pow(par[2]/x[0],2);
291  return y;
292 }
293 
294 int
295 GetMedian(TString ifName, int type, TString pType, wavearray<double>& snr,
297  float T_win, int pp_inetcc, float T_cor, int pp_irho, float T_cut, float T_vED, float T_pen, float T_ifar) {
298 
299  // open input tree
300  TFile *file0 = TFile::Open(ifName);
301  cout << ifName.Data() << endl;
302  if (file0==NULL) {cout << "Error Opening file" << endl;exit(1);}
303  TTree* itree = (TTree *) gROOT->FindObject(TREE_NAME);
304  if (itree==NULL) {cout << "Error Opening tree" << endl;exit(1);}
305 
306  // get detector list
307  TList* list = itree->GetUserInfo();
308  int nIFO=list->GetSize();
309  if (nIFO==0) {cout << "Error : no ifo present in the tree" << endl;exit(1);}
310  for (int n=0;n<list->GetSize();n++) {
311  detector* pDetector;
312  pDetector = (detector*)list->At(n);
313  detectorParams dParams = pDetector->getDetectorParams();
314  //pDetector->print();
315  }
316 
317  // define selection cuts
318  char cut[1024];
319  char tmp[1024];
320  sprintf(cut,"abs(time[0]-time[%d])<%f && netcc[%d]>%f && rho[%d]>%f",
321  nIFO,T_win,pp_inetcc,T_cor,pp_irho,T_cut);
322  if(T_vED>0) {strcpy(tmp,cut);sprintf(cut,"%s && neted[0]/ecor<%f",tmp,T_vED);}
323  if(T_pen>0) {strcpy(tmp,cut);sprintf(cut,"%s && penalty>%f",tmp,T_pen);}
324  if(type>0) {strcpy(tmp,cut);sprintf(cut,"%s && type[1]==%d",tmp,type);}
325  if(T_ifar>0) {strcpy(tmp,cut);sprintf(cut,"%s && ifar>(24.*3600.*365.)*%f",tmp,T_ifar);}
326 
327 #ifdef LINX
328  double fsnr = (MAX_SNR_NET-MIN_SNR_NET)/NSNR;
329 #else
330  double fsnr = TMath::Exp(TMath::Log(MAX_SNR_NET-MIN_SNR_NET)/NSNR);
331 #endif
332  entries.resize(NSNR);
333  median.resize(NSNR);
334  snr.resize(NSNR);
335  int nsnr=0;
336  double max_median = (pType=="MEDIAN50") ? MAX_MEDIAN50 : MAX_MEDIAN90;
337  double infsnr=MIN_SNR_NET;
338 #ifdef LINX
339  double supsnr=fsnr+infsnr;
340 #else
341  double supsnr=fsnr*infsnr;
342 #endif
343  char SNRnet[256]="";
344  strcpy(tmp,"iSNR[0]");
345  for(int i=1;i<nIFO;i++) {sprintf(SNRnet,"%s+iSNR[%d]",tmp,i);strcpy(tmp,SNRnet);}
346  sprintf(SNRnet,"sqrt(%s)",tmp);
347  int nevents=0;
348  for(int n=0;n<NSNR;n++) {
349 
350  char icut[512];
351  sprintf(icut,"%s && %s>%f && %s<=%f",cut,SNRnet,infsnr,SNRnet,supsnr);
352  //sprintf(icut,"%s && sqrt(likelihood/%d)>%f && sqrt(likelihood/%d)<=%f",cut,nIFO,infsnr,nIFO,supsnr);
353  //cout << icut << endl;
354  itree->Draw("erA[0]",icut,"goff");
355 
356  int size = itree->GetSelectedRows();
357  nevents+=size;
358  double* erA = itree->GetV1();
359 
360  if(size>MIN_NSNR) {
361  int* index = new int[size];
362  TMath::Sort(size,erA,index,false);
363 
364  entries[nsnr] = size;
365  median[nsnr] = (pType=="MEDIAN50") ? erA[index[int(size*0.5)]] : erA[index[int(size*0.9)]]; // median
366  snr[nsnr] = (supsnr+infsnr)/2.; // SNRnet
367  if(median[nsnr]<max_median) {
368  cout << "Selected entries : " << entries[nsnr] << "\t SNRnet : "
369  << snr[nsnr] << "\t median : " << median[nsnr] << endl;
370  nsnr++;
371  }
372 
373  delete [] index;
374  }
375 
376 #ifdef LINX
377  infsnr += fsnr;
378  supsnr += fsnr;
379 #else
380  infsnr *= fsnr;
381  supsnr *= fsnr;
382 #endif
383  }
384 
385  entries.resize(nsnr);
386  median.resize(nsnr);
387  snr.resize(nsnr);
388 
389  return nevents; // return total number of selected events
390 }
391 
#define MAX_MEDIAN90
detectorParams getDetectorParams()
Definition: detector.cc:218
static const double C
Definition: GNGen.cc:28
int GetMedian(TString ifName, int type, TString pType, wavearray< double > &snr, wavearray< double > &median, wavearray< double > &entries, float T_win, int pp_inetcc, float T_cor, int pp_irho, float T_cut, float T_vED, float T_pen, float T_ifar)
double T_ifar
TString ofName
double T_pen
#define MIN_SNR_NET
par [0] name
#define B
int n
Definition: cwb_net.C:28
TString("c")
double T_cor
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
double median(std::vector< double > &vec)
Definition: wavegraph.cc:485
Complex Exp(double phase)
Definition: numpy.cc:51
return wmap canvas
i pp_inetcc
#define NSNR
Long_t size
Color_t colors[16]
Double_t medianfit(Double_t *x, Double_t *par)
i drho i
TList * list
#define nIFO
virtual size_t size() const
Definition: wavearray.hh:145
char str[1024]
TGraph * gr
void DrawMedianPRCvsSNR(TString ilist_file, TString pTitle, TString pType, TString ofName, float T_win=0, int pp_inetcc=0, float T_cor=0, int pp_irho=0, float T_cut=0, float T_vED=0, float T_pen=0, float T_ifar=0)
i() int(T_cor *100))
#define TREE_NAME
double * tmp
Definition: testWDM_5.C:31
char cut[512]
static double A
Definition: geodesics.cc:26
#define MIN_NSNR
vector< mdcpar > par
TFile * ifile
int nevents
#define MAX_SNR_NET
ifstream in
wavearray< int > index
double T_win
double fabs(const Complex &x)
Definition: numpy.cc:55
char cmd[1024]
strcpy(RunLabel, RUN_LABEL)
sprintf(tfres,"(1/%g)x(%g) (sec)x(Hz)", 2 *df, df)
double T_cut
TTree * itree
DataType_t * data
Definition: wavearray.hh:319
double T_vED
virtual void resize(unsigned int)
Definition: wavearray.cc:463
wavearray< double > y
Definition: Test10.C:31
TMultiGraph * mg
i drho pp_irho
#define MAX_FILES
exit(0)
#define MAX_MEDIAN50