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() :
36 // default constructor
39 //__________________________________________________________________________________________
40 AliCheb3DCalc::AliCheb3DCalc(const AliCheb3DCalc& src) :
45 fNElemBound2D(src.fNElemBound2D),
56 if (src.fNColsAtRow) {
57 fNColsAtRow = new UShort_t[fNRows];
58 for (int i=fNRows;i--;) fNColsAtRow[i] = src.fNColsAtRow[i];
60 if (src.fColAtRowBg) {
61 fColAtRowBg = new UShort_t[fNRows];
62 for (int i=fNRows;i--;) fColAtRowBg[i] = src.fColAtRowBg[i];
64 if (src.fCoefBound2D0) {
65 fCoefBound2D0 = new UShort_t[fNElemBound2D];
66 for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = src.fCoefBound2D0[i];
68 if (src.fCoefBound2D1) {
69 fCoefBound2D1 = new UShort_t[fNElemBound2D];
70 for (int i=fNElemBound2D;i--;) fCoefBound2D1[i] = src.fCoefBound2D1[i];
73 fCoefs = new Float_t[fNCoefs];
74 for (int i=fNCoefs;i--;) fCoefs[i] = src.fCoefs[i];
76 if (src.fTmpCf1) fTmpCf1 = new Float_t[fNCols];
77 if (src.fTmpCf0) fTmpCf0 = new Float_t[fNRows];
80 //__________________________________________________________________________________________
81 AliCheb3DCalc::AliCheb3DCalc(FILE* stream) :
94 // constructor from coeffs. streem
98 //__________________________________________________________________________________________
99 AliCheb3DCalc& AliCheb3DCalc::operator=(const AliCheb3DCalc& rhs)
101 // assignment operator
104 SetName(rhs.GetName());
105 SetTitle(rhs.GetTitle());
106 fNCoefs = rhs.fNCoefs;
109 if (rhs.fNColsAtRow) {
110 fNColsAtRow = new UShort_t[fNRows];
111 for (int i=fNRows;i--;) fNColsAtRow[i] = rhs.fNColsAtRow[i];
113 if (rhs.fColAtRowBg) {
114 fColAtRowBg = new UShort_t[fNRows];
115 for (int i=fNRows;i--;) fColAtRowBg[i] = rhs.fColAtRowBg[i];
117 if (rhs.fCoefBound2D0) {
118 fCoefBound2D0 = new UShort_t[fNElemBound2D];
119 for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = rhs.fCoefBound2D0[i];
121 if (rhs.fCoefBound2D1) {
122 fCoefBound2D1 = new UShort_t[fNElemBound2D];
123 for (int i=fNElemBound2D;i--;) fCoefBound2D1[i] = rhs.fCoefBound2D1[i];
126 fCoefs = new Float_t[fNCoefs];
127 for (int i=fNCoefs;i--;) fCoefs[i] = rhs.fCoefs[i];
129 if (rhs.fTmpCf1) fTmpCf1 = new Float_t[fNCols];
130 if (rhs.fTmpCf0) fTmpCf0 = new Float_t[fNRows];
135 //__________________________________________________________________________________________
136 void AliCheb3DCalc::Clear(const Option_t*)
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; }
149 //__________________________________________________________________________________________
150 void AliCheb3DCalc::Print(const Option_t* ) const
153 printf("Chebyshev parameterization data %s for 3D->1 function.\n",GetName());
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);
160 //__________________________________________________________________________________________
161 Float_t AliCheb3DCalc::EvalDeriv(int dim, const Float_t *par) const
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
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;}
171 int col0 = fColAtRowBg[id0]; // beginning of local column in the 2D boundary matrix
172 for (int id1=nCLoc;id1--;) {
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);
178 if (dim==1) fTmpCf0[id0] = ChebEval1Deriv(par[1],fTmpCf1,nCLoc);
179 else fTmpCf0[id0] = ChebEval1D(par[1],fTmpCf1,nCLoc);
181 return (dim==0) ? ChebEval1Deriv(par[0],fTmpCf0,fNRows) : ChebEval1D(par[0],fTmpCf0,fNRows);
185 //__________________________________________________________________________________________
186 Float_t AliCheb3DCalc::EvalDeriv2(int dim1,int dim2, const Float_t *par) const
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
191 Bool_t same = dim1==dim2;
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;}
197 int col0 = fColAtRowBg[id0]; // beginning of local column in the 2D boundary matrix
198 for (int id1=nCLoc;id1--;) {
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);
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);
208 return (dim1==0||dim2==0) ? (same ? ChebEval1Deriv2(par[0],fTmpCf0,fNRows):ChebEval1Deriv(par[0],fTmpCf0,fNRows)) :
209 ChebEval1D(par[0],fTmpCf0,fNRows);
213 //_______________________________________________
214 #ifdef _INC_CREATION_ALICHEB3D_
215 void AliCheb3DCalc::SaveData(const char* outfile,Bool_t append) const
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");
227 //_______________________________________________
228 #ifdef _INC_CREATION_ALICHEB3D_
229 void AliCheb3DCalc::SaveData(FILE* stream) const
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);
236 fprintf(stream,"# Number of columns per row\n");
237 for (int i=0;i<fNRows;i++) fprintf(stream,"%d\n",fNColsAtRow[i]);
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]);
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());
249 //_______________________________________________
250 void AliCheb3DCalc::LoadData(FILE* stream)
252 // Load coefs. from the stream
253 if (!stream) {Error("LoadData","No stream provided.\nStop"); exit(1);}
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);
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);}
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];
278 InitElemBound2D(fNElemBound2D);
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];
287 if (fNCoefs<=0) {Error("LoadData","Negtive (%d) number of Chebychef coeffs. is obtained.\nStop\n",fNCoefs);exit(1);}
290 for (int i=0;i<fNCoefs;i++) {
291 ReadLine(buffs,stream);
292 fCoefs[i] = buffs.Atof();
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());
303 //_______________________________________________
304 void AliCheb3DCalc::ReadLine(TString& str,FILE* stream)
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;
312 fprintf(stderr,"AliCheb3D::ReadLine: Failed to read from stream.\nStop");exit(1); // normally, should not reach here
315 //_______________________________________________
316 void AliCheb3DCalc::InitCols(int nc)
318 // Set max.number of significant columns in the coefs matrix
320 if (fTmpCf1) delete[] fTmpCf1;
321 fTmpCf1 = new Float_t [fNCols];
324 //_______________________________________________
325 void AliCheb3DCalc::InitRows(int nr)
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;
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;
338 //_______________________________________________
339 void AliCheb3DCalc::InitElemBound2D(int ne)
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;
345 fCoefBound2D0 = new UShort_t[fNElemBound2D];
346 fCoefBound2D1 = new UShort_t[fNElemBound2D];
347 for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = fCoefBound2D1[i] = 0;
350 //_______________________________________________
351 void AliCheb3DCalc::InitCoefs(int nc)
353 // Set total number of significant coefs
354 if (fCoefs) delete[] fCoefs;
356 fCoefs = new Float_t [fNCoefs];
357 for (int i=fNCoefs;i--;) fCoefs[i] = 0.0;
360 //__________________________________________________________________________________________
361 Float_t AliCheb3DCalc::ChebEval1Deriv(Float_t x, const Float_t * array, int ncf )
363 // evaluate 1D Chebyshev parameterization's derivative. x is the argument mapped to [-1:1] interval
364 if (--ncf<1) return 0;
368 float dcf0=0,dcf1,dcf2=0;
369 b0 = dcf1 = 2*ncf*array[ncf];
370 if (!(--ncf)) return b0/2;
372 for (int i=ncf;i--;) {
375 dcf0 = dcf2 + 2*(i+1)*array[i+1];
376 b0 = dcf0 + x2*b1 -b2;
381 return b0 - x*b1 - dcf0/2;
384 //__________________________________________________________________________________________
385 Float_t AliCheb3DCalc::ChebEval1Deriv2(Float_t x, const Float_t * array, int ncf )
387 // evaluate 1D Chebyshev parameterization's 2nd derivative. x is the argument mapped to [-1:1] interval
388 if (--ncf<2) return 0;
392 float dcf0=0,dcf1=0,dcf2=0;
393 float ddcf0=0,ddcf1,ddcf2=0;
395 dcf2 = 2*ncf*array[ncf];
398 dcf1 = 2*ncf*array[ncf];
399 b0 = ddcf1 = 2*ncf*dcf2;
401 if (!(--ncf)) return b0/2;
403 for (int i=ncf;i--;) {
406 dcf0 = dcf2 + 2*(i+1)*array[i+1];
407 ddcf0 = ddcf2 + 2*(i+1)*dcf1;
408 b0 = ddcf0 + x2*b1 -b2;
418 return b0 - x*b1 - ddcf0/2;
421 //__________________________________________________________________________________________
422 Int_t AliCheb3DCalc::GetMaxColsAtRow() const
425 for (int i=fNElemBound2D;i--;) if (fCoefBound2D0[i]>nmax3d) nmax3d = fCoefBound2D0[i];