ROOT  6.06/09
Reference Guide
mvas.cxx
Go to the documentation of this file.
1 #include "TMVA/mvas.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::mvas( 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  // define Canvas layout here!
28  const Int_t width = 600; // size of canvas
29 
30  // this defines how many canvases we need
31  TCanvas *c = 0;
32 
33  // counter variables
34  Int_t countCanvas = 0;
35 
36  // search for the right histograms in full list of keys
37  TIter next(file->GetListOfKeys());
38  TKey *key(0);
39  while ((key = (TKey*)next())) {
40 
41  if (!TString(key->GetName()).BeginsWith("Method_")) continue;
42  if (!gROOT->GetClass(key->GetClassName())->InheritsFrom("TDirectory")) continue;
43 
44  TString methodName;
45  TMVAGlob::GetMethodName(methodName,key);
46 
47  TDirectory* mDir = (TDirectory*)key->ReadObj();
48 
49  TIter keyIt(mDir->GetListOfKeys());
50  TKey *titkey;
51  while ((titkey = (TKey*)keyIt())) {
52 
53  if (!gROOT->GetClass(titkey->GetClassName())->InheritsFrom("TDirectory")) continue;
54 
55  TDirectory *titDir = (TDirectory *)titkey->ReadObj();
56  TString methodTitle;
57  TMVAGlob::GetMethodTitle(methodTitle,titDir);
58 
59  std::cout << "--- Found directory for method: " << methodName << "::" << methodTitle << std::flush;
60  TString hname = "MVA_" + methodTitle;
61  if (htype == kProbaType ) hname += "_Proba";
62  else if (htype == kRarityType ) hname += "_Rarity";
63  TH1* sig = dynamic_cast<TH1*>(titDir->Get( hname + "_S" ));
64  TH1* bgd = dynamic_cast<TH1*>(titDir->Get( hname + "_B" ));
65 
66  if (sig==0 || bgd==0) {
67  if (htype == kMVAType)
68  cout << ":\t mva distribution not available (this is normal for Cut classifier)" << endl;
69  else if(htype == kProbaType)
70  cout << ":\t probability distribution not available" << endl;
71  else if(htype == kRarityType)
72  cout << ":\t rarity distribution not available" << endl;
73  else if(htype == kCompareType)
74  cout << ":\t overtraining check not available" << endl;
75  else cout << endl;
76  continue;
77  }
78 
79  cout << " containing " << hname << "_S/_B" << endl;
80  // chop off useless stuff
81  sig->SetTitle( Form("TMVA response for classifier: %s", methodTitle.Data()) );
82  if (htype == kProbaType)
83  sig->SetTitle( Form("TMVA probability for classifier: %s", methodTitle.Data()) );
84  else if (htype == kRarityType)
85  sig->SetTitle( Form("TMVA Rarity for classifier: %s", methodTitle.Data()) );
86  else if (htype == kCompareType)
87  sig->SetTitle( Form("TMVA overtraining check for classifier: %s", methodTitle.Data()) );
88 
89  // create new canvas
90  TString ctitle = ((htype == kMVAType) ?
91  Form("TMVA response %s",methodTitle.Data()) :
92  (htype == kProbaType) ?
93  Form("TMVA probability %s",methodTitle.Data()) :
94  (htype == kCompareType) ?
95  Form("TMVA comparison %s",methodTitle.Data()) :
96  Form("TMVA Rarity %s",methodTitle.Data()));
97 
98  c = new TCanvas( Form("canvas%d", countCanvas+1), ctitle,
99  countCanvas*50+200, countCanvas*20, width, (Int_t)width*0.78 );
100 
101  // set the histogram style
103 
104  // normalise both signal and background
105  TMVAGlob::NormalizeHists( sig, bgd );
106 
107  // frame limits (choose judicuous x range)
108  Float_t nrms = 10;
109  cout << "--- Mean and RMS (S): " << sig->GetMean() << ", " << sig->GetRMS() << endl;
110  cout << "--- Mean and RMS (B): " << bgd->GetMean() << ", " << bgd->GetRMS() << endl;
111  Float_t xmin = TMath::Max( TMath::Min(sig->GetMean() - nrms*sig->GetRMS(),
112  bgd->GetMean() - nrms*bgd->GetRMS() ),
113  sig->GetXaxis()->GetXmin() );
114  Float_t xmax = TMath::Min( TMath::Max(sig->GetMean() + nrms*sig->GetRMS(),
115  bgd->GetMean() + nrms*bgd->GetRMS() ),
116  sig->GetXaxis()->GetXmax() );
117  Float_t ymin = 0;
118  Float_t maxMult = (htype == kCompareType) ? 1.3 : 1.2;
119  Float_t ymax = TMath::Max( sig->GetMaximum(), bgd->GetMaximum() )*maxMult;
120 
121  // build a frame
122  Int_t nb = 500;
123  TString hFrameName(TString("frame") + methodTitle);
124  TObject *o = gROOT->FindObject(hFrameName);
125  if(o) delete o;
126  TH2F* frame = new TH2F( hFrameName, sig->GetTitle(),
127  nb, xmin, xmax, nb, ymin, ymax );
128  frame->GetXaxis()->SetTitle( methodTitle + ((htype == kMVAType || htype == kCompareType) ? " response" : "") );
129  if (htype == kProbaType ) frame->GetXaxis()->SetTitle( "Signal probability" );
130  else if (htype == kRarityType ) frame->GetXaxis()->SetTitle( "Signal rarity" );
131  frame->GetYaxis()->SetTitle("(1/N) dN^{ }/^{ }dx");
132  TMVAGlob::SetFrameStyle( frame );
133 
134  // eventually: draw the frame
135  frame->Draw();
136 
137  c->GetPad(0)->SetLeftMargin( 0.105 );
138  frame->GetYaxis()->SetTitleOffset( 1.2 );
139 
140  // Draw legend
141  TLegend *legend= new TLegend( c->GetLeftMargin(), 1 - c->GetTopMargin() - 0.12,
142  c->GetLeftMargin() + (htype == kCompareType ? 0.40 : 0.3), 1 - c->GetTopMargin() );
143  legend->SetFillStyle( 1 );
144  legend->AddEntry(sig,TString("Signal") + ((htype == kCompareType) ? " (test sample)" : ""), "F");
145  legend->AddEntry(bgd,TString("Background") + ((htype == kCompareType) ? " (test sample)" : ""), "F");
146  legend->SetBorderSize(1);
147  legend->SetMargin( (htype == kCompareType ? 0.2 : 0.3) );
148  legend->Draw("same");
149 
150  // overlay signal and background histograms
151  sig->Draw("samehist");
152  bgd->Draw("samehist");
153 
154  if (htype == kCompareType) {
155  // if overtraining check, load additional histograms
156  TH1* sigOv = 0;
157  TH1* bgdOv = 0;
158 
159  TString ovname = hname += "_Train";
160  sigOv = dynamic_cast<TH1*>(titDir->Get( ovname + "_S" ));
161  bgdOv = dynamic_cast<TH1*>(titDir->Get( ovname + "_B" ));
162 
163  if (sigOv == 0 || bgdOv == 0) {
164  cout << "+++ Problem in \"mvas.C\": overtraining check histograms do not exist" << endl;
165  }
166  else {
167  cout << "--- Found comparison histograms for overtraining check" << endl;
168 
169  TLegend *legend2= new TLegend( 1 - c->GetRightMargin() - 0.42, 1 - c->GetTopMargin() - 0.12,
170  1 - c->GetRightMargin(), 1 - c->GetTopMargin() );
171  legend2->SetFillStyle( 1 );
172  legend2->SetBorderSize(1);
173  legend2->AddEntry(sigOv,"Signal (training sample)","P");
174  legend2->AddEntry(bgdOv,"Background (training sample)","P");
175  legend2->SetMargin( 0.1 );
176  legend2->Draw("same");
177  }
178  // normalise both signal and background
179  TMVAGlob::NormalizeHists( sigOv, bgdOv );
180 
181  Int_t col = sig->GetLineColor();
182  sigOv->SetMarkerColor( col );
183  sigOv->SetMarkerSize( 0.7 );
184  sigOv->SetMarkerStyle( 20 );
185  sigOv->SetLineWidth( 1 );
186  sigOv->SetLineColor( col );
187  sigOv->Draw("e1same");
188 
189  col = bgd->GetLineColor();
190  bgdOv->SetMarkerColor( col );
191  bgdOv->SetMarkerSize( 0.7 );
192  bgdOv->SetMarkerStyle( 20 );
193  bgdOv->SetLineWidth( 1 );
194  bgdOv->SetLineColor( col );
195  bgdOv->Draw("e1same");
196 
197  ymax = TMath::Max( ymax, float(TMath::Max( sigOv->GetMaximum(), bgdOv->GetMaximum() )*maxMult ));
198  frame->GetYaxis()->SetLimits( 0, ymax );
199 
200  // for better visibility, plot thinner lines
201  sig->SetLineWidth( 1 );
202  bgd->SetLineWidth( 1 );
203 
204  // perform K-S test
205  cout << "--- Perform Kolmogorov-Smirnov tests" << endl;
206  Double_t kolS = sig->KolmogorovTest( sigOv, "X" );
207  Double_t kolB = bgd->KolmogorovTest( bgdOv, "X" );
208  cout << "--- Goodness of signal (background) consistency: " << kolS << " (" << kolB << ")" << endl;
209 
210  TString probatext = Form( "Kolmogorov-Smirnov test: signal (background) probability = %5.3g (%5.3g)", kolS, kolB );
211  TText* tt = new TText( 0.12, 0.74, probatext );
212  tt->SetNDC(); tt->SetTextSize( 0.032 ); tt->AppendPad();
213  }
214 
215  // redraw axes
216  frame->Draw("sameaxis");
217 
218  // text for overflows
219  Int_t nbin = sig->GetNbinsX();
220  Double_t dxu = sig->GetBinWidth(0);
221  Double_t dxo = sig->GetBinWidth(nbin+1);
222  TString uoflow = Form( "U/O-flow (S,B): (%.1f, %.1f)%% / (%.1f, %.1f)%%",
223  sig->GetBinContent(0)*dxu*100, bgd->GetBinContent(0)*dxu*100,
224  sig->GetBinContent(nbin+1)*dxo*100, bgd->GetBinContent(nbin+1)*dxo*100 );
225  TText* t = new TText( 0.975, 0.115, uoflow );
226  t->SetNDC();
227  t->SetTextSize( 0.030 );
228  t->SetTextAngle( 90 );
229  t->AppendPad();
230 
231  // update canvas
232  c->Update();
233 
234  // save canvas to file
235 
236  TMVAGlob::plot_logo(1.058);
237  if (Save_Images) {
238  if (htype == kMVAType) TMVAGlob::imgconv( c, Form("plots/mva_%s", methodTitle.Data()) );
239  else if (htype == kProbaType) TMVAGlob::imgconv( c, Form("plots/proba_%s", methodTitle.Data()) );
240  else if (htype == kCompareType) TMVAGlob::imgconv( c, Form("plots/overtrain_%s", methodTitle.Data()) );
241  else TMVAGlob::imgconv( c, Form("plots/rarity_%s", methodTitle.Data()) );
242  }
243  countCanvas++;
244 
245  }
246  cout << "";
247  }
248 }
249 
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:244
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:52
virtual void SetLineWidth(Width_t lwidth)
Definition: TAttLine.h:57
void imgconv(TCanvas *c, const TString &fname)
Definition: tmvaglob.cxx:212
float xmin
Definition: THbookFile.cxx:93
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition: TH1.cxx:4629
This class displays a legend box (TPaveText) containing several legend entries.
Definition: TLegend.h:35
virtual void SetLimits(Double_t xmin, Double_t xmax)
Definition: TAxis.h:154
virtual TList * GetListOfKeys() const
Definition: TDirectory.h:155
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
Definition: TDirectory.cxx:727
ClassImp(TSeqCollection) Int_t TSeqCollection TIter next(this)
Return index of object in collection.
float Float_t
Definition: RtypesCore.h:53
THist< 2, float > TH2F
Definition: THist.h:321
float ymin
Definition: THbookFile.cxx:93
TFile * OpenFile(const TString &fin)
Definition: tmvaglob.cxx:192
void NormalizeHists(TH1 *sig, TH1 *bkg=0)
Definition: tmvaglob.cxx:317
virtual TList * GetListOfKeys() const
virtual void Draw(Option_t *option="")
Draw this legend with its current attributes.
Definition: TLegend.cxx:373
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:45
void SetSignalAndBackgroundStyle(TH1 *sig, TH1 *bkg, TH1 *all=0)
Definition: tmvaglob.cxx:8
#define gROOT
Definition: TROOT.h:340
void SetFrameStyle(TH1 *frame, Float_t scale=1.0)
Definition: tmvaglob.cxx:77
Basic string class.
Definition: TString.h:137
Short_t Min(Short_t a, Short_t b)
Definition: TMathBase.h:170
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
void SetMargin(Float_t margin)
Definition: TLegend.h:80
virtual void SetFillStyle(Style_t fstyle)
Definition: TAttFill.h:52
virtual Int_t GetNbinsX() const
Definition: TH1.h:296
void mvas(TString fin="TMVA.root", HistType htype=kMVAType, Bool_t useTMVAStyle=kTRUE)
Definition: mvas.cxx:16
TLegend * legend
Definition: pirndm.C:35
virtual void AppendPad(Option_t *option="")
Append graphics object to current pad.
Definition: TObject.cxx:164
virtual Double_t GetBinWidth(Int_t bin) const
return bin width for 1D historam Better to use h1.GetXaxis().GetBinWidth(bin)
Definition: TH1.cxx:8492
const char * Data() const
Definition: TString.h:349
Float_t GetTopMargin() const
Definition: TAttPad.h:56
Float_t GetRightMargin() const
Definition: TAttPad.h:55
TText * tt
Definition: textangle.C:16
bool BeginsWith(const std::string &theString, const std::string &theSubstring)
virtual void SetMarkerColor(Color_t mcolor=1)
Definition: TAttMarker.h:51
void GetMethodTitle(TString &name, TKey *ikey)
Definition: tmvaglob.cxx:341
Base class for several text objects.
Definition: TText.h:42
virtual void SetNDC(Bool_t isNDC=kTRUE)
Set NDC mode on if isNDC = kTRUE, off otherwise.
Definition: TText.cxx:809
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:30
Double_t GetXmin() const
Definition: TAxis.h:137
virtual void SetLineColor(Color_t lcolor)
Definition: TAttLine.h:54
float ymax
Definition: THbookFile.cxx:93
virtual Double_t GetMean(Int_t axis=1) const
For axis = 1,2 or 3 returns the mean value of the histogram along X,Y or Z axis.
Definition: TH1.cxx:7014
std::string GetMethodName(TCppMethod_t)
Definition: Cppyy.cxx:707
void Initialize(Bool_t useTMVAStyle=kTRUE)
Definition: tmvaglob.cxx:176
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:256
virtual Double_t KolmogorovTest(const TH1 *h2, Option_t *option="") const
Statistical test of compatibility in shape between this histogram and h2, using Kolmogorov test...
Definition: TH1.cxx:7610
char * Form(const char *fmt,...)
virtual void SetMarkerStyle(Style_t mstyle=1)
Definition: TAttMarker.h:53
TAxis * GetYaxis()
Definition: TH1.h:320
float xmax
Definition: THbookFile.cxx:93
void plot_logo(Float_t v_scale=1.0, Float_t skew=1.0)
Definition: tmvaglob.cxx:263
Double_t GetRMS(Int_t axis=1) const
Definition: TH1.h:315
virtual Color_t GetLineColor() const
Definition: TAttLine.h:47
virtual void SetMarkerSize(Size_t msize=1)
Definition: TAttMarker.h:54
Float_t GetLeftMargin() const
Definition: TAttPad.h:54
The Canvas class.
Definition: TCanvas.h:48
double Double_t
Definition: RtypesCore.h:55
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition: TLegend.cxx:280
Describe directory structure in memory.
Definition: TDirectory.h:41
Double_t GetXmax() const
Definition: TAxis.h:138
The TH1 histogram class.
Definition: TH1.h:80
Mother of all ROOT objects.
Definition: TObject.h:58
HistType
Definition: Types.h:71
virtual TVirtualPad * GetPad(Int_t subpadnumber) const
Get a pointer to subpadnumber of this pad.
Definition: TPad.cxx:2774
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:202
virtual void SetTitle(const char *title)
Change (i.e.
Definition: TH1.cxx:6268
virtual void SetTextSize(Float_t tsize=1)
Definition: TAttText.h:60
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2179
virtual Double_t GetMaximum(Double_t maxval=FLT_MAX) const
Return maximum value smaller than maxval of bins in the range, unless the value has been overridden b...
Definition: TH1.cxx:7921
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual void SetTitle(const char *title="")
Change (i.e. set) the title of the TNamed.
Definition: TNamed.cxx:152
virtual void SetBorderSize(Int_t bordersize=4)
Definition: TPave.h:82
TAxis * GetXaxis()
Definition: TH1.h:319
virtual void SetLeftMargin(Float_t leftmargin)
Set Pad left margin in fraction of the pad width.
Definition: TAttPad.cxx:107