7 templateClassImp(TKDTree)
9 //////////////////////////////////////////////////////////////////////
10 // Memory setup of protected data members:
12 // kDataOwner; // Toggle ownership of the data
14 // size of branch node array, and index of first terminal node
15 // fNDim; // number of variables
16 // fNpoints; // number of multidimensional points
17 // fBucketSize; // number of data points per terminal node
19 // fIndPoints : array containing rearanged indexes according to nodes:
20 // | point indexes from 1st terminal node (fBucketSize elements) | ... | point indexes from the last terminal node (<= fBucketSize elements)
23 // fRange : array containing the boundaries of the domain:
24 // | 1st dimension (min + max) | 2nd dimension (min + max) | ...
25 // fBoundaries : nodes boundaries
26 // | 1st node {1st dim * 2 elements | 2nd dim * 2 elements | ...} | 2nd node {...} | ...
27 // the nodes are arranged in the following order :
28 // - first fNnodes are primary nodes
29 // - after are the terminal nodes
30 // fNodes : array of primary nodes
31 ///////////////////////////////////////////////////////////////////////
34 //_________________________________________________________________
35 template <typename Index, typename Value>
36 TKDTree<Index, Value>::TKDTree() :
59 // Default constructor. Nothing is built
63 //_________________________________________________________________
64 template <typename Index, typename Value>
65 TKDTree<Index, Value>::TKDTree(Index npoints, Index ndim, UInt_t bsize, Value **data) :
76 ,fData(data) //Columnwise!!!!!
88 // Allocate data by hand. See TKDTree(TTree*, const Char_t*) for an automatic method.
93 //_________________________________________________________________
94 template <typename Index, typename Value>
95 TKDTree<Index, Value>::~TKDTree()
97 if (fIndBuffer) delete [] fIndBuffer;
98 if (fDistBuffer) delete [] fDistBuffer;
99 if (fkNNdist) delete [] fkNNdist;
100 if (fkNN) delete [] fkNN;
101 if (fAxis) delete [] fAxis;
102 if (fValue) delete [] fValue;
103 if (fIndPoints) delete [] fIndPoints;
104 if (fRange) delete [] fRange;
105 if (fBoundaries) delete [] fBoundaries;
106 if (kDataOwner && fData){
107 for(int idim=0; idim<fNDim; idim++) delete [] fData[idim];
113 //_________________________________________________________________
114 template <typename Index, typename Value>
115 void TKDTree<Index, Value>::Build(){
119 // 1. calculate number of nodes
120 // 2. calculate first terminal row
121 // 3. initialize index array
122 // 4. non recursive building of the binary tree
125 fNnodes = fNpoints/fBucketSize-1;
126 if (fNpoints%fBucketSize) fNnodes++;
130 for (;(fNnodes+1)>(1<<fRowT0);fRowT0++);
141 // allocate space for boundaries
142 fRange = new Value[2*fNDim];
143 fIndPoints= new Index[fNpoints];
144 for (Index i=0; i<fNpoints; i++) fIndPoints[i] = i;
145 fAxis = new UChar_t[fNnodes];
146 fValue = new Value[fNnodes];
148 fCrossNode = (1<<(fRowT0+1))-1;
149 if (fCrossNode<fNnodes) fCrossNode = 2*fCrossNode+1;
151 // fOffset = (((fNnodes+1)-(1<<fRowT0)))*2;
152 Int_t over = (fNnodes+1)-(1<<fRowT0);
153 Int_t filled = ((1<<fRowT0)-over)*fBucketSize;
154 fOffset = fNpoints-filled;
156 // printf("Row0 %d\n", fRowT0);
157 // printf("CrossNode %d\n", fCrossNode);
158 // printf("Offset %d\n", fOffset);
162 // stack for non recursive build - size 128 bytes enough
164 Int_t nodeStack[128];
165 Int_t npointStack[128];
167 Int_t currentIndex = 0;
171 npointStack[0] = fNpoints;
174 Int_t nbucketsall =0;
175 while (currentIndex>=0){
178 Int_t npoints = npointStack[currentIndex];
179 if (npoints<=fBucketSize) {
180 //printf("terminal node : index %d iter %d\n", currentIndex, iter);
183 continue; // terminal node
185 Int_t crow = rowStack[currentIndex];
186 Int_t cpos = posStack[currentIndex];
187 Int_t cnode = nodeStack[currentIndex];
188 //printf("currentIndex %d npoints %d node %d\n", currentIndex, npoints, cnode);
191 Int_t nbuckets0 = npoints/fBucketSize; //current number of buckets
192 if (npoints%fBucketSize) nbuckets0++; //
193 Int_t restRows = fRowT0-rowStack[currentIndex]; // rest of fully occupied node row
194 if (restRows<0) restRows =0;
195 for (;nbuckets0>(2<<restRows); restRows++);
196 Int_t nfull = 1<<restRows;
197 Int_t nrest = nbuckets0-nfull;
198 Int_t nleft =0, nright =0;
200 if (nrest>(nfull/2)){
201 nleft = nfull*fBucketSize;
202 nright = npoints-nleft;
204 nright = nfull*fBucketSize/2;
205 nleft = npoints-nright;
209 //find the axis with biggest spread
211 Value tempspread, min, max;
214 for (Int_t idim=0; idim<fNDim; idim++){
216 Spread(npoints, array, fIndPoints+cpos, min, max);
217 tempspread = max - min;
218 if (maxspread < tempspread) {
219 maxspread=tempspread;
223 //printf("set %d %6.3f %6.3f\n", idim, min, max);
224 fRange[2*idim] = min; fRange[2*idim+1] = max;
226 array = fData[axspread];
227 KOrdStat(npoints, array, nleft, fIndPoints+cpos);
228 fAxis[cnode] = axspread;
229 fValue[cnode] = array[fIndPoints[cpos+nleft]];
230 //printf("Set node %d : ax %d val %f\n", cnode, node->fAxis, node->fValue);
233 npointStack[currentIndex] = nleft;
234 rowStack[currentIndex] = crow+1;
235 posStack[currentIndex] = cpos;
236 nodeStack[currentIndex] = cnode*2+1;
238 npointStack[currentIndex] = nright;
239 rowStack[currentIndex] = crow+1;
240 posStack[currentIndex] = cpos+nleft;
241 nodeStack[currentIndex] = (cnode*2)+2;
245 Info("Build()", Form("points %d left %d right %d", npoints, nleft, nright));
246 if (nleft<nright) Warning("Build", "Problem Left-Right");
247 if (nleft<0 || nright<0) Warning("Build()", "Problem Negative number");
251 //printf("NBuckets\t%d\n", nbucketsall);
256 //_________________________________________________________________
257 template <typename Index, typename Value>
258 Bool_t TKDTree<Index, Value>::FindNearestNeighbors(const Value *point, const Int_t k, Index *&in, Value *&d)
260 // Find "k" nearest neighbors to "point".
262 // Return true in case of success and false in case of failure.
263 // The indexes of the nearest k points are stored in the array "in" in
264 // increasing order of the distance to "point" and the maxim distance
267 // The arrays "in" and "d" are managed internally by the TKDTree.
269 Index inode = FindNode(point);
271 Error("FindNearestNeighbors()", "Point outside data range.");
276 Int_t nCalculations = 0;
278 // allocate working memory
280 fDistBuffer = new Value[fBucketSize];
281 fIndBuffer = new Index[fBucketSize];
285 if(fkNN) printf("Reallocate memory %d -> %d \n", fkNNdim, 2*k);
286 else printf("Allocate %d memory\n", 2*k);
293 fkNN = new Index[fkNNdim];
294 fkNNdist = new Value[fkNNdim];
296 memset(fkNN, -1, k*sizeof(Index));
297 for(int i=0; i<k; i++) fkNNdist[i] = 9999.;
298 Index itmp, jtmp; Value ftmp, gtmp;
300 // calculate number of boundaries with the data domain.
301 if(!fBoundaries) MakeBoundaries();
302 Value *bounds = &fBoundaries[inode*2*fNDim];
304 for(int idim=0; idim<fNDim; idim++){
305 if((bounds[2*idim] - fRange[2*idim]) < 1.E-10) nBounds++;
306 if((bounds[2*idim+1] - fRange[2*idim+1]) < 1.E-10) nBounds++;
308 if(debug>=1) printf("Calculate boundaries [nBounds = %d]\n", nBounds);
314 Int_t nAllNodes = fNnodes + fNpoints/fBucketSize + ((fNpoints%fBucketSize)?1:0);
315 Int_t nodeStack[128], nodeIn[128];
316 Index currentIndex = 0;
317 nodeStack[0] = inode;
319 while(currentIndex>=0){
320 Int_t tnode = nodeStack[currentIndex];
321 Int_t entry = nodeIn[currentIndex];
323 if(debug>=1) printf("Analyse tnode %d entry %d\n", tnode, entry);
325 // check if node is still eligible
326 Int_t inode = (tnode-1)>>1; //tnode/2 + (tnode%2) - 1;
329 dif = point[ax] - val;
330 if((TMath::Abs(dif) > fkNNdist[k-1]) &&
331 ((dif > 0. && (tnode&1)) || (dif < 0. && !(tnode&1)))) {
332 if(debug>=1) printf("\tremove %d\n", (tnode-1)>>1);
336 // end all recursions
337 if(nBounds==2 * fNDim) break;
341 if(IsTerminal(tnode)){
342 if(debug>=2) printf("\tProcess terminal node %d\n", tnode);
343 // Link data to terminal node
344 Int_t offset = (tnode >= fCrossNode) ? (tnode-fCrossNode)*fBucketSize : fOffset+(tnode-fNnodes)*fBucketSize;
345 Index *indexPoints = &fIndPoints[offset];
346 Int_t nbs = (tnode == 2*fNnodes) ? fNpoints%fBucketSize : fBucketSize;
347 nbs = nbs ? nbs : fBucketSize;
348 nCalculations += nbs;
351 for(int idx=0; idx<nbs; idx++){
352 // calculate distance in the L1 metric
353 fDistBuffer[npoints] = 0.;
354 for(int idim=0; idim<fNDim; idim++) fDistBuffer[npoints] += TMath::Abs(point[idim] - fData[idim][indexPoints[idx]]);
355 // register candidate neighbor
356 if(fDistBuffer[npoints] < fkNNdist[k-1]){
357 fIndBuffer[npoints] = indexPoints[idx];
361 for(int ibrowse=0; ibrowse<npoints; ibrowse++){
362 if(fDistBuffer[ibrowse] >= fkNNdist[k-1]) continue;
365 while(fDistBuffer[ibrowse] >= fkNNdist[iNN]) iNN++;
366 if(debug>=2) printf("\t\tinsert data %d @ %d distance %f\n", fIndBuffer[ibrowse], iNN, fDistBuffer[ibrowse]);
368 itmp = fkNN[iNN]; ftmp = fkNNdist[iNN];
369 fkNN[iNN] = fIndBuffer[ibrowse];
370 fkNNdist[iNN] = fDistBuffer[ibrowse];
371 for(int ireplace=iNN+1; ireplace<k; ireplace++){
372 jtmp = fkNN[ireplace]; gtmp = fkNNdist[ireplace];
373 fkNN[ireplace] = itmp; fkNNdist[ireplace] = ftmp;
374 itmp = jtmp; ftmp = gtmp;
375 if(ftmp == 9999.) break;
378 for(int i=0; i<k; i++){
379 if(fkNNdist[i] == 9999.) break;
380 printf("\t\tfkNNdist[%d] = %f\n", i, fkNNdist[i]);
387 Int_t pn = (tnode-1)>>1;//tnode/2 + (tnode%2) - 1;
388 if(pn >= 0 && entry != pn){
389 // check if parent node is eligible at all
390 if(TMath::Abs(point[fAxis[pn]] - fValue[pn]) > fkNNdist[k-1]){
393 // end all recursions
394 if(nBounds==2 * fNDim) break;
397 nodeStack[currentIndex]=pn;
398 nodeIn[currentIndex]=tnode;
399 if(debug>=2) printf("\tregister %d\n", nodeStack[currentIndex]);
401 if(IsTerminal(tnode)) continue;
403 // register children nodes
405 Bool_t kAllow[] = {kTRUE, kTRUE};
408 if(TMath::Abs(point[ax] - val) > fkNNdist[k-1]){
409 if(point[ax] > val) kAllow[0] = kFALSE;
410 else kAllow[1] = kFALSE;
412 for(int ic=1; ic<=2; ic++){
413 if(!kAllow[ic-1]) continue;
415 if(cn < nAllNodes && entry != cn){
417 nodeStack[currentIndex] = cn;
418 nodeIn[currentIndex]=tnode;
419 if(debug>=2) printf("\tregister %d\n", nodeStack[currentIndex]);
432 //_________________________________________________________________
433 template <typename Index, typename Value>
434 Index TKDTree<Index, Value>::FindNode(const Value * point){
436 // find the terminal node to which point belongs
438 Index stackNode[128], inode;
439 Int_t currentIndex =0;
441 while (currentIndex>=0){
442 inode = stackNode[currentIndex];
443 if (IsTerminal(inode)) return inode;
446 if (point[fAxis[inode]]<=fValue[inode]){
448 stackNode[currentIndex]=(inode<<1)+1;
450 if (point[fAxis[inode]]>=fValue[inode]){
452 stackNode[currentIndex]=(inode+1)<<1;
461 //_________________________________________________________________
462 template <typename Index, typename Value>
463 void TKDTree<Index, Value>::FindPoint(Value * point, Index &index, Int_t &iter){
465 // find the index of point
466 // works only if we keep fData pointers
468 Int_t stackNode[128];
469 Int_t currentIndex =0;
473 while (currentIndex>=0){
475 Int_t inode = stackNode[currentIndex];
477 if (IsTerminal(inode)){
478 // investigate terminal node
479 Int_t indexIP = (inode >= fCrossNode) ? (inode-fCrossNode)*fBucketSize : (inode-fNnodes)*fBucketSize+fOffset;
480 printf("terminal %d indexP %d\n", inode, indexIP);
481 for (Int_t ibucket=0;ibucket<fBucketSize;ibucket++){
484 printf("ibucket %d index %d\n", ibucket, indexIP);
485 if (indexIP>=fNpoints) continue;
486 Int_t index0 = fIndPoints[indexIP];
487 for (Int_t idim=0;idim<fNDim;idim++) if (fData[idim][index0]!=point[idim]) isOK = kFALSE;
488 if (isOK) index = index0;
493 if (point[fAxis[inode]]<=fValue[inode]){
495 stackNode[currentIndex]=(inode*2)+1;
497 if (point[fAxis[inode]]>=fValue[inode]){
499 stackNode[currentIndex]=(inode*2)+2;
503 // printf("Iter\t%d\n",iter);
506 //_________________________________________________________________
507 template <typename Index, typename Value>
508 void TKDTree<Index, Value>::FindInRangeA(Value * point, Value * delta, Index *res , Index &npoints, Index & iter, Int_t bnode)
511 // Find all points in the range specified by (point +- range)
512 // res - Resulting indexes are stored in res array
513 // npoints - Number of selected indexes in range
515 // For some cases it is better to don't keep data - because of memory consumption
516 // If the data are not kept - only boundary conditions are investigated
517 // some of the data can be outside of the range
518 // What is guranteed in this mode: All of the points in the range are selected + some fraction of others (but close)
520 Index stackNode[128];
522 Index currentIndex = 0;
523 stackNode[currentIndex] = bnode;
524 while (currentIndex>=0){
526 Int_t inode = stackNode[currentIndex];
529 if (!IsTerminal(inode)){
531 //TKDNode<Index, Value> * node = &(fNodes[inode]);
532 if (point[fAxis[inode]] - delta[fAxis[inode]] < fValue[inode]) {
534 stackNode[currentIndex]= (inode*2)+1;
536 if (point[fAxis[inode]] + delta[fAxis[inode]] >= fValue[inode]){
538 stackNode[currentIndex]= (inode*2)+2;
542 (inode >= fCrossNode) ? (inode-fCrossNode)*fBucketSize : (inode-fNnodes)*fBucketSize+fOffset;
543 for (Int_t ibucket=0;ibucket<fBucketSize;ibucket++){
544 if (indexIP+ibucket>=fNpoints) break;
545 res[npoints] = fIndPoints[indexIP+ibucket];
552 // compress rest if data still accesible
554 Index npoints2 = npoints;
556 for (Index i=0; i<npoints2;i++){
558 for (Index idim = 0; idim< fNDim; idim++){
559 if (TMath::Abs(fData[idim][res[i]]- point[idim])>delta[idim])
563 res[npoints] = res[i];
571 //_________________________________________________________________
572 template <typename Index, typename Value>
573 void TKDTree<Index, Value>::FindInRangeB(Value * point, Value * delta, Index *res , Index &npoints,Index & iter, Int_t bnode)
576 Long64_t goldStatus = (1<<(2*fNDim))-1; // gold status
577 Index stackNode[128];
578 Long64_t stackStatus[128];
580 Index currentIndex = 0;
581 stackNode[currentIndex] = bnode;
582 stackStatus[currentIndex] = 0;
583 while (currentIndex>=0){
584 Int_t inode = stackNode[currentIndex];
585 Long64_t status = stackStatus[currentIndex];
588 if (IsTerminal(inode)){
590 (inode >= fCrossNode) ? (inode-fCrossNode)*fBucketSize : (inode-fNnodes)*fBucketSize+fOffset;
591 for (Int_t ibucket=0;ibucket<fBucketSize;ibucket++){
592 if (indexIP+ibucket>=fNpoints) break;
593 res[npoints] = fIndPoints[indexIP+ibucket];
599 if (status == goldStatus){
601 Int_t iright = inode;
602 for (;ileft<fNnodes; ileft = (ileft<<1)+1);
603 for (;iright<fNnodes; iright = (iright<<1)+2);
605 (ileft >= fCrossNode) ? (ileft-fCrossNode)*fBucketSize : (ileft-fNnodes)*fBucketSize+fOffset;
607 (iright >= fCrossNode) ? (iright-fCrossNode)*fBucketSize : (iright-fNnodes)*fBucketSize+fOffset;
609 Int_t endpoint = indexR+fBucketSize;
610 if (endpoint>fNpoints) endpoint=fNpoints;
611 for (Int_t ipoint=indexL;ipoint<endpoint;ipoint++){
612 res[npoints] = fIndPoints[ipoint];
619 // TKDNode<Index, Value> * node = &(fNodes[inode]);
620 if (point[fAxis[inode]] - delta[fAxis[inode]] < fValue[inode]) {
622 stackNode[currentIndex]= (inode<<1)+1;
623 if (point[fAxis[inode]] + delta[fAxis[inode]] > fValue[inode])
624 stackStatus[currentIndex]= status | (1<<(2*fAxis[inode]));
626 if (point[fAxis[inode]] + delta[fAxis[inode]] >= fValue[inode]){
628 stackNode[currentIndex]= (inode<<1)+2;
629 if (point[fAxis[inode]] - delta[fAxis[inode]]<fValue[inode])
630 stackStatus[currentIndex]= status | (1<<(2*fAxis[inode]+1));
635 Index npoints2 = npoints;
637 for (Index i=0; i<npoints2;i++){
639 for (Index idim = 0; idim< fNDim; idim++){
640 if (TMath::Abs(fData[idim][res[i]]- point[idim])>delta[idim])
644 res[npoints] = res[i];
653 //_________________________________________________________________
654 template <typename Index, typename Value>
655 void TKDTree<Index, Value>::SetData(Index npoints, Index ndim, UInt_t bsize, Value **data)
659 // Check reconstruction/reallocation of memory of data. Maybe it is not
660 // necessary to delete and realocate space but only to use the same space
675 //_________________________________________________________________
676 template <typename Index, typename Value>
677 void TKDTree<Index, Value>::Spread(Index ntotal, Value *a, Index *index, Value &min, Value &max)
683 for (i=0; i<ntotal; i++){
684 if (a[index[i]]<min) min = a[index[i]];
685 if (a[index[i]]>max) max = a[index[i]];
690 //_________________________________________________________________
691 template <typename Index, typename Value>
692 Value TKDTree<Index, Value>::KOrdStat(Index ntotal, Value *a, Index k, Index *index)
695 //copy of the TMath::KOrdStat because I need an Index work array
697 Index i, ir, j, l, mid;
705 if (ir<=l+1) { //active partition contains 1 or 2 elements
706 if (ir == l+1 && a[index[ir]]<a[index[l]])
707 {temp = index[l]; index[l]=index[ir]; index[ir]=temp;}
708 Value tmp = a[index[rk]];
711 mid = (l+ir) >> 1; //choose median of left, center and right
712 {temp = index[mid]; index[mid]=index[l+1]; index[l+1]=temp;}//elements as partitioning element arr.
713 if (a[index[l]]>a[index[ir]]) //also rearrange so that a[l]<=a[l+1]
714 {temp = index[l]; index[l]=index[ir]; index[ir]=temp;}
716 if (a[index[l+1]]>a[index[ir]])
717 {temp=index[l+1]; index[l+1]=index[ir]; index[ir]=temp;}
719 if (a[index[l]]>a[index[l+1]])
720 {temp = index[l]; index[l]=index[l+1]; index[l+1]=temp;}
722 i=l+1; //initialize pointers for partitioning
726 do i++; while (a[index[i]]<a[arr]);
727 do j--; while (a[index[j]]>a[arr]);
728 if (j<i) break; //pointers crossed, partitioning complete
729 {temp=index[i]; index[i]=index[j]; index[j]=temp;}
733 if (j>=rk) ir = j-1; //keep active the partition that
734 if (j<=rk) l=i; //contains the k_th element
739 //_________________________________________________________________
740 template <typename Index, typename Value>
741 void TKDTree<Index, Value>::MakeBoundaries(Value *range)
743 // Build boundaries for each node.
746 if(range) memcpy(fRange, range, fNDimm*sizeof(Value));
747 // total number of nodes including terminal nodes
748 Int_t totNodes = fNnodes + fNpoints/fBucketSize + ((fNpoints%fBucketSize)?1:0);
749 fBoundaries = new Value[totNodes*fNDimm];
750 //Info("MakeBoundaries(Value*)", Form("Allocate boundaries for %d nodes", totNodes));
754 Value *tbounds = 0x0, *cbounds = 0x0;
756 for(int inode=fNnodes-1; inode>=0; inode--){
757 tbounds = &fBoundaries[inode*fNDimm];
758 memcpy(tbounds, fRange, fNDimm*sizeof(Value));
760 // check left child node
762 if(IsTerminal(cn)) CookBoundaries(inode, kTRUE);
763 cbounds = &fBoundaries[fNDimm*cn];
764 for(int idim=0; idim<fNDim; idim++) tbounds[idim<<1] = cbounds[idim<<1];
766 // check right child node
768 if(IsTerminal(cn)) CookBoundaries(inode, kFALSE);
769 cbounds = &fBoundaries[fNDimm*cn];
770 for(int idim=0; idim<fNDim; idim++) tbounds[(idim<<1)+1] = cbounds[(idim<<1)+1];
774 //_________________________________________________________________
775 template <typename Index, typename Value>
776 void TKDTree<Index, Value>::CookBoundaries(const Int_t node, Bool_t LEFT)
778 // define index of this terminal node
779 Int_t index = (node<<1) + (LEFT ? 1 : 2);
780 //Info("CookBoundaries()", Form("Node %d", index));
782 // build and initialize boundaries for this node
783 Value *tbounds = &fBoundaries[fNDimm*index];
784 memcpy(tbounds, fRange, fNDimm*sizeof(Value));
785 Bool_t flag[256]; // cope with up to 128 dimensions
786 memset(flag, kFALSE, fNDimm);
789 // recurse parent nodes
791 while(pn >= 0 && nvals < fNDimm){
793 index = (fAxis[pn]<<1)+1;
795 tbounds[index] = fValue[pn];
800 index = fAxis[pn]<<1;
802 tbounds[index] = fValue[pn];
812 template class TKDTree<Int_t, Float_t>;
813 template class TKDTree<Int_t, Double_t>;