kNN algorithm improved. IO Defined
[u/mrichter/AliRoot.git] / STAT / TKDInterpolator.cxx
index 88bce8aee7857d795d07bffd95d58a3b321db41d..ad7f08d991fa0638ceb49a656e554410b24cceff 100644 (file)
@@ -6,13 +6,15 @@
 #include "TH2.h"
 #include "TObjArray.h"
 #include "TObjString.h"
 #include "TH2.h"
 #include "TObjArray.h"
 #include "TObjString.h"
+#include "TPad.h"
 #include "TBox.h"
 #include "TGraph.h"
 #include "TMarker.h"
 #include "TBox.h"
 #include "TGraph.h"
 #include "TMarker.h"
-
-
+#include "TRandom.h"
+#include "TROOT.h"
 
 ClassImp(TKDInterpolator)
 
 ClassImp(TKDInterpolator)
+ClassImp(TKDInterpolator::TKDNodeInfo)
 
 /////////////////////////////////////////////////////////////////////
 // Memory setup of protected data memebers
 
 /////////////////////////////////////////////////////////////////////
 // Memory setup of protected data memebers
@@ -21,6 +23,8 @@ ClassImp(TKDInterpolator)
 //
 // fRefValues : evaluation value/error of PDF for each terminal node of underlying KD Tree.
 // | 1st terminal node (value) | 2nd terminal node (value) | ... | 1st terminal node (error) | 2nd terminal node (error) | ...
 //
 // fRefValues : evaluation value/error of PDF for each terminal node of underlying KD Tree.
 // | 1st terminal node (value) | 2nd terminal node (value) | ... | 1st terminal node (error) | 2nd terminal node (error) | ...
+//
+// status = |0|0|0|0|0|1(tri-cubic weights)|1(STORE)|1 INT(0 COG )|
 /////////////////////////////////////////////////////////////////////
 
 //_________________________________________________________________
 /////////////////////////////////////////////////////////////////////
 
 //_________________________________________________________________
@@ -28,11 +32,18 @@ TKDInterpolator::TKDInterpolator() : TKDTreeIF()
        ,fNTNodes(0)
        ,fRefPoints(0x0)
        ,fRefValues(0x0)
        ,fNTNodes(0)
        ,fRefPoints(0x0)
        ,fRefValues(0x0)
+       ,fCov(0x0)
+       ,fPar(0x0)
+       ,fPDFstatus(0x0)
+       ,fStatus(4)
+       ,fLambda(0)
        ,fDepth(-1)
        ,fDepth(-1)
-       ,fTmpPoint(0x0)
+       ,fBuffer(0x0)
        ,fKDhelper(0x0)
        ,fFitter(0x0)
 {
        ,fKDhelper(0x0)
        ,fFitter(0x0)
 {
+// Default constructor. To be used with care since in this case building
+// of data structure is completly left to the user responsability.
 }
 
 //_________________________________________________________________
 }
 
 //_________________________________________________________________
@@ -40,22 +51,34 @@ TKDInterpolator::TKDInterpolator(Int_t npoints, Int_t ndim, UInt_t bsize, Float_
        ,fNTNodes(GetNTerminalNodes())
        ,fRefPoints(0x0)
        ,fRefValues(0x0)
        ,fNTNodes(GetNTerminalNodes())
        ,fRefPoints(0x0)
        ,fRefValues(0x0)
+       ,fCov(0x0)
+       ,fPar(0x0)
+       ,fPDFstatus(0x0)
+       ,fStatus(4)
+       ,fLambda(0)
        ,fDepth(-1)
        ,fDepth(-1)
-       ,fTmpPoint(0x0)
+       ,fBuffer(0x0)
        ,fKDhelper(0x0)
        ,fFitter(0x0)
 {
        ,fKDhelper(0x0)
        ,fFitter(0x0)
 {
+// Wrapper constructor for the similar TKDTree one.
+       
        Build();
 }
 
 
 //_________________________________________________________________
        Build();
 }
 
 
 //_________________________________________________________________
-TKDInterpolator::TKDInterpolator(TTree *t, const Char_t *var, const Char_t *cut, UInt_t bsize) : TKDTreeIF()
+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)
        ,fNTNodes(0)
        ,fRefPoints(0x0)
        ,fRefValues(0x0)
+       ,fCov(0x0)
+       ,fPar(0x0)
+       ,fPDFstatus(0x0)
+       ,fStatus(4)
+       ,fLambda(0)
        ,fDepth(-1)
        ,fDepth(-1)
