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 ~Parameters() = default;
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
189 int getSize(const std::string& pass) const;
190
196 template <typename ParType>
197 bool storeParameters(const ParType& p, const std::string& key)
198 {
199 const bool alreadyPresent = hasKey(key);
200 if (alreadyPresent) {
201 LOG(debug) << "Changing parametrization corresponding to key " << key << " from size " << mParameters[key].size() << " to " << p.getName() << " of size " << p.size();
202 } else {
203 mParameters[key] = std::unordered_map<std::string, paramvar_t>{};
204 LOG(debug) << "Adding new parametrization corresponding to key " << key << ": " << p.getName() << " of size " << p.size();
205 }
206 for (int i = 0; i < p.size(); i++) {
207 mParameters[key][p.getParameterName(i)] = p[i];
208 }
209 return alreadyPresent;
210 }
211
213 const auto& getPars(const std::string& pass) const { return mParameters.at(pass); }
214
217 void print(const std::string& pass) const;
218
220 void print() const;
221
224 const auto& getFullMap() { return mParameters; }
225
229 void loadParamFromFile(const TString FileName, const TString ParamName)
230 {
231 TFile f(FileName, "READ");
232 if (!f.IsOpen()) {
233 LOG(fatal) << "Could not open file " << FileName;
234 }
235 if (!f.Get(ParamName)) {
236 f.ls();
237 LOG(fatal) << "Did not find parameters " << ParamName << " in file " << FileName;
238 }
239 LOG(info) << "Loading parameters " << ParamName << " from TFile " << FileName;
241 f.GetObject(ParamName, p);
242 if (!p) {
243 LOG(fatal) << "Could not get parameters " << ParamName << " from file";
244 f.ls();
245 }
246 f.Close();
247
248 for (const auto& pass : p->mParameters) {
249 for (const auto& par : pass.second) {
250 addParameter(pass.first, par.first, par.second);
251 }
252 }
253 print();
254 }
255
256 private:
258 std::unordered_map<std::string, std::unordered_map<std::string, paramvar_t>> mParameters;
259
260 ClassDefOverride(ParameterCollection, 1); // Container for containers of parameter of parametrizations. To be used as a manager, in help of CCDB
261};
262
263} // namespace tof
264} // namespace o2
265
266#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,...
ParameterCollection(TString name="DefaultParameters")
Default constructor.
int getSize(const std::string &pass) const
~ParameterCollection() override=default
Default destructor.
void loadParamFromFile(const TString FileName, const TString ParamName)
void print() const
printing function for the full content of the container
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...
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.
void setParameter(const unsigned int iparam, const paramvar_t value)
void loadParamFromFile(const TString FileName, const TString ParamName)
~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.
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"