Logo ROOT   6.08/07
Reference Guide
probas.cxx
Go to the documentation of this file.
1 #include "TMVA/probas.h"
2 #include <iostream>
3 #include <iomanip>
4 using std::cout;
5 using std::endl;
6 
7 
8 
9 #include "RQ_OBJECT.h"
10 
11 #include "TH1.h"
12 #include "TROOT.h"
13 #include "TList.h"
14 #include "TIterator.h"
15 #include "TStyle.h"
16 #include "TPad.h"
17 #include "TCanvas.h"
18 #include "TLatex.h"
19 #include "TLegend.h"
20 #include "TLine.h"
21 #include "TH2.h"
22 #include "TFormula.h"
23 #include "TFile.h"
24 #include "TApplication.h"
25 #include "TKey.h"
26 #include "TClass.h"
27 #include "TGaxis.h"
28 
29 #include "TGWindow.h"
30 #include "TGButton.h"
31 #include "TGLabel.h"
32 #include "TGNumberEntry.h"
33 
34 // this macro plots the MVA probability distributions (Signal and
35 // Background overlayed) of different MVA methods run in TMVA
36 // (e.g. running TMVAnalysis.C).
37 
38 // input: - Input file (result from TMVA)
39 // - use of TMVA plotting TStyle
40 void TMVA::probas(TString dataset, TString fin , Bool_t useTMVAStyle )
41 {
42  // set style and remove existing canvas'
43  TMVAGlob::Initialize( useTMVAStyle );
44 
45  // switches
46  const Bool_t Draw_CFANN_Logy = kFALSE;
47  const Bool_t Save_Images = kTRUE;
48 
49  // checks if file with name "fin" is already open, and if not opens one
50  TFile* file = TMVAGlob::OpenFile( fin );
51 
52  const Int_t width = 600; // size of canvas
53 
54  // this defines how many canvases we need
55  TCanvas *c = 0;
56 
57  // counter variables
58  Int_t countCanvas = 0;
59 
60  // list of existing MVAs
61  //const Int_t nveto = 1;
62  TString suffixSig = "_tr_S";
63  TString suffixBgd = "_tr_B";
64 
65  // search for the right histograms in full list of keys
66  TList methods;
67  UInt_t nmethods = TMVAGlob::GetListOfMethods( methods,file->GetDirectory(dataset.Data()) );
68  if (nmethods==0) {
69  cout << "--- Probas.C: no methods found!" << endl;
70  return;
71  }
72  TIter next(&methods);
73  TKey *key, *hkey;
74  char fname[200];
75  TH1* sig(0);
76  TH1* bgd(0);
77 
78 
79  while ( (key = (TKey*)next()) ) {
80  TDirectory * mDir = (TDirectory*)key->ReadObj();
81  TList titles;
82  UInt_t ni = TMVAGlob::GetListOfTitles( mDir, titles );
83  TString methodName;
84  TMVAGlob::GetMethodName(methodName,key);
85  if (ni==0) {
86  cout << "+++ No titles found for classifier: " << methodName << endl;
87  return;
88  }
89  TIter nextTitle(&titles);
90  TKey *instkey;
91  TDirectory *instDir;
92 
93  // iterate over all classifiers
94  while ( (instkey = (TKey *)nextTitle()) ) {
95  instDir = (TDirectory *)instkey->ReadObj();
96  TString instName = instkey->GetName();
97  TList h1hists;
98  UInt_t nhists = TMVAGlob::GetListOfKeys( h1hists, "TH1", instDir );
99  if (nhists==0) cout << "*** No histograms found!" << endl;
100  TIter nextInDir(&h1hists);
101  TString methodTitle;
102  TMVAGlob::GetMethodTitle(methodTitle,instDir);
103  Bool_t found = kFALSE;
104  while ( (hkey = (TKey*)nextInDir()) ) {
105  TH1 *th1 = (TH1*)hkey->ReadObj();
106  TString hname= th1->GetName();
107  if (hname.Contains( suffixSig ) && !hname.Contains( "Cut") &&
108  !hname.Contains("original") && !hname.Contains("smoothed")) {
109  // retrieve corresponding signal and background histograms
110  TString hnameS = hname;
111  TString hnameB = hname; hnameB.ReplaceAll("_S","_B");
112 
113  sig = (TH1*)instDir->Get( hnameS );
114  bgd = (TH1*)instDir->Get( hnameB );
115 
116  if (sig == 0 || bgd == 0) {
117  cout << "*** probas.C: big troubles in probas.... histogram: " << hname << " not found" << endl;
118  return;
119  }
120 
121  TH1* sigF(0);
122  TH1* bkgF(0);
123 
124  for (int i=0; i<= 5; i++) {
125  TString hspline = hnameS + Form("_smoothed_hist_from_spline%i",i);
126  sigF = (TH1*)instDir->Get( hspline );
127 
128  if (sigF) {
129  bkgF = (TH1*)instDir->Get( hspline.ReplaceAll("_tr_S","_tr_B") );
130  break;
131  }
132  }
133  if (!sigF){
134  TString hspline = hnameS + TString("_smoothed_hist_from_KDE");
135  sigF = (TH1*)instDir->Get( hspline );
136 
137  if (sigF) {
138  bkgF = (TH1*)instDir->Get( hspline.ReplaceAll("_tr_S","_tr_B") );
139  }
140  }
141 
142  if ((sigF == NULL || bkgF == NULL) &&!hname.Contains("hist") ) {
143  cout << "*** probas.C: big troubles - did not find probability histograms" << endl;
144  return;
145  }
146  else {
147  // remove the signal suffix
148 
149  // check that exist
150  if (NULL != sigF && NULL != bkgF && NULL!=sig && NULL!=bgd) {
151 
152  found = kTRUE;
153  // chop off useless stuff
154  sig->SetTitle( TString("TMVA output for classifier: ") + methodTitle );
155 
156  // create new canvas
157  cout << "--- Book canvas no: " << countCanvas << endl;
158  char cn[20];
159  sprintf( cn, "canvas%d", countCanvas+1 );
160  c = new TCanvas( cn, Form("TMVA Output Fit Variables %s",methodTitle.Data()),
161  countCanvas*50+200, countCanvas*20, width, width*0.78 );
162 
163  // set the histogram style
166 
167  // frame limits (choose judicuous x range)
168  Float_t nrms = 4;
169  Float_t xmin = TMath::Max( TMath::Min(sig->GetMean() - nrms*sig->GetRMS(),
170  bgd->GetMean() - nrms*bgd->GetRMS() ),
171  sig->GetXaxis()->GetXmin() );
172  Float_t xmax = TMath::Min( TMath::Max(sig->GetMean() + nrms*sig->GetRMS(),
173  bgd->GetMean() + nrms*bgd->GetRMS() ),
174  sig->GetXaxis()->GetXmax() );
175  Float_t ymin = 0;
176  Float_t ymax = TMath::Max( sig->GetMaximum(), bgd->GetMaximum() )*1.5;
177 
178  if (Draw_CFANN_Logy && methodName == "CFANN") ymin = 0.01;
179 
180  // build a frame
181  Int_t nb = 500;
182  TH2F* frame = new TH2F( TString("frame") + sig->GetName() + "_proba", sig->GetTitle(),
183  nb, xmin, xmax, nb, ymin, ymax );
184  frame->GetXaxis()->SetTitle(methodTitle);
185  frame->GetYaxis()->SetTitle("Normalized");
186  TMVAGlob::SetFrameStyle( frame );
187 
188  // eventually: draw the frame
189  frame->Draw();
190 
191  if (Draw_CFANN_Logy && methodName == "CFANN") c->SetLogy();
192 
193  // overlay signal and background histograms
195  sig->SetMarkerSize( 0.7 );
196  sig->SetMarkerStyle( 20 );
197  sig->SetLineWidth(1);
198 
200  bgd->SetMarkerSize( 0.7 );
201  bgd->SetMarkerStyle( 24 );
202  bgd->SetLineWidth(1);
203 
204  sig->Draw("samee");
205  bgd->Draw("samee");
206 
207  sigF->SetFillStyle( 0 );
208  bkgF->SetFillStyle( 0 );
209  sigF->Draw("samehist");
210  bkgF->Draw("samehist");
211 
212  // redraw axes
213  frame->Draw("sameaxis");
214 
215  // Draw legend
216  TLegend *legend= new TLegend( c->GetLeftMargin(), 1 - c->GetTopMargin() - 0.2,
217  c->GetLeftMargin() + 0.4, 1 - c->GetTopMargin() );
218  legend->AddEntry(sig,"Signal data","P");
219  legend->AddEntry(sigF,"Signal PDF","L");
220  legend->AddEntry(bgd,"Background data","P");
221  legend->AddEntry(bkgF,"Background PDF","L");
222  legend->Draw("same");
223  legend->SetBorderSize(1);
224  legend->SetMargin( 0.3 );
225 
226  // save canvas to file
227  c->Update();
229  sprintf( fname, "%s/plots/mva_pdf_%s_c%i",dataset.Data(), methodTitle.Data(), countCanvas+1 );
230  if (Save_Images) TMVAGlob::imgconv( c, fname );
231  countCanvas++;
232  }
233  }
234  }
235 
236  }
237  if(!found){
238  cout << "--- No PDFs found for method " << methodTitle << ". Did you request \"CreateMVAPdfs\" in the option string?" << endl;
239  }
240  }
241  }
242 }
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition: TAttLine.h:49
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:7664
void imgconv(TCanvas *c, const TString &fname)
Definition: tmvaglob.cxx:212
float xmin
Definition: THbookFile.cxx:93
Float_t GetLeftMargin() const
Definition: TAttPad.h:46
This class displays a legend box (TPaveText) containing several legend entries.
Definition: TLegend.h:27
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
Definition: TDirectory.cxx:729
float Float_t
Definition: RtypesCore.h:53
return c
float ymin
Definition: THbookFile.cxx:93
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:635
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:373
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:50
void probas(TString dataset, TString fin="TMVA.root", Bool_t useTMVAStyle=kTRUE)
Definition: probas.cxx:40
void SetSignalAndBackgroundStyle(TH1 *sig, TH1 *bkg, TH1 *all=0)
Definition: tmvaglob.cxx:8
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:6760
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: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:72
Double_t GetRMS(Int_t axis=1) const
Definition: TH1.h:320
const Bool_t kFALSE
Definition: Rtypes.h:92
virtual void SetFillStyle(Style_t fstyle)
Set the fill area style.
Definition: TAttFill.h:44
Int_t getSignalLine()
Definition: tmvaglob.h:49
TLegend * legend
Definition: pirndm.C:35
Double_t GetXmin() const
Definition: TAxis.h:139
Int_t getBackgroundLine()
Definition: tmvaglob.h:51
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition: TAttMarker.h:43
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:30
TText * th1
Definition: textalign.C:13
A doubly linked list.
Definition: TList.h:47
float ymax
Definition: THbookFile.cxx:93
std::string GetMethodName(TCppMethod_t)
Definition: Cppyy.cxx:728
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:255
unsigned int UInt_t
Definition: RtypesCore.h:42
char * Form(const char *fmt,...)
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition: TAttMarker.h:45
TAxis * GetYaxis()
Definition: TH1.h:325
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".
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
Definition: TAttMarker.h:46
The Canvas class.
Definition: TCanvas.h:41
UInt_t GetListOfMethods(TList &methods, TDirectory *dir=0)
Definition: tmvaglob.cxx:581
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:44
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:567
The TH1 histogram class.
Definition: TH1.h:80
virtual TObject * ReadObj()
To read a TObject* from the file.
Definition: TKey.cxx:730
Float_t GetTopMargin() const
Definition: TAttPad.h:48
Definition: file.py:1
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:202
#define NULL
Definition: Rtypes.h:82
virtual void SetTitle(const char *title)
Change (i.e.
Definition: TH1.cxx:6027
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2183
const Bool_t kTRUE
Definition: Rtypes.h:91
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:308
Double_t GetXmax() const
Definition: TAxis.h:140
virtual void SetBorderSize(Int_t bordersize=4)
Definition: TPave.h:74
TAxis * GetXaxis()
Definition: TH1.h:324
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:52
const char * Data() const
Definition: TString.h:349
UInt_t GetListOfTitles(TDirectory *rfdir, TList &titles)
Definition: tmvaglob.cxx:634
virtual void SetLogy(Int_t value=1)
Set Lin/Log scale for Y.
Definition: TPad.cxx:5347