76 fStatisticsIsValid(
kFALSE ),
88 fPeriod (
b.fPeriod ),
90 fStatisticsIsValid(
kFALSE ),
91 fSumOfWeights(
b.fSumOfWeights ),
95 Log() << kFATAL <<
" Copy constructor not implemented yet " <<
Endl;
103 for(std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator pIt = fNormalizeTreeTable.begin();
104 pIt != fNormalizeTreeTable.end(); ++pIt) {
113 std::string
type(
"");
116 bt->
ReadXML( node, tmva_Version_Code );
126 fStatisticsIsValid =
kFALSE;
128 if (this->GetRoot() == NULL) {
131 this->GetRoot()->SetPos(
's');
132 this->GetRoot()->SetDepth(0);
134 fSumOfWeights =
event->GetWeight();
141 Log() << kFATAL <<
"<Insert> event vector length != Periode specified in Binary Tree" <<
Endl
142 <<
"--- event size: " <<
event->GetNVariables() <<
" Periode: " << this->GetPeriode() <<
Endl
143 <<
"--- and all this when trying filling the "<<fNNodes+1<<
"th Node" <<
Endl;
146 this->Insert(event, this->GetRoot());
150 if (fCanNormalize) fNormalizeTreeTable.push_back( std::make_pair(0.0,
new const Event(*event)) );
160 fStatisticsIsValid =
kFALSE;
165 this->Insert(event, node->
GetLeft());
171 fSumOfWeights +=
event->GetWeight();
182 this->Insert(event, node->
GetRight());
188 fSumOfWeights +=
event->GetWeight();
196 else Log() << kFATAL <<
"<Insert> neither left nor right :)" <<
Endl;
204 return this->Search( event, this->GetRoot() );
217 return this->Search(event, node->
GetLeft());
219 return this->Search(event, node->
GetRight());
229 if (fSumOfWeights <= 0) {
230 Log() << kWARNING <<
"you asked for the SumOfWeights, which is not filled yet"
231 <<
" I call CalcStatistics which hopefully fixes things"
234 if (fSumOfWeights <= 0)
Log() << kFATAL <<
" Zero events in your Search Tree" <<
Endl;
236 return fSumOfWeights;
244 if (fSumOfWeights <= 0) {
245 Log() << kWARNING <<
"you asked for the SumOfWeights, which is not filled yet"
246 <<
" I call CalcStatistics which hopefully fixes things"
249 if (fSumOfWeights <= 0)
Log() << kFATAL <<
" Zero events in your Search Tree" <<
Endl;
261 fPeriod = theVars.size();
262 return Fill(events, theType);
274 if (fSumOfWeights != 0) {
276 <<
"You are filling a search three that is not empty.. "
277 <<
" do you know what you are doing?"
280 for (
UInt_t ievt=0; ievt<
n; ievt++) {
282 if (theType == -1 || (
Int_t(events[ievt]->
GetClass()) == theType) ) {
283 this->Insert( events[ievt] );
285 fSumOfWeights += events[ievt]->GetWeight();
290 return fSumOfWeights;
298 std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator rightBound,
302 if (leftBound == rightBound)
return;
304 if (actDim == fPeriod) actDim = 0;
305 for (std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator i=leftBound; i!=rightBound; ++i) {
306 i->first = i->second->GetValue( actDim );
309 std::sort( leftBound, rightBound );
311 std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator leftTemp = leftBound;
312 std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator rightTemp = rightBound;
317 if (rightTemp == leftTemp ) {
321 if (leftTemp == rightTemp) {
326 std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator mid = leftTemp;
327 std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator midTemp = mid;
329 if (mid!=leftBound)--midTemp;
331 while (mid != leftBound && mid->second->GetValue( actDim ) == midTemp->second->GetValue( actDim )) {
336 Insert( mid->second );
341 NormalizeTree( leftBound, mid, actDim+1 );
345 NormalizeTree( mid, rightBound, actDim+1 );
359 NormalizeTree( fNormalizeTreeTable.begin(), fNormalizeTreeTable.end(), 0 );
372 if (
n != NULL)
delete n;
382 std::vector<const BinarySearchTreeNode*>* events )
384 return SearchVolume( this->GetRoot(), volume, 0, events );
392 std::vector<const BinarySearchTreeNode*>* events )
394 if (t==NULL)
return 0;
399 if (InVolume( st->
GetEventV(), volume )) {
401 if (NULL != events) events->push_back( st );
409 Int_t d = depth%this->GetPeriode();
411 Log() << kFATAL <<
"<SearchVolume> selector in Searchvolume "
417 if (tl) count += SearchVolume( st->
GetLeft(), volume, (depth+1), events );
418 if (tr) count += SearchVolume( st->
GetRight(), volume, (depth+1), events );
430 for (
UInt_t ivar=0; ivar< fPeriod; ivar++) {
431 result = ( (*(volume->
fLower))[ivar] <
event[ivar] &&
432 (*(volume->
fUpper))[ivar] >=
event[ivar] );
443 if (fStatisticsIsValid)
return;
450 for (
Int_t sb=0; sb<2; sb++) {
452 fMeans[sb] = std::vector<Float_t>(fPeriod);
453 fRMS[sb] = std::vector<Float_t>(fPeriod);
454 fMin[sb] = std::vector<Float_t>(fPeriod);
455 fMax[sb] = std::vector<Float_t>(fPeriod);
456 fSum[sb] = std::vector<Double_t>(fPeriod);
457 fSumSq[sb] = std::vector<Double_t>(fPeriod);
458 for (
UInt_t j=0; j<fPeriod; j++) {
459 fMeans[sb][j] = fRMS[sb][j] = fSum[sb][j] = fSumSq[sb][j] = 0;
460 fMin[sb][j] = FLT_MAX;
461 fMax[sb][j] = -FLT_MAX;
465 if (currentNode == NULL)
return;
468 const std::vector<Float_t> & evtVec = currentNode->
GetEventV();
474 fNEventsW[
type] += weight;
475 fSumOfWeights += weight;
477 for (
UInt_t j=0; j<fPeriod; j++) {
479 fSum[
type][j] += val*weight;
480 fSumSq[
type][j] += val*val*weight;
481 if (val < fMin[
type][j]) fMin[
type][j] = val;
482 if (val > fMax[
type][j]) fMax[
type][j] = val;
485 if ( (currentNode->
GetLeft() != NULL) ) CalcStatistics( currentNode->
GetLeft() );
486 if ( (currentNode->
GetRight() != NULL) ) CalcStatistics( currentNode->
GetRight() );
489 for (
Int_t sb=0; sb<2; sb++) {
490 for (
UInt_t j=0; j<fPeriod; j++) {
491 if (fNEventsW[sb] == 0) { fMeans[sb][j] = fRMS[sb][j] = 0;
continue; }
492 fMeans[sb][j] = fSum[sb][j]/fNEventsW[sb];
493 fRMS[sb][j] =
TMath::Sqrt(fSumSq[sb][j]/fNEventsW[sb] - fMeans[sb][j]*fMeans[sb][j]);
496 fStatisticsIsValid =
kTRUE;
509 if (this->GetRoot() == NULL)
return 0;
511 std::queue< std::pair< const BinarySearchTreeNode*, Int_t > > queue;
512 std::pair< const BinarySearchTreeNode*, Int_t > st = std::make_pair( (
const BinarySearchTreeNode*)this->GetRoot(), 0 );
517 while ( !queue.empty() ) {
518 st = queue.front(); queue.pop();
520 if (count == max_points)
523 if (InVolume( st.first->GetEventV(), volume )) {
525 if (NULL != events) events->push_back( st.first );
530 if (
d ==
Int_t(this->GetPeriode()) )
d = 0;
532 if (
d != st.first->GetSelector()) {
533 Log() << kFATAL <<
"<SearchVolume> selector in Searchvolume "
534 <<
d <<
" != " <<
"node "<< st.first->GetSelector() <<
Endl;
537 tl = (*(volume->
fLower))[
d] < st.first->GetEventV()[
d] && st.first->GetLeft() != NULL;
538 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)