Logo ROOT   6.07/09
Reference Guide
Node.cxx
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Andreas Hoecker, Joerg Stelzer, Helge Voss, Kai Voss
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate Data analysis *
6  * Package: TMVA *
7  * Classes: Node *
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  * Helge Voss <Helge.Voss@cern.ch> - MPI-K Heidelberg, Germany *
16  * Kai Voss <Kai.Voss@cern.ch> - U. of Victoria, Canada *
17  * *
18  * CopyRight (c) 2005: *
19  * CERN, Switzerland *
20  * U. of Victoria, Canada *
21  * MPI-K Heidelberg, Germany *
22  * *
23  * Redistribution and use in source and binary forms, with or without *
24  * modification, are permitted according to the terms listed in LICENSE *
25  * (http://tmva.sourceforge.net/LICENSE) *
26  **********************************************************************************/
27 
28 ////////////////////////////////////////////////////////////////////////////////
29 
30 /*
31  Node for the BinarySearch or Decision Trees.
32 
33  For the binary search tree, it basically consists of the EVENT, and
34  pointers to the parent and daughters
35 
36  In case of the Decision Tree, it specifies parent and daughters, as
37  well as "which variable is used" in the selection of this node,
38  including the respective cut value.
39 */
40 //______________________________________________________________________
41 
42 #include <stdexcept>
43 #include <iosfwd>
44 #include <iostream>
45 
46 #include "TMVA/Node.h"
47 #include "TMVA/Tools.h"
48 
50 
51 Int_t TMVA::Node::fgCount = 0;
52 
54  : fParent( NULL ),
55  fLeft ( NULL),
56  fRight ( NULL ),
57  fPos ( 'u' ),
58  fDepth ( 0 ),
59  fParentTree( NULL )
60 {
61  // default constructor
62  fgCount++;
63 }
64 
65 ////////////////////////////////////////////////////////////////////////////////
66 /// constructor of a daughter node as a daughter of 'p'
67 
68 TMVA::Node::Node( Node* p, char pos )
69  : fParent ( p ),
70  fLeft ( NULL ),
71  fRight( NULL ),
72  fPos ( pos ),
73  fDepth( p->GetDepth() + 1),
74  fParentTree(p->GetParentTree())
75 {
76  fgCount++;
77  if (fPos == 'l' ) p->SetLeft(this);
78  else if (fPos == 'r' ) p->SetRight(this);
79 }
80 
81 ////////////////////////////////////////////////////////////////////////////////
82 /// copy constructor, make sure you don't just copy the poiter to the node, but
83 /// that the parents/daugthers are initialized to 0 (and set by the copy
84 /// constructors of the derived classes
85 
86 TMVA::Node::Node ( const Node &n )
87  : fParent( NULL ),
88  fLeft ( NULL),
89  fRight ( NULL ),
90  fPos ( n.fPos ),
91  fDepth ( n.fDepth ),
92  fParentTree( NULL )
93 {
94  fgCount++;
95 }
96 
97 ////////////////////////////////////////////////////////////////////////////////
98 /// node destructor
99 
101 {
102  fgCount--;
103 }
104 
105 ////////////////////////////////////////////////////////////////////////////////
106 /// retuns the global number of instantiated nodes
107 
109 {
110  return fgCount;
111 }
112 
113 ////////////////////////////////////////////////////////////////////////////////
114 ///recursively go through the part of the tree below this node and count all daughters
115 
117 {
118  Int_t n=1;
119  if (this->GetLeft() != NULL)
120  n+= this->GetLeft()->CountMeAndAllDaughters();
121  if (this->GetRight() != NULL)
122  n+= this->GetRight()->CountMeAndAllDaughters();
123 
124  return n;
125 }
126 
127 // print a node
128 ////////////////////////////////////////////////////////////////////////////////
129 /// output operator for a node
130 
131 std::ostream& TMVA::operator<<( std::ostream& os, const TMVA::Node& node )
132 {
133  node.Print(os);
134  return os; // Return the output stream.
135 }
136 
137 ////////////////////////////////////////////////////////////////////////////////
138 /// output operator with a pointer to the node (which still prints the node itself)
139 
140 std::ostream& TMVA::operator<<( std::ostream& os, const TMVA::Node* node )
141 {
142  if (node!=NULL) node->Print(os);
143  return os; // Return the output stream.
144 }
145 
146 ////////////////////////////////////////////////////////////////////////////////
147 /// add attributes to XML
148 
149 void* TMVA::Node::AddXMLTo( void* parent ) const
150 {
151  std::stringstream s("");
152  AddContentToNode(s);
153  void* node = gTools().AddChild(parent, "Node", s.str().c_str());
154  gTools().AddAttr( node, "pos", fPos );
155  gTools().AddAttr( node, "depth", fDepth );
156  this->AddAttributesToNode(node);
157  if (this->GetLeft()) this->GetLeft()->AddXMLTo(node);
158  if (this->GetRight()) this->GetRight()->AddXMLTo(node);
159  return node;
160 }
161 
162 ////////////////////////////////////////////////////////////////////////////////
163 /// read attributes from XML
164 
165 void TMVA::Node::ReadXML( void* node, UInt_t tmva_Version_Code )
166 {
167  ReadAttributes(node, tmva_Version_Code);
168  const char* content = gTools().GetContent(node);
169  if (content) {
170  std::stringstream s(content);
171  ReadContent(s);
172  }
173  gTools().ReadAttr( node, "pos", fPos );
174  gTools().ReadAttr( node, "depth", fDepth );
175 
176  void* ch = gTools().GetChild(node);
177  while (ch) {
178  Node* n = CreateNode();
179  n->ReadXML(ch, tmva_Version_Code);
180  if (n->GetPos()=='l') { this->SetLeft(n); }
181  else if(n->GetPos()=='r') { this->SetRight(n); }
182  else {
183  std::cout << "neither left nor right" << std::endl;
184  }
185  ch = gTools().GetNextChild(ch);
186  }
187 }
Node * fRight
Definition: Node.h:142
void * AddXMLTo(void *parent) const
add attributes to XML
Definition: Node.cxx:149
int GetCount()
retuns the global number of instantiated nodes
Definition: Node.cxx:108
Node * fLeft
Definition: Node.h:141
virtual void AddAttributesToNode(void *node) const =0
virtual void AddContentToNode(std::stringstream &s) const =0
virtual void ReadAttributes(void *node, UInt_t tmva_Version_Code=TMVA_VERSION_CODE)=0
int Int_t
Definition: RtypesCore.h:41
virtual void SetRight(Node *r)
Definition: Node.h:97
Node()
Definition: Node.cxx:53
virtual ~Node()
node destructor
Definition: Node.cxx:100
UInt_t fDepth
Definition: Node.h:145
void AddAttr(void *node, const char *, const T &value, Int_t precision=16)
Definition: Tools.h:309
void * AddChild(void *parent, const char *childname, const char *content=0, bool isRootNode=false)
add child node
Definition: Tools.cxx:1134
Tools & gTools()
Definition: Tools.cxx:79
virtual void SetLeft(Node *l)
Definition: Node.h:96
void * GetChild(void *parent, const char *childname=0)
get child node
Definition: Tools.cxx:1158
Node * fParent
Definition: Node.h:140
void ReadXML(void *node, UInt_t tmva_Version_Code=TMVA_VERSION_CODE)
read attributes from XML
Definition: Node.cxx:165
virtual void Print(std::ostream &os) const =0
unsigned int UInt_t
Definition: RtypesCore.h:42
const char * GetContent(void *node)
XML helpers.
Definition: Tools.cxx:1182
void ReadAttr(void *node, const char *, T &value)
Definition: Tools.h:296
std::ostream & operator<<(std::ostream &os, const BinaryTree &tree)
print the tree recursinvely using the << operator
Definition: BinaryTree.cxx:159
char fPos
Definition: Node.h:144
#define ClassImp(name)
Definition: Rtypes.h:279
char GetPos() const
Definition: Node.h:124
virtual Node * CreateNode() const =0
void * GetNextChild(void *prevchild, const char *childname=0)
XML helpers.
Definition: Tools.cxx:1170
virtual Node * GetRight() const
Definition: Node.h:92
static Int_t fgCount
Definition: Node.h:150
BinaryTree * fParentTree
Definition: Node.h:147
Abstract ClassifierFactory template that handles arbitrary types.
Int_t CountMeAndAllDaughters() const
recursively go through the part of the tree below this node and count all daughters ...
Definition: Node.cxx:116
static int fgCount
Definition: RStl.cxx:43
#define NULL
Definition: Rtypes.h:82
const Int_t n
Definition: legend1.C:16
virtual void ReadContent(std::stringstream &s)=0
virtual Node * GetLeft() const
Definition: Node.h:91