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