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
19{
20 bool debug=false;
21 if (Nevt <0) Nevt=1000000;
23 // checks if file with name "fin" is already open, and if not opens one
24 TFile* file = TMVAGlob::OpenFile( fin );
27 if (jobDirList.GetSize()==0) {
28 cout << "error could not find jobs" << endl;
29 return;
30 }
31
34
36 //TDirectory* dir0 = (TDirectory*) (file->Get("InputVariables_Id"));
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%
63
64 TIter next(&jobDirList);
65 Float_t mymax=0., mymin=std::numeric_limits<float>::max();
68 Int_t nMethods = 0;
69 // loop over all methods
70 while ( (jobDir = (TDirectory*)next()) ) {
71 TString methodTitle;
73 mvaNames[nMethods]=methodTitle;
74 if (debug) cout << "--- Found directory for method: " << methodTitle << endl;
75 TIter keyIt(jobDir->GetListOfKeys());
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;
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);
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();
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
187 // ============================================================
188
189 c->Update();
190 TString fname = dataset+"/plots/" + hNameRef;
192 } // end loop itrgt
193 return;
194}
195
#define c(i)
Definition RSha256.hxx:101
#define h(i)
Definition RSha256.hxx:106
bool Bool_t
Boolean (0=false, 1=true) (bool)
Definition RtypesCore.h:77
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:71
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
@ kBlack
Definition Rtypes.h:66
@ kBlue
Definition Rtypes.h:67
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
float xmin
float xmax
The Canvas class.
Definition TCanvas.h:23
TDirectory * GetDirectory(const char *apath, Bool_t printError=false, const char *funcname="GetDirectory") override
Find a directory named "apath".
Describe directory structure in memory.
Definition TDirectory.h:45
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:131
A TGraphErrors is a TGraph with error bars.
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:879
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition TKey.h:28
To draw Mathematical Formula.
Definition TLatex.h:18
This class displays a legend box (TPaveText) containing several legend entries.
Definition TLegend.h:23
A doubly linked list.
Definition TList.h:38
Basic string class.
Definition TString.h:138
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
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