Logo ROOT  
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
8void 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
214void 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
#define c(i)
Definition: RSha256.hxx:101
#define h(i)
Definition: RSha256.hxx:106
bool Bool_t
Definition: RtypesCore.h:63
int Int_t
Definition: RtypesCore.h:45
unsigned int UInt_t
Definition: RtypesCore.h:46
float Float_t
Definition: RtypesCore.h:57
double Double_t
Definition: RtypesCore.h:59
const Bool_t kTRUE
Definition: RtypesCore.h:100
Option_t Option_t TPoint TPoint const char x2
Option_t Option_t TPoint TPoint const char x1
Option_t Option_t TPoint TPoint const char y2
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
Option_t Option_t TPoint TPoint const char y1
#define gROOT
Definition: TROOT.h:404
The Canvas class.
Definition: TCanvas.h:23
virtual Int_t GetSize() const
Return the capacity of the collection, i.e.
Definition: TCollection.h:184
Describe directory structure in memory.
Definition: TDirectory.h:45
virtual TList * GetListOfKeys() const
Definition: TDirectory.h:214
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:54
TH1 is the base class of all histogram classes in ROOT.
Definition: TH1.h:58
TAxis * GetXaxis()
Get the behaviour adopted by the object about the statoverflows. See EStatOverflows for more informat...
Definition: TH1.h:319
TAxis * GetYaxis()
Definition: TH1.h:320
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition: TH1.cxx:3070
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:257
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:28
virtual TObject * ReadObj()
To read a TObject* from the file.
Definition: TKey.cxx:750
This class displays a legend box (TPaveText) containing several legend entries.
Definition: TLegend.h:23
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition: TLegend.cxx:331
void Draw(Option_t *option="") override
Draw this legend with its current attributes.
Definition: TLegend.cxx:424
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:1091
void SetMargin(Float_t margin)
Definition: TLegend.h:69
Iterator of linked list.
Definition: TList.h:191
A doubly linked list.
Definition: TList.h:38
void Add(TObject *obj) override
Definition: TList.h:81
TObject * Remove(TObject *obj) override
Remove object from the list.
Definition: TList.cxx:822
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:164
const char * GetName() const override
Returns name of object.
Definition: TNamed.h:47
const char * GetTitle() const override
Returns title of object.
Definition: TNamed.h:48
virtual void SetY1(Double_t y1) override
Set the Y1 value.
Definition: TPave.cxx:678
virtual void SetY2(Double_t y2) override
Set the Y2 value.
Definition: TPave.cxx:691
Basic string class.
Definition: TString.h:136
const char * Data() const
Definition: TString.h:369
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:692
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition: TString.h:615
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:624
static constexpr double nm
UInt_t GetListOfTitles(TDirectory *rfdir, TList &titles)
Definition: tmvaglob.cxx:643
void Initialize(Bool_t useTMVAStyle=kTRUE)
Definition: tmvaglob.cxx:176
TKey * NextKey(TIter &keyIter, TString className)
Definition: tmvaglob.cxx:364
void GetMethodTitle(TString &name, TKey *ikey)
Definition: tmvaglob.cxx:348
void plot_logo(Float_t v_scale=1.0, Float_t skew=1.0)
Definition: tmvaglob.cxx:270
TFile * OpenFile(const TString &fin)
Definition: tmvaglob.cxx:192
void SetFrameStyle(TH1 *frame, Float_t scale=1.0)
Definition: tmvaglob.cxx:77
UInt_t GetListOfMethods(TList &methods, TDirectory *dir=0)
Definition: tmvaglob.cxx:590
void imgconv(TCanvas *c, const TString &fname)
Definition: tmvaglob.cxx:212
void efficiencies(TString dataset, TString fin="TMVA.root", Int_t type=2, Bool_t useTMVAStyle=kTRUE)
void plot_efficiencies(TString dataset, TFile *file, Int_t type=2, TDirectory *BinDir=0)
Short_t Min(Short_t a, Short_t b)
Returns the smallest of a and b.
Definition: TMathBase.h:198
Definition: file.py:1