Logo ROOT   6.10/09
Reference Guide
mvaweights.cxx
Go to the documentation of this file.
1 #include "TMVA/mvaweights.h"
2 #include "TTree.h"
3 #include "TBranch.h"
4 #include "TH2.h"
5 
6 
7 // input: - Input file (result from TMVA)
8 // - use of TMVA plotting TStyle
9 void TMVA::mvaweights( TString fin , Bool_t useTMVAStyle )
10 {
11  // set style and remove existing canvas'
12  TMVAGlob::Initialize( useTMVAStyle );
13 
14  // few modifications
15  TStyle *TMVAStyle = gROOT->GetStyle("Plain"); // our style is based on Plain
16  TMVAStyle->SetTitleW(0.94);
17  TMVAStyle->SetTitleH(.06);
18 
19  TString varx = "var3";
20  TString vary = "var4";
21 
22  // switches
23  const Bool_t Save_Images = kTRUE;
24 
25  // checks if file with name "fin" is already open, and if not opens one
26  TFile* file = TMVAGlob::OpenFile( fin );
27  if (!file) {
28  cout << "Cannot open flie: " << fin << endl;
29  return;
30  }
31 
32  // define Canvas layout here!
33  const Int_t width = 500; // size of canvas
34 
35  // this defines how many canvases we need
36  TCanvas *c = 0;
37 
38  // counter variables
39  Int_t countCanvas = 0;
40 
41  // retrieve trees
42  TTree *tree = (TTree*)file->Get( "TestTree" );
43 
44  // search for the right histograms in full list of keys
46  for (Int_t imva=0; imva<branches->GetEntries(); imva++) {
47  TBranch* b = (TBranch*)(*branches)[imva];
48  TString methodS = b->GetName();
49  cout << "Use MVA output of Method " << methodS <<endl;
50 
51  if (!methodS.BeginsWith("MVA_") || methodS.EndsWith("_Proba")) continue;
52  if (methodS.Contains("Cuts") ) continue;
53 
54  methodS.Remove(0,4);
55  cout << "--- Found variable: \"" << methodS << "\"" << endl;
56 
57  // create new canvas
58  TString cname = Form("TMVA output %s",methodS.Data());
59  c = new TCanvas( Form("canvas%d", countCanvas+1), cname,
60  countCanvas*50+200, countCanvas*20, width, width*1.0 );
61  c->Divide( 1, 1 );
62 
63  // set the histogram style
64  Float_t xmin = tree->GetMinimum( varx );
65  Float_t xmax = tree->GetMaximum( varx );
66  Float_t ymin = tree->GetMinimum( vary );
67  Float_t ymax = tree->GetMaximum( vary );
68 
69  Int_t nbin = 100;
70  TH2F* frame = new TH2F( "frame", "frame", nbin, xmin, xmax, nbin, ymin, ymax );
71  TH2F* frameS = new TH2F( "DataS", "DataS", nbin, xmin, xmax, nbin, ymin, ymax );
72  TH2F* frameB = new TH2F( "DataB", "DataB", nbin, xmin, xmax, nbin, ymin, ymax );
73  TH2F* frameRS = new TH2F( "DataRS", "DataRS", nbin, xmin, xmax, nbin, ymin, ymax );
74  TH2F* frameRB = new TH2F( "DataRB", "DataRB", nbin, xmin, xmax, nbin, ymin, ymax );
75 
76  Int_t nbinC = 20;
77  TH2F* refS = new TH2F( "RefS", "RefS", nbinC, xmin, xmax, nbinC, ymin, ymax );
78  TH2F* refB = new TH2F( "RefB", "RefB", nbinC, xmin, xmax, nbinC, ymin, ymax );
79 
80  Float_t mvaMin = tree->GetMinimum( Form( "MVA_%s", methodS.Data() ) );
81  Float_t mvaMax = tree->GetMaximum( Form( "MVA_%s", methodS.Data() ) );
82 
83  // project trees
84  TString expr = Form( "((MVA_%s-(%f))/(%f-(%f)))", methodS.Data(), mvaMin, mvaMax, mvaMin );
85  cout << "Expression = " << expr << endl;
86  tree->Project( "DataS", Form( "%s:%s", vary.Data(), varx.Data() ),
87  Form( "%s*(type==1)", expr.Data() ) );
88  tree->Project( "DataB", Form( "%s:%s", vary.Data(), varx.Data() ),
89  Form( "%s*(type==0)", expr.Data() ) );
90  tree->Project( "DataRS", Form( "%s:%s", vary.Data(), varx.Data() ),
91  "type==1" );
92  tree->Project( "DataRB", Form( "%s:%s", vary.Data(), varx.Data() ),
93  "type==0" );
94  tree->Project( "RefS", Form( "%s:%s", vary.Data(), varx.Data() ),
95  "type==1", "", 500000 );
96  tree->Project( "RefB", Form( "%s:%s", vary.Data(), varx.Data() ),
97  "type==0", "", 500000, 10000 );
98 
99  Float_t zminS = frameS->GetMinimum();
100  Float_t zmaxS = frameS->GetMaximum();
101  Float_t zminB = frameB->GetMinimum();
102  Float_t zmaxB = frameB->GetMaximum();
103  // normalise
104  for (Int_t i=1; i<=nbin; i++) {
105  for (Int_t j=1; j<=nbin; j++) {
106  // signal
107  Float_t z = frameS->GetBinContent( i, j );
108  z = (z - zminS)/(zmaxS - zminS);
109  Float_t zr = frameRS->GetBinContent( i, j );
110  if (zr > 0) z /= zr;
111  else z = 0.;
112  frameS->SetBinContent( i, j, z );
113 
114  // background
115  z = frameB->GetBinContent( i, j );
116  z = (z - zminB)/(zmaxB - zminB);
117  z = 1 - z;
118  zr = frameRB->GetBinContent( i, j );
119  if (zr > 0) z /= zr;
120  else z = 0.;
121  frameB->SetBinContent( i, j, z );
122  }
123  }
124  zminS = frameS->GetMinimum();
125  zmaxS = frameS->GetMaximum();
126  zminB = frameB->GetMinimum();
127  zmaxB = frameB->GetMaximum();
128  // renormalise
129  for (Int_t i=1; i<=nbin; i++) {
130  for (Int_t j=1; j<=nbin; j++) {
131  // signal
132  Float_t z = frameS->GetBinContent( i, j );
133  z = 1*(z - zminS)/(zmaxS - zminS) - 0;
134  frameS->SetBinContent( i, j, z );
135 
136  // background
137  z = frameB->GetBinContent( i, j );
138  z = 1*(z - zminB)/(zmaxB - zminB) - 0;
139  frameB->SetBinContent( i, j, z );
140  }
141  }
142  frame ->SetMinimum( -1.0 );
143  frame ->SetMaximum( +1.0 );
144  frameS->SetMinimum( -1.0 );
145  frameS->SetMaximum( +1.0 );
146  frameB->SetMinimum( -1.0 );
147  frameB->SetMaximum( +1.0 );
148 
149  // axis labels
150  frame->SetTitle( Form( "Signal and background distributions weighted by %s output",
151  methodS.Data() ) );
152  frame->SetTitleSize( 0.08 );
153  frame->GetXaxis()->SetTitle( varx );
154  frame->GetYaxis()->SetTitle( vary );
155 
156  // style
157  frame->SetLabelSize( 0.04, "X" );
158  frame->SetLabelSize( 0.04, "Y" );
159  frame->SetTitleSize( 0.05, "X" );
160  frame->SetTitleSize( 0.05, "Y" );
161  frame->GetYaxis()->SetTitleOffset( 1.05);// label offset on x axis
162  frame->GetYaxis()->SetTitleOffset( 1.30 );// label offset on x axis
163 
164  // now the weighted functions
165  const Int_t nlevels = 3;
166  Double_t levelsS[nlevels];
167  Double_t levelsB[nlevels];
168  levelsS[0] = 0.3;
169  levelsS[1] = 0.5;
170  levelsS[2] = 0.7;
171  levelsB[0] = -0.3;
172  levelsB[1] = 0.2;
173  levelsB[2] = 0.5;
174  frameS->SetContour( nlevels, levelsS );
175  frameB->SetContour( nlevels, levelsB );
176 
177  frameS->SetLineColor( 104 );
178  frameS->SetFillColor( 104 );
179  frameS->SetLineWidth( 3 );
180  frameB->SetLineColor( 102 );
181  frameB->SetFillColor( 102 );
182  frameB->SetLineWidth( 3 );
183 
184  // set style
185  refS->SetMarkerSize( 0.2 );
186  refS->SetMarkerColor( 104 );
187 
188  refB->SetMarkerSize( 0.2 );
189  refB->SetMarkerColor( 102 );
190 
191  const Int_t nlevelsR = 1;
192  Double_t levelsRS[nlevelsR];
193  Double_t levelsRB[nlevelsR];
194  levelsRS[0] = refS->GetMaximum()*0.3;
195  // levelsRS[1] = refS->GetMaximum()*0.3;
196  levelsRB[0] = refB->GetMaximum()*0.3;
197  // levelsRB[1] = refB->GetMaximum()*0.3;
198  refS->SetContour( nlevelsR, levelsRS );
199  refB->SetContour( nlevelsR, levelsRB );
200 
201  refS->SetLineColor( 104 );
202  refS->SetFillColor( 104 );
203  refS->SetLineWidth( 3 );
204  refB->SetLineColor( 102 );
205  refB->SetFillColor( 102 );
206  refB->SetLineWidth( 3 );
207 
208  // and plot
209  c->cd(1);
210 
211  frame->Draw();
212  frameS->Draw( "contsame" );
213  refS->Draw( "cont3same" );
214  refB->Draw( "cont3same" );
215  // frameB->Draw( "colzsame" );
216 
217  // save canvas to file
218  c->Update();
219  if (Save_Images) {
220  TMVAGlob::imgconv( c, Form("plots/mvaweights_%s", methodS.Data()) );
221  }
222  countCanvas++;
223  }
224 }
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title Offset is a correction factor with respect to the "s...
Definition: TAttAxis.cxx:262
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 Double_t GetMaximum(Double_t maxval=FLT_MAX) const
Return maximum value smaller than maxval of bins in the range, unless the value has been overridden b...
Definition: TH1.cxx:7666
An array of TObjects.
Definition: TObjArray.h:37
void imgconv(TCanvas *c, const TString &fname)
Definition: tmvaglob.cxx:212
float xmin
Definition: THbookFile.cxx:93
virtual void SetMaximum(Double_t maximum=-1111)
Definition: TH1.h:375
float Float_t
Definition: RtypesCore.h:53
float ymin
Definition: THbookFile.cxx:93
void SetTitleW(Float_t w=0)
Definition: TStyle.h:388
virtual void SetContour(Int_t nlevels, const Double_t *levels=0)
Set the number and values of contour levels.
Definition: TH1.cxx:7607
TFile * OpenFile(const TString &fin)
Definition: tmvaglob.cxx:192
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:46
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
virtual void SetMinimum(Double_t minimum=-1111)
Definition: TH1.h:376
void mvaweights(TString fin="TMVA.root", Bool_t useTMVAStyle=kTRUE)
#define gROOT
Definition: TROOT.h:375
virtual void SetLabelSize(Float_t size=0.02, Option_t *axis="X")
Set size of axis&#39; labels.
Definition: Haxis.cxx:285
Basic string class.
Definition: TString.h:129
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
virtual Double_t GetMinimum(const char *columname)
Return minimum of column with name columname.
Definition: TTree.cxx:5939
virtual TObjArray * GetListOfBranches()
Definition: TTree.h:405
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
virtual Double_t GetMaximum(const char *columname)
Return maximum of column with name columname.
Definition: TTree.cxx:5900
TStyle objects may be created to define special styles.
Definition: TStyle.h:27
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
float ymax
Definition: THbookFile.cxx:93
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
tomato 2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:249
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition: TString.h:563
char * Form(const char *fmt,...)
TAxis * GetYaxis()
Definition: TH1.h:301
float xmax
Definition: THbookFile.cxx:93
void SetTitleH(Float_t h=0)
Definition: TStyle.h:389
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
Definition: TAttMarker.h:41
virtual Double_t GetMinimum(Double_t minval=-FLT_MAX) const
Return minimum value larger than minval of bins in the range, unless the value has been overridden by...
Definition: TH1.cxx:7751
TString & Remove(Ssiz_t pos)
Definition: TString.h:621
The Canvas class.
Definition: TCanvas.h:31
double Double_t
Definition: RtypesCore.h:55
virtual void SetTitleSize(Float_t size=0.02, Option_t *axis="X")
Set the axis&#39; title size.
Definition: Haxis.cxx:365
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:572
you should not use this method at all Int_t Int_t z
Definition: TRolke.cxx:630
Definition: file.py:1
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition: TH2.h:84
Int_t GetEntries() const
Return the number of objects in array (i.e.
Definition: TObjArray.cxx:494
Definition: tree.py:1
A TTree object has a header with a name and a title.
Definition: TTree.h:78
virtual void SetTitle(const char *title)
Change (i.e.
Definition: TH1.cxx:6028
virtual void SetBinContent(Int_t bin, Double_t content)
Set bin content.
Definition: TH2.cxx:2471
A TTree is a list of TBranches.
Definition: TBranch.h:57
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:155
THist< 2, float, THistStatContent, THistStatUncertainty > TH2F
Definition: THist.hxx:317
const Bool_t kTRUE
Definition: RtypesCore.h:91
virtual Long64_t Project(const char *hname, const char *varexp, const char *selection="", Option_t *option="", Long64_t nentries=kMaxEntries, Long64_t firstentry=0)
Make a projection of a tree using selections.
Definition: TTree.cxx:7083
TAxis * GetXaxis()
Definition: TH1.h:300
const char * Data() const
Definition: TString.h:347