Logo ROOT  
Reference Guide
variablesMultiClass.cxx
Go to the documentation of this file.
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::variablesMultiClass(TString dataset, TString fin , TString dirName , TString title,
11 Bool_t /* isRegression */, Bool_t useTMVAStyle )
12{
13 TString outfname = dirName;
14 TString tmp = dirName;
15 tmp.ReplaceAll("InputVariables_","");
16 outfname.ToLower(); outfname.ReplaceAll( "input", "" );
17
18 // set style and remove existing canvas'
19 TMVAGlob::Initialize( useTMVAStyle );
20
21 // obtain shorter histogram title
22 TString htitle = title;
23 htitle.ReplaceAll("variables ","variable");
24 htitle.ReplaceAll("and target(s)","");
25 htitle.ReplaceAll("(training sample)","");
26
27 // checks if file with name "fin" is already open, and if not opens one
29
30 TDirectory* dir = (TDirectory*)file->GetDirectory(dataset.Data())->Get( dirName );
31 if (dir==0) {
32 cout << "No information about " << title << " available in directory " << dirName << " of file " << fin << endl;
33 return;
34 }
35 dir->cd();
36
37 // how many plots are in the directory?
39
40 // define Canvas layout here!
41 // default setting
42 Int_t xPad; // no of plots in x
43 Int_t yPad; // no of plots in y
44 Int_t width; // size of canvas
46 switch (noPlots) {
47 case 1:
48 xPad = 1; yPad = 1; width = 550; height = 0.90*width; break;
49 case 2:
50 xPad = 2; yPad = 1; width = 600; height = 0.50*width; break;
51 case 3:
52 xPad = 3; yPad = 1; width = 900; height = 0.4*width; break;
53 case 4:
54 xPad = 2; yPad = 2; width = 600; height = width; break;
55 default:
56 xPad = 3; yPad = 2; width = 800; height = 0.55*width; break;
57 }
58
59 Int_t noPadPerCanv = xPad * yPad ;
60
61 // counter variables
62 Int_t countCanvas = 0;
63 Int_t countPad = 0;
64
65 // loop over all objects in directory
66 TCanvas* canv = 0;
67 Bool_t createNewFig = kFALSE;
68 TIter next(dir->GetListOfKeys());
69
70 std::vector<TString> varnames(TMVAGlob::GetInputVariableNames(dir));
71 std::vector<TString> classnames(TMVAGlob::GetClassNames(dir));
72
73
74 std::vector<TString>::iterator variter = varnames.begin();
75 std::vector<TString>::iterator classiter = classnames.begin();
76
77 /*
78 std::vector<TString>::const_iterator variter = varnames.begin();
79 std::cout << "Available variables:" << std::endl;
80 while(variter != varnames.end()){
81 std::cout << *variter << std::endl;
82 variter++;
83 }
84
85 std::vector<TString>::const_iterator classiter = classnames.begin();
86 std::cout << "Available classes:" << std::endl;
87 while(classiter != classnames.end()){
88 std::cout << *classiter << std::endl;
89 classiter++;
90 }
91 */
92
93 variter = varnames.begin();
94 for(; variter!=varnames.end(); ++variter){
95
96 //create new canvas
97 if (countPad%noPadPerCanv==0) {
98 ++countCanvas;
99 canv = new TCanvas( Form("canvas%d", countCanvas), title,
100 countCanvas*50+50, countCanvas*20, width, height );
101 canv->Divide(xPad,yPad);
102 canv->Draw();
103 }
104 TPad* cPad = (TPad*)canv->cd(countPad++%noPadPerCanv+1);
105 classiter = classnames.begin();
106
107 TObjArray hists;
108 for(; classiter!=classnames.end(); ++classiter){
109 //assemble histogram names
110 TString hname(*variter + "__" + *classiter + "_" + tmp);
111 TH1 *hist = (TH1*)dir->Get(hname);
112 //cout << "Looking for histgram " << hname << endl;
113 if (hist == NULL) {
114 cout << "ERROR!!! couldn't find " << *variter << " histogram for class " << *classiter << endl;
115 //exit(1);
116 return;
117 }
118 hists.Add(hist);
119 }
120
121 // this is set but not stored during plot creation in MVA_Factory
122 //TMVAGlob::SetSignalAndBackgroundStyle(((TH1*)hists[0]), ((TH1*)hists[1]));
124
125 ((TH1*)hists.First())->SetTitle( TString( htitle ) + ": " + *variter );
126
127 TMVAGlob::SetFrameStyle( ((TH1*)hists.First()), 1.2 );
128
129 // normalise all histograms and find maximum
130 Float_t histmax = -1;
131 for(Int_t i=0; i<hists.GetEntriesFast(); ++i){
132 TMVAGlob::NormalizeHist((TH1*)hists[i] );
133 if(((TH1*)hists[i])->GetMaximum() > histmax)
134 histmax = ((TH1*)hists[i])->GetMaximum();
135 }
136
137 // finally plot and overlay
138 Float_t sc = 1.1;
139 if (countPad == 1) sc = 1.3;
140 ((TH1*)hists.First())->SetMaximum( histmax*sc );
141
142 ((TH1*)hists.First())->Draw( "hist" );
143 cPad->SetLeftMargin( 0.17 );
144 ((TH1*)hists.First())->GetYaxis()->SetTitleOffset( 1.70 );
145
146 for(Int_t i=1; i<hists.GetEntriesFast(); ++i){
147
148 ((TH1*)hists[i])->Draw("histsame");
149 TString ytit = TString("(1/N) ") + ((TH1*)hists[i])->GetYaxis()->GetTitle();
150 ((TH1*)hists[i])->GetYaxis()->SetTitle( ytit ); // histograms are normalised
151
152 }
153
154 // Draw legend
155 if (countPad == 1) {
156 TLegend *legend= new TLegend( cPad->GetLeftMargin(),
157 1-cPad->GetTopMargin()-.15,
158 cPad->GetLeftMargin()+.4,
159 1-cPad->GetTopMargin() );
160 legend->SetFillStyle(1);
161
162 classiter = classnames.begin();
163
164 for(Int_t i=0; i<hists.GetEntriesFast(); ++i, ++classiter){
165 legend->AddEntry(((TH1*)hists[i]),*classiter,"F");
166 }
167
168 legend->SetBorderSize(1);
169 legend->SetMargin( 0.3 );
170 legend->Draw("same");
171 }
172
173 // redraw axes
174 ((TH1*)hists.First())->Draw("sameaxis");
175
176
177 // text for overflows
178 Int_t nbin = ((TH1*)hists.First())->GetNbinsX();
179 Double_t dxu = ((TH1*)hists.First())->GetBinWidth(0);
180 Double_t dxo = ((TH1*)hists.First())->GetBinWidth(nbin+1);
181 TString uoflow = "";
182
183 classiter = classnames.begin();
184 for(Int_t i=0; i<hists.GetEntriesFast(); ++i, ++classiter){
185 if(((TH1*)hists[i])->GetBinContent(0)!=0 || ((TH1*)hists[i])->GetBinContent(nbin+1)!=0){
186 uoflow += *classiter;
187 uoflow += Form( " U/O-flow: %.1f / %.1f %%",
188 ((TH1*)hists[i])->GetBinContent(0)*dxu*100, ((TH1*)hists[i])->GetBinContent(nbin+1)*dxo*100);
189 }
190 }
191
192 TText* t = new TText( 0.98, 0.14, uoflow );
193 t->SetNDC();
194 t->SetTextSize( 0.040 );
195 t->SetTextAngle( 90 );
196 t->AppendPad();
197
198
199 // save canvas to file
200 if (countPad%noPadPerCanv==0) {
201 TString fname = dataset+Form( "/plots/%s_c%i", outfname.Data(), countCanvas );
203 TMVAGlob::imgconv( canv, fname );
204 createNewFig = kFALSE;
205 }
206 else {
207 createNewFig = kTRUE;
208 }
209 }
210
211 if (createNewFig) {
212 TString fname = dataset+Form( "/plots/%s_c%i", outfname.Data(), countCanvas );
214 TMVAGlob::imgconv( canv, fname );
215 createNewFig = kFALSE;
216 }
217
218 return;
219}
bool Bool_t
Definition: RtypesCore.h:63
int Int_t
Definition: RtypesCore.h:45
const Bool_t kFALSE
Definition: RtypesCore.h:101
float Float_t
Definition: RtypesCore.h:57
double Double_t
Definition: RtypesCore.h:59
const Bool_t kTRUE
Definition: RtypesCore.h:100
Option_t Option_t width
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t height
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition: TString.cxx:2447
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:43
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition: TAttText.h:47
The Canvas class.
Definition: TCanvas.h:23
void Draw(Option_t *option="") override
Draw a canvas.
Definition: TCanvas.cxx:843
TVirtualPad * cd(Int_t subpadnumber=0) override
Set current canvas & pad.
Definition: TCanvas.cxx:706
Describe directory structure in memory.
Definition: TDirectory.h:45
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
Definition: TDirectory.cxx:814
virtual TList * GetListOfKeys() const
Definition: TDirectory.h:214
virtual Bool_t cd(const char *path=nullptr)
Change current directory to "this" directory.
Definition: TDirectory.cxx:504
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
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:331
void Draw(Option_t *option="") override
Draw this legend with its current attributes.
Definition: TLegend.cxx:424
void SetMargin(Float_t margin)
Definition: TLegend.h:69
An array of TObjects.
Definition: TObjArray.h:31
Int_t GetEntriesFast() const
Definition: TObjArray.h:58
TObject * First() const override
Return the object in the first slot.
Definition: TObjArray.cxx:496
void Add(TObject *obj) override
Definition: TObjArray.h:68
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:27
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:1154
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:1150
const char * Data() const
Definition: TString.h:369
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:692
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 Initialize(Bool_t useTMVAStyle=kTRUE)
Definition: tmvaglob.cxx:176
Int_t GetNumberOfInputVariables(TDirectory *dir)
Definition: tmvaglob.cxx:420
void plot_logo(Float_t v_scale=1.0, Float_t skew=1.0)
Definition: tmvaglob.cxx:270
TFile * OpenFile(const TString &fin)
Definition: tmvaglob.cxx:192
void SetFrameStyle(TH1 *frame, Float_t scale=1.0)
Definition: tmvaglob.cxx:77
std::vector< TString > GetInputVariableNames(TDirectory *dir)
Definition: tmvaglob.cxx:436
void SetMultiClassStyle(TObjArray *hists)
Definition: tmvaglob.cxx:47
void NormalizeHist(TH1 *h)
Definition: tmvaglob.cxx:315
std::vector< TString > GetClassNames(TDirectory *dir)
Definition: tmvaglob.cxx:469
void imgconv(TCanvas *c, const TString &fname)
Definition: tmvaglob.cxx:212
void variablesMultiClass(TString dataset, TString fin="TMVA.root", TString dirName="InputVariables_Id", TString title="TMVA Input Variables", Bool_t isRegression=kFALSE, Bool_t useTMVAStyle=kTRUE)
Definition: file.py:1
th1 Draw()