CPU and Memory tests, coding violations fixed, library
[u/mrichter/AliRoot.git] / STAT / TKDInterpolator.h
1 #ifndef ROOT_TKDInterpolator
2 #define ROOT_TKDInterpolator
3
4 #ifndef ROOT_TKDTree
5 #include "TKDTree.h"
6 #endif
7
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.
11
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 TKDTreeIF<Int_t, Float_t> TKDTreeIF;
18 class TTree;
19 class TLinearFitter;
20 class TKDInterpolator : public TKDTreeIF
21 {
22 public:
23         TKDInterpolator();
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);
26         ~TKDInterpolator();
27
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;}
37                                         
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);
40                 void       GetStatus();
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);
45         
46 private:
47         TKDInterpolator(const TKDInterpolator &);
48         TKDInterpolator& operator=(const TKDInterpolator &);    
49                 void       Build();
50                                         
51 public:
52         class TKDNodeInfo
53         {
54         public:
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);
60         
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
66
67         private:
68                 TKDNodeInfo(const TKDNodeInfo &);
69                 TKDNodeInfo& operator=(const TKDNodeInfo &);
70                 
71                 ClassDef(TKDNodeInfo, 1) // node info for interpolator
72         };
73
74 protected:
75         Int_t     fNTNodes;        //Number of evaluation data points
76         TKDNodeInfo *fTNodes;      //[fNTNodes] interpolation node
77
78 private:
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    
87
88         ClassDef(TKDInterpolator, 1)   // data interpolator based on KD tree
89 };
90
91 //__________________________________________________________________
92 Bool_t  TKDInterpolator::GetCOGPoint(Int_t node, Float_t *coord, Float_t &val, Float_t &err) const
93 {
94         if(node < 0 || node > fNTNodes) return kFALSE;
95
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);
99         return kTRUE;
100 }
101
102 //__________________________________________________________________
103 Bool_t  TKDInterpolator::GetDataPoint(Int_t n, Float_t *p) const
104 {
105         if(n < 0 || n >= fNpoints) return kFALSE;
106         if(!fData) return kFALSE;
107                 
108         for(int i=0; i<fNDim; i++) p[i] = fData[i][n];
109         return kTRUE;
110 }
111
112
113 #endif
114