Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
probas.cxx
Go to the documentation of this file.
1#include "TCanvas.h"
2#include "TFile.h"
3#include "TH2F.h"
4#include "TIterator.h"
5#include "TKey.h"
6#include "TLegend.h"
7#include "TList.h"
8#include "TMVA/probas.h"
9#include "TMVA/tmvaglob.h"
10#include "TString.h"
11
12#include <iostream>
13
14using std::cout;
15using std::endl;
16
17// this macro plots the MVA probability distributions (Signal and
18// Background overlayed) of different MVA methods run in TMVA
19// (e.g. running TMVAnalysis.C).
20
21// input: - Input file (result from TMVA)
22// - use of TMVA plotting TStyle
23void TMVA::probas(TString dataset, TString fin , Bool_t useTMVAStyle )
24{
25 // set style and remove existing canvas'
26 TMVAGlob::Initialize( useTMVAStyle );
27
28 // switches
29 const Bool_t Draw_CFANN_Logy = kFALSE;
30 const Bool_t Save_Images = kTRUE;
31
32 // checks if file with name "fin" is already open, and if not opens one
33 TFile* file = TMVAGlob::OpenFile( fin );
34
35 const Int_t width = 600; // size of canvas
36
37 // this defines how many canvases we need
38 TCanvas *c = 0;
39
40 // counter variables
41 Int_t countCanvas = 0;
42
43 // list of existing MVAs
44 //const Int_t nveto = 1;
45 TString suffixSig = "_tr_S";
46 TString suffixBgd = "_tr_B";
47
48 // search for the right histograms in full list of keys
49 TList methods;
50 UInt_t nmethods = TMVAGlob::GetListOfMethods( methods,file->GetDirectory(dataset.Data()) );
51 if (nmethods==0) {
52 cout << "--- Probas.C: no methods found!" << endl;
53 return;
54 }
55 TIter next(&methods);
56 TKey *key, *hkey;
57 constexpr std::size_t fnameSize = 200;
58 char fname[fnameSize];
59 TH1* sig(0);
60 TH1* bgd(0);
61
62
63 while ( (key = (TKey*)next()) ) {
64 TDirectory * mDir = (TDirectory*)key->ReadObj();
65 TList titles;
66 UInt_t ni = TMVAGlob::GetListOfTitles( mDir, titles );
67 TString methodName;
68 TMVAGlob::GetMethodName(methodName,key);
69 if (ni==0) {
70 cout << "+++ No titles found for classifier: " << methodName << endl;
71 return;
72 }
73 TIter nextTitle(&titles);
74 TKey *instkey;
75 TDirectory *instDir;
76
77 // iterate over all classifiers
78 while ( (instkey = (TKey *)nextTitle()) ) {
79 instDir = (TDirectory *)instkey->ReadObj();
80 TString instName = instkey->GetName();
81 TList h1hists;
82 UInt_t nhists = TMVAGlob::GetListOfKeys( h1hists, "TH1", instDir );
83 if (nhists==0) cout << "*** No histograms found!" << endl;
84 TIter nextInDir(&h1hists);
85 TString methodTitle;
86 TMVAGlob::GetMethodTitle(methodTitle,instDir);
87 Bool_t found = kFALSE;
88 while ( (hkey = (TKey*)nextInDir()) ) {
89 TH1 *th1 = (TH1*)hkey->ReadObj();
90 TString hname= th1->GetName();
91 if (hname.Contains( suffixSig ) && !hname.Contains( "Cut") &&
92 !hname.Contains("original") && !hname.Contains("smoothed")) {
93 // retrieve corresponding signal and background histograms
94 TString hnameS = hname;
95 TString hnameB = hname; hnameB.ReplaceAll("_S","_B");
96
97 sig = (TH1*)instDir->Get( hnameS );
98 bgd = (TH1*)instDir->Get( hnameB );
99
100 if (sig == 0 || bgd == 0) {
101 cout << "*** probas.C: big troubles in probas.... histogram: " << hname << " not found" << endl;
102 return;
103 }
104
105 TH1* sigF(0);
106 TH1* bkgF(0);
107
108 for (int i=0; i<= 5; i++) {
109 TString hspline = hnameS + TString::Format("_smoothed_hist_from_spline%i",i);
110 sigF = (TH1*)instDir->Get( hspline );
111
112 if (sigF) {
113 bkgF = (TH1*)instDir->Get( hspline.ReplaceAll("_tr_S","_tr_B") );
114 break;
115 }
116 }
117 if (!sigF){
118 TString hspline = hnameS + TString("_smoothed_hist_from_KDE");
119 sigF = (TH1*)instDir->Get( hspline );
120
121 if (sigF) {
122 bkgF = (TH1*)instDir->Get( hspline.ReplaceAll("_tr_S","_tr_B") );
123 }
124 }
125
126 if ((sigF == NULL || bkgF == NULL) &&!hname.Contains("hist") ) {
127 cout << "*** probas.C: big troubles - did not find probability histograms" << endl;
128 return;
129 }
130 else {
131 // remove the signal suffix
132
133 // check that exist
134 if (NULL != sigF && NULL != bkgF && NULL!=sig && NULL!=bgd) {
135
136 found = kTRUE;
137 // chop off useless stuff
138 sig->SetTitle( TString("TMVA output for classifier: ") + methodTitle );
139
140 // create new canvas
141 cout << "--- Book canvas no: " << countCanvas << endl;
142 char cn[20];
143 snprintf( cn, 20, "canvas%d", countCanvas+1 );
144 c = new TCanvas( cn, TString::Format("TMVA Output Fit Variables %s",methodTitle.Data()),
145 countCanvas*50+200, countCanvas*20, width, width*0.78 );
146
147 // set the histogram style
150
151 // frame limits (choose judicuous x range)
152 Float_t nrms = 4;
153 Float_t xmin = TMath::Max( TMath::Min(sig->GetMean() - nrms*sig->GetRMS(),
154 bgd->GetMean() - nrms*bgd->GetRMS() ),
155 sig->GetXaxis()->GetXmin() );
156 Float_t xmax = TMath::Min( TMath::Max(sig->GetMean() + nrms*sig->GetRMS(),
157 bgd->GetMean() + nrms*bgd->GetRMS() ),
158 sig->GetXaxis()->GetXmax() );
159 Float_t ymin = 0;
160 Float_t ymax = TMath::Max( sig->GetMaximum(), bgd->GetMaximum() )*1.5;
161
162 if (Draw_CFANN_Logy && methodName == "CFANN") ymin = 0.01;
163
164 // build a frame
165 Int_t nb = 500;
166 TH2F* frame = new TH2F( TString("frame") + sig->GetName() + "_proba", sig->GetTitle(),
167 nb, xmin, xmax, nb, ymin, ymax );
168 frame->GetXaxis()->SetTitle(methodTitle);
169 frame->GetYaxis()->SetTitle("Normalized");
171
172 // eventually: draw the frame
173 frame->Draw();
174
175 if (Draw_CFANN_Logy && methodName == "CFANN") c->SetLogy();
176
177 // overlay signal and background histograms
178 sig->SetMarkerColor( TMVAGlob::getSignalLine() );
179 sig->SetMarkerSize( 0.7 );
180 sig->SetMarkerStyle( 20 );
181 sig->SetLineWidth(1);
182
183 bgd->SetMarkerColor( TMVAGlob::getBackgroundLine() );
184 bgd->SetMarkerSize( 0.7 );
185 bgd->SetMarkerStyle( 24 );
186 bgd->SetLineWidth(1);
187
188 sig->Draw("samee");
189 bgd->Draw("samee");
190
191 sigF->SetFillStyle( 0 );
192 bkgF->SetFillStyle( 0 );
193 sigF->Draw("samehist");
194 bkgF->Draw("samehist");
195
196 // redraw axes
197 frame->Draw("sameaxis");
198
199 // Draw legend
200 TLegend *legend= new TLegend( c->GetLeftMargin(), 1 - c->GetTopMargin() - 0.2,
201 c->GetLeftMargin() + 0.4, 1 - c->GetTopMargin() );
202 legend->AddEntry(sig,"Signal data","P");
203 legend->AddEntry(sigF,"Signal PDF","L");
204 legend->AddEntry(bgd,"Background data","P");
205 legend->AddEntry(bkgF,"Background PDF","L");
206 legend->Draw("same");
207 legend->SetBorderSize(1);
208 legend->SetMargin( 0.3 );
209
210 // save canvas to file
211 c->Update();
213 snprintf( fname, fnameSize, "%s/plots/mva_pdf_%s_c%i",dataset.Data(), methodTitle.Data(), countCanvas+1 );
214 if (Save_Images) TMVAGlob::imgconv( c, fname );
215 countCanvas++;
216 }
217 }
218 }
219
220 }
221 if(!found){
222 cout << "--- No PDFs found for method " << methodTitle << ". Did you request \"CreateMVAPdfs\" in the option string?" << endl;
223 }
224 }
225 }
226}
#define c(i)
Definition RSha256.hxx:101
float Float_t
Definition RtypesCore.h:57
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
Option_t Option_t width
float xmin
float ymin
float xmax
float ymax
#define snprintf
Definition civetweb.c:1540
The Canvas class.
Definition TCanvas.h:23
TDirectory * GetDirectory(const char *apath, Bool_t printError=false, const char *funcname="GetDirectory") override
Find a directory named "apath".
Describe directory structure in memory.
Definition TDirectory.h:45
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:59
TAxis * GetXaxis()
Definition TH1.h:324
TAxis * GetYaxis()
Definition TH1.h:325
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3066
2-D histogram with a float per channel (see TH1 documentation)
Definition TH2.h:307
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition TKey.h:28
virtual TObject * ReadObj()
To read a TObject* from the file.
Definition TKey.cxx:759
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:320
void Draw(Option_t *option="") override
Draw this legend with its current attributes.
Definition TLegend.cxx:425
void SetMargin(Float_t margin)
Definition TLegend.h:69
A doubly linked list.
Definition TList.h:38
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
virtual void SetBorderSize(Int_t bordersize=4)
Sets the border size of the TPave box and shadow.
Definition TPave.h:77
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:376
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition TString.h:704
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:2378
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:632
UInt_t GetListOfTitles(TDirectory *rfdir, TList &titles)
Definition tmvaglob.cxx:643
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
void SetSignalAndBackgroundStyle(TH1 *sig, TH1 *bkg, TH1 *all=nullptr)
Definition tmvaglob.cxx:8
TFile * OpenFile(const TString &fin)
Definition tmvaglob.cxx:192
void SetFrameStyle(TH1 *frame, Float_t scale=1.0)
Definition tmvaglob.cxx:77
Int_t getBackgroundLine()
Definition tmvaglob.h:51
UInt_t GetListOfMethods(TList &methods, TDirectory *dir=nullptr)
Definition tmvaglob.cxx:590
void GetMethodName(TString &name, TKey *mkey)
Definition tmvaglob.cxx:342
Int_t getSignalLine()
Definition tmvaglob.h:49
UInt_t GetListOfKeys(TList &keys, TString inherits, TDirectory *dir=nullptr)
Definition tmvaglob.cxx:382
void imgconv(TCanvas *c, const TString &fname)
Definition tmvaglob.cxx:212
void probas(TString dataset, TString fin="TMVA.root", Bool_t useTMVAStyle=kTRUE)
Short_t Max(Short_t a, Short_t b)
Returns the largest of a and b.
Definition TMathBase.h:250
Short_t Min(Short_t a, Short_t b)
Returns the smallest of a and b.
Definition TMathBase.h:198
auto * th1
Definition textalign.C:14