CPU and Memory tests, coding violations fixed, library
[u/mrichter/AliRoot.git] / STAT / TKDInterpolator.cxx
index ad7f08d991fa0638ceb49a656e554410b24cceff..1d643368b7d0858e25a9a375cf5fb972b6ba23d0 100644 (file)
@@ -1,23 +1,21 @@
 #include "TKDInterpolator.h"
 
 #include "TLinearFitter.h"
-#include "TVector.h"
 #include "TTree.h"
 #include "TH2.h"
 #include "TObjArray.h"
 #include "TObjString.h"
-#include "TPad.h"
 #include "TBox.h"
 #include "TGraph.h"
 #include "TMarker.h"
-#include "TRandom.h"
-#include "TROOT.h"
+#include "TVectorD.h"
+#include "TMatrixD.h"
 
 ClassImp(TKDInterpolator)
 ClassImp(TKDInterpolator::TKDNodeInfo)
 
 /////////////////////////////////////////////////////////////////////
-// Memory setup of protected data memebers
+// Memory setup of protected data members
 // fRefPoints : evaluation point of PDF for each terminal node of underlying KD Tree.
 // | 1st terminal node (fNDim point coordinates) | 2nd terminal node (fNDim point coordinates) | ...
 //
@@ -28,16 +26,94 @@ ClassImp(TKDInterpolator::TKDNodeInfo)
 /////////////////////////////////////////////////////////////////////
 
 //_________________________________________________________________
-TKDInterpolator::TKDInterpolator() : TKDTreeIF()
-       ,fNTNodes(0)
-       ,fRefPoints(0x0)
-       ,fRefValues(0x0)
+TKDInterpolator::TKDNodeInfo::TKDNodeInfo(const Int_t dim): 
+       fNDim(dim)
+       ,fRefPoint(0x0)
+       ,fRefValue(0.)
        ,fCov(0x0)
        ,fPar(0x0)
-       ,fPDFstatus(0x0)
+{
+       if(fNDim) Build(dim);
+}
+
+//_________________________________________________________________
+TKDInterpolator::TKDNodeInfo::~TKDNodeInfo()
+{
+       if(fRefPoint) delete [] fRefPoint;
+       if(fCov){
+               delete fPar;
+               delete fCov;
+       }
+}
+
+//_________________________________________________________________
+void TKDInterpolator::TKDNodeInfo::Build(const Int_t dim)
+{
+// Allocate/Reallocate space for this node.
+
+       if(!dim) return;
+
+       fNDim = dim;
+       Int_t lambda = Int_t(1 + fNDim + .5*fNDim*(fNDim+1));
+       if(fRefPoint) delete [] fRefPoint;
+       fRefPoint = new Float_t[fNDim];
+       if(fCov){
+               fCov->ResizeTo(lambda, lambda);
+               fPar->ResizeTo(lambda);
+       }
+       return;
+}
+
+//_________________________________________________________________
+void TKDInterpolator::TKDNodeInfo::Store(const TVectorD &par, const TMatrixD &cov)
+{
+       if(!fCov){
+               fCov = new TMatrixD(cov.GetNrows(), cov.GetNrows());
+               fPar = new TVectorD(par.GetNrows());
+       }
+       (*fPar) = par;
+       (*fCov) = cov;
+}
+
+//_________________________________________________________________
+Double_t TKDInterpolator::TKDNodeInfo::CookPDF(const Double_t *point, Double_t &result, Double_t &error)
+{
+// Recalculate the PDF for one node from the results of interpolation (parameters and covariance matrix)
+
+       if(fNDim>10) return 0.; // support only up to 10 dimensions
+       
+       Int_t lambda = 1 + fNDim + (fNDim*(fNDim+1)>>1);
+       Double_t fdfdp[66];
+       Int_t ipar = 0;
+       fdfdp[ipar++] = 1.;
+       for(int idim=0; idim<fNDim; idim++){
+               fdfdp[ipar++] = point[idim];
+               for(int jdim=idim; jdim<fNDim; jdim++) fdfdp[ipar++] = point[idim]*point[jdim];
+       }
+
+       // calculate estimation
+       result =0.; error = 0.;
+       for(int i=0; i<lambda; i++){
+               result += fdfdp[i]*(*fPar)(i);
+               for(int j=0; j<lambda; j++) error += fdfdp[i]*fdfdp[j]*(*fCov)(i,j);
+       }       
+       error = TMath::Sqrt(error);
+       
+       //printf("TKDNodeInfo::CookPDF() : %6.3f +- %6.3f\n", result, error);
+
+       return 0.;
+}
+
+
+//_________________________________________________________________
+TKDInterpolator::TKDInterpolator() : TKDTreeIF()
+       ,fNTNodes(0)
+       ,fTNodes(0x0)
        ,fStatus(4)
        ,fLambda(0)
        ,fDepth(-1)
