BinarySearchTreeNode.cxx

Go to the documentation of this file.
00001 // @(#)root/tmva $Id: BinarySearchTreeNode.cxx 36170 2010-10-07 16:43:07Z stelzer $    
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, including
00037 // the respective cut value.
00038 //______________________________________________________________________
00039 
00040 #include <stdexcept>
00041 #include <iomanip>
00042 #include <assert.h>
00043 #include <cstdlib>
00044 
00045 #include "TMVA/BinarySearchTreeNode.h"
00046 #include "TMVA/Event.h"
00047 #include "TMVA/MsgLogger.h"
00048 #include "TMVA/Tools.h"
00049 
00050 ClassImp(TMVA::BinarySearchTreeNode)
00051 
00052 //_______________________________________________________________________
00053 TMVA::BinarySearchTreeNode::BinarySearchTreeNode( const Event* e ) 
00054    : TMVA::Node(),
00055      fEventV  ( std::vector<Float_t>() ),
00056      fTargets ( std::vector<Float_t>() ),
00057      fWeight  ( e==0?0:e->GetWeight()  ),
00058      fClass   ( e==0?0:e->GetClass() ), // see BinarySearchTree.h, line Mean() RMS() Min() and Max()
00059      fSelector( -1 )
00060 {
00061    // constructor of a node for the search tree
00062    if (e!=0) {
00063       for (UInt_t ivar=0; ivar<e->GetNVariables(); ivar++) fEventV.push_back(e->GetValue(ivar));
00064       for (std::vector<Float_t>::const_iterator it = e->GetTargets().begin(); it < e->GetTargets().end(); it++ ) {
00065          fTargets.push_back( (*it) );
00066       }
00067    }
00068 }
00069 
00070 //_______________________________________________________________________
00071 TMVA::BinarySearchTreeNode::BinarySearchTreeNode( BinarySearchTreeNode* parent, char pos ) : 
00072    TMVA::Node(parent,pos),
00073    fEventV  ( std::vector<Float_t>() ),
00074    fTargets ( std::vector<Float_t>() ),
00075    fWeight  ( 0  ),
00076    fClass   ( 0 ),
00077    fSelector( -1 )
00078 {
00079    // constructor of a daughter node as a daughter of 'p'
00080 }
00081 
00082 //_______________________________________________________________________
00083 TMVA::BinarySearchTreeNode::BinarySearchTreeNode ( const BinarySearchTreeNode &n,
00084                                                    BinarySearchTreeNode* parent ) : 
00085    TMVA::Node(n),
00086    fEventV  ( n.fEventV   ),
00087    fTargets ( n.fTargets  ),
00088    fWeight  ( n.fWeight   ),
00089    fClass   ( n.fClass    ),
00090    fSelector( n.fSelector )
00091 {
00092    // copy constructor of a node. It will result in an explicit copy of
00093    // the node and recursively all it's daughters
00094    this->SetParent( parent );
00095    if (n.GetLeft() == 0 ) this->SetLeft(NULL);
00096    else this->SetLeft( new BinarySearchTreeNode( *((BinarySearchTreeNode*)(n.GetLeft())),this));
00097    
00098    if (n.GetRight() == 0 ) this->SetRight(NULL);
00099    else this->SetRight( new BinarySearchTreeNode( *((BinarySearchTreeNode*)(n.GetRight())),this));
00100 
00101 }
00102 
00103 //_______________________________________________________________________
00104 TMVA::BinarySearchTreeNode::~BinarySearchTreeNode()
00105 {
00106    // node destructor
00107 }
00108 
00109 //_______________________________________________________________________
00110 Bool_t TMVA::BinarySearchTreeNode::GoesRight( const TMVA::Event& e ) const 
00111 {
00112    // check if the event fed into the node goes/decends to the right daughter
00113    if (e.GetValue(fSelector) > GetEventV()[fSelector]) return true;
00114    else return false;
00115 }
00116 
00117 //_______________________________________________________________________
00118 Bool_t TMVA::BinarySearchTreeNode::GoesLeft(const TMVA::Event& e) const
00119 {
00120    // check if the event fed into the node goes/decends to the left daughter
00121    if (e.GetValue(fSelector) <= GetEventV()[fSelector]) return true;
00122    else return false;
00123 }
00124 
00125 //_______________________________________________________________________
00126 Bool_t TMVA::BinarySearchTreeNode::EqualsMe(const TMVA::Event& e) const
00127 {
00128    // check if the event fed into the node actually equals the event
00129    // that forms the node (in case of a search tree)
00130    Bool_t result = true;
00131    for (UInt_t i=0; i<GetEventV().size(); i++) {
00132       result&= (e.GetValue(i) == GetEventV()[i]);
00133    }
00134    return result;
00135 }
00136 
00137 //_______________________________________________________________________
00138 void TMVA::BinarySearchTreeNode::Print( ostream& os ) const
00139 {
00140    // print the node
00141    os << "< ***  " << std::endl << " node.Data: ";
00142    std::vector<Float_t>::const_iterator it=fEventV.begin();
00143    os << fEventV.size() << " vars: ";
00144    for (;it!=fEventV.end(); it++) os << " " << std::setw(10) << *it;
00145    os << "  EvtWeight " << std::setw(10) << fWeight;
00146    os << std::setw(10) << (IsSignal()?" Signal":" Background") << std::endl;
00147 
00148    os << "Selector: " <<  this->GetSelector() <<std::endl;
00149    os << "My address is " << long(this) << ", ";
00150    if (this->GetParent() != NULL) os << " parent at addr: " << long(this->GetParent()) ;
00151    if (this->GetLeft() != NULL) os << " left daughter at addr: " << long(this->GetLeft());
00152    if (this->GetRight() != NULL) os << " right daughter at addr: " << long(this->GetRight()) ;
00153    
00154    os << " **** > "<< std::endl;
00155 }
00156 
00157 //_______________________________________________________________________
00158 void TMVA::BinarySearchTreeNode::PrintRec( ostream& os ) const
00159 {
00160    // recursively print the node and its daughters (--> print the 'tree')
00161    os << this->GetDepth() << " " << this->GetPos() << " " << this->GetSelector()
00162       << " data: " <<  std::endl;
00163    std::vector<Float_t>::const_iterator it=fEventV.begin();
00164    os << fEventV.size() << " vars: ";
00165    for (;it!=fEventV.end(); it++) os << " " << std::setw(10) << *it;
00166    os << "  EvtWeight " << std::setw(10) << fWeight;
00167    os << std::setw(10) << (IsSignal()?" Signal":" Background") << std::endl;
00168 
00169    if (this->GetLeft() != NULL)this->GetLeft()->PrintRec(os) ;
00170    if (this->GetRight() != NULL)this->GetRight()->PrintRec(os);
00171 }
00172 
00173 //_______________________________________________________________________
00174 Bool_t TMVA::BinarySearchTreeNode::ReadDataRecord( istream& is, UInt_t /* Tmva_Version_Code */  ) 
00175 {
00176    // Read the data block
00177    Int_t       itmp;
00178    std::string tmp;
00179    UInt_t      depth, selIdx, nvar;
00180    Char_t      pos;
00181    TString     sigbkgd;
00182    Float_t     evtValFloat;
00183 
00184    // read depth and position
00185    is >> itmp;
00186    if ( itmp==-1 ) { return kFALSE; } // Done
00187 
00188    depth=(UInt_t)itmp;
00189    is >> pos >> selIdx;
00190    this->SetDepth(depth);     // depth of the tree
00191    this->SetPos(pos);         // either 's' (root node), 'l', or 'r'
00192    this->SetSelector(selIdx);
00193 
00194    // next line: read and build the event
00195    // coverity[tainted_data_argument]
00196    is >> nvar;
00197    fEventV.clear();
00198    for (UInt_t ivar=0; ivar<nvar; ivar++) {
00199       is >> evtValFloat; fEventV.push_back(evtValFloat);
00200    }
00201    is >> tmp >> fWeight;
00202    is >> sigbkgd;
00203    fClass = (sigbkgd=="S" || sigbkgd=="Signal")?0:1;
00204 
00205    return kTRUE;
00206 }
00207 
00208 //_______________________________________________________________________
00209 void TMVA::BinarySearchTreeNode::ReadAttributes(void* node, UInt_t /* tmva_Version_Code */ )
00210 {
00211    // read attributes from XML
00212    gTools().ReadAttr(node, "selector", fSelector );
00213    gTools().ReadAttr(node, "weight",   fWeight );
00214    std::string sb;
00215    gTools().ReadAttr(node, "type",     sb);
00216    fClass = (sb=="Signal")?0:1;
00217    Int_t nvars;
00218    gTools().ReadAttr(node, "NVars",nvars);
00219    fEventV.resize(nvars);
00220 }
00221 
00222 
00223 //_______________________________________________________________________
00224 void TMVA::BinarySearchTreeNode::AddAttributesToNode(void* node) const {
00225    // adding attributes to tree node
00226    gTools().AddAttr(node, "selector", fSelector );
00227    gTools().AddAttr(node, "weight", fWeight );
00228    gTools().AddAttr(node, "type", (IsSignal()?"Signal":"Background"));
00229    gTools().AddAttr(node, "NVars", fEventV.size());
00230 }
00231 
00232 
00233 //_______________________________________________________________________
00234 void TMVA::BinarySearchTreeNode::AddContentToNode( std::stringstream& s ) const 
00235 {
00236    // adding attributes to tree node
00237    std::ios_base::fmtflags ff = s.flags();
00238    s.precision( 16 );
00239    for (UInt_t i=0; i<fEventV.size();  i++) s << std::scientific << " " << fEventV[i];
00240    for (UInt_t i=0; i<fTargets.size(); i++) s << std::scientific << " " << fTargets[i];
00241    s.flags(ff);
00242 }
00243 //_______________________________________________________________________
00244 void TMVA::BinarySearchTreeNode::ReadContent( std::stringstream& s ) 
00245 {
00246    // read events from node
00247    Float_t temp=0;
00248    for (UInt_t i=0; i<fEventV.size(); i++){
00249       s >> temp;
00250       fEventV[i]=temp;
00251    }
00252    while (s >> temp) fTargets.push_back(temp);
00253 }

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