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