Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 * *
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 * (see tmva/doc/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 <cassert>
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
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
115
116////////////////////////////////////////////////////////////////////////////////
117/// check if the event fed into the node goes/descends 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/descends 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
150void 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 " << (Longptr_t)this << ", ";
161 if (this->GetParent() != NULL) os << " parent at addr: " << (Longptr_t)this->GetParent();
162 if (this->GetLeft() != NULL) os << " left daughter at addr: " << (Longptr_t)this->GetLeft();
163 if (this->GetRight() != NULL) os << " right daughter at addr: " << (Longptr_t)this->GetRight();
164
165 os << " **** > "<< std::endl;
166}
167
168////////////////////////////////////////////////////////////////////////////////
169/// recursively print the node and its daughters (--> print the 'tree')
170
171void 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
188Bool_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;
196
197 // read depth and position
198 is >> itmp;
199 if ( itmp==-1 ) { return kFALSE; } // Done
200
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
224void 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
256void 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
267void 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}
Cppyy::TCppType_t fClass
#define e(i)
Definition RSha256.hxx:103
long Longptr_t
Integer large enough to hold a pointer (platform-dependent)
Definition RtypesCore.h:89
char Char_t
Character 1 byte (char)
Definition RtypesCore.h:51
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int)
Definition RtypesCore.h:60
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:71
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Node for the BinarySearch or Decision Trees.
std::vector< Float_t > fTargets
void PrintRec(std::ostream &os) const override
recursively print the node and its daughters (--> print the 'tree')
std::vector< Float_t > fEventV
virtual ~BinarySearchTreeNode()
node destructor
Bool_t GoesLeft(const Event &) const override
check if the event fed into the node goes/descends to the left daughter
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...
void ReadAttributes(void *node, UInt_t tmva_Version_Code=262657) override
read attributes from XML
BinarySearchTreeNode(const Event *e=nullptr, UInt_t signalClass=0)
constructor of a node for the search tree
Bool_t ReadDataRecord(std::istream &is, UInt_t tmva_Version_Code=262657) override
Read the data block.
Bool_t GoesRight(const Event &) const override
check if the event fed into the node goes/descends to the right daughter
void AddContentToNode(std::stringstream &s) const override
adding attributes to tree node
void AddAttributesToNode(void *node) const override
adding attributes to tree node
void Print(std::ostream &os) const override
print the node
void ReadContent(std::stringstream &s) override
read events from node
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:138
const Int_t n
Definition legend1.C:16
create variable transformations
Tools & gTools()