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