Logo ROOT   6.08/07
Reference Guide
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?
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 );
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 );
185  TMVAGlob::imgconv( canv, fname );
186  createNewFig = kFALSE;
187  }
188 
189  return;
190 }
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 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
Int_t GetNumberOfInputVariables(TDirectory *dir)
Definition: tmvaglob.cxx:413
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
Definition: TDirectory.cxx:729
float Float_t
Definition: RtypesCore.h:53
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:635
TFile * OpenFile(const TString &fin)
Definition: tmvaglob.cxx:192
void NormalizeHists(TH1 *sig, TH1 *bkg=0)
Definition: tmvaglob.cxx:317
virtual const char * GetClassName() const
Definition: TKey.h:77
virtual void Draw(Option_t *option="")
Draw this legend with its current attributes.
Definition: TLegend.cxx:373
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition: TH1.cxx:4638
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
void SetSignalAndBackgroundStyle(TH1 *sig, TH1 *bkg, TH1 *all=0)
Definition: tmvaglob.cxx:8
#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
void ToLower()
Change string to lower-case.
Definition: TString.cxx:1089
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
TLegend * legend
Definition: pirndm.C:35
Int_t GetNumberOfTargets(TDirectory *dir)
Definition: tmvaglob.cxx:396
Base class for several text objects.
Definition: TText.h:33
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:30
const char * GetTitle() const
Returns title of object.
Definition: TAxis.h:135
void Initialize(Bool_t useTMVAStyle=kTRUE)
Definition: tmvaglob.cxx:176
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2851
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
void plot_logo(Float_t v_scale=1.0, Float_t skew=1.0)
Definition: tmvaglob.cxx:263
Bool_t InheritsFrom(const char *cl) const
Return kTRUE if this class inherits from a class with name "classname".
Definition: TClass.cxx:4610
virtual TDirectory * GetDirectory(const char *apath, Bool_t printError=false, const char *funcname="GetDirectory")
Find a directory named "apath".
The Canvas class.
Definition: TCanvas.h:41
virtual Double_t GetBinWidth(Int_t bin) const
Return bin width for 1D histogram.
Definition: TH1.cxx:8273
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
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:567
The TH1 histogram class.
Definition: TH1.h:80
virtual void Draw(Option_t *option="")
Draw a canvas.
Definition: TCanvas.cxx:795
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
virtual Int_t GetNbinsX() const
Definition: TH1.h:301
Short_t GetCycle() const
Return cycle number associated to this key.
Definition: TKey.cxx:564
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:155
void variables(TString dataset, TString fin="TMVA.root", TString dirName="InputVariables_Id", TString title="TMVA Input Variables", Bool_t isRegression=kFALSE, Bool_t useTMVAStyle=kTRUE)
Definition: variables.cxx:10
virtual void SetBorderSize(Int_t bordersize=4)
Definition: TPave.h:74
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:52
virtual void SetLeftMargin(Float_t leftmargin)
Set Pad left margin in fraction of the pad width.
Definition: TAttPad.cxx:110
const char * Data() const
Definition: TString.h:349