-
Notifications
You must be signed in to change notification settings - Fork 652
Expand file tree
/
Copy pathParamBase.h
More file actions
268 lines (214 loc) · 9.48 KB
/
ParamBase.h
File metadata and controls
268 lines (214 loc) · 9.48 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// 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 ParamBase.h
/// \author Nicolò Jacazio <nicolo.jacazio@cern.ch>
/// \since 07/08/2020
/// \brief Set of utilities to handle the parametrization of the PID response for each detector
/// These are the basic storage elements to be kept in the CCDB
///
#ifndef COMMON_CORE_PID_PARAMBASE_H_
#define COMMON_CORE_PID_PARAMBASE_H_
#include <algorithm> // std::copy
#include <map> // std::map
#include <string> // std::string
#include <vector> // std::vector
// ROOT includes
#include "TNamed.h"
#include "TFile.h"
#include "Framework/Logger.h"
namespace o2::pid
{
/// Variable to use for the pid input/output i.e. float, double et cetera
using pidvar_t = float;
/// \brief Class to handle the parameters of a given detector response
class Parameters : public TNamed
{
public:
/// Default constructor
Parameters() = default;
/// Parametric constructor
/// \param size Number of parameters in the container
explicit Parameters(unsigned int size) : mPar(std::vector<pidvar_t>(size)) {}
/// Parametric constructor
/// \param size Number of parameters in the container
Parameters(const TString name, unsigned int size) : TNamed(name, name), mPar(std::vector<pidvar_t>(size)) {}
/// Parametric constructor
/// \param params Parameters to initialize the container
Parameters(const TString name, const std::vector<pidvar_t> params) : TNamed(name, name), mPar{} { SetParameters(params); }
/// Default destructor
~Parameters() override = default;
/// Setter for the parameter at position iparam
/// \param iparam index in the array of the parameters
/// \param value value of the parameter at position iparam
void SetParameter(const unsigned int iparam, const pidvar_t value) { mPar[iparam] = value; }
/// Setter for the parameter, using an array
/// \param param array with parameters
void SetParameters(const pidvar_t* params) { std::copy(params, params + mPar.size(), mPar.begin()); }
/// Setter for the parameter, using a vector
/// \param params vector with parameters
void SetParameters(const std::vector<pidvar_t> params);
/// Setter for the parameter, using a parameter object
/// \param params parameter object with parameters
void SetParameters(const Parameters params) { SetParameters(params.mPar); }
/// Setter for the parameter, using a parameter pointer
/// \param params pointer to parameter object with parameters
void SetParameters(const Parameters* params) { SetParameters(params->mPar); }
/// Printer of the parameter values
void Print(Option_t* option = "") const override;
/// Loader from file
/// \param FileName name of the input file
/// \param ParamName name of the input object
void LoadParamFromFile(const TString FileName, const TString ParamName);
/// Getter for the parameters
/// \return returns an array of parameters
const pidvar_t* GetParameters() const { return mPar.data(); }
/// Getter for the size of the parameter
/// \return returns the size of the parameter array
unsigned int size() const { return mPar.size(); }
/// Getter of the parameter at position i
/// \param i index of the parameter to get
/// \return returns the parameter value at position i
pidvar_t operator[](const unsigned int i) const { return mPar[i]; }
private:
/// Vector of the parameter
std::vector<pidvar_t> mPar;
ClassDefOverride(Parameters, 1); // Container for parameter of parametrizations
};
/// \brief Class to handle the parameters of a given detector response
template <int nPar>
class PidParameters : public TNamed
{
public:
/// Default constructor
explicit PidParameters(TString name = "DefaultParameters") : TNamed(name, name), mPar{} {};
/// Default destructor
~PidParameters() override = default;
/// Setter for the parameter at position iparam
/// \param iparam index in the array of the parameters
/// \param value value of the parameter at position iparam
void SetParameter(const unsigned int iparam, const pidvar_t value) { mPar[iparam] = value; }
/// Setter for the parameter, using an array
/// \param param array with parameters
void SetParameters(const pidvar_t* params) { std::copy(params, params + mPar.size(), mPar.begin()); }
/// Setter for the parameter, using a vector
/// \param params vector with parameters
void SetParameters(const std::array<pidvar_t, nPar> params)
{
for (int i = 0; i < nPar; i++) {
mPar[i] = params[i];
}
}
/// Setter for the parameter, using a parameter object
/// \param params parameter object with parameters
void SetParameters(const PidParameters<nPar> params) { SetParameters(params.mPar); }
/// Setter for the parameter, using a parameter pointer
/// \param params pointer to parameter object with parameters
void SetParameters(const PidParameters<nPar>* params) { SetParameters(params->mPar); }
/// Printer of the parameter values
void Print(Option_t* /*option = ""*/) const override
{
LOG(info) << "PidParameters '" << fName << "'";
for (int i = 0; i < nPar; i++) {
LOG(info) << "Parameter " << i << "/" << nPar - 1 << " is " << mPar[i];
}
}
/// Adds the parameters to the metadata
void AddToMetadata(std::map<std::string, std::string>& metadata) const
{
for (int i = 0; i < nPar; i++) {
metadata[Form("p%i", i)] = Form("%f", mPar[i]);
}
}
/// Loader from file
/// \param FileName name of the input file
/// \param ParamName name of the input object
void LoadParamFromFile(const TString FileName, const TString ParamName)
{
TFile f(FileName, "READ");
if (!f.Get(ParamName)) {
LOG(fatal) << "Did not find parameters " << ParamName << " in file " << FileName;
}
LOG(info) << "Loading parameters " << ParamName << " from TFile " << FileName;
PidParameters<nPar>* p;
f.GetObject(ParamName, p);
if (!p) {
LOG(fatal) << "Could not get parameters " << ParamName << " from file";
f.ls();
}
f.Close();
SetParameters(p);
Print();
}
/// Getter for the parameters
/// \return returns an array of parameters
const pidvar_t* GetParameters() const { return mPar.to_array(); }
/// Getter for the parameters
/// \return returns an array of parameters
pidvar_t GetParameter(int i) const { return mPar[i]; }
/// Getter for the size of the parameter
/// \return returns the size of the parameter array
static int size() { return nPar; }
/// Getter of the parameter at position i
/// \param i index of the parameter to get
/// \return returns the parameter value at position i
pidvar_t operator[](const unsigned int i) const { return mPar[i]; }
private:
/// Array of the parameter
std::array<pidvar_t, nPar> mPar;
ClassDefOverride(PidParameters, 1); // Container for parameter of parametrizations
};
class Parametrization : public TNamed
{
public:
/// Default constructor
Parametrization() : TNamed("DefaultParametrization", "DefaultParametrization"), mParameters{0} {};
/// Parametric constructor
/// \param name Name (and title) of the parametrization
/// \param size Number of parameters of the parametrization
Parametrization(TString name, unsigned int size) : TNamed(name, name), mParameters(name + "Parameters", size) {}
/// Parametric constructor
/// \param name Name (and title) of the parametrization
/// \param params Parameters of the parametrization
Parametrization(TString name, const std::vector<pidvar_t> params) : TNamed(name, name), mParameters{name + "Parameters", params} {}
/// Default destructor
~Parametrization() override = default;
/// Getter for parametrization values, to be reimplemented in the custom parametrization of the user
/// \param x array of variables to use in order to compute the return value
virtual pidvar_t operator()(const pidvar_t* x) const;
/// Printer for the parametrization
void Print(Option_t* option = "") const override;
/// Loader from file
/// \param FileName name of the input file
/// \param ParamName name of the input object
void LoadParamFromFile(const TString FileName, const TString ParamName);
/// Setter for the parameter at position iparam
/// \param iparam index in the array of the parameters
/// \param value value of the parameter at position iparam
void SetParameter(const unsigned int iparam, const pidvar_t value) { mParameters.SetParameter(iparam, value); }
/// Setter for the parameter, using a vector, a parameter object or a pointer to a parameter object
/// \param params vector, parameter object, pointer to parameter object with parameters
template <typename T>
void SetParameters(const T params)
{
mParameters.SetParameters(params);
}
/// Getter for the parameters
Parameters GetParameters() const { return mParameters; }
/// Getter for the parameters
void GetParameters(Parameters*& parameters) { parameters = &mParameters; }
protected:
/// Parameters of the parametrization
Parameters mParameters;
ClassDefOverride(Parametrization, 1); // Container for the parametrization of the response function
};
} // namespace o2::pid
#endif // COMMON_CORE_PID_PARAMBASE_H_