]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliCheb3DCalc.cxx
Pass run related information (beam energy, type, mag. field)
[u/mrichter/AliRoot.git] / STEER / AliCheb3DCalc.cxx
index 6bcc0f8720a4655ee1a23da35faed44da48f29c2..0982bdbd15270968a4313cc7334e9fed7c5f9484 100644 (file)
  * 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     //
-// function supplied in "void (*fcn)(float* inp,float* out)" format           //
-// either in a separate macro file or as a function pointer.                  //
-// Only coefficients needed to guarantee the requested precision are kept.    //
-//                                                                            //
-// The user-callable methods are:                                             //
-// To create the interpolation use:                                           //
-// AliCheb3D(const char* funName,  // name of the file with user function     //
-//          or                                                                //
-// AliCheb3D(void (*ptr)(float*,float*),// pointer on the  user function      //
-//        Int_t     DimOut,     // dimensionality of the function's output    // 
-//        Float_t  *bmin,       // lower 3D bounds of interpolation domain    // 
-//        Float_t  *bmax,       // upper 3D bounds of interpolation domain    // 
-//        Int_t    *npoints,    // number of points in each of 3 input        //
-//                              // dimension, defining the interpolation grid //
-//        Float_t   prec=1E-6); // requested max.absolute difference between  //
-//                              // the interpolation and any point on grid    //
-//                                                                            //
-// To test obtained parameterization use the method                           //
-// TH1* TestRMS(int idim,int npoints = 1000,TH1* histo=0);                    // 
-// it will compare the user output of the user function and interpolation     //
-// for idim-th output dimension and fill the difference in the supplied       //
-// histogram. If no histogram is supplied, it will be created.                //
-//                                                                            //
-// To save the interpolation data:                                            //
-// SaveData(const char* filename, Bool_t append )                             //
-// write text file with data. If append is kTRUE and the output file already  //
-// exists, data will be added in the end of the file.                         //
-// Alternatively, SaveData(FILE* stream) will write the data to               //
-// already existing stream.                                                   //
-//                                                                            //
-// To read back already stored interpolation use either the constructor       // 
-// AliCheb3D(const char* inpFile);                                            //
-// or the default constructor AliCheb3D() followed by                         //
-// AliCheb3D::LoadData(const char* inpFile);                                  //
-//                                                                            //
-// To compute the interpolation use Eval(float* par,float *res) method, with  //
-// par being 3D vector of arguments (inside the validity region) and res is   //
-// the array of DimOut elements for the output.                               //
-//                                                                            //
-// If only one component (say, idim-th) of the output is needed, use faster   //
-// Float_t Eval(Float_t *par,int idim) method.                                //
-//                                                                            //
-// void Print(option="") will print the name, the ranges of validity and      //
-// the absolute precision of the parameterization. Option "l" will also print //
-// the information about the number of coefficients for each output           //
-// dimension.                                                                 //
-//                                                                            //
-// NOTE: during the evaluation no check is done for parameter vector being    //
-// outside the interpolation region. If there is such a risk, use             //
-// Bool_t IsInside(float *par) method. Chebyshev parameterization is not      //
-// good for extrapolation!                                                    //
-//                                                                            //
-// For the properties of Chebyshev parameterization see:                      //
-// H.Wind, CERN EP Internal Report, 81-12/Rev.                                //
-//                                                                            //
-////////////////////////////////////////////////////////////////////////////////
-
+#include <cstdlib>
+#include <TSystem.h>
 #include "AliCheb3DCalc.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)
+//__________________________________________________________________________________________
+AliCheb3DCalc::AliCheb3DCalc() :
+  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();
+  // default constructor
 }
 
