Project
Loading...
Searching...
No Matches
ParameterContainers.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
17
18#ifndef O2_TOF_PARAMCONTAINER_H
19#define O2_TOF_PARAMCONTAINER_H
20
21#include "TNamed.h"
22#include "TFile.h"
23#include "Framework/Logger.h"
24#include "map"
25
26namespace o2
27{
28namespace tof
29{
30using paramvar_t = float;
31
32template <int nPar>
34{
35 public:
37 Parameters(std::array<std::string, nPar> parNames, std::string name) : mName{name}, mPar{}, mParNames{parNames} {};
38
40 virtual ~Parameters() = default; // Ensure proper cleanup in derived classes
41
45 void setParameter(const unsigned int iparam, const paramvar_t value) { mPar[iparam] = value; }
46
49 void setParameters(const paramvar_t* params) { std::copy(params, params + mPar.size(), mPar.begin()); }
50
53 void setParameters(const std::array<paramvar_t, nPar> params)
54 {
55 for (int i = 0; i < nPar; i++) {
56 mPar[i] = params[i];
57 }
58 }
59
63
67
69 void print() const
70 {
71 LOG(info) << "Parameters '" << mName << "'";
72 for (int i = 0; i < nPar; i++) {
73 LOG(info) << "Parameter " << i << "/" << nPar - 1 << " " << mParNames[i] << " is " << mPar[i];
74 }
75 }
76
78 void addToMetadata(std::map<std::string, std::string>& metadata) const
79 {
80 for (int i = 0; i < nPar; i++) {
81 metadata[Form("p%i", i)] = Form("%f", mPar[i]);
82 }
83 }
84
88 void loadParamFromFile(const TString FileName, const TString ParamName)
89 {
90 TFile f(FileName, "READ");
91 if (!f.IsOpen()) {
92 LOG(fatal) << "Could not open file " << FileName;
93 }
94 if (!f.Get(ParamName)) {
95 f.ls();
96 LOG(fatal) << "Did not find parameters " << ParamName << " in file " << FileName;
97 }
98 LOG(info) << "Loading parameters " << ParamName << " from TFile " << FileName;
100 f.GetObject(ParamName, p);
101 if (!p) {
102 LOG(fatal) << "Could not get parameters " << ParamName << " from file";
103 f.ls();
104 }
105 f.Close();
106 setParameters(p);
107 print();
108 }
109
112 const paramvar_t* getParameters() const { return mPar.to_array(); }
113
116 const paramvar_t getParameter(int i) const { return mPar[i]; }
117
120 const std::string getParameterName(int i) const { return mParNames[i]; }
121
124 const std::string getName() const { return mName; }
125
128 static int size() { return nPar; }
129
133 paramvar_t operator[](const unsigned int i) const { return mPar[i]; }
134
135 private:
137 std::array<paramvar_t, nPar> mPar;
138 const std::array<std::string, nPar> mParNames;
139 std::string mName;
140};
141
143class ParameterCollection : public TNamed
144{
145 public:
147 ParameterCollection(TString name = "DefaultParameters") : TNamed(name, name), mParameters{} {};
148
150 ~ParameterCollection() override = default;
151
154 bool hasKey(const std::string& key) const { return (mParameters.find(key) != mParameters.end()); }
155
162 template <typename ParType>
163 bool retrieveParameters(ParType& p, const std::string& key) const
164 {
165 if (!hasKey(key)) { // Can't find the required key. Can't load parameters to the object
166 return false;
167 }
168
169 const auto& toGet = mParameters.at(key);
170 for (int i = 0; i < p.size(); i++) {
171 const auto& name = p.getParameterName(i);
172 if (toGet.find(name) == toGet.end()) {
173 LOG(debug) << "Did not find parameter '" << name << "' in collection, keeping preexisting";
174 continue;
175 }
176 LOG(debug) << "Found parameter '" << name << "' in collection, updating from " << p[i] << " to " << toGet.at(name);
177 p.setParameter(i, toGet.at(name));
178 }
179 return true;
180 }
181
186 bool addParameter(const std::string& pass, const std::string& parName, float value)
187 {
188 const bool alreadyPresent = hasKey(pass);
189 if (alreadyPresent) {
190 LOG(debug) << "Changing parametrization corresponding to key " << pass << " from size " << mParameters[pass].size() << " to " << parName;
191 } else {
192 mParameters[pass] = std::unordered_map<std::string, paramvar_t>{};
193 LOG(debug) << "Adding new parametrization corresponding to key " << pass << ": " << parName;
194 }
195 mParameters[pass][parName] = value;
196 return true;
197 }
198
200 int getSize(const std::string& pass) const
201 {
202 if (!hasKey(pass)) {
203 return -1;
204 }
205 return mParameters.at(pass).size();
206 }
207
213 template <typename ParType>
214 bool storeParameters(const ParType& p, const std::string& key)
215 {
216 const bool alreadyPresent = hasKey(key);
217 if (alreadyPresent) {
218 LOG(debug) << "Changing parametrization corresponding to key " << key << " from size " << mParameters[key].size() << " to " << p.getName() << " of size " << p.size();
219 } else {
220 mParameters[key] = std::unordered_map<std::string, paramvar_t>{};
221 LOG(debug) << "Adding new parametrization corresponding to key " << key << ": " << p.getName() << " of size " << p.size();
222 }
223 for (int i = 0; i < p.size(); i++) {
224 mParameters[key][p.getParameterName(i)] = p[i];
225 }
226 return alreadyPresent;
227 }
228
230 const auto& getPars(const std::string& pass) const { return mParameters.at(pass); }
231
234 void print(const std::string& pass) const
235 {
236 const auto& size = getSize(pass);
237 if (size < 0) {
238 LOG(info) << "empty pass: " << pass;
239 return;
240 }
241 LOG(info) << "Pass \"" << pass << "\" with size " << size;
242 for (const auto& [par, value] : mParameters.at(pass)) {
243 LOG(info) << "par name = " << par << ", value = " << value;
244 }
245 }
246
248 void print() const
249 {
250 for (const auto& [pass, pars] : mParameters) {
251 print(pass);
252 }
253 }
254
257 const auto& getFullMap() { return mParameters; }
258
262 void loadParamFromFile(const TString FileName, const TString ParamName)
263 {
264 TFile f(FileName, "READ");
265 if (!f.IsOpen()) {
266 LOG(fatal) << "Could not open file " << FileName;
267 }
268 if (!f.Get(ParamName)) {
269 f.ls();
270 LOG(fatal) << "Did not find parameters " << ParamName << " in file " << FileName;
271 }
272 LOG(info) << "Loading parameters " << ParamName << " from TFile " << FileName;
274 f.GetObject(ParamName, p);
275 if (!p) {
276 LOG(fatal) << "Could not get parameters " << ParamName << " from file";
277 f.ls();
278 }
279 f.Close();
280
281 for (const auto& pass : p->mParameters) {
282 for (const auto& par : pass.second) {
283 addParameter(pass.first, par.first, par.second);
284 }
285 }
286 print();
287 }
288
289 private:
291 std::unordered_map<std::string, std::unordered_map<std::string, paramvar_t>> mParameters;
292
293 ClassDefOverride(ParameterCollection, 1); // Container for containers of parameter of parametrizations. To be used as a manager, in help of CCDB
294};
295
296} // namespace tof
297} // namespace o2
298
299#endif // O2_TOF_PARAMCONTAINER_H
int32_t i
std::ostringstream debug
StringRef key
Class container to hold different parameters meant to be stored on the CCDB.
const auto & getPars(const std::string &pass) const
getter for the parameters stored in the container matching to a pass
const auto & getFullMap()
Getter of the full map of parameters stored in the container.
bool hasKey(const std::string &key) const
Checks if the container has a particular key e.g. a pass.
bool retrieveParameters(ParType &p, const std::string &key) const
Function to load the parameters from the this container into the array based for the asked key,...
void print() const
printing function for the full content of the container
ParameterCollection(TString name="DefaultParameters")
Default constructor.
void print(const std::string &pass) const
printing function for the content of the pass
bool addParameter(const std::string &pass, const std::string &parName, float value)
Function to add a single parameter conatiner based on the asked key, e.g. pass or version.
int getSize(const std::string &pass) const
~ParameterCollection() override=default
Default destructor.
void loadParamFromFile(const TString FileName, const TString ParamName)
bool storeParameters(const ParType &p, const std::string &key)
Function to push the parameters from the sub container into the collection and store it under a given...
void setParameter(const unsigned int iparam, const paramvar_t value)
void loadParamFromFile(const TString FileName, const TString ParamName)
virtual ~Parameters()=default
Default destructor.
const std::string getName() const
const std::string getParameterName(int i) const
void setParameters(const Parameters< nPar > *params)
void setParameters(const Parameters< nPar > params)
void setParameters(const paramvar_t *params)
void addToMetadata(std::map< std::string, std::string > &metadata) const
Adds the parameters to the metadata.
const paramvar_t getParameter(int i) const
const paramvar_t * getParameters() const
void setParameters(const std::array< paramvar_t, nPar > params)
paramvar_t operator[](const unsigned int i) const
void print() const
Printer of the parameter values.
Parameters(std::array< std::string, nPar > parNames, std::string name)
Default constructor.
GLsizeiptr size
Definition glcorearb.h:659
GLuint const GLchar * name
Definition glcorearb.h:781
GLdouble f
Definition glcorearb.h:310
GLsizei const GLfloat * value
Definition glcorearb.h:819
GLenum const GLfloat * params
Definition glcorearb.h:272
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"