#include #include "TSystem.h" #include "TFile.h" #include "TTree.h" #include "TH2I.h" #include "TLegend.h" #include "TRandom.h" #include "TString.h" #include "TGraph.h" #include "TMarker.h" #include "TStopwatch.h" #include "../src/TKDPDF.h" #include "../src/TKDTree.h" void kNNTest(const int np = 10000, const int ndim = 2); void kNNDraw(const Float_t *p, const int kNN=20); void build(const Int_t ndim = 2, const Int_t nstat = 1000000); Float_t Mem(); //______________________________________________________________ void kNNTest(const int np, const int ndim) { // Test speed and quality of nearest neighbors search. // The results are compared with a simple loop search through the data. // The macro should run in compiled mode. const int ntimes = 1000; // times to repeate kNN search const Int_t kNN = 1; // number of neighbors Float_t **x = new Float_t*[ndim]; for(int idim =0 ; idimGaus(); } TKDTreeIF nnFinder(np, ndim, 1, x); // generate test sample Float_t **p = new Float_t*[ntimes]; for(int ip =0 ; ipGaus(); } Int_t *index; Float_t *d; TStopwatch timer; timer.Start(kTRUE); for(int itime=0; itimeSetBorderSize(1); leg->SetHeader("NN finders"); TH2 *h2 = new TH2I("h2", "", 100, -2., 2., 100, -2., 2.); h2->Draw(); h2->SetStats(kFALSE); TMarker *mp = new TMarker(p[ntimes-1][0], p[ntimes-1][1], 3); mp->SetMarkerColor(4); mp->Draw(); leg->AddEntry(mp, "Target", "p"); TGraph *gKD = new TGraph(kNN); gKD->SetMarkerStyle(24); gKD->SetMarkerColor(2); gKD->SetMarkerSize(.5); for(int ip=0; ipSetPoint(ip, x[0][index[ip]], x[1][index[ip]]); gKD->Draw("p"); leg->AddEntry(gKD, "kDTree", "p"); // STAND ALONE Float_t ftmp, gtmp, dist; Int_t itmp, jtmp; Int_t fkNN[kNN]; Float_t fkNNdist[kNN]; for(int i=0; i= fkNNdist[kNN-1]) continue; //insert neighbor int iNN=0; while(dist >= fkNNdist[iNN]) iNN++; itmp = fkNN[iNN]; ftmp = fkNNdist[iNN]; fkNN[iNN] = idx; fkNNdist[iNN] = dist; for(int ireplace=iNN+1; ireplaceSetMarkerStyle(24); for(int ip=0; ipSetPoint(ip, x[0][fkNN[ip]], x[1][fkNN[ip]]); gSA->Draw("p"); leg->AddEntry(gSA, "Stand Alone", "p"); leg->Draw(); for(int ip=0; ipGaus(); data[0][i]= gRandom->Gaus(); } TKDPDF pdf(npoints, 2, 100, data); pdf.DrawNode(pdf.GetNodeIndex(p)); TMarker *mp = new TMarker(p[0], p[1], 3); mp->SetMarkerColor(4); mp->Draw(); Int_t *index, color; Float_t *d, d0, pknn[2]; Float_t start = Mem(); pdf.FindNearestNeighbors(p, kNN, index, d); Float_t end = Mem(); printf("FindNearestNeighbors memory usage %fKB\n", end-start); d0 = d[kNN-1]; TMarker *ma = new TMarker[kNN]; for(int ip=0; ipBranch(Form("x%d", idim), &pntTrain[idim], Form("x%d/D", idim)); for(int istat=0; istatGaus(); t->Fill(); } f->cd(); t->Write(); f->Close(); delete f; }