Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
9void 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
45 TObjArray* branches = tree->GetListOfBranches();
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 = TString::Format("TMVA output %s",methodS.Data());
59 c = new TCanvas( TString::Format("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( TString::Format( "MVA_%s", methodS.Data() ) );
81 Float_t mvaMax = tree->GetMaximum( TString::Format( "MVA_%s", methodS.Data() ) );
82
83 // project trees
84 TString expr = TString::Format( "((MVA_%s-(%f))/(%f-(%f)))", methodS.Data(), mvaMin, mvaMax, mvaMin );
85 cout << "Expression = " << expr << endl;
86 tree->Project( "DataS", TString::Format( "%s:%s", vary.Data(), varx.Data() ),
87 TString::Format( "%s*(type==1)", expr.Data() ) );
88 tree->Project( "DataB", TString::Format( "%s:%s", vary.Data(), varx.Data() ),
89 TString::Format( "%s*(type==0)", expr.Data() ) );
90 tree->Project( "DataRS", TString::Format( "%s:%s", vary.Data(), varx.Data() ),
91 "type==1" );
92 tree->Project( "DataRB", TString::Format( "%s:%s", vary.Data(), varx.Data() ),
93 "type==0" );
94 tree->Project( "RefS", TString::Format( "%s:%s", vary.Data(), varx.Data() ),
95 "type==1", "", 500000 );
96 tree->Project( "RefB", TString::Format( "%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( TString::Format( "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, TString::Format("plots/mvaweights_%s", methodS.Data()) );
221 }
222 countCanvas++;
223 }
224}
#define b(i)
Definition RSha256.hxx:100
#define c(i)
Definition RSha256.hxx:101
bool Bool_t
Definition RtypesCore.h:63
int Int_t
Definition RtypesCore.h:45
float Float_t
Definition RtypesCore.h:57
double Double_t
Definition RtypesCore.h:59
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char cname
Option_t Option_t width
float xmin
float ymin
float xmax
float ymax
#define gROOT
Definition TROOT.h:406
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition TAttAxis.cxx:298
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:37
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:45
A TTree is a list of TBranches.
Definition TBranch.h:93
The Canvas class.
Definition TCanvas.h:23
TObject * Get(const char *namecycle) override
Return pointer to object identified by namecycle.
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
virtual void SetTitleSize(Float_t size=0.02, Option_t *axis="X")
Set the axis' title size.
Definition Haxis.cxx:365
void SetTitle(const char *title) override
Change/set the title.
Definition TH1.cxx:6686
TAxis * GetXaxis()
Definition TH1.h:324
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:8513
virtual void SetMaximum(Double_t maximum=-1111)
Definition TH1.h:403
TAxis * GetYaxis()
Definition TH1.h:325
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3066
virtual void SetMinimum(Double_t minimum=-1111)
Definition TH1.h:404
virtual void SetContour(Int_t nlevels, const Double_t *levels=nullptr)
Set the number and values of contour levels.
Definition TH1.cxx:8451
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:8603
virtual void SetLabelSize(Float_t size=0.02, Option_t *axis="X")
Set size of axis' labels.
Definition Haxis.cxx:285
2-D histogram with a float per channel (see TH1 documentation)
Definition TH2.h:307
void SetBinContent(Int_t bin, Double_t content) override
Set bin content.
Definition TH2.cxx:2616
Double_t GetBinContent(Int_t binx, Int_t biny) const override
Definition TH2.h:93
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:164
An array of TObjects.
Definition TObjArray.h:31
Int_t GetEntries() const override
Return the number of objects in array (i.e.
Basic string class.
Definition TString.h:139
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Return true if string ends with the specified string.
Definition TString.cxx:2244
const char * Data() const
Definition TString.h:376
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition TString.h:623
TString & Remove(Ssiz_t pos)
Definition TString.h:685
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2378
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:632
TStyle objects may be created to define special styles.
Definition TStyle.h:29
void SetTitleW(Float_t w=0)
Definition TStyle.h:412
void SetTitleH(Float_t h=0)
Definition TStyle.h:413
A TTree represents a columnar dataset.
Definition TTree.h:79
void Initialize(Bool_t useTMVAStyle=kTRUE)
Definition tmvaglob.cxx:176
TFile * OpenFile(const TString &fin)
Definition tmvaglob.cxx:192
void imgconv(TCanvas *c, const TString &fname)
Definition tmvaglob.cxx:212
void mvaweights(TString fin="TMVA.root", Bool_t useTMVAStyle=kTRUE)