Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
variables.cxx
Go to the documentation of this file.
1 #include "TMVA/variables.h"
2 
3 
4 // this macro plots the distributions of the different input variables
5 // used in TMVA (e.g. running TMVAnalysis.C). Signal and Background are overlayed.
6 
7 // input: - Input file (result from TMVA),
8 // - normal/decorrelated/PCA
9 // - use of TMVA plotting TStyle
10 void TMVA::variables(TString dataset, TString fin, TString dirName , TString title ,
11  Bool_t isRegression, Bool_t useTMVAStyle )
12 {
13  TString outfname = dirName;
14  outfname.ToLower(); outfname.ReplaceAll( "input", "" );
15 
16  // set style and remove existing canvas'
17  TMVAGlob::Initialize( useTMVAStyle );
18 
19  // obtain shorter histogram title
20  TString htitle = title;
21  htitle.ReplaceAll("variables ","variable");
22  htitle.ReplaceAll("and target(s)","");
23  htitle.ReplaceAll("(training sample)","");
24 
25  // checks if file with name "fin" is already open, and if not opens one
26  TFile* file = TMVAGlob::OpenFile( fin );
27 
28  TDirectory* dir = (TDirectory*)file->GetDirectory(dataset.Data())->Get(dirName );
29  if (dir==0) {
30  cout << "No information about " << title << " available in directory " << dirName << " of file " << fin << endl;
31  return;
32  }
33  dir->cd();
34 
35  // how many plots are in the directory?
36  Int_t noPlots = TMVAGlob::GetNumberOfInputVariables( dir ) +
37  TMVAGlob::GetNumberOfTargets( dir );
38 
39  // define Canvas layout here!
40  // default setting
41  Int_t xPad; // no of plots in x
42  Int_t yPad; // no of plots in y
43  Int_t width; // size of canvas
44  Int_t height;
45  switch (noPlots) {
46  case 1:
47  xPad = 1; yPad = 1; width = 550; height = 0.90*width; break;
48  case 2:
49  xPad = 2; yPad = 1; width = 600; height = 0.50*width; break;
50  case 3:
51  xPad = 3; yPad = 1; width = 900; height = 0.4*width; break;
52  case 4:
53  xPad = 2; yPad = 2; width = 600; height = width; break;
54  default:
55  xPad = 3; yPad = 2; width = 800; height = 0.55*width; break;
56  }
57 
58  Int_t noPadPerCanv = xPad * yPad ;
59 
60  // counter variables
61  Int_t countCanvas = 0;
62  Int_t countPad = 0;
63 
64  // loop over all objects in directory
65  TCanvas* canv = 0;
66  TKey* key = 0;
67  Bool_t createNewFig = kFALSE;
68  TIter next(dir->GetListOfKeys());
69  while ((key = (TKey*)next())) {
70  if (key->GetCycle() != 1) continue;
71 
72  if (!TString(key->GetName()).Contains("__Signal") &&
73  !(isRegression && TString(key->GetName()).Contains("__Regression"))) continue;
74 
75  // make sure, that we only look at histograms
76  TClass *cl = gROOT->GetClass(key->GetClassName());
77  if (!cl->InheritsFrom("TH1")) continue;
78  TH1 *sig = (TH1*)key->ReadObj();
79  TString hname(sig->GetName());
80 
81  // create new canvas
82  if (countPad%noPadPerCanv==0) {
83  ++countCanvas;
84  canv = new TCanvas( Form("canvas%d", countCanvas), title,
85  countCanvas*50+50, countCanvas*20, width, height );
86  canv->Divide(xPad,yPad);
87  canv->Draw();
88  }
89 
90  TPad* cPad = (TPad*)canv->cd(countPad++%noPadPerCanv+1);
91 
92  // find the corredponding backgrouns histo
93  TString bgname = hname;
94  bgname.ReplaceAll("__Signal","__Background");
95  TH1 *bgd = (TH1*)dir->Get(bgname);
96  if (bgd == NULL) {
97  cout << "ERROR!!! couldn't find background histo for" << hname << endl;
98  return;
99  }
100 
101  // this is set but not stored during plot creation in MVA_Factory
102  TMVAGlob::SetSignalAndBackgroundStyle( sig, (isRegression ? 0 : bgd) );
103 
104  sig->SetTitle( TString( htitle ) + ": " + sig->GetTitle() );
105  TMVAGlob::SetFrameStyle( sig, 1.2 );
106 
107  // normalise both signal and background
108  if (!isRegression) TMVAGlob::NormalizeHists( sig, bgd );
109  else {
110  // change histogram title for target
111  TString nme = sig->GetName();
112  if (nme.Contains( "_target" )) {
113  TString tit = sig->GetTitle();
114  sig->SetTitle( tit.ReplaceAll("Input variable", "Regression target" ) );
115  }
116  }
117 
118  // finally plot and overlay
119  Float_t sc = 1.1;
120  if (countPad == 1) sc = 1.3;
121  sig->SetMaximum( TMath::Max( sig->GetMaximum(), bgd->GetMaximum() )*sc );
122  sig->Draw( "hist" );
123  cPad->SetLeftMargin( 0.17 );
124 
125  sig->GetYaxis()->SetTitleOffset( 1.70 );
126  if (!isRegression) {
127  bgd->Draw("histsame");
128  TString ytit = TString("(1/N) ") + sig->GetYaxis()->GetTitle();
129  sig->GetYaxis()->SetTitle( ytit ); // histograms are normalised
130  }
131 
132  // Draw legend
133  if (countPad == 1 && !isRegression) {
134  TLegend *legend= new TLegend( cPad->GetLeftMargin(),
135  1-cPad->GetTopMargin()-.15,
136  cPad->GetLeftMargin()+.4,
137  1-cPad->GetTopMargin() );
138  legend->SetFillStyle(1);
139  legend->AddEntry(sig,"Signal","F");
140  legend->AddEntry(bgd,"Background","F");
141  legend->SetBorderSize(1);
142  legend->SetMargin( 0.3 );
143  legend->Draw("same");
144  }
145 
146  // redraw axes
147  sig->Draw("sameaxis");
148 
149  // text for overflows
150  Int_t nbin = sig->GetNbinsX();
151  Double_t dxu = sig->GetBinWidth(0);
152  Double_t dxo = sig->GetBinWidth(nbin+1);
153  TString uoflow = "";
154  if (isRegression) {
155  uoflow = Form( "U/O-flow: %.1f%% / %.1f%%",
156  sig->GetBinContent(0)*dxu*100, sig->GetBinContent(nbin+1)*dxo*100 );
157  }
158  else {
159  uoflow = Form( "U/O-flow (S,B): (%.1f, %.1f)%% / (%.1f, %.1f)%%",
160  sig->GetBinContent(0)*dxu*100, bgd->GetBinContent(0)*dxu*100,
161  sig->GetBinContent(nbin+1)*dxo*100, bgd->GetBinContent(nbin+1)*dxo*100 );
162  }
163 
164  TText* t = new TText( 0.98, 0.14, uoflow );
165  t->SetNDC();
166  t->SetTextSize( 0.040 );
167  t->SetTextAngle( 90 );
168  t->AppendPad();
169 
170  // save canvas to file
171  if (countPad%noPadPerCanv==0) {
172  TString fname = Form( "%s/plots/%s_c%i",dataset.Data(), outfname.Data(), countCanvas );
173  TMVAGlob::plot_logo();
174  TMVAGlob::imgconv( canv, fname );
175  createNewFig = kFALSE;
176  }
177  else {
178  createNewFig = kTRUE;
179  }
180  }
181 
182  if (createNewFig) {
183  TString fname = Form( "%s/plots/%s_c%i",dataset.Data(), outfname.Data(), countCanvas );
184  TMVAGlob::plot_logo();
185  TMVAGlob::imgconv( canv, fname );
186  createNewFig = kFALSE;
187  }
188 
189  return;
190 }