-
Notifications
You must be signed in to change notification settings - Fork 0
/
BDTControlPlots.C
executable file
·172 lines (144 loc) · 5.04 KB
/
BDTControlPlots.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <vector>
#include <string>
#include "tmvaglob.C"
#include "TH1.h"
#include "TGraph.h"
// input: - Input file (result from TMVA),
// - use of TMVA plotting TStyle
void bdtcontrolplots(TDirectory *);
void BDTControlPlots( TString fin = "TMVA.root", Bool_t useTMVAStyle = kTRUE )
{
// set style and remove existing canvas'
TMVAGlob::Initialize( useTMVAStyle );
// checks if file with name "fin" is already open, and if not opens one
TFile* file;
file = TMVAGlob::OpenFile( fin );
// get all titles of the method BDT
TList titles;
TString methodName = "Method_BDT";
UInt_t ninst = TMVAGlob::GetListOfTitles(methodName,titles);
if (ninst==0) {
cout << "Could not locate directory 'Method_BDT' in file " << fin << endl;
return;
}
// loop over all titles
TIter keyIter(&titles);
TDirectory *bdtdir;
TKey *key;
while ((key = TMVAGlob::NextKey(keyIter,"TDirectory"))) {
bdtdir = (TDirectory *)key->ReadObj();
bdtcontrolplots( bdtdir );
}
}
void bdtcontrolplots( TDirectory *bdtdir ) {
const Int_t nPlots = 6;
Int_t width = 900;
Int_t height = 600;
char cn[100], cn2[100];
const TString titName = bdtdir->GetName();
sprintf( cn, "cv_%s", titName.Data() );
TCanvas *c = new TCanvas( cn, Form( "%s Control Plots", titName.Data() ),
width, height );
c->Divide(3,2);
TString hname[nPlots]={"BoostMonitor","BoostWeight","BoostWeightVsTree","ErrFractHist","NodesBeforePruning",titName+"_FOMvsIterFrame"};
Bool_t BoostMonitorIsDone=kFALSE;
for (Int_t i=0; i<nPlots; i++){
Int_t color = 4;
c->cd(i+1);
TH1 *h = (TH1*) bdtdir->Get(hname[i]);
if (h){
h->SetMaximum(h->GetMaximum()*1.3);
h->SetMinimum( 0 );
h->SetMarkerColor(color);
h->SetMarkerSize( 0.7 );
h->SetMarkerStyle( 24 );
h->SetLineWidth(1);
h->SetLineColor(color);
if(hname[i]=="NodesBeforePruning")h->SetTitle("Nodes before/after pruning");
h->Draw();
if(hname[i]=="NodesBeforePruning"){
TH1 *h2 = (TH1*) bdtdir->Get("NodesAfterPruning");
h2->SetLineWidth(1);
h2->SetLineColor(2);
h2->Draw("same");
}
if(hname[i]=="BoostMonitor"){ // a plot only available in case DoBoostMontior option has bee set
TGraph *g = (TGraph*) bdtdir->Get("BoostMonitorGraph");
g->Draw("LP*");
BoostMonitorIsDone = kTRUE;
}
if(hname[i]==titName+"_FOMvsIterFrame"){ // a plot only available in case DoBoostMontior option has bee set
TGraph *g = (TGraph*) bdtdir->Get(titName+"_FOMvsIter");
g->Draw();
}
c->Update();
}
}
TCanvas *c2 = NULL;
if (BoostMonitorIsDone){
sprintf( cn2, "cv2_%s", titName.Data() );
c2 = new TCanvas( cn2, Form( "%s BoostWeights", titName.Data() ),
1200, 1200 );
c2->Divide(5,5);
Int_t ipad=1;
TIter keys( bdtdir->GetListOfKeys() );
TKey *key;
// gDirectory->ls();
while ( (key = (TKey*)keys.Next()) && ipad < 26) {
TObject *obj=key->ReadObj();
if (obj->IsA()->InheritsFrom(TH1::Class())){
TH1F *hx = (TH1F*)obj;
TString hhname(Form("%s",obj->GetTitle()));
if (hhname.Contains("BoostWeightsInTreeB")){
c2->cd(ipad++);
hx->SetLineColor(4);
hx->Draw();
hhname.ReplaceAll("TreeB","TreeS");
bdtdir->GetObject(hhname.Data(),hx);
if (hx) {
hx->SetLineColor(2);
hx->Draw("same");
}
}
c2->Update();
}
}
}
// write to file
TString fname = Form( "plots/%s_ControlPlots", titName.Data() );
TMVAGlob::imgconv( c, fname );
if (c2){
fname = Form( "plots/%s_ControlPlots2", titName.Data() );
TMVAGlob::imgconv( c2, fname );
}
TCanvas *c3 = NULL;
if (BoostMonitorIsDone){
sprintf( cn2, "cv3_%s", titName.Data() );
c3 = new TCanvas( cn2, Form( "%s Variables", titName.Data() ),
1200, 1200 );
c3->Divide(5,5);
Int_t ipad=1;
TIter keys( bdtdir->GetListOfKeys() );
TKey *key;
// gDirectory->ls();
while ( (key = (TKey*)keys.Next()) && ipad < 26) {
TObject *obj=key->ReadObj();
if (obj->IsA()->InheritsFrom(TH1::Class())){
TH1F *hx = (TH1F*)obj;
TString hname(Form("%s",obj->GetTitle()));
if (hname.Contains("SigVar0AtTree")){
c3->cd(ipad++);
hx->SetLineColor(4);
hx->Draw();
hname.ReplaceAll("Sig","Bkg");
bdtdir->GetObject(hname.Data(),hx);
if (hx) {
hx->SetLineColor(2);
hx->Draw("same");
}
}
c3->Update();
}
}
}
}