-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)
+//__________________________________________________________________________________________
+AliCheb3DCalc::AliCheb3DCalc(const AliCheb3DCalc& src) :
+  TNamed(src), 
+  fNCoefs(src.fNCoefs), 
+  fNRows(src.fNRows), 
+  fNCols(src.fNCols),
+  fNElemBound2D(src.fNElemBound2D), 
+  fNColsAtRow(0), 
+  fColAtRowBg(0), 
+  fCoefBound2D0(0), 
+  fCoefBound2D1(0), 
+  fCoefs(0), 
+  fTmpCf1(0), 
+  fTmpCf0(0)
 {
-    // Default constructor
-    Init0();
-    LoadData(stream);
+  // copy constructor
+  //
+  if (src.fNColsAtRow) {
+    fNColsAtRow = new UShort_t[fNRows]; 
+    for (int i=fNRows;i--;) fNColsAtRow[i] = src.fNColsAtRow[i];
+  }
+  if (src.fColAtRowBg) {
+    fColAtRowBg = new UShort_t[fNRows]; 
+    for (int i=fNRows;i--;) fColAtRowBg[i] = src.fColAtRowBg[i];
+  }
+  if (src.fCoefBound2D0) {
+    fCoefBound2D0 = new UShort_t[fNElemBound2D];
+    for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = src.fCoefBound2D0[i];
+  }
+  if (src.fCoefBound2D1) {
+    fCoefBound2D1 = new UShort_t[fNElemBound2D];
+    for (int i=fNElemBound2D;i--;) fCoefBound2D1[i] = src.fCoefBound2D1[i];
+  }
+  if (src.fCoefs) {
+    fCoefs = new Float_t[fNCoefs];
+    for (int i=fNCoefs;i--;) fCoefs[i] = src.fCoefs[i];
+  }
+  if (src.fTmpCf1) fTmpCf1 = new Float_t[fNCols];
+  if (src.fTmpCf0) fTmpCf0 = new Float_t[fNRows];
 }
 
+//__________________________________________________________________________________________
+AliCheb3DCalc::AliCheb3DCalc(FILE* stream) :
+  fNCoefs(0), 
+  fNRows(0), 
+  fNCols(0), 
+  fNElemBound2D(0), 
+  fNColsAtRow(0), 
+  fColAtRowBg(0), 
+  fCoefBound2D0(0), 
+  fCoefBound2D1(0), 
+  fCoefs(0), 
+  fTmpCf1(0), 
+  fTmpCf0(0)
+{
+  // constructor from coeffs. streem
+  LoadData(stream);
+}
 
