ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/LJMet/MultivariateAnalysis/macros/variables.C
Revision: 1.2
Committed: Thu May 7 00:33:26 2009 UTC (15 years, 11 months ago) by kukartse
Content type: text/plain
Branch: MAIN
Changes since 1.1: +6 -2 lines
Log Message:
tmvaglob and variables changed for paper style

File Contents

# User Rev Content
1 kukartse 1.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=kTRUE )
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 kukartse 1.2 //xPad = 3; yPad = 2; width = 800; height = 0.55*width; break;
58     xPad = 1; yPad = 1; width = 800; height = 0.75*width; break;
59 kukartse 1.1 }
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     TMVAGlob::SetFrameStyle( sig, 1.2 );
109    
110     // normalise both signal and background
111     TMVAGlob::NormalizeHists( sig, bgd );
112    
113     // finally plot and overlay
114     Float_t sc = 1.1;
115     if (countPad==2) sc = 1.3;
116     sig->SetMaximum( TMath::Max( sig->GetMaximum(), bgd->GetMaximum() )*sc );
117     // DEBUG signal color
118     sig->SetLineColor(4);
119     sig->Draw( "hist" );
120     cPad->SetLeftMargin( 0.17 );
121    
122     bgd->Draw("histsame");
123     sig->GetXaxis()->SetTitle( title );
124     sig->GetYaxis()->SetTitleOffset( 1.80 );
125     sig->GetYaxis()->SetTitle("Normalised");
126    
127     // Draw legend
128     if (countPad==2){
129     TLegend *legend= new TLegend( cPad->GetLeftMargin(),
130     1-cPad->GetTopMargin()-.15,
131     cPad->GetLeftMargin()+.4,
132     1-cPad->GetTopMargin() );
133     legend->SetFillStyle(1);
134     legend->AddEntry(sig,"Signal","F");
135     legend->AddEntry(bgd,"Background","F");
136     legend->Draw("same");
137     legend->SetBorderSize(1);
138     legend->SetMargin( 0.3 );
139     }
140    
141     // redraw axes
142     sig->Draw("sameaxis");
143    
144     // text for overflows
145     Int_t nbin = sig->GetNbinsX();
146     TString uoflow = Form( "U/O-flow (S,B): (%.1f, %.1f)% / (%.1f, %.1f)%",
147     sig->GetBinContent(0)*100, bgd->GetBinContent(0)*100,
148     sig->GetBinContent(nbin+1)*100, bgd->GetBinContent(nbin+1)*100 );
149     TText* t = new TText( 0.98, 0.14, uoflow );
150     t->SetNDC();
151     t->SetTextSize( 0.040 );
152     t->SetTextAngle( 90 );
153     t->AppendPad();
154    
155     // save canvas to file
156     if (countPad%noPadPerCanv==0) {
157     TString fname = Form( "plots/%s_c%i", outfname[type].Data(), countCanvas );
158     TMVAGlob::plot_logo();
159     TMVAGlob::imgconv( canv, fname );
160 kukartse 1.2 //fname.Append(".eps");
161     //cPad->SaveAs(fname );
162 kukartse 1.1 }
163 kukartse 1.2
164 kukartse 1.1 }
165 kukartse 1.2
166 kukartse 1.1
167     return;
168     }