Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 * *
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 * (see tmva/doc/LICENSE) *
26 **********************************************************************************/
27
28/*! \class TMVA::Node
29\ingroup TMVA
30Node for the BinarySearch or Decision Trees.
31
32For the binary search tree, it basically consists of the EVENT, and
33pointers to the parent and daughters
34
35In case of the Decision Tree, it specifies parent and daughters, as
36well as "which variable is used" in the selection of this node,
37including the respective cut value.
38*/
39
40#include <stdexcept>
41#include <iosfwd>
42#include <iostream>
43
44#include "TMVA/Node.h"
45#include "TMVA/Tools.h"
46
47
49
51 : fParent( NULL ),
52 fLeft ( NULL),
53 fRight ( NULL ),
54 fPos ( 'u' ),
55 fDepth ( 0 ),
56 fParentTree( NULL )
57{
58 // default constructor
59 fgCount++;
60}
61
62////////////////////////////////////////////////////////////////////////////////
63/// constructor of a daughter node as a daughter of 'p'
64
65TMVA::Node::Node( Node* p, char pos )
66 : fParent ( p ),
67 fLeft ( NULL ),
68 fRight( NULL ),
69 fPos ( pos ),
70 fDepth( p->GetDepth() + 1),
71 fParentTree(p->GetParentTree())
72{
73 fgCount++;
74 if (fPos == 'l' ) p->SetLeft(this);
75 else if (fPos == 'r' ) p->SetRight(this);
76}
77
78////////////////////////////////////////////////////////////////////////////////
79/// copy constructor, make sure you don't just copy the pointer to the node, but
80/// that the parents/daughters are initialized to 0 (and set by the copy
81/// constructors of the derived classes
82
84 : fParent( NULL ),
85 fLeft ( NULL),
86 fRight ( NULL ),
87 fPos ( n.fPos ),
88 fDepth ( n.fDepth ),
89 fParentTree( NULL )
90{
91 fgCount++;
92}
93
94////////////////////////////////////////////////////////////////////////////////
95/// node destructor
96
98{
99 fgCount--;
100}
101
102////////////////////////////////////////////////////////////////////////////////
103/// returns the global number of instantiated nodes
104
106{
107 return fgCount;
108}
109
110////////////////////////////////////////////////////////////////////////////////
111///recursively go through the part of the tree below this node and count all daughters
112
114{
115 Int_t n=1;
116 if (this->GetLeft() != NULL)
117 n+= this->GetLeft()->CountMeAndAllDaughters();
118 if (this->GetRight() != NULL)
119 n+= this->GetRight()->CountMeAndAllDaughters();
120
121 return n;
122}
123
124// print a node
125////////////////////////////////////////////////////////////////////////////////
126/// output operator for a node
127
128std::ostream& TMVA::operator<<( std::ostream& os, const TMVA::Node& node )
129{
130 node.Print(os);
131 return os; // Return the output stream.
132}
133
134////////////////////////////////////////////////////////////////////////////////
135/// output operator with a pointer to the node (which still prints the node itself)
136
137std::ostream& TMVA::operator<<( std::ostream& os, const TMVA::Node* node )
138{
139 if (node!=NULL) node->Print(os);
140 return os; // Return the output stream.
141}
142
143////////////////////////////////////////////////////////////////////////////////
144/// add attributes to XML
145
146void* TMVA::Node::AddXMLTo( void* parent ) const
147{
148 std::stringstream s("");
149 AddContentToNode(s);
150 void* node = gTools().AddChild(parent, "Node", s.str().c_str());
151 gTools().AddAttr( node, "pos", fPos );
152 gTools().AddAttr( node, "depth", fDepth );
153 this->AddAttributesToNode(node);
154 if (this->GetLeft()) this->GetLeft()->AddXMLTo(node);
155 if (this->GetRight()) this->GetRight()->AddXMLTo(node);
156 return node;
157}
158
159////////////////////////////////////////////////////////////////////////////////
160/// read attributes from XML
161
163{
164 ReadAttributes(node, tmva_Version_Code);
165 const char* content = gTools().GetContent(node);
166 if (content) {
167 std::stringstream s(content);
168 ReadContent(s);
169 }
170 gTools().ReadAttr( node, "pos", fPos );
171 gTools().ReadAttr( node, "depth", fDepth );
172
173 void* ch = gTools().GetChild(node);
174 while (ch) {
175 Node* n = CreateNode();
176 n->ReadXML(ch, tmva_Version_Code);
177 if (n->GetPos()=='l') { this->SetLeft(n); }
178 else if(n->GetPos()=='r') { this->SetRight(n); }
179 else {
180 std::cout << "neither left nor right" << std::endl;
181 }
182 ch = gTools().GetNextChild(ch);
183 }
184}
static ROOT::Experimental::RTreeMapBase::Node CreateNode(const ROOT::Experimental::RNTupleInspector &insp, const ROOT::RFieldDescriptor &fldDesc, std::uint64_t childrenIdx, std::uint64_t nChildren, ROOT::DescriptorId_t rootId, size_t rootSize)
static int fgCount
Definition RStl.cxx:38
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
winID h TVirtualViewer3D TVirtualGLPainter p
Node for the BinarySearch or Decision Trees.
Definition Node.h:58
static Int_t fgCount
counter of all nodes present.. for debug.. to spot memory leaks...
Definition Node.h:148
virtual void Print(std::ostream &os) const =0
char fPos
position, i.e. it is a left (l) or right (r) daughter
Definition Node.h:142
void * AddXMLTo(void *parent) const
add attributes to XML
Definition Node.cxx:146
int GetCount()
returns the global number of instantiated nodes
Definition Node.cxx:105
Int_t CountMeAndAllDaughters() const
recursively go through the part of the tree below this node and count all daughters
Definition Node.cxx:113
virtual ~Node()
node destructor
Definition Node.cxx:97
void ReadXML(void *node, UInt_t tmva_Version_Code=262657)
read attributes from XML
Definition Node.cxx:162
const char * GetContent(void *node)
XML helpers.
Definition Tools.cxx:1174
void ReadAttr(void *node, const char *, T &value)
read attribute from xml
Definition Tools.h:329
void * GetChild(void *parent, const char *childname=nullptr)
get child node
Definition Tools.cxx:1150
void AddAttr(void *node, const char *, const T &value, Int_t precision=16)
add attribute to xml
Definition Tools.h:347
void * AddChild(void *parent, const char *childname, const char *content=nullptr, bool isRootNode=false)
add child node
Definition Tools.cxx:1124
void * GetNextChild(void *prevchild, const char *childname=nullptr)
XML helpers.
Definition Tools.cxx:1162
const Int_t n
Definition legend1.C:16
Tools & gTools()
std::ostream & operator<<(std::ostream &os, const BinaryTree &tree)