ROOT  6.06/09
Reference Guide
BinarySearchTreeNode.cxx
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Andreas Hoecker, Joerg Stelzer, Helge Voss, Kai Voss
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate Data analysis *
6  * Package: TMVA *
7  * Classes: Node *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * Implementation (see header file for description) *
12  * *
13  * Authors (alphabetical): *
14  * Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland *
15  * Helge Voss <Helge.Voss@cern.ch> - MPI-K Heidelberg, Germany *
16  * Kai Voss <Kai.Voss@cern.ch> - U. of Victoria, Canada *
17  * *
18  * CopyRight (c) 2005: *
19  * CERN, Switzerland *
20  * U. of Victoria, Canada *
21  * MPI-K Heidelberg, Germany *
22  * *
23  * Redistribution and use in source and binary forms, with or without *
24  * modification, are permitted according to the terms listed in LICENSE *
25  * (http://tmva.sourceforge.net/LICENSE) *
26  **********************************************************************************/
27 
28 //_______________________________________________________________________
29 //
30 // Node for the BinarySearch or Decision Trees
31 //
32 // for the binary search tree, it basically consists of the EVENT, and
33 // pointers to the parent and daughters
34 //
35 // in case of the Decision Tree, it specifies parent and daughters, as
36 // well as "which variable is used" in the selection of this node, including
37 // the respective cut value.
38 //______________________________________________________________________
39 
40 #include <stdexcept>
41 #include <iomanip>
42 #include <assert.h>
43 #include <cstdlib>
44 
46 #include "TMVA/Event.h"
47 #include "TMVA/MsgLogger.h"
48 #include "TMVA/Tools.h"
49 
51 
52 ////////////////////////////////////////////////////////////////////////////////
53 /// constructor of a node for the search tree
54 
55 TMVA::BinarySearchTreeNode::BinarySearchTreeNode( const Event* e, UInt_t /* signalClass */ )
56  : TMVA::Node(),
57  fEventV ( std::vector<Float_t>() ),
58  fTargets ( std::vector<Float_t>() ),
59  fWeight ( e==0?0:e->GetWeight() ),
60  fClass ( e==0?0:e->GetClass() ), // see BinarySearchTree.h, line Mean() RMS() Min() and Max()
61  fSelector( -1 )
62 {
63  if (e!=0) {
64  for (UInt_t ivar=0; ivar<e->GetNVariables(); ivar++) fEventV.push_back(e->GetValue(ivar));
65  for (std::vector<Float_t>::const_iterator it = e->GetTargets().begin(); it < e->GetTargets().end(); it++ ) {
66  fTargets.push_back( (*it) );
67  }
68  }
69 }
70 
71 ////////////////////////////////////////////////////////////////////////////////
72 /// constructor of a daughter node as a daughter of 'p'
73 
75  TMVA::Node(parent,pos),
76  fEventV ( std::vector<Float_t>() ),
77  fTargets ( std::vector<Float_t>() ),
78  fWeight ( 0 ),
79  fClass ( 0 ),
80  fSelector( -1 )
81 {
82 }
83 
84 ////////////////////////////////////////////////////////////////////////////////
85 /// copy constructor of a node. It will result in an explicit copy of
86 /// the node and recursively all it's daughters
87 
89  BinarySearchTreeNode* parent ) :
90  TMVA::Node(n),
91  fEventV ( n.fEventV ),
92  fTargets ( n.fTargets ),
93  fWeight ( n.fWeight ),
94  fClass ( n.fClass ),
95  fSelector( n.fSelector )
96 {
97  this->SetParent( parent );
98  if (n.GetLeft() == 0 ) this->SetLeft(NULL);
99  else this->SetLeft( new BinarySearchTreeNode( *((BinarySearchTreeNode*)(n.GetLeft())),this));
100 
101  if (n.GetRight() == 0 ) this->SetRight(NULL);
102  else this->SetRight( new BinarySearchTreeNode( *((BinarySearchTreeNode*)(n.GetRight())),this));
103 
104 }
105 
106 ////////////////////////////////////////////////////////////////////////////////
107 /// node destructor
108 
110 {
111 }
112 
113 ////////////////////////////////////////////////////////////////////////////////
114 /// check if the event fed into the node goes/decends to the right daughter
115 
117 {
118  if (e.GetValue(fSelector) > GetEventV()[fSelector]) return true;
119  else return false;
120 }
121 
122 ////////////////////////////////////////////////////////////////////////////////
123 /// check if the event fed into the node goes/decends to the left daughter
124 
126 {
127  if (e.GetValue(fSelector) <= GetEventV()[fSelector]) return true;
128  else return false;
129 }
130 
131 ////////////////////////////////////////////////////////////////////////////////
132 /// check if the event fed into the node actually equals the event
133 /// that forms the node (in case of a search tree)
134 
136 {
137  Bool_t result = true;
138  for (UInt_t i=0; i<GetEventV().size(); i++) {
139  result&= (e.GetValue(i) == GetEventV()[i]);
140  }
141  return result;
142 }
143 
144 ////////////////////////////////////////////////////////////////////////////////
145 /// print the node
146 
147 void TMVA::BinarySearchTreeNode::Print( std::ostream& os ) const
148 {
149  os << "< *** " << std::endl << " node.Data: ";
150  std::vector<Float_t>::const_iterator it=fEventV.begin();
151  os << fEventV.size() << " vars: ";
152  for (;it!=fEventV.end(); it++) os << " " << std::setw(10) << *it;
153  os << " EvtWeight " << std::setw(10) << fWeight;
154  os << std::setw(10) << "Class: " << GetClass() << std::endl;
155 
156  os << "Selector: " << this->GetSelector() <<std::endl;
157  os << "My address is " << long(this) << ", ";
158  if (this->GetParent() != NULL) os << " parent at addr: " << long(this->GetParent()) ;
159  if (this->GetLeft() != NULL) os << " left daughter at addr: " << long(this->GetLeft());
160  if (this->GetRight() != NULL) os << " right daughter at addr: " << long(this->GetRight()) ;
161 
162  os << " **** > "<< std::endl;
163 }
164 
165 ////////////////////////////////////////////////////////////////////////////////
166 /// recursively print the node and its daughters (--> print the 'tree')
167 
168 void TMVA::BinarySearchTreeNode::PrintRec( std::ostream& os ) const
169 {
170  os << this->GetDepth() << " " << this->GetPos() << " " << this->GetSelector()
171  << " data: " << std::endl;
172  std::vector<Float_t>::const_iterator it=fEventV.begin();
173  os << fEventV.size() << " vars: ";
174  for (;it!=fEventV.end(); it++) os << " " << std::setw(10) << *it;
175  os << " EvtWeight " << std::setw(10) << fWeight;
176  os << std::setw(10) << "Class: " << GetClass() << std::endl;
177 
178  if (this->GetLeft() != NULL)this->GetLeft()->PrintRec(os) ;
179  if (this->GetRight() != NULL)this->GetRight()->PrintRec(os);
180 }
181 
182 ////////////////////////////////////////////////////////////////////////////////
183 /// Read the data block
184 
185 Bool_t TMVA::BinarySearchTreeNode::ReadDataRecord( std::istream& is, UInt_t /* Tmva_Version_Code */ )
186 {
187  Int_t itmp;
188  std::string tmp;
189  UInt_t depth, selIdx, nvar;
190  Char_t pos;
191  TString sigbkgd;
192  Float_t evtValFloat;
193 
194  // read depth and position
195  is >> itmp;
196  if ( itmp==-1 ) { return kFALSE; } // Done
197 
198  depth=(UInt_t)itmp;
199  is >> pos >> selIdx;
200  this->SetDepth(depth); // depth of the tree
201  this->SetPos(pos); // either 's' (root node), 'l', or 'r'
202  this->SetSelector(selIdx);
203 
204  // next line: read and build the event
205  // coverity[tainted_data_argument]
206  is >> nvar;
207  fEventV.clear();
208  for (UInt_t ivar=0; ivar<nvar; ivar++) {
209  is >> evtValFloat; fEventV.push_back(evtValFloat);
210  }
211  is >> tmp >> fWeight;
212  is >> sigbkgd;
213  fClass = (sigbkgd=="S" || sigbkgd=="Signal")?0:1;
214 
215  return kTRUE;
216 }
217 
218 ////////////////////////////////////////////////////////////////////////////////
219 /// read attributes from XML
220 
221 void TMVA::BinarySearchTreeNode::ReadAttributes(void* node, UInt_t /* tmva_Version_Code */ )
222 {
223  gTools().ReadAttr(node, "selector", fSelector );
224  gTools().ReadAttr(node, "weight", fWeight );
225  std::string sb;
226  gTools().ReadAttr(node, "type", sb);
227  if (sb=="Signal" || sb=="0")
228  fClass=0;
229  if (sb=="1")
230  fClass=1;
231 // fClass = (sb=="Signal")?0:1;
232  Int_t nvars;
233  gTools().ReadAttr(node, "NVars",nvars);
234  fEventV.resize(nvars);
235 }
236 
237 
238 ////////////////////////////////////////////////////////////////////////////////
239 /// adding attributes to tree node
240 
242  gTools().AddAttr(node, "selector", fSelector );
243  gTools().AddAttr(node, "weight", fWeight );
244 // gTools().AddAttr(node, "type", (IsSignal()?"Signal":"Background"));
245  gTools().AddAttr(node, "type", GetClass());
246  gTools().AddAttr(node, "NVars", fEventV.size());
247 }
248 
249 
250 ////////////////////////////////////////////////////////////////////////////////
251 /// adding attributes to tree node
252 
253 void TMVA::BinarySearchTreeNode::AddContentToNode( std::stringstream& s ) const
254 {
255  std::ios_base::fmtflags ff = s.flags();
256  s.precision( 16 );
257  for (UInt_t i=0; i<fEventV.size(); i++) s << std::scientific << " " << fEventV[i];
258  for (UInt_t i=0; i<fTargets.size(); i++) s << std::scientific << " " << fTargets[i];
259  s.flags(ff);
260 }
261 ////////////////////////////////////////////////////////////////////////////////
262 /// read events from node
263 
264 void TMVA::BinarySearchTreeNode::ReadContent( std::stringstream& s )
265 {
266  Float_t temp=0;
267  for (UInt_t i=0; i<fEventV.size(); i++){
268  s >> temp;
269  fEventV[i]=temp;
270  }
271  while (s >> temp) fTargets.push_back(temp);
272 }
float Float_t
Definition: RtypesCore.h:53
virtual Bool_t GoesRight(const Event &) const
check if the event fed into the node goes/decends to the right daughter
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
virtual void SetRight(Node *r)
Definition: Node.h:97
const Bool_t kFALSE
Definition: Rtypes.h:92
STL namespace.
void AddAttr(void *node, const char *, const T &value, Int_t precision=16)
Definition: Tools.h:308
Float_t GetValue(UInt_t ivar) const
return value of i'th variable
Definition: Event.cxx:231
TClass * GetClass(T *)
Definition: TClass.h:555
Tools & gTools()
Definition: Tools.cxx:79
virtual void SetLeft(Node *l)
Definition: Node.h:96
virtual Bool_t ReadDataRecord(std::istream &is, UInt_t tmva_Version_Code=TMVA_VERSION_CODE)
Read the data block.
virtual void PrintRec(std::ostream &os) const
recursively print the node and its daughters (–> print the 'tree')
TClass * fClass
pointer to the foreign object
virtual ~BinarySearchTreeNode()
node destructor
virtual void ReadAttributes(void *node, UInt_t tmva_Version_Code=TMVA_VERSION_CODE)
read attributes from XML
unsigned int UInt_t
Definition: RtypesCore.h:42
virtual void AddAttributesToNode(void *node) const
adding attributes to tree node
Double_t GetWeight(Double_t x) const
void ReadAttr(void *node, const char *, T &value)
Definition: Tools.h:295
virtual void SetParent(Node *p)
Definition: Node.h:98
virtual void AddContentToNode(std::stringstream &s) const
adding attributes to tree node
virtual Bool_t GoesLeft(const Event &) const
check if the event fed into the node goes/decends to the left daughter
virtual Node * GetRight() const
Definition: Node.h:92
BinarySearchTreeNode(const Event *e=NULL, UInt_t signalClass=0)
char Char_t
Definition: RtypesCore.h:29
Abstract ClassifierFactory template that handles arbitrary types.
virtual Bool_t EqualsMe(const Event &) const
check if the event fed into the node actually equals the event that forms the node (in case of a sear...
#define NULL
Definition: Rtypes.h:82
double result[121]
const Bool_t kTRUE
Definition: Rtypes.h:91
const Int_t n
Definition: legend1.C:16
virtual void Print(std::ostream &os) const
print the node
virtual void ReadContent(std::stringstream &s)
read events from node
virtual Node * GetLeft() const
Definition: Node.h:91
ClassImp(TMVA::BinarySearchTreeNode) TMVA
constructor of a node for the search tree