272template <
typename Index,
typename Value>
286 ,fBoundaries(nullptr)
294template <
typename Index,
typename Value>
308 ,fBoundaries(nullptr)
346template <
typename Index,
typename Value>
360 ,fBoundaries(nullptr)
375template <
typename Index,
typename Value>
378 if (fAxis)
delete [] fAxis;
379 if (fValue)
delete [] fValue;
380 if (fIndPoints)
delete [] fIndPoints;
381 if (fRange)
delete [] fRange;
382 if (fBoundaries)
delete [] fBoundaries;
386 for(
int idim=0; idim<fNDim; idim++)
delete [] fData[idim];
413template <
typename Index,
typename Value>
417 fNNodes = fNPoints/fBucketSize-1;
418 if (fNPoints%fBucketSize) fNNodes++;
419 fTotalNodes = fNNodes + fNPoints/fBucketSize + ((fNPoints%fBucketSize)?1:0);
422 for ( ;(fNNodes+1)>(1<<fRowT0);fRowT0++) {}
434 fRange =
new Value[2*fNDim];
435 fIndPoints=
new Index[fNPoints];
436 for (Index i=0; i<fNPoints; i++) fIndPoints[i] = i;
438 fValue =
new Value[fNNodes];
440 fCrossNode = (1<<(fRowT0+1))-1;
441 if (fCrossNode<fNNodes) fCrossNode = 2*fCrossNode+1;
444 Int_t over = (fNNodes+1)-(1<<fRowT0);
445 Int_t filled = ((1<<fRowT0)-over)*fBucketSize;
446 fOffset = fNPoints-filled;
457 Int_t nodeStack[128];
458 Int_t npointStack[128];
460 Int_t currentIndex = 0;
463 npointStack[0] = fNPoints;
466 while (currentIndex>=0){
468 Int_t npoints = npointStack[currentIndex];
469 if (npoints<=fBucketSize) {
473 Int_t crow = rowStack[currentIndex];
474 Int_t cpos = posStack[currentIndex];
475 Int_t cnode = nodeStack[currentIndex];
479 Int_t nbuckets0 = npoints/fBucketSize;
480 if (npoints%fBucketSize) nbuckets0++;
481 Int_t restRows = fRowT0-rowStack[currentIndex];
482 if (restRows<0) restRows =0;
483 for (;nbuckets0>(2<<restRows); restRows++) {}
484 Int_t nfull = 1<<restRows;
485 Int_t nrest = nbuckets0-nfull;
486 Int_t nleft =0, nright =0;
488 if (nrest>(nfull/2)){
489 nleft = nfull*fBucketSize;
490 nright = npoints-nleft;
492 nright = nfull*fBucketSize/2;
493 nleft = npoints-nright;
499 Value tempspread, min, max;
502 for (
Int_t idim=0; idim<fNDim; idim++){
504 Spread(npoints, array, fIndPoints+cpos, min, max);
505 tempspread = max - min;
506 if (maxspread < tempspread) {
507 maxspread=tempspread;
512 fRange[2*idim] = min; fRange[2*idim+1] = max;
514 array = fData[axspread];
515 KOrdStat(npoints, array, nleft, fIndPoints+cpos);
516 fAxis[cnode] = axspread;
517 fValue[cnode] = array[fIndPoints[cpos+nleft]];
521 npointStack[currentIndex] = nleft;
522 rowStack[currentIndex] = crow+1;
523 posStack[currentIndex] = cpos;
524 nodeStack[currentIndex] = cnode*2+1;
526 npointStack[currentIndex] = nright;
527 rowStack[currentIndex] = crow+1;
528 posStack[currentIndex] = cpos+nleft;
529 nodeStack[currentIndex] = (cnode*2)+2;
533 Info(
"Build()",
"%s",
Form(
"points %d left %d right %d", npoints, nleft, nright));
534 if (nleft<nright)
Warning(
"Build",
"Problem Left-Right");
535 if (nleft<0 || nright<0)
Warning(
"Build()",
"Problem Negative number");
545template <
typename Index,
typename Value>
550 Error(
"FindNearestNeighbors",
"Working arrays must be allocated by the user!");
554 dist[i]=std::numeric_limits<Value>::max();
557 MakeBoundariesExact();
558 UpdateNearestNeighbors(0, point,
kNN, ind, dist);
565template <
typename Index,
typename Value>
570 DistanceToNode(point, inode, min, max);
571 if (min > dist[
kNN-1]){
575 if (IsTerminal(inode)) {
577 Index
f1, l1, f2, l2;
578 GetNodePointsIndexes(inode,
f1, l1, f2, l2);
579 for (
Int_t ipoint=
f1; ipoint<=l1; ipoint++){
580 Double_t d = Distance(point, fIndPoints[ipoint]);
584 while(ishift<kNN && d>dist[ishift])
593 ind[ishift]=fIndPoints[ipoint];
598 if (point[fAxis[inode]]<fValue[inode]){
600 UpdateNearestNeighbors(GetLeft(inode), point,
kNN, ind, dist);
601 UpdateNearestNeighbors(GetRight(inode), point,
kNN, ind, dist);
603 UpdateNearestNeighbors(GetRight(inode), point,
kNN, ind, dist);
604 UpdateNearestNeighbors(GetLeft(inode), point,
kNN, ind, dist);
612template <
typename Index,
typename Value>
617 for (
Int_t idim=0; idim<fNDim; idim++){
618 dist+=(point[idim]-fData[idim][ind])*(point[idim]-fData[idim][ind]);
622 for (
Int_t idim=0; idim<fNDim; idim++){
623 dist+=
TMath::Abs(point[idim]-fData[idim][ind]);
637template <
typename Index,
typename Value>
640 Value *bound = GetBoundaryExact(inode);
646 for (
Int_t idim=0; idim<fNDimm; idim+=2){
647 dist1 = (point[idim/2]-bound[idim])*(point[idim/2]-bound[idim]);
648 dist2 = (point[idim/2]-bound[idim+1])*(point[idim/2]-bound[idim+1]);
650 if (point[idim/2]<bound[idim] || point[idim/2]>bound[idim+1])
651 min+= (dist1>dist2)? dist2 : dist1;
653 max+= (dist1>dist2)? dist1 : dist2;
658 for (
Int_t idim=0; idim<fNDimm; idim+=2){
659 dist1 =
TMath::Abs(point[idim/2]-bound[idim]);
660 dist2 =
TMath::Abs(point[idim/2]-bound[idim+1]);
662 min+= (dist1>dist2)? dist2 : dist1;
664 max+= (dist1>dist2)? dist1 : dist2;
674template <
typename Index,
typename Value>
677 Index stackNode[128], inode;
678 Int_t currentIndex =0;
680 while (currentIndex>=0){
681 inode = stackNode[currentIndex];
682 if (IsTerminal(inode))
return inode;
685 if (point[fAxis[inode]]<=fValue[inode]){
687 stackNode[currentIndex]=(inode<<1)+1;
689 if (point[fAxis[inode]]>=fValue[inode]){
691 stackNode[currentIndex]=(inode+1)<<1;
705template <
typename Index,
typename Value>
707 Int_t stackNode[128];
708 Int_t currentIndex =0;
712 while (currentIndex>=0){
714 Int_t inode = stackNode[currentIndex];
716 if (IsTerminal(inode)){
718 Int_t indexIP = (inode >= fCrossNode) ? (inode-fCrossNode)*fBucketSize : (inode-fNNodes)*fBucketSize+fOffset;
719 printf(
"terminal %d indexP %d\n", inode, indexIP);
720 for (
Int_t ibucket=0;ibucket<fBucketSize;ibucket++){
723 printf(
"ibucket %d index %d\n", ibucket, indexIP);
724 if (indexIP>=fNPoints)
continue;
725 Int_t index0 = fIndPoints[indexIP];
726 for (
Int_t idim=0;idim<fNDim;idim++)
if (fData[idim][index0]!=point[idim]) isOK =
kFALSE;
727 if (isOK)
index = index0;
732 if (point[fAxis[inode]]<=fValue[inode]){
734 stackNode[currentIndex]=(inode*2)+1;
736 if (point[fAxis[inode]]>=fValue[inode]){
738 stackNode[currentIndex]=(inode*2)+2;
751template <
typename Index,
typename Value>
754 MakeBoundariesExact();
755 UpdateRange(0, point, range, res);
761template <
typename Index,
typename Value>
765 DistanceToNode(point, inode, min, max);
770 if (max<range && max>0) {
773 Index
f1, l1, f2, l2;
774 GetNodePointsIndexes(inode,
f1, l1, f2, l2);
776 for (
Int_t ipoint=
f1; ipoint<=l1; ipoint++){
777 res.push_back(fIndPoints[ipoint]);
779 for (
Int_t ipoint=f2; ipoint<=l2; ipoint++){
780 res.push_back(fIndPoints[ipoint]);
786 if (IsTerminal(inode)){
788 Index
f1, l1, f2, l2;
790 GetNodePointsIndexes(inode,
f1, l1, f2, l2);
791 for (
Int_t ipoint=
f1; ipoint<=l1; ipoint++){
792 d = Distance(point, fIndPoints[ipoint]);
794 res.push_back(fIndPoints[ipoint]);
799 if (point[fAxis[inode]]<fValue[inode]){
801 UpdateRange(GetLeft(inode),point, range, res);
802 UpdateRange(GetRight(inode),point, range, res);
804 UpdateRange(GetRight(inode),point, range, res);
805 UpdateRange(GetLeft(inode),point, range, res);
814template <
typename Index,
typename Value>
817 if (!IsTerminal(node)){
818 printf(
"GetPointsIndexes() only for terminal nodes, use GetNodePointsIndexes() instead\n");
821 Int_t offset = (node >= fCrossNode) ? (node-fCrossNode)*fBucketSize : fOffset+(node-fNNodes)*fBucketSize;
822 return &fIndPoints[
offset];
843template <
typename Index,
typename Value>
847 if (IsTerminal(node)){
849 Index
offset = (node >= fCrossNode) ? (node-fCrossNode)*fBucketSize : fOffset+(node-fNNodes)*fBucketSize;
851 last1 =
offset + GetNPointsNode(node)-1;
857 Index firsttermnode = fNNodes;
860 Index
f1, l1, f2, l2;
862 while (ileft<firsttermnode)
863 ileft = GetLeft(ileft);
865 while (iright<firsttermnode)
866 iright = GetRight(iright);
873 GetNodePointsIndexes(firsttermnode,
f1, l1, f2, l2);
875 GetNodePointsIndexes(iright,
f1, l1, f2, l2);
877 GetNodePointsIndexes(ileft,
f1, l1, f2, l2);
879 GetNodePointsIndexes(fTotalNodes-1,
f1, l1, f2, l2);
883 GetNodePointsIndexes(ileft,
f1, l1, f2, l2);
885 GetNodePointsIndexes(iright,
f1, l1, f2, l2);
897template <
typename Index,
typename Value>
900 if (IsTerminal(inode)){
902 if (inode!=fTotalNodes-1)
return fBucketSize;
904 if (fOffset%fBucketSize==0)
return fBucketSize;
905 else return fOffset%fBucketSize;
910 GetNodePointsIndexes(inode,
f1, l1, f2, l2);
920template <
typename Index,
typename Value>
945template <
typename Index,
typename Value>
948 if (fAxis || fValue) {
949 Error(
"SetData",
"The tree has already been built, no updates possible");
954 fData =
new Value*[fNDim];
965template <
typename Index,
typename Value>
971 for (i=0; i<ntotal; i++){
982template <
typename Index,
typename Value>
985 Index i, ir, j,
l, mid;
1014 do i++;
while (
a[
index[i]]<
a[arr]);
1015 do j--;
while (
a[
index[j]]>
a[arr]);
1021 if (j>=rk) ir = j-1;
1036template <
typename Index,
typename Value>
1040 if(range) memcpy(fRange, range, fNDimm*
sizeof(Value));
1042 Int_t totNodes = fNNodes + fNPoints/fBucketSize + ((fNPoints%fBucketSize)?1:0);
1043 fBoundaries =
new Value[totNodes*fNDimm];
1048 Value *tbounds =
nullptr, *cbounds =
nullptr;
1050 for(
int inode=fNNodes-1; inode>=0; inode--){
1051 tbounds = &fBoundaries[inode*fNDimm];
1052 memcpy(tbounds, fRange, fNDimm*
sizeof(Value));
1056 if(IsTerminal(cn)) CookBoundaries(inode,
kTRUE);
1057 cbounds = &fBoundaries[fNDimm*cn];
1058 for(
int idim=0; idim<fNDim; idim++) tbounds[idim<<1] = cbounds[idim<<1];
1062 if(IsTerminal(cn)) CookBoundaries(inode,
kFALSE);
1063 cbounds = &fBoundaries[fNDimm*cn];
1064 for(
int idim=0; idim<fNDim; idim++) tbounds[(idim<<1)+1] = cbounds[(idim<<1)+1];
1071template <
typename Index,
typename Value>
1078 Value *tbounds = &fBoundaries[fNDimm*
index];
1079 memcpy(tbounds, fRange, fNDimm*
sizeof(Value));
1081 memset(flag,
kFALSE, fNDimm);
1086 while(pn >= 0 && nvals < fNDimm){
1088 index = (fAxis[pn]<<1)+1;
1090 tbounds[
index] = fValue[pn];
1095 index = fAxis[pn]<<1;
1097 tbounds[
index] = fValue[pn];
1116template <
typename Index,
typename Value>
1126 fBoundaries =
new Value[fTotalNodes*fNDimm];
1127 Value *min =
new Value[fNDim];
1128 Value *max =
new Value[fNDim];
1129 for (Index inode=fNNodes; inode<fTotalNodes; inode++){
1131 for (Index idim=0; idim<fNDim; idim++){
1132 min[idim]= std::numeric_limits<Value>::max();
1133 max[idim]=-std::numeric_limits<Value>::max();
1135 Index *
points = GetPointsIndexes(inode);
1136 Index npoints = GetNPointsNode(inode);
1138 for (Index ipoint=0; ipoint<npoints; ipoint++){
1139 for (Index idim=0; idim<fNDim; idim++){
1140 if (fData[idim][
points[ipoint]]<min[idim])
1141 min[idim]=fData[idim][
points[ipoint]];
1142 if (fData[idim][
points[ipoint]]>max[idim])
1143 max[idim]=fData[idim][
points[ipoint]];
1146 for (Index idim=0; idim<fNDimm; idim+=2){
1147 fBoundaries[inode*fNDimm + idim]=min[idim/2];
1148 fBoundaries[inode*fNDimm + idim+1]=max[idim/2];
1156 for (Index inode=fNNodes-1; inode>=0; inode--){
1158 left = GetLeft(inode)*fNDimm;
1159 right = GetRight(inode)*fNDimm;
1160 for (Index idim=0; idim<fNDimm; idim+=2){
1162 fBoundaries[inode*fNDimm+idim]=
TMath::Min(fBoundaries[left+idim], fBoundaries[right+idim]);
1164 fBoundaries[inode*fNDimm+idim+1]=
TMath::Max(fBoundaries[left+idim+1], fBoundaries[right+idim+1]);
1175template <
typename Index,
typename Value>
1178 for (;inode<fNNodes;){
1179 if (
TMath::Abs(point[fAxis[inode]] - fValue[inode])<delta[fAxis[inode]])
break;
1180 inode = (point[fAxis[inode]] < fValue[inode]) ? (inode*2)+1: (inode*2)+2;
1187template <
typename Index,
typename Value>
1190 if(!fBoundaries) MakeBoundaries();
1198template <
typename Index,
typename Value>
1201 if(!fBoundaries) MakeBoundariesExact();
1208template <
typename Index,
typename Value>
1211 if(!fBoundaries) MakeBoundaries();
1212 return &fBoundaries[node*2*fNDim];
1218template <
typename Index,
typename Value>
1221 if(!fBoundaries) MakeBoundariesExact();
1222 return &fBoundaries[node*2*fNDim];
1234 data[0] = &data0[0];
1235 data[1] = &data0[npoints];
1236 for (
Int_t i=0;i<npoints;i++) {
int Int_t
Signed integer 4 bytes (int)
unsigned char UChar_t
Unsigned Character 1 byte (unsigned char)
float Float_t
Float 4 bytes (float)
void Info(const char *location, const char *msgfmt,...)
Use this function for informational messages.
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t points
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
TKDTreeIF * TKDTreeTestBuild()
TKDTree< Int_t, Float_t > TKDTreeIF
R__EXTERN TRandom * gRandom
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Class implementing a kd-tree.
void FindPoint(Value *point, Index &index, Int_t &iter)
find the index of point works only if we keep fData pointers
void SetData(Index npoints, Index ndim, UInt_t bsize, Value **data)
Set the data array. See the constructor function comments for details.
~TKDTree() override
Destructor By default, the original data is not owned by kd-tree and is not deleted with it.
void GetNodePointsIndexes(Int_t node, Int_t &first1, Int_t &last1, Int_t &first2, Int_t &last2) const
Return the indices of points in that node Indices are returned as the first and last value of the par...
Value * GetBoundaryExact(const Int_t node)
Get a boundary.
void FindBNodeA(Value *point, Value *delta, Int_t &inode)
find the smallest node covering the full range - start
void UpdateRange(Index inode, Value *point, Value range, std::vector< Index > &res)
Internal recursive function with the implementation of range searches.
Index FindNode(const Value *point) const
returns the index of the terminal node to which point belongs (index in the fAxis,...
Value KOrdStat(Index ntotal, Value *a, Index k, Index *index) const
copy of the TMath::KOrdStat because I need an Index work array
Value * GetBoundariesExact()
Get the boundaries.
void MakeBoundariesExact()
Build boundaries for each node.
void CookBoundaries(const Int_t node, Bool_t left)
define index of this terminal node
void MakeBoundaries(Value *range=nullptr)
Build boundaries for each node.
void UpdateNearestNeighbors(Index inode, const Value *point, Int_t kNN, Index *ind, Value *dist)
Update the nearest neighbors values by examining the node inode.
Double_t Distance(const Value *point, Index ind, Int_t type=2) const
Find the distance between point of the first argument and the point at index value ind Type argument ...
Index * GetPointsIndexes(Int_t node) const
return the indices of the points in that terminal node for all the nodes except last,...
void Spread(Index ntotal, Value *a, Index *index, Value &min, Value &max) const
Calculate spread of the array a.
void Build()
Build the kd-tree.
void DistanceToNode(const Value *point, Index inode, Value &min, Value &max, Int_t type=2)
Find the minimal and maximal distance from a given point to a given node.
TKDTree()
Default constructor. Nothing is built.
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.
Index GetNPointsNode(Int_t node) const
Get number of points in this node for all the terminal nodes except last, the size is fBucketSize for...
Value * GetBoundaries()
Get the boundaries.
Value * GetBoundary(const Int_t node)
Get a boundary.
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,...
kNN::Event describes point in input variable vector-space, with additional functionality like distanc...
Mother of all ROOT objects.
Double_t Rndm() override
Machine independent random number generator.
Short_t Max(Short_t a, Short_t b)
Returns the largest of a and b.
Double_t Sqrt(Double_t x)
Returns the square root of x.
Short_t Min(Short_t a, Short_t b)
Returns the smallest of a and b.
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
static uint64_t sum(uint64_t i)