-       ,fTmpPoint(0x0)
+       ,fBuffer(0x0)
        ,fKDhelper(0x0)
        ,fFitter(0x0)
 {
        ,fKDhelper(0x0)
        ,fFitter(0x0)
 {
@@ -65,24 +88,28 @@ TKDInterpolator::TKDInterpolator(TTree *t, const Char_t *var, const Char_t *cut,
 //
 // 
 
 //
 // 
 
-       fNpoints = t->GetEntriesFast();
        TObjArray *vars = TString(var).Tokenize(":");
        TObjArray *vars = TString(var).Tokenize(":");
-       fNDim = vars->GetEntriesFast();
-       if(fNDim > 6/*kDimMax*/) Warning("TKDInterpolator(TTree*, UInt_t, const Char_t)", Form("Variable number exceed maximum dimension %d. Results are unpredictable.", 6/*kDimMax*/));
+       fNDim = vars->GetEntriesFast(); fNDimm = 2*fNDim;
+       if(fNDim > 6/*kDimMax*/) Warning("TKDInterpolator(TTree*, const Char_t, const Char_t, UInt_t)", Form("Variable number exceed maximum dimension %d. Results are unpredictable.", 6/*kDimMax*/));
        fBucketSize = bsize;
 
        fBucketSize = bsize;
 
-       printf("Allocating %d points in %d dimensions.\n", fNpoints, fNDim);
-       Float_t *mem = new Float_t[fNDim*fNpoints];
-       fData = new Float_t*[fNDim];
-       for(int idim=0; idim<fNDim; idim++) fData[idim] = &mem[idim*fNpoints];
-       kDataOwner = kTRUE;
-       
+       Int_t np;
        Double_t *v;
        for(int idim=0; idim<fNDim; idim++){
        Double_t *v;
        for(int idim=0; idim<fNDim; idim++){
-               if(!(t->Draw(((TObjString*)(*vars)[idim])->GetName(), cut, "goff"))){
-                       Warning("TKDInterpolator(TTree*, UInt_t, const Char_t)", Form("Can not access data for %s", ((TObjString*)(*vars)[idim])->GetName() ));
+               if(!(np = t->Draw(((TObjString*)(*vars)[idim])->GetName(), cut, "goff", nentries, firstentry))){
+                       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());
                        continue;
                }
                        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));
+                       fData = new Float_t*[fNDim];
+                       for(int idim=0; idim<fNDim; idim++) fData[idim] = new Float_t[fNpoints];
+                       kDataOwner = kTRUE;
+               }
                v = t->GetV1();
                for(int ip=0; ip<fNpoints; ip++) fData[idim][ip] = (Float_t)v[ip];
        }
                v = t->GetV1();
                for(int ip=0; ip<fNpoints; ip++) fData[idim][ip] = (Float_t)v[ip];
        }
