68 fStatisticsIsValid(
kFALSE ),
80 fPeriod (
b.fPeriod ),
82 fStatisticsIsValid(
kFALSE ),
83 fSumOfWeights(
b.fSumOfWeights ),
87 Log() << kFATAL <<
" Copy constructor not implemented yet " <<
Endl;
95 for(std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator pIt = fNormalizeTreeTable.begin();
96 pIt != fNormalizeTreeTable.end(); ++pIt) {
105 std::string
type(
"");
108 bt->
ReadXML( node, tmva_Version_Code );
118 fStatisticsIsValid =
kFALSE;
120 if (this->GetRoot() == NULL) {
123 this->GetRoot()->SetPos(
's');
124 this->GetRoot()->SetDepth(0);
126 fSumOfWeights =
event->GetWeight();
133 Log() << kFATAL <<
"<Insert> event vector length != Periode specified in Binary Tree" <<
Endl
134 <<
"--- event size: " <<
event->GetNVariables() <<
" Periode: " << this->GetPeriode() <<
Endl
135 <<
"--- and all this when trying filling the "<<fNNodes+1<<
"th Node" <<
Endl;
138 this->Insert(event, this->GetRoot());
142 if (fCanNormalize) fNormalizeTreeTable.push_back( std::make_pair(0.0,
new const Event(*event)) );
152 fStatisticsIsValid =
kFALSE;
157 this->Insert(event, node->
GetLeft());
163 fSumOfWeights +=
event->GetWeight();
174 this->Insert(event, node->
GetRight());
180 fSumOfWeights +=
event->GetWeight();
188 else Log() << kFATAL <<
"<Insert> neither left nor right :)" <<
Endl;
196 return this->Search( event, this->GetRoot() );
209 return this->Search(event, node->
GetLeft());
211 return this->Search(event, node->
GetRight());
221 if (fSumOfWeights <= 0) {
222 Log() << kWARNING <<
"you asked for the SumOfWeights, which is not filled yet"
223 <<
" I call CalcStatistics which hopefully fixes things"
226 if (fSumOfWeights <= 0)
Log() << kFATAL <<
" Zero events in your Search Tree" <<
Endl;
228 return fSumOfWeights;
236 if (fSumOfWeights <= 0) {
237 Log() << kWARNING <<
"you asked for the SumOfWeights, which is not filled yet"
238 <<
" I call CalcStatistics which hopefully fixes things"
241 if (fSumOfWeights <= 0)
Log() << kFATAL <<
" Zero events in your Search Tree" <<
Endl;
253 fPeriod = theVars.size();
254 return Fill(events, theType);
266 if (fSumOfWeights != 0) {
268 <<
"You are filling a search three that is not empty.. "
269 <<
" do you know what you are doing?"
272 for (
UInt_t ievt=0; ievt<
n; ievt++) {
274 if (theType == -1 || (
Int_t(events[ievt]->
GetClass()) == theType) ) {
275 this->Insert( events[ievt] );
277 fSumOfWeights += events[ievt]->GetWeight();
282 return fSumOfWeights;
290 std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator rightBound,
294 if (leftBound == rightBound)
return;
296 if (actDim == fPeriod) actDim = 0;
297 for (std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator i=leftBound; i!=rightBound; ++i) {
298 i->first = i->second->GetValue( actDim );
301 std::sort( leftBound, rightBound );
303 std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator leftTemp = leftBound;
304 std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator rightTemp = rightBound;
309 if (rightTemp == leftTemp ) {
313 if (leftTemp == rightTemp) {
318 std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator mid = leftTemp;
319 std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator midTemp = mid;
321 if (mid!=leftBound)--midTemp;
323 while (mid != leftBound && mid->second->GetValue( actDim ) == midTemp->second->GetValue( actDim )) {
328 Insert( mid->second );
333 NormalizeTree( leftBound, mid, actDim+1 );
337 NormalizeTree( mid, rightBound, actDim+1 );
351 NormalizeTree( fNormalizeTreeTable.begin(), fNormalizeTreeTable.end(), 0 );
364 if (
n != NULL)
delete n;
374 std::vector<const BinarySearchTreeNode*>* events )
376 return SearchVolume( this->GetRoot(), volume, 0, events );
384 std::vector<const BinarySearchTreeNode*>* events )
386 if (t==NULL)
return 0;
391 if (InVolume( st->
GetEventV(), volume )) {
393 if (NULL != events) events->push_back( st );
401 Int_t d = depth%this->GetPeriode();
403 Log() << kFATAL <<
"<SearchVolume> selector in Searchvolume "
409 if (tl) count += SearchVolume( st->
GetLeft(), volume, (depth+1), events );
410 if (tr) count += SearchVolume( st->
GetRight(), volume, (depth+1), events );
422 for (
UInt_t ivar=0; ivar< fPeriod; ivar++) {
423 result = ( (*(volume->
fLower))[ivar] <
event[ivar] &&
424 (*(volume->
fUpper))[ivar] >=
event[ivar] );
435 if (fStatisticsIsValid)
return;
442 for (
Int_t sb=0; sb<2; sb++) {
444 fMeans[sb] = std::vector<Float_t>(fPeriod);
445 fRMS[sb] = std::vector<Float_t>(fPeriod);
446 fMin[sb] = std::vector<Float_t>(fPeriod);
447 fMax[sb] = std::vector<Float_t>(fPeriod);
448 fSum[sb] = std::vector<Double_t>(fPeriod);
449 fSumSq[sb] = std::vector<Double_t>(fPeriod);
450 for (
UInt_t j=0; j<fPeriod; j++) {
451 fMeans[sb][j] = fRMS[sb][j] = fSum[sb][j] = fSumSq[sb][j] = 0;
452 fMin[sb][j] = FLT_MAX;
453 fMax[sb][j] = -FLT_MAX;
457 if (currentNode == NULL)
return;
460 const std::vector<Float_t> & evtVec = currentNode->
GetEventV();
466 fNEventsW[
type] += weight;
467 fSumOfWeights += weight;
469 for (
UInt_t j=0; j<fPeriod; j++) {
471 fSum[
type][j] += val*weight;
472 fSumSq[
type][j] += val*val*weight;
473 if (val < fMin[
type][j]) fMin[
type][j] = val;
474 if (val > fMax[
type][j]) fMax[
type][j] = val;
477 if ( (currentNode->
GetLeft() != NULL) ) CalcStatistics( currentNode->
GetLeft() );
478 if ( (currentNode->
GetRight() != NULL) ) CalcStatistics( currentNode->
GetRight() );
481 for (
Int_t sb=0; sb<2; sb++) {
482 for (
UInt_t j=0; j<fPeriod; j++) {
483 if (fNEventsW[sb] == 0) { fMeans[sb][j] = fRMS[sb][j] = 0;
continue; }
484 fMeans[sb][j] = fSum[sb][j]/fNEventsW[sb];
485 fRMS[sb][j] =
TMath::Sqrt(fSumSq[sb][j]/fNEventsW[sb] - fMeans[sb][j]*fMeans[sb][j]);
488 fStatisticsIsValid =
kTRUE;
501 if (this->GetRoot() == NULL)
return 0;
503 std::queue< std::pair< const BinarySearchTreeNode*, Int_t > > queue;
504 std::pair< const BinarySearchTreeNode*, Int_t > st = std::make_pair( (
const BinarySearchTreeNode*)this->GetRoot(), 0 );
509 while ( !queue.empty() ) {
510 st = queue.front(); queue.pop();
512 if (count == max_points)
515 if (InVolume( st.first->GetEventV(), volume )) {
517 if (NULL != events) events->push_back( st.first );
522 if (
d ==
Int_t(this->GetPeriode()) )
d = 0;
524 if (
d != st.first->GetSelector()) {
525 Log() << kFATAL <<
"<SearchVolume> selector in Searchvolume "
526 <<
d <<
" != " <<
"node "<< st.first->GetSelector() <<
Endl;
529 tl = (*(volume->
fLower))[
d] < st.first->GetEventV()[
d] && st.first->GetLeft() != NULL;
530 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)