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
20 #include "AliCheb3DCalc.h"
22 ClassImp(AliCheb3DCalc)
24 //__________________________________________________________________________________________
25 AliCheb3DCalc::AliCheb3DCalc() :
39 //__________________________________________________________________________________________
40 AliCheb3DCalc::AliCheb3DCalc(const AliCheb3DCalc& src) :
45 fNElemBound2D(src.fNElemBound2D),
54 if (src.fNColsAtRow) {
55 fNColsAtRow = new Int_t[fNRows];
56 for (int i=fNRows;i--;) fNColsAtRow[i] = src.fNColsAtRow[i];
58 if (src.fColAtRowBg) {
59 fColAtRowBg = new Int_t[fNRows];
60 for (int i=fNRows;i--;) fColAtRowBg[i] = src.fColAtRowBg[i];
62 if (src.fCoefBound2D0) {
63 fCoefBound2D0 = new Int_t[fNElemBound2D];
64 for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = src.fCoefBound2D0[i];
66 if (src.fCoefBound2D1) {
67 fCoefBound2D1 = new Int_t[fNElemBound2D];
68 for (int i=fNElemBound2D;i--;) fCoefBound2D1[i] = src.fCoefBound2D1[i];
71 fCoefs = new Float_t[fNCoefs];
72 for (int i=fNCoefs;i--;) fCoefs[i] = src.fCoefs[i];
74 if (src.fTmpCf1) fTmpCf1 = new Float_t[fNCols];
75 if (src.fTmpCf0) fTmpCf0 = new Float_t[fNRows];
78 //__________________________________________________________________________________________
79 AliCheb3DCalc::AliCheb3DCalc(FILE* stream) :
95 //__________________________________________________________________________________________
96 AliCheb3DCalc& AliCheb3DCalc::operator=(const AliCheb3DCalc& rhs)
100 SetName(rhs.GetName());
101 SetTitle(rhs.GetTitle());
102 fNCoefs = rhs.fNCoefs;
105 if (rhs.fNColsAtRow) {
106 fNColsAtRow = new Int_t[fNRows];
107 for (int i=fNRows;i--;) fNColsAtRow[i] = rhs.fNColsAtRow[i];
109 if (rhs.fColAtRowBg) {
110 fColAtRowBg = new Int_t[fNRows];
111 for (int i=fNRows;i--;) fColAtRowBg[i] = rhs.fColAtRowBg[i];
113 if (rhs.fCoefBound2D0) {
114 fCoefBound2D0 = new Int_t[fNElemBound2D];
115 for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = rhs.fCoefBound2D0[i];
117 if (rhs.fCoefBound2D1) {
118 fCoefBound2D1 = new Int_t[fNElemBound2D];
119 for (int i=fNElemBound2D;i--;) fCoefBound2D1[i] = rhs.fCoefBound2D1[i];
122 fCoefs = new Float_t[fNCoefs];
123 for (int i=fNCoefs;i--;) fCoefs[i] = rhs.fCoefs[i];
125 if (rhs.fTmpCf1) fTmpCf1 = new Float_t[fNCols];
126 if (rhs.fTmpCf0) fTmpCf0 = new Float_t[fNRows];
131 //__________________________________________________________________________________________
132 void AliCheb3DCalc::Clear(Option_t*)
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; }
145 //__________________________________________________________________________________________
146 void AliCheb3DCalc::Print(Option_t* ) const
148 printf("Chebyshev parameterization data %s for 3D->1 function.\n",GetName());
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);
155 //__________________________________________________________________________________________
156 Float_t AliCheb3DCalc::Eval(Float_t *par) const
158 // evaluate Chebyshev parameterization for 3D function.
159 // VERY IMPORTANT: par must contain the function arguments ALREADY MAPPED to [-1:1] interval
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--;) {
170 fTmpCf1[id1] = (ncfRC=fCoefBound2D0[id]) ? ChebEval1D(z,fCoefs + fCoefBound2D1[id], ncfRC) : 0.0;
172 fTmpCf0[id0] = nCLoc>0 ? ChebEval1D(y,fTmpCf1,nCLoc):0.0;
174 return ChebEval1D(x,fTmpCf0,fNRows);
178 //__________________________________________________________________________________________
179 Float_t AliCheb3DCalc::EvalDeriv(int dim, Float_t *par) const
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
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;}
192 int Col0 = fColAtRowBg[id0]; // beginning of local column in the 2D boundary matrix
193 for (int id1=nCLoc;id1--;) {
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);
199 if (dim==1) fTmpCf0[id0] = ChebEval1Deriv(y,fTmpCf1,nCLoc);
200 else fTmpCf0[id0] = ChebEval1D(y,fTmpCf1,nCLoc);
202 return (dim==0) ? ChebEval1Deriv(x,fTmpCf0,fNRows) : ChebEval1D(x,fTmpCf0,fNRows);
206 //__________________________________________________________________________________________
207 Float_t AliCheb3DCalc::EvalDeriv2(int dim1,int dim2, Float_t *par) const
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
215 Bool_t same = dim1==dim2;
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;}
221 int Col0 = fColAtRowBg[id0]; // beginning of local column in the 2D boundary matrix
222 for (int id1=nCLoc;id1--;) {
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);
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);
232 return (dim1==0||dim2==0) ? (same ? ChebEval1Deriv2(x,fTmpCf0,fNRows):ChebEval1Deriv(x,fTmpCf0,fNRows)) : ChebEval1D(x,fTmpCf0,fNRows);
236 //_______________________________________________
237 #ifdef _INC_CREATION_ALICHEB3D_
238 void AliCheb3DCalc::SaveData(const char* outfile,Bool_t append) const
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");
250 //_______________________________________________
251 #ifdef _INC_CREATION_ALICHEB3D_
252 void AliCheb3DCalc::SaveData(FILE* stream) const
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);
259 fprintf(stream,"# Number of columns per row\n");
260 for (int i=0;i<fNRows;i++) fprintf(stream,"%d\n",fNColsAtRow[i]);
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]);
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());
272 //_______________________________________________
273 void AliCheb3DCalc::LoadData(FILE* stream)
275 // Load coefs. from the stream
276 if (!stream) {Error("LoadData","No stream provided.\nStop"); exit(1);}
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);
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);}
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];
301 InitElemBound2D(fNElemBound2D);
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];
310 if (fNCoefs<=0) {Error("LoadData","Negtive (%d) number of Chebychef coeffs. is obtained.\nStop\n",fNCoefs);exit(1);}
313 for (int i=0;i<fNCoefs;i++) {
314 ReadLine(buffs,stream);
315 fCoefs[i] = buffs.Atof();
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());
326 //_______________________________________________
327 void AliCheb3DCalc::ReadLine(TString& str,FILE* stream)
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;
335 fprintf(stderr,"AliCheb3D::ReadLine: Failed to read from stream.\nStop");exit(1); // normally, should not reach here
338 //_______________________________________________
339 void AliCheb3DCalc::InitCols(int nc)
341 // Set max.number of significant columns in the coefs matrix
343 if (fTmpCf1) delete[] fTmpCf1;
344 fTmpCf1 = new Float_t [fNCols];
347 //_______________________________________________
348 void AliCheb3DCalc::InitRows(int nr)
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;
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;
361 //_______________________________________________
362 void AliCheb3DCalc::InitElemBound2D(int ne)
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;
368 fCoefBound2D0 = new Int_t[fNElemBound2D];
369 fCoefBound2D1 = new Int_t[fNElemBound2D];
370 for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = fCoefBound2D1[i] = 0;
373 //_______________________________________________
374 void AliCheb3DCalc::InitCoefs(int nc)
376 // Set total number of significant coefs
377 if (fCoefs) delete[] fCoefs;
379 fCoefs = new Float_t [fNCoefs];
380 for (int i=fNCoefs;i--;) fCoefs[i] = 0.0;
383 //__________________________________________________________________________________________
384 Float_t AliCheb3DCalc::ChebEval1Deriv(Float_t x, const Float_t * array, int ncf )
386 // evaluate 1D Chebyshev parameterization's derivative. x is the argument mapped to [-1:1] interval
387 if (--ncf<1) return 0;
391 float dcf0=0,dcf1,dcf2=0;
392 b0 = dcf1 = 2*ncf*array[ncf];
393 if (!(--ncf)) return b0/2;
395 for (int i=ncf;i--;) {
398 dcf0 = dcf2 + 2*(i+1)*array[i+1];
399 b0 = dcf0 + x2*b1 -b2;
404 return b0 - x*b1 - dcf0/2;
407 //__________________________________________________________________________________________
408 Float_t AliCheb3DCalc::ChebEval1Deriv2(Float_t x, const Float_t * array, int ncf )
410 // evaluate 1D Chebyshev parameterization's 2nd derivative. x is the argument mapped to [-1:1] interval
411 if (--ncf<2) return 0;
415 float dcf0=0,dcf1=0,dcf2=0;
416 float ddcf0=0,ddcf1,ddcf2=0;
418 dcf2 = 2*ncf*array[ncf];
421 dcf1 = 2*ncf*array[ncf];
422 b0 = ddcf1 = 2*ncf*dcf2;
424 if (!(--ncf)) return b0/2;
426 for (int i=ncf;i--;) {
429 dcf0 = dcf2 + 2*(i+1)*array[i+1];
430 ddcf0 = ddcf2 + 2*(i+1)*dcf1;
431 b0 = ddcf0 + x2*b1 -b2;
441 return b0 - x*b1 - ddcf0/2;