Logo ROOT   6.10/09
Reference Guide
correlationscatters.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
13 void TMVA::correlationscatters(TString dataset, TString fin , TString var,
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 \"correlationscatters\" 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::GetNumberOfInputVariables( vardir ); // subtraction of target(s) no longer necessary
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  TString thename[2] = { "_Signal", "_Background" };
84  if (isRegression) thename[0] = "_Regression";
85  for (UInt_t itype = 0; itype < 2; itype++) {
86 
87  TIter next(gDirectory->GetListOfKeys());
88  TKey * key = 0;
89  TCanvas* canv = 0;
90 
91  Int_t countPad = 0;
92 
93  while ( (key = (TKey*)next()) ) {
94 
95  if (key->GetCycle() != 1) continue;
96 
97  // make sure, that we only look at histograms
98  TClass *cl = gROOT->GetClass(key->GetClassName());
99  if (!cl->InheritsFrom("TH1")) continue;
100  TH1 *scat = (TH1*)key->ReadObj();
101  TString hname = scat->GetName();
102 
103  // check for all signal histograms
104  if (! (hname.EndsWith( thename[itype] + extension ) &&
105  hname.Contains( TString("_") + var + "_" ) && hname.BeginsWith("scat_")) ) {
106  scat->Delete();
107  continue;
108  }
109 
110  // found a new signal plot
111 
112  // create new canvas
113  if (countPad%noPadPerCanv==0) {
114  ++countCanvas;
115  TString ext = extension; ext.Remove( 0, 1 );
116  canv = new TCanvas( Form("canvas%d", countCanvas),
117  Form("Correlation profiles for '%s'-transformed %s variables",
118  ext.Data(), (isRegression ? "" : (itype==0) ? "signal" : "background")),
119  countCanvas*50+200, countCanvas*20, width, height );
120  canv->Divide(xPad,yPad);
121  }
122 
123  if (!canv) continue;
124 
125  canv->cd(countPad++%noPadPerCanv+1);
126 
127  // find the corredponding backgrouns histo
128  TString bgname = hname;
129  bgname.ReplaceAll("scat_","prof_");
130  TH1 *prof = (TH1*)gDirectory->Get(bgname);
131  if (prof == NULL) {
132  cout << "ERROR!!! couldn't find background histo for" << hname << endl;
133  //exit(1);
134  return;
135  }
136  // this is set but not stored during plot creation in MVA_Factory
138 
139  // chop off "signal"
140  TMVAGlob::SetFrameStyle( scat, 1.2 );
141 
142  // normalise both signal and background
143  scat->Scale( 1.0/scat->GetSumOfWeights() );
144 
145  // finally plot and overlay
146  scat->SetMarkerColor( 4);
147  scat->Draw("col");
148  prof->SetMarkerColor( gConfig().fVariablePlotting.fUsePaperStyle ? 1 : 2 );
149  prof->SetMarkerSize( 0.2 );
150  prof->SetLineColor( gConfig().fVariablePlotting.fUsePaperStyle ? 1 : 2 );
151  prof->SetLineWidth( gConfig().fVariablePlotting.fUsePaperStyle ? 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( "%s/plots/correlationscatter_%s_%s_c%i",dataset.Data(),var.Data(), extension.Data(), countCanvas );
164  TMVAGlob::imgconv( canv, fname );
165  }
166  }
167  if (countPad%noPadPerCanv!=0) {
168  canv->Update();
169 
170  TString fname = Form( "%s/plots/correlationscatter_%s_%s_c%i",dataset.Data(),var.Data(), extension.Data(), countCanvas );
172  TMVAGlob::imgconv( canv, fname );
173  }
174  }
175 }
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition: TAttLine.h:43
virtual void Scale(Double_t c1=1, Option_t *option="")
Multiply this histogram by a constant c1.
Definition: TH1.cxx:5937
void imgconv(TCanvas *c, const TString &fname)
Definition: tmvaglob.cxx:212
virtual TList * GetListOfKeys() const
Definition: TDirectory.h:148
Int_t GetNumberOfInputVariables(TDirectory *dir)
Definition: tmvaglob.cxx:413
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:640
TFile * OpenFile(const TString &fin)
Definition: tmvaglob.cxx:192
Config & gConfig()
virtual Double_t GetSumOfWeights() const
Return the sum of weights excluding under/overflows.
Definition: TH1.cxx:7103
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:46
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:679
void SetSignalAndBackgroundStyle(TH1 *sig, TH1 *bkg, TH1 *all=0)
Definition: tmvaglob.cxx:8
#define gROOT
Definition: TROOT.h:375
void SetFrameStyle(TH1 *frame, Float_t scale=1.0)
Definition: tmvaglob.cxx:77
Basic string class.
Definition: TString.h:129
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
virtual void SetFillStyle(Style_t fstyle)
Set the fill area style.
Definition: TAttFill.h:39
#define NULL
Definition: RtypesCore.h:88
Iterator of linked list.
Definition: TList.h:183
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition: TAttMarker.h:38
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Return true if string ends with the specified string.
Definition: TString.cxx:2231
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:24
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
virtual void Delete(Option_t *option="")
Delete this object.
Definition: TObject.cxx:176
void Initialize(Bool_t useTMVAStyle=kTRUE)
Definition: tmvaglob.cxx:176
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2851
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition: TString.h:563
unsigned int UInt_t
Definition: RtypesCore.h:42
char * Form(const char *fmt,...)
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:71
void plot_logo(Float_t v_scale=1.0, Float_t skew=1.0)
Definition: tmvaglob.cxx:263
Bool_t InheritsFrom(const char *cl) const
Return kTRUE if this class inherits from a class with name "classname".
Definition: TClass.cxx:4602
virtual TDirectory * GetDirectory(const char *apath, Bool_t printError=false, const char *funcname="GetDirectory")
Find a directory named "apath".
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
Definition: TAttMarker.h:41
const char * extension
Definition: civetweb.c:5005
TString & Remove(Ssiz_t pos)
Definition: TString.h:621
The Canvas class.
Definition: TCanvas.h:31
Describe directory structure in memory.
Definition: TDirectory.h:34
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:572
The TH1 histogram class.
Definition: TH1.h:56
virtual Bool_t cd(const char *path=0)
Change current directory to "this" directory.
Definition: TDirectory.cxx:435
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1135
Definition: file.py:1
void correlationscatters(TString dataset, TString fin, TString var="var3", TString dirName_="InputVariables_Id", TString title="TMVA Input Variable", Bool_t isRegression=kFALSE, Bool_t useTMVAStyle=kTRUE)
#define gDirectory
Definition: TDirectory.h:211
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2208
const char * Data() const
Definition: TString.h:347