ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 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 =
51  TMVAGlob::OpenFile( fin );
52 
53  const Int_t width = 600; // size of canvas
54 
55  // this defines how many canvases we need
56  TCanvas *c = 0;
57 
58  // counter variables
59  Int_t countCanvas = 0;
60 
61  // list of existing MVAs
62  //const Int_t nveto = 1;
63  TString suffixSig = "_tr_S";
64  TString suffixBgd = "_tr_B";
65 
66  // search for the right histograms in full list of keys
67  TList methods;
68  UInt_t nmethods = TMVAGlob::GetListOfMethods( methods );
69  if (nmethods==0) {
70  cout << "--- Probas.C: no methods found!" << endl;
71  return;
72  }
73  TIter next(&methods);
74  TKey *key, *hkey;
75  char fname[200];
76  TH1* sig(0);
77  TH1* bgd(0);
78 
79 
80  while ( (key = (TKey*)next()) ) {
81  TDirectory * mDir = (TDirectory*)key->ReadObj();
82  TList titles;
83  UInt_t ni = TMVAGlob::GetListOfTitles( mDir, titles );
84  TString methodName;
85  TMVAGlob::GetMethodName(methodName,key);
86  if (ni==0) {
87  cout << "+++ No titles found for classifier: " << methodName << endl;
88  return;
89  }
90  TIter nextTitle(&titles);
91  TKey *instkey;
92  TDirectory *instDir;
93 
94  // iterate over all classifiers
95  while ( (instkey = (TKey *)nextTitle()) ) {
96  instDir = (TDirectory *)instkey->ReadObj();
97  TString instName = instkey->GetName();
98  TList h1hists;
99  UInt_t nhists = TMVAGlob::GetListOfKeys( h1hists, "TH1", instDir );
100  if (nhists==0) cout << "*** No histograms found!" << endl;
101  TIter nextInDir(&h1hists);
102  TString methodTitle;
103  TMVAGlob::GetMethodTitle(methodTitle,instDir);
104  Bool_t found = kFALSE;
105  while ( (hkey = (TKey*)nextInDir()) ) {
106  TH1 *th1 = (TH1*)hkey->ReadObj();
107  TString hname= th1->GetName();
108  if (hname.Contains( suffixSig ) && !hname.Contains( "Cut") &&
109  !hname.Contains("original") && !hname.Contains("smoothed")) {
110  // retrieve corresponding signal and background histograms
111  TString hnameS = hname;
112  TString hnameB = hname; hnameB.ReplaceAll("_S","_B");
113 
114  sig = (TH1*)instDir->Get( hnameS );
115  bgd = (TH1*)instDir->Get( hnameB );
116 
117  if (sig == 0 || bgd == 0) {
118  cout << "*** probas.C: big troubles in probas.... histogram: " << hname << " not found" << endl;
119  return;
120  }
121 
122  TH1* sigF(0);
123  TH1* bkgF(0);
124 
125  for (int i=0; i<= 5; i++) {
126  TString hspline = hnameS + Form("_smoothed_hist_from_spline%i",i);
127  sigF = (TH1*)instDir->Get( hspline );
128 
129  if (sigF) {
130  bkgF = (TH1*)instDir->Get( hspline.ReplaceAll("_tr_S","_tr_B") );
131  break;
132  }
133  }
134  if (!sigF){
135  TString hspline = hnameS + TString("_smoothed_hist_from_KDE");
136  sigF = (TH1*)instDir->Get( hspline );
137 
138  if (sigF) {
139  bkgF = (TH1*)instDir->Get( hspline.ReplaceAll("_tr_S","_tr_B") );
140  }
141  }
142 
143  if ((sigF == NULL || bkgF == NULL) &&!hname.Contains("hist") ) {
144  cout << "*** probas.C: big troubles - did not find probability histograms" << endl;
145  return;
146  }
147  else {
148  // remove the signal suffix
149 
150  // check that exist
151  if (NULL != sigF && NULL != bkgF && NULL!=sig && NULL!=bgd) {
152 
153  found = kTRUE;
154  // chop off useless stuff
155  sig->SetTitle( TString("TMVA output for classifier: ") + methodTitle );
156 
157  // create new canvas
158  cout << "--- Book canvas no: " << countCanvas << endl;
159  char cn[20];
160  sprintf( cn, "canvas%d", countCanvas+1 );
161  c = new TCanvas( cn, Form("TMVA Output Fit Variables %s",methodTitle.Data()),
162  countCanvas*50+200, countCanvas*20, width, width*0.78 );
163 
164  // set the histogram style
167 
168  // frame limits (choose judicuous x range)
169  Float_t nrms = 4;
170  Float_t xmin = TMath::Max( TMath::Min(sig->GetMean() - nrms*sig->GetRMS(),
171  bgd->GetMean() - nrms*bgd->GetRMS() ),
172  sig->GetXaxis()->GetXmin() );
173  Float_t xmax = TMath::Min( TMath::Max(sig->GetMean() + nrms*sig->GetRMS(),
174  bgd->GetMean() + nrms*bgd->GetRMS() ),
175  sig->GetXaxis()->GetXmax() );
176  Float_t ymin = 0;
177  Float_t ymax = TMath::Max( sig->GetMaximum(), bgd->GetMaximum() )*1.5;
178 
179  if (Draw_CFANN_Logy && methodName == "CFANN") ymin = 0.01;
180 
181  // build a frame
182  Int_t nb = 500;
183  TH2F* frame = new TH2F( TString("frame") + sig->GetName() + "_proba", sig->GetTitle(),
184  nb, xmin, xmax, nb, ymin, ymax );
185  frame->GetXaxis()->SetTitle(methodTitle);
186  frame->GetYaxis()->SetTitle("Normalized");
187  TMVAGlob::SetFrameStyle( frame );
188 
189  // eventually: draw the frame
190  frame->Draw();
191 
192  if (Draw_CFANN_Logy && methodName == "CFANN") c->SetLogy();
193 
194  // overlay signal and background histograms
196  sig->SetMarkerSize( 0.7 );
197  sig->SetMarkerStyle( 20 );
198  sig->SetLineWidth(1);
199 
201  bgd->SetMarkerSize( 0.7 );
202  bgd->SetMarkerStyle( 24 );
203  bgd->SetLineWidth(1);
204 
205  sig->Draw("samee");
206  bgd->Draw("samee");
207 
208  sigF->SetFillStyle( 0 );
209  bkgF->SetFillStyle( 0 );
210  sigF->Draw("samehist");
211  bkgF->Draw("samehist");
212 
213  // redraw axes
214  frame->Draw("sameaxis");
215 
216  // Draw legend
217  TLegend *legend= new TLegend( c->GetLeftMargin(), 1 - c->GetTopMargin() - 0.2,
218  c->GetLeftMargin() + 0.4, 1 - c->GetTopMargin() );
219  legend->AddEntry(sig,"Signal data","P");
220  legend->AddEntry(sigF,"Signal PDF","L");
221  legend->AddEntry(bgd,"Background data","P");
222  legend->AddEntry(bkgF,"Background PDF","L");
223  legend->Draw("same");
224  legend->SetBorderSize(1);
225  legend->SetMargin( 0.3 );
226 
227  // save canvas to file
228  c->Update();
230  sprintf( fname, "plots/mva_pdf_%s_c%i", methodTitle.Data(), countCanvas+1 );
231  if (Save_Images) TMVAGlob::imgconv( c, fname );
232  countCanvas++;
233  }
234  }
235  }
236 
237  }
238  if(!found){
239  cout << "--- No PDFs found for method " << methodTitle << ". Did you request \"CreateMVAPdfs\" in the option string?" << endl;
240  }
241  }
242  }
243 }
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
This class displays a legend box (TPaveText) containing several legend entries.
Definition: TLegend.h:35
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
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
void SetSignalAndBackgroundStyle(TH1 *sig, TH1 *bkg, TH1 *all=0)
Definition: tmvaglob.cxx:8
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:80
const Bool_t kFALSE
Definition: Rtypes.h:92
virtual void SetFillStyle(Style_t fstyle)
Definition: TAttFill.h:52
TLegend * legend
Definition: pirndm.C:35
const char * Data() const
Definition: TString.h:349
Float_t GetTopMargin() const
Definition: TAttPad.h:56
virtual void SetMarkerColor(Color_t mcolor=1)
Definition: TAttMarker.h:51
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
Double_t GetXmin() const
Definition: TAxis.h:137
void probas(TString fin="TMVA.root", Bool_t useTMVAStyle=kTRUE)
Definition: probas.cxx:40
TText * th1
Definition: textalign.C:13
A doubly linked list.
Definition: TList.h:47
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:706
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
unsigned int UInt_t
Definition: RtypesCore.h:42
char * Form(const char *fmt,...)
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
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 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
UInt_t GetListOfMethods(TList &methods, TDirectory *dir=0)
Definition: tmvaglob.cxx:581
static Int_t c_SignalLine
Definition: tmvaglob.h:49
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
Double_t GetXmax() const
Definition: TAxis.h:138
The TH1 histogram class.
Definition: TH1.h:80
virtual TObject * ReadObj()
To read a TObject* from the file.
Definition: TKey.cxx:727
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:202
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:567
#define NULL
Definition: Rtypes.h:82
virtual void SetTitle(const char *title)
Change (i.e.
Definition: TH1.cxx:6268
static Int_t c_BackgroundLine
Definition: tmvaglob.h:51
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
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:5314