Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
efficiencies.cxx
Go to the documentation of this file.
1 #include "TMVA/efficiencies.h"
2 
3 #include "TH2F.h"
4 #include "TFile.h"
5 #include "TIterator.h"
6 #include "TKey.h"
7 
8 void TMVA::plot_efficiencies(TString dataset, TFile* /*file*/, Int_t type , TDirectory* BinDir)
9 {
10  // input: - Input file (result from TMVA),
11  // - type = 1 --> plot efficiency(B) versus eff(S)
12  // = 2 --> plot rejection (B) versus efficiency (S)
13  // = 3 --> plot 1/eff(B) versus efficiency (S)
14 
15  Bool_t __PLOT_LOGO__ = kTRUE;
16  Bool_t __SAVE_IMAGE__ = kTRUE;
17 
18  // the coordinates
19  Float_t x1 = 0;
20  Float_t x2 = 1;
21  Float_t y1 = 0;
22  Float_t y2 = 0.8;
23 
24  // reverse order if "rejection"
25  if (type == 2) {
26  Float_t z = y1;
27  y1 = 1 - y2;
28  y2 = 1 - z;
29  // cout << "--- type==2: plot background rejection versus signal efficiency" << endl;
30  } else if (type == 3) {
31  y1 = 0;
32  y2 = -1; // will be set to the max found in the histograms
33 
34  } else {
35  // cout << "--- type==1: plot background efficiency versus signal efficiency" << endl;
36  }
37  // create canvas
38  TCanvas* c = new TCanvas( "c", "the canvas", 200, 0, 650, 500 );
39 
40  // global style settings
41  c->SetGrid();
42  c->SetTicks();
43 
44  // legend
45  Float_t x0L = 0.107, y0H = 0.899;
46  Float_t dxL = 0.457-x0L, dyH = 0.22;
47  if (type == 2) {
48  x0L = 0.15;
49  y0H = 1 - y0H + dyH + 0.07;
50  }
51  TLegend *legend = new TLegend( x0L, y0H-dyH, x0L+dxL, y0H );
52  //legend->SetTextSize( 0.05 );
53  legend->SetHeader( "MVA Method:" );
54  legend->SetMargin( 0.4 );
55 
56  TString xtit = "Signal efficiency";
57  TString ytit = "Background efficiency";
58  if (type == 2) ytit = "Background rejection";
59  if (type == 3) ytit = "1/(Background eff.)";
60  TString ftit = ytit + " versus " + xtit;
61 
62  TString hNameRef = "effBvsS";
63  if (type == 2) hNameRef = "rejBvsS";
64  if (type == 3) hNameRef = "invBeffvsSeff";
65 
66 
67  if (TString(BinDir->GetName()).Contains("multicut")){
68  ftit += " Bin: ";
69  ftit += (BinDir->GetTitle());
70  }
71 
72  TList xhists;
73  TList xmethods;
74  UInt_t xnm = TMVAGlob::GetListOfMethods( xmethods ,BinDir);
75  if (xnm==0){
76  cout << "ups .. no methods found in to plot ROC curve for ... give up" << endl;
77  return;
78  }
79  TIter xnext(&xmethods);
80  // loop over all methods
81  TKey *xkey;
82  while ((xkey = (TKey*)xnext())) {
83  TDirectory * mDir = (TDirectory*)xkey->ReadObj();
84  TList titles;
85  UInt_t ninst = TMVAGlob::GetListOfTitles(mDir,titles);
86  if (ninst==0) cout << "hmm... sorry, but this printout was supposed to be only to keep the compiler quite.. never supposed to happen :(" << endl;
87  TIter nextTitle(&titles);
88  TKey *titkey;
89  TDirectory *titDir;
90  while ((titkey = TMVAGlob::NextKey(nextTitle,"TDirectory"))) {
91  titDir = (TDirectory *)titkey->ReadObj();
92  TString methodTitle;
93  TMVAGlob::GetMethodTitle(methodTitle,titDir);
94  TIter nextKey( titDir->GetListOfKeys() );
95  TKey *hkey2;
96  while ((hkey2 = TMVAGlob::NextKey(nextKey,"TH1"))) {
97  TH1 *h = (TH1*)hkey2->ReadObj();
98  TString hname = h->GetName();
99  if (hname.Contains( hNameRef ) && hname.BeginsWith( "MVA_" )) {
100  if (type==3 && h->GetMaximum() > y2) y2 = h->GetMaximum()*1.1;
101  }
102  }
103  }
104  }
105 
106 
107  // draw empty frame
108  if(gROOT->FindObject("frame")!=0) gROOT->FindObject("frame")->Delete();
109  TH2F* frame = new TH2F( "frame", ftit, 500, x1, x2, 500, y1, y2 );
110  frame->GetXaxis()->SetTitle( xtit );
111  frame->GetYaxis()->SetTitle( ytit );
112  TMVAGlob::SetFrameStyle( frame, 1.0 );
113 
114  frame->Draw();
115 
116  Int_t color = 1;
117  Int_t nmva = 0;
118  TKey *key;
119 
120  TList hists;
121  TList methods;
122  UInt_t nm = TMVAGlob::GetListOfMethods( methods,BinDir );
123  if (nm==0){
124  cout << "ups .. no methods found in to plot ROC curve for ... give up" << endl;
125  return;
126  }
127  // TIter next(file->GetListOfKeys());
128  TIter next(&methods);
129 
130  // loop over all methods
131  while ((key = (TKey*)next())) {
132  TDirectory * mDir = (TDirectory*)key->ReadObj();
133  TList titles;
134  UInt_t ninst = TMVAGlob::GetListOfTitles(mDir,titles);
135  if (ninst==0) cout << "hmm... sorry, but this printout was supposed to be only to keep the compiler quite.. never supposed to happen :(" << endl;
136  TIter nextTitle(&titles);
137  TKey *titkey;
138  TDirectory *titDir;
139  while ((titkey = TMVAGlob::NextKey(nextTitle,"TDirectory"))) {
140  titDir = (TDirectory *)titkey->ReadObj();
141  TString methodTitle;
142  TMVAGlob::GetMethodTitle(methodTitle,titDir);
143  TIter nextKey( titDir->GetListOfKeys() );
144  TKey *hkey2;
145  while ((hkey2 = TMVAGlob::NextKey(nextKey,"TH1"))) {
146  TH1 *h = (TH1*)hkey2->ReadObj();
147  TString hname = h->GetName();
148  if (hname.Contains( hNameRef ) && hname.BeginsWith( "MVA_" )) {
149  h->SetLineWidth(3);
150  h->SetLineColor(color);
151  color++; if (color == 5 || color == 10 || color == 11) color++;
152  h->Draw("csame");
153  hists.Add(h);
154  nmva++;
155  }
156  }
157  }
158  }
159 
160  while (hists.GetSize()) {
161  TListIter hIt(&hists);
162  TH1* hist(0);
163  Double_t largestInt=-1;
164  TH1* histWithLargestInt(0);
165  while ((hist = (TH1*)hIt())!=0) {
166  Double_t integral = hist->Integral(1,hist->FindBin(0.9999));
167  if (integral>largestInt) {
168  largestInt = integral;
169  histWithLargestInt = hist;
170  }
171  }
172  if (histWithLargestInt == 0) {
173  cout << "ERROR - unknown hist \"histWithLargestInt\" --> serious problem in ROOT file" << endl;
174  break;
175  }
176  legend->AddEntry(histWithLargestInt,TString(histWithLargestInt->GetTitle()).ReplaceAll("MVA_",""),"l");
177  hists.Remove(histWithLargestInt);
178  }
179 
180  // rescale legend box size
181  // current box size has been tuned for 3 MVAs + 1 title
182  if (type == 1) {
183  dyH *= (1.0 + Float_t(nmva - 3.0)/4.0);
184  legend->SetY1( y0H - dyH );
185  }
186  else {
187  dyH *= (Float_t(TMath::Min(10,nmva) - 3.0)/4.0);
188  legend->SetY2( y0H + dyH);
189  }
190 
191  // redraw axes
192  frame->Draw("sameaxis");
193  legend->Draw("same");
194 
195  // ============================================================
196 
197  if (__PLOT_LOGO__) TMVAGlob::plot_logo();
198 
199  // ============================================================
200 
201  c->Update();
202 
203  TString fname = dataset+"/plots/" + hNameRef;
204  if (TString(BinDir->GetName()).Contains("multicut")){
205  TString fprepend(BinDir->GetName());
206  fprepend.ReplaceAll("multicutMVA_","");
207  fname = dataset+"plots/" + fprepend + "_" + hNameRef;
208  }
209  if (__SAVE_IMAGE__) TMVAGlob::imgconv( c, fname );
210 
211  return;
212 }
213 
214 void TMVA::efficiencies(TString dataset, TString fin , Int_t type , Bool_t useTMVAStyle )
215 {
216  // argument: type = 1 --> plot efficiency(B) versus eff(S)
217  // type = 2 --> plot rejection (B) versus efficiency (S)
218 
219  // set style and remove existing canvas'
220  TMVAGlob::Initialize( useTMVAStyle );
221 
222  // checks if file with name "fin" is already open, and if not opens one
223  TFile* file = TMVAGlob::OpenFile( fin );
224 
225  plot_efficiencies(dataset, file, type, file->GetDirectory(dataset.Data()));
226 
227  return;
228 }
229