Logo coherent WaveBurst  
Config Reference Guide
Logo
Draw_FARvsRHO.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 #define nFAR_MAX 32
21 
22 #define RHO_MIN 4.5
23 //#define RHO_MIN 0.0
24 
25 int nFAR;
26 TString FAR_PLOT_NAME;
27 TString FAR_TITLE;
28 TString FAR_NAME[nFAR_MAX];
33 TString FAR_PATH[nFAR_MAX];
35 
36 int readParameters(TString fname, wavearray<double>& RHO, wavearray<double>& eRHO, wavearray<double>& FAR, wavearray<double>& eFAR);
37 void writeParameters(TString fname, wavearray<double>& RHO, wavearray<double>& FAR);
38 void PlotFAR(int nfar, TString far_plot_name, double rho_min=-1, double rho_max=-1);
39 
40 TString PLOT_PATH = "./";
41 
42 void
43 Draw_FARvsRHO(TString ifile_bkg, TString ifile_fgd, TString ofile="") {
44 
45 
46  Style_t markers[32]= {20,21,22,23,29,29,21,22,20,21,22,23,29,20,21,22,
47  21,22,23,29,29,21,22,20,21,22,23,29,20,21,22,20 };
48 
49  Color_t colors[32] = { 8, 0, 2, 8,43, 7, 8, 4, 4, 2,43, 1, 3, 1, 6, 7,
50  6, 3, 2, 8,43, 7, 8, 4, 4, 2,43, 1, 3, 1, 6, 7 };
51 
52  for(int i=0;i<nFAR_MAX;i++) {
53  LINE_COLOR[i] = colors[i];
54  LINE_STYLE[i] = 1;
55  LINE_MARKER[i] = markers[i];
56  ZERO_LAG_TIME[i] = -1;
57  OBS_TIME[i] = 1;
58  }
59 
60  FAR_TITLE="";
61  FAR_PLOT_NAME="";
62 
63  nFAR = 2;
64  FAR_PATH[0]=ifile_bkg;
65  FAR_PATH[1]=ifile_fgd;
66 
67  PlotFAR(nFAR, ofile, RHO_MIN, -1);
68 
69 }
70 
71 void PlotFAR(int nfar, TString far_plot_name, double rho_min, double rho_max) {
72 
73  // create plots
74  gStyle->SetFrameBorderMode(0); // remove the red box around canvas
75  gROOT->ForceStyle();
76 
77  gStyle->SetTitleFont(72);
78  gStyle->SetMarkerColor(50);
79  gStyle->SetLineColor(kWhite);
80  gStyle->SetTitleW(0.98);
81  gStyle->SetTitleH(0.05);
82  gStyle->SetTitleY(0.98);
83  gStyle->SetFillColor(kWhite);
84  gStyle->SetLineColor(kWhite);
85  gStyle->SetTitleFont(12,"D");
86 
87  TCanvas *canvas = new TCanvas("roc", "roc", 300,40, 800, 600);
88  canvas->Clear();
89  canvas->ToggleEventStatus();
90  canvas->SetLogy();
91  canvas->SetGridx();
92  canvas->SetGridy();
93  canvas->SetFillColor(kWhite);
94 
95  double far_min=1e10;
96  double rmin=1e20;
97  double rmax=0;
98  char fname[1024];
99  int nGR=0;
100  int line_style[3*nFAR_MAX];
101  int line_marker[3*nFAR_MAX];
102  int line_color[3*nFAR_MAX];
103  TString FAR_name[3*nFAR_MAX];
104  bool sigma_lines[3*nFAR_MAX];
105  TGraphErrors* gr[3*nFAR_MAX];
106  for(int n=0;n<nFAR;n++) {
107  cout << n << " OBS_TIME : " << OBS_TIME[n] << endl;
108  if(OBS_TIME[n]>0) if(far_min>1./OBS_TIME[n]) far_min=1./OBS_TIME[n];
109  vector<double> rho_far;
110  vector<double> far;
111  // read FAR
112  sprintf(fname,"%s",FAR_PATH[n].Data());
113  cout << "rate_threshold : " << fname << endl;
114  wavearray<double> RHO;
115  wavearray<double> FAR;
116  wavearray<double> eRHO;
117  wavearray<double> eFAR;
118  int far_size = readParameters(fname, RHO, eRHO, FAR, eFAR);
119  gr[nGR] = NULL;
120  if(far_size==0) continue;
121  if(rmin<RHO[0]) rmin=RHO[0];
122  if(rmax<RHO[far_size-1]) rmax=RHO[far_size-1];
123  sigma_lines[nGR] = false;
124  line_style[nGR]=LINE_STYLE[n];
125  line_marker[nGR]=LINE_MARKER[n];
126  line_color[nGR]=LINE_COLOR[n];
127  FAR_name[nGR]=FAR_NAME[n];
128  gr[nGR++] = new TGraphErrors(far_size,RHO.data,FAR.data,eRHO.data,eFAR.data);
129  gr[nGR-1]->SetMarkerStyle(20);
130  //gr[nGR-1]->SetMarkerSize(0.35);
131  gr[nGR-1]->SetMarkerSize(0.55);
132  if(n==0) gr[nGR-1]->SetMarkerColor(kBlack);
133  if(n==1) {
134  gr[nGR-1]->SetMarkerColor(kRed);
135  gr[nGR-1]->SetMarkerStyle(22);
136  gr[nGR-1]->SetMarkerSize(1.3);
137  }
138  if(n==2) gr[nGR-1]->SetMarkerColor(kRed);
139  gr[nGR-1]->SetName(TString::Format("gr%d",nGR-1));
140  }
141 
142  TMultiGraph* mg = new TMultiGraph();
143  char gTitle[256];
144  if(FAR_TITLE!="") strcpy(gTitle,FAR_TITLE.Data());
145  else sprintf(gTitle,"FAR Comparison");
146  if(FAR_PATH[1].Contains("lag0_slag0")) sprintf(gTitle,"FAR vs Rank");
147  else sprintf(gTitle,"FAR vs Rank (fake lag)");
148  mg->SetName("mg");
149  mg->SetTitle(gTitle);
150  for(int n=0;n<nGR;n++) if(gr[n]!=NULL) mg->Add(gr[n]);
151  //mg->Paint("APL");
152  //mg->Paint("AL");
153  mg->Paint("AP");
154 
155  mg->GetHistogram()->GetXaxis()->SetLabelSize(0.03);
156  mg->GetHistogram()->GetYaxis()->SetLabelSize(0.03);
157  mg->GetHistogram()->GetXaxis()->SetTitleSize(0.03);
158  mg->GetHistogram()->GetYaxis()->SetTitleSize(0.03);
159  mg->GetHistogram()->GetXaxis()->SetLabelFont(42);
160  mg->GetHistogram()->GetYaxis()->SetLabelFont(42);
161  mg->GetHistogram()->GetYaxis()->SetLabelOffset(0.01);
162  mg->GetHistogram()->GetYaxis()->SetTitleOffset(1.5);
163 // mg->GetHistogram()->SetMinimum(far_min/2.);
164 /*
165  if(rho_min>=0) rmin=rho_min*sqrt(2);
166  if(rho_max>=0) rmax=rho_max*sqrt(2);
167  rmax=16*sqrt(2);
168 */
169  if(rho_min>=0) rmin=rho_min;
170  if(rho_max>=0) rmax=rho_max;
171 // if(rmin<RHO_MIN) rmin=RHO_MIN;
172 // mg->GetHistogram()->GetXaxis()->SetRangeUser(rmin,rmax);
173 
174  mg->GetXaxis()->SetTitle(gr[0]->GetXaxis()->GetTitle());
175  mg->GetXaxis()->SetLabelFont(42);
176  mg->GetYaxis()->SetLabelFont(42);
177  mg->GetXaxis()->SetTitleFont(42);
178  mg->GetYaxis()->SetTitleFont(42);
179  mg->GetXaxis()->SetTitleOffset(1.20);
180  mg->GetYaxis()->SetTitleOffset(1.20);
181  mg->GetXaxis()->SetTitleSize(0.03);
182  mg->GetYaxis()->SetTitleSize(0.03);
183  mg->GetXaxis()->CenterTitle(true); //PAPER
184  mg->GetYaxis()->CenterTitle(true); //PAPER
185  mg->GetXaxis()->SetTitle("Coherent Network SNR ( #rho )");
186  //mg->GetXaxis()->SetTitle("#eta_{c}");
187  mg->GetYaxis()->SetTitle("FAR ( yr^{-1} )");
188 
189  mg->Draw("ALP");
190 
191  // draw the legend
192 
193  TLegend* leg;
194 // leg = new TLegend(0.62,0.72,0.90,0.90,NULL,"brNDC");
195 // leg = new TLegend(0.72,0.85,1.0,1.0,NULL,"brNDC");
196  leg = new TLegend(0.72,0.87,1.0,1.0,NULL,"brNDC");
197 
198  leg->SetBorderSize(1);
199  leg->SetTextAlign(22);
200  leg->SetTextFont(12);
201  leg->SetLineColor(1);
202  leg->SetLineStyle(1);
203  leg->SetLineWidth(1);
204  leg->SetFillColor(0);
205  leg->SetFillStyle(1001);
206  leg->SetTextSize(0.03);
207  leg->SetLineColor(kBlack);
208  leg->SetFillColor(kWhite);
209 
210  char legLabel[256];
211  for(int n=0;n<nGR;n++) {
212  if(n==0) strcpy(legLabel,"Background");
213  if(n==1) strcpy(legLabel,"Foreground");
214  if(gr[n]!=NULL) TLegendEntry* eleg = leg->AddEntry(gr[n],legLabel,"lp");
215  }
216  leg->SetBorderSize(1);
217  leg->SetTextAlign(22);
218  leg->SetTextFont(12);
219  leg->SetLineColor(1);
220  leg->SetLineStyle(1);
221  leg->SetLineWidth(1);
222  leg->SetFillColor(0);
223  leg->SetFillStyle(1001);
224  leg->SetTextSize(0.05);
225  leg->SetLineColor(kBlack);
226  leg->Draw();
227 
228  // save plot
229  if(far_plot_name!="") {
230  char gfileName[1024];
231  sprintf(gfileName,"%s.gif",far_plot_name.Data());
232  canvas->Print(gfileName);
233  TString pfileName=gfileName;
234  pfileName.ReplaceAll(".gif",".png");
235  char cmd[1024];
236  sprintf(cmd,"convert %s %s",gfileName,pfileName.Data());
237  cout << cmd << endl;
238  gSystem->Exec(cmd);
239  sprintf(cmd,"rm %s",gfileName);
240  cout << cmd << endl;
241  gSystem->Exec(cmd);
242  gSystem->Exit(0);
243  }
244 
245  return;
246 }
247 
248 void writeParameters(TString fname, wavearray<double>& RHO, wavearray<double>& FAR) {
249 
250  ofstream out;
251  out.open(fname.Data(),ios::out);
252  if (!out.good()) {cout << "Error Opening File : " << fname.Data() << endl;exit(1);}
253 
254  for(int i=0;i<RHO.size();i++) {
255  out << RHO[i] << "\t" << FAR[i] << endl;
256  }
257 
258  out.close();
259 }
260 
261 
262 
263 int readParameters(TString fname, wavearray<double>& RHO, wavearray<double>& eRHO, wavearray<double>& FAR, wavearray<double>& eFAR) {
264 
265  double year = (24.*3600.*365.);
266 
267  RHO.resize(1000000);
268  eRHO.resize(1000000);
269  FAR.resize(1000000);
270  eFAR.resize(1000000);
271 
272  RHO=0.;
273  FAR=0.;
274 
275  ifstream in;
276  in.open(fname.Data(),ios::in);
277  if (!in.good()) {cout << "Error Opening File : " << fname.Data() << endl;exit(1);}
278  cout << fname.Data() << endl;
279 
280  wavearray<double> rho(1000000);
281  wavearray<double> far(1000000);
282  int m=0;
283  while (1) {
284  in >> rho[m] >> far[m];
285  if (!in.good()) break;
286  if(rho[m]>=RHO_MIN) m++;
287  }
288  in.close();
289  rho.resize(m);
290  far.resize(m);
291 
292  if(rho.size()==0) {
293  RHO.resize(0);
294  eRHO.resize(0);
295  FAR.resize(0);
296  eFAR.resize(0);
297  return 0;
298  }
299 
300  double pifar=far[rho.size()-1];
301  RHO[0] = rho[rho.size()-1];
302  eRHO[0] = 0;
303  FAR[0] = far[rho.size()-1];
304  eFAR[0] = 0;
305  int n=1;
306  for(int i=rho.size()-2;i>=0;i--) {
307  if(far[i]>pifar) {
308  RHO[n] = rho[i];
309  eRHO[n] = 0;
310  FAR[n] = far[i];
311  eFAR[n] = 0;
312  pifar=far[i];
313  n++;
314  }
315  }
316 
317  RHO.resize(n);
318  eRHO.resize(n);
319  FAR.resize(n);
320  eFAR.resize(n);
321 
322  wavearray<double> xRHO=RHO;
323  wavearray<double> xFAR=FAR;
324  for(int i=0;i<RHO.size();i++) {
325  RHO[i]=xRHO[RHO.size()-i-1];
326  FAR[i]=xFAR[FAR.size()-i-1];
327  FAR[i]*=year;
328  }
329  for(int i=0;i<RHO.size();i++) {
330 // cout << i << " " << RHO[i] << " " << FAR[i] << endl;
331  }
332 
333  return RHO.size();
334 }
335 
336 
337 
TString FAR_TITLE
Definition: Draw_FARvsRHO.C:27
int LINE_COLOR[nFAR_MAX]
Definition: Draw_FARvsRHO.C:29
int LINE_STYLE[nFAR_MAX]
Definition: Draw_FARvsRHO.C:30
int LINE_MARKER[nFAR_MAX]
Definition: Draw_FARvsRHO.C:31
void Draw_FARvsRHO(TString ifile_bkg, TString ifile_fgd, TString ofile="")
Definition: Draw_FARvsRHO.C:43
#define RHO_MIN
Definition: Draw_FARvsRHO.C:22
#define nFAR_MAX
Definition: Draw_FARvsRHO.C:20
TString FAR_PLOT_NAME
Definition: Draw_FARvsRHO.C:26
double ZERO_LAG_TIME[nFAR_MAX]
Definition: Draw_FARvsRHO.C:34
void writeParameters(TString fname, wavearray< double > &RHO, wavearray< double > &FAR)
void PlotFAR(int nfar, TString far_plot_name, double rho_min=-1, double rho_max=-1)
Definition: Draw_FARvsRHO.C:71
TString FAR_NAME[nFAR_MAX]
Definition: Draw_FARvsRHO.C:28
TString PLOT_PATH
Definition: Draw_FARvsRHO.C:40
int nFAR
Definition: Draw_FARvsRHO.C:25
double OBS_TIME[nFAR_MAX]
Definition: Draw_FARvsRHO.C:32
TString FAR_PATH[nFAR_MAX]
Definition: Draw_FARvsRHO.C:33
int readParameters(TString fname, wavearray< double > &RHO, wavearray< double > &eRHO, wavearray< double > &FAR, wavearray< double > &eFAR)
TCanvas * canvas
Definition: Make_PP_IFAR.C:166
wavearray< double > xFAR
Definition: Make_PP_IFAR.C:184
TLegend * leg
Definition: Make_PP_IFAR.C:173
TGraph * gr
Definition: Make_PP_IFAR.C:167
strcpy(analysis,"2G")
shift breaksw case n
Definition: cwb_clchunk.csh:75
shift breaksw case m
char fname[256]