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