Logo ROOT   6.14/05
Reference Guide
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 
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition: TAttLine.h:43
virtual Double_t GetMaximum(Double_t maxval=FLT_MAX) const
Return maximum value smaller than maxval of bins in the range, unless the value has been overridden b...
Definition: TH1.cxx:7844
void imgconv(TCanvas *c, const TString &fname)
Definition: tmvaglob.cxx:212
virtual TList * GetListOfKeys() const
Definition: TDirectory.h:150
This class displays a legend box (TPaveText) containing several legend entries.
Definition: TLegend.h:23
float Float_t
Definition: RtypesCore.h:53
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:687
TFile * OpenFile(const TString &fin)
Definition: tmvaglob.cxx:192
virtual void Draw(Option_t *option="")
Draw this legend with its current attributes.
Definition: TLegend.cxx:423
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:47
#define gROOT
Definition: TROOT.h:410
void SetFrameStyle(TH1 *frame, Float_t scale=1.0)
Definition: tmvaglob.cxx:77
Basic string class.
Definition: TString.h:131
Short_t Min(Short_t a, Short_t b)
Definition: TMathBase.h:168
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
void SetMargin(Float_t margin)
Definition: TLegend.h:69
TKey * NextKey(TIter &keyIter, TString className)
Definition: tmvaglob.cxx:357
Iterator of linked list.
Definition: TList.h:197
virtual void SetY2(Double_t y2)
Set the Y2 value.
Definition: TPave.cxx:676
static const double x2[5]
virtual void SetGrid(Int_t valuex=1, Int_t valuey=1)
Definition: TPad.h:327
void GetMethodTitle(TString &name, TKey *ikey)
Definition: tmvaglob.cxx:341
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:24
A doubly linked list.
Definition: TList.h:44
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
void Initialize(Bool_t useTMVAStyle=kTRUE)
Definition: tmvaglob.cxx:176
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2974
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:250
virtual TObject * Remove(TObject *obj)
Remove object from the list.
Definition: TList.cxx:818
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition: TString.h:610
unsigned int UInt_t
Definition: RtypesCore.h:42
TAxis * GetYaxis()
Definition: TH1.h:316
void plot_logo(Float_t v_scale=1.0, Float_t skew=1.0)
Definition: tmvaglob.cxx:263
virtual TDirectory * GetDirectory(const char *apath, Bool_t printError=false, const char *funcname="GetDirectory")
Find a directory named "apath".
static constexpr double nm
#define h(i)
Definition: RSha256.hxx:106
The Canvas class.
Definition: TCanvas.h:31
virtual void SetY1(Double_t y1)
Set the Y1 value.
Definition: TPave.cxx:663
static const double x1[5]
UInt_t GetListOfMethods(TList &methods, TDirectory *dir=0)
Definition: tmvaglob.cxx:583
void efficiencies(TString dataset, TString fin="TMVA.root", Int_t type=2, Bool_t useTMVAStyle=kTRUE)
double Double_t
Definition: RtypesCore.h:55
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition: TLegend.cxx:330
Describe directory structure in memory.
Definition: TDirectory.h:34
int type
Definition: TGX11.cxx:120
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:619
The TH1 histogram class.
Definition: TH1.h:56
virtual void SetTicks(Int_t valuex=1, Int_t valuey=1)
Definition: TPad.h:347
you should not use this method at all Int_t Int_t z
Definition: TRolke.cxx:630
void plot_efficiencies(TString dataset, TFile *file, Int_t type=2, TDirectory *BinDir=0)
virtual TObject * ReadObj()
To read a TObject* from the file.
Definition: TKey.cxx:722
virtual void Add(TObject *obj)
Definition: TList.h:87
Definition: file.py:1
#define c(i)
Definition: RSha256.hxx:101
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2248
virtual Int_t GetSize() const
Return the capacity of the collection, i.e.
Definition: TCollection.h:182
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:164
THist< 2, float, THistStatContent, THistStatUncertainty > TH2F
Definition: THist.hxx:291
const Bool_t kTRUE
Definition: RtypesCore.h:87
TAxis * GetXaxis()
Get the behaviour adopted by the object about the statoverflows. See EStatOverflows for more informat...
Definition: TH1.h:315
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48
const char * Data() const
Definition: TString.h:364
UInt_t GetListOfTitles(TDirectory *rfdir, TList &titles)
Definition: tmvaglob.cxx:636
virtual void SetHeader(const char *header="", Option_t *option="")
Sets the header, which is the "title" that appears at the top of the legend.
Definition: TLegend.cxx:1099