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 int Col0 = fColAtRowBg[id0]; // beginning of local column in the 2D boundary matrix
190 for (int id1=nCLoc;id1--;) {
192 if (dim==2) fTmpCf1[id1] = (ncfRC=fCoefBound2D0[id]) ? ChebEval1Deriv(z,fCoefs + fCoefBound2D1[id], ncfRC) : 0.0;
193 else fTmpCf1[id1] = (ncfRC=fCoefBound2D0[id]) ? ChebEval1D(z,fCoefs + fCoefBound2D1[id], ncfRC) : 0.0;
195 if (dim==1) fTmpCf0[id0] = nCLoc>0 ? ChebEval1Deriv(y,fTmpCf1,nCLoc):0.0;
196 else fTmpCf0[id0] = nCLoc>0 ? ChebEval1D(y,fTmpCf1,nCLoc):0.0;
198 return (dim==0) ? ChebEval1Deriv(x,fTmpCf0,fNRows) : ChebEval1D(x,fTmpCf0,fNRows);
202 //_______________________________________________
203 #ifdef _INC_CREATION_ALICHEB3D_
204 void AliCheb3DCalc::SaveData(const char* outfile,Bool_t append) const
206 // writes coefficients data to output text file, optionallt appending on the end of existing file
207 TString strf = outfile;
208 gSystem->ExpandPathName(strf);
209 FILE* stream = fopen(strf,append ? "a":"w");
216 //_______________________________________________
217 #ifdef _INC_CREATION_ALICHEB3D_
218 void AliCheb3DCalc::SaveData(FILE* stream) const
220 // writes coefficients data to existing output stream
221 // Note: fNCols, fNElemBound2D and fColAtRowBg is not stored, will be computed on fly during the loading of this file
222 fprintf(stream,"#\nSTART %s\n",GetName());
223 fprintf(stream,"# Number of rows\n%d\n",fNRows);
225 fprintf(stream,"# Number of columns per row\n");
226 for (int i=0;i<fNRows;i++) fprintf(stream,"%d\n",fNColsAtRow[i]);
228 fprintf(stream,"# Number of Coefs in each significant block of third dimension\n");
229 for (int i=0;i<fNElemBound2D;i++) fprintf(stream,"%d\n",fCoefBound2D0[i]);
231 fprintf(stream,"# Coefficients\n");
232 for (int i=0;i<fNCoefs;i++) fprintf(stream,"%+.8e\n",fCoefs[i]);
233 fprintf(stream,"END %s\n",GetName());
238 //_______________________________________________
239 void AliCheb3DCalc::LoadData(FILE* stream)
241 // Load coefs. from the stream
242 if (!stream) {Error("LoadData","No stream provided.\nStop"); exit(1);}
245 ReadLine(buffs,stream);
246 if (!buffs.BeginsWith("START")) {Error("LoadData","Expected: \"START <fit_name>\", found \"%s\"\nStop\n",buffs.Data());exit(1);}
247 if (buffs.First(' ')>0) SetName(buffs.Data()+buffs.First(' ')+1);
249 ReadLine(buffs,stream); // NRows
250 fNRows = buffs.Atoi();
251 if (fNRows<1) {Error("LoadData","Expected: '<number_of_rows>', found \"%s\"\nStop\n",buffs.Data());exit(1);}
257 for (int id0=0;id0<fNRows;id0++) {
258 ReadLine(buffs,stream); // n.cols at this row
259 fNColsAtRow[id0] = buffs.Atoi();
260 fColAtRowBg[id0] = fNElemBound2D; // begining of this row in 2D boundary surface
261 fNElemBound2D += fNColsAtRow[id0];
262 if (fNCols<fNColsAtRow[id0]) fNCols = fNColsAtRow[id0];
267 InitElemBound2D(fNElemBound2D);
269 for (int i=0;i<fNElemBound2D;i++) {
270 ReadLine(buffs,stream); // n.coeffs at 3-d dimension for the given column/row
271 fCoefBound2D0[i] = buffs.Atoi();
272 fCoefBound2D1[i] = fNCoefs;
273 fNCoefs += fCoefBound2D0[i];
276 if (fNCoefs<=0) {Error("LoadData","Negtive (%d) number of Chebychef coeffs. is obtained.\nStop\n",fNCoefs);exit(1);}
279 for (int i=0;i<fNCoefs;i++) {
280 ReadLine(buffs,stream);
281 fCoefs[i] = buffs.Atof();
283 // check end_of_data record
284 ReadLine(buffs,stream);
285 if (!buffs.BeginsWith("END") || !buffs.Contains(GetName())) {
286 Error("LoadData","Expected \"END %s\", found \"%s\".\nStop\n",GetName(),buffs.Data());
292 //_______________________________________________
293 void AliCheb3DCalc::ReadLine(TString& str,FILE* stream)
295 // read single line from the stream, skipping empty and commented lines. EOF is not expected
296 while (str.Gets(stream)) {
297 str = str.Strip(TString::kBoth,' ');
298 if (str.IsNull()||str.BeginsWith("#")) continue;
301 fprintf(stderr,"AliCheb3D::ReadLine: Failed to read from stream.\nStop");exit(1); // normally, should not reach here
304 //_______________________________________________
305 void AliCheb3DCalc::InitCols(int nc)
307 // Set max.number of significant columns in the coefs matrix
309 if (fTmpCf1) delete[] fTmpCf1;
310 fTmpCf1 = new Float_t [fNCols];
313 //_______________________________________________
314 void AliCheb3DCalc::InitRows(int nr)
316 // Set max.number of significant rows in the coefs matrix
317 if (fNColsAtRow) delete[] fNColsAtRow;
318 if (fColAtRowBg) delete[] fColAtRowBg;
319 if (fTmpCf0) delete[] fTmpCf0;
321 fNColsAtRow = new Int_t[fNRows];
322 fTmpCf0 = new Float_t [fNRows];
323 fColAtRowBg = new Int_t[fNRows];
324 for (int i=fNRows;i--;) fNColsAtRow[i] = fColAtRowBg[i] = 0;
327 //_______________________________________________
328 void AliCheb3DCalc::InitElemBound2D(int ne)
330 // Set max number of significant coefs for given row/column of coefs 3D matrix
331 if (fCoefBound2D0) delete[] fCoefBound2D0;
332 if (fCoefBound2D1) delete[] fCoefBound2D1;
334 fCoefBound2D0 = new Int_t[fNElemBound2D];
335 fCoefBound2D1 = new Int_t[fNElemBound2D];
336 for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = fCoefBound2D1[i] = 0;
339 //_______________________________________________
340 void AliCheb3DCalc::InitCoefs(int nc)
342 // Set total number of significant coefs
343 if (fCoefs) delete[] fCoefs;
345 fCoefs = new Float_t [fNCoefs];
346 for (int i=fNCoefs;i--;) fCoefs[i] = 0.0;
349 //__________________________________________________________________________________________
350 Float_t AliCheb3DCalc::ChebEval1Deriv(Float_t x, const Float_t * array, int ncf )
352 // evaluate 1D Chebyshev parameterization's derivative. x is the argument mapped to [-1:1] interval
353 if (--ncf<2) return 0;
357 float dcf0=0,dcf1,dcf2=0;
358 b0 = dcf1 = 2*ncf*array[ncf];
359 if (!(--ncf)) dcf0 = dcf1;
361 for (int i=ncf;i--;) {
364 dcf0 = dcf2 + 2*(i+1)*array[i+1];
365 b0 = dcf0 + x2*b1 -b2;
370 return b0 - x*b1 - dcf0/2;