5 #include "TStopwatch.h"
6 #include "../src/TKDTree.h"
12 static struct mallinfo memdebug;
13 memdebug = mallinfo();
14 return memdebug.uordblks/1024.;
18 void TestBuild(const Int_t npoints = 1000000, const Int_t bsize = 100);
19 void TestSpeed(const Int_t npower2 = 20, const Int_t bsize = 10);
21 void testindexes(Int_t nloop, Int_t npoints);
22 void testkdtreeIF(Int_t npoints=1000, Int_t bsize=9, Int_t nloop=1000, Int_t mode = 2);
23 void TestSizeIF(Int_t npoints=1000, Int_t nrows = 150, Int_t nsec=18, Int_t mode = 1, Int_t bsize=9);
25 //______________________________________________________________________
28 printf("\n\tTesting kDTree memory usage ...\n");
31 printf("\n\tTesting kDTree speed ...\n");
35 //______________________________________________________________________
36 void TestBuild(const Int_t npoints, const Int_t bsize){
37 Float_t *data0 = new Float_t[npoints*2];
40 data[1] = &data0[npoints];
41 for (Int_t i=0;i<npoints;i++) {
42 data[1][i]= gRandom->Rndm();
43 data[0][i]= gRandom->Rndm();
45 Float_t before =Mem();
46 TKDTreeIF *kdtree = new TKDTreeIF(npoints, 2, bsize, data);
47 Float_t after = Mem();
48 printf("Memory usage %f KB\n",after-before);
51 printf("Memory leak %f KB\n", end-before);
55 //______________________________________________________________________
56 void TestSpeed(const Int_t npower2, const Int_t bsize)
59 printf("Please specify a power of 2 greater than 10\n");
63 Int_t npoints = Int_t(pow(2., npower2))*bsize;
64 Float_t *data0 = new Float_t[npoints*2];
67 data[1] = &data0[npoints];
68 for (Int_t i=0;i<npoints;i++) {
69 data[1][i]= gRandom->Rndm();
70 data[0][i]= gRandom->Rndm();
73 TGraph *g = new TGraph(npower2-10);
77 TKDTreeIF *kdtree = 0x0;
78 for(int i=10; i<npower2; i++){
79 tpoints = Int_t(pow(2., i))*bsize;
81 kdtree = new TKDTreeIF(tpoints, 2, bsize, data);
83 g->SetPoint(i-10, i, timer.CpuTime());
84 printf("npoints [%d] nodes [%d] cpu time %f [s]\n", tpoints, kdtree->GetNNodes(), timer.CpuTime());
92 //______________________________________________________________________
93 void TestSizeIF(Int_t npoints, Int_t nrows, Int_t nsec, Int_t mode, Int_t bsize)
96 // test size to build kdtree
98 Float_t before =Mem();
99 for (Int_t isec=0; isec<nsec;isec++)
100 for (Int_t irow=0;irow<nrows;irow++){
101 testkdtreeIF(npoints,0,mode,bsize);
103 Float_t after = Mem();
104 printf("Memory usage %f\n",after-before);
108 void testindexes(Int_t nloop, Int_t npoints){
112 TKDTree<Int_t, Float_t> *kdtree = new TKDTree<Int_t, Float_t>(0,0,0,0);
118 for (Int_t iloop=0;iloop<nloop;iloop++)
119 for (Int_t index=1;index<npoints;index++){
120 //row = TMath::Log2(index);
122 // if (index< (16<<row)) row=0;
123 // for (; index>=(32<<row);row+=5);
124 // for (; index>=(2<<row);row++);
125 // collumn= index-(1<<row);
126 TKDTree<Int_t, Float_t>::GetCoord(index,row,collumn);
128 //Int_t index2=kdtree->GetIndex(row,collumn);
129 //printf("%d\t%d\t%d\t%d\n",index,index2,row,collumn);
130 if (kdtree->GetIndex(row,collumn)!=index || collumn<0) {
139 void testkdtreeIF(Int_t npoints, Int_t bsize, Int_t nloop, Int_t mode)
142 // Test speed and functionality of 2D kdtree.
144 // npoints - number of data points
145 // bsize - bucket size
146 // nloop - number of loops
147 // mode - tasks to be performed by the kdTree
148 // - 0 : time building the tree
152 Float_t rangey = 100;
153 Float_t rangez = 100;
154 Float_t drangey = 0.1;
155 Float_t drangez = 0.1;
158 Float_t *data0 = new Float_t[npoints*2];
161 data[1] = &data0[npoints];
163 for (i=0; i<npoints; i++){
164 data[0][i] = gRandom->Uniform(-rangey, rangey);
165 data[1][i] = gRandom->Uniform(-rangez, rangez);
170 printf("building kdTree ...\n");
172 TKDTreeIF *kdtree = new TKDTreeIF(npoints, 2, bsize, data);
175 if(mode == 0) return;
180 Float_t counteriter = 0;
181 Float_t counterfound = 0;
185 if (nloop) timer.Start(kTRUE);
188 for (Int_t kloop = 0;kloop<nloop;kloop++){
194 for (Int_t i=0;i<npoints;i++){
195 Float_t point[2]={data[0][i],data[1][i]};
196 Float_t delta[2]={drangey,drangez};
200 //kdtree->FindBNode(point,delta, bnode);
202 kdtree->FindInRangeA(point,delta,res,nfound,iter,bnode);
204 //Bool_t isOK = kTRUE;
205 Bool_t isOK = kFALSE;
206 for (Int_t ipoint=0;ipoint<nfound;ipoint++)
207 if (res[ipoint]==i) isOK =kTRUE;
209 counterfound+=nfound;
226 counteriter/=npoints;
227 counterfound/=npoints;
228 if (nloop) printf("Find nearest point:\t%f\t%f\t%f\n",countern, counteriter, counterfound);