Node.cxx

Go to the documentation of this file.
00001 // @(#)root/tmva $Id: Node.cxx 37986 2011-02-04 21:42:15Z pcanal $    
00002 // Author: Andreas Hoecker, Joerg Stelzer, Helge Voss, Kai Voss 
00003 
00004 /**********************************************************************************
00005  * Project: TMVA - a Root-integrated toolkit for multivariate Data analysis       *
00006  * Package: TMVA                                                                  *
00007  * Classes: Node                                                                  *
00008  * Web    : http://tmva.sourceforge.net                                           *
00009  *                                                                                *
00010  * Description:                                                                   *
00011  *      Implementation (see header file for description)                          *
00012  *                                                                                *
00013  * Authors (alphabetical):                                                        *
00014  *      Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland              *
00015  *      Helge Voss      <Helge.Voss@cern.ch>     - MPI-K Heidelberg, Germany      *
00016  *      Kai Voss        <Kai.Voss@cern.ch>       - U. of Victoria, Canada         *
00017  *                                                                                *
00018  * CopyRight (c) 2005:                                                            *
00019  *      CERN, Switzerland                                                         * 
00020  *      U. of Victoria, Canada                                                    * 
00021  *      MPI-K Heidelberg, Germany                                                 * 
00022  *                                                                                *
00023  * Redistribution and use in source and binary forms, with or without             *
00024  * modification, are permitted according to the terms listed in LICENSE           *
00025  * (http://tmva.sourceforge.net/LICENSE)                                          *
00026  **********************************************************************************/
00027 
00028 //_______________________________________________________________________
00029 /*
00030   Node for the BinarySearch or Decision Trees.
00031   
00032   For the binary search tree, it basically consists of the EVENT, and
00033   pointers to the parent and daughters
00034 
00035   In case of the Decision Tree, it specifies parent and daughters, as
00036   well as "which variable is used" in the selection of this node,
00037   including the respective cut value.
00038 */
00039 //______________________________________________________________________
00040 
00041 #include <stdexcept>
00042 #include <iosfwd>
00043 #include <iostream>
00044 
00045 #include "TMVA/Node.h"
00046 #include "TMVA/Tools.h"
00047 
00048 ClassImp(TMVA::Node)
00049 
00050 Int_t TMVA::Node::fgCount = 0;
00051 
00052 TMVA::Node::Node() 
00053    : fParent( NULL ),
00054      fLeft  ( NULL),
00055      fRight ( NULL ),
00056      fPos   ( 'u' ),
00057      fDepth ( 0 ),
00058      fParentTree( NULL )
00059 {
00060    // default constructor
00061    fgCount++;
00062 }
00063 
00064 //_______________________________________________________________________
00065 TMVA::Node::Node( Node* p, char pos ) 
00066    : fParent ( p ), 
00067      fLeft ( NULL ), 
00068      fRight( NULL ),  
00069      fPos  ( pos ), 
00070      fDepth( p->GetDepth() + 1), 
00071      fParentTree(p->GetParentTree()) 
00072 {
00073    // constructor of a daughter node as a daughter of 'p'
00074 
00075    fgCount++;
00076    if (fPos == 'l' ) p->SetLeft(this);
00077    else if (fPos == 'r' ) p->SetRight(this);
00078 }
00079 
00080 //_______________________________________________________________________
00081 TMVA::Node::Node ( const Node &n ) 
00082    : fParent( NULL ), 
00083      fLeft  ( NULL), 
00084      fRight ( NULL ), 
00085      fPos   ( n.fPos ), 
00086      fDepth ( n.fDepth ), 
00087      fParentTree( NULL )
00088 {
00089    // copy constructor, make sure you don't just copy the poiter to the node, but
00090    // that the parents/daugthers are initialized to 0 (and set by the copy 
00091    // constructors of the derived classes 
00092    fgCount++;
00093 }
00094 
00095 //_______________________________________________________________________
00096 TMVA::Node::~Node()
00097 {
00098    // node destructor
00099    fgCount--;
00100 }
00101 
00102 //_______________________________________________________________________
00103 int TMVA::Node::GetCount()
00104 {
00105    // retuns the global number of instantiated nodes
00106    return fgCount;
00107 }
00108 
00109 //_______________________________________________________________________
00110 Int_t TMVA::Node::CountMeAndAllDaughters() const 
00111 {
00112    //recursively go through the part of the tree below this node and count all daughters
00113    Int_t n=1;
00114    if (this->GetLeft() != NULL) 
00115       n+= this->GetLeft()->CountMeAndAllDaughters(); 
00116    if (this->GetRight() != NULL) 
00117       n+= this->GetRight()->CountMeAndAllDaughters(); 
00118   
00119    return n;
00120 }
00121 
00122 // print a node
00123 //_______________________________________________________________________
00124 ostream& TMVA::operator<<( ostream& os, const TMVA::Node& node )
00125 { 
00126    // output operator for a node  
00127    node.Print(os);
00128    return os;                // Return the output stream.
00129 }
00130 
00131 //_______________________________________________________________________
00132 ostream& TMVA::operator<<( ostream& os, const TMVA::Node* node )
00133 { 
00134    // output operator with a pointer to the node (which still prints the node itself)
00135    if (node!=NULL) node->Print(os);
00136    return os;                // Return the output stream.
00137 }
00138 
00139 //_______________________________________________________________________
00140 void* TMVA::Node::AddXMLTo( void* parent ) const
00141 {
00142    // add attributes to XML
00143    std::stringstream s("");
00144    AddContentToNode(s);
00145    void* node = gTools().AddChild(parent, "Node", s.str().c_str());
00146    gTools().AddAttr( node, "pos",   fPos );
00147    gTools().AddAttr( node, "depth", fDepth );
00148    this->AddAttributesToNode(node);
00149    if (this->GetLeft())  this->GetLeft()->AddXMLTo(node);
00150    if (this->GetRight()) this->GetRight()->AddXMLTo(node);
00151    return node;
00152 }
00153 
00154 //_______________________________________________________________________
00155 void TMVA::Node::ReadXML( void* node,  UInt_t tmva_Version_Code )
00156 {
00157    // read attributes from XML
00158    ReadAttributes(node, tmva_Version_Code);
00159    const char* content = gTools().GetContent(node);
00160    if (content) {
00161       std::stringstream s(content);
00162       ReadContent(s);
00163    }
00164    gTools().ReadAttr( node, "pos",   fPos );
00165    gTools().ReadAttr( node, "depth", fDepth );
00166 
00167    void* ch = gTools().GetChild(node);
00168    while (ch) {
00169       Node* n = CreateNode();
00170       n->ReadXML(ch, tmva_Version_Code);
00171       if (n->GetPos()=='l')     { this->SetLeft(n);  }
00172       else if(n->GetPos()=='r') { this->SetRight(n); }
00173       else { 
00174          std::cout << "neither left nor right" << std::endl;
00175       }
00176       ch = gTools().GetNextChild(ch);
00177    }
00178 }

Generated on Tue Jul 5 15:25:12 2011 for ROOT_528-00b_version by  doxygen 1.5.1