@@ -94,9 +121,15 @@ TKDInterpolator::TKDInterpolator(TTree *t, const Char_t *var, const Char_t *cut,
 //_________________________________________________________________
 TKDInterpolator::~TKDInterpolator()
 {
 //_________________________________________________________________
 TKDInterpolator::~TKDInterpolator()
 {
+       if(fCov){
+               delete [] fPar;
+               delete [] fCov;
+               delete [] fPDFstatus;
+       }
+       
        if(fFitter) delete fFitter;
        if(fKDhelper) delete fKDhelper;
        if(fFitter) delete fFitter;
        if(fKDhelper) delete fKDhelper;
-       if(fTmpPoint) delete [] fTmpPoint;
+       if(fBuffer) delete [] fBuffer;
        
        if(fRefPoints){
                for(int idim=0; idim<fNDim; idim++) delete [] fRefPoints[idim] ;
        
        if(fRefPoints){
                for(int idim=0; idim<fNDim; idim++) delete [] fRefPoints[idim] ;
@@ -108,8 +141,13 @@ TKDInterpolator::~TKDInterpolator()
 //_________________________________________________________________
 void TKDInterpolator::Build()
 {
 //_________________________________________________________________
 void TKDInterpolator::Build()
 {
+// Fill interpolator's data array i.e.
+//  - estimation points 
+//  - corresponding PDF values
+
        if(!fBoundaries) MakeBoundaries();
        if(!fBoundaries) MakeBoundaries();
-       
+       fLambda = 1 + fNDim + fNDim*(fNDim+1)/2;
+
        // allocate memory for data
        fRefValues = new Float_t[fNTNodes];
        fRefPoints = new Float_t*[fNDim];
        // allocate memory for data
        fRefValues = new Float_t[fNTNodes];
        fRefPoints = new Float_t*[fNDim];
@@ -149,64 +187,229 @@ void TKDInterpolator::Build()
        }
 }
 
        }
 }
 
-//_________________________________________________________________
-Double_t TKDInterpolator::Eval(Float_t *point)
+//__________________________________________________________________
+void TKDInterpolator::GetStatus()
 {
 {
+       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]);
+       }
 
 
-       // calculate number of parameters in the parabolic expresion
-       Int_t kNN = 1 + fNDim + fNDim*(fNDim+1)/2;
-
-       // prepare workers
-       if(!fTmpPoint) fTmpPoint = new Double_t[fNDim];
-       if(!fKDhelper) fKDhelper = new TKDTreeIF(GetNTerminalNodes(), fNDim, kNN, fRefPoints);
-       if(!fFitter){
-               // generate formula for nD
-               TString 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);
-               }
-               fFitter = new TLinearFitter(kNN, formula.Data());
+       printf("cov 0x%x\n", fCov);           //[fNTNodes] cov matrix array for nodes
+       printf("pars 0x%x\n", fPar);           //[fNTNodes] parameters array for nodes
+       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));
+               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)
+{
+// Evaluate PDF for "point". The result is returned in "result" and error in "error". The function returns the chi2 of the fit.
+//
+// Observations:
+//
+// 1. The default method used for interpolation is kCOG.
+// 2. The initial number of neighbors used for the estimation is set to Int(alpha*fLambda) (alpha = 1.5)
+                       
+       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);
+
+       // Allocate memory
+       if(!fBuffer) fBuffer = new Double_t[2*fLambda];
+       if(!fKDhelper) fKDhelper = new TKDTreeIF(fNTNodes, fNDim, 30, fRefPoints);
+       if(!fFitter) SetIntInterpolation(kFALSE);
        
        
-       Int_t kNN_old = 0;
-       Int_t *index;
-       Float_t dist;
-       fFitter->ClearPoints();
+       // generate parabolic for nD
+       //Float_t alpha = Float_t(2*lambda + 1) / fNTNodes; // the bandwidth or smoothing parameter
+       //Int_t npoints = Int_t(alpha * fNTNodes);
+       //printf("Params : %d NPoints %d\n", lambda, npoints);
+       // prepare workers
+
+       Int_t *index,  // indexes of NN 
+             ipar,    // local looping variable
+                               npoints = Int_t(1.5*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{
        do{
-               if(!fKDhelper->FindNearestNeighbors(point, kNN, index, dist)){
-                       Error("Eval()", Form("Failed retriving %d neighbours for point:", kNN));
+               // find nearest neighbors
+               for(int idim=0; idim<fNDim; idim++) pointF[idim] = (Float_t)point[idim];
+               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 idim=0; idim<fNDim; idim++) printf("%f ", point[idim]);
                        printf("\n");
                        return -1;
                }
-               for(int in=kNN_old; in<kNN; in++){
-                       for(int idim=0; idim<fNDim; idim++) fTmpPoint[idim] = fRefPoints[idim][index[in]];
-                       fFitter->AddPoint(fTmpPoint, TMath::Log(fRefValues[index[in]]), 1.);
+               // add points to fitter
+               fFitter->ClearPoints();
+               for(int in=0; in<npoints; in++){
+                       if(fStatus&1){ // INT
+                               for(int idim=0; idim<fNDim; idim++) pointF[idim] = fRefPoints[idim][index[in]];
+                               Float_t *bounds = GetBoundary(FindNode(pointF));
+                               
+                               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;
+                               }
+                       } else { // COG
+                               for(int idim=0; idim<fNDim; idim++) fBuffer[idim] = fRefPoints[idim][index[in]];
+                       }
+
+                       // calculate tri-cubic weighting function
+                       if(fStatus&4){
+                               d = dist[in]/ dist[npoints];
+                               w0 = (1. - d*d*d); w = w0*w0*w0;
+                       } else w = 1.;
+                        
+                       //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);
                }
                }
-               kNN_old = kNN;
-               kNN += 4;
+               npoints += 4;
        } while(fFitter->Eval());
 
        } while(fFitter->Eval());
 
-       // calculate evaluation
-       TVectorD par(kNN);
+       // retrive fitter results
+       TMatrixD cov(fLambda, fLambda);
+       TVectorD par(fLambda);
+       fFitter->GetCovarianceMatrix(cov);
        fFitter->GetParameters(par);
        fFitter->GetParameters(par);
