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
87 fStatisticsIsValid(
kFALSE ),
89 fCanNormalize( kFALSE )
91 fNEventsW[0]=fNEventsW[1]=0.;
99 fPeriod ( b.fPeriod ),
101 fStatisticsIsValid(
kFALSE ),
102 fSumOfWeights( b.fSumOfWeights ),
106 Log() <<
kFATAL <<
" Copy constructor not implemented yet " <<
Endl;
114 for(std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator pIt = fNormalizeTreeTable.begin();
115 pIt != fNormalizeTreeTable.end(); pIt++) {
124 std::string
type(
"");
127 bt->
ReadXML( node, tmva_Version_Code );
137 fStatisticsIsValid =
kFALSE;
139 if (this->GetRoot() ==
NULL) {
142 this->GetRoot()->SetPos(
's');
143 this->GetRoot()->SetDepth(0);
145 fSumOfWeights =
event->GetWeight();
152 Log() <<
kFATAL <<
"<Insert> event vector length != Periode specified in Binary Tree" <<
Endl
153 <<
"--- event size: " <<
event->GetNVariables() <<
" Periode: " << this->GetPeriode() <<
Endl
154 <<
"--- and all this when trying filling the "<<fNNodes+1<<
"th Node" <<
Endl;
157 this->Insert(event, this->GetRoot());
161 if (fCanNormalize) fNormalizeTreeTable.push_back( std::make_pair(0.0,
new const Event(*event)) );
171 fStatisticsIsValid =
kFALSE;
176 this->Insert(event, node->
GetLeft());
182 fSumOfWeights +=
event->GetWeight();
193 this->Insert(event, node->
GetRight());
199 fSumOfWeights +=
event->GetWeight();
207 else Log() <<
kFATAL <<
"<Insert> neither left nor right :)" <<
Endl;
215 return this->Search( event, this->GetRoot() );
228 return this->Search(event, node->
GetLeft());
230 return this->Search(event, node->
GetRight());
240 if (fSumOfWeights <= 0) {
241 Log() <<
kWARNING <<
"you asked for the SumOfWeights, which is not filled yet"
242 <<
" I call CalcStatistics which hopefully fixes things"
245 if (fSumOfWeights <= 0)
Log() <<
kFATAL <<
" Zero events in your Search Tree" <<
Endl;
247 return fSumOfWeights;
255 if (fSumOfWeights <= 0) {
256 Log() <<
kWARNING <<
"you asked for the SumOfWeights, which is not filled yet"
257 <<
" I call CalcStatistics which hopefully fixes things"
260 if (fSumOfWeights <= 0)
Log() <<
kFATAL <<
" Zero events in your Search Tree" <<
Endl;
272 fPeriod = theVars.size();
273 return Fill(events, theType);
285 if (fSumOfWeights != 0) {
287 <<
"You are filling a search three that is not empty.. "
288 <<
" do you know what you are doing?"
291 for (
UInt_t ievt=0; ievt<
n; ievt++) {
293 if (theType == -1 || (
Int_t(events[ievt]->
GetClass()) == theType) ) {
294 this->Insert( events[ievt] );
296 fSumOfWeights += events[ievt]->GetWeight();
301 return fSumOfWeights;
307 std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator rightBound,
312 if (leftBound == rightBound)
return;
314 if (actDim == fPeriod) actDim = 0;
315 for (std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator i=leftBound; i!=rightBound; i++) {
316 i->first = i->second->GetValue( actDim );
319 std::sort( leftBound, rightBound );
321 std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator leftTemp = leftBound;
322 std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator rightTemp = rightBound;
327 if (rightTemp == leftTemp ) {
331 if (leftTemp == rightTemp) {
336 std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator
mid = leftTemp;
337 std::vector< std::pair<Double_t, const TMVA::Event*> >::iterator midTemp =
mid;
339 if (mid!=leftBound) midTemp--;
341 while (mid != leftBound && mid->second->GetValue( actDim ) == midTemp->second->GetValue( actDim )) {
346 Insert( mid->second );
351 NormalizeTree( leftBound, mid, actDim+1 );
355 NormalizeTree( mid, rightBound, actDim+1 );
369 NormalizeTree( fNormalizeTreeTable.begin(), fNormalizeTreeTable.end(), 0 );
382 if (n !=
NULL)
delete n;
392 std::vector<const BinarySearchTreeNode*>* events )
394 return SearchVolume( this->GetRoot(), volume, 0, events );
402 std::vector<const BinarySearchTreeNode*>* events )
404 if (t==
NULL)
return 0;
409 if (InVolume( st->
GetEventV(), volume )) {
411 if (
NULL != events) events->push_back( st );
419 Int_t d = depth%this->GetPeriode();
421 Log() <<
kFATAL <<
"<SearchVolume> selector in Searchvolume "
427 if (tl) count += SearchVolume( st->
GetLeft(), volume, (depth+1), events );
428 if (tr) count += SearchVolume( st->
GetRight(), volume, (depth+1), events );
438 for (
UInt_t ivar=0; ivar< fPeriod; ivar++) {
439 result = ( (*(volume->
fLower))[ivar] <
event[ivar] &&
440 (*(volume->
fUpper))[ivar] >=
event[ivar] );
451 if (fStatisticsIsValid)
return;
458 for (
Int_t sb=0; sb<2; sb++) {
460 fMeans[sb] = std::vector<Float_t>(fPeriod);
461 fRMS[sb] = std::vector<Float_t>(fPeriod);
462 fMin[sb] = std::vector<Float_t>(fPeriod);
463 fMax[sb] = std::vector<Float_t>(fPeriod);
464 fSum[sb] = std::vector<Double_t>(fPeriod);
465 fSumSq[sb] = std::vector<Double_t>(fPeriod);
466 for (
UInt_t j=0; j<fPeriod; j++) {
467 fMeans[sb][j] = fRMS[sb][j] = fSum[sb][j] = fSumSq[sb][j] = 0;
468 fMin[sb][j] = FLT_MAX;
469 fMax[sb][j] = -FLT_MAX;
473 if (currentNode ==
NULL)
return;
476 const std::vector<Float_t> & evtVec = currentNode->
GetEventV();
482 fNEventsW[
type] += weight;
483 fSumOfWeights += weight;
485 for (
UInt_t j=0; j<fPeriod; j++) {
487 fSum[
type][j] += val*weight;
488 fSumSq[
type][j] += val*val*weight;
489 if (val < fMin[type][j]) fMin[
type][j] = val;
490 if (val > fMax[type][j]) fMax[
type][j] = val;
497 for (
Int_t sb=0; sb<2; sb++) {
498 for (
UInt_t j=0; j<fPeriod; j++) {
499 if (fNEventsW[sb] == 0) { fMeans[sb][j] = fRMS[sb][j] = 0;
continue; }
500 fMeans[sb][j] = fSum[sb][j]/fNEventsW[sb];
501 fRMS[sb][j] =
TMath::Sqrt(fSumSq[sb][j]/fNEventsW[sb] - fMeans[sb][j]*fMeans[sb][j]);
504 fStatisticsIsValid =
kTRUE;
515 if (this->GetRoot() ==
NULL)
return 0;
517 std::queue< std::pair< const BinarySearchTreeNode*, Int_t > > queue;
518 std::pair< const BinarySearchTreeNode*, Int_t > st = std::make_pair( (
const BinarySearchTreeNode*)this->GetRoot(), 0 );
523 while ( !queue.empty() ) {
524 st = queue.front(); queue.pop();
526 if (count == max_points)
529 if (InVolume( st.first->GetEventV(), volume )) {
531 if (
NULL != events) events->push_back( st.first );
536 if ( d ==
Int_t(this->GetPeriode()) ) d = 0;
538 if (d != st.first->GetSelector()) {
539 Log() <<
kFATAL <<
"<SearchVolume> selector in Searchvolume "
540 << d <<
" != " <<
"node "<< st.first->GetSelector() <<
Endl;
543 tl = (*(volume->
fLower))[d] < st.first->GetEventV()[d] && st.first->GetLeft() !=
NULL;
544 tr = (*(volume->
fUpper))[d] >= st.first->GetEventV()[d] && st.first->GetRight() !=
NULL;
std::vector< Double_t > * fLower
MsgLogger & Endl(MsgLogger &ml)
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t mid
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
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 ...
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)
UInt_t GetNVariables() const
accessor to the number of variables
BinarySearchTree(void)
default constructor
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" ...
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
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