]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/STEERBase/AliCheb3DCalc.cxx
Fixed methods to store/read whole AliMagWrapCheb content to text file: for transfer...
[u/mrichter/AliRoot.git] / STEER / STEERBase / AliCheb3DCalc.cxx
CommitLineData
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
20ClassImp(AliCheb3DCalc)
21
40389866 22//__________________________________________________________________________________________
23AliCheb3DCalc::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),
f69ec3f4 34 fTmpCf0(0),
35 fPrec(0)
5406439e 36{
37 // default constructor
38}
99adacae 39
40389866 40//__________________________________________________________________________________________
41AliCheb3DCalc::AliCheb3DCalc(const AliCheb3DCalc& src) :
42 TNamed(src),
43 fNCoefs(src.fNCoefs),
44 fNRows(src.fNRows),
45 fNCols(src.fNCols),
46 fNElemBound2D(src.fNElemBound2D),
47 fNColsAtRow(0),
48 fColAtRowBg(0),
49 fCoefBound2D0(0),
50 fCoefBound2D1(0),
51 fCoefs(0),
52 fTmpCf1(0),
f69ec3f4 53 fTmpCf0(0),
54 fPrec(src.fPrec)
99adacae 55{
5406439e 56 // copy constructor
57 //
40389866 58 if (src.fNColsAtRow) {
1d18ebe0 59 fNColsAtRow = new UShort_t[fNRows];
40389866 60 for (int i=fNRows;i--;) fNColsAtRow[i] = src.fNColsAtRow[i];
61 }
62 if (src.fColAtRowBg) {
1d18ebe0 63 fColAtRowBg = new UShort_t[fNRows];
40389866 64 for (int i=fNRows;i--;) fColAtRowBg[i] = src.fColAtRowBg[i];
65 }
66 if (src.fCoefBound2D0) {
1d18ebe0 67 fCoefBound2D0 = new UShort_t[fNElemBound2D];
40389866 68 for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = src.fCoefBound2D0[i];
69 }
70 if (src.fCoefBound2D1) {
1d18ebe0 71 fCoefBound2D1 = new UShort_t[fNElemBound2D];
40389866 72 for (int i=fNElemBound2D;i--;) fCoefBound2D1[i] = src.fCoefBound2D1[i];
73 }
74 if (src.fCoefs) {
75 fCoefs = new Float_t[fNCoefs];
76 for (int i=fNCoefs;i--;) fCoefs[i] = src.fCoefs[i];
77 }
78 if (src.fTmpCf1) fTmpCf1 = new Float_t[fNCols];
79 if (src.fTmpCf0) fTmpCf0 = new Float_t[fNRows];
99adacae 80}
81
40389866 82//__________________________________________________________________________________________
83AliCheb3DCalc::AliCheb3DCalc(FILE* stream) :
84 fNCoefs(0),
85 fNRows(0),
86 fNCols(0),
87 fNElemBound2D(0),
88 fNColsAtRow(0),
89 fColAtRowBg(0),
90 fCoefBound2D0(0),
91 fCoefBound2D1(0),
92 fCoefs(0),
93 fTmpCf1(0),
f69ec3f4 94 fTmpCf0(0),
95 fPrec(0)
99adacae 96{
5406439e 97 // constructor from coeffs. streem
40389866 98 LoadData(stream);
99adacae 99}
100
40389866 101//__________________________________________________________________________________________
102AliCheb3DCalc& AliCheb3DCalc::operator=(const AliCheb3DCalc& rhs)
99adacae 103{
5406439e 104 // assignment operator
40389866 105 if (this != &rhs) {
106 Clear();
107 SetName(rhs.GetName());
108 SetTitle(rhs.GetTitle());
109 fNCoefs = rhs.fNCoefs;
110 fNRows = rhs.fNRows;
111 fNCols = rhs.fNCols;
f69ec3f4 112 fPrec = rhs.fPrec;
40389866 113 if (rhs.fNColsAtRow) {
1d18ebe0 114 fNColsAtRow = new UShort_t[fNRows];
40389866 115 for (int i=fNRows;i--;) fNColsAtRow[i] = rhs.fNColsAtRow[i];
99adacae 116 }
40389866 117 if (rhs.fColAtRowBg) {
1d18ebe0 118 fColAtRowBg = new UShort_t[fNRows];
40389866 119 for (int i=fNRows;i--;) fColAtRowBg[i] = rhs.fColAtRowBg[i];
99adacae 120 }
40389866 121 if (rhs.fCoefBound2D0) {
1d18ebe0 122 fCoefBound2D0 = new UShort_t[fNElemBound2D];
40389866 123 for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = rhs.fCoefBound2D0[i];
99adacae 124 }
40389866 125 if (rhs.fCoefBound2D1) {
1d18ebe0 126 fCoefBound2D1 = new UShort_t[fNElemBound2D];
40389866 127 for (int i=fNElemBound2D;i--;) fCoefBound2D1[i] = rhs.fCoefBound2D1[i];
99adacae 128 }
40389866 129 if (rhs.fCoefs) {
130 fCoefs = new Float_t[fNCoefs];
131 for (int i=fNCoefs;i--;) fCoefs[i] = rhs.fCoefs[i];
99adacae 132 }
40389866 133 if (rhs.fTmpCf1) fTmpCf1 = new Float_t[fNCols];
134 if (rhs.fTmpCf0) fTmpCf0 = new Float_t[fNRows];
135 }
136 return *this;
99adacae 137}
138
139//__________________________________________________________________________________________
5406439e 140void AliCheb3DCalc::Clear(const Option_t*)
99adacae 141{
142 // delete all dynamycally allocated structures
143 if (fTmpCf1) { delete[] fTmpCf1; fTmpCf1 = 0;}
144 if (fTmpCf0) { delete[] fTmpCf0; fTmpCf0 = 0;}
145 if (fCoefs) { delete[] fCoefs; fCoefs = 0;}
146 if (fCoefBound2D0) { delete[] fCoefBound2D0; fCoefBound2D0 = 0; }
147 if (fCoefBound2D1) { delete[] fCoefBound2D1; fCoefBound2D1 = 0; }
148 if (fNColsAtRow) { delete[] fNColsAtRow; fNColsAtRow = 0; }
149 if (fColAtRowBg) { delete[] fColAtRowBg; fColAtRowBg = 0; }
150 //
151}
152
99adacae 153//__________________________________________________________________________________________
5406439e 154void AliCheb3DCalc::Print(const Option_t* ) const
99adacae 155{
5406439e 156 // print info
f69ec3f4 157 printf("Chebyshev parameterization data %s for 3D->1 function. Prec:%e\n",GetName(),fPrec);
99adacae 158 int nmax3d = 0;
159 for (int i=fNElemBound2D;i--;) if (fCoefBound2D0[i]>nmax3d) nmax3d = fCoefBound2D0[i];
160 printf("%d coefficients in %dx%dx%d matrix\n",fNCoefs,fNRows,fNCols,nmax3d);
161 //
162}
163
40389866 164//__________________________________________________________________________________________
5406439e 165Float_t AliCheb3DCalc::EvalDeriv(int dim, const Float_t *par) const
40389866 166{
167 // evaluate Chebyshev parameterization derivative in given dimension for 3D function.
168 // VERY IMPORTANT: par must contain the function arguments ALREADY MAPPED to [-1:1] interval
40389866 169 //
170 int ncfRC;
171 for (int id0=fNRows;id0--;) {
172 int nCLoc = fNColsAtRow[id0]; // number of significant coefs on this row
1cf34ee8 173 if (!nCLoc) {fTmpCf0[id0]=0; continue;}
174 //
5406439e 175 int col0 = fColAtRowBg[id0]; // beginning of local column in the 2D boundary matrix
40389866 176 for (int id1=nCLoc;id1--;) {
5406439e 177 int id = id1+col0;
1cf34ee8 178 if (!(ncfRC=fCoefBound2D0[id])) { fTmpCf1[id1]=0; continue;}
ff66b122 179 if (dim==2) fTmpCf1[id1] = ChebEval1Deriv(par[2],fCoefs + fCoefBound2D1[id], ncfRC);
180 else fTmpCf1[id1] = ChebEval1D(par[2],fCoefs + fCoefBound2D1[id], ncfRC);
40389866 181 }
ff66b122 182 if (dim==1) fTmpCf0[id0] = ChebEval1Deriv(par[1],fTmpCf1,nCLoc);
183 else fTmpCf0[id0] = ChebEval1D(par[1],fTmpCf1,nCLoc);
40389866 184 }
ff66b122 185 return (dim==0) ? ChebEval1Deriv(par[0],fTmpCf0,fNRows) : ChebEval1D(par[0],fTmpCf0,fNRows);
40389866 186 //
187}
188
1cf34ee8 189//__________________________________________________________________________________________
5406439e 190Float_t AliCheb3DCalc::EvalDeriv2(int dim1,int dim2, const Float_t *par) const
1cf34ee8 191{
192 // evaluate Chebyshev parameterization 2n derivative in given dimensions for 3D function.
193 // VERY IMPORTANT: par must contain the function arguments ALREADY MAPPED to [-1:1] interval
1cf34ee8 194 //
195 Bool_t same = dim1==dim2;
196 int ncfRC;
197 for (int id0=fNRows;id0--;) {
198 int nCLoc = fNColsAtRow[id0]; // number of significant coefs on this row
199 if (!nCLoc) {fTmpCf0[id0]=0; continue;}
200 //
5406439e 201 int col0 = fColAtRowBg[id0]; // beginning of local column in the 2D boundary matrix
1cf34ee8 202 for (int id1=nCLoc;id1--;) {
5406439e 203 int id = id1+col0;
1cf34ee8 204 if (!(ncfRC=fCoefBound2D0[id])) { fTmpCf1[id1]=0; continue;}
ff66b122 205 if (dim1==2||dim2==2) fTmpCf1[id1] = same ? ChebEval1Deriv2(par[2],fCoefs + fCoefBound2D1[id], ncfRC)
206 : ChebEval1Deriv(par[2],fCoefs + fCoefBound2D1[id], ncfRC);
207 else fTmpCf1[id1] = ChebEval1D(par[2],fCoefs + fCoefBound2D1[id], ncfRC);
1cf34ee8 208 }
ff66b122 209 if (dim1==1||dim2==1) fTmpCf0[id0] = same ? ChebEval1Deriv2(par[1],fTmpCf1,nCLoc):ChebEval1Deriv(par[1],fTmpCf1,nCLoc);
210 else fTmpCf0[id0] = ChebEval1D(par[1],fTmpCf1,nCLoc);
1cf34ee8 211 }
ff66b122 212 return (dim1==0||dim2==0) ? (same ? ChebEval1Deriv2(par[0],fTmpCf0,fNRows):ChebEval1Deriv(par[0],fTmpCf0,fNRows)) :
213 ChebEval1D(par[0],fTmpCf0,fNRows);
1cf34ee8 214 //
215}
216
99adacae 217//_______________________________________________
218#ifdef _INC_CREATION_ALICHEB3D_
219void AliCheb3DCalc::SaveData(const char* outfile,Bool_t append) const
220{
221 // writes coefficients data to output text file, optionallt appending on the end of existing file
222 TString strf = outfile;
223 gSystem->ExpandPathName(strf);
224 FILE* stream = fopen(strf,append ? "a":"w");
225 SaveData(stream);
226 fclose(stream);
227 //
228}
229#endif
230
231//_______________________________________________
232#ifdef _INC_CREATION_ALICHEB3D_
233void AliCheb3DCalc::SaveData(FILE* stream) const
234{
235 // writes coefficients data to existing output stream
236 // Note: fNCols, fNElemBound2D and fColAtRowBg is not stored, will be computed on fly during the loading of this file
237 fprintf(stream,"#\nSTART %s\n",GetName());
238 fprintf(stream,"# Number of rows\n%d\n",fNRows);
239 //
240 fprintf(stream,"# Number of columns per row\n");
241 for (int i=0;i<fNRows;i++) fprintf(stream,"%d\n",fNColsAtRow[i]);
242 //
243 fprintf(stream,"# Number of Coefs in each significant block of third dimension\n");
244 for (int i=0;i<fNElemBound2D;i++) fprintf(stream,"%d\n",fCoefBound2D0[i]);
245 //
246 fprintf(stream,"# Coefficients\n");
247 for (int i=0;i<fNCoefs;i++) fprintf(stream,"%+.8e\n",fCoefs[i]);
f69ec3f4 248 //
249 fprintf(stream,"# Precision\n");
250 fprintf(stream,"%+.8e\n",fPrec);
251 //
99adacae 252 fprintf(stream,"END %s\n",GetName());
253 //
254}
255#endif
256
257//_______________________________________________
258void AliCheb3DCalc::LoadData(FILE* stream)
259{
260 // Load coefs. from the stream
261 if (!stream) {Error("LoadData","No stream provided.\nStop"); exit(1);}
262 TString buffs;
263 Clear();
264 ReadLine(buffs,stream);
265 if (!buffs.BeginsWith("START")) {Error("LoadData","Expected: \"START <fit_name>\", found \"%s\"\nStop\n",buffs.Data());exit(1);}
266 if (buffs.First(' ')>0) SetName(buffs.Data()+buffs.First(' ')+1);
267 //
268 ReadLine(buffs,stream); // NRows
269 fNRows = buffs.Atoi();
b39f9e02 270 if (fNRows<0 && !buffs.IsDigit()) {Error("LoadData","Expected: '<number_of_rows>', found \"%s\"\nStop\n",buffs.Data());exit(1);}
99adacae 271 //
272 fNCols = 0;
273 fNElemBound2D = 0;
274 InitRows(fNRows);
275 //
276 for (int id0=0;id0<fNRows;id0++) {
277 ReadLine(buffs,stream); // n.cols at this row
278 fNColsAtRow[id0] = buffs.Atoi();
279 fColAtRowBg[id0] = fNElemBound2D; // begining of this row in 2D boundary surface
280 fNElemBound2D += fNColsAtRow[id0];
281 if (fNCols<fNColsAtRow[id0]) fNCols = fNColsAtRow[id0];
282 }
283 InitCols(fNCols);
284 //
285 fNCoefs = 0;
286 InitElemBound2D(fNElemBound2D);
287 //
288 for (int i=0;i<fNElemBound2D;i++) {
289 ReadLine(buffs,stream); // n.coeffs at 3-d dimension for the given column/row
290 fCoefBound2D0[i] = buffs.Atoi();
291 fCoefBound2D1[i] = fNCoefs;
292 fNCoefs += fCoefBound2D0[i];
293 }
294 //
b39f9e02 295 // if (fNCoefs<=0) {Error("LoadData","Negtive (%d) number of Chebychef coeffs. is obtained.\nStop\n",fNCoefs);exit(1);}
99adacae 296 //
297 InitCoefs(fNCoefs);
298 for (int i=0;i<fNCoefs;i++) {
299 ReadLine(buffs,stream);
300 fCoefs[i] = buffs.Atof();
301 }
f69ec3f4 302 //
303 // read precision
304 ReadLine(buffs,stream);
305 fPrec = buffs.Atof();
306 //
99adacae 307 // check end_of_data record
308 ReadLine(buffs,stream);
309 if (!buffs.BeginsWith("END") || !buffs.Contains(GetName())) {
310 Error("LoadData","Expected \"END %s\", found \"%s\".\nStop\n",GetName(),buffs.Data());
311 exit(1);
312 }
313 //
314}
315
316//_______________________________________________
317void AliCheb3DCalc::ReadLine(TString& str,FILE* stream)
318{
319 // read single line from the stream, skipping empty and commented lines. EOF is not expected
320 while (str.Gets(stream)) {
321 str = str.Strip(TString::kBoth,' ');
322 if (str.IsNull()||str.BeginsWith("#")) continue;
323 return;
324 }
325 fprintf(stderr,"AliCheb3D::ReadLine: Failed to read from stream.\nStop");exit(1); // normally, should not reach here
326}
327
328//_______________________________________________
329void AliCheb3DCalc::InitCols(int nc)
330{
331 // Set max.number of significant columns in the coefs matrix
332 fNCols = nc;
b39f9e02 333 if (fTmpCf1) {delete[] fTmpCf1; fTmpCf1 = 0;}
334 if (fNCols>0) fTmpCf1 = new Float_t [fNCols];
99adacae 335}
336
337//_______________________________________________
338void AliCheb3DCalc::InitRows(int nr)
339{
340 // Set max.number of significant rows in the coefs matrix
b39f9e02 341 if (fNColsAtRow) {delete[] fNColsAtRow; fNColsAtRow = 0;}
342 if (fColAtRowBg) {delete[] fColAtRowBg; fColAtRowBg = 0;}
343 if (fTmpCf0) {delete[] fTmpCf0; fTmpCf0 = 0;}
99adacae 344 fNRows = nr;
b39f9e02 345 if (fNRows>0) {
346 fNColsAtRow = new UShort_t[fNRows];
347 fTmpCf0 = new Float_t [fNRows];
348 fColAtRowBg = new UShort_t[fNRows];
349 for (int i=fNRows;i--;) fNColsAtRow[i] = fColAtRowBg[i] = 0;
350 }
99adacae 351}
352
353//_______________________________________________
354void AliCheb3DCalc::InitElemBound2D(int ne)
355{
356 // Set max number of significant coefs for given row/column of coefs 3D matrix
b39f9e02 357 if (fCoefBound2D0) {delete[] fCoefBound2D0; fCoefBound2D0 = 0;}
358 if (fCoefBound2D1) {delete[] fCoefBound2D1; fCoefBound2D1 = 0;}
99adacae 359 fNElemBound2D = ne;
b39f9e02 360 if (fNElemBound2D>0) {
361 fCoefBound2D0 = new UShort_t[fNElemBound2D];
362 fCoefBound2D1 = new UShort_t[fNElemBound2D];
363 for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = fCoefBound2D1[i] = 0;
364 }
99adacae 365}
366
367//_______________________________________________
368void AliCheb3DCalc::InitCoefs(int nc)
369{
370 // Set total number of significant coefs
b39f9e02 371 if (fCoefs) {delete[] fCoefs; fCoefs = 0;}
99adacae 372 fNCoefs = nc;
b39f9e02 373 if (fNCoefs>0) {
374 fCoefs = new Float_t [fNCoefs];
375 for (int i=fNCoefs;i--;) fCoefs[i] = 0.0;
376 }
99adacae 377}
378
40389866 379//__________________________________________________________________________________________
380Float_t AliCheb3DCalc::ChebEval1Deriv(Float_t x, const Float_t * array, int ncf )
381{
382 // evaluate 1D Chebyshev parameterization's derivative. x is the argument mapped to [-1:1] interval
1cf34ee8 383 if (--ncf<1) return 0;
40389866 384 Float_t b0, b1, b2;
385 Float_t x2 = x+x;
386 b1 = b2 = 0;
387 float dcf0=0,dcf1,dcf2=0;
388 b0 = dcf1 = 2*ncf*array[ncf];
1cf34ee8 389 if (!(--ncf)) return b0/2;
40389866 390 //
391 for (int i=ncf;i--;) {
392 b2 = b1;
393 b1 = b0;
394 dcf0 = dcf2 + 2*(i+1)*array[i+1];
395 b0 = dcf0 + x2*b1 -b2;
396 dcf2 = dcf1;
397 dcf1 = dcf0;
398 }
399 //
400 return b0 - x*b1 - dcf0/2;
401}
1cf34ee8 402
403//__________________________________________________________________________________________
404Float_t AliCheb3DCalc::ChebEval1Deriv2(Float_t x, const Float_t * array, int ncf )
405{
406 // evaluate 1D Chebyshev parameterization's 2nd derivative. x is the argument mapped to [-1:1] interval
407 if (--ncf<2) return 0;
408 Float_t b0, b1, b2;
409 Float_t x2 = x+x;
410 b1 = b2 = 0;
411 float dcf0=0,dcf1=0,dcf2=0;
412 float ddcf0=0,ddcf1,ddcf2=0;
413 //
414 dcf2 = 2*ncf*array[ncf];
415 --ncf;
416
417 dcf1 = 2*ncf*array[ncf];
418 b0 = ddcf1 = 2*ncf*dcf2;
419 //
420 if (!(--ncf)) return b0/2;
421 //
422 for (int i=ncf;i--;) {
423 b2 = b1;
424 b1 = b0;
425 dcf0 = dcf2 + 2*(i+1)*array[i+1];
426 ddcf0 = ddcf2 + 2*(i+1)*dcf1;
427 b0 = ddcf0 + x2*b1 -b2;
428 //
429 ddcf2 = ddcf1;
430 ddcf1 = ddcf0;
431 //
432 dcf2 = dcf1;
433 dcf1 = dcf0;
434 //
435 }
436 //
437 return b0 - x*b1 - ddcf0/2;
438}
2572efdf 439
440//__________________________________________________________________________________________
441Int_t AliCheb3DCalc::GetMaxColsAtRow() const
442{
443 int nmax3d = 0;
444 for (int i=fNElemBound2D;i--;) if (fCoefBound2D0[i]>nmax3d) nmax3d = fCoefBound2D0[i];
445 return nmax3d;
446}