]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/STEERBase/AliCheb3D.cxx
HMPID module
[u/mrichter/AliRoot.git] / STEER / STEERBase / AliCheb3D.cxx
CommitLineData
5406439e 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 **************************************************************************/
0eea9d4d 15
16#include <TString.h>
17#include <TSystem.h>
0eea9d4d 18#include <TROOT.h>
40389866 19#include <TRandom.h>
5406439e 20#include <stdio.h>
21#include <TMethodCall.h>
22#include <TMath.h>
23#include <TH1.h>
0eea9d4d 24#include "AliCheb3D.h"
5406439e 25#include "AliCheb3DCalc.h"
b8bec44f 26#include "AliLog.h"
0eea9d4d 27
0eea9d4d 28ClassImp(AliCheb3D)
29
f69ec3f4 30const Float_t AliCheb3D::fgkMinPrec = 1.e-12f;
31
40389866 32//__________________________________________________________________________________________
33AliCheb3D::AliCheb3D() :
34 fDimOut(0),
35 fPrec(0),
36 fChebCalc(1),
37 fMaxCoefs(0),
38 fResTmp(0),
39 fGrid(0),
40 fUsrFunName(""),
41 fUsrMacro(0)
0eea9d4d 42{
5e6a3170 43// Default constructor
c8f37c50 44 for (int i=3;i--;) {
d97d27ff 45 fBMin[i] = fBMax[i] = fBScale[i] = fBOffset[i] = fArgsTmp[i] = 0;
c8f37c50 46 fNPoints[i] = 0;
34473f9e 47 fGridOffs[i] = 0;
c8f37c50 48 }
0eea9d4d 49}
50
40389866 51//__________________________________________________________________________________________
52AliCheb3D::AliCheb3D(const AliCheb3D& src) :
53 TNamed(src),
54 fDimOut(src.fDimOut),
55 fPrec(src.fPrec),
56 fChebCalc(1),
57 fMaxCoefs(src.fMaxCoefs),
58 fResTmp(0),
59 fGrid(0),
60 fUsrFunName(src.fUsrFunName),
61 fUsrMacro(0)
0eea9d4d 62{
40389866 63 // read coefs from text file
64 for (int i=3;i--;) {
65 fBMin[i] = src.fBMin[i];
66 fBMax[i] = src.fBMax[i];
67 fBScale[i] = src.fBScale[i];
68 fBOffset[i] = src.fBOffset[i];
69 fNPoints[i] = src.fNPoints[i];
c8f37c50 70 fGridOffs[i] = src.fGridOffs[i];
f9efbbf6 71 fArgsTmp[i] = 0;
40389866 72 }
73 for (int i=0;i<fDimOut;i++) {
74 AliCheb3DCalc* cbc = src.GetChebCalc(i);
75 if (cbc) fChebCalc.AddAtAndExpand(new AliCheb3DCalc(*cbc),i);
76 }
0eea9d4d 77}
78
40389866 79//__________________________________________________________________________________________
80AliCheb3D::AliCheb3D(const char* inpFile) :
81 fDimOut(0),
82 fPrec(0),
83 fChebCalc(1),
84 fMaxCoefs(0),
85 fResTmp(0),
86 fGrid(0),
87 fUsrFunName(""),
88 fUsrMacro(0)
0bc7b414 89{
40389866 90 // read coefs from text file
c8f37c50 91 for (int i=3;i--;) {
92 fBMin[i] = fBMax[i] = fBScale[i] = fBOffset[i] = 0;
93 fNPoints[i] = 0;
34473f9e 94 fGridOffs[i] = 0;
f9efbbf6 95 fArgsTmp[i] = 0;
c8f37c50 96 }
40389866 97 LoadData(inpFile);
c437b1a5 98}
99
40389866 100//__________________________________________________________________________________________
101AliCheb3D::AliCheb3D(FILE* stream) :
102 fDimOut(0),
103 fPrec(0),
104 fChebCalc(1),
105 fMaxCoefs(0),
106 fResTmp(0),
107 fGrid(0),
108 fUsrFunName(""),
109 fUsrMacro(0)
c437b1a5 110{
40389866 111 // read coefs from stream
c8f37c50 112 for (int i=3;i--;) {
113 fBMin[i] = fBMax[i] = fBScale[i] = fBOffset[i] = 0;
114 fNPoints[i] = 0;
34473f9e 115 fGridOffs[i] = 0;
f9efbbf6 116 fArgsTmp[i] = 0;
c8f37c50 117 }
40389866 118 LoadData(stream);
0bc7b414 119}
0eea9d4d 120
121//__________________________________________________________________________________________
122#ifdef _INC_CREATION_ALICHEB3D_
f69ec3f4 123AliCheb3D::AliCheb3D(const char* funName, int DimOut, const Float_t *bmin, const Float_t *bmax, Int_t *npoints, Float_t prec, const Float_t* precD) :
40389866 124 TNamed(funName,funName),
125 fDimOut(0),
f69ec3f4 126 fPrec(TMath::Max(fgkMinPrec,prec)),
40389866 127 fChebCalc(1),
128 fMaxCoefs(0),
129 fResTmp(0),
130 fGrid(0),
131 fUsrFunName("") ,
132 fUsrMacro(0)
0eea9d4d 133{
134 // Construct the parameterization for the function
135 // funName : name of the file containing the function: void funName(Float_t * inp,Float_t * out)
136 // DimOut : dimension of the vector computed by the user function
137 // bmin : array of 3 elements with the lower boundaries of the region where the function is defined
138 // bmax : array of 3 elements with the upper boundaries of the region where the function is defined
139 // npoints : array of 3 elements with the number of points to compute in each of 3 dimension
140 // prec : max allowed absolute difference between the user function and computed parameterization on the requested grid
f69ec3f4 141 // precD : optional array with precisions per output dimension (if >fgkMinPrec will override common prec)
fa638b6a 142 if (DimOut<1) AliFatalF("Requested output dimension is %d",fDimOut);
c8f37c50 143 for (int i=3;i--;) {
144 fBMin[i] = fBMax[i] = fBScale[i] = fBOffset[i] = 0;
145 fNPoints[i] = 0;
146 fGridOffs[i] = 0.;
f9efbbf6 147 fArgsTmp[i] = 0;
c8f37c50 148 }
f69ec3f4 149 SetDimOut(DimOut,precD);
0eea9d4d 150 PrepareBoundaries(bmin,bmax);
151 DefineGrid(npoints);
152 SetUsrFunction(funName);
153 ChebFit();
154 //
155}
156#endif
157
158//__________________________________________________________________________________________
159#ifdef _INC_CREATION_ALICHEB3D_
f69ec3f4 160AliCheb3D::AliCheb3D(void (*ptr)(float*,float*), int DimOut, Float_t *bmin,Float_t *bmax, Int_t *npoints, Float_t prec, const Float_t* precD) :
161 TNamed("Cheb3D","Cheb3D"),
40389866 162 fDimOut(0),
f69ec3f4 163 fPrec(TMath::Max(fgkMinPrec,prec)),
40389866 164 fChebCalc(1),
165 fMaxCoefs(0),
166 fResTmp(0),
167 fGrid(0),
168 fUsrFunName(""),
169 fUsrMacro(0)
0eea9d4d 170{
171 // Construct the parameterization for the function
172 // ptr : pointer on the function: void fun(Float_t * inp,Float_t * out)
173 // DimOut : dimension of the vector computed by the user function
174 // bmin : array of 3 elements with the lower boundaries of the region where the function is defined
175 // bmax : array of 3 elements with the upper boundaries of the region where the function is defined
176 // npoints : array of 3 elements with the number of points to compute in each of 3 dimension
177 // prec : max allowed absolute difference between the user function and computed parameterization on the requested grid
f69ec3f4 178 // precD : optional array with precisions per output dimension (if >fgkMinPrec will override common prec)
0eea9d4d 179 //
fa638b6a 180 if (DimOut<1) AliFatalF("Requested output dimension is %d",fDimOut);
c8f37c50 181 for (int i=3;i--;) {
182 fBMin[i] = fBMax[i] = fBScale[i] = fBOffset[i] = 0;
183 fNPoints[i] = 0;
184 fGridOffs[i] = 0.;
f9efbbf6 185 fArgsTmp[i] = 0;
c8f37c50 186 }
f69ec3f4 187 SetDimOut(DimOut,precD);
0eea9d4d 188 PrepareBoundaries(bmin,bmax);
189 DefineGrid(npoints);
190 SetUsrFunction(ptr);
191 ChebFit();
192 //
193}
194#endif
195
40389866 196//__________________________________________________________________________________________
197#ifdef _INC_CREATION_ALICHEB3D_
f69ec3f4 198AliCheb3D::AliCheb3D(void (*ptr)(float*,float*), int DimOut, Float_t *bmin,Float_t *bmax, Int_t *npX,Int_t *npY,Int_t *npZ, Float_t prec, const Float_t* precD) :
199 TNamed("Cheb3D","Cheb3D"),
40389866 200 fDimOut(0),
f69ec3f4 201 fPrec(TMath::Max(fgkMinPrec,prec)),
40389866 202 fChebCalc(1),
203 fMaxCoefs(0),
204 fResTmp(0),
205 fGrid(0),
206 fUsrFunName(""),
207 fUsrMacro(0)
208{
209 // Construct very economic parameterization for the function
210 // ptr : pointer on the function: void fun(Float_t * inp,Float_t * out)
211 // DimOut : dimension of the vector computed by the user function
212 // bmin : array of 3 elements with the lower boundaries of the region where the function is defined
213 // bmax : array of 3 elements with the upper boundaries of the region where the function is defined
214 // npX : array of 3 elements with the number of points to compute in each dimension for 1st component
215 // npY : array of 3 elements with the number of points to compute in each dimension for 2nd component
216 // npZ : array of 3 elements with the number of points to compute in each dimension for 3d component
217 // prec : max allowed absolute difference between the user function and computed parameterization on the requested grid
f69ec3f4 218 // precD : optional array with precisions per output dimension (if >fgkMinPrec will override common prec)
40389866 219 //
fa638b6a 220 if (DimOut<1) AliFatalF("Requested output dimension is %d",fDimOut);
c8f37c50 221 for (int i=3;i--;) {
222 fBMin[i] = fBMax[i] = fBScale[i] = fBOffset[i] = 0;
223 fNPoints[i] = 0;
224 fGridOffs[i] = 0.;
f9efbbf6 225 fArgsTmp[i] = 0;
c8f37c50 226 }
f69ec3f4 227 SetDimOut(DimOut,precD);
40389866 228 PrepareBoundaries(bmin,bmax);
229 SetUsrFunction(ptr);
230 //
231 DefineGrid(npX);
232 ChebFit(0);
233 DefineGrid(npY);
234 ChebFit(1);
235 DefineGrid(npZ);
236 ChebFit(2);
237 //
238}
239#endif
240
241
242//__________________________________________________________________________________________
243#ifdef _INC_CREATION_ALICHEB3D_
f69ec3f4 244AliCheb3D::AliCheb3D(void (*ptr)(float*,float*), int DimOut, Float_t *bmin,Float_t *bmax, Float_t prec, Bool_t run, const Float_t* precD) :
245 TNamed("Cheb3D","Cheb3D"),
40389866 246 fDimOut(0),
f69ec3f4 247 fPrec(TMath::Max(fgkMinPrec,prec)),
40389866 248 fChebCalc(1),
249 fMaxCoefs(0),
250 fResTmp(0),
251 fGrid(0),
252 fUsrFunName(""),
253 fUsrMacro(0)
254{
255 // Construct very economic parameterization for the function with automatic calculation of the root's grid
256 // ptr : pointer on the function: void fun(Float_t * inp,Float_t * out)
257 // DimOut : dimension of the vector computed by the user function
258 // bmin : array of 3 elements with the lower boundaries of the region where the function is defined
259 // bmax : array of 3 elements with the upper boundaries of the region where the function is defined
260 // prec : max allowed absolute difference between the user function and computed parameterization on the requested grid
f69ec3f4 261 // precD : optional array with precisions per output dimension (if >fgkMinPrec will override common prec)
40389866 262 //
fa638b6a 263 if (DimOut!=3) AliFatalF("This constructor works only for 3D fits, %dD fit was requested",fDimOut);
264 if (DimOut<1) AliFatalF("Requested output dimension is %d",fDimOut);
c8f37c50 265 for (int i=3;i--;) {
266 fBMin[i] = fBMax[i] = fBScale[i] = fBOffset[i] = 0;
267 fNPoints[i] = 0;
268 fGridOffs[i] = 0.;
f9efbbf6 269 fArgsTmp[i] = 0;
c8f37c50 270 }
f69ec3f4 271 SetDimOut(DimOut,precD);
40389866 272 PrepareBoundaries(bmin,bmax);
273 SetUsrFunction(ptr);
274 //
1d18ebe0 275 if (run) {
276 int gridNC[3][3];
277 EstimateNPoints(prec,gridNC);
278 DefineGrid(gridNC[0]);
279 ChebFit(0);
280 DefineGrid(gridNC[1]);
281 ChebFit(1);
282 DefineGrid(gridNC[2]);
283 ChebFit(2);
284 }
40389866 285 //
286}
287#endif
288
289
290//__________________________________________________________________________________________
291AliCheb3D& AliCheb3D::operator=(const AliCheb3D& rhs)
292{
5406439e 293 // assignment operator
294 //
40389866 295 if (this != &rhs) {
296 Clear();
297 fDimOut = rhs.fDimOut;
298 fPrec = rhs.fPrec;
299 fMaxCoefs = rhs.fMaxCoefs;
300 fUsrFunName = rhs.fUsrFunName;
301 fUsrMacro = 0;
302 for (int i=3;i--;) {
303 fBMin[i] = rhs.fBMin[i];
304 fBMax[i] = rhs.fBMax[i];
305 fBScale[i] = rhs.fBScale[i];
306 fBOffset[i] = rhs.fBOffset[i];
307 fNPoints[i] = rhs.fNPoints[i];
308 }
309 for (int i=0;i<fDimOut;i++) {
310 AliCheb3DCalc* cbc = rhs.GetChebCalc(i);
311 if (cbc) fChebCalc.AddAtAndExpand(new AliCheb3DCalc(*cbc),i);
312 }
313 }
314 return *this;
315 //
316}
0bc7b414 317
0eea9d4d 318//__________________________________________________________________________________________
5406439e 319void AliCheb3D::Clear(const Option_t*)
0eea9d4d 320{
5406439e 321 // clear all dynamic structures
322 //
0eea9d4d 323 if (fResTmp) { delete[] fResTmp; fResTmp = 0; }
324 if (fGrid) { delete[] fGrid; fGrid = 0; }
325 if (fUsrMacro) { delete fUsrMacro; fUsrMacro = 0;}
d8d3669f 326 fChebCalc.SetOwner(kTRUE);
0eea9d4d 327 fChebCalc.Delete();
328 //
329}
330
331//__________________________________________________________________________________________
5406439e 332void AliCheb3D::Print(const Option_t* opt) const
0eea9d4d 333{
5406439e 334 // print info
335 //
0eea9d4d 336 printf("%s: Chebyshev parameterization for 3D->%dD function. Precision: %e\n",GetName(),fDimOut,fPrec);
337 printf("Region of validity: [%+.5e:%+.5e] [%+.5e:%+.5e] [%+.5e:%+.5e]\n",fBMin[0],fBMax[0],fBMin[1],fBMax[1],fBMin[2],fBMax[2]);
338 TString opts = opt; opts.ToLower();
339 if (opts.Contains("l")) for (int i=0;i<fDimOut;i++) {printf("Output dimension %d:\n",i+1); GetChebCalc(i)->Print();}
340 //
341}
342
0eea9d4d 343//__________________________________________________________________________________________
5406439e 344void AliCheb3D::PrepareBoundaries(const Float_t *bmin, const Float_t *bmax)
0eea9d4d 345{
346 // Set and check boundaries defined by user, prepare coefficients for their conversion to [-1:1] interval
347 //
348 for (int i=3;i--;) {
349 fBMin[i] = bmin[i];
350 fBMax[i] = bmax[i];
351 fBScale[i] = bmax[i]-bmin[i];
352 if (fBScale[i]<=0) {
fa638b6a 353 AliFatalF("Boundaries for %d-th dimension are not increasing: %+.4e %+.4e\nStop\n",i,fBMin[i],fBMax[i]);
0eea9d4d 354 }
355 fBOffset[i] = bmin[i] + fBScale[i]/2.0;
356 fBScale[i] = 2./fBScale[i];
357 }
358 //
359}
360
40389866 361
0eea9d4d 362//__________________________________________________________________________________________
363#ifdef _INC_CREATION_ALICHEB3D_
40389866 364
365// Pointer on user function (faster altrnative to TMethodCall)
366void (*gUsrFunAliCheb3D) (float* ,float* );
367
368void AliCheb3D::EvalUsrFunction()
369{
370 // call user supplied function
371 if (gUsrFunAliCheb3D) gUsrFunAliCheb3D(fArgsTmp,fResTmp);
372 else fUsrMacro->Execute();
373}
374
0eea9d4d 375void AliCheb3D::SetUsrFunction(const char* name)
376{
377 // load user macro with function definition and compile it
378 gUsrFunAliCheb3D = 0;
379 fUsrFunName = name;
380 gSystem->ExpandPathName(fUsrFunName);
381 if (fUsrMacro) delete fUsrMacro;
382 TString tmpst = fUsrFunName;
383 tmpst += "+"; // prepare filename to compile
fa638b6a 384 if (gROOT->LoadMacro(tmpst.Data())) AliFatalF("Failed to load user function from %s",name);
0eea9d4d 385 fUsrMacro = new TMethodCall();
386 tmpst = tmpst.Data() + tmpst.Last('/')+1; //Strip away any path preceding the macro file name
387 int dot = tmpst.Last('.');
388 if (dot>0) tmpst.Resize(dot);
389 fUsrMacro->InitWithPrototype(tmpst.Data(),"Float_t *,Float_t *");
390 long args[2];
391 args[0] = (long)fArgsTmp;
392 args[1] = (long)fResTmp;
393 fUsrMacro->SetParamPtrs(args);
394 //
395}
396#endif
397
398//__________________________________________________________________________________________
399#ifdef _INC_CREATION_ALICHEB3D_
400void AliCheb3D::SetUsrFunction(void (*ptr)(float*,float*))
401{
5406439e 402 // assign user training function
403 //
0eea9d4d 404 if (fUsrMacro) delete fUsrMacro;
405 fUsrMacro = 0;
406 fUsrFunName = "";
407 gUsrFunAliCheb3D = ptr;
408}
409#endif
410
411//__________________________________________________________________________________________
412#ifdef _INC_CREATION_ALICHEB3D_
da7cd221 413void AliCheb3D::EvalUsrFunction(const Float_t *x, Float_t *res)
5406439e 414{
415 // evaluate user function value
416 //
0eea9d4d 417 for (int i=3;i--;) fArgsTmp[i] = x[i];
418 if (gUsrFunAliCheb3D) gUsrFunAliCheb3D(fArgsTmp,fResTmp);
419 else fUsrMacro->Execute();
420 for (int i=fDimOut;i--;) res[i] = fResTmp[i];
421}
422#endif
423
424//__________________________________________________________________________________________
425#ifdef _INC_CREATION_ALICHEB3D_
5406439e 426Int_t AliCheb3D::CalcChebCoefs(const Float_t *funval,int np, Float_t *outCoefs, Float_t prec)
0eea9d4d 427{
428 // Calculate Chebyshev coeffs using precomputed function values at np roots.
429 // If prec>0, estimate the highest coeff number providing the needed precision
430 //
431 double sm; // do summations in double to minimize the roundoff error
432 for (int ic=0;ic<np;ic++) { // compute coeffs
433 sm = 0;
434 for (int ir=0;ir<np;ir++) {
435 float rt = TMath::Cos( ic*(ir+0.5)*TMath::Pi()/np);
436 sm += funval[ir]*rt;
437 }
438 outCoefs[ic] = Float_t( sm * ((ic==0) ? 1./np : 2./np) );
439 }
440 //
441 if (prec<=0) return np;
442 //
443 sm = 0;
444 int cfMax = 0;
445 for (cfMax=np;cfMax--;) {
446 sm += TMath::Abs(outCoefs[cfMax]);
447 if (sm>=prec) break;
448 }
449 if (++cfMax==0) cfMax=1;
450 return cfMax;
451 //
452}
453#endif
454
455//__________________________________________________________________________________________
456#ifdef _INC_CREATION_ALICHEB3D_
457void AliCheb3D::DefineGrid(Int_t* npoints)
458{
459 // prepare the grid of Chebyshev roots in each dimension
460 const int kMinPoints = 1;
461 int ntot = 0;
462 fMaxCoefs = 1;
463 for (int id=3;id--;) {
464 fNPoints[id] = npoints[id];
fa638b6a 465 if (fNPoints[id]<kMinPoints) AliFatalF("at %d-th dimension %d point is requested, at least %d is needed",id,fNPoints[id],kMinPoints);
0eea9d4d 466 ntot += fNPoints[id];
467 fMaxCoefs *= fNPoints[id];
468 }
40389866 469 printf("Computing Chebyshev nodes on [%2d/%2d/%2d] grid\n",npoints[0],npoints[1],npoints[2]);
470 if (fGrid) delete[] fGrid;
0eea9d4d 471 fGrid = new Float_t [ntot];
472 //
473 int curp = 0;
474 for (int id=3;id--;) {
475 int np = fNPoints[id];
476 fGridOffs[id] = curp;
477 for (int ip=0;ip<np;ip++) {
478 Float_t x = TMath::Cos( TMath::Pi()*(ip+0.5)/np );
479 fGrid[curp++] = MapToExternal(x,id);
480 }
481 }
482 //
483}
484#endif
485
486//__________________________________________________________________________________________
487#ifdef _INC_CREATION_ALICHEB3D_
488Int_t AliCheb3D::ChebFit()
489{
490 // prepare parameterization for all output dimensions
491 int ir=0;
492 for (int i=fDimOut;i--;) ir+=ChebFit(i);
493 return ir;
494}
495#endif
496
497//__________________________________________________________________________________________
498#ifdef _INC_CREATION_ALICHEB3D_
499Int_t AliCheb3D::ChebFit(int dmOut)
500{
501 // prepare paramaterization of 3D function for dmOut-th dimension
502 int maxDim = 0;
503 for (int i=0;i<3;i++) if (maxDim<fNPoints[i]) maxDim = fNPoints[i];
504 Float_t *fvals = new Float_t [ fNPoints[0] ];
505 Float_t *tmpCoef3D = new Float_t [ fNPoints[0]*fNPoints[1]*fNPoints[2] ];
506 Float_t *tmpCoef2D = new Float_t [ fNPoints[0]*fNPoints[1] ];
507 Float_t *tmpCoef1D = new Float_t [ maxDim ];
508 //
0eea9d4d 509 // 1D Cheb.fit for 0-th dimension at current steps of remaining dimensions
510 int ncmax = 0;
511 //
40389866 512 printf("Dim%d : 00.00%% Done",dmOut);fflush(stdout);
0eea9d4d 513 AliCheb3DCalc* cheb = GetChebCalc(dmOut);
514 //
f69ec3f4 515 Float_t prec = cheb->GetPrecision();
516 if (prec<fgkMinPrec) prec = fPrec; // no specific precision for this dim.
517 //
518 Float_t rTiny = 0.1*prec/Float_t(maxDim); // neglect coefficient below this threshold
519 //
40389866 520 float ncals2count = fNPoints[2]*fNPoints[1]*fNPoints[0];
521 float ncals = 0;
522 float frac = 0;
523 float fracStep = 0.001;
524 //
0eea9d4d 525 for (int id2=fNPoints[2];id2--;) {
526 fArgsTmp[2] = fGrid[ fGridOffs[2]+id2 ];
527 //
528 for (int id1=fNPoints[1];id1--;) {
529 fArgsTmp[1] = fGrid[ fGridOffs[1]+id1 ];
530 //
531 for (int id0=fNPoints[0];id0--;) {
532 fArgsTmp[0] = fGrid[ fGridOffs[0]+id0 ];
533 EvalUsrFunction(); // compute function values at Chebyshev roots of 0-th dimension
534 fvals[id0] = fResTmp[dmOut];
40389866 535 float fr = (++ncals)/ncals2count;
536 if (fr-frac>=fracStep) {
537 frac = fr;
538 printf("\b\b\b\b\b\b\b\b\b\b\b");
539 printf("%05.2f%% Done",fr*100);
540 fflush(stdout);
541 }
542 //
0eea9d4d 543 }
f69ec3f4 544 int nc = CalcChebCoefs(fvals,fNPoints[0], tmpCoef1D, prec);
0eea9d4d 545 for (int id0=fNPoints[0];id0--;) tmpCoef2D[id1 + id0*fNPoints[1]] = tmpCoef1D[id0];
546 if (ncmax<nc) ncmax = nc; // max coefs to be kept in dim0 to guarantee needed precision
547 }
548 //
549 // once each 1d slice of given 2d slice is parametrized, parametrize the Cheb.coeffs
550 for (int id0=fNPoints[0];id0--;) {
551 CalcChebCoefs( tmpCoef2D+id0*fNPoints[1], fNPoints[1], tmpCoef1D, -1);
552 for (int id1=fNPoints[1];id1--;) tmpCoef3D[id2 + fNPoints[2]*(id1+id0*fNPoints[1])] = tmpCoef1D[id1];
553 }
554 }
555 //
556 // now fit the last dimensions Cheb.coefs
557 for (int id0=fNPoints[0];id0--;) {
558 for (int id1=fNPoints[1];id1--;) {
559 CalcChebCoefs( tmpCoef3D+ fNPoints[2]*(id1+id0*fNPoints[1]), fNPoints[2], tmpCoef1D, -1);
560 for (int id2=fNPoints[2];id2--;) tmpCoef3D[id2+ fNPoints[2]*(id1+id0*fNPoints[1])] = tmpCoef1D[id2]; // store on place
561 }
562 }
563 //
f69ec3f4 564 // now find 2D surface which separates significant coefficients of 3D matrix from nonsignificant ones (up to prec)
1d18ebe0 565 UShort_t *tmpCoefSurf = new UShort_t[ fNPoints[0]*fNPoints[1] ];
0eea9d4d 566 for (int id0=fNPoints[0];id0--;) for (int id1=fNPoints[1];id1--;) tmpCoefSurf[id1+id0*fNPoints[1]]=0;
567 Double_t resid = 0;
568 for (int id0=fNPoints[0];id0--;) {
569 for (int id1=fNPoints[1];id1--;) {
570 for (int id2=fNPoints[2];id2--;) {
571 int id = id2 + fNPoints[2]*(id1+id0*fNPoints[1]);
572 Float_t cfa = TMath::Abs(tmpCoef3D[id]);
5406439e 573 if (cfa < rTiny) {tmpCoef3D[id] = 0; continue;} // neglect coefs below the threshold
0eea9d4d 574 resid += cfa;
f69ec3f4 575 if (resid<prec) continue; // this coeff is negligible
0eea9d4d 576 // otherwise go back 1 step
577 resid -= cfa;
578 tmpCoefSurf[id1+id0*fNPoints[1]] = id2+1; // how many coefs to keep
579 break;
580 }
581 }
582 }
583 /*
584 printf("\n\nCoeffs\n");
585 int cnt = 0;
586 for (int id0=0;id0<fNPoints[0];id0++) {
587 for (int id1=0;id1<fNPoints[1];id1++) {
588 for (int id2=0;id2<fNPoints[2];id2++) {
589 printf("%2d%2d%2d %+.4e |",id0,id1,id2,tmpCoef3D[cnt++]);
590 }
591 printf("\n");
592 }
593 printf("\n");
594 }
595 */
596 // see if there are rows to reject, find max.significant column at each row
5406439e 597 int nRows = fNPoints[0];
1d18ebe0 598 UShort_t *tmpCols = new UShort_t[nRows];
0eea9d4d 599 for (int id0=fNPoints[0];id0--;) {
600 int id1 = fNPoints[1];
601 while (id1>0 && tmpCoefSurf[(id1-1)+id0*fNPoints[1]]==0) id1--;
602 tmpCols[id0] = id1;
603 }
604 // find max significant row
5406439e 605 for (int id0=nRows;id0--;) {if (tmpCols[id0]>0) break; nRows--;}
0eea9d4d 606 // find max significant column and fill the permanent storage for the max sigificant column of each row
5406439e 607 cheb->InitRows(nRows); // create needed arrays;
1d18ebe0 608 UShort_t *nColsAtRow = cheb->GetNColsAtRow();
609 UShort_t *colAtRowBg = cheb->GetColAtRowBg();
5406439e 610 int nCols = 0;
8f499a66 611 int nElemBound2D = 0;
5406439e 612 for (int id0=0;id0<nRows;id0++) {
613 nColsAtRow[id0] = tmpCols[id0]; // number of columns to store for this row
8f499a66 614 colAtRowBg[id0] = nElemBound2D; // begining of this row in 2D boundary surface
615 nElemBound2D += tmpCols[id0];
5406439e 616 if (nCols<nColsAtRow[id0]) nCols = nColsAtRow[id0];
0eea9d4d 617 }
5406439e 618 cheb->InitCols(nCols);
0eea9d4d 619 delete[] tmpCols;
620 //
621 // create the 2D matrix defining the boundary of significance for 3D coeffs.matrix
622 // and count the number of siginifacnt coefficients
623 //
8f499a66 624 cheb->InitElemBound2D(nElemBound2D);
1d18ebe0 625 UShort_t *coefBound2D0 = cheb->GetCoefBound2D0();
626 UShort_t *coefBound2D1 = cheb->GetCoefBound2D1();
0eea9d4d 627 fMaxCoefs = 0; // redefine number of coeffs
5406439e 628 for (int id0=0;id0<nRows;id0++) {
629 int nCLoc = nColsAtRow[id0];
630 int col0 = colAtRowBg[id0];
0eea9d4d 631 for (int id1=0;id1<nCLoc;id1++) {
5406439e 632 coefBound2D0[col0 + id1] = tmpCoefSurf[id1+id0*fNPoints[1]]; // number of coefs to store for 3-d dimension
633 coefBound2D1[col0 + id1] = fMaxCoefs;
634 fMaxCoefs += coefBound2D0[col0 + id1];
0eea9d4d 635 }
636 }
637 //
638 // create final compressed 3D matrix for significant coeffs
639 cheb->InitCoefs(fMaxCoefs);
5406439e 640 Float_t *coefs = cheb->GetCoefs();
0eea9d4d 641 int count = 0;
5406439e 642 for (int id0=0;id0<nRows;id0++) {
643 int ncLoc = nColsAtRow[id0];
644 int col0 = colAtRowBg[id0];
0eea9d4d 645 for (int id1=0;id1<ncLoc;id1++) {
5406439e 646 int ncf2 = coefBound2D0[col0 + id1];
0eea9d4d 647 for (int id2=0;id2<ncf2;id2++) {
5406439e 648 coefs[count++] = tmpCoef3D[id2 + fNPoints[2]*(id1+id0*fNPoints[1])];
0eea9d4d 649 }
650 }
651 }
652 /*
653 printf("\n\nNewSurf\n");
654 for (int id0=0;id0<fNPoints[0];id0++) {
655 for (int id1=0;id1<fNPoints[1];id1++) {
656 printf("(%2d %2d) %2d |",id0,id1,tmpCoefSurf[id1+id0*fNPoints[1]]);
657 }
658 printf("\n");
659 }
660 */
661 //
662 delete[] tmpCoefSurf;
663 delete[] tmpCoef1D;
664 delete[] tmpCoef2D;
665 delete[] tmpCoef3D;
666 delete[] fvals;
667 //
40389866 668 printf("\b\b\b\b\b\b\b\b\b\b\b\b");
669 printf("100.00%% Done\n");
0eea9d4d 670 return 1;
671}
672#endif
673
674//_______________________________________________
675#ifdef _INC_CREATION_ALICHEB3D_
676void AliCheb3D::SaveData(const char* outfile,Bool_t append) const
677{
678 // writes coefficients data to output text file, optionallt appending on the end of existing file
679 TString strf = outfile;
680 gSystem->ExpandPathName(strf);
681 FILE* stream = fopen(strf,append ? "a":"w");
682 SaveData(stream);
683 fclose(stream);
684 //
685}
686#endif
687
688//_______________________________________________
689#ifdef _INC_CREATION_ALICHEB3D_
690void AliCheb3D::SaveData(FILE* stream) const
691{
692 // writes coefficients data to existing output stream
693 //
694 fprintf(stream,"\n# These are automatically generated data for the Chebyshev interpolation of 3D->%dD function\n",fDimOut);
695 fprintf(stream,"#\nSTART %s\n",GetName());
696 fprintf(stream,"# Dimensionality of the output\n%d\n",fDimOut);
697 fprintf(stream,"# Interpolation abs. precision\n%+.8e\n",fPrec);
698 //
699 fprintf(stream,"# Lower boundaries of interpolation region\n");
700 for (int i=0;i<3;i++) fprintf(stream,"%+.8e\n",fBMin[i]);
701 fprintf(stream,"# Upper boundaries of interpolation region\n");
702 for (int i=0;i<3;i++) fprintf(stream,"%+.8e\n",fBMax[i]);
40389866 703 fprintf(stream,"# Parameterization for each output dimension follows:\n");
0eea9d4d 704 //
705 for (int i=0;i<fDimOut;i++) GetChebCalc(i)->SaveData(stream);
706 fprintf(stream,"#\nEND %s\n#\n",GetName());
707 //
708}
709#endif
710
7f42fc51 711//__________________________________________________________________________________________
712#ifdef _INC_CREATION_ALICHEB3D_
713void AliCheb3D::InvertSign()
714{
715 // invert the sign of all parameterizations
716 for (int i=fDimOut;i--;) {
717 AliCheb3DCalc* par = GetChebCalc(i);
718 int ncf = par->GetNCoefs();
719 float *coefs = par->GetCoefs();
720 for (int j=ncf;j--;) coefs[j] = -coefs[j];
721 }
722}
723#endif
724
725
0eea9d4d 726//_______________________________________________
727void AliCheb3D::LoadData(const char* inpFile)
728{
5406439e 729 // load coefficients data from txt file
730 //
0eea9d4d 731 TString strf = inpFile;
732 gSystem->ExpandPathName(strf);
733 FILE* stream = fopen(strf.Data(),"r");
734 LoadData(stream);
735 fclose(stream);
736 //
737}
738
739//_______________________________________________
740void AliCheb3D::LoadData(FILE* stream)
741{
5406439e 742 // load coefficients data from stream
743 //
fa638b6a 744 if (!stream) AliFatal("No stream provided");
0eea9d4d 745 TString buffs;
746 Clear();
747 AliCheb3DCalc::ReadLine(buffs,stream);
fa638b6a 748 if (!buffs.BeginsWith("START")) AliFatalF("Expected: \"START <fit_name>\", found \"%s\"",buffs.Data());
0eea9d4d 749 SetName(buffs.Data()+buffs.First(' ')+1);
750 //
751 AliCheb3DCalc::ReadLine(buffs,stream); // N output dimensions
752 fDimOut = buffs.Atoi();
fa638b6a 753 if (fDimOut<1) AliFatalF("Expected: '<number_of_output_dimensions>', found \"%s\"",buffs.Data());
0eea9d4d 754 //
755 SetDimOut(fDimOut);
756 //
757 AliCheb3DCalc::ReadLine(buffs,stream); // Interpolation abs. precision
758 fPrec = buffs.Atof();
fa638b6a 759 if (fPrec<=0) AliFatalF("Expected: '<abs.precision>', found \"%s\"",buffs.Data());
0eea9d4d 760 //
761 for (int i=0;i<3;i++) { // Lower boundaries of interpolation region
762 AliCheb3DCalc::ReadLine(buffs,stream);
763 fBMin[i] = buffs.Atof();
764 }
765 for (int i=0;i<3;i++) { // Upper boundaries of interpolation region
766 AliCheb3DCalc::ReadLine(buffs,stream);
767 fBMax[i] = buffs.Atof();
768 }
769 PrepareBoundaries(fBMin,fBMax);
770 //
771 // data for each output dimension
772 for (int i=0;i<fDimOut;i++) GetChebCalc(i)->LoadData(stream);
773 //
774 // check end_of_data record
775 AliCheb3DCalc::ReadLine(buffs,stream);
776 if (!buffs.BeginsWith("END") || !buffs.Contains(GetName())) {
fa638b6a 777 AliFatalF("Expected \"END %s\", found \"%s\"",GetName(),buffs.Data());
0eea9d4d 778 }
779 //
780}
781
782//_______________________________________________
f69ec3f4 783void AliCheb3D::SetDimOut(const int d, const float* prec)
0eea9d4d 784{
5406439e 785 // init output dimensions
0eea9d4d 786 fDimOut = d;
787 if (fResTmp) delete fResTmp;
40389866 788 fResTmp = new Float_t[fDimOut];
0eea9d4d 789 fChebCalc.Delete();
f69ec3f4 790 for (int i=0;i<d;i++) {
791 AliCheb3DCalc* clc = new AliCheb3DCalc();
792 clc->SetPrecision(prec && prec[i]>fgkMinPrec ? prec[i] : fPrec);
793 fChebCalc.AddAtAndExpand(clc,i);
794 }
0eea9d4d 795}
796
797//_______________________________________________
798void AliCheb3D::ShiftBound(int id,float dif)
799{
5406439e 800 // modify the bounds of the grid
801 //
0eea9d4d 802 if (id<0||id>2) {printf("Maximum 3 dimensions are supported\n"); return;}
803 fBMin[id] += dif;
804 fBMax[id] += dif;
805 fBOffset[id] += dif;
806}
807
808//_______________________________________________
809#ifdef _INC_CREATION_ALICHEB3D_
810TH1* AliCheb3D::TestRMS(int idim,int npoints,TH1* histo)
811{
812 // fills the difference between the original function and parameterization (for idim-th component of the output)
813 // to supplied histogram. Calculations are done in npoints random points.
814 // If the hostgram was not supplied, it will be created. It is up to the user to delete it!
815 if (!fUsrMacro) {
816 printf("No user function is set\n");
817 return 0;
818 }
f69ec3f4 819 float prc = GetChebCalc(idim)->GetPrecision();
820 if (prc<fgkMinPrec) prc = fPrec; // no dimension specific precision
821 if (!histo) histo = new TH1D(GetName(),"Control: Function - Parametrization",100,-2*prc,2*prc);
0eea9d4d 822 for (int ip=npoints;ip--;) {
823 gRandom->RndmArray(3,(Float_t *)fArgsTmp);
824 for (int i=3;i--;) fArgsTmp[i] = fBMin[i] + fArgsTmp[i]*(fBMax[i]-fBMin[i]);
825 EvalUsrFunction();
826 Float_t valFun = fResTmp[idim];
827 Eval(fArgsTmp,fResTmp);
828 Float_t valPar = fResTmp[idim];
829 histo->Fill(valFun - valPar);
830 }
831 return histo;
832 //
833}
834#endif
40389866 835
836//_______________________________________________
837#ifdef _INC_CREATION_ALICHEB3D_
1d18ebe0 838
f69ec3f4 839void AliCheb3D::EstimateNPoints(float prec, int gridBC[3][3],Int_t npd1,Int_t npd2,Int_t npd3)
1d18ebe0 840{
841 // Estimate number of points to generate a training data
842 //
843 const int kScp = 9;
844 const float kScl[9] = {0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9};
845 //
846 const float sclDim[2] = {0.001,0.999};
847 const int compDim[3][2] = { {1,2}, {2,0}, {0,1} };
848 static float xyz[3];
849 Int_t npdTst[3] = {npd1,npd2,npd3};
850 //
851
852 for (int i=3;i--;)for (int j=3;j--;) gridBC[i][j] = -1;
853 //
854 for (int idim=0;idim<3;idim++) {
855 float dimMN = fBMin[idim] + sclDim[0]*(fBMax[idim]-fBMin[idim]);
856 float dimMX = fBMin[idim] + sclDim[1]*(fBMax[idim]-fBMin[idim]);
857 //
858 int id1 = compDim[idim][0]; // 1st fixed dim
859 int id2 = compDim[idim][1]; // 2nd fixed dim
860 for (int i1=0;i1<kScp;i1++) {
861 xyz[ id1 ] = fBMin[id1] + kScl[i1]*( fBMax[id1]-fBMin[id1] );
862 for (int i2=0;i2<kScp;i2++) {
863 xyz[ id2 ] = fBMin[id2] + kScl[i2]*( fBMax[id2]-fBMin[id2] );
f69ec3f4 864 int* npt = GetNCNeeded(xyz,idim, dimMN,dimMX, prec, npdTst[idim]); // npoints for Bx,By,Bz
1d18ebe0 865 for (int ib=0;ib<3;ib++) if (npt[ib]>gridBC[ib][idim]) gridBC[ib][idim] = npt[ib];
866 }
867 }
868 }
869}
870
871/*
f69ec3f4 872void AliCheb3D::EstimateNPoints(float prec, int gridBC[3][3])
40389866 873{
5406439e 874 // Estimate number of points to generate a training data
875 //
40389866 876 const float sclA[9] = {0.1, 0.5, 0.9, 0.1, 0.5, 0.9, 0.1, 0.5, 0.9} ;
877 const float sclB[9] = {0.1, 0.1, 0.1, 0.5, 0.5, 0.5, 0.9, 0.9, 0.9} ;
878 const float sclDim[2] = {0.01,0.99};
879 const int compDim[3][2] = { {1,2}, {2,0}, {0,1} };
880 static float xyz[3];
881 //
882 for (int i=3;i--;)for (int j=3;j--;) gridBC[i][j] = -1;
883 //
884 for (int idim=0;idim<3;idim++) {
885 float dimMN = fBMin[idim] + sclDim[0]*(fBMax[idim]-fBMin[idim]);
886 float dimMX = fBMin[idim] + sclDim[1]*(fBMax[idim]-fBMin[idim]);
887 //
888 for (int it=0;it<9;it++) { // test in 9 points
889 int id1 = compDim[idim][0]; // 1st fixed dim
890 int id2 = compDim[idim][1]; // 2nd fixed dim
891 xyz[ id1 ] = fBMin[id1] + sclA[it]*( fBMax[id1]-fBMin[id1] );
892 xyz[ id2 ] = fBMin[id2] + sclB[it]*( fBMax[id2]-fBMin[id2] );
893 //
f69ec3f4 894 int* npt = GetNCNeeded(xyz,idim, dimMN,dimMX, prec); // npoints for Bx,By,Bz
2572efdf 895 for (int ib=0;ib<3;ib++) if (npt[ib]>gridBC[ib][idim]) gridBC[ib][idim] = npt[ib];//+2;
40389866 896 //
897 }
898 }
899}
900
901
902int* AliCheb3D::GetNCNeeded(float xyz[3],int DimVar, float mn,float mx, float prec)
903{
2572efdf 904 // estimate needed number of chebyshev coefs for given function description in DimVar dimension
40389866 905 // The values for two other dimensions must be set beforehand
906 //
907 static int curNC[3];
908 static int retNC[3];
909 const int kMaxPoint = 400;
910 float* gridVal = new float[3*kMaxPoint];
911 float* coefs = new float[3*kMaxPoint];
912 //
913 float scale = mx-mn;
914 float offs = mn + scale/2.0;
915 scale = 2./scale;
916 //
917 int curNP;
918 int maxNC=-1;
919 int maxNCPrev=-1;
920 for (int i=0;i<3;i++) retNC[i] = -1;
921 for (int i=0;i<3;i++) fArgsTmp[i] = xyz[i];
922 //
2572efdf 923 for (curNP=3; curNP<kMaxPoint; curNP+=3) {
40389866 924 maxNCPrev = maxNC;
925 //
926 for (int i=0;i<curNP;i++) { // get function values on Cheb. nodes
927 float x = TMath::Cos( TMath::Pi()*(i+0.5)/curNP );
928 fArgsTmp[DimVar] = x/scale+offs; // map to requested interval
929 EvalUsrFunction();
930 for (int ib=3;ib--;) gridVal[ib*kMaxPoint + i] = fResTmp[ib];
931 }
932 //
933 for (int ib=0;ib<3;ib++) {
934 curNC[ib] = AliCheb3D::CalcChebCoefs(&gridVal[ib*kMaxPoint], curNP, &coefs[ib*kMaxPoint],prec);
935 if (maxNC < curNC[ib]) maxNC = curNC[ib];
936 if (retNC[ib] < curNC[ib]) retNC[ib] = curNC[ib];
937 }
938 if ( (curNP-maxNC)>3 && (maxNC-maxNCPrev)<1 ) break;
939 maxNCPrev = maxNC;
940 //
941 }
942 delete[] gridVal;
943 delete[] coefs;
944 return retNC;
945 //
946}
1d18ebe0 947*/
948
949
950int* AliCheb3D::GetNCNeeded(float xyz[3],int DimVar, float mn,float mx, float prec, Int_t npCheck)
951{
952 // estimate needed number of chebyshev coefs for given function description in DimVar dimension
953 // The values for two other dimensions must be set beforehand
954 //
955 static int retNC[3];
956 static int npChLast = 0;
957 static float *gridVal=0,*coefs=0;
958 if (npCheck<3) npCheck = 3;
959 if (npChLast<npCheck) {
960 if (gridVal) delete[] gridVal;
961 if (coefs) delete[] coefs;
962 gridVal = new float[3*npCheck];
963 coefs = new float[3*npCheck];
964 npChLast = npCheck;
965 }
966 //
967 float scale = mx-mn;
968 float offs = mn + scale/2.0;
969 scale = 2./scale;
970 //
971 for (int i=0;i<3;i++) fArgsTmp[i] = xyz[i];
972 for (int i=0;i<npCheck;i++) {
973 fArgsTmp[DimVar] = TMath::Cos( TMath::Pi()*(i+0.5)/npCheck)/scale+offs; // map to requested interval
974 EvalUsrFunction();
975 for (int ib=3;ib--;) gridVal[ib*npCheck + i] = fResTmp[ib];
976 }
977 //
978 for (int ib=0;ib<3;ib++) retNC[ib] = AliCheb3D::CalcChebCoefs(&gridVal[ib*npCheck], npCheck, &coefs[ib*npCheck],prec);
979 return retNC;
980 //
981}
982
983
40389866 984#endif