Logo coherent WaveBurst  
Library Reference Guide
Logo
cwbANN_purity_SoB.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 //
20 // Neural Network Analysis
21 // Author : Serena Vinciguerra
22 // Note : run with ACLiC -> root -l macro/cwb_mlp.C+
23 //
24 
25 #define XIFO 4
26 
27 #pragma GCC system_header
28 
29 #include "cwb.hh"
30 #include "config.hh"
31 #include "network.hh"
32 #include "wavearray.hh"
33 #include "TString.h"
34 #include "TObjArray.h"
35 #include "TObjString.h"
36 #include "TPaletteAxis.h"
37 #include "TMultiLayerPerceptron.h"
38 #include "TMLPAnalyzer.h"
39 #include "TRandom.h"
40 #include "TComplex.h"
41 #include "TMath.h"
42 #include "mdc.hh"
43 #include "watplot.hh"
44 #include <vector>
45 
46 
47 //#define SIG_FILE "/home/serena.vinciguerra/test0/S6D_R11_SIM_EOBNRv2_advL1H1V1_2G_run1_2/merge/NN_wave_S6D_R11_SIM_EOBNRv2_advL1H1V1_2G_run1_2.M1.root"
48 //#define BG_FILE "/home/serena.vinciguerra/test0/S6D_R11_BKG_L1H1V1_2G_MP_run1/merge/NN_wave_S6D_R11_BKG_L1H1V1_2G_MP_run1.M1.root"
49 #define NRHO 50
50 #define NCC 50
51 #define nIFO 3 // number of detectors
52 //#define differentW
53 #define RHO_CC
54 #define CREATE_NETWORK
55 #define trainTEST
56 TString select7Train_non_es(TString ORIGINAL_FILE,std::vector<int>* choice,TString IDname);
57 
58 //NOTA_BENE:OGNI VOLTA CHE VIENE RICHIAMATA LA MACRO LA NN PRECEDENTE VIENE SOVRASCRITTA
59 //NOTA_BENE: OGNI VOLTA CHE FACCIO UN GRAFICO ROC SI SOVRASCRIVE SE CAMBIO SOLO COSE DIVERSE DA NTRAINBG E NTRAINS E Y
60 //NOTA_BENE: DOVE SI USA QST MACRO DEVONO ESISTERE LA CARRTELLA png(se def Graph), tree, NN
61 
62 void cwbANN_purity_SoB(int nTrain,int nTrainBg, TString option,TString TREE_FILE,int q,int p,double rm=0., double cm=0., int lm=5, Int_t nepoch=700) {
63 
64  if (!gROOT->GetClass("TMultiLayerPerceptron")) {
65  gSystem->Load("libMLP");
66  }
67 
68  //gSystem->Load("select7_C.so");
69 // gROOT->LoadMacro("select7Train_non_es.C");
70 // cout<<soglia<<endl;
71  double error;
72  //int y=0;
73 // int q=0;//sig
74 // int p=0;//bg
75  double maxTrain=0;
76  // cout<<"y:"<<y<<endl;
77  if(nTrain>nTrainBg) maxTrain=nTrain;
78  else maxTrain=nTrainBg;
79 
80  TFile* ifile=TFile::Open(TREE_FILE.Data());
81  TTree* NNTree=(TTree*)ifile->Get("nnTree");
82  //NNTree->SetDirectory(0);
83  //ifile->Close();
84 
85  int entries=NNTree->GetEntries();
86  cout<<"entries: "<<entries<<endl;
87 
88 //estraggo le info che servono anche al tree di def dell'mlp
89  int ndim;
90  int ninp;
91  int y;
92  int entriesTot;
93  int bg_entries;
94  int sig_entries;
95  NNTree->SetBranchAddress("#Entries_type",&entriesTot);
96  NNTree->SetBranchAddress("Matrix_dim",&ndim);
97  NNTree->SetBranchAddress("#inputs",&ninp);
98  NNTree->SetBranchAddress("amplitude_mode",&y);
99 
100  NNTree->GetEntry(0);
101  sig_entries=entriesTot;
102  NNTree->GetEntry(entries-1);
103  bg_entries=entriesTot;
104 
105  int const NDIM=ndim;
106  int const nINP=ninp;
107  cout<<"NDIM "<<NDIM<<endl;
108  cout<<"nINP"<<nINP<<endl;
109 
110  cout<<"sig e "<<sig_entries<<endl;
111  cout<<"bg e "<<bg_entries<<endl;
112 
113 //contolli
114  if(sig_entries==bg_entries) cout<<"Error: training with a single type of event"<<endl;
115 
116  if(bg_entries<nTrainBg){cout<<"Error: Bg: num train>num events"<<endl;
117  exit(0);}
118  if(sig_entries<nTrain) {cout<<"Error: Sig: num train>num events"<<endl;
119  exit(0);}
120 
121  if(!nTrainBg) nTrainBg=bg_entries;
122  if(!nTrain) nTrain=sig_entries;
123 //fine controlli
124 
125 //definizione start
126  // q=gRandom->Integer(sig_entries-nTrain);
127  // p=gRandom->Integer(bg_entries-nTrainBg)+sig_entries;
128  //q=1;
129  //p=1;
130 
131  cout<<"p"<<p<<endl;
132  cout<<"q"<<q<<endl;
133 //fine defnizione start
134  TString TreeF(TREE_FILE);
135  TreeF.ReplaceAll("/home/serena.vinciguerra/","");
136  TreeF.ReplaceAll("/","_");
137  TreeF.ReplaceAll(".root","");
138 TString structure(option);
139  structure.ReplaceAll(":","_");
140  char nomeNN[512];
141  sprintf(nomeNN,"NN/mlpNetwork_colored_nTS%i_nTB%i_structure%s_lm%i_epochs%i_s%i_b%i_ROC_TREEfile_%s.root",nTrain,nTrainBg,structure.Data(),lm,nepoch,q,p,TreeF.Data());
142 cout<<nomeNN<<endl;
143  char nameTrain[512];
144  sprintf(nameTrain,"PropertyGraphs/mlpNetwork_colored_nTS%i_nTB%i_s%i_b%i_TREEfile_%s.root",nTrain,nTrainBg,q,p,TreeF.Data());
145 #ifdef RHO_CC
146 TString SIG_FILE;
147 TString BG_FILE;
148 char FILE_NAME[516];
149 NNTree->SetBranchAddress("Files_name",&FILE_NAME);
150 NNTree->GetEntry(0);
151 SIG_FILE=FILE_NAME;
152 NNTree->GetEntry(entries-1);
153 BG_FILE=FILE_NAME;
154 cout<<"fine ifdef RHO_CC"<<endl;
155 #endif
156 
157 TString FileSelectName(nameTrain);
158 TString nomefile;
159 FileSelectName.ReplaceAll("PropertyGraphs/mlpNetwork","selectTREE/TreeTrain");
160 TFile *f0=TFile::Open(FileSelectName.Data(),"read");
161 
162 if (!f0) {
163 
164 //RICOSTRUZIONE TREE per Train
165 std::vector<int>* choice=new vector<int>;
166 cout<<"vector definition"<<endl;
167 //choice->push_back(q);
168 for (int z=0;z<nTrain;z++) choice->push_back(q+z);
169 //(*choice)[nTrain]=p;
170 for (int z=0;z<nTrainBg;z++) choice->push_back(z+p);
171 // gSystem->Load("select7_C.so");
172 
173 nomefile=select7Train_non_es(TREE_FILE.Data(),choice,nameTrain);
174 cout<<"new file created"<<endl;
175 }
176 else {
177 nomefile=FileSelectName.Data();
178 cout<<"open file-Train pre-existing: "<<nomefile.Data()<<endl;
179 f0->Close();}
180 
181 //char nomefile[516];
182 //sprintf(nomefile,"selectTREE/TreeTrain_size%i_s%i_b%i.root",choice->size(),q,p);
183 TFile* fsel=TFile::Open(nomefile.Data());
184 TTree* tTrain1=(TTree*)fsel->Get("nnTree");
185 cout<<"def Tree per Train"<<endl;
186  // add leaf
187  Int_t type;
188  Float_t x[nINP];
189  char ilabel[nINP][16];
190  // char llabel[nINP][16];
191  for(int i=0;i<nINP;i++) {
192  sprintf(ilabel[i],"x%d",i+1);
193  // cout<<"ilabel "<<ilabel[i]<<endl;
194  // sprintf(llabel[i],"%s/F",ilabel[i]);
195  // tTrain->Branch(ilabel[i], &x[i],llabel[i]);
196  tTrain1->SetBranchAddress(ilabel[i], &x[i]);
197  }
198  tTrain1->SetBranchAddress("type",&type);
199  #ifdef differentW
200  Float_t w[nINP];
201  char wlabel[nINP][16];
202  // char llabel2[nINP][16];
203 
204  for(int i=0;i<nINP;i++) {
205  sprintf(wlabel[i],"w%d",i+1);
206  // sprintf(llabel2[i],"%s/F",wlabel[i]);
207  tTrain1->SetBranchAddress(wlabel[i], &w[i]);
208  }
209  #else
210  float w;
211  tTrain1->SetBranchAddress("w",&w);
212  #endif
213  ifile->Close();
214 
215 #ifdef RHO_CC
216  TChain sigTree("waveburst");//cerca il Tree "waveburst nei file
217  sigTree.Add(SIG_FILE.Data());//determina i file
218  netevent signal(&sigTree,nIFO);
219  int sig_entries2 = signal.GetEntries();
220  cout << "sig entries2 : " << sig_entries2 << endl;
221 
222  TChain bgTree("waveburst");
223  bgTree.Add(BG_FILE.Data());
224  netevent background(&bgTree,nIFO);
225  int bg_entries2 = background.GetEntries();
226  cout << "bg entries2 : " << bg_entries2 << endl;
227 
228  TH1F *ibg = new TH1F("bgh", "rho[0]", 50, 0, 50);
229  TH1F *isig = new TH1F("sigh", "rho[0]", 50, 0, 18);
230  TH1F *ibg2 = new TH1F("bgh2", "netcc[1]", 50, 0, 1.2);
231  TH1F *isig2 = new TH1F("sigh2", "netcc[1]", 50, 0, 1.1);
232 // TH1F *ibg3 = new TH1F("bgh3", "test secondo giro", 50, -1, 2);
233 // TH1F *isig3 = new TH1F("sigh3", "test secondo giro", 50, -1, 2);
234 
235  ibg->SetDirectory(0);
236  isig->SetDirectory(0);
237  ibg2->SetDirectory(0);
238  isig2->SetDirectory(0);
239 // ibg3->SetDirectory(0);
240 // isig3->SetDirectory(0);
241  Int_t i;
242  double minRHO=0.;
243  double minCC=0.;
244  double maxRHO=0.;
245  double maxCC=0.;
246 
247 TTree* t_NNout=new TTree("rho_cc_NNout","rho_cc_NNout");
248 double rho_NN=0.;
249 double cc_NN=0.;
250 double NNout=0.;
251 int NNtype;
252 t_NNout->Branch("rho",&rho_NN,"rho/D");
253 t_NNout->Branch("cc",&cc_NN,"cc/D");
254 t_NNout->Branch("type",&NNtype,"type/I");
255 t_NNout->Branch("NNout",&NNout,"NNout/D");
256 
257 for(int h=q;h<q+nTrain;h++){
258  int resto0=0;
259  if(q%2==0) resto0=(h)%2;
260  else resto0=(h+1)%2;
261  sigTree.GetEntry(h);
262  if (resto0==0&&h<q+2) minCC=(double)signal.netcc[1];
263  if (resto0==0&&h<q+2) minRHO=(double)signal.rho[0];
264  if (signal.rho[0]<minRHO&&resto0==0)minRHO=(double)signal.rho[0];
265  if (signal.netcc[1]<minCC&&resto0==0)minCC=(double)signal.netcc[1];
266  if (signal.rho[0]>maxRHO&&resto0==0)maxRHO=(double)signal.rho[0];
267  if (signal.netcc[1]>maxCC&&resto0==0)maxCC=(double)signal.netcc[1];
268  isig->Fill(signal.rho[0]);
269  isig2->Fill(signal.netcc[1]);
270  }
271 
272 for(int h=(p-sig_entries);h<(p-sig_entries+nTrainBg);h++){
273  int resto0=0;
274  if((p-sig_entries+nTrainBg)%2==0) resto0=(h)%2;
275  else resto0=(h+1)%2;
276  bgTree.GetEntry(h);
277  if (resto0==0&&h<q+2) minCC=(double)signal.netcc[1];
278  if (resto0==0&&h<q+2) minRHO=(double)signal.rho[0];
279  if (background.rho[0]<minRHO&&resto0==0)minRHO=(double)background.rho[0];
280  if (background.netcc[1]<minCC&&resto0==0)minCC=(double)background.netcc[1];
281  if (background.rho[0]>maxRHO&&resto0==0)maxRHO=(double)background.rho[0];
282  if (background.netcc[1]>maxCC&&resto0==0)maxCC=(double)background.netcc[1];
283  ibg->Fill(background.rho[0]);
284  ibg2->Fill(background.netcc[1]);
285  }
286 cout<<"max RHO"<<maxRHO<<" maxCC "<<maxCC<<" minRHO "<<minRHO<<" minCC "<<minCC<<endl;
287 #endif
288 if (cm!=0) maxCC=cm;
289 if (rm!=0) maxRHO=rm;
290 
291 
292 TString layout;
293 for (int i=0;i<nINP-1;i++) {
294 char add[512];
295 sprintf(add,"@%s,",ilabel[i]);
296 layout+=add;
297 
298 }
299 char add1[512];
300 sprintf(add1,"@%s:%s:type",ilabel[nINP-1],option.Data());
301 layout+=add1;
302 cout<<layout.Data()<<endl;
303 
304 // char layout[5096]="";
305 // for (int i=0;i<nINP-1;i++) sprintf(layout,"%s@%s,",layout,ilabel[i]);
306 // sprintf(layout,"%s@%s:%s:type",layout,ilabel[nINP-1],option.Data());
307  // cout << "MultiLayer Perceptron layout -> " << layout << endl;
308 
309 #ifdef CREATE_NETWORK
310  TCanvas *errors=new TCanvas("errors","errors",0,0,600,600);
311  TMultiLayerPerceptron *mlp = new TMultiLayerPerceptron(layout,"w",tTrain1,"Entry$%2","(Entry$+1)%2");
312  if (lm==1)mlp->SetLearningMethod(TMultiLayerPerceptron::kStochastic);
313  if(lm==2)mlp->SetLearningMethod(TMultiLayerPerceptron::kBatch);
314  if(lm==3)mlp->SetLearningMethod(TMultiLayerPerceptron::kSteepestDescent);
315  if(lm==4)mlp->SetLearningMethod(TMultiLayerPerceptron::kRibierePolak);
316  if(lm==5)mlp->SetLearningMethod(TMultiLayerPerceptron::kFletcherReeves);
317  if(lm==6)mlp->SetLearningMethod(TMultiLayerPerceptron::kBFGS);
318  if(lm>6||lm<1){
319  cout<<"Invalid Learning Method:1<=lm<=6"<<endl;
320  exit(0);
321  }
322 
323  mlp->Train(nepoch, "text,current,graph,update=10");
324  // char nomePNG[128];
325  TString PNGname(nomeNN);
326  TString PNGnameroot(nomeNN);
327  PNGname.ReplaceAll("NN/","ErrorGraphs/");
328  PNGnameroot.ReplaceAll("NN/","ErrorGraphs/");
329  PNGname.ReplaceAll(".root",".png");
330 // sprintf(nomePNG,"ErrorGraphs/mlpNetwork_nTS%i_nTB%i_lm%i_epochs%i_s%i_b%i.png",nTrain,nTrainBg,q,p);
331  errors->SaveAs(PNGname.Data());
332  errors->Print(PNGnameroot.Data());
333  // char nomeNN[128];
334  // sprintf(nomeNN,"NN/mlpNetwork_nTS%i_nTB%i__lm%i_epochs%i_s%i_b%i.root",nTrain,nTrainBg,q,p);
335  // TFile fnet(nomeNN,"RECREATE");
336  TFile fnet(nomeNN,"RECREATE");
337 
338  mlp->Write();
339 // fnet.Close();
340 
341 #else
342 cout<<"ci sono"<<endl;
343  TFile fnet(nomeNN);
344 cout<<"non ci sono più"<<endl;
345 // TFile fnet = TFile::Open(nomeNN);
346  TMultiLayerPerceptron *mlp = (TMultiLayerPerceptron*)fnet.Get("TMultiLayerPerceptron");
347  if(mlp==NULL) {cout << "Error getting mlp" << endl;exit(1);}
348  //fnet->Close();
349 #endif
350 
351  // Use TMLPAnalyzer to see what it looks for
352  TCanvas* mlpa_canvas = new TCanvas("mlpa_canvas","Network analysis");
353 #ifdef RHO_CC
354  mlpa_canvas->Divide(2,2);
355 
356  //DISEGNA L'ISTROGRMMA DI RHO
357  mlpa_canvas->cd(1);
358  ibg->SetLineColor(kBlue);
359  ibg->SetFillStyle(3008); ibg->SetFillColor(kBlue);
360  isig->SetLineColor(kRed);
361  isig->SetFillStyle(3003); isig->SetFillColor(kRed);
362  ibg->SetStats(0);
363  isig->SetStats(0);
364 // isig->GetYaxis()->SetRangeUser(0,maxTrain/2);
365 // ibg->GetYaxis()->SetRangeUser(0,maxTrain/2);
366  ibg->Draw();
367  isig->Draw("same");
368  TLegend *ilegend = new TLegend(.75, .80, .95, .95);
369  ilegend->AddEntry(ibg, "Background");
370  ilegend->AddEntry(isig, "Signal");
371  ilegend->Draw();
372  cout << "Integral ibg : " << ibg->GetEntries() << endl;
373  cout << "Integral isig : " << isig->GetEntries() << endl;
374 
375 
376  //DISEGNA L'ISTOGRMMA CC
377  mlpa_canvas->cd(2);
378  // ibg2->GetYaxis()->SetRangeUser(0,maxTrain/2);
379  // isig2->GetYaxis()->SetRangeUser(0,maxTrain/2);
380  ibg2->SetLineColor(kBlue);
381  ibg2->SetFillStyle(3008); ibg2->SetFillColor(kBlue);
382  isig2->SetLineColor(kRed);
383  isig2->SetFillStyle(3003); isig2->SetFillColor(kRed);
384  ibg2->SetStats(0);
385  isig2->SetStats(0);
386  ibg2->Draw();
387  isig2->Draw("same");
388  TLegend *ilegend2 = new TLegend(.75, .80, .95, .95);
389  ilegend2->AddEntry(ibg2, "Background");
390  ilegend2->AddEntry(isig2, "Signal");
391  ilegend2->Draw();
392  cout << "Integral ibg : " << ibg2->GetEntries() << endl;
393  cout << "Integral isig : " << isig2->GetEntries() << endl;
394 #endif
395 
396  // Build and train the NN ptsumf is used as a weight since we are primarly
397 
398  TMLPAnalyzer ana(mlp);
399  // Initialisation
400  ana.GatherInformations();
401  // output to the console
402  ana.CheckNetwork();
403 #ifdef CREATE_NETWORK
404 #ifdef RHO_CC
405  mlpa_canvas->cd(4);
406  // shows how each variable influences the network
407  ana.DrawDInputs();
408  // mlpa_canvas->cd(2);
409  // shows the network structure
410  // mlp->Draw();
411  //ana.DrawDInputs();
412  mlpa_canvas->cd(3);
413  // draws the resulting network
414  gPad->SetLogy();
415  ana.DrawNetwork(0,"type==1","type==0"); //disegna solo il test sample
416 #else
417 mlpa_canvas->Divide(1,2);
418 mlpa_canvas->cd(1);
419  ana.DrawDInputs();
420 mlpa_canvas->cd(2);
421  gPad->SetLogy();
422  ana.DrawNetwork(0,"type==1","type==0"); //disegna solo il test sample
423 #endif
424 
425 //DEFINISCO IL TREE DOVE SALVO TUTTE LE INFO SUL SET TEST
426 
427 TTree* t= new TTree("info","info");
428 t->Branch("amplitude_mode",&y,"amplitude_mode/I");
429 // int nDim=NDIM;
430 t->Branch("Matrix_dim",&ndim,"Matrix_dim/I");
431 // int nInp=nINP;
432 t->Branch("#inputs",&ninp,"#inputs/I");
433  char NNname[516];
434  sprintf(NNname,"%s",option.Data());
435 t->Branch("NNname",&NNname,"NNname/C");
436 t->Branch("#trainBg",&nTrainBg,"#trainBg/I");
437 t->Branch("#trainSig",&nTrain,"#trainSig/I");
438 //double threshold;
439 //t->Branch("threshold",&soglia,"threshold/D");
440 t->Branch("Rand_start_Bg",&p,"Rand_start_Bg/I");
441 t->Branch("Rand_start_Sig",&q,"Rand_start_Sig/I");
442 t->Branch("epochs",&nepoch,"epocs/I");
443 t->Branch("learning_method",&lm,"learning_method/I");
444 t->Fill();
445 
446 t->Write();
447 //t->SetDirectory(fnet);
448 
449 double errTot=mlp->GetError(TMultiLayerPerceptron::kTest);
450 
451 
452 mlpa_canvas->cd(0);
453 mlpa_canvas->Write();
454 TString CanvNN(nomeNN);
455 TString CanvNNroot(nomeNN);
456 CanvNN.ReplaceAll("NN/","mlpa_canv/");
457 CanvNNroot.ReplaceAll("NN/","mlpa_canv/");
458 CanvNN.ReplaceAll(".root",".png");
459 mlpa_canvas->SaveAs(CanvNN.Data());
460 mlpa_canvas->Print(CanvNNroot.Data());
461 #endif
462 fnet.Close();
463 //fnet.Close();
464 
465 //Testin the Train sample
466  int const npunti=40;
467  double params[nINP];
468  float TA[npunti+1];
469  float FA[npunti+1];
470  float thres0[npunti+1];
471  float TA1[npunti+1];
472  float FA1[npunti+1];
473 // float thres01[npunti+1];
474 
475  for (int i=0;i<=npunti;i++){
476  TA[i]=0;
477  TA1[i]=0;
478  FA[i]=0;
479  FA1[i]=0;
480  // thres01[i]=0;
481  thres0[i]=0;
482  }
483  float passo=0;
484  passo=(log(1.1)-log(0.5))/(npunti/2);
485  cout<<"passo "<<passo<<endl;
486  thres0[npunti]=0.5;
487  cout<<"soglia0 "<<thres0[0]<<endl;
488  for(int i=npunti;i>npunti/2;i--) {
489  thres0[i-1]=thres0[i]*exp(passo);}
490  for(int i=0;i<(npunti/2);i++) {
491  thres0[npunti/2+i+1]=1.6 -thres0[npunti/2+i+1];
492  thres0[npunti/2-i-1]=1.-thres0[npunti/2+i+1];
493 // cout<<"i: "<<i+11<<" "<<thres0[11+i]<<endl;
494 // cout<<"i: "<<11-i<<" "<<thres0[11-i]<<endl;
495 // thres0[i+1]=thres0[i]+passo;
496  }
497 thres0[npunti/2]=0.5;
498  for(int i=0;i<(npunti);i++) {
499  cout<<thres0[i]<<endl;
500  }
501 
502 /*for(int i=0;i<(npunti/2);i++) {
503  thres0[npunti/2+i+1]=thres0[npunti/2+i]*exp(passo);}
504  for(int i=0;i<(npunti/2);i++) {
505  thres0[npunti/2+i+1]=1.5 -thres0[npunti/2+i+1];
506  thres0[npunti/2-i-1]=1.-thres0[npunti/2+i+1];
507 // thres0[i+1]=thres0[i]+passo;
508  }
509 */
510 //TH2D* PUREZZA_S=new TH2D("Purezza","Purezza",NCC,0.,1.2,NRHO,0.,50);
511 TH2D* PUREZZA_S=new TH2D("Purezza","Purezza",NCC,minCC,maxCC,NRHO,minRHO,maxRHO);
512 TH2D* h2BgO_R=new TH2D("rho_cc_out","rho_cc_out",50,0.,50,50,-0.5,1.5);
513 TH2D* h2SigO_R=new TH2D("rho_cc_out","rho_cc_out",50,0.,50,50,-0.5,1.5);
514 TH2D* h2BgR_C=new TH2D("rho_cc_out","rho_cc_out",50,0.,1.2,50,0.,50);
515 TH2D* h2SigR_C=new TH2D("rho_cc_out","rho_cc_out",50,0.,1.2,50,0.,50);
516 TH2D* h2Bg=new TH2D("rho_cc_out","rho_cc_out",50,0.,1.2,50,-0.5,1.5);
517 TH2D* h2Sig=new TH2D("rho_cc_out","rho_cc_out",50,0.,1.2,50,-0.5,1.5);
518 h2Bg->SetDirectory(0);
519 h2Sig->SetDirectory(0);
520 PUREZZA_S->SetDirectory(0);
521  double tt1=0;
522  double yy1=0;
523  double ty1=0;
524  double tmedio1=0;
525  double ymedio1=0;
526  double tt=0;
527  double yy=0;
528  double ty=0;
529  double tmedio=0;
530  double ymedio=0;
531  double rho1[NRHO];
532  double cc1[NCC];
533 int const CCRHO=NRHO*NCC;
534  double pur[CCRHO];
535  double tot[CCRHO];
536  for (int ii=0;ii<NRHO; ii++) {
537  rho1[ii]=0.;
538  for (int ji=0;ji<NCC; ji++) {
539  if (ii==0) cc1[ji]=0.;
540  pur[NRHO*ii+ji]=0.;
541  tot[NRHO*ii+ji]=0.;
542  }
543  }
544  double deltacc=0.;
545  //deltacc=1.2/NRHO;
546  deltacc=(maxCC-minCC)/NCC;
547  double deltarho=0.;
548  //deltarho=50/NRHO;
549  deltarho=(maxRHO-minRHO)/NRHO;
550  //for (int ii=0;ii<NRHO;ii++) { rho1[ii]=50.0-ii*deltarho;
551  for (int ii=0;ii<NRHO;ii++) {
552  rho1[ii]=maxRHO-(ii+1)*deltarho;
553 /* cout<<" rho_i "<<rho1[ii];
554  rho1[ii]=100*rho1[ii];
555  int rho0=0;
556  rho0=(int)(rho1[ii]+0.5);
557  rho1[ii]=0.;
558  rho1[ii]=(double)rho0/100;
559  cout<<" rho_f "<<rho1[ii]<<endl;
560 */ }
561  for (int ji=0;ji<NCC;ji++) {
562  cc1[ji]=(ji)*deltacc+minCC;
563  /*cout<<" cc_i "<<cc1[ji]<<endl;
564  cc1[ji]=100*cc1[ji];
565  int cc0=0;
566  cc0=(int)(cc1[ji]+0.5);
567  cc1[ji]=0.;
568  cc1[ji]=(double)cc0/100;
569  cout<<" cc_f "<<cc1[ji]<<endl;
570 */ }
571  for (int i = 0; i <(double)tTrain1->GetEntries(); i++) //o va +1 o +0,5?
572  {
573  tTrain1->GetEntry(i);
574  for (int j=0;j<nINP;j++)
575  {
576  params[j]=0;
577  params[j]=(double)x[j];
578  }
579  int j=npunti;
580  double output=mlp->Evaluate(0,params);
581  cout<<"output "<<output;
582  //<<endl;
583  int resto=i%2;
584  if (resto==0){
585  NNout=output;
586  if(type==1){
587  NNtype=1;
588  rho_NN=(double)signal.rho[0];
589  cc_NN=(double)signal.netcc[1];
590  }
591  if(type==0){
592  NNtype=0;
593  rho_NN=(double)background.rho[0];
594  cc_NN=(double)background.netcc[1];
595  }
596  t_NNout->Fill();
597  }
598 #ifdef RHO_CC
599  if (type==1&&resto==0){
600  sigTree.GetEntry(i+q);
601  h2SigO_R->Fill(signal.rho[0],output);
602  for (int ii=0; ii<NRHO;ii++){
603  if (signal.rho[0]>=rho1[ii]) {
604  for (int ji=0;ji<NCC;ji++){
605  if (signal.netcc[1]>=cc1[ji]) {
606  if (output>0.5){ pur[ii*NRHO+ji]=pur[ii*NRHO+ji]+1;
607  tot[ii*NRHO+ji]=tot[ii*NRHO+ji]+1;
608  }
609  }
610  }
611  }
612  }
613  if (output<0.1) cout<<"output<0.1, while type=1 event_index: "<<i+q<<" of file: "<<SIG_FILE.Data()<<endl;
614  h2SigR_C->Fill(signal.netcc[1],signal.rho[0]);
615  h2Sig->Fill(signal.netcc[1],output);
616  }
617  if (type==0&&resto==0){
618  bgTree.GetEntry(p-sig_entries+i-nTrain);
619  h2BgO_R->Fill(background.rho[0],output);
620  for (int ii=0; ii<NRHO;ii++){
621  for (int ji=0;ji<NCC;ji++){
622  if (background.rho[0]>rho1[ii]) {
623  if (background.netcc[1]>cc1[ji]) {
624  if (output>0.5) { tot[ii*NRHO+ji]=tot[ii*NRHO+ji]+1;
625  }
626  }
627  }
628  }
629  }
630  if (output>0.9) cout<<"output>0.9, while type=0 event_index: "<<p-sig_entries+i-nTrain<<" of file: "<<BG_FILE.Data()<<endl;
631  h2BgR_C->Fill(background.netcc[1],background.rho[0]);
632  h2Bg->Fill(background.netcc[1],output);
633  }
634 #endif
635  // cout<<"output "<<output<<" type "<<type;
636  // cout<<" #entry "<<i<<endl;
637  int entries=nTrain;
638  if(resto==0){
639  while(output<thres0[j]&&(j>0)) j=j-1;
640  for (int k=npunti;k>=0;k--){
641  if(type==0) {
642  if(k<=j) FA[k]=FA[k]+1;}
643  // cout<<"FA di "<<k<<" : "<<FA[k]<<" relativa a soglia "<<thres0[k]<<endl;}
644  if(type==1) {
645  if(k<=j) TA[k]=TA[k]+1; }
646  }
647  tmedio=tmedio+type;
648  ymedio=ymedio+output;
649  yy=output*output+yy;
650  tt=type*type+tt;
651  ty=type*output+ty;
652  }
653  if(resto!=0){
654  while(output<thres0[j]&&(j>0)) j=j-1;
655  for (int k=npunti;k>=0;k--){
656  if(type==0) {
657  if(k<=j) FA1[k]=FA1[k]+1;}
658  // cout<<"FA di "<<k<<" : "<<FA[k]<<" relativa a soglia "<<thres0[k]<<endl;}
659  if(type==1) {
660  if(k<=j) TA1[k]=TA1[k]+1; }
661  }
662  tmedio1=tmedio1+type;
663  ymedio1=ymedio1+output;
664  yy1=output*output+yy1;
665  tt1=type*type+tt1;
666  ty1=type*output+ty1;
667  }
668 
669  }
670 /* h2Bg->SetLineColor(kBlue);
671  h2Bg->SetFillStyle(3008); ibg->SetFillColor(kBlue);
672  h2Sig->SetLineColor(kRed);
673  h2Sig->SetFillStyle(3003); isig->SetFillColor(kRed);
674 */
675 
676  TString NNoutName(nomeNN);
677  NNoutName.ReplaceAll("NN/","NNout/NNout_");
678  TFile* f_NNout=new TFile(NNoutName,"RECREATE");
679  t_NNout->Write();
680  f_NNout->Close();
681 
682 cout<<""<<endl;
683  for (int ii=0;ii<NRHO; ii++) for (int ji=0;ji<NCC; ji++) {
684  cout<<" pur "<<pur[NRHO*ii+ji]<<" tot "<<tot[NRHO*ii+ji]<<" cc "<<cc1[ji]<< " rho "<<rho1[ii];
685 //<<endl;
686  if(tot[NRHO*ii+ji]>0) pur[NRHO*ii+ji]=pur[NRHO*ii+ji]/tot[NRHO*ii+ji];
687  cout<<" pur_fin "<<pur[NRHO*ii+ji];
688  //cout<<" index "<<NRHO*ii+ji;
689  cout<<""<<endl;
690  //if(tot[NRHO*ii+ji]>0) PUREZZA_S->Fill(cc1[ji],rho1[ii],pur[NRHO*ii+ji]);
691  if(tot[NRHO*ii+ji]>0) PUREZZA_S->Fill(cc1[ji]+deltacc/2,rho1[ii]+deltarho/2,pur[NRHO*ii+ji]);
692  }
693  h2Bg->SetMarkerStyle(7);
694  h2Sig->SetMarkerStyle(6);
695  h2Bg->SetMarkerColor(4);
696  h2Sig->SetMarkerColor(2);
697  h2BgO_R->SetMarkerStyle(7);
698  h2SigO_R->SetMarkerStyle(6);
699  h2BgO_R->SetMarkerColor(4);
700  h2SigO_R->SetMarkerColor(2);
701  h2BgR_C->SetMarkerStyle(7);
702  h2SigR_C->SetMarkerStyle(6);
703  h2BgR_C->SetMarkerColor(4);
704  h2SigR_C->SetMarkerColor(2);
705  // h2Bg->SetStats(0);
706  // h2Bg->SetStats(0);
707 
708  TCanvas *cRHO_CC_OUT=new TCanvas("RHO_CC_NNOUT","RHO_CC_NNOUT",1200,700);
709  //cRHO_CC_OUT->Divide(3,1);
710  cRHO_CC_OUT->Divide(2,2);
711  cRHO_CC_OUT->cd(1)->SetTitle("OUT_CC");
712  h2Bg->GetXaxis()->SetTitle("cc");
713  h2Bg->GetYaxis()->SetTitle("NNoutput");
714  h2Bg->GetZaxis()->SetTitle("count");
715  h2Bg->Draw();
716  h2Sig->Draw("same");
717  cRHO_CC_OUT->cd(2)->SetTitle("OUT_RHO");
718  h2BgO_R->GetXaxis()->SetTitle("rho");
719  h2BgO_R->GetYaxis()->SetTitle("NNoutput");
720  h2BgO_R->GetZaxis()->SetTitle("count");
721  h2BgO_R->Draw();
722  h2SigO_R->Draw("same");
723  cRHO_CC_OUT->cd(3)->SetTitle("RHO_CC");
724  h2BgR_C->GetXaxis()->SetTitle("cc");
725  h2BgR_C->GetYaxis()->SetTitle("rho");
726  h2BgR_C->GetZaxis()->SetTitle("count");
727  h2BgR_C->Draw();
728  h2SigR_C->Draw("same");
729  cRHO_CC_OUT->cd(4)->SetTitle("PUREZZA");
730  PUREZZA_S->SetStats(0);
731  PUREZZA_S->GetXaxis()->SetTitle("cc");
732  PUREZZA_S->GetYaxis()->SetTitle("rho");
733  PUREZZA_S->GetZaxis()->SetTitle("count");
734  PUREZZA_S->Draw("colz");
735 // PUREZZA_B->Draw("same");
736  TString cRHO_CC_OUTname(nomeNN);
737  TString cRHO_CC_OUTnameroot(nomeNN);
738  cRHO_CC_OUTname.ReplaceAll(".root",".png");
739  cRHO_CC_OUTnameroot.ReplaceAll("NN/mlpNetwork","CC_OUT/RHO_CC_OUTgraph");
740  cRHO_CC_OUTname.ReplaceAll("NN/mlpNetwork","CC_OUT/RHO_CC_OUTgraph");
741  cRHO_CC_OUT->SaveAs(cRHO_CC_OUTname.Data());
742  cRHO_CC_OUT->Print(cRHO_CC_OUTnameroot.Data());
743 // }
744  tmedio1=tmedio1/nTrain;
745  ymedio1=ymedio1/nTrain;
746 // tt1=tt1/nTrain;
747 // yy1=yy1/nTrain;
748 // ty1=ty1/nTrain;
749  tmedio=tmedio/nTrain;
750  ymedio=ymedio/nTrain;
751 // tt=tt/nTrain;
752 // yy=yy/nTrain;
753 // ty=ty/nTrain;
754  cout<<"tmedio1: "<<tmedio1<<" ymedio1 "<<ymedio1<<" tt1 "<<tt1<<" yy1 "<<yy1<<" ty1 "<<ty1<<" entries "<<entries<<endl;
755  double corr=0.;
756  double corr1=0.;
757  corr1=(ty1-nTrain*ymedio1*tmedio1)/sqrt((tt1-nTrain*tmedio1*tmedio1)*(yy1-nTrain*ymedio1*ymedio1));
758  corr=(ty-nTrain*ymedio*tmedio)/sqrt((tt-nTrain*tmedio*tmedio)*(yy-nTrain*ymedio*ymedio));
759  cout<<"corr "<<corr<<endl;
760  cout<<"corr1 "<<corr1<<endl;
761 
762 
763  for (int k=0;k<=npunti;k++) {
764  // cout<<"su "<<nTrain/2<<" TA: "<<TA[k]<<endl;
765  // cout<<"su "<<nTrainBg/2<<" FA: "<<FA[k]<<endl;
766  TA1[k]=(TA1[k]/nTrain)*2;
767  TA[k]=(TA[k]/nTrain)*2;
768  //cout<<"Eff: "<<TA[k]<<endl;
769  FA1[k]=(FA1[k]/nTrainBg)*2;
770  FA[k]=(FA[k]/nTrainBg)*2;
771  }
772  TString nf(nomeNN);
773  cout<<"ng "<<nf.Data()<<endl;
774  char nomefileTest[516];
775  int npunti2=npunti;
776  //sprintf(nomefile,"thresFiles/thres_np%i_nTS%i_nTB%i.root",npunti,nTrainS,nTrainB);
777  sprintf(nomefileTest,"thresFiles/thres_np%i_Test_onTrainSet_",npunti2);
778  nf.ReplaceAll("NN/mlpNetwork",nomefileTest);
779  cout<<"nf "<<nf.Data()<<endl;
780  TFile *gfile=new TFile(nf.Data(),"RECREATE");
781 
782 //#ifdef ROC_GRAPH
783  TGraph* gROC1=new TGraph(npunti+1,FA1,TA1);
784  gROC1->SetTitle("ROC_train(threshold)");
785  gROC1->SetMarkerStyle(7);
786  TGraph* gFA1=new TGraph(npunti+1,thres0,FA1);
787  gFA1->SetTitle("False Alarm _train(threshold)");
788  gFA1->SetMarkerStyle(7);
789  TGraph* gTA1=new TGraph(npunti+1,thres0,TA1);
790  gTA1->SetTitle("Efficiency _train(threshold)");
791  gTA1->SetMarkerStyle(7);
792 
793  TGraph* gROC=new TGraph(npunti+1,FA,TA);
794  gROC->SetTitle("ROC_test(threshold)");
795  gROC->SetMarkerStyle(7);
796  TGraph* gFA=new TGraph(npunti+1,thres0,FA);
797  gFA->SetTitle("False Alarm _test(threshold)");
798  gFA->SetMarkerStyle(7);
799  TGraph* gTA=new TGraph(npunti+1,thres0,TA);
800  gTA->SetTitle("Efficiency _test(threshold)");
801  gTA->SetMarkerStyle(7);
802 
803  TCanvas *cROC=new TCanvas("ROC_graph","ROC_graph");
804  cROC->Divide(2,3);
805  cROC->cd(2)->SetLogx();
806  // cROC->cd(2)->SetLogy();
807  gROC->Draw("apl");
808  cROC->cd(6);
809  cROC->cd(6)->SetLogy();
810  gFA->Draw("apl");
811  cROC->cd(4);
812  cROC->cd(4)->SetLogy();
813  gTA->Draw("apl");
814  cROC->cd(1)->SetLogx();
815  // cROC->cd(1)->SetLogy();
816  gROC1->Draw("apl");
817  cROC->cd(5);
818  cROC->cd(5)->SetLogy();
819  gFA1->Draw("apl");
820  cROC->cd(3);
821  cROC->cd(3)->SetLogy();
822  gTA1->Draw("apl");
823 
824  cROC->Write();
825  TString CANV_NAME(nomeNN);
826  TString CANV_NAMEroot(nomeNN);
827  CANV_NAME.ReplaceAll("NN/mlpNetwork","TRAIN_GRAPHS/ROC");
828  CANV_NAMEroot.ReplaceAll("NN/mlpNetwork","TRAIN_GRAPHS/ROC");
829  CANV_NAME.ReplaceAll(".root",".png");
830  cROC->SaveAs(CANV_NAME.Data());
831  cROC->Print(CANV_NAMEroot.Data());
832 //#endif
833 //#ifdef TH_TREE
834  TTree *ThTree=new TTree("ROC","ROC");
835 char FAlabel1[128];
836 char TAlabel1[128];
837 char FAlabel[128];
838 char TAlabel[128];
839 char thlabel[128];
840 sprintf(FAlabel1,"FalseAlarm_train[%i]/F",npunti);
841 sprintf(TAlabel1,"TrueAlarm_train[%i]/F",npunti);
842 sprintf(FAlabel,"FalseAlarm_test[%i]/F",npunti);
843 sprintf(TAlabel,"TrueAlarm_test[%i]/F",npunti);
844 sprintf(thlabel,"thresholds[%i]/F",npunti);
845 // float FAl=0;
846  ThTree->Branch("FalseAlarm_test",&FA,FAlabel);
847  ThTree->Branch("FalseAlarm_train",&FA1,FAlabel1);
848 // float TAl=0;
849  ThTree->Branch("Efficiency_train",&TA1,TAlabel1);
850  ThTree->Branch("Efficiency_test",&TA,TAlabel);
851 // double thresholds=0;
852  ThTree->Branch("thresholds",&thres0,thlabel);
853  ThTree->Branch("correlation_train",&corr1,"correlation_train/D");
854  ThTree->Branch("correlation_test",&corr,"correlation_test/D");
855 // ThTree->Branch("#TrainNN_Sig",&nTrainSNN,"#TrainNN_Sig/I");
856  int u=npunti;
857  ThTree->Branch("#points",&u,"#points/I");
858 // ThTree->Branch("#TrainNN_Bg",&nTrainBNN,"#TrainNN_Bg/I");
859  ThTree->Fill();
860  ThTree->Write();
861 //#endif
862 
863  gfile->Close();
864 }
865 
866 
867 
868 
869 TString select7Train_non_es(TString ORIGINAL_FILE,std::vector<int>* choice,TString nomeNN) {
870 TString IDname(nomeNN);
871 
872 //IDname.ReplaceAll("NN/","PropertyGraphs/");
873 
874 TFile* fOriginal=TFile::Open(ORIGINAL_FILE.Data());
875 TTree* tOriginal=(TTree*)fOriginal->Get("nnTree");
876 
877 if((choice->size())>(tOriginal->GetEntries())){cout<<"Error:startB+nTrainB>Entries"<<endl;
878  // exit(0);
879  }
880 
881 //estrazione nINP
882 int ninp;
883 tOriginal->SetBranchAddress("#inputs",&ninp);
884 tOriginal->GetEntry(0);
885 
886 int const nINP=ninp;
887 int const NDIM=sqrt(nINP);
888 
889 //definizione nuovo tree
890 TTree* t=new TTree("nnTree","nnTree");
891 
892 int type;
893 tOriginal->SetBranchAddress("type",&type);
894 t->Branch("type",&type,"type/I");
895 
896 float x[nINP];
897 char xlabel[nINP][16];
898 char xllabel[nINP][16];
899 #ifdef defferentW
900 float w[nINP];
901 char wlabel[nINP][16];
902 char wllabel[nINP][16];
903 #else
904 float w;
905 tOriginal->SetBranchAddress("w",&w);
906 t->Branch("w",&w,"w/F");
907 #endif
908 
909 #ifdef trainTEST
910 float SDs[nINP];
911 char SDlabels[nINP][16];
912 char SDllabels[nINP][16];
913 float RMSs[nINP];
914 char RMSlabels[nINP][16];
915 char RMSllabels[nINP][16];
916 float averages[nINP];
917 char averagelabels[nINP][16];
918 char averagellabels[nINP][16];
919 float SDb[nINP];
920 char SDlabelb[nINP][16];
921 char SDllabelb[nINP][16];
922 float RMSb[nINP];
923 char RMSlabelb[nINP][16];
924 char RMSllabelb[nINP][16];
925 float averageb[nINP];
926 char averagelabelb[nINP][16];
927 char averagellabelb[nINP][16];
928 float SD[nINP];
929 char SDlabel[nINP][16];
930 char SDllabel[nINP][16];
931 float RMS[nINP];
932 char RMSlabel[nINP][16];
933 char RMSllabel[nINP][16];
934 float average[nINP];
935 char averagelabel[nINP][16];
936 char averagellabel[nINP][16];
937 
938 TTree *tTrainTest=new TTree("trainTEST","trainTEST");
939 #endif
940 
941 for (int i=0; i<nINP;i++){
942  #ifdef trainTEST
943  RMS[i]=0;
944  average[i]=0;
945  SD[i]=0;
946  RMSs[i]=0;
947  averages[i]=0;
948  SDs[i]=0;
949  RMSb[i]=0;
950  averageb[i]=0;
951  SDb[i]=0;
952  #endif
953  sprintf(xlabel[i],"x%i",i+1);
954  sprintf(xllabel[i],"%s/F",xlabel[i]);
955  tOriginal->SetBranchAddress(xlabel[i],&x[i]);
956  t->Branch(xlabel[i],&x[i],xllabel[i]);
957  #ifdef differentW
958  sprintf(wlabel[i],"w%i",i+1);
959  sprintf(wllabel[i],"%s/F",wlabel[i]);
960  tOriginal->SetBranchAddress(wlabel[i],&w[i]);
961  t->Branch(wlabel[i],w[i],wllabel[i]);
962  #endif
963  }
964 int size=0;
965 size=choice->size();
966 int nTSB=0;
967 nTSB=size/2;
968 for (int k=0;k<choice->size();k++){
969  //cout<<"choice di "<<k<<" : "<<(*choice)[k]<<endl;
970  tOriginal->GetEntry((*choice)[k]);
971  #ifdef trainTEST
972  for (int i=0;i<nINP;i++){
973  RMS[i]=RMS[i]+x[i]*x[i];
974  average[i]=average[i]+x[i];
975  if(k<nTSB){
976  RMSs[i]=RMSs[i]+x[i]*x[i];
977  averages[i]=averages[i]+x[i];
978  }
979  if(k>=nTSB){
980  RMSb[i]=RMSb[i]+x[i]*x[i];
981  averageb[i]=averageb[i]+x[i];
982  }
983  }
984  #endif
985  for (int u=0;u<k;u++) if( (*choice)[u]==(*choice)[k]) {cout<<"Error:same event taken 2 times"<<"u "<<u<<" choise "<<(*choice)[u]<<endl; exit(0);}
986 // h[k]=(*choice)[k];
987  t->Fill();
988  }
989 /*
990 for (int k=b;k<b+nTrainB;k++){
991  tOriginal->GetEntry(k);
992  t->Fill();
993 
994 */
995 //cout<<t->GetEntries();
996 int Ntest=choice->size();
997 #ifdef trainTEST
998 /*TGraph *RMSg=new TGraph();
999 TGraph *AVg=new TGraph();
1000 TGraph *SDg=new TGraph();
1001 */
1002 TH2D *RMShs=new TH2D("RMShs","RMShs",NDIM,0,NDIM,NDIM,0,NDIM);
1003 TH2D *AVhs=new TH2D("AVhs","Avhs",NDIM,0,NDIM,NDIM,0,NDIM);
1004 TH2D *SDhs=new TH2D("SDhs","SDhs",NDIM,0,NDIM,NDIM,0,NDIM);
1005 TH2D *RMShb=new TH2D("RMShb","RMShb",NDIM,0,NDIM,NDIM,0,NDIM);
1006 TH2D *AVhb=new TH2D("AVhb","Avhb",NDIM,0,NDIM,NDIM,0,NDIM);
1007 TH2D *SDhb=new TH2D("SDhb","SDhb",NDIM,0,NDIM,NDIM,0,NDIM);
1008 TH2D *RMSh=new TH2D("RMSh","RMSh",NDIM,0,NDIM,NDIM,0,NDIM);
1009 TH2D *AVh=new TH2D("AVh","Avh",NDIM,0,NDIM,NDIM,0,NDIM);
1010 TH2D *SDh=new TH2D("SDh","SDh",NDIM,0,NDIM,NDIM,0,NDIM);
1011 //TH1D *RMSh=new TH1D("RMSh","RMSh",nINP,0,nINP-1);
1012 //TH1D *AVh=new TH1D("AVh","Avh",nINP,0,nINP-1);
1013 //TH1D *SDh=new TH1D("SDh","SDh",nINP,0,nINP-1);
1014 RMSh->SetStats(0);
1015 SDh->SetStats(0);
1016 AVh->SetStats(0);
1017 SDhb->SetStats(0);
1018 AVhb->SetStats(0);
1019 RMShb->SetStats(0);
1020 SDhs->SetStats(0);
1021 AVhs->SetStats(0);
1022 RMShs->SetStats(0);
1023 
1024 
1025 
1026 for (int i=0;i<nINP;i++){
1027  RMSs[i]=RMSs[i]/Ntest*2;
1028  averages[i]=averages[i]/Ntest*2;
1029  SDs[i]=RMSs[i]-pow(averages[i],2);
1030  SDs[i]=pow(SDs[i],0.5);
1031  RMSs[i]=pow(RMSs[i],0.5);
1032 
1033  cout << "SIGNAL: " << averages[i] << " " << RMSs[i] << " " << SDs[i] << endl;
1034 
1035  RMSb[i]=RMSb[i]/Ntest*2;
1036  averageb[i]=averageb[i]/Ntest*2;
1037  SDb[i]=RMSb[i]-pow(averageb[i],2);
1038  SDb[i]=pow(SDb[i],0.5);
1039  RMSb[i]=pow(RMSb[i],0.5);
1040 
1041  cout << "BKG: " << averageb[i] << " " << RMSb[i] << " " << SDb[i] << endl;
1042 
1043  RMS[i]=RMS[i]/Ntest;
1044  average[i]=average[i]/Ntest;
1045  SD[i]=RMS[i]-pow(average[i],2);
1046  SD[i]=pow(SD[i],0.5);
1047  RMS[i]=pow(RMS[i],0.5);
1048 
1049  cout << "ALL: " << average[i] << " " << RMS[i] << " " << SD[i] << endl;
1050 
1051  sprintf(SDlabels[i],"SDs%i",i+1);
1052  sprintf(SDllabels[i],"%s/F",SDlabels[i]);
1053  tTrainTest->Branch(SDlabels[i],&SDs[i],SDllabels[i]);
1054  sprintf(RMSlabels[i],"RMSs%i",i+1);
1055  sprintf(RMSllabels[i],"%s/F",RMSlabels[i]);
1056  tTrainTest->Branch(RMSlabels[i],&RMSs[i],RMSllabels[i]);
1057  sprintf(averagelabels[i],"averages%i",i+1);
1058  sprintf(averagellabels[i],"%s/F",averagelabels[i]);
1059  tTrainTest->Branch(averagelabels[i],&averages[i],averagellabels[i]);
1060  sprintf(SDlabelb[i],"SDb%i",i+1);
1061  sprintf(SDllabelb[i],"%s/F",SDlabelb[i]);
1062  tTrainTest->Branch(SDlabelb[i],&SDb[i],SDllabelb[i]);
1063  sprintf(RMSlabelb[i],"RMSb%i",i+1);
1064  sprintf(RMSllabelb[i],"%s/F",RMSlabelb[i]);
1065  tTrainTest->Branch(RMSlabelb[i],&RMSb[i],RMSllabelb[i]);
1066  sprintf(averagelabelb[i],"averageb%i",i+1);
1067  sprintf(averagellabelb[i],"%s/F",averagelabelb[i]);
1068  tTrainTest->Branch(averagelabelb[i],&averageb[i],averagellabelb[i]);
1069  sprintf(SDlabel[i],"SD%i",i+1);
1070  sprintf(SDllabel[i],"%s/F",SDlabel[i]);
1071  tTrainTest->Branch(SDlabel[i],&SD[i],SDllabel[i]);
1072  sprintf(RMSlabel[i],"RMS%i",i+1);
1073  sprintf(RMSllabel[i],"%s/F",RMSlabel[i]);
1074  tTrainTest->Branch(RMSlabel[i],&RMS[i],RMSllabel[i]);
1075  sprintf(averagelabel[i],"average%i",i+1);
1076  sprintf(averagellabel[i],"%s/F",averagelabel[i]);
1077  tTrainTest->Branch(averagelabel[i],&average[i],averagellabel[i]);
1078  /* RMSg->SetPoint(i,i,RMS[i]);
1079  AVg->SetPoint(i,i,average[i]);
1080  SDg->SetPoint(i,i,SD[i]);
1081  */
1082  int fi=0;
1083  int ti=0;
1084  ti=i/NDIM;
1085  fi=i-ti*NDIM;
1086 // cout<<"ti: "<<ti<<" fi: "<<fi<<endl;
1087  RMShs->Fill(ti,fi,RMSs[i]);
1088  AVhs->Fill(ti,fi,averages[i]);
1089  SDhs->Fill(ti,fi,SDs[i]);
1090  RMShb->Fill(ti,fi,RMSb[i]);
1091  AVhb->Fill(ti,fi,averageb[i]);
1092  SDhb->Fill(ti,fi,SDb[i]);
1093  RMSh->Fill(ti,fi,RMS[i]);
1094  AVh->Fill(ti,fi,average[i]);
1095  SDh->Fill(ti,fi,SD[i]);
1096 // RMSh->Fill(i,RMS[i]);
1097 // AVh->Fill(i,average[i]);
1098 // SDh->Fill(i,SD[i]);
1099  }
1100 TCanvas *cProp=new TCanvas ("TrainSet_Properties","TrainSet_Properties",0,0,900,600);
1101 cProp->Divide(3,3);
1102 cProp->cd(1);
1103 RMSh->SetTitle("RMS");
1104 RMSh->Draw("COLZ");
1105 cProp->cd(2);
1106 AVh->SetTitle("Average");
1107 AVh->Draw("COLZ");
1108 cProp->cd(3);
1109 SDh->SetTitle("SD");
1110 SDh->Draw("COLZ");
1111 cProp->cd(4);
1112 RMShs->SetTitle("RMS_s");
1113 RMShs->Draw("COLZ");
1114 cProp->cd(5);
1115 AVhs->SetTitle("Average_s");
1116 AVhs->Draw("COLZ");
1117 cProp->cd(6);
1118 SDhs->SetTitle("SD_s");
1119 SDhs->Draw("COLZ");
1120 cProp->cd(7);
1121 RMShb->SetTitle("RMS_b");
1122 RMShb->Draw("COLZ");
1123 cProp->cd(8);
1124 AVhb->SetTitle("Average_b");
1125 AVhb->Draw("COLZ");
1126 cProp->cd(9);
1127 SDhb->SetTitle("SD_b");
1128 SDhb->Draw("COLZ");
1129 
1130 TString Prop_name(IDname);
1131 TString Prop_nameroot(IDname);
1132 Prop_name.ReplaceAll(".root","Prop.png");
1133 Prop_nameroot.ReplaceAll(".root","Prop.root");
1134 cProp->SaveAs(Prop_name.Data());
1135 cProp->Print(Prop_nameroot.Data());
1136 cProp->Close();
1137 #endif
1138 int ent=t->GetEntries();
1139 if (choice->size()!=ent) cout<<"Error in Tree dimension: Entries new tree="<<t->GetEntries()<<" vector->size"<<choice->size()<<endl;
1140 
1141 //char nomefile[516];
1142 //sprintf(nomefile,"selectTREE/TreeTrain_size%i_s%i_b%i.root",choice->size(),s,b);
1143 TString NOME_FILE(IDname);
1144 NOME_FILE.ReplaceAll("PropertyGraphs/mlpNetwork","selectTREE/TreeTrain");
1145 TFile* ofile=new TFile(NOME_FILE.Data(),"RECREATE");
1146 t->SetDirectory(ofile);
1147 t->Write();
1148 ofile->Close();
1149 fOriginal->Close();
1150 
1151 #ifdef trainTEST
1152 /*TCanvas *matrixIndex_RMS=new TCanvas("matrixIndex_RMS","matrixIndex_RMS",0,0,600,600);
1153 tTrainTest->Draw("RMS");
1154 matrixIndex*/
1155 TFile* TrainTest= new TFile(IDname.Data(),"RECREATE");
1156 tTrainTest->Write();
1157 TrainTest->Close();
1158 #endif
1159 cout<<NOME_FILE.Data()<<endl;
1160 return NOME_FILE;
1161 }
1162 
1163 
char ofile[1024]
wavearray< double > t(hp.size())
#define minCC
Float_t * rho
biased null statistics
Definition: netevent.hh:112
int error
Definition: cwb_compile.C:43
wavearray< double > z
Definition: Test10.C:32
TString("c")
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
Long_t size
int j
Definition: cwb_net.C:28
i drho i
r add(tfmap, const_cast< char *>("hchannel"))
wavearray< double > w
Definition: Test1.C:27
wavearray< double > h
Definition: Regression_H1.C:25
bool log
Definition: WaveMDC.C:41
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
char output[256]
int k
Float_t * netcc
effective correlated SNR
Definition: netevent.hh:113
TFile * ifile
wavearray< double > yy
Definition: TestFrame5.C:12
TString select7Train_non_es(TString ORIGINAL_FILE, std::vector< int > *choice, TString IDname)
if[["$OSTYPE"=~ "linux"]]
Definition: config.sh:3
#define deltarho
sprintf(tfres,"(1/%g)x(%g) (sec)x(Hz)", 2 *df, df)
TString gfile
#define NCC
#define nIFO
#define NRHO
Int_t GetEntries()
Definition: netevent.cc:403
wavearray< double > y
Definition: Test10.C:31
#define deltacc
void cwbANN_purity_SoB(int nTrain, int nTrainBg, TString option, TString TREE_FILE, int q, int p, double rm=0., double cm=0., int lm=5, Int_t nepoch=700)
exit(0)
#define maxCC