* *
* File and Version Information: *
**********************************************************************************/
#include "TMVA/MethodHMatrix.h"
#include "TMVA/Tools.h"
#include "TMatrix.h"
#include "Riostream.h"
#include <algorithm>
ClassImp(TMVA::MethodHMatrix)
//Begin_Html
/*
H-Matrix method, which is implemented as a simple comparison of
chi-squared estimators for signal and background, taking into
account the linear correlations between the input variables
This MVA approach is used by the DØ collaboration (FNAL) for the
purpose of electron identification (see, eg.,
<a href="http://arxiv.org/abs/hep-ex/9507007">hep-ex/9507007</a>).
As it is implemented in TMVA, it is usually equivalent or worse than
the Fisher-Mahalanobis discriminant, and it has only been added for
the purpose of completeness.
Two χ<sup>2</sup> estimators are computed for an event, each one
for signal and background, using the estimates for the means and
covariance matrices obtained from the training sample:<br>
<center>
<img vspace=6 src="gif/tmva_chi2.gif" align="bottom" >
</center>
TMVA then uses as normalised analyser for event (<i>i</i>) the ratio:
(<i>χ<sub>S</sub>(i)<sup>2</sup> − χ<sub>B</sub><sup>2</sup>(i)</i>)
(<i>χ<sub>S</sub><sup>2</sup>(i) + χ<sub>B</sub><sup>2</sup>(i)</i>).
*/
//End_Html
TMVA::MethodHMatrix::MethodHMatrix( TString jobName, vector<TString>* theVariables,
TTree* theTree, TString theOption,
TDirectory* theTargetDir )
: TMVA::MethodBase( jobName, theVariables, theTree, theOption, theTargetDir )
{
InitHMatrix();
}
TMVA::MethodHMatrix::MethodHMatrix( vector<TString> *theVariables,
TString theWeightFile,
TDirectory* theTargetDir )
: TMVA::MethodBase( theVariables, theWeightFile, theTargetDir )
{
InitHMatrix();
}
void TMVA::MethodHMatrix::InitHMatrix( void )
{
fMethodName = "HMatrix";
fMethod = TMVA::Types::HMatrix;
fTestvar = fTestvarPrefix+GetMethodName();
fNormaliseInputVars = kTRUE;
fInvHMatrixS = new TMatrixD( fNvar, fNvar );
fInvHMatrixB = new TMatrixD( fNvar, fNvar );
fVecMeanS = new TVectorD( fNvar );
fVecMeanB = new TVectorD( fNvar );
}
TMVA::MethodHMatrix::~MethodHMatrix( void )
{
if (NULL != fInvHMatrixS) delete fInvHMatrixS;
if (NULL != fInvHMatrixB) delete fInvHMatrixB;
if (NULL != fVecMeanS ) delete fVecMeanS;
if (NULL != fVecMeanB ) delete fVecMeanB;
}
void TMVA::MethodHMatrix::Train( void )
{
if (!CheckSanity()) {
cout << "--- " << GetName() << ": Error: sanity check failed" << endl;
exit(1);
}
Double_t meanS, meanB, rmsS, rmsB, xmin, xmax;
for (Int_t ivar=0; ivar<fNvar; ivar++) {
TMVA::Tools::ComputeStat( fTrainingTree, (*fInputVars)[ivar],
meanS, meanB, rmsS, rmsB, xmin, xmax,
fNormaliseInputVars );
(*fVecMeanS)(ivar) = meanS;
(*fVecMeanB)(ivar) = meanB;
}
TMVA::Tools::GetCovarianceMatrix( fTrainingTree, fInvHMatrixS, fInputVars, 1,
fNormaliseInputVars );
TMVA::Tools::GetCovarianceMatrix( fTrainingTree, fInvHMatrixB, fInputVars, 0,
fNormaliseInputVars );
fInvHMatrixS->Invert();
fInvHMatrixB->Invert();
WriteWeightsToFile();
}
Double_t TMVA::MethodHMatrix::GetMvaValue( TMVA::Event *e )
{
Double_t myMVA = 0;
Double_t s = GetChi2( e, kSignal );
Double_t b = GetChi2( e, kBackground );
if ((s + b) > 0) myMVA = (b - s)/(s + b);
else {
cout << "--- " << GetName() << ": Big trouble: s+b: " << s+b << " ==> abort"
<< endl;
exit(1);
}
return myMVA;
}
Double_t TMVA::MethodHMatrix::GetChi2( TMVA::Event *e, Type type ) const
{
Int_t ivar,jvar;
vector<Double_t> val( fNvar );
for (ivar=0; ivar<fNvar; ivar++) {
val[ivar] = e->GetData(ivar);
if (fNormaliseInputVars)
val[ivar] = __N__( val[ivar], GetXminNorm( ivar ), GetXmaxNorm( ivar ) );
}
Double_t chi2 = 0;
for (ivar=0; ivar<fNvar; ivar++) {
for (jvar=0; jvar<fNvar; jvar++) {
if (type == kSignal)
chi2 += ( (val[ivar] - (*fVecMeanS)(ivar))*(val[jvar] - (*fVecMeanS)(jvar))
* (*fInvHMatrixS)(ivar,jvar) );
else
chi2 += ( (val[ivar] - (*fVecMeanB)(ivar))*(val[jvar] - (*fVecMeanB)(jvar))
* (*fInvHMatrixB)(ivar,jvar) );
}
}
if (chi2 < 0) {
cout << "--- " << GetName() << ": Error in ::GetChi2: negative chi2 ==> abort"
<< chi2 << endl;
exit(1);
}
return chi2;
}
void TMVA::MethodHMatrix::WriteWeightsToFile( void )
{
Int_t ivar,jvar;
TString fname = GetWeightFileName();
cout << "--- " << GetName() << ": creating weight file: " << fname << endl;
ofstream fout( fname );
if (!fout.good( )) {
cout << "--- " << GetName() << ": Error in ::WriteWeightsToFile: "
<< "unable to open output weight file: " << fname << endl;
exit(1);
}
fout << this->GetMethodName() <<endl;
fout << "NVars= " << fNvar <<endl;
for (ivar=0; ivar<fNvar; ivar++) {
TString var = (*fInputVars)[ivar];
fout << var << " " << GetXminNorm( var ) << " " << GetXmaxNorm( var ) << endl;
}
for (ivar=0; ivar<fNvar; ivar++) {
fout << (*fVecMeanS)(ivar) << " " << (*fVecMeanB)(ivar) << endl;
}
for (ivar=0; ivar<fNvar; ivar++) {
for (jvar=0; jvar<fNvar; jvar++) {
fout << (*fInvHMatrixS)(ivar,jvar) << " ";
}
fout << endl;
}
for (ivar=0; ivar<fNvar; ivar++) {
for (jvar=0; jvar<fNvar; jvar++) {
fout << (*fInvHMatrixB)(ivar,jvar) << " ";
}
fout << endl;
}
fout.close();
}
void TMVA::MethodHMatrix::ReadWeightsFromFile( void )
{
Int_t ivar,jvar;
TString fname = GetWeightFileName();
cout << "--- " << GetName() << ": reading weight file: " << fname << endl;
ifstream fin( fname );
if (!fin.good( )) {
cout << "--- " << GetName() << ": Error in ::ReadWeightsFromFile: "
<< "unable to open input file: " << fname << endl;
exit(1);
}
TString var, dummy;
Double_t xmin, xmax;
fin >> dummy;
this->SetMethodName(dummy);
fin >> dummy >> fNvar;
for (ivar=0; ivar<fNvar; ivar++) {
fin >> var >> xmin >> xmax;
if (var != (*fInputVars)[ivar]) {
cout << "--- " << GetName() << ": Error while reading weight file; "
<< "unknown variable: " << var << " at position: " << ivar << ". "
<< "Expected variable: " << (*fInputVars)[ivar] << " ==> abort" << endl;
exit(1);
}
this->SetXminNorm( ivar, xmin );
this->SetXmaxNorm( ivar, xmax );
}
for (ivar=0; ivar<fNvar; ivar++)
fin >> (*fVecMeanS)(ivar) >> (*fVecMeanB)(ivar);
for (ivar=0; ivar<fNvar; ivar++)
for (jvar=0; jvar<fNvar; jvar++)
fin >> (*fInvHMatrixS)(ivar,jvar);
for (ivar=0; ivar<fNvar; ivar++)
for (jvar=0; jvar<fNvar; jvar++)
fin >> (*fInvHMatrixB)(ivar,jvar);
fin.close();
}
void TMVA::MethodHMatrix::WriteHistosToFile( void )
{
cout << "--- " << GetName() << ": write " << GetName()
<<" special histos to file: " << fBaseDir->GetPath() << endl;
}
ROOT page - Class index - Class Hierarchy - Top of the page
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.