]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliCheb3D.cxx
Possibility to fix some of the parameters. New method to get the number of free param...
[u/mrichter/AliRoot.git] / STEER / AliCheb3D.cxx
index 41246b2efd90580259b250508c4550b396d6d8ea..1f6e65cb1410f1473161ebef437523fa6e2d6794 100644 (file)
@@ -1,22 +1,5 @@
-/**************************************************************************
- * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
- *                                                                        *
- * Author: The ALICE Off-line Project.                                    *
- * Contributors are mentioned in the code where appropriate.              *
- *                                                                        *
- * Permission to use, copy, modify and distribute this software and its   *
- * documentation strictly for non-commercial purposes is hereby granted   *
- * without fee, provided that the above copyright notice appears in all   *
- * copies and that both the copyright notice and this permission notice   *
- * appear in the supporting documentation. The authors make no claims     *
- * about the suitability of this software for any purpose. It is          *
- * provided "as is" without express or implied warranty.                  *
- **************************************************************************/
-
-/* $Id$ */
-
 // Author: ruben.shahoyan@cern.ch   09/09/2006
-//
+
 ////////////////////////////////////////////////////////////////////////////////
 //                                                                            //
 // AliCheb3D produces the interpolation of the user 3D->NDimOut arbitrary     //
 
 #include <TString.h>
 #include <TSystem.h>
-#include <TRandom.h>
 #include <TROOT.h>
+#include <TRandom.h>
 #include "AliCheb3D.h"
-#include "AliLog.h"
-
-
-
-ClassImp(AliCheb3DCalc)
-
-
-AliCheb3DCalc::AliCheb3DCalc():
-    TNamed("", ""),
-    fNCoefs(0),  
-    fNRows(0),
-    fNCols(0),
-    fNElemBound2D(0),
-    fNColsAtRow(0),
-    fColAtRowBg(0),
-    fCoefBound2D0(0),
-    fCoefBound2D1(0),
-    fCoefs(0),
-    fTmpCf1(0),
-    fTmpCf0(0)
-{
-    // Default constructor
-    Init0();
-}
-
-AliCheb3DCalc::AliCheb3DCalc(FILE* stream):
-    TNamed("", ""),
-    fNCoefs(0),  
-    fNRows(0),
-    fNCols(0),
-    fNElemBound2D(0),
-    fNColsAtRow(0),
-    fColAtRowBg(0),
-    fCoefBound2D0(0),
-    fCoefBound2D1(0),
-    fCoefs(0),
-    fTmpCf1(0),
-    fTmpCf0(0)
-{
-    // Default constructor
-    Init0();
-    LoadData(stream);
-}
 
-AliCheb3DCalc::AliCheb3DCalc(const AliCheb3DCalc& cheb):
-    TNamed("", ""),
-    fNCoefs(0),  
-    fNRows(0),
-    fNCols(0),
-    fNElemBound2D(0),
-    fNColsAtRow(0),
-    fColAtRowBg(0),
-    fCoefBound2D0(0),
-    fCoefBound2D1(0),
-    fCoefs(0),
-    fTmpCf1(0),
-    fTmpCf0(0)
-{
-    // Copy constructor
-    cheb.Copy(*this);
-}
 
-void AliCheb3DCalc::Copy(TObject &) const
-{
-  //dummy Copy function
-  AliFatal("Not implemented!");
-}
 
-//__________________________________________________________________________________________
-void AliCheb3DCalc::Clear(Option_t*)
-{
-  // delete all dynamycally allocated structures
-  if (fTmpCf1)       { delete[] fTmpCf1;  fTmpCf1 = 0;}
-  if (fTmpCf0)       { delete[] fTmpCf0;  fTmpCf0 = 0;}
-  if (fCoefs)        { delete[] fCoefs;   fCoefs  = 0;}
-  if (fCoefBound2D0) { delete[] fCoefBound2D0; fCoefBound2D0 = 0; }
-  if (fCoefBound2D1) { delete[] fCoefBound2D1; fCoefBound2D1 = 0; }
-  if (fNColsAtRow)   { delete[] fNColsAtRow;   fNColsAtRow = 0; }
-  if (fColAtRowBg)   { delete[] fColAtRowBg;   fColAtRowBg = 0; }
-  //
-}
-
-//__________________________________________________________________________________________
-void AliCheb3DCalc::Init0()
-{
-  // reset everything to 0
-  fNCoefs = fNRows = fNCols = fNElemBound2D = 0;
-  fCoefs = 0;
-  fCoefBound2D0 = fCoefBound2D1 = 0;
-  fNColsAtRow = fColAtRowBg = 0;
-  fTmpCf0 = fTmpCf1 = 0;
-}
+ClassImp(AliCheb3D)
 
 //__________________________________________________________________________________________
