Logo ROOT  
Reference Guide
mvasMulticlass.cxx
Go to the documentation of this file.
2#include "TMVA/Types.h"
3#include "TLegend.h"
4#include "TText.h"
5#include "TH2.h"
6
7
8
9// this macro plots the resulting MVA distributions (Signal and
10// Background overlayed) of different MVA methods run in TMVA
11// (e.g. running TMVAnalysis.C).
12
13
14// input: - Input file (result from TMVA)
15// - use of TMVA plotting TStyle
16void TMVA::mvasMulticlass(TString dataset, TString fin , HistType htype , Bool_t useTMVAStyle )
17{
18 // set style and remove existing canvas'
19 TMVAGlob::Initialize( useTMVAStyle );
20
21 // switches
22 const Bool_t Save_Images = kTRUE;
23
24 // checks if file with name "fin" is already open, and if not opens one
26
27 // find directory from input variable transformation and extract class names
28 TIter keys_dir = file->GetDirectory(dataset.Data())->GetListOfKeys();
29 TKey *key;
30 TDirectory *tempdir = 0;
31 while ((key = (TKey *)keys_dir())) {
32 TString name = key->GetName();
33 if (name.BeginsWith("InputVariables_")) {
34 tempdir = (TDirectory *)(file->GetDirectory(dataset.Data())->Get(name));
35 break;
36 }
37 }
38 std::vector<TString> classnames(TMVAGlob::GetClassNames(tempdir));
39
40 // define Canvas layout here!
41 // Int_t xPad = 1; // no of plots in x
42 // Int_t yPad = 1; // no of plots in y
43 // Int_t noPad = xPad * yPad ;
44 const Int_t width = 600; // size of canvas
45
46 // this defines how many canvases we need
47 TCanvas *c = 0;
48
49 // counter variables
50 Int_t countCanvas = 0;
51
52 // search for the right histograms in full list of keys
53 TIter next(file->GetDirectory(dataset.Data())->GetListOfKeys());
54 key = 0;
55 while ((key = (TKey*)next())) {
56
57 if (!TString(key->GetName()).BeginsWith("Method_")) continue;
58 if (!gROOT->GetClass(key->GetClassName())->InheritsFrom("TDirectory")) continue;
59
60 TString methodName;
61 TMVAGlob::GetMethodName(methodName,key);
62
63 TDirectory* mDir = (TDirectory*)key->ReadObj();
64
65 TIter keyIt(mDir->GetListOfKeys());
66 TKey *titkey;
67 while ((titkey = (TKey*)keyIt())) {
68
69 if (!gROOT->GetClass(titkey->GetClassName())->InheritsFrom("TDirectory")) continue;
70
71 TDirectory *titDir = (TDirectory *)titkey->ReadObj();
72 TString methodTitle;
73 TMVAGlob::GetMethodTitle(methodTitle,titDir);
74
75 cout << "--- Found directory for method: " << methodName << "::" << methodTitle << endl;
76 TString hname = "MVA_" + methodTitle;
77 for(UInt_t icls = 0; icls < classnames.size(); ++icls){
78 TObjArray hists;
79
80 std::vector<TString>::iterator classiter = classnames.begin();
81 for(; classiter!=classnames.end(); ++classiter){
82 TString name(hname+"_Test_"+ classnames.at(icls)
83 + "_prob_for_" + *classiter);
84 TH1 *hist = (TH1*)titDir->Get(name);
85 if (hist==0){
86 cout << ":\t mva distribution not available (this is normal for Cut classifier)" << endl;
87 continue;
88 }
89 hists.Add(hist);
90 }
91
92
93 // chop off useless stuff
94 ((TH1*)hists.First())->SetTitle( Form("TMVA response for classifier: %s", methodTitle.Data() ));
95
96 // create new canvas
97 //cout << "Create canvas..." << endl;
98 TString ctitle = ((htype == kMVAType) ?
99 Form("TMVA response for class %s %s", classnames.at(icls).Data(),methodTitle.Data()) :
100 Form("TMVA comparison for class %s %s", classnames.at(icls).Data(),methodTitle.Data())) ;
101
102 c = new TCanvas( Form("canvas%d", countCanvas+1), ctitle,
103 countCanvas*50+200, countCanvas*20, width, (Int_t)width*0.78 );
104
105 // set the histogram style
106 //cout << "Set histogram style..." << endl;
108
109 // normalise all histograms and find maximum
110 Float_t histmax = -1;
111 for(Int_t i=0; i<hists.GetEntriesFast(); ++i){
112 TMVAGlob::NormalizeHist((TH1*)hists[i] );
113 if(((TH1*)hists[i])->GetMaximum() > histmax)
114 histmax = ((TH1*)hists[i])->GetMaximum();
115 }
116
117 // frame limits (between 0 and 1 per definition)
118 Float_t xmin = 0;
119 Float_t xmax = 1;
120 Float_t ymin = 0;
121 Float_t maxMult = (htype == kCompareType) ? 1.3 : 1.2;
122 Float_t ymax = histmax*maxMult;
123 // build a frame
124 Int_t nb = 500;
125 TString hFrameName(TString::Format("frame_class%d_",icls) + methodTitle);
126 TObject *o = gROOT->FindObject(hFrameName);
127 if(o) delete o;
128 TH1F* frame = new TH1F( hFrameName, ((TH1*)hists.First())->GetTitle(),
129 nb, xmin, xmax);
130 frame->SetMinimum(ymin);
131 frame->SetMaximum(ymax);
132 frame->GetXaxis()->SetTitle(methodTitle + " response for " + classnames.at(icls));
133 frame->GetYaxis()->SetTitle("(1/N) dN^{ }/^{ }dx");
135
136 // eventually: draw the frame
137 frame->Draw();
138
139 c->GetPad(0)->SetLeftMargin( 0.105 );
140 frame->GetYaxis()->SetTitleOffset( 1.2 );
141
142 // Draw legend
143 TLegend *legend= new TLegend( c->GetLeftMargin(), 1 - c->GetTopMargin() - 0.12,
144 c->GetLeftMargin() + (htype == kCompareType ? 0.40 : 0.3), 1 - c->GetTopMargin() );
145 legend->SetFillStyle( 1 );
146 classiter = classnames.begin();
147
148 for(Int_t i=0; i<hists.GetEntriesFast(); ++i, ++classiter){
149 legend->AddEntry(((TH1*)hists[i]),*classiter,"F");
150 }
151
152 legend->SetBorderSize(1);
153 legend->SetMargin( 0.3 );
154 legend->Draw("same");
155
156
157 for(Int_t i=0; i<hists.GetEntriesFast(); ++i){
158
159 ((TH1*)hists[i])->Draw("histsame");
160 TString ytit = TString("(1/N) ") + ((TH1*)hists[i])->GetYaxis()->GetTitle();
161 ((TH1*)hists[i])->GetYaxis()->SetTitle( ytit ); // histograms are normalised
162
163 }
164
165
166 if (htype == kCompareType) {
167
168 TObjArray othists;
169 // if overtraining check, load additional histograms
170 classiter = classnames.begin();
171 for(; classiter!=classnames.end(); ++classiter){
172 TString name(hname+"_Train_"+ classnames.at(icls)
173 + "_prob_for_" + *classiter);
174 TH1 *hist = (TH1*)titDir->Get(name);
175 if (hist==0){
176 cout << ":\t comparison histogram for overtraining check not available!" << endl;
177 continue;
178 }
179 othists.Add(hist);
180 }
181
182 TLegend *legend2= new TLegend( 1 - c->GetRightMargin() - 0.42, 1 - c->GetTopMargin() - 0.12,
183 1 - c->GetRightMargin(), 1 - c->GetTopMargin() );
184 legend2->SetFillStyle( 1 );
185 legend2->SetBorderSize(1);
186
187 classiter = classnames.begin();
188 for(Int_t i=0; i<othists.GetEntriesFast(); ++i, ++classiter){
189 legend2->AddEntry(((TH1*)othists[i]),*classiter+" (training sample)","P");
190 }
191 legend2->SetMargin( 0.1 );
192 legend2->Draw("same");
193
194 // normalise all histograms and get maximum
195 for(Int_t i=0; i<othists.GetEntriesFast(); ++i){
196 TMVAGlob::NormalizeHist((TH1*)othists[i] );
197 if(((TH1*)othists[i])->GetMaximum() > histmax)
198 histmax = ((TH1*)othists[i])->GetMaximum();
199 }
200
202 for(Int_t i=0; i<othists.GetEntriesFast(); ++i){
203 Int_t col = ((TH1*)hists[i])->GetLineColor();
204 ((TH1*)othists[i])->SetMarkerSize( 0.7 );
205 ((TH1*)othists[i])->SetMarkerStyle( 20 );
206 ((TH1*)othists[i])->SetMarkerColor( col );
207 ((TH1*)othists[i])->SetLineWidth( 1 );
208 ((TH1*)othists[i])->Draw("e1same");
209 }
210
211 ymax = histmax*maxMult;
212 frame->SetMaximum( ymax );
213
214 // for better visibility, plot thinner lines
216 for(Int_t i=0; i<hists.GetEntriesFast(); ++i){
217 ((TH1*)hists[i])->SetLineWidth( 1 );
218 }
219
220
221 // perform K-S test
222
223 cout << "--- Perform Kolmogorov-Smirnov tests" << endl;
224 cout << "--- Goodness of consistency for class " << classnames.at(icls)<< endl;
225 //TString probatext("Kolmogorov-Smirnov test: ");
226 for(Int_t j=0; j<othists.GetEntriesFast(); ++j){
227 Float_t kol = ((TH1*)hists[j])->KolmogorovTest(((TH1*)othists[j]),"X");
228 cout << classnames.at(j) << ": " << kol << endl;
229 //probatext.Append(classnames.at(j)+Form(" %.3f ",kol));
230 }
231
232
233
234 //TText* tt = new TText( 0.12, 0.74, probatext );
235 //tt->SetNDC(); tt->SetTextSize( 0.032 ); tt->AppendPad();
236
237 }
238
239
240 // redraw axes
241 frame->Draw("sameaxis");
242
243 // text for overflows
244 //Int_t nbin = sig->GetNbinsX();
245 //Double_t dxu = sig->GetBinWidth(0);
246 //Double_t dxo = sig->GetBinWidth(nbin+1);
247 //TString uoflow = Form( "U/O-flow (S,B): (%.1f, %.1f)%% / (%.1f, %.1f)%%",
248 // sig->GetBinContent(0)*dxu*100, bgd->GetBinContent(0)*dxu*100,
249 // sig->GetBinContent(nbin+1)*dxo*100, bgd->GetBinContent(nbin+1)*dxo*100 );
250 //TText* t = new TText( 0.975, 0.115, uoflow );
251 //t->SetNDC();
252 //t->SetTextSize( 0.030 );
253 //t->SetTextAngle( 90 );
254 //t->AppendPad();
255
256 // update canvas
257 c->Update();
258
259 // save canvas to file
260
261 TMVAGlob::plot_logo(1.058);
262 if (Save_Images) {
263 if (htype == kMVAType) TMVAGlob::imgconv( c, Form("%s/plots/mva_%s_%s",dataset.Data(),classnames.at(icls).Data(), methodTitle.Data()) );
264 else if (htype == kCompareType) TMVAGlob::imgconv( c, Form("%s/plots/overtrain_%s_%s",dataset.Data(),classnames.at(icls).Data(), methodTitle.Data()) );
265
266 }
267 countCanvas++;
268 }
269 }
270 cout << "";
271 }
272}
#define c(i)
Definition: RSha256.hxx:101
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
const Bool_t kTRUE
Definition: RtypesCore.h:100
Option_t Option_t width
char name[80]
Definition: TGX11.cxx:110
float xmin
Definition: THbookFile.cxx:95
float ymin
Definition: THbookFile.cxx:95
float xmax
Definition: THbookFile.cxx:95
float ymax
Definition: THbookFile.cxx:95
#define gROOT
Definition: TROOT.h:404
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition: TString.cxx:2447
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition: TAttAxis.cxx:301
virtual void SetFillStyle(Style_t fstyle)
Set the fill area style.
Definition: TAttFill.h:39
The Canvas class.
Definition: TCanvas.h:23
TIter begin() const
Definition: TCollection.h:292
Describe directory structure in memory.
Definition: TDirectory.h:45
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
Definition: TDirectory.cxx:814
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
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:574
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
virtual void SetMaximum(Double_t maximum=-1111)
Definition: TH1.h:397
TAxis * GetYaxis()
Definition: TH1.h:320
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition: TH1.cxx:3070
virtual void SetMinimum(Double_t minimum=-1111)
Definition: TH1.h:398
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:28
virtual const char * GetClassName() const
Definition: TKey.h:75
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
void SetMargin(Float_t margin)
Definition: TLegend.h:69
@ kCompareType
Definition: Types.h:66
@ kMVAType
Definition: Types.h:66
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
An array of TObjects.
Definition: TObjArray.h:31
Int_t GetEntriesFast() const
Definition: TObjArray.h:58
TObject * First() const override
Return the object in the first slot.
Definition: TObjArray.cxx:496
void Add(TObject *obj) override
Definition: TObjArray.h:68
Mother of all ROOT objects.
Definition: TObject.h:37
virtual void SetBorderSize(Int_t bordersize=4)
Definition: TPave.h:73
Basic string class.
Definition: TString.h:136
const char * Data() const
Definition: TString.h:369
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition: TString.h:615
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition: TString.cxx:2336
void Initialize(Bool_t useTMVAStyle=kTRUE)
Definition: tmvaglob.cxx:176
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
void GetMethodName(TString &name, TKey *mkey)
Definition: tmvaglob.cxx:342
void SetMultiClassStyle(TObjArray *hists)
Definition: tmvaglob.cxx:47
void NormalizeHist(TH1 *h)
Definition: tmvaglob.cxx:315
std::vector< TString > GetClassNames(TDirectory *dir)
Definition: tmvaglob.cxx:469
void imgconv(TCanvas *c, const TString &fname)
Definition: tmvaglob.cxx:212
void mvasMulticlass(TString dataset, TString fin="TMVAMulticlass.root", HistType htype=kMVAType, Bool_t useTMVAStyle=kTRUE)
Definition: file.py:1