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