+       ,fAlpha(.5)
+       ,fRefPoints(0x0)
        ,fBuffer(0x0)
        ,fKDhelper(0x0)
        ,fFitter(0x0)
@@ -48,21 +124,19 @@ TKDInterpolator::TKDInterpolator() : TKDTreeIF()
 
 //_________________________________________________________________
 TKDInterpolator::TKDInterpolator(Int_t npoints, Int_t ndim, UInt_t bsize, Float_t **data) : TKDTreeIF(npoints, ndim, bsize, data)
-       ,fNTNodes(GetNTerminalNodes())
-       ,fRefPoints(0x0)
-       ,fRefValues(0x0)
-       ,fCov(0x0)
-       ,fPar(0x0)
-       ,fPDFstatus(0x0)
+       ,fNTNodes(GetNTNodes())
+       ,fTNodes(0x0)
        ,fStatus(4)
        ,fLambda(0)
        ,fDepth(-1)
+       ,fAlpha(.5)
+       ,fRefPoints(0x0)
        ,fBuffer(0x0)
        ,fKDhelper(0x0)
        ,fFitter(0x0)
 {
-// Wrapper constructor for the similar TKDTree one.
-       
+// Wrapper constructor for the TKDTree.
+
        Build();
 }
 
@@ -70,14 +144,12 @@ TKDInterpolator::TKDInterpolator(Int_t npoints, Int_t ndim, UInt_t bsize, Float_
 //_________________________________________________________________
 TKDInterpolator::TKDInterpolator(TTree *t, const Char_t *var, const Char_t *cut, UInt_t bsize, Long64_t nentries, Long64_t firstentry) : TKDTreeIF()
        ,fNTNodes(0)
-       ,fRefPoints(0x0)
-       ,fRefValues(0x0)
-       ,fCov(0x0)
-       ,fPar(0x0)
-       ,fPDFstatus(0x0)
+       ,fTNodes(0x0)
        ,fStatus(4)
        ,fLambda(0)
        ,fDepth(-1)
+       ,fAlpha(.5)
+       ,fRefPoints(0x0)
        ,fBuffer(0x0)
        ,fKDhelper(0x0)
        ,fFitter(0x0)
@@ -100,12 +172,12 @@ TKDInterpolator::TKDInterpolator(TTree *t, const Char_t *var, const Char_t *cut,
                        Warning("TKDInterpolator(TTree*, const Char_t, const Char_t, UInt_t)", Form("Can not access data for keys %s. Key defined on tree :", ((TObjString*)(*vars)[idim])->GetName() ));
                        TIterator *it = (t->GetListOfLeaves())->MakeIterator();
                        TObject *o;
-                       while(o = (*it)()) printf("\t%s\n", o->GetName());
+                       while((o = (*it)())) printf("\t%s\n", o->GetName());
                        continue;
                }
                if(!fNpoints){
                        fNpoints = np;
-                       Info("TKDInterpolator(TTree*, const Char_t, const Char_t, UInt_t)", Form("Allocating %d data points in %d dimensions.", fNpoints, fNDim));
+                       //Info("TKDInterpolator(TTree*, const Char_t, const Char_t, UInt_t)", Form("Allocating %d data points in %d dimensions.", fNpoints, fNDim));
                        fData = new Float_t*[fNDim];
                        for(int idim=0; idim<fNDim; idim++) fData[idim] = new Float_t[fNpoints];
                        kDataOwner = kTRUE;
@@ -114,19 +186,12 @@ TKDInterpolator::TKDInterpolator(TTree *t, const Char_t *var, const Char_t *cut,
                for(int ip=0; ip<fNpoints; ip++) fData[idim][ip] = (Float_t)v[ip];
        }
        TKDTreeIF::Build();
-       fNTNodes = GetNTerminalNodes();
        Build();
 }
 
 //_________________________________________________________________
 TKDInterpolator::~TKDInterpolator()
 {
-       if(fCov){
-               delete [] fPar;
-               delete [] fCov;
-               delete [] fPDFstatus;
-       }
-       
        if(fFitter) delete fFitter;
        if(fKDhelper) delete fKDhelper;
        if(fBuffer) delete [] fBuffer;
@@ -135,7 +200,7 @@ TKDInterpolator::~TKDInterpolator()
                for(int idim=0; idim<fNDim; idim++) delete [] fRefPoints[idim] ;
                delete [] fRefPoints;
        }
-       if(fRefValues) delete [] fRefValues;
+       if(fTNodes) delete [] fTNodes;
 }
 
 //_________________________________________________________________
