Logo ROOT   6.08/07
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
22  TFile *file = TMVAGlob::OpenFile( fin );
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;
97  first=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
129  Int_t height;
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 backgroung 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 );
228  first=kFALSE;
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 }
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title Offset is a correction factor with respect to the "s...
Definition: TAttAxis.cxx:262
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
virtual void Scale(Double_t c1=1, Option_t *option="")
Multiply this histogram by a constant c1.
Definition: TH1.cxx:5936
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:7664
void imgconv(TCanvas *c, const TString &fname)
Definition: tmvaglob.cxx:212
Float_t GetLeftMargin() const
Definition: TAttPad.h:46
virtual TList * GetListOfKeys() const
Definition: TDirectory.h:158
virtual void SetMaximum(Double_t maximum=-1111)
Definition: TH1.h:399
This class displays a legend box (TPaveText) containing several legend entries.
Definition: TLegend.h:27
float Float_t
Definition: RtypesCore.h:53
return c
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:635
virtual void SetContour(Int_t nlevels, const Double_t *levels=0)
Set the number and values of contour levels.
Definition: TH1.cxx:7605
TFile * OpenFile(const TString &fin)
Definition: tmvaglob.cxx:192
virtual void Draw(Option_t *option="")
Draw this legend with its current attributes.
Definition: TLegend.cxx:373
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:50
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
virtual void SetMinimum(Double_t minimum=-1111)
Definition: TH1.h:400
#define gROOT
Definition: TROOT.h:364
void SetFrameStyle(TH1 *frame, Float_t scale=1.0)
Definition: tmvaglob.cxx:77
Basic string class.
Definition: TString.h:137
virtual void Update()
Update pad.
Definition: TPad.cxx:2738
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
void SetMargin(Float_t margin)
Definition: TLegend.h:72
const Bool_t kFALSE
Definition: Rtypes.h:92
virtual void SetFillStyle(Style_t fstyle)
Set the fill area style.
Definition: TAttFill.h:44
TKey * NextKey(TIter &keyIter, TString className)
Definition: tmvaglob.cxx:357
TLegend * legend
Definition: pirndm.C:35
std::vector< std::vector< double > > Data
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:30
A doubly linked list.
Definition: TList.h:47
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:46
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2851
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:42
tomato 2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:255
void rulevisCorr(TString fin="TMVA.root", TMVAGlob::TypeOfPlot type=TMVAGlob::kNorm, bool useTMVAStyle=kTRUE)
Definition: rulevisCorr.cxx:15
unsigned int UInt_t
Definition: RtypesCore.h:42
The most important graphics class in the ROOT system.
Definition: TPad.h:37
char * Form(const char *fmt,...)
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:81
TAxis * GetYaxis()
Definition: TH1.h:325
Bool_t InheritsFrom(const char *cl) const
Return kTRUE if this class inherits from a class with name "classname".
Definition: TClass.cxx:4610
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:7749
The Canvas class.
Definition: TCanvas.h:41
virtual TKey * GetKey(const char *, Short_t=9999) const
Definition: TDirectory.h:156
double Double_t
Definition: RtypesCore.h:55
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition: TLegend.cxx:280
Describe directory structure in memory.
Definition: TDirectory.h:44
int type
Definition: TGX11.cxx:120
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:567
virtual void SetRightMargin(Float_t rightmargin)
Set Pad right margin in fraction of the pad width.
Definition: TAttPad.cxx:120
virtual TObject * ReadObj()
To read a TObject* from the file.
Definition: TKey.cxx:730
Float_t GetTopMargin() const
Definition: TAttPad.h:48
virtual Bool_t cd(const char *path=0)
Change current directory to "this" directory.
Definition: TDirectory.cxx:435
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1089
Definition: file.py:1
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:202
#define NULL
Definition: Rtypes.h:82
virtual void SetTitle(const char *title)
Change (i.e.
Definition: TH1.cxx:6027
Definition: first.py:1
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2183
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual void SetBorderSize(Int_t bordersize=4)
Definition: TPave.h:74
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:1557
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:52
const char * Data() const
Definition: TString.h:349
UInt_t GetListOfTitles(TDirectory *rfdir, TList &titles)
Definition: tmvaglob.cxx:634