Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
10void 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}
int Int_t
Definition RtypesCore.h:45
const Bool_t kFALSE
Definition RtypesCore.h:92
bool Bool_t
Definition RtypesCore.h:63
double Double_t
Definition RtypesCore.h:59
float Float_t
Definition RtypesCore.h:57
const Bool_t kTRUE
Definition RtypesCore.h:91
include TDocParser_001 C image html pict1_TDocParser_001 png width
#define gROOT
Definition TROOT.h:406
char * Form(const char *fmt,...)
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition TAttAxis.cxx:293
virtual void SetFillStyle(Style_t fstyle)
Set the fill area style.
Definition TAttFill.h:39
virtual void SetLeftMargin(Float_t leftmargin)
Set Pad left margin in fraction of the pad width.
Definition TAttPad.cxx:109
Float_t GetLeftMargin() const
Definition TAttPad.h:44
Float_t GetTopMargin() const
Definition TAttPad.h:46
virtual void SetTextAngle(Float_t tangle=0)
Set the text angle.
Definition TAttText.h:42
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition TAttText.h:46
const char * GetTitle() const
Returns title of object.
Definition TAxis.h:129
The Canvas class.
Definition TCanvas.h:23
void Draw(Option_t *option="") override
Draw a canvas.
Definition TCanvas.cxx:845
TVirtualPad * cd(Int_t subpadnumber=0) override
Set current canvas & pad.
Definition TCanvas.cxx:708
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:80
Bool_t InheritsFrom(const char *cl) const
Return kTRUE if this class inherits from a class with name "classname".
Definition TClass.cxx:4851
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
Definition TClass.cxx:2957
Describe directory structure in memory.
Definition TDirectory.h:45
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
virtual TList * GetListOfKeys() const
Definition TDirectory.h:177
virtual Bool_t cd(const char *path=nullptr)
Change current directory to "this" directory.
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition TFile.h:54
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:58
virtual void SetTitle(const char *title)
See GetStatOverflows for more information.
Definition TH1.cxx:6678
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:8390
virtual Int_t GetNbinsX() const
Definition TH1.h:296
virtual void SetMaximum(Double_t maximum=-1111)
Definition TH1.h:398
TAxis * GetYaxis()
Definition TH1.h:321
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition TH1.cxx:3073
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition TH1.cxx:4993
virtual Double_t GetBinWidth(Int_t bin) const
Return bin width for 1D histogram.
Definition TH1.cxx:9003
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition TKey.h:28
virtual const char * GetClassName() const
Definition TKey.h:76
Short_t GetCycle() const
Return cycle number associated to this key.
Definition TKey.cxx:577
virtual TObject * ReadObj()
To read a TObject* from the file.
Definition TKey.cxx:750
This class displays a legend box (TPaveText) containing several legend entries.
Definition TLegend.h:23
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition TLegend.cxx:330
virtual void Draw(Option_t *option="")
Draw this legend with its current attributes.
Definition TLegend.cxx:423
void SetMargin(Float_t margin)
Definition TLegend.h:69
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:164
virtual const char * GetTitle() const
Returns title of object.
Definition TNamed.h:48
virtual const char * GetName() const
Returns name of object.
Definition TNamed.h:47
virtual void AppendPad(Option_t *option="")
Append graphics object to current pad.
Definition TObject.cxx:107
The most important graphics class in the ROOT system.
Definition TPad.h:26
void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0) override
Automatic pad generation by division.
Definition TPad.cxx:1177
virtual void SetBorderSize(Int_t bordersize=4)
Definition TPave.h:73
Basic string class.
Definition TString.h:136
void ToLower()
Change string to lower-case.
Definition TString.cxx:1145
const char * Data() const
Definition TString.h:369
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition TString.h:692
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:624
Base class for several text objects.
Definition TText.h:22
virtual void SetNDC(Bool_t isNDC=kTRUE)
Set NDC mode on if isNDC = kTRUE, off otherwise.
Definition TText.cxx:813
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)
Short_t Max(Short_t a, Short_t b)
Definition TMathBase.h:212
Definition file.py:1