@@ -145,29 +210,32 @@ void TKDInterpolator::Build()
 //  - estimation points 
 //  - corresponding PDF values
 
+       fNTNodes = TKDTreeIF::GetNTNodes();
        if(!fBoundaries) MakeBoundaries();
-       fLambda = 1 + fNDim + fNDim*(fNDim+1)/2;
-
+       fLambda = 1 + fNDim + (fNDim*(fNDim+1)>>1);
+       //printf("after MakeBoundaries() %d\n", memory());
+       
        // allocate memory for data
-       fRefValues = new Float_t[fNTNodes];
-       fRefPoints = new Float_t*[fNDim];
-       for(int id=0; id<fNDim; id++){
-               fRefPoints[id] = new Float_t[fNTNodes];
-               for(int in=0; in<fNTNodes; in++) fRefPoints[id][in] = 0.;
-       }
+       fTNodes = new TKDNodeInfo[fNTNodes];
+       for(int in=0; in<fNTNodes; in++) fTNodes[in].Build(fNDim);
+       //printf("after BuildNodes() %d\n", memory());
 
        Float_t *bounds = 0x0;
        Int_t *indexPoints;
        for(int inode=0, tnode = fNnodes; inode<fNTNodes-1; inode++, tnode++){
-               fRefValues[inode] =  Float_t(fBucketSize)/fNpoints;
+               fTNodes[inode].fRefValue =  Float_t(fBucketSize)/fNpoints;
                bounds = GetBoundary(tnode);
-               for(int idim=0; idim<fNDim; idim++) fRefValues[inode] /= (bounds[2*idim+1] - bounds[2*idim]);
-
+               for(int idim=0; idim<fNDim; idim++) fTNodes[inode].fRefValue /= (bounds[2*idim+1] - bounds[2*idim]);
+               
                indexPoints = GetPointsIndexes(tnode);
                // loop points in this terminal node
                for(int idim=0; idim<fNDim; idim++){
-                       for(int ip = 0; ip<fBucketSize; ip++) fRefPoints[idim][inode] += fData[idim][indexPoints[ip]];
-                       fRefPoints[idim][inode] /= fBucketSize;
+                       fTNodes[inode].fRefPoint[idim] = 0.;
+                       for(int ip = 0; ip<fBucketSize; ip++){
+/*                             printf("\t\tindex[%d] = %d %f\n", ip, indexPoints[ip], fData[idim][indexPoints[ip]]);*/
+                               fTNodes[inode].fRefPoint[idim] += fData[idim][indexPoints[ip]];
+                       }
+                       fTNodes[inode].fRefPoint[idim] /= fBucketSize;
                }
        }
 
@@ -175,46 +243,47 @@ void TKDInterpolator::Build()
        Int_t counts = fNpoints%fBucketSize;
        counts = counts ? counts : fBucketSize;
        Int_t inode = fNTNodes - 1, tnode = inode + fNnodes;
-       fRefValues[inode] =  Float_t(counts)/fNpoints;
+       fTNodes[inode].fRefValue =  Float_t(counts)/fNpoints;
        bounds = GetBoundary(tnode);
-       for(int idim=0; idim<fNDim; idim++) fRefValues[inode] /= (bounds[2*idim+1] - bounds[2*idim]);
+       for(int idim=0; idim<fNDim; idim++) fTNodes[inode].fRefValue /= (bounds[2*idim+1] - bounds[2*idim]);
 
-       indexPoints = GetPointsIndexes(tnode);
        // loop points in this terminal node
