Logo coherent WaveBurst  
Library Reference Guide
Logo
JoinCutANN_Mchirp_ROCcurves_rho2.C
Go to the documentation of this file.
1 /*
2 # Copyright (C) 2019 Serena Vinciguerra
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 #define XIFO 4
20 
21 #pragma GCC system_header
22 
23 #include "cwb.hh"
24 #include "config.hh"
25 #include "network.hh"
26 #include "wavearray.hh"
27 #include "TString.h"
28 #include "TObjArray.h"
29 #include "TObjString.h"
30 #include "TPaletteAxis.h"
31 #include "TMultiLayerPerceptron.h"
32 #include "TMLPAnalyzer.h"
33 #include "TRandom.h"
34 #include "TComplex.h"
35 #include "TMath.h"
36 #include "mdc.hh"
37 #include "watplot.hh"
38 #include <vector>
39 
40 
41 //#define nINP 64
42 #define Ncc3D 50
43 #define maxCC 1
44 #define minCC 0
45 #define nIFO 3
46 #define nRHO 3
47 #define nANN 10
48 #define deltaANN 0.1
49 #define deltacc 0.05
50 #define deltarho 1
51 #define nCC 4
52 #define NPIX 20
53 #define RHOth 5.5
54 #define nth 10
55 #define rhomin 6
56 #define ccmin 0.6
57 #define iMc 1
58 #define NMc_inj 50
59 #define minMc_inj 0
60 #define maxMc_inj 100
61 #define NMc_r 50
62 #define maxMc_r 40
63 #define minMc_r -5
64 #define NANN_av 50
65 #define minANN_av -0.2
66 #define maxANN_av 1.5
67 
68 //definition parameters for SNR_plots
69 #define N_ANNth 50
70 #define N_RHOth 50
71 #define ANNth_max 1.5
72 #define ANNth_min -0.5
73 #define RHOth_max 10.0
74 #define RHOth_min 4.5
75 
76 #define x_asym 0.00009999999999999999999999999999
77 #define nANNth 6
78 #define min_yasym -0.1
79 #define max_yasym 0.6
80 #define max_Mcth 2.5
81 #define min_Mcth 1.5
82 #define npoints 50
83 #define max_k 10
84 #define min_k 0.01
85 
86 #define cutMc1 0.5
87 #define cutMc2 1.5
88 #define cutANN1 0.15
89 #define cutANN2 0.25
90 #define maxrho 20.
91 #define minrho 0.
92 
93 
94 using namespace std;
95 void graph(TString ifile);
96 void Annth(TString ifile, int &FAth_n, int &FDth_n, double &thresFA);
97 void Mcth(TString ifile, int &McFAth_n, int &McFDth_n, double &McthresFA);
100 void SNR_plots(TString ifile);
101 void CoG_plots(TString ifile);
102 void CutMcANN(TString ifile);
103 //void Test0(TString NN_FILE,TString TEST_FILE,TString ofile, int TS=0, int TB=0, int s=0, int b=0, int uf=0){
104 
105 //NN_FILE-NN_FILE8: name of the files where the interested ANNs are saved (!!they must become from a directory whose name ends in "NN").
106 //TEST_FILE: name of the file which contains the events you are going to analyse (!!they must become from a directory whose name ends in "nnTREE").
107 //TS, TB: numbers of events to test for each class.
108 //s, b: index values of the first events considered for both the classes. If b < #SIG_events, b is newly set to b=b+#SIG_events
109 //uf !=0 means the Test file of events is the same used to train the network, in this case ni==0. It means that the events used for the training are analysed only by the other ANNs, in this way we test the events with at least NNn-1 networks. If uf==0 the Test and Train files are different, in this case ni!=0 and all the events can beanalysed by all the nNN networks.
110 //consider_all=1(0): the events not used for the training procedure are (aren't) considered for the test.
111 //ni: =0 excludes from the test the training set of events
112 void JoinCutANN_Mchirp_ROCcurves_rho2(TString NN_FILE,TString NN_FILE2, TString NN_FILE3, TString NN_FILE4, TString NN_FILE5, TString NN_FILE6, TString NN_FILE7, TString NN_FILE8, TString TEST_FILE, TString NOMEtot_S, int TS=0, int TB=0, int s=0, int b=0, int uf=1, int consider_all=0, int av_on_nNN=0){
113 int ni=0;
114 double FAdes=0.01;
115 if (uf==0&&av_on_nNN!=0) cout<<"all events were not used for the training procedure"<<endl;
116 if(uf==0) {ni=1; consider_all=1; av_on_nNN=0;}
117 else ni=0; //events used for the training not considered for the ANN trained on them
118 
119 int TS0=0;
120 TS0=TS;
121 int TB0=0;
122 TB0=TB;
123 //if(uf!=0&&consider_all==0) ni=1;
124 
125 //COUNT THE ANNs
126 int n_NN=0;
127 if (NN_FILE.CompareTo("")){
128  n_NN=n_NN+1;
129  }
130 if (NN_FILE2.CompareTo("")){
131  n_NN=n_NN+1;
132  }
133 if (NN_FILE3.CompareTo("")){
134  n_NN=n_NN+1;
135  }
136 if (NN_FILE4.CompareTo("")){
137  n_NN=n_NN+1;
138  }
139 if (NN_FILE5.CompareTo("")){
140  n_NN=n_NN+1;
141  }
142 if (NN_FILE6.CompareTo("")){
143  n_NN=n_NN+1;
144  }
145 if (NN_FILE7.CompareTo("")){
146  n_NN=n_NN+1;
147  }
148 if (NN_FILE8.CompareTo("")){
149  n_NN=n_NN+1;
150  }
151 int const nNN=n_NN;
152 
153 // int p=0;
154  char NNi[nNN][1024];
155  char NNi2[nNN][1024];
156  sprintf(NNi2[0],"%s",NN_FILE.Data());
157  if(nNN>1) sprintf(NNi2[1],"%s",NN_FILE2.Data());
158  if(nNN>2) sprintf(NNi2[2],"%s",NN_FILE3.Data());
159  if(nNN>3) sprintf(NNi2[3],"%s",NN_FILE4.Data());
160  if(nNN>4) sprintf(NNi2[4],"%s",NN_FILE5.Data());
161  if(nNN>5) sprintf(NNi2[5],"%s",NN_FILE6.Data());
162  if(nNN>6) sprintf(NNi2[6],"%s",NN_FILE7.Data());
163  if(nNN>7) sprintf(NNi2[7],"%s",NN_FILE8.Data());
164 
165 if(nNN==1) {av_on_nNN=1; consider_all=0; ni=0;}
166 //saving the ANN names
167  for (int u=0;u<nNN;u++){
168  int p=0;
169  while (NNi2[u][p]){
170  if (NNi2[u][p]=='N'){
171  if((NNi2[u][p+1]=='N')&&(NNi2[u][p+2]=='\/')) {
172  int hh=p+3;
173  while (NNi2[u][hh]!='\0'){NNi[u][hh-p-3]=NNi2[u][hh];hh=hh+1;}
174  break;
175  }
176  }
177  p=p+1;
178  }
179 }
180 
181 //saving the Test-file name
182 int q=0;
183  char Filei[1024];
184  char Filei2[1024];
185  sprintf(Filei2,"%s",TEST_FILE.Data());
186  while (Filei2[q]){
187  if(Filei2[q]=='n'&&Filei2[q+1]=='n'&&Filei2[q+2]=='T'&&Filei2[q+3]=='R'&&Filei2[q+4]=='E'&&(Filei2[q+5]=='E')&&(Filei2[q+6]=='\/')) {
188  int hh=q+7;
189  while (Filei2[hh]!='\0') {Filei[hh-q-7]=Filei2[hh];hh=hh+1;
190  }
191  for (int h0=hh-q-7;h0<1024;h0++) Filei[h0]='\0';
192  break;
193  }
194  q=q+1;
195  }
196 
197  cout<<Filei<<" original String: "<<Filei2<<endl;
198 
199 
200  TString NNi0[nNN];
201  NNi0[0]=NNi[0];
202  if(nNN>1) NNi0[1]=NNi[1];
203  if(nNN>2) NNi0[2]=NNi[2];
204  if(nNN>3) NNi0[3]=NNi[3];
205  if(nNN>4) NNi0[4]=NNi[4];
206  if(nNN>5) NNi0[5]=NNi[5];
207  if(nNN>6) NNi0[6]=NNi[6];
208  if(nNN>7) NNi0[7]=NNi[7];
209 
210  TString Filei0(Filei);
211 
212 //removing the extensions
213 for (int u=0;u<nNN;u++){
214  NNi0[u].ReplaceAll(".root","");
215  }
216  Filei0.ReplaceAll(".root","");
217 
218 //Definitions of some parameters
219  TFile *fnet[nNN];
220  TMultiLayerPerceptron* mlp[nNN];
221  TTree* infot[nNN];
222  int TotBg[nNN];
223  int FD0[nNN]; //FD=False Dismissal
224  int FA0[nNN]; //FA=False Alarm
225 
226  for (int yy=0; yy<nNN; yy++){TotBg[yy]=0;FA0[yy]=0;FD0[yy]=0;}
227 
228  int NNs[nNN]; //first SIG-event considered for ANN trainings
229  int NNb[nNN]; //first BKG-event considered for ANN trainings
230  int NNnTS[nNN]; //number of SIG-events considered for ANN trainings
231  int NNnTB[nNN]; //number of BKG-events considered for ANN trainings
232 
233 int min_NNb=5000000;
234 int min_NNs=5000000;
235 int max_NNb=0;
236 int max_NNs=0;
237 
238 //Extracting ANNs and information from the files
239 //if (uf!=0&&ni==0&&(TS!=0||TB!=0)){
240  for (int u=0;u<nNN;u++){
241  fnet[u]=TFile::Open(NNi2[u]);
242  mlp[u]=(TMultiLayerPerceptron*)fnet[u]->Get("TMultiLayerPerceptron");
243 
244  cout<<"dopo TMLP"<<endl;
245  if(mlp[u]==NULL) {cout << "Error getting mlp" << endl;exit(1);}
246  infot[u]=(TTree*)fnet[u]->Get("info");
247 
248  infot[u]->SetBranchAddress("Rand_start_Sig",&NNs[u]);
249  infot[u]->SetBranchAddress("Rand_start_Bg",&NNb[u]);
250  infot[u]->SetBranchAddress("#trainSig",&NNnTS[u]);
251  infot[u]->SetBranchAddress("#trainBg",&NNnTB[u]);
252  infot[u]->GetEntry(0);
253  cout<<"n "<<u<<" b "<<NNb[u]<<" min_NNb "<<min_NNb<<endl;
254 
255  if(NNs[u]<min_NNs) min_NNs=NNs[u];
256  if(NNb[u]<min_NNb) min_NNb=NNb[u];
257  if(NNs[u]>max_NNs) max_NNs=NNs[u];
258  if(NNb[u]>max_NNb) max_NNb=NNb[u];
259  cout<<"n "<<u<<" s "<<NNs[u]<<" b "<<NNb[u]<<" s fin "<<NNs[u]+NNnTS[u]<<" b fin "<<NNb[u]+NNnTB[u]<<" nTS "<<NNnTS[u]<<" nTB "<<NNnTB[u]<<endl;
260  }
261 
262 //Changing b value to its sum to the first event considered for the training
263 //if (uf!=0&&ni==0&&(b<min_NNb)&&(TS!=0||TB!=0)){ b+=min_NNb;cout<<" change in b value to have BKG events: "<<b<<endl;}
264 /*if (uf!=0&&ni==0&&(b<min_NNb)){ b+=min_NNb;cout<<" change in b value to have BKG events: "<<b<<endl;}
265 if (uf!=0&&ni==0&&(s<min_NNs)){ s+=min_NNs;cout<<" change in s value to have SIG events: "<<s<<endl;}*/
266 //if (uf!=0&&consider_all==0&&av_on_nNN==0&&(b<min_NNb)){ b+=min_NNb;cout<<" change in b value to have BKG events: "<<b<<endl;}
267 //if (uf!=0&&consider_all==0&&av_on_nNN==0&&(s<min_NNs)){ s+=min_NNs;cout<<" change in s value to have SIG events: "<<s<<endl;}
268 
269 //if (uf!=0&&av_on_nNN!=0&&(b>min_NNb)&&(b<))
270 cout<<" min_NNb "<<min_NNb<<" b "<<b<<endl;
271 cout<<"Def. tree e mlp"<<endl;
272 
273 //opening the Test-file
274  TFile* fTEST =TFile::Open(TEST_FILE.Data());
275 cout<<"Def. tree e mlp"<<endl;
276  TTree* NNTree=(TTree*)fTEST->Get("nnTree");
277 cout<<"Def. tree e mlp"<<endl;
278  int entries=NNTree->GetEntries();
279 // int entries=0;
280 
281  cout<<"entries: "<<entries<<endl;
282  cout<<" NNTree->GetEntries() "<<NNTree->GetEntries()<<endl;
283 //exit(0);
284 //Extract information necessary for the tree which defines the mlp
285  int ndim;
286  int ninp;
287  int y;
288  int entriesTot;
289  int bg_entries;
290  int sig_entries;
291 
292  NNTree->SetBranchAddress("#Entries_type",&entriesTot);
293  NNTree->SetBranchAddress("Matrix_dim",&ndim);
294  NNTree->SetBranchAddress("#inputs",&ninp);
295  NNTree->SetBranchAddress("amplitude_mode",&y);
296 
297  NNTree->GetEntry(0);
298  sig_entries=entriesTot;
299  NNTree->GetEntry(entries-1);
300  bg_entries=entriesTot;
301  cout<<"sig "<<sig_entries<<" bg "<<bg_entries<<endl;
302  int const NDIM=ndim;
303  int const nINP=ninp;
304  cout<<"NDIM: "<<NDIM<<endl;
305  cout<<"nINP: "<<nINP<<endl;
306  cout<<"sig e: "<<sig_entries<<endl;
307  cout<<"bg e: "<<bg_entries<<endl;
308 
309 //defining the sample which has less events
310  int minevents=0;
311  if (sig_entries>bg_entries) minevents=bg_entries;
312  else minevents=sig_entries;
313 
314  //if(b==0) b=sig_entries;
315  if(TB==0 && TS==0){
316  TS=minevents;
317  TB=minevents;
318  }
319 cout<<"TS "<<TS<<" TB "<<TB<<" minevents "<<minevents<<endl;
320 //defining compatible parameters
321  if (b<sig_entries){
322  int a =b;
323  b+=sig_entries;
324  cout<<"Error: Bg index<sig_entries: new set of b parameter-> b="<<b<<" instead of b="<<a<<endl;
325  //exit(0);
326 }
327  if(s>sig_entries){
328  int a=s;
329  s-=sig_entries;
330  cout<<"Error: Sig index>sig_entries: new set of s parameter-> s="<<b<<" instead of s="<<a<<endl;
331 }
332 if((TS>sig_entries-s||TB>(bg_entries-(b-sig_entries)))&&(TS==TB)) {TS=minevents-s;TB=minevents-(b-sig_entries);
333  if (TS<TB) TB=TS;
334  else TS=TB;
335  cout<<"Error:S>sig_entries or TB>bg_entries, to maintain ugual number of analysed events TS=TB="<<TB<<endl;
336  }
337  if((TS>sig_entries-s||TB>bg_entries-(b-sig_entries))&&(TS!=TB)) {
338  if(TS>sig_entries) TS=sig_entries-s;
339  if (TB>bg_entries) TB=bg_entries-(b-sig_entries);
340  cout<<"Error:S>sig_entries or TB>bg_entries, new TS and TB values are thus define-> TS="<<TS<<" TB="<<TB<<endl;
341  }
342 
343 
344  char NOMEtot[1024];
345  sprintf(NOMEtot,"%s",NOMEtot_S.Data());
346  cout<<"output name: "<<NOMEtot<<endl;
347 
348 //extracting original files
349  TString SIG_FILE;
350  TString BG_FILE;
351  char FILE_NAME[516];
352  NNTree->SetBranchAddress("Files_name",&FILE_NAME);
353  NNTree->GetEntry(0);
354  SIG_FILE=FILE_NAME;
355  NNTree->GetEntry(entries-1);
356  BG_FILE=FILE_NAME;
357  cout<<"fine ifdef RHO_CC"<<endl;
358 
359 //extracting other information
360  TChain sigTree("waveburst");//search the Tree "waveburst" in files
361  sigTree.Add(SIG_FILE.Data());
362  netevent signal(&sigTree,nIFO);
363  int sig_entries2 = signal.GetEntries();
364  cout << "sig entries2 : " << sig_entries2 << endl;
365 
366  TChain bgTree("waveburst");
367  bgTree.Add(BG_FILE.Data());
368  netevent background(&bgTree,nIFO);
369  int bg_entries2 = background.GetEntries();
370  cout << "bg entries2 : " << bg_entries2 << endl;
371 
372  cout<<"b: "<<b<<endl;
373  cout<<"s: "<<s<<endl;
374 
375  // add leaf
376  Float_t x[nINP];
377  for (int jj=0; jj<nINP;jj++) x[jj]=0.;
378  char ilabel[nINP][16];
379 
380  // define a branch for each input
381  for(int i=0;i<nINP;i++) {
382  sprintf(ilabel[i],"x%i",i+1);
383  NNTree->SetBranchAddress(ilabel[i], &x[i]);
384  }
385 
386 char ofile[1024];
387 sprintf(ofile,"average_file/%s.root",NOMEtot);
388 TFile*f =new TFile(ofile,"RECREATE");
389  TTree* NNTree2=new TTree("Parameters","Parameters");
390  NNTree2->SetDirectory(f);
391 
392 // defining useful information
393  double average=0.;
394  double out[nNN];
395  for(int y=0; y<nNN; y++) out[y]=0.;
396  double Mc=0.;
397  double NNcc=0.;
398  double NNrho=0.;
399  double Mc_inj=0.;
400  double std=0.;
401  int ev_ind=0;
402  int CoG=0;
403  // int ev_ind;
404  int index_ev=0;
405  char TestFile[1024];
406 //adding new information to the original tree
407  NNTree2->Branch("Center_of_Gravity",&CoG,"Center_of_Gravity/I");
408  NNTree2->Branch("Average",&average,"Average/D");
409  NNTree2->Branch("StandardDevaition",&std,"StandardDeviation/D");
410  NNTree2->Branch("cc",&NNcc,"cc/D");
411  NNTree2->Branch("Mchirp",&Mc,"Mchirp/D");
412  NNTree2->Branch("Mchirp_injected",&Mc_inj,"Mchirp_injected/D");
413  NNTree2->Branch("rho",&NNrho,"rho/D");
414  NNTree2->Branch("index",&index_ev,"index/I");
415 
416 // NNTree2->Branch("event_index",&ev_ind,"event_index/I");
417  NNTree2->Branch("Test_file",&TestFile,"Test_file/C");
418 
419  for(int u=0;u<nNN;u++){
420  char NNoutl[512];
421  char NNoutl2[512];
422  char NNnamel[512];
423  char NNnamel2[512];
424  sprintf(NNoutl,"NNout%i",u);
425  sprintf(NNoutl2,"NNout%i/D",u);
426  sprintf(NNnamel,"NNname%i",u);
427  sprintf(NNnamel2,"NNname%i/C",u);
428  NNTree2->Branch(NNoutl,&out[u],NNoutl2);
429  NNTree2->Branch(NNnamel,&NNi[u],NNnamel2);
430  }
431 
432  char Testf[1024];
433  NNTree2->Branch("TestFile",&Testf,"TestFile/C");
434  sprintf(Testf,"%s",TEST_FILE.Data());
435  int nTestS=0;
436 
437 
438 //defining how many examples to use
439  int scount=0;
440  if(uf==0) nTestS=TS;
441  else {
442  //for(int n=s;n<s+TS;n++) {
443  for(int n=s;n<sig_entries;n++) {
444  int countNN=0;
445  for(int u=0;u<nNN;u++){
446  if (uf!=0&&n>NNs[u]&&n<(NNs[u]+NNnTS[u])) countNN=countNN+1;
447  }
448 
449  // if(consider_all==0&&av_on_nNN==0&&countNN==0)continue; //skip if no network is trained on the considered event
450  // if(consider_all==0&&av_on_nNN!=0&&countNN!=0) {sigskip=sigskip+1; continue; }
451 
452  if(countNN==0&&av_on_nNN!=0) scount=scount+1;
453  else{
454  //if(nNN==1&&countNN==1&&av_on_nNN!=0) scount=scount+1;
455  if(countNN==1&&consider_all==0&&av_on_nNN==0) scount=scount+1;
456  if(countNN<2&&consider_all!=0) scount=scount+1; //if you are indifferently intereseted in average on nNN or nNN-1
457  if(countNN>1){cout<<"Error: training non independent"<<n<<endl; exit(0);} //exit if the ANNs are training on dependent set of events
458  cout<<"n "<<n<<"; countNN "<<countNN<<"; scount "<< scount<<"; consider_all "<<consider_all<<"; av_on_nNN "<<av_on_nNN<<endl;
459  if(scount==TS) break;}
460  }
461  nTestS=scount;
462 }
463 cout<<" nTestS "<< nTestS<<" TS "<<TS<<endl;
464 //exit(0);
465 int B_eff=0;
466 int FA=0;
467 int bg_05=0;
468 int cont_su=0;
469 int cont_su5=0;
470 int cont_su4=0;
471 int cont_su3=0;
472 
473 int nTestB=0;
474  int bcount=0;
475  if(uf==0) nTestB=TB;
476  else {
477  //for(int n=b;n<b+TB;n++) {
478  for(int n=b;n<sig_entries+bg_entries;n++) {
479  int countNN=0;
480  for(int u=0;u<nNN;u++){
481  if (uf!=0&&n>NNb[u]&&n<(NNb[u]+NNnTB[u])) countNN=countNN+1;
482  }
483  if(countNN==0&&av_on_nNN!=0) bcount=bcount+1;
484  //if(nNN==1&&countNN==1&&av_on_nNN!=0) bcount=bcount+1;
485  if(countNN==1&&consider_all==0&&av_on_nNN==0) bcount=bcount+1;
486  if(countNN<2&&consider_all!=0) bcount=bcount+1; //if you are indifferently intereseted in average on nNN or nNN-1
487  if(countNN>1){cout<<"Error: training non independent"<<n<<endl; exit(0);} //exit if the ANNs are training on dependent set of events
488  if(bcount==TB) break;
489  }
490  nTestB=bcount;
491 }
492 
493 if(uf!=0) nTestS=nTestS-1;
494 if(uf!=0) nTestB=nTestB-1;
495 //TB=TB-1;
496 //TS=TS-1;
497 cout<<"TS "<<TS<<" TB "<<TB<<" nTestS "<<nTestS<<" nTestB "<<nTestB<<endl;
498 if(TS0==0&&TB0==0) {
499  if(nTestB>nTestS) {nTestB=nTestS; TB=nTestS; TS=nTestS;}
500  else {nTestS=nTestB; TB=nTestB; TS=nTestB;}
501  }
502 
503 if(nTestS<TS) TS= nTestS;
504 else nTestS=TS;
505 
506 // NNTree2->Branch("#TestSig",&TS,"#TestSig/I");
507  NNTree2->Branch("#TestSig",&TS,"#TestSig/I");
508  //NNTree2->Branch("#TestSig",&nTestS,"#TestSig/I");
509  cout<<"nTestS: "<<nTestS<<" TS: "<<TS<<endl;
510  cout<<"dopo def tree"<<endl;
511 cout<<"s test "<<s<< " s+TS "<<s+TS<<" b "<<b<<" b+ TB "<<b+TB<<" nTestS "<<nTestS<<" TB "<<TB<<" nTestB "<<nTestB<<endl;
512 double params[nINP];
513  int sig_05=0;
514  for (int i=0; i<nINP; i++) params[i]=0.;
515 
516  int S_eff=0;
517  int FD=0;
518 int sigskip=0;
519 
520  TString txt_originaldata(NOMEtot_S);
521  txt_originaldata.ReplaceAll(".root","_originalData.txt");
522  //txt_originaldata.ReplaceAll(".root","_originalData.txt");
523  char txt_originaldata_c[1024];
524  sprintf(txt_originaldata_c,"txt_files/%s.txt",txt_originaldata.Data());
525  ofstream od_txt(txt_originaldata_c);
526 
527 
528  //for(int n=s;n<s+TS;n++) {
529  for(int n=s;n<sig_entries;n++) {
530 double ti_0=0;
531 double fi_0=0;
532 
533  index_ev=n;
534  average=0.;
535  int indNN=-1;
536  int countNN=0;
537  for(int u=0;u<nNN;u++){
538  if (uf!=0&&n>=NNs[u]&&n<(NNs[u]+NNnTS[u])) { indNN=u;countNN=countNN+1;}
539  }
540  if (countNN>1){cout<<"Error: training non independent"; exit(0);}
541  if(nNN==1){cout<<"out==Averege:only 1NN is considered"<<endl;}
542  if(consider_all==0&&av_on_nNN==0&&countNN==0)continue; //skip if no network is trained on the considered event
543  if(consider_all==0&&av_on_nNN!=0&&countNN!=0) {sigskip=sigskip+1; continue; }
544  NNTree->GetEntry(n);
545  signal.GetEntry(n);
546  char line_od[1024];
547 
548  // exracting parameters
549  NNcc=(double)signal.netcc[0];
550  NNrho=(double)signal.rho[1];
551  Mc=(double)signal.chirp[iMc];
552  Mc_inj=(double)signal.chirp[0];
553 
554  sprintf(TestFile,"%s",TEST_FILE.Data());
555 
556  sprintf(line_od,"%i \n",n);
557  od_txt<<line_od;
558  char line_od2[1024];
559  // evaluating the event
560  for (int i=0; i<nINP; i++){
561  params[i]=x[i];
562 
563  int ti_i=0;
564  int fi_i=0;
565  ti_i=i/NDIM;
566  fi_i=i-ti_i*NDIM;
567  ti_0+=(double)x[i]*ti_i;
568  fi_0+=(double)x[i]*fi_i;
569  sprintf(line_od2,"%s %1.5f",line_od2, x[i]);
570  }
571  sprintf(line_od2,"%s \n",line_od2);
572 
573  if (ti_0<fi_0) CoG=1;
574  else CoG=0;
575 
576 //cout<<" ti_0 "<<ti_0<<" fi_0 "<<fi_0<<" CoG "<<CoG<<endl;
577  od_txt<<line_od2;
578  for (int u=0;u<nNN;u++) {
579 // cout<<" u "<<u<<" nNN "<<nNN<<endl;
580  double output=mlp[u]->Evaluate(0,params);
581 // cout<<output<<endl;
582  out[u]=output;
583  }
584 
585 // cout<<"Dopo param"<<endl;
586 
587 //calculating the average and the standard deviation
588 
589  int c_nNN0=0;
590  for (int u=0;u<nNN;u++){
591  if((u!=indNN&&ni==0)||uf==0) {average=out[u]+average;std+=out[u]*out[u];c_nNN0=c_nNN0+1; if(out[u]<0.6) FD0[u]=FD0[u]+1;}
592  }
593 
594  average=average/(c_nNN0);
595  if((c_nNN0)!=1)std=pow((std/(nNN-1-countNN)-average*average),0.5);
596  else std=-1;
597 
598  char line_od3[1024];
599  sprintf(line_od3,"ev_ind %i average %1.5f Mc %1.5f Mc_inj %1.5f n %i \n",index_ev, average,Mc,Mc_inj,n);
600  od_txt<<line_od3;
601 
602  if (average<0.6) FD=FD+1;
603  NNTree2->Fill();
604  S_eff=S_eff+1;
605  if(S_eff==1) s=n;
606  if(S_eff==TS) break;
607  cout<<" cc "<<NNcc<<" NNrho "<<NNrho<<" Mc_inj "<< Mc_inj<<endl;
608 }
609 
610 od_txt.close();
611 //exit(0);
612 cout<<txt_originaldata_c<<endl;
613 //exit(0);
614 //closing the cycle on SIG events
615 cout<<" S_eff "<<S_eff<<" s "<<s<<" b "<<b<<" sigskip "<<sigskip<<endl;
616 //exit(0);
617 //defining BKG parameters
618 
619 int bgskip=0;
620 //opening the cycle on BKG events
621  for(int n=b;n<sig_entries+bg_entries;n++) {
622 double ti_0=0;
623 double fi_0=0;
624  index_ev=0;
625  index_ev=n;
626  average=0.;
627  int indNN=-1;
628  int countNN=0;
629  for(int u=0;u<nNN;u++){
630  cout<<" uf "<<uf<<" n "<<n<<" u "<<u<<" NN b[u] "<<NNb[u]<<" NNb[u]+NNnTB[u]"<<NNb[u]+NNnTB[u]<<endl;
631  if (uf!=0&&n>=NNb[u]&&n<(NNb[u]+NNnTB[u])) { indNN=u;countNN=countNN+1; }
632  }
633  if (countNN>1){cout<<"Error: training non independent"; exit(0);}
634  if(nNN==1){cout<<"out==Averege:only 1NN is considered"<<endl;}
635  cout<<"consider_all "<<consider_all<<" av_on_nNN "<<av_on_nNN<<" countNN "<<countNN<<" indNN "<<indNN<<endl;
636  if(consider_all==0&&av_on_nNN==0&&countNN==0)continue; //skip if no network is trained on the considered event
637  if(consider_all==0&&av_on_nNN!=0&&countNN!=0) { cout<<" n skipped "<<n<<endl; bgskip=bgskip+1; continue;}
638  NNTree->GetEntry(n);
639  cout<<"BKG->n: "<<n<<"Bg index"<<(n-sig_entries)<<endl;
640  background.GetEntry(n-sig_entries);
641  //NNcc=(double)background.netcc[1];
642  NNcc=(double)background.netcc[0];
643  //NNrho=(double)background.rho[0];
644  NNrho=(double)background.rho[1];
645  Mc=(double)background.chirp[iMc];
646  Mc_inj=(double)background.chirp[0];
647  /*int hn=0;
648  ev_ind=0;
649  hn=n;
650  ev_ind=hn;*/
651  sprintf(TestFile,"%s",TEST_FILE.Data());
652 
653 for (int i=0; i<nINP; i++){
654  params[i]=x[i];
655  int ti_i=0;
656  int fi_i=0;
657  ti_i=i/NDIM;
658  fi_i=i-ti_i*NDIM;
659  ti_0+=(double)x[i]*ti_i;
660  fi_0+=(double)x[i]*fi_i;
661 
662  }
663  if (ti_0<fi_0) CoG=1;
664  else CoG=0;
665 
666  for(int u=0;u<nNN;u++) {
667  double output=mlp[u]->Evaluate(0,params);
668  cout<<output<<endl;
669  out[u]=output;
670  }
671 
672  int c_nNN0=0;
673  int nnsu=0;
674  for (int u=0;u<nNN;u++){
675  if((u!=indNN&&ni==0)||uf==0) {average=out[u]+average;std+=out[u]*out[u];c_nNN0=c_nNN0+1; TotBg[u]=TotBg[u]+1;if(out[u]>0.6) {FA0[u]=FA0[u]+1;nnsu=nnsu+1;}}
676  }
677 
678 
679  average=average/(c_nNN0);
680  if((c_nNN0)!=1)std=pow((std/(nNN-1-countNN)-average*average),0.5);
681  else std=-1;
682 
683  if(nnsu==6) cont_su=cont_su+1;
684  if(nnsu>4) cont_su5=cont_su5+1;
685  if(nnsu>3) cont_su4=cont_su4+1;
686  if(nnsu>2) cont_su3=cont_su3+1;
687 
688 //calculating the final average value
689 
690 
691 //saving false alarms
692  if(average>0.6) FA=FA+1;
693 
694  NNTree2->Fill();
695  B_eff=B_eff+1;
696  if(B_eff==1) b=n;
697  if(B_eff==TB) break;
698  }
699 //cout<<"bkg filled"<<endl;
700 
701 NNTree2->Write();
702 f->Close();
703 cout<<"B_eff "<<B_eff<<" TB "<<TB<<endl;
704 //cout<<"closed file"<<endl;
705 cout<<ofile<<endl;
706 
707 int cont=0;
708 
709 double freq_c[nNN];
710 double meanf=0.;
711 double dev=0.;
712 
713 for (int yy=0; yy<nNN; yy++){
714  freq_c[yy]=0.;
715 // cout<<" FA0[yy] "<<FA0[yy]<<" FD0[yy] "<<FD0[yy]<<" FD "<<FD<<" TotBg[yy] "<<TotBg[yy]<<endl;
716  if(TotBg[yy]!=0){freq_c[yy]=(double)FA0[yy]/TotBg[yy];
717  //cout<<" freq_c[yy] "<<freq_c[yy]<<endl;
718  }
719  if(freq_c[yy]!=0) {meanf=freq_c[yy]+meanf;cont=cont+1;
720  //cout<<" cont "<<cont<<endl;
721  }
722  dev=freq_c[yy]*freq_c[yy]+dev;
723 }
724 
725 if(cont==0) cout<<"Error cont==0"<<endl;
726 if(cont!=0) meanf=meanf/cont;
727 dev=0.;
728 for (int yy=0; yy<nNN; yy++){
729 //cout<<" dev "<<dev<<endl;
730 //cout<<"freq_c[yy] "<<freq_c[yy]<<" meanf "<<meanf<<endl;
731 if(freq_c[yy]!=0)dev+=(freq_c[yy]-meanf)*(freq_c[yy]-meanf);
732 }
733 double McFAdes=FAdes;
734 int FAth_n=FAdes*B_eff;
735 int McFAth_n=FAdes*B_eff;
736 cout<<"FAth_n "<<FAth_n<<" FAdes*B_eff "<<FAdes*B_eff<<endl;
737 int FDth_n=-1;
738 int McFDth_n=-1;
739 double thresFA=-1;
740 double McthresFA=-1;
741 //Annth(ofile, FAth_n, FDth_n, thresFA);
742 //Mcth(ofile, McFAth_n, McFDth_n, McthresFA);
743 //graph(ofile);
744 //PlotsAv_cc(ofile);
745 //PlotsAv_Mc(ofile);
746 //SNR_plots(ofile);
747 //CoG_plots(ofile);
748 CutMcANN(ofile);
749 if(cont!=0) dev=pow(dev/(cont-1),0.5);
750 cout<<"meanf "<<meanf<<" dev "<<dev<<endl;
751 cout<<" FA average "<<FA<<endl;
752 cout<<" cont_su "<<cont_su<<" cont_su5 "<<cont_su5<<" cont_su4 "<<cont_su4<<" cont_su3 "<<cont_su3<<endl;
753 
754 cout<<" S_eff "<<S_eff<<" B_eff "<<B_eff<<" s "<<s<<" b "<<b<<" bgskip "<<bgskip<<" sigskip "<<sigskip<<endl;
755 cout<<" FA0[1] "<<FA0[1]<<" TotBg[1] "<<TotBg[1]<<endl;
756 cout<<" FD0[1] "<<FD0[1]<<" TotBg[1] "<<TotBg[1]<<endl;
757 //double S_eff2=(double
758 double FDdes=(double)FDth_n/S_eff;
759 double McFDdes=(double)McFDth_n/S_eff;
760 FAdes=(double)FAth_n/B_eff;
761 McFAdes=(double)McFAth_n/B_eff;
762 cout<<"FAth_n "<<FAth_n<<" FAdes: "<<FAdes<<" ANN_av_FDdes "<<FDdes<<" ANN_av_FDth_n "<<FDth_n<<" ANN_av_thresFA "<<thresFA<<endl;
763 cout<<"McFAth_n "<<McFAth_n<<" McFAdes: "<<McFAdes<<" Mc_FDdes "<<McFDdes<<" McFDth_n "<<McFDth_n<<" McthresFA "<<McthresFA<<endl;
764 }
765 
767  TString name(ifile);
768  name.ReplaceAll("average_file/","");
769  TFile* fTEST =TFile::Open(ifile.Data());
770  TTree* NNTree2=(TTree*)fTEST->Get("Parameters");
771  double av;
772  double cc;
773  double rho;
774  int nSi;
775  NNTree2->SetBranchAddress("Average",&av);
776  NNTree2->SetBranchAddress("cc",&cc);
777  NNTree2->SetBranchAddress("rho",&rho);
778  NNTree2->SetBranchAddress("#TestSig",&nSi);
779  NNTree2->GetEntry(0);
780  int const nSig=nSi;
781  cout<<"nSig: "<<nSig<<" nSi: "<<nSi<<endl;
782  cout<<" BG: "<<NNTree2->GetEntries()-nSi<<endl;
783  int const ncurve=nANN*nCC;
784  cout<<"dentro funzione dopodef"<<endl;
785 //LOG(NBg)vs RHO------------------------------------------
786  double* rhoSig[ncurve];
787  for (int i=0;i<ncurve;i++) rhoSig[i]=new double[nSig];
788  cout<<"dopo def rhoSig"<<endl;
789  //double rhoSig[20][10];
790  int NSig[ncurve];
791 // int nBg=0;
792  int const nBg=NNTree2->GetEntries()-nSig;
793  for (int i=0;i<ncurve;i++) {
794  NSig[i]=0;
795  for (int j=0;j<nSig;j++) rhoSig[i][j]=0.;
796  }
797  cout<<"dopo def rhoSig"<<endl;
798  double* rhoBg[ncurve];
799  for (int i=0;i<ncurve;i++) rhoBg[i]=new double[nBg];
800  //double rhoBg[20][10];
801  int NBg[ncurve];
802  for (int i=0;i<ncurve;i++) {
803  NBg[i]=0;
804  for (int j=0;j<nBg;j++) rhoBg[i][j]=0.;
805  }
806  cout<<"dopo def rhoBg"<<endl;
807  double ccTh[nCC];
808  for (int i=0;i<nCC;i++) ccTh[i]=0.;
809  double NNTh[nANN];
810  for (int i=0;i<nANN;i++) NNTh[i]=0.;
811  //double deltacc=0.;
812 // double deltaANN=0.;
813 // deltacc=0.2/nCC;
814  //deltaANN=0.6/(nANN-1);
815 
816  cout<<NNTree2->GetEntries()<<endl;
817  for(int n=0;n<NNTree2->GetEntries();n++){
818  cout<<n<<endl;
819  NNTree2->GetEntry(n);
820  cout<<"rho "<<rho<<" cc "<<cc<<" av "<<av<<endl;
821  for(int i=0; i<nCC;i++){
822  ccTh[i]=0.5+i*deltacc;
823  if(cc<ccTh[i]) continue;
824  for(int j=0; j<nANN;j++){
825  if(j==0) NNTh[j]=-1000.;
826  //else NNTh[j]=0.5+(j-1)*deltaANN;
827  //else NNTh[j]=0.1+(j-1)*deltaANN;
828  else NNTh[j]=0.+(j)*deltaANN;
829  //else NNTh[j]=0.+(j-1)*deltaANN/1000.;
830  //else NNTh[j]=1.;
831  if(av<NNTh[j]) continue;
832  int ni=0;
833  if(n>=nSig) {
834 
835  NBg[i*nANN+j]= NBg[i*nANN+j]+1;
836  while(rhoBg[i*nANN+j][ni]!=0)ni=ni+1;
837  rhoBg[i*nANN+j][ni]=rho;
838  // cout<<"rho: "<<rho<<" colonna "<<i*nANN+j<<" riga "<<ni<<endl;
839 // cout<<" soglia_cc "<<ccTh[i]<<" soglia_ANN "<<NNTh[j]<<endl;
840  }
841  else {
842  NSig[i*nANN+j]= NSig[i*nANN+j]+1;
843  while(rhoSig[i*nANN+j][ni]!=0)ni=ni+1;
844  rhoSig[i*nANN+j][ni]=rho;
845  // cout<<"rho: "<<rho<<" colonna "<<i*nANN+j<<" riga "<<ni<<endl;
846 // cout<<" soglia_cc "<<ccTh[i]<<" soglia_ANN "<<NNTh[j]<<endl;
847  }
848  // }
849  }
850  }
851  }
852  cout<<"dopo riempimento variabili"<<endl;
853  int* indexS[ncurve];
854  for (int i=0;i<ncurve;i++) indexS[i]=new int[nSig];
855 
856  TGraph * gS[ncurve];
857  for (int y=0;y<ncurve;y++) {
858  int igS=0;
859  int igS_p=0;
860  /* int indexS[nSig];
861  double rhoS[nSig];
862  for(int i=0;i<nSig;i++) {
863  rhoS[i]=0.;
864  indexS[i]=0;
865  }*/
866  // ig=1;
867  // for(int i=0;i<nSig;i++) rhoS[i]=rhoSig[y][i];
868  gS[y]=new TGraph();
869 // gS[y]->SetMarkerStyle(7);
870  TMath::Sort(nSig,rhoSig[y],indexS[y],false);
871 // cout<<"dopo Sort "<<y<<endl;
872  for (int k=0;k<nSig;k++) {
873  int ii=indexS[y][k];
874  int yy=0;
875  if (k>0){
876 // cout<<"k "<<k<<endl;
877  int ij=indexS[y][k-1];
878  //if(rhoSig[y][ii]!=0&&rhoSig[y][ii]!=rhoSig[y][ij]) {
879  if(rhoSig[y][ii]!=0) {
880  yy=NSig[y]-igS;
881  //gS[y]->SetPoint(igS,rhoSig[y][ii],yy);
882  if(rhoSig[y][ii]!=rhoSig[y][ij]) gS[y]->SetPoint(igS_p++,rhoSig[y][ii],yy);
883  cout<<"igS"<<igS<<" x "<<rhoSig[y][ii]<<" y: "<<yy<<endl;
884  igS=igS+1;
885  }
886  }
887  else {
888  if(rhoSig[y][ii]!=0){
889  yy=NSig[y]-igS;
890  gS[y]->SetPoint(0,rhoSig[y][ii],yy);
891  igS=igS+1;
892 // cout<<" x "<<rhoSig[y][ii]<<" y: "<<yy<<endl;
893  }
894 
895  }
896  }
897  }
898  // cout<<"dopo inserimento puntiiS"<<endl;
899 // cout<<ncurve<<endl;
900  int* indexB[ncurve];
901  for (int i=0;i<ncurve;i++) indexB[i]=new int[nBg];
902 
903  TGraph * gB[ncurve];
904  for (int y=0;y<ncurve;y++) {
905  int igB=0;
906  int igB_p=0;
907  gB[y]=new TGraph();
908  TMath::Sort(nBg,rhoBg[y],indexB[y],false);
909 // cout<<"dopo Sort "<<y<<endl;
910  for (int k=0;k<nBg;k++) {
911  int ii=indexB[y][k];
912  int yy=0;
913  if (k>0){
914  int ij=indexB[y][k-1];
915  //if(rhoBg[y][ii]!=0&&rhoBg[y][ii]!=rhoBg[y][ij]) {
916  if(rhoBg[y][ii]!=0) {
917  yy=NBg[y]-igB;
918  //gB[y]->SetPoint(igB,rhoBg[y][ii],yy);
919  if(rhoBg[y][ii]!=rhoBg[y][ij]) gB[y]->SetPoint(igB_p++,rhoBg[y][ii],yy);
920  igB=igB+1;
921 // cout<<"igB"<<igB<<" x "<<rhoBg[y][ii]<<" y: "<<yy<<endl;
922  }
923  }
924  else {
925  if(rhoBg[y][ii]!=0) {
926  yy=NBg[y]-igB;
927  gB[y]->SetPoint(0,rhoBg[y][ii],yy);
928  igB=igB+1;
929 // cout<<"igB"<<igB<<" x "<<rhoBg[y][ii]<<" y: "<<yy<<endl;
930  }
931  }
932  }
933  }
934  cout<<"dopo inserimento puntiB"<<endl;
935  //gB[1]->SetMarkerStyle(7);
936  //gB[1]->SetPoint(1,1,2);
937  TCanvas* cS=new TCanvas("Efficiency_vs_rho","Efficiency_vs_rho",0,0,1200,700);
938  cS->Divide(2,2);
939  //cS->cd(1)->SetLogy();
940  cS->cd(1);
941  TMultiGraph* mg1=new TMultiGraph();
942 // gS[0]->SetMarkerStyle(7);
943  gS[0]->SetMarkerColor(2);
944  gS[0]->SetLineColor(2);
945  mg1->SetTitle("cc=0.5;rho;#Events");
946  if(gS[0]->GetN()!=0) mg1->Add(gS[0]);
947 // gS[0]->Draw("apl");
948  for (int h=1;h<nANN;h++){
949  gS[h]->SetMarkerColor(3);
950  gS[h]->SetLineColor(3);
951 // gS[h]->SetMarkerStyle(h+1);
952  if(gS[h]->GetN()!=0) mg1->Add(gS[h]);
953  // gS[h]->Draw("apl,same");
954  }
955  mg1->Draw("apl");
956  //cS->cd(2)->SetLogy();
957  cS->cd(2);
958  TMultiGraph* mg2=new TMultiGraph();
959 // gS[nANN]->SetMarkerStyle(7);
960  gS[nANN]->SetMarkerColor(2);
961  gS[nANN]->SetLineColor(2);
962  mg2->SetTitle("cc=0.55;rho;#Events");
963  if(gS[nANN]->GetN()!=0) mg2->Add(gS[nANN]);
964  for (int h=1;h<nANN;h++){
965  gS[nANN+h]->SetMarkerColor(3);
966  gS[nANN+h]->SetLineColor(3);
967 // gS[nANN+h]->SetMarkerStyle(h+1);
968  if(gS[nANN+h]->GetN()!=0) mg2->Add(gS[nANN+h]);
969  // gS[nANN+h]->Draw("apl,same");
970  }
971  mg2->Draw("apl");
972  //cS->cd(3)->SetLogy();
973  cS->cd(3);
974  TMultiGraph* mg3=new TMultiGraph();
975 // gS[nANN*2]->SetMarkerStyle(7);
976  gS[nANN*2]->SetMarkerColor(2);
977  gS[nANN*2]->SetLineColor(2);
978  mg3->SetTitle("cc=0.6;rho;#Events");
979  if(gS[nANN*2]->GetN()!=0) mg3->Add(gS[nANN*2]);
980  for (int h=1;h<nANN;h++){
981  gS[2*nANN+h]->SetMarkerColor(3);
982  gS[2*nANN+h]->SetLineColor(3);
983 // gS[2*nANN+h]->SetMarkerStyle(h+1);
984  if(gS[2*nANN+h]->GetN()!=0) mg3->Add(gS[2*nANN+h]);
985  // gS[2*nANN+h]->Draw("apl,same");
986  }
987  mg3->Draw("apl");
988  //cS->cd(4)->SetLogy();
989  cS->cd(4);
990  TMultiGraph* mg4=new TMultiGraph();
991 // gS[nANN*3]->SetMarkerStyle(7);
992  gS[nANN*3]->SetMarkerColor(2);
993  gS[nANN*3]->SetLineColor(2);
994  mg4->SetTitle("cc=0.65;rho;#Events");
995  if(gS[nANN*3]->GetN()!=0) mg4->Add(gS[nANN*3]);
996  // gS[nANN*3]->Draw("apl");
997  for (int h=1;h<nANN;h++){
998  gS[3*nANN+h]->SetMarkerColor(3);
999  gS[3*nANN+h]->SetLineColor(3);
1000 // gS[3*nANN+h]->SetMarkerStyle(h+1);
1001  if(gS[3*nANN+h]->GetN()!=0) mg4->Add(gS[3*nANN+h]);
1002  // gS[3*nANN+h]->Draw("apl,same");
1003  }
1004  mg4->Draw("apl");
1005  cout<<"nuovo canv"<<endl;
1006  TCanvas* cB=new TCanvas("Number_vs_rho","Number_vs_rho",0,0,1200,700);
1007  cB->Divide(2,2);
1008  cB->cd(1)->SetLogy();
1009  TMultiGraph* mg1B=new TMultiGraph();
1010 // gB[0]->SetMarkerStyle(7);
1011  gB[0]->SetMarkerColor(2);
1012  gB[0]->SetLineColor(2);
1013  mg1B->SetTitle("cc=0.5;rho;#Events");
1014  if(gB[0]->GetN()!=0) mg1B->Add(gB[0]);
1015  for (int h=1;h<nANN;h++){
1016  gB[h]->SetMarkerColor(3);
1017  gB[h]->SetLineColor(3);
1018  // gB[h]>SetMarkerStyle(h+1);
1019  if(gB[h]->GetN()!=0) mg1B->Add(gB[h]);
1020  //gB[h]->Draw("apl,same");
1021  }
1022  mg1B->Draw("apl");
1023  cB->cd(2)->SetLogy();
1024  TMultiGraph* mg2B=new TMultiGraph();
1025  //gB[nANN]->SetMarkerStyle(7);
1026  gB[nANN]->SetMarkerColor(2);
1027  gB[nANN]->SetLineColor(2);
1028  mg2B->SetTitle("cc=0.55;rho;#Events");
1029  if(gB[nANN]->GetN()!=0) mg2B->Add(gB[nANN]);
1030  for (int h=1;h<nANN;h++){
1031  gB[nANN+h]->SetMarkerColor(3);
1032  gB[nANN+h]->SetLineColor(3);
1033  //gB[nANN+h]>SetMarkerStyle(h+1);
1034  if(gB[nANN+h]->GetN()!=0) mg2B->Add(gB[nANN+h]);
1035  //gB[h]->Draw("apl,same");
1036  }
1037  mg2B->Draw("apl");
1038  cB->cd(3)->SetLogy();
1039  TMultiGraph* mg3B=new TMultiGraph();
1040 // gB[2*nANN]->SetMarkerStyle(7);
1041  gB[2*nANN]->SetMarkerColor(2);
1042  gB[2*nANN]->SetLineColor(2);
1043  mg3B->SetTitle("cc=0.6;rho;#Events");
1044  if(gB[2*nANN]->GetN()!=0) mg3B->Add(gB[2*nANN]);
1045  for (int h=1;h<nANN;h++){
1046  gB[2*nANN+h]->SetMarkerColor(3);
1047  gB[2*nANN+h]->SetLineColor(3);
1048 // gB[2*nANN+h]>SetMarkerStyle(h+1);
1049  if(gB[2*nANN+h]->GetN()!=0) mg3B->Add(gB[2*nANN+h]);
1050  //gB[h]->Draw("apl,same");
1051  }
1052  mg3B->Draw("apl");
1053  cB->cd(4)->SetLogy();
1054  TMultiGraph* mg4B=new TMultiGraph();
1055 // gB[3*nANN]->SetMarkerStyle(7);
1056  gB[3*nANN]->SetMarkerColor(2);
1057  gB[3*nANN]->SetLineColor(2);
1058  mg4B->SetTitle("cc=0.65;rho;#Events");
1059  if(gB[3*nANN]->GetN()!=0) mg4B->Add(gB[3*nANN]);
1060  for (int h=1;h<nANN;h++){
1061  gB[3*nANN+h]->SetMarkerColor(3);
1062  gB[3*nANN+h]->SetLineColor(3);
1063 // gB[3*nANN+h]>SetMarkerStyle(h+1);
1064  if(gB[3*nANN+h]->GetN()!=0) mg4B->Add(gB[3*nANN+h]);
1065  //gB[h]->Draw("apl,same");
1066  }
1067  mg4B->Draw("apl");
1068 
1069 // cout<<"dopo Draw()"<<endl;
1070  TString CnameS(name);
1071  TString CnameB(name);
1072  TString CnameSroot(name);
1073  TString CnameBroot(name);
1074  char CnameS2[1024];
1075  char CnameB2[1024];
1076  char CnameS2root[1024];
1077  char CnameB2root[1024];
1078  CnameS.ReplaceAll(".root",".png");
1079  CnameB.ReplaceAll(".root",".png");
1080  sprintf(CnameS2,"logN_rho_av/logN_rho_S_dANN%1.2f_%s",deltaANN,CnameS.Data());
1081  sprintf(CnameB2,"logN_rho_av/logN_rho_B_dANN%1.2f_%s",deltaANN,CnameB.Data());
1082  sprintf(CnameS2root,"logN_rho_av/logN_rho_S_dANN%1.2f_%s",deltaANN,CnameSroot.Data());
1083  sprintf(CnameB2root,"logN_rho_av/logN_rho_B_dANN%1.2f_%s",deltaANN,CnameBroot.Data());
1084  cS->SaveAs(CnameS2);
1085  cB->SaveAs(CnameB2);
1086  cS->Print(CnameS2root);
1087  cB->Print(CnameB2root);
1088 // cout<<"fine"<<endl;
1089 
1090 }
1091 
1092 
1093 void Annth(TString ifile, int &FAth_n, int &FDth_n, double &thresFA){
1094  TString name(ifile);
1095  name.ReplaceAll("average_file/","");
1096  TFile* fTEST =TFile::Open(ifile.Data());
1097  TTree* NNTree2=(TTree*)fTEST->Get("Parameters");
1098  double av;
1099  double cc;
1100  double rho;
1101  int nSi;
1102  NNTree2->SetBranchAddress("Average",&av);
1103  NNTree2->SetBranchAddress("cc",&cc);
1104  NNTree2->SetBranchAddress("rho",&rho);
1105  NNTree2->SetBranchAddress("#TestSig",&nSi);
1106  NNTree2->GetEntry(0);
1107  int const nSig=nSi;
1108  cout<<"nSig: "<<nSig<<" nSi: "<<nSi<<endl;
1109  int const ncurve2=nRHO*nCC;
1110 
1111 
1112  double* ANNSig[ncurve2];
1113  for (int i=0;i<ncurve2;i++) ANNSig[i]=new double[nSig];
1114  //double rhoSig[20][10];
1115  int NSig[ncurve2];
1116 // int nBg=0;
1117  int const nBg=NNTree2->GetEntries()-nSig;
1118  for (int i=0;i<ncurve2;i++) {
1119  NSig[i]=0;
1120  for (int j=0;j<nSig;j++) ANNSig[i][j]=0.;
1121  }
1122  double* ANNBg[ncurve2];
1123  for (int i=0;i<ncurve2;i++) ANNBg[i]=new double[nBg];
1124 
1125  //double rhoBg[20][10];
1126  int NBg[ncurve2];
1127  for (int i=0;i<ncurve2;i++) {
1128  NBg[i]=0;
1129  for (int j=0;j<nBg;j++) ANNBg[i][j]=0.;
1130  }
1131  double ccTh[nCC];
1132  for (int i=0;i<nCC;i++) ccTh[i]=0.;
1133  double rhoTh[nRHO];
1134  for (int i=0;i<nRHO;i++) rhoTh[i]=0.;
1135  // double deltacc=0.;
1136  // double deltarho=0.;
1137  // deltacc=0.2/nCC;
1138 // deltarho=1./(nRHO);
1139 
1140 //int thresFA=-1;
1141  for(int n=0;n<NNTree2->GetEntries();n++){
1142  NNTree2->GetEntry(n);
1143  cout<<"rho "<<rho<<" cc "<<cc<<" av "<<av<<endl;
1144  for(int i=0; i<nCC;i++){
1145  ccTh[i]=0.5+i*deltacc;
1146  ccTh[0]=0.;
1147  if(cc<ccTh[i]) continue;
1148  for(int j=0; j<nRHO;j++){
1149  rhoTh[j]=5+j*deltarho;
1150  rhoTh[0]=0.;
1151  if(rho<rhoTh[j]) continue;
1152  int ni=0;
1153  if(n>=nSig) {
1154  NBg[i*nRHO+j]= NBg[i*nRHO+j]+1;
1155  if(i*nRHO+j==0) cout<<" NBg[i*nRHO+j] "<<NBg[i*nRHO+j]<<endl;
1156  while(ANNBg[i*nRHO+j][ni]!=0)ni=ni+1;
1157  ANNBg[i*nRHO+j][ni]=av;
1158  // if (NBg[0]==FAth_n) thresFA=av;
1159  // cout<<" soglia_cc "<<ccTh[i]<<" soglia_RHO "<<rhoTh[j]<<endl;
1160  }
1161  else {
1162  NSig[i*nRHO+j]= NSig[i*nRHO+j]+1;
1163  if(i*nRHO+j==0) cout<<" NSig[i*nRHO+j] "<<NSig[i*nRHO+j]<<endl;
1164  while(ANNSig[i*nRHO+j][ni]!=0)ni=ni+1;
1165  ANNSig[i*nRHO+j][ni]=av;
1166  // cout<<" soglia_cc "<<ccTh[i]<<" soglia_RHO "<<rhoTh[j]<<endl;
1167  }
1168  }
1169  }
1170  }
1171  int* indexS[ncurve2];
1172  for (int i=0;i<ncurve2;i++) indexS[i]=new int[nSig];
1173 
1174  TGraph * gS[ncurve2];
1175  for (int y=0;y<ncurve2;y++) {
1176  int igS=0;
1177  int igS_p=0;
1178  gS[y]=new TGraph();
1179  TMath::Sort(nSig,ANNSig[y],indexS[y],false);
1180  for (int k=0;k<nSig;k++) {
1181  int ii=indexS[y][k];
1182  int yy=0;
1183  if (k>0){
1184  //cout<<"k "<<k<<endl;
1185  int ij=indexS[y][k-1];
1186  //if(ANNSig[y][ii]!=0&&ANNSig[y][ii]!=ANNSig[y][ij]) {
1187  if(ANNSig[y][ii]!=0) {
1188  yy=NSig[y]-igS;
1189  //gS[y]->SetPoint(igS,ANNSig[y][ii],yy);
1190  if(ANNSig[y][ii]!=ANNSig[y][ij]) gS[y]->SetPoint(igS_p++,ANNSig[y][ii],yy);
1191  // cout<<"igS"<<igS<<" x "<<ANNSig[y][ii]<<" y: "<<yy<<endl;
1192  igS=igS+1;
1193 
1194  }
1195  }
1196  else {
1197  if(ANNSig[y][ii]!=0){
1198  yy=NSig[y]-igS;
1199  gS[y]->SetPoint(0,ANNSig[y][ii],yy);
1200  igS=igS+1;
1201  // cout<<" x "<<ANNSig[y][ii]<<" y: "<<yy<<endl;
1202  }
1203 
1204  }
1205  }
1206  }
1207 
1208 
1209 int thres_ind=0;
1210  int* indexB[ncurve2];
1211  for (int i=0;i<ncurve2;i++) indexB[i]=new int[nBg];
1212 
1213  TGraph * gB[ncurve2];
1214  for (int y=0;y<ncurve2;y++) {
1215  int igB=0;
1216  int igB_p=0;
1217  gB[y]=new TGraph();
1218  TMath::Sort(nBg,ANNBg[y],indexB[y],false);
1219  // cout<<"dopo Sort "<<y<<endl;
1220  for (int k=0;k<nBg;k++) {
1221  int ii=indexB[y][k];
1222  int ik=indexB[y][k-1];
1223  int yy=0;
1224  if (k>0){
1225  int ij=indexB[y][k-1];
1226  //if(ANNBg[y][ii]!=0&&ANNBg[y][ii]!=ANNBg[y][ij]) {
1227  if(ANNBg[y][ii]!=0) {
1228  yy=NBg[y]-igB;
1229  if(y==0 && yy==FAth_n+1) {thresFA=ANNBg[y][ii];}
1230  if (y==0 && yy<FAth_n+1) if(ANNBg[y][ii]==ANNBg[y][ik]){FAth_n=FAth_n-1;}
1231  if(y==0) cout<<"yy "<<yy<<" y "<<y<<" FAth_n "<<FAth_n<<" thresFA "<<thresFA<<" ANNBg[y][ii] "<<ANNBg[y][ii]<<endl;
1232  //gB[y]->SetPoint(igB,ANNBg[y][ii],yy);
1233  if(ANNBg[y][ii]!=ANNBg[y][ij]) gB[y]->SetPoint(igB_p++,ANNBg[y][ii],yy);
1234  igB=igB+1;
1235  // cout<<"igB"<<igB<<" x "<<ANNBg[y][ii]<<" y: "<<yy<<endl;
1236  }
1237  }
1238  else {
1239  if(ANNBg[y][ii]!=0) {
1240  yy=NBg[y]-igB;
1241  gB[y]->SetPoint(0,ANNBg[y][ii],yy);
1242  igB=igB+1;
1243  // cout<<"igB"<<igB<<" x "<<ANNBg[y][ii]<<" y: "<<yy<<endl;
1244  }
1245  }
1246  }
1247  }
1248 //int FDth_n=0;
1249 //for(int i=0; i<nCC;i++){
1250 // for(int j=0; j<nRHO;j++){
1251 FDth_n=0;
1252  for(int ni=1;ni<nSig;ni++){
1253  cout<<"FAth_n "<<FAth_n<<" thresFA "<<thresFA<<" ANNSig[0][indexS[0][ni]] "<<ANNSig[0][indexS[0][ni]]<<" FDth_n "<<FDth_n<<" NSig[0]-(ni-1 )"<<NSig[0]-(ni-1)<<" NSig[0] "<<NSig[0]<<endl;
1254 // if (ANNSig[0][indexS[0][ni]]==thresFA || (ANNSig[0][indexS[0][ni-1]]<thresFA && ANNSig[0][indexS[0][ni]]>thresFA)) FDth_n=NSig[0]-(ni-1);
1255  if (ANNSig[0][indexS[0][ni]]<=thresFA) FDth_n=FDth_n+1;
1256  }
1257 // }
1258 //}
1259 //exit(0);
1260  TCanvas* cS=new TCanvas("Efficiency_vs_ANN","Efficiency_vs_ANN",0,0,1200,700);
1261  cS->Divide(2,2);
1262  //cS->cd(1)->SetLogy();
1263  cS->cd(1);
1264  TMultiGraph* mg1=new TMultiGraph();
1265  mg1->SetTitle("cc: no_cut;ANN;#Events");
1266  for (int h=0;h<nRHO;h++){
1267  gS[h]->SetLineColor(4);
1268  if(gS[h]->GetN()!=0) mg1->Add(gS[h]);
1269  }
1270  mg1->Draw("al");
1271  cS->cd(2);
1272  // cS->cd(2)->SetLogy();
1273  TMultiGraph* mg2=new TMultiGraph();
1274  mg2->SetTitle("cc=0.55;ANN;#Events");
1275  for (int h=0;h<nRHO;h++){
1276  gS[nRHO+h]->SetLineColor(4);
1277  if(gS[nRHO+h]->GetN()!=0) mg2->Add(gS[nRHO+h]);
1278  }
1279  mg2->Draw("al");
1280 
1281  //cS->cd(3)->SetLogy();
1282  cS->cd(3);
1283  TMultiGraph* mg3=new TMultiGraph();
1284  mg3->SetTitle("cc=0.6;ANN;#Events");
1285  for (int h=0;h<nRHO;h++){
1286  gS[2*nRHO+h]->SetLineColor(4);
1287  if(gS[2*nRHO+h]->GetN()!=0) mg3->Add(gS[2*nRHO+h]);
1288  }
1289  mg3->Draw("al");
1290  //cS->cd(4)->SetLogy();
1291  cS->cd(4);
1292  TMultiGraph* mg4=new TMultiGraph();
1293  mg4->SetTitle("cc=0.65;ANN;#Events");
1294  for (int h=0;h<nRHO;h++){
1295  gS[3*nRHO+h]->SetLineColor(4);
1296  if(gS[3*nRHO+h]->GetN()!=0) mg4->Add(gS[3*nRHO+h]);
1297  }
1298  mg4->Draw("al");
1299 
1300  TCanvas* cB=new TCanvas("Number_vs_ANN","Number_vs_ANN",0,0,1200,700);
1301  cB->Divide(2,2);
1302  cB->cd(1)->SetLogy();
1303  TMultiGraph* mg1B=new TMultiGraph();
1304  mg1B->SetTitle("cc: no_cut;ANN;#Events");
1305  for (int h=0;h<nRHO;h++){
1306  gB[h]->SetLineColor(4);
1307  if(gB[h]->GetN()!=0) mg1B->Add(gB[h]);
1308  }
1309  mg1B->Draw("al");
1310  cB->cd(2)->SetLogy();
1311  TMultiGraph* mg2B=new TMultiGraph();
1312  mg2B->SetTitle("cc=0.55;ANN;#Events");
1313  for (int h=0;h<nRHO;h++){
1314  gB[nRHO+h]->SetLineColor(4);
1315  if(gB[nRHO+h]->GetN()!=0) mg2B->Add(gB[nRHO+h]);
1316  }
1317  mg2B->Draw("al");
1318  cB->cd(3)->SetLogy();
1319  TMultiGraph* mg3B=new TMultiGraph();
1320  mg3B->SetTitle("cc=0.6;ANN;#Events");
1321  for (int h=0;h<nRHO;h++){
1322  gB[2*nRHO+h]->SetLineColor(4);
1323  if(gB[2*nRHO+h]->GetN()!=0) mg3B->Add(gB[2*nRHO+h]);
1324  }
1325  mg3B->Draw("al");
1326  cB->cd(4)->SetLogy();
1327  TMultiGraph* mg4B=new TMultiGraph();
1328  mg4B->SetTitle("cc=0.65;ANN;#Events");
1329  for (int h=0;h<nRHO;h++){
1330  gB[3*nRHO+h]->SetLineColor(4);
1331  if(gB[3*nRHO+h]->GetN()!=0) mg4B->Add(gB[3*nRHO+h]);
1332  }
1333  mg4B->Draw("al");
1334 
1335 
1336  //cout<<"dopo Draw()"<<endl;
1337  TString CnameS(name);
1338  TString CnameB(name);
1339  TString CnameSroot(name);
1340  TString CnameBroot(name);
1341  char CnameS2[1024];
1342  char CnameB2[1024];
1343  char CnameS2root[1024];
1344  char CnameB2root[1024];
1345  CnameS.ReplaceAll(".root",".png");
1346  CnameB.ReplaceAll(".root",".png");
1347  sprintf(CnameS2,"ANNthres_av/N_ANN_S_%s",CnameS.Data());
1348  sprintf(CnameB2,"ANNthres_av/N_ANN_B_%s",CnameB.Data());
1349  sprintf(CnameS2root,"ANNthres_av/N_ANN_S_%s",CnameSroot.Data());
1350  sprintf(CnameB2root,"ANNthres_av/N_ANN_B_%s",CnameBroot.Data());
1351  cS->SaveAs(CnameS2);
1352  cB->SaveAs(CnameB2);
1353  cS->Print(CnameS2root);
1354  cB->Print(CnameB2root);
1355  //cout<<"fine"<<endl;
1356 
1357 //CARTELLA Annth
1358 
1359 
1360 
1361 }
1362 
1363 void Mcth(TString ifile, int &McFAth_n, int &McFDth_n, double &McthresFA){
1364 TString name(ifile);
1365  name.ReplaceAll("outfile/","");
1366  name.ReplaceAll("average_file/","");
1367  TFile* fTEST =TFile::Open(ifile.Data());
1368  TTree* NNTree2=(TTree*)fTEST->Get("Parameters");
1369  //double outbis;
1370  double av;
1371  double out;
1372  double cc;
1373  double Mc;
1374  double Mc_inj;
1375  double rho;
1376  int nSi;
1377 // NNTree2->SetBranchAddress("ANNout",&out);
1378  NNTree2->SetBranchAddress("Mchirp",&Mc);
1379  NNTree2->SetBranchAddress("Average",&av);
1380  NNTree2->SetBranchAddress("cc",&cc);
1381  NNTree2->SetBranchAddress("rho",&rho);
1382  NNTree2->SetBranchAddress("Mchirp_injected",&Mc_inj);
1383  NNTree2->SetBranchAddress("#TestSig",&nSi);
1384  NNTree2->GetEntry(0);
1385  int const nSig=nSi;
1386  cout<<"nSig: "<<nSig<<" nSi: "<<nSi<<endl;
1387  int const ncurve2=nRHO*nCC;
1388 
1389  double* McSig[ncurve2];
1390  for (int i=0;i<ncurve2;i++) McSig[i]=new double[nSig];
1391  //double rhoSig[20][10];
1392  int NSig[ncurve2];
1393 // int nBg=0;
1394  int const nBg=NNTree2->GetEntries()-nSig;
1395  for (int i=0;i<ncurve2;i++) {
1396  NSig[i]=0;
1397  for (int j=0;j<nSig;j++) McSig[i][j]=0.;
1398  }
1399  double* McBg[ncurve2];
1400  for (int i=0;i<ncurve2;i++) McBg[i]=new double[nBg];
1401 
1402  //double rhoBg[20][10];
1403  int NBg[ncurve2];
1404  for (int i=0;i<ncurve2;i++) {
1405  NBg[i]=0;
1406  for (int j=0;j<nBg;j++) McBg[i][j]=0.;
1407  }
1408  double ccTh[nCC];
1409  for (int i=0;i<nCC;i++) ccTh[i]=0.;
1410  double rhoTh[nRHO];
1411  for (int i=0;i<nRHO;i++) rhoTh[i]=0.;
1412 // double deltacc=0.;
1413 // double deltarho=0.;
1414 // deltacc=0.2/nCC;
1415 // deltarho=1./(nRHO);
1416 
1417  for(int n=0;n<NNTree2->GetEntries();n++){
1418  NNTree2->GetEntry(n);
1419  cout<<"rho "<<rho<<" cc "<<cc<<" av "<<av<<endl;
1420  for(int i=0; i<nCC;i++){
1421  ccTh[i]=0.5+i*deltacc;
1422  ccTh[0]=0.;
1423  if(cc<ccTh[i]) continue;
1424  for(int j=0; j<nRHO;j++){
1425  rhoTh[j]=5+j*deltarho;
1426  rhoTh[0]=0.;
1427  if(rho<rhoTh[j]) continue;
1428  int ni=0;
1429  if(n>=nSig) {
1430  NBg[i*nRHO+j]= NBg[i*nRHO+j]+1;
1431  if(i*nRHO+j==0) cout<<" NBg[i*nRHO+j] "<<NBg[i*nRHO+j]<<endl;
1432  while(McBg[i*nRHO+j][ni]!=0)ni=ni+1;
1433  McBg[i*nRHO+j][ni]=Mc;
1434  // if (NBg[0]==FAth_n) thresFA=av;
1435  // cout<<" soglia_cc "<<ccTh[i]<<" soglia_RHO "<<rhoTh[j]<<endl;
1436  }
1437  else {
1438  NSig[i*nRHO+j]= NSig[i*nRHO+j]+1;
1439  if(i*nRHO+j==0) cout<<" NSig[i*nRHO+j] "<<NSig[i*nRHO+j]<<endl;
1440  while(McSig[i*nRHO+j][ni]!=0)ni=ni+1;
1441  McSig[i*nRHO+j][ni]=Mc;
1442  // cout<<" soglia_cc "<<ccTh[i]<<" soglia_RHO "<<rhoTh[j]<<endl;
1443  }
1444  }
1445  }
1446  }
1447 
1448  int* indexS[ncurve2];
1449  for (int i=0;i<ncurve2;i++) indexS[i]=new int[nSig];
1450 
1451  TGraph * gS[ncurve2];
1452  for (int y=0;y<ncurve2;y++) {
1453  int igS=0;
1454  int igS_p=0;
1455  gS[y]=new TGraph();
1456  TMath::Sort(nSig,McSig[y],indexS[y],false);
1457  for (int k=0;k<nSig;k++) {
1458  int ii=indexS[y][k];
1459  int yy=0;
1460  if (k>0){
1461  //cout<<"k "<<k<<endl;
1462  int ij=indexS[y][k-1];
1463  //if(ANNSig[y][ii]!=0&&ANNSig[y][ii]!=ANNSig[y][ij]) {
1464  if(McSig[y][ii]!=0) {
1465  yy=NSig[y]-igS;
1466  //gS[y]->SetPoint(igS,ANNSig[y][ii],yy);
1467  if(McSig[y][ii]!=McSig[y][ij]) gS[y]->SetPoint(igS_p++,McSig[y][ii],yy);
1468  // cout<<"igS"<<igS<<" x "<<ANNSig[y][ii]<<" y: "<<yy<<endl;
1469  igS=igS+1;
1470 
1471  }
1472  }
1473  else {
1474  if(McSig[y][ii]!=0){
1475  yy=NSig[y]-igS;
1476  gS[y]->SetPoint(0,McSig[y][ii],yy);
1477  igS=igS+1;
1478  // cout<<" x "<<ANNSig[y][ii]<<" y: "<<yy<<endl;
1479  }
1480 
1481  }
1482  }
1483  }
1484 
1485 int thres_ind=0;
1486  int* indexB[ncurve2];
1487  for (int i=0;i<ncurve2;i++) indexB[i]=new int[nBg];
1488 
1489  TGraph * gB[ncurve2];
1490  for (int y=0;y<ncurve2;y++) {
1491  int igB=0;
1492  int igB_p=0;
1493  gB[y]=new TGraph();
1494  TMath::Sort(nBg,McBg[y],indexB[y],false);
1495  // cout<<"dopo Sort "<<y<<endl;
1496  for (int k=0;k<nBg;k++) {
1497  int ii=indexB[y][k];
1498  int ik=indexB[y][k-1];
1499  int yy=0;
1500  if (k>0){
1501  int ij=indexB[y][k-1];
1502  //if(ANNBg[y][ii]!=0&&ANNBg[y][ii]!=ANNBg[y][ij]) {
1503  if(McBg[y][ii]!=0) {
1504  yy=NBg[y]-igB;
1505  if(y==0 && yy==McFAth_n+1) {McthresFA=McBg[y][ii];
1506  }
1507  if (y==0 && yy<McFAth_n+1) { cout<<"dentro primo if"<<endl;
1508  //if(McBg[y][ii]==McBg[y][ik]){McFAth_n=McFAth_n-1; cout<<"dentro secondo if"<<endl;}
1509  if(McBg[y][ii]>McBg[y][ik]){McFAth_n=McFAth_n;cout<<"maggiore ii "<<" diff"<<(McBg[y][ii]-McBg[y][ik])<<endl;}
1510  if(McBg[y][ii]<McBg[y][ik]){McFAth_n=McFAth_n;cout<<"minore ii"<<endl;}
1511  if(McBg[y][ii]==McBg[y][ik]){McFAth_n=McFAth_n-1;cout<<"uguale"<<endl;}
1512  //else { McFAth_n=McFAth_n-1;cout<<"dentro secondo if"<<endl;}
1513 }
1514  if(y==0) cout<<"ii "<<ii<<" yy "<<yy<<" y "<<y<<" McFAth_n "<<McFAth_n<<"McthresFA "<<McthresFA<<" McBg[y][ii] "<<McBg[y][ii]<<" McBg[y][indexB[y][k-1] "<<McBg[y][indexB[y][k-1]]<<endl;
1515  //gB[y]->SetPoint(igB,ANNBg[y][ii],yy);
1516  if(McBg[y][ii]!=McBg[y][ij]) gB[y]->SetPoint(igB_p++,McBg[y][ii],yy);
1517  igB=igB+1;
1518  // cout<<"igB"<<igB<<" x "<<ANNBg[y][ii]<<" y: "<<yy<<endl;
1519  }
1520  }
1521  else {
1522  if(McBg[y][ii]!=0) {
1523  yy=NBg[y]-igB;
1524  gB[y]->SetPoint(0,McBg[y][ii],yy);
1525  igB=igB+1;
1526  // cout<<"igB"<<igB<<" x "<<ANNBg[y][ii]<<" y: "<<yy<<endl;
1527  }
1528  }
1529  }
1530  }
1531 
1532 McFDth_n=0;
1533  for(int ni=1;ni<nSig;ni++){
1534  cout<<"McFAth_n "<<McFAth_n<<" McthresFA "<<McthresFA<<" McSig[0][indexS[0][ni]] "<<McSig[0][indexS[0][ni]]<<" McFDth_n "<<McFDth_n<<" NSig[0]-(ni-1 )"<<NSig[0]-(ni-1)<<" NSig[0] "<<NSig[0]<<endl;
1535 // if (ANNSig[0][indexS[0][ni]]==thresFA || (ANNSig[0][indexS[0][ni-1]]<thresFA && ANNSig[0][indexS[0][ni]]>thresFA)) FDth_n=NSig[0]-(ni-1);
1536  if (McSig[0][indexS[0][ni]]<=McthresFA) McFDth_n=McFDth_n+1;
1537  }
1538 
1539 
1540  TCanvas* cS=new TCanvas("Efficiency_vs_Mchirp","Efficiency_vs_Mchirp",0,0,1200,700);
1541  cS->Divide(2,2);
1542  //cS->cd(1)->SetLogy();
1543  cS->cd(1);
1544  TMultiGraph* mg1=new TMultiGraph();
1545  mg1->SetTitle("cc: no_cut;Mchirp;#Events");
1546  for (int h=0;h<nRHO;h++){
1547  gS[h]->SetLineColor(4);
1548  if(gS[h]->GetN()!=0) mg1->Add(gS[h]);
1549  }
1550  mg1->Draw("al");
1551  cS->cd(2);
1552  // cS->cd(2)->SetLogy();
1553  TMultiGraph* mg2=new TMultiGraph();
1554  mg2->SetTitle("cc=0.55;Mchirp;#Events");
1555  for (int h=0;h<nRHO;h++){
1556  gS[nRHO+h]->SetLineColor(4);
1557  if(gS[nRHO+h]->GetN()!=0) mg2->Add(gS[nRHO+h]);
1558  }
1559  mg2->Draw("al");
1560 
1561  //cS->cd(3)->SetLogy();
1562  cS->cd(3);
1563  TMultiGraph* mg3=new TMultiGraph();
1564  mg3->SetTitle("cc=0.6;Mchirp;#Events");
1565  for (int h=0;h<nRHO;h++){
1566  gS[2*nRHO+h]->SetLineColor(4);
1567  if(gS[2*nRHO+h]->GetN()!=0) mg3->Add(gS[2*nRHO+h]);
1568  }
1569  mg3->Draw("al");
1570  //cS->cd(4)->SetLogy();
1571  cS->cd(4);
1572  TMultiGraph* mg4=new TMultiGraph();
1573  mg4->SetTitle("cc=0.65;Mchirp;#Events");
1574  for (int h=0;h<nRHO;h++){
1575  gS[3*nRHO+h]->SetLineColor(4);
1576  if(gS[3*nRHO+h]->GetN()!=0) mg4->Add(gS[3*nRHO+h]);
1577  }
1578  mg4->Draw("al");
1579 
1580  TCanvas* cB=new TCanvas("Number_vs_Mchirp","Number_vs_Mchirp",0,0,1200,700);
1581  cB->Divide(2,2);
1582  cB->cd(1)->SetLogy();
1583  TMultiGraph* mg1B=new TMultiGraph();
1584  mg1B->SetTitle("cc: no_cut;Mchirp;#Events");
1585  for (int h=0;h<nRHO;h++){
1586  gB[h]->SetLineColor(4);
1587  if(gB[h]->GetN()!=0) mg1B->Add(gB[h]);
1588  }
1589  mg1B->Draw("al");
1590  cB->cd(2)->SetLogy();
1591  TMultiGraph* mg2B=new TMultiGraph();
1592  mg2B->SetTitle("cc=0.55;Mchirp;#Events");
1593  for (int h=0;h<nRHO;h++){
1594  gB[nRHO+h]->SetLineColor(4);
1595  if(gB[nRHO+h]->GetN()!=0) mg2B->Add(gB[nRHO+h]);
1596  }
1597  mg2B->Draw("al");
1598  cB->cd(3)->SetLogy();
1599  TMultiGraph* mg3B=new TMultiGraph();
1600  mg3B->SetTitle("cc=0.6;Mchirp;#Events");
1601  for (int h=0;h<nRHO;h++){
1602  gB[2*nRHO+h]->SetLineColor(4);
1603  if(gB[2*nRHO+h]->GetN()!=0) mg3B->Add(gB[2*nRHO+h]);
1604  }
1605  mg3B->Draw("al");
1606  cB->cd(4)->SetLogy();
1607  TMultiGraph* mg4B=new TMultiGraph();
1608  mg4B->SetTitle("cc=0.6;Mchirp;#Events");
1609  for (int h=0;h<nRHO;h++){
1610  gB[3*nRHO+h]->SetLineColor(4);
1611  if(gB[3*nRHO+h]->GetN()!=0) mg4B->Add(gB[3*nRHO+h]);
1612  }
1613  mg4B->Draw("al");
1614 
1615 
1616  TString CnameS(name);
1617  TString CnameB(name);
1618  TString CnameSroot(name);
1619  TString CnameBroot(name);
1620  char CnameS2[1024];
1621  char CnameB2[1024];
1622  char CnameS2root[1024];
1623  char CnameB2root[1024];
1624  CnameS.ReplaceAll(".root",".png");
1625  CnameB.ReplaceAll(".root",".png");
1626  sprintf(CnameS2,"Mcthres/N_Mc_S_%s",CnameS.Data());
1627  sprintf(CnameB2,"Mcthres/N_Mc_B_%s",CnameB.Data());
1628  sprintf(CnameS2root,"Mcthres/N_Mc_S_%s",CnameSroot.Data());
1629  sprintf(CnameB2root,"Mcthres/N_Mc_B_%s",CnameBroot.Data());
1630  cS->SaveAs(CnameS2);
1631  cB->SaveAs(CnameB2);
1632  cS->Print(CnameS2root);
1633  cB->Print(CnameB2root);
1634 
1635 
1636 }
1637 
1639  TString name(ifile);
1640  name.ReplaceAll("outfile/","");
1641  name.ReplaceAll("average_file/","");
1642  TFile* fTEST =TFile::Open(ifile.Data());
1643  TTree* NNTree2=(TTree*)fTEST->Get("Parameters");
1644  //double outbis;
1645  double av;
1646  double out;
1647  double cc;
1648  double rho;
1649  int nSi;
1650 // NNTree2->SetBranchAddress("ANNout",&out);
1651  NNTree2->SetBranchAddress("Average",&av);
1652  NNTree2->SetBranchAddress("cc",&cc);
1653  NNTree2->SetBranchAddress("rho",&rho);
1654  NNTree2->SetBranchAddress("#TestSig",&nSi);
1655  NNTree2->GetEntry(0);
1656  int const nSig=nSi;
1657  cout<<"nSig: "<<nSig<<" nSi: "<<nSi<<endl;
1658  int const nBg=NNTree2->GetEntries()-nSig;
1659  cout<<"nBg: "<<nBg<<" Entries: "<<NNTree2->GetEntries()<<endl;
1660 
1661  TGraph * gS[3];
1662  TGraph * gB[3];
1663  gB[0]=new TGraph();
1664  gS[0]=new TGraph();
1665 // cout<<"nSig: "<<nSig<<endl;
1666  for (int n = 0; n <NNTree2->GetEntries(); n++){
1667  NNTree2->GetEntry(n);
1668  if(n<nSig) {
1669  gS[0]->SetPoint(n,cc,av);
1670  cout<<"Sig_graph1: x="<<cc<<" y: "<<av<<endl;
1671  }
1672  else {
1673  gB[0]->SetPoint(n-nSig,cc,av);
1674  cout<<"Bg_graph1: x="<<cc<<" y: "<<av<<endl;
1675  }
1676  }
1677 
1678 
1679  /*for (int a=0;a<nBg;a++){
1680  if(aRHOB[a]>=RHOth){
1681  for (int b=0;b<nth;b++){
1682  if(aCCB[a]>=cc1th[b]){
1683  for(int c=0;c<nth+1;c++){
1684  if(aANNB[a]>=ANN1th[c]) Z[b*nth+c]=Z[b*nth+c]+1;
1685  if(c=10) cout<<" ANN "<<ANN1th[c]<<" Zcount: "<<Z[b*nth+c]<<" aNNB "<<aANNB[a]<<" a "<<a<<" b "<<b<<endl;
1686  }
1687  }
1688  }
1689  }
1690  }*/
1691  //gS[0]->GetHistogram()->GetXaxis()->SetTitle("Mchirp");
1692  //gB[0]->GetHistogram()->GetYaxis()->SetTitle("Average on ANN ouputs");
1693  //gS[0]->GetHistogram()->GetXaxis()->SetTitle("Mchirp");
1694  //gB[0]->GetHistogram()->GetYaxis()->SetTitle("Average on ANN ouputs");
1695  gS[0]->SetMarkerColor(2);
1696  gB[0]->SetMarkerColor(4);
1697  gS[0]->SetMarkerStyle(6);
1698  gB[0]->SetMarkerStyle(7);
1699 
1700  TCanvas* c=new TCanvas("Plots","Plots",0,0,1200,700);
1701  c->Divide(1,2);
1702  c->cd(1);
1703  TMultiGraph* mg1=new TMultiGraph();
1704  mg1->SetTitle("Av_cc");
1705  if(gB[0]->GetN()!=0) mg1->Add(gB[0]);
1706  if(gS[0]->GetN()!=0) mg1->Add(gS[0]);
1707  mg1->Draw("ap");
1708  mg1->GetHistogram()->GetXaxis()->SetTitle("cc");
1709  mg1->GetHistogram()->GetYaxis()->SetTitle("Average on ANN ouputs");
1710 // mg1->Draw("ap");
1711  c->cd(2);
1712  TMultiGraph* mg2=new TMultiGraph();
1713  mg2->SetTitle("Av_cc");
1714  if(gS[0]->GetN()!=0) mg2->Add(gS[0]);
1715  if(gB[0]->GetN()!=0) mg2->Add(gB[0]);
1716  mg2->Draw("ap");
1717  mg2->GetHistogram()->GetXaxis()->SetTitle("cc");
1718  mg2->GetHistogram()->GetYaxis()->SetTitle("Average on ANN ouputs");
1719 
1720  cout<<" name "<<name<<endl;
1721  TString Cname(name);
1722  TString Cnameroot(name);
1723  char Cname2[1024];
1724  char Cname2root[1024];
1725  Cname.ReplaceAll(".root",".png");
1726  sprintf(Cname2,"average_png/out_Plots_%s",Cname.Data());
1727  sprintf(Cname2root,"average_png/out_Plots_%s",Cnameroot.Data());
1728  c->SaveAs(Cname2);
1729  c->Print(Cname2root);
1730 /*
1731  TCanvas* c2=new TCanvas("Plots_Bkg_on_Sig","Plots_Bkg_on_Sig",0,0,1200,700);
1732  c2->Divide(2,2);
1733  c2->cd(1);
1734  gS[0]->Draw("ap");
1735  gB[0]->Draw("p,same");
1736 
1737  c2->cd(2);
1738  gS[1]->Draw("ap");
1739  gB[1]->Draw("p,same");
1740  c2->cd(3);
1741  gS[2]->Draw("ap");
1742  gB[2]->Draw("p,same");
1743  c2->cd(4);
1744  TText* text2=new TText(0.37,0.0,"no cuts on ANN");
1745  hglitch->SetStats(0);
1746  hglitch->GetXaxis()->SetTitle("cc");
1747  hglitch->GetYaxis()->SetTitle("ANNoutput");
1748  hglitch->GetZaxis()->SetTitle("count");
1749  hglitch->Draw("colz");
1750  text2->Draw();
1751  gPad->SetLogz();
1752 
1753  TString Cname_2(name);
1754  TString Cname_2root(name);
1755  char Cname2_2[1024];
1756  char Cname2_2root[1024];
1757  Cname_2.ReplaceAll(".root",".png");
1758  sprintf(Cname2_2,"CC_RHO_ANN_Plots/CC_RHO_ANN_Plots_BoS_%s",Cname_2.Data());
1759  sprintf(Cname2_2root,"CC_RHO_ANN_Plots/CC_RHO_ANN_Plots_BoS_%s",Cname_2root.Data());
1760  c2->SaveAs(Cname2_2);
1761  c2->Print(Cname2_2root);
1762 
1763 //CARTELLA CCi_RHO_ANN_Plots */
1764 }
1766  TString name(ifile);
1767  name.ReplaceAll("outfile/","");
1768  name.ReplaceAll("average_file/","");
1769  TFile* fTEST =TFile::Open(ifile.Data());
1770  TTree* NNTree2=(TTree*)fTEST->Get("Parameters");
1771  //double outbis;
1772  double av;
1773  double out;
1774  double cc;
1775  double Mc;
1776  double Mc_inj;
1777  double rho;
1778  int nSi;
1779 // NNTree2->SetBranchAddress("ANNout",&out);
1780  NNTree2->SetBranchAddress("Mchirp",&Mc);
1781  NNTree2->SetBranchAddress("Average",&av);
1782  NNTree2->SetBranchAddress("cc",&cc);
1783  NNTree2->SetBranchAddress("rho",&rho);
1784  NNTree2->SetBranchAddress("Mchirp_injected",&Mc_inj);
1785  NNTree2->SetBranchAddress("#TestSig",&nSi);
1786  NNTree2->GetEntry(0);
1787  int const nSig=nSi;
1788  cout<<"nSig: "<<nSig<<" nSi: "<<nSi<<endl;
1789  //exit(0);
1790  int const nBg=NNTree2->GetEntries()-nSig;
1791 
1792  TGraph * gS[3];
1793  TGraph * gB[3];
1794  gB[0]=new TGraph();
1795  gS[0]=new TGraph();
1796 
1797 
1798  TCanvas* ANN_Mc_c3D=new TCanvas("ANN_average_vs_Mc_reconstructed","ANN_average_vs_Mc_reconstructed",0,0,1200,700);
1799  ANN_Mc_c3D->Divide(1,2);
1800  TH2D* ANN_Mc_SIG3D=new TH2D("ANN_average_vs_Mc_recostructed_sig","ANN_average_vs_Mc_recostructed_sig",NMc_r,minMc_r,maxMc_r,NANN_av,minANN_av,maxANN_av);
1801  TH2D* ANN_Mc_BKG3D=new TH2D("ANN_average_vs_Mc_recostructed_bkg","ANN_average_vs_Mc_recostructed_bkg",NMc_r,minMc_r,maxMc_r,NANN_av,minANN_av,maxANN_av);
1802  // Mc_inj_g->SetPoint(0,rhoSig[y][ii],yy);
1803  ANN_Mc_BKG3D->SetDirectory(0);
1804  ANN_Mc_BKG3D->SetStats(0);
1805  ANN_Mc_SIG3D->SetDirectory(0);
1806  ANN_Mc_SIG3D->SetStats(0);
1807 
1808  TCanvas* ANN_cc_c3D=new TCanvas("ANN_average_vs_cc","ANN_average_vs_cc",0,0,1200,700);
1809  ANN_cc_c3D->Divide(1,2);
1810  TH2D* ANN_cc_SIG3D=new TH2D("ANN_average_vs_cc_SIG","ANN_average_vs_cc_SIG",Ncc3D, minCC, maxCC,NANN_av,minANN_av,maxANN_av);
1811  TH2D* ANN_cc_BKG3D=new TH2D("ANN_average_vs_cc_BKG","ANN_average_vs_cc_BKG",Ncc3D, minCC, maxCC,NANN_av,minANN_av,maxANN_av);
1812  ANN_cc_SIG3D->SetDirectory(0);
1813  ANN_cc_SIG3D->SetStats(0);
1814  ANN_cc_BKG3D->SetDirectory(0);
1815  ANN_cc_BKG3D->SetStats(0);
1816  double deltacc3D=0;
1817  deltacc3D=(maxCC-minCC)/(Ncc3D);
1818 
1819 
1820  TCanvas* Mc_inj_c=new TCanvas("ANN_vs_Mc_injected","ANN_vs_Mc_injected",0,0,1200,700);
1821  Mc_inj_c->Divide(1,2);
1822  TH2D* Mc_inj_gMc=new TH2D("Mc_injected_Mc_recostructed","Mc_injected_recostructed",NMc_inj,minMc_inj,maxMc_inj,NMc_r,minMc_r,maxMc_r);
1823  TH2D* Mc_inj_g=new TH2D("Mc_injected_comparison","Mc_injected_comparison",NMc_inj,minMc_inj,maxMc_inj,NANN_av,minANN_av,maxANN_av);
1824  // Mc_inj_g->SetPoint(0,rhoSig[y][ii],yy);
1825  Mc_inj_gMc->SetDirectory(0);
1826  Mc_inj_gMc->SetStats(0);
1827  Mc_inj_g->SetDirectory(0);
1828  Mc_inj_g->SetStats(0);
1829  double deltaMc_inj=0;
1830  deltaMc_inj=(maxMc_inj-minMc_inj)/(NMc_inj);
1831  double deltaANN_av_M=0;
1832  deltaANN_av_M=(maxANN_av-minANN_av)/(NANN_av);
1833  double deltaMc_r=0;
1834  deltaMc_r=(maxMc_r-minMc_r)/(NMc_r);
1835 
1836 
1837  TString txt_name0(name);
1838  txt_name0.ReplaceAll(".root",".txt");
1839  char txt_name[1024];
1840  sprintf(txt_name,"txt_files/%s",txt_name0.Data());
1841  ofstream file_txt_out(txt_name);
1842 
1843 
1844 // cout<<"nSig: "<<nSig<<endl;
1845  int aa1=0;
1846  int bb1=0;
1847  for (int n = 0; n <NNTree2->GetEntries(); n++){
1848  NNTree2->GetEntry(n);
1849  char line[2048];
1850  if(n<nSig&&n!=nSig) {
1851  Mc_inj_gMc->Fill(Mc_inj,Mc);
1852  Mc_inj_g->Fill(Mc_inj,av);
1853  gS[0]->SetPoint(n,Mc,av);
1854  cout<<" n "<<n<<" Mc "<<Mc<<" av "<<av<<" M_inj "<<Mc_inj<<endl;
1855  cout<<"Sig_graph1: x="<<cc<<" y: "<<av<<endl;
1856  aa1=aa1+1;
1857  ANN_Mc_SIG3D->Fill(Mc,av);
1858  ANN_cc_SIG3D->Fill(cc,av);
1859  sprintf(line,"SIG:Average_%1.5f; Mc_rec_%1.5f; Mc_inj_%1.5f; event %i; count_%i; nSig_%i\n",av,Mc,Mc_inj,n,aa1,nSig);
1860  file_txt_out<<line;
1861  cout<<" n "<<n<<" nSig "<<nSig<<endl;
1862  }
1863  else {
1864  bb1=bb1+1;
1865  gB[0]->SetPoint(n-nSig,Mc,av);
1866  ANN_Mc_BKG3D->Fill(Mc,av);
1867  ANN_cc_BKG3D->Fill(cc,av);
1868  // cout<<"Bg_graph1: x="<<cc<<" y: "<<av<<endl;
1869  sprintf(line,"BKG:Average %1.5f; Mc_rec %1.5f; Mc_inj_%1.5f; event %i; count_%i\n",av,Mc,Mc_inj,n,bb1);
1870  file_txt_out<<line;
1871 
1872  }
1873  }
1874  //exit(0);
1875 
1876 
1877  gS[0]->SetMarkerColor(2);
1878  gB[0]->SetMarkerColor(4);
1879  gS[0]->SetMarkerStyle(6);
1880  gB[0]->SetMarkerStyle(7);
1881 
1882  ANN_Mc_c3D->cd(1);
1883  ANN_Mc_SIG3D->GetXaxis()->SetTitle("SIG:Mchirp_reconstructed");
1884  ANN_Mc_SIG3D->GetYaxis()->SetTitle("SIG:ANN_average");
1885  ANN_Mc_SIG3D->GetZaxis()->SetTitle("count");
1886  ANN_Mc_SIG3D->Draw("colz");
1887  ANN_Mc_c3D->cd(2);
1888  ANN_Mc_BKG3D->GetXaxis()->SetTitle("BKG:Mchirp_reconstructed");
1889  ANN_Mc_BKG3D->GetYaxis()->SetTitle("BKG:ANN_average");
1890  ANN_Mc_BKG3D->GetZaxis()->SetTitle("count");
1891  ANN_Mc_BKG3D->Draw("colz");
1892 
1893 
1894  TString ANN_Mc_name3D(name);
1895  TString ANN_Mc_rootname3D(name);
1896  char ANN_Mc_cname23D[1024];
1897  char ANN_Mc_crootname23D[1024];
1898  ANN_Mc_name3D.ReplaceAll(".root",".png");
1899  sprintf(ANN_Mc_cname23D,"average_png/nSig%i_nBg%i_3DMc_ANNav_Plots_%s",nSig,nBg,ANN_Mc_name3D.Data());
1900  sprintf(ANN_Mc_crootname23D,"average_png/nSig%i_nBg%i_3DMc_ANNav_Plots_%s",nSig,nBg,ANN_Mc_rootname3D.Data());
1901  ANN_Mc_c3D->SaveAs(ANN_Mc_cname23D);
1902  ANN_Mc_c3D->Print(ANN_Mc_crootname23D);
1903 
1904 
1905  ANN_cc_c3D->cd(1);
1906  ANN_cc_SIG3D->GetXaxis()->SetTitle("SIG:cc");
1907  ANN_cc_SIG3D->GetYaxis()->SetTitle("SIG:ANN_average");
1908  ANN_cc_SIG3D->GetZaxis()->SetTitle("count");
1909  ANN_cc_SIG3D->Draw("colz");
1910  ANN_cc_c3D->cd(2);
1911  ANN_cc_BKG3D->GetXaxis()->SetTitle("BKG:cc");
1912  ANN_cc_BKG3D->GetYaxis()->SetTitle("BKG:ANN_average");
1913  ANN_cc_BKG3D->GetZaxis()->SetTitle("count");
1914  ANN_cc_BKG3D->Draw("colz");
1915 
1916 
1917  TString ANN_cc_cname3D(name);
1918  TString ANN_cc_crootname3D(name);
1919  char ANN_cc_cname23D[1024];
1920  char ANN_cc_crootname23D[1024];
1921  ANN_cc_cname3D.ReplaceAll(".root",".png");
1922  sprintf(ANN_cc_cname23D,"average_png/nSig%i_nBg%i_3DANNav_cc_Plots_%s",nSig,nBg,ANN_cc_cname3D.Data());
1923  sprintf(ANN_cc_crootname23D,"average_png/nSig%i_nBg%i_3DANNav_cc_Plots_%s",nSig,nBg,ANN_cc_crootname3D.Data());
1924  ANN_cc_c3D->SaveAs(ANN_cc_cname23D);
1925  ANN_cc_c3D->Print(ANN_cc_crootname23D);
1926 
1927  Mc_inj_c->cd(1);
1928  Mc_inj_g->GetXaxis()->SetTitle("Mchirp_injected");
1929  Mc_inj_g->GetYaxis()->SetTitle("ANN_average");
1930  Mc_inj_g->GetZaxis()->SetTitle("count");
1931  Mc_inj_g->Draw("colz");
1932  Mc_inj_c->cd(2);
1933  Mc_inj_gMc->GetXaxis()->SetTitle("Mchirp_injected");
1934  Mc_inj_gMc->GetYaxis()->SetTitle("Mchirp_reconstructed");
1935  Mc_inj_gMc->GetZaxis()->SetTitle("count");
1936  Mc_inj_gMc->Draw("colz");
1937 
1938 
1939  TString Mc_inj_cname(name);
1940  TString Mc_inj_crootname(name);
1941  char Mc_inj_cname2[1024];
1942  char Mc_inj_crootname2[1024];
1943  Mc_inj_cname.ReplaceAll(".root",".png");
1944  sprintf(Mc_inj_cname2,"average_png/nSig%i_nBg%i_Mc_inj_Plots_%s",nSig,nBg,Mc_inj_cname.Data());
1945  sprintf(Mc_inj_crootname2,"average_png/nSig%i_nBg%i_Mc_inj_Plots_%s",nSig,nBg,Mc_inj_crootname.Data());
1946  Mc_inj_c->SaveAs(Mc_inj_cname2);
1947  Mc_inj_c->Print(Mc_inj_crootname2);
1948 
1949 
1950  TCanvas* c=new TCanvas("Plots","Plots",0,0,1200,700);
1951  c->Divide(1,2);
1952  c->cd(1);
1953  TMultiGraph* mg1=new TMultiGraph();
1954  mg1->SetTitle("Av_Mc");
1955  if(gB[0]->GetN()!=0) mg1->Add(gB[0]);
1956  if(gS[0]->GetN()!=0) mg1->Add(gS[0]);
1957  mg1->Draw("ap");
1958  mg1->GetHistogram()->GetXaxis()->SetTitle("Mchirp");
1959  mg1->GetHistogram()->GetYaxis()->SetTitle("Average on ANN ouputs");
1960  c->cd(2);
1961  TMultiGraph* mg2=new TMultiGraph();
1962  mg2->SetTitle("Av_Mc");
1963  if(gS[0]->GetN()!=0) mg2->Add(gS[0]);
1964  if(gB[0]->GetN()!=0) mg2->Add(gB[0]);
1965  mg2->Draw("ap");
1966  mg2->GetHistogram()->GetXaxis()->SetTitle("Mchirp");
1967  mg2->GetHistogram()->GetYaxis()->SetTitle("Average on ANN ouputs");
1968 
1969  cout<<" name "<<name<<endl;
1970  TString Cname(name);
1971  TString Cnameroot(name);
1972  char Cname2[1024];
1973  char Cname2root[1024];
1974  Cname.ReplaceAll(".root",".png");
1975  sprintf(Cname2,"average_png/nSig%i_nBg%i_Mc_Plots_%s",nSig,nBg,Cname.Data());
1976  sprintf(Cname2root,"average_png/nSig%i_nBg%i_Mc_Plots_%s",nSig,nBg,Cnameroot.Data());
1977  cout<<"Cname2 "<<Cname2<<endl;
1978  c->SaveAs(Cname2);
1979  c->Print(Cname2root);
1980  cout<<" gS[0]->GetN() "<<gS[0]->GetN()<<endl;
1981  //cout<<"entries "<<mg1->GetHistogram()->GetEntries()<<endl;
1982  cout<<" nSig "<<nSig<<" nBg "<<nBg<<" aa1 "<<aa1<<endl;
1983 }
1984 
1985 
1987 
1988  TString name(ifile);
1989  name.ReplaceAll("outfile/","");
1990  name.ReplaceAll("average_file/","");
1991  TFile* fTEST =TFile::Open(ifile.Data());
1992  TTree* NNTree2=(TTree*)fTEST->Get("Parameters");
1993  double av;
1994  double out;
1995  double cc;
1996  double Mc;
1997  double Mc_inj;
1998  double rho;
1999  int nSi;
2000  NNTree2->SetBranchAddress("Mchirp",&Mc);
2001  NNTree2->SetBranchAddress("Average",&av);
2002  NNTree2->SetBranchAddress("cc",&cc);
2003  NNTree2->SetBranchAddress("rho",&rho);
2004  NNTree2->SetBranchAddress("Mchirp_injected",&Mc_inj);
2005  NNTree2->SetBranchAddress("#TestSig",&nSi);
2006  NNTree2->GetEntry(0);
2007  int const nSig=nSi;
2008  cout<<"nSig: "<<nSig<<" nSi: "<<nSi<<endl;
2009  int const nBg=NNTree2->GetEntries()-nSig;
2010  int const nTot=NNTree2->GetEntries();
2011  int const n_points=N_ANNth*N_RHOth;
2012  int nBgSur[3][n_points];
2013  int nSigSur[3][n_points];
2014  double zax[3][n_points];
2015 
2016  for (int i=0; i<3; i++) for (int u=0; u<n_points; u++) {nBgSur[i][u]=0; nSigSur[i][u]=0;zax[i][u]=0.;}
2017 
2018  double pANNth[N_ANNth];
2019  double pRHOth[N_RHOth];
2020 
2021  double pCCth[3];
2022 
2023  pCCth[0]=0.;
2024  pCCth[1]=0.;
2025  pCCth[2]=0.;
2026 
2027  pCCth[0]=0.5;
2028  pCCth[1]=0.6;
2029  pCCth[2]=0.7;
2030 
2031  double deltaANNth=0.;
2032  double deltaRHOth=0.;
2033 
2034  deltaANNth=(double)(ANNth_max-ANNth_min)/N_ANNth;
2035  deltaRHOth=(double)(RHOth_max-RHOth_min)/N_RHOth;
2036 
2037 
2038  TCanvas* SNR_c=new TCanvas("SIG-BKG_reduction","SIG-BKG_reduction",0,0,1200,700);
2039  SNR_c->Divide(1,3);
2040 
2041  TH2D* SNR3Dcc0=new TH2D("cc=0.5","cc=0.5",N_ANNth,ANNth_min,ANNth_max,N_RHOth,RHOth_min,RHOth_max);
2042  TH2D* SNR3Dcc1=new TH2D("cc=0.6","cc=0.6",N_ANNth,ANNth_min,ANNth_max,N_RHOth,RHOth_min,RHOth_max);
2043  TH2D* SNR3Dcc2=new TH2D("cc=0.7","cc=0.7",N_ANNth,ANNth_min,ANNth_max,N_RHOth,RHOth_min,RHOth_max);
2044  SNR3Dcc0->SetDirectory(0);
2045  SNR3Dcc0->SetStats(0);
2046  SNR3Dcc1->SetDirectory(0);
2047  SNR3Dcc1->SetStats(0);
2048  SNR3Dcc2->SetDirectory(0);
2049  SNR3Dcc2->SetStats(0);
2050 
2051  for (int u=0; u<N_ANNth; u++) { pANNth[u]=0.; pANNth[u]=ANNth_min+u*deltaANNth; cout<<" pANNth[u] "<<pANNth[u]<<" u "<<u<<endl;}
2052  for (int u=0; u<N_RHOth; u++) { pRHOth[u]=0.; pRHOth[u]=RHOth_min+u*deltaRHOth; cout<<" pRHOth[u] "<<pRHOth[u]<<" u "<<u<<endl;}
2053 
2054 cout<<"fino def cose"<<endl;
2055 /*
2056  for (int u=0; u<nTot; u++){
2057  NNTree2->GetEntry(u);
2058  cout<<" cc "<<cc<<" av "<<av<<" rho "<< rho<<endl;
2059  for (int y=2; y>-1; y--){
2060  // cout<<" cc "<<cc<<" pCCth "<<pCCth[y]<<" y "<<y<<endl;
2061  if(cc>pCCth[y]) {
2062  for (int i=N_ANNth-1; i>-1; i--){
2063  // cout<<" av "<<av<<" pANNth[i] "<<pANNth[i]<<" i "<<i<<endl;
2064  if(av>pANNth[i]){
2065  for (int j=N_RHOth-1; j>-1; j++){
2066  //cout<<" rho "<<rho<<" pRHOth[j] "<<pRHOth[j]<<" j "<<j<<" nSig "<<nSig<<" u "<<u<<endl;
2067  if(rho>pRHOth[j]){
2068  for(int yy=y; yy>-1;yy--) for (int ii=i; ii>-1; ii--) for (int jj=j; jj>-1;jj--){
2069  if(u<nSig) nSigSur[yy][jj+N_ANNth*ii]=nSigSur[yy][jj+N_ANNth*ii]+1;
2070  else nBgSur[yy][jj+N_ANNth*ii]=nBgSur[yy][jj+N_ANNth*ii]+1;
2071  // cout<<" yy "<<yy<<" ii "<<ii<<" jj "<<jj<<" nBgSur[yy][jj+N_ANNth*ii] "<<nBgSur[yy][jj+N_ANNth*ii]<<" nSigSur[yy][jj+N_ANNth*ii] "<<nSigSur[yy][jj+N_ANNth*ii]<<endl;
2072  }
2073  break;
2074  }
2075  else continue;
2076  }
2077  break;
2078  }
2079  else continue;
2080  }
2081  break;
2082  }
2083  else continue;
2084  }
2085  }
2086 //exit(0);
2087 cout<<"dopo ciclo"<<endl;
2088 */
2089 for (int u=0; u<nTot; u++){
2090  NNTree2->GetEntry(u);
2091  for (int y=0; y<3; y++){
2092  if(cc>pCCth[y]){
2093  for (int i=0; i<N_ANNth; i++){
2094  if(av>pANNth[i]) {
2095  for (int j=0; j<N_RHOth; j++){
2096  if(rho>pRHOth[j]){
2097  if(u<nSig) nSigSur[y][j+N_ANNth*i]=nSigSur[y][j+N_ANNth*i]+1;
2098  else nBgSur[y][j+N_ANNth*i]=nBgSur[y][j+N_ANNth*i]+1;
2099  }
2100  else continue;
2101  }
2102  }
2103  else continue;
2104  }
2105  }
2106  else continue;
2107  }
2108  }
2109 
2110 
2111 //exit(0);
2112 int nBgSur0=0;
2113 nBgSur0=nBgSur[0][0];
2114 int nSigSur0=0;
2115 nSigSur0=nSigSur[0][0];
2116 if(nBgSur0==0){nBgSur0=nBg;}
2117 if(nSigSur0==0){nSigSur0=nSig;}
2118 int count_zax=0;
2119 cout<<"dopo ciclo"<<" nSigSur0 "<<nSigSur0<<" nBgSur0 "<<nBgSur0<<endl;
2120 for (int u=0; u<3; u++) for (int j=0; j<N_RHOth; j++) for (int i=0; i<N_ANNth; i++) {
2121  //cout<<" u "<<u<<" j "<<j<<" i "<<i<<" nSigSur[u][i+j*N_ANNth] "<<nSigSur[u][i+j*N_ANNth]<<endl;
2122  //if(nSigSur[u][i+j*N_ANNth]!=0){zax[u][i+j*N_ANNth]=(nBgSur[u][i+j*N_ANNth]/nBgSur0)/(nSigSur[u][i+j*N_ANNth]/nSigSur0);};
2123  if(nSigSur[u][i+j*N_ANNth]==0) {
2124  if (nSigSur[u][i+j*N_ANNth]==0 && nBgSur[u][i+j*N_ANNth]==0) zax[u][i+j*N_ANNth]=-1;
2125  else zax[u][i+j*N_ANNth]=-2;
2126  }
2127  else {
2128  double num=0.;
2129  double den=0.;
2130  num=(double)nBgSur[u][i+j*N_ANNth]/nBgSur0;
2131  den=(double)nSigSur0/nSigSur[u][i+j*N_ANNth];
2132  zax[u][i+j*N_ANNth]=(double)num/den; count_zax=count_zax+1;
2133  cout<<(double)(nBgSur[u][i+j*N_ANNth]/nBgSur0)*(nSigSur0/nSigSur[u][i+j*N_ANNth])<<endl;
2134  }
2135 // cout<<" nBgSur[yy][jj+N_ANNth*ii] "<<nBgSur[u][j+N_ANNth*i]<<" nSigSur[y][j+N_ANNth*i] "<<nSigSur[u][j+N_ANNth*i]<<" zax[u][i+j*N_ANNth] "<<zax[u][i+j*N_ANNth]<<endl;
2136  //else zax[u][i+j*N_ANNth]=(nBgSur[u][i+j*N_ANNth]/nBgSur0)/(nSigSur[u][i+j*N_ANNth]/nSigSur0);
2137  if(u==2){
2138  /*cout<<" zax[0][i+j*N_ANNth] "<<zax[0][i+j*N_ANNth]<<endl;
2139  cout<<" zax[1][i+j*N_ANNth] "<<zax[1][i+j*N_ANNth]<<endl;
2140  cout<<" zax[2][i+j*N_ANNth] "<<zax[2][i+j*N_ANNth]<<endl;*/
2141  SNR3Dcc0->Fill(pANNth[i]+ deltaANNth/2.,pRHOth[j]+ deltaRHOth/2.,zax[0][i+j*N_ANNth]);
2142  SNR3Dcc1->Fill(pANNth[i]+ deltaANNth/2.,pRHOth[j]+ deltaRHOth/2.,zax[1][i+j*N_ANNth]);
2143  SNR3Dcc2->Fill(pANNth[i]+ deltaANNth/2.,pRHOth[j]+ deltaRHOth/2.,zax[2][i+j*N_ANNth]);
2144  }
2145 }
2146 for(int yy=0; yy<3;yy++) {
2147  cout<<" pCCth "<<pCCth[yy]<<endl;
2148  for (int ii=N_ANNth-1; ii>-1; ii--) {
2149  cout<<" pANNth[i] "<<pANNth[ii]<<endl;
2150  for (int jj=N_RHOth-1; jj>-1;jj--){
2151  cout<<" pRHOth[jj] "<<pRHOth[jj]<<endl;
2152  cout<<" nBgSur[yy][ii+N_ANNth*jj] "<<nBgSur[yy][ii+N_ANNth*jj]<<endl;
2153  cout<<" nSigSur[yy][ii+N_ANNth*jj] "<<nSigSur[yy][ii+N_ANNth*jj]<<endl;
2154  cout<<" zax[0][i+j*N_ANNth]"<<zax[yy][ii+jj*N_ANNth]<<endl;
2155  cout<<" (double)(nBgSur[u][i+j*N_ANNth]/nBgSur0)"<<(double)nBgSur[yy][ii+jj*N_ANNth]/nBgSur0<<endl;
2156  //cout<<" yy "<<yy<<" ii "<<ii<<" jj "<<jj<<" nBgSur[yy][jj+N_ANNth*ii] "<<nBgSur[yy][jj+N_ANNth*ii]<<" nSigSur[yy][jj+N_ANNth*ii] "<<nSigSur[yy][jj+N_ANNth*ii]<<endl;
2157  }
2158  }
2159  }
2160 cout<<"coutn_zax "<<count_zax<<endl;
2161  SNR_c->cd(1);
2162  SNR3Dcc0->GetXaxis()->SetTitle("ANN_average_thresholds");
2163  SNR3Dcc0->GetYaxis()->SetTitle("RHO_thresholds");
2164  SNR3Dcc0->GetZaxis()->SetTitle("normBKG/normSIG");
2165  SNR3Dcc0->Draw("colz");
2166 
2167  SNR_c->cd(2);
2168  SNR3Dcc1->GetXaxis()->SetTitle("ANN_average_thresholds");
2169  SNR3Dcc1->GetYaxis()->SetTitle("RHO_thresholds");
2170  SNR3Dcc1->GetZaxis()->SetTitle("normBKG/normSIG");
2171  SNR3Dcc1->Draw("colz");
2172 
2173  SNR_c->cd(3);
2174  SNR3Dcc2->GetXaxis()->SetTitle("ANN_average_thresholds");
2175  SNR3Dcc2->GetYaxis()->SetTitle("RHO_thresholds");
2176  SNR3Dcc2->GetZaxis()->SetTitle("normBKG/normSIG");
2177  SNR3Dcc2->Draw("colz");
2178 
2179  TString SNR_n(name);
2180  TString SNR_nroot(name);
2181  char SNR_c_n[1024];
2182  char SNR_c_nroot[1024];
2183  SNR_n.ReplaceAll(".root",".png");
2184  sprintf(SNR_c_n,"SNR_plots/nSig%i_nBg%i_invSNR_Plots_%s",nSig,nBg,SNR_n.Data());
2185  sprintf(SNR_c_nroot,"SNR_plots/nSig%i_nBg%i_invSNR_Plots_%s",nSig,nBg,SNR_nroot.Data());
2186  SNR_c->SaveAs(SNR_c_n);
2187  SNR_c->Print(SNR_c_nroot);
2188 
2189 
2190 }
2191 
2193 
2194  TString name(ifile);
2195  name.ReplaceAll("outfile/","");
2196  name.ReplaceAll("average_file/","");
2197  TFile* fTEST =TFile::Open(ifile.Data());
2198  TTree* NNTree2=(TTree*)fTEST->Get("Parameters");
2199  double av;
2200  double out;
2201  double cc;
2202  double Mc;
2203  double Mc_inj;
2204  double rho;
2205  int nSi;
2206  int CoG;
2207  NNTree2->SetBranchAddress("Center_of_Gravity",&CoG);
2208  NNTree2->SetBranchAddress("Mchirp",&Mc);
2209  NNTree2->SetBranchAddress("Average",&av);
2210  NNTree2->SetBranchAddress("cc",&cc);
2211  NNTree2->SetBranchAddress("rho",&rho);
2212  NNTree2->SetBranchAddress("Mchirp_injected",&Mc_inj);
2213  NNTree2->SetBranchAddress("#TestSig",&nSi);
2214  NNTree2->GetEntry(0);
2215  int const nSig=nSi;
2216  cout<<"nSig: "<<nSig<<" nSi: "<<nSi<<endl;
2217  int const nTot=NNTree2->GetEntries();
2218  int const nBg=nTot-nSig;
2219 
2220  TCanvas* CoG_c=new TCanvas("SIG-BKG_reduction","SIG-BKG_reduction",0,0,1200,700);
2221 
2222  TH1D* imp=new TH1D("Center_of_Gravity_VS_ANN","Center_of_Gravity_VS_ANN",N_ANNth,ANNth_min,ANNth_max);
2223  TH1D* CoG1Sig=new TH1D("Center_of_Gravity_VS_ANN","Center_of_Gravity_VS_ANN",N_ANNth,ANNth_min,ANNth_max);
2224  TH1D* CoG1Bg=new TH1D("Center_of_Gravity_VS_ANN","Center_of_Gravity_VS_ANN",N_ANNth,ANNth_min,ANNth_max);
2225  TH1D* CoG0Sig=new TH1D("Center_of_Gravity_VS_ANN","Center_of_Gravity_VS_ANN",N_ANNth,ANNth_min,ANNth_max);
2226  TH1D* CoG0Bg=new TH1D("Center_of_Gravity_VS_ANN","Center_of_Gravity_VS_ANN",N_ANNth,ANNth_min,ANNth_max);
2227 
2228  CoG1Sig->SetDirectory(0);
2229  CoG1Sig->SetStats(0);
2230  CoG0Sig->SetDirectory(0);
2231  CoG0Sig->SetStats(0);
2232  CoG1Bg->SetDirectory(0);
2233  CoG1Bg->SetStats(0);
2234  CoG0Bg->SetDirectory(0);
2235  CoG0Bg->SetStats(0);
2236 
2237  for (int n=0; n<nTot; n++){
2238  NNTree2->GetEntry(n);
2239  if (n<nSig) {
2240  if(CoG==0) {CoG0Sig->Fill(av);}
2241  else {CoG1Sig->Fill(av);}
2242  }
2243  else {
2244  if(CoG==0) {CoG0Bg->Fill(av);}
2245  else {CoG1Bg->Fill(av);}
2246  }
2247  cout<<" CoG "<<CoG<<endl;
2248  }
2249 
2250  imp->SetDirectory(0);
2251  imp->SetStats(0);
2252  int max_n=0;
2253  // if(nBg>nSig) max_n=nBg;
2254  // else max_n=nSig;
2255 
2256  int max[4];
2257  for(int i=0; i<4; i++) max[i]=0;
2258 
2259  max[0]=CoG1Sig->GetMaximum();
2260  max[1]=CoG0Sig->GetMaximum();
2261  max[2]=CoG1Bg->GetMaximum();
2262  max[3]=CoG0Bg->GetMaximum();
2263 
2264  max_n=max[0];
2265  for(int i=0; i<4; i++) if(max_n<max[i]) max_n=max[i] ;
2266 
2267  imp->Fill(ANNth_min,max_n);
2268  imp->SetLineColor(10);
2269  imp->SetBins(N_ANNth,ANNth_min,ANNth_max);
2270  imp->GetXaxis()->SetTitle("ANN");
2271  imp->GetYaxis()->SetTitle("Count");
2272  imp->Draw();
2273 
2274  CoG1Sig->SetFillStyle(3018);
2275  CoG0Sig->SetFillStyle(3018);
2276  CoG0Bg->SetFillStyle(3004);
2277  CoG1Bg->SetFillStyle(3004);
2278  CoG1Sig->SetFillColor(2);
2279  CoG0Sig->SetFillColor(6);
2280  CoG0Bg->SetFillColor(4);
2281  CoG1Bg->SetFillColor(9);
2282  CoG1Sig->SetLineColor(2);
2283  CoG0Sig->SetLineColor(6);
2284  CoG0Bg->SetLineColor(4);
2285  CoG1Bg->SetLineColor(9);
2286  // CoG1Sig->SetBins(N_ANNth,ANNth_min,ANNth_max);
2287  // CoG1Sig->GetXaxis()->SetTitle("ANN");
2288  // CoG1Sig->GetYaxis()->SetTitle("Count");
2289  CoG1Sig->Draw("same");
2290  CoG0Sig->Draw("same");
2291  CoG0Bg->Draw("same");
2292  CoG1Bg->Draw("same");
2293 
2294  TString CoG_n(name);
2295  TString CoG_nroot(name);
2296  char CoG_c_n[1024];
2297  char CoG_c_nroot[1024];
2298  CoG_n.ReplaceAll(".root",".png");
2299  sprintf(CoG_c_n,"CoG_plots/nSig%i_nBg%i_invSNR_Plots_%s",nSig,nBg,CoG_n.Data());
2300  sprintf(CoG_c_nroot,"CoG_plots/nSig%i_nBg%i_invSNR_Plots_%s",nSig,nBg,CoG_nroot.Data());
2301  CoG_c->SaveAs(CoG_c_n);
2302  CoG_c->Print(CoG_c_nroot);
2303 
2304 }
2305 
2307 
2308  TString name(ifile);
2309  name.ReplaceAll("outfile/","");
2310  name.ReplaceAll("average_file/","");
2311  TFile* fTEST =TFile::Open(ifile.Data());
2312  TTree* NNTree2=(TTree*)fTEST->Get("Parameters");
2313  double av;
2314  double out;
2315  double cc;
2316  double Mc;
2317  double Mc_inj;
2318  double rho;
2319  int nSi;
2320  int CoG;
2321  NNTree2->SetBranchAddress("Center_of_Gravity",&CoG);
2322  NNTree2->SetBranchAddress("Mchirp",&Mc);
2323  NNTree2->SetBranchAddress("Average",&av);
2324  NNTree2->SetBranchAddress("cc",&cc);
2325  NNTree2->SetBranchAddress("rho",&rho);
2326  NNTree2->SetBranchAddress("Mchirp_injected",&Mc_inj);
2327  NNTree2->SetBranchAddress("#TestSig",&nSi);
2328  NNTree2->GetEntry(0);
2329  int const nSig=nSi;
2330  cout<<"nSig: "<<nSig<<" nSi: "<<nSi<<endl;
2331  int const nTot=NNTree2->GetEntries();
2332  int const nBg=nTot-nSig;
2333 
2334 
2335 TGraph *Mc_g[nANNth];
2336 TGraph *ANNMc_g[nANNth];
2337 TGraph * rho_g[7];
2338 
2339 double Mccuts[7];
2340 double ANNcuts[7];
2341 double TA_rho[7][npoints];
2342 double FA_rho[7][npoints];
2343 double FA_Mc[nANNth+1][npoints];
2344 double FA_ANNMc[nANNth][npoints];
2345 double TA_Mc[nANNth+1][npoints];
2346 double TA_ANNMc[nANNth][npoints];
2347 double th_Mc[npoints];
2348 double th_k[npoints];
2349 double th_rho[npoints];
2350 
2351 for(int i=0; i<7; i++){
2352 Mccuts[i]=0.0;
2353 ANNcuts[i]=0.0;
2354 }
2355 ANNcuts[0]=-1.0;
2356 ANNcuts[1]=cutANN2;
2357 ANNcuts[2]=-1.0;
2358 Mccuts[2]=cutMc2;
2359 ANNcuts[3]=cutANN1;
2360 Mccuts[3]=cutMc1;
2361 Mccuts[4]=cutMc2;
2362 ANNcuts[4]=cutANN2;
2363 ANNcuts[5]=cutANN2;
2364 Mccuts[5]=cutMc1;
2365 Mccuts[6]=cutMc2;
2366 ANNcuts[6]=cutANN1;
2367 
2368 for(int i=0; i<7; i++){
2369  for (int j=0; j<npoints; j++){
2370  TA_rho[i][j]=0.;
2371  FA_rho[i][j]=0.;
2372  }
2373 }
2374 double delta_rho=0.;
2375 delta_rho=(double)(maxrho-minrho)/npoints;
2376 
2377 for(int i=0; i<npoints; i++){
2378 th_rho[i]=0.;
2379 th_rho[i]=minrho+i*delta_rho;
2380 }
2381 double yasym[nANNth];
2382 //Mc_g[0]=new TGraph();
2383 for (int i=0; i<nANNth; i++){
2384 // Mc_g[i+1]=new TGraph();
2385 // ANNMc_g[i]=new TGraph();
2386  for (int j=0; j<npoints; j++){
2387  FA_Mc[i+1][j]=0.;
2388  FA_ANNMc[i][j]=0.;
2389  TA_Mc[i+1][j]=0.;
2390  TA_ANNMc[i][j]=0.;
2391  }
2392 }
2393 
2394 //ANN thresholds
2395 double delta_yasym=0.;
2396 delta_yasym=(double)(max_yasym-min_yasym)/nANNth;
2397 
2398 cout<<" delta_yasym "<<delta_yasym<<endl;
2399 for (int i=0; i<nANNth; i++){
2400  yasym[i]=0.;
2401  yasym[i]=min_yasym+i*delta_yasym;
2402  cout<<" yasym[i] "<<yasym[i]<<" i "<<i<<endl;
2403 }
2404 
2405 
2406 //thresholds on the parameters necessary to built the TA-FA curves
2407 double delta_Mc=0.;
2408 //delta_Mc=(max_Mcth-min_Mcth)/nANNth;
2409 delta_Mc=(double)(max_Mcth-min_Mcth)/npoints;
2410 
2411 double delta_logk=0.;
2412 double min_logk=0.;
2413 double max_logk=0.;
2414 
2415 min_logk=log10(min_k);
2416 max_logk=log10(max_k);
2417 delta_logk=(log10(max_k)-log10(min_k))/npoints;
2418 
2419 for (int i=0; i<npoints; i++){
2420  FA_Mc[0][i]=0.;
2421  TA_Mc[0][i]=0.;
2422  th_Mc[i]=min_Mcth+i*delta_Mc;
2423  th_k[i]=pow(10,min_logk+i*delta_logk);
2424  cout<<" th_k[i] "<<th_k[i]<<" th_Mc[i] "<<th_Mc[i]<<endl;
2425  }
2426 
2427 //FA and TA definitions in function of rho
2428 int countS=0;
2429 int countB=0;
2430 for(int n=0; n<nTot; n++){
2431 NNTree2->GetEntry(n);
2432  if(cc<0.5) continue;
2433  if(n<nSig) countS=countS+1;
2434  else countB=countB+1;
2435  for(int i=0; i<7; i++){
2436  if(Mc>Mccuts[i]&&av>ANNcuts[i]) {
2437  for (int j=0; j<npoints;j++) {
2438  if(rho>th_rho[j]&&n<nSig) TA_rho[i][j]=TA_rho[i][j]+1;
2439  if(rho>th_rho[j]&&(n>nSig||n==nSig)) FA_rho[i][j]=FA_rho[i][j]+1;
2440  }
2441  }
2442  }
2443 }
2444 
2445 
2446 for(int i=0; i<7; i++){
2447  for (int j=0; j<npoints;j++) {
2448  if(i==6) cout<<"TA_rho[i][j] "<<TA_rho[i][j]<<" countS "<<countS<<" FA_rho[i][j] "<<FA_rho[i][j]<<" countB "<<countB<<endl;
2449  FA_rho[i][j]=(double)FA_rho[i][j]/countB;
2450  TA_rho[i][j]=(double)TA_rho[i][j]/countS;
2451  if(FA_rho[i][j]==0) FA_rho[i][j]=0.00001;
2452  if(i==6) cout<<"TA_rho[i][j] "<<TA_rho[i][j]<<" FA_rho[i][j] "<<FA_rho[i][j]<<endl;
2453  }
2454  }
2455 
2456 
2457 // FA and TA definitions
2458 for (int n=0; n<nTot; n++){
2459  NNTree2->GetEntry(n);
2460  if(n<nSig){
2461  for(int i=0; i<nANNth; i++){
2462  for(int j=0; j<npoints; j++){
2463  if(Mc>th_Mc[j]&&i==0)TA_Mc[0][j]=TA_Mc[0][j]+1;
2464  if(Mc>th_Mc[j]&&av>yasym[i]) TA_Mc[i+1][j]=TA_Mc[i+1][j]+1;
2465  if(av>((th_k[j]/(Mc-x_asym))+yasym[i]))TA_ANNMc[i][j]=TA_Mc[i+1][j]+1;
2466 // if(i==0&&j==0)cout<<" av "<<av<<" Mc "<<Mc<<" th_k[j] "<<th_k[j]<<" x_asym "<<x_asym<<" asym[i] "<<yasym[i]<<" ((th_k[j]/(Mc-x_asym))+yasym[i])) "<<(th_k[j]/(Mc-x_asym))+yasym[i]<<" TA_ANNMc[i][j] "<<TA_ANNMc[i][j]<<endl;
2467  }
2468  }
2469  }
2470 
2471  else {
2472  for(int i=0; i<nANNth; i++){
2473  for(int j=0; j<npoints; j++){
2474  if(Mc>th_Mc[j]&&i==0)FA_Mc[0][j]=FA_Mc[0][j]+1;
2475  if(Mc>th_Mc[j]&&av>yasym[i]) FA_Mc[i+1][j]=FA_Mc[i+1][j]+1;
2476  if(av>(th_k[j]/(Mc-x_asym)+yasym[i]))FA_ANNMc[i][j]=FA_Mc[i+1][j]+1;
2477  }
2478  }
2479  }
2480 
2481 }
2482 //exit(0);
2483 
2484 for(int j=0; j<npoints; j++){
2485  TA_Mc[0][j]=TA_Mc[0][j]/nSig;
2486  FA_Mc[0][j]=FA_Mc[0][j]/nBg;
2487  for(int i=0; i<nANNth; i++){
2488  TA_Mc[i+1][j]=(double)TA_Mc[i+1][j]/nSig;
2489  FA_Mc[i+1][j]=(double)FA_Mc[i+1][j]/nBg;
2490  TA_ANNMc[i][j]=(double)TA_ANNMc[i][j]/nSig;
2491  FA_ANNMc[i][j]=(double)FA_ANNMc[i][j]/nBg;
2492  }
2493 }
2494 
2495 Mc_g[0]=new TGraph(npoints,FA_Mc[0],TA_Mc[0]);
2496 TGraph* g0=new TGraph();
2497 g0->SetPoint(0.00001,1,0);
2498 for (int i=0; i<7; i++){
2499  rho_g[i]=new TGraph(npoints,FA_rho[i],TA_rho[i]);
2500  }
2501  TCanvas* canrho=new TCanvas("Efficiency_vs_FalseAlarm","Efficiency_vs_Alarm",0,0,1200,700);
2502  canrho->SetLogx();
2503  TLegend *legrho = new TLegend(.75, .2, .99, .4);
2504  TMultiGraph* mg0=new TMultiGraph();
2505  if(g0->GetN()!=0) mg0->Add(g0);
2506  g0->SetMarkerColor(0);
2507 
2508  mg0->SetTitle("Comparison;FalseAlarms;Efficiency");
2509  for (int i=0;i<7;i++){
2510  rho_g[i]->SetLineColor(2+i);
2511  rho_g[i]->SetMarkerColor(2+i);
2512  rho_g[i]->SetMarkerStyle(i+20);
2513  //if((i+2)!=6&&(i+2)!=7&&(i+2)!=1) ANNMc_g[i]->SetMarkerSize(0.5);
2514  rho_g[i]->SetMarkerSize(0.5);
2515  // char def[1024];
2516  char defANNMc_rho[1024];
2517  sprintf(defANNMc_rho,"Mc_%1.2f & ANNcut_%1.2f",Mccuts[i],ANNcuts[i]);
2518  if(rho_g[i]->GetN()!=0) legrho->AddEntry(rho_g[i],defANNMc_rho,"pl");
2519  if(rho_g[i]->GetN()!=0) mg0->Add(rho_g[i]);
2520  // if(ANNMc_g[0]->GetN()!=0) mg1->Add(ANNMc_g[0]);
2521  }
2522  mg0->Draw("apl");
2523  legrho->Draw();
2524  TString rhoCname(name);
2525  TString rhoCnamer(name);
2526  rhoCname.ReplaceAll(".root",".png");
2527  char rhoCname2[1024];
2528  char rhoCname2r[1024];
2529  sprintf(rhoCname2,"Cuts_caROC/TAvsFA_changingRHO%s",rhoCname.Data());
2530  sprintf(rhoCname2r,"Cuts_caROC/TAvsFA_changingRHO%s",rhoCnamer.Data());
2531  canrho->SaveAs(rhoCname2);
2532  canrho->Print(rhoCname2r);
2533 
2534 
2535 for(int j=0; j<npoints; j++) cout<<" th_Mc[j] "<<th_Mc[j]<<" j "<<j<<" TA_Mc[0][j] "<<TA_Mc[0][j]<<" FA_Mc[0][j] "<<FA_Mc[0][j]<<endl;
2536 
2537 
2538 for (int i=0; i<nANNth; i++){
2539  Mc_g[i+1]=new TGraph(npoints,FA_Mc[i+1],TA_Mc[i+1]);
2540  ANNMc_g[i]=new TGraph(npoints,FA_ANNMc[i],TA_ANNMc[i]);
2541 }
2542 
2543  TCanvas* can=new TCanvas("Efficiency_vs_FalseAlarm","Efficiency_vs_Alarm",0,0,1200,700);
2544  can->SetLogx();
2545  TLegend *leg = new TLegend(.75, .4, .99, .65);
2546  //TLegend *leg =new TLengend(.75, .80, .95, .95);
2547  TMultiGraph* mg1=new TMultiGraph();
2548  mg1->SetTitle("Comparison;FalseAlarms;Efficiency");
2549  Mc_g[0]->SetLineColor(3);
2550  Mc_g[0]->SetMarkerStyle(1);
2551  for (int i=0;i<nANNth;i++){
2552  Mc_g[i+1]->SetLineColor(4);
2553  //if((i+2)!=6&&(i+1)!=7&&(i+2)!=1)Mc_g[i+1]->SetMarkerSize(0.5);
2554  Mc_g[i+1]->SetMarkerSize(0.5);
2555  Mc_g[i+1]->SetMarkerStyle(i+20);
2556  Mc_g[i+1]->SetMarkerColor(4);
2557  ANNMc_g[i]->SetLineColor(2);
2558  ANNMc_g[i]->SetMarkerColor(2);
2559  ANNMc_g[i]->SetMarkerStyle(i+20);
2560  //if((i+2)!=6&&(i+2)!=7&&(i+2)!=1) ANNMc_g[i]->SetMarkerSize(0.5);
2561  ANNMc_g[i]->SetMarkerSize(0.5);
2562  char defMc[1024];
2563  char defANNMc[1024];
2564  sprintf(defMc,"Mc & ANNcut_%1.2f",yasym[i]);
2565  sprintf(defANNMc,"Hyperbola_cut,y_asymtoto_%1.2f","yasym[i]");
2566  if(Mc_g[i+1]->GetN()!=0) leg->AddEntry(Mc_g[i+1],defMc,"pl");
2567  if(Mc_g[i+1]->GetN()!=0) mg1->Add(Mc_g[i+1]);
2568  if(ANNMc_g[i]->GetN()!=0) leg->AddEntry(ANNMc_g[i],defANNMc,"pl");
2569  if(ANNMc_g[i]->GetN()!=0) mg1->Add(ANNMc_g[i]);
2570  // if(ANNMc_g[0]->GetN()!=0) mg1->Add(ANNMc_g[0]);
2571  }
2572  if(Mc_g[0]->GetN()!=0) mg1->Add(Mc_g[0]);
2573  if(Mc_g[0]->GetN()!=0) leg->AddEntry(Mc_g[0],"Mc, no ANNcut","pl");
2574  mg1->Draw("apl");
2575  leg->Draw();
2576 // ANNMc_g[0]= new TGraph(npoints, )
2577 // cout<<" TA_ANNMc[0][0] "<<TA_ANNMc[0][0]<<" FA_ANNMc[0][0] "<<FA_ANNMc[0][0]<<endl;
2578  TString Cname(name);
2579  TString Cnamer(name);
2580  Cname.ReplaceAll(".root",".png");
2581  char Cname2[1024];
2582  char Cname2r[1024];
2583  sprintf(Cname2,"Cuts_caROC/TAvsFA_%s",Cname.Data());
2584  sprintf(Cname2r,"Cuts_caROC/TAvsFA_%s",Cnamer.Data());
2585  can->SaveAs(Cname2);
2586  can->Print(Cname2r);
2587 
2588 }
2589 
2590 
char ofile[1024]
double rho
Float_t * rho
biased null statistics
Definition: netevent.hh:112
void CoG_plots(TString ifile)
wavearray< double > a(hp.size())
par [0] name
int n
Definition: cwb_net.C:28
TString("c")
ofstream out
Definition: cwb_merge.C:214
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
Int_t GetEntry(Int_t)
Definition: netevent.cc:409
CWB::Toolbox TB
void CutMcANN(TString ifile)
STL namespace.
int j
Definition: cwb_net.C:28
i drho i
wavearray< double > hh
Definition: Regression_H1.C:73
cout<< "SNR "<< xsnr<< endl;wavearray< double > f
Definition: ComputeSNR.C:75
wavearray< double > h
Definition: Regression_H1.C:25
void Annth(TString ifile, int &FAth_n, int &FDth_n, double &thresFA)
cout<< "Injected signals: "<< mdc.GetEntries()<< endl;cout<< "Injected signals in histogram factor_events_inj: "<< NEVTS<< endl;float myifar, ecor, m1, m2, netcc[3], neted, penalty;float rho[2];float chirp[6];float range[2];float frequency[2];float iSNR[3], sSNR[3];sim.SetBranchAddress("mass", mass);sim.SetBranchAddress("factor", &factor);sim.SetBranchAddress("range", range);sim.SetBranchAddress("chirp", chirp);sim.SetBranchAddress("rho", rho);sim.SetBranchAddress("netcc", netcc);sim.SetBranchAddress("neted", &neted);sim.SetBranchAddress("ifar", &myifar);sim.SetBranchAddress("ecor", &ecor);sim.SetBranchAddress("penalty", &penalty);sim.SetBranchAddress("time", mytime);sim.SetBranchAddress("iSNR", iSNR);sim.SetBranchAddress("sSNR", sSNR);sim.SetBranchAddress("spin", spin);sim.SetBranchAddress("frequency", frequency);float **volume=new float *[NBINS_mass1];float **volume_first_shell=new float *[NBINS_mass1];float **radius=new float *[NBINS_mass1];float **error_volume=new float *[NBINS_mass1];float **error_volume_first_shell=new float *[NBINS_mass1];float **error_radius=new float *[NBINS_mass1];for(int i=0;i< NBINS_mass1;i++) { volume[i]=new float[NBINS_mass2];volume_first_shell[i]=new float[NBINS_mass2];radius[i]=new float[NBINS_mass2];error_volume[i]=new float[NBINS_mass2];error_volume_first_shell[i]=new float[NBINS_mass2];error_radius[i]=new float[NBINS_mass2];for(int j=0;j< NBINS_mass2;j++) { volume[i][j]=0.;volume_first_shell[i][j]=0.;radius[i][j]=0.;error_volume[i][j]=0.;error_volume_first_shell[i][j]=0.;error_radius[i][j]=0.;} } float **spin_mtot_volume=new float *[NBINS_MTOT+1];float **spin_mtot_radius=new float *[NBINS_MTOT+1];float **error_spin_mtot_volume=new float *[NBINS_MTOT+1];float **error_spin_mtot_radius=new float *[NBINS_MTOT+1];for(int i=0;i< NBINS_MTOT+1;i++) { spin_mtot_volume[i]=new float[NBINS_SPIN+1];spin_mtot_radius[i]=new float[NBINS_SPIN+1];error_spin_mtot_volume[i]=new float[NBINS_SPIN+1];error_spin_mtot_radius[i]=new float[NBINS_SPIN+1];for(int j=0;j< NBINS_SPIN+1;j++) { spin_mtot_volume[i][j]=0.;error_spin_mtot_volume[i][j]=0.;spin_mtot_radius[i][j]=0.;error_spin_mtot_radius[i][j]=0.;} } char fname[1024];sprintf(fname, "%s/recovered_signals.txt", netdir);ofstream fev;fev.open(fname, std::ofstream::out);sprintf(line, "#GPS@L1 FAR[Hz] eFAR[Hz] Pval " "ePval factor rho frequency iSNR sSNR \");fev<< line<< endl;ofstream *fev_single=new ofstream[nfactor];for(int l=1;l< nfactor+1;l++) { sprintf(fname, "%s/recovered_signals_%d.txt", netdir, l);fev_single[l - 1].open(fname, std::ofstream::out);fev_single[l - 1]<< line<< endl;} double Vrho[RHO_NBINS], eVrho[RHO_NBINS], Rrho[RHO_NBINS], eRrho[RHO_NBINS], Trho[RHO_NBINS];for(int i=0;i< RHO_NBINS;i++) { Vrho[i]=0.;eVrho[i]=0.;Rrho[i]=0.;eRrho[i]=0.;Trho[i]=RHO_MIN+i *RHO_BIN;} double dV, dV1, dV_spin_mtot, nevts, internal_volume;int nT;int countv=0;int cnt=0;int cnt2=0;int cntfreq=0;bool bcut=false;double liveTot=sim.GetMaximum("ifar");double BKG_LIVETIME_yr=liveTot/CYS;double BKG_LIVETIME_Myr=BKG_LIVETIME_yr/(1.e6);cout.precision(14);cout<< "Total live time ---> background
void PlotsAv_cc(TString ifile)
char output[256]
int k
Float_t * netcc
effective correlated SNR
Definition: netevent.hh:113
TFile * ifile
wavearray< double > yy
Definition: TestFrame5.C:12
s s
Definition: cwb_net.C:155
void graph(TString ifile)
void Mcth(TString ifile, int &McFAth_n, int &McFDth_n, double &McthresFA)
long int num
sprintf(tfres,"(1/%g)x(%g) (sec)x(Hz)", 2 *df, df)
Int_t GetEntries()
Definition: netevent.cc:403
char line[1024]
void PlotsAv_Mc(TString ifile)
wavearray< double > y
Definition: Test10.C:31
void SNR_plots(TString ifile)
Float_t * chirp
range to source: [0/1]-rec/inj
Definition: netevent.hh:128
exit(0)
void JoinCutANN_Mchirp_ROCcurves_rho2(TString NN_FILE, TString NN_FILE2, TString NN_FILE3, TString NN_FILE4, TString NN_FILE5, TString NN_FILE6, TString NN_FILE7, TString NN_FILE8, TString TEST_FILE, TString NOMEtot_S, int TS=0, int TB=0, int s=0, int b=0, int uf=1, int consider_all=0, int av_on_nNN=0)