1 |
#include "tmvaglob.C"
|
2 |
|
3 |
// this macro plots the MVA probability distributions (Signal and
|
4 |
// Background overlayed) of different MVA methods run in TMVA
|
5 |
// (e.g. running TMVAnalysis.C).
|
6 |
|
7 |
// input: - Input file (result from TMVA)
|
8 |
// - use of TMVA plotting TStyle
|
9 |
void probas( TString fin = "TMVA.root", Bool_t useTMVAStyle = kTRUE )
|
10 |
{
|
11 |
cout << "--- probas.C" << endl;
|
12 |
// set style and remove existing canvas'
|
13 |
TMVAGlob::Initialize( useTMVAStyle );
|
14 |
|
15 |
// switches
|
16 |
const Bool_t Draw_CFANN_Logy = kFALSE;
|
17 |
const Bool_t Save_Images = kTRUE;
|
18 |
|
19 |
// checks if file with name "fin" is already open, and if not opens one
|
20 |
TFile* file = TMVAGlob::OpenFile( fin );
|
21 |
|
22 |
// define Canvas layout here!
|
23 |
Int_t xPad = 1; // no of plots in x
|
24 |
Int_t yPad = 1; // no of plots in y
|
25 |
Int_t noPad = xPad * yPad ;
|
26 |
const Int_t width = 600; // size of canvas
|
27 |
|
28 |
// this defines how many canvases we need
|
29 |
TCanvas *c = 0;
|
30 |
|
31 |
// counter variables
|
32 |
Int_t countCanvas = 0;
|
33 |
|
34 |
// list of existing MVAs
|
35 |
const Int_t nveto = 1;
|
36 |
TString suffixSig = "_tr_S";
|
37 |
TString suffixBgd = "_tr_B";
|
38 |
|
39 |
// search for the right histograms in full list of keys
|
40 |
TList methods;
|
41 |
UInt_t nmethods = TMVAGlob::GetListOfMethods( methods );
|
42 |
if (nmethods==0) {
|
43 |
cout << "No methods found!" << endl;
|
44 |
return;
|
45 |
}
|
46 |
TIter next(&methods);
|
47 |
TKey *key, *hkey;
|
48 |
char fname[200];
|
49 |
TH1* sig(0);
|
50 |
TH1* bgd(0);
|
51 |
while ((key = (TKey*)next())) {
|
52 |
TDirectory * mDir = (TDirectory*)key->ReadObj();
|
53 |
TList titles;
|
54 |
UInt_t ni = TMVAGlob::GetListOfTitles( mDir, titles );
|
55 |
TString methodName;
|
56 |
TMVAGlob::GetMethodName(methodName,key);
|
57 |
if (ni==0) {
|
58 |
cout << "No titles found for " << methodName << endl;
|
59 |
return;
|
60 |
}
|
61 |
TIter nextTitle(&titles);
|
62 |
TKey *instkey;
|
63 |
TDirectory *instDir;
|
64 |
while ((instkey = (TKey *)nextTitle())) {
|
65 |
instDir = (TDirectory *)instkey->ReadObj();
|
66 |
TString instName = instkey->GetName();
|
67 |
TList h1hists;
|
68 |
UInt_t nhists = TMVAGlob::GetListOfKeys( h1hists, "TH1", instDir );
|
69 |
if (nhists==0) cout << "No hists found!" << endl;
|
70 |
TIter nextInDir(&h1hists);
|
71 |
TString methodTitle;
|
72 |
TMVAGlob::GetMethodTitle(methodTitle,instDir);
|
73 |
while (hkey = (TKey*)nextInDir()) {
|
74 |
TH1 *th1 = (TH1*)hkey->ReadObj();
|
75 |
TString hname= th1->GetName();
|
76 |
if (hname.Contains( suffixSig ) && !hname.Contains( "Cut") &&
|
77 |
!hname.Contains("original") && !hname.Contains("smoothed")) {
|
78 |
|
79 |
// retrieve corresponding signal and background histograms
|
80 |
TString hnameS = hname;
|
81 |
TString hnameB = hname; hnameB.ReplaceAll("_S","_B");
|
82 |
|
83 |
sig = (TH1*)instDir->Get( hnameS );
|
84 |
bgd = (TH1*)instDir->Get( hnameB );
|
85 |
|
86 |
if (sig == 0 || bgd == 0) {
|
87 |
cout << "--- probas.C: big troubles in probas.... histogram: " << hname << " not found" << endl;
|
88 |
return;
|
89 |
}
|
90 |
|
91 |
TH1* sigF(0);
|
92 |
TH1* bkgF(0);
|
93 |
for (int i=0; i<= 5; i++) {
|
94 |
TString hspline = hnameS + Form("_smoothed_hist_from_spline%i",i);
|
95 |
sigF = (TH1*)instDir->Get( hspline );
|
96 |
|
97 |
if (sigF) {
|
98 |
bkgF = (TH1*)instDir->Get( hspline.ReplaceAll("_tr_S","_tr_B") );
|
99 |
break;
|
100 |
}
|
101 |
}
|
102 |
if ((sigF == NULL || bkgF == NULL) &&!hname.Contains("hist") ) {
|
103 |
cout << "--- probas.C: big troubles - did not found histogram " << hspline.Data() << " "
|
104 |
<< sigF << " " << bkgF << endl;
|
105 |
return;
|
106 |
}
|
107 |
else {
|
108 |
// remove the signal suffix
|
109 |
|
110 |
// check that exist
|
111 |
if (NULL != sigF && NULL != bkgF && NULL!=sig && NULL!=bgd) {
|
112 |
|
113 |
TString hname = sig->GetName();
|
114 |
|
115 |
// chop off useless stuff
|
116 |
sig->SetTitle( TString("TMVA output for classifier: ") + methodTitle );
|
117 |
|
118 |
// create new canvas
|
119 |
cout << "--- Book canvas no: " << countCanvas << endl;
|
120 |
char cn[20];
|
121 |
sprintf( cn, "canvas%d", countCanvas+1 );
|
122 |
c = new TCanvas( cn, Form("TMVA Output Fit Variables %s",methodTitle.Data()),
|
123 |
countCanvas*50+200, countCanvas*20, width, width*0.78 );
|
124 |
|
125 |
// set the histogram style
|
126 |
TMVAGlob::SetSignalAndBackgroundStyle( sig, bgd );
|
127 |
TMVAGlob::SetSignalAndBackgroundStyle( sigF, bkgF );
|
128 |
|
129 |
// frame limits (choose judicuous x range)
|
130 |
Float_t nrms = 4;
|
131 |
cout << "--- mean and RMS (S): " << sig->GetMean() << ", " << sig->GetRMS() << endl;
|
132 |
cout << "--- mean and RMS (B): " << bgd->GetMean() << ", " << bgd->GetRMS() << endl;
|
133 |
Float_t xmin = TMath::Max( TMath::Min(sig->GetMean() - nrms*sig->GetRMS(),
|
134 |
bgd->GetMean() - nrms*bgd->GetRMS() ),
|
135 |
sig->GetXaxis()->GetXmin() );
|
136 |
Float_t xmax = TMath::Min( TMath::Max(sig->GetMean() + nrms*sig->GetRMS(),
|
137 |
bgd->GetMean() + nrms*bgd->GetRMS() ),
|
138 |
sig->GetXaxis()->GetXmax() );
|
139 |
Float_t ymin = 0;
|
140 |
Float_t ymax = TMath::Max( sig->GetMaximum(), bgd->GetMaximum() )*1.5;
|
141 |
|
142 |
if (Draw_CFANN_Logy && mvaName[imva] == "CFANN") ymin = 0.01;
|
143 |
|
144 |
// build a frame
|
145 |
Int_t nb = 500;
|
146 |
TH2F* frame = new TH2F( TString("frame") + sig->GetName(), sig->GetTitle(),
|
147 |
nb, xmin, xmax, nb, ymin, ymax );
|
148 |
frame->GetXaxis()->SetTitle(methodTitle);
|
149 |
frame->GetYaxis()->SetTitle("Normalized");
|
150 |
TMVAGlob::SetFrameStyle( frame );
|
151 |
|
152 |
// eventually: draw the frame
|
153 |
frame->Draw();
|
154 |
|
155 |
if (Draw_CFANN_Logy && mvaName[imva] == "CFANN") c->SetLogy();
|
156 |
|
157 |
// overlay signal and background histograms
|
158 |
sig->SetMarkerColor(4);
|
159 |
sig->SetMarkerSize( 0.7 );
|
160 |
sig->SetMarkerStyle( 20 );
|
161 |
sig->SetLineWidth(1);
|
162 |
|
163 |
bgd->SetMarkerColor(2);
|
164 |
bgd->SetMarkerSize( 0.7 );
|
165 |
bgd->SetMarkerStyle( 24 );
|
166 |
bgd->SetLineWidth(1);
|
167 |
|
168 |
sig->Draw("samee");
|
169 |
bgd->Draw("samee");
|
170 |
|
171 |
sigF->SetFillStyle( 0 );
|
172 |
bkgF->SetFillStyle( 0 );
|
173 |
sigF->Draw("samehist");
|
174 |
bkgF->Draw("samehist");
|
175 |
|
176 |
// redraw axes
|
177 |
frame->Draw("sameaxis");
|
178 |
|
179 |
// Draw legend
|
180 |
TLegend *legend= new TLegend( c->GetLeftMargin(), 1 - c->GetTopMargin() - 0.2,
|
181 |
c->GetLeftMargin() + 0.4, 1 - c->GetTopMargin() );
|
182 |
legend->AddEntry(sig,"Signal data","P");
|
183 |
legend->AddEntry(sigF,"Signal PDF","L");
|
184 |
legend->AddEntry(bgd,"Background data","P");
|
185 |
legend->AddEntry(bkgF,"Background PDF","L");
|
186 |
legend->Draw("same");
|
187 |
legend->SetBorderSize(1);
|
188 |
legend->SetMargin( 0.3 );
|
189 |
|
190 |
// save canvas to file
|
191 |
c->Update();
|
192 |
TMVAGlob::plot_logo();
|
193 |
sprintf( fname, "plots/mva_pdf_%s_c%i", methodTitle.Data(), countCanvas+1 );
|
194 |
if (Save_Images) TMVAGlob::imgconv( c, fname );
|
195 |
countCanvas++;
|
196 |
}
|
197 |
}
|
198 |
}
|
199 |
}
|
200 |
}
|
201 |
}
|
202 |
}
|