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 **************************************************************************/
18 // Author: ruben.shahoyan@cern.ch 09/09/2006
20 ////////////////////////////////////////////////////////////////////////////////
22 // AliCheb3D produces the interpolation of the user 3D->NDimOut arbitrary //
23 // function supplied in "void (*fcn)(float* inp,float* out)" format //
24 // either in a separate macro file or as a function pointer. //
25 // Only coefficients needed to guarantee the requested precision are kept. //
27 // The user-callable methods are: //
28 // To create the interpolation use: //
29 // AliCheb3D(const char* funName, // name of the file with user function //
31 // AliCheb3D(void (*ptr)(float*,float*),// pointer on the user function //
32 // Int_t DimOut, // dimensionality of the function's output //
33 // Float_t *bmin, // lower 3D bounds of interpolation domain //
34 // Float_t *bmax, // upper 3D bounds of interpolation domain //
35 // Int_t *npoints, // number of points in each of 3 input //
36 // // dimension, defining the interpolation grid //
37 // Float_t prec=1E-6); // requested max.absolute difference between //
38 // // the interpolation and any point on grid //
40 // To test obtained parameterization use the method //
41 // TH1* TestRMS(int idim,int npoints = 1000,TH1* histo=0); //
42 // it will compare the user output of the user function and interpolation //
43 // for idim-th output dimension and fill the difference in the supplied //
44 // histogram. If no histogram is supplied, it will be created. //
46 // To save the interpolation data: //
47 // SaveData(const char* filename, Bool_t append ) //
48 // write text file with data. If append is kTRUE and the output file already //
49 // exists, data will be added in the end of the file. //
50 // Alternatively, SaveData(FILE* stream) will write the data to //
51 // already existing stream. //
53 // To read back already stored interpolation use either the constructor //
54 // AliCheb3D(const char* inpFile); //
55 // or the default constructor AliCheb3D() followed by //
56 // AliCheb3D::LoadData(const char* inpFile); //
58 // To compute the interpolation use Eval(float* par,float *res) method, with //
59 // par being 3D vector of arguments (inside the validity region) and res is //
60 // the array of DimOut elements for the output. //
62 // If only one component (say, idim-th) of the output is needed, use faster //
63 // Float_t Eval(Float_t *par,int idim) method. //
65 // void Print(option="") will print the name, the ranges of validity and //
66 // the absolute precision of the parameterization. Option "l" will also print //
67 // the information about the number of coefficients for each output //
70 // NOTE: during the evaluation no check is done for parameter vector being //
71 // outside the interpolation region. If there is such a risk, use //
72 // Bool_t IsInside(float *par) method. Chebyshev parameterization is not //
73 // good for extrapolation! //
75 // For the properties of Chebyshev parameterization see: //
76 // H.Wind, CERN EP Internal Report, 81-12/Rev. //
78 ////////////////////////////////////////////////////////////////////////////////
80 #include "AliCheb3DCalc.h"
82 ClassImp(AliCheb3DCalc)
85 AliCheb3DCalc::AliCheb3DCalc():
99 // Default constructor
103 AliCheb3DCalc::AliCheb3DCalc(FILE* stream):
117 // Default constructor
123 AliCheb3DCalc::AliCheb3DCalc(const AliCheb3DCalc& src) :
125 fNCoefs(src.fNCoefs),
128 fNElemBound2D(src.fNElemBound2D),
138 if (src.fNColsAtRow) {
139 fNColsAtRow = new Int_t[fNRows];
140 for (int i=fNRows;i--;) fNColsAtRow[i] = src.fNColsAtRow[i];
142 if (src.fColAtRowBg) {
143 fColAtRowBg = new Int_t[fNRows];
144 for (int i=fNRows;i--;) fColAtRowBg[i] = src.fColAtRowBg[i];
146 if (src.fCoefBound2D0) {
147 fCoefBound2D0 = new Int_t[fNElemBound2D];
148 for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = src.fCoefBound2D0[i];
150 if (src.fCoefBound2D1) {
151 fCoefBound2D1 = new Int_t[fNElemBound2D];
152 for (int i=fNElemBound2D;i--;) fCoefBound2D1[i] = src.fCoefBound2D1[i];
155 fCoefs = new Float_t[fNCoefs];
156 for (int i=fNCoefs;i--;) fCoefs[i] = src.fCoefs[i];
158 if (src.fTmpCf1) fTmpCf1 = new Float_t[fNCols];
159 if (src.fTmpCf0) fTmpCf0 = new Float_t[fNRows];
162 AliCheb3DCalc& AliCheb3DCalc::operator=(const AliCheb3DCalc& rhs)
164 // Assignment operator
167 SetName(rhs.GetName());
168 SetTitle(rhs.GetTitle());
169 fNCoefs = rhs.fNCoefs;
172 if (rhs.fNColsAtRow) {
173 fNColsAtRow = new Int_t[fNRows];
174 for (int i=fNRows;i--;) fNColsAtRow[i] = rhs.fNColsAtRow[i];
176 if (rhs.fColAtRowBg) {
177 fColAtRowBg = new Int_t[fNRows];
178 for (int i=fNRows;i--;) fColAtRowBg[i] = rhs.fColAtRowBg[i];
180 if (rhs.fCoefBound2D0) {
181 fCoefBound2D0 = new Int_t[fNElemBound2D];
182 for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = rhs.fCoefBound2D0[i];
184 if (rhs.fCoefBound2D1) {
185 fCoefBound2D1 = new Int_t[fNElemBound2D];
186 for (int i=fNElemBound2D;i--;) fCoefBound2D1[i] = rhs.fCoefBound2D1[i];
189 fCoefs = new Float_t[fNCoefs];
190 for (int i=fNCoefs;i--;) fCoefs[i] = rhs.fCoefs[i];
192 if (rhs.fTmpCf1) fTmpCf1 = new Float_t[fNCols];
193 if (rhs.fTmpCf0) fTmpCf0 = new Float_t[fNRows];
198 //__________________________________________________________________________________________
199 void AliCheb3DCalc::Clear(Option_t*)
201 // delete all dynamycally allocated structures
202 if (fTmpCf1) { delete[] fTmpCf1; fTmpCf1 = 0;}
203 if (fTmpCf0) { delete[] fTmpCf0; fTmpCf0 = 0;}
204 if (fCoefs) { delete[] fCoefs; fCoefs = 0;}
205 if (fCoefBound2D0) { delete[] fCoefBound2D0; fCoefBound2D0 = 0; }
206 if (fCoefBound2D1) { delete[] fCoefBound2D1; fCoefBound2D1 = 0; }
207 if (fNColsAtRow) { delete[] fNColsAtRow; fNColsAtRow = 0; }
208 if (fColAtRowBg) { delete[] fColAtRowBg; fColAtRowBg = 0; }
212 //__________________________________________________________________________________________
213 void AliCheb3DCalc::Init0()
215 // reset everything to 0
216 fNCoefs = fNRows = fNCols = fNElemBound2D = 0;
218 fCoefBound2D0 = fCoefBound2D1 = 0;
219 fNColsAtRow = fColAtRowBg = 0;
220 fTmpCf0 = fTmpCf1 = 0;
223 //__________________________________________________________________________________________
224 void AliCheb3DCalc::Print(Option_t* ) const
226 // Print the Chebychev paramterization data
227 printf("Chebyshev parameterization data %s for 3D->1 function.\n",GetName());
229 for (int i=fNElemBound2D;i--;) if (fCoefBound2D0[i]>nmax3d) nmax3d = fCoefBound2D0[i];
230 printf("%d coefficients in %dx%dx%d matrix\n",fNCoefs,fNRows,fNCols,nmax3d);
234 //__________________________________________________________________________________________
235 Float_t AliCheb3DCalc::Eval(Float_t *par) const
237 // evaluate Chebyshev parameterization for 3D function.
238 // VERY IMPORTANT: par must contain the function arguments ALREADY MAPPED to [-1:1] interval
244 for (int id0=fNRows;id0--;) {
245 int nCLoc = fNColsAtRow[id0]; // number of significant coefs on this row
246 int col0 = fColAtRowBg[id0]; // beginning of local column in the 2D boundary matrix
247 for (int id1=nCLoc;id1--;) {
249 fTmpCf1[id1] = (ncfRC=fCoefBound2D0[id]) ? ChebEval1D(z,fCoefs + fCoefBound2D1[id], ncfRC) : 0.0;
251 fTmpCf0[id0] = nCLoc>0 ? ChebEval1D(y,fTmpCf1,nCLoc):0.0;
253 return ChebEval1D(x,fTmpCf0,fNRows);
257 //_______________________________________________
258 #ifdef _INC_CREATION_ALICHEB3D_
259 void AliCheb3DCalc::SaveData(const char* outfile,Bool_t append) const
261 // writes coefficients data to output text file, optionallt appending on the end of existing file
262 TString strf = outfile;
263 gSystem->ExpandPathName(strf);
264 FILE* stream = fopen(strf,append ? "a":"w");
271 //_______________________________________________
272 #ifdef _INC_CREATION_ALICHEB3D_
273 void AliCheb3DCalc::SaveData(FILE* stream) const
275 // writes coefficients data to existing output stream
276 // Note: fNCols, fNElemBound2D and fColAtRowBg is not stored, will be computed on fly during the loading of this file
277 fprintf(stream,"#\nSTART %s\n",GetName());
278 fprintf(stream,"# Number of rows\n%d\n",fNRows);
280 fprintf(stream,"# Number of columns per row\n");
281 for (int i=0;i<fNRows;i++) fprintf(stream,"%d\n",fNColsAtRow[i]);
283 fprintf(stream,"# Number of Coefs in each significant block of third dimension\n");
284 for (int i=0;i<fNElemBound2D;i++) fprintf(stream,"%d\n",fCoefBound2D0[i]);
286 fprintf(stream,"# Coefficients\n");
287 for (int i=0;i<fNCoefs;i++) fprintf(stream,"%+.8e\n",fCoefs[i]);
288 fprintf(stream,"END %s\n",GetName());
293 //_______________________________________________
294 void AliCheb3DCalc::LoadData(FILE* stream)
296 // Load coefs. from the stream
297 if (!stream) {Error("LoadData","No stream provided.\nStop"); exit(1);}
300 ReadLine(buffs,stream);
301 if (!buffs.BeginsWith("START")) {Error("LoadData","Expected: \"START <fit_name>\", found \"%s\"\nStop\n",buffs.Data());exit(1);}
302 if (buffs.First(' ')>0) SetName(buffs.Data()+buffs.First(' ')+1);
304 ReadLine(buffs,stream); // NRows
305 fNRows = buffs.Atoi();
306 if (fNRows<1) {Error("LoadData","Expected: '<number_of_rows>', found \"%s\"\nStop\n",buffs.Data());exit(1);}
312 for (int id0=0;id0<fNRows;id0++) {
313 ReadLine(buffs,stream); // n.cols at this row
314 fNColsAtRow[id0] = buffs.Atoi();
315 fColAtRowBg[id0] = fNElemBound2D; // begining of this row in 2D boundary surface
316 fNElemBound2D += fNColsAtRow[id0];
317 if (fNCols<fNColsAtRow[id0]) fNCols = fNColsAtRow[id0];
322 InitElemBound2D(fNElemBound2D);
324 for (int i=0;i<fNElemBound2D;i++) {
325 ReadLine(buffs,stream); // n.coeffs at 3-d dimension for the given column/row
326 fCoefBound2D0[i] = buffs.Atoi();
327 fCoefBound2D1[i] = fNCoefs;
328 fNCoefs += fCoefBound2D0[i];
331 if (fNCoefs<=0) {Error("LoadData","Negtive (%d) number of Chebychef coeffs. is obtained.\nStop\n",fNCoefs);exit(1);}
334 for (int i=0;i<fNCoefs;i++) {
335 ReadLine(buffs,stream);
336 fCoefs[i] = buffs.Atof();
338 // check end_of_data record
339 ReadLine(buffs,stream);
340 if (!buffs.BeginsWith("END") || !buffs.Contains(GetName())) {
341 Error("LoadData","Expected \"END %s\", found \"%s\".\nStop\n",GetName(),buffs.Data());
347 //_______________________________________________
348 void AliCheb3DCalc::ReadLine(TString& str,FILE* stream)
350 // read single line from the stream, skipping empty and commented lines. EOF is not expected
351 while (str.Gets(stream)) {
352 str = str.Strip(TString::kBoth,' ');
353 if (str.IsNull()||str.BeginsWith("#")) continue;
356 fprintf(stderr,"AliCheb3D::ReadLine: Failed to read from stream.\nStop");exit(1); // normally, should not reach here
359 //_______________________________________________
360 void AliCheb3DCalc::InitCols(int nc)
362 // Set max.number of significant columns in the coefs matrix
364 if (fTmpCf1) delete[] fTmpCf1;
365 fTmpCf1 = new Float_t [fNCols];
368 //_______________________________________________
369 void AliCheb3DCalc::InitRows(int nr)
371 // Set max.number of significant rows in the coefs matrix
372 if (fNColsAtRow) delete[] fNColsAtRow;
373 if (fColAtRowBg) delete[] fColAtRowBg;
374 if (fTmpCf0) delete[] fTmpCf0;
376 fNColsAtRow = new Int_t[fNRows];
377 fTmpCf0 = new Float_t [fNRows];
378 fColAtRowBg = new Int_t[fNRows];
379 for (int i=fNRows;i--;) fNColsAtRow[i] = fColAtRowBg[i] = 0;
382 //_______________________________________________
383 void AliCheb3DCalc::InitElemBound2D(int ne)
385 // Set max number of significant coefs for given row/column of coefs 3D matrix
386 if (fCoefBound2D0) delete[] fCoefBound2D0;
387 if (fCoefBound2D1) delete[] fCoefBound2D1;
389 fCoefBound2D0 = new Int_t[fNElemBound2D];
390 fCoefBound2D1 = new Int_t[fNElemBound2D];
391 for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = fCoefBound2D1[i] = 0;
394 //_______________________________________________
395 void AliCheb3DCalc::InitCoefs(int nc)
397 // Set total number of significant coefs
398 if (fCoefs) delete[] fCoefs;
400 fCoefs = new Float_t [fNCoefs];
401 for (int i=fNCoefs;i--;) fCoefs[i] = 0.0;