Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 = TString::Format( "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 = TString::Format( "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 = TString::Format( "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 = TString::Format( "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 = TString::Format( "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 = TString::Format( "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(TString::Format("MultiClassFoam%u", cls)))) {
86 foam_list.Add(new TPair(fm, new TObjString(TString::Format("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 = nullptr; // 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(TString::Format("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(nullptr);
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 = nullptr;
161 TH2D* projection = nullptr;
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(TString::Format("canvas_%p_%i:%i", foam, i, k),
177 TString::Format("Foam projections %i:%i", i, k),
178 (Int_t)(400/(1.-0.2)), 400);
179 canvas->SetRightMargin(0.2);
180
181 TString title = TString::Format("%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(nullptr);
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 = nullptr; // the foam key
213 TCanvas *canv = nullptr; // 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(TString::Format("canvas_%s",name.Data()),
224 TString::Format("%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( TString::Format("Intg=%.5f", cell->GetIntg()) );
264 t->AddText( TString::Format("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( TString::Format("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( TString::Format("dim=%i", kBest) );
282 t->AddText( TString::Format("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:384
char name[80]
Definition TGX11.cxx:110
R__EXTERN TStyle * gStyle
Definition TStyle.h:433
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:716
void Update() override
Update canvas pad buffers.
Definition TCanvas.cxx:2476
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:1839
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 an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
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:4089
void Close(Option_t *option="") override
Close a file.
Definition TFile.cxx:950
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:669
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:8905
void SetTitle(const char *title) override
Change/set the title.
Definition TH1.cxx:6686
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3066
2-D histogram with a double per channel (see TH1 documentation)
Definition TH2.h:357
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:762
Use the TLine constructor to create a simple line.
Definition TLine.h:22
Iterator of linked list.
Definition TList.h:193
A doubly linked list.
Definition TList.h:38
void Add(TObject *obj) override
Definition TList.h:83
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
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
TH1D * Draw1Dim(ECellValue cell_value, Int_t nbin, PDEFoamKernelBase *kernel=nullptr)
Draws 1-dimensional foam (= histogram)
Definition PDEFoam.cxx:1217
PDEFoamCell * GetRootCell() const
Definition PDEFoam.h:202
virtual TH2D * Project2(Int_t idim1, Int_t idim2, ECellValue cell_value=kValue, PDEFoamKernelBase *kernel=nullptr, UInt_t nbin=50)
Project foam variable idim1 and variable idim2 to histogram.
Definition PDEFoam.cxx:1269
Int_t GetTotDim() const
Definition PDEFoam.h:197
@ 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:274
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.
void Draw(Option_t *option="") override
Draw this pavetext with its current attributes.
virtual void SetBorderSize(Int_t bordersize=4)
Sets the border size of the TPave box and shadow.
Definition TPave.h:77
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:376
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
void SetNumberContours(Int_t number=20)
Set the default number of contour levels when drawing 2-d plots.
Definition TStyle.cxx:1496
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)
const char * String
Definition TXMLSetup.cxx:94