-       Double_t result = par[0];
-       Int_t ipar = 0;
+       Double_t chi2 = fFitter->GetChisquare()/(npoints - 4 - fLambda);
+
+       // store results
+       if(fStatus&2 && fStatus&1){
+               fPar[node] = par;
+               fCov[node] = cov;
+               fPDFstatus[node] = 1;
+       }
+               
+       // Build df/dpi|x values
+       Double_t *fdfdp = &fBuffer[fLambda];
+       ipar = 0;
+       fdfdp[ipar++] = 1.;
        for(int idim=0; idim<fNDim; idim++){
        for(int idim=0; idim<fNDim; idim++){
-               result += par[++ipar]*point[idim];
-               for(int jdim=idim; jdim<fNDim; jdim++) result += par[++ipar]*point[idim]*point[jdim];
+               fdfdp[ipar++] = point[idim];
+               for(int jdim=idim; jdim<fNDim; jdim++) fdfdp[ipar++] = point[idim]*point[jdim];
        }
        }
-       return TMath::Exp(result);
+
+       // calculate estimation
+       result =0.; error = 0.;
+       for(int i=0; i<fLambda; i++){
+               result += fdfdp[i]*par(i);
+               for(int j=0; j<fLambda; j++) error += fdfdp[i]*fdfdp[j]*cov(i,j);
+       }       
+       error = TMath::Sqrt(error);
+
+       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(Int_t depth, Int_t ax1, Int_t ax2)
+void TKDInterpolator::DrawNodes(UInt_t ax1, UInt_t ax2, Int_t depth)
 {
 // Draw nodes structure projected on plane "ax1:ax2". The parameter
 // "depth" specifies the bucket size per node. If depth == -1 draw only
 // terminal nodes and evaluation points (default -1 i.e. bucket size per node equal bucket size specified by the user)
 {
 // Draw nodes structure projected on plane "ax1:ax2". The parameter
 // "depth" specifies the bucket size per node. If depth == -1 draw only
 // terminal nodes and evaluation points (default -1 i.e. bucket size per node equal bucket size specified by the user)
+//
+// Observation:
+// This function creates the nodes (TBox) array for the specified depth
+// but don't delete it. Abusing this function may cause memory leaks !
+
 
        if(!fBoundaries) MakeBoundaries();
 
 
        if(!fBoundaries) MakeBoundaries();
 
@@ -221,12 +424,14 @@ void TKDInterpolator::DrawNodes(Int_t depth, Int_t ax1, Int_t ax2)
 
        //printf("depth %d nodes %d\n", depth, nnodes);
        
 
        //printf("depth %d nodes %d\n", depth, nnodes);
        
-       //TH2 *h2 = new TH2S("hframe", "", 100, fRange[2*ax1], fRange[2*ax1+1], 100, fRange[2*ax2], fRange[2*ax2+1]);
-       TH2 *h2 = new TH2S("hframe", "", 100, 0., 1., 100, 0., 1.);
+       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]);
+       h2->GetXaxis()->SetTitle(Form("x_{%d}", ax1));
+       h2->GetYaxis()->SetTitle(Form("x_{%d}", ax2));
        h2->Draw();
        
        const Float_t border = 0.;//1.E-4;
        h2->Draw();
        
        const Float_t border = 0.;//1.E-4;
-       TBox **node_array = new TBox*[nnodes], *node;
+       TBox *node_array = new TBox[nnodes], *node;
        Float_t *bounds = 0x0;
        nnodes = 0;
        for(int inode = 0; inode <= 2*fNnodes; inode++){
        Float_t *bounds = 0x0;
        nnodes = 0;
        for(int inode = 0; inode <= 2*fNnodes; inode++){
@@ -234,65 +439,150 @@ void TKDInterpolator::DrawNodes(Int_t depth, Int_t ax1, Int_t ax2)
                        if(!IsTerminal(inode)) continue;
                } else if((inode+1) >> depth != 1) continue;
 
                        if(!IsTerminal(inode)) continue;
                } else if((inode+1) >> depth != 1) continue;
 
-               node = node_array[nnodes++];
+               node = &node_array[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.));
                bounds = GetBoundary(inode);
                bounds = GetBoundary(inode);
-               node = new TBox(bounds[2*ax1]+border, bounds[2*ax2]+border, bounds[2*ax1+1]-border, bounds[2*ax2+1]-border);
-               node->SetFillStyle(0);  
-               node->SetFillColor(51+inode);
-               node->Draw();
+               node->DrawBox(bounds[2*ax1]+border, bounds[2*ax2]+border, bounds[2*ax1+1]-border, bounds[2*ax2+1]-border);
        }
        if(depth != -1) return;
 
        // Draw reference points
        TGraph *ref = new TGraph(GetNTerminalNodes());
        }
        if(depth != -1) return;
 
        // Draw reference points
        TGraph *ref = new TGraph(GetNTerminalNodes());
