50 printf(
"\n\tTesting kDTree memory usage ...\n");
52 printf(
"\n\tTesting kDTree speed ...\n");
65 data[1] = &data0[npoints];
66 for (
Int_t i=0;i<npoints;i++) {
74 printf(
"Memory usage %f KB\n",after-before);
77 printf(
"Memory leak %f KB\n", end-before);
97 for (
Int_t i=0;i<npoints;i++) {
102 kdtree =
new TKDTreeIF(npoints, 2, bsize);
110 for (
Int_t i=0;i<npoints;i++) {
114 kdtree =
new TKDTreeIF(npoints, 2, bsize);
115 kdtree->SetData(0, data0);
116 kdtree->SetData(1, data1);
119 printf(
"fNNodes %d, fRowT0 %d, fCrossNode %d, fOffset %d\n",kdtree->GetNNodes(), kdtree->GetRowT0(), kdtree->GetCrossNode(), kdtree->GetOffset());
122 for (
Int_t i=0;i<npoints;i++) {
126 kdtree =
new TKDTreeIF(npoints, 2, bsize);
127 kdtree->SetData(0, data0);
128 kdtree->SetData(1, data1);
131 printf(
"fNNodes %d, fRowT0 %d, fCrossNode %d, fOffset %d\n",kdtree->GetNNodes(), kdtree->GetRowT0(), kdtree->GetCrossNode(), kdtree->GetOffset());
134 for (
Int_t i=0;i<npoints;i++) {
138 kdtree =
new TKDTreeIF(npoints, 2, bsize);
139 kdtree->SetData(0, data0);
140 kdtree->SetData(1, data1);
143 printf(
"fNNodes %d, fRowT0 %d, fCrossNode %d, fOffset %d\n",kdtree->GetNNodes(), kdtree->GetRowT0(), kdtree->GetCrossNode(), kdtree->GetOffset());
146 for (
Int_t i=0;i<npoints;i++) {
150 kdtree =
new TKDTreeIF(npoints, 2, bsize);
151 kdtree->SetData(0, data0);
152 kdtree->SetData(1, data1);
155 printf(
"fNNodes %d, fRowT0 %d, fCrossNode %d, fOffset %d\n",kdtree->GetNNodes(), kdtree->GetRowT0(), kdtree->GetCrossNode(), kdtree->GetOffset());
158 for (
Int_t i=0;i<npoints;i++) {
162 kdtree =
new TKDTreeIF(npoints, 2, bsize);
163 kdtree->SetData(0, data0);
164 kdtree->SetData(1, data1);
167 printf(
"fNNodes %d, fRowT0 %d, fCrossNode %d, fOffset %d\n",kdtree->GetNNodes(), kdtree->GetRowT0(), kdtree->GetCrossNode(), kdtree->GetOffset());
188 data[1] = &data0[npoints];
189 for (
Int_t i=0;i<npoints;i++) {
202 printf(
"different number of nodes\n");
205 for (
Int_t inode=0; inode<nnodes; inode++){
215 printf(
"Memory leak %f KB\n", end-before);
229 printf(
"Please specify a power of 2 greater than 10\n");
237 data[1] = &data0[npoints];
238 for (
Int_t i=0;i<npoints;i++) {
248 for(
int i=10; i<npower2; i++){
251 kdtree =
new TKDTreeIF(tpoints, 2, bsize, data);
274 void TestSizeIF(Int_t nsec, Int_t nrows, Int_t npoints, Int_t bsize, Int_t mode)
276 Float_t before =Mem();
277 for (Int_t isec=0; isec<nsec;isec++)
278 for (Int_t irow=0;irow<nrows;irow++){
279 TestkdtreeIF(npoints,1,mode,bsize);
281 Float_t after = Mem();
282 printf("Memory usage %f\n",after-before);
298 void TestkdtreeIF(Int_t npoints, Int_t bsize, Int_t nloop, Int_t mode)
301 Float_t rangey = 100;
302 Float_t rangez = 100;
303 Float_t drangey = 0.1;
304 Float_t drangez = 0.1;
307 Float_t *data0 = new Float_t[npoints*2];
310 data[1] = &data0[npoints];
312 for (Int_t i=0; i<npoints; i++){
313 data[0][i] = gRandom->Uniform(-rangey, rangey);
314 data[1][i] = gRandom->Uniform(-rangez, rangez);
319 printf("building kdTree ...\n");
321 TKDTreeIF *kdtree = new TKDTreeIF(npoints, 2, bsize, data);
325 if(mode == 0) return;
328 Float_t counteriter = 0;
329 Float_t counterfound = 0;
332 if (nloop) timer.Start(kTRUE);
333 Int_t *res = new Int_t[npoints];
335 for (Int_t kloop = 0;kloop<nloop;kloop++){
341 for (Int_t i=0;i<npoints;i++){
342 Float_t point[2]={data[0][i],data[1][i]};
343 Float_t delta[2]={drangey,drangez};
347 //kdtree->FindBNode(point,delta, bnode);
349 kdtree->FindInRangeA(point,delta,res,nfound,iter,bnode);
351 //Bool_t isOK = kTRUE;
352 Bool_t isOK = kFALSE;
353 for (Int_t ipoint=0;ipoint<nfound;ipoint++)
354 if (res[ipoint]==i) isOK =kTRUE;
356 counterfound+=nfound;
375 counteriter/=npoints;
376 counterfound/=npoints;
377 if (nloop) printf("Find nearest point:\t%f\t%f\t%f\n",countern, counteriter, counterfound);
387 Int_t npoints = 10000;
395 for (
Int_t i=0; i<npoints; i++){
417 for (
Int_t itime=0; itime<ntimes; itime++){
420 for (
Int_t i=0; i<npoints; i++){
423 dist[i]+=(x[i]-x[ipoint])*(x[i]-x[ipoint]);
424 dist[i]+=(y[i]-y[ipoint])*(y[i]-y[ipoint]);
425 dist[i]+=(z[i]-z[ipoint])*(z[i]-z[ipoint]);
438 for (
Int_t inn=0; inn<nn; inn++){
439 if (
TMath::Abs(dist2[inn]-dist[index[inn]])>1
E-8) {
448 printf(
"Nearest neighbors found for %d random points\n", ntimes);
449 printf(
"%d neighbors are wrong compared to \"brute force\" method\n", diff1);
478 printf(
"%d points, range=%f\n", npoints, range);
483 for (
Int_t i=0; i<npoints; i++){
490 std::vector<Int_t> results2;
503 for (
Int_t itime=0; itime<ntimes; itime++){
510 for (
Int_t ipoint=0; ipoint<npoints; ipoint++){
512 dist[ipoint]+=(x[ipoint]-point[0])*(x[ipoint]-point[0]);
513 dist[ipoint]+=(y[ipoint]-point[1])*(y[ipoint]-point[1]);
514 dist[ipoint]+=(z[ipoint]-point[2])*(z[ipoint]-point[2]);
516 index[ipoint]=ipoint;
520 while (np1<npoints && dist[index[np1]]<=range){
521 results1[np1]=index[np1];
529 printf(
"different numbers of points found, %d %d\n", np1,
Int_t(results2.size()));
535 std::sort(results2.begin(), results2.end());
537 for (
Int_t i=0; i<np1; i++){
538 if (
TMath::Abs(results1[index[i]]-results2[i])>1
E-8) ndiff++;
541 printf(
"%d differences found between \"brute force\" method and kd-tree\n", ndiff);
557 int main(
int argc,
char **argv) {
559 for (
Int_t i=1 ; i<argc ; i++) {
560 std::string arg = argv[i] ;
569 std::cerr <<
"Usage: " << argv[0] <<
" [-g] [-v]\n";
570 std::cerr <<
" where:\n";
571 std::cerr <<
" -g : graphics mode\n";
573 std::cerr << std::endl;
double dist(Rotation3D const &r1, Rotation3D const &r2)
void TestSpeed(Int_t npower2=20, Int_t bsize=10)
Test of building time of kdTree.
void SetData(Index npoints, Index ndim, UInt_t bsize, Value **data)
Set the data array. See the constructor function comments for details.
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
void TestMembers()
This is not really a test, it's a function that illustrates the internal behaviour of the kd-tree...
TKDTree< Int_t, Float_t > TKDTreeIF
virtual Double_t Rndm(Int_t i=0)
Machine independent random number generator.
Value GetNodeValue(Int_t id) const
Int_t GetOffset()
cross node
Double_t CpuTime()
Stop the stopwatch (if it is running) and return the cputime (in seconds) passed between the start an...
void FindInRange(Value *point, Value range, std::vector< Index > &res)
Find all points in the sphere of a given radius "range" around the given point 1st argument - the poi...
void TestConstr(const Int_t npoints=1000000, const Int_t bsize=100)
compare the results of different data setting functions nothing printed - all works correctly ...
Int_t GetCrossNode()
smallest terminal row
virtual void Draw(Option_t *chopt="")
Draw this graph with its current attributes.
virtual int GetProcInfo(ProcInfo_t *info) const
Returns cpu and memory used by this process into the ProcInfo_t structure.
void Build()
Build the kd-tree.
void Stop()
Stop the stopwatch.
virtual void Run(Bool_t retrn=kFALSE)
Main application eventloop. Calls system dependent eventloop via gSystem.
void TestBuild(const Int_t npoints=1000000, const Int_t bsize=100)
Test kdTree for memory leaks.
double pow(double, double)
void Sort(Index n, const Element *a, Index *index, Bool_t down=kTRUE)
void TestNeighbors()
Test TKDTree::FindNearestNeighbors() function.
R__EXTERN TSystem * gSystem
void FindNearestNeighbors(const Value *point, Int_t k, Index *ind, Value *dist)
Find kNN nearest neighbors to the point in the first argument Returns 1 on success, 0 on failure Arrays ind and dist are provided by the user and are assumed to be at least kNN elements long.
virtual void SetMarkerStyle(Style_t mstyle=1)
R__EXTERN TRandom * gRandom
TKDTree< Int_t, Double_t > TKDTreeID
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
virtual void SetPoint(Int_t i, Double_t x, Double_t y)
Set x and y values for point number i.
A Graph is a graphics object made of two arrays X and Y with npoints each.
This class creates the ROOT Application Environment that interfaces to the windowing system eventloop...
Double_t Sqrt(Double_t x)
int main(int argc, char **argv)