Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
correlationscattersMultiClass.cxx
Go to the documentation of this file.
2#include "TMVA/Config.h"
3
4
5
6// this macro plots the correlations (as scatter plots) of
7// the various input variable combinations used in TMVA (e.g. running
8// TMVAnalysis.C). Signal and Background are plotted separately
9
10// input: - Input file (result from TMVA),
11// - normal/decorrelated/PCA
12// - use of TMVA plotting TStyle
14 TString dirName_ , TString /*title*/,
15 Bool_t /*isRegression */,
16 Bool_t useTMVAStyle )
17{
18 // set style and remove existing canvas'
19 TMVAGlob::Initialize( useTMVAStyle );
20
21 TString extension = dirName_;
22 extension.ReplaceAll( "InputVariables", "" );
23 extension.ReplaceAll( " ", "" );
24 if (extension == "") extension = "_Id"; // use 'Id' for 'idendtity transform'
25
26 var.ReplaceAll( extension, "" );
27 cout << "Called macro \"correlationscattersMultiClass\" for variable: \"" << var
28 << "\", transformation type \"" << dirName_
29 << "\" (extension: \"" << extension << "\")" << endl;
30
31 // checks if file with name "fin" is already open, and if not opens one
32 TFile* file = TMVAGlob::OpenFile( fin );
33
34 TString dirName = dirName_ + "/CorrelationPlots";
35
36 // find out number of input variables
37 TDirectory* vardir = (TDirectory*)file->GetDirectory(dataset.Data())->Get( "InputVariables_Id" );
38 if (!vardir) {
39 cout << "ERROR: no such directory: \"InputVariables\"" << endl;
40 return;
41 }
42 Int_t noVars = TMVAGlob::GetNumberOfInputVariablesMultiClass( vardir );
43
44 TDirectory* dir = (TDirectory*)file->GetDirectory(dataset.Data())->Get( dirName );
45 if (dir==0) {
46 cout << "No information about " << extension << " available in " << fin << endl;
47 return;
48 }
49 dir->cd();
50
51 TListIter keyIt(dir->GetListOfKeys());
52 Int_t noPlots = noVars - 1;
53
54 cout << "noPlots: " << noPlots << " --> noVars: " << noVars << endl;
55 if (noVars != Int_t(noVars)) {
56 cout << "*** Warning: problem in inferred number of variables ... not an integer *** " << endl;
57 }
58
59 // define Canvas layout here!
60 // default setting
61 Int_t xPad; // no of plots in x
62 Int_t yPad; // no of plots in y
63 Int_t width; // size of canvas
64 Int_t height;
65 switch (noPlots) {
66 case 1:
67 xPad = 1; yPad = 1; width = 400; height = width; break;
68 case 2:
69 xPad = 2; yPad = 1; width = 700; height = 0.55*width; break;
70 case 3:
71 xPad = 3; yPad = 1; width = 800; height = 0.4*width; break;
72 case 4:
73 xPad = 2; yPad = 2; width = 600; height = width; break;
74 default:
75 xPad = 3; yPad = 2; width = 800; height = 0.55*width; break;
76 }
77 Int_t noPadPerCanv = xPad * yPad ;
78
79 // counter variables
80 Int_t countCanvas = 0;
81
82 // loop over all objects in "input_variables" directory
83
84 std::vector<TString> classnames = TMVAGlob::GetClassNames(dir);
85 UInt_t ncls = classnames.end()-classnames.begin();
86
87 for (UInt_t itype = 0; itype < ncls; itype++) {
88
89 TIter next(gDirectory->GetListOfKeys());
90 TKey * key = 0;
91 TCanvas* canv = 0;
92
93 Int_t countPad = 0;
94
95 while ( (key = (TKey*)next()) ) {
96
97 if (key->GetCycle() != 1) continue;
98
99 // make sure, that we only look at histograms
100 TClass *cl = gROOT->GetClass(key->GetClassName());
101 if (!cl->InheritsFrom("TH1")) continue;
102 TH1 *scat = (TH1*)key->ReadObj();
103 TString hname = scat->GetName();
104
105 //std::cout << classnames[itype] << " " << extension << "" << hname << " " << var << std::endl;
106 // check for scatter plots
107 if (! (hname.EndsWith( classnames[itype] + extension ) &&
108 hname.Contains( TString("_") + var + "_" ) && hname.BeginsWith("scat_")) ) {
109 scat->Delete();
110 continue;
111 }
112
113 // found a new signal plot
114
115 // create new canvas
116 if (countPad%noPadPerCanv==0) {
117 ++countCanvas;
118 TString ext = extension; ext.Remove( 0, 1 );
119 canv = new TCanvas( Form("canvas%d", countCanvas),
120 Form("Correlation profiles for '%s'-transformed variables (%s)",
121 ext.Data(), classnames[itype].Data()),
122 countCanvas*50+200, countCanvas*20, width, height );
123 canv->Divide(xPad,yPad);
124 }
125
126 if (!canv) continue;
127
128 canv->cd(countPad++%noPadPerCanv+1);
129
130 // find the corredponding profile plot
131 TString bgname = hname;
132 bgname.ReplaceAll("scat_","prof_");
133 TH1 *prof = (TH1*)gDirectory->Get(bgname);
134 if (prof == NULL) {
135 cout << "ERROR!!! couldn't find profile plot for" << hname << endl;
136 //exit(1);
137 return;
138 }
139 // this is set but not stored during plot creation in MVA_Factory
140 TMVAGlob::SetSignalAndBackgroundStyle( scat, prof );
141
142 // chop off "signal"
143 TMVAGlob::SetFrameStyle( scat, 1.2 );
144
145 // normalise both signal and background
146 scat->Scale( 1.0/scat->GetSumOfWeights() );
147
148 // finally plot and overlay
149 // Float_t sc = 1.1;
150 // if (countPad==2) sc = 1.3;
151 scat->SetMarkerColor( 4);
152 scat->Draw("col");
153 prof->SetMarkerColor( gConfig().fVariablePlotting.fUsePaperStyle ? 1 : 2 );
154 prof->SetMarkerSize( 0.2 );
155 prof->SetLineColor( gConfig().fVariablePlotting.fUsePaperStyle ? 1 : 2 );
156 prof->SetLineWidth( gConfig().fVariablePlotting.fUsePaperStyle ? 2 : 1 );
157 prof->SetFillStyle( 3002 );
158 prof->SetFillColor( 46 );
159 prof->Draw("samee1");
160 // redraw axes
161 scat->Draw("sameaxis");
162
163 // save canvas to file
164 if (countPad%noPadPerCanv==0) {
165 canv->Update();
166
167 TString fname = dataset+Form( "/plots/correlationscatter_%s_%s_c%i",var.Data(), extension.Data(), countCanvas );
168 TMVAGlob::plot_logo();
169 TMVAGlob::imgconv( canv, fname );
170 }
171 }
172 if (countPad%noPadPerCanv!=0) {
173 canv->Update();
174
175 TString fname = dataset+Form( "/plots/correlationscatter_%s_%s_c%i",var.Data(), extension.Data(), countCanvas );
176 TMVAGlob::plot_logo();
177 TMVAGlob::imgconv( canv, fname );
178 }
179 }
180}
int Int_t
Definition RtypesCore.h:45
unsigned int UInt_t
Definition RtypesCore.h:46
bool Bool_t
Definition RtypesCore.h:63
#define gDirectory
Definition TDirectory.h:290
include TDocParser_001 C image html pict1_TDocParser_001 png width
#define gROOT
Definition TROOT.h:406
char * Form(const char *fmt,...)
const char * extension
Definition civetweb.c:7793
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:37
virtual void SetFillStyle(Style_t fstyle)
Set the fill area style.
Definition TAttFill.h:39
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition TAttLine.h:43
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:40
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition TAttMarker.h:38
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
Definition TAttMarker.h:41
The Canvas class.
Definition TCanvas.h:23
TVirtualPad * cd(Int_t subpadnumber=0) override
Set current canvas & pad.
Definition TCanvas.cxx:708
void Update() override
Update canvas pad buffers.
Definition TCanvas.cxx:2504
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:80
Bool_t InheritsFrom(const char *cl) const
Return kTRUE if this class inherits from a class with name "classname".
Definition TClass.cxx:4851
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
Definition TClass.cxx:2957
Describe directory structure in memory.
Definition TDirectory.h:45
virtual TList * GetListOfKeys() const
Definition TDirectory.h:177
virtual Bool_t cd(const char *path=nullptr)
Change current directory to "this" directory.
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition TFile.h:54
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:58
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition TH1.cxx:3073
virtual void Scale(Double_t c1=1, Option_t *option="")
Multiply this histogram by a constant c1.
Definition TH1.cxx:6564
virtual Double_t GetSumOfWeights() const
Return the sum of weights excluding under/overflows.
Definition TH1.cxx:7810
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition TKey.h:28
virtual const char * GetClassName() const
Definition TKey.h:76
Short_t GetCycle() const
Return cycle number associated to this key.
Definition TKey.cxx:577
virtual TObject * ReadObj()
To read a TObject* from the file.
Definition TKey.cxx:750
Iterator of linked list.
Definition TList.h:200
virtual const char * GetName() const
Returns name of object.
Definition TNamed.h:47
virtual void Delete(Option_t *option="")
Delete this object.
Definition TObject.cxx:171
void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0) override
Automatic pad generation by division.
Definition TPad.cxx:1177
Basic string class.
Definition TString.h:136
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Return true if string ends with the specified string.
Definition TString.cxx:2197
const char * Data() const
Definition TString.h:369
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition TString.h:692
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition TString.h:615
TString & Remove(Ssiz_t pos)
Definition TString.h:673
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:624
void correlationscattersMultiClass(TString dataset, TString fin="TMVA.root", TString var="var3", TString dirName_="InputVariables_Id", TString title="TMVA Input Variable", Bool_t isRegression=kFALSE, Bool_t useTMVAStyle=kTRUE)
Config & gConfig()
Definition file.py:1