Logo ROOT  
Reference Guide
PlotFoams.cxx
Go to the documentation of this file.
1#include "TMVA/PlotFoams.h"
2
3#include "TControlBar.h"
4#include "TVectorT.h"
5#include "TLine.h"
6#include "TPaveText.h"
9
10#include <sstream>
11#include <string>
12#include <cfloat>
13
14#include "TMVA/PDEFoam.h"
15
16void TMVA::PlotFoams( TString fileName,
17 bool useTMVAStyle )
18{
19 cout << "read file: " << fileName << endl;
20 cout << "kValue = " << kValue << endl;
21 TFile *file = TFile::Open(fileName);
22
23 // set style and remove existing canvas'
24 TMVAGlob::Initialize( useTMVAStyle );
25
26 // create control bar
27 TControlBar* cbar = new TControlBar( "vertical", "Choose cell value for plot:", 50, 50 );
28 if ((gDirectory->Get("SignalFoam") && gDirectory->Get("BgFoam")) ||
29 gDirectory->Get("MultiTargetRegressionFoam")) {
30 TString macro = Form( "TMVA::Plot(\"%s\",%s, \"Event density\", %s)",
31 fileName.Data(), "TMVA::kValueDensity", (useTMVAStyle ? "kTRUE" : "kFALSE") );
32 cbar->AddButton( "Event density", macro, "Plot event density", "button" );
33 } else if (gDirectory->Get("DiscrFoam") || gDirectory->Get("MultiClassFoam0")){
34 TString macro = Form( "TMVA::Plot(\"%s\", %s, \"Discriminator\", %s)",
35 fileName.Data(), "TMVA::kValue", (useTMVAStyle ? "kTRUE" : "kFALSE") );
36 cbar->AddButton( "Discriminator", macro, "Plot discriminator", "button" );
37 } else if (gDirectory->Get("MonoTargetRegressionFoam")){
38 TString macro = Form( "TMVA::Plot(\"%s\", %s, \"Target\", %s)",
39 fileName.Data(), "TMVA::kValue", (useTMVAStyle ? "kTRUE" : "kFALSE") );
40 cbar->AddButton( "Target", macro, "Plot target", "button" );
41 } else {
42 cout << "Error: no foams found in file: " << fileName << endl;
43 return;
44 }
45
46 TString macro_rms = Form( "TMVA::Plot(\"%s\", %s, \"Variance\", %s)",
47 fileName.Data(), "TMVA::kRms", (useTMVAStyle ? "kTRUE" : "kFALSE") );
48 cbar->AddButton( "Variance", macro_rms, "Plot variance", "button" );
49 TString macro_rms_ov_mean = Form( "TMVA::Plot(\"%s\", %s, \"Variance/Mean\", %s)",
50 fileName.Data(), "TMVA::kRmsOvMean", (useTMVAStyle ? "kTRUE" : "kFALSE") );
51 cbar->AddButton( "Variance/Mean", macro_rms_ov_mean, "Plot variance over mean", "button" );
52 TString macro_cell_tree = Form( "TMVA::PlotCellTree(\"%s\", \"Cell tree\", %s)",
53 fileName.Data(), (useTMVAStyle ? "kTRUE" : "kFALSE") );
54 cbar->AddButton( "Cell tree", macro_cell_tree, "Plot cell tree", "button" );
55
56 cbar->Show();
57 file->Close();
58}
59
60// foam plotting macro
61void TMVA::Plot(TString fileName, TMVA::ECellValue cv, TString cv_long, bool useTMVAStyle )
62{
63 cout << "read file: " << fileName << endl;
64 TFile *file = TFile::Open(fileName);
65
67 if (useTMVAStyle) TMVAGlob::SetTMVAStyle();
68
69 // fileNamed foams and foam type
70 TMVA::PDEFoam* SignalFoam = (TMVA::PDEFoam*) gDirectory->Get("SignalFoam");
71 TMVA::PDEFoam* BgFoam = (TMVA::PDEFoam*) gDirectory->Get("BgFoam");
72 TMVA::PDEFoam* DiscrFoam = (TMVA::PDEFoam*) gDirectory->Get("DiscrFoam");
73 TMVA::PDEFoam* MultiClassFoam0 = (TMVA::PDEFoam*) gDirectory->Get("MultiClassFoam0");
74 TMVA::PDEFoam* MonoTargetRegressionFoam = (TMVA::PDEFoam*) gDirectory->Get("MonoTargetRegressionFoam");
75 TMVA::PDEFoam* MultiTargetRegressionFoam = (TMVA::PDEFoam*) gDirectory->Get("MultiTargetRegressionFoam");
76 TList foam_list; // the foams and their captions
77 if (SignalFoam && BgFoam) {
78 foam_list.Add(new TPair(SignalFoam, new TObjString("Signal Foam")));
79 foam_list.Add(new TPair(BgFoam, new TObjString("Background Foam")));
80 } else if (DiscrFoam) {
81 foam_list.Add(new TPair(DiscrFoam, new TObjString("Discriminator Foam")));
82 } else if (MultiClassFoam0) {
83 UInt_t cls = 0;
84 TMVA::PDEFoam *fm = NULL;
85 while ((fm = (TMVA::PDEFoam*) gDirectory->Get(Form("MultiClassFoam%u", cls)))) {
86 foam_list.Add(new TPair(fm, new TObjString(Form("Discriminator Foam %u",cls))));
87 cls++;
88 }
89 } else if (MonoTargetRegressionFoam) {
90 foam_list.Add(new TPair(MonoTargetRegressionFoam,
91 new TObjString("MonoTargetRegression Foam")));
92 } else if (MultiTargetRegressionFoam) {
93 foam_list.Add(new TPair(MultiTargetRegressionFoam,
94 new TObjString("MultiTargetRegression Foam")));
95 } else {
96 cout << "ERROR: no Foams found in file: " << fileName << endl;
97 return;
98 }
99
100 // loop over all foams and print out a debug message
101 TListIter foamIter(&foam_list);
102 TPair *fm_pair = NULL;
103 Int_t kDim = 0; // foam dimensions
104 while ((fm_pair = (TPair*) foamIter())) {
105 kDim = ((TMVA::PDEFoam*) fm_pair->Key())->GetTotDim();
106 cout << "Foam loaded: " << ((TObjString*) fm_pair->Value())->String()
107 << " (dimension = " << kDim << ")" << endl;
108 }
109
110 // kernel to use for the projection
112
113 // plot foams
114 if (kDim == 1) {
115 Plot1DimFoams(foam_list, cv, cv_long, kernel);
116 } else {
117 PlotNDimFoams(foam_list, cv, cv_long, kernel);
118 }
119
120 file->Close();
121}
122
123
124void TMVA::Plot1DimFoams(TList& foam_list, TMVA::ECellValue cell_value,
125 const TString& cell_value_description,
127{
128 // visualize a 1 dimensional PDEFoam via a histogram
129 TCanvas* canvas = NULL;
130 TH1D* projection = NULL;
131
132 // loop over all foams and draw the histogram
133 TListIter it(&foam_list);
134 TPair* fm_pair = NULL; // the (foam, caption) pair
135 while ((fm_pair = (TPair*) it())) {
136 TMVA::PDEFoam* foam = (TMVA::PDEFoam*) fm_pair->Key();
137 if (!foam) continue;
138 TString foam_caption(((TObjString*) fm_pair->Value())->String());
139 TString variable_name(foam->GetVariableName(0)->String());
140
141 canvas = new TCanvas(Form("canvas_%p",foam),
142 "1-dimensional PDEFoam", 400, 400);
143
144 projection = foam->Draw1Dim(cell_value, 100, kernel);
145 projection->SetTitle(cell_value_description + " of " + foam_caption
146 + ";" + variable_name);
147 projection->Draw();
148 projection->SetDirectory(0);
149
150 canvas->Update();
151 }
152}
153
154
155void TMVA::PlotNDimFoams(TList& foam_list, TMVA::ECellValue cell_value,
156 const TString& cell_value_description,
158{
159 // draw 2 dimensional PDEFoam projections
160 TCanvas* canvas = NULL;
161 TH2D* projection = NULL;
162
163 // loop over all foams and draw the projection
164 TListIter it(&foam_list);
165 TPair* fm_pair = NULL; // the (foam, caption) pair
166 while ((fm_pair = (TPair*) it())) {
167 TMVA::PDEFoam* foam = (TMVA::PDEFoam*) fm_pair->Key();
168 if (!foam) continue;
169 TString foam_caption(((TObjString*) fm_pair->Value())->String());
170 const Int_t kDim = ((TMVA::PDEFoam*) fm_pair->Key())->GetTotDim();
171
172 // draw all possible projections (kDim*(kDim-1)/2)
173 for (Int_t i = 0; i < kDim; ++i) {
174 for (Int_t k = i + 1; k < kDim; ++k) {
175
176 canvas = new TCanvas(Form("canvas_%p_%i:%i", foam, i, k),
177 Form("Foam projections %i:%i", i, k),
178 (Int_t)(400/(1.-0.2)), 400);
179 canvas->SetRightMargin(0.2);
180
181 TString title = Form("%s of %s: Projection %s:%s;%s;%s",
182 cell_value_description.Data(),
183 foam_caption.Data(),
184 foam->GetVariableName(i)->String().Data(),
185 foam->GetVariableName(k)->String().Data(),
186 foam->GetVariableName(i)->String().Data(),
187 foam->GetVariableName(k)->String().Data());
188
189 projection = foam->Project2(i, k, cell_value, kernel);
190 projection->SetTitle(title);
191 projection->Draw("COLZ");
192 projection->SetDirectory(0);
193
194 canvas->Update();
195 }
196 }
197 } // loop over foams
198}
199
200
201void TMVA::PlotCellTree(TString fileName, TString cv_long, bool useTMVAStyle )
202{
203 // Draw the PDEFoam cell tree
204
205 cout << "read file: " << fileName << endl;
206 TFile *file = TFile::Open(fileName);
207
208 if (useTMVAStyle) TMVAGlob::SetTMVAStyle();
209
210 // find foams
211 TListIter foamIter(gDirectory->GetListOfKeys());
212 TKey *foam_key = NULL; // the foam key
213 TCanvas *canv = NULL; // the canvas
214 while ((foam_key = (TKey*) foamIter())) {
215 TString name(foam_key->GetName());
216 TString class_name(foam_key->GetClassName());
217 if (!class_name.Contains("PDEFoam"))
218 continue;
219 cout << "PDEFoam found: " << class_name << " " << name << endl;
220
221 // read the foam
222 TMVA::PDEFoam *foam = (TMVA::PDEFoam*) foam_key->ReadObj();
223 canv = new TCanvas(Form("canvas_%s",name.Data()),
224 Form("%s of %s",cv_long.Data(),name.Data()), 640, 480);
225 canv->cd();
226 // get cell tree depth
227 const UInt_t depth = foam->GetRootCell()->GetTreeDepth();
228 const Double_t ystep = 1.0 / depth;
229 DrawCell(foam->GetRootCell(), foam, 0.5, 1.-0.5*ystep, 0.25, ystep);
230 }
231
232 file->Close();
233}
234
237 Double_t xscale, Double_t yscale )
238{
239 // recursively draw cell and it's daughters
240
241 Float_t xsize = xscale*1.5;
242 Float_t ysize = yscale/3;
243 if (xsize > 0.15) xsize=0.1; //xscale/2;
244 if (cell->GetDau0() != NULL) {
245 TLine *a1 = new TLine(x-xscale/4, y-ysize, x-xscale, y-ysize*2);
246 a1->SetLineWidth(2);
247 a1->Draw();
248 DrawCell(cell->GetDau0(), foam, x-xscale, y-yscale, xscale/2, yscale);
249 }
250 if (cell->GetDau1() != NULL){
251 TLine *a1 = new TLine(x+xscale/4, y-ysize, x+xscale, y-ysize*2);
252 a1->SetLineWidth(2);
253 a1->Draw();
254 DrawCell(cell->GetDau1(), foam, x+xscale, y-yscale, xscale/2, yscale);
255 }
256
257 TPaveText *t = new TPaveText(x-xsize, y-ysize, x+xsize, y+ysize, "NDC");
258
259 t->SetBorderSize(1);
260 t->SetFillStyle(1);
261
262 // draw all cell elements
263 t->AddText( Form("Intg=%.5f", cell->GetIntg()) );
264 t->AddText( Form("Var=%.5f", cell->GetDriv()) );
265 TVectorD *vec = (TVectorD*) cell->GetElement();
266 if (vec) {
267 for (Int_t i = 0; i < vec->GetNrows(); ++i)
268 t->AddText( Form("E[%i]=%.5f", i, (*vec)[i]) );
269 }
270
271 if (cell->GetStat() != 1) {
272 // cell is inactive --> draw split point
273 t->SetFillColor( TColor::GetColor("#BBBBBB") );
274 t->SetTextColor( TColor::GetColor("#000000") );
275
276 // cell position and size
277 TMVA::PDEFoamVect cellPosi(foam->GetTotDim()), cellSize(foam->GetTotDim());
278 cell->GetHcub(cellPosi, cellSize);
279 Int_t kBest = cell->GetBest(); // best division variable
280 Double_t xBest = cell->GetXdiv(); // best division point
281 t->AddText( Form("dim=%i", kBest) );
282 t->AddText( Form("cut=%.5g", foam->VarTransformInvers(kBest,cellPosi[kBest] + xBest*cellSize[kBest])) );
283 } else {
284 t->SetFillColor( TColor::GetColor("#DD0033") );
285 t->SetTextColor( TColor::GetColor("#FFFFFF") );
286 }
287
288 t->Draw();
289}
int Int_t
Definition: RtypesCore.h:45
unsigned int UInt_t
Definition: RtypesCore.h:46
float Float_t
Definition: RtypesCore.h:57
double Double_t
Definition: RtypesCore.h:59
#define gDirectory
Definition: TDirectory.h:348
char name[80]
Definition: TGX11.cxx:110
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition: TString.cxx:2447
R__EXTERN TStyle * gStyle
Definition: TStyle.h:414
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
virtual void SetFillStyle(Style_t fstyle)
Set the fill area style.
Definition: TAttFill.h:39
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition: TAttLine.h:43
virtual void SetRightMargin(Float_t rightmargin)
Set Pad right margin in fraction of the pad width.
Definition: TAttPad.cxx:119
virtual void SetTextColor(Color_t tcolor=1)
Set the text color.
Definition: TAttText.h:44
The Canvas class.
Definition: TCanvas.h:23
TVirtualPad * cd(Int_t subpadnumber=0) override
Set current canvas & pad.
Definition: TCanvas.cxx:706
void Update() override
Update canvas pad buffers.
Definition: TCanvas.cxx:2492
static Int_t GetColor(const char *hexcolor)
Static method returning color number for color specified by hex color string of form: "#rrggbb",...
Definition: TColor.cxx:1822
A Control Bar is a fully user configurable tool which provides fast access to frequently used operati...
Definition: TControlBar.h:26
void Show()
Show control bar.
void AddButton(TControlBarButton *button)
Add button.
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:54
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:4019
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:617
virtual void SetDirectory(TDirectory *dir)
By default, when a histogram is created, it is added to the list of histogram objects in the current ...
Definition: TH1.cxx:8814
void SetTitle(const char *title) override
See GetStatOverflows for more information.
Definition: TH1.cxx:6710
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition: TH1.cxx:3070
2-D histogram with a double per channel (see TH1 documentation)}
Definition: TH2.h:300
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:28
virtual const char * GetClassName() const
Definition: TKey.h:75
virtual TObject * ReadObj()
To read a TObject* from the file.
Definition: TKey.cxx:750
Use the TLine constructor to create a simple line.
Definition: TLine.h:22
Iterator of linked list.
Definition: TList.h:191
A doubly linked list.
Definition: TList.h:38
void Add(TObject *obj) override
Definition: TList.h:81
Double_t GetXdiv() const
Definition: PDEFoamCell.h:77
Int_t GetStat() const
Definition: PDEFoamCell.h:91
UInt_t GetTreeDepth(UInt_t depth=0)
Get depth of cell tree, starting at this cell.
PDEFoamCell * GetDau1() const
Definition: PDEFoamCell.h:95
Double_t GetIntg() const
Definition: PDEFoamCell.h:86
PDEFoamCell * GetDau0() const
Definition: PDEFoamCell.h:94
Int_t GetBest() const
Definition: PDEFoamCell.h:78
Double_t GetDriv() const
Definition: PDEFoamCell.h:87
TObject * GetElement() const
Definition: PDEFoamCell.h:107
void GetHcub(PDEFoamVect &, PDEFoamVect &) const
Provides size and position of the cell These parameter are calculated by analyzing information in all...
This class is the abstract kernel interface for PDEFoam.
This class is a trivial PDEFoam kernel estimator.
Implementation of PDEFoam.
Definition: PDEFoam.h:79
TObjString * GetVariableName(Int_t idx)
Definition: PDEFoam.h:219
Float_t VarTransformInvers(Int_t idim, Float_t x) const
Definition: PDEFoam.h:298
PDEFoamCell * GetRootCell() const
Definition: PDEFoam.h:202
TH1D * Draw1Dim(ECellValue cell_value, Int_t nbin, PDEFoamKernelBase *kernel=NULL)
Draws 1-dimensional foam (= histogram)
Definition: PDEFoam.cxx:1219
Int_t GetTotDim() const
Definition: PDEFoam.h:197
virtual TH2D * Project2(Int_t idim1, Int_t idim2, ECellValue cell_value=kValue, PDEFoamKernelBase *kernel=NULL, UInt_t nbin=50)
Project foam variable idim1 and variable idim2 to histogram.
Definition: PDEFoam.cxx:1271
ECellValue
Definition: PDEFoam.h:69
@ kValue
Definition: PDEFoam.h:69
const char * GetName() const override
Returns name of object.
Definition: TNamed.h:47
Collectable string class.
Definition: TObjString.h:28
TString & String()
Definition: TObjString.h:48
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition: TObject.cxx:197
Class used by TMap to store (key,value) pairs.
Definition: TMap.h:102
TObject * Value() const
Definition: TMap.h:121
TObject * Key() const
Definition: TMap.h:120
A Pave (see TPave) with text, lines or/and boxes inside.
Definition: TPaveText.h:21
virtual TText * AddText(Double_t x1, Double_t y1, const char *label)
Add a new Text line to this pavetext at given coordinates.
Definition: TPaveText.cxx:183
void Draw(Option_t *option="") override
Draw this pavetext with its current attributes.
Definition: TPaveText.cxx:234
virtual void SetBorderSize(Int_t bordersize=4)
Definition: TPave.h:73
Basic string class.
Definition: TString.h:136
const char * Data() const
Definition: TString.h:369
void SetNumberContours(Int_t number=20)
Set the default number of contour levels when drawing 2-d plots.
Definition: TStyle.cxx:1449
Double_t y[n]
Definition: legend1.C:17
Double_t x[n]
Definition: legend1.C:17
void Initialize(Bool_t useTMVAStyle=kTRUE)
Definition: tmvaglob.cxx:176
void SetTMVAStyle()
Definition: tmvaglob.cxx:96
void Plot1DimFoams(TList &foam_list, TMVA::ECellValue cell_value, const TString &cell_value_description, TMVA::PDEFoamKernelBase *kernel)
void PlotNDimFoams(TList &foam_list, TMVA::ECellValue cell_value, const TString &cell_value_description, TMVA::PDEFoamKernelBase *kernel)
void PlotFoams(TString fileName="weights/TMVAClassification_PDEFoam.weights_foams.root", bool useTMVAStyle=kTRUE)
void DrawCell(TMVA::PDEFoamCell *cell, TMVA::PDEFoam *foam, Double_t x, Double_t y, Double_t xscale, Double_t yscale)
void Plot(TString fileName, TMVA::ECellValue cv, TString cv_long, bool useTMVAStyle=kTRUE)
void PlotCellTree(TString fileName, TString cv_long, bool useTMVAStyle=kTRUE)
Definition: file.py:1
const char * String
Definition: TXMLSetup.cxx:94
Definition: civetweb.c:1856