+       indexPoints = GetPointsIndexes(tnode);
        for(int idim=0; idim<fNDim; idim++){
-               for(int ip = 0; ip<counts; ip++) fRefPoints[idim][inode] += fData[idim][indexPoints[ip]];
-               fRefPoints[idim][inode] /= counts;
+               fTNodes[inode].fRefPoint[idim] = 0.;
+               for(int ip = 0; ip<counts; ip++) fTNodes[inode].fRefPoint[idim] += fData[idim][indexPoints[ip]];
+               fTNodes[inode].fRefPoint[idim] /= counts;
        }
 }
 
 //__________________________________________________________________
 void TKDInterpolator::GetStatus()
 {
+// Prints the status of the interpolator
+
        printf("Interpolator Status :\n");
        printf("  Method : %s\n", fStatus&1 ? "INT" : "COG");
        printf("  Store  : %s\n", fStatus&2 ? "YES" : "NO");
        printf("  Weights: %s\n", fStatus&4 ? "YES" : "NO");
-
-       printf("nodes %d\n", fNTNodes);        //Number of evaluation data points
-       printf("refs 0x%x\n", fRefPoints);    //[fNDim][fNTNodes]
-       printf("vals 0x%x\n", fRefValues);     //[fNTNodes]
-       for(int i=0; i<fNTNodes; i++){
-               for(int idim=0; idim<fNDim; idim++) printf("%f ", fRefPoints[idim][i]);
-               printf("[%f]\n", fRefValues[i]);
-       }
-
-       printf("cov 0x%x\n", fCov);           //[fNTNodes] cov matrix array for nodes
-       printf("pars 0x%x\n", fPar);           //[fNTNodes] parameters array for nodes
+       return;
+       
+       printf("fNTNodes %d\n", fNTNodes);        //Number of evaluation data points
        for(int i=0; i<fNTNodes; i++){
                printf("%d ", i);
-               for(int ip=0; ip<3; ip++) printf("p%d[%f] ", ip, fPar[i](ip));
+               for(int idim=0; idim<fNDim; idim++) printf("%f ", fTNodes[i].fRefPoint[idim]);
+               printf("[%f] %s\n", fTNodes[i].fRefValue, fTNodes[i].fCov ? "true" : "false");
+               printf("Fit parameters : ");
+               if(!fTNodes[i].fPar){
+                       printf("Not defined.\n");
+                       continue;
+               }
+               for(int ip=0; ip<3; ip++) printf("p%d[%f] ", ip, (*fTNodes[i].fPar)(ip));
                printf("\n");
        }
-       printf("pdf 0x%x\n", fPDFstatus);     //[fNTNodes] status bit for node's PDF
 }
 
 //_________________________________________________________________
