87 std::cerr <<
"Distance: two events have different dimensions" << std::endl;
92 for (
UInt_t ivar = 0; ivar < nvar; ++ivar) {
133 Int_t dp = os.precision();
143 os << std::setfill(
' ') << std::setw(5) << std::setprecision(3) <<
GetVar(ivar);
150 os <<
" no variables";
152 os << std::setprecision(dp);
209 Log() <<
kFATAL <<
"<Add> Cannot add event: tree is already built" <<
Endl;
214 fDimn =
event.GetNVar();
217 Log() <<
kFATAL <<
"ModulekNN::Add() - number of dimension does not match previous events" <<
Endl;
227 std::map<Short_t, UInt_t>::iterator cit =
fCount.find(event.
GetType());
228 if (cit ==
fCount.end()) {
229 fCount[
event.GetType()] = 1;
242 Log() <<
kFATAL <<
"ModulekNN::Fill - tree has already been created" <<
Endl;
249 if (option.find(
"trim") != std::string::npos) {
250 for (std::map<Short_t, UInt_t>::const_iterator it =
fCount.begin(); it !=
fCount.end(); ++it) {
251 if (min == 0 || min > it->second) {
256 Log() <<
kINFO <<
"<Fill> Will trim all event types to " << min <<
" events" <<
Endl;
263 for (EventVec::const_iterator event =
fEvent.begin();
event !=
fEvent.end(); ++event) {
264 std::map<Short_t, UInt_t>::iterator cit =
fCount.find(event->GetType());
265 if (cit ==
fCount.end()) {
266 fCount[
event->GetType()] = 1;
268 else if (cit->second < min) {
276 fVar[d].push_back(event->GetVar(d));
279 evec.push_back(*event);
282 Log() <<
kINFO <<
"<Fill> Erased " <<
fEvent.size() - evec.size() <<
" events" <<
Endl;
291 for (VarMap::iterator it =
fVar.begin(); it !=
fVar.end(); ++it) {
292 std::sort((it->second).begin(), (it->second).end());
295 if (option.find(
"metric") != std::string::npos && ifrac > 0) {
300 for (VarMap::iterator it =
fVar.begin(); it !=
fVar.end(); ++it) {
301 std::sort((it->second).begin(), (it->second).end());
312 Log() <<
kFATAL <<
"ModulekNN::Fill() - failed to create tree" <<
Endl;
316 for (EventVec::const_iterator event =
fEvent.begin();
event !=
fEvent.end(); ++event) {
317 fTree->Add(*event, 0);
319 std::map<Short_t, UInt_t>::iterator cit =
fCount.find(event->GetType());
320 if (cit ==
fCount.end()) {
321 fCount[
event->GetType()] = 1;
328 for (std::map<Short_t, UInt_t>::const_iterator it =
fCount.begin(); it !=
fCount.end(); ++it) {
329 Log() <<
kINFO <<
"<Fill> Class " << it->first <<
" has " << std::setw(8)
330 << it->second <<
" events" <<
Endl;
345 Log() <<
kFATAL <<
"ModulekNN::Find() - tree has not been filled" <<
Endl;
349 Log() <<
kFATAL <<
"ModulekNN::Find() - number of dimension does not match training events" <<
Endl;
353 Log() <<
kFATAL <<
"ModulekNN::Find() - requested 0 nearest neighbors" <<
Endl;
360 event =
Scale(event);
367 if(option.find(
"weight") != std::string::npos)
392 typedef std::map<Short_t, UInt_t>::const_iterator const_iterator;
393 TTHREAD_TLS_DECL_ARG(const_iterator,cit,
fCount.end());
395 if (cit ==
fCount.end()) {
401 if (option ==
"flat") {
404 VarMap::const_iterator vit =
fVar.find(d);
405 if (vit ==
fVar.end()) {
409 const std::vector<Double_t> &vvec = vit->second;
416 const VarType min = vvec.front();
417 const VarType max = vvec.back();
418 const VarType width = max - min;
420 if (width < 0.0 || width > 0.0) {
428 const Event event(dvec, 1.0, etype);
450 const UInt_t size = (
fVar.begin()->second).size();
452 Log() <<
kWARNING <<
"<Optimize> Cannot build a tree without events" <<
Endl;
456 VarMap::const_iterator it =
fVar.begin();
457 for (; it !=
fVar.end(); ++it) {
458 if ((it->second).size() != size) {
459 Log() <<
kWARNING <<
"<Optimize> # of variables doesn't match between dimensions" <<
Endl;
465 Log() <<
kWARNING <<
"<Optimize> Optimization depth exceeds number of events" <<
Endl;
469 Log() <<
kHEADER <<
"Optimizing tree for " <<
fDimn <<
" variables with " << size <<
" values" <<
Endl;
471 std::vector<Node<Event> *> pvec, cvec;
474 if (it ==
fVar.end() || (it->second).size() < 2) {
483 pvec.push_back(tree);
485 for (
UInt_t depth = 1; depth < odepth; ++depth) {
488 VarMap::const_iterator vit =
fVar.find(mod);
489 if (vit ==
fVar.end()) {
493 const std::vector<Double_t> &dvec = vit->second;
495 if (dvec.size() < 2) {
501 for (std::vector<
Node<Event> *>::iterator pit = pvec.begin(); pit != pvec.end(); ++pit) {
504 const VarType lmedian = dvec[size*ichild/(2*pvec.size() + 1)];
507 const VarType rmedian = dvec[size*ichild/(2*pvec.size() + 1)];
519 cvec.push_back(lchild);
520 cvec.push_back(rchild);
542 Log() <<
kFATAL <<
"ModulekNN::ComputeMetric - fraction can not exceed 100%" <<
Endl;
546 Log() <<
kFATAL <<
"ModulekNN::ComputeMetric - metric is already computed" <<
Endl;
549 if (
fEvent.size() < 100) {
550 Log() <<
kFATAL <<
"ModulekNN::ComputeMetric - number of events is too small" <<
Endl;
554 const UInt_t lfrac = (100 - ifrac)/2;
555 const UInt_t rfrac = 100 - (100 - ifrac)/2;
557 Log() <<
kINFO <<
"Computing scale factor for 1d distributions: " 558 <<
"(ifrac, bottom, top) = (" << ifrac <<
"%, " << lfrac <<
"%, " << rfrac <<
"%)" <<
Endl;
562 for (VarMap::const_iterator vit =
fVar.begin(); vit !=
fVar.end(); ++vit) {
563 const std::vector<Double_t> &dvec = vit->second;
565 std::vector<Double_t>::const_iterator beg_it = dvec.end();
566 std::vector<Double_t>::const_iterator end_it = dvec.end();
569 for (std::vector<Double_t>::const_iterator dit = dvec.begin(); dit != dvec.end(); ++dit, ++
dist) {
571 if ((100*dist)/dvec.size() == lfrac && beg_it == dvec.end()) {
575 if ((100*dist)/dvec.size() == rfrac && end_it == dvec.end()) {
580 if (beg_it == dvec.end() || end_it == dvec.end()) {
581 beg_it = dvec.begin();
584 assert(beg_it != end_it &&
"Empty vector");
592 if (!(lpos < rpos)) {
593 Log() <<
kFATAL <<
"ModulekNN::ComputeMetric() - min value is greater than max value" <<
Endl;
609 for (
UInt_t ievent = 0; ievent <
fEvent.size(); ++ievent) {
613 fVar[ivar].push_back(
fEvent[ievent].GetVar(ivar));
629 Log() <<
kFATAL <<
"ModulekNN::Scale() - mismatched metric and event size" <<
Endl;
635 for (
UInt_t ivar = 0; ivar <
event.GetNVar(); ++ivar) {
636 std::map<int, Double_t>::const_iterator fit =
fVarScale.find(ivar);
638 Log() <<
kFATAL <<
"ModulekNN::Scale() - failed to find scale for " << ivar <<
Endl;
642 if (fit->second > 0.0) {
643 vvec[ivar] =
event.GetVar(ivar)/fit->second;
646 Log() <<
kFATAL <<
"Variable " << ivar <<
" has zero width" <<
Endl;
650 return Event(vvec, event.
GetWeight(),
event.GetType(),
event.GetTargets());
666 os <<
"----------------------------------------------------------------------"<< std::endl;
667 os <<
"Printing knn result" << std::endl;
672 std::map<Short_t, Double_t> min, max;
674 os <<
"Printing " <<
fkNNList.size() <<
" nearest neighbors" << std::endl;
675 for (List::const_iterator it =
fkNNList.begin(); it !=
fkNNList.end(); ++it) {
676 os << ++count <<
": " << it->second <<
": " << it->first->GetEvent() << std::endl;
678 const Event &
event = it->first->GetEvent();
679 for (
UShort_t ivar = 0; ivar <
event.GetNVar(); ++ivar) {
680 if (min.find(ivar) == min.end()) {
681 min[ivar] =
event.GetVar(ivar);
683 else if (min[ivar] > event.GetVar(ivar)) {
684 min[ivar] =
event.GetVar(ivar);
687 if (max.find(ivar) == max.end()) {
688 max[ivar] =
event.GetVar(ivar);
690 else if (max[ivar] < event.GetVar(ivar)) {
691 max[ivar] =
event.GetVar(ivar);
696 if (min.size() == max.size()) {
697 for (std::map<Short_t, Double_t>::const_iterator mit = min.begin(); mit != min.end(); ++mit) {
699 Log() <<
kINFO <<
"(var, min, max) = (" << i <<
"," << min[i] <<
", " << max[i] <<
")" <<
Endl;
703 os <<
"----------------------------------------------------------------------" << std::endl;
Event()
default constructor
double dist(Rotation3D const &r1, Rotation3D const &r2)
static long int sum(long int i)
MsgLogger & Endl(MsgLogger &ml)
ModulekNN()
default constructor
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...
std::ostream & operator<<(std::ostream &os, const Event &event)
streamer
LongDouble_t Power(LongDouble_t x, LongDouble_t y)
std::map< Int_t, Double_t > fVarScale
std::vector< TMVA::kNN::Event > EventVec
const VarVec & GetTargets() const
void SetTargets(const VarVec &tvec)
Bool_t Fill(const UShort_t odepth, UInt_t ifrac, const std::string &option="")
fill the tree
const VarVec & GetVars() const
void SetNodeL(Node *node)
void Add(const Event &event)
add an event to tree
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...
Double_t GetWeight() const
VarType GetDist(VarType var, UInt_t ivar) const
static TRandom3 & GetRndmThreadLocal()
VarType GetVar(UInt_t i) const
std::map< Short_t, UInt_t > fCount
Node< Event > * Optimize(UInt_t optimize_depth)
Optimize() balances binary tree for first odepth levels for each depth we split sorted depth % dimens...
void SetNodeR(Node *node)
std::vector< VarType > VarVec
void ComputeMetric(UInt_t ifrac)
compute scale factor for each variable (dimension) so that distance is computed uniformely along each...