1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
21 #include <TMethodCall.h>
24 #include "AliCheb3D.h"
25 #include "AliCheb3DCalc.h"
29 //__________________________________________________________________________________________
30 AliCheb3D::AliCheb3D() :
40 for (int i=3;i--;) fBMin[i] = fBMax[i] = fBScale[i] = fBOffset[i] = 0;
43 //__________________________________________________________________________________________
44 AliCheb3D::AliCheb3D(const AliCheb3D& src) :
49 fMaxCoefs(src.fMaxCoefs),
52 fUsrFunName(src.fUsrFunName),
55 // read coefs from text file
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];
63 for (int i=0;i<fDimOut;i++) {
64 AliCheb3DCalc* cbc = src.GetChebCalc(i);
65 if (cbc) fChebCalc.AddAtAndExpand(new AliCheb3DCalc(*cbc),i);
69 //__________________________________________________________________________________________
70 AliCheb3D::AliCheb3D(const char* inpFile) :
80 // read coefs from text file
81 for (int i=3;i--;) fBMin[i] = fBMax[i] = fBScale[i] = fBOffset[i] = 0;
85 //__________________________________________________________________________________________
86 AliCheb3D::AliCheb3D(FILE* stream) :
96 // read coefs from stream
97 for (int i=3;i--;) fBMin[i] = fBMax[i] = fBScale[i] = fBOffset[i] = 0;
101 //__________________________________________________________________________________________
102 #ifdef _INC_CREATION_ALICHEB3D_
103 AliCheb3D::AliCheb3D(const char* funName, int DimOut, Float_t *bmin,Float_t *bmax, Int_t *npoints, Float_t prec) :
104 TNamed(funName,funName),
106 fPrec(TMath::Max(1.E-12f,prec)),
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
122 if (DimOut<1) {Error("AliCheb3D","Requested output dimension is %d\nStop\n",fDimOut); exit(1);}
124 PrepareBoundaries(bmin,bmax);
126 SetUsrFunction(funName);
132 //__________________________________________________________________________________________
133 #ifdef _INC_CREATION_ALICHEB3D_
134 AliCheb3D::AliCheb3D(void (*ptr)(float*,float*), int DimOut, Float_t *bmin,Float_t *bmax, Int_t *npoints, Float_t prec) :
136 fPrec(TMath::Max(1.E-12f,prec)),
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
152 if (DimOut<1) {Error("AliCheb3D","Requested output dimension is %d\nStop\n",fDimOut); exit(1);}
154 PrepareBoundaries(bmin,bmax);
162 //__________________________________________________________________________________________
163 #ifdef _INC_CREATION_ALICHEB3D_
164 AliCheb3D::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) :
166 fPrec(TMath::Max(1.E-12f,prec)),
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
184 if (DimOut<1) {Error("AliCheb3D","Requested output dimension is %d\nStop\n",fDimOut); exit(1);}
186 PrepareBoundaries(bmin,bmax);
200 //__________________________________________________________________________________________
201 #ifdef _INC_CREATION_ALICHEB3D_
202 AliCheb3D::AliCheb3D(void (*ptr)(float*,float*), int DimOut, Float_t *bmin,Float_t *bmax, Float_t prec) :
204 fPrec(TMath::Max(1.E-12f,prec)),
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
219 if (DimOut!=3) {Error("AliCheb3D","This constructor works only for 3D fits, %dD fit was requested\n",fDimOut); exit(1);}
221 PrepareBoundaries(bmin,bmax);
225 EstimateNPoints(prec,gridNC);
226 DefineGrid(gridNC[0]);
228 DefineGrid(gridNC[1]);
230 DefineGrid(gridNC[2]);
237 //__________________________________________________________________________________________
238 AliCheb3D& AliCheb3D::operator=(const AliCheb3D& rhs)
240 // assignment operator
244 fDimOut = rhs.fDimOut;
246 fMaxCoefs = rhs.fMaxCoefs;
247 fUsrFunName = rhs.fUsrFunName;
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];
256 for (int i=0;i<fDimOut;i++) {
257 AliCheb3DCalc* cbc = rhs.GetChebCalc(i);
258 if (cbc) fChebCalc.AddAtAndExpand(new AliCheb3DCalc(*cbc),i);
265 //__________________________________________________________________________________________
266 void AliCheb3D::Clear(const Option_t*)
268 // clear all dynamic structures
270 if (fResTmp) { delete[] fResTmp; fResTmp = 0; }
271 if (fGrid) { delete[] fGrid; fGrid = 0; }
272 if (fUsrMacro) { delete fUsrMacro; fUsrMacro = 0;}
277 //__________________________________________________________________________________________
278 void AliCheb3D::Print(const Option_t* opt) const
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();}
289 //__________________________________________________________________________________________
290 void AliCheb3D::PrepareBoundaries(const Float_t *bmin, const Float_t *bmax)
292 // Set and check boundaries defined by user, prepare coefficients for their conversion to [-1:1] interval
297 fBScale[i] = bmax[i]-bmin[i];
299 Error("PrepareBoundaries","Boundaries for %d-th dimension are not increasing: %+.4e %+.4e\nStop\n",i,fBMin[i],fBMax[i]);
302 fBOffset[i] = bmin[i] + fBScale[i]/2.0;
303 fBScale[i] = 2./fBScale[i];
309 //__________________________________________________________________________________________
310 #ifdef _INC_CREATION_ALICHEB3D_
312 // Pointer on user function (faster altrnative to TMethodCall)
313 void (*gUsrFunAliCheb3D) (float* ,float* );
315 void AliCheb3D::EvalUsrFunction()
317 // call user supplied function
318 if (gUsrFunAliCheb3D) gUsrFunAliCheb3D(fArgsTmp,fResTmp);
319 else fUsrMacro->Execute();
322 void AliCheb3D::SetUsrFunction(const char* name)
324 // load user macro with function definition and compile it
325 gUsrFunAliCheb3D = 0;
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 *");
338 args[0] = (long)fArgsTmp;
339 args[1] = (long)fResTmp;
340 fUsrMacro->SetParamPtrs(args);
345 //__________________________________________________________________________________________
346 #ifdef _INC_CREATION_ALICHEB3D_
347 void AliCheb3D::SetUsrFunction(void (*ptr)(float*,float*))
349 // assign user training function
351 if (fUsrMacro) delete fUsrMacro;
354 gUsrFunAliCheb3D = ptr;
358 //__________________________________________________________________________________________
359 #ifdef _INC_CREATION_ALICHEB3D_
360 void AliCheb3D::EvalUsrFunction(const Float_t *x, Float_t *res)
362 // evaluate user function value
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];
371 //__________________________________________________________________________________________
372 #ifdef _INC_CREATION_ALICHEB3D_
373 Int_t AliCheb3D::CalcChebCoefs(const Float_t *funval,int np, Float_t *outCoefs, Float_t prec)
375 // Calculate Chebyshev coeffs using precomputed function values at np roots.
376 // If prec>0, estimate the highest coeff number providing the needed precision
378 double sm; // do summations in double to minimize the roundoff error
379 for (int ic=0;ic<np;ic++) { // compute coeffs
381 for (int ir=0;ir<np;ir++) {
382 float rt = TMath::Cos( ic*(ir+0.5)*TMath::Pi()/np);
385 outCoefs[ic] = Float_t( sm * ((ic==0) ? 1./np : 2./np) );
388 if (prec<=0) return np;
392 for (cfMax=np;cfMax--;) {
393 sm += TMath::Abs(outCoefs[cfMax]);
396 if (++cfMax==0) cfMax=1;
402 //__________________________________________________________________________________________
403 #ifdef _INC_CREATION_ALICHEB3D_
404 void AliCheb3D::DefineGrid(Int_t* npoints)
406 // prepare the grid of Chebyshev roots in each dimension
407 const int kMinPoints = 1;
410 for (int id=3;id--;) {
411 fNPoints[id] = npoints[id];
412 if (fNPoints[id]<kMinPoints) {
413 Error("DefineGrid","at %d-th dimension %d point is requested, at least %d is needed\nStop\n",id,fNPoints[id],kMinPoints);
416 ntot += fNPoints[id];
417 fMaxCoefs *= fNPoints[id];
419 printf("Computing Chebyshev nodes on [%2d/%2d/%2d] grid\n",npoints[0],npoints[1],npoints[2]);
420 if (fGrid) delete[] fGrid;
421 fGrid = new Float_t [ntot];
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);
436 //__________________________________________________________________________________________
437 #ifdef _INC_CREATION_ALICHEB3D_
438 Int_t AliCheb3D::ChebFit()
440 // prepare parameterization for all output dimensions
442 for (int i=fDimOut;i--;) ir+=ChebFit(i);
447 //__________________________________________________________________________________________
448 #ifdef _INC_CREATION_ALICHEB3D_
449 Int_t AliCheb3D::ChebFit(int dmOut)
451 // prepare paramaterization of 3D function for dmOut-th dimension
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 ];
459 Float_t rTiny = fPrec/Float_t(maxDim); // neglect coefficient below this threshold
461 // 1D Cheb.fit for 0-th dimension at current steps of remaining dimensions
464 printf("Dim%d : 00.00%% Done",dmOut);fflush(stdout);
465 AliCheb3DCalc* cheb = GetChebCalc(dmOut);
467 float ncals2count = fNPoints[2]*fNPoints[1]*fNPoints[0];
470 float fracStep = 0.001;
472 for (int id2=fNPoints[2];id2--;) {
473 fArgsTmp[2] = fGrid[ fGridOffs[2]+id2 ];
475 for (int id1=fNPoints[1];id1--;) {
476 fArgsTmp[1] = fGrid[ fGridOffs[1]+id1 ];
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];
482 float fr = (++ncals)/ncals2count;
483 if (fr-frac>=fracStep) {
485 printf("\b\b\b\b\b\b\b\b\b\b\b");
486 printf("%05.2f%% Done",fr*100);
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
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];
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
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;
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]);
520 if (cfa < rTiny) {tmpCoef3D[id] = 0; continue;} // neglect coefs below the threshold
522 if (resid<fPrec) continue; // this coeff is negligible
523 // otherwise go back 1 step
525 tmpCoefSurf[id1+id0*fNPoints[1]] = id2+1; // how many coefs to keep
531 printf("\n\nCoeffs\n");
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++]);
543 // see if there are rows to reject, find max.significant column at each row
544 int nRows = fNPoints[0];
545 int *tmpCols = new int[nRows];
546 for (int id0=fNPoints[0];id0--;) {
547 int id1 = fNPoints[1];
548 while (id1>0 && tmpCoefSurf[(id1-1)+id0*fNPoints[1]]==0) id1--;
551 // find max significant row
552 for (int id0=nRows;id0--;) {if (tmpCols[id0]>0) break; nRows--;}
553 // find max significant column and fill the permanent storage for the max sigificant column of each row
554 cheb->InitRows(nRows); // create needed arrays;
555 int *nColsAtRow = cheb->GetNColsAtRow();
556 int *colAtRowBg = cheb->GetColAtRowBg();
558 int NElemBound2D = 0;
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
562 NElemBound2D += tmpCols[id0];
563 if (nCols<nColsAtRow[id0]) nCols = nColsAtRow[id0];
565 cheb->InitCols(nCols);
568 // create the 2D matrix defining the boundary of significance for 3D coeffs.matrix
569 // and count the number of siginifacnt coefficients
571 cheb->InitElemBound2D(NElemBound2D);
572 int *coefBound2D0 = cheb->GetCoefBound2D0();
573 int *coefBound2D1 = cheb->GetCoefBound2D1();
574 fMaxCoefs = 0; // redefine number of coeffs
575 for (int id0=0;id0<nRows;id0++) {
576 int nCLoc = nColsAtRow[id0];
577 int col0 = colAtRowBg[id0];
578 for (int id1=0;id1<nCLoc;id1++) {
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];
585 // create final compressed 3D matrix for significant coeffs
586 cheb->InitCoefs(fMaxCoefs);
587 Float_t *coefs = cheb->GetCoefs();
589 for (int id0=0;id0<nRows;id0++) {
590 int ncLoc = nColsAtRow[id0];
591 int col0 = colAtRowBg[id0];
592 for (int id1=0;id1<ncLoc;id1++) {
593 int ncf2 = coefBound2D0[col0 + id1];
594 for (int id2=0;id2<ncf2;id2++) {
595 coefs[count++] = tmpCoef3D[id2 + fNPoints[2]*(id1+id0*fNPoints[1])];
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]]);
609 delete[] tmpCoefSurf;
615 printf("\b\b\b\b\b\b\b\b\b\b\b\b");
616 printf("100.00%% Done\n");
621 //_______________________________________________
622 #ifdef _INC_CREATION_ALICHEB3D_
623 void AliCheb3D::SaveData(const char* outfile,Bool_t append) const
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");
635 //_______________________________________________
636 #ifdef _INC_CREATION_ALICHEB3D_
637 void AliCheb3D::SaveData(FILE* stream) const
639 // writes coefficients data to existing output stream
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);
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]);
650 fprintf(stream,"# Parameterization for each output dimension follows:\n");
652 for (int i=0;i<fDimOut;i++) GetChebCalc(i)->SaveData(stream);
653 fprintf(stream,"#\nEND %s\n#\n",GetName());
658 //_______________________________________________
659 void AliCheb3D::LoadData(const char* inpFile)
661 // load coefficients data from txt file
663 TString strf = inpFile;
664 gSystem->ExpandPathName(strf);
665 FILE* stream = fopen(strf.Data(),"r");
671 //_______________________________________________
672 void AliCheb3D::LoadData(FILE* stream)
674 // load coefficients data from stream
676 if (!stream) {Error("LoadData","No stream provided.\nStop"); exit(1);}
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);
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);}
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);}
693 for (int i=0;i<3;i++) { // Lower boundaries of interpolation region
694 AliCheb3DCalc::ReadLine(buffs,stream);
695 fBMin[i] = buffs.Atof();
697 for (int i=0;i<3;i++) { // Upper boundaries of interpolation region
698 AliCheb3DCalc::ReadLine(buffs,stream);
699 fBMax[i] = buffs.Atof();
701 PrepareBoundaries(fBMin,fBMax);
703 // data for each output dimension
704 for (int i=0;i<fDimOut;i++) GetChebCalc(i)->LoadData(stream);
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());
715 //_______________________________________________
716 void AliCheb3D::SetDimOut(const int d)
718 // init output dimensions
720 if (fResTmp) delete fResTmp;
721 fResTmp = new Float_t[fDimOut];
723 for (int i=0;i<d;i++) fChebCalc.AddAtAndExpand(new AliCheb3DCalc(),i);
726 //_______________________________________________
727 void AliCheb3D::ShiftBound(int id,float dif)
729 // modify the bounds of the grid
731 if (id<0||id>2) {printf("Maximum 3 dimensions are supported\n"); return;}
737 //_______________________________________________
738 #ifdef _INC_CREATION_ALICHEB3D_
739 TH1* AliCheb3D::TestRMS(int idim,int npoints,TH1* histo)
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!
745 printf("No user function is set\n");
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]);
753 Float_t valFun = fResTmp[idim];
754 Eval(fArgsTmp,fResTmp);
755 Float_t valPar = fResTmp[idim];
756 histo->Fill(valFun - valPar);
763 //_______________________________________________
764 #ifdef _INC_CREATION_ALICHEB3D_
765 void AliCheb3D::EstimateNPoints(float Prec, int gridBC[3][3])
767 // Estimate number of points to generate a training data
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} };
775 for (int i=3;i--;)for (int j=3;j--;) gridBC[i][j] = -1;
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]);
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] );
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;
795 int* AliCheb3D::GetNCNeeded(float xyz[3],int DimVar, float mn,float mx, float prec)
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
802 const int kMaxPoint = 400;
803 float* gridVal = new float[3*kMaxPoint];
804 float* coefs = new float[3*kMaxPoint];
807 float offs = mn + scale/2.0;
813 for (int i=0;i<3;i++) retNC[i] = -1;
814 for (int i=0;i<3;i++) fArgsTmp[i] = xyz[i];
816 for (curNP=5; curNP<kMaxPoint; curNP+=5) {
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
823 for (int ib=3;ib--;) gridVal[ib*kMaxPoint + i] = fResTmp[ib];
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];
831 if ( (curNP-maxNC)>3 && (maxNC-maxNCPrev)<1 ) break;