-Double_t TKDInterpolator::Eval(const Double_t *point, Double_t &result, Double_t &error)
+Double_t TKDInterpolator::Eval(const Double_t *point, Double_t &result, Double_t &error, Bool_t force)
 {
 // Evaluate PDF for "point". The result is returned in "result" and error in "error". The function returns the chi2 of the fit.
 //
@@ -226,12 +295,25 @@ Double_t TKDInterpolator::Eval(const Double_t *point, Double_t &result, Double_t
        Float_t pointF[50]; // local Float_t conversion for "point"
        for(int idim=0; idim<fNDim; idim++) pointF[idim] = (Float_t)point[idim];
        Int_t node = FindNode(pointF) - fNnodes;
-       if(fPDFstatus && (fStatus&1) && fPDFstatus[node]) return CookPDF(point, node, result, error);
+       if((fStatus&1) && fTNodes[node].fCov && !force) return fTNodes[node].CookPDF(point, result, error);
 
        // Allocate memory
        if(!fBuffer) fBuffer = new Double_t[2*fLambda];
-       if(!fKDhelper) fKDhelper = new TKDTreeIF(fNTNodes, fNDim, 30, fRefPoints);
-       if(!fFitter) SetIntInterpolation(kFALSE);
+       if(!fKDhelper){ 
+               fRefPoints = new Float_t*[fNDim];
+               for(int id=0; id<fNDim; id++){ 
+                       fRefPoints[id] = new Float_t[fNTNodes];
+                       for(int in=0; in<fNTNodes; in++) fRefPoints[id][in] = fTNodes[in].fRefPoint[id];
+               }
+//             for(int in=0; in<fNTNodes; in++){
+//                     printf("%3d ", in);
+//                     for(int id=0; id<fNDim; id++) printf("%6.3f ", fTNodes[in].fRefPoint[id]/*fRefPoints[id][in]*/);
+//                     printf("\n");
+//             }
+               fKDhelper = new TKDTreeIF(fNTNodes, fNDim, 30, fRefPoints);
+               fKDhelper->MakeBoundaries();
+       }
+       if(!fFitter) fFitter = new TLinearFitter(fLambda, Form("hyp%d", fLambda-1));
        
        // generate parabolic for nD
        //Float_t alpha = Float_t(2*lambda + 1) / fNTNodes; // the bandwidth or smoothing parameter
@@ -241,13 +323,14 @@ Double_t TKDInterpolator::Eval(const Double_t *point, Double_t &result, Double_t
 
        Int_t *index,  // indexes of NN 
              ipar,    // local looping variable
-                               npoints = Int_t(1.5*fLambda); // number of data points used for interpolation
+                               npoints = Int_t((1.+fAlpha)*fLambda); // number of data points used for interpolation
        Float_t *dist, // distances of NN
                                        d,     // NN normalized distance
                                        w0,    // work
                                        w;     // tri-cubic weight function
        Double_t sig   // bucket error 
                = TMath::Sqrt(1./fBucketSize);
+
        do{
                // find nearest neighbors
                for(int idim=0; idim<fNDim; idim++) pointF[idim] = (Float_t)point[idim];
@@ -259,11 +342,11 @@ Double_t TKDInterpolator::Eval(const Double_t *point, Double_t &result, Double_t
                }
                // add points to fitter
                fFitter->ClearPoints();
+               TKDNodeInfo *node = 0x0;
                for(int in=0; in<npoints; in++){
+                       node = &fTNodes[index[in]];
                        if(fStatus&1){ // INT
-                               for(int idim=0; idim<fNDim; idim++) pointF[idim] = fRefPoints[idim][index[in]];
-                               Float_t *bounds = GetBoundary(FindNode(pointF));
-                               
+                               Float_t *bounds = GetBoundary(FindNode(node->fRefPoint));
                                ipar = 0;
                                for(int idim=0; idim<fNDim; idim++){
                                        fBuffer[ipar++] = .5*(bounds[2*idim] + bounds[2*idim+1]);
@@ -271,7 +354,12 @@ Double_t TKDInterpolator::Eval(const Double_t *point, Double_t &result, Double_t
                                        for(int jdim=idim+1; jdim<fNDim; jdim++) fBuffer[ipar++] = (bounds[2*idim] + bounds[2*idim+1]) * (bounds[2*jdim] + bounds[2*jdim+1]) * .25;
                                }
                        } else { // COG
-                               for(int idim=0; idim<fNDim; idim++) fBuffer[idim] = fRefPoints[idim][index[in]];
+                               Float_t *p = node->fRefPoint;
+                               ipar = 0;
+                               for(int idim=0; idim<fNDim; idim++){
+                                       fBuffer[ipar++] = p[idim];
+                                       for(int jdim=idim; jdim<fNDim; jdim++) fBuffer[ipar++] = p[idim]*p[jdim];
+                               }
                        }
 
                        // calculate tri-cubic weighting function
@@ -282,7 +370,7 @@ Double_t TKDInterpolator::Eval(const Double_t *point, Double_t &result, Double_t
                         
                        //for(int idim=0; idim<fNDim; idim++) printf("%f ", fBuffer[idim]);
                        //printf("\nd[%f] w[%f] sig[%f]\n", d, w, sig);
-                       fFitter->AddPoint(fBuffer, fRefValues[index[in]], fRefValues[index[in]]*sig/w);
+                       fFitter->AddPoint(fBuffer, node->fRefValue, node->fRefValue*sig/w);
                }
                npoints += 4;
        } while(fFitter->Eval());
@@ -295,11 +383,7 @@ Double_t TKDInterpolator::Eval(const Double_t *point, Double_t &result, Double_t
        Double_t chi2 = fFitter->GetChisquare()/(npoints - 4 - fLambda);
 
        // store results
-       if(fStatus&2 && fStatus&1){
-               fPar[node] = par;
-               fCov[node] = cov;
-               fPDFstatus[node] = 1;
-       }
+       if(fStatus&2 && fStatus&1) fTNodes[node].Store(par, cov);
                
        // Build df/dpi|x values
        Double_t *fdfdp = &fBuffer[fLambda];
@@ -321,84 +405,6 @@ Double_t TKDInterpolator::Eval(const Double_t *point, Double_t &result, Double_t
        return chi2;
 }
 
-// //_________________________________________________________________
-// Double_t TKDInterpolator::Eval1(const Double_t *point, Int_t npoints, Double_t &result, Double_t &error)
-// {
-// // Evaluate PDF at k-dimensional position "point". The initial number of
-// // neighbour estimation points is set to "npoints". The default method
-// // used for interpolation is kCOG.
-// 
-//     // calculate number of parameters in the parabolic expresion
-//     Int_t lambda = 1 + fNDim + fNDim*(fNDim+1)/2;
-// 
-//     if(!fBuffer) fBuffer = new Double_t[lambda-1];
-//     if(!fKDhelper) fKDhelper = new TKDTreeIF(GetNTerminalNodes(), fNDim, npoints, fRefPoints);
-// 
-//     if(!fFitter) fFitter = new TLinearFitter(lambda, Form("hyp%d", fNDim+1));
-//     else fFitter->SetFormula(Form("hyp%d", fNDim+1));
-// 
-// 
-//     Float_t pointF[50];
-//     for(int idim=0; idim<fNDim; idim++) pointF[idim] = point[idim];
-//     Int_t istart = 0;
-//     Int_t *index, ipar;
-//     Float_t *bounds, *dist, *w = new Float_t[fNDim];
-//     Double_t uncertainty = TMath::Sqrt(1./fBucketSize);
-//     fFitter->ClearPoints();
-//     do{
-//             if(!fKDhelper->FindNearestNeighbors(pointF, npoints+1, index, dist)){
-//                     Error("Eval()", Form("Failed retriving %d neighbours for point:", npoints));
-//                     for(int idim=0; idim<fNDim; idim++) printf("%f ", point[idim]);
-//                     printf("\n");
-//                     return -1;
-//             }
-//             for(int in=istart; in<npoints; in++){
-//                     for(int idim=0; idim<fNDim; idim++) w[idim] = fRefPoints[idim][index[in]];
-//                     bounds = GetBoundary(FindNode(w));
-// 
-//                     ipar = 0;
-//                     for(int idim=0; idim<fNDim; idim++){
-//                             fBuffer[ipar++] = .5*(bounds[2*idim] + bounds[2*idim+1]);
-//                             fBuffer[ipar++] = (bounds[2*idim]*bounds[2*idim] + bounds[2*idim] * bounds[2*idim+1] + bounds[2*idim+1] * bounds[2*idim+1])/3.;
-//                             for(int jdim=idim+1; jdim<fNDim; jdim++) fBuffer[ipar++] = (bounds[2*idim] + bounds[2*idim+1]) * (bounds[2*jdim] + bounds[2*jdim+1]) * .25;
-//                     }
-// 
-//                     fFitter->AddPoint(fBuffer, fRefValues[index[in]], fRefValues[index[in]]*uncertainty);
-//             }
-//             istart = npoints;
-//             npoints += 4;
-//     } while(fFitter->Eval());
-//     delete [] w;
-// 
-//     // calculate evaluation
-// //  fFitter->PrintResults(3);
-//     TMatrixD cov(lambda, lambda);
-//     TVectorD par(lambda);
-//     fFitter->GetCovarianceMatrix(cov);
-//     fFitter->GetParameters(par);
-// 
-//     // Build temporary array to keep values df/dpi|x
-//     Double_t f[100];
-//     ipar = 0;
-//     f[ipar++] = 1.;
-//     for(int idim=0; idim<fNDim; idim++){
-//             f[ipar++] = point[idim];
-//             for(int jdim=idim; jdim<fNDim; jdim++) f[ipar++] = point[idim]*point[jdim];
-//     }
-//     result =0.; error = 0.;
-//     for(int i=0; i<lambda; i++){
-//             result += f[i]*par[i];
-//             for(int j=0; j<lambda; j++) error += f[i]*f[j]*cov(i,j);
-//     }
-//     error = TMath::Sqrt(error);
-//     Double_t chi2 = fFitter->GetChisquare()/(npoints - 4 - lambda);
-// 
-//     for(int ipar=0; ipar<lambda; ipar++) printf("%d %8.6e %8.6e\n", ipar, par[ipar], TMath::Sqrt(cov(ipar, ipar)));
-//     printf("result %6.3f +- %6.3f [%f]\n", result, error, chi2);
-//     return chi2;
-// }
-
-
 //_________________________________________________________________
 void TKDInterpolator::DrawNodes(UInt_t ax1, UInt_t ax2, Int_t depth)
 {
@@ -424,14 +430,13 @@ void TKDInterpolator::DrawNodes(UInt_t ax1, UInt_t ax2, Int_t depth)
 
        //printf("depth %d nodes %d\n", depth, nnodes);
        
-       TH2 *h2 = 0x0;
-       if(!(h2 = (TH2S*)gROOT->FindObject("hNodes"))) h2 = new TH2S("hNodes", "", 100, fRange[2*ax1], fRange[2*ax1+1], 100, fRange[2*ax2], fRange[2*ax2+1]);
+       TH2 *h2 = new TH2S("hNodes", "", 100, fRange[2*ax1], fRange[2*ax1+1], 100, fRange[2*ax2], fRange[2*ax2+1]);
        h2->GetXaxis()->SetTitle(Form("x_{%d}", ax1));
        h2->GetYaxis()->SetTitle(Form("x_{%d}", ax2));
        h2->Draw();
        
-       const Float_t border = 0.;//1.E-4;
-       TBox *node_array = new TBox[nnodes], *node;
+       const Float_t kBorder = 0.;//1.E-4;
+       TBox *nodeArray = new TBox[nnodes], *node;
        Float_t *bounds = 0x0;
        nnodes = 0;
        for(int inode = 0; inode <= 2*fNnodes; inode++){
@@ -439,21 +444,21 @@ void TKDInterpolator::DrawNodes(UInt_t ax1, UInt_t ax2, Int_t depth)
                        if(!IsTerminal(inode)) continue;
                } else if((inode+1) >> depth != 1) continue;
 
-               node = &node_array[nnodes++];
+               node = &nodeArray[nnodes++];
                //node = new TBox(bounds[2*ax1]+border, bounds[2*ax2]+border, bounds[2*ax1+1]-border, bounds[2*ax2+1]-border);
                node->SetFillStyle(3002);       
-               node->SetFillColor(50+Int_t(gRandom->Uniform()*50.));
+               node->SetFillColor(50+inode/*Int_t(gRandom->Uniform()*50.)*/);
                bounds = GetBoundary(inode);
-               node->DrawBox(bounds[2*ax1]+border, bounds[2*ax2]+border, bounds[2*ax1+1]-border, bounds[2*ax2+1]-border);
+               node->DrawBox(bounds[2*ax1]+kBorder, bounds[2*ax2]+kBorder, bounds[2*ax1+1]-kBorder, bounds[2*ax2+1]-kBorder);
        }
        if(depth != -1) return;
 
        // Draw reference points
-       TGraph *ref = new TGraph(GetNTerminalNodes());
+       TGraph *ref = new TGraph(fNTNodes);
        ref->SetMarkerStyle(3);
        ref->SetMarkerSize(.7);
        ref->SetMarkerColor(2);
-       for(int inode = 0; inode < GetNTerminalNodes(); inode++) ref->SetPoint(inode, fRefPoints[ax1][inode], fRefPoints[ax2][inode]);
+       for(int inode = 0; inode < fNTNodes; inode++) ref->SetPoint(inode, fTNodes[inode].fRefPoint[ax1], fTNodes[inode].fRefPoint[ax2]);
        ref->Draw("p");
        return;
 }
@@ -467,7 +472,7 @@ void TKDInterpolator::DrawNode(Int_t tnode, UInt_t ax1, UInt_t ax2)
 // This function creates some graphical objects
 // but don't delete it. Abusing this function may cause memory leaks !
 
-       if(tnode < 0 || tnode >= GetNTerminalNodes()){
+       if(tnode < 0 || tnode >= fNTNodes){
                Warning("DrawNode()", Form("Terminal node %d outside defined range.", tnode));
                return;
        }
@@ -484,7 +489,7 @@ void TKDInterpolator::DrawNode(Int_t tnode, UInt_t ax1, UInt_t ax2)
        for(int ip = 0; ip<nPoints; ip++) g->SetPoint(ip, fData[ax1][index[ip]], fData[ax2][index[ip]]);
 
        // draw estimation point
-       TMarker *m=new TMarker(fRefPoints[ax1][inode], fRefPoints[ax2][inode], 20);
+       TMarker *m=new TMarker(fTNodes[inode].fRefPoint[ax1], fTNodes[inode].fRefPoint[ax2], 20);
        m->SetMarkerColor(2);
        m->SetMarkerSize(1.7);
        
@@ -493,7 +498,6 @@ void TKDInterpolator::DrawNode(Int_t tnode, UInt_t ax1, UInt_t ax2)
        TBox *n = new TBox(bounds[2*ax1], bounds[2*ax2], bounds[2*ax1+1], bounds[2*ax2+1]);
        n->SetFillStyle(0);
 
-       if(gPad) gPad->Clear(); 
        g->Draw("ap");
        m->Draw();
        n->Draw();
@@ -503,86 +507,29 @@ void TKDInterpolator::DrawNode(Int_t tnode, UInt_t ax1, UInt_t ax2)
 
 
 //__________________________________________________________________
-void TKDInterpolator::SetIntInterpolation(const Bool_t on)
+void TKDInterpolator::SetInterpolationMethod(const Bool_t on)
 {
-// Set interpolation bit to "on" and build/delete memory
+// Set interpolation bit to "on".
        
        if(on) fStatus += fStatus&1 ? 0 : 1;
        else fStatus += fStatus&1 ? -1 : 0;
-       TString formula;
-       if(on) formula = Form("hyp%d", fLambda-1);
-       else {
-               formula = "1";
-               for(int idim=0; idim<fNDim; idim++){
-                       formula += Form("++x[%d]", idim);
-                       for(int jdim=idim; jdim<fNDim; jdim++) formula += Form("++x[%d]*x[%d]", idim, jdim);
-               }
-       }
-       if(!fFitter) fFitter = new TLinearFitter(fLambda, formula.Data());
-       else fFitter->SetFormula(formula.Data());
 }
 
 
 //_________________________________________________________________
-void TKDInterpolator::SetSetStore(const Bool_t on)
+void TKDInterpolator::SetStore(const Bool_t on)
 {
-// Set store bit to "on" and build/delete memory
+// Set store bit to "on"
        
-       if(on){
-               fStatus += fStatus&2 ? 0 : 2;
-               if(!fCov){
-                       fPDFstatus = new Bool_t[fNTNodes];
-                       fCov = new TMatrixD[fNTNodes];
-                       fPar = new TVectorD[fNTNodes];
-                       for(int i=0; i<fNTNodes; i++){
-                               fPDFstatus[i] = kFALSE;
-                               fCov[i].ResizeTo(fLambda, fLambda);
-                               fPar[i].ResizeTo(fLambda);
-                       }
-               }
-       } else {
-               fStatus += fStatus&2 ? -2 : 0;
-               if(fCov){
-                       delete [] fPar;
-                       delete [] fCov;
-                       delete [] fPDFstatus;
-               }
-       }
+       if(on) fStatus += fStatus&2 ? 0 : 2;
+       else fStatus += fStatus&2 ? -2 : 0;
 }
 
 //_________________________________________________________________
-void TKDInterpolator::SetUseWeights(const Bool_t on)
+void TKDInterpolator::SetWeights(const Bool_t on)
 {
+// Set weights bit to "on"
+       
        if(on) fStatus += fStatus&4 ? 0 : 4;
        else fStatus += fStatus&4 ? -4 : 0;
 }
-
-
-//_________________________________________________________________
-Double_t TKDInterpolator::CookPDF(const Double_t *point, const Int_t node, Double_t &result, Double_t &error)
-{
-// Recalculate the PDF for one node from the results of interpolation (parameters and covariance matrix)
-
-       Info("CookPDF()", Form("Called for node %d", node));
-
-       if(!fBuffer) fBuffer = new Double_t[2*fLambda];
-       Double_t *fdfdp = &fBuffer[fLambda];
-       Int_t ipar = 0;
-       fdfdp[ipar++] = 1.;
-       for(int idim=0; idim<fNDim; idim++){
-               fdfdp[ipar++] = point[idim];
-               for(int jdim=idim; jdim<fNDim; jdim++) fdfdp[ipar++] = point[idim]*point[jdim];
-       }
-
-       // calculate estimation
-       result =0.; error = 0.;
-       for(int i=0; i<fLambda; i++){
-               result += fdfdp[i]*fPar[node](i);
-               for(int j=0; j<fLambda; j++) error += fdfdp[i]*fdfdp[j]*fCov[node](i,j);
-       }       
-       error = TMath::Sqrt(error);
-       printf("result[CookPDF] %6.3f +- %6.3f\n", result, error);
-
-       return 0.;
-}
-