ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/LJMet/MultivariateAnalysis/macros/efficiencies.C
Revision: 1.2
Committed: Mon Apr 27 03:46:44 2009 UTC (16 years ago) by kukartse
Content type: text/plain
Branch: MAIN
Changes since 1.1: +2 -0 lines
Log Message:
a class and a script for pseudoexperiments added

File Contents

# User Rev Content
1 kukartse 1.1 #include "tmvaglob.C"
2    
3     void plot_efficiencies( TFile* file, Int_t type = 2, TDirectory* BinDir)
4     {
5     // input: - Input file (result from TMVA),
6     // - type = 1 --> plot efficiency(B) versus eff(S)
7     // = 2 --> plot rejection (B) versus efficiency (S)
8    
9     Bool_t __PLOT_LOGO__ = kTRUE;
10     Bool_t __SAVE_IMAGE__ = kTRUE;
11    
12     // the coordinates
13     Float_t x1 = 0;
14     Float_t x2 = 1;
15     Float_t y1 = 0;
16     Float_t y2 = 0.8;
17    
18     // reverse order if "rejection"
19     if (type == 2) {
20     Float_t z = y1;
21     y1 = 1 - y2;
22     y2 = 1 - z;
23     // cout << "--- type==2: plot background rejection versus signal efficiency" << endl;
24     }
25     else {
26     // cout << "--- type==1: plot background efficiency versus signal efficiency" << endl;
27     }
28     // create canvas
29     TCanvas* c = new TCanvas( "c", "the canvas", 200, 0, 650, 500 );
30    
31     // global style settings
32     c->SetGrid();
33     c->SetTicks();
34    
35     // legend
36     Float_t x0L = 0.107, y0H = 0.899;
37     Float_t dxL = 0.457-x0L, dyH = 0.22;
38     if (type == 2) {
39     x0L = 0.15;
40     y0H = 1 - y0H + dyH + 0.07;
41     }
42     TLegend *legend = new TLegend( x0L, y0H-dyH, x0L+dxL, y0H );
43     legend->SetTextSize( 0.05 );
44     legend->SetHeader( "MVA Method:" );
45     legend->SetMargin( 0.4 );
46    
47     TString xtit = "Signal efficiency";
48     TString ytit = "Background efficiency";
49     if (type == 2) ytit = "Background rejection";
50     TString ftit = ytit + " versus " + xtit;
51    
52     if (TString(BinDir->GetName()).Contains("multicut")){
53     ftit += " Bin: ";
54     ftit += (BinDir->GetTitle());
55     }
56    
57     // draw empty frame
58     if(gROOT->FindObject("frame")!=0) gROOT->FindObject("frame")->Delete();
59     TH2F* frame = new TH2F( "frame", ftit, 500, x1, x2, 500, y1, y2 );
60     frame->GetXaxis()->SetTitle( xtit );
61     frame->GetYaxis()->SetTitle( ytit );
62     TMVAGlob::SetFrameStyle( frame, 1.0 );
63    
64     frame->Draw();
65    
66     Int_t color = 1;
67     Int_t nmva = 0;
68     TKey *key, *hkey;
69    
70     TString hNameRef = "effBvsS";
71     if (type == 2) hNameRef = "rejBvsS";
72    
73     TList hists;
74     TList methods;
75     UInt_t nm = TMVAGlob::GetListOfMethods( methods );
76     // TIter next(file->GetListOfKeys());
77     TIter next(&methods);
78    
79     // loop over all methods
80     while (key = (TKey*)next()) {
81     TDirectory * mDir = (TDirectory*)key->ReadObj();
82     TList titles;
83     UInt_t ninst = TMVAGlob::GetListOfTitles(mDir,titles);
84     TIter nextTitle(&titles);
85     TKey *titkey;
86     TDirectory *titDir;
87     while ((titkey = TMVAGlob::NextKey(nextTitle,"TDirectory"))) {
88     titDir = (TDirectory *)titkey->ReadObj();
89     TString methodTitle;
90     TMVAGlob::GetMethodTitle(methodTitle,titDir);
91     TIter nextKey( titDir->GetListOfKeys() );
92     while ((hkey = TMVAGlob::NextKey(nextKey,"TH1"))) {
93     TH1 *h = (TH1*)hkey->ReadObj();
94     TString hname = h->GetName();
95     if (hname.Contains( hNameRef ) && hname.BeginsWith( "MVA_" )) {
96     h->SetLineWidth(3);
97     h->SetLineColor(color);
98     color++; if (color == 5 || color == 10 || color == 11) color++;
99     h->Draw("csame");
100 kukartse 1.2 cout << "Area under ROC: " << h.Integral("width") << endl;
101     cout << "Ratio over/under ROC: " << (1.0-h.Integral("width"))/h.Integral("width") << endl;
102 kukartse 1.1 hists.Add(h);
103     nmva++;
104     }
105     }
106     }
107     }
108    
109     while (hists.GetSize()) {
110     TListIter hIt(&hists);
111     TH1* hist(0);
112     Double_t largestInt=0;
113     TH1* histWithLargestInt(0);
114     while ((hist = (TH1*)hIt())!=0) {
115     Double_t integral = hist->Integral(1,hist->FindBin(0.9999));
116     if (integral>largestInt) {
117     largestInt = integral;
118     histWithLargestInt = hist;
119     }
120     }
121     legend->AddEntry(histWithLargestInt,TString(histWithLargestInt->GetTitle()).ReplaceAll("MVA_",""),"l");
122     hists.Remove(histWithLargestInt);
123     }
124    
125     // rescale legend box size
126     // current box size has been tuned for 3 MVAs + 1 title
127     if (type == 1) {
128     dyH *= (1.0 + Float_t(nmva - 3.0)/4.0);
129     legend->SetY1( y0H - dyH );
130     }
131     else {
132     dyH *= (Float_t(nmva - 3.0)/4.0);
133     legend->SetY2( y0H + dyH);
134     }
135    
136     // redraw axes
137     frame->Draw("sameaxis");
138     legend->Draw("same");
139    
140     // ============================================================
141    
142     if (__PLOT_LOGO__) TMVAGlob::plot_logo();
143    
144     // ============================================================
145    
146     c->Update();
147    
148     TString fname = "plots/" + hNameRef;
149     if (TString(BinDir->GetName()).Contains("multicut")){
150     TString fprepend(BinDir->GetName());
151     fprepend.ReplaceAll("multicutMVA_","");
152     fname = "plots/" + fprepend + "_" + hNameRef;
153     }
154     if (__SAVE_IMAGE__) TMVAGlob::imgconv( c, fname );
155    
156     return;
157     }
158    
159     void efficiencies( TString fin = "TMVA.root", Int_t type = 2, Bool_t useTMVAStyle = kTRUE )
160     {
161     // argument: type = 1 --> plot efficiency(B) versus eff(S)
162     // type = 2 --> plot rejection (B) versus efficiency (S)
163    
164     // set style and remove existing canvas'
165     TMVAGlob::Initialize( useTMVAStyle );
166    
167     // checks if file with name "fin" is already open, and if not opens one
168     TFile* file = TMVAGlob::OpenFile( fin );
169    
170     // check if multi-cut MVA or only one set of MVAs
171     Bool_t multiMVA=kFALSE;
172     TIter nextDir(file->GetListOfKeys());
173     TKey *key;
174     // loop over all directories and check if
175     // one contains the key word 'multicutMVA'
176     while ((key = (TKey*)nextDir())) {
177     TClass *cl = gROOT->GetClass(key->GetClassName());
178     if (!cl->InheritsFrom("TDirectory")) continue;
179     TDirectory *d = (TDirectory*)key->ReadObj();
180     TString path(d->GetPath());
181     if (path.Contains("multicutMVA")){
182     multiMVA=kTRUE;
183     plot_efficiencies( file, type, d );
184     }
185     }
186     plot_efficiencies( file, type, gDirectory );
187    
188     return;
189     }
190