90 const UInt_t nvar = GetNVar();
92 if (nvar !=
other.GetNVar()) {
93 std::cerr <<
"Distance: two events have different dimensions" << std::endl;
149 os << std::setfill(
' ') << std::setw(5) << std::setprecision(3) << GetVar(
ivar);
156 os <<
" no variables";
158 os << std::setprecision(
dp);
186 delete fTree; fTree = 0;
215 Log() << kFATAL <<
"<Add> Cannot add event: tree is already built" <<
Endl;
220 fDimn =
event.GetNVar();
222 else if (fDimn != event.
GetNVar()) {
223 Log() << kFATAL <<
"ModulekNN::Add() - number of dimension does not match previous events" <<
Endl;
227 fEvent.push_back(event);
233 std::map<Short_t, UInt_t>::iterator
cit = fCount.find(event.
GetType());
234 if (
cit == fCount.
end()) {
235 fCount[
event.GetType()] = 1;
248 Log() << kFATAL <<
"ModulekNN::Fill - tree has already been created" <<
Endl;
255 if (
option.find(
"trim") != std::string::npos) {
256 for (std::map<Short_t, UInt_t>::const_iterator it = fCount.begin(); it != fCount.end(); ++it) {
257 if (min == 0 || min > it->second) {
262 Log() << kINFO <<
"<Fill> Will trim all event types to " << min <<
" events" <<
Endl;
269 for (EventVec::const_iterator event = fEvent.begin();
event != fEvent.end(); ++event) {
270 std::map<Short_t, UInt_t>::iterator
cit = fCount.find(event->
GetType());
271 if (
cit == fCount.
end()) {
272 fCount[
event->GetType()] = 1;
274 else if (
cit->second < min) {
282 fVar[
d].push_back(event->
GetVar(
d));
285 evec.push_back(*event);
288 Log() << kINFO <<
"<Fill> Erased " << fEvent.size() -
evec.size() <<
" events" <<
Endl;
297 for (VarMap::iterator it = fVar.begin(); it != fVar.end(); ++it) {
298 std::sort((it->second).begin(), (it->second).end());
301 if (
option.find(
"metric") != std::string::npos &&
ifrac > 0) {
302 ComputeMetric(
ifrac);
306 for (VarMap::iterator it = fVar.begin(); it != fVar.end(); ++it) {
307 std::sort((it->second).begin(), (it->second).end());
318 Log() << kFATAL <<
"ModulekNN::Fill() - failed to create tree" <<
Endl;
322 for (EventVec::const_iterator event = fEvent.begin();
event != fEvent.end(); ++event) {
323 fTree->Add(*event, 0);
325 std::map<Short_t, UInt_t>::iterator
cit = fCount.find(event->
GetType());
326 if (
cit == fCount.
end()) {
327 fCount[
event->GetType()] = 1;
334 for (std::map<Short_t, UInt_t>::const_iterator it = fCount.begin(); it != fCount.end(); ++it) {
335 Log() << kINFO <<
"<Fill> Class " << it->first <<
" has " << std::setw(8)
336 << it->second <<
" events" <<
Endl;
351 Log() << kFATAL <<
"ModulekNN::Find() - tree has not been filled" <<
Endl;
354 if (fDimn != event.
GetNVar()) {
355 Log() << kFATAL <<
"ModulekNN::Find() - number of dimension does not match training events" <<
Endl;
359 Log() << kFATAL <<
"ModulekNN::Find() - requested 0 nearest neighbors" <<
Endl;
365 if (!fVarScale.empty()) {
366 event = Scale(event);
373 if(
option.find(
"weight") != std::string::npos)
378 kNN::Find<kNN::Event>(fkNNList, fTree, event,
Double_t(
nfind), 0.0);
384 kNN::Find<kNN::Event>(fkNNList, fTree, event,
nfind);
395 if (fCount.empty() || !fTree) {
398 typedef std::map<Short_t, UInt_t>::const_iterator const_iterator;
401 if (
cit == fCount.
end()) {
410 VarMap::const_iterator
vit = fVar.find(
d);
415 const std::vector<Double_t> &
vvec =
vit->second;
422 const VarType min =
vvec.front();
423 const VarType max =
vvec.back();
424 const VarType
width = max - min;
427 dvec.push_back(min +
width*GetRndmThreadLocal().Rndm());
451 if (fVar.empty() || fDimn != fVar.size()) {
452 Log() << kWARNING <<
"<Optimize> Cannot build a tree" <<
Endl;
458 Log() << kWARNING <<
"<Optimize> Cannot build a tree without events" <<
Endl;
462 VarMap::const_iterator it = fVar.begin();
463 for (; it != fVar.end(); ++it) {
464 if ((it->second).size() !=
size) {
465 Log() << kWARNING <<
"<Optimize> # of variables doesn't match between dimensions" <<
Endl;
471 Log() << kWARNING <<
"<Optimize> Optimization depth exceeds number of events" <<
Endl;
475 Log() << kHEADER <<
"Optimizing tree for " << fDimn <<
" variables with " <<
size <<
" values" <<
Endl;
477 std::vector<Node<Event> *>
pvec,
cvec;
480 if (it == fVar.end() || (it->second).size() < 2) {
481 Log() << kWARNING <<
"<Optimize> Missing 0 variable" <<
Endl;
489 pvec.push_back(tree);
494 VarMap::const_iterator
vit = fVar.find(
mod);
496 Log() << kFATAL <<
"Missing " <<
mod <<
" variable" <<
Endl;
499 const std::vector<Double_t> &
dvec =
vit->second;
501 if (
dvec.size() < 2) {
502 Log() << kFATAL <<
"Missing " <<
mod <<
" variable" <<
Endl;
548 Log() << kFATAL <<
"ModulekNN::ComputeMetric - fraction can not exceed 100%" <<
Endl;
551 if (!fVarScale.empty()) {
552 Log() << kFATAL <<
"ModulekNN::ComputeMetric - metric is already computed" <<
Endl;
555 if (fEvent.size() < 100) {
556 Log() << kFATAL <<
"ModulekNN::ComputeMetric - number of events is too small" <<
Endl;
563 Log() << kINFO <<
"Computing scale factor for 1d distributions: "
564 <<
"(ifrac, bottom, top) = (" <<
ifrac <<
"%, " <<
lfrac <<
"%, " <<
rfrac <<
"%)" <<
Endl;
569 const std::vector<Double_t> &
dvec =
vit->second;
599 Log() << kFATAL <<
"ModulekNN::ComputeMetric() - min value is greater than max value" <<
Endl;
630 if (fVarScale.empty()) {
634 if (event.
GetNVar() != fVarScale.size()) {
635 Log() << kFATAL <<
"ModulekNN::Scale() - mismatched metric and event size" <<
Endl;
642 std::map<int, Double_t>::const_iterator fit = fVarScale.find(
ivar);
643 if (fit == fVarScale.end()) {
644 Log() << kFATAL <<
"ModulekNN::Scale() - failed to find scale for " <<
ivar <<
Endl;
648 if (fit->second > 0.0) {
652 Log() << kFATAL <<
"Variable " <<
ivar <<
" has zero width" <<
Endl;
672 os <<
"----------------------------------------------------------------------"<< std::endl;
673 os <<
"Printing knn result" << std::endl;
674 os << fkNNEvent << std::endl;
678 std::map<Short_t, Double_t> min, max;
680 os <<
"Printing " << fkNNList.size() <<
" nearest neighbors" << std::endl;
681 for (List::const_iterator it = fkNNList.begin(); it != fkNNList.end(); ++it) {
682 os << ++count <<
": " << it->second <<
": " << it->first->GetEvent() << std::endl;
684 const Event &
event = it->first->GetEvent();
686 if (min.find(
ivar) == min.end()) {
693 if (max.find(
ivar) == max.end()) {
702 if (min.size() == max.size()) {
703 for (std::map<Short_t, Double_t>::const_iterator
mit = min.
begin();
mit != min.
end(); ++
mit) {
705 Log() << kINFO <<
"(var, min, max) = (" << i <<
"," << min[i] <<
", " << max[i] <<
")" <<
Endl;
709 os <<
"----------------------------------------------------------------------" << std::endl;
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
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 Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
void Print(GNN_Data &d, std::string txt="")
const_iterator begin() const
const_iterator end() const
ostringstream derivative to redirect and format output
VarType GetDist(VarType var, UInt_t ivar) const
void SetTargets(const VarVec &tvec)
const VarVec & GetTargets() const
Double_t GetWeight() const
Event()
default constructor
VarType GetVar(UInt_t i) const
const VarVec & GetVars() const
Bool_t Fill(const UShort_t odepth, UInt_t ifrac, const std::string &option="")
fill the tree
Node< Event > * Optimize(UInt_t optimize_depth)
Optimize() balances binary tree for first odepth levels for each depth we split sorted depth % dimens...
ModulekNN()
default constructor
Bool_t Find(Event event, UInt_t nfind=100, const std::string &option="count") const
find in tree if tree has been filled then search for nfind closest events if metic (fVarScale map) is...
const Event Scale(const Event &event) const
scale each event variable so that rms of variables is approximately 1.0 this allows comparisons of va...
void ComputeMetric(UInt_t ifrac)
compute scale factor for each variable (dimension) so that distance is computed uniformly along each ...
void Add(const Event &event)
add an event to tree
This file contains binary tree and global function template that searches tree for k-nearest neigbors...
UInt_t Find(std::list< std::pair< const Node< T > *, Float_t > > &nlist, const Node< T > *node, const T &event, UInt_t nfind)
std::ostream & operator<<(std::ostream &os, const Event &event)
MsgLogger & Endl(MsgLogger &ml)
LongDouble_t Power(LongDouble_t x, LongDouble_t y)
Returns x raised to the power y.
static uint64_t sum(uint64_t i)