00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include <string>
00039 #include <stdexcept>
00040
00041 #include "TMVA/BinaryTree.h"
00042 #include "TMVA/MsgLogger.h"
00043 #include "TMVA/Event.h"
00044 #include "TMVA/Tools.h"
00045
00046 ClassImp(TMVA::BinaryTree)
00047
00048 TMVA::MsgLogger* TMVA::BinaryTree::fgLogger = 0;
00049
00050
00051 TMVA::BinaryTree::BinaryTree( void )
00052 : fRoot ( NULL ),
00053 fNNodes( 0 ),
00054 fDepth ( 0 )
00055 {
00056
00057 if (!fgLogger) fgLogger = new MsgLogger("BinaryTree");
00058 }
00059
00060
00061 TMVA::BinaryTree::~BinaryTree( void )
00062 {
00063
00064 this->DeleteNode( fRoot );
00065 fRoot=0;
00066 }
00067
00068
00069 void TMVA::BinaryTree::DeleteNode( TMVA::Node* node )
00070 {
00071
00072 if (node != NULL) {
00073 this->DeleteNode(node->GetLeft());
00074 this->DeleteNode(node->GetRight());
00075
00076 delete node;
00077 }
00078 }
00079
00080
00081 TMVA::Node* TMVA::BinaryTree::GetLeftDaughter( Node *n)
00082 {
00083
00084 return (Node*) n->GetLeft();
00085 }
00086
00087
00088 TMVA::Node* TMVA::BinaryTree::GetRightDaughter( Node *n)
00089 {
00090
00091 return (Node*) n->GetRight();
00092 }
00093
00094
00095 UInt_t TMVA::BinaryTree::CountNodes(TMVA::Node *n)
00096 {
00097
00098
00099 if (n == NULL){
00100 n = (Node*)this->GetRoot();
00101 if (n == NULL) return 0 ;
00102 }
00103
00104 UInt_t countNodes=1;
00105
00106 if (this->GetLeftDaughter(n) != NULL){
00107 countNodes += this->CountNodes( this->GetLeftDaughter(n) );
00108 }
00109 if (this->GetRightDaughter(n) != NULL) {
00110 countNodes += this->CountNodes( this->GetRightDaughter(n) );
00111 }
00112
00113 return fNNodes = countNodes;
00114 }
00115
00116
00117 void TMVA::BinaryTree::Print(ostream & os) const
00118 {
00119
00120 this->GetRoot()->PrintRec(os);
00121 os << "-1" << std::endl;
00122 }
00123
00124
00125 void* TMVA::BinaryTree::AddXMLTo(void* parent) const {
00126
00127
00128 void* bdt = gTools().AddChild(parent, "BinaryTree");
00129 gTools().AddAttr( bdt, "type" , ClassName() );
00130 this->GetRoot()->AddXMLTo(bdt);
00131 return bdt;
00132 }
00133
00134
00135 void TMVA::BinaryTree::ReadXML(void* node, UInt_t tmva_Version_Code ) {
00136
00137
00138 this->DeleteNode( fRoot );
00139 fRoot= CreateNode();
00140
00141 void* trnode = gTools().GetChild(node);
00142 fRoot->ReadXML(trnode, tmva_Version_Code);
00143
00144 this->SetTotalTreeDepth();
00145 }
00146
00147
00148
00149 ostream& TMVA::operator<< (ostream& os, const TMVA::BinaryTree& tree)
00150 {
00151
00152 tree.Print(os);
00153 return os;
00154 }
00155
00156
00157 void TMVA::BinaryTree::Read(istream & istr, UInt_t tmva_Version_Code )
00158 {
00159
00160
00161
00162
00163 Node * currentNode = GetRoot();
00164 Node* parent = 0;
00165
00166 if(currentNode==0) {
00167 currentNode=CreateNode();
00168 SetRoot(currentNode);
00169 }
00170
00171 while(1) {
00172 if ( ! currentNode->ReadDataRecord(istr, tmva_Version_Code) ) {
00173 delete currentNode;
00174 this->SetTotalTreeDepth();
00175 return;
00176 }
00177
00178
00179 while( parent!=0 && parent->GetDepth() != currentNode->GetDepth()-1) parent=parent->GetParent();
00180
00181 if (parent!=0) {
00182 currentNode->SetParent(parent);
00183 if (currentNode->GetPos()=='l') parent->SetLeft(currentNode);
00184 if (currentNode->GetPos()=='r') parent->SetRight(currentNode);
00185 }
00186
00187 parent = currentNode;
00188
00189 currentNode = CreateNode();
00190 }
00191 }
00192
00193
00194 istream& TMVA::operator>> (istream& istr, TMVA::BinaryTree& tree)
00195 {
00196
00197 tree.Read(istr);
00198 return istr;
00199 }
00200
00201 void TMVA::BinaryTree::SetTotalTreeDepth( Node *n)
00202 {
00203
00204
00205
00206 if (n == NULL){
00207 n = (Node*) this->GetRoot();
00208 if (n == NULL) {
00209 Log() << kFATAL << "SetTotalTreeDepth: started with undefined ROOT node" <<Endl;
00210 return ;
00211 }
00212 }
00213 if (this->GetLeftDaughter(n) != NULL){
00214 this->SetTotalTreeDepth( this->GetLeftDaughter(n) );
00215 }
00216 if (this->GetRightDaughter(n) != NULL) {
00217 this->SetTotalTreeDepth( this->GetRightDaughter(n) );
00218 }
00219 if (n->GetDepth() > this->GetTotalTreeDepth()) this->SetTotalTreeDepth(n->GetDepth());
00220
00221 return;
00222 }