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