]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STAT/Macros/testKNN.C
new HOWTO macros
[u/mrichter/AliRoot.git] / STAT / Macros / testKNN.C
CommitLineData
de72b239 1const Float_t p[]={1.4, -.6}; //}{1.7, -.4};
2void testKNN(const Float_t *p, const int kNN=20)
3{
4// Draw "kNN" nearest neighbours of point "p". The distance (in the L1
5// metric) is encoded in the color code.
6// To build the data refere to function build().
7
8 TFile::Open("2D_Gauss.root");
9 TKDInterpolator in(db, "x0:x1", "x0>-1.5&&x0<2.&&x1>-2.&&x1<2.", 300);
10 in.DrawNode(in.FindNode(p)-in.GetNNodes());
11
12 TMarker *mp = new TMarker(p[0], p[1], 3);
13 mp->SetMarkerColor(4);
14 mp->Draw();
15
16 Int_t *index, color;
17 Float_t d, d0, pknn[2];
18 in.FindNearestNeighbors(p, kNN, index, d0);
19 TMarker *ma = new TMarker[kNN];
20 for(int ip=0; ip<kNN; ip++){
21 in.GetDataPoint(index[ip], pknn);
22 d = TMath::Abs(p[0]-pknn[0]) + TMath::Abs(p[1]-pknn[1]);
23 ma[ip].SetMarkerStyle(4);
24 color = 101 - Int_t(50. * d/d0);
25 ma[ip].SetMarkerColor(color);
26 ma[ip].DrawMarker(pknn[0], pknn[1]);
27 }
28}
29
30void build(const Int_t ndim = 2, const Int_t nstat = 100000)
31{
32// Build "nstat" data points in "ndim" dimensions taken from an
33// uncorrelated 2D Gauss distribution.
34
35
36 printf("build data ... \n");
37 Double_t pntTrain[ndim];
38 f = TFile::Open(Form("%dD_Gauss.root", ndim), "RECREATE");
39 t = new TTree("db", "gauss database");
40 for(int idim=0; idim<ndim; idim++) t->Branch(Form("x%d", idim), &pntTrain[idim], Form("x%d/D", idim));
41
42 for(int istat=0; istat<nstat; istat++){
43 for(int idim=0; idim<ndim; idim++) pntTrain[idim] = gRandom->Gaus();
44 t->Fill();
45 }
46 f->cd();
47 t->Write();
48 f->Close();
49 delete f;
50}