ROOT  6.06/09
Reference Guide
BinaryTree.cxx
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Andreas Hoecker, Joerg Stelzer, Helge Voss, Kai Voss, Eckhard von Toerne
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : TMVA::BinaryTree *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * Implementation (see header file for description) *
12  * *
13  * Authors (alphabetical): *
14  * Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland *
15  * Joerg Stelzer <stelzer@cern.ch> - DESY, Germany *
16  * Helge Voss <Helge.Voss@cern.ch> - MPI-K Heidelberg, Germany *
17  * Kai Voss <Kai.Voss@cern.ch> - U. of Victoria, Canada *
18  * Eckhard v. Toerne <evt@uni-bonn.de> - U of Bonn, Germany *
19  * *
20  * Copyright (c) 2005-2011: *
21  * CERN, Switzerland *
22  * U. of Victoria, Canada *
23  * MPI-K Heidelberg, Germany *
24  * U. of Bonn, Germany *
25  * *
26  * Redistribution and use in source and binary forms, with or without *
27  * modification, are permitted according to the terms listed in LICENSE *
28  * (http://tmva.sourceforge.net/LICENSE) *
29  **********************************************************************************/
30 
31 //////////////////////////////////////////////////////////////////////////
32 // //
33 // BinaryTree //
34 // //
35 // Base class for BinarySearch and Decision Trees //
36 // //
37 //////////////////////////////////////////////////////////////////////////
38 
39 #include <string>
40 #include <stdexcept>
41 
42 #include "TMVA/BinaryTree.h"
43 #include "TMVA/MsgLogger.h"
44 #include "TMVA/Event.h"
45 #include "TMVA/Tools.h"
46 
48 
49 ////////////////////////////////////////////////////////////////////////////////
50 /// constructor for a yet "empty" tree. Needs to be filled afterwards
51 
52 TMVA::BinaryTree::BinaryTree( void )
53  : fRoot ( NULL ),
54  fNNodes( 0 ),
55  fDepth ( 0 )
56 {
57 }
58 
59 ////////////////////////////////////////////////////////////////////////////////
60 ///destructor (deletes the nodes and "events" if owned by the tree
61 
63 {
64  this->DeleteNode( fRoot );
65  fRoot=0;
66 }
67 
68 ////////////////////////////////////////////////////////////////////////////////
69 /// protected, recursive, function used by the class destructor and when Pruning
70 
72 {
73  if (node != NULL) { //If the node is not NULL...
74  this->DeleteNode(node->GetLeft()); //Delete its left node.
75  this->DeleteNode(node->GetRight()); //Delete its right node.
76 
77  delete node; // Delete the node in memory
78  }
79 }
80 
81 ////////////////////////////////////////////////////////////////////////////////
82 /// get left daughter node current node "n"
83 
85 {
86  return (Node*) n->GetLeft();
87 }
88 
89 ////////////////////////////////////////////////////////////////////////////////
90 /// get right daughter node current node "n"
91 
93 {
94  return (Node*) n->GetRight();
95 }
96 
97 ////////////////////////////////////////////////////////////////////////////////
98 /// return the number of nodes in the tree. (make a new count --> takes time)
99 
101 {
102  if (n == NULL){ //default, start at the tree top, then descend recursively
103  n = (Node*)this->GetRoot();
104  if (n == NULL) return 0 ;
105  }
106 
107  UInt_t countNodes=1;
108 
109  if (this->GetLeftDaughter(n) != NULL){
110  countNodes += this->CountNodes( this->GetLeftDaughter(n) );
111  }
112  if (this->GetRightDaughter(n) != NULL) {
113  countNodes += this->CountNodes( this->GetRightDaughter(n) );
114  }
115 
116  return fNNodes = countNodes;
117 }
118 
119 ////////////////////////////////////////////////////////////////////////////////
120 /// recursively print the tree
121 
122 void TMVA::BinaryTree::Print(std::ostream & os) const
123 {
124  this->GetRoot()->PrintRec(os);
125  os << "-1" << std::endl;
126 }
127 
128 ////////////////////////////////////////////////////////////////////////////////
129 /// add attributes to XML
130 
131 void* TMVA::BinaryTree::AddXMLTo(void* parent) const {
132  void* bdt = gTools().AddChild(parent, "BinaryTree");
133  gTools().AddAttr( bdt, "type" , ClassName() );
134  this->GetRoot()->AddXMLTo(bdt);
135  return bdt;
136 }
137 
138 ////////////////////////////////////////////////////////////////////////////////
139 /// read attributes from XML
140 
141 void TMVA::BinaryTree::ReadXML(void* node, UInt_t tmva_Version_Code ) {
142  this->DeleteNode( fRoot );
143  fRoot= CreateNode();
144 
145  void* trnode = gTools().GetChild(node);
146  fRoot->ReadXML(trnode, tmva_Version_Code);
147 
148  this->SetTotalTreeDepth();
149 }
150 
151 
152 ////////////////////////////////////////////////////////////////////////////////
153 /// print the tree recursinvely using the << operator
154 
155 std::ostream& TMVA::operator<< (std::ostream& os, const TMVA::BinaryTree& tree)
156 {
157  tree.Print(os);
158  return os; // Return the output stream.
159 }
160 
161 ////////////////////////////////////////////////////////////////////////////////
162 /// Read the binary tree from an input stream.
163 /// The input stream format depends on the tree type,
164 /// it is defined be the node of the tree
165 
166 void TMVA::BinaryTree::Read(std::istream & istr, UInt_t tmva_Version_Code )
167 {
168  Node * currentNode = GetRoot();
169  Node* parent = 0;
170 
171  if(currentNode==0) {
172  currentNode=CreateNode();
173  SetRoot(currentNode);
174  }
175 
176  while(1) {
177  if ( ! currentNode->ReadDataRecord(istr, tmva_Version_Code) ) {
178  delete currentNode;
179  this->SetTotalTreeDepth();
180  return;
181  }
182 
183  // find parent node
184  while( parent!=0 && parent->GetDepth() != currentNode->GetDepth()-1) parent=parent->GetParent();
185 
186  if (parent!=0) { // link new node to parent
187  currentNode->SetParent(parent);
188  if (currentNode->GetPos()=='l') parent->SetLeft(currentNode);
189  if (currentNode->GetPos()=='r') parent->SetRight(currentNode);
190  }
191 
192  parent = currentNode; // latest node read might be parent of new one
193 
194  currentNode = CreateNode(); // create a new node to be read next
195  }
196 }
197 
198 ////////////////////////////////////////////////////////////////////////////////
199 /// read the tree from an std::istream
200 
201 std::istream& TMVA::operator>> (std::istream& istr, TMVA::BinaryTree& tree)
202 {
203  tree.Read(istr);
204  return istr;
205 }
206 ////////////////////////////////////////////////////////////////////////////////
207 /// descend a tree to find all its leaf nodes, fill max depth reached in the
208 /// tree at the same time.
209 
211 {
212  if (n == NULL){ //default, start at the tree top, then descend recursively
213  n = (Node*) this->GetRoot();
214  if (n == NULL) {
215  Log() << kFATAL << "SetTotalTreeDepth: started with undefined ROOT node" <<Endl;
216  return ;
217  }
218  }
219  if (this->GetLeftDaughter(n) != NULL){
220  this->SetTotalTreeDepth( this->GetLeftDaughter(n) );
221  }
222  if (this->GetRightDaughter(n) != NULL) {
223  this->SetTotalTreeDepth( this->GetRightDaughter(n) );
224  }
225  if (n->GetDepth() > this->GetTotalTreeDepth()) this->SetTotalTreeDepth(n->GetDepth());
226 
227  return;
228 }
229 
230 ////////////////////////////////////////////////////////////////////////////////
231 
233  TTHREAD_TLS_DECL_ARG(MsgLogger,logger,"BinaryTree");
234  return logger;
235 }
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:162
virtual void Read(std::istream &istr, UInt_t tmva_Version_Code=TMVA_VERSION_CODE)
Read the binary tree from an input stream.
Definition: BinaryTree.cxx:166
virtual ~BinaryTree()
destructor (deletes the nodes and "events" if owned by the tree
Definition: BinaryTree.cxx:62
virtual void SetRight(Node *r)
Definition: Node.h:97
void AddAttr(void *node, const char *, const T &value, Int_t precision=16)
Definition: Tools.h:308
void DeleteNode(Node *)
protected, recursive, function used by the class destructor and when Pruning
Definition: BinaryTree.cxx:71
void * AddChild(void *parent, const char *childname, const char *content=0, bool isRootNode=false)
add child node
Definition: Tools.cxx:1134
virtual Bool_t ReadDataRecord(std::istream &, UInt_t tmva_Version_Code=TMVA_VERSION_CODE)=0
Tools & gTools()
Definition: Tools.cxx:79
UInt_t GetDepth() const
Definition: Node.h:118
virtual void SetLeft(Node *l)
Definition: Node.h:96
void * GetChild(void *parent, const char *childname=0)
get child node
Definition: Tools.cxx:1158
const std::string ClassName(PyObject *pyobj)
Retrieve the class name from the given python object (which may be just an instance of the class)...
Definition: Utility.cxx:691
ClassImp(TMVA::BinaryTree) TMVA
constructor for a yet "empty" tree. Needs to be filled afterwards
Definition: BinaryTree.cxx:47
void SetTotalTreeDepth(Int_t depth)
Definition: BinaryTree.h:101
return
Definition: TBase64.cxx:62
unsigned int UInt_t
Definition: RtypesCore.h:42
virtual void ReadXML(void *node, UInt_t tmva_Version_Code=TMVA_VERSION_CODE)
read attributes from XML
Definition: BinaryTree.cxx:141
virtual void Print(std::ostream &os) const
recursively print the tree
Definition: BinaryTree.cxx:122
std::ostream & operator<<(std::ostream &os, const BinaryTree &tree)
print the tree recursinvely using the << operator
Definition: BinaryTree.cxx:155
std::istream & operator>>(std::istream &istr, BinaryTree &tree)
read the tree from an std::istream
Definition: BinaryTree.cxx:201
virtual void SetParent(Node *p)
Definition: Node.h:98
char GetPos() const
Definition: Node.h:124
virtual Node * GetParent() const
Definition: Node.h:93
virtual Node * GetRight() const
Definition: Node.h:92
virtual void * AddXMLTo(void *parent) const
add attributes to XML
Definition: BinaryTree.cxx:131
UInt_t CountNodes(Node *n=NULL)
return the number of nodes in the tree. (make a new count –> takes time)
Definition: BinaryTree.cxx:100
Abstract ClassifierFactory template that handles arbitrary types.
Node * GetRightDaughter(Node *n)
get right daughter node current node "n"
Definition: BinaryTree.cxx:92
#define NULL
Definition: Rtypes.h:82
Node * GetLeftDaughter(Node *n)
get left daughter node current node "n"
Definition: BinaryTree.cxx:84
MsgLogger & Log() const
Definition: BinaryTree.cxx:232
const Int_t n
Definition: legend1.C:16
Definition: math.cpp:60
virtual Node * GetLeft() const
Definition: Node.h:91