Line data Source code
1 : /**************************************************************************
2 : * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 : * *
4 : * Author: The ALICE Off-line Project. *
5 : * Contributors are mentioned in the code where appropriate. *
6 : * *
7 : * Permission to use, copy, modify and distribute this software and its *
8 : * documentation strictly for non-commercial purposes is hereby granted *
9 : * without fee, provided that the above copyright notice appears in all *
10 : * copies and that both the copyright notice and this permission notice *
11 : * appear in the supporting documentation. The authors make no claims *
12 : * about the suitability of this software for any purpose. It is *
13 : * provided "as is" without express or implied warranty. *
14 : **************************************************************************/
15 :
16 : #include <TString.h>
17 : #include <TSystem.h>
18 : #include <TROOT.h>
19 : #include <TRandom.h>
20 : #include <stdio.h>
21 : #include <TMethodCall.h>
22 : #include <TMath.h>
23 : #include <TH1.h>
24 : #include "AliCheb3D.h"
25 : #include "AliCheb3DCalc.h"
26 : #include "AliLog.h"
27 :
28 176 : ClassImp(AliCheb3D)
29 :
30 : const Float_t AliCheb3D::fgkMinPrec = 1.e-12f;
31 :
32 : //__________________________________________________________________________________________
33 22920 : AliCheb3D::AliCheb3D() :
34 22920 : fDimOut(0),
35 22920 : fPrec(0),
36 22920 : fChebCalc(1),
37 22920 : fMaxCoefs(0),
38 22920 : fResTmp(0),
39 22920 : fGrid(0),
40 22920 : fUsrFunName(""),
41 22920 : fUsrMacro(0)
42 114600 : {
43 : // Default constructor
44 183360 : for (int i=3;i--;) {
45 68760 : fBMin[i] = fBMax[i] = fBScale[i] = fBOffset[i] = fArgsTmp[i] = 0;
46 68760 : fNPoints[i] = 0;
47 68760 : fGridOffs[i] = 0;
48 : }
49 45840 : }
50 :
51 : //__________________________________________________________________________________________
52 : AliCheb3D::AliCheb3D(const AliCheb3D& src) :
53 0 : TNamed(src),
54 0 : fDimOut(src.fDimOut),
55 0 : fPrec(src.fPrec),
56 0 : fChebCalc(1),
57 0 : fMaxCoefs(src.fMaxCoefs),
58 0 : fResTmp(0),
59 0 : fGrid(0),
60 0 : fUsrFunName(src.fUsrFunName),
61 0 : fUsrMacro(0)
62 0 : {
63 : // read coefs from text file
64 0 : for (int i=3;i--;) {
65 0 : fBMin[i] = src.fBMin[i];
66 0 : fBMax[i] = src.fBMax[i];
67 0 : fBScale[i] = src.fBScale[i];
68 0 : fBOffset[i] = src.fBOffset[i];
69 0 : fNPoints[i] = src.fNPoints[i];
70 0 : fGridOffs[i] = src.fGridOffs[i];
71 0 : fArgsTmp[i] = 0;
72 : }
73 0 : for (int i=0;i<fDimOut;i++) {
74 0 : AliCheb3DCalc* cbc = src.GetChebCalc(i);
75 0 : if (cbc) fChebCalc.AddAtAndExpand(new AliCheb3DCalc(*cbc),i);
76 : }
77 0 : }
78 :
79 : //__________________________________________________________________________________________
80 0 : AliCheb3D::AliCheb3D(const char* inpFile) :
81 0 : fDimOut(0),
82 0 : fPrec(0),
83 0 : fChebCalc(1),
84 0 : fMaxCoefs(0),
85 0 : fResTmp(0),
86 0 : fGrid(0),
87 0 : fUsrFunName(""),
88 0 : fUsrMacro(0)
89 0 : {
90 : // read coefs from text file
91 0 : for (int i=3;i--;) {
92 0 : fBMin[i] = fBMax[i] = fBScale[i] = fBOffset[i] = 0;
93 0 : fNPoints[i] = 0;
94 0 : fGridOffs[i] = 0;
95 0 : fArgsTmp[i] = 0;
96 : }
97 0 : LoadData(inpFile);
98 0 : }
99 :
100 : //__________________________________________________________________________________________
101 0 : AliCheb3D::AliCheb3D(FILE* stream) :
102 0 : fDimOut(0),
103 0 : fPrec(0),
104 0 : fChebCalc(1),
105 0 : fMaxCoefs(0),
106 0 : fResTmp(0),
107 0 : fGrid(0),
108 0 : fUsrFunName(""),
109 0 : fUsrMacro(0)
110 0 : {
111 : // read coefs from stream
112 0 : for (int i=3;i--;) {
113 0 : fBMin[i] = fBMax[i] = fBScale[i] = fBOffset[i] = 0;
114 0 : fNPoints[i] = 0;
115 0 : fGridOffs[i] = 0;
116 0 : fArgsTmp[i] = 0;
117 : }
118 0 : LoadData(stream);
119 0 : }
120 :
121 : //__________________________________________________________________________________________
122 : #ifdef _INC_CREATION_ALICHEB3D_
123 : AliCheb3D::AliCheb3D(const char* funName, int DimOut, const Float_t *bmin, const Float_t *bmax, Int_t *npoints, Float_t prec, const Float_t* precD) :
124 0 : TNamed(funName,funName),
125 0 : fDimOut(0),
126 0 : fPrec(TMath::Max(fgkMinPrec,prec)),
127 0 : fChebCalc(1),
128 0 : fMaxCoefs(0),
129 0 : fResTmp(0),
130 0 : fGrid(0),
131 0 : fUsrFunName("") ,
132 0 : fUsrMacro(0)
133 0 : {
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
141 : // precD : optional array with precisions per output dimension (if >fgkMinPrec will override common prec)
142 0 : if (DimOut<1) AliFatalF("Requested output dimension is %d",fDimOut);
143 0 : for (int i=3;i--;) {
144 0 : fBMin[i] = fBMax[i] = fBScale[i] = fBOffset[i] = 0;
145 0 : fNPoints[i] = 0;
146 0 : fGridOffs[i] = 0.;
147 0 : fArgsTmp[i] = 0;
148 : }
149 0 : SetDimOut(DimOut,precD);
150 0 : PrepareBoundaries(bmin,bmax);
151 0 : DefineGrid(npoints);
152 0 : SetUsrFunction(funName);
153 0 : ChebFit();
154 : //
155 0 : }
156 : #endif
157 :
158 : //__________________________________________________________________________________________
159 : #ifdef _INC_CREATION_ALICHEB3D_
160 : AliCheb3D::AliCheb3D(void (*ptr)(float*,float*), int DimOut, Float_t *bmin,Float_t *bmax, Int_t *npoints, Float_t prec, const Float_t* precD) :
161 0 : TNamed("Cheb3D","Cheb3D"),
162 0 : fDimOut(0),
163 0 : fPrec(TMath::Max(fgkMinPrec,prec)),
164 0 : fChebCalc(1),
165 0 : fMaxCoefs(0),
166 0 : fResTmp(0),
167 0 : fGrid(0),
168 0 : fUsrFunName(""),
169 0 : fUsrMacro(0)
170 0 : {
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
178 : // precD : optional array with precisions per output dimension (if >fgkMinPrec will override common prec)
179 : //
180 0 : if (DimOut<1) AliFatalF("Requested output dimension is %d",fDimOut);
181 0 : for (int i=3;i--;) {
182 0 : fBMin[i] = fBMax[i] = fBScale[i] = fBOffset[i] = 0;
183 0 : fNPoints[i] = 0;
184 0 : fGridOffs[i] = 0.;
185 0 : fArgsTmp[i] = 0;
186 : }
187 0 : SetDimOut(DimOut,precD);
188 0 : PrepareBoundaries(bmin,bmax);
189 0 : DefineGrid(npoints);
190 0 : SetUsrFunction(ptr);
191 0 : ChebFit();
192 : //
193 0 : }
194 : #endif
195 :
196 : //__________________________________________________________________________________________
197 : #ifdef _INC_CREATION_ALICHEB3D_
198 : AliCheb3D::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 0 : TNamed("Cheb3D","Cheb3D"),
200 0 : fDimOut(0),
201 0 : fPrec(TMath::Max(fgkMinPrec,prec)),
202 0 : fChebCalc(1),
203 0 : fMaxCoefs(0),
204 0 : fResTmp(0),
205 0 : fGrid(0),
206 0 : fUsrFunName(""),
207 0 : fUsrMacro(0)
208 0 : {
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
218 : // precD : optional array with precisions per output dimension (if >fgkMinPrec will override common prec)
219 : //
220 0 : if (DimOut<1) AliFatalF("Requested output dimension is %d",fDimOut);
221 0 : for (int i=3;i--;) {
222 0 : fBMin[i] = fBMax[i] = fBScale[i] = fBOffset[i] = 0;
223 0 : fNPoints[i] = 0;
224 0 : fGridOffs[i] = 0.;
225 0 : fArgsTmp[i] = 0;
226 : }
227 0 : SetDimOut(DimOut,precD);
228 0 : PrepareBoundaries(bmin,bmax);
229 0 : SetUsrFunction(ptr);
230 : //
231 0 : DefineGrid(npX);
232 0 : ChebFit(0);
233 0 : DefineGrid(npY);
234 0 : ChebFit(1);
235 0 : DefineGrid(npZ);
236 0 : ChebFit(2);
237 : //
238 0 : }
239 : #endif
240 :
241 :
242 : //__________________________________________________________________________________________
243 : #ifdef _INC_CREATION_ALICHEB3D_
244 : AliCheb3D::AliCheb3D(void (*ptr)(float*,float*), int DimOut, Float_t *bmin,Float_t *bmax, Float_t prec, Bool_t run, const Float_t* precD) :
245 0 : TNamed("Cheb3D","Cheb3D"),
246 0 : fDimOut(0),
247 0 : fPrec(TMath::Max(fgkMinPrec,prec)),
248 0 : fChebCalc(1),
249 0 : fMaxCoefs(0),
250 0 : fResTmp(0),
251 0 : fGrid(0),
252 0 : fUsrFunName(""),
253 0 : fUsrMacro(0)
254 0 : {
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
261 : // precD : optional array with precisions per output dimension (if >fgkMinPrec will override common prec)
262 : //
263 0 : if (DimOut!=3) AliFatalF("This constructor works only for 3D fits, %dD fit was requested",fDimOut);
264 0 : if (DimOut<1) AliFatalF("Requested output dimension is %d",fDimOut);
265 0 : for (int i=3;i--;) {
266 0 : fBMin[i] = fBMax[i] = fBScale[i] = fBOffset[i] = 0;
267 0 : fNPoints[i] = 0;
268 0 : fGridOffs[i] = 0.;
269 0 : fArgsTmp[i] = 0;
270 : }
271 0 : SetDimOut(DimOut,precD);
272 0 : PrepareBoundaries(bmin,bmax);
273 0 : SetUsrFunction(ptr);
274 : //
275 0 : if (run) {
276 0 : int gridNC[3][3];
277 0 : EstimateNPoints(prec,gridNC);
278 0 : DefineGrid(gridNC[0]);
279 0 : ChebFit(0);
280 0 : DefineGrid(gridNC[1]);
281 0 : ChebFit(1);
282 0 : DefineGrid(gridNC[2]);
283 0 : ChebFit(2);
284 0 : }
285 : //
286 0 : }
287 : #endif
288 :
289 :
290 : //__________________________________________________________________________________________
291 : AliCheb3D& AliCheb3D::operator=(const AliCheb3D& rhs)
292 : {
293 : // assignment operator
294 : //
295 0 : if (this != &rhs) {
296 0 : Clear();
297 0 : fDimOut = rhs.fDimOut;
298 0 : fPrec = rhs.fPrec;
299 0 : fMaxCoefs = rhs.fMaxCoefs;
300 0 : fUsrFunName = rhs.fUsrFunName;
301 0 : fUsrMacro = 0;
302 0 : for (int i=3;i--;) {
303 0 : fBMin[i] = rhs.fBMin[i];
304 0 : fBMax[i] = rhs.fBMax[i];
305 0 : fBScale[i] = rhs.fBScale[i];
306 0 : fBOffset[i] = rhs.fBOffset[i];
307 0 : fNPoints[i] = rhs.fNPoints[i];
308 : }
309 0 : for (int i=0;i<fDimOut;i++) {
310 0 : AliCheb3DCalc* cbc = rhs.GetChebCalc(i);
311 0 : if (cbc) fChebCalc.AddAtAndExpand(new AliCheb3DCalc(*cbc),i);
312 : }
313 0 : }
314 0 : return *this;
315 : //
316 0 : }
317 :
318 : //__________________________________________________________________________________________
319 : void AliCheb3D::Clear(const Option_t*)
320 : {
321 : // clear all dynamic structures
322 : //
323 45840 : if (fResTmp) { delete[] fResTmp; fResTmp = 0; }
324 22920 : if (fGrid) { delete[] fGrid; fGrid = 0; }
325 22920 : if (fUsrMacro) { delete fUsrMacro; fUsrMacro = 0;}
326 22920 : fChebCalc.SetOwner(kTRUE);
327 22920 : fChebCalc.Delete();
328 : //
329 22920 : }
330 :
331 : //__________________________________________________________________________________________
332 : void AliCheb3D::Print(const Option_t* opt) const
333 : {
334 : // print info
335 : //
336 0 : printf("%s: Chebyshev parameterization for 3D->%dD function. Precision: %e\n",GetName(),fDimOut,fPrec);
337 0 : printf("Region of validity: [%+.5e:%+.5e] [%+.5e:%+.5e] [%+.5e:%+.5e]\n",fBMin[0],fBMax[0],fBMin[1],fBMax[1],fBMin[2],fBMax[2]);
338 0 : TString opts = opt; opts.ToLower();
339 0 : if (opts.Contains("l")) for (int i=0;i<fDimOut;i++) {printf("Output dimension %d:\n",i+1); GetChebCalc(i)->Print();}
340 : //
341 0 : }
342 :
343 : //__________________________________________________________________________________________
344 : void AliCheb3D::PrepareBoundaries(const Float_t *bmin, const Float_t *bmax)
345 : {
346 : // Set and check boundaries defined by user, prepare coefficients for their conversion to [-1:1] interval
347 : //
348 0 : for (int i=3;i--;) {
349 0 : fBMin[i] = bmin[i];
350 0 : fBMax[i] = bmax[i];
351 0 : fBScale[i] = bmax[i]-bmin[i];
352 0 : if (fBScale[i]<=0) {
353 0 : AliFatalF("Boundaries for %d-th dimension are not increasing: %+.4e %+.4e\nStop\n",i,fBMin[i],fBMax[i]);
354 0 : }
355 0 : fBOffset[i] = bmin[i] + fBScale[i]/2.0;
356 0 : fBScale[i] = 2./fBScale[i];
357 : }
358 : //
359 0 : }
360 :
361 :
362 : //__________________________________________________________________________________________
363 : #ifdef _INC_CREATION_ALICHEB3D_
364 :
365 : // Pointer on user function (faster altrnative to TMethodCall)
366 : void (*gUsrFunAliCheb3D) (float* ,float* );
367 :
368 : void AliCheb3D::EvalUsrFunction()
369 : {
370 : // call user supplied function
371 0 : if (gUsrFunAliCheb3D) gUsrFunAliCheb3D(fArgsTmp,fResTmp);
372 0 : else fUsrMacro->Execute();
373 0 : }
374 :
375 : void AliCheb3D::SetUsrFunction(const char* name)
376 : {
377 : // load user macro with function definition and compile it
378 0 : gUsrFunAliCheb3D = 0;
379 0 : fUsrFunName = name;
380 0 : gSystem->ExpandPathName(fUsrFunName);
381 0 : if (fUsrMacro) delete fUsrMacro;
382 0 : TString tmpst = fUsrFunName;
383 0 : tmpst += "+"; // prepare filename to compile
384 0 : if (gROOT->LoadMacro(tmpst.Data())) AliFatalF("Failed to load user function from %s",name);
385 0 : fUsrMacro = new TMethodCall();
386 0 : tmpst = tmpst.Data() + tmpst.Last('/')+1; //Strip away any path preceding the macro file name
387 0 : int dot = tmpst.Last('.');
388 0 : if (dot>0) tmpst.Resize(dot);
389 0 : fUsrMacro->InitWithPrototype(tmpst.Data(),"Float_t *,Float_t *");
390 0 : long args[2];
391 0 : args[0] = (long)fArgsTmp;
392 0 : args[1] = (long)fResTmp;
393 0 : fUsrMacro->SetParamPtrs(args);
394 : //
395 0 : }
396 : #endif
397 :
398 : //__________________________________________________________________________________________
399 : #ifdef _INC_CREATION_ALICHEB3D_
400 : void AliCheb3D::SetUsrFunction(void (*ptr)(float*,float*))
401 : {
402 : // assign user training function
403 : //
404 0 : if (fUsrMacro) delete fUsrMacro;
405 0 : fUsrMacro = 0;
406 0 : fUsrFunName = "";
407 0 : gUsrFunAliCheb3D = ptr;
408 0 : }
409 : #endif
410 :
411 : //__________________________________________________________________________________________
412 : #ifdef _INC_CREATION_ALICHEB3D_
413 : void AliCheb3D::EvalUsrFunction(const Float_t *x, Float_t *res)
414 : {
415 : // evaluate user function value
416 : //
417 0 : for (int i=3;i--;) fArgsTmp[i] = x[i];
418 0 : if (gUsrFunAliCheb3D) gUsrFunAliCheb3D(fArgsTmp,fResTmp);
419 0 : else fUsrMacro->Execute();
420 0 : for (int i=fDimOut;i--;) res[i] = fResTmp[i];
421 0 : }
422 : #endif
423 :
424 : //__________________________________________________________________________________________
425 : #ifdef _INC_CREATION_ALICHEB3D_
426 : Int_t AliCheb3D::CalcChebCoefs(const Float_t *funval,int np, Float_t *outCoefs, Float_t prec)
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 0 : for (int ic=0;ic<np;ic++) { // compute coeffs
433 : sm = 0;
434 0 : for (int ir=0;ir<np;ir++) {
435 0 : float rt = TMath::Cos( ic*(ir+0.5)*TMath::Pi()/np);
436 0 : sm += funval[ir]*rt;
437 : }
438 0 : outCoefs[ic] = Float_t( sm * ((ic==0) ? 1./np : 2./np) );
439 : }
440 : //
441 0 : if (prec<=0) return np;
442 : //
443 : sm = 0;
444 : int cfMax = 0;
445 0 : for (cfMax=np;cfMax--;) {
446 0 : sm += TMath::Abs(outCoefs[cfMax]);
447 0 : if (sm>=prec) break;
448 : }
449 0 : if (++cfMax==0) cfMax=1;
450 : return cfMax;
451 : //
452 0 : }
453 : #endif
454 :
455 : //__________________________________________________________________________________________
456 : #ifdef _INC_CREATION_ALICHEB3D_
457 : void 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 0 : fMaxCoefs = 1;
463 0 : for (int id=3;id--;) {
464 0 : fNPoints[id] = npoints[id];
465 0 : if (fNPoints[id]<kMinPoints) AliFatalF("at %d-th dimension %d point is requested, at least %d is needed",id,fNPoints[id],kMinPoints);
466 0 : ntot += fNPoints[id];
467 0 : fMaxCoefs *= fNPoints[id];
468 : }
469 0 : printf("Computing Chebyshev nodes on [%2d/%2d/%2d] grid\n",npoints[0],npoints[1],npoints[2]);
470 0 : if (fGrid) delete[] fGrid;
471 0 : fGrid = new Float_t [ntot];
472 : //
473 : int curp = 0;
474 0 : for (int id=3;id--;) {
475 0 : int np = fNPoints[id];
476 0 : fGridOffs[id] = curp;
477 0 : for (int ip=0;ip<np;ip++) {
478 0 : Float_t x = TMath::Cos( TMath::Pi()*(ip+0.5)/np );
479 0 : fGrid[curp++] = MapToExternal(x,id);
480 : }
481 : }
482 : //
483 0 : }
484 : #endif
485 :
486 : //__________________________________________________________________________________________
487 : #ifdef _INC_CREATION_ALICHEB3D_
488 : Int_t AliCheb3D::ChebFit()
489 : {
490 : // prepare parameterization for all output dimensions
491 : int ir=0;
492 0 : for (int i=fDimOut;i--;) ir+=ChebFit(i);
493 0 : return ir;
494 : }
495 : #endif
496 :
497 : //__________________________________________________________________________________________
498 : #ifdef _INC_CREATION_ALICHEB3D_
499 : Int_t AliCheb3D::ChebFit(int dmOut)
500 : {
501 : // prepare paramaterization of 3D function for dmOut-th dimension
502 : int maxDim = 0;
503 0 : for (int i=0;i<3;i++) if (maxDim<fNPoints[i]) maxDim = fNPoints[i];
504 0 : Float_t *fvals = new Float_t [ fNPoints[0] ];
505 0 : Float_t *tmpCoef3D = new Float_t [ fNPoints[0]*fNPoints[1]*fNPoints[2] ];
506 0 : Float_t *tmpCoef2D = new Float_t [ fNPoints[0]*fNPoints[1] ];
507 0 : Float_t *tmpCoef1D = new Float_t [ maxDim ];
508 : //
509 : // 1D Cheb.fit for 0-th dimension at current steps of remaining dimensions
510 : int ncmax = 0;
511 : //
512 0 : printf("Dim%d : 00.00%% Done",dmOut);fflush(stdout);
513 0 : AliCheb3DCalc* cheb = GetChebCalc(dmOut);
514 : //
515 0 : Float_t prec = cheb->GetPrecision();
516 0 : if (prec<fgkMinPrec) prec = fPrec; // no specific precision for this dim.
517 : //
518 0 : Float_t rTiny = 0.1*prec/Float_t(maxDim); // neglect coefficient below this threshold
519 : //
520 0 : float ncals2count = fNPoints[2]*fNPoints[1]*fNPoints[0];
521 : float ncals = 0;
522 : float frac = 0;
523 : float fracStep = 0.001;
524 : //
525 0 : for (int id2=fNPoints[2];id2--;) {
526 0 : fArgsTmp[2] = fGrid[ fGridOffs[2]+id2 ];
527 : //
528 0 : for (int id1=fNPoints[1];id1--;) {
529 0 : fArgsTmp[1] = fGrid[ fGridOffs[1]+id1 ];
530 : //
531 0 : for (int id0=fNPoints[0];id0--;) {
532 0 : fArgsTmp[0] = fGrid[ fGridOffs[0]+id0 ];
533 0 : EvalUsrFunction(); // compute function values at Chebyshev roots of 0-th dimension
534 0 : fvals[id0] = fResTmp[dmOut];
535 0 : float fr = (++ncals)/ncals2count;
536 0 : if (fr-frac>=fracStep) {
537 : frac = fr;
538 0 : printf("\b\b\b\b\b\b\b\b\b\b\b");
539 0 : printf("%05.2f%% Done",fr*100);
540 0 : fflush(stdout);
541 0 : }
542 : //
543 : }
544 0 : int nc = CalcChebCoefs(fvals,fNPoints[0], tmpCoef1D, prec);
545 0 : for (int id0=fNPoints[0];id0--;) tmpCoef2D[id1 + id0*fNPoints[1]] = tmpCoef1D[id0];
546 0 : 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 0 : for (int id0=fNPoints[0];id0--;) {
551 0 : CalcChebCoefs( tmpCoef2D+id0*fNPoints[1], fNPoints[1], tmpCoef1D, -1);
552 0 : 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 0 : for (int id0=fNPoints[0];id0--;) {
558 0 : for (int id1=fNPoints[1];id1--;) {
559 0 : CalcChebCoefs( tmpCoef3D+ fNPoints[2]*(id1+id0*fNPoints[1]), fNPoints[2], tmpCoef1D, -1);
560 0 : for (int id2=fNPoints[2];id2--;) tmpCoef3D[id2+ fNPoints[2]*(id1+id0*fNPoints[1])] = tmpCoef1D[id2]; // store on place
561 : }
562 : }
563 : //
564 : // now find 2D surface which separates significant coefficients of 3D matrix from nonsignificant ones (up to prec)
565 0 : UShort_t *tmpCoefSurf = new UShort_t[ fNPoints[0]*fNPoints[1] ];
566 0 : for (int id0=fNPoints[0];id0--;) for (int id1=fNPoints[1];id1--;) tmpCoefSurf[id1+id0*fNPoints[1]]=0;
567 : Double_t resid = 0;
568 0 : for (int id0=fNPoints[0];id0--;) {
569 0 : for (int id1=fNPoints[1];id1--;) {
570 0 : for (int id2=fNPoints[2];id2--;) {
571 0 : int id = id2 + fNPoints[2]*(id1+id0*fNPoints[1]);
572 0 : Float_t cfa = TMath::Abs(tmpCoef3D[id]);
573 0 : if (cfa < rTiny) {tmpCoef3D[id] = 0; continue;} // neglect coefs below the threshold
574 0 : resid += cfa;
575 0 : if (resid<prec) continue; // this coeff is negligible
576 : // otherwise go back 1 step
577 0 : resid -= cfa;
578 0 : tmpCoefSurf[id1+id0*fNPoints[1]] = id2+1; // how many coefs to keep
579 0 : 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
597 0 : int nRows = fNPoints[0];
598 0 : UShort_t *tmpCols = new UShort_t[nRows];
599 0 : for (int id0=fNPoints[0];id0--;) {
600 0 : int id1 = fNPoints[1];
601 0 : while (id1>0 && tmpCoefSurf[(id1-1)+id0*fNPoints[1]]==0) id1--;
602 0 : tmpCols[id0] = id1;
603 : }
604 : // find max significant row
605 0 : for (int id0=nRows;id0--;) {if (tmpCols[id0]>0) break; nRows--;}
606 : // find max significant column and fill the permanent storage for the max sigificant column of each row
607 0 : cheb->InitRows(nRows); // create needed arrays;
608 0 : UShort_t *nColsAtRow = cheb->GetNColsAtRow();
609 0 : UShort_t *colAtRowBg = cheb->GetColAtRowBg();
610 : int nCols = 0;
611 : int nElemBound2D = 0;
612 0 : for (int id0=0;id0<nRows;id0++) {
613 0 : nColsAtRow[id0] = tmpCols[id0]; // number of columns to store for this row
614 0 : colAtRowBg[id0] = nElemBound2D; // begining of this row in 2D boundary surface
615 0 : nElemBound2D += tmpCols[id0];
616 0 : if (nCols<nColsAtRow[id0]) nCols = nColsAtRow[id0];
617 : }
618 0 : cheb->InitCols(nCols);
619 0 : 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 : //
624 0 : cheb->InitElemBound2D(nElemBound2D);
625 0 : UShort_t *coefBound2D0 = cheb->GetCoefBound2D0();
626 0 : UShort_t *coefBound2D1 = cheb->GetCoefBound2D1();
627 0 : fMaxCoefs = 0; // redefine number of coeffs
628 0 : for (int id0=0;id0<nRows;id0++) {
629 0 : int nCLoc = nColsAtRow[id0];
630 0 : int col0 = colAtRowBg[id0];
631 0 : for (int id1=0;id1<nCLoc;id1++) {
632 0 : coefBound2D0[col0 + id1] = tmpCoefSurf[id1+id0*fNPoints[1]]; // number of coefs to store for 3-d dimension
633 0 : coefBound2D1[col0 + id1] = fMaxCoefs;
634 0 : fMaxCoefs += coefBound2D0[col0 + id1];
635 : }
636 : }
637 : //
638 : // create final compressed 3D matrix for significant coeffs
639 0 : cheb->InitCoefs(fMaxCoefs);
640 0 : Float_t *coefs = cheb->GetCoefs();
641 : int count = 0;
642 0 : for (int id0=0;id0<nRows;id0++) {
643 0 : int ncLoc = nColsAtRow[id0];
644 0 : int col0 = colAtRowBg[id0];
645 0 : for (int id1=0;id1<ncLoc;id1++) {
646 0 : int ncf2 = coefBound2D0[col0 + id1];
647 0 : for (int id2=0;id2<ncf2;id2++) {
648 0 : coefs[count++] = tmpCoef3D[id2 + fNPoints[2]*(id1+id0*fNPoints[1])];
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 0 : delete[] tmpCoefSurf;
663 0 : delete[] tmpCoef1D;
664 0 : delete[] tmpCoef2D;
665 0 : delete[] tmpCoef3D;
666 0 : delete[] fvals;
667 : //
668 0 : printf("\b\b\b\b\b\b\b\b\b\b\b\b");
669 0 : printf("100.00%% Done\n");
670 0 : return 1;
671 : }
672 : #endif
673 :
674 : //_______________________________________________
675 : #ifdef _INC_CREATION_ALICHEB3D_
676 : void 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 0 : TString strf = outfile;
680 0 : gSystem->ExpandPathName(strf);
681 0 : FILE* stream = fopen(strf,append ? "a":"w");
682 0 : SaveData(stream);
683 0 : fclose(stream);
684 : //
685 0 : }
686 : #endif
687 :
688 : //_______________________________________________
689 : #ifdef _INC_CREATION_ALICHEB3D_
690 : void AliCheb3D::SaveData(FILE* stream) const
691 : {
692 : // writes coefficients data to existing output stream
693 : //
694 0 : fprintf(stream,"\n# These are automatically generated data for the Chebyshev interpolation of 3D->%dD function\n",fDimOut);
695 0 : fprintf(stream,"#\nSTART %s\n",GetName());
696 0 : fprintf(stream,"# Dimensionality of the output\n%d\n",fDimOut);
697 0 : fprintf(stream,"# Interpolation abs. precision\n%+.8e\n",fPrec);
698 : //
699 0 : fprintf(stream,"# Lower boundaries of interpolation region\n");
700 0 : for (int i=0;i<3;i++) fprintf(stream,"%+.8e\n",fBMin[i]);
701 0 : fprintf(stream,"# Upper boundaries of interpolation region\n");
702 0 : for (int i=0;i<3;i++) fprintf(stream,"%+.8e\n",fBMax[i]);
703 0 : fprintf(stream,"# Parameterization for each output dimension follows:\n");
704 : //
705 0 : for (int i=0;i<fDimOut;i++) GetChebCalc(i)->SaveData(stream);
706 0 : fprintf(stream,"#\nEND %s\n#\n",GetName());
707 : //
708 0 : }
709 : #endif
710 :
711 : //__________________________________________________________________________________________
712 : #ifdef _INC_CREATION_ALICHEB3D_
713 : void AliCheb3D::InvertSign()
714 : {
715 : // invert the sign of all parameterizations
716 0 : for (int i=fDimOut;i--;) {
717 0 : AliCheb3DCalc* par = GetChebCalc(i);
718 0 : int ncf = par->GetNCoefs();
719 0 : float *coefs = par->GetCoefs();
720 0 : for (int j=ncf;j--;) coefs[j] = -coefs[j];
721 : }
722 0 : }
723 : #endif
724 :
725 :
726 : //_______________________________________________
727 : void AliCheb3D::LoadData(const char* inpFile)
728 : {
729 : // load coefficients data from txt file
730 : //
731 0 : TString strf = inpFile;
732 0 : gSystem->ExpandPathName(strf);
733 0 : FILE* stream = fopen(strf.Data(),"r");
734 0 : LoadData(stream);
735 0 : fclose(stream);
736 : //
737 0 : }
738 :
739 : //_______________________________________________
740 : void AliCheb3D::LoadData(FILE* stream)
741 : {
742 : // load coefficients data from stream
743 : //
744 0 : if (!stream) AliFatal("No stream provided");
745 0 : TString buffs;
746 0 : Clear();
747 0 : AliCheb3DCalc::ReadLine(buffs,stream);
748 0 : if (!buffs.BeginsWith("START")) AliFatalF("Expected: \"START <fit_name>\", found \"%s\"",buffs.Data());
749 0 : SetName(buffs.Data()+buffs.First(' ')+1);
750 : //
751 0 : AliCheb3DCalc::ReadLine(buffs,stream); // N output dimensions
752 0 : fDimOut = buffs.Atoi();
753 0 : if (fDimOut<1) AliFatalF("Expected: '<number_of_output_dimensions>', found \"%s\"",buffs.Data());
754 : //
755 0 : SetDimOut(fDimOut);
756 : //
757 0 : AliCheb3DCalc::ReadLine(buffs,stream); // Interpolation abs. precision
758 0 : fPrec = buffs.Atof();
759 0 : if (fPrec<=0) AliFatalF("Expected: '<abs.precision>', found \"%s\"",buffs.Data());
760 : //
761 0 : for (int i=0;i<3;i++) { // Lower boundaries of interpolation region
762 0 : AliCheb3DCalc::ReadLine(buffs,stream);
763 0 : fBMin[i] = buffs.Atof();
764 : }
765 0 : for (int i=0;i<3;i++) { // Upper boundaries of interpolation region
766 0 : AliCheb3DCalc::ReadLine(buffs,stream);
767 0 : fBMax[i] = buffs.Atof();
768 : }
769 0 : PrepareBoundaries(fBMin,fBMax);
770 : //
771 : // data for each output dimension
772 0 : for (int i=0;i<fDimOut;i++) GetChebCalc(i)->LoadData(stream);
773 : //
774 : // check end_of_data record
775 0 : AliCheb3DCalc::ReadLine(buffs,stream);
776 0 : if (!buffs.BeginsWith("END") || !buffs.Contains(GetName())) {
777 0 : AliFatalF("Expected \"END %s\", found \"%s\"",GetName(),buffs.Data());
778 0 : }
779 : //
780 0 : }
781 :
782 : //_______________________________________________
783 : void AliCheb3D::SetDimOut(const int d, const float* prec)
784 : {
785 : // init output dimensions
786 0 : fDimOut = d;
787 0 : if (fResTmp) delete fResTmp;
788 0 : fResTmp = new Float_t[fDimOut];
789 0 : fChebCalc.Delete();
790 0 : for (int i=0;i<d;i++) {
791 0 : AliCheb3DCalc* clc = new AliCheb3DCalc();
792 0 : clc->SetPrecision(prec && prec[i]>fgkMinPrec ? prec[i] : fPrec);
793 0 : fChebCalc.AddAtAndExpand(clc,i);
794 : }
795 0 : }
796 :
797 : //_______________________________________________
798 : void AliCheb3D::ShiftBound(int id,float dif)
799 : {
800 : // modify the bounds of the grid
801 : //
802 0 : if (id<0||id>2) {printf("Maximum 3 dimensions are supported\n"); return;}
803 0 : fBMin[id] += dif;
804 0 : fBMax[id] += dif;
805 0 : fBOffset[id] += dif;
806 0 : }
807 :
808 : //_______________________________________________
809 : #ifdef _INC_CREATION_ALICHEB3D_
810 : TH1* 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 0 : if (!fUsrMacro) {
816 0 : printf("No user function is set\n");
817 0 : return 0;
818 : }
819 0 : float prc = GetChebCalc(idim)->GetPrecision();
820 0 : if (prc<fgkMinPrec) prc = fPrec; // no dimension specific precision
821 0 : if (!histo) histo = new TH1D(GetName(),"Control: Function - Parametrization",100,-2*prc,2*prc);
822 0 : for (int ip=npoints;ip--;) {
823 0 : gRandom->RndmArray(3,(Float_t *)fArgsTmp);
824 0 : for (int i=3;i--;) fArgsTmp[i] = fBMin[i] + fArgsTmp[i]*(fBMax[i]-fBMin[i]);
825 0 : EvalUsrFunction();
826 0 : Float_t valFun = fResTmp[idim];
827 0 : Eval(fArgsTmp,fResTmp);
828 0 : Float_t valPar = fResTmp[idim];
829 0 : histo->Fill(valFun - valPar);
830 : }
831 : return histo;
832 : //
833 0 : }
834 : #endif
835 :
836 : //_______________________________________________
837 : #ifdef _INC_CREATION_ALICHEB3D_
838 :
839 : void AliCheb3D::EstimateNPoints(float prec, int gridBC[3][3],Int_t npd1,Int_t npd2,Int_t npd3)
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 0 : Int_t npdTst[3] = {npd1,npd2,npd3};
850 : //
851 :
852 0 : for (int i=3;i--;)for (int j=3;j--;) gridBC[i][j] = -1;
853 : //
854 0 : for (int idim=0;idim<3;idim++) {
855 0 : float dimMN = fBMin[idim] + sclDim[0]*(fBMax[idim]-fBMin[idim]);
856 0 : float dimMX = fBMin[idim] + sclDim[1]*(fBMax[idim]-fBMin[idim]);
857 : //
858 0 : int id1 = compDim[idim][0]; // 1st fixed dim
859 0 : int id2 = compDim[idim][1]; // 2nd fixed dim
860 0 : for (int i1=0;i1<kScp;i1++) {
861 0 : xyz[ id1 ] = fBMin[id1] + kScl[i1]*( fBMax[id1]-fBMin[id1] );
862 0 : for (int i2=0;i2<kScp;i2++) {
863 0 : xyz[ id2 ] = fBMin[id2] + kScl[i2]*( fBMax[id2]-fBMin[id2] );
864 0 : int* npt = GetNCNeeded(xyz,idim, dimMN,dimMX, prec, npdTst[idim]); // npoints for Bx,By,Bz
865 0 : for (int ib=0;ib<3;ib++) if (npt[ib]>gridBC[ib][idim]) gridBC[ib][idim] = npt[ib];
866 : }
867 : }
868 : }
869 0 : }
870 :
871 : /*
872 : void AliCheb3D::EstimateNPoints(float prec, int gridBC[3][3])
873 : {
874 : // Estimate number of points to generate a training data
875 : //
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 : //
894 : int* npt = GetNCNeeded(xyz,idim, dimMN,dimMX, prec); // npoints for Bx,By,Bz
895 : for (int ib=0;ib<3;ib++) if (npt[ib]>gridBC[ib][idim]) gridBC[ib][idim] = npt[ib];//+2;
896 : //
897 : }
898 : }
899 : }
900 :
901 :
902 : int* AliCheb3D::GetNCNeeded(float xyz[3],int DimVar, float mn,float mx, float prec)
903 : {
904 : // estimate needed number of chebyshev coefs for given function description in DimVar dimension
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 : //
923 : for (curNP=3; curNP<kMaxPoint; curNP+=3) {
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 : }
947 : */
948 :
949 :
950 : int* 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 0 : if (npCheck<3) npCheck = 3;
959 0 : if (npChLast<npCheck) {
960 0 : if (gridVal) delete[] gridVal;
961 0 : if (coefs) delete[] coefs;
962 0 : gridVal = new float[3*npCheck];
963 0 : coefs = new float[3*npCheck];
964 0 : npChLast = npCheck;
965 0 : }
966 : //
967 0 : float scale = mx-mn;
968 0 : float offs = mn + scale/2.0;
969 0 : scale = 2./scale;
970 : //
971 0 : for (int i=0;i<3;i++) fArgsTmp[i] = xyz[i];
972 0 : for (int i=0;i<npCheck;i++) {
973 0 : fArgsTmp[DimVar] = TMath::Cos( TMath::Pi()*(i+0.5)/npCheck)/scale+offs; // map to requested interval
974 0 : EvalUsrFunction();
975 0 : for (int ib=3;ib--;) gridVal[ib*npCheck + i] = fResTmp[ib];
976 : }
977 : //
978 0 : for (int ib=0;ib<3;ib++) retNC[ib] = AliCheb3D::CalcChebCoefs(&gridVal[ib*npCheck], npCheck, &coefs[ib*npCheck],prec);
979 0 : return retNC;
980 : //
981 : }
982 :
983 :
984 : #endif
|