58 #ifndef ROOT_TMVA_MsgLogger
61 #ifndef ROOT_TMVA_MethodBase
64 #ifndef ROOT_TMVA_Tools
67 #ifndef ROOT_TMVA_Event
70 #ifndef ROOT_TMVA_BinarySearchTree
82 TMVA::BinarySearchTree::BinarySearchTree(
void ) :
86 fStatisticsIsValid(
kFALSE ),
88 fCanNormalize( kFALSE )
90 fNEventsW[0]=fNEventsW[1]=0.;
98 fPeriod ( b.fPeriod ),
100 fStatisticsIsValid(
kFALSE ),
101 fSumOfWeights( b.fSumOfWeights ),
105 Log() <<
kFATAL <<
" Copy constructor not implemented yet " <<
Endl;
113 for(std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator pIt = fNormalizeTreeTable.begin();
114 pIt != fNormalizeTreeTable.end(); pIt++) {
123 std::string
type(
"");
126 bt->
ReadXML( node, tmva_Version_Code );
136 fStatisticsIsValid =
kFALSE;
138 if (this->GetRoot() ==
NULL) {
141 this->GetRoot()->SetPos(
's');
142 this->GetRoot()->SetDepth(0);
144 fSumOfWeights =
event->GetWeight();
151 Log() <<
kFATAL <<
"<Insert> event vector length != Periode specified in Binary Tree" <<
Endl
152 <<
"--- event size: " <<
event->GetNVariables() <<
" Periode: " << this->GetPeriode() <<
Endl
153 <<
"--- and all this when trying filling the "<<fNNodes+1<<
"th Node" <<
Endl;
156 this->Insert(event, this->GetRoot());
160 if (fCanNormalize) fNormalizeTreeTable.push_back( std::make_pair(0.0,
new const Event(*event)) );
170 fStatisticsIsValid =
kFALSE;
175 this->Insert(event, node->
GetLeft());
181 fSumOfWeights +=
event->GetWeight();
192 this->Insert(event, node->
GetRight());
198 fSumOfWeights +=
event->GetWeight();
206 else Log() <<
kFATAL <<
"<Insert> neither left nor right :)" <<
Endl;
214 return this->Search( event, this->GetRoot() );
227 return this->Search(event, node->
GetLeft());
229 return this->Search(event, node->
GetRight());
239 if (fSumOfWeights <= 0) {
240 Log() <<
kWARNING <<
"you asked for the SumOfWeights, which is not filled yet"
241 <<
" I call CalcStatistics which hopefully fixes things"
244 if (fSumOfWeights <= 0)
Log() <<
kFATAL <<
" Zero events in your Search Tree" <<
Endl;
246 return fSumOfWeights;
254 if (fSumOfWeights <= 0) {
255 Log() <<
kWARNING <<
"you asked for the SumOfWeights, which is not filled yet"
256 <<
" I call CalcStatistics which hopefully fixes things"
259 if (fSumOfWeights <= 0)
Log() <<
kFATAL <<
" Zero events in your Search Tree" <<
Endl;
271 fPeriod = theVars.size();
272 return Fill(events, theType);
284 if (fSumOfWeights != 0) {
286 <<
"You are filling a search three that is not empty.. "
287 <<
" do you know what you are doing?"
290 for (
UInt_t ievt=0; ievt<
n; ievt++) {
292 if (theType == -1 || (
Int_t(events[ievt]->
GetClass()) == theType) ) {
293 this->Insert( events[ievt] );
295 fSumOfWeights += events[ievt]->GetWeight();
300 return fSumOfWeights;
306 std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator rightBound,
311 if (leftBound == rightBound)
return;
313 if (actDim == fPeriod) actDim = 0;
314 for (std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator i=leftBound; i!=rightBound; i++) {
315 i->first = i->second->GetValue( actDim );
318 std::sort( leftBound, rightBound );
320 std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator leftTemp = leftBound;
321 std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator rightTemp = rightBound;
326 if (rightTemp == leftTemp ) {
330 if (leftTemp == rightTemp) {
335 std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator mid = leftTemp;
336 std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator midTemp = mid;
338 if (mid!=leftBound) midTemp--;
340 while (mid != leftBound && mid->second->GetValue( actDim ) == midTemp->second->GetValue( actDim )) {
345 Insert( mid->second );
350 NormalizeTree( leftBound, mid, actDim+1 );
354 NormalizeTree( mid, rightBound, actDim+1 );
368 NormalizeTree( fNormalizeTreeTable.begin(), fNormalizeTreeTable.end(), 0 );
381 if (n !=
NULL)
delete n;
391 std::vector<const BinarySearchTreeNode*>* events )
393 return SearchVolume( this->GetRoot(), volume, 0, events );
401 std::vector<const BinarySearchTreeNode*>* events )
403 if (t==
NULL)
return 0;
408 if (InVolume( st->
GetEventV(), volume )) {
410 if (
NULL != events) events->push_back( st );
418 Int_t d = depth%this->GetPeriode();
420 Log() <<
kFATAL <<
"<SearchVolume> selector in Searchvolume "
426 if (tl) count += SearchVolume( st->
GetLeft(), volume, (depth+1), events );
427 if (tr) count += SearchVolume( st->
GetRight(), volume, (depth+1), events );
437 for (
UInt_t ivar=0; ivar< fPeriod; ivar++) {
438 result = ( (*(volume->
fLower))[ivar] <
event[ivar] &&
439 (*(volume->
fUpper))[ivar] >=
event[ivar] );
450 if (fStatisticsIsValid)
return;
457 for (
Int_t sb=0; sb<2; sb++) {
459 fMeans[sb] = std::vector<Float_t>(fPeriod);
460 fRMS[sb] = std::vector<Float_t>(fPeriod);
461 fMin[sb] = std::vector<Float_t>(fPeriod);
462 fMax[sb] = std::vector<Float_t>(fPeriod);
463 fSum[sb] = std::vector<Double_t>(fPeriod);
464 fSumSq[sb] = std::vector<Double_t>(fPeriod);
465 for (
UInt_t j=0; j<fPeriod; j++) {
466 fMeans[sb][j] = fRMS[sb][j] = fSum[sb][j] = fSumSq[sb][j] = 0;
467 fMin[sb][j] = FLT_MAX;
468 fMax[sb][j] = -FLT_MAX;
472 if (currentNode ==
NULL)
return;
475 const std::vector<Float_t> & evtVec = currentNode->
GetEventV();
481 fNEventsW[
type] += weight;
482 fSumOfWeights += weight;
484 for (
UInt_t j=0; j<fPeriod; j++) {
486 fSum[
type][j] += val*weight;
487 fSumSq[
type][j] += val*val*weight;
488 if (val < fMin[type][j]) fMin[
type][j] = val;
489 if (val > fMax[type][j]) fMax[
type][j] = val;
496 for (
Int_t sb=0; sb<2; sb++) {
497 for (
UInt_t j=0; j<fPeriod; j++) {
498 if (fNEventsW[sb] == 0) { fMeans[sb][j] = fRMS[sb][j] = 0;
continue; }
499 fMeans[sb][j] = fSum[sb][j]/fNEventsW[sb];
500 fRMS[sb][j] =
TMath::Sqrt(fSumSq[sb][j]/fNEventsW[sb] - fMeans[sb][j]*fMeans[sb][j]);
503 fStatisticsIsValid =
kTRUE;
514 if (this->GetRoot() ==
NULL)
return 0;
516 std::queue< std::pair< const BinarySearchTreeNode*, Int_t > > queue;
517 std::pair< const BinarySearchTreeNode*, Int_t > st = std::make_pair( (
const BinarySearchTreeNode*)this->GetRoot(), 0 );
522 while ( !queue.empty() ) {
523 st = queue.front(); queue.pop();
525 if (count == max_points)
528 if (InVolume( st.first->GetEventV(), volume )) {
530 if (
NULL != events) events->push_back( st.first );
535 if ( d ==
Int_t(this->GetPeriode()) ) d = 0;
537 if (d != st.first->GetSelector()) {
538 Log() <<
kFATAL <<
"<SearchVolume> selector in Searchvolume "
539 << d <<
" != " <<
"node "<< st.first->GetSelector() <<
Endl;
542 tl = (*(volume->
fLower))[
d] < st.first->GetEventV()[
d] && st.first->GetLeft() !=
NULL;
543 tr = (*(volume->
fUpper))[
d] >= st.first->GetEventV()[
d] && st.first->GetRight() !=
NULL;
std::vector< Double_t > * fLower
MsgLogger & Endl(MsgLogger &ml)
virtual ~BinarySearchTree(void)
destructor
Bool_t InVolume(const std::vector< Float_t > &, Volume *) const
std::vector< Double_t > * fUpper
Int_t SearchVolumeWithMaxLimit(TMVA::Volume *, std::vector< const TMVA::BinarySearchTreeNode * > *events=0, Int_t=-1)
virtual void SetRight(Node *r)
void NormalizeTree()
Normalisation of tree.
virtual Bool_t GoesLeft(const Event &) const =0
virtual void SetLeft(Node *l)
void SetSelector(Short_t i)
void Clear(TMVA::Node *n=0)
clear nodes
ClassImp(TMVA::BinarySearchTree) TMVA
default constructor
Double_t SearchVolume(Volume *, std::vector< const TMVA::BinarySearchTreeNode * > *events=0)
search the whole tree and add up all weigths of events that lie within the given voluem ...
UInt_t GetNVariables() const
accessor to the number of variables
Double_t GetSumOfWeights(void) const
return the sum of event (node) weights
void CalcStatistics(TMVA::Node *n=0)
calculate basic statistics (mean, rms for each variable)
static BinarySearchTree * CreateFromXML(void *node, UInt_t tmva_Version_Code=TMVA_VERSION_CODE)
re-create a new tree (decision tree or search tree) from XML
void Insert(const Event *)
insert a new "event" in the binary tree
virtual void ReadXML(void *node, UInt_t tmva_Version_Code=TMVA_VERSION_CODE)
read attributes from XML
virtual void SetParent(Node *p)
Float_t GetWeight() const
Short_t GetSelector() const
virtual Node * GetRight() const
virtual Bool_t GoesRight(const Event &) const =0
Double_t Fill(const std::vector< TMVA::Event * > &events, const std::vector< Int_t > &theVars, Int_t theType=-1)
create the search tree from the event collection using ONLY the variables specified in "theVars" ...
const std::vector< Float_t > & GetEventV() const
Double_t Sqrt(Double_t x)
BinarySearchTreeNode * Search(Event *event) const
search the tree to find the node matching "event"
virtual Node * GetLeft() const