ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/LJMet/MultivariateAnalysis/macros/variables.C
Revision: 1.5
Committed: Sat Jul 4 17:59:56 2009 UTC (15 years, 10 months ago) by kukartse
Content type: text/plain
Branch: MAIN
CVS Tags: V00-03-01, ZMorph_BASE_20100408, gak040610_morphing, V00-02-02, gak011410, gak010310, ejterm2010_25nov2009, V00-02-01, V00-02-00, gak112409, CMSSW_22X_branch_base, segala101609, HEAD
Branch point for: ZMorph-V00-03-01, CMSSW_22X_branch
Changes since 1.4: +1 -1 lines
Log Message:
*** empty log message ***

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 variables( TString fin = "TMVA.root", TMVAGlob::TypeOfPlot type = TMVAGlob::kNormal, bool useTMVAStyle=kFALSE )
10 {
11
12 const TString directories[3] = { "InputVariables_NoTransform",
13 "InputVariables_DecorrTransform",
14 "InputVariables_PCATransform" };
15
16 const TString titles[3] = { "TMVA Input Variable",
17 "Decorrelated TMVA Input Variables",
18 "Principal Component Transformed TMVA Input Variables" };
19
20 const TString outfname[3] = { "variables",
21 "variables_decorr",
22 "variables_pca" };
23
24
25 // set style and remove existing canvas'
26 TMVAGlob::Initialize( useTMVAStyle );
27
28 // checks if file with name "fin" is already open, and if not opens one
29 TFile* file = TMVAGlob::OpenFile( fin );
30
31 TDirectory* dir = (TDirectory*)file->Get( directories[type] );
32 if (dir==0) {
33 cout << "No information about " << titles[type] << " available in " << fin << endl;
34 return;
35 }
36 dir->cd();
37
38 // how many plots are in the directory?
39 Int_t noPlots = ((dir->GetListOfKeys())->GetEntries()) / 2;
40
41 // define Canvas layout here!
42 // default setting
43 Int_t xPad; // no of plots in x
44 Int_t yPad; // no of plots in y
45 Int_t width; // size of canvas
46 Int_t height;
47 switch (noPlots) {
48 case 1:
49 xPad = 1; yPad = 1; width = 550; height = 0.50*width; break;
50 case 2:
51 xPad = 2; yPad = 1; width = 600; height = 0.50*width; break;
52 case 3:
53 xPad = 3; yPad = 1; width = 900; height = 0.4*width; break;
54 case 4:
55 xPad = 2; yPad = 2; width = 600; height = width; break;
56 default:
57 //xPad = 3; yPad = 2; width = 800; height = 0.55*width; break;
58 xPad = 1; yPad = 1; width = 800; height = 0.75*width; break;
59 }
60 Int_t noPadPerCanv = xPad * yPad ;
61
62 // counter variables
63 Int_t countCanvas = 0;
64 Int_t countPad = 0;
65
66 // loop over all objects in directory
67 TIter next(dir->GetListOfKeys());
68 TKey * key = 0;
69 TCanvas * canv = 0;
70 while ((key = (TKey*)next())) {
71 if (key->GetCycle() != 1) continue;
72
73 if(! TString(key->GetName()).Contains("__S")) 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), titles[type],
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("__S","__B");
95 TH1 *bgd = (TH1*)dir->Get(bgname);
96 if (bgd == NULL) {
97 cout << "ERROR!!! couldn't find backgroung histo for" << hname << endl;
98 exit;
99 }
100
101 // this is set but not stored during plot creation in MVA_Factory
102 TMVAGlob::SetSignalAndBackgroundStyle( sig, bgd );
103
104 // chop off "signal"
105 TString title(sig->GetTitle());
106 title.ReplaceAll("signal","");
107 //sig->SetTitle( TString( titles[type] ) + ": " + title );
108 sig->SetTitle( "" );
109 TMVAGlob::SetFrameStyle( sig, 1.2 );
110
111 // normalise both signal and background
112 TMVAGlob::NormalizeHists( sig, bgd );
113
114 // finally plot and overlay
115 Float_t sc = 1.1;
116 if (countPad==2) sc = 1.3;
117 sig->SetMaximum( TMath::Max( sig->GetMaximum(), bgd->GetMaximum() )*sc );
118 // DEBUG signal color
119 sig->SetLineColor(4);
120 sig->Draw( "hist" );
121 cPad->SetLeftMargin( 0.17 );
122
123 bgd->Draw("histsame");
124 //sig->GetXaxis()->SetTitle( title );
125 sig->GetXaxis()->SetTitle( "" );
126 sig->GetYaxis()->SetTitleOffset( 1.80 );
127 //sig->GetYaxis()->SetTitle("Normalised");
128 sig->GetYaxis()->SetTitle("");
129
130 // Draw legend
131 if (countPad==2){
132 TLegend *legend= new TLegend( cPad->GetLeftMargin(),
133 1-cPad->GetTopMargin()-.15,
134 cPad->GetLeftMargin()+.4,
135 1-cPad->GetTopMargin() );
136 legend->SetFillStyle(1);
137 legend->AddEntry(sig,"Signal","F");
138 legend->AddEntry(bgd,"Background","F");
139 legend->Draw("same");
140 legend->SetBorderSize(1);
141 legend->SetMargin( 0.3 );
142 }
143
144 // redraw axes
145 sig->Draw("sameaxis");
146
147 // text for overflows
148 Int_t nbin = sig->GetNbinsX();
149 TString uoflow = Form( "U/O-flow (S,B): (%.1f, %.1f)% / (%.1f, %.1f)%",
150 sig->GetBinContent(0)*100, bgd->GetBinContent(0)*100,
151 sig->GetBinContent(nbin+1)*100, bgd->GetBinContent(nbin+1)*100 );
152 TText* t = new TText( 0.98, 0.14, uoflow );
153 t->SetNDC();
154 t->SetTextSize( 0.040 );
155 t->SetTextAngle( 90 );
156 //t->AppendPad();
157
158 // save canvas to file
159 if (countPad%noPadPerCanv==0) {
160 //TString fname = Form( "plots/%s_c%i", outfname[type].Data(), countCanvas );
161 TString fname = Form( "plots/%s_%s", outfname[type].Data(), title.Data() );
162 //TMVAGlob::plot_logo();
163 TMVAGlob::imgconv( canv, fname );
164 //fname.Append(".eps");
165 //cPad->SaveAs(fname );
166 }
167
168 }
169
170
171 return;
172 }