]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/AliCheb3D.cxx
Inherits from AliPIDResponse
[u/mrichter/AliRoot.git] / STEER / AliCheb3D.cxx
CommitLineData
5406439e 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
0eea9d4d 15
16#include <TString.h>
17#include <TSystem.h>
0eea9d4d 18#include <TROOT.h>
40389866 19#include <TRandom.h>
5406439e 20#include <stdio.h>
21#include <TMethodCall.h>
22#include <TMath.h>
23#include <TH1.h>
0eea9d4d 24#include "AliCheb3D.h"
5406439e 25#include "AliCheb3DCalc.h"
0eea9d4d 26
0eea9d4d 27ClassImp(AliCheb3D)
28
40389866 29//__________________________________________________________________________________________
30AliCheb3D::AliCheb3D() :
31 fDimOut(0),
32 fPrec(0),
33 fChebCalc(1),
34 fMaxCoefs(0),
35 fResTmp(0),
36 fGrid(0),
37 fUsrFunName(""),
38 fUsrMacro(0)
0eea9d4d 39{
5e6a3170 40// Default constructor
c8f37c50 41 for (int i=3;i--;) {
d97d27ff 42 fBMin[i] = fBMax[i] = fBScale[i] = fBOffset[i] = fArgsTmp[i] = 0;
c8f37c50 43 fNPoints[i] = 0;
34473f9e 44 fGridOffs[i] = 0;
c8f37c50 45 }
0eea9d4d 46}
47
40389866 48//__________________________________________________________________________________________
49AliCheb3D::AliCheb3D(const AliCheb3D& src) :
50 TNamed(src),
51 fDimOut(src.fDimOut),
52 fPrec(src.fPrec),
53 fChebCalc(1),
54 fMaxCoefs(src.fMaxCoefs),
55 fResTmp(0),
56 fGrid(0),
57 fUsrFunName(src.fUsrFunName),
58 fUsrMacro(0)
0eea9d4d 59{
40389866 60 // read coefs from text file
61 for (int i=3;i--;) {
62 fBMin[i] = src.fBMin[i];
63 fBMax[i] = src.fBMax[i];
64 fBScale[i] = src.fBScale[i];
65 fBOffset[i] = src.fBOffset[i];
66 fNPoints[i] = src.fNPoints[i];
c8f37c50 67 fGridOffs[i] = src.fGridOffs[i];
f9efbbf6 68 fArgsTmp[i] = 0;
40389866 69 }
70 for (int i=0;i<fDimOut;i++) {
71 AliCheb3DCalc* cbc = src.GetChebCalc(i);
72 if (cbc) fChebCalc.AddAtAndExpand(new AliCheb3DCalc(*cbc),i);
73 }
0eea9d4d 74}
75
40389866 76//__________________________________________________________________________________________
77AliCheb3D::AliCheb3D(const char* inpFile) :
78 fDimOut(0),
79 fPrec(0),
80 fChebCalc(1),
81 fMaxCoefs(0),
82 fResTmp(0),
83 fGrid(0),
84 fUsrFunName(""),
85 fUsrMacro(0)
0bc7b414 86{
40389866 87 // read coefs from text file
c8f37c50 88 for (int i=3;i--;) {
89 fBMin[i] = fBMax[i] = fBScale[i] = fBOffset[i] = 0;
90 fNPoints[i] = 0;
34473f9e 91 fGridOffs[i] = 0;
f9efbbf6 92 fArgsTmp[i] = 0;
c8f37c50 93 }
40389866 94 LoadData(inpFile);
c437b1a5 95}
96
40389866 97//__________________________________________________________________________________________
98AliCheb3D::AliCheb3D(FILE* stream) :
99 fDimOut(0),
100 fPrec(0),
101 fChebCalc(1),
102 fMaxCoefs(0),
103 fResTmp(0),
104 fGrid(0),
105 fUsrFunName(""),
106 fUsrMacro(0)
c437b1a5 107{
40389866 108 // read coefs from stream
c8f37c50 109 for (int i=3;i--;) {
110 fBMin[i] = fBMax[i] = fBScale[i] = fBOffset[i] = 0;
111 fNPoints[i] = 0;
34473f9e 112 fGridOffs[i] = 0;
f9efbbf6 113 fArgsTmp[i] = 0;
c8f37c50 114 }
40389866 115 LoadData(stream);
0bc7b414 116}
0eea9d4d 117
118//__________________________________________________________________________________________
119#ifdef _INC_CREATION_ALICHEB3D_
5e6a3170 120AliCheb3D::AliCheb3D(const char* funName, int DimOut, const Float_t *bmin, const Float_t *bmax, Int_t *npoints, Float_t prec) :
40389866 121 TNamed(funName,funName),
122 fDimOut(0),
123 fPrec(TMath::Max(1.E-12f,prec)),
124 fChebCalc(1),
125 fMaxCoefs(0),
126 fResTmp(0),
127 fGrid(0),
128 fUsrFunName("") ,
129 fUsrMacro(0)
0eea9d4d 130{
131 // Construct the parameterization for the function
132 // funName : name of the file containing the function: void funName(Float_t * inp,Float_t * out)
133 // DimOut : dimension of the vector computed by the user function
134 // bmin : array of 3 elements with the lower boundaries of the region where the function is defined
135 // bmax : array of 3 elements with the upper boundaries of the region where the function is defined
136 // npoints : array of 3 elements with the number of points to compute in each of 3 dimension
137 // prec : max allowed absolute difference between the user function and computed parameterization on the requested grid
138 //
0eea9d4d 139 if (DimOut<1) {Error("AliCheb3D","Requested output dimension is %d\nStop\n",fDimOut); exit(1);}
c8f37c50 140 for (int i=3;i--;) {
141 fBMin[i] = fBMax[i] = fBScale[i] = fBOffset[i] = 0;
142 fNPoints[i] = 0;
143 fGridOffs[i] = 0.;
f9efbbf6 144 fArgsTmp[i] = 0;
c8f37c50 145 }
0eea9d4d 146 SetDimOut(DimOut);
147 PrepareBoundaries(bmin,bmax);
148 DefineGrid(npoints);
149 SetUsrFunction(funName);
150 ChebFit();
151 //
152}
153#endif
154
155//__________________________________________________________________________________________
156#ifdef _INC_CREATION_ALICHEB3D_
40389866 157AliCheb3D::AliCheb3D(void (*ptr)(float*,float*), int DimOut, Float_t *bmin,Float_t *bmax, Int_t *npoints, Float_t prec) :
158 fDimOut(0),
159 fPrec(TMath::Max(1.E-12f,prec)),
160 fChebCalc(1),
161 fMaxCoefs(0),
162 fResTmp(0),
163 fGrid(0),
164 fUsrFunName(""),
165 fUsrMacro(0)
0eea9d4d 166{
167 // Construct the parameterization for the function
168 // ptr : pointer on the function: void fun(Float_t * inp,Float_t * out)
169 // DimOut : dimension of the vector computed by the user function
170 // bmin : array of 3 elements with the lower boundaries of the region where the function is defined
171 // bmax : array of 3 elements with the upper boundaries of the region where the function is defined
172 // npoints : array of 3 elements with the number of points to compute in each of 3 dimension
173 // prec : max allowed absolute difference between the user function and computed parameterization on the requested grid
174 //
0eea9d4d 175 if (DimOut<1) {Error("AliCheb3D","Requested output dimension is %d\nStop\n",fDimOut); exit(1);}
c8f37c50 176 if (DimOut<1) {Error("AliCheb3D","Requested output dimension is %d\nStop\n",fDimOut); exit(1);}
177 for (int i=3;i--;) {
178 fBMin[i] = fBMax[i] = fBScale[i] = fBOffset[i] = 0;
179 fNPoints[i] = 0;
180 fGridOffs[i] = 0.;
f9efbbf6 181 fArgsTmp[i] = 0;
c8f37c50 182 }
0eea9d4d 183 SetDimOut(DimOut);
184 PrepareBoundaries(bmin,bmax);
185 DefineGrid(npoints);
186 SetUsrFunction(ptr);
187 ChebFit();
188 //
189}
190#endif
191
40389866 192//__________________________________________________________________________________________
193#ifdef _INC_CREATION_ALICHEB3D_
194AliCheb3D::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) :
195 fDimOut(0),
196 fPrec(TMath::Max(1.E-12f,prec)),
197 fChebCalc(1),
198 fMaxCoefs(0),
199 fResTmp(0),
200 fGrid(0),
201 fUsrFunName(""),
202 fUsrMacro(0)
203{
204 // Construct very economic parameterization for the function
205 // ptr : pointer on the function: void fun(Float_t * inp,Float_t * out)
206 // DimOut : dimension of the vector computed by the user function
207 // bmin : array of 3 elements with the lower boundaries of the region where the function is defined
208 // bmax : array of 3 elements with the upper boundaries of the region where the function is defined
209 // npX : array of 3 elements with the number of points to compute in each dimension for 1st component
210 // npY : array of 3 elements with the number of points to compute in each dimension for 2nd component
211 // npZ : array of 3 elements with the number of points to compute in each dimension for 3d component
212 // prec : max allowed absolute difference between the user function and computed parameterization on the requested grid
213 //
214 if (DimOut<1) {Error("AliCheb3D","Requested output dimension is %d\nStop\n",fDimOut); exit(1);}
c8f37c50 215 if (DimOut<1) {Error("AliCheb3D","Requested output dimension is %d\nStop\n",fDimOut); exit(1);}
216 for (int i=3;i--;) {
217 fBMin[i] = fBMax[i] = fBScale[i] = fBOffset[i] = 0;
218 fNPoints[i] = 0;
219 fGridOffs[i] = 0.;
f9efbbf6 220 fArgsTmp[i] = 0;
c8f37c50 221 }
40389866 222 SetDimOut(DimOut);
223 PrepareBoundaries(bmin,bmax);
224 SetUsrFunction(ptr);
225 //
226 DefineGrid(npX);
227 ChebFit(0);
228 DefineGrid(npY);
229 ChebFit(1);
230 DefineGrid(npZ);
231 ChebFit(2);
232 //
233}
234#endif
235
236
237//__________________________________________________________________________________________
238#ifdef _INC_CREATION_ALICHEB3D_
1d18ebe0 239AliCheb3D::AliCheb3D(void (*ptr)(float*,float*), int DimOut, Float_t *bmin,Float_t *bmax, Float_t prec, Bool_t run) :
40389866 240 fDimOut(0),
241 fPrec(TMath::Max(1.E-12f,prec)),
242 fChebCalc(1),
243 fMaxCoefs(0),
244 fResTmp(0),
245 fGrid(0),
246 fUsrFunName(""),
247 fUsrMacro(0)
248{
249 // Construct very economic parameterization for the function with automatic calculation of the root's grid
250 // ptr : pointer on the function: void fun(Float_t * inp,Float_t * out)
251 // DimOut : dimension of the vector computed by the user function
252 // bmin : array of 3 elements with the lower boundaries of the region where the function is defined
253 // bmax : array of 3 elements with the upper boundaries of the region where the function is defined
254 // prec : max allowed absolute difference between the user function and computed parameterization on the requested grid
255 //
256 if (DimOut!=3) {Error("AliCheb3D","This constructor works only for 3D fits, %dD fit was requested\n",fDimOut); exit(1);}
c8f37c50 257 if (DimOut<1) {Error("AliCheb3D","Requested output dimension is %d\nStop\n",fDimOut); exit(1);}
258 for (int i=3;i--;) {
259 fBMin[i] = fBMax[i] = fBScale[i] = fBOffset[i] = 0;
260 fNPoints[i] = 0;
261 fGridOffs[i] = 0.;
f9efbbf6 262 fArgsTmp[i] = 0;
c8f37c50 263 }
40389866 264 SetDimOut(DimOut);
265 PrepareBoundaries(bmin,bmax);
266 SetUsrFunction(ptr);
267 //
1d18ebe0 268 if (run) {
269 int gridNC[3][3];
270 EstimateNPoints(prec,gridNC);
271 DefineGrid(gridNC[0]);
272 ChebFit(0);
273 DefineGrid(gridNC[1]);
274 ChebFit(1);
275 DefineGrid(gridNC[2]);
276 ChebFit(2);
277 }
40389866 278 //
279}
280#endif
281
282
283//__________________________________________________________________________________________
284AliCheb3D& AliCheb3D::operator=(const AliCheb3D& rhs)
285{
5406439e 286 // assignment operator
287 //
40389866 288 if (this != &rhs) {
289 Clear();
290 fDimOut = rhs.fDimOut;
291 fPrec = rhs.fPrec;
292 fMaxCoefs = rhs.fMaxCoefs;
293 fUsrFunName = rhs.fUsrFunName;
294 fUsrMacro = 0;
295 for (int i=3;i--;) {
296 fBMin[i] = rhs.fBMin[i];
297 fBMax[i] = rhs.fBMax[i];
298 fBScale[i] = rhs.fBScale[i];
299 fBOffset[i] = rhs.fBOffset[i];
300 fNPoints[i] = rhs.fNPoints[i];
301 }
302 for (int i=0;i<fDimOut;i++) {
303 AliCheb3DCalc* cbc = rhs.GetChebCalc(i);
304 if (cbc) fChebCalc.AddAtAndExpand(new AliCheb3DCalc(*cbc),i);
305 }
306 }
307 return *this;
308 //
309}
0bc7b414 310
0eea9d4d 311//__________________________________________________________________________________________
5406439e 312void AliCheb3D::Clear(const Option_t*)
0eea9d4d 313{
5406439e 314 // clear all dynamic structures
315 //
0eea9d4d 316 if (fResTmp) { delete[] fResTmp; fResTmp = 0; }
317 if (fGrid) { delete[] fGrid; fGrid = 0; }
318 if (fUsrMacro) { delete fUsrMacro; fUsrMacro = 0;}
d8d3669f 319 fChebCalc.SetOwner(kTRUE);
0eea9d4d 320 fChebCalc.Delete();
321 //
322}
323
324//__________________________________________________________________________________________
5406439e 325void AliCheb3D::Print(const Option_t* opt) const
0eea9d4d 326{
5406439e 327 // print info
328 //
0eea9d4d 329 printf("%s: Chebyshev parameterization for 3D->%dD function. Precision: %e\n",GetName(),fDimOut,fPrec);
330 printf("Region of validity: [%+.5e:%+.5e] [%+.5e:%+.5e] [%+.5e:%+.5e]\n",fBMin[0],fBMax[0],fBMin[1],fBMax[1],fBMin[2],fBMax[2]);
331 TString opts = opt; opts.ToLower();
332 if (opts.Contains("l")) for (int i=0;i<fDimOut;i++) {printf("Output dimension %d:\n",i+1); GetChebCalc(i)->Print();}
333 //
334}
335
0eea9d4d 336//__________________________________________________________________________________________
5406439e 337void AliCheb3D::PrepareBoundaries(const Float_t *bmin, const Float_t *bmax)
0eea9d4d 338{
339 // Set and check boundaries defined by user, prepare coefficients for their conversion to [-1:1] interval
340 //
341 for (int i=3;i--;) {
342 fBMin[i] = bmin[i];
343 fBMax[i] = bmax[i];
344 fBScale[i] = bmax[i]-bmin[i];
345 if (fBScale[i]<=0) {
346 Error("PrepareBoundaries","Boundaries for %d-th dimension are not increasing: %+.4e %+.4e\nStop\n",i,fBMin[i],fBMax[i]);
347 exit(1);
348 }
349 fBOffset[i] = bmin[i] + fBScale[i]/2.0;
350 fBScale[i] = 2./fBScale[i];
351 }
352 //
353}
354
40389866 355
0eea9d4d 356//__________________________________________________________________________________________
357#ifdef _INC_CREATION_ALICHEB3D_
40389866 358
359// Pointer on user function (faster altrnative to TMethodCall)
360void (*gUsrFunAliCheb3D) (float* ,float* );
361
362void AliCheb3D::EvalUsrFunction()
363{
364 // call user supplied function
365 if (gUsrFunAliCheb3D) gUsrFunAliCheb3D(fArgsTmp,fResTmp);
366 else fUsrMacro->Execute();
367}
368
0eea9d4d 369void AliCheb3D::SetUsrFunction(const char* name)
370{
371 // load user macro with function definition and compile it
372 gUsrFunAliCheb3D = 0;
373 fUsrFunName = name;
374 gSystem->ExpandPathName(fUsrFunName);
375 if (fUsrMacro) delete fUsrMacro;
376 TString tmpst = fUsrFunName;
377 tmpst += "+"; // prepare filename to compile
378 if (gROOT->LoadMacro(tmpst.Data())) {Error("SetUsrFunction","Failed to load user function from %s\nStop\n",name); exit(1);}
379 fUsrMacro = new TMethodCall();
380 tmpst = tmpst.Data() + tmpst.Last('/')+1; //Strip away any path preceding the macro file name
381 int dot = tmpst.Last('.');
382 if (dot>0) tmpst.Resize(dot);
383 fUsrMacro->InitWithPrototype(tmpst.Data(),"Float_t *,Float_t *");
384 long args[2];
385 args[0] = (long)fArgsTmp;
386 args[1] = (long)fResTmp;
387 fUsrMacro->SetParamPtrs(args);
388 //
389}
390#endif
391
392//__________________________________________________________________________________________
393#ifdef _INC_CREATION_ALICHEB3D_
394void AliCheb3D::SetUsrFunction(void (*ptr)(float*,float*))
395{
5406439e 396 // assign user training function
397 //
0eea9d4d 398 if (fUsrMacro) delete fUsrMacro;
399 fUsrMacro = 0;
400 fUsrFunName = "";
401 gUsrFunAliCheb3D = ptr;
402}
403#endif
404
405//__________________________________________________________________________________________
406#ifdef _INC_CREATION_ALICHEB3D_
da7cd221 407void AliCheb3D::EvalUsrFunction(const Float_t *x, Float_t *res)
5406439e 408{
409 // evaluate user function value
410 //
0eea9d4d 411 for (int i=3;i--;) fArgsTmp[i] = x[i];
412 if (gUsrFunAliCheb3D) gUsrFunAliCheb3D(fArgsTmp,fResTmp);
413 else fUsrMacro->Execute();
414 for (int i=fDimOut;i--;) res[i] = fResTmp[i];
415}
416#endif
417
418//__________________________________________________________________________________________
419#ifdef _INC_CREATION_ALICHEB3D_
5406439e 420Int_t AliCheb3D::CalcChebCoefs(const Float_t *funval,int np, Float_t *outCoefs, Float_t prec)
0eea9d4d 421{
422 // Calculate Chebyshev coeffs using precomputed function values at np roots.
423 // If prec>0, estimate the highest coeff number providing the needed precision
424 //
425 double sm; // do summations in double to minimize the roundoff error
426 for (int ic=0;ic<np;ic++) { // compute coeffs
427 sm = 0;
428 for (int ir=0;ir<np;ir++) {
429 float rt = TMath::Cos( ic*(ir+0.5)*TMath::Pi()/np);
430 sm += funval[ir]*rt;
431 }
432 outCoefs[ic] = Float_t( sm * ((ic==0) ? 1./np : 2./np) );
433 }
434 //
435 if (prec<=0) return np;
436 //
437 sm = 0;
438 int cfMax = 0;
439 for (cfMax=np;cfMax--;) {
440 sm += TMath::Abs(outCoefs[cfMax]);
441 if (sm>=prec) break;
442 }
443 if (++cfMax==0) cfMax=1;
444 return cfMax;
445 //
446}
447#endif
448
449//__________________________________________________________________________________________
450#ifdef _INC_CREATION_ALICHEB3D_
451void AliCheb3D::DefineGrid(Int_t* npoints)
452{
453 // prepare the grid of Chebyshev roots in each dimension
454 const int kMinPoints = 1;
455 int ntot = 0;
456 fMaxCoefs = 1;
457 for (int id=3;id--;) {
458 fNPoints[id] = npoints[id];
459 if (fNPoints[id]<kMinPoints) {
40389866 460 Error("DefineGrid","at %d-th dimension %d point is requested, at least %d is needed\nStop\n",id,fNPoints[id],kMinPoints);
0eea9d4d 461 exit(1);
462 }
463 ntot += fNPoints[id];
464 fMaxCoefs *= fNPoints[id];
465 }
40389866 466 printf("Computing Chebyshev nodes on [%2d/%2d/%2d] grid\n",npoints[0],npoints[1],npoints[2]);
467 if (fGrid) delete[] fGrid;
0eea9d4d 468 fGrid = new Float_t [ntot];
469 //
470 int curp = 0;
471 for (int id=3;id--;) {
472 int np = fNPoints[id];
473 fGridOffs[id] = curp;
474 for (int ip=0;ip<np;ip++) {
475 Float_t x = TMath::Cos( TMath::Pi()*(ip+0.5)/np );
476 fGrid[curp++] = MapToExternal(x,id);
477 }
478 }
479 //
480}
481#endif
482
483//__________________________________________________________________________________________
484#ifdef _INC_CREATION_ALICHEB3D_
485Int_t AliCheb3D::ChebFit()
486{
487 // prepare parameterization for all output dimensions
488 int ir=0;
489 for (int i=fDimOut;i--;) ir+=ChebFit(i);
490 return ir;
491}
492#endif
493
494//__________________________________________________________________________________________
495#ifdef _INC_CREATION_ALICHEB3D_
496Int_t AliCheb3D::ChebFit(int dmOut)
497{
498 // prepare paramaterization of 3D function for dmOut-th dimension
499 int maxDim = 0;
500 for (int i=0;i<3;i++) if (maxDim<fNPoints[i]) maxDim = fNPoints[i];
501 Float_t *fvals = new Float_t [ fNPoints[0] ];
502 Float_t *tmpCoef3D = new Float_t [ fNPoints[0]*fNPoints[1]*fNPoints[2] ];
503 Float_t *tmpCoef2D = new Float_t [ fNPoints[0]*fNPoints[1] ];
504 Float_t *tmpCoef1D = new Float_t [ maxDim ];
505 //
1d18ebe0 506 Float_t rTiny = 0.1*fPrec/Float_t(maxDim); // neglect coefficient below this threshold
0eea9d4d 507 //
508 // 1D Cheb.fit for 0-th dimension at current steps of remaining dimensions
509 int ncmax = 0;
510 //
40389866 511 printf("Dim%d : 00.00%% Done",dmOut);fflush(stdout);
0eea9d4d 512 AliCheb3DCalc* cheb = GetChebCalc(dmOut);
513 //
40389866 514 float ncals2count = fNPoints[2]*fNPoints[1]*fNPoints[0];
515 float ncals = 0;
516 float frac = 0;
517 float fracStep = 0.001;
518 //
0eea9d4d 519 for (int id2=fNPoints[2];id2--;) {
520 fArgsTmp[2] = fGrid[ fGridOffs[2]+id2 ];
521 //
522 for (int id1=fNPoints[1];id1--;) {
523 fArgsTmp[1] = fGrid[ fGridOffs[1]+id1 ];
524 //
525 for (int id0=fNPoints[0];id0--;) {
526 fArgsTmp[0] = fGrid[ fGridOffs[0]+id0 ];
527 EvalUsrFunction(); // compute function values at Chebyshev roots of 0-th dimension
528 fvals[id0] = fResTmp[dmOut];
40389866 529 float fr = (++ncals)/ncals2count;
530 if (fr-frac>=fracStep) {
531 frac = fr;
532 printf("\b\b\b\b\b\b\b\b\b\b\b");
533 printf("%05.2f%% Done",fr*100);
534 fflush(stdout);
535 }
536 //
0eea9d4d 537 }
538 int nc = CalcChebCoefs(fvals,fNPoints[0], tmpCoef1D, fPrec);
539 for (int id0=fNPoints[0];id0--;) tmpCoef2D[id1 + id0*fNPoints[1]] = tmpCoef1D[id0];
540 if (ncmax<nc) ncmax = nc; // max coefs to be kept in dim0 to guarantee needed precision
541 }
542 //
543 // once each 1d slice of given 2d slice is parametrized, parametrize the Cheb.coeffs
544 for (int id0=fNPoints[0];id0--;) {
545 CalcChebCoefs( tmpCoef2D+id0*fNPoints[1], fNPoints[1], tmpCoef1D, -1);
546 for (int id1=fNPoints[1];id1--;) tmpCoef3D[id2 + fNPoints[2]*(id1+id0*fNPoints[1])] = tmpCoef1D[id1];
547 }
548 }
549 //
550 // now fit the last dimensions Cheb.coefs
551 for (int id0=fNPoints[0];id0--;) {
552 for (int id1=fNPoints[1];id1--;) {
553 CalcChebCoefs( tmpCoef3D+ fNPoints[2]*(id1+id0*fNPoints[1]), fNPoints[2], tmpCoef1D, -1);
554 for (int id2=fNPoints[2];id2--;) tmpCoef3D[id2+ fNPoints[2]*(id1+id0*fNPoints[1])] = tmpCoef1D[id2]; // store on place
555 }
556 }
557 //
558 // now find 2D surface which separates significant coefficients of 3D matrix from nonsignificant ones (up to fPrec)
1d18ebe0 559 UShort_t *tmpCoefSurf = new UShort_t[ fNPoints[0]*fNPoints[1] ];
0eea9d4d 560 for (int id0=fNPoints[0];id0--;) for (int id1=fNPoints[1];id1--;) tmpCoefSurf[id1+id0*fNPoints[1]]=0;
561 Double_t resid = 0;
562 for (int id0=fNPoints[0];id0--;) {
563 for (int id1=fNPoints[1];id1--;) {
564 for (int id2=fNPoints[2];id2--;) {
565 int id = id2 + fNPoints[2]*(id1+id0*fNPoints[1]);
566 Float_t cfa = TMath::Abs(tmpCoef3D[id]);
5406439e 567 if (cfa < rTiny) {tmpCoef3D[id] = 0; continue;} // neglect coefs below the threshold
0eea9d4d 568 resid += cfa;
569 if (resid<fPrec) continue; // this coeff is negligible
570 // otherwise go back 1 step
571 resid -= cfa;
572 tmpCoefSurf[id1+id0*fNPoints[1]] = id2+1; // how many coefs to keep
573 break;
574 }
575 }
576 }
577 /*
578 printf("\n\nCoeffs\n");
579 int cnt = 0;
580 for (int id0=0;id0<fNPoints[0];id0++) {
581 for (int id1=0;id1<fNPoints[1];id1++) {
582 for (int id2=0;id2<fNPoints[2];id2++) {
583 printf("%2d%2d%2d %+.4e |",id0,id1,id2,tmpCoef3D[cnt++]);
584 }
585 printf("\n");
586 }
587 printf("\n");
588 }
589 */
590 // see if there are rows to reject, find max.significant column at each row
5406439e 591 int nRows = fNPoints[0];
1d18ebe0 592 UShort_t *tmpCols = new UShort_t[nRows];
0eea9d4d 593 for (int id0=fNPoints[0];id0--;) {
594 int id1 = fNPoints[1];
595 while (id1>0 && tmpCoefSurf[(id1-1)+id0*fNPoints[1]]==0) id1--;
596 tmpCols[id0] = id1;
597 }
598 // find max significant row
5406439e 599 for (int id0=nRows;id0--;) {if (tmpCols[id0]>0) break; nRows--;}
0eea9d4d 600 // find max significant column and fill the permanent storage for the max sigificant column of each row
5406439e 601 cheb->InitRows(nRows); // create needed arrays;
1d18ebe0 602 UShort_t *nColsAtRow = cheb->GetNColsAtRow();
603 UShort_t *colAtRowBg = cheb->GetColAtRowBg();
5406439e 604 int nCols = 0;
0eea9d4d 605 int NElemBound2D = 0;
5406439e 606 for (int id0=0;id0<nRows;id0++) {
607 nColsAtRow[id0] = tmpCols[id0]; // number of columns to store for this row
608 colAtRowBg[id0] = NElemBound2D; // begining of this row in 2D boundary surface
0eea9d4d 609 NElemBound2D += tmpCols[id0];
5406439e 610 if (nCols<nColsAtRow[id0]) nCols = nColsAtRow[id0];
0eea9d4d 611 }
5406439e 612 cheb->InitCols(nCols);
0eea9d4d 613 delete[] tmpCols;
614 //
615 // create the 2D matrix defining the boundary of significance for 3D coeffs.matrix
616 // and count the number of siginifacnt coefficients
617 //
618 cheb->InitElemBound2D(NElemBound2D);
1d18ebe0 619 UShort_t *coefBound2D0 = cheb->GetCoefBound2D0();
620 UShort_t *coefBound2D1 = cheb->GetCoefBound2D1();
0eea9d4d 621 fMaxCoefs = 0; // redefine number of coeffs
5406439e 622 for (int id0=0;id0<nRows;id0++) {
623 int nCLoc = nColsAtRow[id0];
624 int col0 = colAtRowBg[id0];
0eea9d4d 625 for (int id1=0;id1<nCLoc;id1++) {
5406439e 626 coefBound2D0[col0 + id1] = tmpCoefSurf[id1+id0*fNPoints[1]]; // number of coefs to store for 3-d dimension
627 coefBound2D1[col0 + id1] = fMaxCoefs;
628 fMaxCoefs += coefBound2D0[col0 + id1];
0eea9d4d 629 }
630 }
631 //
632 // create final compressed 3D matrix for significant coeffs
633 cheb->InitCoefs(fMaxCoefs);
5406439e 634 Float_t *coefs = cheb->GetCoefs();
0eea9d4d 635 int count = 0;
5406439e 636 for (int id0=0;id0<nRows;id0++) {
637 int ncLoc = nColsAtRow[id0];
638 int col0 = colAtRowBg[id0];
0eea9d4d 639 for (int id1=0;id1<ncLoc;id1++) {
5406439e 640 int ncf2 = coefBound2D0[col0 + id1];
0eea9d4d 641 for (int id2=0;id2<ncf2;id2++) {
5406439e 642 coefs[count++] = tmpCoef3D[id2 + fNPoints[2]*(id1+id0*fNPoints[1])];
0eea9d4d 643 }
644 }
645 }
646 /*
647 printf("\n\nNewSurf\n");
648 for (int id0=0;id0<fNPoints[0];id0++) {
649 for (int id1=0;id1<fNPoints[1];id1++) {
650 printf("(%2d %2d) %2d |",id0,id1,tmpCoefSurf[id1+id0*fNPoints[1]]);
651 }
652 printf("\n");
653 }
654 */
655 //
656 delete[] tmpCoefSurf;
657 delete[] tmpCoef1D;
658 delete[] tmpCoef2D;
659 delete[] tmpCoef3D;
660 delete[] fvals;
661 //
40389866 662 printf("\b\b\b\b\b\b\b\b\b\b\b\b");
663 printf("100.00%% Done\n");
0eea9d4d 664 return 1;
665}
666#endif
667
668//_______________________________________________
669#ifdef _INC_CREATION_ALICHEB3D_
670void AliCheb3D::SaveData(const char* outfile,Bool_t append) const
671{
672 // writes coefficients data to output text file, optionallt appending on the end of existing file
673 TString strf = outfile;
674 gSystem->ExpandPathName(strf);
675 FILE* stream = fopen(strf,append ? "a":"w");
676 SaveData(stream);
677 fclose(stream);
678 //
679}
680#endif
681
682//_______________________________________________
683#ifdef _INC_CREATION_ALICHEB3D_
684void AliCheb3D::SaveData(FILE* stream) const
685{
686 // writes coefficients data to existing output stream
687 //
688 fprintf(stream,"\n# These are automatically generated data for the Chebyshev interpolation of 3D->%dD function\n",fDimOut);
689 fprintf(stream,"#\nSTART %s\n",GetName());
690 fprintf(stream,"# Dimensionality of the output\n%d\n",fDimOut);
691 fprintf(stream,"# Interpolation abs. precision\n%+.8e\n",fPrec);
692 //
693 fprintf(stream,"# Lower boundaries of interpolation region\n");
694 for (int i=0;i<3;i++) fprintf(stream,"%+.8e\n",fBMin[i]);
695 fprintf(stream,"# Upper boundaries of interpolation region\n");
696 for (int i=0;i<3;i++) fprintf(stream,"%+.8e\n",fBMax[i]);
40389866 697 fprintf(stream,"# Parameterization for each output dimension follows:\n");
0eea9d4d 698 //
699 for (int i=0;i<fDimOut;i++) GetChebCalc(i)->SaveData(stream);
700 fprintf(stream,"#\nEND %s\n#\n",GetName());
701 //
702}
703#endif
704
7f42fc51 705//__________________________________________________________________________________________
706#ifdef _INC_CREATION_ALICHEB3D_
707void AliCheb3D::InvertSign()
708{
709 // invert the sign of all parameterizations
710 for (int i=fDimOut;i--;) {
711 AliCheb3DCalc* par = GetChebCalc(i);
712 int ncf = par->GetNCoefs();
713 float *coefs = par->GetCoefs();
714 for (int j=ncf;j--;) coefs[j] = -coefs[j];
715 }
716}
717#endif
718
719
0eea9d4d 720//_______________________________________________
721void AliCheb3D::LoadData(const char* inpFile)
722{
5406439e 723 // load coefficients data from txt file
724 //
0eea9d4d 725 TString strf = inpFile;
726 gSystem->ExpandPathName(strf);
727 FILE* stream = fopen(strf.Data(),"r");
728 LoadData(stream);
729 fclose(stream);
730 //
731}
732
733//_______________________________________________
734void AliCheb3D::LoadData(FILE* stream)
735{
5406439e 736 // load coefficients data from stream
737 //
0eea9d4d 738 if (!stream) {Error("LoadData","No stream provided.\nStop"); exit(1);}
739 TString buffs;
740 Clear();
741 AliCheb3DCalc::ReadLine(buffs,stream);
742 if (!buffs.BeginsWith("START")) {Error("LoadData","Expected: \"START <fit_name>\", found \"%s\"\nStop\n",buffs.Data());exit(1);}
743 SetName(buffs.Data()+buffs.First(' ')+1);
744 //
745 AliCheb3DCalc::ReadLine(buffs,stream); // N output dimensions
746 fDimOut = buffs.Atoi();
747 if (fDimOut<1) {Error("LoadData","Expected: '<number_of_output_dimensions>', found \"%s\"\nStop\n",buffs.Data());exit(1);}
748 //
749 SetDimOut(fDimOut);
750 //
751 AliCheb3DCalc::ReadLine(buffs,stream); // Interpolation abs. precision
752 fPrec = buffs.Atof();
753 if (fPrec<=0) {Error("LoadData","Expected: '<abs.precision>', found \"%s\"\nStop\n",buffs.Data());exit(1);}
754 //
755 for (int i=0;i<3;i++) { // Lower boundaries of interpolation region
756 AliCheb3DCalc::ReadLine(buffs,stream);
757 fBMin[i] = buffs.Atof();
758 }
759 for (int i=0;i<3;i++) { // Upper boundaries of interpolation region
760 AliCheb3DCalc::ReadLine(buffs,stream);
761 fBMax[i] = buffs.Atof();
762 }
763 PrepareBoundaries(fBMin,fBMax);
764 //
765 // data for each output dimension
766 for (int i=0;i<fDimOut;i++) GetChebCalc(i)->LoadData(stream);
767 //
768 // check end_of_data record
769 AliCheb3DCalc::ReadLine(buffs,stream);
770 if (!buffs.BeginsWith("END") || !buffs.Contains(GetName())) {
771 Error("LoadData","Expected \"END %s\", found \"%s\".\nStop\n",GetName(),buffs.Data());
772 exit(1);
773 }
774 //
775}
776
777//_______________________________________________
5406439e 778void AliCheb3D::SetDimOut(const int d)
0eea9d4d 779{
5406439e 780 // init output dimensions
0eea9d4d 781 fDimOut = d;
782 if (fResTmp) delete fResTmp;
40389866 783 fResTmp = new Float_t[fDimOut];
0eea9d4d 784 fChebCalc.Delete();
785 for (int i=0;i<d;i++) fChebCalc.AddAtAndExpand(new AliCheb3DCalc(),i);
786}
787
788//_______________________________________________
789void AliCheb3D::ShiftBound(int id,float dif)
790{
5406439e 791 // modify the bounds of the grid
792 //
0eea9d4d 793 if (id<0||id>2) {printf("Maximum 3 dimensions are supported\n"); return;}
794 fBMin[id] += dif;
795 fBMax[id] += dif;
796 fBOffset[id] += dif;
797}
798
799//_______________________________________________
800#ifdef _INC_CREATION_ALICHEB3D_
801TH1* AliCheb3D::TestRMS(int idim,int npoints,TH1* histo)
802{
803 // fills the difference between the original function and parameterization (for idim-th component of the output)
804 // to supplied histogram. Calculations are done in npoints random points.
805 // If the hostgram was not supplied, it will be created. It is up to the user to delete it!
806 if (!fUsrMacro) {
807 printf("No user function is set\n");
808 return 0;
809 }
810 if (!histo) histo = new TH1D(GetName(),"Control: Function - Parametrization",100,-2*fPrec,2*fPrec);
811 for (int ip=npoints;ip--;) {
812 gRandom->RndmArray(3,(Float_t *)fArgsTmp);
813 for (int i=3;i--;) fArgsTmp[i] = fBMin[i] + fArgsTmp[i]*(fBMax[i]-fBMin[i]);
814 EvalUsrFunction();
815 Float_t valFun = fResTmp[idim];
816 Eval(fArgsTmp,fResTmp);
817 Float_t valPar = fResTmp[idim];
818 histo->Fill(valFun - valPar);
819 }
820 return histo;
821 //
822}
823#endif
40389866 824
825//_______________________________________________
826#ifdef _INC_CREATION_ALICHEB3D_
1d18ebe0 827
828void AliCheb3D::EstimateNPoints(float Prec, int gridBC[3][3],Int_t npd1,Int_t npd2,Int_t npd3)
829{
830 // Estimate number of points to generate a training data
831 //
832 const int kScp = 9;
833 const float kScl[9] = {0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9};
834 //
835 const float sclDim[2] = {0.001,0.999};
836 const int compDim[3][2] = { {1,2}, {2,0}, {0,1} };
837 static float xyz[3];
838 Int_t npdTst[3] = {npd1,npd2,npd3};
839 //
840
841 for (int i=3;i--;)for (int j=3;j--;) gridBC[i][j] = -1;
842 //
843 for (int idim=0;idim<3;idim++) {
844 float dimMN = fBMin[idim] + sclDim[0]*(fBMax[idim]-fBMin[idim]);
845 float dimMX = fBMin[idim] + sclDim[1]*(fBMax[idim]-fBMin[idim]);
846 //
847 int id1 = compDim[idim][0]; // 1st fixed dim
848 int id2 = compDim[idim][1]; // 2nd fixed dim
849 for (int i1=0;i1<kScp;i1++) {
850 xyz[ id1 ] = fBMin[id1] + kScl[i1]*( fBMax[id1]-fBMin[id1] );
851 for (int i2=0;i2<kScp;i2++) {
852 xyz[ id2 ] = fBMin[id2] + kScl[i2]*( fBMax[id2]-fBMin[id2] );
853 int* npt = GetNCNeeded(xyz,idim, dimMN,dimMX, Prec, npdTst[idim]); // npoints for Bx,By,Bz
854 for (int ib=0;ib<3;ib++) if (npt[ib]>gridBC[ib][idim]) gridBC[ib][idim] = npt[ib];
855 }
856 }
857 }
858}
859
860/*
40389866 861void AliCheb3D::EstimateNPoints(float Prec, int gridBC[3][3])
862{
5406439e 863 // Estimate number of points to generate a training data
864 //
40389866 865 const float sclA[9] = {0.1, 0.5, 0.9, 0.1, 0.5, 0.9, 0.1, 0.5, 0.9} ;
866 const float sclB[9] = {0.1, 0.1, 0.1, 0.5, 0.5, 0.5, 0.9, 0.9, 0.9} ;
867 const float sclDim[2] = {0.01,0.99};
868 const int compDim[3][2] = { {1,2}, {2,0}, {0,1} };
869 static float xyz[3];
870 //
871 for (int i=3;i--;)for (int j=3;j--;) gridBC[i][j] = -1;
872 //
873 for (int idim=0;idim<3;idim++) {
874 float dimMN = fBMin[idim] + sclDim[0]*(fBMax[idim]-fBMin[idim]);
875 float dimMX = fBMin[idim] + sclDim[1]*(fBMax[idim]-fBMin[idim]);
876 //
877 for (int it=0;it<9;it++) { // test in 9 points
878 int id1 = compDim[idim][0]; // 1st fixed dim
879 int id2 = compDim[idim][1]; // 2nd fixed dim
880 xyz[ id1 ] = fBMin[id1] + sclA[it]*( fBMax[id1]-fBMin[id1] );
881 xyz[ id2 ] = fBMin[id2] + sclB[it]*( fBMax[id2]-fBMin[id2] );
882 //
883 int* npt = GetNCNeeded(xyz,idim, dimMN,dimMX, Prec); // npoints for Bx,By,Bz
2572efdf 884 for (int ib=0;ib<3;ib++) if (npt[ib]>gridBC[ib][idim]) gridBC[ib][idim] = npt[ib];//+2;
40389866 885 //
886 }
887 }
888}
889
890
891int* AliCheb3D::GetNCNeeded(float xyz[3],int DimVar, float mn,float mx, float prec)
892{
2572efdf 893 // estimate needed number of chebyshev coefs for given function description in DimVar dimension
40389866 894 // The values for two other dimensions must be set beforehand
895 //
896 static int curNC[3];
897 static int retNC[3];
898 const int kMaxPoint = 400;
899 float* gridVal = new float[3*kMaxPoint];
900 float* coefs = new float[3*kMaxPoint];
901 //
902 float scale = mx-mn;
903 float offs = mn + scale/2.0;
904 scale = 2./scale;
905 //
906 int curNP;
907 int maxNC=-1;
908 int maxNCPrev=-1;
909 for (int i=0;i<3;i++) retNC[i] = -1;
910 for (int i=0;i<3;i++) fArgsTmp[i] = xyz[i];
911 //
2572efdf 912 for (curNP=3; curNP<kMaxPoint; curNP+=3) {
40389866 913 maxNCPrev = maxNC;
914 //
915 for (int i=0;i<curNP;i++) { // get function values on Cheb. nodes
916 float x = TMath::Cos( TMath::Pi()*(i+0.5)/curNP );
917 fArgsTmp[DimVar] = x/scale+offs; // map to requested interval
918 EvalUsrFunction();
919 for (int ib=3;ib--;) gridVal[ib*kMaxPoint + i] = fResTmp[ib];
920 }
921 //
922 for (int ib=0;ib<3;ib++) {
923 curNC[ib] = AliCheb3D::CalcChebCoefs(&gridVal[ib*kMaxPoint], curNP, &coefs[ib*kMaxPoint],prec);
924 if (maxNC < curNC[ib]) maxNC = curNC[ib];
925 if (retNC[ib] < curNC[ib]) retNC[ib] = curNC[ib];
926 }
927 if ( (curNP-maxNC)>3 && (maxNC-maxNCPrev)<1 ) break;
928 maxNCPrev = maxNC;
929 //
930 }
931 delete[] gridVal;
932 delete[] coefs;
933 return retNC;
934 //
935}
1d18ebe0 936*/
937
938
939int* AliCheb3D::GetNCNeeded(float xyz[3],int DimVar, float mn,float mx, float prec, Int_t npCheck)
940{
941 // estimate needed number of chebyshev coefs for given function description in DimVar dimension
942 // The values for two other dimensions must be set beforehand
943 //
944 static int retNC[3];
945 static int npChLast = 0;
946 static float *gridVal=0,*coefs=0;
947 if (npCheck<3) npCheck = 3;
948 if (npChLast<npCheck) {
949 if (gridVal) delete[] gridVal;
950 if (coefs) delete[] coefs;
951 gridVal = new float[3*npCheck];
952 coefs = new float[3*npCheck];
953 npChLast = npCheck;
954 }
955 //
956 float scale = mx-mn;
957 float offs = mn + scale/2.0;
958 scale = 2./scale;
959 //
960 for (int i=0;i<3;i++) fArgsTmp[i] = xyz[i];
961 for (int i=0;i<npCheck;i++) {
962 fArgsTmp[DimVar] = TMath::Cos( TMath::Pi()*(i+0.5)/npCheck)/scale+offs; // map to requested interval
963 EvalUsrFunction();
964 for (int ib=3;ib--;) gridVal[ib*npCheck + i] = fResTmp[ib];
965 }
966 //
967 for (int ib=0;ib<3;ib++) retNC[ib] = AliCheb3D::CalcChebCoefs(&gridVal[ib*npCheck], npCheck, &coefs[ib*npCheck],prec);
968 return retNC;
969 //
970}
971
972
40389866 973#endif