ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/LJMet/MultivariateAnalysis/macros/variables.C
Revision: 1.1
Committed: Thu Nov 20 22:34:50 2008 UTC (16 years, 5 months ago) by kukartse
Content type: text/plain
Branch: MAIN
CVS Tags: V00-01-12, V00-01-11, V00-01-10, gak031009, gak030509, gak022309, gak021209, gak040209, gak012809, V00-01-09, V00-01-08, V00-01-07, V00-01-06, V00-01-05, V00-01-04, V00-00-07, V00-00-06, V00-00-05, V00-00-04, V00-01-03, V00-00-02, V00-00-01
Log Message:
created /macros with TMVA scripts in it

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     xPad = 3; yPad = 2; width = 800; height = 0.55*width; break;
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     TIter next(dir->GetListOfKeys());
67     TKey * key = 0;
68     TCanvas * canv = 0;
69     while ((key = (TKey*)next())) {
70     if (key->GetCycle() != 1) continue;
71    
72     if(! TString(key->GetName()).Contains("__S")) continue;
73    
74     // make sure, that we only look at histograms
75     TClass *cl = gROOT->GetClass(key->GetClassName());
76     if (!cl->InheritsFrom("TH1")) continue;
77     TH1 *sig = (TH1*)key->ReadObj();
78     TString hname(sig->GetName());
79    
80     // create new canvas
81     if (countPad%noPadPerCanv==0) {
82     ++countCanvas;
83     canv = new TCanvas( Form("canvas%d", countCanvas), titles[type],
84     countCanvas*50+50, countCanvas*20, width, height );
85     canv->Divide(xPad,yPad);
86     canv->Draw();
87     }
88    
89     TPad* cPad = (TPad*)canv->cd(countPad++%noPadPerCanv+1);
90    
91     // find the corredponding backgrouns histo
92     TString bgname = hname;
93     bgname.ReplaceAll("__S","__B");
94     TH1 *bgd = (TH1*)dir->Get(bgname);
95     if (bgd == NULL) {
96     cout << "ERROR!!! couldn't find backgroung histo for" << hname << endl;
97     exit;
98     }
99    
100     // this is set but not stored during plot creation in MVA_Factory
101     TMVAGlob::SetSignalAndBackgroundStyle( sig, bgd );
102    
103     // chop off "signal"
104     TString title(sig->GetTitle());
105     title.ReplaceAll("signal","");
106     sig->SetTitle( TString( titles[type] ) + ": " + title );
107     TMVAGlob::SetFrameStyle( sig, 1.2 );
108    
109     // normalise both signal and background
110     TMVAGlob::NormalizeHists( sig, bgd );
111    
112     // finally plot and overlay
113     Float_t sc = 1.1;
114     if (countPad==2) sc = 1.3;
115     sig->SetMaximum( TMath::Max( sig->GetMaximum(), bgd->GetMaximum() )*sc );
116     // DEBUG signal color
117     sig->SetLineColor(4);
118     sig->Draw( "hist" );
119     cPad->SetLeftMargin( 0.17 );
120    
121     bgd->Draw("histsame");
122     sig->GetXaxis()->SetTitle( title );
123     sig->GetYaxis()->SetTitleOffset( 1.80 );
124     sig->GetYaxis()->SetTitle("Normalised");
125    
126     // Draw legend
127     if (countPad==2){
128     TLegend *legend= new TLegend( cPad->GetLeftMargin(),
129     1-cPad->GetTopMargin()-.15,
130     cPad->GetLeftMargin()+.4,
131     1-cPad->GetTopMargin() );
132     legend->SetFillStyle(1);
133     legend->AddEntry(sig,"Signal","F");
134     legend->AddEntry(bgd,"Background","F");
135     legend->Draw("same");
136     legend->SetBorderSize(1);
137     legend->SetMargin( 0.3 );
138     }
139    
140     // redraw axes
141     sig->Draw("sameaxis");
142    
143     // text for overflows
144     Int_t nbin = sig->GetNbinsX();
145     TString uoflow = Form( "U/O-flow (S,B): (%.1f, %.1f)% / (%.1f, %.1f)%",
146     sig->GetBinContent(0)*100, bgd->GetBinContent(0)*100,
147     sig->GetBinContent(nbin+1)*100, bgd->GetBinContent(nbin+1)*100 );
148     TText* t = new TText( 0.98, 0.14, uoflow );
149     t->SetNDC();
150     t->SetTextSize( 0.040 );
151     t->SetTextAngle( 90 );
152     t->AppendPad();
153    
154     // save canvas to file
155     if (countPad%noPadPerCanv==0) {
156     TString fname = Form( "plots/%s_c%i", outfname[type].Data(), countCanvas );
157     TMVAGlob::plot_logo();
158     TMVAGlob::imgconv( canv, fname );
159     }
160    
161     }
162    
163     return;
164     }