Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:10:57

0001 // @(#)root/tmva $Id$
0002 // Author: Andreas Hoecker, Joerg Stelzer, Helge Voss, Kai Voss, Eckhard von Toerne
0003 
0004 /**********************************************************************************
0005  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis       *
0006  * Package: TMVA                                                                  *
0007  * Class  : BinaryTree                                                            *
0008  *                                             *
0009  *                                                                                *
0010  * Description:                                                                   *
0011  *      BinaryTree: A base class for BinarySearch- or Decision-Trees              *
0012  *                                                                                *
0013  * Authors (alphabetical):                                                        *
0014  *      Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland              *
0015  *      Helge Voss      <Helge.Voss@cern.ch>     - MPI-K Heidelberg, Germany      *
0016  *      Kai Voss        <Kai.Voss@cern.ch>       - U. of Victoria, Canada         *
0017  *      Eckhard v. Toerne  <evt@uni-bonn.de>     - U of Bonn, Germany             *
0018  *                                                                                *
0019  * Copyright (c) 2005-2011:                                                       *
0020  *      CERN, Switzerland                                                         *
0021  *      U. of Victoria, Canada                                                    *
0022  *      MPI-K Heidelberg, Germany                                                 *
0023  *      U. of Bonn, Germany                                                       *
0024  *                                                                                *
0025  * Redistribution and use in source and binary forms, with or without             *
0026  * modification, are permitted according to the terms listed in LICENSE           *
0027  * (see tmva/doc/LICENSE)                                          *
0028  **********************************************************************************/
0029 
0030 #ifndef ROOT_TMVA_BinaryTree
0031 #define ROOT_TMVA_BinaryTree
0032 
0033 #include "TMVA/Version.h"
0034 
0035 //////////////////////////////////////////////////////////////////////////
0036 //                                                                      //
0037 // BinaryTree                                                           //
0038 //                                                                      //
0039 // Base class for BinarySearch and Decision Trees                       //
0040 //                                                                      //
0041 //////////////////////////////////////////////////////////////////////////
0042 
0043 #include <iosfwd>
0044 #include "TROOT.h"
0045 
0046 #include "TMVA/Node.h"
0047 
0048 // -----------------------------------------------------------------------------
0049 
0050 // the actual tree class
0051 // Handles allocation, deallocation, and sorting of nodes.
0052 // the Tree consists of a "root-node" wich might have  0 to 2 daughter nodes
0053 
0054 namespace TMVA {
0055 
0056    class BinaryTree;
0057    class MsgLogger;
0058 
0059    std::ostream& operator<< ( std::ostream& os, const BinaryTree& tree );
0060    std::istream& operator>> ( std::istream& istr,     BinaryTree& tree );
0061 
0062    class BinaryTree {
0063 
0064       friend std::ostream& operator<< ( std::ostream& os, const BinaryTree& tree );
0065       friend std::istream& operator>> ( std::istream& istr,     BinaryTree& tree );
0066 
0067    public:
0068 
0069       // or a tree with Root node "n", any daughters of this node are automatically in the tree
0070       BinaryTree( void );
0071 
0072       virtual ~BinaryTree();
0073 
0074       virtual Node* CreateNode(UInt_t size=0) const = 0;
0075       virtual BinaryTree* CreateTree() const = 0;
0076       //      virtual BinaryTree* CreateFromXML(void* node, UInt_t tmva_Version_Code = TMVA_VERSION_CODE) = 0;
0077       virtual const char* ClassName() const = 0;
0078 
0079       // set the root node of the tree
0080       void SetRoot( Node* r ) { fRoot = r; }
0081 
0082       // Retrieves the address of the root node
0083       virtual Node* GetRoot() const { return fRoot; }
0084 
0085       // get number of Nodes in the Tree as counted while booking the nodes;
0086       UInt_t GetNNodes() const { return fNNodes; }
0087 
0088       // count the number of Nodes in the Tree by looping through the tree and updates
0089       // the stored number. (e.g. useful when pruning, as the number count is updated when
0090       // building the tree.
0091       UInt_t CountNodes( Node* n = nullptr );
0092 
0093       UInt_t GetTotalTreeDepth() const { return fDepth; }
0094 
0095       void SetTotalTreeDepth( Int_t depth ) { fDepth = depth; }
0096       void SetTotalTreeDepth( Node* n = nullptr );
0097 
0098       Node* GetLeftDaughter ( Node* n);
0099       Node* GetRightDaughter( Node* n);
0100 
0101       virtual void Print( std::ostream& os ) const;
0102       virtual void Read ( std::istream& istr, UInt_t tmva_Version_Code = TMVA_VERSION_CODE );
0103       virtual void* AddXMLTo(void* parent) const;
0104       virtual void  ReadXML(void* node, UInt_t tmva_Version_Code = TMVA_VERSION_CODE );
0105 
0106    private:
0107 
0108 
0109    protected:
0110       Node*      fRoot; ///< the root node of the tree
0111                         ///< the tree only has it's root node, the "daughters" are taken care
0112                         ///< of by the "node" properties of the "root"
0113 
0114       // delete a node (and the corresponding event if owned by the tree)
0115       void       DeleteNode( Node* );
0116 
0117       UInt_t     fNNodes;           ///< total number of nodes in the tree (counted)
0118       UInt_t     fDepth;            ///< maximal depth in tree reached
0119 
0120       MsgLogger& Log() const;
0121 
0122       ClassDef(BinaryTree,0); // Base class for BinarySearch and Decision Trees
0123    };
0124 
0125 } // namespace TMVA
0126 
0127 #endif
0128