ROOT logo
// @(#)root/tmva $Id: Node.h 31458 2009-11-30 13:58:20Z stelzer $    
// Author: Andreas Hoecker, Joerg Stelzer, Helge Voss, Kai Voss 

/**********************************************************************************
 * Project: TMVA - a Root-integrated toolkit for multivariate data analysis       *
 * Package: TMVA                                                                  *
 * Classes: Node                                                                  *
 * Web    : http://tmva.sourceforge.net                                           *
 *                                                                                *
 * Description:                                                                   *
 *      Node for the BinarySearch or Decision Trees                               *
 *                                                                                *
 * Authors (alphabetical):                                                        *
 *      Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland              *
 *      Helge Voss      <Helge.Voss@cern.ch>     - MPI-K Heidelberg, Germany      *
 *      Kai Voss        <Kai.Voss@cern.ch>       - U. of Victoria, Canada         *
 *                                                                                *
 * Copyright (c) 2005:                                                            *
 *      CERN, Switzerland                                                         * 
 *      U. of Victoria, Canada                                                    * 
 *      MPI-K Heidelberg, Germany                                                 * 
 *                                                                                *
 * Redistribution and use in source and binary forms, with or without             *
 * modification, are permitted according to the terms listed in LICENSE           *
 * (http://tmva.sourceforge.net/LICENSE)                                          *
 **********************************************************************************/

#ifndef ROOT_TMVA_Node
#define ROOT_TMVA_Node

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// Node                                                                 //
//                                                                      //
// Node base class for the BinarySearch or Decision Trees Nodes         //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#include <iosfwd>
#ifndef ROOT_Rtypes
#include "Rtypes.h"
#endif

#ifndef ROOT_TMVA_Version
#include "TMVA/Version.h"
#endif

namespace TMVA {

   class Node;
   class Event;
   class BinaryTree;

   std::ostream& operator<<( std::ostream& os, const Node& node );
   std::ostream& operator<<( std::ostream& os, const Node* node );

   // a class used to identify a Node; (needed for recursive reading from text file)
   // (currently it is NOT UNIQUE... but could eventually made it
   // a node in the tree structure
   class Node {

      // output operator for a node
      friend std::ostream& operator << (std::ostream& os, const Node& node);
      // output operator with a pointer to the node (which still prints the node itself)
      friend std::ostream& operator << (std::ostream& os, const Node* node);
    
   public:

      // constructor of a node 
      Node();
      
      // constructor of a daughter node as a daughter of 'p'
      Node( Node* p, char pos );

      // copy constructor
      Node( const Node &n );

      // destructor
      virtual ~Node();

      virtual Node* CreateNode() const = 0;

      // test event if i{ decends the tree at this node to the right  
      virtual Bool_t GoesRight( const Event& ) const = 0;
      // test event if it decends the tree at this node to the left 

      virtual Bool_t GoesLeft ( const Event& ) const = 0;
      // test event if it is equal to the event that "makes the node" (just for the "search tree"  

      // return pointer to the left daughter node
      inline Node* GetLeft  () const { return fLeft;   }
      // return pointer to the right daughter node
      inline Node* GetRight () const { return fRight;  }
      // return pointer to the parent node
      inline Node* GetParent() const { return fParent; }
    
      // set pointer to the left daughter node
      inline void SetLeft  (Node* l) { fLeft   = l;} 
      // set pointer to the right daughter node
      inline void SetRight (Node* r) { fRight  = r;} 
      // set pointer to the parent node
      inline void SetParent(Node* p) { fParent = p;} 
    
      //recursively go through the part of the tree below this node and count all daughters
      Int_t  CountMeAndAllDaughters() const;
    
      // printout of the node
      virtual void Print( std::ostream& os ) const = 0;

      // recursive printout of the node and it daughters 
      virtual void PrintRec ( std::ostream& os ) const = 0;

      void* AddXMLTo(void* parent) const;
      void  ReadXML(void* node, UInt_t tmva_Version_Code = TMVA_VERSION_CODE );
      virtual void AddAttributesToNode(void* node) const = 0;
      virtual void AddContentToNode(std::stringstream& s) const = 0;

      // Set depth, layer of the where the node is within the tree, seen from the top (root)
      void SetDepth(UInt_t d){fDepth=d;}
      
      // Return depth, layer of the where the node is within the tree, seen from the top (root)
      UInt_t GetDepth() const {return fDepth;}
      
      // set node position, i.e, the node is a left (l) or right (r) daugther
      void SetPos(char s) {fPos=s;}
      
      // Return the node position, i.e, the node is a left (l) or right (r) daugther
      char GetPos() const {return fPos;}

