AliRoot Core  3dc7879 (3dc7879)
AliCheb3D.h
Go to the documentation of this file.
1 // Author: ruben.shahoyan@cern.ch 09/09/2006
2 
4 // //
5 // AliCheb3D produces the interpolation of the user 3D->NDimOut arbitrary //
6 // function supplied in "void (*fcn)(float* inp,float* out)" format //
7 // either in a separate macro file or as a function pointer. //
8 // Only coefficients needed to guarantee the requested precision are kept. //
9 // //
10 // The user-callable methods are: //
11 // To create the interpolation use: //
12 // AliCheb3D(const char* funName, // name of the file with user function //
13 // or //
14 // AliCheb3D(void (*ptr)(float*,float*),// pointer on the user function //
15 // Int_t DimOut, // dimensionality of the function's output //
16 // Float_t *bmin, // lower 3D bounds of interpolation domain //
17 // Float_t *bmax, // upper 3D bounds of interpolation domain //
18 // Int_t *npoints, // number of points in each of 3 input //
19 // // dimension, defining the interpolation grid //
20 // Float_t prec=1E-6); // requested max.absolute difference between //
21 // // the interpolation and any point on grid //
22 // //
23 // To test obtained parameterization use the method //
24 // TH1* TestRMS(int idim,int npoints = 1000,TH1* histo=0); //
25 // it will compare the user output of the user function and interpolation //
26 // for idim-th output dimension and fill the difference in the supplied //
27 // histogram. If no histogram is supplied, it will be created. //
28 // //
29 // To save the interpolation data: //
30 // SaveData(const char* filename, Bool_t append ) //
31 // write text file with data. If append is kTRUE and the output file already //
32 // exists, data will be added in the end of the file. //
33 // Alternatively, SaveData(FILE* stream) will write the data to //
34 // already existing stream. //
35 // //
36 // To read back already stored interpolation use either the constructor //
37 // AliCheb3D(const char* inpFile); //
38 // or the default constructor AliCheb3D() followed by //
39 // AliCheb3D::LoadData(const char* inpFile); //
40 // //
41 // To compute the interpolation use Eval(float* par,float *res) method, with //
42 // par being 3D vector of arguments (inside the validity region) and res is //
43 // the array of DimOut elements for the output. //
44 // //
45 // If only one component (say, idim-th) of the output is needed, use faster //
46 // Float_t Eval(Float_t *par,int idim) method. //
47 // //
48 // void Print(option="") will print the name, the ranges of validity and //
49 // the absolute precision of the parameterization. Option "l" will also print //
50 // the information about the number of coefficients for each output //
51 // dimension. //
52 // //
53 // NOTE: during the evaluation no check is done for parameter vector being //
54 // outside the interpolation region. If there is such a risk, use //
55 // Bool_t IsInside(float *par) method. Chebyshev parameterization is not //
56 // good for extrapolation! //
57 // //
58 // For the properties of Chebyshev parameterization see: //
59 // H.Wind, CERN EP Internal Report, 81-12/Rev. //
60 // //
62 
63 
64 #ifndef ALICHEB3D_H
65 #define ALICHEB3D_H
66 
67 #include <TNamed.h>
68 #include <TObjArray.h>
69 #include "AliCheb3DCalc.h"
70 
71 class TString;
72 class TSystem;
73 class TRandom;
74 class TH1;
75 class TMethodCall;
76 class TRandom;
77 class TROOT;
78 class stdio;
79 
80 
81 
82 class AliCheb3D: public TNamed
83 {
84  public:
85  AliCheb3D();
86  AliCheb3D(const AliCheb3D& src);
87  AliCheb3D(const char* inpFile);
88  AliCheb3D(FILE* stream);
89  //
90 #ifdef _INC_CREATION_ALICHEB3D_
91  AliCheb3D(const char* funName, Int_t DimOut, const Float_t *bmin, const Float_t *bmax, Int_t *npoints, Float_t prec=1E-6, const Float_t* precD=0);
92  AliCheb3D(void (*ptr)(float*,float*), Int_t DimOut, Float_t *bmin,Float_t *bmax, Int_t *npoints, Float_t prec=1E-6, const Float_t* precD=0);
93  AliCheb3D(void (*ptr)(float*,float*), int DimOut, Float_t *bmin,Float_t *bmax, Int_t *npX,Int_t *npY,Int_t *npZ, Float_t prec=1E-6, const Float_t* precD=0);
94  AliCheb3D(void (*ptr)(float*,float*), int DimOut, Float_t *bmin,Float_t *bmax, Float_t prec=1E-6, Bool_t run=kTRUE, const Float_t* precD=0);
95 #endif
96  //
99  //
100  AliCheb3D& operator=(const AliCheb3D& rhs);
101  void Eval(const Float_t *par, Float_t *res);
102  Float_t Eval(const Float_t *par,int idim);
103  void Eval(const Double_t *par, Double_t *res);
104  Double_t Eval(const Double_t *par,int idim);
105  //
106  void EvalDeriv(int dimd, const Float_t *par, Float_t *res);
107  void EvalDeriv2(int dimd1, int dimd2, const Float_t *par,Float_t *res);
108  Float_t EvalDeriv(int dimd, const Float_t *par, int idim);
109  Float_t EvalDeriv2(int dimd1,int dimd2, const Float_t *par, int idim);
110  void EvalDeriv3D(const Float_t *par, Float_t dbdr[3][3]);
111  void EvalDeriv3D2(const Float_t *par, Float_t dbdrdr[3][3][3]);
112  void Print(const Option_t* opt="") const;
113  Bool_t IsInside(const Float_t *par) const;
114  Bool_t IsInside(const Double_t *par) const;
115  //
116  AliCheb3DCalc* GetChebCalc(int i) const {return (AliCheb3DCalc*)fChebCalc.UncheckedAt(i);}
117  Float_t GetBoundMin(int i) const {return fBMin[i];}
118  Float_t GetBoundMax(int i) const {return fBMax[i];}
119  Float_t* GetBoundMin() const {return (float*)fBMin;}
120  Float_t* GetBoundMax() const {return (float*)fBMax;}
121  Float_t GetPrecision() const {return fPrec;}
122  void ShiftBound(int id,float dif);
123  //
124  void LoadData(const char* inpFile);
125  void LoadData(FILE* stream);
126  //
127 #ifdef _INC_CREATION_ALICHEB3D_
128  void InvertSign();
129  int* GetNCNeeded(float xyz[3],int DimVar, float mn,float mx, float prec, Int_t npCheck=30);
130  void EstimateNPoints(float prec, int gridBC[3][3],Int_t npd1=30,Int_t npd2=30,Int_t npd3=30);
131  void SaveData(const char* outfile,Bool_t append=kFALSE) const;
132  void SaveData(FILE* stream=stdout) const;
133  //
134  void SetUsrFunction(const char* name);
135  void SetUsrFunction(void (*ptr)(float*,float*));
136  void EvalUsrFunction(const Float_t *x, Float_t *res);
137  TH1* TestRMS(int idim,int npoints = 1000,TH1* histo=0);
138  static Int_t CalcChebCoefs(const Float_t *funval,int np, Float_t *outCoefs, Float_t prec=-1);
139  const Float_t* GetBScale() const {return fBScale;}
140  const Float_t* GetBOffset() const {return fBOffset;}
141  void PrepareBoundaries(const Float_t *bmin,const Float_t *bmax);
142 #endif
143  //
144  protected:
145  void Clear(const Option_t* option = "");
146  void SetDimOut(const int d, const float* prec=0);
147  //
148 #ifdef _INC_CREATION_ALICHEB3D_
149  void EvalUsrFunction();
150  void DefineGrid(Int_t* npoints);
151  Int_t ChebFit(); // fit all output dimensions
152  Int_t ChebFit(int dmOut);
153  void SetPrecision(float prec) {fPrec = prec;}
154 #endif
155  //
156  Float_t MapToInternal(Float_t x,Int_t d) const; // map x to [-1:1]
157  Float_t MapToExternal(Float_t x,Int_t d) const {return x/fBScale[d]+fBOffset[d];} // map from [-1:1] to x
158  Double_t MapToInternal(Double_t x,Int_t d) const; // map x to [-1:1]
159  Double_t MapToExternal(Double_t x,Int_t d) const {return x/fBScale[d]+fBOffset[d];} // map from [-1:1] to x
160  //
161  protected:
162  Int_t fDimOut; // dimension of the ouput array
163  Float_t fPrec; // requested precision
164  Float_t fBMin[3]; // min boundaries in each dimension
165  Float_t fBMax[3]; // max boundaries in each dimension
166  Float_t fBScale[3]; // scale for boundary mapping to [-1:1] interval
167  Float_t fBOffset[3]; // offset for boundary mapping to [-1:1] interval
168  TObjArray fChebCalc; // Chebyshev parameterization for each output dimension
169  //
170  Int_t fMaxCoefs;
171  Int_t fNPoints[3];
172  Float_t fArgsTmp[3];
173  Float_t * fResTmp;
174  Float_t * fGrid;
175  Int_t fGridOffs[3];
176  TString fUsrFunName;
177  TMethodCall* fUsrMacro;
178  //
179  static const Float_t fgkMinPrec; // smallest precision
180  //
181  ClassDef(AliCheb3D,2) // Chebyshev parametrization for 3D->N function
182 };
183 
184 //__________________________________________________________________________________________
185 inline Bool_t AliCheb3D::IsInside(const Float_t *par) const
186 {
187  // check if the point is inside of the fitted box
188  for (int i=3;i--;) if (fBMin[i]>par[i] || par[i]>fBMax[i]) return kFALSE;
189  return kTRUE;
190 }
191 
192 //__________________________________________________________________________________________
193 inline Bool_t AliCheb3D::IsInside(const Double_t *par) const
194 {
195  // check if the point is inside of the fitted box
196  for (int i=3;i--;) if (fBMin[i]>par[i] || par[i]>fBMax[i]) return kFALSE;
197  return kTRUE;
198 }
199 
200 //__________________________________________________________________________________________
201 inline void AliCheb3D::Eval(const Float_t *par, Float_t *res)
202 {
203  // evaluate Chebyshev parameterization for 3d->DimOut function
204  for (int i=3;i--;) fArgsTmp[i] = MapToInternal(par[i],i);
205  for (int i=fDimOut;i--;) res[i] = GetChebCalc(i)->Eval(fArgsTmp);
206  //
207 }
208 //__________________________________________________________________________________________
209 inline void AliCheb3D::Eval(const Double_t *par, Double_t *res)
210 {
211  // evaluate Chebyshev parameterization for 3d->DimOut function
212  for (int i=3;i--;) fArgsTmp[i] = MapToInternal(par[i],i);
213  for (int i=fDimOut;i--;) res[i] = GetChebCalc(i)->Eval(fArgsTmp);
214  //
215 }
216 
217 //__________________________________________________________________________________________
218 inline Double_t AliCheb3D::Eval(const Double_t *par, int idim)
219 {
220  // evaluate Chebyshev parameterization for idim-th output dimension of 3d->DimOut function
221  for (int i=3;i--;) fArgsTmp[i] = MapToInternal(par[i],i);
222  return GetChebCalc(idim)->Eval(fArgsTmp);
223  //
224 }
225 
226 //__________________________________________________________________________________________
227 inline Float_t AliCheb3D::Eval(const Float_t *par, int idim)
228 {
229  // evaluate Chebyshev parameterization for idim-th output dimension of 3d->DimOut function
230  for (int i=3;i--;) fArgsTmp[i] = MapToInternal(par[i],i);
231  return GetChebCalc(idim)->Eval(fArgsTmp);
232  //
233 }
234 
235 //__________________________________________________________________________________________
236 inline void AliCheb3D::EvalDeriv3D(const Float_t *par, Float_t dbdr[3][3])
237 {
238  // return gradient matrix
239  for (int i=3;i--;) fArgsTmp[i] = MapToInternal(par[i],i);
240  for (int ib=3;ib--;) for (int id=3;id--;) dbdr[ib][id] = GetChebCalc(ib)->EvalDeriv(id,fArgsTmp)*fBScale[id];
241 }
242 
243 //__________________________________________________________________________________________
244 inline void AliCheb3D::EvalDeriv3D2(const Float_t *par, Float_t dbdrdr[3][3][3])
245 {
246  // return gradient matrix
247  for (int i=3;i--;) fArgsTmp[i] = MapToInternal(par[i],i);
248  for (int ib=3;ib--;) for (int id=3;id--;)for (int id1=3;id1--;)
249  dbdrdr[ib][id][id1] = GetChebCalc(ib)->EvalDeriv2(id,id1,fArgsTmp)*fBScale[id]*fBScale[id1];
250 }
251 
252 //__________________________________________________________________________________________
253 inline void AliCheb3D::EvalDeriv(int dimd, const Float_t *par, Float_t *res)
254 {
255  // evaluate Chebyshev parameterization derivative for 3d->DimOut function
256  for (int i=3;i--;) fArgsTmp[i] = MapToInternal(par[i],i);
257  for (int i=fDimOut;i--;) res[i] = GetChebCalc(i)->EvalDeriv(dimd,fArgsTmp)*fBScale[dimd];;
258  //
259 }
260 
261 //__________________________________________________________________________________________
262 inline void AliCheb3D::EvalDeriv2(int dimd1,int dimd2, const Float_t *par, Float_t *res)
263 {
264  // evaluate Chebyshev parameterization 2nd derivative over dimd1 and dimd2 dimensions for 3d->DimOut function
265  for (int i=3;i--;) fArgsTmp[i] = MapToInternal(par[i],i);
266  for (int i=fDimOut;i--;) res[i] = GetChebCalc(i)->EvalDeriv2(dimd1,dimd2,fArgsTmp)*fBScale[dimd1]*fBScale[dimd2];
267  //
268 }
269 
270 //__________________________________________________________________________________________
271 inline Float_t AliCheb3D::EvalDeriv(int dimd, const Float_t *par, int idim)
272 {
273  // evaluate Chebyshev parameterization derivative over dimd dimention for idim-th output dimension of 3d->DimOut function
274  for (int i=3;i--;) fArgsTmp[i] = MapToInternal(par[i],i);
275  return GetChebCalc(idim)->EvalDeriv(dimd,fArgsTmp)*fBScale[dimd];
276  //
277 }
278 
279 //__________________________________________________________________________________________
280 inline Float_t AliCheb3D::EvalDeriv2(int dimd1,int dimd2, const Float_t *par, int idim)
281 {
282  // evaluate Chebyshev parameterization 2ns derivative over dimd1 and dimd2 dimensions for idim-th output dimension of 3d->DimOut function
283  for (int i=3;i--;) fArgsTmp[i] = MapToInternal(par[i],i);
284  return GetChebCalc(idim)->EvalDeriv2(dimd1,dimd2,fArgsTmp)*fBScale[dimd1]*fBScale[dimd2];
285  //
286 }
287 
288 //__________________________________________________________________________________________
289 inline Float_t AliCheb3D::MapToInternal(Float_t x,Int_t d) const
290 {
291  // map x to [-1:1]
292 #ifdef _BRING_TO_BOUNDARY_
293  float res = (x-fBOffset[d])*fBScale[d];
294  if (res<-1) return -1;
295  if (res> 1) return 1;
296  return res;
297 #else
298  return (x-fBOffset[d])*fBScale[d];
299 #endif
300 }
301 
302 //__________________________________________________________________________________________
303 inline Double_t AliCheb3D::MapToInternal(Double_t x,Int_t d) const
304 {
305  // map x to [-1:1]
306 #ifdef _BRING_TO_BOUNDARY_
307  double res = (x-fBOffset[d])*fBScale[d];
308  if (res<-1) return -1;
309  if (res> 1) return 1;
310  return res;
311 #else
312  return (x-fBOffset[d])*fBScale[d];
313 #endif
314 }
315 
316 #endif
Float_t Eval(const Float_t *par) const
int * GetNCNeeded(float xyz[3], int DimVar, float mn, float mx, float prec, Int_t npCheck=30)
Definition: AliCheb3D.cxx:950
Float_t GetBoundMin(int i) const
Definition: AliCheb3D.h:117
Float_t * GetBoundMax() const
Definition: AliCheb3D.h:120
TMethodCall * fUsrMacro
name of user macro containing the function of "void (*fcn)(float*,float*)" format ...
Definition: AliCheb3D.h:177
Int_t fMaxCoefs
Definition: AliCheb3D.h:170
#define TObjArray
Float_t fArgsTmp[3]
number of used points in each dimension
Definition: AliCheb3D.h:172
void EvalDeriv2(int dimd1, int dimd2, const Float_t *par, Float_t *res)
Definition: AliCheb3D.h:262
Double_t MapToExternal(Double_t x, Int_t d) const
Definition: AliCheb3D.h:159
const Float_t * GetBScale() const
Definition: AliCheb3D.h:139
Int_t fDimOut
Definition: AliCheb3D.h:162
void SetDimOut(const int d, const float *prec=0)
Definition: AliCheb3D.cxx:783
AliCheb3DCalc * GetChebCalc(int i) const
Definition: AliCheb3D.h:116
void InvertSign()
Definition: AliCheb3D.cxx:713
void Eval(const Float_t *par, Float_t *res)
Definition: AliCheb3D.h:201
Float_t fBMin[3]
Definition: AliCheb3D.h:164
~AliCheb3D()
Definition: AliCheb3D.h:98
npoints
Definition: driftITSTPC.C:85
TH1 * TestRMS(int idim, int npoints=1000, TH1 *histo=0)
Definition: AliCheb3D.cxx:810
TObjArray fChebCalc
Definition: AliCheb3D.h:168
Float_t fBScale[3]
Definition: AliCheb3D.h:166
Int_t ChebFit()
Definition: AliCheb3D.cxx:488
void Print(const Option_t *opt="") const
Definition: AliCheb3D.cxx:332
Float_t fBMax[3]
Definition: AliCheb3D.h:165
Float_t MapToInternal(Float_t x, Int_t d) const
Definition: AliCheb3D.h:289
void SetUsrFunction(const char *name)
Definition: AliCheb3D.cxx:375
Float_t EvalDeriv2(int dim1, int dim2, const Float_t *par) const
void EvalDeriv3D2(const Float_t *par, Float_t dbdrdr[3][3][3])
Definition: AliCheb3D.h:244
Float_t fBOffset[3]
Definition: AliCheb3D.h:167
Float_t * GetBoundMin() const
Definition: AliCheb3D.h:119
TString fUsrFunName
start of grid for each dimension
Definition: AliCheb3D.h:176
Bool_t IsInside(const Float_t *par) const
Definition: AliCheb3D.h:185
Int_t fNPoints[3]
max possible number of coefs per parameterization
Definition: AliCheb3D.h:171
void EstimateNPoints(float prec, int gridBC[3][3], Int_t npd1=30, Int_t npd2=30, Int_t npd3=30)
Definition: AliCheb3D.cxx:839
Float_t EvalDeriv(int dim, const Float_t *par) const
Float_t MapToExternal(Float_t x, Int_t d) const
Definition: AliCheb3D.h:157
static const Float_t fgkMinPrec
Pointer to MethodCall for function from user macro.
Definition: AliCheb3D.h:179
void ShiftBound(int id, float dif)
Definition: AliCheb3D.cxx:798
Float_t * fResTmp
temporary vector for coefs caluclation
Definition: AliCheb3D.h:173
void SetPrecision(float prec)
Definition: AliCheb3D.h:153
Int_t fGridOffs[3]
temporary buffer for Chebyshef roots grid
Definition: AliCheb3D.h:175
void Clear(const Option_t *option="")
Definition: AliCheb3D.cxx:319
const Float_t * GetBOffset() const
Definition: AliCheb3D.h:140
void EvalDeriv(int dimd, const Float_t *par, Float_t *res)
Definition: AliCheb3D.h:253
void EvalUsrFunction()
Definition: AliCheb3D.cxx:368
AliCheb3D & operator=(const AliCheb3D &rhs)
Definition: AliCheb3D.cxx:291
void SaveData(const char *outfile, Bool_t append=kFALSE) const
Definition: AliCheb3D.cxx:676
static Int_t CalcChebCoefs(const Float_t *funval, int np, Float_t *outCoefs, Float_t prec=-1)
Definition: AliCheb3D.cxx:426
void LoadData(const char *inpFile)
Definition: AliCheb3D.cxx:727
void EvalDeriv3D(const Float_t *par, Float_t dbdr[3][3])
Definition: AliCheb3D.h:236
void res(Char_t i)
Definition: Resolution.C:2
void DefineGrid(Int_t *npoints)
Definition: AliCheb3D.cxx:457
Float_t fPrec
Definition: AliCheb3D.h:163
Float_t GetBoundMax(int i) const
Definition: AliCheb3D.h:118
TObjArray * GetChebCalcArray() const
Definition: AliCheb3D.h:97
void PrepareBoundaries(const Float_t *bmin, const Float_t *bmax)
Definition: AliCheb3D.cxx:344
Float_t GetPrecision() const
Definition: AliCheb3D.h:121
Float_t * fGrid
temporary vector for results of user function caluclation
Definition: AliCheb3D.h:174