BinarySearchTree.h

Go to the documentation of this file.
00001 // @(#)root/tmva $Id: BinarySearchTree.h 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  * Class  : BinarySearchTree                                                      *
00008  * Web    : http://tmva.sourceforge.net                                           *
00009  *                                                                                *
00010  * Description:                                                                   *
00011  *      BinarySearchTree incl. volume Search method                               *
00012  *                                                                                *
00013  * Authors (alphabetical):                                                        *
00014  *      Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland              *
00015  *      Joerg Stelzer   <Joerg.Stelzer@cern.ch>  - CERN, Switzerland              *
00016  *      Helge Voss      <Helge.Voss@cern.ch>     - MPI-K Heidelberg, Germany      *
00017  *      Kai Voss        <Kai.Voss@cern.ch>       - U. of Victoria, Canada         *
00018  *                                                                                *
00019  * Copyright (c) 2005:                                                            *
00020  *      CERN, Switzerland                                                         * 
00021  *      U. of Victoria, Canada                                                    * 
00022  *      MPI-K Heidelberg, Germany                                                 * 
00023  *      LAPP, Annecy, France                                                      *
00024  *                                                                                *
00025  * Redistribution and use in source and binary forms, with or without             *
00026  * modification, are permitted according to the terms listed in LICENSE           *
00027  * (http://tmva.sourceforge.net/LICENSE)                                          *
00028  **********************************************************************************/
00029 
00030 #ifndef ROOT_TMVA_BinarySearchTree
00031 #define ROOT_TMVA_BinarySearchTree
00032 
00033 //////////////////////////////////////////////////////////////////////////
00034 //                                                                      //
00035 // BinarySearchTree                                                     //
00036 //                                                                      //
00037 // A simple Binary search tree including volume search method           //
00038 //                                                                      //
00039 //////////////////////////////////////////////////////////////////////////
00040 
00041 #include <vector>
00042 #include <queue>
00043 #ifndef ROOT_time
00044 #include "time.h"
00045 #endif
00046 
00047 #ifndef ROOT_TMVA_Volume
00048 #include "TMVA/Volume.h"
00049 #endif
00050 #ifndef ROOT_TMVA_BinaryTree
00051 #include "TMVA/BinaryTree.h"
00052 #endif
00053 #ifndef ROOT_TMVA_BinarySearchTreeNode
00054 #include "TMVA/BinarySearchTreeNode.h"
00055 #endif
00056 #ifndef ROOT_TMVA_VariableInfo
00057 #include "TMVA/VariableInfo.h"
00058 #endif
00059 
00060 class TString;
00061 class TTree;
00062 
00063 // -----------------------------------------------------------------------------
00064 // the binary search tree
00065 
00066 namespace TMVA {
00067 
00068    class DataSet;
00069    class Event;
00070    //   class MethodBase;
00071    
00072    class BinarySearchTree : public BinaryTree {
00073       
00074    public:
00075       
00076       // constructor
00077       BinarySearchTree( void );
00078     
00079       // copy constructor
00080       BinarySearchTree (const BinarySearchTree &b);
00081 
00082       // destructor
00083       virtual ~BinarySearchTree( void );
00084     
00085       virtual Node * CreateNode( UInt_t ) const { return new BinarySearchTreeNode(); }
00086       virtual BinaryTree* CreateTree() const { return new BinarySearchTree(); }
00087       static BinarySearchTree* CreateFromXML(void* node, UInt_t tmva_Version_Code = TMVA_VERSION_CODE);
00088       virtual const char* ClassName() const { return "BinarySearchTree"; }
00089 
00090       // Searches for a node with the specified data 
00091       // by calling  the private, recursive, function for searching
00092       BinarySearchTreeNode* Search( Event * event ) const;
00093     
00094       // Adds an item to the tree, 
00095       void Insert( const Event * );
00096     
00097       // get sum of weights of the nodes;
00098       Double_t GetSumOfWeights( void ) const;
00099 
00100       //get sum of weights of the nodes of given type;
00101       Double_t GetSumOfWeights( Int_t theType ) const;
00102     
00103       //set the periode (number of variables)
00104       void SetPeriode( Int_t p )      { fPeriod = p; }
00105 
00106       // return periode (number of variables)
00107       UInt_t  GetPeriode( void ) const { return fPeriod; }
00108 
00109       // counts events (weights) within a given volume 
00110       Double_t SearchVolume( Volume*, std::vector<const TMVA::BinarySearchTreeNode*>* events = 0 );
00111     
00112       // Create the search tree from the event collection 
00113       // using ONLY the variables specified in "theVars"
00114       Double_t Fill( const std::vector<TMVA::Event*>& events, const std::vector<Int_t>& theVars, Int_t theType = -1 );
00115     
00116       // create the search tree from the events in a TTree
00117       // using ALL the variables specified included in the Event
00118       Double_t Fill( const std::vector<TMVA::Event*>& events, Int_t theType = -1 );
00119 
00120       void NormalizeTree ();      
00121       
00122       void CalcStatistics( TMVA::Node* n = 0 );      
00123       void Clear         ( TMVA::Node* n = 0 );
00124 
00125       // access to mean for signal and background for each variable
00126       Float_t Mean(Types::ESBType sb, UInt_t var ) { return fMeans[sb==Types::kSignal?0:1][var]; }
00127 
00128       // access to RMS for signal and background for each variable
00129       Float_t RMS(Types::ESBType sb, UInt_t var ) { return fRMS[sb==Types::kSignal?0:1][var]; }
00130 
00131       // access to Minimum for signal and background for each variable
00132       Float_t Min(Types::ESBType sb, UInt_t var ) { return fMin[sb==Types::kSignal?0:1][var]; }
00133 
00134       // access to Maximum for signal and background for each variable
00135       Float_t Max(Types::ESBType sb, UInt_t var ) { return fMax[sb==Types::kSignal?0:1][var]; }
00136 
00137       Int_t SearchVolumeWithMaxLimit( TMVA::Volume*, std::vector<const TMVA::BinarySearchTreeNode*>* events = 0, Int_t = -1);
00138 
00139       // access to RMS for each variable
00140       Float_t RMS(UInt_t var ) { return fRMS[0][var]; } // attention! class 0 is taken as signal!
00141 
00142       void SetNormalize( Bool_t norm ) { fCanNormalize = norm; }
00143 
00144    private:
00145 
00146       // add a new  node to the tree (as daughter) 
00147       void       Insert( const Event*, Node* );
00148       // recursively search the nodes for Event
00149       BinarySearchTreeNode*      Search( Event*, Node *) const ;
00150     
00151       //check of Event variables lie with the volumde
00152       Bool_t   InVolume    (const std::vector<Float_t>&, Volume* ) const;
00153       //
00154       void     DestroyNode ( BinarySearchTreeNode* );
00155 
00156 
00157       void     NormalizeTree( std::vector< std::pair< Double_t, const TMVA::Event* > >::iterator, 
00158                               std::vector< std::pair< Double_t, const TMVA::Event* > >::iterator, UInt_t );
00159 
00160       // recursive search through daughter nodes in weight counting
00161       Double_t SearchVolume( Node*, Volume*, Int_t, 
00162                              std::vector<const TMVA::BinarySearchTreeNode*>* events );
00163       UInt_t fPeriod;            // periode (number of event variables)
00164       UInt_t fCurrentDepth;      // internal variable, counting the depth of the tree during insertion    
00165       Bool_t fStatisticsIsValid; // flag if last stat calculation is still valid, set to false if new node is insert
00166 
00167       std::vector<Float_t>        fMeans[2];    // mean for signal and background for each variable
00168       std::vector<Float_t>        fRMS[2];      // RMS for signal and background for each variable
00169       std::vector<Float_t>        fMin[2];      // RMS for signal and background for each variable
00170       std::vector<Float_t>        fMax[2];      // RMS for signal and background for each variable
00171       std::vector<Double_t>       fSum[2];      // Sum for signal and background for each variable
00172       std::vector<Double_t>       fSumSq[2];    // Squared Sum for signal and background for each variable
00173       Double_t                    fNEventsW[2]; // Number of events per class, taking into account event weights
00174       Double_t                    fSumOfWeights;// Total number of events (weigthed) counted during filling
00175                                                 // should be the same as fNEventsW[0]+fNEventsW[1].. used as a check
00176       
00177       Bool_t                      fCanNormalize; // the tree can be normalised
00178       std::vector< std::pair<Double_t,const TMVA::Event*> > fNormalizeTreeTable;
00179       
00180       ClassDef(BinarySearchTree,0) // Binary search tree including volume search method  
00181    };
00182   
00183 } // namespace TMVA
00184 
00185 #endif

Generated on Tue Jul 5 14:27:18 2011 for ROOT_528-00b_version by  doxygen 1.5.1