1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 #include "AliCheb3DCalc.h"
20 ClassImp(AliCheb3DCalc)
22 //__________________________________________________________________________________________
23 AliCheb3DCalc::AliCheb3DCalc() :
37 // default constructor
40 //__________________________________________________________________________________________
41 AliCheb3DCalc::AliCheb3DCalc(const AliCheb3DCalc& src) :
46 fNElemBound2D(src.fNElemBound2D),
58 if (src.fNColsAtRow) {
59 fNColsAtRow = new UShort_t[fNRows];
60 for (int i=fNRows;i--;) fNColsAtRow[i] = src.fNColsAtRow[i];
62 if (src.fColAtRowBg) {
63 fColAtRowBg = new UShort_t[fNRows];
64 for (int i=fNRows;i--;) fColAtRowBg[i] = src.fColAtRowBg[i];
66 if (src.fCoefBound2D0) {
67 fCoefBound2D0 = new UShort_t[fNElemBound2D];
68 for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = src.fCoefBound2D0[i];
70 if (src.fCoefBound2D1) {
71 fCoefBound2D1 = new UShort_t[fNElemBound2D];
72 for (int i=fNElemBound2D;i--;) fCoefBound2D1[i] = src.fCoefBound2D1[i];
75 fCoefs = new Float_t[fNCoefs];
76 for (int i=fNCoefs;i--;) fCoefs[i] = src.fCoefs[i];
78 if (src.fTmpCf1) fTmpCf1 = new Float_t[fNCols];
79 if (src.fTmpCf0) fTmpCf0 = new Float_t[fNRows];
82 //__________________________________________________________________________________________
83 AliCheb3DCalc::AliCheb3DCalc(FILE* stream) :
97 // constructor from coeffs. streem
101 //__________________________________________________________________________________________
102 AliCheb3DCalc& AliCheb3DCalc::operator=(const AliCheb3DCalc& rhs)
104 // assignment operator
107 SetName(rhs.GetName());
108 SetTitle(rhs.GetTitle());
109 fNCoefs = rhs.fNCoefs;
113 if (rhs.fNColsAtRow) {
114 fNColsAtRow = new UShort_t[fNRows];
115 for (int i=fNRows;i--;) fNColsAtRow[i] = rhs.fNColsAtRow[i];
117 if (rhs.fColAtRowBg) {
118 fColAtRowBg = new UShort_t[fNRows];
119 for (int i=fNRows;i--;) fColAtRowBg[i] = rhs.fColAtRowBg[i];
121 if (rhs.fCoefBound2D0) {
122 fCoefBound2D0 = new UShort_t[fNElemBound2D];
123 for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = rhs.fCoefBound2D0[i];
125 if (rhs.fCoefBound2D1) {
126 fCoefBound2D1 = new UShort_t[fNElemBound2D];
127 for (int i=fNElemBound2D;i--;) fCoefBound2D1[i] = rhs.fCoefBound2D1[i];
130 fCoefs = new Float_t[fNCoefs];
131 for (int i=fNCoefs;i--;) fCoefs[i] = rhs.fCoefs[i];
133 if (rhs.fTmpCf1) fTmpCf1 = new Float_t[fNCols];
134 if (rhs.fTmpCf0) fTmpCf0 = new Float_t[fNRows];
139 //__________________________________________________________________________________________
140 void AliCheb3DCalc::Clear(const Option_t*)
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; }
153 //__________________________________________________________________________________________
154 void AliCheb3DCalc::Print(const Option_t* ) const
157 printf("Chebyshev parameterization data %s for 3D->1 function. Prec:%e\n",GetName(),fPrec);
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);
164 //__________________________________________________________________________________________
165 Float_t AliCheb3DCalc::EvalDeriv(int dim, const Float_t *par) const
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
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;}
175 int col0 = fColAtRowBg[id0]; // beginning of local column in the 2D boundary matrix
176 for (int id1=nCLoc;id1--;) {
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);
182 if (dim==1) fTmpCf0[id0] = ChebEval1Deriv(par[1],fTmpCf1,nCLoc);
183 else fTmpCf0[id0] = ChebEval1D(par[1],fTmpCf1,nCLoc);
185 return (dim==0) ? ChebEval1Deriv(par[0],fTmpCf0,fNRows) : ChebEval1D(par[0],fTmpCf0,fNRows);
189 //__________________________________________________________________________________________
190 Float_t AliCheb3DCalc::EvalDeriv2(int dim1,int dim2, const Float_t *par) const
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
195 Bool_t same = dim1==dim2;
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;}
201 int col0 = fColAtRowBg[id0]; // beginning of local column in the 2D boundary matrix
202 for (int id1=nCLoc;id1--;) {
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);
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);
212 return (dim1==0||dim2==0) ? (same ? ChebEval1Deriv2(par[0],fTmpCf0,fNRows):ChebEval1Deriv(par[0],fTmpCf0,fNRows)) :
213 ChebEval1D(par[0],fTmpCf0,fNRows);
217 //_______________________________________________
218 #ifdef _INC_CREATION_ALICHEB3D_
219 void AliCheb3DCalc::SaveData(const char* outfile,Bool_t append) const
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");
231 //_______________________________________________
232 #ifdef _INC_CREATION_ALICHEB3D_
233 void AliCheb3DCalc::SaveData(FILE* stream) const
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);
240 fprintf(stream,"# Number of columns per row\n");
241 for (int i=0;i<fNRows;i++) fprintf(stream,"%d\n",fNColsAtRow[i]);
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]);
246 fprintf(stream,"# Coefficients\n");
247 for (int i=0;i<fNCoefs;i++) fprintf(stream,"%+.8e\n",fCoefs[i]);
249 fprintf(stream,"# Precision\n");
250 fprintf(stream,"%+.8e\n",fPrec);
252 fprintf(stream,"END %s\n",GetName());
257 //_______________________________________________
258 void AliCheb3DCalc::LoadData(FILE* stream)
260 // Load coefs. from the stream
261 if (!stream) {Error("LoadData","No stream provided.\nStop"); exit(1);}
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);
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);}
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];
286 InitElemBound2D(fNElemBound2D);
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];
295 if (fNCoefs<=0) {Error("LoadData","Negtive (%d) number of Chebychef coeffs. is obtained.\nStop\n",fNCoefs);exit(1);}
298 for (int i=0;i<fNCoefs;i++) {
299 ReadLine(buffs,stream);
300 fCoefs[i] = buffs.Atof();
304 ReadLine(buffs,stream);
305 fPrec = buffs.Atof();
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());
316 //_______________________________________________
317 void AliCheb3DCalc::ReadLine(TString& str,FILE* stream)
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;
325 fprintf(stderr,"AliCheb3D::ReadLine: Failed to read from stream.\nStop");exit(1); // normally, should not reach here
328 //_______________________________________________
329 void AliCheb3DCalc::InitCols(int nc)
331 // Set max.number of significant columns in the coefs matrix
333 if (fTmpCf1) delete[] fTmpCf1;
334 fTmpCf1 = new Float_t [fNCols];
337 //_______________________________________________
338 void AliCheb3DCalc::InitRows(int nr)
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;
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;
351 //_______________________________________________
352 void AliCheb3DCalc::InitElemBound2D(int ne)
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;
358 fCoefBound2D0 = new UShort_t[fNElemBound2D];
359 fCoefBound2D1 = new UShort_t[fNElemBound2D];
360 for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = fCoefBound2D1[i] = 0;
363 //_______________________________________________
364 void AliCheb3DCalc::InitCoefs(int nc)
366 // Set total number of significant coefs
367 if (fCoefs) delete[] fCoefs;
369 fCoefs = new Float_t [fNCoefs];
370 for (int i=fNCoefs;i--;) fCoefs[i] = 0.0;
373 //__________________________________________________________________________________________
374 Float_t AliCheb3DCalc::ChebEval1Deriv(Float_t x, const Float_t * array, int ncf )
376 // evaluate 1D Chebyshev parameterization's derivative. x is the argument mapped to [-1:1] interval
377 if (--ncf<1) return 0;
381 float dcf0=0,dcf1,dcf2=0;
382 b0 = dcf1 = 2*ncf*array[ncf];
383 if (!(--ncf)) return b0/2;
385 for (int i=ncf;i--;) {
388 dcf0 = dcf2 + 2*(i+1)*array[i+1];
389 b0 = dcf0 + x2*b1 -b2;
394 return b0 - x*b1 - dcf0/2;
397 //__________________________________________________________________________________________
398 Float_t AliCheb3DCalc::ChebEval1Deriv2(Float_t x, const Float_t * array, int ncf )
400 // evaluate 1D Chebyshev parameterization's 2nd derivative. x is the argument mapped to [-1:1] interval
401 if (--ncf<2) return 0;
405 float dcf0=0,dcf1=0,dcf2=0;
406 float ddcf0=0,ddcf1,ddcf2=0;
408 dcf2 = 2*ncf*array[ncf];
411 dcf1 = 2*ncf*array[ncf];
412 b0 = ddcf1 = 2*ncf*dcf2;
414 if (!(--ncf)) return b0/2;
416 for (int i=ncf;i--;) {
419 dcf0 = dcf2 + 2*(i+1)*array[i+1];
420 ddcf0 = ddcf2 + 2*(i+1)*dcf1;
421 b0 = ddcf0 + x2*b1 -b2;
431 return b0 - x*b1 - ddcf0/2;
434 //__________________________________________________________________________________________
435 Int_t AliCheb3DCalc::GetMaxColsAtRow() const
438 for (int i=fNElemBound2D;i--;) if (fCoefBound2D0[i]>nmax3d) nmax3d = fCoefBound2D0[i];