ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/jbabb/ScriptsAndMacros/efficiencies.C
Revision: 1.1
Committed: Sat Sep 3 21:22:32 2011 UTC (13 years, 8 months ago) by jbabb
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 jbabb 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     hists.Add(h);
101     nmva++;
102     }
103     }
104     }
105     }
106    
107     while (hists.GetSize()) {
108     TListIter hIt(&hists);
109     TH1* hist(0);
110     Double_t largestInt=-1;
111     TH1* histWithLargestInt(0);
112     while ((hist = (TH1*)hIt())!=0) {
113     Double_t integral = hist->Integral(1,hist->FindBin(0.9999));
114     if (integral>largestInt) {
115     largestInt = integral;
116     histWithLargestInt = hist;
117     }
118     }
119     if (histWithLargestInt == 0) {
120     cout << "ERROR - unknown hist \"histWithLargestInt\" --> serious problem in ROOT file" << endl;
121     break;
122     }
123     legend->AddEntry(histWithLargestInt,TString(histWithLargestInt->GetTitle()).ReplaceAll("MVA_",""),"l");
124     hists.Remove(histWithLargestInt);
125     }
126    
127     // rescale legend box size
128     // current box size has been tuned for 3 MVAs + 1 title
129     if (type == 1) {
130     dyH *= (1.0 + Float_t(nmva - 3.0)/4.0);
131     legend->SetY1( y0H - dyH );
132     }
133     else {
134     dyH *= (Float_t(nmva - 3.0)/4.0);
135     legend->SetY2( y0H + dyH);
136     }
137    
138     // redraw axes
139     frame->Draw("sameaxis");
140     legend->Draw("same");
141    
142     // ============================================================
143    
144     if (__PLOT_LOGO__) TMVAGlob::plot_logo();
145    
146     // ============================================================
147    
148     c->Update();
149    
150     TString fname = "plots/" + hNameRef;
151     if (TString(BinDir->GetName()).Contains("multicut")){
152     TString fprepend(BinDir->GetName());
153     fprepend.ReplaceAll("multicutMVA_","");
154     fname = "plots/" + fprepend + "_" + hNameRef;
155     }
156     if (__SAVE_IMAGE__) TMVAGlob::imgconv( c, fname );
157    
158     return;
159     }
160    
161     void efficiencies( TString fin = "TMVA.root", Int_t type = 2, Bool_t useTMVAStyle = kTRUE )
162     {
163     // argument: type = 1 --> plot efficiency(B) versus eff(S)
164     // type = 2 --> plot rejection (B) versus efficiency (S)
165    
166     // set style and remove existing canvas'
167     TMVAGlob::Initialize( useTMVAStyle );
168    
169     // checks if file with name "fin" is already open, and if not opens one
170     TFile* file = TMVAGlob::OpenFile( fin );
171    
172     // check if multi-cut MVA or only one set of MVAs
173     Bool_t multiMVA=kFALSE;
174     TIter nextDir(file->GetListOfKeys());
175     TKey *key;
176     // loop over all directories and check if
177     // one contains the key word 'multicutMVA'
178     while ((key = (TKey*)nextDir())) {
179     TClass *cl = gROOT->GetClass(key->GetClassName());
180     if (!cl->InheritsFrom("TDirectory")) continue;
181     TDirectory *d = (TDirectory*)key->ReadObj();
182     TString path(d->GetPath());
183     if (path.Contains("multicutMVA")){
184     multiMVA=kTRUE;
185     plot_efficiencies( file, type, d );
186     }
187     }
188     plot_efficiencies( file, type, gDirectory );
189    
190     return;
191     }
192