1 |
benhoob |
1.1 |
#include "tmvaglob.C"
|
2 |
|
|
|
3 |
|
|
// this macro plots the correlations (as scatter plots) of
|
4 |
|
|
// the various input variable combinations used in TMVA (e.g. running
|
5 |
|
|
// TMVAnalysis.C). Signal and Background are plotted separately
|
6 |
|
|
|
7 |
|
|
// input: - Input file (result from TMVA),
|
8 |
|
|
// - normal/decorrelated/PCA
|
9 |
|
|
// - use of TMVA plotting TStyle
|
10 |
|
|
void correlationscatters( TString fin = "TMVA.root", TString var= "var3",
|
11 |
|
|
TString dirName_ = "InputVariables_Id", TString title = "TMVA Input Variable",
|
12 |
|
|
Bool_t isRegression = kFALSE,
|
13 |
|
|
Bool_t useTMVAStyle = kTRUE )
|
14 |
|
|
{
|
15 |
|
|
// set style and remove existing canvas'
|
16 |
|
|
TMVAGlob::Initialize( useTMVAStyle );
|
17 |
|
|
|
18 |
|
|
TString extension = dirName_;
|
19 |
|
|
extension.ReplaceAll( "InputVariables", "" );
|
20 |
|
|
extension.ReplaceAll( " ", "" );
|
21 |
|
|
if (extension == "") extension = "_Id"; // use 'Id' for 'idendtity transform'
|
22 |
|
|
|
23 |
|
|
var.ReplaceAll( extension, "" );
|
24 |
|
|
cout << "Called macro \"correlationscatters\" for variable: \"" << var
|
25 |
|
|
<< "\", transformation type \"" << dirName_
|
26 |
|
|
<< "\" (extension: \"" << extension << "\")" << endl;
|
27 |
|
|
|
28 |
|
|
// checks if file with name "fin" is already open, and if not opens one
|
29 |
|
|
TFile* file = TMVAGlob::OpenFile( fin );
|
30 |
|
|
|
31 |
|
|
TString dirName = dirName_ + "/CorrelationPlots";
|
32 |
|
|
|
33 |
|
|
// find out number of input variables
|
34 |
|
|
TDirectory* vardir = (TDirectory*)gDirectory->Get( "InputVariables_Id" );
|
35 |
|
|
if (!vardir) {
|
36 |
|
|
cout << "ERROR: no such directory: \"InputVariables\"" << endl;
|
37 |
|
|
return;
|
38 |
|
|
}
|
39 |
|
|
Int_t noVars = TMVAGlob::GetNumberOfInputVariables( vardir ); // subtraction of target(s) no longer necessary
|
40 |
|
|
|
41 |
|
|
TDirectory* dir = (TDirectory*)gDirectory->Get( dirName );
|
42 |
|
|
if (dir==0) {
|
43 |
|
|
cout << "No information about " << extension << " available in " << fin << endl;
|
44 |
|
|
return;
|
45 |
|
|
}
|
46 |
|
|
dir->cd();
|
47 |
|
|
|
48 |
|
|
TListIter keyIt(dir->GetListOfKeys());
|
49 |
|
|
TKey* key = 0;
|
50 |
|
|
Int_t noPlots = noVars - 1;
|
51 |
|
|
|
52 |
|
|
cout << "noPlots: " << noPlots << " --> noVars: " << noVars << endl;
|
53 |
|
|
if (noVars != Int_t(noVars)) {
|
54 |
|
|
cout << "*** Warning: problem in inferred number of variables ... not an integer *** " << endl;
|
55 |
|
|
}
|
56 |
|
|
|
57 |
|
|
// define Canvas layout here!
|
58 |
|
|
// default setting
|
59 |
|
|
Int_t xPad; // no of plots in x
|
60 |
|
|
Int_t yPad; // no of plots in y
|
61 |
|
|
Int_t width; // size of canvas
|
62 |
|
|
Int_t height;
|
63 |
|
|
switch (noPlots) {
|
64 |
|
|
case 1:
|
65 |
|
|
xPad = 1; yPad = 1; width = 400; height = width; break;
|
66 |
|
|
case 2:
|
67 |
|
|
xPad = 2; yPad = 1; width = 700; height = 0.55*width; break;
|
68 |
|
|
case 3:
|
69 |
|
|
xPad = 3; yPad = 1; width = 800; height = 0.4*width; break;
|
70 |
|
|
case 4:
|
71 |
|
|
xPad = 2; yPad = 2; width = 600; height = width; break;
|
72 |
|
|
default:
|
73 |
|
|
xPad = 3; yPad = 2; width = 800; height = 0.55*width; break;
|
74 |
|
|
}
|
75 |
|
|
Int_t noPadPerCanv = xPad * yPad ;
|
76 |
|
|
|
77 |
|
|
// counter variables
|
78 |
|
|
Int_t countCanvas = 0;
|
79 |
|
|
|
80 |
|
|
// loop over all objects in "input_variables" directory
|
81 |
|
|
Int_t ncls = (isRegression ? 1 : 2);
|
82 |
|
|
TString thename[2] = { "_Signal", "_Background" };
|
83 |
|
|
if (isRegression) thename[0] = "_Regression";
|
84 |
|
|
for (UInt_t itype = 0; itype < 2; itype++) {
|
85 |
|
|
|
86 |
|
|
TIter next(gDirectory->GetListOfKeys());
|
87 |
|
|
TKey * key = 0;
|
88 |
|
|
TCanvas* canv = 0;
|
89 |
|
|
|
90 |
|
|
Int_t countPad = 0;
|
91 |
|
|
|
92 |
|
|
while ((key = (TKey*)next())) {
|
93 |
|
|
|
94 |
|
|
if (key->GetCycle() != 1) continue;
|
95 |
|
|
|
96 |
|
|
// make sure, that we only look at histograms
|
97 |
|
|
TClass *cl = gROOT->GetClass(key->GetClassName());
|
98 |
|
|
if (!cl->InheritsFrom("TH1")) continue;
|
99 |
|
|
TH1 *scat = (TH1*)key->ReadObj();
|
100 |
|
|
TString hname = scat->GetName();
|
101 |
|
|
|
102 |
|
|
// check for all signal histograms
|
103 |
|
|
if (! (hname.EndsWith( thename[itype] + extension ) &&
|
104 |
|
|
hname.Contains( TString("_") + var + "_" ) && hname.BeginsWith("scat_")) ) {
|
105 |
|
|
scat->Delete();
|
106 |
|
|
continue;
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
// found a new signal plot
|
110 |
|
|
|
111 |
|
|
// create new canvas
|
112 |
|
|
if (countPad%noPadPerCanv==0) {
|
113 |
|
|
++countCanvas;
|
114 |
|
|
TString ext = extension; ext.Remove( 0, 1 );
|
115 |
|
|
canv = new TCanvas( Form("canvas%d", countCanvas),
|
116 |
|
|
Form("Correlation profiles for '%s'-transformed %s variables",
|
117 |
|
|
ext.Data(), (isRegression ? "" : (itype==0) ? "signal" : "background")),
|
118 |
|
|
countCanvas*50+200, countCanvas*20, width, height );
|
119 |
|
|
canv->Divide(xPad,yPad);
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
if (!canv) continue;
|
123 |
|
|
|
124 |
|
|
canv->cd(countPad++%noPadPerCanv+1);
|
125 |
|
|
|
126 |
|
|
// find the corredponding backgrouns histo
|
127 |
|
|
TString bgname = hname;
|
128 |
|
|
bgname.ReplaceAll("scat_","prof_");
|
129 |
|
|
TH1 *prof = (TH1*)gDirectory->Get(bgname);
|
130 |
|
|
if (prof == NULL) {
|
131 |
|
|
cout << "ERROR!!! couldn't find background histo for" << hname << endl;
|
132 |
|
|
exit;
|
133 |
|
|
}
|
134 |
|
|
// this is set but not stored during plot creation in MVA_Factory
|
135 |
|
|
TMVAGlob::SetSignalAndBackgroundStyle( scat, prof );
|
136 |
|
|
|
137 |
|
|
// chop off "signal"
|
138 |
|
|
TMVAGlob::SetFrameStyle( scat, 1.2 );
|
139 |
|
|
|
140 |
|
|
// normalise both signal and background
|
141 |
|
|
scat->Scale( 1.0/scat->GetSumOfWeights() );
|
142 |
|
|
|
143 |
|
|
// finally plot and overlay
|
144 |
|
|
Float_t sc = 1.1;
|
145 |
|
|
if (countPad==2) sc = 1.3;
|
146 |
|
|
scat->SetMarkerColor( 4);
|
147 |
|
|
scat->Draw("col");
|
148 |
|
|
prof->SetMarkerColor( TMVAGlob::UsePaperStyle ? 1 : 2 );
|
149 |
|
|
prof->SetMarkerSize( 0.2 );
|
150 |
|
|
prof->SetLineColor( TMVAGlob::UsePaperStyle ? 1 : 2 );
|
151 |
|
|
prof->SetLineWidth( TMVAGlob::UsePaperStyle ? 2 : 1 );
|
152 |
|
|
prof->SetFillStyle( 3002 );
|
153 |
|
|
prof->SetFillColor( 46 );
|
154 |
|
|
prof->Draw("samee1");
|
155 |
|
|
// redraw axes
|
156 |
|
|
scat->Draw("sameaxis");
|
157 |
|
|
|
158 |
|
|
// save canvas to file
|
159 |
|
|
if (countPad%noPadPerCanv==0) {
|
160 |
|
|
canv->Update();
|
161 |
|
|
|
162 |
|
|
TString fname = Form( "plots/correlationscatter_%s_%s_c%i",var.Data(), extension.Data(), countCanvas );
|
163 |
|
|
TMVAGlob::plot_logo();
|
164 |
|
|
TMVAGlob::imgconv( canv, fname );
|
165 |
|
|
}
|
166 |
|
|
}
|
167 |
|
|
if (countPad%noPadPerCanv!=0) {
|
168 |
|
|
canv->Update();
|
169 |
|
|
|
170 |
|
|
TString fname = Form( "plots/correlationscatter_%s_%s_c%i",var.Data(), extension.Data(), countCanvas );
|
171 |
|
|
TMVAGlob::plot_logo();
|
172 |
|
|
TMVAGlob::imgconv( canv, fname );
|
173 |
|
|
}
|
174 |
|
|
}
|
175 |
|
|
}
|