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 **************************************************************************/
17 // Author: ruben.shahoyan@cern.ch 09/09/2006
19 #include "AliCheb3DCalc.h"
21 ClassImp(AliCheb3DCalc)
23 //__________________________________________________________________________________________
24 AliCheb3DCalc::AliCheb3DCalc() :
38 //__________________________________________________________________________________________
39 AliCheb3DCalc::AliCheb3DCalc(const AliCheb3DCalc& src) :
44 fNElemBound2D(src.fNElemBound2D),
53 if (src.fNColsAtRow) {
54 fNColsAtRow = new Int_t[fNRows];
55 for (int i=fNRows;i--;) fNColsAtRow[i] = src.fNColsAtRow[i];
57 if (src.fColAtRowBg) {
58 fColAtRowBg = new Int_t[fNRows];
59 for (int i=fNRows;i--;) fColAtRowBg[i] = src.fColAtRowBg[i];
61 if (src.fCoefBound2D0) {
62 fCoefBound2D0 = new Int_t[fNElemBound2D];
63 for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = src.fCoefBound2D0[i];
65 if (src.fCoefBound2D1) {
66 fCoefBound2D1 = new Int_t[fNElemBound2D];
67 for (int i=fNElemBound2D;i--;) fCoefBound2D1[i] = src.fCoefBound2D1[i];
70 fCoefs = new Float_t[fNCoefs];
71 for (int i=fNCoefs;i--;) fCoefs[i] = src.fCoefs[i];
73 if (src.fTmpCf1) fTmpCf1 = new Float_t[fNCols];
74 if (src.fTmpCf0) fTmpCf0 = new Float_t[fNRows];
77 //__________________________________________________________________________________________
78 AliCheb3DCalc::AliCheb3DCalc(FILE* stream) :
94 //__________________________________________________________________________________________
95 AliCheb3DCalc& AliCheb3DCalc::operator=(const AliCheb3DCalc& rhs)
99 SetName(rhs.GetName());
100 SetTitle(rhs.GetTitle());
101 fNCoefs = rhs.fNCoefs;
104 if (rhs.fNColsAtRow) {
105 fNColsAtRow = new Int_t[fNRows];
106 for (int i=fNRows;i--;) fNColsAtRow[i] = rhs.fNColsAtRow[i];
108 if (rhs.fColAtRowBg) {
109 fColAtRowBg = new Int_t[fNRows];
110 for (int i=fNRows;i--;) fColAtRowBg[i] = rhs.fColAtRowBg[i];
112 if (rhs.fCoefBound2D0) {
113 fCoefBound2D0 = new Int_t[fNElemBound2D];
114 for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = rhs.fCoefBound2D0[i];
116 if (rhs.fCoefBound2D1) {
117 fCoefBound2D1 = new Int_t[fNElemBound2D];
118 for (int i=fNElemBound2D;i--;) fCoefBound2D1[i] = rhs.fCoefBound2D1[i];
121 fCoefs = new Float_t[fNCoefs];
122 for (int i=fNCoefs;i--;) fCoefs[i] = rhs.fCoefs[i];
124 if (rhs.fTmpCf1) fTmpCf1 = new Float_t[fNCols];
125 if (rhs.fTmpCf0) fTmpCf0 = new Float_t[fNRows];
130 //__________________________________________________________________________________________
131 void AliCheb3DCalc::Clear(Option_t*)
133 // delete all dynamycally allocated structures
134 if (fTmpCf1) { delete[] fTmpCf1; fTmpCf1 = 0;}
135 if (fTmpCf0) { delete[] fTmpCf0; fTmpCf0 = 0;}
136 if (fCoefs) { delete[] fCoefs; fCoefs = 0;}
137 if (fCoefBound2D0) { delete[] fCoefBound2D0; fCoefBound2D0 = 0; }
138 if (fCoefBound2D1) { delete[] fCoefBound2D1; fCoefBound2D1 = 0; }
139 if (fNColsAtRow) { delete[] fNColsAtRow; fNColsAtRow = 0; }
140 if (fColAtRowBg) { delete[] fColAtRowBg; fColAtRowBg = 0; }
144 //__________________________________________________________________________________________
145 void AliCheb3DCalc::Print(Option_t* ) const
147 printf("Chebyshev parameterization data %s for 3D->1 function.\n",GetName());
149 for (int i=fNElemBound2D;i--;) if (fCoefBound2D0[i]>nmax3d) nmax3d = fCoefBound2D0[i];
150 printf("%d coefficients in %dx%dx%d matrix\n",fNCoefs,fNRows,fNCols,nmax3d);
154 //__________________________________________________________________________________________
155 Float_t AliCheb3DCalc::Eval(Float_t *par) const
157 // evaluate Chebyshev parameterization for 3D function.
158 // VERY IMPORTANT: par must contain the function arguments ALREADY MAPPED to [-1:1] interval
164 for (int id0=fNRows;id0--;) {
165 int nCLoc = fNColsAtRow[id0]; // number of significant coefs on this row
166 int Col0 = fColAtRowBg[id0]; // beginning of local column in the 2D boundary matrix
167 for (int id1=nCLoc;id1--;) {
169 fTmpCf1[id1] = (ncfRC=fCoefBound2D0[id]) ? ChebEval1D(z,fCoefs + fCoefBound2D1[id], ncfRC) : 0.0;
171 fTmpCf0[id0] = nCLoc>0 ? ChebEval1D(y,fTmpCf1,nCLoc):0.0;
173 return ChebEval1D(x,fTmpCf0,fNRows);
177 //__________________________________________________________________________________________
178 Float_t AliCheb3DCalc::EvalDeriv(int dim, Float_t *par) const
180 // evaluate Chebyshev parameterization derivative in given dimension for 3D function.
181 // VERY IMPORTANT: par must contain the function arguments ALREADY MAPPED to [-1:1] interval
187 for (int id0=fNRows;id0--;) {
188 int nCLoc = fNColsAtRow[id0]; // number of significant coefs on this row
189 if (!nCLoc) {fTmpCf0[id0]=0; continue;}
191 int Col0 = fColAtRowBg[id0]; // beginning of local column in the 2D boundary matrix
192 for (int id1=nCLoc;id1--;) {
194 if (!(ncfRC=fCoefBound2D0[id])) { fTmpCf1[id1]=0; continue;}
195 if (dim==2) fTmpCf1[id1] = ChebEval1Deriv(z,fCoefs + fCoefBound2D1[id], ncfRC);
196 else fTmpCf1[id1] = ChebEval1D(z,fCoefs + fCoefBound2D1[id], ncfRC);
198 if (dim==1) fTmpCf0[id0] = ChebEval1Deriv(y,fTmpCf1,nCLoc);
199 else fTmpCf0[id0] = ChebEval1D(y,fTmpCf1,nCLoc);
201 return (dim==0) ? ChebEval1Deriv(x,fTmpCf0,fNRows) : ChebEval1D(x,fTmpCf0,fNRows);
205 //__________________________________________________________________________________________
206 Float_t AliCheb3DCalc::EvalDeriv2(int dim1,int dim2, Float_t *par) const
208 // evaluate Chebyshev parameterization 2n derivative in given dimensions for 3D function.
209 // VERY IMPORTANT: par must contain the function arguments ALREADY MAPPED to [-1:1] interval
214 Bool_t same = dim1==dim2;
216 for (int id0=fNRows;id0--;) {
217 int nCLoc = fNColsAtRow[id0]; // number of significant coefs on this row
218 if (!nCLoc) {fTmpCf0[id0]=0; continue;}
220 int Col0 = fColAtRowBg[id0]; // beginning of local column in the 2D boundary matrix
221 for (int id1=nCLoc;id1--;) {
223 if (!(ncfRC=fCoefBound2D0[id])) { fTmpCf1[id1]=0; continue;}
224 if (dim1==2||dim2==2) fTmpCf1[id1] = same ? ChebEval1Deriv2(z,fCoefs + fCoefBound2D1[id], ncfRC)
225 : ChebEval1Deriv(z,fCoefs + fCoefBound2D1[id], ncfRC);
226 else fTmpCf1[id1] = ChebEval1D(z,fCoefs + fCoefBound2D1[id], ncfRC);
228 if (dim1==1||dim2==1) fTmpCf0[id0] = same ? ChebEval1Deriv2(y,fTmpCf1,nCLoc):ChebEval1Deriv(y,fTmpCf1,nCLoc);
229 else fTmpCf0[id0] = ChebEval1D(y,fTmpCf1,nCLoc);
231 return (dim1==0||dim2==0) ? (same ? ChebEval1Deriv2(x,fTmpCf0,fNRows):ChebEval1Deriv(x,fTmpCf0,fNRows)) : ChebEval1D(x,fTmpCf0,fNRows);
235 //_______________________________________________
236 #ifdef _INC_CREATION_ALICHEB3D_
237 void AliCheb3DCalc::SaveData(const char* outfile,Bool_t append) const
239 // writes coefficients data to output text file, optionallt appending on the end of existing file
240 TString strf = outfile;
241 gSystem->ExpandPathName(strf);
242 FILE* stream = fopen(strf,append ? "a":"w");
249 //_______________________________________________
250 #ifdef _INC_CREATION_ALICHEB3D_
251 void AliCheb3DCalc::SaveData(FILE* stream) const
253 // writes coefficients data to existing output stream
254 // Note: fNCols, fNElemBound2D and fColAtRowBg is not stored, will be computed on fly during the loading of this file
255 fprintf(stream,"#\nSTART %s\n",GetName());
256 fprintf(stream,"# Number of rows\n%d\n",fNRows);
258 fprintf(stream,"# Number of columns per row\n");
259 for (int i=0;i<fNRows;i++) fprintf(stream,"%d\n",fNColsAtRow[i]);
261 fprintf(stream,"# Number of Coefs in each significant block of third dimension\n");
262 for (int i=0;i<fNElemBound2D;i++) fprintf(stream,"%d\n",fCoefBound2D0[i]);
264 fprintf(stream,"# Coefficients\n");
265 for (int i=0;i<fNCoefs;i++) fprintf(stream,"%+.8e\n",fCoefs[i]);
266 fprintf(stream,"END %s\n",GetName());
271 //_______________________________________________
272 void AliCheb3DCalc::LoadData(FILE* stream)
274 // Load coefs. from the stream
275 if (!stream) {Error("LoadData","No stream provided.\nStop"); exit(1);}
278 ReadLine(buffs,stream);
279 if (!buffs.BeginsWith("START")) {Error("LoadData","Expected: \"START <fit_name>\", found \"%s\"\nStop\n",buffs.Data());exit(1);}
280 if (buffs.First(' ')>0) SetName(buffs.Data()+buffs.First(' ')+1);
282 ReadLine(buffs,stream); // NRows
283 fNRows = buffs.Atoi();
284 if (fNRows<1) {Error("LoadData","Expected: '<number_of_rows>', found \"%s\"\nStop\n",buffs.Data());exit(1);}
290 for (int id0=0;id0<fNRows;id0++) {
291 ReadLine(buffs,stream); // n.cols at this row
292 fNColsAtRow[id0] = buffs.Atoi();
293 fColAtRowBg[id0] = fNElemBound2D; // begining of this row in 2D boundary surface
294 fNElemBound2D += fNColsAtRow[id0];
295 if (fNCols<fNColsAtRow[id0]) fNCols = fNColsAtRow[id0];
300 InitElemBound2D(fNElemBound2D);
302 for (int i=0;i<fNElemBound2D;i++) {
303 ReadLine(buffs,stream); // n.coeffs at 3-d dimension for the given column/row
304 fCoefBound2D0[i] = buffs.Atoi();
305 fCoefBound2D1[i] = fNCoefs;
306 fNCoefs += fCoefBound2D0[i];
309 if (fNCoefs<=0) {Error("LoadData","Negtive (%d) number of Chebychef coeffs. is obtained.\nStop\n",fNCoefs);exit(1);}
312 for (int i=0;i<fNCoefs;i++) {
313 ReadLine(buffs,stream);
314 fCoefs[i] = buffs.Atof();
316 // check end_of_data record
317 ReadLine(buffs,stream);
318 if (!buffs.BeginsWith("END") || !buffs.Contains(GetName())) {
319 Error("LoadData","Expected \"END %s\", found \"%s\".\nStop\n",GetName(),buffs.Data());
325 //_______________________________________________
326 void AliCheb3DCalc::ReadLine(TString& str,FILE* stream)
328 // read single line from the stream, skipping empty and commented lines. EOF is not expected
329 while (str.Gets(stream)) {
330 str = str.Strip(TString::kBoth,' ');
331 if (str.IsNull()||str.BeginsWith("#")) continue;
334 fprintf(stderr,"AliCheb3D::ReadLine: Failed to read from stream.\nStop");exit(1); // normally, should not reach here
337 //_______________________________________________
338 void AliCheb3DCalc::InitCols(int nc)
340 // Set max.number of significant columns in the coefs matrix
342 if (fTmpCf1) delete[] fTmpCf1;
343 fTmpCf1 = new Float_t [fNCols];
346 //_______________________________________________
347 void AliCheb3DCalc::InitRows(int nr)
349 // Set max.number of significant rows in the coefs matrix
350 if (fNColsAtRow) delete[] fNColsAtRow;
351 if (fColAtRowBg) delete[] fColAtRowBg;
352 if (fTmpCf0) delete[] fTmpCf0;
354 fNColsAtRow = new Int_t[fNRows];
355 fTmpCf0 = new Float_t [fNRows];
356 fColAtRowBg = new Int_t[fNRows];
357 for (int i=fNRows;i--;) fNColsAtRow[i] = fColAtRowBg[i] = 0;
360 //_______________________________________________
361 void AliCheb3DCalc::InitElemBound2D(int ne)
363 // Set max number of significant coefs for given row/column of coefs 3D matrix
364 if (fCoefBound2D0) delete[] fCoefBound2D0;
365 if (fCoefBound2D1) delete[] fCoefBound2D1;
367 fCoefBound2D0 = new Int_t[fNElemBound2D];
368 fCoefBound2D1 = new Int_t[fNElemBound2D];
369 for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = fCoefBound2D1[i] = 0;
372 //_______________________________________________
373 void AliCheb3DCalc::InitCoefs(int nc)
375 // Set total number of significant coefs
376 if (fCoefs) delete[] fCoefs;
378 fCoefs = new Float_t [fNCoefs];
379 for (int i=fNCoefs;i--;) fCoefs[i] = 0.0;
382 //__________________________________________________________________________________________
383 Float_t AliCheb3DCalc::ChebEval1Deriv(Float_t x, const Float_t * array, int ncf )
385 // evaluate 1D Chebyshev parameterization's derivative. x is the argument mapped to [-1:1] interval
386 if (--ncf<1) return 0;
390 float dcf0=0,dcf1,dcf2=0;
391 b0 = dcf1 = 2*ncf*array[ncf];
392 if (!(--ncf)) return b0/2;
394 for (int i=ncf;i--;) {
397 dcf0 = dcf2 + 2*(i+1)*array[i+1];
398 b0 = dcf0 + x2*b1 -b2;
403 return b0 - x*b1 - dcf0/2;
406 //__________________________________________________________________________________________
407 Float_t AliCheb3DCalc::ChebEval1Deriv2(Float_t x, const Float_t * array, int ncf )
409 // evaluate 1D Chebyshev parameterization's 2nd derivative. x is the argument mapped to [-1:1] interval
410 if (--ncf<2) return 0;
414 float dcf0=0,dcf1=0,dcf2=0;
415 float ddcf0=0,ddcf1,ddcf2=0;
417 dcf2 = 2*ncf*array[ncf];
420 dcf1 = 2*ncf*array[ncf];
421 b0 = ddcf1 = 2*ncf*dcf2;
423 if (!(--ncf)) return b0/2;
425 for (int i=ncf;i--;) {
428 dcf0 = dcf2 + 2*(i+1)*array[i+1];
429 ddcf0 = ddcf2 + 2*(i+1)*dcf1;
430 b0 = ddcf0 + x2*b1 -b2;
440 return b0 - x*b1 - ddcf0/2;