ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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/Node.h"
49 #include "TMVA/Tools.h"
50 
52 
53 ////////////////////////////////////////////////////////////////////////////////
54 /// constructor of a node for the search tree
55 
56 TMVA::BinarySearchTreeNode::BinarySearchTreeNode( const Event* e, UInt_t /* signalClass */ )
57  : TMVA::Node(),
58  fEventV ( std::vector<Float_t>() ),
59  fTargets ( std::vector<Float_t>() ),
60  fWeight ( e==0?0:e->GetWeight() ),
61  fClass ( e==0?0:e->GetClass() ), // see BinarySearchTree.h, line Mean() RMS() Min() and Max()
62  fSelector( -1 )
63 {
64  if (e!=0) {
65  for (UInt_t ivar=0; ivar<e->GetNVariables(); ivar++) fEventV.push_back(e->GetValue(ivar));
66  for (std::vector<Float_t>::const_iterator it = e->GetTargets().begin(); it < e->GetTargets().end(); it++ ) {
67  fTargets.push_back( (*it) );
68  }
69  }
70 }
71 
72 ////////////////////////////////////////////////////////////////////////////////
73 /// constructor of a daughter node as a daughter of 'p'
74 
76  TMVA::Node(parent,pos),
77  fEventV ( std::vector<Float_t>() ),
78  fTargets ( std::vector<Float_t>() ),
79  fWeight ( 0 ),
80  fClass ( 0 ),
81  fSelector( -1 )
82 {
83 }
84 
85 ////////////////////////////////////////////////////////////////////////////////
86 /// copy constructor of a node. It will result in an explicit copy of
87 /// the node and recursively all it's daughters
88 
90  BinarySearchTreeNode* parent ) :
91  TMVA::Node(n),
92  fEventV ( n.fEventV ),
93  fTargets ( n.fTargets ),
94  fWeight ( n.fWeight ),
95  fClass ( n.fClass ),
96  fSelector( n.fSelector )
97 {
98  this->SetParent( parent );
99  if (n.GetLeft() == 0 ) this->SetLeft(NULL);
100  else this->SetLeft( new BinarySearchTreeNode( *((BinarySearchTreeNode*)(n.GetLeft())),this));
101 
102  if (n.GetRight() == 0 ) this->SetRight(NULL);
103  else this->SetRight( new BinarySearchTreeNode( *((BinarySearchTreeNode*)(n.GetRight())),this));
104 
105 }
106 
107 ////////////////////////////////////////////////////////////////////////////////
108 /// node destructor
109 
111 {
112 }
113 
114 ////////////////////////////////////////////////////////////////////////////////
115 /// check if the event fed into the node goes/decends to the right daughter
116 
118 {
119  if (e.GetValue(fSelector) > GetEventV()[fSelector]) return true;
120  else return false;
121 }
122 
123 ////////////////////////////////////////////////////////////////////////////////
124 /// check if the event fed into the node goes/decends to the left daughter
125 
127 {
128  if (e.GetValue(fSelector) <= GetEventV()[fSelector]) return true;
129  else return false;
130 }
131 
132 ////////////////////////////////////////////////////////////////////////////////
133 /// check if the event fed into the node actually equals the event
134 /// that forms the node (in case of a search tree)
135 
137 {
138  Bool_t result = true;
139  for (UInt_t i=0; i<GetEventV().size(); i++) {
140  result&= (e.GetValue(i) == GetEventV()[i]);
141  }
142  return result;
143 }
144 
145 ////////////////////////////////////////////////////////////////////////////////
146 /// print the node
147 
148 void TMVA::BinarySearchTreeNode::Print( std::ostream& os ) const
149 {
150  os << "< *** " << std::endl << " node.Data: ";
151  std::vector<Float_t>::const_iterator it=fEventV.begin();
152  os << fEventV.size() << " vars: ";
153  for (;it!=fEventV.end(); it++) os << " " << std::setw(10) << *it;
154  os << " EvtWeight " << std::setw(10) << fWeight;
155  os << std::setw(10) << "Class: " << GetClass() << std::endl;
156 
157  os << "Selector: " << this->GetSelector() <<std::endl;
158  os << "My address is " << long(this) << ", ";
159  if (this->GetParent() != NULL) os << " parent at addr: " << long(this->GetParent()) ;
160  if (this->GetLeft() != NULL) os << " left daughter at addr: " << long(this->GetLeft());
161  if (this->GetRight() != NULL) os << " right daughter at addr: " << long(this->GetRight()) ;
162 
163  os << " **** > "<< std::endl;
164 }
165 
166 ////////////////////////////////////////////////////////////////////////////////
167 /// recursively print the node and its daughters (--> print the 'tree')
168 
169 void TMVA::BinarySearchTreeNode::PrintRec( std::ostream& os ) const
170 {
171  os << this->GetDepth() << " " << this->GetPos() << " " << this->GetSelector()
172  << " data: " << std::endl;
173  std::vector<Float_t>::const_iterator it=fEventV.begin();
174  os << fEventV.size() << " vars: ";
175  for (;it!=fEventV.end(); it++) os << " " << std::setw(10) << *it;
176  os << " EvtWeight " << std::setw(10) << fWeight;
177  os << std::setw(10) << "Class: " << GetClass() << std::endl;
178 
179  if (this->GetLeft() != NULL)this->GetLeft()->PrintRec(os) ;
180  if (this->GetRight() != NULL)this->GetRight()->PrintRec(os);
181 }
182 
183 ////////////////////////////////////////////////////////////////////////////////
184 /// Read the data block
185 
186 Bool_t TMVA::BinarySearchTreeNode::ReadDataRecord( std::istream& is, UInt_t /* Tmva_Version_Code */ )
187 {
188  Int_t itmp;
189  std::string tmp;
190  UInt_t depth, selIdx, nvar;
191  Char_t pos;
192  TString sigbkgd;
193  Float_t evtValFloat;
194 
195  // read depth and position
196  is >> itmp;
197  if ( itmp==-1 ) { return kFALSE; } // Done
198 
199  depth=(UInt_t)itmp;
200  is >> pos >> selIdx;
201  this->SetDepth(depth); // depth of the tree
202  this->SetPos(pos); // either 's' (root node), 'l', or 'r'
203  this->SetSelector(selIdx);
204 
205  // next line: read and build the event
206  // coverity[tainted_data_argument]
207  is >> nvar;
208  fEventV.clear();
209  for (UInt_t ivar=0; ivar<nvar; ivar++) {
210  is >> evtValFloat; fEventV.push_back(evtValFloat);
211  }
212  is >> tmp >> fWeight;
213  is >> sigbkgd;
214  fClass = (sigbkgd=="S" || sigbkgd=="Signal")?0:1;
215 
216  return kTRUE;
217 }
218 
219 ////////////////////////////////////////////////////////////////////////////////
220 /// read attributes from XML
221 
222 void TMVA::BinarySearchTreeNode::ReadAttributes(void* node, UInt_t /* tmva_Version_Code */ )
223 {
224  gTools().ReadAttr(node, "selector", fSelector );
225  gTools().ReadAttr(node, "weight", fWeight );
226  std::string sb;
227  gTools().ReadAttr(node, "type", sb);
228  if (sb=="Signal" || sb=="0")
229  fClass=0;
230  if (sb=="1")
231  fClass=1;
232 // fClass = (sb=="Signal")?0:1;
233  Int_t nvars;
234  gTools().ReadAttr(node, "NVars",nvars);
235  fEventV.resize(nvars);
236 }
237 
238 
239 ////////////////////////////////////////////////////////////////////////////////
240 /// adding attributes to tree node
241 
243  gTools().AddAttr(node, "selector", fSelector );
244  gTools().AddAttr(node, "weight", fWeight );
245 // gTools().AddAttr(node, "type", (IsSignal()?"Signal":"Background"));
246  gTools().AddAttr(node, "type", GetClass());
247  gTools().AddAttr(node, "NVars", fEventV.size());
248 }
249 
250 
251 ////////////////////////////////////////////////////////////////////////////////
252 /// adding attributes to tree node
253 
254 void TMVA::BinarySearchTreeNode::AddContentToNode( std::stringstream& s ) const
255 {
256  std::ios_base::fmtflags ff = s.flags();
257  s.precision( 16 );
258  for (UInt_t i=0; i<fEventV.size(); i++) s << std::scientific << " " << fEventV[i];
259  for (UInt_t i=0; i<fTargets.size(); i++) s << std::scientific << " " << fTargets[i];
260  s.flags(ff);
261 }
262 ////////////////////////////////////////////////////////////////////////////////
263 /// read events from node
264 
265 void TMVA::BinarySearchTreeNode::ReadContent( std::stringstream& s )
266 {
267  Float_t temp=0;
268  for (UInt_t i=0; i<fEventV.size(); i++){
269  s >> temp;
270  fEventV[i]=temp;
271  }
272  while (s >> temp) fTargets.push_back(temp);
273 }
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
void AddAttr(void *node, const char *, const T &value, Int_t precision=16)
Definition: Tools.h:308
TClass * GetClass(T *)
Definition: TClass.h:554
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
TNode * Node
Definition: na49.C:11
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
Float_t GetValue(UInt_t ivar) const
return value of i'th variable
Definition: Event.cxx:231
virtual Node * GetRight() const
Definition: Node.h:92
BinarySearchTreeNode(const Event *e=NULL, UInt_t signalClass=0)
char Char_t
Definition: RtypesCore.h:29
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