]>
Commit | Line | Data |
---|---|---|
99adacae | 1 | /************************************************************************** |
2 | * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. * | |
3 | * * | |
4 | * Author: The ALICE Off-line Project. * | |
5 | * Contributors are mentioned in the code where appropriate. * | |
6 | * * | |
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 | **************************************************************************/ | |
15 | ||
99adacae | 16 | |
17 | // Author: ruben.shahoyan@cern.ch 09/09/2006 | |
18 | // | |
99adacae | 19 | #include "AliCheb3DCalc.h" |
20 | ||
21 | ClassImp(AliCheb3DCalc) | |
22 | ||
40389866 | 23 | //__________________________________________________________________________________________ |
24 | AliCheb3DCalc::AliCheb3DCalc() : | |
25 | fNCoefs(0), | |
26 | fNRows(0), | |
27 | fNCols(0), | |
28 | fNElemBound2D(0), | |
29 | fNColsAtRow(0), | |
30 | fColAtRowBg(0), | |
31 | fCoefBound2D0(0), | |
32 | fCoefBound2D1(0), | |
33 | fCoefs(0), | |
34 | fTmpCf1(0), | |
35 | fTmpCf0(0) | |
36 | {} | |
99adacae | 37 | |
40389866 | 38 | //__________________________________________________________________________________________ |
39 | AliCheb3DCalc::AliCheb3DCalc(const AliCheb3DCalc& src) : | |
40 | TNamed(src), | |
41 | fNCoefs(src.fNCoefs), | |
42 | fNRows(src.fNRows), | |
43 | fNCols(src.fNCols), | |
44 | fNElemBound2D(src.fNElemBound2D), | |
45 | fNColsAtRow(0), | |
46 | fColAtRowBg(0), | |
47 | fCoefBound2D0(0), | |
48 | fCoefBound2D1(0), | |
49 | fCoefs(0), | |
50 | fTmpCf1(0), | |
51 | fTmpCf0(0) | |
99adacae | 52 | { |
40389866 | 53 | if (src.fNColsAtRow) { |
54 | fNColsAtRow = new Int_t[fNRows]; | |
55 | for (int i=fNRows;i--;) fNColsAtRow[i] = src.fNColsAtRow[i]; | |
56 | } | |
57 | if (src.fColAtRowBg) { | |
58 | fColAtRowBg = new Int_t[fNRows]; | |
59 | for (int i=fNRows;i--;) fColAtRowBg[i] = src.fColAtRowBg[i]; | |
60 | } | |
61 | if (src.fCoefBound2D0) { | |
62 | fCoefBound2D0 = new Int_t[fNElemBound2D]; | |
63 | for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = src.fCoefBound2D0[i]; | |
64 | } | |
65 | if (src.fCoefBound2D1) { | |
66 | fCoefBound2D1 = new Int_t[fNElemBound2D]; | |
67 | for (int i=fNElemBound2D;i--;) fCoefBound2D1[i] = src.fCoefBound2D1[i]; | |
68 | } | |
69 | if (src.fCoefs) { | |
70 | fCoefs = new Float_t[fNCoefs]; | |
71 | for (int i=fNCoefs;i--;) fCoefs[i] = src.fCoefs[i]; | |
72 | } | |
73 | if (src.fTmpCf1) fTmpCf1 = new Float_t[fNCols]; | |
74 | if (src.fTmpCf0) fTmpCf0 = new Float_t[fNRows]; | |
99adacae | 75 | } |
76 | ||
40389866 | 77 | //__________________________________________________________________________________________ |
78 | AliCheb3DCalc::AliCheb3DCalc(FILE* stream) : | |
79 | fNCoefs(0), | |
80 | fNRows(0), | |
81 | fNCols(0), | |
82 | fNElemBound2D(0), | |
83 | fNColsAtRow(0), | |
84 | fColAtRowBg(0), | |
85 | fCoefBound2D0(0), | |
86 | fCoefBound2D1(0), | |
87 | fCoefs(0), | |
88 | fTmpCf1(0), | |
89 | fTmpCf0(0) | |
99adacae | 90 | { |
40389866 | 91 | LoadData(stream); |
99adacae | 92 | } |
93 | ||
40389866 | 94 | //__________________________________________________________________________________________ |
95 | AliCheb3DCalc& AliCheb3DCalc::operator=(const AliCheb3DCalc& rhs) | |
99adacae | 96 | { |
40389866 | 97 | if (this != &rhs) { |
98 | Clear(); | |
99 | SetName(rhs.GetName()); | |
100 | SetTitle(rhs.GetTitle()); | |
101 | fNCoefs = rhs.fNCoefs; | |
102 | fNRows = rhs.fNRows; | |
103 | fNCols = rhs.fNCols; | |
104 | if (rhs.fNColsAtRow) { | |
105 | fNColsAtRow = new Int_t[fNRows]; | |
106 | for (int i=fNRows;i--;) fNColsAtRow[i] = rhs.fNColsAtRow[i]; | |
99adacae | 107 | } |
40389866 | 108 | if (rhs.fColAtRowBg) { |
109 | fColAtRowBg = new Int_t[fNRows]; | |
110 | for (int i=fNRows;i--;) fColAtRowBg[i] = rhs.fColAtRowBg[i]; | |
99adacae | 111 | } |
40389866 | 112 | if (rhs.fCoefBound2D0) { |
113 | fCoefBound2D0 = new Int_t[fNElemBound2D]; | |
114 | for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = rhs.fCoefBound2D0[i]; | |
99adacae | 115 | } |
40389866 | 116 | if (rhs.fCoefBound2D1) { |
117 | fCoefBound2D1 = new Int_t[fNElemBound2D]; | |
118 | for (int i=fNElemBound2D;i--;) fCoefBound2D1[i] = rhs.fCoefBound2D1[i]; | |
99adacae | 119 | } |
40389866 | 120 | if (rhs.fCoefs) { |
121 | fCoefs = new Float_t[fNCoefs]; | |
122 | for (int i=fNCoefs;i--;) fCoefs[i] = rhs.fCoefs[i]; | |
99adacae | 123 | } |
40389866 | 124 | if (rhs.fTmpCf1) fTmpCf1 = new Float_t[fNCols]; |
125 | if (rhs.fTmpCf0) fTmpCf0 = new Float_t[fNRows]; | |
126 | } | |
127 | return *this; | |
99adacae | 128 | } |
129 | ||
130 | //__________________________________________________________________________________________ | |
131 | void AliCheb3DCalc::Clear(Option_t*) | |
132 | { | |
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; } | |
141 | // | |
142 | } | |
143 | ||
99adacae | 144 | //__________________________________________________________________________________________ |
145 | void AliCheb3DCalc::Print(Option_t* ) const | |
146 | { | |
99adacae | 147 | printf("Chebyshev parameterization data %s for 3D->1 function.\n",GetName()); |
148 | int nmax3d = 0; | |
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); | |
151 | // | |
152 | } | |
153 | ||
154 | //__________________________________________________________________________________________ | |
155 | Float_t AliCheb3DCalc::Eval(Float_t *par) const | |
156 | { | |
157 | // evaluate Chebyshev parameterization for 3D function. | |
158 | // VERY IMPORTANT: par must contain the function arguments ALREADY MAPPED to [-1:1] interval | |
159 | Float_t &z = par[2]; | |
160 | Float_t &y = par[1]; | |
161 | Float_t &x = par[0]; | |
162 | // | |
163 | int ncfRC; | |
164 | for (int id0=fNRows;id0--;) { | |
165 | int nCLoc = fNColsAtRow[id0]; // number of significant coefs on this row | |
40389866 | 166 | int Col0 = fColAtRowBg[id0]; // beginning of local column in the 2D boundary matrix |
99adacae | 167 | for (int id1=nCLoc;id1--;) { |
40389866 | 168 | int id = id1+Col0; |
99adacae | 169 | fTmpCf1[id1] = (ncfRC=fCoefBound2D0[id]) ? ChebEval1D(z,fCoefs + fCoefBound2D1[id], ncfRC) : 0.0; |
170 | } | |
171 | fTmpCf0[id0] = nCLoc>0 ? ChebEval1D(y,fTmpCf1,nCLoc):0.0; | |
172 | } | |
173 | return ChebEval1D(x,fTmpCf0,fNRows); | |
174 | // | |
175 | } | |
176 | ||
40389866 | 177 | //__________________________________________________________________________________________ |
178 | Float_t AliCheb3DCalc::EvalDeriv(int dim, Float_t *par) const | |
179 | { | |
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 | |
182 | Float_t &z = par[2]; | |
183 | Float_t &y = par[1]; | |
184 | Float_t &x = par[0]; | |
185 | // | |
186 | int ncfRC; | |
187 | for (int id0=fNRows;id0--;) { | |
188 | int nCLoc = fNColsAtRow[id0]; // number of significant coefs on this row | |
1cf34ee8 | 189 | if (!nCLoc) {fTmpCf0[id0]=0; continue;} |
190 | // | |
40389866 | 191 | int Col0 = fColAtRowBg[id0]; // beginning of local column in the 2D boundary matrix |
192 | for (int id1=nCLoc;id1--;) { | |
193 | int id = id1+Col0; | |
1cf34ee8 | 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); | |
40389866 | 197 | } |
1cf34ee8 | 198 | if (dim==1) fTmpCf0[id0] = ChebEval1Deriv(y,fTmpCf1,nCLoc); |
199 | else fTmpCf0[id0] = ChebEval1D(y,fTmpCf1,nCLoc); | |
40389866 | 200 | } |
201 | return (dim==0) ? ChebEval1Deriv(x,fTmpCf0,fNRows) : ChebEval1D(x,fTmpCf0,fNRows); | |
202 | // | |
203 | } | |
204 | ||
1cf34ee8 | 205 | //__________________________________________________________________________________________ |
206 | Float_t AliCheb3DCalc::EvalDeriv2(int dim1,int dim2, Float_t *par) const | |
207 | { | |
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 | |
210 | Float_t &z = par[2]; | |
211 | Float_t &y = par[1]; | |
212 | Float_t &x = par[0]; | |
213 | // | |
214 | Bool_t same = dim1==dim2; | |
215 | int ncfRC; | |
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;} | |
219 | // | |
220 | int Col0 = fColAtRowBg[id0]; // beginning of local column in the 2D boundary matrix | |
221 | for (int id1=nCLoc;id1--;) { | |
222 | int id = id1+Col0; | |
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); | |
227 | } | |
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); | |
230 | } | |
231 | return (dim1==0||dim2==0) ? (same ? ChebEval1Deriv2(x,fTmpCf0,fNRows):ChebEval1Deriv(x,fTmpCf0,fNRows)) : ChebEval1D(x,fTmpCf0,fNRows); | |
232 | // | |
233 | } | |
234 | ||
99adacae | 235 | //_______________________________________________ |
236 | #ifdef _INC_CREATION_ALICHEB3D_ | |
237 | void AliCheb3DCalc::SaveData(const char* outfile,Bool_t append) const | |
238 | { | |
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"); | |
243 | SaveData(stream); | |
244 | fclose(stream); | |
245 | // | |
246 | } | |
247 | #endif | |
248 | ||
249 | //_______________________________________________ | |
250 | #ifdef _INC_CREATION_ALICHEB3D_ | |
251 | void AliCheb3DCalc::SaveData(FILE* stream) const | |
252 | { | |
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); | |
257 | // | |
258 | fprintf(stream,"# Number of columns per row\n"); | |
259 | for (int i=0;i<fNRows;i++) fprintf(stream,"%d\n",fNColsAtRow[i]); | |
260 | // | |
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]); | |
263 | // | |
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()); | |
267 | // | |
268 | } | |
269 | #endif | |
270 | ||
271 | //_______________________________________________ | |
272 | void AliCheb3DCalc::LoadData(FILE* stream) | |
273 | { | |
274 | // Load coefs. from the stream | |
275 | if (!stream) {Error("LoadData","No stream provided.\nStop"); exit(1);} | |
276 | TString buffs; | |
277 | Clear(); | |
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); | |
281 | // | |
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);} | |
285 | // | |
286 | fNCols = 0; | |
287 | fNElemBound2D = 0; | |
288 | InitRows(fNRows); | |
289 | // | |
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]; | |
296 | } | |
297 | InitCols(fNCols); | |
298 | // | |
299 | fNCoefs = 0; | |
300 | InitElemBound2D(fNElemBound2D); | |
301 | // | |
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]; | |
307 | } | |
308 | // | |
309 | if (fNCoefs<=0) {Error("LoadData","Negtive (%d) number of Chebychef coeffs. is obtained.\nStop\n",fNCoefs);exit(1);} | |
310 | // | |
311 | InitCoefs(fNCoefs); | |
312 | for (int i=0;i<fNCoefs;i++) { | |
313 | ReadLine(buffs,stream); | |
314 | fCoefs[i] = buffs.Atof(); | |
315 | } | |
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()); | |
320 | exit(1); | |
321 | } | |
322 | // | |
323 | } | |
324 | ||
325 | //_______________________________________________ | |
326 | void AliCheb3DCalc::ReadLine(TString& str,FILE* stream) | |
327 | { | |
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; | |
332 | return; | |
333 | } | |
334 | fprintf(stderr,"AliCheb3D::ReadLine: Failed to read from stream.\nStop");exit(1); // normally, should not reach here | |
335 | } | |
336 | ||
337 | //_______________________________________________ | |
338 | void AliCheb3DCalc::InitCols(int nc) | |
339 | { | |
340 | // Set max.number of significant columns in the coefs matrix | |
341 | fNCols = nc; | |
342 | if (fTmpCf1) delete[] fTmpCf1; | |
343 | fTmpCf1 = new Float_t [fNCols]; | |
344 | } | |
345 | ||
346 | //_______________________________________________ | |
347 | void AliCheb3DCalc::InitRows(int nr) | |
348 | { | |
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; | |
353 | fNRows = nr; | |
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; | |
358 | } | |
359 | ||
360 | //_______________________________________________ | |
361 | void AliCheb3DCalc::InitElemBound2D(int ne) | |
362 | { | |
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; | |
366 | fNElemBound2D = ne; | |
367 | fCoefBound2D0 = new Int_t[fNElemBound2D]; | |
368 | fCoefBound2D1 = new Int_t[fNElemBound2D]; | |
369 | for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = fCoefBound2D1[i] = 0; | |
370 | } | |
371 | ||
372 | //_______________________________________________ | |
373 | void AliCheb3DCalc::InitCoefs(int nc) | |
374 | { | |
375 | // Set total number of significant coefs | |
376 | if (fCoefs) delete[] fCoefs; | |
377 | fNCoefs = nc; | |
378 | fCoefs = new Float_t [fNCoefs]; | |
379 | for (int i=fNCoefs;i--;) fCoefs[i] = 0.0; | |
380 | } | |
381 | ||
40389866 | 382 | //__________________________________________________________________________________________ |
383 | Float_t AliCheb3DCalc::ChebEval1Deriv(Float_t x, const Float_t * array, int ncf ) | |
384 | { | |
385 | // evaluate 1D Chebyshev parameterization's derivative. x is the argument mapped to [-1:1] interval | |
1cf34ee8 | 386 | if (--ncf<1) return 0; |
40389866 | 387 | Float_t b0, b1, b2; |
388 | Float_t x2 = x+x; | |
389 | b1 = b2 = 0; | |
390 | float dcf0=0,dcf1,dcf2=0; | |
391 | b0 = dcf1 = 2*ncf*array[ncf]; | |
1cf34ee8 | 392 | if (!(--ncf)) return b0/2; |
40389866 | 393 | // |
394 | for (int i=ncf;i--;) { | |
395 | b2 = b1; | |
396 | b1 = b0; | |
397 | dcf0 = dcf2 + 2*(i+1)*array[i+1]; | |
398 | b0 = dcf0 + x2*b1 -b2; | |
399 | dcf2 = dcf1; | |
400 | dcf1 = dcf0; | |
401 | } | |
402 | // | |
403 | return b0 - x*b1 - dcf0/2; | |
404 | } | |
1cf34ee8 | 405 | |
406 | //__________________________________________________________________________________________ | |
407 | Float_t AliCheb3DCalc::ChebEval1Deriv2(Float_t x, const Float_t * array, int ncf ) | |
408 | { | |
409 | // evaluate 1D Chebyshev parameterization's 2nd derivative. x is the argument mapped to [-1:1] interval | |
410 | if (--ncf<2) return 0; | |
411 | Float_t b0, b1, b2; | |
412 | Float_t x2 = x+x; | |
413 | b1 = b2 = 0; | |
414 | float dcf0=0,dcf1=0,dcf2=0; | |
415 | float ddcf0=0,ddcf1,ddcf2=0; | |
416 | // | |
417 | dcf2 = 2*ncf*array[ncf]; | |
418 | --ncf; | |
419 | ||
420 | dcf1 = 2*ncf*array[ncf]; | |
421 | b0 = ddcf1 = 2*ncf*dcf2; | |
422 | // | |
423 | if (!(--ncf)) return b0/2; | |
424 | // | |
425 | for (int i=ncf;i--;) { | |
426 | b2 = b1; | |
427 | b1 = b0; | |
428 | dcf0 = dcf2 + 2*(i+1)*array[i+1]; | |
429 | ddcf0 = ddcf2 + 2*(i+1)*dcf1; | |
430 | b0 = ddcf0 + x2*b1 -b2; | |
431 | // | |
432 | ddcf2 = ddcf1; | |
433 | ddcf1 = ddcf0; | |
434 | // | |
435 | dcf2 = dcf1; | |
436 | dcf1 = dcf0; | |
437 | // | |
438 | } | |
439 | // | |
440 | return b0 - x*b1 - ddcf0/2; | |
441 | } | |
442 |