]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/STEERBase/AliCheb3DCalc.cxx
Optionally dimensions-specific precision can be asked for Chebishev fits
[u/mrichter/AliRoot.git] / STEER / STEERBase / AliCheb3DCalc.cxx
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  **************************************************************************/
15
16 #include <cstdlib>
17 #include <TSystem.h>
18 #include "AliCheb3DCalc.h"
19
20 ClassImp(AliCheb3DCalc)
21
22 //__________________________________________________________________________________________
23 AliCheb3DCalc::AliCheb3DCalc() :
24   fNCoefs(0), 
25   fNRows(0), 
26   fNCols(0), 
27   fNElemBound2D(0), 
28   fNColsAtRow(0), 
29   fColAtRowBg(0),
30   fCoefBound2D0(0), 
31   fCoefBound2D1(0), 
32   fCoefs(0), 
33   fTmpCf1(0), 
34   fTmpCf0(0),
35   fPrec(0)
36 {
37   // default constructor
38 }
39
40 //__________________________________________________________________________________________
41 AliCheb3DCalc::AliCheb3DCalc(const AliCheb3DCalc& src) :
42   TNamed(src), 
43   fNCoefs(src.fNCoefs), 
44   fNRows(src.fNRows), 
45   fNCols(src.fNCols),
46   fNElemBound2D(src.fNElemBound2D), 
47   fNColsAtRow(0), 
48   fColAtRowBg(0), 
49   fCoefBound2D0(0), 
50   fCoefBound2D1(0), 
51   fCoefs(0), 
52   fTmpCf1(0), 
53   fTmpCf0(0), 
54   fPrec(src.fPrec)
55 {
56   // copy constructor
57   //
58   if (src.fNColsAtRow) {
59     fNColsAtRow = new UShort_t[fNRows]; 
60     for (int i=fNRows;i--;) fNColsAtRow[i] = src.fNColsAtRow[i];
61   }
62   if (src.fColAtRowBg) {
63     fColAtRowBg = new UShort_t[fNRows]; 
64     for (int i=fNRows;i--;) fColAtRowBg[i] = src.fColAtRowBg[i];
65   }
66   if (src.fCoefBound2D0) {
67     fCoefBound2D0 = new UShort_t[fNElemBound2D];
68     for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = src.fCoefBound2D0[i];
69   }
70   if (src.fCoefBound2D1) {
71     fCoefBound2D1 = new UShort_t[fNElemBound2D];
72     for (int i=fNElemBound2D;i--;) fCoefBound2D1[i] = src.fCoefBound2D1[i];
73   }
74   if (src.fCoefs) {
75     fCoefs = new Float_t[fNCoefs];
76     for (int i=fNCoefs;i--;) fCoefs[i] = src.fCoefs[i];
77   }
78   if (src.fTmpCf1) fTmpCf1 = new Float_t[fNCols];
79   if (src.fTmpCf0) fTmpCf0 = new Float_t[fNRows];
80 }
81
82 //__________________________________________________________________________________________
83 AliCheb3DCalc::AliCheb3DCalc(FILE* stream) :
84   fNCoefs(0), 
85   fNRows(0), 
86   fNCols(0), 
87   fNElemBound2D(0), 
88   fNColsAtRow(0), 
89   fColAtRowBg(0), 
90   fCoefBound2D0(0), 
91   fCoefBound2D1(0), 
92   fCoefs(0), 
93   fTmpCf1(0), 
94   fTmpCf0(0),
95   fPrec(0)
96 {
97   // constructor from coeffs. streem
98   LoadData(stream);
99 }
100
101 //__________________________________________________________________________________________
102 AliCheb3DCalc& AliCheb3DCalc::operator=(const AliCheb3DCalc& rhs)
103 {
104   // assignment operator
105   if (this != &rhs) {
106     Clear();
107     SetName(rhs.GetName());
108     SetTitle(rhs.GetTitle());
109     fNCoefs = rhs.fNCoefs;
110     fNRows  = rhs.fNRows;
111     fNCols  = rhs.fNCols;    
112     fPrec   = rhs.fPrec;
113     if (rhs.fNColsAtRow) {
114       fNColsAtRow = new UShort_t[fNRows]; 
115       for (int i=fNRows;i--;) fNColsAtRow[i] = rhs.fNColsAtRow[i];
116     }
117     if (rhs.fColAtRowBg) {
118       fColAtRowBg = new UShort_t[fNRows]; 
119       for (int i=fNRows;i--;) fColAtRowBg[i] = rhs.fColAtRowBg[i];
120     }
121     if (rhs.fCoefBound2D0) {
122       fCoefBound2D0 = new UShort_t[fNElemBound2D];
123       for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = rhs.fCoefBound2D0[i];
124     }
125     if (rhs.fCoefBound2D1) {
126       fCoefBound2D1 = new UShort_t[fNElemBound2D];
127       for (int i=fNElemBound2D;i--;) fCoefBound2D1[i] = rhs.fCoefBound2D1[i];
128     }
129     if (rhs.fCoefs) {
130       fCoefs = new Float_t[fNCoefs];
131       for (int i=fNCoefs;i--;) fCoefs[i] = rhs.fCoefs[i];
132     }
133     if (rhs.fTmpCf1) fTmpCf1 = new Float_t[fNCols];
134     if (rhs.fTmpCf0) fTmpCf0 = new Float_t[fNRows];    
135   }
136   return *this;
137 }
138
139 //__________________________________________________________________________________________
140 void AliCheb3DCalc::Clear(const Option_t*)
141 {
142   // delete all dynamycally allocated structures
143   if (fTmpCf1)       { delete[] fTmpCf1;  fTmpCf1 = 0;}
144   if (fTmpCf0)       { delete[] fTmpCf0;  fTmpCf0 = 0;}
145   if (fCoefs)        { delete[] fCoefs;   fCoefs  = 0;}
146   if (fCoefBound2D0) { delete[] fCoefBound2D0; fCoefBound2D0 = 0; }
147   if (fCoefBound2D1) { delete[] fCoefBound2D1; fCoefBound2D1 = 0; }
148   if (fNColsAtRow)   { delete[] fNColsAtRow;   fNColsAtRow = 0; }
149   if (fColAtRowBg)   { delete[] fColAtRowBg;   fColAtRowBg = 0; }
150   //
151 }
152
153 //__________________________________________________________________________________________
154 void AliCheb3DCalc::Print(const Option_t* ) const
155 {
156   // print info
157   printf("Chebyshev parameterization data %s for 3D->1 function. Prec:%e\n",GetName(),fPrec);
158   int nmax3d = 0; 
159   for (int i=fNElemBound2D;i--;) if (fCoefBound2D0[i]>nmax3d) nmax3d = fCoefBound2D0[i];
160   printf("%d coefficients in %dx%dx%d matrix\n",fNCoefs,fNRows,fNCols,nmax3d);
161   //
162 }
163
164 //__________________________________________________________________________________________
165 Float_t  AliCheb3DCalc::EvalDeriv(int dim, const Float_t  *par) const
166 {
167   // evaluate Chebyshev parameterization derivative in given dimension  for 3D function.
168   // VERY IMPORTANT: par must contain the function arguments ALREADY MAPPED to [-1:1] interval
169   //
170   int ncfRC;
171   for (int id0=fNRows;id0--;) {
172     int nCLoc = fNColsAtRow[id0];                   // number of significant coefs on this row
173     if (!nCLoc) {fTmpCf0[id0]=0; continue;}
174     // 
175     int col0  = fColAtRowBg[id0];                   // beginning of local column in the 2D boundary matrix
176     for (int id1=nCLoc;id1--;) {
177       int id = id1+col0;
178       if (!(ncfRC=fCoefBound2D0[id])) { fTmpCf1[id1]=0; continue;}
179       if (dim==2) fTmpCf1[id1] = ChebEval1Deriv(par[2],fCoefs + fCoefBound2D1[id], ncfRC);
180       else        fTmpCf1[id1] = ChebEval1D(par[2],fCoefs + fCoefBound2D1[id], ncfRC);
181     }
182     if (dim==1)   fTmpCf0[id0] = ChebEval1Deriv(par[1],fTmpCf1,nCLoc);
183     else          fTmpCf0[id0] = ChebEval1D(par[1],fTmpCf1,nCLoc);
184   }
185   return (dim==0) ? ChebEval1Deriv(par[0],fTmpCf0,fNRows) : ChebEval1D(par[0],fTmpCf0,fNRows);
186   //
187 }
188
189 //__________________________________________________________________________________________
190 Float_t  AliCheb3DCalc::EvalDeriv2(int dim1,int dim2, const Float_t  *par) const
191 {
192   // evaluate Chebyshev parameterization 2n derivative in given dimensions  for 3D function.
193   // VERY IMPORTANT: par must contain the function arguments ALREADY MAPPED to [-1:1] interval
194   //
195   Bool_t same = dim1==dim2;
196   int ncfRC;
197   for (int id0=fNRows;id0--;) {
198     int nCLoc = fNColsAtRow[id0];                   // number of significant coefs on this row
199     if (!nCLoc) {fTmpCf0[id0]=0; continue;}
200     //
201     int col0  = fColAtRowBg[id0];                   // beginning of local column in the 2D boundary matrix
202     for (int id1=nCLoc;id1--;) {
203       int id = id1+col0;
204       if (!(ncfRC=fCoefBound2D0[id])) { fTmpCf1[id1]=0; continue;}
205       if (dim1==2||dim2==2) fTmpCf1[id1] = same ? ChebEval1Deriv2(par[2],fCoefs + fCoefBound2D1[id], ncfRC) 
206                               :                   ChebEval1Deriv(par[2],fCoefs + fCoefBound2D1[id], ncfRC);
207       else        fTmpCf1[id1] = ChebEval1D(par[2],fCoefs + fCoefBound2D1[id], ncfRC);
208     }
209     if (dim1==1||dim2==1) fTmpCf0[id0] = same ? ChebEval1Deriv2(par[1],fTmpCf1,nCLoc):ChebEval1Deriv(par[1],fTmpCf1,nCLoc);
210     else                  fTmpCf0[id0] = ChebEval1D(par[1],fTmpCf1,nCLoc);
211   }
212   return (dim1==0||dim2==0) ? (same ? ChebEval1Deriv2(par[0],fTmpCf0,fNRows):ChebEval1Deriv(par[0],fTmpCf0,fNRows)) : 
213     ChebEval1D(par[0],fTmpCf0,fNRows);
214   //
215 }
216
217 //_______________________________________________
218 #ifdef _INC_CREATION_ALICHEB3D_
219 void AliCheb3DCalc::SaveData(const char* outfile,Bool_t append) const
220 {
221   // writes coefficients data to output text file, optionallt appending on the end of existing file
222   TString strf = outfile;
223   gSystem->ExpandPathName(strf);
224   FILE* stream = fopen(strf,append ? "a":"w");
225   SaveData(stream);
226   fclose(stream);
227   //
228 }
229 #endif
230
231 //_______________________________________________
232 #ifdef _INC_CREATION_ALICHEB3D_
233 void AliCheb3DCalc::SaveData(FILE* stream) const
234 {
235   // writes coefficients data to existing output stream
236   // Note: fNCols, fNElemBound2D and fColAtRowBg is not stored, will be computed on fly during the loading of this file
237   fprintf(stream,"#\nSTART %s\n",GetName());
238   fprintf(stream,"# Number of rows\n%d\n",fNRows);
239   //
240   fprintf(stream,"# Number of columns per row\n");
241   for (int i=0;i<fNRows;i++) fprintf(stream,"%d\n",fNColsAtRow[i]);
242   //
243   fprintf(stream,"# Number of Coefs in each significant block of third dimension\n");
244   for (int i=0;i<fNElemBound2D;i++) fprintf(stream,"%d\n",fCoefBound2D0[i]);
245   //
246   fprintf(stream,"# Coefficients\n");
247   for (int i=0;i<fNCoefs;i++) fprintf(stream,"%+.8e\n",fCoefs[i]);
248   //
249   fprintf(stream,"# Precision\n");
250   fprintf(stream,"%+.8e\n",fPrec);
251   //
252   fprintf(stream,"END %s\n",GetName());
253   //
254 }
255 #endif
256
257 //_______________________________________________
258 void AliCheb3DCalc::LoadData(FILE* stream)
259 {
260   // Load coefs. from the stream 
261   if (!stream) {Error("LoadData","No stream provided.\nStop"); exit(1);}
262   TString buffs;
263   Clear();
264   ReadLine(buffs,stream);
265   if (!buffs.BeginsWith("START")) {Error("LoadData","Expected: \"START <fit_name>\", found \"%s\"\nStop\n",buffs.Data());exit(1);}
266   if (buffs.First(' ')>0) SetName(buffs.Data()+buffs.First(' ')+1);
267   //
268   ReadLine(buffs,stream); // NRows
269   fNRows = buffs.Atoi(); 
270   if (fNRows<1) {Error("LoadData","Expected: '<number_of_rows>', found \"%s\"\nStop\n",buffs.Data());exit(1);}
271   //
272   fNCols = 0;
273   fNElemBound2D = 0;
274   InitRows(fNRows);
275   //
276   for (int id0=0;id0<fNRows;id0++) {
277     ReadLine(buffs,stream);               // n.cols at this row
278     fNColsAtRow[id0] = buffs.Atoi();
279     fColAtRowBg[id0] = fNElemBound2D;     // begining of this row in 2D boundary surface
280     fNElemBound2D   += fNColsAtRow[id0];
281     if (fNCols<fNColsAtRow[id0]) fNCols = fNColsAtRow[id0];
282   }
283   InitCols(fNCols);
284   //  
285   fNCoefs = 0; 
286   InitElemBound2D(fNElemBound2D);
287   //
288   for (int i=0;i<fNElemBound2D;i++) {
289     ReadLine(buffs,stream);               // n.coeffs at 3-d dimension for the given column/row
290     fCoefBound2D0[i] = buffs.Atoi();
291     fCoefBound2D1[i] = fNCoefs;
292     fNCoefs += fCoefBound2D0[i];
293   }
294   //
295   if (fNCoefs<=0) {Error("LoadData","Negtive (%d) number of Chebychef coeffs. is obtained.\nStop\n",fNCoefs);exit(1);}
296   //
297   InitCoefs(fNCoefs);
298   for (int i=0;i<fNCoefs;i++) {
299     ReadLine(buffs,stream);
300     fCoefs[i] = buffs.Atof();
301   }
302   //
303   // read precision
304   ReadLine(buffs,stream);
305   fPrec = buffs.Atof();
306   //
307   // check end_of_data record
308   ReadLine(buffs,stream);
309   if (!buffs.BeginsWith("END") || !buffs.Contains(GetName())) {
310     Error("LoadData","Expected \"END %s\", found \"%s\".\nStop\n",GetName(),buffs.Data());
311     exit(1);
312   }
313   //
314 }
315
316 //_______________________________________________
317 void AliCheb3DCalc::ReadLine(TString& str,FILE* stream) 
318 {
319   // read single line from the stream, skipping empty and commented lines. EOF is not expected
320   while (str.Gets(stream)) {
321     str = str.Strip(TString::kBoth,' ');
322     if (str.IsNull()||str.BeginsWith("#")) continue;
323     return;
324   }
325   fprintf(stderr,"AliCheb3D::ReadLine: Failed to read from stream.\nStop");exit(1); // normally, should not reach here
326 }
327
328 //_______________________________________________
329 void AliCheb3DCalc::InitCols(int nc)
330 {
331   // Set max.number of significant columns in the coefs matrix
332   fNCols = nc;
333   if (fTmpCf1) delete[] fTmpCf1;
334   fTmpCf1 = new Float_t [fNCols];
335 }
336
337 //_______________________________________________
338 void AliCheb3DCalc::InitRows(int nr)
339 {
340   // Set max.number of significant rows in the coefs matrix
341   if (fNColsAtRow) delete[] fNColsAtRow;
342   if (fColAtRowBg) delete[] fColAtRowBg;
343   if (fTmpCf0)     delete[] fTmpCf0;
344   fNRows = nr;
345   fNColsAtRow = new UShort_t[fNRows];
346   fTmpCf0     = new Float_t [fNRows];
347   fColAtRowBg = new UShort_t[fNRows];
348   for (int i=fNRows;i--;) fNColsAtRow[i] = fColAtRowBg[i] = 0;
349 }
350
351 //_______________________________________________
352 void AliCheb3DCalc::InitElemBound2D(int ne)
353 {
354   // Set max number of significant coefs for given row/column of coefs 3D matrix
355   if (fCoefBound2D0) delete[] fCoefBound2D0; 
356   if (fCoefBound2D1) delete[] fCoefBound2D1; 
357   fNElemBound2D = ne;
358   fCoefBound2D0 = new UShort_t[fNElemBound2D];
359   fCoefBound2D1 = new UShort_t[fNElemBound2D];
360   for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = fCoefBound2D1[i] = 0;
361 }
362
363 //_______________________________________________
364 void AliCheb3DCalc::InitCoefs(int nc)
365 {
366   // Set total number of significant coefs
367   if (fCoefs) delete[] fCoefs; 
368   fNCoefs = nc;
369   fCoefs = new Float_t [fNCoefs];
370   for (int i=fNCoefs;i--;) fCoefs[i] = 0.0;
371 }
372
373 //__________________________________________________________________________________________
374 Float_t AliCheb3DCalc::ChebEval1Deriv(Float_t  x, const Float_t * array, int ncf )
375 {
376   // evaluate 1D Chebyshev parameterization's derivative. x is the argument mapped to [-1:1] interval
377   if (--ncf<1) return 0;
378   Float_t b0, b1, b2;
379   Float_t x2 = x+x;
380   b1 = b2 = 0;
381   float dcf0=0,dcf1,dcf2=0;
382   b0 = dcf1 = 2*ncf*array[ncf];
383   if (!(--ncf)) return b0/2;
384   //
385   for (int i=ncf;i--;) {
386     b2 = b1;      
387     b1 = b0;
388     dcf0 = dcf2 + 2*(i+1)*array[i+1];
389     b0 = dcf0 + x2*b1 -b2;
390     dcf2 = dcf1;  
391     dcf1 = dcf0;
392   }
393   //
394   return b0 - x*b1 - dcf0/2;
395 }
396
397 //__________________________________________________________________________________________
398 Float_t AliCheb3DCalc::ChebEval1Deriv2(Float_t  x, const Float_t * array, int ncf )
399 {
400   // evaluate 1D Chebyshev parameterization's 2nd derivative. x is the argument mapped to [-1:1] interval
401   if (--ncf<2) return 0;
402   Float_t b0, b1, b2;
403   Float_t x2 = x+x;
404   b1 = b2 = 0;
405   float dcf0=0,dcf1=0,dcf2=0;
406   float ddcf0=0,ddcf1,ddcf2=0;
407   //
408   dcf2 = 2*ncf*array[ncf]; 
409   --ncf; 
410
411   dcf1 = 2*ncf*array[ncf]; 
412   b0 = ddcf1 = 2*ncf*dcf2; 
413   //
414   if (!(--ncf)) return b0/2;
415   //
416   for (int i=ncf;i--;) {
417     b2 = b1;                        
418     b1 = b0;
419     dcf0  = dcf2 + 2*(i+1)*array[i+1];
420     ddcf0 = ddcf2 + 2*(i+1)*dcf1; 
421     b0 = ddcf0 + x2*b1 -b2;
422     //
423     ddcf2 = ddcf1;  
424     ddcf1 = ddcf0;
425     //
426     dcf2 = dcf1;
427     dcf1 = dcf0;
428     //
429   }
430   //
431   return b0 - x*b1 - ddcf0/2;
432 }
433
434 //__________________________________________________________________________________________
435 Int_t AliCheb3DCalc::GetMaxColsAtRow() const
436 {
437   int nmax3d = 0; 
438   for (int i=fNElemBound2D;i--;) if (fCoefBound2D0[i]>nmax3d) nmax3d = fCoefBound2D0[i];
439   return nmax3d;
440 }