ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
rulevisHists.cxx
Go to the documentation of this file.
1 #include "TMVA/rulevisHists.h"
2 #include "TH2.h"
3 
4 
5 // This macro plots the distributions of the different input variables overlaid on
6 // the sum of importance per bin.
7 // The scale goes from violett (no importance) to red (high importance).
8 // Areas where many important rules are active, will thus be very red.
9 //
10 // input: - Input file (result from TMVA),
11 // - normal/decorrelated/PCA
12 // - use of TMVA plotting TStyle
13 void TMVA::rulevisHists( TString fin, TMVAGlob::TypeOfPlot type , bool useTMVAStyle )
14 {
15  // set style and remove existing canvas'
16  TMVAGlob::Initialize( useTMVAStyle );
17 
18  // checks if file with name "fin" is already open, and if not opens one
19  //TFile *file =
20  TMVAGlob::OpenFile( fin );
21 
22  // get all titles of the method rulefit
23  TList titles;
24  TString dirname = "Method_RuleFit";
25  UInt_t ninst = TMVAGlob::GetListOfTitles(dirname,titles);
26  if (ninst==0) return;
27 
28  // get top dir containing all hists of the variables
30  if (vardir==0) return;
31 
32  TDirectory* corrdir = TMVAGlob::GetCorrelationPlotsDir( type, vardir );
33  if (corrdir==0) return;
34 
35  // loop over all titles
36  TIter keyIter(&titles);
37  TDirectory *rfdir;
38  TKey *rfkey;
39  while ((rfkey = TMVAGlob::NextKey(keyIter,"TDirectory"))) {
40  rfdir = (TDirectory *)rfkey->ReadObj();
41  rulevisHists( rfdir, vardir, corrdir, type );
42  }
43 }
44 
46  //
47  if (rfdir==0) return;
48  if (vardir==0) return;
49  if (corrdir==0) return;
50  //
51  const TString rfName = rfdir->GetName();
52  const TString maintitle = rfName + " : Rule Importance";
53  const TString rfNameOpt = "_RF2D_";
54  const TString outfname[TMVAGlob::kNumOfMethods] = { "rulevisHists",
55  "rulevisHists_decorr",
56  "rulevisCorr_pca",
57  "rulevisCorr_gaussdecorr" };
58 
59  const TString outputName = outfname[type]+"_"+rfdir->GetName();
60  //
61  TIter rfnext(rfdir->GetListOfKeys());
62  TKey *rfkey;
63  Double_t rfmax=0;//keep compiler quiet .. it IS in any case
64  Double_t rfmin=0;// later initialized
65  // Bool_t allEmpty=kTRUE;
67  while ((rfkey = (TKey*)rfnext())) {
68  // make sure, that we only look at histograms
69  TClass *cl = gROOT->GetClass(rfkey->GetClassName());
70  if (!cl->InheritsFrom("TH2F")) continue;
71  TH2F *hrf = (TH2F*)rfkey->ReadObj();
72  TString hname= hrf->GetName();
73  if (hname.Contains("__RF_")){ // found a new RF plot
74  Double_t valmin = hrf->GetMinimum();
75  Double_t valmax = hrf->GetMaximum();
76  if (first) {
77  rfmin=valmin;
78  rfmax=valmax;
79  first = kFALSE;
80  } else {
81  if (valmax>rfmax) rfmax=valmax;
82  if (valmin<rfmin) rfmin=valmin;
83  }
84  // if (hrf->GetEntries()>0) allEmpty=kFALSE;
85  }
86  }
87  if (first) {
88  cout << "ERROR: no RF plots found..." << endl;
89  return;
90  }
91 
92  const Int_t nContours = 100;
93  Double_t contourLevels[nContours];
94  Double_t dcl = (rfmax-rfmin)/Double_t(nContours-1);
95  //
96  for (Int_t i=0; i<nContours; i++) {
97  contourLevels[i] = rfmin+dcl*Double_t(i);
98  }
99 
100  ///////////////////////////
101  vardir->cd();
102 
103  // how many plots are in the directory?
104  Int_t noPlots = ((vardir->GetListOfKeys())->GetEntries()) / 2;
105 
106  // define Canvas layout here!
107  // default setting
108  Int_t xPad; // no of plots in x
109  Int_t yPad; // no of plots in y
110  Int_t width; // size of canvas
111  Int_t height;
112  switch (noPlots) {
113  case 1:
114  xPad = 1; yPad = 1; width = 500; height = 0.7*width; break;
115  case 2:
116  xPad = 2; yPad = 1; width = 600; height = 0.7*width; break;
117  case 3:
118  xPad = 3; yPad = 1; width = 900; height = 0.4*width; break;
119  case 4:
120  xPad = 2; yPad = 2; width = 600; height = width; break;
121  default:
122  xPad = 3; yPad = 2; width = 800; height = 0.7*width; break;
123  }
124  Int_t noPad = xPad * yPad ;
125 
126  // this defines how many canvases we need
127  const Int_t noCanvas = 1 + (Int_t)((noPlots - 0.001)/noPad);
128  TCanvas **c = new TCanvas*[noCanvas];
129  for (Int_t ic=0; ic<noCanvas; ic++) c[ic] = 0;
130 
131  // counter variables
132  Int_t countCanvas = 0;
133  Int_t countPad = 1;
134 
135  // loop over all objects in directory
136  TIter next(vardir->GetListOfKeys());
137  TKey *key;
138  //
139  first = kTRUE;
140 
141  while ((key = (TKey*)next())) {
142 
143  // make sure, that we only look at histograms
144  TClass *cl = gROOT->GetClass(key->GetClassName());
145  if (!cl->InheritsFrom("TH1")) continue;
146  TH1F* sig = (TH1F*)key->ReadObj();
147  TString hname= sig->GetName();
148 
149  // check for all signal histograms
150  if (hname.Contains("__S")){ // found a new signal plot
151  // create new canvas
152  if ((c[countCanvas]==NULL) || (countPad>noPad)) {
153  char cn[20];
154  sprintf( cn, "rulehist%d_", countCanvas+1 );
155  TString cname(cn);
156  cname += rfdir->GetName();
157  c[countCanvas] = new TCanvas( cname, maintitle,
158  countCanvas*50+200, countCanvas*20, width, height );
159  // style
160  c[countCanvas]->Divide(xPad,yPad);
161  countPad = 1;
162  }
163 
164  // save canvas to file
165  TPad *cPad = (TPad *)(c[countCanvas]->GetPad(countPad));
166  c[countCanvas]->cd(countPad);
167  countPad++;
168 
169  // find the corredponding background histo
170  TString bgname = hname;
171  bgname.ReplaceAll("__S","__B");
172  TKey *hkey = vardir->GetKey(bgname);
173  TH1F* bgd = (TH1F*)hkey->ReadObj();
174  if (bgd == NULL) {
175  cout << "ERROR!!! couldn't find backgroung histo for" << hname << endl;
176  //exit(1);
177  return;
178  }
179 
180  TString rfname = hname;
181  rfname.ReplaceAll("__S","__RF");
182  TKey *hrfkey = rfdir->GetKey(rfname);
183  TH2F *hrf = (TH2F*)hrfkey->ReadObj();
184  // Double_t wv = hrf->GetMaximum();
185  // if (rfmax>0.0)
186  // hrf->Scale(1.0/rfmax);
187  hrf->SetMinimum(rfmin); // make sure it's zero -> for palette axis
188  hrf->SetMaximum(rfmax); // make sure max is 1.0 -> idem
189  hrf->SetContour(nContours,&contourLevels[0]);
190 
191  // this is set but not stored during plot creation in MVA_Factory
192  // TMVAGlob::SetSignalAndBackgroundStyle( sigK, bgd );
193  sig->SetFillStyle(3002);
194  sig->SetFillColor(1);
195  sig->SetLineColor(1);
196  sig->SetLineWidth(2);
197 
198  bgd->SetFillStyle(3554);
199  bgd->SetFillColor(1);
200  bgd->SetLineColor(1);
201  bgd->SetLineWidth(2);
202 
203  // chop off "signal"
204  TString title(hrf->GetTitle());
205  title.ReplaceAll("signal","");
206 
207  // finally plot and overlay
208  Float_t sc = 1.1;
209  if (countPad==2) sc = 1.3;
210  sig->SetMaximum( TMath::Max( sig->GetMaximum(), bgd->GetMaximum() )*sc );
211  Double_t smax = sig->GetMaximum();
212 
213  if (first) {
214  hrf->SetTitle( maintitle );
215  first = kFALSE;
216  } else {
217  hrf->SetTitle( "" );
218  }
219  hrf->Draw("colz ah");
220  TMVAGlob::SetFrameStyle( hrf, 1.2 );
221 
222  sig->Draw("same ah");
223  bgd->Draw("same ah");
224  // draw axis using range [0,smax]
225  hrf->GetXaxis()->SetTitle( title );
226  hrf->GetYaxis()->SetTitleOffset( 1.30 );
227  hrf->GetYaxis()->SetTitle("Events");
228  hrf->GetYaxis()->SetLimits(0,smax);
229  hrf->Draw("same axis");
230 
231  cPad->SetRightMargin(0.13);
232  cPad->Update();
233 
234  // Draw legend
235  if (countPad==2){
236  TLegend *legend= new TLegend( cPad->GetLeftMargin(),
237  1-cPad->GetTopMargin()-.18,
238  cPad->GetLeftMargin()+.4,
239  1-cPad->GetTopMargin() );
240  legend->AddEntry(sig,"Signal","F");
241  legend->AddEntry(bgd,"Background","F");
242  legend->Draw("same");
243  legend->SetBorderSize(1);
244  legend->SetMargin( 0.3 );
245  legend->SetFillColor(19);
246  legend->SetFillStyle(1);
247  }
248 
249  // save canvas to file
250  if (countPad > noPad) {
251  c[countCanvas]->Update();
252  TString fname = Form( "plots/%s_c%i", outputName.Data(), countCanvas+1 );
253  TMVAGlob::imgconv( c[countCanvas], fname );
254  // TMVAGlob::plot_logo(); // don't understand why this doesn't work ... :-(
255  countCanvas++;
256  }
257  }
258  }
259 
260  if (countPad <= noPad) {
261  c[countCanvas]->Update();
262  TString fname = Form( "plots/%s_c%i", outputName.Data(), countCanvas+1 );
263  TMVAGlob::imgconv( c[countCanvas], fname );
264  }
265 }
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:245
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:52
virtual void SetLineWidth(Width_t lwidth)
Definition: TAttLine.h:57
void imgconv(TCanvas *c, const TString &fname)
Definition: tmvaglob.cxx:212
virtual void SetMaximum(Double_t maximum=-1111)
Definition: TH1.h:394
This class displays a legend box (TPaveText) containing several legend entries.
Definition: TLegend.h:35
virtual void SetLimits(Double_t xmin, Double_t xmax)
Definition: TAxis.h:154
virtual TList * GetListOfKeys() const
Definition: TDirectory.h:158
ClassImp(TSeqCollection) Int_t TSeqCollection TIter next(this)
Return index of object in collection.
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:7863
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
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
virtual void SetMinimum(Double_t minimum=-1111)
Definition: TH1.h:395
#define gROOT
Definition: TROOT.h:344
void SetFrameStyle(TH1 *frame, Float_t scale=1.0)
Definition: tmvaglob.cxx:77
Basic string class.
Definition: TString.h:137
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:570
virtual void Update()
Update pad.
Definition: TPad.cxx:2721
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
void SetMargin(Float_t margin)
Definition: TLegend.h:80
const Bool_t kFALSE
Definition: Rtypes.h:92
virtual void SetFillStyle(Style_t fstyle)
Definition: TAttFill.h:52
TKey * NextKey(TIter &keyIter, TString className)
Definition: tmvaglob.cxx:357
TDirectory * GetCorrelationPlotsDir(TMVAGlob::TypeOfPlot type, TDirectory *dir=0)
Definition: tmvaglob.cxx:716
TLegend * legend
Definition: pirndm.C:35
const char * Data() const
Definition: TString.h:349
virtual TKey * GetKey(const char *, Short_t=9999) const
Definition: TDirectory.h:156
Float_t GetTopMargin() const
Definition: TAttPad.h:56
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)
Definition: TAttLine.h:54
TDirectory * GetInputVariablesDir(TMVAGlob::TypeOfPlot type, TDirectory *dir=0)
Definition: tmvaglob.cxx:698
void Initialize(Bool_t useTMVAStyle=kTRUE)
Definition: tmvaglob.cxx:176
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
TPaveLabel title(3, 27.1, 15, 28.7,"ROOT Environment and Tools")
virtual void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:256
unsigned int UInt_t
Definition: RtypesCore.h:42
The most important graphics class in the ROOT system.
Definition: TPad.h:46
char * Form(const char *fmt,...)
bool first
Definition: line3Dfit.C:48
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:81
TAxis * GetYaxis()
Definition: TH1.h:320
Float_t GetLeftMargin() const
Definition: TAttPad.h:54
The Canvas class.
Definition: TCanvas.h:48
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
virtual void SetRightMargin(Float_t rightmargin)
Set Pad right margin in fraction of the pad width.
Definition: TAttPad.cxx:117
virtual TObject * ReadObj()
To read a TObject* from the file.
Definition: TKey.cxx:727
virtual Bool_t cd(const char *path=0)
Change current directory to "this" directory.
Definition: TDirectory.cxx:433
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:1073
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:202
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:567
#define NULL
Definition: Rtypes.h:82
void rulevisHists(TString fin="TMVA.root", TMVAGlob::TypeOfPlot type=TMVAGlob::kNorm, bool useTMVAStyle=kTRUE)
Bool_t InheritsFrom(const char *cl) const
Return kTRUE if this class inherits from a class with name "classname".
Definition: TClass.cxx:4498
virtual void SetTitle(const char *title)
Change (i.e.
Definition: TH1.cxx:6268
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2179
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:7921
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual void SetTitle(const char *title="")
Change (i.e. set) the title of the TNamed.
Definition: TNamed.cxx:152
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:8006
virtual void SetBorderSize(Int_t bordersize=4)
Definition: TPave.h:82
TAxis * GetXaxis()
Definition: TH1.h:319
UInt_t GetListOfTitles(TDirectory *rfdir, TList &titles)
Definition: tmvaglob.cxx:634