Logo coherent WaveBurst  
Library Reference Guide
Logo
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TunePPCutsTool.C
Go to the documentation of this file.
1 /*
2 # Copyright (C) 2019 Gabriele Vedovato
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17 
18 
19 // ---------------------------------------------------------------------------------
20 // INFOS
21 // ---------------------------------------------------------------------------------
22 
23 // This macro is used to tune the PP cut thresholds
24 //
25 // how to use - Ex: root -l -b TunePPCutsTool_Config.C 'TunePPCutsTool.C(100,2000,100,30,"output_rnd_walk_cuts.out")'
26 //
27 // where output_rnd_walk_cuts.out is the output random walk cut values
28 //
29 // where TunePPCutsTool_Config.C is the configuration file. For example:
30 //
31 
32 /*
33 {
34  #include <O3/SEARCHES/OFFLINE/BBH/LH/PP_Cuts.hh>
35 
36  #define IFILE_BKG "wave_O3_K15_C01_LH_BBH_BKG_xrun2.M1.V_hvetoLH.C_rho1_gt_6.root"
37  #define IFILE_SIM "wave_O3_K15_C01_LH_BBH_SIM_BBH_BROAD_ISOTROPIC_xrun1.M1.V_hvetoLH.root"
38 
39  #define BKG_LIVE_TIME 35266962627.00 // non-zero lags : 77934 lags - 35266962627.00 sec = 1118.3 years
40 
41  // definition of the basic selection cuts used in O3b run
42  TCut bin_cut_standard = bin1_cut;
43  TCut bin_cut_basic = TCut("bin_cut_basic",(dqveto+lveto_cut).GetTitle());
44 
45  #define PP_CUTS " \n\
46  # \n\
47  # name cmp mean sigma min max enabled_tune \n\
48  # \n\
49  netcc[0] > 0.7 0.1 0.5 0.9 1 \n\
50  netcc[2] > 0.7 0.1 0.5 0.9 1 \n\
51  norm > 2.5 1.0 1.0 5.0 1 \n\
52  Qveto[0] > 0.25 0.1 0.0 0.5 1 \n\
53  log10(penalty) < 0.2 0.1 0.1 0.5 1 \n\
54  frequency[0] > 60.0 10.0 40.0 80.0 1 \n\
55  frequency[0] < 300.0 50.0 150.0 350.0 1 \n\
56  chirp[1] > 1.0 3.0 0.5 5.5 1 \n\
57  chirp[1] < 70.0 10.0 50.0 90.0 1 \n\
58  #Qveto[2] > 0.0 1.0 0.0 5.0 1 \n\
59  "
60 }
61 */
62 
63 // ----------------------------------------------------------------------------
64 // includes
65 // ----------------------------------------------------------------------------
66 
67 #include <boost/any.hpp>
68 
69 // ----------------------------------------------------------------------------
70 // defines
71 // ----------------------------------------------------------------------------
72 
73 #define YEAR (24*3600*365.)
74 
75 #define nCUT_MAX 20 // number of PP cut max defined in the input config file
76 
77 #define nBKG_MAX 10 // number of loudest background listed
78 
79 #define nIFAR 14 // number of ifar factor used to print inital/final detections
80 
81 #define SIGMA_FACTOR 1. // factor used to rescale input sigma
82 
83 #define CHECK_DNEVT_VS_IFAR // enable dnevt vs IFAR check -> the increment of detected events must be >=0 for each IFAR
84  // see CheckIncrementalDetectionsVsIFAR function
85 
86 // ----------------------------------------------------------------------------
87 // global variables
88 // ----------------------------------------------------------------------------
89 
90 int gNBKG; // number of background events for the correnspondig IFAR
91 int gNCTRY; // number of random trials for central tuning
92 int gNIFTRY; // number of random trials for inital/final tuning
93 int gNCUT; // number of PP cuts
94 int gNMDC; // readed from the input mdc files
95 
96 TString gCUT_NAME[nCUT_MAX]; // PP cut name
97 TString gCUT_CMP[nCUT_MAX]; // compare symbol '< , >, ...'
98 double gCUT_MEA[nCUT_MAX]; // tuning mean threshold (= standard PP cut threshold)
99 double gCUT_SIG[nCUT_MAX]; // tuning sigma threshold
100 double gCUT_MIN[nCUT_MAX]; // tuning min threshold
101 double gCUT_MAX[nCUT_MAX]; // tuning max threshold
102 
103 TCut gZL_CUT; // zero lag selection cut
104 
105 TString gOFNAME; // file name used to output the random walk of cut theresholds
106 
107 // ----------------------------------------------------------------------------
108 // functions
109 // ----------------------------------------------------------------------------
110 
111 int GetSelectedEvents(TTree* tree_BKG, TTree* tree_SIM, TString user_cut, int ncut, double* cut_thr, double& rho_thr);
112 void PrintLoudest(TTree* tree_BKG, TString user_cut, int ncut, double* cut_thr, double rho_thr, TString label="", int nLoudest=nBKG_MAX);
113 int ReadCutList(std::stringstream* in, TString* name, TString* cmp, double* mean, double* sigma, double* min, double* max);
114 void PrintPPCuts(int nevt_new, int nevt_old, int ncut, double* cut_thr, double rho_thri, TString user_cut, bool bppcuts, bool beff, bool bbcut);
115 void DumpIncrementalDetectionsVsIFAR(TTree* tree_BKG, TTree* tree_SIM, TString user_cut, double* cut_thr, TString ofname, bool app);
116 bool CheckIncrementalDetectionsVsIFAR(TTree* tree_BKG, TTree* tree_SIM, TString user_cut, double* cut_thr);
117 void PrintCovariance(TTree* tree, bool norm=true);
118 void DumpPPCuts(TString ofname, int ncut, double* cut_thr, bool app);
119 
120 void TunePPCutsTool(double IFAR=10.0, int nCTRY=1000, int nIFTRY=200, int seed=0, TString ofname="") {
121 //
122 // IFAR : input IFAR used to tunong the PP cut thresholds
123 // nCTRY : input trials for central tuning
124 // nIFTRY : input trials for initial and final tuning
125 // seed : seed used for random number generator (optional)
126 // ofname : file name used to output the random walk of cut theresholds, id ="" the output file is disabled
127 // the extension must be '.out'
128 //
129 
130  // check ofname extension
131  if(ofname!="") {
132  if(ofname.EndsWith(".out")) {
133  } else {
134  cout << endl << "TunePPCutsTool.C - Error : ofname extension must be '.out' !!!" << endl << endl;exit(1);
135  }
136  }
137 
138  gOFNAME = ofname.ReplaceAll(".out",TString::Format("_IFAR_%.2f.out",IFAR));
139  gNBKG = BKG_LIVE_TIME/IFAR/YEAR;
140  gNCTRY = nCTRY;
141  gNIFTRY = nIFTRY;
142 
143  if(gNBKG==0) {cout << endl << "IFAR too high -> nBKG = 0, macro aborted" << endl << endl;exit(1);}
144 
145  cout << endl;
146  cout << "--------------------------------------------------------------------------------------------" << endl;
147  cout << " Input Parameters " << endl;
148  cout << "--------------------------------------------------------------------------------------------" << endl;
149  cout << endl;
150  cout << " IFAR = " << IFAR << " (years) ->" << "\tnBKG = " << gNBKG << "\tnCTRY = "
151  << gNCTRY << "\tnIFTRY = " << gNIFTRY << "\tseed = " << seed << endl;
152  cout << endl;
153  cout << " gOFNAME = " << gOFNAME << endl;
154  cout << endl;
155 
156  gErrorIgnoreLevel = kError; // disable tree selection warning messages
157 
158  // ---------------------------------------------------------------
159  // Open input background ROOT file
160  // ---------------------------------------------------------------
161 
162  TString ifile_BKG = IFILE_BKG;
163  cout << " " << ifile_BKG << endl;
164 
165  TFile *rfile_BKG = TFile::Open(ifile_BKG);
166  if(rfile_BKG==NULL) {cout << "Error opening file: " << ifile_BKG << endl;exit(1);}
167 
168  TTree* tree_BKG = (TTree *) gROOT->FindObject("waveburst");
169  if(tree_BKG==NULL) {cout << "waveburst tree not found !!!" << endl;exit(1);}
170 
171  // Get detector names
172  TString net="";
173  vector<TString> ifo;
174  TList* ifoList = tree_BKG->GetUserInfo();
175  detectorParams dParams[NIFO_MAX];
176  for (int n=0;n<ifoList->GetSize();n++) {
177  detector* pDetector = (detector*)ifoList->At(n);
178  dParams[n] = pDetector->getDetectorParams();
179  ifo.push_back(dParams[n].name);
180  net+=TString(dParams[n].name);
181  //pDetector->print();
182  }
183  if(ifo.size()==0) {
184  cout << "ifo names are not contained in the input wave root file: " << IFILE_BKG << endl;
185  gSystem->Exit(1);
186  }
187 
188  int nIFO = ifoList->GetSize(); // get number of detectors in the network
189 
190  //cout << "network = " << net << endl;
191 
192  // define zero lag cut
193  gZL_CUT = TCut("gZL_CUT",TString::Format("(lag[%d]==0) && (slag[%d]==0)",nIFO,nIFO).Data());
194 
195  // ---------------------------------------------------------------
196  // Open input simulation ROOT file
197  // ---------------------------------------------------------------
198 
199  TString ifile_SIM = IFILE_SIM;
200  cout << " " << ifile_SIM << endl;
201 
202  TFile *rfile_SIM = TFile::Open(ifile_SIM);
203  if(rfile_SIM==NULL) {cout << "Error opening file: " << ifile_SIM << endl;exit(1);}
204 
205  TTree* tree_SIM = (TTree *) gROOT->FindObject("waveburst");
206  if(tree_SIM==NULL) {cout << "waveburst tree not found !!!" << endl;exit(1);}
207 
208  // ---------------------------------------------------------------
209  // Open input mdc ROOT file
210  // ---------------------------------------------------------------
211 
212  TString ifile_MDC = IFILE_SIM;
213  ifile_MDC.ReplaceAll("wave_","mdc_");
214  cout << " " << ifile_MDC << endl;
215 
216  TFile *rfile_MDC = TFile::Open(ifile_MDC);
217  if(rfile_MDC==NULL) {cout << "Error opening file: " << ifile_MDC << endl;exit(1);}
218 
219  TTree* tree_MDC = (TTree *) gROOT->FindObject("mdc");
220  if(tree_MDC==NULL) {cout << "mdc tree not found !!!" << endl;exit(1);}
221 
222  // retrive the number of injections
223  gNMDC = tree_MDC->GetEntries();
224  cout << endl << " gNMDC = " << gNMDC << " (injections)" << endl << endl;
225 
226  // ---------------------------------------------------------------
227  // clone tree -> memory resident -> faster
228  // ---------------------------------------------------------------
229 
230  gROOT->cd(0);
231  TTree* mtree_BKG = tree_BKG->CloneTree();
232  TTree* mtree_SIM = tree_SIM->CloneTree();
233 
234  // ---------------------------------------------------------------
235  // read cut list
236  // ---------------------------------------------------------------
237 
238  std::stringstream pp_cuts(PP_CUTS);
240 
241  TString user_cut = bin_cut_basic.GetTitle();
242 
243  // print standard PP cut string
244  cout << endl << " bin1_cut_standard ..." << endl << endl;
245  cout << " -> " << bin_cut_standard.GetTitle() << endl << endl;
246  cout << endl << " bin1_cut_basic ..." << endl << endl;
247  cout << " -> " << bin_cut_basic.GetTitle() << endl << endl;
248 
249  bool answer = CWB::Toolbox::question("do you want to continue ? ");
250  if(!answer) exit(1);
251 
252  double cut_thr[nCUT_MAX];
253  double cut_mean[nCUT_MAX];
254  double cut_sigma[nCUT_MAX];
255  double rho_thr;
256 
257  // init cut_mean, cut_sigma
258  for(int i=0;i<gNCUT;i++) {
259  cut_mean[i] = gCUT_MEA[i];
260  cut_sigma[i] = gCUT_SIG[i]*SIGMA_FACTOR;
261  }
262 
263  // ---------------------------------------------------------------
264  // print covariance tree_BKG
265  // ---------------------------------------------------------------
266 
267  cout << endl << " background covariance matrix ..." << endl << endl;
268  PrintCovariance(tree_BKG);
269  cout << endl << " simulation covariance matrix ..." << endl << endl;
270  PrintCovariance(tree_SIM);
271 
272  answer = CWB::Toolbox::question("do you want to continue ? ");
273  if(!answer) exit(1);
274 
275  // ---------------------------------------------------------------
276  // print detections/loudest for standard PP cuts
277  // ---------------------------------------------------------------
278 
279  int nevt_std = GetSelectedEvents(mtree_BKG, mtree_SIM, bin_cut_standard.GetTitle(), 0, NULL, rho_thr);
280  cout << endl << " nevt standard = " << nevt_std << endl << endl;
281  if(nevt_std==0) {cout << endl << " Error - input IFAR is too low ..." << endl << endl;exit(1);}
282  PrintPPCuts(nevt_std, nevt_std, gNCUT, gCUT_MEA, rho_thr, user_cut, false, true, false);
283 
284  // ---------------------------------------------------------------
285  // tune PP cuts
286  // ---------------------------------------------------------------
287 
288  if(gOFNAME!="") DumpPPCuts(gOFNAME, gNCUT, cut_mean, false);
289  if(gOFNAME!="") DumpPPCuts(gOFNAME, gNCUT, cut_mean, true);
290 
291  if(seed>=0) gRandom->SetSeed(seed);
292 
293  double cut_thr_max[nCUT_MAX];
294  double rho_thr_max=0.0;
295  int nevt_max=nevt_std;
296 
297  // INITIAL TUNING
298 
299  // generate random threshold values around the cut_thr_max (in the min:max range) individually for each cut
300  cout << endl << "Initial tuning ..." << endl << endl;
301  if(gOFNAME!="") DumpPPCuts(gOFNAME, 0, NULL, true); // dump blank line
302  for(int k=0;k<gNCUT;k++) cut_thr_max[k]=cut_mean[k];
303  for(int i=0;i<gNCUT;i++) {
304  if(gCUT_SIG[i]==0) continue; // skip fixed cuts
305  cout << " " << i << " -> " << gCUT_NAME[i] << " ..." << endl;
306  for(int k=0;k<gNCUT;k++) cut_thr[k]=cut_thr_max[k];
307  for(int j=0;j<gNIFTRY;j++) {
308  cut_thr[i]=gRandom->Uniform(gCUT_MIN[i],gCUT_MAX[i]);
309 
310  int nevt_new = GetSelectedEvents(mtree_BKG, mtree_SIM, user_cut, gNCUT, cut_thr, rho_thr);
311 #ifdef CHECK_DNEVT_VS_IFAR
312  bool check_dnevt = (nevt_new>nevt_max) ? CheckIncrementalDetectionsVsIFAR(tree_BKG, tree_SIM, user_cut, cut_thr) : true;
313 #else
314  bool check_dnevt = true;
315 #endif
316  if(nevt_new>nevt_max && check_dnevt==true) {
317 
318  cout << endl << " " << gCUT_NAME[i] << " : standard = " << gCUT_MEA[i] << " -> tuned = " << cut_thr[i] << endl;
319  PrintPPCuts(nevt_new, nevt_std, gNCUT, cut_thr, rho_thr, user_cut, false, true, false);
320 
321  // update cut_mean
322  for(int i=0;i<gNCUT;i++) cut_mean[i]=cut_thr[i];
323 
324  // update max values
325  for(int i=0;i<gNCUT;i++) cut_thr_max[i]=cut_thr[i];
326  rho_thr_max=rho_thr;
327  nevt_max=nevt_new;
328 
329  if(gOFNAME!="") DumpPPCuts(gOFNAME, gNCUT, cut_mean, true);
330  }
331  }
332  }
333  if(gOFNAME!="") DumpPPCuts(gOFNAME, 0, NULL, true); // dump blank line
334 
335  // CENTRAL TUNING
336 
337  // generate random threshold values around the cut_thr_max (in the min:max range) globaly for all cuts
338  cout << endl << "Central tuning ..." << endl << endl;
339  for(int i=0;i<gNCTRY;i++) {
340  if(i%1000==0) cout << "Loop ->\t" << i << " / " << gNCTRY << endl;
341 
342  for(int i=0;i<gNCUT;i++) {
343  cut_thr[i]=gRandom->Gaus(cut_mean[i],cut_sigma[i]);
344  if(cut_thr[i]<gCUT_MIN[i]) cut_thr[i]=gCUT_MIN[i];
345  if(cut_thr[i]>gCUT_MAX[i]) cut_thr[i]=gCUT_MAX[i];
346  }
347 
348  int nevt_new = GetSelectedEvents(mtree_BKG, mtree_SIM, user_cut, gNCUT, cut_thr, rho_thr);
349 #ifdef CHECK_DNEVT_VS_IFAR
350  bool check_dnevt = (nevt_new>nevt_max) ? CheckIncrementalDetectionsVsIFAR(tree_BKG, tree_SIM, user_cut, cut_thr) : true;
351 #else
352  bool check_dnevt = true;
353 #endif
354  if(nevt_new>nevt_max && check_dnevt==true) {
355 
356  PrintPPCuts(nevt_new, nevt_std, gNCUT, cut_thr, rho_thr, user_cut, false, true, false);
357 
358  // update cut_mean
359  for(int i=0;i<gNCUT;i++) cut_mean[i]=cut_thr[i];
360 
361  // update max values
362  for(int i=0;i<gNCUT;i++) cut_thr_max[i]=cut_thr[i];
363  rho_thr_max=rho_thr;
364  nevt_max=nevt_new;
365 
366  if(gOFNAME!="") DumpPPCuts(gOFNAME, gNCUT, cut_mean, true);
367  }
368  }
369 
370  // FINAL TUNING
371 
372  // generate random threshold values around the cut_thr_max (in the min:max range) individually for each cut
373  cout << endl << "Final tuning ..." << endl << endl;
374  if(gOFNAME!="") DumpPPCuts(gOFNAME, 0, NULL, true); // dump blank line
375  for(int i=0;i<gNCUT;i++) {
376  if(gCUT_SIG[i]==0) continue; // skip fixed cuts
377  cout << " " << i << " -> " << gCUT_NAME[i] << " ..." << endl;
378  for(int k=0;k<gNCUT;k++) cut_thr[k]=cut_thr_max[k];
379  for(int j=0;j<gNIFTRY;j++) {
380  cut_thr[i]=gRandom->Uniform(gCUT_MIN[i],gCUT_MAX[i]);
381 
382  int nevt_new = GetSelectedEvents(mtree_BKG, mtree_SIM, user_cut, gNCUT, cut_thr, rho_thr);
383 #ifdef CHECK_DNEVT_VS_IFAR
384  bool check_dnevt = (nevt_new>nevt_max) ? CheckIncrementalDetectionsVsIFAR(tree_BKG, tree_SIM, user_cut, cut_thr) : true;
385 #else
386  bool check_dnevt = true;
387 #endif
388  if(nevt_new>nevt_max && check_dnevt==true) {
389 
390  cout << endl << " " << gCUT_NAME[i] << " : standard = " << gCUT_MEA[i] << " -> tuned = " << cut_thr[i] << endl;
391  PrintPPCuts(nevt_new, nevt_std, gNCUT, cut_thr, rho_thr, user_cut, false, true, false);
392 
393  // update cut_mean
394  for(int i=0;i<gNCUT;i++) cut_mean[i]=cut_thr[i];
395 
396  // update max values
397  for(int i=0;i<gNCUT;i++) cut_thr_max[i]=cut_thr[i];
398  rho_thr_max=rho_thr;
399  nevt_max=nevt_new;
400 
401  if(gOFNAME!="") DumpPPCuts(gOFNAME, gNCUT, cut_mean, true);
402  }
403  }
404  }
405  if(gOFNAME!="") DumpPPCuts(gOFNAME, 0, NULL, true); // dump blank line
406 
407  // if no improvements we use the input mean PP cut thresholds
408  if(rho_thr_max==0.0) {
409  rho_thr_max=rho_thr;
410  for(int i=0;i<gNCUT;i++) cut_thr_max[i]=gCUT_MEA[i];
411  }
412 
413  // ---------------------------------------------------------------
414  // print detections/loudest for tuned PP cuts
415  // ---------------------------------------------------------------
416 
417  cout << endl;
418  cout << "-------------------------------------------------------------------------" << endl;
419  cout << "IFAR = " << IFAR << " (years) " << "\tnBKG = " << gNBKG << endl;
420  cout << "nCTRY = " << gNCTRY << "\tnIFTRY = " << gNIFTRY << endl;
421  cout << "-------------------------------------------------------------------------" << endl;
422  cout << endl;
423 
424  PrintLoudest(tree_BKG, bin_cut_standard.GetTitle(), 0, NULL, rho_thr, "standard", nBKG_MAX); // loudest background with standard PP cuts
425  PrintLoudest(tree_BKG, user_cut, gNCUT, cut_thr_max, rho_thr_max, "tuned", nBKG_MAX); // loudest background with tuned PP cuts
426  PrintPPCuts(nevt_max, nevt_std, gNCUT, cut_thr_max, rho_thr_max, user_cut, true, true, false); // list tuned PP cuts
427 
428  // ---------------------------------------------------------------
429  // Dump incremental detections: tuned/standard vs IFAR
430  // ---------------------------------------------------------------
431 
432  DumpIncrementalDetectionsVsIFAR(tree_BKG, tree_SIM, user_cut, cut_thr_max, gOFNAME, true);
433 
434  exit(0);
435 }
436 
437 int GetSelectedEvents(TTree* tree_BKG, TTree* tree_SIM, TString user_cut, int ncut, double* cut_thr, double& rho_thr) {
438 
439  TString sel="rho[1]";
440 
441  // make cut string
442  TString cut="";
443  if(user_cut!="") cut = TString::Format("(%s)",user_cut.Data());
444  if(cut!="" && ncut>0) cut = cut + " && ";
445  for(int i=0;i<ncut;i++) {
446  TString sAND = i==ncut-1 ? "" : "&&";
447  cut = cut + TString::Format("(%s%s%f)%s",gCUT_NAME[i].Data(),gCUT_CMP[i].Data(),cut_thr[i],sAND.Data());
448  }
449 
450  TString cut_bkg = cut + TString::Format(" && !(%s)",gZL_CUT.GetTitle());
451  tree_BKG->Draw(sel, cut_bkg, "goff");
452  int nBKG = (Int_t)tree_BKG->GetSelectedRows();
453 
454  int ndet_SIM=0;
455  if(nBKG>gNBKG) {
456 
457  // get rho[1] @ selected IFAR -> @ selected gNBKG
458  int *index = new Int_t[nBKG];
459  double* value = tree_BKG->GetV1();
460  TMath::Sort(nBKG,value,index,true);
461  rho_thr = value[index[gNBKG]];
462  delete [] index;
463 
464  cut = cut + TString::Format(" && rho[1]>%f",rho_thr);
465  tree_SIM->Draw(sel,cut,"goff");
466  ndet_SIM = (Int_t)tree_SIM->GetSelectedRows();
467  }
468 
469  return ndet_SIM;
470 }
471 
472 void PrintLoudest(TTree* tree_BKG, TString user_cut, int ncut, double* cut_thr, double rho_thr, TString label, int nLoudest) {
473 
474  // make cut string
475  TString cut="";
476  if(user_cut!="") cut = TString::Format("(%s)",user_cut.Data());
477  if(cut!="" && ncut>0) cut = cut + " && ";
478  for(int i=0;i<ncut;i++) {
479  TString sAND = i==ncut-1 ? "" : "&&";
480  cut = cut + TString::Format("(%s%s%f)%s",gCUT_NAME[i].Data(),gCUT_CMP[i].Data(),cut_thr[i],sAND.Data());
481  }
482 
483  // print PP cuts string
484  cout << endl << label << " PP cuts string ..." << endl;
485  cout << endl << cut << endl << endl;
486 
487  // print Loudest background list
488  cout << endl << label << " Loudest background list ..." << endl;
489  TString cut_bkg = cut + TString::Format(" && !(%s)",gZL_CUT.GetTitle());
490  tree_BKG->Draw("rho[1]",cut_bkg.Data(),"goff");
491  int ndet_BKG = (Int_t)tree_BKG->GetSelectedRows();
492  int *index = new Int_t[ndet_BKG];
493  double* value = tree_BKG->GetV1();
494  TMath::Sort(ndet_BKG,value,index,true);
495  double rho_sup = value[index[0]];
496  cout << endl;
497  for(int i=0;i<nLoudest;i++) cout << "\t" << i << " -> \t" << value[index[i]] << endl;
498  cout << endl;
499  delete [] index;
500 }
501 
502 void PrintPPCuts(int nevt_new, int nevt_old, int ncut, double* cut_thr, double rho_thr, TString user_cut, bool bppcuts, bool beff, bool bppcut) {
503 
504  // print PP cuts
505  if(bppcuts) {
506  char sout[1024];
507  cout << endl << " PP cuts list ..." << endl << endl;
508  // print header
509  sprintf(sout,"%*s %*s %*s %*s %*s %*s", 25, "name", 5, "cmp", 10, "tuned", 10, "mean", 14, "mean-tuned", 12, "increment");
510  cout << endl << sout << endl << endl;
511  for(int i=0;i<ncut;i++) {
512  float dthr = 100*(cut_thr[i]-gCUT_MEA[i])/gCUT_MEA[i];
513  sprintf(sout,"%*s %*s %*.2f %*.2f %*.2f %*.1f %s", 25, gCUT_NAME[i].Data(), 5, gCUT_CMP[i].Data(), 10, cut_thr[i], 10, gCUT_MEA[i], 14, cut_thr[i]-gCUT_MEA[i], 10, dthr, "%");
514  cout << sout << endl;
515  }
516  }
517 
518  // print eff inprovement respect to the old PP cuts
519  if(beff) {
520  double dnevt = (nevt_new-nevt_old)/double(nevt_old);
521  cout << endl;
522  cout << " @ rho[1] > " << rho_thr << "\t -> nevt_new = " << nevt_new << "\t -> dnevt = " << 100.*dnevt << " %" << endl;
523  cout << endl;
524  }
525 
526  // print PP cut string
527  if(bppcut) {
528  // make cut string
529  TString cut="";
530  if(user_cut!="") cut = TString::Format(" (%s)",user_cut.Data());
531  if(cut!="" && ncut>0) cut = cut + " && ";
532  for(int i=0;i<ncut;i++) {
533  TString sAND = i==ncut-1 ? "" : "&&";
534  cut = cut + TString::Format("(%s%s%f)%s",gCUT_NAME[i].Data(),gCUT_CMP[i].Data(),cut_thr[i],sAND.Data());
535  }
536  cout << endl << cut << endl << endl;
537  }
538 }
539 
540 void DumpPPCuts(TString ofname, int ncut, double* cut_thr, bool app) {
541 
542  FILE *fp;
543  char mode[3];
544  if(app) strcpy(mode, "a"); else strcpy(mode, "w");
545 
546  if((fp = fopen(ofname, mode)) == NULL ) {
547  cout << " DumpPPCuts error: cannot open file " << ofname <<". \n";
548  return;
549  };
550 
551  char sout[1024];
552 
553  // dump header
554  sprintf(sout,"");
555  for(int i=0;i<ncut;i++) sprintf(sout,"%s %*s",sout,15,gCUT_NAME[i].Data());
556  //if(!app) cout << endl << sout << endl << endl;
557  if(!app) fprintf(fp,"\n%s\n\n",sout);
558 
559  // dump values
560  sprintf(sout,"");
561  for(int i=0;i<ncut;i++) sprintf(sout,"%s %*.4f",sout,15,cut_thr[i]);
562  //if(app) cout << sout << endl;
563  if(app) fprintf(fp,"%s\n",sout);
564 
565  // dump blank line
566  if(app && ncut==0) fprintf(fp,"\n");
567 
568  fclose(fp);
569 }
570 
571 void DumpIncrementalDetectionsVsIFAR(TTree* tree_BKG, TTree* tree_SIM, TString user_cut, double* cut_thr, TString ofname, bool app) {
572 
573  FILE *fp=NULL;
574  char mode[3];
575  if(app) strcpy(mode, "a"); else strcpy(mode, "w");
576  char sout[1024];
577 
578  if(ofname!="") {
579  if((fp = fopen(ofname, mode)) == NULL) {
580  cout << " DumpIncrementalDetectionsVsIFAR error: cannot open file " << ofname <<". \n";
581  return;
582  }
583  if(fp!=NULL) fprintf(fp,"\n\nDump incremental detections: tuned/standard vs IFAR\n\n");
584  }
585 
586  double ifar_min = 1;
587  double ifar_max = BKG_LIVE_TIME/YEAR/4;
588 
589  double difar = (ifar_max-ifar_min)/nIFAR;
590 
591  cout << endl;
592  cout << "Dump incremental detections: tuned/standard vs IFAR" << endl;
593  cout << endl;
594  double ifar = ifar_min;
595  int gNBKG_save = gNBKG;
596  for(int i=0;i<nIFAR;i++) {
597 
598  //double ifar = ifar_min + i*difar;
599  ifar *= 2;
600 
601  gNBKG = BKG_LIVE_TIME/ifar/YEAR;
602 
603  double rho_thr_std, rho_thr_tuned;
604 
605  int nevt_std = GetSelectedEvents(tree_BKG, tree_SIM, bin_cut_standard.GetTitle(), 0, NULL, rho_thr_std);
606  int nevt_tuned = GetSelectedEvents(tree_BKG, tree_SIM, user_cut, gNCUT, cut_thr, rho_thr_tuned);
607 
608  if(nevt_std!=0 && nevt_tuned!=0) { // if (nevt_std==0 || nevt_tuned==0) the minimum rho threshold is not sufficient
609 
610  // print eff inprovement respect to the standard PP cuts
611  double dnevt = (nevt_tuned-nevt_std)/double(nevt_std);
612  sprintf(sout," @ ifar = %.2f (@ rho(std)=%.2f -> %d bkg) -> nevt(std/tuned) = %d/%d (@ rho=%.2f) -> dnevt = %.2f %s", \
613  ifar, rho_thr_std, gNBKG, nevt_std, nevt_tuned, rho_thr_tuned, 100.*dnevt, "%");
614  cout << sout << endl;
615  if(fp!=NULL) fprintf(fp,"%s\n",sout);
616  }
617  if(gNBKG==1) break;
618  }
619  gNBKG = gNBKG_save; // restore gNBKG
620  cout << endl;
621 
622  if(fp!=NULL) {fprintf(fp,"\n");fclose(fp);}
623 }
624 
625 bool CheckIncrementalDetectionsVsIFAR(TTree* tree_BKG, TTree* tree_SIM, TString user_cut, double* cut_thr) {
626 //
627 // return false if not all detections (vs IFAR) dnevt are >=0
628 //
629 
630  double ifar_min = 1;
631  double ifar_max = BKG_LIVE_TIME/YEAR/4;
632 
633  double difar = (ifar_max-ifar_min)/nIFAR;
634 
635  bool check_dnevt=true;
636  double ifar = ifar_min;
637  int gNBKG_save = gNBKG;
638  for(int i=0;i<nIFAR;i++) {
639 
640  //double ifar = ifar_min + i*difar;
641  ifar *= 2;
642 
643  gNBKG = BKG_LIVE_TIME/ifar/YEAR;
644 
645  double rho_thr_std, rho_thr_tuned;
646 
647  int nevt_std = GetSelectedEvents(tree_BKG, tree_SIM, bin_cut_standard.GetTitle(), 0, NULL, rho_thr_std);
648  int nevt_tuned = GetSelectedEvents(tree_BKG, tree_SIM, user_cut, gNCUT, cut_thr, rho_thr_tuned);
649 
650  if(nevt_std!=0 && nevt_tuned!=0) { // if (nevt_std==0 || nevt_tuned==0) the minimum rho threshold is not sufficient
651  // check eff inprovement respect to the standard PP cuts
652  double dnevt = (nevt_tuned-nevt_std)/double(nevt_std);
653  if(dnevt<0) check_dnevt=false;
654  }
655  if(gNBKG==1 || check_dnevt==false) break;
656  }
657  gNBKG = gNBKG_save; // restore gNBKG
658 
659  return check_dnevt;
660 }
661 
662 int ReadCutList(std::stringstream* in, TString* name, TString* cmp, double* mean, double* sigma, double* min, double* max) {
663 
664  // Read Cuts file list
665 
666  int size=0;
667  char str[1024];
668  int fpos=0;
669  while(true) {
670  in->getline(str,1024);
671  if (!in->good()) break;
672  if(str[0] != '#' && str[0] != ' ') size++;
673  }
674  in->clear(ios::goodbit);
675  in->seekg(0, ios::beg);
676  if (size>nCUT_MAX) {cout << "ReadCutList Error - PP cuts > " << nCUT_MAX << endl;exit(1);}
677 
678  char sout[1024];
679  char xname[256], xcmp[256];
680  double xmean, xsigma, xmin, xmax;
681  int xenabled;
682  int ncut=0;
683  cout << endl << " Input cut list ..." << endl;
684  // print header
685  sprintf(sout,"%*s %*s %*s %*s %*s %*s %*s %*s", 3, "id" , 25, "name", 10, "cmp", 10, "mean", 10, "sigma", 10, "min", 10, "max", 15, "tune-enabled");
686  cout << endl << sout << endl << endl;
687  while(true) {
688  fpos=in->tellg();
689  in->getline(str,1024);
690  if (!in->good()) break;
691  in->seekg(fpos, ios::beg);
692  *in >> xname >> xcmp >> xmean >> xsigma >> xmin >> xmax >> xenabled;
693  if(in->good() && !(xname[0] == '#' || xname[0]=='\0' || xname[0] == ' ')) {
694  name[ncut] = xname;
695  cmp[ncut] = xcmp;
696  mean[ncut] = xmean;
697  sigma[ncut]= xenabled==0 ? 0 : xsigma; // if xenabled=0 than sigma=0 and parameter is fixed to xmean;
698  min[ncut] = xmin;
699  max[ncut] = xmax;
700  sprintf(sout,"%*d %*s %*s %*.2f %*.2f %*.2f %*.2f %*d", 3, ncut+1 , 25, xname, 10, xcmp, 10, xmean, 10, xsigma, 10, xmin, 10, xmax, 10, xenabled);
701  cout << sout << endl;
702  ncut++;
703  } else in->clear();
704  }
705  cout << endl;
706 
707  return ncut;
708 }
709 
710 void PrintCovariance(TTree* tree, bool norm) {
711 
712  int entries = tree->GetEntries();
713  double* value[nCUT_MAX];
714 
715  for(int i=0;i<gNCUT;i++) {
716  cout << i << " " << gCUT_NAME[i].Data() << endl;
717  tree->Draw(gCUT_NAME[i], "", "goff");
718  value[i] = new double[entries];
719  double* val = tree->GetV1();
720  for(int j=0;j<entries;j++) value[i][j]=val[j];
721  }
722 
723  TPrincipal pca(gNCUT, "ND");
724  double data[nCUT_MAX];
725 
726  for(int i=0;i<entries;i++) {
727  for(int j=0;j<gNCUT;j++) data[j]=value[j][i];
728  pca.AddRow(data);
729  }
730 
731  // get covariance matrix
732  TMatrixD* matrix = (TMatrixD*)pca.GetCovarianceMatrix();
733  //matrix->Print("%11.3g");
734 
735  // do normalization
736  TMatrixD nmatrix = *matrix;
737  for(int i=0;i<matrix->GetNrows();i++) {
738  for(int j=0;j<matrix->GetNcols();j++) {
739  nmatrix(i,j)/=sqrt((*matrix)(i,i) * (*matrix)(j,j));
740  nmatrix(i,j)*=100.;
741  }
742  }
743  //nmatrix.Print();
744 
745  if(norm) {
746  *matrix=nmatrix;
747  matrix->Print("f= %11.0f");
748  } else {
749  matrix->Print("f= %11.3g");
750  }
751 
752  cout << endl;
753  cout << " list of loudest correlation (>30%) ... " << endl;
754  cout << endl;
755  char sout[1024];
756  for(int i=0;i<matrix->GetNrows();i++) {
757  for(int j=0;j<matrix->GetNcols();j++) {
758  if(fabs((*matrix)(i,j))>30 && i!=j && gCUT_NAME[i]!=gCUT_NAME[j]) {
759  sprintf(sout," %*s vs %*s %*s = %*.2f %s", 35, gCUT_NAME[i].Data(), 15, gCUT_NAME[j].Data(), 15, " -> correlation", 7, (*matrix)(i,j), "%");
760  cout << sout << endl;
761  //cout << " " << gCUT_NAME[i] << "/" << gCUT_NAME[j] << "\t\tCorrelation = " << matrix(i,j) << " %" << endl;
762  }
763  }
764  }
765  cout << endl;
766 
767  for(int i=0;i<gNCUT;i++) delete [] value[i];
768 }
769 
#define SIGMA_FACTOR
TTree * tree
Definition: TimeSortTree.C:20
detectorParams getDetectorParams()
Definition: detector.cc:218
#define PP_CUTS
void DumpIncrementalDetectionsVsIFAR(TTree *tree_BKG, TTree *tree_SIM, TString user_cut, double *cut_thr, TString ofname, bool app)
double gCUT_MIN[nCUT_MAX]
par [0] value
double min(double x, double y)
Definition: eBBH.cc:31
par [0] name
int n
Definition: cwb_net.C:28
TString("c")
#define nBKG_MAX
void DumpPPCuts(TString ofname, int ncut, double *cut_thr, bool app)
#define BKG_LIVE_TIME
Long_t size
int j
Definition: cwb_net.C:28
i drho i
TCut bin_cut_basic
int gNMDC
char ifo[NIFO_MAX][8]
network ** net
NOISE_MDC_SIMULATION.
fprintf(stdout,"start=%f duration=%f rate=%f\, x.start(), x.size()/x.rate(), x.rate())
size_t mode
#define nIFO
char val[20]
char str[1024]
#define IFILE_BKG
TString label
Definition: MergeTrees.C:21
const int NIFO_MAX
Definition: wat.hh:22
char cut[512]
int gNIFTRY
#define nCUT_MAX
TString gCUT_NAME[nCUT_MAX]
int k
TString sel("slag[1]:slag[2]")
double sigma
void PrintPPCuts(int nevt_new, int nevt_old, int ncut, double *cut_thr, double rho_thri, TString user_cut, bool bppcuts, bool beff, bool bbcut)
void TunePPCutsTool(double IFAR=10.0, int nCTRY=1000, int nIFTRY=200, int seed=0, TString ofname="")
TCut gZL_CUT
int gNCUT
static bool question(TString question)
Definition: Toolbox.cc:4808
#define IFILE_SIM
double gCUT_MAX[nCUT_MAX]
TString gCUT_CMP[nCUT_MAX]
TString gOFNAME
void PrintLoudest(TTree *tree_BKG, TString user_cut, int ncut, double *cut_thr, double rho_thr, TString label="", int nLoudest=nBKG_MAX)
void PrintCovariance(TTree *tree, bool norm=true)
char answer[256]
int GetSelectedEvents(TTree *tree_BKG, TTree *tree_SIM, TString user_cut, int ncut, double *cut_thr, double &rho_thr)
ifstream in
#define YEAR
wavearray< int > index
double fabs(const Complex &x)
Definition: numpy.cc:55
bool CheckIncrementalDetectionsVsIFAR(TTree *tree_BKG, TTree *tree_SIM, TString user_cut, double *cut_thr)
double gCUT_SIG[nCUT_MAX]
strcpy(RunLabel, RUN_LABEL)
#define nIFAR
sprintf(tfres,"(1/%g)x(%g) (sec)x(Hz)", 2 *df, df)
int gNCTRY
int gNBKG
int ReadCutList(std::stringstream *in, TString *name, TString *cmp, double *mean, double *sigma, double *min, double *max)
double gCUT_MEA[nCUT_MAX]
fclose(ftrig)
exit(0)