]>
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 | ||
b09247a2 | 16 | #include <cstdlib> |
5406439e | 17 | #include <TSystem.h> |
ff66b122 | 18 | #include "AliCheb3DCalc.h" |
99adacae | 19 | |
20 | ClassImp(AliCheb3DCalc) | |
21 | ||
40389866 | 22 | //__________________________________________________________________________________________ |
23 | AliCheb3DCalc::AliCheb3DCalc() : | |
24 | fNCoefs(0), | |
25 | fNRows(0), | |
26 | fNCols(0), | |
27 | fNElemBound2D(0), | |
28 | fNColsAtRow(0), | |
29 | fColAtRowBg(0), | |
30 | fCoefBound2D0(0), | |
31 | fCoefBound2D1(0), | |
32 | fCoefs(0), | |
33 | fTmpCf1(0), | |
34 | fTmpCf0(0) | |
5406439e | 35 | { |
36 | // default constructor | |
37 | } | |
99adacae | 38 | |
40389866 | 39 | //__________________________________________________________________________________________ |
40 | AliCheb3DCalc::AliCheb3DCalc(const AliCheb3DCalc& src) : | |
41 | TNamed(src), | |
42 | fNCoefs(src.fNCoefs), | |
43 | fNRows(src.fNRows), | |
44 | fNCols(src.fNCols), | |
45 | fNElemBound2D(src.fNElemBound2D), | |
46 | fNColsAtRow(0), | |
47 | fColAtRowBg(0), | |
48 | fCoefBound2D0(0), | |
49 | fCoefBound2D1(0), | |
50 | fCoefs(0), | |
51 | fTmpCf1(0), | |
52 | fTmpCf0(0) | |
99adacae | 53 | { |
5406439e | 54 | // copy constructor |
55 | // | |
40389866 | 56 | if (src.fNColsAtRow) { |
1d18ebe0 | 57 | fNColsAtRow = new UShort_t[fNRows]; |
40389866 | 58 | for (int i=fNRows;i--;) fNColsAtRow[i] = src.fNColsAtRow[i]; |
59 | } | |
60 | if (src.fColAtRowBg) { | |
1d18ebe0 | 61 | fColAtRowBg = new UShort_t[fNRows]; |
40389866 | 62 | for (int i=fNRows;i--;) fColAtRowBg[i] = src.fColAtRowBg[i]; |
63 | } | |
64 | if (src.fCoefBound2D0) { | |
1d18ebe0 | 65 | fCoefBound2D0 = new UShort_t[fNElemBound2D]; |
40389866 | 66 | for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = src.fCoefBound2D0[i]; |
67 | } | |
68 | if (src.fCoefBound2D1) { | |
1d18ebe0 | 69 | fCoefBound2D1 = new UShort_t[fNElemBound2D]; |
40389866 | 70 | for (int i=fNElemBound2D;i--;) fCoefBound2D1[i] = src.fCoefBound2D1[i]; |
71 | } | |
72 | if (src.fCoefs) { | |
73 | fCoefs = new Float_t[fNCoefs]; | |
74 | for (int i=fNCoefs;i--;) fCoefs[i] = src.fCoefs[i]; | |
75 | } | |
76 | if (src.fTmpCf1) fTmpCf1 = new Float_t[fNCols]; | |
77 | if (src.fTmpCf0) fTmpCf0 = new Float_t[fNRows]; | |
99adacae | 78 | } |
79 | ||
40389866 | 80 | //__________________________________________________________________________________________ |
81 | AliCheb3DCalc::AliCheb3DCalc(FILE* stream) : | |
82 | fNCoefs(0), | |
83 | fNRows(0), | |
84 | fNCols(0), | |
85 | fNElemBound2D(0), | |
86 | fNColsAtRow(0), | |
87 | fColAtRowBg(0), | |
88 | fCoefBound2D0(0), | |
89 | fCoefBound2D1(0), | |
90 | fCoefs(0), | |
91 | fTmpCf1(0), | |
92 | fTmpCf0(0) | |
99adacae | 93 | { |
5406439e | 94 | // constructor from coeffs. streem |
40389866 | 95 | LoadData(stream); |
99adacae | 96 | } |
97 | ||
40389866 | 98 | //__________________________________________________________________________________________ |
99 | AliCheb3DCalc& AliCheb3DCalc::operator=(const AliCheb3DCalc& rhs) | |
99adacae | 100 | { |
5406439e | 101 | // assignment operator |
40389866 | 102 | if (this != &rhs) { |
103 | Clear(); | |
104 | SetName(rhs.GetName()); | |
105 | SetTitle(rhs.GetTitle()); | |
106 | fNCoefs = rhs.fNCoefs; | |
107 | fNRows = rhs.fNRows; | |
108 | fNCols = rhs.fNCols; | |
109 | if (rhs.fNColsAtRow) { | |
1d18ebe0 | 110 | fNColsAtRow = new UShort_t[fNRows]; |
40389866 | 111 | for (int i=fNRows;i--;) fNColsAtRow[i] = rhs.fNColsAtRow[i]; |
99adacae | 112 | } |
40389866 | 113 | if (rhs.fColAtRowBg) { |
1d18ebe0 | 114 | fColAtRowBg = new UShort_t[fNRows]; |
40389866 | 115 | for (int i=fNRows;i--;) fColAtRowBg[i] = rhs.fColAtRowBg[i]; |
99adacae | 116 | } |
40389866 | 117 | if (rhs.fCoefBound2D0) { |
1d18ebe0 | 118 | fCoefBound2D0 = new UShort_t[fNElemBound2D]; |
40389866 | 119 | for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = rhs.fCoefBound2D0[i]; |
99adacae | 120 | } |
40389866 | 121 | if (rhs.fCoefBound2D1) { |
1d18ebe0 | 122 | fCoefBound2D1 = new UShort_t[fNElemBound2D]; |
40389866 | 123 | for (int i=fNElemBound2D;i--;) fCoefBound2D1[i] = rhs.fCoefBound2D1[i]; |
99adacae | 124 | } |
40389866 | 125 | if (rhs.fCoefs) { |
126 | fCoefs = new Float_t[fNCoefs]; | |
127 | for (int i=fNCoefs;i--;) fCoefs[i] = rhs.fCoefs[i]; | |
99adacae | 128 | } |
40389866 | 129 | if (rhs.fTmpCf1) fTmpCf1 = new Float_t[fNCols]; |
130 | if (rhs.fTmpCf0) fTmpCf0 = new Float_t[fNRows]; | |
131 | } | |
132 | return *this; | |
99adacae | 133 | } |
134 | ||
135 | //__________________________________________________________________________________________ | |
5406439e | 136 | void AliCheb3DCalc::Clear(const Option_t*) |
99adacae | 137 | { |
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; } | |
146 | // | |
147 | } | |
148 | ||
99adacae | 149 | //__________________________________________________________________________________________ |
5406439e | 150 | void AliCheb3DCalc::Print(const Option_t* ) const |
99adacae | 151 | { |
5406439e | 152 | // print info |
99adacae | 153 | printf("Chebyshev parameterization data %s for 3D->1 function.\n",GetName()); |
154 | int nmax3d = 0; | |
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); | |
157 | // | |
158 | } | |
159 | ||
40389866 | 160 | //__________________________________________________________________________________________ |
5406439e | 161 | Float_t AliCheb3DCalc::EvalDeriv(int dim, const Float_t *par) const |
40389866 | 162 | { |
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 | |
40389866 | 165 | // |
166 | int ncfRC; | |
167 | for (int id0=fNRows;id0--;) { | |
168 | int nCLoc = fNColsAtRow[id0]; // number of significant coefs on this row | |
1cf34ee8 | 169 | if (!nCLoc) {fTmpCf0[id0]=0; continue;} |
170 | // | |
5406439e | 171 | int col0 = fColAtRowBg[id0]; // beginning of local column in the 2D boundary matrix |
40389866 | 172 | for (int id1=nCLoc;id1--;) { |
5406439e | 173 | int id = id1+col0; |
1cf34ee8 | 174 | if (!(ncfRC=fCoefBound2D0[id])) { fTmpCf1[id1]=0; continue;} |
ff66b122 | 175 | if (dim==2) fTmpCf1[id1] = ChebEval1Deriv(par[2],fCoefs + fCoefBound2D1[id], ncfRC); |
176 | else fTmpCf1[id1] = ChebEval1D(par[2],fCoefs + fCoefBound2D1[id], ncfRC); | |
40389866 | 177 | } |
ff66b122 | 178 | if (dim==1) fTmpCf0[id0] = ChebEval1Deriv(par[1],fTmpCf1,nCLoc); |
179 | else fTmpCf0[id0] = ChebEval1D(par[1],fTmpCf1,nCLoc); | |
40389866 | 180 | } |
ff66b122 | 181 | return (dim==0) ? ChebEval1Deriv(par[0],fTmpCf0,fNRows) : ChebEval1D(par[0],fTmpCf0,fNRows); |
40389866 | 182 | // |
183 | } | |
184 | ||
1cf34ee8 | 185 | //__________________________________________________________________________________________ |
5406439e | 186 | Float_t AliCheb3DCalc::EvalDeriv2(int dim1,int dim2, const Float_t *par) const |
1cf34ee8 | 187 | { |
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 | |
1cf34ee8 | 190 | // |
191 | Bool_t same = dim1==dim2; | |
192 | int ncfRC; | |
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;} | |
196 | // | |
5406439e | 197 | int col0 = fColAtRowBg[id0]; // beginning of local column in the 2D boundary matrix |
1cf34ee8 | 198 | for (int id1=nCLoc;id1--;) { |
5406439e | 199 | int id = id1+col0; |
1cf34ee8 | 200 | if (!(ncfRC=fCoefBound2D0[id])) { fTmpCf1[id1]=0; continue;} |
ff66b122 | 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); | |
1cf34ee8 | 204 | } |
ff66b122 | 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); | |
1cf34ee8 | 207 | } |
ff66b122 | 208 | return (dim1==0||dim2==0) ? (same ? ChebEval1Deriv2(par[0],fTmpCf0,fNRows):ChebEval1Deriv(par[0],fTmpCf0,fNRows)) : |
209 | ChebEval1D(par[0],fTmpCf0,fNRows); | |
1cf34ee8 | 210 | // |
211 | } | |
212 | ||
99adacae | 213 | //_______________________________________________ |
214 | #ifdef _INC_CREATION_ALICHEB3D_ | |
215 | void AliCheb3DCalc::SaveData(const char* outfile,Bool_t append) const | |
216 | { | |
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"); | |
221 | SaveData(stream); | |
222 | fclose(stream); | |
223 | // | |
224 | } | |
225 | #endif | |
226 | ||
227 | //_______________________________________________ | |
228 | #ifdef _INC_CREATION_ALICHEB3D_ | |
229 | void AliCheb3DCalc::SaveData(FILE* stream) const | |
230 | { | |
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); | |
235 | // | |
236 | fprintf(stream,"# Number of columns per row\n"); | |
237 | for (int i=0;i<fNRows;i++) fprintf(stream,"%d\n",fNColsAtRow[i]); | |
238 | // | |
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]); | |
241 | // | |
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()); | |
245 | // | |
246 | } | |
247 | #endif | |
248 | ||
249 | //_______________________________________________ | |
250 | void AliCheb3DCalc::LoadData(FILE* stream) | |
251 | { | |
252 | // Load coefs. from the stream | |
253 | if (!stream) {Error("LoadData","No stream provided.\nStop"); exit(1);} | |
254 | TString buffs; | |
255 | Clear(); | |
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); | |
259 | // | |
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);} | |
263 | // | |
264 | fNCols = 0; | |
265 | fNElemBound2D = 0; | |
266 | InitRows(fNRows); | |
267 | // | |
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]; | |
274 | } | |
275 | InitCols(fNCols); | |
276 | // | |
277 | fNCoefs = 0; | |
278 | InitElemBound2D(fNElemBound2D); | |
279 | // | |
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]; | |
285 | } | |
286 | // | |
287 | if (fNCoefs<=0) {Error("LoadData","Negtive (%d) number of Chebychef coeffs. is obtained.\nStop\n",fNCoefs);exit(1);} | |
288 | // | |
289 | InitCoefs(fNCoefs); | |
290 | for (int i=0;i<fNCoefs;i++) { | |
291 | ReadLine(buffs,stream); | |
292 | fCoefs[i] = buffs.Atof(); | |
293 | } | |
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()); | |
298 | exit(1); | |
299 | } | |
300 | // | |
301 | } | |
302 | ||
303 | //_______________________________________________ | |
304 | void AliCheb3DCalc::ReadLine(TString& str,FILE* stream) | |
305 | { | |
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; | |
310 | return; | |
311 | } | |
312 | fprintf(stderr,"AliCheb3D::ReadLine: Failed to read from stream.\nStop");exit(1); // normally, should not reach here | |
313 | } | |
314 | ||
315 | //_______________________________________________ | |
316 | void AliCheb3DCalc::InitCols(int nc) | |
317 | { | |
318 | // Set max.number of significant columns in the coefs matrix | |
319 | fNCols = nc; | |
320 | if (fTmpCf1) delete[] fTmpCf1; | |
321 | fTmpCf1 = new Float_t [fNCols]; | |
322 | } | |
323 | ||
324 | //_______________________________________________ | |
325 | void AliCheb3DCalc::InitRows(int nr) | |
326 | { | |
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; | |
331 | fNRows = nr; | |
1d18ebe0 | 332 | fNColsAtRow = new UShort_t[fNRows]; |
99adacae | 333 | fTmpCf0 = new Float_t [fNRows]; |
1d18ebe0 | 334 | fColAtRowBg = new UShort_t[fNRows]; |
99adacae | 335 | for (int i=fNRows;i--;) fNColsAtRow[i] = fColAtRowBg[i] = 0; |
336 | } | |
337 | ||
338 | //_______________________________________________ | |
339 | void AliCheb3DCalc::InitElemBound2D(int ne) | |
340 | { | |
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; | |
344 | fNElemBound2D = ne; | |
1d18ebe0 | 345 | fCoefBound2D0 = new UShort_t[fNElemBound2D]; |
346 | fCoefBound2D1 = new UShort_t[fNElemBound2D]; | |
99adacae | 347 | for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = fCoefBound2D1[i] = 0; |
348 | } | |
349 | ||
350 | //_______________________________________________ | |
351 | void AliCheb3DCalc::InitCoefs(int nc) | |
352 | { | |
353 | // Set total number of significant coefs | |
354 | if (fCoefs) delete[] fCoefs; | |
355 | fNCoefs = nc; | |
356 | fCoefs = new Float_t [fNCoefs]; | |
357 | for (int i=fNCoefs;i--;) fCoefs[i] = 0.0; | |
358 | } | |
359 | ||
40389866 | 360 | //__________________________________________________________________________________________ |
361 | Float_t AliCheb3DCalc::ChebEval1Deriv(Float_t x, const Float_t * array, int ncf ) | |
362 | { | |
363 | // evaluate 1D Chebyshev parameterization's derivative. x is the argument mapped to [-1:1] interval | |
1cf34ee8 | 364 | if (--ncf<1) return 0; |
40389866 | 365 | Float_t b0, b1, b2; |
366 | Float_t x2 = x+x; | |
367 | b1 = b2 = 0; | |
368 | float dcf0=0,dcf1,dcf2=0; | |
369 | b0 = dcf1 = 2*ncf*array[ncf]; | |
1cf34ee8 | 370 | if (!(--ncf)) return b0/2; |
40389866 | 371 | // |
372 | for (int i=ncf;i--;) { | |
373 | b2 = b1; | |
374 | b1 = b0; | |
375 | dcf0 = dcf2 + 2*(i+1)*array[i+1]; | |
376 | b0 = dcf0 + x2*b1 -b2; | |
377 | dcf2 = dcf1; | |
378 | dcf1 = dcf0; | |
379 | } | |
380 | // | |
381 | return b0 - x*b1 - dcf0/2; | |
382 | } | |
1cf34ee8 | 383 | |
384 | //__________________________________________________________________________________________ | |
385 | Float_t AliCheb3DCalc::ChebEval1Deriv2(Float_t x, const Float_t * array, int ncf ) | |
386 | { | |
387 | // evaluate 1D Chebyshev parameterization's 2nd derivative. x is the argument mapped to [-1:1] interval | |
388 | if (--ncf<2) return 0; | |
389 | Float_t b0, b1, b2; | |
390 | Float_t x2 = x+x; | |
391 | b1 = b2 = 0; | |
392 | float dcf0=0,dcf1=0,dcf2=0; | |
393 | float ddcf0=0,ddcf1,ddcf2=0; | |
394 | // | |
395 | dcf2 = 2*ncf*array[ncf]; | |
396 | --ncf; | |
397 | ||
398 | dcf1 = 2*ncf*array[ncf]; | |
399 | b0 = ddcf1 = 2*ncf*dcf2; | |
400 | // | |
401 | if (!(--ncf)) return b0/2; | |
402 | // | |
403 | for (int i=ncf;i--;) { | |
404 | b2 = b1; | |
405 | b1 = b0; | |
406 | dcf0 = dcf2 + 2*(i+1)*array[i+1]; | |
407 | ddcf0 = ddcf2 + 2*(i+1)*dcf1; | |
408 | b0 = ddcf0 + x2*b1 -b2; | |
409 | // | |
410 | ddcf2 = ddcf1; | |
411 | ddcf1 = ddcf0; | |
412 | // | |
413 | dcf2 = dcf1; | |
414 | dcf1 = dcf0; | |
415 | // | |
416 | } | |
417 | // | |
418 | return b0 - x*b1 - ddcf0/2; | |
419 | } | |
2572efdf | 420 | |
421 | //__________________________________________________________________________________________ | |
422 | Int_t AliCheb3DCalc::GetMaxColsAtRow() const | |
423 | { | |
424 | int nmax3d = 0; | |
425 | for (int i=fNElemBound2D;i--;) if (fCoefBound2D0[i]>nmax3d) nmax3d = fCoefBound2D0[i]; | |
426 | return nmax3d; | |
427 | } |