-AliCheb3DCalc::AliCheb3DCalc(const AliCheb3DCalc& src) :
-    TNamed(src), 
-    fNCoefs(src.fNCoefs), 
-    fNRows(src.fNRows), 
-    fNCols(src.fNCols),
-    fNElemBound2D(src.fNElemBound2D), 
-    fNColsAtRow(0), 
-    fColAtRowBg(0), 
-    fCoefBound2D0(0), 
-    fCoefBound2D1(0), 
-    fCoefs(0), 
-    fTmpCf1(0), 
-    fTmpCf0(0)
+//__________________________________________________________________________________________
+AliCheb3DCalc& AliCheb3DCalc::operator=(const AliCheb3DCalc& rhs)
 {
-    // Copy constructor
-    if (src.fNColsAtRow) {
-       fNColsAtRow = new Int_t[fNRows]; 
-       for (int i=fNRows;i--;) fNColsAtRow[i] = src.fNColsAtRow[i];
+  // assignment operator
+  if (this != &rhs) {
+    Clear();
+    SetName(rhs.GetName());
+    SetTitle(rhs.GetTitle());
+    fNCoefs = rhs.fNCoefs;
+    fNRows  = rhs.fNRows;
+    fNCols  = rhs.fNCols;    
+    if (rhs.fNColsAtRow) {
+      fNColsAtRow = new UShort_t[fNRows]; 
+      for (int i=fNRows;i--;) fNColsAtRow[i] = rhs.fNColsAtRow[i];
     }
-    if (src.fColAtRowBg) {
-       fColAtRowBg = new Int_t[fNRows]; 
-       for (int i=fNRows;i--;) fColAtRowBg[i] = src.fColAtRowBg[i];
+    if (rhs.fColAtRowBg) {
+      fColAtRowBg = new UShort_t[fNRows]; 
+      for (int i=fNRows;i--;) fColAtRowBg[i] = rhs.fColAtRowBg[i];
     }
-    if (src.fCoefBound2D0) {
-       fCoefBound2D0 = new Int_t[fNElemBound2D];
-       for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = src.fCoefBound2D0[i];
+    if (rhs.fCoefBound2D0) {
+      fCoefBound2D0 = new UShort_t[fNElemBound2D];
+      for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = rhs.fCoefBound2D0[i];
     }
-    if (src.fCoefBound2D1) {
-       fCoefBound2D1 = new Int_t[fNElemBound2D];
-       for (int i=fNElemBound2D;i--;) fCoefBound2D1[i] = src.fCoefBound2D1[i];
+    if (rhs.fCoefBound2D1) {
+      fCoefBound2D1 = new UShort_t[fNElemBound2D];
+      for (int i=fNElemBound2D;i--;) fCoefBound2D1[i] = rhs.fCoefBound2D1[i];
     }
-    if (src.fCoefs) {
-       fCoefs = new Float_t[fNCoefs];
-       for (int i=fNCoefs;i--;) fCoefs[i] = src.fCoefs[i];
-    }
-    if (src.fTmpCf1) fTmpCf1 = new Float_t[fNCols];
-    if (src.fTmpCf0) fTmpCf0 = new Float_t[fNRows];
-}
-
-AliCheb3DCalc& AliCheb3DCalc::operator=(const AliCheb3DCalc& rhs)
-{
-    // Assignment operator
-    if (this != &rhs) {
-       Clear();
-       SetName(rhs.GetName());
-       SetTitle(rhs.GetTitle());
-       fNCoefs = rhs.fNCoefs;
-       fNRows  = rhs.fNRows;
-       fNCols  = rhs.fNCols;    
-       if (rhs.fNColsAtRow) {
-           fNColsAtRow = new Int_t[fNRows]; 
-           for (int i=fNRows;i--;) fNColsAtRow[i] = rhs.fNColsAtRow[i];
-       }
-       if (rhs.fColAtRowBg) {
-           fColAtRowBg = new Int_t[fNRows]; 
-           for (int i=fNRows;i--;) fColAtRowBg[i] = rhs.fColAtRowBg[i];
-       }
-       if (rhs.fCoefBound2D0) {
-           fCoefBound2D0 = new Int_t[fNElemBound2D];
-           for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = rhs.fCoefBound2D0[i];
-       }
-       if (rhs.fCoefBound2D1) {
-           fCoefBound2D1 = new Int_t[fNElemBound2D];
-           for (int i=fNElemBound2D;i--;) fCoefBound2D1[i] = rhs.fCoefBound2D1[i];
-       }
-       if (rhs.fCoefs) {
-           fCoefs = new Float_t[fNCoefs];
-           for (int i=fNCoefs;i--;) fCoefs[i] = rhs.fCoefs[i];
-       }
-       if (rhs.fTmpCf1) fTmpCf1 = new Float_t[fNCols];
-       if (rhs.fTmpCf0) fTmpCf0 = new Float_t[fNRows];    
+    if (rhs.fCoefs) {
+      fCoefs = new Float_t[fNCoefs];
+      for (int i=fNCoefs;i--;) fCoefs[i] = rhs.fCoefs[i];
     }
-    return *this;
+    if (rhs.fTmpCf1) fTmpCf1 = new Float_t[fNCols];
+    if (rhs.fTmpCf0) fTmpCf0 = new Float_t[fNRows];    
+  }
+  return *this;
 }
 
 //__________________________________________________________________________________________
-void AliCheb3DCalc::Clear(Option_t*)
+void AliCheb3DCalc::Clear(const Option_t*)
 {
   // delete all dynamycally allocated structures
   if (fTmpCf1)       { delete[] fTmpCf1;  fTmpCf1 = 0;}
@@ -210,20 +147,9 @@ void AliCheb3DCalc::Clear(Option_t*)
 }
 
 //__________________________________________________________________________________________
-void AliCheb3DCalc::Init0()
+void AliCheb3DCalc::Print(const Option_t* ) const
 {
-  // reset everything to 0
-  fNCoefs = fNRows = fNCols = fNElemBound2D = 0;
-  fCoefs = 0;
-  fCoefBound2D0 = fCoefBound2D1 = 0;
-  fNColsAtRow = fColAtRowBg = 0;
-  fTmpCf0 = fTmpCf1 = 0;
-}
-
-//__________________________________________________________________________________________
-void AliCheb3DCalc::Print(Option_t* ) const
-{
-  // Print the Chebychev paramterization data
+  // print info
   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];
@@ -232,25 +158,55 @@ void AliCheb3DCalc::Print(Option_t* ) const
 }
 
 //__________________________________________________________________________________________
-Float_t  AliCheb3DCalc::Eval(Float_t  *par) const
+Float_t  AliCheb3DCalc::EvalDeriv(int dim, const Float_t  *par) const
+{
+  // evaluate Chebyshev parameterization derivative in given dimension  for 3D function.
+  // VERY IMPORTANT: par must contain the function arguments ALREADY MAPPED to [-1:1] interval
+  //
+  int ncfRC;
+  for (int id0=fNRows;id0--;) {
+    int nCLoc = fNColsAtRow[id0];                   // number of significant coefs on this row
+    if (!nCLoc) {fTmpCf0[id0]=0; continue;}
+    // 
+    int col0  = fColAtRowBg[id0];                   // beginning of local column in the 2D boundary matrix
+    for (int id1=nCLoc;id1--;) {
+      int id = id1+col0;
+      if (!(ncfRC=fCoefBound2D0[id])) { fTmpCf1[id1]=0; continue;}
+      if (dim==2) fTmpCf1[id1] = ChebEval1Deriv(par[2],fCoefs + fCoefBound2D1[id], ncfRC);
+      else        fTmpCf1[id1] = ChebEval1D(par[2],fCoefs + fCoefBound2D1[id], ncfRC);
+    }
+    if (dim==1)   fTmpCf0[id0] = ChebEval1Deriv(par[1],fTmpCf1,nCLoc);
+    else          fTmpCf0[id0] = ChebEval1D(par[1],fTmpCf1,nCLoc);
+  }
+  return (dim==0) ? ChebEval1Deriv(par[0],fTmpCf0,fNRows) : ChebEval1D(par[0],fTmpCf0,fNRows);
+  //
+}
+
+//__________________________________________________________________________________________
+Float_t  AliCheb3DCalc::EvalDeriv2(int dim1,int dim2, const Float_t  *par) const
 {
-  // evaluate Chebyshev parameterization for 3D function.
+  // evaluate Chebyshev parameterization 2n derivative in given dimensions  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];
   //
+  Bool_t same = dim1==dim2;
   int ncfRC;
   for (int id0=fNRows;id0--;) {
     int nCLoc = fNColsAtRow[id0];                   // number of significant coefs on this row
+    if (!nCLoc) {fTmpCf0[id0]=0; continue;}
+    //
     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;
+      if (!(ncfRC=fCoefBound2D0[id])) { fTmpCf1[id1]=0; continue;}
+      if (dim1==2||dim2==2) fTmpCf1[id1] = same ? ChebEval1Deriv2(par[2],fCoefs + fCoefBound2D1[id], ncfRC) 
+                             :                   ChebEval1Deriv(par[2],fCoefs + fCoefBound2D1[id], ncfRC);
+      else        fTmpCf1[id1] = ChebEval1D(par[2],fCoefs + fCoefBound2D1[id], ncfRC);
     }
-    fTmpCf0[id0] = nCLoc>0 ? ChebEval1D(y,fTmpCf1,nCLoc):0.0;
+    if (dim1==1||dim2==1) fTmpCf0[id0] = same ? ChebEval1Deriv2(par[1],fTmpCf1,nCLoc):ChebEval1Deriv(par[1],fTmpCf1,nCLoc);
+    else                  fTmpCf0[id0] = ChebEval1D(par[1],fTmpCf1,nCLoc);
   }
-  return ChebEval1D(x,fTmpCf0,fNRows);
+  return (dim1==0||dim2==0) ? (same ? ChebEval1Deriv2(par[0],fTmpCf0,fNRows):ChebEval1Deriv(par[0],fTmpCf0,fNRows)) : 
+    ChebEval1D(par[0],fTmpCf0,fNRows);
   //
 }
 
@@ -373,9 +329,9 @@ void AliCheb3DCalc::InitRows(int nr)
   if (fColAtRowBg) delete[] fColAtRowBg;
   if (fTmpCf0)     delete[] fTmpCf0;
   fNRows = nr;
-  fNColsAtRow = new Int_t[fNRows];
+  fNColsAtRow = new UShort_t[fNRows];
   fTmpCf0     = new Float_t [fNRows];
-  fColAtRowBg = new Int_t[fNRows];
+  fColAtRowBg = new UShort_t[fNRows];
   for (int i=fNRows;i--;) fNColsAtRow[i] = fColAtRowBg[i] = 0;
 }
 
@@ -386,8 +342,8 @@ void AliCheb3DCalc::InitElemBound2D(int ne)
   if (fCoefBound2D0) delete[] fCoefBound2D0; 
   if (fCoefBound2D1) delete[] fCoefBound2D1; 
   fNElemBound2D = ne;
-  fCoefBound2D0 = new Int_t[fNElemBound2D];
-  fCoefBound2D1 = new Int_t[fNElemBound2D];
+  fCoefBound2D0 = new UShort_t[fNElemBound2D];
+  fCoefBound2D1 = new UShort_t[fNElemBound2D];
   for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = fCoefBound2D1[i] = 0;
 }
 
@@ -401,4 +357,71 @@ void AliCheb3DCalc::InitCoefs(int nc)
   for (int i=fNCoefs;i--;) fCoefs[i] = 0.0;
 }
 