-void AliCheb3DCalc::Print(Option_t* ) const
+AliCheb3D::AliCheb3D() : 
+  fDimOut(0), 
+  fPrec(0), 
+  fChebCalc(1), 
+  fMaxCoefs(0), 
+  fResTmp(0), 
+  fGrid(0), 
+  fUsrFunName(""), 
+  fUsrMacro(0) 
 {
-  printf("Chebyshev parameterization data %s for 3D->1 function.\n",GetName());
-  int nmax3d = 0; 
-  for (int i=fNElemBound2D;i--;) if (fCoefBound2D0[i]>nmax3d) nmax3d = fCoefBound2D0[i];
-  printf("%d coefficients in %dx%dx%d matrix\n",fNCoefs,fNRows,fNCols,nmax3d);
-  //
+  for (int i=3;i--;) fBMin[i] = fBMax[i] = fBScale[i] = fBOffset[i] = 0;
 }
 
 //__________________________________________________________________________________________
-Float_t  AliCheb3DCalc::Eval(Float_t  *par) const
-{
-  // evaluate Chebyshev parameterization for 3D function.
-  // VERY IMPORTANT: par must contain the function arguments ALREADY MAPPED to [-1:1] interval
-  Float_t  &z = par[2];
-  Float_t  &y = par[1];
-  Float_t  &x = par[0];
-  //
-  int ncfRC;
-  for (int id0=fNRows;id0--;) {
-    int nCLoc = fNColsAtRow[id0];                   // number of significant coefs on this row
-    int Col0  = fColAtRowBg[id0];                   // beginning of local column in the 2D boundary matrix
-    for (int id1=nCLoc;id1--;) {
-      int id = id1+Col0;
-      fTmpCf1[id1] = (ncfRC=fCoefBound2D0[id]) ? ChebEval1D(z,fCoefs + fCoefBound2D1[id], ncfRC) : 0.0;
-    }
-    fTmpCf0[id0] = nCLoc>0 ? ChebEval1D(y,fTmpCf1,nCLoc):0.0;
-  }
-  return ChebEval1D(x,fTmpCf0,fNRows);
-  //
-}
-
-//_______________________________________________
-#ifdef _INC_CREATION_ALICHEB3D_
-void AliCheb3DCalc::SaveData(const char* outfile,Bool_t append) const
-{
-  // writes coefficients data to output text file, optionallt appending on the end of existing file
-  TString strf = outfile;
-  gSystem->ExpandPathName(strf);
-  FILE* stream = fopen(strf,append ? "a":"w");
-  SaveData(stream);
-  fclose(stream);
-  //
-}
-#endif
-
-//_______________________________________________
-#ifdef _INC_CREATION_ALICHEB3D_
-void AliCheb3DCalc::SaveData(FILE* stream) const
-{
-  // writes coefficients data to existing output stream
-  // Note: fNCols, fNElemBound2D and fColAtRowBg is not stored, will be computed on fly during the loading of this file
-  fprintf(stream,"#\nSTART %s\n",GetName());
-  fprintf(stream,"# Number of rows\n%d\n",fNRows);
-  //
-  fprintf(stream,"# Number of columns per row\n");
-  for (int i=0;i<fNRows;i++) fprintf(stream,"%d\n",fNColsAtRow[i]);
-  //
-  fprintf(stream,"# Number of Coefs in each significant block of third dimension\n");
-  for (int i=0;i<fNElemBound2D;i++) fprintf(stream,"%d\n",fCoefBound2D0[i]);
-  //
-  fprintf(stream,"# Coefficients\n");
-  for (int i=0;i<fNCoefs;i++) fprintf(stream,"%+.8e\n",fCoefs[i]);
-  fprintf(stream,"END %s\n",GetName());
-  //
-}
-#endif
-
-//_______________________________________________
-void AliCheb3DCalc::LoadData(FILE* stream)
-{
-  // Load coefs. from the stream 
-  if (!stream) {Error("LoadData","No stream provided.\nStop"); exit(1);}
-  TString buffs;
-  Clear();
-  ReadLine(buffs,stream);
-  if (!buffs.BeginsWith("START")) {Error("LoadData","Expected: \"START <fit_name>\", found \"%s\"\nStop\n",buffs.Data());exit(1);}
-  if (buffs.First(' ')>0) SetName(buffs.Data()+buffs.First(' ')+1);
-  //
-  ReadLine(buffs,stream); // NRows
-  fNRows = buffs.Atoi(); 
-  if (fNRows<1) {Error("LoadData","Expected: '<number_of_rows>', found \"%s\"\nStop\n",buffs.Data());exit(1);}
-  //
-  fNCols = 0;
-  fNElemBound2D = 0;
-  InitRows(fNRows);
-  //
-  for (int id0=0;id0<fNRows;id0++) {
-    ReadLine(buffs,stream);               // n.cols at this row
-    fNColsAtRow[id0] = buffs.Atoi();
-    fColAtRowBg[id0] = fNElemBound2D;     // begining of this row in 2D boundary surface
-    fNElemBound2D   += fNColsAtRow[id0];
-    if (fNCols<fNColsAtRow[id0]) fNCols = fNColsAtRow[id0];
-  }
-  InitCols(fNCols);
-  //  
-  fNCoefs = 0; 
-  InitElemBound2D(fNElemBound2D);
-  //
-  for (int i=0;i<fNElemBound2D;i++) {
-    ReadLine(buffs,stream);               // n.coeffs at 3-d dimension for the given column/row
-    fCoefBound2D0[i] = buffs.Atoi();
-    fCoefBound2D1[i] = fNCoefs;
-    fNCoefs += fCoefBound2D0[i];
-  }
-  //
-  if (fNCoefs<=0) {Error("LoadData","Negtive (%d) number of Chebychef coeffs. is obtained.\nStop\n",fNCoefs);exit(1);}
-  //
-  InitCoefs(fNCoefs);
-  for (int i=0;i<fNCoefs;i++) {
-    ReadLine(buffs,stream);
-    fCoefs[i] = buffs.Atof();
-  }
-  // check end_of_data record
-  ReadLine(buffs,stream);
-  if (!buffs.BeginsWith("END") || !buffs.Contains(GetName())) {
-    Error("LoadData","Expected \"END %s\", found \"%s\".\nStop\n",GetName(),buffs.Data());
-    exit(1);
+AliCheb3D::AliCheb3D(const AliCheb3D& src) : 
+  TNamed(src),
+  fDimOut(src.fDimOut), 
+  fPrec(src.fPrec), 
+  fChebCalc(1), 
+  fMaxCoefs(src.fMaxCoefs), 
+  fResTmp(0),
+  fGrid(0), 
+  fUsrFunName(src.fUsrFunName), 
+  fUsrMacro(0)
+{
+  // read coefs from text file
+  for (int i=3;i--;) {
+    fBMin[i]    = src.fBMin[i];
+    fBMax[i]    = src.fBMax[i];
+    fBScale[i]  = src.fBScale[i];
+    fBOffset[i] = src.fBOffset[i];
+    fNPoints[i] = src.fNPoints[i];
   }
-  //
-}
-
-//_______________________________________________
-void AliCheb3DCalc::ReadLine(TString& str,FILE* stream) 
-{
-  // read single line from the stream, skipping empty and commented lines. EOF is not expected
-  while (str.Gets(stream)) {
-    str = str.Strip(TString::kBoth,' ');
-    if (str.IsNull()||str.BeginsWith("#")) continue;
-    return;
+  for (int i=0;i<fDimOut;i++) {
+    AliCheb3DCalc* cbc = src.GetChebCalc(i);
+    if (cbc) fChebCalc.AddAtAndExpand(new AliCheb3DCalc(*cbc),i);
   }
-  fprintf(stderr,"AliCheb3D::ReadLine: Failed to read from stream.\nStop");exit(1); // normally, should not reach here
-}
-
-//_______________________________________________
-void AliCheb3DCalc::InitCols(int nc)
-{
-  // Set max.number of significant columns in the coefs matrix
-  fNCols = nc;
-  if (fTmpCf1) delete[] fTmpCf1;
-  fTmpCf1 = new Float_t [fNCols];
-}
-
-//_______________________________________________
-void AliCheb3DCalc::InitRows(int nr)
-{
-  // Set max.number of significant rows in the coefs matrix
-  if (fNColsAtRow) delete[] fNColsAtRow;
-  if (fColAtRowBg) delete[] fColAtRowBg;
-  if (fTmpCf0)     delete[] fTmpCf0;
-  fNRows = nr;
-  fNColsAtRow = new Int_t[fNRows];
-  fTmpCf0     = new Float_t [fNRows];
-  fColAtRowBg = new Int_t[fNRows];
-  for (int i=fNRows;i--;) fNColsAtRow[i] = fColAtRowBg[i] = 0;
-}
-
-//_______________________________________________
-void AliCheb3DCalc::InitElemBound2D(int ne)
-{
-  // Set max number of significant coefs for given row/column of coefs 3D matrix
-  if (fCoefBound2D0) delete[] fCoefBound2D0; 
-  if (fCoefBound2D1) delete[] fCoefBound2D1; 
-  fNElemBound2D = ne;
-  fCoefBound2D0 = new Int_t[fNElemBound2D];
-  fCoefBound2D1 = new Int_t[fNElemBound2D];
-  for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = fCoefBound2D1[i] = 0;
-}
-
-//_______________________________________________
-void AliCheb3DCalc::InitCoefs(int nc)
-{
-  // Set total number of significant coefs
-  if (fCoefs) delete[] fCoefs; 
-  fNCoefs = nc;
-  fCoefs = new Float_t [fNCoefs];
-  for (int i=fNCoefs;i--;) fCoefs[i] = 0.0;
-}
-
-
-
-
-ClassImp(AliCheb3D)
-
-AliCheb3D::AliCheb3D():
-    TNamed("", ""),
-    fDimOut(0),
-    fPrec(0.),
-    fChebCalc(),
-    fMaxCoefs(0),
-    fResTmp(0),
-    fGrid(0),
-    fUsrFunName(),
-    fUsrMacro(0)            
-{
-    // Default constructor
-    Init0();
 }
 
-AliCheb3D::AliCheb3D(const char* inputFile):
-    TNamed("", ""),
-    fDimOut(0),
-    fPrec(0.),
-    fChebCalc(),
-    fMaxCoefs(0),
-    fResTmp(0),
-    fGrid(0),
-    fUsrFunName(),
-    fUsrMacro(0)            
-{
-    // Default constructor
-    Init0();
-    LoadData(inputFile);
-}
-
-
-
-AliCheb3D::AliCheb3D(FILE* stream):
-    TNamed("", ""),
-    fDimOut(0),
-    fPrec(0.),
-    fChebCalc(),
-    fMaxCoefs(0),
-    fResTmp(0),
-    fGrid(0),
-    fUsrFunName(),
-    fUsrMacro(0)            
-{
-    // Default constructor
-    Init0();
-    LoadData(stream);
+//__________________________________________________________________________________________
+AliCheb3D::AliCheb3D(const char* inpFile) : 
+  fDimOut(0), 
+  fPrec(0),  
+  fChebCalc(1),
+  fMaxCoefs(0),  
+  fResTmp(0),
+  fGrid(0), 
+  fUsrFunName(""), 
+  fUsrMacro(0)
+{
+  // read coefs from text file
+  for (int i=3;i--;) fBMin[i] = fBMax[i] = fBScale[i] = fBOffset[i] = 0;
+  LoadData(inpFile);
 }
 
-AliCheb3D::AliCheb3D(const AliCheb3D& cheb):
-    TNamed("", ""),
-    fDimOut(0),
-    fPrec(0.),
-    fChebCalc(),
-    fMaxCoefs(0),
-    fResTmp(0),
-    fGrid(0),
-    fUsrFunName(),
-    fUsrMacro(0)            
-{
-    // Copy constructor
-    cheb.Copy(*this);
+//__________________________________________________________________________________________
+AliCheb3D::AliCheb3D(FILE* stream) : 
+  fDimOut(0), 
+  fPrec(0), 
+  fChebCalc(1), 
+  fMaxCoefs(0),
+  fResTmp(0),
+  fGrid(0),
+  fUsrFunName(""),
+  fUsrMacro(0)
+{
+  // read coefs from stream
+  for (int i=3;i--;) fBMin[i] = fBMax[i] = fBScale[i] = fBOffset[i] = 0;
+  LoadData(stream);
 }
 
 //__________________________________________________________________________________________
 #ifdef _INC_CREATION_ALICHEB3D_
-AliCheb3D::AliCheb3D(const char* funName, int DimOut, Float_t  *bmin,Float_t  *bmax, Int_t *npoints, Float_t prec) : TNamed(funName,funName)
+AliCheb3D::AliCheb3D(const char* funName, int DimOut, Float_t  *bmin,Float_t  *bmax, Int_t *npoints, Float_t prec) : 
+  TNamed(funName,funName), 
+  fDimOut(0), 
+  fPrec(TMath::Max(1.E-12f,prec)), 
+  fChebCalc(1), 
+  fMaxCoefs(0), 
+  fResTmp(0), 
+  fGrid(0), 
+  fUsrFunName("") ,
+  fUsrMacro(0)
 {
   // Construct the parameterization for the function
   // funName : name of the file containing the function: void funName(Float_t * inp,Float_t * out)
@@ -436,8 +163,6 @@ AliCheb3D::AliCheb3D(const char* funName, int DimOut, Float_t  *bmin,Float_t  *b
   // npoints : array of 3 elements with the number of points to compute in each of 3 dimension
   // prec    : max allowed absolute difference between the user function and computed parameterization on the requested grid
   //
-  Init0();
-  fPrec = TMath::Max(1.E-12f,prec);
   if (DimOut<1) {Error("AliCheb3D","Requested output dimension is %d\nStop\n",fDimOut); exit(1);}
   SetDimOut(DimOut);
   PrepareBoundaries(bmin,bmax);
@@ -450,7 +175,15 @@ AliCheb3D::AliCheb3D(const char* funName, int DimOut, Float_t  *bmin,Float_t  *b
 
 //__________________________________________________________________________________________
 #ifdef _INC_CREATION_ALICHEB3D_
-AliCheb3D::AliCheb3D(void (*ptr)(float*,float*), int DimOut, Float_t  *bmin,Float_t  *bmax, Int_t *npoints, Float_t prec) : TNamed("AliCheb3D","AliCheb3D")
+AliCheb3D::AliCheb3D(void (*ptr)(float*,float*), int DimOut, Float_t  *bmin,Float_t  *bmax, Int_t *npoints, Float_t prec) : 
+  fDimOut(0), 
+  fPrec(TMath::Max(1.E-12f,prec)), 
+  fChebCalc(1), 
+  fMaxCoefs(0), 
+  fResTmp(0), 
+  fGrid(0), 
+  fUsrFunName(""),
+  fUsrMacro(0)
 {
   // Construct the parameterization for the function
   // ptr     : pointer on the function: void fun(Float_t * inp,Float_t * out)
@@ -460,8 +193,6 @@ AliCheb3D::AliCheb3D(void (*ptr)(float*,float*), int DimOut, Float_t  *bmin,Floa
   // npoints : array of 3 elements with the number of points to compute in each of 3 dimension
   // prec    : max allowed absolute difference between the user function and computed parameterization on the requested grid
   //
-  Init0();
-  fPrec = TMath::Max(1.E-12f,prec);
   if (DimOut<1) {Error("AliCheb3D","Requested output dimension is %d\nStop\n",fDimOut); exit(1);}
   SetDimOut(DimOut);
   PrepareBoundaries(bmin,bmax);
@@ -472,11 +203,105 @@ AliCheb3D::AliCheb3D(void (*ptr)(float*,float*), int DimOut, Float_t  *bmin,Floa
 }
 #endif
 
+//__________________________________________________________________________________________
+#ifdef _INC_CREATION_ALICHEB3D_
+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) : 
+  fDimOut(0), 
+  fPrec(TMath::Max(1.E-12f,prec)), 
+  fChebCalc(1), 
+  fMaxCoefs(0), 
+  fResTmp(0), 
+  fGrid(0), 
+  fUsrFunName(""),
+  fUsrMacro(0)
+{
+  // Construct very economic  parameterization for the function
+  // ptr     : pointer on the function: void fun(Float_t * inp,Float_t * out)
+  // DimOut  : dimension of the vector computed by the user function
+  // bmin    : array of 3 elements with the lower boundaries of the region where the function is defined
+  // bmax    : array of 3 elements with the upper boundaries of the region where the function is defined
+  // npX     : array of 3 elements with the number of points to compute in each dimension for 1st component 
+  // npY     : array of 3 elements with the number of points to compute in each dimension for 2nd component 
+  // npZ     : array of 3 elements with the number of points to compute in each dimension for 3d  component 
+  // prec    : max allowed absolute difference between the user function and computed parameterization on the requested grid
+  //
+  if (DimOut<1) {Error("AliCheb3D","Requested output dimension is %d\nStop\n",fDimOut); exit(1);}
+  SetDimOut(DimOut);
+  PrepareBoundaries(bmin,bmax);
+  SetUsrFunction(ptr);
+  //
+  DefineGrid(npX);
+  ChebFit(0);
+  DefineGrid(npY);
+  ChebFit(1);
+  DefineGrid(npZ);
+  ChebFit(2);
+  //
+}
+#endif
 
-void AliCheb3D::Copy(TObject &) const
-{
-  //dummy Copy function
-  AliFatal("Not implemented!");
+
+//__________________________________________________________________________________________
+#ifdef _INC_CREATION_ALICHEB3D_
+AliCheb3D::AliCheb3D(void (*ptr)(float*,float*), int DimOut, Float_t  *bmin,Float_t  *bmax, Float_t prec) : 
+  fDimOut(0), 
+  fPrec(TMath::Max(1.E-12f,prec)), 
+  fChebCalc(1), 
+  fMaxCoefs(0), 
+  fResTmp(0), 
+  fGrid(0), 
+  fUsrFunName(""),
+  fUsrMacro(0)
+{
+  // Construct very economic  parameterization for the function with automatic calculation of the root's grid
+  // ptr     : pointer on the function: void fun(Float_t * inp,Float_t * out)
+  // DimOut  : dimension of the vector computed by the user function
+  // bmin    : array of 3 elements with the lower boundaries of the region where the function is defined
+  // bmax    : array of 3 elements with the upper boundaries of the region where the function is defined
+  // prec    : max allowed absolute difference between the user function and computed parameterization on the requested grid
+  //
+  if (DimOut!=3) {Error("AliCheb3D","This constructor works only for 3D fits, %dD fit was requested\n",fDimOut); exit(1);}
+  SetDimOut(DimOut);
+  PrepareBoundaries(bmin,bmax);
+  SetUsrFunction(ptr);
+  //
+  int gridNC[3][3];
+  EstimateNPoints(prec,gridNC);
+  DefineGrid(gridNC[0]);
+  ChebFit(0);
+  DefineGrid(gridNC[1]);
+  ChebFit(1);
+  DefineGrid(gridNC[2]);
+  ChebFit(2);
+  //
+}
+#endif
+
+
+//__________________________________________________________________________________________
+AliCheb3D& AliCheb3D::operator=(const AliCheb3D& rhs)
+{
+  if (this != &rhs) {
+    Clear();
+    fDimOut   = rhs.fDimOut;
+    fPrec     = rhs.fPrec;
+    fMaxCoefs = rhs.fMaxCoefs;
+    fUsrFunName = rhs.fUsrFunName;
+    fUsrMacro   = 0;
+    for (int i=3;i--;) {
+      fBMin[i]    = rhs.fBMin[i];
+      fBMax[i]    = rhs.fBMax[i];
+      fBScale[i]  = rhs.fBScale[i];
+      fBOffset[i] = rhs.fBOffset[i];
+      fNPoints[i] = rhs.fNPoints[i];
+    } 
+    for (int i=0;i<fDimOut;i++) {
+      AliCheb3DCalc* cbc = rhs.GetChebCalc(i);
+      if (cbc) fChebCalc.AddAtAndExpand(new AliCheb3DCalc(*cbc),i);
+    }    
+  }
+  return *this;
+  //
 }
 
 //__________________________________________________________________________________________
@@ -499,20 +324,6 @@ void AliCheb3D::Print(Option_t* opt) const
   //
 }
 
-//__________________________________________________________________________________________
-void AliCheb3D::Init0()
-{
-  for (int i=3;i--;) fBMin[i] = fBMax[i] = fBScale[i] = fBOffset[i] = 0;
-  fMaxCoefs = 0;
-  fGrid = 0;
-  fResTmp = 0;
-  fUsrFunName = "";
-  fUsrMacro = 0;
-#ifdef _INC_CREATION_ALICHEB3D_
-  gUsrFunAliCheb3D = 0;
-#endif
-}
-
 //__________________________________________________________________________________________
 void AliCheb3D::PrepareBoundaries(Float_t  *bmin,Float_t  *bmax)
 {
@@ -532,8 +343,20 @@ void AliCheb3D::PrepareBoundaries(Float_t  *bmin,Float_t  *bmax)
   //
 }
 
+
 //__________________________________________________________________________________________
 #ifdef _INC_CREATION_ALICHEB3D_
+
+// Pointer on user function (faster altrnative to TMethodCall)
+void (*gUsrFunAliCheb3D) (float* ,float* );
+
+void AliCheb3D::EvalUsrFunction() 
+{
+  // call user supplied function
+  if   (gUsrFunAliCheb3D) gUsrFunAliCheb3D(fArgsTmp,fResTmp);
+  else fUsrMacro->Execute(); 
+}
+
 void AliCheb3D::SetUsrFunction(const char* name)
 {
   // load user macro with function definition and compile it
@@ -620,12 +443,14 @@ void AliCheb3D::DefineGrid(Int_t* npoints)
   for (int id=3;id--;) { 
     fNPoints[id] = npoints[id];
     if (fNPoints[id]<kMinPoints) {
-      Error("DefineGrid","at %d-th dimension %d point is requested, at least %d is needed\nStop\n",fNPoints[id],kMinPoints);
+      Error("DefineGrid","at %d-th dimension %d point is requested, at least %d is needed\nStop\n",id,fNPoints[id],kMinPoints);
       exit(1);
     }
     ntot += fNPoints[id];
     fMaxCoefs *= fNPoints[id];
   }
+  printf("Computing Chebyshev nodes on [%2d/%2d/%2d] grid\n",npoints[0],npoints[1],npoints[2]);
+  if (fGrid) delete[] fGrid;
   fGrid = new Float_t [ntot];
   //
   int curp = 0;
@@ -669,8 +494,14 @@ Int_t AliCheb3D::ChebFit(int dmOut)
   // 1D Cheb.fit for 0-th dimension at current steps of remaining dimensions
   int ncmax = 0;
   //
+  printf("Dim%d : 00.00%% Done",dmOut);fflush(stdout);
   AliCheb3DCalc* cheb =  GetChebCalc(dmOut);
   //
+  float ncals2count = fNPoints[2]*fNPoints[1]*fNPoints[0];
+  float ncals = 0;
+  float frac = 0;
+  float fracStep = 0.001;
+  //
   for (int id2=fNPoints[2];id2--;) {
     fArgsTmp[2] = fGrid[ fGridOffs[2]+id2 ];
     //
@@ -681,6 +512,14 @@ Int_t AliCheb3D::ChebFit(int dmOut)
        fArgsTmp[0] = fGrid[ fGridOffs[0]+id0 ];
        EvalUsrFunction();     // compute function values at Chebyshev roots of 0-th dimension
        fvals[id0] =  fResTmp[dmOut];
+       float fr = (++ncals)/ncals2count;
+       if (fr-frac>=fracStep) {
+         frac = fr;
+         printf("\b\b\b\b\b\b\b\b\b\b\b");
+         printf("%05.2f%% Done",fr*100);
+         fflush(stdout);
+       }
+       //
       }
       int nc = CalcChebCoefs(fvals,fNPoints[0], tmpCoef1D, fPrec);
       for (int id0=fNPoints[0];id0--;) tmpCoef2D[id1 + id0*fNPoints[1]] = tmpCoef1D[id0];
@@ -711,8 +550,7 @@ Int_t AliCheb3D::ChebFit(int dmOut)
       for (int id2=fNPoints[2];id2--;) {
        int id = id2 + fNPoints[2]*(id1+id0*fNPoints[1]);
        Float_t  cfa = TMath::Abs(tmpCoef3D[id]);
-       if (cfa < RTiny) {tmpCoef3D[id] = 0; continue;} // neglect coeefs below the threshold
-
+       if (cfa < RTiny) {tmpCoef3D[id] = 0; continue;} // neglect coefs below the threshold
        resid += cfa;
        if (resid<fPrec) continue; // this coeff is negligible
        // otherwise go back 1 step
@@ -807,6 +645,8 @@ Int_t AliCheb3D::ChebFit(int dmOut)
   delete[] tmpCoef3D;
   delete[] fvals;
   //
+  printf("\b\b\b\b\b\b\b\b\b\b\b\b");
+  printf("100.00%% Done\n");
   return 1;
 }
 #endif
@@ -840,7 +680,7 @@ void AliCheb3D::SaveData(FILE* stream) const
   for (int i=0;i<3;i++) fprintf(stream,"%+.8e\n",fBMin[i]);
   fprintf(stream,"# Upper boundaries of interpolation region\n");
   for (int i=0;i<3;i++) fprintf(stream,"%+.8e\n",fBMax[i]);
-  fprintf(stream,"# Parameterization for each output dimension follows:\n",GetName());
+  fprintf(stream,"# Parameterization for each output dimension follows:\n");
   //
   for (int i=0;i<fDimOut;i++) GetChebCalc(i)->SaveData(stream);
   fprintf(stream,"#\nEND %s\n#\n",GetName());
@@ -906,7 +746,7 @@ void AliCheb3D::SetDimOut(int d)
 {
   fDimOut = d;
   if (fResTmp) delete fResTmp;
-  fResTmp = new Float_t[fDimOut]; // RRR
+  fResTmp = new Float_t[fDimOut];
   fChebCalc.Delete();
   for (int i=0;i<d;i++) fChebCalc.AddAtAndExpand(new AliCheb3DCalc(),i);
 }
@@ -945,3 +785,80 @@ TH1* AliCheb3D::TestRMS(int idim,int npoints,TH1* histo)
   //
 }
 #endif
+
+//_______________________________________________
+#ifdef _INC_CREATION_ALICHEB3D_
+void AliCheb3D::EstimateNPoints(float Prec, int gridBC[3][3])
+{
+  const float sclA[9] = {0.1, 0.5, 0.9, 0.1, 0.5, 0.9, 0.1, 0.5, 0.9} ;
+  const float sclB[9] = {0.1, 0.1, 0.1, 0.5, 0.5, 0.5, 0.9, 0.9, 0.9} ;
+  const float sclDim[2] = {0.01,0.99};
+  const int   compDim[3][2] = { {1,2}, {2,0}, {0,1} };
+  static float xyz[3];
+  //
+  for (int i=3;i--;)for (int j=3;j--;) gridBC[i][j] = -1;
+  //
+  for (int idim=0;idim<3;idim++) {
+    float dimMN = fBMin[idim] + sclDim[0]*(fBMax[idim]-fBMin[idim]);
+    float dimMX = fBMin[idim] + sclDim[1]*(fBMax[idim]-fBMin[idim]);
+    //
+    for (int it=0;it<9;it++) { // test in 9 points
+      int id1 = compDim[idim][0]; // 1st fixed dim
+      int id2 = compDim[idim][1]; // 2nd fixed dim
+      xyz[ id1 ] = fBMin[id1] + sclA[it]*( fBMax[id1]-fBMin[id1] );
+      xyz[ id2 ] = fBMin[id2] + sclB[it]*( fBMax[id2]-fBMin[id2] );
+      //
+      int* npt = GetNCNeeded(xyz,idim, dimMN,dimMX, Prec); // npoints for Bx,By,Bz
+      for (int ib=0;ib<3;ib++) if (npt[ib]>gridBC[ib][idim]) gridBC[ib][idim] = npt[ib]+2;
+      //
+    }
+  }
+}
+
+
+int* AliCheb3D::GetNCNeeded(float xyz[3],int DimVar, float mn,float mx, float prec)
+{
+  // estimate needed number of chebyshev coefs for given function desctiption in DimVar dimension
+  // The values for two other dimensions must be set beforehand
+  //
+  static int curNC[3];
+  static int retNC[3];
+  const int kMaxPoint = 400;
+  float* gridVal = new float[3*kMaxPoint];
+  float* coefs   = new float[3*kMaxPoint];
+  //
+  float scale = mx-mn;
+  float offs  = mn + scale/2.0;
+  scale = 2./scale;
+  // 
+  int curNP;
+  int maxNC=-1;
+  int maxNCPrev=-1;
+  for (int i=0;i<3;i++) retNC[i] = -1;
+  for (int i=0;i<3;i++) fArgsTmp[i] = xyz[i];
+  //
+  for (curNP=5; curNP<kMaxPoint; curNP+=5) { 
+    maxNCPrev = maxNC;
+    //
+    for (int i=0;i<curNP;i++) { // get function values on Cheb. nodes
+      float x = TMath::Cos( TMath::Pi()*(i+0.5)/curNP );
+      fArgsTmp[DimVar] =  x/scale+offs; // map to requested interval
+      EvalUsrFunction();
+      for (int ib=3;ib--;) gridVal[ib*kMaxPoint + i] = fResTmp[ib];
+    }
+    //
+    for (int ib=0;ib<3;ib++) {
+      curNC[ib] = AliCheb3D::CalcChebCoefs(&gridVal[ib*kMaxPoint], curNP, &coefs[ib*kMaxPoint],prec);
+      if (maxNC < curNC[ib]) maxNC = curNC[ib];
+      if (retNC[ib] < curNC[ib]) retNC[ib] = curNC[ib];
+    }
+    if ( (curNP-maxNC)>3 &&  (maxNC-maxNCPrev)<1 ) break;
+    maxNCPrev = maxNC;
+    //
+  }
+  delete[] gridVal;
+  delete[] coefs;
+  return retNC;
+  //
+}
+#endif