#include "TMatrixDBase.h"
#include "TObjString.h"
#include "TTree.h"
#include "Riostream.h"
#include <stdexcept>
#ifndef ROOT_TMVA_Tools
#include "TMVA/Tools.h"
#endif
#ifndef ROOT_TMVA_DataSet
#include "TMVA/DataSet.h"
#endif
#ifndef ROOT_TMVA_Event
#include "TMVA/Event.h"
#endif
#ifndef ROOT_TMVA_BinarySearchTree
#include "TMVA/BinarySearchTree.h"
#endif
ClassImp(TMVA::BinarySearchTree)
;
using std::vector;
TMVA::BinarySearchTree::BinarySearchTree( void ) :
TMVA::BinaryTree(),
fSumOfWeights( 0 ),
fPeriode ( 1 ),
fCurrentDepth( 0 )
{
fLogger.SetSource( "BinarySearchTree" );
}
TMVA::BinarySearchTree::BinarySearchTree( const BinarySearchTree &b)
: TMVA::BinaryTree(),
fSumOfWeights( b.fSumOfWeights ),
fPeriode ( b.fPeriode ),
fCurrentDepth( 0 )
{
fLogger.SetSource( "BinarySearchTree" );
fLogger << kFATAL << " Copy constructor not implemented yet " << Endl;
}
TMVA::BinarySearchTree::~BinarySearchTree( void )
{
}
void TMVA::BinarySearchTree::Insert( TMVA::Event* event, Bool_t eventOwnership )
{
fCurrentDepth=0;
if (this->GetRoot() == NULL) {
this->SetRoot( new TMVA::BinarySearchTreeNode(event, eventOwnership));
this->GetRoot()->SetPos('s');
this->GetRoot()->SetDepth(0);
fNNodes++;
fSumOfWeights+=event->GetWeight();
((BinarySearchTreeNode*)this->GetRoot())->SetSelector((UInt_t)0);
this->SetPeriode(event->GetNVars());
}
else {
if (event->GetNVars() != (UInt_t)this->GetPeriode()) {
fLogger << kFATAL << "<Insert> event vector length != Periode specified in Binary Tree" << Endl
<< "--- event size: " << event->GetNVars() << " Periode: " << this->GetPeriode() << Endl
<< "--- and all this when trying filling the "<<fNNodes+1<<"th Node" << Endl;
}
this->Insert(event, this->GetRoot(), eventOwnership);
}
}
void TMVA::BinarySearchTree::Insert( TMVA::Event *event,
TMVA::Node *node, Bool_t eventOwnership )
{
fCurrentDepth++;
if (node->GoesLeft(*event)){
if (node->GetLeft() != NULL){
this->Insert(event, node->GetLeft(), eventOwnership);
}
else {
TMVA::BinarySearchTreeNode* current =
new TMVA::BinarySearchTreeNode(event, eventOwnership);
fNNodes++;
fSumOfWeights+=event->GetWeight();
current->SetSelector(fCurrentDepth%((Int_t)event->GetNVars()));
current->SetParent(node);
current->SetPos('l');
current->SetDepth( node->GetDepth() + 1 );
node->SetLeft(current);
}
}
else if (node->GoesRight(*event)) {
if (node->GetRight() != NULL) {
this->Insert(event, node->GetRight(), eventOwnership);
}
else {
TMVA::BinarySearchTreeNode* current =
new TMVA::BinarySearchTreeNode(event, eventOwnership);
fNNodes++;
fSumOfWeights+=event->GetWeight();
current->SetSelector(fCurrentDepth%((Int_t)event->GetNVars()));
current->SetParent(node);
current->SetPos('r');
current->SetDepth( node->GetDepth() + 1 );
node->SetRight(current);
}
}
else fLogger << kFATAL << "<Insert> neither left nor right :)" << Endl;
}
TMVA::BinarySearchTreeNode* TMVA::BinarySearchTree::Search( TMVA::Event* event ) const
{
return this->Search( event, this->GetRoot() );
}
TMVA::BinarySearchTreeNode* TMVA::BinarySearchTree::Search(TMVA::Event* event, TMVA::Node* node) const
{
if (node != NULL) {
if (((BinarySearchTreeNode*)(node))->EqualsMe(*event))
return (BinarySearchTreeNode*)node;
if (node->GoesLeft(*event))
return this->Search(event, node->GetLeft());
else
return this->Search(event, node->GetRight());
}
else return NULL;
}
Double_t TMVA::BinarySearchTree::GetSumOfWeights( void ) const
{
return fSumOfWeights;
}
Int_t TMVA::BinarySearchTree::Fill( const DataSet& ds, TTree* theTree, Int_t theType,
Types::EPreprocessingMethod corr, Types::ESBType type )
{
Int_t nevents=0;
fPeriode = ds.GetNVariables();
Int_t n=theTree->GetEntries();
for (Int_t ievt=0; ievt<n; ievt++) {
ds.ReadEvent(theTree,ievt,corr,type);
if (theType==-1 || ds.Event().Type()==theType) {
this->Insert( new TMVA::Event(ds.Event()), kTRUE );
nevents++;
}
}
if (nevents <= 0) {
fLogger << kFATAL << "<Fill> number of events in tree is zero: " << nevents << Endl;
}
return nevents;
}
Int_t TMVA::BinarySearchTree::Fill( vector<TMVA::Event*> theTree, vector<Int_t> theVars,
Int_t theType )
{
fPeriode = (theVars).size();
Int_t nevents = 0;
Int_t n=theTree.size();
for (Int_t ievt=0; ievt<n; ievt++) {
if (theType == -1 || theTree[ievt]->Type() == theType) {
TMVA::Event *e = new TMVA::Event(*theTree[ievt]);
this->Insert( e , kTRUE);
nevents++;
}
}
if (nevents <= 0) {
fLogger << kFATAL << "<Fill> number of events "
<< "that got filled into the tree is <= zero: " << nevents << Endl;
}
return nevents;
}
Int_t TMVA::BinarySearchTree::Fill( vector<TMVA::Event*> theTree, Int_t theType )
{
Int_t n=theTree.size();
Int_t nevents = 0;
for (Int_t ievt=0; ievt<n; ievt++) {
if (theType == -1 || theTree[ievt]->Type() == theType) {
this->Insert( theTree[ievt] , kFALSE);
nevents++;
}
}
return nevents;
}
Double_t TMVA::BinarySearchTree::SearchVolume( TMVA::Volume* volume,
std::vector<TMVA::Event*>* events )
{
return SearchVolume( this->GetRoot(), volume, 0, events );
}
Double_t TMVA::BinarySearchTree::SearchVolume( TMVA::Node* t, TMVA::Volume* volume, Int_t depth,
std::vector<TMVA::Event*>* events )
{
BinarySearchTreeNode* st = (BinarySearchTreeNode*)t;
if (st==NULL) return 0;
Double_t count = 0.0;
if (InVolume( st->GetEvent(), volume )) {
count += st->GetEvent()->GetWeight();
if (NULL != events) events->push_back( st->GetEvent() );
}
if (st->GetLeft()==NULL && st->GetRight()==NULL) return count;
Bool_t tl, tr;
Int_t d = depth%this->GetPeriode();
if (d != st->GetSelector()) {
fLogger << kFATAL << "<SearchVolume> selector in Searchvolume "
<< d << " != " << "node "<< st->GetSelector() << Endl;
}
tl = (*(volume->fLower))[d] < (st->GetEvent()->GetVal(d));
tr = (*(volume->fUpper))[d] >= (st->GetEvent()->GetVal(d));
if (tl) count += SearchVolume( st->GetLeft(), volume, (depth+1), events );
if (tr) count += SearchVolume( st->GetRight(), volume, (depth+1), events );
return count;
}
Bool_t TMVA::BinarySearchTree::InVolume( TMVA::Event* event, TMVA::Volume* volume ) const
{
Bool_t result = false;
for (UInt_t ivar=0; ivar< fPeriode; ivar++) {
result = ( (*(volume->fLower))[ivar] < ((event->GetVal(ivar))) &&
(*(volume->fUpper))[ivar] >= ((event->GetVal(ivar))) );
if (!result) break;
}
return result;
}
ROOT page - Class index - Class Hierarchy - Top of the page
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.