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