Logo ROOT   6.12/07
Reference Guide
BDTControlPlots.cxx
Go to the documentation of this file.
1 #include "TMVA/BDTControlPlots.h"
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 
12 void 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 
43 void 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  char cn[100], cn2[100];
50  const TString titName = bdtdir->GetName();
51  sprintf( cn, "cv_%s", titName.Data() );
52  TCanvas *c = new TCanvas( cn, Form( "%s Control Plots", titName.Data() ),
53  width, height );
54  c->Divide(3,2);
55 
56 
57 
58 
59  TString hname[nPlots]={"BoostMonitor","BoostWeight","BoostWeightVsTree","ErrFractHist","NodesBeforePruning",titName+"_FOMvsIterFrame"};
60 
61  Bool_t BoostMonitorIsDone=kFALSE;
62 
63  for (Int_t i=0; i<nPlots; i++){
64  Int_t color = 4;
65  c->cd(i+1);
66  TH1 *h = (TH1*) bdtdir->Get(hname[i]);
67 
68  if (h){
69  h->SetMaximum(h->GetMaximum()*1.3);
70  h->SetMinimum( 0 );
71  h->SetMarkerColor(color);
72  h->SetMarkerSize( 0.7 );
73  h->SetMarkerStyle( 24 );
74  h->SetLineWidth(1);
75  h->SetLineColor(color);
76  if(hname[i]=="NodesBeforePruning")h->SetTitle("Nodes before/after pruning");
77  h->Draw();
78  if(hname[i]=="NodesBeforePruning"){
79  TH1 *h2 = (TH1*) bdtdir->Get("NodesAfterPruning");
80  h2->SetLineWidth(1);
81  h2->SetLineColor(2);
82  h2->Draw("same");
83  }
84  if(hname[i]=="BoostMonitor"){ // a plot only available in case DoBoostMontior option has bee set
85  TGraph *g = (TGraph*) bdtdir->Get("BoostMonitorGraph");
86  g->Draw("LP*");
87  BoostMonitorIsDone = kTRUE;
88  }
89  if(hname[i]==titName+"_FOMvsIterFrame"){ // a plot only available in case DoBoostMontior option has bee set
90  TGraph *g = (TGraph*) bdtdir->Get(titName+"_FOMvsIter");
91  g->Draw();
92  }
93  c->Update();
94  }
95  }
96 
97 
98  TCanvas *c2 = NULL;
99  if (BoostMonitorIsDone){
100  sprintf( cn2, "cv2_%s", titName.Data() );
101  c2 = new TCanvas( cn2, Form( "%s BoostWeights", titName.Data() ),
102  1200, 1200 );
103  c2->Divide(5,5);
104  Int_t ipad=1;
105 
106  TIter keys( bdtdir->GetListOfKeys() );
107  TKey *key;
108  // gDirectory->ls();
109  while ( (key = (TKey*)keys.Next()) && ipad < 26) {
110  TObject *obj=key->ReadObj();
111  if (obj->IsA()->InheritsFrom(TH1::Class())){
112  TH1F *hx = (TH1F*)obj;
113  TString hhname_(Form("%s",obj->GetTitle()));
114  if (hhname_.Contains("BoostWeightsInTreeB")){
115  c2->cd(ipad++);
116  hx->SetLineColor(4);
117  hx->Draw();
118  hhname_.ReplaceAll("TreeB","TreeS");
119  bdtdir->GetObject(hhname_.Data(),hx);
120  if (hx) {
121  hx->SetLineColor(2);
122  hx->Draw("same");
123  }
124  }
125  c2->Update();
126  }
127  }
128 
129  }
130 
131  // write to file
132  TString fname = dataset+Form( "/plots/%s_ControlPlots", titName.Data() );
133  TMVAGlob::imgconv( c, fname );
134 
135  if (c2){
136  fname = dataset+Form( "/plots/%s_ControlPlots2", titName.Data() );
137  TMVAGlob::imgconv( c2, fname );
138  }
139 
140  TCanvas *c3 = NULL;
141  if (BoostMonitorIsDone){
142  sprintf( cn2, "cv3_%s", titName.Data() );
143  c3 = new TCanvas( cn2, Form( "%s Variables", titName.Data() ),
144  1200, 1200 );
145  c3->Divide(5,5);
146  Int_t ipad=1;
147 
148  TIter keys( bdtdir->GetListOfKeys() );
149  TKey *key;
150  // gDirectory->ls();
151  while ( (key = (TKey*)keys.Next()) && ipad < 26) {
152  TObject *obj=key->ReadObj();
153  if (obj->IsA()->InheritsFrom(TH1::Class())){
154  TH1F *hx = (TH1F*)obj;
155  TString hname_(Form("%s",obj->GetTitle()));
156  if (hname_.Contains("SigVar0AtTree")){
157  c3->cd(ipad++);
158  hx->SetLineColor(4);
159  hx->Draw();
160  hname_.ReplaceAll("Sig","Bkg");
161  bdtdir->GetObject(hname_.Data(),hx);
162  if (hx) {
163  hx->SetLineColor(2);
164  hx->Draw("same");
165  }
166  }
167  c3->Update();
168  }
169  }
170 
171  }
172 
173 
174 }
175 
176 
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition: TAttLine.h:43
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:7807
void imgconv(TCanvas *c, const TString &fname)
Definition: tmvaglob.cxx:212
virtual TList * GetListOfKeys() const
Definition: TDirectory.h:150
virtual void SetMaximum(Double_t maximum=-1111)
Definition: TH1.h:390
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
Definition: TDirectory.cxx:805
void GetObject(const char *namecycle, T *&ptr)
Definition: TDirectory.h:139
TFile * OpenFile(const TString &fin)
Definition: tmvaglob.cxx:192
TH1 * h
Definition: legend2.C:5
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:46
virtual void SetMinimum(Double_t minimum=-1111)
Definition: TH1.h:391
Basic string class.
Definition: TString.h:125
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:567
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
TKey * NextKey(TIter &keyIter, TString className)
Definition: tmvaglob.cxx:357
virtual void Draw(Option_t *chopt="")
Draw this graph with its current attributes.
Definition: TGraph.cxx:745
void Class()
Definition: Class.C:29
void BDTControlPlots(TString dataset, TString fin="TMVA.root", Bool_t useTMVAStyle=kTRUE)
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition: TAttMarker.h:38
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:24
A doubly linked list.
Definition: TList.h:44
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
void Initialize(Bool_t useTMVAStyle=kTRUE)
Definition: tmvaglob.cxx:176
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2969
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition: TObject.cxx:443
unsigned int UInt_t
Definition: RtypesCore.h:42
char * Form(const char *fmt,...)
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition: TAttMarker.h:40
virtual TDirectory * GetDirectory(const char *apath, Bool_t printError=false, const char *funcname="GetDirectory")
Find a directory named "apath".
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
Definition: TAttMarker.h:41
const Bool_t kFALSE
Definition: RtypesCore.h:88
The Canvas class.
Definition: TCanvas.h:31
return c2
Definition: legend2.C:14
Describe directory structure in memory.
Definition: TDirectory.h:34
The TH1 histogram class.
Definition: TH1.h:56
void bdtcontrolplots(TString dataset, TDirectory *)
Mother of all ROOT objects.
Definition: TObject.h:37
virtual const char * GetTitle() const
Returns title of object.
Definition: TObject.cxx:401
virtual TObject * ReadObj()
To read a TObject* from the file.
Definition: TKey.cxx:722
Definition: file.py:1
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:41
virtual void SetTitle(const char *title)
See GetStatOverflows for more information.
Definition: TH1.cxx:6154
const Bool_t kTRUE
Definition: RtypesCore.h:87
return c3
Definition: legend3.C:15
const char * Data() const
Definition: TString.h:345
UInt_t GetListOfTitles(TDirectory *rfdir, TList &titles)
Definition: tmvaglob.cxx:636
static constexpr double g