1 #ifndef ROOT_TKDInterpolator
2 #define ROOT_TKDInterpolator
8 // Non parametric interpolation class based on local polinomial regression.
9 // The class can be used to approximate PDF together with TKDTree or for
10 // general regression when the data points are given.
12 template <typename Value> class TVectorT;
13 typedef struct TVectorT<Double_t> TVectorD;
14 template <typename Value> class TMatrixT;
15 typedef class TMatrixT<Double_t> TMatrixD;
16 template <typename Index, typename Value> class TKDTree;
17 typedef class TKDTree<Int_t, Float_t> TKDTreeIF;
20 class TKDInterpolator : public TKDTreeIF
24 TKDInterpolator(TTree *t, const Char_t *var, const Char_t *cut = 0, UInt_t bsize = 100, Long64_t nentries = 1000000000, Long64_t firstentry = 0);
25 TKDInterpolator(Int_t npoints, Int_t ndim, UInt_t bsize, Float_t **data);
28 Double_t Eval(const Double_t *point, Double_t &result, Double_t &error, Bool_t force = kFALSE);
29 Float_t GetAlpha() const {return fAlpha;}
30 inline Bool_t GetCOGPoint(Int_t node, Float_t *coord, Float_t &val, Float_t &error) const;
31 inline Bool_t GetDataPoint(Int_t n, Float_t *p) const;
32 TKDTreeIF* GetHelper() {return fKDhelper;}
33 Bool_t GetInterpolationMethod() const {return fStatus&1;}
34 Int_t GetNTNodes() const {return fNTNodes;}
35 Bool_t GetStore() const {return fStatus&2;}
36 Bool_t GetWeights() const {return fStatus&4;}
38 void DrawNodes(UInt_t ax1 = 0, UInt_t ax2 = 1, Int_t depth = -1);
39 void DrawNode(Int_t tnode, UInt_t ax1 = 0, UInt_t ax2 = 1);
41 void SetAlpha(const Float_t a){if(a>0.) fAlpha = a;}
42 void SetInterpolationMethod(const Bool_t on = kTRUE);
43 void SetStore(const Bool_t on = kTRUE);
44 void SetWeights(const Bool_t on = kTRUE);
47 TKDInterpolator(const TKDInterpolator &);
48 TKDInterpolator& operator=(const TKDInterpolator &);
55 TKDNodeInfo(const Int_t ndim = 0);
56 virtual ~TKDNodeInfo();
57 void Build(const Int_t ndim);
58 Double_t CookPDF(const Double_t *point, Double_t &result, Double_t &error);
59 void Store(const TVectorD &par, const TMatrixD &cov);
61 Int_t fNDim; // data dimension
62 Float_t *fRefPoint; //[fNDim] node's COG
63 Float_t fRefValue; // measured value for node
64 TMatrixD *fCov; // interpolator covariance matrix
65 TVectorD *fPar; // interpolator parameters
68 TKDNodeInfo(const TKDNodeInfo &);
69 TKDNodeInfo& operator=(const TKDNodeInfo &);
71 ClassDef(TKDNodeInfo, 1) // node info for interpolator
75 Int_t fNTNodes; //Number of evaluation data points
76 TKDNodeInfo *fTNodes; //[fNTNodes] interpolation node
79 UChar_t fStatus; // status of the interpolator
80 UChar_t fLambda; // number of parameters in polynom
81 Short_t fDepth; //! depth of the KD Tree structure used
82 Float_t fAlpha; // alpha parameter
83 Float_t **fRefPoints; //! temporary storage of COG data
84 Double_t *fBuffer; //! working space [2*fLambda]
85 TKDTreeIF *fKDhelper; //! kNN finder
86 TLinearFitter *fFitter; //! linear fitter
88 ClassDef(TKDInterpolator, 1) // data interpolator based on KD tree
91 //__________________________________________________________________
92 Bool_t TKDInterpolator::GetCOGPoint(Int_t node, Float_t *coord, Float_t &val, Float_t &err) const
94 if(node < 0 || node > fNTNodes) return kFALSE;
96 for(int idim=0; idim<fNDim; idim++) coord[idim] = fTNodes[node].fRefPoint[idim];
97 val = fTNodes[node].fRefValue;
98 err = fTNodes[node].fRefValue/TMath::Sqrt(fBucketSize);
102 //__________________________________________________________________
103 Bool_t TKDInterpolator::GetDataPoint(Int_t n, Float_t *p) const
105 if(n < 0 || n >= fNpoints) return kFALSE;
106 if(!fData) return kFALSE;
108 for(int i=0; i<fNDim; i++) p[i] = fData[i][n];