85 const UInt_t nvar = GetNVar();
88 std::cerr <<
"Distance: two events have different dimensions" << std::endl;
93 for (
UInt_t ivar = 0; ivar < nvar; ++ivar) {
94 sum += GetDist(other.
GetVar(ivar), ivar);
134 Int_t dp = os.precision();
136 for (
UInt_t ivar = 0; ivar != GetNVar(); ++ivar) {
144 os << std::setfill(
' ') << std::setw(5) << std::setprecision(3) << GetVar(ivar);
151 os <<
" no variables";
153 os << std::setprecision(dp);
181 delete fTree; fTree = 0;
210 Log() <<
kFATAL <<
"<Add> Cannot add event: tree is already built" <<
Endl;
215 fDimn =
event.GetNVar();
217 else if (fDimn != event.
GetNVar()) {
218 Log() <<
kFATAL <<
"ModulekNN::Add() - number of dimension does not match previous events" <<
Endl;
222 fEvent.push_back(event);
224 for (
UInt_t ivar = 0; ivar < fDimn; ++ivar) {
225 fVar[ivar].push_back(event.
GetVar(ivar));
228 std::map<Short_t, UInt_t>::iterator cit = fCount.find(event.
GetType());
229 if (cit == fCount.end()) {
230 fCount[
event.GetType()] = 1;
243 Log() <<
kFATAL <<
"ModulekNN::Fill - tree has already been created" <<
Endl;
250 if (option.find(
"trim") != std::string::npos) {
251 for (std::map<Short_t, UInt_t>::const_iterator it = fCount.begin(); it != fCount.end(); ++it) {
252 if (min == 0 || min > it->second) {
257 Log() <<
kINFO <<
"<Fill> Will trim all event types to " << min <<
" events" <<
Endl;
264 for (EventVec::const_iterator event = fEvent.begin();
event != fEvent.end(); ++event) {
265 std::map<Short_t, UInt_t>::iterator cit = fCount.find(event->GetType());
266 if (cit == fCount.end()) {
267 fCount[
event->GetType()] = 1;
269 else if (cit->second < min) {
277 fVar[
d].push_back(event->GetVar(
d));
280 evec.push_back(*event);
283 Log() <<
kINFO <<
"<Fill> Erased " << fEvent.size() - evec.size() <<
" events" <<
Endl;
292 for (VarMap::iterator it = fVar.begin(); it != fVar.end(); ++it) {
293 std::sort((it->second).begin(), (it->second).end());
296 if (option.find(
"metric") != std::string::npos && ifrac > 0) {
297 ComputeMetric(ifrac);
301 for (VarMap::iterator it = fVar.begin(); it != fVar.end(); ++it) {
302 std::sort((it->second).begin(), (it->second).end());
313 Log() <<
kFATAL <<
"ModulekNN::Fill() - failed to create tree" <<
Endl;
317 for (EventVec::const_iterator event = fEvent.begin();
event != fEvent.end(); ++event) {
318 fTree->Add(*event, 0);
320 std::map<Short_t, UInt_t>::iterator cit = fCount.find(event->GetType());
321 if (cit == fCount.end()) {
322 fCount[
event->GetType()] = 1;
329 for (std::map<Short_t, UInt_t>::const_iterator it = fCount.begin(); it != fCount.end(); ++it) {
330 Log() <<
kINFO <<
"<Fill> Class " << it->first <<
" has " << std::setw(8)
331 << it->second <<
" events" <<
Endl;
346 Log() <<
kFATAL <<
"ModulekNN::Find() - tree has not been filled" <<
Endl;
349 if (fDimn != event.
GetNVar()) {
350 Log() <<
kFATAL <<
"ModulekNN::Find() - number of dimension does not match training events" <<
Endl;
354 Log() <<
kFATAL <<
"ModulekNN::Find() - requested 0 nearest neighbors" <<
Endl;
360 if (!fVarScale.empty()) {
361 event = Scale(event);
368 if(option.find(
"weight") != std::string::npos)
373 kNN::Find<kNN::Event>(fkNNList, fTree, event,
Double_t(nfind), 0.0);
379 kNN::Find<kNN::Event>(fkNNList, fTree, event, nfind);
390 if (fCount.empty() || !fTree) {
393 typedef std::map<Short_t, UInt_t>::const_iterator const_iterator;
394 TTHREAD_TLS_DECL_ARG(const_iterator,cit,fCount.end());
396 if (cit == fCount.end()) {
397 cit = fCount.begin();
402 if (option ==
"flat") {
405 VarMap::const_iterator vit = fVar.find(
d);
406 if (vit == fVar.end()) {
410 const std::vector<Double_t> &vvec = vit->second;
421 if (width < 0.0 || width > 0.0) {
422 dvec.push_back(min + width*GetRndmThreadLocal().Rndm());
429 const Event event(dvec, 1.0, etype);
446 if (fVar.empty() || fDimn != fVar.size()) {
451 const UInt_t size = (fVar.begin()->second).size();
453 Log() <<
kWARNING <<
"<Optimize> Cannot build a tree without events" <<
Endl;
457 VarMap::const_iterator it = fVar.begin();
458 for (; it != fVar.end(); ++it) {
459 if ((it->second).size() != size) {
460 Log() <<
kWARNING <<
"<Optimize> # of variables doesn't match between dimensions" <<
Endl;
465 if (
double(fDimn*size) <
TMath::Power(2.0,
double(odepth))) {
466 Log() <<
kWARNING <<
"<Optimize> Optimization depth exceeds number of events" <<
Endl;
470 Log() <<
kINFO <<
"Optimizing tree for " << fDimn <<
" variables with " << size <<
" values" <<
Endl;
472 std::vector<Node<Event> *> pvec, cvec;
475 if (it == fVar.end() || (it->second).size() < 2) {
480 const Event pevent(
VarVec(fDimn, (it->second)[size/2]), -1.0, -1);
484 pvec.push_back(tree);
486 for (
UInt_t depth = 1; depth < odepth; ++depth) {
487 const UInt_t mod = depth % fDimn;
489 VarMap::const_iterator vit = fVar.find(mod);
490 if (vit == fVar.end()) {
494 const std::vector<Double_t> &dvec = vit->second;
496 if (dvec.size() < 2) {
502 for (std::vector<
Node<Event> *>::iterator pit = pvec.begin(); pit != pvec.end(); ++pit) {
505 const VarType lmedian = dvec[size*ichild/(2*pvec.size() + 1)];
508 const VarType rmedian = dvec[size*ichild/(2*pvec.size() + 1)];
511 const Event levent(
VarVec(fDimn, lmedian), -1.0, -1);
512 const Event revent(
VarVec(fDimn, rmedian), -1.0, -1);
520 cvec.push_back(lchild);
521 cvec.push_back(rchild);
543 Log() <<
kFATAL <<
"ModulekNN::ComputeMetric - fraction can not exceed 100%" <<
Endl;
546 if (!fVarScale.empty()) {
547 Log() <<
kFATAL <<
"ModulekNN::ComputeMetric - metric is already computed" <<
Endl;
550 if (fEvent.size() < 100) {
551 Log() <<
kFATAL <<
"ModulekNN::ComputeMetric - number of events is too small" <<
Endl;
555 const UInt_t lfrac = (100 - ifrac)/2;
556 const UInt_t rfrac = 100 - (100 - ifrac)/2;
558 Log() <<
kINFO <<
"Computing scale factor for 1d distributions: "
559 <<
"(ifrac, bottom, top) = (" << ifrac <<
"%, " << lfrac <<
"%, " << rfrac <<
"%)" <<
Endl;
563 for (VarMap::const_iterator vit = fVar.begin(); vit != fVar.end(); ++vit) {
564 const std::vector<Double_t> &dvec = vit->second;
566 std::vector<Double_t>::const_iterator beg_it = dvec.end();
567 std::vector<Double_t>::const_iterator end_it = dvec.end();
570 for (std::vector<Double_t>::const_iterator dit = dvec.begin(); dit != dvec.end(); ++dit, ++
dist) {
572 if ((100*dist)/dvec.size() == lfrac && beg_it == dvec.end()) {
576 if ((100*dist)/dvec.size() == rfrac && end_it == dvec.end()) {
581 if (beg_it == dvec.end() || end_it == dvec.end()) {
582 beg_it = dvec.begin();
585 assert(beg_it != end_it &&
"Empty vector");
593 if (!(lpos < rpos)) {
594 Log() <<
kFATAL <<
"ModulekNN::ComputeMetric() - min value is greater than max value" <<
Endl;
605 fVarScale[vit->first] = rpos - lpos;
610 for (
UInt_t ievent = 0; ievent < fEvent.size(); ++ievent) {
611 fEvent[ievent] = Scale(fEvent[ievent]);
613 for (
UInt_t ivar = 0; ivar < fDimn; ++ivar) {
614 fVar[ivar].push_back(fEvent[ievent].GetVar(ivar));
625 if (fVarScale.empty()) {
629 if (event.
GetNVar() != fVarScale.size()) {
630 Log() <<
kFATAL <<
"ModulekNN::Scale() - mismatched metric and event size" <<
Endl;
636 for (
UInt_t ivar = 0; ivar <
event.GetNVar(); ++ivar) {
637 std::map<int, Double_t>::const_iterator fit = fVarScale.find(ivar);
638 if (fit == fVarScale.end()) {
639 Log() <<
kFATAL <<
"ModulekNN::Scale() - failed to find scale for " << ivar <<
Endl;
643 if (fit->second > 0.0) {
644 vvec[ivar] =
event.GetVar(ivar)/fit->second;
647 Log() <<
kFATAL <<
"Variable " << ivar <<
" has zero width" <<
Endl;
651 return Event(vvec, event.
GetWeight(),
event.GetType(),
event.GetTargets());
667 os <<
"----------------------------------------------------------------------"<< std::endl;
668 os <<
"Printing knn result" << std::endl;
669 os << fkNNEvent << std::endl;
673 std::map<Short_t, Double_t>
min,
max;
675 os <<
"Printing " << fkNNList.size() <<
" nearest neighbors" << std::endl;
676 for (List::const_iterator it = fkNNList.begin(); it != fkNNList.end(); ++it) {
677 os << ++count <<
": " << it->second <<
": " << it->first->GetEvent() << std::endl;
679 const Event &
event = it->first->GetEvent();
680 for (
UShort_t ivar = 0; ivar <
event.GetNVar(); ++ivar) {
681 if (min.find(ivar) == min.end()) {
682 min[ivar] =
event.GetVar(ivar);
684 else if (min[ivar] > event.GetVar(ivar)) {
685 min[ivar] =
event.GetVar(ivar);
688 if (max.find(ivar) == max.end()) {
689 max[ivar] =
event.GetVar(ivar);
691 else if (max[ivar] < event.GetVar(ivar)) {
692 max[ivar] =
event.GetVar(ivar);
697 if (min.size() == max.size()) {
698 for (std::map<Short_t, Double_t>::const_iterator mit = min.begin(); mit != min.end(); ++mit) {
700 Log() <<
kINFO <<
"(var, min, max) = (" << i <<
"," << min[i] <<
", " << max[i] <<
")" <<
Endl;
704 os <<
"----------------------------------------------------------------------" << std::endl;
double dist(Rotation3D const &r1, Rotation3D const &r2)
RooCmdArg Optimize(Int_t flag=2)
static Vc_ALWAYS_INLINE int_v min(const int_v &x, const int_v &y)
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::vector< TMVA::kNN::Event > EventVec
Bool_t Fill(const UShort_t odepth, UInt_t ifrac, const std::string &option="")
fill the tree
void SetNodeL(Node *node)
VarType GetVar(UInt_t i) const
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...
void SetTargets(const VarVec &tvec)
Event()
default constructor
VarType GetDist(VarType var, UInt_t ivar) const
void Print(std::ostream &os, const OptionType &opt)
UInt_t Find(std::list< std::pair< const Node< T > *, Float_t > > &nlist, const Node< T > *node, const T &event, UInt_t nfind)
static Vc_ALWAYS_INLINE int_v max(const int_v &x, const int_v &y)
const VarVec & GetTargets() const
Double_t GetWeight() const
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
const VarVec & GetVars() const
void ComputeMetric(UInt_t ifrac)
compute scale factor for each variable (dimension) so that distance is computed uniformely along each...