/usr/include/root/TMVA/NodekNN.h is in libroot-tmva-dev 5.34.19+dfsg-1.2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 | // @(#)root/tmva $Id$
// Author: Rustem Ospanov
/**********************************************************************************
* Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
* Package: TMVA *
* Class : Node *
* Web : http://tmva.sourceforge.net *
* *
* Description: *
* kd-tree (binary tree) template *
* *
* Author: *
* Rustem Ospanov <rustem@fnal.gov> - U. of Texas at Austin, USA *
* *
* Copyright (c) 2007: *
* CERN, Switzerland *
* MPI-K Heidelberg, Germany *
* U. of Texas at Austin, USA *
* *
* Redistribution and use in source and binary forms, with or without *
* modification, are permitted according to the terms listed in LICENSE *
* (http://tmva.sourceforge.net/LICENSE) *
**********************************************************************************/
#ifndef ROOT_TMVA_NodekNN
#define ROOT_TMVA_NodekNN
// C++
#include <list>
#include <string>
#include <iostream>
// ROOT
#ifndef ROOT_Rtypes
#include "Rtypes.h"
#endif
//////////////////////////////////////////////////////////////////////////
// //
// kNN::Node //
// //
// This file contains binary tree and global function template //
// that searches tree for k-nearest neigbors //
// //
// Node class template parameter T has to provide these functions: //
// rtype GetVar(UInt_t) const; //
// - rtype is any type convertible to Float_t //
// UInt_t GetNVar(void) const; //
// rtype GetWeight(void) const; //
// - rtype is any type convertible to Double_t //
// //
// Find function template parameter T has to provide these functions: //
// (in addition to above requirements) //
// rtype GetDist(Float_t, UInt_t) const; //
// - rtype is any type convertible to Float_t //
// rtype GetDist(const T &) const; //
// - rtype is any type convertible to Float_t //
// //
// where T::GetDist(Float_t, UInt_t) <= T::GetDist(const T &) //
// for any pair of events and any variable number for these events //
// //
//////////////////////////////////////////////////////////////////////////
namespace TMVA
{
namespace kNN
{
template <class T>
class Node
{
public:
Node(const Node *parent, const T &event, Int_t mod);
~Node();
const Node* Add(const T &event, UInt_t depth);
void SetNodeL(Node *node);
void SetNodeR(Node *node);
const T& GetEvent() const;
const Node* GetNodeL() const;
const Node* GetNodeR() const;
const Node* GetNodeP() const;
Double_t GetWeight() const;
Float_t GetVarDis() const;
Float_t GetVarMin() const;
Float_t GetVarMax() const;
UInt_t GetMod() const;
void Print() const;
void Print(std::ostream& os, const std::string &offset = "") const;
private:
// these methods are private and not implemented by design
// use provided public constructor for all uses of this template class
Node();
Node(const Node &);
const Node& operator=(const Node &);
private:
const Node* fNodeP;
Node* fNodeL;
Node* fNodeR;
const T fEvent;
const Float_t fVarDis;
Float_t fVarMin;
Float_t fVarMax;
const UInt_t fMod;
};
// recursive search for k-nearest neighbor: k = nfind
template<class T>
UInt_t Find(std::list<std::pair<const Node<T> *, Float_t> > &nlist,
const Node<T> *node, const T &event, UInt_t nfind);
// recursive search for k-nearest neighbor
// find k events with sum of event weights >= nfind
template<class T>
UInt_t Find(std::list<std::pair<const Node<T> *, Float_t> > &nlist,
const Node<T> *node, const T &event, Double_t nfind, Double_t ncurr);
// recursively travel upward until root node is reached
template <class T>
UInt_t Depth(const Node<T> *node);
// prInt_t node content and content of its children
//template <class T>
//std::ostream& operator<<(std::ostream& os, const Node<T> &node);
//
// Inlined functions for Node template
//
template <class T>
inline void Node<T>::SetNodeL(Node<T> *node)
{
fNodeL = node;
}
template <class T>
inline void Node<T>::SetNodeR(Node<T> *node)
{
fNodeR = node;
}
template <class T>
inline const T& Node<T>::GetEvent() const
{
return fEvent;
}
template <class T>
inline const Node<T>* Node<T>::GetNodeL() const
{
return fNodeL;
}
template <class T>
inline const Node<T>* Node<T>::GetNodeR() const
{
return fNodeR;
}
template <class T>
inline const Node<T>* Node<T>::GetNodeP() const
{
return fNodeP;
}
template <class T>
inline Double_t Node<T>::GetWeight() const
{
return fEvent.GetWeight();
}
template <class T>
inline Float_t Node<T>::GetVarDis() const
{
return fVarDis;
}
template <class T>
inline Float_t Node<T>::GetVarMin() const
{
return fVarMin;
}
template <class T>
inline Float_t Node<T>::GetVarMax() const
{
return fVarMax;
}
template <class T>
inline UInt_t Node<T>::GetMod() const
{
return fMod;
}
//
// Inlined global function(s)
//
template <class T>
inline UInt_t Depth(const Node<T> *node)
{
if (!node) return 0;
else return Depth(node->GetNodeP()) + 1;
}
} // end of kNN namespace
} // end of TMVA namespace
//-------------------------------------------------------------------------------------------
template<class T>
TMVA::kNN::Node<T>::Node(const Node<T> *parent, const T &event, const Int_t mod)
:fNodeP(parent),
fNodeL(0),
fNodeR(0),
fEvent(event),
fVarDis(event.GetVar(mod)),
fVarMin(fVarDis),
fVarMax(fVarDis),
fMod(mod)
{}
//-------------------------------------------------------------------------------------------
template<class T>
TMVA::kNN::Node<T>::~Node()
{
if (fNodeL) delete fNodeL;
if (fNodeR) delete fNodeR;
}
//-------------------------------------------------------------------------------------------
template<class T>
const TMVA::kNN::Node<T>* TMVA::kNN::Node<T>::Add(const T &event, const UInt_t depth)
{
// This is Node member function that adds a new node to a binary tree.
// each node contains maximum and minimum values of splitting variable
// left or right nodes are added based on value of splitting variable
assert(fMod == depth % event.GetNVar() && "Wrong recursive depth in Node<>::Add");
const Float_t value = event.GetVar(fMod);
fVarMin = std::min(fVarMin, value);
fVarMax = std::max(fVarMax, value);
Node<T> *node = 0;
if (value < fVarDis) {
if (fNodeL)
{
return fNodeL->Add(event, depth + 1);
}
else {
fNodeL = new Node<T>(this, event, (depth + 1) % event.GetNVar());
node = fNodeL;
}
}
else {
if (fNodeR) {
return fNodeR->Add(event, depth + 1);
}
else {
fNodeR = new Node<T>(this, event, (depth + 1) % event.GetNVar());
node = fNodeR;
}
}
return node;
}
//-------------------------------------------------------------------------------------------
template<class T>
void TMVA::kNN::Node<T>::Print() const
{
Print(std::cout);
}
//-------------------------------------------------------------------------------------------
template<class T>
void TMVA::kNN::Node<T>::Print(std::ostream& os, const std::string &offset) const
{
os << offset << "-----------------------------------------------------------" << std::endl;
os << offset << "Node: mod " << fMod
<< " at " << fVarDis
<< " with weight: " << GetWeight() << std::endl
<< offset << fEvent;
if (fNodeL) {
os << offset << "Has left node " << std::endl;
}
if (fNodeR) {
os << offset << "Has right node" << std::endl;
}
if (fNodeL) {
os << offset << "PrInt_t left node " << std::endl;
fNodeL->Print(os, offset + " ");
}
if (fNodeR) {
os << offset << "PrInt_t right node" << std::endl;
fNodeR->Print(os, offset + " ");
}
if (!fNodeL && !fNodeR) {
os << std::endl;
}
}
//-------------------------------------------------------------------------------------------
template<class T>
UInt_t TMVA::kNN::Find(std::list<std::pair<const TMVA::kNN::Node<T> *, Float_t> > &nlist,
const TMVA::kNN::Node<T> *node, const T &event, const UInt_t nfind)
{
// This is a global templated function that searches for k-nearest neighbors.
// list contains k or less nodes that are closest to event.
// only nodes with positive weights are added to list.
// each node contains maximum and minimum values of splitting variable
// for all its children - this range is checked to avoid descending into
// nodes that are defintely outside current minimum neighbourhood.
//
// This function should be modified with care.
//
if (!node || nfind < 1) {
return 0;
}
const Float_t value = event.GetVar(node->GetMod());
if (node->GetWeight() > 0.0) {
Float_t max_dist = 0.0;
if (!nlist.empty()) {
max_dist = nlist.back().second;
if (nlist.size() == nfind) {
if (value > node->GetVarMax() &&
event.GetDist(node->GetVarMax(), node->GetMod()) > max_dist) {
return 0;
}
if (value < node->GetVarMin() &&
event.GetDist(node->GetVarMin(), node->GetMod()) > max_dist) {
return 0;
}
}
}
const Float_t distance = event.GetDist(node->GetEvent());
Bool_t insert_this = kFALSE;
Bool_t remove_back = kFALSE;
if (nlist.size() < nfind) {
insert_this = kTRUE;
}
else if (nlist.size() == nfind) {
if (distance < max_dist) {
insert_this = kTRUE;
remove_back = kTRUE;
}
}
else {
std::cerr << "TMVA::kNN::Find() - logic error in recursive procedure" << std::endl;
return 1;
}
if (insert_this) {
// need typename keyword because qualified dependent names
// are not valid types unless preceded by 'typename'.
typename std::list<std::pair<const Node<T> *, Float_t> >::iterator lit = nlist.begin();
// find a place where current node should be inserted
for (; lit != nlist.end(); ++lit) {
if (distance < lit->second) {
break;
}
else {
continue;
}
}
nlist.insert(lit, std::pair<const Node<T> *, Float_t>(node, distance));
if (remove_back) {
nlist.pop_back();
}
}
}
UInt_t count = 1;
if (node->GetNodeL() && node->GetNodeR()) {
if (value < node->GetVarDis()) {
count += Find(nlist, node->GetNodeL(), event, nfind);
count += Find(nlist, node->GetNodeR(), event, nfind);
}
else {
count += Find(nlist, node->GetNodeR(), event, nfind);
count += Find(nlist, node->GetNodeL(), event, nfind);
}
}
else {
if (node->GetNodeL()) {
count += Find(nlist, node->GetNodeL(), event, nfind);
}
if (node->GetNodeR()) {
count += Find(nlist, node->GetNodeR(), event, nfind);
}
}
return count;
}
//-------------------------------------------------------------------------------------------
template<class T>
UInt_t TMVA::kNN::Find(std::list<std::pair<const TMVA::kNN::Node<T> *, Float_t> > &nlist,
const TMVA::kNN::Node<T> *node, const T &event, const Double_t nfind, Double_t ncurr)
{
// This is a global templated function that searches for k-nearest neighbors.
// list contains all nodes that are closest to event
// and have sum of event weights >= nfind.
// Only nodes with positive weights are added to list.
// Requirement for used classes:
// - each node contains maximum and minimum values of splitting variable
// for all its children
// - min and max range is checked to avoid descending into
// nodes that are defintely outside current minimum neighbourhood.
//
// This function should be modified with care.
//
if (!node || !(nfind < 0.0)) {
return 0;
}
const Float_t value = event.GetVar(node->GetMod());
if (node->GetWeight() > 0.0) {
Float_t max_dist = 0.0;
if (!nlist.empty()) {
max_dist = nlist.back().second;
if (!(ncurr < nfind)) {
if (value > node->GetVarMax() &&
event.GetDist(node->GetVarMax(), node->GetMod()) > max_dist) {
return 0;
}
if (value < node->GetVarMin() &&
event.GetDist(node->GetVarMin(), node->GetMod()) > max_dist) {
return 0;
}
}
}
const Float_t distance = event.GetDist(node->GetEvent());
Bool_t insert_this = kFALSE;
if (ncurr < nfind) {
insert_this = kTRUE;
}
else if (!nlist.empty()) {
if (distance < max_dist) {
insert_this = kTRUE;
}
}
else {
std::cerr << "TMVA::kNN::Find() - logic error in recursive procedure" << std::endl;
return 1;
}
if (insert_this) {
// (re)compute total current weight when inserting a new node
ncurr = 0;
// need typename keyword because qualified dependent names
// are not valid types unless preceded by 'typename'.
typename std::list<std::pair<const Node<T> *, Float_t> >::iterator lit = nlist.begin();
// find a place where current node should be inserted
for (; lit != nlist.end(); ++lit) {
if (distance < lit->second) {
break;
}
ncurr += lit -> first -> GetWeight();
}
lit = nlist.insert(lit, std::pair<const Node<T> *, Float_t>(node, distance));
for (; lit != nlist.end(); ++lit) {
ncurr += lit -> first -> GetWeight();
if (!(ncurr < nfind)) {
++lit;
break;
}
}
if(lit != nlist.end())
{
nlist.erase(lit, nlist.end());
}
}
}
UInt_t count = 1;
if (node->GetNodeL() && node->GetNodeR()) {
if (value < node->GetVarDis()) {
count += Find(nlist, node->GetNodeL(), event, nfind, ncurr);
count += Find(nlist, node->GetNodeR(), event, nfind, ncurr);
}
else {
count += Find(nlist, node->GetNodeR(), event, nfind, ncurr);
count += Find(nlist, node->GetNodeL(), event, nfind, ncurr);
}
}
else {
if (node->GetNodeL()) {
count += Find(nlist, node->GetNodeL(), event, nfind, ncurr);
}
if (node->GetNodeR()) {
count += Find(nlist, node->GetNodeR(), event, nfind, ncurr);
}
}
return count;
}
#endif
|