Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
regression_averagedevs.cxx
Go to the documentation of this file.
1#include <limits>
2
4
5#include "TLatex.h"
6#include "TGraphErrors.h"
7#include "TFrame.h"
8
9#include <cmath>
10
11/*
12 this macro plots the quadratic deviation of the estimated from the target value, averaged over the first nevt events in test sample (all if Nevt=-1)
13 a) normal average
14 b) truncated average, using best 90%
15 created January 2009, Eckhard von Toerne, University of Bonn, Germany
16*/
17
18void TMVA::regression_averagedevs(TString dataset,TString fin, Int_t Nevt, Bool_t useTMVAStyle )
19{
20 bool debug=false;
21 if (Nevt <0) Nevt=1000000;
22 TMVAGlob::Initialize( useTMVAStyle );
23 // checks if file with name "fin" is already open, and if not opens one
24 TFile* file = TMVAGlob::OpenFile( fin );
25 TList jobDirList;
26 TMVAGlob::GetListOfJobs((TFile*)file->GetDirectory(dataset.Data()),jobDirList);
27 if (jobDirList.GetSize()==0) {
28 cout << "error could not find jobs" << endl;
29 return;
30 }
31
32 Bool_t __PLOT_LOGO__ = kTRUE;
33 Bool_t __SAVE_IMAGE__ = kTRUE;
34
35 TDirectory* dir0 = (TDirectory*) (jobDirList.At(0));
36 //TDirectory* dir0 = (TDirectory*) (file->Get("InputVariables_Id"));
37 Int_t nTargets = TMVAGlob::GetNumberOfTargets( dir0);
38
39 if (debug) cout << "found targets " << nTargets<<endl;
40 TCanvas* c=0;
41 for (Int_t itrgt = 0 ; itrgt < nTargets; itrgt++){
42 if (debug) cout << "loop targets " << itrgt<<endl;
43 TString xtit = "Method";
44 TString ytit = "Average Quadratic Deviation";
45 TString ftit = ytit + " versus " + xtit + TString::Format(" for target %d",itrgt);
46 c = new TCanvas( TString::Format("c%d",itrgt), ftit , 50+20*itrgt, 10*itrgt, 750, 650 );
47
48 // global style settings
49 c->SetGrid();
50 c->SetTickx(1);
51 c->SetTicky(0);
52 c->SetTopMargin(0.28);
53 c->SetBottomMargin(0.1);
54
55 TString hNameRef = TString::Format("regression_average_devs_target%d",itrgt);
56
57 const Int_t maxMethods = 100;
58 // const Int_t maxTargets = 100;
59 Float_t m[4][maxMethods]; // h0 train-all, h1 train-90%, h2 test-all, h3 test-90%
60 Float_t em[4][maxMethods];
61 Float_t x[4][maxMethods];
62 Float_t ex[4][maxMethods];
63
64 TIter next(&jobDirList);
65 Float_t mymax=0., mymin=std::numeric_limits<float>::max();
66 TString mvaNames[maxMethods];
67 TDirectory *jobDir;
68 Int_t nMethods = 0;
69 // loop over all methods
70 while ( (jobDir = (TDirectory*)next()) ) {
71 TString methodTitle;
72 TMVAGlob::GetMethodTitle(methodTitle,jobDir);
73 mvaNames[nMethods]=methodTitle;
74 if (debug) cout << "--- Found directory for method: " << methodTitle << endl;
75 TIter keyIt(jobDir->GetListOfKeys());
76 TKey *histKey;
77 while ( (histKey = (TKey*)keyIt()) ) {
78 if (histKey->ReadObj()->InheritsFrom("TH1F") ){
79 TString s(histKey->ReadObj()->GetName());
80 if( !s.Contains("Quadr_Dev") ) continue;
81 if( !s.Contains(TString::Format("target_%d_",itrgt))) continue;
82 Int_t ihist = 0 ;
83 if( !s.Contains("best90perc") && s.Contains("train")) ihist=0;
84 if( s.Contains("best90perc") && s.Contains("train")) ihist=1;
85 if( !s.Contains("best90perc") && s.Contains("test")) ihist=2;
86 if( s.Contains("best90perc") && s.Contains("test")) ihist=3;
87 if (debug) cout <<"using histogram" << s << ", ihist="<<ihist<<endl;
88 TH1F* h = (TH1F*) (histKey->ReadObj());
89 m[ihist][nMethods] = sqrt(h->GetMean());
90 em[ihist][nMethods] = h->GetRMS()/(sqrt(h->GetEntries())*2.*h->GetMean());
91 x[ihist][nMethods] = nMethods+0.44+0.12*ihist;
92 ex[ihist][nMethods] = 0.001;
93 mymax= m[ihist][nMethods] > mymax ? m[ihist][nMethods] : mymax;
94 mymin= m[ihist][nMethods] < mymin ? m[ihist][nMethods] : mymin;
95 if (debug) cout << "m"<< ihist << "="<<m[ihist][nMethods]<<endl;
96 }
97 }
98 nMethods++;
99 }
100 TH1F* haveragedevs= new TH1F(TString::Format("haveragedevs%d",itrgt),ftit,nMethods,0.,nMethods);
101 for (int i=0;i<nMethods;i++) haveragedevs->GetXaxis()->SetBinLabel(i+1, mvaNames[i]);
102 haveragedevs->SetStats(0);
103 TGraphErrors* graphTrainAv= new TGraphErrors(nMethods,x[0],m[0],ex[0],em[0]);
104 TGraphErrors* graphTruncTrainAv= new TGraphErrors(nMethods,x[1],m[1],ex[1],em[1]);
105 TGraphErrors* graphTestAv= new TGraphErrors(nMethods,x[2],m[2],ex[2],em[2]);
106 TGraphErrors* graphTruncTestAv= new TGraphErrors(nMethods,x[3],m[3],ex[3],em[3]);
107
108 Double_t xmax = 1.2 * mymax;
109 Double_t xmin = 0.8 * mymin - (mymax - mymin)*0.05;
110 Double_t xheader = 0.2;
111 Double_t yheader = xmax*0.92;
112 xmin = xmin > 0.? xmin : 0.;
113 if (mymin > 1.e-20 && log10(mymax/mymin)>1.5){
114 c->SetLogy();
115 cout << "--- result differ significantly using log scale for display of regression results"<< endl;
116 xmax = 1.5 * xmax;
117 xmin = 0.75 * mymin;
118 yheader = xmax*0.78;
119 }
120 Float_t x0L = 0.03, y0H = 0.91;
121 Float_t dxL = 0.457-x0L, dyH = 0.14;
122 // TLegend *legend = new TLegend( x0L, y0H-dyH, x0L+dxL, y0H , "Average Deviation = (#sum_{evts} (f_{MVA} - f_{target})^{2} )^{1/2}");
123 TLegend *legend = new TLegend( x0L, y0H-dyH, x0L+dxL, y0H );
124 legend->SetTextSize( 0.035 );
125 legend->SetTextAlign(12);
126 legend->SetMargin( 0.1 );
127
128 TH1F *hr = c->DrawFrame(-1.,0.,nMethods+1, xmax);
129 cout << endl;
130 cout << "Training: Average Deviation between target " << itrgt <<" and estimate" << endl;
131 cout << TString::Format("%-15s%-15s%-15s", "Method","Average Dev.","trunc. Aver.(90%)") <<endl;
132 for (int i=0;i<nMethods;i++){
133 cout << TString::Format("%-15s:%#10.3g%#10.3g",
134 (const char*)mvaNames[i], m[0][i],m[1][i])<<endl;
135 // cout << mvaNames[i] << " " << m[0][i]<< " "<< m[1][i]<<endl;
136 hr->GetXaxis()->SetBinLabel(i+1," ");
137 }
138 cout << endl;
139 cout << "Testing: Average Deviation between target " << itrgt <<" and estimate" << endl;
140 cout << TString::Format("%-15s%-15s%-15s", "Method","Average Dev.","trunc. Aver.(90%)") <<endl;
141 for (int i=0;i<nMethods;i++){
142 cout << TString::Format("%-15s:%#10.3g%#10.3g",
143 (const char*)mvaNames[i], m[2][i],m[3][i])<<endl;
144 //cout << mvaNames[i] << " " << m[2][i]<< " "<< m[3][i]<<endl;
145 }
146
147 haveragedevs->SetMinimum(xmin);
148 haveragedevs->SetMaximum(xmax);
149 haveragedevs->SetXTitle("Method");
150 haveragedevs->SetYTitle("Deviation from target");
151 haveragedevs->Draw();
152 c->GetFrame()->SetFillColor(21);
153 c->GetFrame()->SetBorderSize(12);
154 graphTrainAv->SetMarkerSize(1.);
155 graphTrainAv->SetMarkerColor(kBlue);
156 graphTrainAv->SetMarkerStyle(25);
157 graphTrainAv->Draw("P");
158
159 graphTruncTrainAv->SetMarkerSize(1.);
160 graphTruncTrainAv->SetMarkerColor(kBlack);
161 graphTruncTrainAv->SetMarkerStyle(25);
162 graphTruncTrainAv->Draw("P");
163
164 graphTestAv->SetMarkerSize(1.);
165 graphTestAv->SetMarkerColor(kBlue);
166 graphTestAv->SetMarkerStyle(21);
167 graphTestAv->Draw("P");
168
169 graphTruncTestAv->SetMarkerSize(1.);
170 graphTruncTestAv->SetMarkerColor(kBlack);
171 graphTruncTestAv->SetMarkerStyle(21);
172 graphTruncTestAv->Draw("P");
173 legend->AddEntry(graphTrainAv,TString("Training Sample, Average Deviation"),"p");
174 legend->AddEntry(graphTruncTrainAv,TString("Training Sample, truncated Average Dev. (best 90%)"),"p");
175 legend->AddEntry(graphTestAv,TString("Test Sample, Average Deviation"),"p");
176 legend->AddEntry(graphTruncTestAv,TString("Test Sample, truncated Average Dev. (best 90%)"),"p");
177
178 legend->Draw();
179 TLatex legHeader;
180 legHeader.SetTextSize(0.035);
181 legHeader.SetTextAlign(12);
182 //legHeader.DrawLatex(x0L, y0H+0.01, "Average Deviation = (#sum (_{ } f_{MVA} - f_{target})^{2} )^{1/2}");
183 legHeader.DrawLatex(xheader, yheader, "Average Deviation = (#sum (_{ } f_{MVA} - f_{target})^{2} )^{1/2}");
184 // ============================================================
185
186 if (__PLOT_LOGO__) TMVAGlob::plot_logo();
187 // ============================================================
188
189 c->Update();
190 TString fname = dataset+"/plots/" + hNameRef;
191 if (__SAVE_IMAGE__) TMVAGlob::imgconv( c, fname );
192 } // end loop itrgt
193 return;
194}
195
#define c(i)
Definition RSha256.hxx:101
#define h(i)
Definition RSha256.hxx:106
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
bool Bool_t
Boolean (0=false, 1=true) (bool).
Definition RtypesCore.h:77
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
float Float_t
Float 4 bytes (float).
Definition RtypesCore.h:71
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
@ kBlack
Definition Rtypes.h:66
@ kBlue
Definition Rtypes.h:67
float xmin
float xmax
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition TAttMarker.h:41
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition TAttMarker.h:43
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
Definition TAttMarker.h:48
virtual void SetTextAlign(Short_t align=11)
Set the text alignment.
Definition TAttText.h:48
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition TAttText.h:53
virtual void SetBinLabel(Int_t bin, const char *label)
Set label for bin.
Definition TAxis.cxx:891
virtual Int_t GetSize() const
Return the capacity of the collection, i.e.
TDirectory * GetDirectory(const char *apath, Bool_t printError=false, const char *funcname="GetDirectory") override
Find a directory using apath.
virtual TList * GetListOfKeys() const
Definition TDirectory.h:224
void Draw(Option_t *chopt="") override
Default Draw method for all objects.
virtual void SetXTitle(const char *title)
Definition TH1.h:667
TAxis * GetXaxis()
Definition TH1.h:571
virtual void SetMaximum(Double_t maximum=-1111)
Definition TH1.h:652
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3097
virtual void SetMinimum(Double_t minimum=-1111)
Definition TH1.h:653
virtual void SetYTitle(const char *title)
Definition TH1.h:668
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition TH1.cxx:9127
virtual TObject * ReadObj()
To read a TObject* from the file.
Definition TKey.cxx:792
TLatex * DrawLatex(Double_t x, Double_t y, const char *text)
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
void Draw(Option_t *option="") override
Draw this box with its current attributes.
void SetMargin(Float_t margin)
Definition TLegend.h:69
TObject * At(Int_t idx) const override
Returns the object at position idx. Returns 0 if idx is out of range.
Definition TList.cxx:487
virtual const char * GetName() const
Returns name of object.
Definition TObject.cxx:462
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition TObject.cxx:549
Basic string class.
Definition TString.h:138
const char * Data() const
Definition TString.h:384
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:2385
RVec< PromoteType< T > > log10(const RVec< T > &v)
Definition RVec.hxx:1838
Double_t x[n]
Definition legend1.C:17
Double_t ex[n]
Definition legend1.C:17
VecExpr< UnaryOp< Sqrt< T >, VecExpr< A, T, D >, T >, T, D > sqrt(const VecExpr< A, T, D > &rhs)
UInt_t GetListOfJobs(TFile *file, TList &jobdirs)
Definition tmvaglob.cxx:615
void Initialize(Bool_t useTMVAStyle=kTRUE)
Definition tmvaglob.cxx:176
void GetMethodTitle(TString &name, TKey *ikey)
Definition tmvaglob.cxx:348
void plot_logo(Float_t v_scale=1.0, Float_t skew=1.0)
Definition tmvaglob.cxx:270
TFile * OpenFile(const TString &fin)
Definition tmvaglob.cxx:192
Int_t GetNumberOfTargets(TDirectory *dir)
Definition tmvaglob.cxx:403
void imgconv(TCanvas *c, const TString &fname)
Definition tmvaglob.cxx:212
void regression_averagedevs(TString dataset, TString fin, Int_t Nevt=-1, Bool_t useTMVAStyle=kTRUE)
TMarker m
Definition textangle.C:8