Project
Loading...
Searching...
No Matches
Chebyshev3D.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
15
16#ifndef ALICEO2_MATHUTILS_CHEBYSHEV3D_H_
17#define ALICEO2_MATHUTILS_CHEBYSHEV3D_H_
18
19#include <TNamed.h> // for TNamed
20#include <TObjArray.h> // for TObjArray
21#include <cstdio> // for FILE, stdout
22#include "MathUtils/Chebyshev3DCalc.h" // for Chebyshev3DCalc, etc
23#include "Rtypes.h" // for Float_t, Int_t, Double_t, Bool_t, etc
24#include "TString.h" // for TString
25
26class TH1; // lines 15-15
27class TMethodCall; // lines 16-16
28
29namespace o2
30{
31namespace math_utils
32{
33
64class Chebyshev3D : public TNamed
65{
66 public:
68
70
71 Chebyshev3D(const char* inpFile);
72
73 Chebyshev3D(FILE* stream);
74
75#ifdef _INC_CREATION_Chebyshev3D_
85 Chebyshev3D(const char* funName, Int_t dimOut, const Float_t* bmin, const Float_t* bmax, const Int_t* npoints,
86 Float_t prec = 1E-6, const Float_t* precD = nullptr);
96 Chebyshev3D(void (*ptr)(float*, float*), Int_t dimOut, const Float_t* bmin, const Float_t* bmax, const Int_t* npoints,
97 Float_t prec = 1E-6, const Float_t* precD = nullptr);
109 Chebyshev3D(void (*ptr)(float*, float*), int dimOut, const Float_t* bmin, const Float_t* bmax,
110 const Int_t* npX, const Int_t* npY, const Int_t* npZ,
111 Float_t prec = 1E-6, const Float_t* precD = nullptr);
120 Chebyshev3D(void (*ptr)(float*, float*), int DimOut, const Float_t* bmin, const Float_t* bmax, Float_t prec = 1E-6,
121 Bool_t run = kTRUE, const Float_t* precD = nullptr);
122#endif
123
124 ~Chebyshev3D() override
125 {
126 Clear();
127 }
128
129 Chebyshev3D& operator=(const Chebyshev3D& rhs);
130
131 void Eval(const Float_t* par, Float_t* res);
132
133 Float_t Eval(const Float_t* par, int idim);
134
135 void Eval(const Double_t* par, Double_t* res);
136
137 Double_t Eval(const Double_t* par, int idim);
138
139 void evaluateDerivative(int dimd, const Float_t* par, Float_t* res);
140
141 void evaluateDerivative2(int dimd1, int dimd2, const Float_t* par, Float_t* res);
142
143 Float_t evaluateDerivative(int dimd, const Float_t* par, int idim);
144
145 Float_t evaluateDerivative2(int dimd1, int dimd2, const Float_t* par, int idim);
146
147 void evaluateDerivative3D(const Float_t* par, Float_t dbdr[3][3]);
148
149 void evaluateDerivative3D2(const Float_t* par, Float_t dbdrdr[3][3][3]);
150
151 void Print(const Option_t* opt = "") const override;
152
153 Bool_t isInside(const Float_t* par) const;
154
155 Bool_t isInside(const Double_t* par) const;
156
158 {
159 return (Chebyshev3DCalc*)mChebyshevParameter.UncheckedAt(i);
160 }
161
163 {
164 return mMinBoundaries[i];
165 }
166
168 {
169 return mMaxBoundaries[i];
170 }
171
173 {
174 return (float*)mMinBoundaries;
175 }
176
178 {
179 return (float*)mMaxBoundaries;
180 }
181
183 {
184 return (float*)mBoundaryMappingScale;
185 }
186
188 {
189 return (float*)mBoundaryMappingOffset;
190 }
191
193 {
194 return mPrecision;
195 }
196
197 void shiftBound(int id, float dif);
198
199 void loadData(const char* inpFile);
200
201 void loadData(FILE* stream);
202
203#ifdef _INC_CREATION_Chebyshev3D_
204 void invertSign();
205 int* getNcNeeded(float xyz[3], int dimVar, float mn, float mx, float prec, Int_t npCheck = 30);
206 void estimateNumberOfPoints(float prec, int gridBC[3][3], Int_t npd1 = 30, Int_t npd2 = 30, Int_t npd3 = 30);
207 void saveData(const char* outfile, Bool_t append = kFALSE) const;
208 void saveData(FILE* stream = stdout) const;
209
210 void setuserFunction(const char* name);
211 void setuserFunction(void (*ptr)(float*, float*));
212 void evaluateUserFunction(const Float_t* x, Float_t* res);
213 TH1* TestRMS(int idim, int npoints = 1000, TH1* histo = nullptr);
214 static Int_t calculateChebyshevCoefficients(const Float_t* funval, int np, Float_t* outCoefs, Float_t prec = -1);
215#endif
216
217 protected:
218 void Clear(const Option_t* option = "") override;
219
220 void setDimOut(const int d, const float* prec = nullptr);
221
222 void prepareBoundaries(const Float_t* bmin, const Float_t* bmax);
223
224#ifdef _INC_CREATION_Chebyshev3D_
225 void evaluateUserFunction();
226 void defineGrid(const Int_t* npoints);
227 Int_t chebyshevFit(); // fit all output dimensions
228 Int_t chebyshevFit(int dmOut);
229 void setPrecision(float prec)
230 {
231 mPrecision = prec;
232 }
233#endif
234
235 Float_t mapToInternal(Float_t x, Int_t d) const; // map x to [-1:1]
237 {
238 return x / mBoundaryMappingScale[d] + mBoundaryMappingOffset[d];
239 } // map from [-1:1] to x
240 Double_t mapToInternal(Double_t x, Int_t d) const; // map x to [-1:1]
241 Double_t mapToExternal(Double_t x, Int_t d) const
242 {
243 return x / mBoundaryMappingScale[d] + mBoundaryMappingOffset[d];
244 } // map from [-1:1] to x
245
246 private:
247 Int_t mOutputArrayDimension;
248 Float_t mPrecision;
249 Float_t mMinBoundaries[3];
250 Float_t mMaxBoundaries[3];
251 Float_t mBoundaryMappingScale[3];
252 Float_t mBoundaryMappingOffset[3];
253 TObjArray mChebyshevParameter;
254
255 Int_t mMaxCoefficients;
256 Int_t mNumberOfPoints[3];
257 Float_t mTemporaryCoefficient[3];
258 Float_t* mTemporaryUserResults;
259 Float_t* mTemporaryChebyshevGrid;
260 Int_t mTemporaryChebyshevGridOffs[3];
261 TString mUserFunctionName;
262 TMethodCall* mUserMacro;
263
264 static const Float_t sMinimumPrecision;
265
266 ClassDefOverride(o2::math_utils::Chebyshev3D,
267 2) // Chebyshev parametrization for 3D->N function
268};
269
271inline Bool_t Chebyshev3D::isInside(const Float_t* par) const
272{
273 for (int i = 3; i--;) {
274 if (mMinBoundaries[i] > par[i] || par[i] > mMaxBoundaries[i]) {
275 return kFALSE;
276 }
277 }
278 return kTRUE;
279}
280
282inline Bool_t Chebyshev3D::isInside(const Double_t* par) const
283{
284 for (int i = 3; i--;) {
285 if (mMinBoundaries[i] > par[i] || par[i] > mMaxBoundaries[i]) {
286 return kFALSE;
287 }
288 }
289 return kTRUE;
290}
291
293inline void Chebyshev3D::Eval(const Float_t* par, Float_t* res)
294{
295 for (int i = 3; i--;) {
296 mTemporaryCoefficient[i] = mapToInternal(par[i], i);
297 }
298 for (int i = mOutputArrayDimension; i--;) {
299 res[i] = getChebyshevCalc(i)->Eval(mTemporaryCoefficient);
300 }
301}
302
304inline void Chebyshev3D::Eval(const Double_t* par, Double_t* res)
305{
306 for (int i = 3; i--;) {
307 mTemporaryCoefficient[i] = mapToInternal(par[i], i);
308 }
309 for (int i = mOutputArrayDimension; i--;) {
310 res[i] = getChebyshevCalc(i)->Eval(mTemporaryCoefficient);
311 }
312}
313
315inline Double_t Chebyshev3D::Eval(const Double_t* par, int idim)
316{
317 for (int i = 3; i--;) {
318 mTemporaryCoefficient[i] = mapToInternal(par[i], i);
319 }
320 return getChebyshevCalc(idim)->Eval(mTemporaryCoefficient);
321}
322
324inline Float_t Chebyshev3D::Eval(const Float_t* par, int idim)
325{
326 for (int i = 3; i--;) {
327 mTemporaryCoefficient[i] = mapToInternal(par[i], i);
328 }
329 return getChebyshevCalc(idim)->Eval(mTemporaryCoefficient);
330}
331
333inline void Chebyshev3D::evaluateDerivative3D(const Float_t* par, Float_t dbdr[3][3])
334{
335 for (int i = 3; i--;) {
336 mTemporaryCoefficient[i] = mapToInternal(par[i], i);
337 }
338 for (int ib = 3; ib--;) {
339 for (int id = 3; id--;) {
340 dbdr[ib][id] = getChebyshevCalc(ib)->evaluateDerivative(id, mTemporaryCoefficient) * mBoundaryMappingScale[id];
341 }
342 }
343}
344
346inline void Chebyshev3D::evaluateDerivative3D2(const Float_t* par, Float_t dbdrdr[3][3][3])
347{
348 for (int i = 3; i--;) {
349 mTemporaryCoefficient[i] = mapToInternal(par[i], i);
350 }
351 for (int ib = 3; ib--;) {
352 for (int id = 3; id--;) {
353 for (int id1 = 3; id1--;) {
354 dbdrdr[ib][id][id1] = getChebyshevCalc(ib)->evaluateDerivative2(id, id1, mTemporaryCoefficient) *
355 mBoundaryMappingScale[id] * mBoundaryMappingScale[id1];
356 }
357 }
358 }
359}
360
361// Evaluates Chebyshev parameterization derivative for 3d->DimOut function
362inline void Chebyshev3D::evaluateDerivative(int dimd, const Float_t* par, Float_t* res)
363{
364 for (int i = 3; i--;) {
365 mTemporaryCoefficient[i] = mapToInternal(par[i], i);
366 }
367 for (int i = mOutputArrayDimension; i--;) {
368 res[i] = getChebyshevCalc(i)->evaluateDerivative(dimd, mTemporaryCoefficient) * mBoundaryMappingScale[dimd];
369 };
370}
371
372// Evaluates Chebyshev parameterization 2nd derivative over dimd1 and dimd2 dimensions for 3d->DimOut function
373inline void Chebyshev3D::evaluateDerivative2(int dimd1, int dimd2, const Float_t* par, Float_t* res)
374{
375 for (int i = 3; i--;) {
376 mTemporaryCoefficient[i] = mapToInternal(par[i], i);
377 }
378 for (int i = mOutputArrayDimension; i--;) {
379 res[i] = getChebyshevCalc(i)->evaluateDerivative2(dimd1, dimd2, mTemporaryCoefficient) *
380 mBoundaryMappingScale[dimd1] * mBoundaryMappingScale[dimd2];
381 }
382}
383
386inline Float_t Chebyshev3D::evaluateDerivative(int dimd, const Float_t* par, int idim)
387{
388 for (int i = 3; i--;) {
389 mTemporaryCoefficient[i] = mapToInternal(par[i], i);
390 }
391 return getChebyshevCalc(idim)->evaluateDerivative(dimd, mTemporaryCoefficient) * mBoundaryMappingScale[dimd];
392}
393
396inline Float_t Chebyshev3D::evaluateDerivative2(int dimd1, int dimd2, const Float_t* par, int idim)
397{
398 for (int i = 3; i--;) {
399 mTemporaryCoefficient[i] = mapToInternal(par[i], i);
400 }
401 return getChebyshevCalc(idim)->evaluateDerivative2(dimd1, dimd2, mTemporaryCoefficient) *
402 mBoundaryMappingScale[dimd1] * mBoundaryMappingScale[dimd2];
403}
404
407{
408#ifdef _BRING_TO_BOUNDARY_
409 T res = (x - mBoundaryMappingOffset[d]) * mBoundaryMappingScale[d];
410 if (res < -1) {
411 return -1;
412 }
413 if (res > 1) {
414 return 1;
415 }
416 return res;
417#else
418 return (x - mBoundaryMappingOffset[d]) * mBoundaryMappingScale[d];
419#endif
420}
421
423inline Double_t Chebyshev3D::mapToInternal(Double_t x, Int_t d) const
424{
425#ifdef _BRING_TO_BOUNDARY_
426 T res = (x - mBoundaryMappingOffset[d]) * mBoundaryMappingScale[d];
427 if (res < -1) {
428 return -1;
429 }
430 if (res > 1) {
431 return 1;
432 }
433 return res;
434#else
435 return (x - mBoundaryMappingOffset[d]) * mBoundaryMappingScale[d];
436#endif
437}
438} // namespace math_utils
439} // namespace o2
440
441#endif
int32_t i
uint32_t res
Definition RawData.h:0
TBranch * ptr
Float_t Eval(const Float_t *par) const
Float_t evaluateDerivative(int dim, const Float_t *par) const
Float_t evaluateDerivative2(int dim1, int dim2, const Float_t *par) const
Float_t * getBoundMax() const
void evaluateDerivative3D(const Float_t *par, Float_t dbdr[3][3])
Returns the gradient matrix.
void prepareBoundaries(const Float_t *bmin, const Float_t *bmax)
Bool_t isInside(const Float_t *par) const
Checks if the point is inside of the fitted box.
void loadData(const char *inpFile)
Float_t * getBoundMin() const
void Clear(const Option_t *option="") override
Float_t mapToExternal(Float_t x, Int_t d) const
Float_t * getBoundaryMappingOffset() const
void evaluateDerivative(int dimd, const Float_t *par, Float_t *res)
Float_t getPrecision() const
Float_t mapToInternal(Float_t x, Int_t d) const
Μaps x to [-1:1].
Float_t * getBoundaryMappingScale() const
void evaluateDerivative2(int dimd1, int dimd2, const Float_t *par, Float_t *res)
Double_t mapToExternal(Double_t x, Int_t d) const
void evaluateDerivative3D2(const Float_t *par, Float_t dbdrdr[3][3][3])
Returns the gradient matrix.
void shiftBound(int id, float dif)
Float_t getBoundMin(int i) const
Chebyshev3DCalc * getChebyshevCalc(int i) const
void setDimOut(const int d, const float *prec=nullptr)
void Print(const Option_t *opt="") const override
void Eval(const Float_t *par, Float_t *res)
Evaluates Chebyshev parameterization for 3d->DimOut function.
Float_t getBoundMax(int i) const
Chebyshev3D & operator=(const Chebyshev3D &rhs)
GLint GLenum GLint x
Definition glcorearb.h:403
GLenum src
Definition glcorearb.h:1767
GLuint const GLchar * name
Definition glcorearb.h:781
GLuint GLuint stream
Definition glcorearb.h:1806
GLuint id
Definition glcorearb.h:650
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...