+//__________________________________________________________________________________________
+Float_t AliCheb3DCalc::ChebEval1Deriv(Float_t  x, const Float_t * array, int ncf )
+{
+  // evaluate 1D Chebyshev parameterization's derivative. x is the argument mapped to [-1:1] interval
+  if (--ncf<1) return 0;
+  Float_t b0, b1, b2;
+  Float_t x2 = x+x;
+  b1 = b2 = 0;
+  float dcf0=0,dcf1,dcf2=0;
+  b0 = dcf1 = 2*ncf*array[ncf];
+  if (!(--ncf)) return b0/2;
+  //
+  for (int i=ncf;i--;) {
+    b2 = b1;      
+    b1 = b0;
+    dcf0 = dcf2 + 2*(i+1)*array[i+1];
+    b0 = dcf0 + x2*b1 -b2;
+    dcf2 = dcf1;  
+    dcf1 = dcf0;
+  }
+  //
+  return b0 - x*b1 - dcf0/2;
+}
+
+//__________________________________________________________________________________________
+Float_t AliCheb3DCalc::ChebEval1Deriv2(Float_t  x, const Float_t * array, int ncf )
+{
+  // evaluate 1D Chebyshev parameterization's 2nd derivative. x is the argument mapped to [-1:1] interval
+  if (--ncf<2) return 0;
+  Float_t b0, b1, b2;
+  Float_t x2 = x+x;
+  b1 = b2 = 0;
+  float dcf0=0,dcf1=0,dcf2=0;
+  float ddcf0=0,ddcf1,ddcf2=0;
+  //
+  dcf2 = 2*ncf*array[ncf]; 
+  --ncf; 
+
+  dcf1 = 2*ncf*array[ncf]; 
+  b0 = ddcf1 = 2*ncf*dcf2; 
+  //
+  if (!(--ncf)) return b0/2;
+  //
+  for (int i=ncf;i--;) {
+    b2 = b1;                        
+    b1 = b0;
+    dcf0  = dcf2 + 2*(i+1)*array[i+1];
+    ddcf0 = ddcf2 + 2*(i+1)*dcf1; 
+    b0 = ddcf0 + x2*b1 -b2;
+    //
+    ddcf2 = ddcf1;  
+    ddcf1 = ddcf0;
+    //
+    dcf2 = dcf1;
+    dcf1 = dcf0;
+    //
+  }
+  //
+  return b0 - x*b1 - ddcf0/2;
+}
 
+//__________________________________________________________________________________________
+Int_t AliCheb3DCalc::GetMaxColsAtRow() const
+{
+  int nmax3d = 0; 
+  for (int i=fNElemBound2D;i--;) if (fCoefBound2D0[i]>nmax3d) nmax3d = fCoefBound2D0[i];
+  return nmax3d;
+}