      // Return the pointer to the Parent tree to which the Node belongs 
      TMVA::BinaryTree* GetParentTree() const {return fParentTree;}

      // set the pointer to the Parent Tree to which the Node belongs 
      void SetParentTree(TMVA::BinaryTree* t) {fParentTree = t;} 

      int GetCount(){return fgCount;}

      virtual Bool_t ReadDataRecord( std::istream&, UInt_t tmva_Version_Code = TMVA_VERSION_CODE ) = 0;
      virtual void ReadAttributes(void* node, UInt_t tmva_Version_Code = TMVA_VERSION_CODE  ) = 0;
      virtual void ReadContent(std::stringstream& s) =0;

   private: 

      Node*   fParent;              // the previous (parent) node
      Node*   fLeft;                // pointers to the two "daughter" nodes
      Node*   fRight;               // pointers to the two "daughter" nodes

      char    fPos;                 // position, i.e. it is a left (l) or right (r) daughter 
      UInt_t  fDepth;               // depth of the node within the tree (seen from root node)

      BinaryTree*  fParentTree;     // pointer to the parent tree to which the Node belongs 

      static Int_t fgCount;         // counter of all nodes present.. for debug.. to spot memory leaks...

   public:
      ClassDef(Node,0) // Node for the BinarySearch or Decision Trees
   };

} // namespace TMVA

#endif

 Node.h:1
 Node.h:2
 Node.h:3
 Node.h:4
 Node.h:5
 Node.h:6
 Node.h:7
 Node.h:8
 Node.h:9
 Node.h:10
 Node.h:11
 Node.h:12
 Node.h:13
 Node.h:14
 Node.h:15
 Node.h:16
 Node.h:17
 Node.h:18
 Node.h:19
 Node.h:20
 Node.h:21
 Node.h:22
 Node.h:23
 Node.h:24
 Node.h:25
 Node.h:26
 Node.h:27
 Node.h:28
 Node.h:29
 Node.h:30
 Node.h:31
 Node.h:32
 Node.h:33
 Node.h:34
 Node.h:35
 Node.h:36
 Node.h:37
 Node.h:38
 Node.h:39
 Node.h:40
 Node.h:41
 Node.h:42
 Node.h:43
 Node.h:44
 Node.h:45
 Node.h:46
 Node.h:47
 Node.h:48
 Node.h:49
 Node.h:50
 Node.h:51
 Node.h:52
 Node.h:53
 Node.h:54
 Node.h:55
 Node.h:56
 Node.h:57
 Node.h:58
 Node.h:59
 Node.h:60
 Node.h:61
 Node.h:62
 Node.h:63
 Node.h:64
 Node.h:65
 Node.h:66
 Node.h:67
 Node.h:68
 Node.h:69
 Node.h:70
 Node.h:71
 Node.h:72
 Node.h:73
 Node.h:74
 Node.h:75
 Node.h:76
 Node.h:77
 Node.h:78
 Node.h:79
 Node.h:80
 Node.h:81
 Node.h:82
 Node.h:83
 Node.h:84
 Node.h:85
 Node.h:86
 Node.h:87
 Node.h:88
 Node.h:89
 Node.h:90
 Node.h:91
 Node.h:92
 Node.h:93
 Node.h:94
 Node.h:95
 Node.h:96
 Node.h:97
 Node.h:98
 Node.h:99
 Node.h:100
 Node.h:101
 Node.h:102
 Node.h:103
 Node.h:104
 Node.h:105
 Node.h:106
 Node.h:107
 Node.h:108
 Node.h:109
 Node.h:110
 Node.h:111
 Node.h:112
 Node.h:113
 Node.h:114
 Node.h:115
 Node.h:116
 Node.h:117
 Node.h:118
 Node.h:119
 Node.h:120
 Node.h:121
 Node.h:122
 Node.h:123
 Node.h:124
 Node.h:125
 Node.h:126
 Node.h:127
 Node.h:128
 Node.h:129
 Node.h:130
 Node.h:131
 Node.h:132
 Node.h:133
 Node.h:134
 Node.h:135
 Node.h:136
 Node.h:137
 Node.h:138
 Node.h:139
 Node.h:140
 Node.h:141
 Node.h:142
 Node.h:143
 Node.h:144
 Node.h:145
 Node.h:146
 Node.h:147
 Node.h:148
 Node.h:149
 Node.h:150
 Node.h:151
 Node.h:152
 Node.h:153
 Node.h:154
 Node.h:155
 Node.h:156
 Node.h:157
 Node.h:158
 Node.h:159
 Node.h:160
 Node.h:161
 Node.h:162