Logo ROOT  
Reference Guide
training_history.cxx
Go to the documentation of this file.
2
3#include "TH2F.h"
4#include "TFile.h"
5#include "TIterator.h"
6#include "TKey.h"
7
8void TMVA::plot_training_history(TString dataset, TFile* /*file*/, TDirectory* BinDir)
9{
10
11 Bool_t __PLOT_LOGO__ = kTRUE;
12 Bool_t __SAVE_IMAGE__ = kTRUE;
13
14 // the coordinates
15 Float_t x1 = 999;
16 Float_t x2 = -999;
17 Float_t y1 = 999.;
18 Float_t y2 = -999;
19
20 // create canvas
21 TCanvas* c = new TCanvas( "c", "the canvas", 200, 0, 650, 500 );
22
23 // global style settings
24 c->SetGrid();
25 c->SetTicks();
26
27 // legend
28 Float_t x0L = 0.107, y0H = 0.899;
29 Float_t dxL = 0.457-x0L, dyH = 0.22;
30
31 TLegend *legend = new TLegend( x0L, y0H-dyH, x0L+dxL, y0H );
32 //legend->SetTextSize( 0.05 );
33 legend->SetHeader( "MVA Method:" );
34 legend->SetMargin( 0.4 );
35
36 TString xtit = "Training Step";
37 TString ytit = "";
38
39 TString ftit = "Training History";
40
41 TString hNameRef = "";//training_history";
42
43 TList xhists;
44 TList xmethods;
45 UInt_t xnm = TMVAGlob::GetListOfMethods( xmethods ,BinDir);
46 if (xnm==0){
47 cout << "ups .. no methods found in to plot training history for ... give up" << endl;
48 return;
49 }
50 TIter xnext(&xmethods);
51 // loop over all methods
52 TKey *xkey;
53 while ((xkey = (TKey*)xnext())) {
54 TDirectory * mDir = (TDirectory*)xkey->ReadObj();
55 TList titles;
56 UInt_t ninst = TMVAGlob::GetListOfTitles(mDir,titles);
57 if (ninst==0) cout << "hmm... sorry, but this printout was supposed to be only to keep the compiler quite.. never supposed to happen :(" << endl;
58 TIter nextTitle(&titles);
59 TKey *titkey;
60 TDirectory *titDir;
61 while ((titkey = TMVAGlob::NextKey(nextTitle,"TDirectory"))) {
62 titDir = (TDirectory *)titkey->ReadObj();
63 TString methodTitle;
64 TMVAGlob::GetMethodTitle(methodTitle,titDir);
65 TIter nextKey( titDir->GetListOfKeys() );
66 TKey *hkey2;
67 while ((hkey2 = TMVAGlob::NextKey(nextKey,"TH1"))) {
68 TH1 *h = (TH1*)hkey2->ReadObj();
69 TString hname = h->GetName();
70 if (hname.Contains( hNameRef ) && hname.BeginsWith( "TrainingHistory_" )) {
71
72 if (h->GetMaximum() > y2) y2 = h->GetMaximum()*1.1;
73 if (h->GetMinimum() < y1) y1 = h->GetMinimum();
74 if (h->GetBinLowEdge(0) < x1 ) x1 = h->GetBinLowEdge(0);
75 if (h->GetBinLowEdge(h->GetNbinsX()+1 ) > x2 ) x2 = h->GetBinLowEdge(h->GetNbinsX()+1 );
76
77 }
78 }
79 }
80 }
81
82
83 // draw empty frame
84 if(gROOT->FindObject("frame")!=0) gROOT->FindObject("frame")->Delete();
85 TH2F* frame = new TH2F( "frame", ftit, 500, x1, x2, 500, y1, y2 );
86 frame->GetXaxis()->SetTitle( xtit );
87 frame->GetYaxis()->SetTitle( ytit );
88 TMVAGlob::SetFrameStyle( frame, 1.0 );
89
90 frame->Draw();
91
92 Int_t color = 1;
93 Int_t nmva = 0;
94 TKey *key;
95
96 TList hists;
97 TList methods;
98 UInt_t nm = TMVAGlob::GetListOfMethods( methods,BinDir );
99 if (nm==0){
100 cout << "ups .. no methods found in to plot ROC curve for ... give up" << endl;
101 return;
102 }
103 // TIter next(file->GetListOfKeys());
104 TIter next(&methods);
105
106 // loop over all methods
107 while ((key = (TKey*)next())) {
108 TDirectory * mDir = (TDirectory*)key->ReadObj();
109 TList titles;
110 UInt_t ninst = TMVAGlob::GetListOfTitles(mDir,titles);
111 if (ninst==0) cout << "hmm... sorry, but this printout was supposed to be only to keep the compiler quite.. never supposed to happen :(" << endl;
112 TIter nextTitle(&titles);
113 TKey *titkey;
114 TDirectory *titDir;
115 while ((titkey = TMVAGlob::NextKey(nextTitle,"TDirectory"))) {
116 titDir = (TDirectory *)titkey->ReadObj();
117 TString methodTitle;
118 TMVAGlob::GetMethodTitle(methodTitle,titDir);
119 TIter nextKey( titDir->GetListOfKeys() );
120 TKey *hkey2;
121 while ((hkey2 = TMVAGlob::NextKey(nextKey,"TH1"))) {
122 TH1 *h = (TH1*)hkey2->ReadObj();
123 TString hname = h->GetName();
124 if (hname.Contains( hNameRef ) && hname.BeginsWith( "TrainingHistory_" )) {
125 h->SetLineWidth(3);
126 h->SetLineColor(color);
127 color++; if (color == 5 || color == 10 || color == 11) color++;
128 h->Draw("csame");
129 hists.Add(h);
130 nmva++;
131 }
132 }
133 }
134 }
135
136 while (hists.GetSize()) {
137 TListIter hIt(&hists);
138 TH1* hist(0);
139 Double_t largestInt=-1;
140 TH1* histWithLargestInt(0);
141 while ((hist = (TH1*)hIt())!=0) {
142 Double_t integral = hist->Integral(1,hist->FindBin(0.9999));
143 if (integral>largestInt) {
144 largestInt = integral;
145 histWithLargestInt = hist;
146 }
147 }
148 if (histWithLargestInt == 0) {
149 cout << "ERROR - unknown hist \"histWithLargestInt\" --> serious problem in ROOT file" << endl;
150 break;
151 }
152 legend->AddEntry(histWithLargestInt,TString(histWithLargestInt->GetTitle()).ReplaceAll("MVA_",""),"l");
153 hists.Remove(histWithLargestInt);
154 }
155
156 // rescale legend box size
157 // current box size has been tuned for 3 MVAs + 1 title
158 dyH *= (Float_t(TMath::Min(10,nmva) - 3.0)/4.0);
159 legend->SetY2( y0H + dyH);
160
161 // redraw axes
162 frame->Draw("sameaxis");
163 legend->Draw("same");
164
165 // ============================================================
166
167 if (__PLOT_LOGO__) TMVAGlob::plot_logo();
168
169 // ============================================================
170
171 c->Update();
172
173 TString fname = dataset+"/plots/" + hNameRef;
174 if (TString(BinDir->GetName()).Contains("multicut")){
175 TString fprepend(BinDir->GetName());
176 fprepend.ReplaceAll("multicutMVA_","");
177 fname = dataset+"plots/" + fprepend + "_" + hNameRef;
178 }
179 if (__SAVE_IMAGE__) TMVAGlob::imgconv( c, fname );
180
181 return;
182}
183
184void TMVA::training_history(TString dataset, TString fin , Bool_t useTMVAStyle )
185{
186
187 // set style and remove existing canvas'
188 TMVAGlob::Initialize( useTMVAStyle );
189
190 // checks if file with name "fin" is already open, and if not opens one
191 TFile* file = TMVAGlob::OpenFile( fin );
192
193 plot_training_history(dataset, file, file->GetDirectory(dataset.Data()));
194
195 return;
196}
197
#define c(i)
Definition: RSha256.hxx:101
#define h(i)
Definition: RSha256.hxx:106
static const double x2[5]
static const double x1[5]
int Int_t
Definition: RtypesCore.h:41
unsigned int UInt_t
Definition: RtypesCore.h:42
bool Bool_t
Definition: RtypesCore.h:59
double Double_t
Definition: RtypesCore.h:55
float Float_t
Definition: RtypesCore.h:53
const Bool_t kTRUE
Definition: RtypesCore.h:87
#define gROOT
Definition: TROOT.h:415
The Canvas class.
Definition: TCanvas.h:31
virtual Int_t GetSize() const
Return the capacity of the collection, i.e.
Definition: TCollection.h:182
Describe directory structure in memory.
Definition: TDirectory.h:34
virtual TList * GetListOfKeys() const
Definition: TDirectory.h:160
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:48
The TH1 histogram class.
Definition: TH1.h:56
TAxis * GetXaxis()
Get the behaviour adopted by the object about the statoverflows. See EStatOverflows for more informat...
Definition: TH1.h:316
TAxis * GetYaxis()
Definition: TH1.h:317
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2998
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:251
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:24
virtual TObject * ReadObj()
To read a TObject* from the file.
Definition: TKey.cxx:729
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:330
virtual void SetHeader(const char *header="", Option_t *option="")
Sets the header, which is the "title" that appears at the top of the legend.
Definition: TLegend.cxx:1099
virtual void Draw(Option_t *option="")
Draw this legend with its current attributes.
Definition: TLegend.cxx:423
void SetMargin(Float_t margin)
Definition: TLegend.h:69
Iterator of linked list.
Definition: TList.h:200
A doubly linked list.
Definition: TList.h:44
virtual void Add(TObject *obj)
Definition: TList.h:87
virtual TObject * Remove(TObject *obj)
Remove object from the list.
Definition: TList.cxx:819
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:164
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
virtual void SetY2(Double_t y2)
Set the Y2 value.
Definition: TPave.cxx:686
Basic string class.
Definition: TString.h:131
const char * Data() const
Definition: TString.h:364
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:687
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition: TString.h:610
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:619
static constexpr double nm
UInt_t GetListOfTitles(TDirectory *rfdir, TList &titles)
Definition: tmvaglob.cxx:636
void Initialize(Bool_t useTMVAStyle=kTRUE)
Definition: tmvaglob.cxx:176
TKey * NextKey(TIter &keyIter, TString className)
Definition: tmvaglob.cxx:357
void GetMethodTitle(TString &name, TKey *ikey)
Definition: tmvaglob.cxx:341
void plot_logo(Float_t v_scale=1.0, Float_t skew=1.0)
Definition: tmvaglob.cxx:263
TFile * OpenFile(const TString &fin)
Definition: tmvaglob.cxx:192
void SetFrameStyle(TH1 *frame, Float_t scale=1.0)
Definition: tmvaglob.cxx:77
UInt_t GetListOfMethods(TList &methods, TDirectory *dir=0)
Definition: tmvaglob.cxx:583
void imgconv(TCanvas *c, const TString &fname)
Definition: tmvaglob.cxx:212
void plot_training_history(TString dataset, TFile *file, TDirectory *BinDir=0)
void training_history(TString dataset, TString fin="TMVA.root", Bool_t useTMVAStyle=kTRUE)
Short_t Min(Short_t a, Short_t b)
Definition: TMathBase.h:180
Definition: file.py:1