1 #include "TKDInterpolatorBase.h"
2 #include "TKDNodeInfo.h"
6 #include "TClonesArray.h"
7 #include "TLinearFitter.h"
10 #include "TObjArray.h"
11 #include "TObjString.h"
19 ClassImp(TKDInterpolatorBase)
21 /////////////////////////////////////////////////////////////////////
22 // Memory setup of protected data members
23 // fRefPoints : evaluation point of PDF for each terminal node of underlying KD Tree.
24 // | 1st terminal node (fNDim point coordinates) | 2nd terminal node (fNDim point coordinates) | ...
26 // fRefValues : evaluation value/error of PDF for each terminal node of underlying KD Tree.
27 // | 1st terminal node (value) | 2nd terminal node (value) | ... | 1st terminal node (error) | 2nd terminal node (error) | ...
29 // status = |0|0|0|0|0|1(tri-cubic weights)|1(STORE)|1 INT(0 COG )|
30 /////////////////////////////////////////////////////////////////////
33 //_________________________________________________________________
34 TKDInterpolatorBase::TKDInterpolatorBase(Int_t dim) :
39 ,fLambda(1 + dim + (dim*(dim+1)>>1))
47 // Default constructor. To be used with care since in this case building
48 // of data structure is completly left to the user responsability.
51 //_________________________________________________________________
52 void TKDInterpolatorBase::Build(Int_t n)
54 // allocate memory for data
56 if(fTNodes) delete fTNodes;
59 if(Int_t((1.+fAlpha)*fLambda) > fNTNodes){
60 Warning("TKDInterpolatorBase::Build()", Form("Minimum number of points [%d] needed for interpolation exceeds number of evaluation points [%d]. Please increase granularity.", Int_t((1.+fAlpha)*fLambda), fNTNodes));
62 fTNodes = new TClonesArray("TKDNodeInfo", fNTNodes);
63 for(int in=0; in<fNTNodes; in++) new ((*fTNodes)[in]) TKDNodeInfo(fNSize);
66 //_________________________________________________________________
67 TKDInterpolatorBase::~TKDInterpolatorBase()
69 if(fFitter) delete fFitter;
70 if(fKDhelper) delete fKDhelper;
71 if(fBuffer) delete [] fBuffer;
74 for(int idim=0; idim<fNSize; idim++) delete [] fRefPoints[idim] ;
77 if(fTNodes) delete fTNodes;
81 //__________________________________________________________________
82 Bool_t TKDInterpolatorBase::GetCOGPoint(Int_t inode, Float_t *&coord, Float_t &val, Float_t &err) const
84 if(inode < 0 || inode > fNTNodes) return kFALSE;
86 TKDNodeInfo *node = (TKDNodeInfo*)(*fTNodes)[inode];
87 coord = &(node->Data()[0]);
93 //_________________________________________________________________
94 TKDNodeInfo* TKDInterpolatorBase::GetNodeInfo(Int_t inode) const
96 if(!fTNodes || inode >= fNTNodes) return 0x0;
97 return (TKDNodeInfo*)(*fTNodes)[inode];
101 //__________________________________________________________________
102 void TKDInterpolatorBase::GetStatus()
104 // Prints the status of the interpolator
106 printf("Interpolator Status :\n");
107 printf(" Dim : %d [%d]\n", fNSize, fLambda);
108 printf(" Method : %s\n", fStatus&1 ? "INT" : "COG");
109 printf(" Store : %s\n", fStatus&2 ? "YES" : "NO");
110 printf(" Weights: %s\n", fStatus&4 ? "YES" : "NO");
112 printf("fNTNodes %d\n", fNTNodes); //Number of evaluation data points
113 for(int i=0; i<fNTNodes; i++){
114 TKDNodeInfo *node = (TKDNodeInfo*)(*fTNodes)[i];
115 printf("%d ", i); node->Print();
119 //_________________________________________________________________
120 Double_t TKDInterpolatorBase::Eval(const Double_t *point, Double_t &result, Double_t &error, Bool_t force)
122 // Evaluate PDF for "point". The result is returned in "result" and error in "error". The function returns the chi2 of the fit.
126 // 1. The default method used for interpolation is kCOG.
127 // 2. The initial number of neighbors used for the estimation is set to Int(alpha*fLambda) (alpha = 1.5)
129 Float_t pointF[50]; // local Float_t conversion for "point"
130 for(int idim=0; idim<fNSize; idim++) pointF[idim] = (Float_t)point[idim];
131 Int_t nodeIndex = GetNodeIndex(pointF);
133 Error("TKDInterpolatorBase::Eval()", "Can not retrive node for data point.");
138 TKDNodeInfo *node = (TKDNodeInfo*)(*fTNodes)[nodeIndex];
139 if((fStatus&1) && node->Cov() && !force) return node->CookPDF(point, result, error);
142 if(!fBuffer) fBuffer = new Double_t[2*fLambda];
144 fRefPoints = new Float_t*[fNSize];
145 for(int id=0; id<fNSize; id++){
146 fRefPoints[id] = new Float_t[fNTNodes];
147 for(int in=0; in<fNTNodes; in++) fRefPoints[id][in] = ((TKDNodeInfo*)(*fTNodes)[in])->Data()[id];
149 fKDhelper = new TKDTreeIF(fNTNodes, fNSize, kNhelper, fRefPoints);
151 fKDhelper->MakeBoundariesExact();
153 if(!fFitter) fFitter = new TLinearFitter(fLambda, Form("hyp%d", fLambda-1));
155 // generate parabolic for nD
156 //Float_t alpha = Float_t(2*lambda + 1) / fNTNodes; // the bandwidth or smoothing parameter
157 //Int_t npoints = Int_t(alpha * fNTNodes);
158 //printf("Params : %d NPoints %d\n", lambda, npoints);
161 Int_t ipar, // local looping variable
162 npoints_new = Int_t((1.+fAlpha)*fLambda),
163 npoints(0); // number of data points used for interpolation
164 Int_t *index = new Int_t[2*npoints_new]; // indexes of NN
165 Float_t *dist = new Float_t[2*npoints_new], // distances of NN
166 d, // NN normalized distance
168 w; // tri-cubic weight function
170 Bool_t kDOWN = kFALSE;
172 if(npoints == npoints_new){
173 Error("TKDInterpolatorBase::Eval()", Form("Interpolation failed and number of interpolation points (%d) Can not be increased further.", npoints));
177 } else npoints = npoints_new;
178 if(npoints > fNTNodes){
179 Warning("TKDInterpolatorBase::Eval()", Form("The number of interpolation points requested (%d) exceeds number of PDF values (%d). Downscale.", npoints, fNTNodes));
184 // find nearest neighbors
185 for(int idim=0; idim<fNSize; idim++) pointF[idim] = (Float_t)point[idim];
186 fKDhelper->FindNearestNeighbors(pointF, npoints+1, index, dist);
188 // add points to fitter
189 fFitter->ClearPoints();
190 TKDNodeInfo *tnode = 0x0;
191 for(int in=0; in<npoints; in++){
192 tnode = (TKDNodeInfo*)(*fTNodes)[index[in]];
194 if(fStatus&1){ // INT
195 Float_t *bounds = &(tnode->Data()[fNSize]);
197 for(int idim=0; idim<fNSize; idim++){
198 fBuffer[ipar++] = .5*(bounds[2*idim] + bounds[2*idim+1]);
199 fBuffer[ipar++] = (bounds[2*idim]*bounds[2*idim] + bounds[2*idim] * bounds[2*idim+1] + bounds[2*idim+1] * bounds[2*idim+1])/3.;
200 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;
203 Float_t *p = &(tnode->Data()[0]);
205 for(int idim=0; idim<fNSize; idim++){
206 fBuffer[ipar++] = p[idim];
207 for(int jdim=idim; jdim<fNSize; jdim++) fBuffer[ipar++] = p[idim]*p[jdim];
211 // calculate tri-cubic weighting function
213 d = dist[in]/ dist[npoints];
214 w0 = (1. - d*d*d); w = w0*w0*w0;
217 // printf("%2d x[", index[in]);
218 // for(int idim=0; idim<fLambda-1; idim++) printf("%f ", fBuffer[idim]);
219 // printf("] v[%f +- %f] (%f, %f)\n", tnode->Val()[0], tnode->Val()[1]/w, tnode->Val()[1], w);
220 fFitter->AddPoint(fBuffer, tnode->Val()[0], tnode->Val()[1]/w);
222 npoints_new = npoints+ (kDOWN ? 0 : kdN);
223 } while(fFitter->Eval());
227 // retrive fitter results
228 TMatrixD cov(fLambda, fLambda);
229 TVectorD par(fLambda);
230 fFitter->GetCovarianceMatrix(cov);
231 fFitter->GetParameters(par);
232 Double_t chi2 = fFitter->GetChisquare()/(npoints - 4 - fLambda);
235 if(fStatus&2 && fStatus&1) node->Store(par, cov);
237 // Build df/dpi|x values
238 Double_t *fdfdp = &fBuffer[fLambda];
241 for(int idim=0; idim<fNSize; idim++){
242 fdfdp[ipar++] = point[idim];
243 for(int jdim=idim; jdim<fNSize; jdim++) fdfdp[ipar++] = point[idim]*point[jdim];
246 // calculate estimation
247 result =0.; error = 0.;
248 for(int i=0; i<fLambda; i++){
249 result += fdfdp[i]*par(i);
250 for(int j=0; j<fLambda; j++) error += fdfdp[i]*fdfdp[j]*cov(i,j);
252 error = TMath::Sqrt(error);
257 //_________________________________________________________________
258 void TKDInterpolatorBase::DrawBins(UInt_t ax1, UInt_t ax2, Float_t ax1min, Float_t ax1max, Float_t ax2min, Float_t ax2max)
260 // Draw nodes structure projected on plane "ax1:ax2". The parameter
261 // "depth" specifies the bucket size per node. If depth == -1 draw only
262 // terminal nodes and evaluation points (default -1 i.e. bucket size per node equal bucket size specified by the user)
265 // This function creates the nodes (TBox) array for the specified depth
266 // but don't delete it. Abusing this function may cause memory leaks !
270 TH2 *h2 = new TH2S("hNodes", "", 100, ax1min, ax1max, 100, ax2min, ax2max);
271 h2->GetXaxis()->SetTitle(Form("x_{%d}", ax1));
272 h2->GetYaxis()->SetTitle(Form("x_{%d}", ax2));
275 const Float_t kBorder = 0.;//1.E-4;
276 TBox *boxArray = new TBox[fNTNodes], *box;
277 Float_t *bounds = 0x0;
278 for(int inode = 0; inode < fNTNodes; inode++){
279 box = &boxArray[inode];
280 box->SetFillStyle(3002);
281 box->SetFillColor(50+Int_t(gRandom->Uniform()*50.));
283 bounds = &(((TKDNodeInfo*)(*fTNodes)[inode])->Data()[fNSize]);
284 box->DrawBox(bounds[2*ax1]+kBorder, bounds[2*ax2]+kBorder, bounds[2*ax1+1]-kBorder, bounds[2*ax2+1]-kBorder);
287 // Draw reference points
288 TGraph *ref = new TGraph(fNTNodes);
289 ref->SetMarkerStyle(3);
290 ref->SetMarkerSize(.7);
291 ref->SetMarkerColor(2);
292 for(int inode = 0; inode < fNTNodes; inode++){
293 TKDNodeInfo *node = (TKDNodeInfo*)(*fTNodes)[inode];
294 ref->SetPoint(inode, node->Data()[ax1], node->Data()[ax2]);
300 //_________________________________________________________________
301 void TKDInterpolatorBase::SetAlpha(Float_t a)
304 Warning("TKDInterpolatorBase::SetAlpha()", "The scale parameter has to be larger than 0.5");
309 if(Int_t((a+1.)*fLambda) > fNTNodes){
310 fAlpha = TMath::Max(0.5, Float_t(fNTNodes)/fLambda-1.);
311 Warning("TKDInterpolatorBase::SetAlpha()", Form("Interpolation neighborhood exceeds number of evaluation points. Downscale alpha to %f", fAlpha));
312 printf("n[%d] nodes[%d]\n", Int_t((fAlpha+1.)*fLambda), fNTNodes);
319 //__________________________________________________________________
320 void TKDInterpolatorBase::SetInterpolationMethod(Bool_t on)
322 // Set interpolation bit to "on".
324 if(on) fStatus += fStatus&1 ? 0 : 1;
325 else fStatus += fStatus&1 ? -1 : 0;
329 //_________________________________________________________________
330 void TKDInterpolatorBase::SetStore(Bool_t on)
332 // Set store bit to "on"
334 if(on) fStatus += fStatus&2 ? 0 : 2;
335 else fStatus += fStatus&2 ? -2 : 0;
338 //_________________________________________________________________
339 void TKDInterpolatorBase::SetWeights(Bool_t on)
341 // Set weights bit to "on"
343 if(on) fStatus += fStatus&4 ? 0 : 4;
344 else fStatus += fStatus&4 ? -4 : 0;