Logo ROOT  
Reference Guide
ResultsRegression.cxx
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Andreas Hoecker, Peter Speckmayer, Joerg Stelzer, Helge Voss
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : ResultsRegression *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * Implementation (see header for description) *
12  * *
13  * Authors (alphabetical): *
14  * Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland *
15  * Peter Speckmayer <Peter.Speckmayer@cern.ch> - CERN, Switzerland *
16  * Joerg Stelzer <Joerg.Stelzer@cern.ch> - CERN, Switzerland *
17  * Helge Voss <Helge.Voss@cern.ch> - MPI-K Heidelberg, Germany *
18  * *
19  * Copyright (c) 2006: *
20  * CERN, Switzerland *
21  * MPI-K Heidelberg, Germany *
22  * *
23  * Redistribution and use in source and binary forms, with or without *
24  * modification, are permitted according to the terms listed in LICENSE *
25  * (http://tmva.sourceforge.net/LICENSE) *
26  **********************************************************************************/
27 
28 /*! \class TMVA::ResultsRegression
29 \ingroup TMVA
30 Class that is the base-class for a vector of result
31 */
32 #include "TMVA/ResultsRegression.h"
33 
34 #include "TMVA/DataSet.h"
35 #include "TMVA/DataSetInfo.h"
36 #include "TMVA/MsgLogger.h"
37 #include "TMVA/Results.h"
38 #include "TMVA/Types.h"
39 #include "TMVA/VariableInfo.h"
40 
41 #include "TH1F.h"
42 #include "TH2F.h"
43 #include "TString.h"
44 
45 #include <vector>
46 
47 
48 ////////////////////////////////////////////////////////////////////////////////
49 /// constructor
50 
52  : Results( dsi, resultsName ),
53  fLogger( new MsgLogger(Form("ResultsRegression%s",resultsName.Data()) , kINFO) )
54 {
55 }
56 
57 ////////////////////////////////////////////////////////////////////////////////
58 /// destructor
59 
61 {
62  delete fLogger;
63 }
64 
65 ////////////////////////////////////////////////////////////////////////////////
66 
67 void TMVA::ResultsRegression::SetValue( std::vector<Float_t>& value, Int_t ievt )
68 {
69  if (ievt >= (Int_t)fRegValues.size()) fRegValues.resize( ievt+1 );
70  fRegValues[ievt] = value;
71 }
72 
73 ////////////////////////////////////////////////////////////////////////////////
74 
76 {
77  DataSet* ds = GetDataSet();
78  ds->SetCurrentType( GetTreeType() );
79  const DataSetInfo* dsi = GetDataSetInfo();
80  TString name( Form("tgt_%d",tgtNum) );
81  VariableInfo vinf = dsi->GetTargetInfo(tgtNum);
82  Float_t xmin=0., xmax=0.;
83  if (truncate){
84  xmax = truncvalue;
85  }
86  else{
87  for (Int_t ievt=0; ievt<ds->GetNEvents(); ievt++) {
88  const Event* ev = ds->GetEvent(ievt);
89  std::vector<Float_t> regVal = fRegValues.at(ievt);
90  Float_t val = regVal.at( tgtNum ) - ev->GetTarget( tgtNum );
91  val *= val;
92  xmax = val> xmax? val: xmax;
93  }
94  }
95  xmax *= 1.1;
96  Int_t nbins = 500;
97  TH1F* h = new TH1F( name, name, nbins, xmin, xmax);
98  h->SetDirectory(0);
99  h->GetXaxis()->SetTitle("Quadratic Deviation");
100  h->GetYaxis()->SetTitle("Weighted Entries");
101 
102  for (Int_t ievt=0; ievt<ds->GetNEvents(); ievt++) {
103  const Event* ev = ds->GetEvent(ievt);
104  std::vector<Float_t> regVal = fRegValues.at(ievt);
105  Float_t val = regVal.at( tgtNum ) - ev->GetTarget( tgtNum );
106  val *= val;
107  Float_t weight = ev->GetWeight();
108  if (!truncate || val<=truncvalue ) h->Fill( val, weight);
109  }
110  return h;
111 }
112 
113 ////////////////////////////////////////////////////////////////////////////////
114 
116 {
117  DataSet* ds = GetDataSet();
118  ds->SetCurrentType( GetTreeType() );
119 
120  TString name( Form("tgt_%d_var_%d",tgtNum, varNum) );
121  const DataSetInfo* dsi = GetDataSetInfo();
122  Float_t xmin, xmax;
123  Bool_t takeTargets = kFALSE;
124  if (varNum >= dsi->GetNVariables()) {
125  takeTargets = kTRUE;
126  varNum -= dsi->GetNVariables();
127  }
128  if (!takeTargets) {
129  VariableInfo vinf = dsi->GetVariableInfo(varNum);
130  xmin = vinf.GetMin();
131  xmax = vinf.GetMax();
132 
133  for (Int_t ievt=0; ievt<ds->GetNEvents(); ievt++) {
134  const Event* ev = ds->GetEvent(ievt);
135  Float_t val = ev->GetValue(varNum);
136 
137  if (val < xmin ) xmin = val;
138  if (val > xmax ) xmax = val;
139  }
140 
141  }
142  else {
143  VariableInfo vinf = dsi->GetTargetInfo(varNum);
144  xmin = vinf.GetMin();
145  xmax = vinf.GetMax();
146 
147  for (Int_t ievt=0; ievt<ds->GetNEvents(); ievt++) {
148  const Event* ev = ds->GetEvent(ievt);
149  Float_t val = ev->GetTarget(varNum);
150 
151  if (val < xmin ) xmin = val;
152  if (val > xmax ) xmax = val;
153  }
154  }
155 
156  Float_t ymin = FLT_MAX;
157  Float_t ymax = -FLT_MAX;
158 
159  for (Int_t ievt=0; ievt<ds->GetNEvents(); ievt++) {
160  const Event* ev = ds->GetEvent(ievt);
161  std::vector<Float_t> regVal = fRegValues.at(ievt);
162 
163  Float_t diff = regVal.at( tgtNum ) - ev->GetTarget( tgtNum );
164  if (diff < ymin ) ymin = diff;
165  if (diff > ymax ) ymax = diff;
166  }
167 
168  Int_t nxbins = 50;
169  Int_t nybins = 50;
170 
171  Float_t epsilon = TMath::Abs(xmax-xmin)/((Float_t)nxbins-1);
172  xmin -= 1.01*epsilon;
173  xmax += 1.01*epsilon;
174 
175  epsilon = (ymax-ymin)/((Float_t)nybins-1);
176  ymin -= 1.01*epsilon;
177  ymax += 1.01*epsilon;
178 
179 
180  TH2F* h = new TH2F( name, name, nxbins, xmin, xmax, nybins, ymin, ymax );
181  h->SetDirectory(0);
182 
183  h->GetXaxis()->SetTitle( (takeTargets ? dsi->GetTargetInfo(varNum).GetTitle() : dsi->GetVariableInfo(varNum).GetTitle() ) );
184  TString varName( dsi->GetTargetInfo(tgtNum).GetTitle() );
185  TString yName( varName+TString("_{regression} - ") + varName+TString("_{true}") );
186  h->GetYaxis()->SetTitle( yName );
187 
188  for (Int_t ievt=0; ievt<ds->GetNEvents(); ievt++) {
189  const Event* ev = ds->GetEvent(ievt);
190  std::vector<Float_t> regVal = fRegValues.at(ievt);
191 
192  Float_t xVal = (takeTargets?ev->GetTarget( varNum ):ev->GetValue( varNum ));
193  Float_t yVal = regVal.at( tgtNum ) - ev->GetTarget( tgtNum );
194 
195  h->Fill( xVal, yVal );
196  }
197 
198  return h;
199 }
200 
201 ////////////////////////////////////////////////////////////////////////////////
202 
204 {
205  Log() << kINFO << "Create variable histograms" << Endl;
206  const DataSetInfo* dsi = GetDataSetInfo();
207 
208  for (UInt_t ivar = 0; ivar < dsi->GetNVariables(); ivar++) {
209  for (UInt_t itgt = 0; itgt < dsi->GetNTargets(); itgt++) {
210  TH2F* h = DeviationAsAFunctionOf( ivar, itgt );
211  TString name( Form("%s_reg_var%d_rtgt%d",prefix.Data(),ivar,itgt) );
212  h->SetName( name );
213  h->SetTitle( name );
214  Store( h );
215  }
216  }
217  Log() << kINFO << "Create regression target histograms" << Endl;
218  for (UInt_t ivar = 0; ivar < dsi->GetNTargets(); ivar++) {
219  for (UInt_t itgt = 0; itgt < dsi->GetNTargets(); itgt++) {
220  TH2F* h = DeviationAsAFunctionOf( dsi->GetNVariables()+ivar, itgt );
221  TString name( Form("%s_reg_tgt%d_rtgt%d",prefix.Data(),ivar,itgt) );
222  h->SetName( name );
223  h->SetTitle( name );
224  Store( h );
225  }
226  }
227 
228  Log() << kINFO << "Create regression average deviation" << Endl;
229  for (UInt_t itgt = 0; itgt < dsi->GetNTargets(); itgt++) {
230  TH1F* h = QuadraticDeviation(itgt);
231  TString name( Form("%s_Quadr_Deviation_target_%d_",prefix.Data(),itgt) );
232  h->SetName( name );
233  h->SetTitle( name );
234  Double_t yq[1], xq[]={0.9};
235  h->GetQuantiles(1,yq,xq);
236  Store( h );
237 
238  TH1F* htrunc = QuadraticDeviation(itgt, true, yq[0]);
239  TString name2( Form("%s_Quadr_Dev_best90perc_target_%d_",prefix.Data(),itgt) );
240  htrunc->SetName( name2 );
241  htrunc->SetTitle( name2 );
242  Store( htrunc );
243  }
244  Log() << kINFO << "Results created" << Endl;
245 }
ymax
float ymax
Definition: THbookFile.cxx:95
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TH1F.h
TMVA::ResultsRegression::DeviationAsAFunctionOf
TH2F * DeviationAsAFunctionOf(UInt_t varNum, UInt_t tgtNum)
Definition: ResultsRegression.cxx:115
TH2F
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:251
TString::Data
const char * Data() const
Definition: TString.h:369
DataSetInfo.h
Form
char * Form(const char *fmt,...)
TNamed::GetTitle
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:54
TMVA::VariableInfo::GetMax
Double_t GetMax() const
Definition: VariableInfo.h:110
TMVA::DataSet::SetCurrentType
void SetCurrentType(Types::ETreeType type) const
Definition: DataSet.h:112
xmax
float xmax
Definition: THbookFile.cxx:95
TMath::Log
Double_t Log(Double_t x)
Definition: TMath.h:760
TMVA::DataSetInfo::GetNVariables
UInt_t GetNVariables() const
Definition: DataSetInfo.h:127
Float_t
float Float_t
Definition: RtypesCore.h:57
VariableInfo.h
TMVA::Event::GetTarget
Float_t GetTarget(UInt_t itgt) const
Definition: Event.h:102
TMath::Abs
Short_t Abs(Short_t d)
Definition: TMathBase.h:120
TH1::SetName
virtual void SetName(const char *name)
Change the name of this histogram.
Definition: TH1.cxx:8415
TString
Definition: TString.h:136
TString.h
TMVA::DataSetInfo::GetVariableInfo
VariableInfo & GetVariableInfo(Int_t i)
Definition: DataSetInfo.h:105
bool
TMVA::ResultsRegression::~ResultsRegression
~ResultsRegression()
destructor
Definition: ResultsRegression.cxx:60
TH1::SetTitle
virtual void SetTitle(const char *title)
See GetStatOverflows for more information.
Definition: TH1.cxx:6344
TMVA::VariableInfo::GetMin
Double_t GetMin() const
Definition: VariableInfo.h:109
TMVA::Event::GetValue
Float_t GetValue(UInt_t ivar) const
return value of i'th variable
Definition: Event.cxx:236
TMVA::DataSetInfo
Definition: DataSetInfo.h:62
MsgLogger.h
ResultsRegression.h
xmin
float xmin
Definition: THbookFile.cxx:95
h
#define h(i)
Definition: RSha256.hxx:124
TMVA::ResultsRegression::CreateDeviationHistograms
void CreateDeviationHistograms(TString prefix)
Definition: ResultsRegression.cxx:203
epsilon
REAL epsilon
Definition: triangle.c:617
TMVA::DataSet::GetEvent
const Event * GetEvent() const
Definition: DataSet.cxx:202
TMVA::DataSet::GetNEvents
Long64_t GetNEvents(Types::ETreeType type=Types::kMaxTreeType) const
Definition: DataSet.h:206
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TMVA::ResultsRegression::QuadraticDeviation
TH1F * QuadraticDeviation(UInt_t tgtNum, Bool_t truncate=false, Double_t truncvalue=0.)
Definition: ResultsRegression.cxx:75
TMVA::VariableInfo
Definition: VariableInfo.h:70
TMVA::DataSet
Definition: DataSet.h:81
Types.h
TMVA::Results
Definition: Results.h:57
TH2F.h
TMVA::Endl
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:182
unsigned int
ymin
float ymin
Definition: THbookFile.cxx:95
TMVA::DataSetInfo::GetNTargets
UInt_t GetNTargets() const
Definition: DataSetInfo.h:128
Double_t
double Double_t
Definition: RtypesCore.h:59
TMVA::ResultsRegression::SetValue
void SetValue(std::vector< Float_t > &value, Int_t ievt)
Definition: ResultsRegression.cxx:67
TMVA::MsgLogger
Definition: MsgLogger.h:83
TMVA::Event::GetWeight
Double_t GetWeight() const
return the event weight - depending on whether the flag IgnoreNegWeightsInTraining is or not.
Definition: Event.cxx:381
TH1F
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:572
TMVA::Event
Definition: Event.h:51
name
char name[80]
Definition: TGX11.cxx:110
Results.h
TMVA::ResultsRegression::ResultsRegression
ResultsRegression(const DataSetInfo *dsi, TString resultsName)
constructor
Definition: ResultsRegression.cxx:51
DataSet.h
TMVA::DataSetInfo::GetTargetInfo
VariableInfo & GetTargetInfo(Int_t i)
Definition: DataSetInfo.h:119
int