Logo ROOT  
Reference Guide
rulevisCorr.cxx
Go to the documentation of this file.
1#include "TMVA/rulevisCorr.h"
2
3#include "TH2.h"
4
5
6
7// This macro plots the distributions of the different input variables overlaid on
8// the sum of importance per bin.
9// The scale goes from violett (no importance) to red (high importance).
10// Areas where many important rules are active, will thus be very red.
11//
12// input: - Input file (result from TMVA),
13// - normal/decorrelated/PCA
14// - use of TMVA plotting TStyle
16{
17
18 // set style and remove existing canvas'
19 // TMVAGlob::Initialize( useTMVAStyle );
20
21 // checks if file with name "fin" is already open, and if not opens one
23
24 // get top dir containing all hists of the variables
25 // TDirectory* vardir = (TDirectory*)file->Get( "InputVariables_Id" );
26 // TDirectory* vardir = TMVAGlob::GetInputVariablesDir( type );
27 // if (vardir==0) return;
28
29 // TDirectory* corrdir = TMVAGlob::GetCorrelationPlotsDir( type, vardir );
30 // if (corrdir==0) return;
31
32 // get all titles of the method rulefit
33 TList titles;
34 TString dirname="Method_RuleFit";
35 UInt_t ninst = TMVAGlob::GetListOfTitles(dirname,titles);
36 if (ninst==0) return;
37
38 TDirectory* dir = (TDirectory*)file->Get(dirname );
39
40 // loop over rulefit methods
41 TIter next(dir->GetListOfKeys());
42 TKey *key(0);
43 while ((key = (TKey*)next())) {
44
45 if (!gROOT->GetClass(key->GetClassName())->InheritsFrom("TDirectory")) continue;
46
47 TDirectory* rfdir = (TDirectory*)key->ReadObj();
48 TDirectory* vardir = rfdir;
49 TDirectory* corrdir = rfdir;
50
51 // loop over all titles
52 TIter keyIter(&titles);
53
54 // while ((rfkey = TMVAGlob::NextKey(keyIter,"TDirectory"))) {
55 // rfdir = (TDirectory *)rfkey->ReadObj();
56 rulevisCorr( rfdir, vardir, corrdir, type );
57 // }
58 }
59}
60
62 //
63 if (rfdir==0) return;
64 if (vardir==0) return;
65 if (corrdir==0) return;
66 //
67 const TString rfName = rfdir->GetName();
68 const TString maintitle = rfName + " : Rule Importance, 2D";
69 const TString rfNameOpt = "_RF2D_";
70 const TString outfname[TMVAGlob::kNumOfMethods] = { "rulevisCorr",
71 "rulevisCorr_decorr",
72 "rulevisCorr_pca",
73 "rulevisCorr_gaussdecorr" };
74 const TString outputName = outfname[type]+"_"+rfdir->GetName();
75 //
76 TIter rfnext(rfdir->GetListOfKeys());
77 TKey *rfkey;
78 Double_t rfmax = -1;
79 Double_t rfmin = -1;
80 // Bool_t allEmpty=kTRUE;
82 TH2F *hrf;
83 while ((rfkey = TMVAGlob::NextKey(rfnext,"TH2F"))) {
84 hrf = (TH2F *)rfkey->ReadObj();
85 TString hname= hrf->GetName();
86 if (hname.Contains(rfNameOpt)){ // found a new RF2D plot
87 Double_t valmin = hrf->GetMinimum();
88 Double_t valmax = hrf->GetMaximum();
89 if (first) {
90 rfmax = valmax;
91 rfmin = valmin;
92 } else {
93 if (valmax>rfmax) rfmax = valmax;
94 if (valmin<rfmin) rfmin = valmin;
95 }
96 // if (hrf->GetEntries()>0) allEmpty=kFALSE;
98 }
99 }
100 if (first) {
101 cout << "ERROR: no RF2D plots found..." << endl;
102 return;
103 }
104 Double_t minrange = rfmin;
105 Double_t maxrange = rfmax;
106 Double_t targetrange = maxrange - minrange;
107
108 const Int_t nContours = 100;
109 Double_t contourLevels[nContours];
110 Double_t dcl = targetrange/Double_t(nContours-1);
111 //
112 for (Int_t i=0; i<nContours; i++) {
113 contourLevels[i] = rfmin+dcl*Double_t(i);
114 }
115
116 ///////////////////////////
117 vardir->cd();
118
119 // how many plots are in the directory?
120 Int_t noVars = ((vardir->GetListOfKeys())->GetEntries()) / 2;
121 Int_t noPlots = (noVars*(noVars+1)/2) - noVars;
122
123 // *** CONTINUE HERE ***
124 // define Canvas layout here!
125 // default setting
126 Int_t xPad; // no of plots in x
127 Int_t yPad; // no of plots in y
128 Int_t width; // size of canvas
130 switch (noPlots) {
131 case 1:
132 xPad = 1; yPad = 1; width = 500; height = 0.7*width; break;
133 case 2:
134 xPad = 2; yPad = 1; width = 600; height = 0.7*width; break;
135 case 3:
136 xPad = 3; yPad = 1; width = 900; height = 0.4*width; break;
137 case 4:
138 xPad = 2; yPad = 2; width = 600; height = width; break;
139 default:
140 xPad = 3; yPad = 2; width = 800; height = 0.7*width; break;
141 }
142 Int_t noPad = xPad * yPad ;
143
144 // this defines how many canvases we need
145 const Int_t noCanvas = 1 + (Int_t)((noPlots - 0.001)/noPad);
146 TCanvas **c = new TCanvas*[noCanvas];
147 for (Int_t ic=0; ic<noCanvas; ic++) c[ic] = 0;
148
149 // counter variables
150 Int_t countCanvas = 0;
151 Int_t countPad = 1;
152
153 // loop over all objects in directory
154 TIter next(corrdir->GetListOfKeys());
155 TKey *key;
156 // TH2F *sigCpy=0;
157 // TH2F *bgdCpy=0;
158 first = kTRUE;
159 //
160 while ((key = (TKey*)next())) {
161
162 // make sure, that we only look at histograms
163 TClass *cl = gROOT->GetClass(key->GetClassName());
164 if (!cl->InheritsFrom("TH2")) continue;
165 TH2F* sig = (TH2F*)key->ReadObj();
166 TString hname= sig->GetName();
167 // check for all signal histograms
168 if (hname.Contains("_sig_")){ // found a new signal plot
169
170 // create new canvas
171 if ((c[countCanvas]==NULL) || (countPad>noPad)) {
172 char cn[20];
173 sprintf( cn, "rulecorr%d_", countCanvas+1 );
174 TString cname(cn);
175 cname += rfdir->GetName();
176 c[countCanvas] = new TCanvas( cname, maintitle,
177 countCanvas*50+200, countCanvas*20, width, height );
178 // style
179 c[countCanvas]->Divide(xPad,yPad);
180 countPad = 1;
181 }
182 // save canvas to file
183 TPad *cPad = (TPad *)(c[countCanvas]->GetPad(countPad));
184 c[countCanvas]->cd(countPad);
185 countPad++;
186
187 // find the corredponding background histo
188 TString bgname = hname;
189 bgname.ReplaceAll("_sig_","_bgd_");
190 TKey* hkey = corrdir->GetKey(bgname);
191 TH2F* bgd = (TH2F*)hkey->ReadObj();
192 if (bgd == NULL) {
193 cout << "ERROR!!! couldn't find background histo for" << hname << endl;
194 return;
195 }
196 const Int_t rebin=6;
197 sig->Rebin2D(rebin,rebin);
198 bgd->Rebin2D(rebin,rebin);
199 //
200 TString rfname = hname;
201 rfname.ReplaceAll("_sig_",rfNameOpt);
202 TKey *hrfkey = rfdir->GetKey(rfname);
203 TH2F* hrf2 = (TH2F*)hrfkey->ReadObj();
204 // Double_t wmin = hrf2->GetMinimum();
205 // Double_t wmax = hrf2->GetMaximum();
206 // Double_t wmean = (wmax+wmin)/2.0;
207 // Double_t wrange = wmax-wmin;
208 // Double_t wscale = (wrange>0.0 ? targetrange/wrange : 1.0);
209 // if (rfmax>0.0)
210 // hrf2->Scale(1.0/rfmax);
211 hrf2->SetMinimum(minrange); // make sure it's zero -> for palette axis
212 hrf2->SetMaximum(maxrange); // make sure max is 1.0 -> idem
213 hrf2->SetContour(nContours,&contourLevels[0]);
214
215 // this is set but not stored during plot creation in MVA_Factory
216 // TMVAGlob::SetSignalAndBackgroundStyle( sigK, bgd );
217 sig->SetFillColor(1);
218 sig->SetLineColor(1);
219
220 bgd->SetFillColor(15);
221 bgd->SetLineColor(15);
222
223 // chop off "signal"
224 TString title(hrf2->GetTitle());
225 title.ReplaceAll("signal","");
226 if (first) {
227 hrf2->SetTitle( maintitle );
229 } else {
230 hrf2->SetTitle( "" );
231 }
232 TMVAGlob::SetFrameStyle( hrf2, 1.2 );
233
234 // finally plot and overlay
235 hrf2->Draw("colz ah");
236 Float_t sc = 1.1;
237 if (countPad==2) sc = 1.3;
238 sig->SetMaximum( TMath::Max( sig->GetMaximum(), bgd->GetMaximum() )*sc );
239 Double_t smax = sig->GetMaximum();
240
241 sig->Scale(1.0/smax);
242 sig->SetContour(5);
243 sig->Draw("same cont3");
244 TMVAGlob::SetFrameStyle( sig, 1.2 );
245
246 bgd->Scale(1.0/smax);
247 bgd->SetContour(5);
248 bgd->Draw("same cont3");
249 TMVAGlob::SetFrameStyle( bgd, 1.2 );
250 // sig->GetXaxis()->SetTitle( title );
251 sig->GetYaxis()->SetTitleOffset( 1.30 );
252 // sig->GetYaxis()->SetTitle("Events");
253
254 // redraw axes
255 sig->Draw("sameaxis");
256
257 cPad->SetRightMargin(0.13);
258 cPad->Update();
259
260 // Draw legend
261 if (countPad==2){
262 TLegend *legend= new TLegend( cPad->GetLeftMargin(),
263 1-cPad->GetTopMargin()-.18,
264 cPad->GetLeftMargin()+.4,
265 1-cPad->GetTopMargin() );
266 legend->AddEntry(sig,"Signal","F");
267 legend->AddEntry(bgd,"Background","F");
268 legend->Draw("same");
269 legend->SetBorderSize(1);
270 legend->SetMargin( 0.3 );
271 legend->SetFillColor(19);
272 legend->SetFillStyle(3001);
273 }
274
275 // save canvas to file
276 if (countPad > noPad) {
277 c[countCanvas]->Update();
278 TString fname = Form( "plots/%s_c%i", outputName.Data(), countCanvas+1 );
279 TMVAGlob::imgconv( c[countCanvas], fname );
280 // TMVAGlob::plot_logo(); // don't understand why this doesn't work ... :-(
281 countCanvas++;
282 }
283 }
284 }
285
286 if (countPad <= noPad) {
287 c[countCanvas]->Update();
288 TString fname = Form( "plots/%s_c%i", outfname[type].Data(), countCanvas+1 );
289 TMVAGlob::imgconv( c[countCanvas], fname );
290 }
291}
#define c(i)
Definition: RSha256.hxx:101
bool Bool_t
Definition: RtypesCore.h:63
int Int_t
Definition: RtypesCore.h:45
const Bool_t kFALSE
Definition: RtypesCore.h:101
unsigned int UInt_t
Definition: RtypesCore.h:46
float Float_t
Definition: RtypesCore.h:57
double Double_t
Definition: RtypesCore.h:59
const Bool_t kTRUE
Definition: RtypesCore.h:100
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char cname
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 WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t height
#define gROOT
Definition: TROOT.h:404
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition: TString.cxx:2447
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition: TAttAxis.cxx:301
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 SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
Float_t GetLeftMargin() const
Definition: TAttPad.h:44
virtual void SetRightMargin(Float_t rightmargin)
Set Pad right margin in fraction of the pad width.
Definition: TAttPad.cxx:119
Float_t GetTopMargin() const
Definition: TAttPad.h:46
The Canvas class.
Definition: TCanvas.h:23
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition: TClass.h:80
Bool_t InheritsFrom(const char *cl) const override
Return kTRUE if this class inherits from a class with name "classname".
Definition: TClass.cxx:4862
Describe directory structure in memory.
Definition: TDirectory.h:45
virtual TKey * GetKey(const char *, Short_t=9999) const
Definition: TDirectory.h:212
virtual TList * GetListOfKeys() const
Definition: TDirectory.h:214
virtual Bool_t cd(const char *path=nullptr)
Change current directory to "this" directory.
Definition: TDirectory.cxx:504
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:54
void SetTitle(const char *title) override
See GetStatOverflows for more information.
Definition: TH1.cxx:6710
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:8422
virtual void SetMaximum(Double_t maximum=-1111)
Definition: TH1.h:397
TAxis * GetYaxis()
Definition: TH1.h:320
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition: TH1.cxx:3070
virtual void SetContour(Int_t nlevels, const Double_t *levels=0)
Set the number and values of contour levels.
Definition: TH1.cxx:8360
virtual void SetMinimum(Double_t minimum=-1111)
Definition: TH1.h:398
virtual void Scale(Double_t c1=1, Option_t *option="")
Multiply this histogram by a constant c1.
Definition: TH1.cxx:6596
virtual Double_t GetMinimum(Double_t minval=-FLT_MAX) const
Return minimum value larger than minval of bins in the range, unless the value has been overridden by...
Definition: TH1.cxx:8512
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:257
virtual TH2 * Rebin2D(Int_t nxgroup=2, Int_t nygroup=2, const char *newname="")
Rebin this histogram grouping nxgroup/nygroup bins along the xaxis/yaxis together.
Definition: TH2.cxx:1660
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
This class displays a legend box (TPaveText) containing several legend entries.
Definition: TLegend.h:23
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition: TLegend.cxx:331
void Draw(Option_t *option="") override
Draw this legend with its current attributes.
Definition: TLegend.cxx:424
void SetMargin(Float_t margin)
Definition: TLegend.h:69
A doubly linked list.
Definition: TList.h:38
const char * GetName() const override
Returns name of object.
Definition: TNamed.h:47
const char * GetTitle() const override
Returns title of object.
Definition: TNamed.h:48
The most important graphics class in the ROOT system.
Definition: TPad.h:27
void Update() override
Update pad.
Definition: TPad.cxx:2823
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
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:692
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:624
UInt_t GetListOfTitles(TDirectory *rfdir, TList &titles)
Definition: tmvaglob.cxx:643
TKey * NextKey(TIter &keyIter, TString className)
Definition: tmvaglob.cxx:364
TFile * OpenFile(const TString &fin)
Definition: tmvaglob.cxx:192
void SetFrameStyle(TH1 *frame, Float_t scale=1.0)
Definition: tmvaglob.cxx:77
void imgconv(TCanvas *c, const TString &fname)
Definition: tmvaglob.cxx:212
void rulevisCorr(TString fin="TMVA.root", TMVAGlob::TypeOfPlot type=TMVAGlob::kNorm, bool useTMVAStyle=kTRUE)
Short_t Max(Short_t a, Short_t b)
Returns the largest of a and b.
Definition: TMathBase.h:250
Definition: file.py:1
Definition: first.py:1