ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/benhoob/HWW/variablesMultiClass.C
Revision: 1.1
Committed: Mon Feb 14 12:39:14 2011 UTC (14 years, 3 months ago) by benhoob
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Log Message:
Initial commit

File Contents

# Content
1 #include "tmvaglob.C"
2
3 // this macro plots the distributions of the different input variables
4 // used in TMVA (e.g. running TMVAnalysis.C). Signal and Background are overlayed.
5
6 // input: - Input file (result from TMVA),
7 // - normal/decorrelated/PCA
8 // - use of TMVA plotting TStyle
9 void variablesMultiClass( TString fin = "TMVA.root", TString dirName = "InputVariables_Id", TString title = "TMVA Input Variables",
10 Bool_t isRegression = kFALSE, Bool_t useTMVAStyle = kTRUE )
11 {
12 TString outfname = dirName;
13 TString tmp = dirName;
14 tmp.ReplaceAll("InputVariables_","");
15 outfname.ToLower(); outfname.ReplaceAll( "input", "" );
16
17 // set style and remove existing canvas'
18 TMVAGlob::Initialize( useTMVAStyle );
19
20 // obtain shorter histogram title
21 TString htitle = title;
22 htitle.ReplaceAll("variables ","variable");
23 htitle.ReplaceAll("and target(s)","");
24 htitle.ReplaceAll("(training sample)","");
25
26 // checks if file with name "fin" is already open, and if not opens one
27 TFile* file = TMVAGlob::OpenFile( fin );
28
29 TDirectory* dir = (TDirectory*)file->Get( dirName );
30 if (dir==0) {
31 cout << "No information about " << title << " available in directory " << dirName << " of file " << fin << endl;
32 return;
33 }
34 dir->cd();
35
36 // how many plots are in the directory?
37 Int_t noPlots = TMVAGlob::GetNumberOfInputVariables( 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
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;
116 }
117 hists.Add(hist);
118 }
119
120 // this is set but not stored during plot creation in MVA_Factory
121 //TMVAGlob::SetSignalAndBackgroundStyle(((TH1*)hists[0]), ((TH1*)hists[1]));
122 TMVAGlob::SetMultiClassStyle( &hists );
123
124 ((TH1*)hists.First())->SetTitle( TString( htitle ) + ": " + *variter );
125
126 TMVAGlob::SetFrameStyle( ((TH1*)hists.First()), 1.2 );
127
128 // normalise all histograms and find maximum
129 Float_t histmax = -1;
130 for(Int_t i=0; i<hists.GetEntriesFast(); ++i){
131 TMVAGlob::NormalizeHist((TH1*)hists[i] );
132 if(((TH1*)hists[i])->GetMaximum() > histmax)
133 histmax = ((TH1*)hists[i])->GetMaximum();
134 }
135
136 // finally plot and overlay
137 Float_t sc = 1.1;
138 if (countPad == 1) sc = 1.3;
139 ((TH1*)hists.First())->SetMaximum( histmax*sc );
140
141 ((TH1*)hists.First())->Draw( "hist" );
142 cPad->SetLeftMargin( 0.17 );
143 ((TH1*)hists.First())->GetYaxis()->SetTitleOffset( 1.70 );
144
145 for(Int_t i=1; i<hists.GetEntriesFast(); ++i){
146
147 ((TH1*)hists[i])->Draw("histsame");
148 TString ytit = TString("(1/N) ") + ((TH1*)hists[i])->GetYaxis()->GetTitle();
149 ((TH1*)hists[i])->GetYaxis()->SetTitle( ytit ); // histograms are normalised
150
151 }
152
153 // Draw legend
154 if (countPad == 1) {
155 TLegend *legend= new TLegend( cPad->GetLeftMargin(),
156 1-cPad->GetTopMargin()-.15,
157 cPad->GetLeftMargin()+.4,
158 1-cPad->GetTopMargin() );
159 legend->SetFillStyle(1);
160
161 classiter = classnames.begin();
162
163 for(Int_t i=0; i<hists.GetEntriesFast(); ++i, ++classiter){
164 legend->AddEntry(((TH1*)hists[i]),*classiter,"F");
165 }
166
167 legend->SetBorderSize(1);
168 legend->SetMargin( 0.3 );
169 legend->Draw("same");
170 }
171
172 // redraw axes
173 ((TH1*)hists.First())->Draw("sameaxis");
174
175
176 // text for overflows
177 Int_t nbin = ((TH1*)hists.First())->GetNbinsX();
178 Double_t dxu = ((TH1*)hists.First())->GetBinWidth(0);
179 Double_t dxo = ((TH1*)hists.First())->GetBinWidth(nbin+1);
180 TString uoflow = "";
181
182 classiter = classnames.begin();
183 for(Int_t i=0; i<hists.GetEntriesFast(); ++i, ++classiter){
184 if(((TH1*)hists[i])->GetBinContent(0)!=0 || ((TH1*)hists[i])->GetBinContent(nbin+1)!=0){
185 uoflow += *classiter;
186 uoflow += Form( " U/O-flow: %.1f / %.1f %%",
187 ((TH1*)hists[i])->GetBinContent(0)*dxu*100, ((TH1*)hists[i])->GetBinContent(nbin+1)*dxo*100);
188 }
189 }
190
191 TText* t = new TText( 0.98, 0.14, uoflow );
192 t->SetNDC();
193 t->SetTextSize( 0.040 );
194 t->SetTextAngle( 90 );
195 t->AppendPad();
196
197
198 // save canvas to file
199 if (countPad%noPadPerCanv==0) {
200 TString fname = Form( "plots/%s_c%i", outfname.Data(), countCanvas );
201 TMVAGlob::plot_logo();
202 TMVAGlob::imgconv( canv, fname );
203 createNewFig = kFALSE;
204 }
205 else {
206 createNewFig = kTRUE;
207 }
208 }
209
210 if (createNewFig) {
211 TString fname = Form( "plots/%s_c%i", outfname.Data(), countCanvas );
212 TMVAGlob::plot_logo();
213 TMVAGlob::imgconv( canv, fname );
214 createNewFig = kFALSE;
215 }
216
217 return;
218 }