#include <limits>
#ifndef ROOT_TMVA_CCTreeWrapper
#include "TMVA/CCTreeWrapper.h"
#endif
using namespace TMVA;
CCTreeWrapper::CCTreeNode::CCTreeNode( DecisionTreeNode* n ) : fDTNode(n) {
if(((DecisionTreeNode*) n->GetRight()) != NULL &&
((DecisionTreeNode*) n->GetLeft()) != NULL ) {
SetRight( new CCTreeNode( ((DecisionTreeNode*) n->GetRight()) ) );
GetRight()->SetParent(this);
SetLeft( new CCTreeNode( ((DecisionTreeNode*) n->GetLeft()) ) );
GetLeft()->SetParent(this);
}
fNLeafDaughters = 0;
fNodeResubstitutionEstimate = -1.0;
fResubstitutionEstimate = -1.0;
fAlphaC = -1.0;
fMinAlphaC = -1.0;
}
CCTreeWrapper::CCTreeNode::~CCTreeNode() {
if(GetLeft() != NULL) delete GetLeftDaughter();
if(GetRight() != NULL) delete GetRightDaughter();
}
Bool_t CCTreeWrapper::CCTreeNode::ReadDataRecord( std::istream& in ) {
std::string header, title;
in >> header;
in >> title; in >> fNLeafDaughters;
in >> title; in >> fNodeResubstitutionEstimate;
in >> title; in >> fResubstitutionEstimate;
in >> title; in >> fAlphaC;
in >> title; in >> fMinAlphaC;
return true;
}
void CCTreeWrapper::CCTreeNode::Print( ostream& os ) const {
os << "----------------------" << std::endl
<< "|~T_t| " << fNLeafDaughters << std::endl
<< "R(t): " << fNodeResubstitutionEstimate << std::endl
<< "R(T_t): " << fResubstitutionEstimate << std::endl
<< "g(t): " << fAlphaC << std::endl
<< "G(t): " << fMinAlphaC << std::endl;
}
void CCTreeWrapper::CCTreeNode::PrintRec( ostream& os ) const {
this->Print(os);
if(this->GetLeft() != NULL && this->GetRight() != NULL) {
this->GetLeft()->PrintRec(os);
this->GetRight()->PrintRec(os);
}
}
CCTreeWrapper::CCTreeWrapper( DecisionTree* T, SeparationBase* qualityIndex ) : fRoot(NULL) {
fDTParent = T;
fRoot = new CCTreeNode( dynamic_cast<DecisionTreeNode*>(T->GetRoot()) );
fQualityIndex = qualityIndex;
InitTree(fRoot);
}
CCTreeWrapper::~CCTreeWrapper( ) {
delete fRoot;
}
void CCTreeWrapper::InitTree( CCTreeNode* t ) {
Double_t s = t->GetDTNode()->GetNSigEvents();
Double_t b = t->GetDTNode()->GetNBkgEvents();
t->SetNodeResubstitutionEstimate((s+b)*fQualityIndex->GetSeparationIndex(s,b));
if(t->GetLeft() != NULL && t->GetRight() != NULL) {
InitTree(t->GetLeftDaughter());
InitTree(t->GetRightDaughter());
t->SetNLeafDaughters(t->GetLeftDaughter()->GetNLeafDaughters() +
t->GetRightDaughter()->GetNLeafDaughters());
t->SetResubstitutionEstimate(t->GetLeftDaughter()->GetResubstitutionEstimate() +
t->GetRightDaughter()->GetResubstitutionEstimate());
t->SetAlphaC((t->GetNodeResubstitutionEstimate() - t->GetResubstitutionEstimate()) /
(t->GetNLeafDaughters() - 1));
t->SetMinAlphaC(std::min(t->GetAlphaC(), std::min(t->GetLeftDaughter()->GetMinAlphaC(),
t->GetRightDaughter()->GetMinAlphaC())));
}
else {
t->SetNLeafDaughters(1);
t->SetResubstitutionEstimate((s+b)*fQualityIndex->GetSeparationIndex(s,b));
t->SetAlphaC(std::numeric_limits<double>::infinity( ));
t->SetMinAlphaC(std::numeric_limits<double>::infinity( ));
}
}
void CCTreeWrapper::PruneNode( CCTreeNode* t ) {
if( t->GetLeft() != NULL &&
t->GetRight() != NULL ) {
CCTreeNode* l = t->GetLeftDaughter();
CCTreeNode* r = t->GetRightDaughter();
t->SetNLeafDaughters( 1 );
t->SetResubstitutionEstimate( t->GetNodeResubstitutionEstimate() );
t->SetAlphaC( std::numeric_limits<double>::infinity( ) );
t->SetMinAlphaC( std::numeric_limits<double>::infinity( ) );
delete l;
delete r;
t->SetLeft(NULL);
t->SetRight(NULL);
}else{
cout << " ERROR in CCTreeWrapper::PruneNode: you try to prune a leaf node.. that does not make sense " << endl;
}
}
Double_t CCTreeWrapper::TestTreeQuality( const EventList* validationSample ) {
Double_t ncorrect=0, nfalse=0;
for (UInt_t ievt=0; ievt < validationSample->size(); ievt++) {
Bool_t isSignalType = (CheckEvent(*(*validationSample)[ievt]) > fDTParent->GetNodePurityLimit() ) ? 1 : 0;
if (isSignalType == (*validationSample)[ievt]->IsSignal()) {
ncorrect += (*validationSample)[ievt]->GetWeight();
}
else{
nfalse += (*validationSample)[ievt]->GetWeight();
}
}
return ncorrect / (ncorrect + nfalse);
}
Double_t CCTreeWrapper::CheckEvent( const TMVA::Event & e, Bool_t useYesNoLeaf ) {
const DecisionTreeNode* current = fRoot->GetDTNode();
CCTreeNode* t = fRoot;
while(
t->GetLeft() != NULL &&
t->GetRight() != NULL){
if (current->GoesRight(e)) {
t = t->GetRightDaughter();
current = t->GetDTNode();
}
else {
t = t->GetLeftDaughter();
current = t->GetDTNode();
}
}
if (useYesNoLeaf) return (current->GetPurity() > fDTParent->GetNodePurityLimit() ? 1.0 : -1.0);
else return current->GetPurity();
}
Last change: Sat Nov 1 10:21:30 2008
Last generated: 2008-11-01 10:21
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.