67 fStatisticsIsValid(
kFALSE ),
79 fPeriod (
b.fPeriod ),
81 fStatisticsIsValid(
kFALSE ),
82 fSumOfWeights(
b.fSumOfWeights ),
86 Log() << kFATAL <<
" Copy constructor not implemented yet " <<
Endl;
94 for(std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator pIt = fNormalizeTreeTable.begin();
95 pIt != fNormalizeTreeTable.end(); ++pIt) {
104 std::string
type(
"");
107 bt->
ReadXML( node, tmva_Version_Code );
117 fStatisticsIsValid =
kFALSE;
119 if (this->GetRoot() == NULL) {
122 this->GetRoot()->SetPos(
's');
123 this->GetRoot()->SetDepth(0);
125 fSumOfWeights =
event->GetWeight();
132 Log() << kFATAL <<
"<Insert> event vector length != Periode specified in Binary Tree" <<
Endl
133 <<
"--- event size: " <<
event->GetNVariables() <<
" Periode: " << this->GetPeriode() <<
Endl
134 <<
"--- and all this when trying filling the "<<fNNodes+1<<
"th Node" <<
Endl;
137 this->Insert(event, this->GetRoot());
141 if (fCanNormalize) fNormalizeTreeTable.push_back( std::make_pair(0.0,
new const Event(*event)) );
151 fStatisticsIsValid =
kFALSE;
156 this->Insert(event, node->
GetLeft());
162 fSumOfWeights +=
event->GetWeight();
173 this->Insert(event, node->
GetRight());
179 fSumOfWeights +=
event->GetWeight();
187 else Log() << kFATAL <<
"<Insert> neither left nor right :)" <<
Endl;
195 return this->Search( event, this->GetRoot() );
208 return this->Search(event, node->
GetLeft());
210 return this->Search(event, node->
GetRight());
220 if (fSumOfWeights <= 0) {
221 Log() << kWARNING <<
"you asked for the SumOfWeights, which is not filled yet"
222 <<
" I call CalcStatistics which hopefully fixes things"
225 if (fSumOfWeights <= 0) Log() << kFATAL <<
" Zero events in your Search Tree" <<
Endl;
227 return fSumOfWeights;
235 if (fSumOfWeights <= 0) {
236 Log() << kWARNING <<
"you asked for the SumOfWeights, which is not filled yet"
237 <<
" I call CalcStatistics which hopefully fixes things"
240 if (fSumOfWeights <= 0) Log() << kFATAL <<
" Zero events in your Search Tree" <<
Endl;
252 fPeriod = theVars.size();
253 return Fill(events, theType);
265 if (fSumOfWeights != 0) {
267 <<
"You are filling a search three that is not empty.. "
268 <<
" do you know what you are doing?"
271 for (
UInt_t ievt=0; ievt<
n; ievt++) {
273 if (theType == -1 || (
Int_t(events[ievt]->GetClass()) == theType) ) {
274 this->Insert( events[ievt] );
276 fSumOfWeights += events[ievt]->GetWeight();
281 return fSumOfWeights;
289 std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator rightBound,
293 if (leftBound == rightBound)
return;
295 if (actDim == fPeriod) actDim = 0;
296 for (std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator i=leftBound; i!=rightBound; ++i) {
297 i->first = i->second->GetValue( actDim );
300 std::sort( leftBound, rightBound );
302 std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator leftTemp = leftBound;
303 std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator rightTemp = rightBound;
308 if (rightTemp == leftTemp ) {
312 if (leftTemp == rightTemp) {
317 std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator mid = leftTemp;
318 std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator midTemp = mid;
320 if (mid!=leftBound)--midTemp;
322 while (mid != leftBound && mid->second->GetValue( actDim ) == midTemp->second->GetValue( actDim )) {
327 Insert( mid->second );
332 NormalizeTree( leftBound, mid, actDim+1 );
336 NormalizeTree( mid, rightBound, actDim+1 );
350 NormalizeTree( fNormalizeTreeTable.begin(), fNormalizeTreeTable.end(), 0 );
363 if (
n != NULL)
delete n;
373 std::vector<const BinarySearchTreeNode*>* events )
375 return SearchVolume( this->GetRoot(), volume, 0, events );
383 std::vector<const BinarySearchTreeNode*>* events )
385 if (t==NULL)
return 0;
390 if (InVolume( st->
GetEventV(), volume )) {
392 if (NULL != events) events->push_back( st );
400 Int_t d = depth%this->GetPeriode();
402 Log() << kFATAL <<
"<SearchVolume> selector in Searchvolume "
408 if (tl) count += SearchVolume( st->
GetLeft(), volume, (depth+1), events );
409 if (tr) count += SearchVolume( st->
GetRight(), volume, (depth+1), events );
421 for (
UInt_t ivar=0; ivar< fPeriod; ivar++) {
422 result = ( (*(volume->
fLower))[ivar] <
event[ivar] &&
423 (*(volume->
fUpper))[ivar] >=
event[ivar] );
434 if (fStatisticsIsValid)
return;
441 for (
Int_t sb=0; sb<2; sb++) {
443 fMeans[sb] = std::vector<Float_t>(fPeriod);
444 fRMS[sb] = std::vector<Float_t>(fPeriod);
445 fMin[sb] = std::vector<Float_t>(fPeriod);
446 fMax[sb] = std::vector<Float_t>(fPeriod);
447 fSum[sb] = std::vector<Double_t>(fPeriod);
448 fSumSq[sb] = std::vector<Double_t>(fPeriod);
449 for (
UInt_t j=0; j<fPeriod; j++) {
450 fMeans[sb][j] = fRMS[sb][j] = fSum[sb][j] = fSumSq[sb][j] = 0;
451 fMin[sb][j] = FLT_MAX;
452 fMax[sb][j] = -FLT_MAX;
456 if (currentNode == NULL)
return;
459 const std::vector<Float_t> & evtVec = currentNode->
GetEventV();
465 fNEventsW[
type] += weight;
466 fSumOfWeights += weight;
468 for (
UInt_t j=0; j<fPeriod; j++) {
470 fSum[
type][j] += val*weight;
471 fSumSq[
type][j] += val*val*weight;
472 if (val < fMin[
type][j]) fMin[
type][j] = val;
473 if (val > fMax[
type][j]) fMax[
type][j] = val;
476 if ( (currentNode->
GetLeft() != NULL) ) CalcStatistics( currentNode->
GetLeft() );
477 if ( (currentNode->
GetRight() != NULL) ) CalcStatistics( currentNode->
GetRight() );
480 for (
Int_t sb=0; sb<2; sb++) {
481 for (
UInt_t j=0; j<fPeriod; j++) {
482 if (fNEventsW[sb] == 0) { fMeans[sb][j] = fRMS[sb][j] = 0;
continue; }
483 fMeans[sb][j] = fSum[sb][j]/fNEventsW[sb];
484 fRMS[sb][j] =
TMath::Sqrt(fSumSq[sb][j]/fNEventsW[sb] - fMeans[sb][j]*fMeans[sb][j]);
487 fStatisticsIsValid =
kTRUE;
500 if (this->GetRoot() == NULL)
return 0;
502 std::queue< std::pair< const BinarySearchTreeNode*, Int_t > > queue;
503 std::pair< const BinarySearchTreeNode*, Int_t > st = std::make_pair( (
const BinarySearchTreeNode*)this->GetRoot(), 0 );
508 while ( !queue.empty() ) {
509 st = queue.front(); queue.pop();
511 if (count == max_points)
514 if (InVolume( st.first->GetEventV(), volume )) {
516 if (NULL != events) events->push_back( st.first );
521 if (
d ==
Int_t(this->GetPeriode()) )
d = 0;
523 if (
d != st.first->GetSelector()) {
524 Log() << kFATAL <<
"<SearchVolume> selector in Searchvolume "
525 <<
d <<
" != " <<
"node "<< st.first->GetSelector() <<
Endl;
528 tl = (*(volume->
fLower))[
d] < st.first->GetEventV()[
d] && st.first->GetLeft() != NULL;
529 tr = (*(volume->
fUpper))[
d] >= st.first->GetEventV()[
d] && st.first->GetRight() != NULL;
Node for the BinarySearch or Decision Trees.
void SetSelector(Short_t i)
Float_t GetWeight() const
const std::vector< Float_t > & GetEventV() const
Short_t GetSelector() const
A simple Binary search tree including a volume search method.
BinarySearchTreeNode * Search(Event *event) const
search the tree to find the node matching "event"
Bool_t InVolume(const std::vector< Float_t > &, Volume *) const
test if the data points are in the given volume
virtual ~BinarySearchTree(void)
destructor
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"
void NormalizeTree()
Normalisation of tree.
Double_t GetSumOfWeights(void) const
return the sum of event (node) weights
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 Clear(TMVA::Node *n=0)
clear nodes
BinarySearchTree(void)
default constructor
void CalcStatistics(TMVA::Node *n=0)
calculate basic statistics (mean, rms for each variable)
Double_t SearchVolume(Volume *, std::vector< const TMVA::BinarySearchTreeNode * > *events=0)
search the whole tree and add up all weights of events that lie within the given volume
void Insert(const Event *)
insert a new "event" in the binary tree
Int_t SearchVolumeWithMaxLimit(TMVA::Volume *, std::vector< const TMVA::BinarySearchTreeNode * > *events=0, Int_t=-1)
recursively walk through the daughter nodes and add up all weights of events that lie within the give...
Base class for BinarySearch and Decision Trees.
virtual void ReadXML(void *node, UInt_t tmva_Version_Code=TMVA_VERSION_CODE)
read attributes from XML
UInt_t GetNVariables() const
accessor to the number of variables
Node for the BinarySearch or Decision Trees.
virtual Node * GetLeft() const
virtual void SetRight(Node *r)
virtual Bool_t GoesLeft(const Event &) const =0
virtual void SetLeft(Node *l)
virtual void SetParent(Node *p)
virtual Bool_t GoesRight(const Event &) const =0
virtual Node * GetRight() const
Volume for BinarySearchTree.
std::vector< Double_t > * fLower
std::vector< Double_t > * fUpper
MsgLogger & Endl(MsgLogger &ml)
Double_t Sqrt(Double_t x)