Logo ROOT   6.10/09
Reference Guide
mvasMulticlass.cxx
Go to the documentation of this file.
1 #include "TMVA/mvasMulticlass.h"
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
16 void 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
25  TFile* file = TMVAGlob::OpenFile( fin );
26 
27  TDirectory* tempdir = (TDirectory*)file->GetDirectory(dataset.Data())->Get("InputVariables_Id" );
28  std::vector<TString> classnames(TMVAGlob::GetClassNames(tempdir));
29 
30  // define Canvas layout here!
31  // Int_t xPad = 1; // no of plots in x
32  // Int_t yPad = 1; // no of plots in y
33  // Int_t noPad = xPad * yPad ;
34  const Int_t width = 600; // size of canvas
35 
36  // this defines how many canvases we need
37  TCanvas *c = 0;
38 
39  // counter variables
40  Int_t countCanvas = 0;
41 
42  // search for the right histograms in full list of keys
43  TIter next(file->GetDirectory(dataset.Data())->GetListOfKeys());
44  TKey *key(0);
45  while ((key = (TKey*)next())) {
46 
47  if (!TString(key->GetName()).BeginsWith("Method_")) continue;
48  if (!gROOT->GetClass(key->GetClassName())->InheritsFrom("TDirectory")) continue;
49 
50  TString methodName;
51  TMVAGlob::GetMethodName(methodName,key);
52 
53  TDirectory* mDir = (TDirectory*)key->ReadObj();
54 
55  TIter keyIt(mDir->GetListOfKeys());
56  TKey *titkey;
57  while ((titkey = (TKey*)keyIt())) {
58 
59  if (!gROOT->GetClass(titkey->GetClassName())->InheritsFrom("TDirectory")) continue;
60 
61  TDirectory *titDir = (TDirectory *)titkey->ReadObj();
62  TString methodTitle;
63  TMVAGlob::GetMethodTitle(methodTitle,titDir);
64 
65  cout << "--- Found directory for method: " << methodName << "::" << methodTitle << endl;
66  TString hname = "MVA_" + methodTitle;
67  for(UInt_t icls = 0; icls < classnames.size(); ++icls){
68  TObjArray hists;
69 
70  std::vector<TString>::iterator classiter = classnames.begin();
71  for(; classiter!=classnames.end(); ++classiter){
72  TString name(hname+"_Test_"+ classnames.at(icls)
73  + "_prob_for_" + *classiter);
74  TH1 *hist = (TH1*)titDir->Get(name);
75  if (hist==0){
76  cout << ":\t mva distribution not available (this is normal for Cut classifier)" << endl;
77  continue;
78  }
79  hists.Add(hist);
80  }
81 
82 
83  // chop off useless stuff
84  ((TH1*)hists.First())->SetTitle( Form("TMVA response for classifier: %s", methodTitle.Data() ));
85 
86  // create new canvas
87  //cout << "Create canvas..." << endl;
88  TString ctitle = ((htype == kMVAType) ?
89  Form("TMVA response for class %s %s", classnames.at(icls).Data(),methodTitle.Data()) :
90  Form("TMVA comparison for class %s %s", classnames.at(icls).Data(),methodTitle.Data())) ;
91 
92  c = new TCanvas( Form("canvas%d", countCanvas+1), ctitle,
93  countCanvas*50+200, countCanvas*20, width, (Int_t)width*0.78 );
94 
95  // set the histogram style
96  //cout << "Set histogram style..." << endl;
98 
99  // normalise all histograms and find maximum
100  Float_t histmax = -1;
101  for(Int_t i=0; i<hists.GetEntriesFast(); ++i){
102  TMVAGlob::NormalizeHist((TH1*)hists[i] );
103  if(((TH1*)hists[i])->GetMaximum() > histmax)
104  histmax = ((TH1*)hists[i])->GetMaximum();
105  }
106 
107  // frame limits (between 0 and 1 per definition)
108  Float_t xmin = 0;
109  Float_t xmax = 1;
110  Float_t ymin = 0;
111  Float_t maxMult = (htype == kCompareType) ? 1.3 : 1.2;
112  Float_t ymax = histmax*maxMult;
113  // build a frame
114  Int_t nb = 500;
115  TString hFrameName(TString("frame") + methodTitle);
116  TObject *o = gROOT->FindObject(hFrameName);
117  if(o) delete o;
118  TH2F* frame = new TH2F( hFrameName, ((TH1*)hists.First())->GetTitle(),
119  nb, xmin, xmax, nb, ymin, ymax );
120  frame->GetXaxis()->SetTitle( methodTitle + " response for "+classnames.at(icls));
121  frame->GetYaxis()->SetTitle("(1/N) dN^{ }/^{ }dx");
122  TMVAGlob::SetFrameStyle( frame );
123 
124  // eventually: draw the frame
125  frame->Draw();
126 
127  c->GetPad(0)->SetLeftMargin( 0.105 );
128  frame->GetYaxis()->SetTitleOffset( 1.2 );
129 
130  // Draw legend
131  TLegend *legend= new TLegend( c->GetLeftMargin(), 1 - c->GetTopMargin() - 0.12,
132  c->GetLeftMargin() + (htype == kCompareType ? 0.40 : 0.3), 1 - c->GetTopMargin() );
133  legend->SetFillStyle( 1 );
134  classiter = classnames.begin();
135 
136  for(Int_t i=0; i<hists.GetEntriesFast(); ++i, ++classiter){
137  legend->AddEntry(((TH1*)hists[i]),*classiter,"F");
138  }
139 
140  legend->SetBorderSize(1);
141  legend->SetMargin( 0.3 );
142  legend->Draw("same");
143 
144 
145  for(Int_t i=0; i<hists.GetEntriesFast(); ++i){
146 
147  ((TH1*)hists[i])->Draw("histsame");
148  TString ytit = TString("(1/N) ") + ((TH1*)hists[i])->GetYaxis()->GetTitle();
149  ((TH1*)hists[i])->GetYaxis()->SetTitle( ytit ); // histograms are normalised
150 
151  }
152 
153 
154  if (htype == kCompareType) {
155 
156  TObjArray othists;
157  // if overtraining check, load additional histograms
158  classiter = classnames.begin();
159  for(; classiter!=classnames.end(); ++classiter){
160  TString name(hname+"_Train_"+ classnames.at(icls)
161  + "_prob_for_" + *classiter);
162  TH1 *hist = (TH1*)titDir->Get(name);
163  if (hist==0){
164  cout << ":\t comparison histogram for overtraining check not available!" << endl;
165  continue;
166  }
167  othists.Add(hist);
168  }
169 
170  TLegend *legend2= new TLegend( 1 - c->GetRightMargin() - 0.42, 1 - c->GetTopMargin() - 0.12,
171  1 - c->GetRightMargin(), 1 - c->GetTopMargin() );
172  legend2->SetFillStyle( 1 );
173  legend2->SetBorderSize(1);
174 
175  classiter = classnames.begin();
176  for(Int_t i=0; i<othists.GetEntriesFast(); ++i, ++classiter){
177  legend2->AddEntry(((TH1*)othists[i]),*classiter+" (training sample)","P");
178  }
179  legend2->SetMargin( 0.1 );
180  legend2->Draw("same");
181 
182  // normalise all histograms and get maximum
183  for(Int_t i=0; i<othists.GetEntriesFast(); ++i){
184  TMVAGlob::NormalizeHist((TH1*)othists[i] );
185  if(((TH1*)othists[i])->GetMaximum() > histmax)
186  histmax = ((TH1*)othists[i])->GetMaximum();
187  }
188 
189  TMVAGlob::SetMultiClassStyle( &othists );
190  for(Int_t i=0; i<othists.GetEntriesFast(); ++i){
191  Int_t col = ((TH1*)hists[i])->GetLineColor();
192  ((TH1*)othists[i])->SetMarkerSize( 0.7 );
193  ((TH1*)othists[i])->SetMarkerStyle( 20 );
194  ((TH1*)othists[i])->SetMarkerColor( col );
195  ((TH1*)othists[i])->SetLineWidth( 1 );
196  ((TH1*)othists[i])->Draw("e1same");
197  }
198 
199  ymax = histmax*maxMult;
200  frame->GetYaxis()->SetLimits( 0, ymax );
201 
202  // for better visibility, plot thinner lines
203  TMVAGlob::SetMultiClassStyle( &othists );
204  for(Int_t i=0; i<hists.GetEntriesFast(); ++i){
205  ((TH1*)hists[i])->SetLineWidth( 1 );
206  }
207 
208 
209  // perform K-S test
210 
211  cout << "--- Perform Kolmogorov-Smirnov tests" << endl;
212  cout << "--- Goodness of consistency for class " << classnames.at(icls)<< endl;
213  //TString probatext("Kolmogorov-Smirnov test: ");
214  for(Int_t j=0; j<othists.GetEntriesFast(); ++j){
215  Float_t kol = ((TH1*)hists[j])->KolmogorovTest(((TH1*)othists[j]),"X");
216  cout << classnames.at(j) << ": " << kol << endl;
217  //probatext.Append(classnames.at(j)+Form(" %.3f ",kol));
218  }
219 
220 
221 
222  //TText* tt = new TText( 0.12, 0.74, probatext );
223  //tt->SetNDC(); tt->SetTextSize( 0.032 ); tt->AppendPad();
224 
225  }
226 
227 
228  // redraw axes
229  frame->Draw("sameaxis");
230 
231  // text for overflows
232  //Int_t nbin = sig->GetNbinsX();
233  //Double_t dxu = sig->GetBinWidth(0);
234  //Double_t dxo = sig->GetBinWidth(nbin+1);
235  //TString uoflow = Form( "U/O-flow (S,B): (%.1f, %.1f)%% / (%.1f, %.1f)%%",
236  // sig->GetBinContent(0)*dxu*100, bgd->GetBinContent(0)*dxu*100,
237  // sig->GetBinContent(nbin+1)*dxo*100, bgd->GetBinContent(nbin+1)*dxo*100 );
238  //TText* t = new TText( 0.975, 0.115, uoflow );
239  //t->SetNDC();
240  //t->SetTextSize( 0.030 );
241  //t->SetTextAngle( 90 );
242  //t->AppendPad();
243 
244  // update canvas
245  c->Update();
246 
247  // save canvas to file
248 
249  TMVAGlob::plot_logo(1.058);
250  if (Save_Images) {
251  if (htype == kMVAType) TMVAGlob::imgconv( c, Form("%s/plots/mva_%s_%s",dataset.Data(),classnames.at(icls).Data(), methodTitle.Data()) );
252  else if (htype == kCompareType) TMVAGlob::imgconv( c, Form("%s/plots/overtrain_%s_%s",dataset.Data(),classnames.at(icls).Data(), methodTitle.Data()) );
253 
254  }
255  countCanvas++;
256  }
257  }
258  cout << "";
259  }
260 }
261 
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title Offset is a correction factor with respect to the "s...
Definition: TAttAxis.cxx:262
An array of TObjects.
Definition: TObjArray.h:37
void imgconv(TCanvas *c, const TString &fname)
Definition: tmvaglob.cxx:212
float xmin
Definition: THbookFile.cxx:93
Float_t GetLeftMargin() const
Definition: TAttPad.h:44
virtual TList * GetListOfKeys() const
Definition: TDirectory.h:148
This class displays a legend box (TPaveText) containing several legend entries.
Definition: TLegend.h:23
virtual void SetLimits(Double_t xmin, Double_t xmax)
Definition: TAxis.h:154
virtual TVirtualPad * GetPad(Int_t subpadnumber) const
Get a pointer to subpadnumber of this pad.
Definition: TPad.cxx:2835
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
Definition: TDirectory.cxx:729
float Float_t
Definition: RtypesCore.h:53
float ymin
Definition: THbookFile.cxx:93
TFile * OpenFile(const TString &fin)
Definition: tmvaglob.cxx:192
virtual void Draw(Option_t *option="")
Draw this legend with its current attributes.
Definition: TLegend.cxx:452
std::vector< TString > GetClassNames(TDirectory *dir)
Definition: tmvaglob.cxx:462
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:46
void NormalizeHist(TH1 *h)
Definition: tmvaglob.cxx:308
#define gROOT
Definition: TROOT.h:375
void SetFrameStyle(TH1 *frame, Float_t scale=1.0)
Definition: tmvaglob.cxx:77
UInt_t GetListOfKeys(TList &keys, TString inherits, TDirectory *dir=0)
Definition: tmvaglob.cxx:375
Basic string class.
Definition: TString.h:129
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
void SetMargin(Float_t margin)
Definition: TLegend.h:69
virtual void SetFillStyle(Style_t fstyle)
Set the fill area style.
Definition: TAttFill.h:39
TLegend * legend
Definition: pirndm.C:35
void GetMethodTitle(TString &name, TKey *ikey)
Definition: tmvaglob.cxx:341
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:24
void SetMultiClassStyle(TObjArray *hists)
Definition: tmvaglob.cxx:47
TObject * First() const
Return the object in the first slot.
Definition: TObjArray.cxx:471
float ymax
Definition: THbookFile.cxx:93
std::string GetMethodName(TCppMethod_t)
Definition: Cppyy.cxx:733
void Initialize(Bool_t useTMVAStyle=kTRUE)
Definition: tmvaglob.cxx:176
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2851
tomato 2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:249
unsigned int UInt_t
Definition: RtypesCore.h:42
Int_t GetEntriesFast() const
Definition: TObjArray.h:64
char * Form(const char *fmt,...)
TAxis * GetYaxis()
Definition: TH1.h:301
TIter begin() const
Definition: TCollection.h:187
float xmax
Definition: THbookFile.cxx:93
void plot_logo(Float_t v_scale=1.0, Float_t skew=1.0)
Definition: tmvaglob.cxx:263
virtual TDirectory * GetDirectory(const char *apath, Bool_t printError=false, const char *funcname="GetDirectory")
Find a directory named "apath".
The Canvas class.
Definition: TCanvas.h:31
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition: TLegend.cxx:359
Describe directory structure in memory.
Definition: TDirectory.h:34
The TH1 histogram class.
Definition: TH1.h:56
Mother of all ROOT objects.
Definition: TObject.h:37
Float_t GetTopMargin() const
Definition: TAttPad.h:46
Definition: file.py:1
void Add(TObject *obj)
Definition: TObjArray.h:73
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2208
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:155
THist< 2, float, THistStatContent, THistStatUncertainty > TH2F
Definition: THist.hxx:317
const Bool_t kTRUE
Definition: RtypesCore.h:91
Float_t GetRightMargin() const
Definition: TAttPad.h:45
virtual void SetBorderSize(Int_t bordersize=4)
Definition: TPave.h:70
TAxis * GetXaxis()
Definition: TH1.h:300
virtual void SetLeftMargin(Float_t leftmargin)
Set Pad left margin in fraction of the pad width.
Definition: TAttPad.cxx:110
void mvasMulticlass(TString dataset, TString fin="TMVAMulticlass.root", HistType htype=kMVAType, Bool_t useTMVAStyle=kTRUE)
const char * Data() const
Definition: TString.h:347