1 #include "TKDInterpolatorBase.h"
2 #include "TKDNodeInfo.h"
5 #include "TClonesArray.h"
6 #include "TLinearFitter.h"
10 #include "TObjString.h"
18 ClassImp(TKDInterpolatorBase)
20 /////////////////////////////////////////////////////////////////////
21 // Memory setup of protected data members
22 // fRefPoints : evaluation point of PDF for each terminal node of underlying KD Tree.
23 // | 1st terminal node (fNDim point coordinates) | 2nd terminal node (fNDim point coordinates) | ...
25 // fRefValues : evaluation value/error of PDF for each terminal node of underlying KD Tree.
26 // | 1st terminal node (value) | 2nd terminal node (value) | ... | 1st terminal node (error) | 2nd terminal node (error) | ...
28 // status = |0|0|0|0|0|1(tri-cubic weights)|1(STORE)|1 INT(0 COG )|
29 /////////////////////////////////////////////////////////////////////
32 //_________________________________________________________________
33 TKDInterpolatorBase::TKDInterpolatorBase(Int_t dim) :
38 ,fLambda(1 + dim + (dim*(dim+1)>>1))
46 // Default constructor. To be used with care since in this case building
47 // of data structure is completly left to the user responsability.
50 //_________________________________________________________________
51 void TKDInterpolatorBase::Build(Int_t n)
53 // allocate memory for data
55 if(fTNodes) delete fTNodes;
57 fTNodes = new TClonesArray("TKDNodeInfo", fNTNodes);
58 for(int in=0; in<fNTNodes; in++) new ((*fTNodes)[in]) TKDNodeInfo(fNSize);
61 //_________________________________________________________________
62 TKDInterpolatorBase::~TKDInterpolatorBase()
64 if(fFitter) delete fFitter;
65 if(fKDhelper) delete fKDhelper;
66 if(fBuffer) delete [] fBuffer;
69 for(int idim=0; idim<fNSize; idim++) delete [] fRefPoints[idim] ;
72 if(fTNodes) delete fTNodes;
76 //__________________________________________________________________
77 Bool_t TKDInterpolatorBase::GetCOGPoint(Int_t inode, Float_t *&coord, Float_t &val, Float_t &err) const
79 if(inode < 0 || inode > fNTNodes) return kFALSE;
81 TKDNodeInfo *node = (TKDNodeInfo*)(*fTNodes)[inode];
82 coord = &(node->Data()[0]);
88 //_________________________________________________________________
89 TKDNodeInfo* TKDInterpolatorBase::GetNodeInfo(Int_t inode) const
91 if(!fTNodes || inode >= fNTNodes) return 0x0;
92 return (TKDNodeInfo*)(*fTNodes)[inode];
96 //__________________________________________________________________
97 void TKDInterpolatorBase::GetStatus()
99 // Prints the status of the interpolator
101 printf("Interpolator Status :\n");
102 printf(" Dim : %d [%d]\n", fNSize, fLambda);
103 printf(" Method : %s\n", fStatus&1 ? "INT" : "COG");
104 printf(" Store : %s\n", fStatus&2 ? "YES" : "NO");
105 printf(" Weights: %s\n", fStatus&4 ? "YES" : "NO");
107 printf("fNTNodes %d\n", fNTNodes); //Number of evaluation data points
108 for(int i=0; i<fNTNodes; i++){
109 TKDNodeInfo *node = (TKDNodeInfo*)(*fTNodes)[i];
110 printf("%d ", i); node->Print();
114 //_________________________________________________________________
115 Double_t TKDInterpolatorBase::Eval(const Double_t *point, Double_t &result, Double_t &error, Bool_t force)
117 // Evaluate PDF for "point". The result is returned in "result" and error in "error". The function returns the chi2 of the fit.
121 // 1. The default method used for interpolation is kCOG.
122 // 2. The initial number of neighbors used for the estimation is set to Int(alpha*fLambda) (alpha = 1.5)
124 Float_t pointF[50]; // local Float_t conversion for "point"
125 for(int idim=0; idim<fNSize; idim++) pointF[idim] = (Float_t)point[idim];
126 Int_t nodeIndex = GetNodeIndex(pointF);
132 TKDNodeInfo *node = (TKDNodeInfo*)(*fTNodes)[nodeIndex];
133 if((fStatus&1) && node->Cov() && !force) return node->CookPDF(point, result, error);
136 if(!fBuffer) fBuffer = new Double_t[2*fLambda];
138 fRefPoints = new Float_t*[fNSize];
139 for(int id=0; id<fNSize; id++){
140 fRefPoints[id] = new Float_t[fNTNodes];
141 for(int in=0; in<fNTNodes; in++) fRefPoints[id][in] = ((TKDNodeInfo*)(*fTNodes)[in])->Data()[id];
143 fKDhelper = new TKDTreeIF(fNTNodes, fNSize, 30, fRefPoints);
144 fKDhelper->MakeBoundaries();
146 if(!fFitter) fFitter = new TLinearFitter(fLambda, Form("hyp%d", fLambda-1));
148 // generate parabolic for nD
149 //Float_t alpha = Float_t(2*lambda + 1) / fNTNodes; // the bandwidth or smoothing parameter
150 //Int_t npoints = Int_t(alpha * fNTNodes);
151 //printf("Params : %d NPoints %d\n", lambda, npoints);
154 Int_t *index, // indexes of NN
155 ipar, // local looping variable
156 npoints = Int_t((1.+fAlpha)*fLambda); // number of data points used for interpolation
157 Float_t *dist, // distances of NN
158 d, // NN normalized distance
160 w; // tri-cubic weight function
163 // find nearest neighbors
164 for(int idim=0; idim<fNSize; idim++) pointF[idim] = (Float_t)point[idim];
165 if(!fKDhelper->FindNearestNeighbors(pointF, npoints+1, index, dist)){
166 Error("Eval()", Form("Failed retriving %d neighbours for point:", npoints));
167 for(int idim=0; idim<fNSize; idim++) printf("%f ", point[idim]);
171 // add points to fitter
172 fFitter->ClearPoints();
173 TKDNodeInfo *tnode = 0x0;
174 for(int in=0; in<npoints; in++){
175 tnode = (TKDNodeInfo*)(*fTNodes)[index[in]];
177 if(fStatus&1){ // INT
178 Float_t *bounds = &(tnode->Data()[fNSize]);
180 for(int idim=0; idim<fNSize; idim++){
181 fBuffer[ipar++] = .5*(bounds[2*idim] + bounds[2*idim+1]);
182 fBuffer[ipar++] = (bounds[2*idim]*bounds[2*idim] + bounds[2*idim] * bounds[2*idim+1] + bounds[2*idim+1] * bounds[2*idim+1])/3.;
183 for(int jdim=idim+1; jdim<fNSize; jdim++) fBuffer[ipar++] = (bounds[2*idim] + bounds[2*idim+1]) * (bounds[2*jdim] + bounds[2*jdim+1]) * .25;
186 Float_t *p = &(tnode->Data()[0]);
188 for(int idim=0; idim<fNSize; idim++){
189 fBuffer[ipar++] = p[idim];
190 for(int jdim=idim; jdim<fNSize; jdim++) fBuffer[ipar++] = p[idim]*p[jdim];
194 // calculate tri-cubic weighting function
196 d = dist[in]/ dist[npoints];
197 w0 = (1. - d*d*d); w = w0*w0*w0;
201 // for(int idim=0; idim<fLambda-1; idim++) printf("%f ", fBuffer[idim]);
202 // printf("] v[%f +- %f] (%f, %f)\n", tnode->Val()[0], tnode->Val()[1]/w, tnode->Val()[1], w);
203 fFitter->AddPoint(fBuffer, tnode->Val()[0], tnode->Val()[1]/w);
206 } while(fFitter->Eval());
208 // retrive fitter results
209 TMatrixD cov(fLambda, fLambda);
210 TVectorD par(fLambda);
211 fFitter->GetCovarianceMatrix(cov);
212 fFitter->GetParameters(par);
213 Double_t chi2 = fFitter->GetChisquare()/(npoints - 4 - fLambda);
216 if(fStatus&2 && fStatus&1) node->Store(par, cov);
218 // Build df/dpi|x values
219 Double_t *fdfdp = &fBuffer[fLambda];
222 for(int idim=0; idim<fNSize; idim++){
223 fdfdp[ipar++] = point[idim];
224 for(int jdim=idim; jdim<fNSize; jdim++) fdfdp[ipar++] = point[idim]*point[jdim];
227 // calculate estimation
228 result =0.; error = 0.;
229 for(int i=0; i<fLambda; i++){
230 result += fdfdp[i]*par(i);
231 for(int j=0; j<fLambda; j++) error += fdfdp[i]*fdfdp[j]*cov(i,j);
233 error = TMath::Sqrt(error);
238 //_________________________________________________________________
239 void TKDInterpolatorBase::DrawBins(UInt_t ax1, UInt_t ax2, Float_t ax1min, Float_t ax1max, Float_t ax2min, Float_t ax2max)
241 // Draw nodes structure projected on plane "ax1:ax2". The parameter
242 // "depth" specifies the bucket size per node. If depth == -1 draw only
243 // terminal nodes and evaluation points (default -1 i.e. bucket size per node equal bucket size specified by the user)
246 // This function creates the nodes (TBox) array for the specified depth
247 // but don't delete it. Abusing this function may cause memory leaks !
251 TH2 *h2 = new TH2S("hNodes", "", 100, ax1min, ax1max, 100, ax2min, ax2max);
252 h2->GetXaxis()->SetTitle(Form("x_{%d}", ax1));
253 h2->GetYaxis()->SetTitle(Form("x_{%d}", ax2));
256 const Float_t kBorder = 0.;//1.E-4;
257 TBox *boxArray = new TBox[fNTNodes], *box;
258 Float_t *bounds = 0x0;
259 for(int inode = 0; inode < fNTNodes; inode++){
260 box = &boxArray[inode];
261 box->SetFillStyle(3002);
262 box->SetFillColor(50+inode/*Int_t(gRandom->Uniform()*50.)*/);
264 bounds = &(((TKDNodeInfo*)(*fTNodes)[inode])->Data()[fNSize]);
265 box->DrawBox(bounds[2*ax1]+kBorder, bounds[2*ax2]+kBorder, bounds[2*ax1+1]-kBorder, bounds[2*ax2+1]-kBorder);
268 // Draw reference points
269 TGraph *ref = new TGraph(fNTNodes);
270 ref->SetMarkerStyle(3);
271 ref->SetMarkerSize(.7);
272 ref->SetMarkerColor(2);
273 for(int inode = 0; inode < fNTNodes; inode++){
274 TKDNodeInfo *node = (TKDNodeInfo*)(*fTNodes)[inode];
275 ref->SetPoint(inode, node->Data()[ax1], node->Data()[ax2]);
281 //__________________________________________________________________
282 void TKDInterpolatorBase::SetInterpolationMethod(Bool_t on)
284 // Set interpolation bit to "on".
286 if(on) fStatus += fStatus&1 ? 0 : 1;
287 else fStatus += fStatus&1 ? -1 : 0;
291 //_________________________________________________________________
292 void TKDInterpolatorBase::SetStore(Bool_t on)
294 // Set store bit to "on"
296 if(on) fStatus += fStatus&2 ? 0 : 2;
297 else fStatus += fStatus&2 ? -2 : 0;
300 //_________________________________________________________________
301 void TKDInterpolatorBase::SetWeights(Bool_t on)
303 // Set weights bit to "on"
305 if(on) fStatus += fStatus&4 ? 0 : 4;
306 else fStatus += fStatus&4 ? -4 : 0;