Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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
15 void TMVA::rulevisCorr( TString fin , TMVAGlob::TypeOfPlot type , bool )
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 
61 void TMVA::rulevisCorr( TDirectory *rfdir, TDirectory *vardir, TDirectory *corrdir, TMVAGlob::TypeOfPlot type) {
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;
81  Bool_t first=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 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 );
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 }