-
Notifications
You must be signed in to change notification settings - Fork 494
Expand file tree
/
Copy pathSplineHelper.h
More file actions
165 lines (132 loc) · 5.87 KB
/
SplineHelper.h
File metadata and controls
165 lines (132 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// \file SplineHelper.h
/// \brief Definition of SplineHelper class
///
/// \author Sergey Gorbunov <sergey.gorbunov@cern.ch>
#ifndef ALICEO2_GPUCOMMON_TPCFASTTRANSFORMATION_SPLINEHELPER_H
#define ALICEO2_GPUCOMMON_TPCFASTTRANSFORMATION_SPLINEHELPER_H
#include <cmath>
#include <vector>
#include "GPUCommonDef.h"
#include "Rtypes.h"
#include "TString.h"
#include "Spline1D.h"
#include "Spline.h"
#include "Spline1DHelper.h"
#include <functional>
namespace GPUCA_NAMESPACE
{
namespace gpu
{
///
/// The SplineHelper class is to initialize Spline* objects
///
template <typename DataT>
class SplineHelper
{
public:
/// _____________ Constructors / destructors __________________________
/// Default constructor
SplineHelper();
/// Copy constructor: disabled
SplineHelper(const SplineHelper&) CON_DELETE;
/// Assignment operator: disabled
SplineHelper& operator=(const SplineHelper&) CON_DELETE;
/// Destructor
~SplineHelper() CON_DEFAULT;
/// _______________ Main functionality ________________________
/// Create best-fit spline parameters for a given input function F
void approximateFunction(SplineContainer<DataT>& spline,
const double xMin[/* Xdim */], const double xMax[/* Xdim */],
std::function<void(const double x[/* Xdim */], double f[/* Fdim */])> F,
const int nAxiliaryDataPoints[/* Xdim */] = nullptr);
/// _______________ Interface for a step-wise construction of the best-fit spline ________________________
/// precompute everything needed for the construction
int setSpline(const SplineContainer<DataT>& spline, const int nAxiliaryPoints[/* Xdim */]);
/// approximate std::function, output in Fparameters
void approximateFunction(
DataT* Fparameters, const double xMin[/* mXdimensions */], const double xMax[/* mXdimensions */],
std::function<void(const double x[/* mXdimensions */], double f[/* mFdimensions */])> F) const;
/// approximate std::function, output in Fparameters. F calculates values for a batch of points.
void approximateFunctionBatch(
DataT* Fparameters, const double xMin[/* mXdimensions */], const double xMax[/* mXdimensions */],
std::function<void(const std::vector<double> x[/* mXdimensions */], double f[/*mFdimensions*/])> F,
unsigned int batchsize) const;
/// approximate a function given as an array of values at data points
void approximateFunction(
DataT* Fparameters, const double DataPointF[/*getNumberOfDataPoints() x nFdim*/]) const;
int getNumberOfDataPoints(int dimX) const { return mHelpers[dimX].getNumberOfDataPoints(); }
int getNumberOfDataPoints() const { return mNumberOfDataPoints; }
const Spline1DHelper<DataT>& getHelper(int dimX) const { return mHelpers[dimX]; }
/// _______________ Utilities ________________________
/// Gives error string
const char* getLastError() const { return mError.Data(); }
#if !defined(GPUCA_GPUCODE) && !defined(GPUCA_STANDALONE)
/// Test the Spline class functionality
static int test(const bool draw = 0, const bool drawDataPoints = 1);
#endif
static int arraytopoints(int point, int result[], const int numbers[], int dim);
static int pointstoarray(const int indices[], const int numbers[], int dim);
private:
/// Stores an error message
int storeError(Int_t code, const char* msg);
TString mError = ""; ///< error string
int mXdimensions; ///< number of X dimensions
int mFdimensions; ///< number of F dimensions
int mNumberOfParameters; ///< number of parameters
int mNumberOfDataPoints; ///< number of data points
std::vector<Spline1DHelper<DataT>> mHelpers;
};
template <typename DataT>
void SplineHelper<DataT>::approximateFunction(
SplineContainer<DataT>& spline,
const double xMin[/* Xdim */], const double xMax[/* Xdim */],
std::function<void(const double x[/* Xdim */], double f[/* Fdim */])> F,
const int nAxiliaryDataPoints[/* Xdim */])
{
/// Create best-fit spline parameters for a given input function F
setSpline(spline, nAxiliaryDataPoints);
approximateFunction(spline.getParameters(), xMin, xMax, F);
DataT xxMin[spline.getXdimensions()];
DataT xxMax[spline.getXdimensions()];
for (int i = 0; i < spline.getXdimensions(); i++) {
xxMin[i] = xMin[i];
xxMax[i] = xMax[i];
}
spline.setXrange(xxMin, xxMax);
}
template <typename DataT>
int SplineHelper<DataT>::setSpline(
const SplineContainer<DataT>& spline, const int nAxiliaryPoints[/* Xdim */])
{
// Prepare creation of an irregular spline
// The should be at least one (better, two) axiliary measurements on each segnment between two knots and at least 2*nKnots measurements in total
// Returns 0 when the spline can not be constructed with the given nAxiliaryPoints
int ret = 0;
mXdimensions = spline.getXdimensions();
mFdimensions = spline.getYdimensions();
mNumberOfParameters = spline.getNumberOfParameters();
mNumberOfDataPoints = 1;
mHelpers.clear();
mHelpers.resize(mXdimensions);
for (int i = 0; i < mXdimensions; i++) {
int np = (nAxiliaryPoints != nullptr) ? nAxiliaryPoints[i] : 4;
if (mHelpers[i].setSpline(spline.getGrid(i), mFdimensions, np) != 0) {
ret = storeError(-2, "SplineHelper::setSpline: error by setting an axis");
}
mNumberOfDataPoints *= mHelpers[i].getNumberOfDataPoints();
}
return ret;
}
} // namespace gpu
} // namespace GPUCA_NAMESPACE
#endif