-       ref->SetMarkerStyle(2);
+       ref->SetMarkerStyle(3);
+       ref->SetMarkerSize(.7);
        ref->SetMarkerColor(2);
        ref->SetMarkerColor(2);
-       Float_t val, error;
        for(int inode = 0; inode < GetNTerminalNodes(); inode++) ref->SetPoint(inode, fRefPoints[ax1][inode], fRefPoints[ax2][inode]);
        ref->Draw("p");
        return;
 }
 
 //_________________________________________________________________
        for(int inode = 0; inode < GetNTerminalNodes(); inode++) ref->SetPoint(inode, fRefPoints[ax1][inode], fRefPoints[ax2][inode]);
        ref->Draw("p");
        return;
 }
 
 //_________________________________________________________________
-void TKDInterpolator::DrawNode(Int_t tnode, Int_t ax1, Int_t ax2)
+void TKDInterpolator::DrawNode(Int_t tnode, UInt_t ax1, UInt_t ax2)
 {
 // Draw node "node" and the data points within.
 {
 // Draw node "node" and the data points within.
+//
+// Observation:
+// This function creates some graphical objects
+// but don't delete it. Abusing this function may cause memory leaks !
 
        if(tnode < 0 || tnode >= GetNTerminalNodes()){
                Warning("DrawNode()", Form("Terminal node %d outside defined range.", tnode));
                return;
        }
 
 
        if(tnode < 0 || tnode >= GetNTerminalNodes()){
                Warning("DrawNode()", Form("Terminal node %d outside defined range.", tnode));
                return;
        }
 
-       //TH2 *h2 = new TH2S("hframe", "", 1, fRange[2*ax1], fRange[2*ax1+1], 1, fRange[2*ax2], fRange[2*ax2+1]);
-       TH2 *h2 = new TH2S("hframe", "", 1, 0., 1., 1, 0., 1.);
-       h2->Draw();
-
        Int_t inode = tnode;
        tnode += fNnodes;
        // select zone of interest in the indexes array
        Int_t *index = GetPointsIndexes(tnode);
        Int_t nPoints = (tnode == 2*fNnodes) ? fNpoints%fBucketSize : fBucketSize;
 
        Int_t inode = tnode;
        tnode += fNnodes;
        // select zone of interest in the indexes array
        Int_t *index = GetPointsIndexes(tnode);
        Int_t nPoints = (tnode == 2*fNnodes) ? fNpoints%fBucketSize : fBucketSize;
 
-       printf("true index %d points %d\n", tnode, nPoints);
-
        // draw data points
        TGraph *g = new TGraph(nPoints);
        // draw data points
        TGraph *g = new TGraph(nPoints);
-       g->SetMarkerStyle(3);
-       g->SetMarkerSize(.8);
+       g->SetMarkerStyle(7);
        for(int ip = 0; ip<nPoints; ip++) g->SetPoint(ip, fData[ax1][index[ip]], fData[ax2][index[ip]]);
        for(int ip = 0; ip<nPoints; ip++) g->SetPoint(ip, fData[ax1][index[ip]], fData[ax2][index[ip]]);
-       g->Draw("p");
 
        // draw estimation point
 
        // draw estimation point
-       TMarker *m=new TMarker(fRefPoints[ax1][inode], fRefPoints[ax2][inode], 2);
+       TMarker *m=new TMarker(fRefPoints[ax1][inode], fRefPoints[ax2][inode], 20);
        m->SetMarkerColor(2);
        m->SetMarkerColor(2);
-       m->Draw();
+       m->SetMarkerSize(1.7);
        
        // draw node contour
        Float_t *bounds = GetBoundary(tnode);
        TBox *n = new TBox(bounds[2*ax1], bounds[2*ax2], bounds[2*ax1+1], bounds[2*ax2+1]);
        n->SetFillStyle(0);
        
        // draw node contour
        Float_t *bounds = GetBoundary(tnode);
        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();
        
        return;
 }
 
        n->Draw();
        
        return;
 }
 
+
+//__________________________________________________________________
+void TKDInterpolator::SetIntInterpolation(const Bool_t on)
+{
+// Set interpolation bit to "on" and build/delete memory
+       
+       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)
+{
+// Set store bit to "on" and build/delete memory
+       
+       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;
+               }
+       }
+}
+
+//_________________________________________________________________
+void TKDInterpolator::SetUseWeights(const Bool_t 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.;
+}
+