Project
Loading...
Searching...
No Matches
dcs-ccdb.cxx
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
12#include "CCDB/CcdbApi.h"
17#if defined(MUON_SUBSYSTEM_MCH)
19#elif defined(MUON_SUBSYSTEM_MID)
21#endif
22#include <boost/program_options.hpp>
23#include <ctime>
24#include <iostream>
25#include <map>
26#include <numeric>
27#include <regex>
28#include <string>
29#include <unordered_map>
30#include <vector>
31#include "subsysname.h"
32#include <TFile.h>
33
34namespace po = boost::program_options;
37using DPMAP = std::unordered_map<DPID, std::vector<DPVAL>>;
38
40{
41 union Converter {
42 uint64_t raw_data;
43 double value;
44 } converter;
45 converter.raw_data = v.payload_pt1;
46 return s + converter.value;
47};
48
49std::string CcdbDpConfName()
50{
51 return fmt::format("{}/Config/DCSDPconfig", o2::muon::subsysname());
52}
53
55
56/*
57 * Return the data point values with min and max timestamps
58 */
59std::pair<DPVAL, DPVAL> computeTimeRange(const std::vector<DPVAL>& dps)
60{
61 DPVAL dmin, dmax;
62 uint64_t minTime{std::numeric_limits<uint64_t>::max()};
63 uint64_t maxTime{0};
64
65 for (auto d : dps) {
66 const auto ts = d.get_epoch_time();
67 if (ts < minTime) {
68 dmin = d;
69 minTime = ts;
70 }
71 if (ts > maxTime) {
72 dmax = d;
73 maxTime = ts;
74 }
75 }
76 return std::make_pair(dmin, dmax);
77}
78
79void dump(const std::string what, DPMAP m, int verbose)
80{
81 std::cout << "size of " << what << " map = " << m.size() << std::endl;
82 if (verbose > 0) {
83 for (auto& i : m) {
84 auto v = i.second;
85 auto timeRange = computeTimeRange(v);
86 auto mean = std::accumulate(v.begin(), v.end(), 0.0, sum);
87 if (v.size()) {
88 mean /= v.size();
89 }
90 auto vv = v;
91 auto last = std::unique(vv.begin(), vv.end());
92 vv.erase(last, vv.end());
93
94 std::cout << fmt::format("{:64s} {:4d} ({:4d} unique) values of mean {:7.2f} : ", i.first.get_alias(), v.size(), vv.size(), mean);
95 if (verbose > 1) {
96 std::cout << "\n";
97 for (auto dp : vv) {
98 std::cout << fmt::format(" {:7.2f} ", sum(0., dp)) << dp << "\n";
99 }
100 }
101 std::cout << "timeRange=" << timeRange.first << " " << timeRange.second << "\n";
102 }
103 }
104}
105
106void doQueryHVLV(const std::string fileName)
107{
108 std::cout << "Reading from file " << fileName << "\n";
109 std::unique_ptr<TFile> fin(TFile::Open(fileName.c_str()));
110 if (fin->IsZombie()) {
111 return;
112 }
113 TClass* cl = TClass::GetClass(typeid(DPMAP));
114
115 DPMAP* m = static_cast<DPMAP*>(fin->GetObjectChecked("ccdb_object", cl));
116 if (!m) {
117 std::cerr << "Could not read ccdb_object from file " << fileName << "\n";
118 return;
119 }
120 dump(fileName, *m, verboseLevel);
121}
122
123void doQueryHVLV(const std::string ccdbUrl, uint64_t timestamp, bool hv, bool lv)
124{
125 std::vector<std::string> what;
126 if (hv) {
127 what.emplace_back(fmt::format("{}/Calib/HV", o2::muon::subsysname()));
128 }
129 if (lv) {
130 what.emplace_back(fmt::format("{}/Calib/LV", o2::muon::subsysname()));
131 }
132
134 api.init(ccdbUrl);
135 for (auto w : what) {
136 std::map<std::string, std::string> metadata;
137 auto* m = api.retrieveFromTFileAny<DPMAP>(w, metadata, timestamp);
138 dump(w, *m, verboseLevel);
139 }
140}
141
142void doQueryDataPointConfig(const std::string ccdbUrl, uint64_t timestamp,
143 const std::string dpConfName)
144{
146 api.init(ccdbUrl);
147 using DPCONF = std::unordered_map<DPID, std::string>;
148 std::map<std::string, std::string> metadata;
149 auto* m = api.retrieveFromTFileAny<DPCONF>(dpConfName.c_str(), metadata, timestamp);
150 std::cout << "size of dpconf map = " << m->size() << std::endl;
151 if (verboseLevel > 0) {
152 for (auto& i : *m) {
153 std::cout << i.second << " " << i.first << "\n";
154 }
155 }
156}
157
158void makeCCDBEntryForDCS(const std::string ccdbUrl, uint64_t timestamp)
159{
160 std::unordered_map<DPID, std::string> dpid2DataDesc;
161#if defined(MUON_SUBSYSTEM_MCH)
162 auto aliases = o2::mch::dcs::aliases();
163#elif defined(MUON_SUBSYSTEM_MID)
164 auto aliases = o2::mid::dcs::aliases();
165#endif
166
167 DPID dpidtmp;
168 for (const auto& a : aliases) {
169 DPID::FILL(dpidtmp, a, o2::dcs::DeliveryType::DPVAL_DOUBLE);
170 dpid2DataDesc[dpidtmp] = fmt::format("{}DATAPOINTS", o2::muon ::subsysname());
171 }
172
174 api.init(ccdbUrl);
175 std::map<std::string, std::string> md;
176 std::cout << "storing config of " << dpid2DataDesc.size() << " "
177 << o2::muon::subsysname() << " data points to "
178 << CcdbDpConfName() << "\n";
179
181}
182
183void makeDefaultCCDBEntry(const std::string ccdbUrl, uint64_t timestamp)
184{
185 // Notice that the timestamp is in ms
186 uint64_t timestamp_seconds = timestamp / 1000;
187 uint64_t timestamp_ms = timestamp % 1000;
188 DPMAP dpMap;
189 std::string ccdb = fmt::format("{}/Calib/HV", o2::muon::subsysname());
190#if defined(MUON_SUBSYSTEM_MCH)
191 std::array<o2::mch::dcs::MeasurementType, 2> types{o2::mch::dcs::MeasurementType::HV_V, o2::mch::dcs::MeasurementType::HV_I};
192 std::array<double, 2> defaultValues{1650., 0.1};
193 for (size_t itype = 0; itype < 2; ++itype) {
194 auto aliases = o2::mch::dcs::aliases({types[itype]});
195 for (const auto& alias : aliases) {
196 auto obj = o2::dcs::createDataPointCompositeObject(alias, defaultValues[itype], timestamp_seconds, timestamp_ms);
197 dpMap[obj.id].emplace_back(obj.data);
198 }
199 }
200#elif defined(MUON_SUBSYSTEM_MID)
201 std::array<o2::mid::dcs::MeasurementType, 2> types{o2::mid::dcs::MeasurementType::HV_V, o2::mid::dcs::MeasurementType::HV_I};
202 std::array<double, 2> defaultValues{9600., 5};
203 for (size_t itype = 0; itype < 2; ++itype) {
204 std::vector<o2::mid::dcs::MeasurementType> typeVec{types[itype]};
205 auto aliases = o2::mid::dcs::aliases(typeVec);
206 for (auto& alias : aliases) {
207 auto obj = o2::dcs::createDataPointCompositeObject(alias, defaultValues[itype], 1, 0);
208 dpMap[obj.id].emplace_back(obj.data);
209 obj = o2::dcs::createDataPointCompositeObject(alias, defaultValues[itype], timestamp_seconds, timestamp_ms);
210 dpMap[obj.id].emplace_back(obj.data);
211 }
212 }
213#endif
214
216 api.init(ccdbUrl);
217 std::map<std::string, std::string> md;
218 md["default"] = "true";
219 std::cout << "storing default values of " << o2::muon::subsysname() << " data points to " << ccdb << "\n";
220
221#if defined(MUON_SUBSYSTEM_MCH)
222 md["Created"] = "1";
223 api.storeAsTFileAny(&dpMap, ccdb, md, 1, 9999999999999);
224#elif defined(MUON_SUBSYSTEM_MID)
225 api.storeAsTFileAny(&dpMap, ccdb, md, 1, timestamp);
226#endif
227}
228
229bool match(const std::vector<std::string>& queries, const char* pattern)
230{
231 return std::find_if(queries.begin(), queries.end(), [pattern](std::string s) { return std::regex_match(s, std::regex(pattern, std::regex::extended | std::regex::icase)); }) != queries.end();
232}
233
234int main(int argc, char** argv)
235{
236 po::variables_map vm;
237 po::options_description usage("Usage");
238
239 std::string ccdbUrl;
240 std::string dpConfName;
241 uint64_t timestamp;
242 bool lv;
243 bool hv;
244 bool dpconf;
245 bool put;
246 bool upload_default;
247 std::string fileName;
248
249 uint64_t now = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
250
251 // clang-format off
252 usage.add_options()
253 ("help,h", "produce help message")
254 ("ccdb,c",po::value<std::string>(&ccdbUrl)->default_value("http://localhost:6464"),"ccdb url")
255 ("query,q",po::value<std::vector<std::string>>(),"what to query (if anything)")
256 ("timestamp,t",po::value<uint64_t>(&timestamp)->default_value(now),"timestamp for query or put (in ms)")
257 ("put-datapoint-config,p",po::bool_switch(&put),"upload datapoint configuration")
258 ("verbose,v",po::value<int>(&verboseLevel)->default_value(0),"verbose level")
259 ("datapoint-conf-name",po::value<std::string>(&dpConfName)->default_value(CcdbDpConfName()),"dp conf name (only if not from mch or mid)")
260 ("file,f",po::value<std::string>(&fileName)->default_value(""),"read from file instead of from ccdb")
261 ("upload-default-values,u",po::bool_switch(&upload_default),"upload default values")
262 ;
263 // clang-format on
264
265 po::options_description cmdline;
266 cmdline.add(usage);
267
268 po::store(po::command_line_parser(argc, argv).options(cmdline).run(), vm);
269
270 if (vm.count("help")) {
271 std::cout << "This program printout summary information from "
272 << o2::muon::subsysname() << " DCS entries.\n";
273 std::cout << usage << "\n";
274 return 2;
275 }
276
277 try {
278 po::notify(vm);
279 } catch (boost::program_options::error& e) {
280 std::cout << "Error: " << e.what() << "\n";
281 exit(1);
282 }
283
284 if (fileName.size() > 0) {
285 doQueryHVLV(fileName);
286 }
287
288 if (vm.count("query")) {
289 auto query = vm["query"].as<std::vector<std::string>>();
290
291 hv = match(query, ".*(hv)");
292#if defined(MUON_SUBSYSTEM_MCH)
293 lv = match(query, ".*(lv)");
294#else
295 lv = false;
296#endif
297 dpconf = match(query, ".*(dpconf)");
298
299 if (!hv && !lv && !dpconf) {
300 std::cout << "Must specify at least one of dpconf,hv";
301#if defined(MUON_SUBSYSTEM_MCH)
302 std::cout << ",lv";
303#endif
304 std::cout << " parameter to --query option\n";
305 std::cout
306 << usage << "\n";
307 return 3;
308 }
309
310 if (hv || lv) {
311 doQueryHVLV(ccdbUrl, timestamp, hv, lv);
312 }
313
314 if (dpconf) {
315 doQueryDataPointConfig(ccdbUrl, timestamp, dpConfName);
316 }
317 }
318
319 if (put) {
320 makeCCDBEntryForDCS(ccdbUrl, timestamp);
321 }
322 if (upload_default) {
323 makeDefaultCCDBEntry(ccdbUrl, timestamp);
324 }
325 return 0;
326}
int32_t i
int storeAsTFileAny(const T *obj, std::string const &path, std::map< std::string, std::string > const &metadata, long startValidityTimestamp=-1, long endValidityTimestamp=-1, std::vector< char >::size_type maxSize=0) const
Definition CcdbApi.h:157
void init(std::string const &hosts)
Definition CcdbApi.cxx:165
std::enable_if<!std::is_base_of< o2::conf::ConfigurableParam, T >::value, T * >::type retrieveFromTFileAny(std::string const &path, std::map< std::string, std::string > const &metadata, long timestamp=-1, std::map< std::string, std::string > *headers=nullptr, std::string const &etag="", const std::string &createdNotAfter="", const std::string &createdNotBefore="") const
Definition CcdbApi.h:660
static constexpr long INFINITE_TIMESTAMP
static void FILL(const DataPointIdentifier &dpid, const std::string &alias, const DeliveryType type) noexcept
void doQueryHVLV(const std::string fileName)
Definition dcs-ccdb.cxx:106
std::unordered_map< DPID, std::vector< DPVAL > > DPMAP
Definition dcs-ccdb.cxx:37
int verboseLevel
Definition dcs-ccdb.cxx:54
float sum(float s, o2::dcs::DataPointValue v)
Definition dcs-ccdb.cxx:39
bool match(const std::vector< std::string > &queries, const char *pattern)
Definition dcs-ccdb.cxx:229
void makeCCDBEntryForDCS(const std::string ccdbUrl, uint64_t timestamp)
Definition dcs-ccdb.cxx:158
void dump(const std::string what, DPMAP m, int verbose)
Definition dcs-ccdb.cxx:79
void makeDefaultCCDBEntry(const std::string ccdbUrl, uint64_t timestamp)
Definition dcs-ccdb.cxx:183
std::pair< DPVAL, DPVAL > computeTimeRange(const std::vector< DPVAL > &dps)
Definition dcs-ccdb.cxx:59
void doQueryDataPointConfig(const std::string ccdbUrl, uint64_t timestamp, const std::string dpConfName)
Definition dcs-ccdb.cxx:142
std::string CcdbDpConfName()
Definition dcs-ccdb.cxx:49
const GLfloat * m
Definition glcorearb.h:4066
const GLdouble * v
Definition glcorearb.h:832
GLsizei GLenum GLenum * types
Definition glcorearb.h:2516
GLsizei const GLfloat * value
Definition glcorearb.h:819
GLboolean GLboolean GLboolean GLboolean a
Definition glcorearb.h:1233
GLubyte GLubyte GLubyte GLubyte w
Definition glcorearb.h:852
GLsizeiptr const void GLenum usage
Definition glcorearb.h:659
o2::dcs::DataPointCompositeObject createDataPointCompositeObject(const std::string &alias, T val, uint32_t seconds, uint16_t msec, uint16_t flags=0)
std::vector< std::string > aliases(std::vector< MeasurementType > types={ MeasurementType::HV_V, MeasurementType::HV_I, MeasurementType::LV_V_FEE_ANALOG, MeasurementType::LV_V_FEE_DIGITAL, MeasurementType::LV_V_SOLAR})
std::vector< std::string > aliases(std::vector< MeasurementType > types={ MeasurementType::HV_V, MeasurementType::HV_I})
Definition DCSNamer.cxx:127
const char * subsysname()
Definition subsysname.h:17
uint64_t get_epoch_time() const noexcept
std::string ccdbUrl
#define main
std::array< uint16_t, 5 > pattern