39 TMVA::CCTreeWrapper::CCTreeNode::CCTreeNode( DecisionTreeNode* n ) :
42 fNodeResubstitutionEstimate(-1.0),
43 fResubstitutionEstimate(-1.0),
48 if ( n != NULL && n->GetRight() != NULL && n->GetLeft() != NULL ) {
49 SetRight(
new CCTreeNode( ((DecisionTreeNode*) n->GetRight()) ) );
50 GetRight()->SetParent(
this);
51 SetLeft(
new CCTreeNode( ((DecisionTreeNode*) n->GetLeft()) ) );
52 GetLeft()->SetParent(
this);
59 TMVA::CCTreeWrapper::CCTreeNode::~CCTreeNode() {
60 if(GetLeft() != NULL)
delete GetLeftDaughter();
61 if(GetRight() != NULL)
delete GetRightDaughter();
67 Bool_t TMVA::CCTreeWrapper::CCTreeNode::ReadDataRecord( std::istream& in, UInt_t ) {
68 std::string header, title;
70 in >> title; in >> fNLeafDaughters;
71 in >> title; in >> fNodeResubstitutionEstimate;
72 in >> title; in >> fResubstitutionEstimate;
73 in >> title; in >> fAlphaC;
74 in >> title; in >> fMinAlphaC;
81 void TMVA::CCTreeWrapper::CCTreeNode::Print( std::ostream& os )
const {
82 os <<
"----------------------" << std::endl
83 <<
"|~T_t| " << fNLeafDaughters << std::endl
84 <<
"R(t): " << fNodeResubstitutionEstimate << std::endl
85 <<
"R(T_t): " << fResubstitutionEstimate << std::endl
86 <<
"g(t): " << fAlphaC << std::endl
87 <<
"G(t): " << fMinAlphaC << std::endl;
93 void TMVA::CCTreeWrapper::CCTreeNode::PrintRec( std::ostream& os )
const {
95 if(this->GetLeft() != NULL && this->GetRight() != NULL) {
96 this->GetLeft()->PrintRec(os);
97 this->GetRight()->PrintRec(os);
104 TMVA::CCTreeWrapper::CCTreeWrapper( DecisionTree* T, SeparationBase* qualityIndex ) :
108 fRoot =
new CCTreeNode( dynamic_cast<DecisionTreeNode*>(T->GetRoot()) );
109 fQualityIndex = qualityIndex;
116 TMVA::CCTreeWrapper::~CCTreeWrapper( ) {
123 void TMVA::CCTreeWrapper::InitTree( CCTreeNode* t )
125 Double_t s = t->GetDTNode()->GetNSigEvents();
126 Double_t b = t->GetDTNode()->GetNBkgEvents();
130 t->SetNodeResubstitutionEstimate((s+b)*fQualityIndex->GetSeparationIndex(s,b));
132 if(t->GetLeft() != NULL && t->GetRight() != NULL) {
134 InitTree(t->GetLeftDaughter());
135 InitTree(t->GetRightDaughter());
137 t->SetNLeafDaughters(t->GetLeftDaughter()->GetNLeafDaughters() +
138 t->GetRightDaughter()->GetNLeafDaughters());
140 t->SetResubstitutionEstimate(t->GetLeftDaughter()->GetResubstitutionEstimate() +
141 t->GetRightDaughter()->GetResubstitutionEstimate());
143 t->SetAlphaC((t->GetNodeResubstitutionEstimate() - t->GetResubstitutionEstimate()) /
144 (t->GetNLeafDaughters() - 1));
146 t->SetMinAlphaC(std::min(t->GetAlphaC(), std::min(t->GetLeftDaughter()->GetMinAlphaC(),
147 t->GetRightDaughter()->GetMinAlphaC())));
150 t->SetNLeafDaughters(1);
151 t->SetResubstitutionEstimate((s+b)*fQualityIndex->GetSeparationIndex(s,b));
152 t->SetAlphaC(std::numeric_limits<double>::infinity( ));
153 t->SetMinAlphaC(std::numeric_limits<double>::infinity( ));
160 void TMVA::CCTreeWrapper::PruneNode( CCTreeNode* t )
162 if( t->GetLeft() != NULL &&
163 t->GetRight() != NULL ) {
164 CCTreeNode* l = t->GetLeftDaughter();
165 CCTreeNode* r = t->GetRightDaughter();
166 t->SetNLeafDaughters( 1 );
167 t->SetResubstitutionEstimate( t->GetNodeResubstitutionEstimate() );
168 t->SetAlphaC( std::numeric_limits<double>::infinity( ) );
169 t->SetMinAlphaC( std::numeric_limits<double>::infinity( ) );
175 std::cout <<
" ERROR in CCTreeWrapper::PruneNode: you try to prune a leaf node.. that does not make sense " << std::endl;
183 Double_t TMVA::CCTreeWrapper::TestTreeQuality(
const EventList* validationSample )
185 Double_t ncorrect=0, nfalse=0;
186 for (UInt_t ievt=0; ievt < validationSample->size(); ievt++) {
187 Bool_t isSignalType = (CheckEvent(*(*validationSample)[ievt]) > fDTParent->GetNodePurityLimit() ) ? 1 : 0;
189 if (isSignalType == ((*validationSample)[ievt]->GetClass() == 0)) {
190 ncorrect += (*validationSample)[ievt]->GetWeight();
193 nfalse += (*validationSample)[ievt]->GetWeight();
196 return ncorrect / (ncorrect + nfalse);
203 Double_t TMVA::CCTreeWrapper::TestTreeQuality(
const DataSet* validationSample )
205 validationSample->SetCurrentType(Types::kValidation);
207 Double_t ncorrect=0, nfalse=0;
208 for (Long64_t ievt=0; ievt<validationSample->GetNEvents(); ievt++){
209 const Event *ev = validationSample->GetEvent(ievt);
211 Bool_t isSignalType = (CheckEvent(*ev) > fDTParent->GetNodePurityLimit() ) ? 1 : 0;
213 if (isSignalType == (ev->GetClass() == 0)) {
214 ncorrect += ev->GetWeight();
217 nfalse += ev->GetWeight();
220 return ncorrect / (ncorrect + nfalse);
226 Double_t TMVA::CCTreeWrapper::CheckEvent(
const TMVA::Event & e, Bool_t useYesNoLeaf )
228 const DecisionTreeNode* current = fRoot->GetDTNode();
229 CCTreeNode* t = fRoot;
232 t->GetLeft() != NULL &&
233 t->GetRight() != NULL){
234 if (current->GoesRight(e)) {
236 t = t->GetRightDaughter();
237 current = t->GetDTNode();
241 t = t->GetLeftDaughter();
242 current = t->GetDTNode();
246 if (useYesNoLeaf)
return (current->GetPurity() > fDTParent->GetNodePurityLimit() ? 1.0 : -1.0);
247 else return current->GetPurity();
252 void TMVA::CCTreeWrapper::CCTreeNode::AddAttributesToNode(
void* )
const
257 void TMVA::CCTreeWrapper::CCTreeNode::AddContentToNode( std::stringstream& )
const
262 void TMVA::CCTreeWrapper::CCTreeNode::ReadAttributes(
void* , UInt_t )
267 void TMVA::CCTreeWrapper::CCTreeNode::ReadContent( std::stringstream& )