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
31Node for the BinarySearch or Decision Trees
32
33for the binary search tree, it basically consists of the EVENT, and
34pointers to the parent and daughters
35
36in case of the Decision Tree, it specifies parent and daughters, as
37well as "which variable is used" in the selection of this node, including
38the 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
151void 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
172void 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
189Bool_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
225void 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
257void 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
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}
long
Definition: Converters.cxx:876
Cppyy::TCppType_t fClass
#define e(i)
Definition: RSha256.hxx:103
int Int_t
Definition: RtypesCore.h:45
char Char_t
Definition: RtypesCore.h:33
unsigned int UInt_t
Definition: RtypesCore.h:46
const Bool_t kFALSE
Definition: RtypesCore.h:101
bool Bool_t
Definition: RtypesCore.h:63
float Float_t
Definition: RtypesCore.h:57
const Bool_t kTRUE
Definition: RtypesCore.h:100
#define ClassImp(name)
Definition: Rtypes.h:364
Node for the BinarySearch or Decision Trees.
std::vector< Float_t > fTargets
virtual void PrintRec(std::ostream &os) const
recursively print the node and its daughters (--> print the 'tree')
std::vector< Float_t > fEventV
virtual void ReadContent(std::stringstream &s)
read events from node
virtual Bool_t GoesLeft(const Event &) const
check if the event fed into the node goes/descends to the left daughter
virtual ~BinarySearchTreeNode()
node destructor
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...
virtual void Print(std::ostream &os) const
print the node
virtual void AddAttributesToNode(void *node) const
adding attributes to tree node
virtual void ReadAttributes(void *node, UInt_t tmva_Version_Code=TMVA_VERSION_CODE)
read attributes from XML
const std::vector< Float_t > & GetTargets() const
BinarySearchTreeNode(const Event *e=NULL, UInt_t signalClass=0)
constructor of a node for the search tree
virtual Bool_t GoesRight(const Event &) const
check if the event fed into the node goes/descends to the right daughter
virtual void AddContentToNode(std::stringstream &s) const
adding attributes to tree node
virtual Bool_t ReadDataRecord(std::istream &is, UInt_t tmva_Version_Code=TMVA_VERSION_CODE)
Read the data block.
Node for the BinarySearch or Decision Trees.
Definition: Node.h:58
virtual void SetRight(Node *r)
Definition: Node.h:95
virtual void SetLeft(Node *l)
Definition: Node.h:94
virtual void SetParent(Node *p)
Definition: Node.h:96
void ReadAttr(void *node, const char *, T &value)
read attribute from xml
Definition: Tools.h:329
void AddAttr(void *node, const char *, const T &value, Int_t precision=16)
add attribute to xml
Definition: Tools.h:347
Basic string class.
Definition: TString.h:136
const Int_t n
Definition: legend1.C:16
TClass * GetClass(T *)
Definition: TClass.h:658
static constexpr double s
create variable transformations
Tools & gTools()