Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
BDTControlPlots.cxx
Go to the documentation of this file.
2#include <vector>
3#include <string>
4
5
6#include "TH1.h"
7#include "TGraph.h"
8
9// input: - Input file (result from TMVA),
10// - use of TMVA plotting TStyle
11
12void TMVA::BDTControlPlots(TString dataset, TString fin , Bool_t useTMVAStyle )
13{
14 // set style and remove existing canvas'
15 TMVAGlob::Initialize( useTMVAStyle );
16
17 // checks if file with name "fin" is already open, and if not opens one
18 TFile* file = TMVAGlob::OpenFile( fin );
19
20 if (file == NULL) {
21 cout << "Problems with input file, tried to open " << fin << " but somehow did not succeed .." << endl;
22 return;
23 }
24
25 // get all titles of the method BDT
26 TList titles;
27 TString methodName = "Method_BDT";
28 UInt_t ninst = TMVAGlob::GetListOfTitles(methodName,titles,file->GetDirectory(dataset.Data()));
29 if (ninst==0) {
30 cout << "Could not locate directory 'Method_BDT' in file " << fin << endl;
31 return;
32 }
33 // loop over all titles
34 TIter keyIter(&titles);
35 TDirectory *bdtdir;
36 TKey *key;
37 while ((key = TMVAGlob::NextKey(keyIter,"TDirectory"))) {
38 bdtdir = (TDirectory *)key->ReadObj();
39 bdtcontrolplots(dataset, bdtdir );
40 }
41}
42
43void TMVA::bdtcontrolplots(TString dataset, TDirectory *bdtdir ) {
44
45 const Int_t nPlots = 6;
46
47 Int_t width = 900;
48 Int_t height = 600;
49 constexpr std::size_t bufferSize = 100;
50 char cn[bufferSize];
51 char cn2[bufferSize];
52 const TString titName = bdtdir->GetName();
53 snprintf( cn, bufferSize, "cv_%s", titName.Data() );
54 TCanvas *c = new TCanvas( cn, TString::Format( "%s Control Plots", titName.Data() ),
55 width, height );
56 c->Divide(3,2);
57
58
59
60
61 TString hname[nPlots]={"BoostMonitor","BoostWeight","BoostWeightVsTree","ErrFractHist","NodesBeforePruning",titName+"_FOMvsIterFrame"};
62
63 Bool_t BoostMonitorIsDone=kFALSE;
64
65 for (Int_t i=0; i<nPlots; i++){
66 Int_t color = 4;
67 c->cd(i+1);
68 TH1 *h = (TH1*) bdtdir->Get(hname[i]);
69
70 if (h){
71 h->SetMaximum(h->GetMaximum()*1.3);
72 h->SetMinimum( 0 );
73 h->SetMarkerColor(color);
74 h->SetMarkerSize( 0.7 );
75 h->SetMarkerStyle( 24 );
76 h->SetLineWidth(1);
77 h->SetLineColor(color);
78 if(hname[i]=="NodesBeforePruning")h->SetTitle("Nodes before/after pruning");
79 h->Draw();
80 if(hname[i]=="NodesBeforePruning"){
81 TH1 *h2 = (TH1*) bdtdir->Get("NodesAfterPruning");
82 h2->SetLineWidth(1);
83 h2->SetLineColor(2);
84 h2->Draw("same");
85 }
86 if(hname[i]=="BoostMonitor"){ // a plot only available in case DoBoostMontior option has bee set
87 TGraph *g = (TGraph*) bdtdir->Get("BoostMonitorGraph");
88 g->Draw("LP*");
89 BoostMonitorIsDone = kTRUE;
90 }
91 if(hname[i]==titName+"_FOMvsIterFrame"){ // a plot only available in case DoBoostMontior option has bee set
92 TGraph *g = (TGraph*) bdtdir->Get(titName+"_FOMvsIter");
93 g->Draw();
94 }
95 c->Update();
96 }
97 }
98
99
100 TCanvas *c2 = NULL;
101 if (BoostMonitorIsDone){
102 snprintf( cn2, bufferSize, "cv2_%s", titName.Data() );
103 c2 = new TCanvas( cn2, TString::Format( "%s BoostWeights", titName.Data() ),
104 1200, 1200 );
105 c2->Divide(5,5);
106 Int_t ipad=1;
107
108 TIter keys( bdtdir->GetListOfKeys() );
109 TKey *key;
110 // gDirectory->ls();
111 while ( (key = (TKey*)keys.Next()) && ipad < 26) {
112 TObject *obj=key->ReadObj();
113 if (obj->IsA()->InheritsFrom(TH1::Class())){
114 TH1F *hx = (TH1F*)obj;
115 TString hhname_ = obj->GetTitle();
116 if (hhname_.Contains("BoostWeightsInTreeB")){
117 c2->cd(ipad++);
118 hx->SetLineColor(4);
119 hx->Draw();
120 hhname_.ReplaceAll("TreeB","TreeS");
121 bdtdir->GetObject(hhname_.Data(),hx);
122 if (hx) {
123 hx->SetLineColor(2);
124 hx->Draw("same");
125 }
126 }
127 c2->Update();
128 }
129 }
130
131 }
132
133 // write to file
134 TString fname = dataset+TString::Format( "/plots/%s_ControlPlots", titName.Data() );
135 TMVAGlob::imgconv( c, fname );
136
137 if (c2){
138 fname = dataset+TString::Format( "/plots/%s_ControlPlots2", titName.Data() );
139 TMVAGlob::imgconv( c2, fname );
140 }
141
142 TCanvas *c3 = NULL;
143 if (BoostMonitorIsDone){
144 snprintf( cn2, bufferSize, "cv3_%s", titName.Data() );
145 c3 = new TCanvas( cn2, TString::Format( "%s Variables", titName.Data() ),
146 1200, 1200 );
147 c3->Divide(5,5);
148 Int_t ipad=1;
149
150 TIter keys( bdtdir->GetListOfKeys() );
151 TKey *key;
152 // gDirectory->ls();
153 while ( (key = (TKey*)keys.Next()) && ipad < 26) {
154 TObject *obj=key->ReadObj();
155 if (obj->IsA()->InheritsFrom(TH1::Class())){
156 TH1F *hx = (TH1F*)obj;
157 TString hname_ = obj->GetTitle();
158 if (hname_.Contains("SigVar0AtTree")){
159 c3->cd(ipad++);
160 hx->SetLineColor(4);
161 hx->Draw();
162 hname_.ReplaceAll("Sig","Bkg");
163 bdtdir->GetObject(hname_.Data(),hx);
164 if (hx) {
165 hx->SetLineColor(2);
166 hx->Draw("same");
167 }
168 }
169 c3->Update();
170 }
171 }
172
173 }
174
175
176}
177
178
#define c(i)
Definition RSha256.hxx:101
#define g(i)
Definition RSha256.hxx:105
#define h(i)
Definition RSha256.hxx:106
bool Bool_t
Definition RtypesCore.h:63
int Int_t
Definition RtypesCore.h:45
unsigned int UInt_t
Definition RtypesCore.h:46
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
Option_t Option_t width
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t height
#define snprintf
Definition civetweb.c:1540
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:40
The Canvas class.
Definition TCanvas.h:23
Bool_t InheritsFrom(const char *cl) const override
Return kTRUE if this class inherits from a class with name "classname".
Definition TClass.cxx:4874
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.
virtual TList * GetListOfKeys() const
Definition TDirectory.h:223
void GetObject(const char *namecycle, T *&ptr)
Get an object with proper type checking.
Definition TDirectory.h:212
A ROOT file is composed of a header, followed by consecutive data records (TKey instances) with a wel...
Definition TFile.h:53
A TGraph is an object made of two arrays X and Y with npoints each.
Definition TGraph.h:41
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:621
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:59
static TClass * Class()
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3062
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:758
A doubly linked list.
Definition TList.h:38
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
Mother of all ROOT objects.
Definition TObject.h:41
virtual const char * GetTitle() const
Returns title of object.
Definition TObject.cxx:483
virtual TClass * IsA() const
Definition TObject.h:245
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition TObject.cxx:274
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
return c2
Definition legend2.C:14
return c3
Definition legend3.C:15
constexpr std::size_t bufferSize
UInt_t GetListOfTitles(TDirectory *rfdir, TList &titles)
Definition tmvaglob.cxx:643
void Initialize(Bool_t useTMVAStyle=kTRUE)
Definition tmvaglob.cxx:176
TKey * NextKey(TIter &keyIter, TString className)
Definition tmvaglob.cxx:364
TFile * OpenFile(const TString &fin)
Definition tmvaglob.cxx:192
void imgconv(TCanvas *c, const TString &fname)
Definition tmvaglob.cxx:212
void bdtcontrolplots(TString dataset, TDirectory *)
void BDTControlPlots(TString dataset, TString fin="TMVA.root", Bool_t useTMVAStyle=kTRUE)