00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include <stdexcept>
00041 #include <iomanip>
00042 #include <assert.h>
00043 #include <cstdlib>
00044
00045 #include "TMVA/BinarySearchTreeNode.h"
00046 #include "TMVA/Event.h"
00047 #include "TMVA/MsgLogger.h"
00048 #include "TMVA/Tools.h"
00049
00050 ClassImp(TMVA::BinarySearchTreeNode)
00051
00052
00053 TMVA::BinarySearchTreeNode::BinarySearchTreeNode( const Event* e )
00054 : TMVA::Node(),
00055 fEventV ( std::vector<Float_t>() ),
00056 fTargets ( std::vector<Float_t>() ),
00057 fWeight ( e==0?0:e->GetWeight() ),
00058 fClass ( e==0?0:e->GetClass() ),
00059 fSelector( -1 )
00060 {
00061
00062 if (e!=0) {
00063 for (UInt_t ivar=0; ivar<e->GetNVariables(); ivar++) fEventV.push_back(e->GetValue(ivar));
00064 for (std::vector<Float_t>::const_iterator it = e->GetTargets().begin(); it < e->GetTargets().end(); it++ ) {
00065 fTargets.push_back( (*it) );
00066 }
00067 }
00068 }
00069
00070
00071 TMVA::BinarySearchTreeNode::BinarySearchTreeNode( BinarySearchTreeNode* parent, char pos ) :
00072 TMVA::Node(parent,pos),
00073 fEventV ( std::vector<Float_t>() ),
00074 fTargets ( std::vector<Float_t>() ),
00075 fWeight ( 0 ),
00076 fClass ( 0 ),
00077 fSelector( -1 )
00078 {
00079
00080 }
00081
00082
00083 TMVA::BinarySearchTreeNode::BinarySearchTreeNode ( const BinarySearchTreeNode &n,
00084 BinarySearchTreeNode* parent ) :
00085 TMVA::Node(n),
00086 fEventV ( n.fEventV ),
00087 fTargets ( n.fTargets ),
00088 fWeight ( n.fWeight ),
00089 fClass ( n.fClass ),
00090 fSelector( n.fSelector )
00091 {
00092
00093
00094 this->SetParent( parent );
00095 if (n.GetLeft() == 0 ) this->SetLeft(NULL);
00096 else this->SetLeft( new BinarySearchTreeNode( *((BinarySearchTreeNode*)(n.GetLeft())),this));
00097
00098 if (n.GetRight() == 0 ) this->SetRight(NULL);
00099 else this->SetRight( new BinarySearchTreeNode( *((BinarySearchTreeNode*)(n.GetRight())),this));
00100
00101 }
00102
00103
00104 TMVA::BinarySearchTreeNode::~BinarySearchTreeNode()
00105 {
00106
00107 }
00108
00109
00110 Bool_t TMVA::BinarySearchTreeNode::GoesRight( const TMVA::Event& e ) const
00111 {
00112
00113 if (e.GetValue(fSelector) > GetEventV()[fSelector]) return true;
00114 else return false;
00115 }
00116
00117
00118 Bool_t TMVA::BinarySearchTreeNode::GoesLeft(const TMVA::Event& e) const
00119 {
00120
00121 if (e.GetValue(fSelector) <= GetEventV()[fSelector]) return true;
00122 else return false;
00123 }
00124
00125
00126 Bool_t TMVA::BinarySearchTreeNode::EqualsMe(const TMVA::Event& e) const
00127 {
00128
00129
00130 Bool_t result = true;
00131 for (UInt_t i=0; i<GetEventV().size(); i++) {
00132 result&= (e.GetValue(i) == GetEventV()[i]);
00133 }
00134 return result;
00135 }
00136
00137
00138 void TMVA::BinarySearchTreeNode::Print( ostream& os ) const
00139 {
00140
00141 os << "< *** " << std::endl << " node.Data: ";
00142 std::vector<Float_t>::const_iterator it=fEventV.begin();
00143 os << fEventV.size() << " vars: ";
00144 for (;it!=fEventV.end(); it++) os << " " << std::setw(10) << *it;
00145 os << " EvtWeight " << std::setw(10) << fWeight;
00146 os << std::setw(10) << (IsSignal()?" Signal":" Background") << std::endl;
00147
00148 os << "Selector: " << this->GetSelector() <<std::endl;
00149 os << "My address is " << long(this) << ", ";
00150 if (this->GetParent() != NULL) os << " parent at addr: " << long(this->GetParent()) ;
00151 if (this->GetLeft() != NULL) os << " left daughter at addr: " << long(this->GetLeft());
00152 if (this->GetRight() != NULL) os << " right daughter at addr: " << long(this->GetRight()) ;
00153
00154 os << " **** > "<< std::endl;
00155 }
00156
00157
00158 void TMVA::BinarySearchTreeNode::PrintRec( ostream& os ) const
00159 {
00160
00161 os << this->GetDepth() << " " << this->GetPos() << " " << this->GetSelector()
00162 << " data: " << std::endl;
00163 std::vector<Float_t>::const_iterator it=fEventV.begin();
00164 os << fEventV.size() << " vars: ";
00165 for (;it!=fEventV.end(); it++) os << " " << std::setw(10) << *it;
00166 os << " EvtWeight " << std::setw(10) << fWeight;
00167 os << std::setw(10) << (IsSignal()?" Signal":" Background") << std::endl;
00168
00169 if (this->GetLeft() != NULL)this->GetLeft()->PrintRec(os) ;
00170 if (this->GetRight() != NULL)this->GetRight()->PrintRec(os);
00171 }
00172
00173
00174 Bool_t TMVA::BinarySearchTreeNode::ReadDataRecord( istream& is, UInt_t )
00175 {
00176
00177 Int_t itmp;
00178 std::string tmp;
00179 UInt_t depth, selIdx, nvar;
00180 Char_t pos;
00181 TString sigbkgd;
00182 Float_t evtValFloat;
00183
00184
00185 is >> itmp;
00186 if ( itmp==-1 ) { return kFALSE; }
00187
00188 depth=(UInt_t)itmp;
00189 is >> pos >> selIdx;
00190 this->SetDepth(depth);
00191 this->SetPos(pos);
00192 this->SetSelector(selIdx);
00193
00194
00195
00196 is >> nvar;
00197 fEventV.clear();
00198 for (UInt_t ivar=0; ivar<nvar; ivar++) {
00199 is >> evtValFloat; fEventV.push_back(evtValFloat);
00200 }
00201 is >> tmp >> fWeight;
00202 is >> sigbkgd;
00203 fClass = (sigbkgd=="S" || sigbkgd=="Signal")?0:1;
00204
00205 return kTRUE;
00206 }
00207
00208
00209 void TMVA::BinarySearchTreeNode::ReadAttributes(void* node, UInt_t )
00210 {
00211
00212 gTools().ReadAttr(node, "selector", fSelector );
00213 gTools().ReadAttr(node, "weight", fWeight );
00214 std::string sb;
00215 gTools().ReadAttr(node, "type", sb);
00216 fClass = (sb=="Signal")?0:1;
00217 Int_t nvars;
00218 gTools().ReadAttr(node, "NVars",nvars);
00219 fEventV.resize(nvars);
00220 }
00221
00222
00223
00224 void TMVA::BinarySearchTreeNode::AddAttributesToNode(void* node) const {
00225
00226 gTools().AddAttr(node, "selector", fSelector );
00227 gTools().AddAttr(node, "weight", fWeight );
00228 gTools().AddAttr(node, "type", (IsSignal()?"Signal":"Background"));
00229 gTools().AddAttr(node, "NVars", fEventV.size());
00230 }
00231
00232
00233
00234 void TMVA::BinarySearchTreeNode::AddContentToNode( std::stringstream& s ) const
00235 {
00236
00237 std::ios_base::fmtflags ff = s.flags();
00238 s.precision( 16 );
00239 for (UInt_t i=0; i<fEventV.size(); i++) s << std::scientific << " " << fEventV[i];
00240 for (UInt_t i=0; i<fTargets.size(); i++) s << std::scientific << " " << fTargets[i];
00241 s.flags(ff);
00242 }
00243
00244 void TMVA::BinarySearchTreeNode::ReadContent( std::stringstream& s )
00245 {
00246
00247 Float_t temp=0;
00248 for (UInt_t i=0; i<fEventV.size(); i++){
00249 s >> temp;
00250 fEventV[i]=temp;
00251 }
00252 while (s >> temp) fTargets.push_back(temp);
00253 }