Project
Loading...
Searching...
No Matches
ctpCCDBManager.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
16#include "CCDB/CcdbApi.h"
18#include <sstream>
19#include <regex>
21#include <fairlogger/Logger.h>
22using namespace o2::ctp;
23std::string ctpCCDBManager::mCCDBHost = "http://o2-ccdb.internal";
24std::string ctpCCDBManager::mQCDBHost = "http://ali-qcdb.cern.ch:8083";
25// std::string ctpCCDBManager::mQCDBHost = "none";
26//
27int ctpCCDBManager::saveRunScalersToCCDB(CTPRunScalers& scalers, long timeStart, long timeStop)
28{
29 // data base
30 if (mCCDBHost == "none") {
31 LOG(info) << "Scalers not written to CCDB none";
32 return 0;
33 }
34 // CTPActiveRun* run = mActiveRuns[i];
35 using namespace std::chrono_literals;
36 std::chrono::seconds days3 = 259200s;
37 std::chrono::seconds min10 = 600s;
38 long time3days = std::chrono::duration_cast<std::chrono::milliseconds>(days3).count();
39 long time10min = std::chrono::duration_cast<std::chrono::milliseconds>(min10).count();
40 long tmin = timeStart - time10min;
41 long tmax = timeStop + time3days;
43 map<string, string> metadata; // can be empty
44 metadata["runNumber"] = std::to_string(scalers.getRunNumber());
45 api.init(mCCDBHost.c_str()); // or http://localhost:8080 for a local installation
46 // store abitrary user object in strongly typed manner
47 int ret = api.storeAsTFileAny(&(scalers), mCCDBPathCTPScalers, metadata, tmin, tmax);
48 if (ret == 0) {
49 LOG(info) << "CTP scalers saved in ccdb:" << mCCDBHost << " run:" << scalers.getRunNumber() << " tmin:" << tmin << " tmax:" << tmax;
50 } else {
51 LOG(fatal) << "Problem writing to database ret:" << ret;
52 }
53 return ret;
54}
55int ctpCCDBManager::saveRunScalersToQCDB(CTPRunScalers& scalers, long timeStart, long timeStop)
56{
57 // data base
58 if (mQCDBHost == "none") {
59 LOG(info) << "Scalers not written to QCDB none";
60 return 0;
61 }
62 // CTPActiveRun* run = mActiveRuns[i];q
63 using namespace std::chrono_literals;
64 std::chrono::seconds days3 = 259200s;
65 std::chrono::seconds min10 = 600s;
66 long time3days = std::chrono::duration_cast<std::chrono::milliseconds>(days3).count();
67 long time10min = std::chrono::duration_cast<std::chrono::milliseconds>(min10).count();
68 long tmin = timeStart - time10min;
69 long tmax = timeStop + time3days;
71 map<string, string> metadata; // can be empty
72 metadata["runNumber"] = std::to_string(scalers.getRunNumber());
73 api.init(mQCDBHost.c_str()); // or http://localhost:8080 for a local installation
74 // store abitrary user object in strongly typed manner
75 int ret = api.storeAsTFileAny(&(scalers), mQCDBPathCTPScalers, metadata, tmin, tmax);
76 if (ret == 0) {
77 LOG(info) << "CTP scalers saved in qcdb:" << mQCDBHost << " run:" << scalers.getRunNumber() << " tmin:" << tmin << " tmax:" << tmax;
78 } else {
79 LOG(fatal) << "CTP scalers Problem writing to database qcdb ret:" << ret;
80 }
81 return ret;
82}
84{
85 // data base
86 if (mCCDBHost == "none") {
87 LOG(info) << "CTP config not written to CCDB none";
88 return 0;
89 }
90 using namespace std::chrono_literals;
91 std::chrono::seconds days3 = 259200s;
92 std::chrono::seconds min10 = 600s;
93 long time3days = std::chrono::duration_cast<std::chrono::milliseconds>(days3).count();
94 long time10min = std::chrono::duration_cast<std::chrono::milliseconds>(min10).count();
95 long tmin = timeStart - time10min;
96 long tmax = timeStart + time3days;
98 map<string, string> metadata; // can be empty
99 metadata["runNumber"] = std::to_string(cfg->getRunNumber());
100 api.init(mCCDBHost.c_str()); // or http://localhost:8080 for a local installation
101 // store abitrary user object in strongly typed manner
102 int ret = api.storeAsTFileAny(cfg, CCDBPathCTPConfig, metadata, tmin, tmax);
103 if (ret == 0) {
104 LOG(info) << "CTP config saved in ccdb:" << mCCDBHost << " run:" << cfg->getRunNumber() << " tmin:" << tmin << " tmax:" << tmax;
105 } else {
106 LOG(fatal) << "CTPConfig: Problem writing to database ret:" << ret;
107 }
108 return ret;
109}
110int ctpCCDBManager::saveSoxOrbit(uint32_t runNumber, uint32_t soxOrbit, long timestamp)
111{
112 // data base
113 if (mCCDBHost == "none") {
114 LOG(info) << "SOX Orbit not written to CCDB none";
115 return 0;
116 }
117 std::vector<int64_t> vect;
118 if (timestamp == 0) {
119 auto now = std::chrono::system_clock::now();
120 timestamp = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count();
121 }
122 vect.push_back(timestamp);
123 vect.push_back((uint64_t)runNumber);
124 vect.push_back((uint64_t)soxOrbit);
125 long tmin = timestamp / 1000;
126 long tmax = tmin + 381928219;
128 map<string, string> metadata; // can be empty
129 metadata["runNumber"] = std::to_string(runNumber);
130 api.init(mCCDBHost.c_str()); // or http://localhost:8080 for a local installation
131
132 // store abitrary user object in strongly typed manner
133 int ret = api.storeAsTFileAny(&vect, mCCDBPathSoxOrbit, metadata, tmin, tmax);
134 if (ret == 0) {
135 LOG(info) << "SOX orbit saved in ccdb:" << mCCDBHost << " run:" << runNumber << " tmin:" << tmin << " tmax:" << tmax;
136 } else {
137 LOG(fatal) << "SOX orbit Problem writing to database ret:" << ret;
138 }
139 return 0;
140}
142{
143 // data base
144 if (mCCDBHost == "none") {
145 LOG(info) << "Orbit Reset not written to CCDB none";
146 return 0;
147 }
148 std::vector<int64_t> vect;
149 if (timeStamp == 0) {
150 auto now = std::chrono::system_clock::now();
151 timeStamp = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count();
152 LOG(warn) << "Received timestamp = 0 , using current time:" << timeStamp;
153 }
154 vect.push_back(timeStamp);
155 long tmin = timeStamp / 1000;
156 long tmax = tmin + 381928219;
158 map<string, string> metadata; // can be empty
159 api.init(mCCDBHost.c_str()); // or http://localhost:8080 for a local installation
160
161 // store abitrary user object in strongly typed manner
162 int ret = api.storeAsTFileAny(&vect, mCCDBPathOrbitReset, metadata, tmin, tmax);
163 if (ret == 0) {
164 LOG(info) << "Orbit reset saved in ccdb:" << mCCDBHost << " tmin:" << tmin << " tmax:" << tmax;
165 } else {
166 LOG(fatal) << "Orbit reset Problem writing to database ret:" << ret;
167 }
168 return 0;
169}
170CTPConfiguration ctpCCDBManager::getConfigFromCCDB(long timestamp, std::string run, bool& ok)
171{
173 mgr.setURL(mCCDBHost);
174 map<string, string> metadata; // can be empty
175 metadata["runNumber"] = run;
176 auto ctpconfigdb = mgr.getSpecific<CTPConfiguration>(CCDBPathCTPConfig, timestamp, metadata);
177 if (ctpconfigdb == nullptr) {
178 LOG(info) << "CTP config not in database, timestamp:" << timestamp;
179 ok = 0;
180 } else {
181 // ctpconfigdb->printStream(std::cout);
182 LOG(info) << "CTP config found. Run:" << run;
183 ok = 1;
184 }
185 return *ctpconfigdb;
186}
188{
189 bool ok;
190 auto ctpconfig = getConfigFromCCDB(timestamp, run, ok);
191 if (ok == 0) {
192 LOG(error) << "CTP config not in CCDB";
193 return CTPConfiguration();
194 }
195 return ctpconfig;
196}
197CTPRunScalers ctpCCDBManager::getScalersFromCCDB(long timestamp, std::string run, bool& ok)
198{
200 mgr.setURL(mCCDBHost);
201 map<string, string> metadata; // can be empty
202 metadata["runNumber"] = run;
203 auto ctpscalers = mgr.getSpecific<CTPRunScalers>(mCCDBPathCTPScalers, timestamp, metadata);
204 if (ctpscalers == nullptr) {
205 LOG(info) << "CTPRunScalers not in database, timestamp:" << timestamp;
206 ok = 0;
207 } else {
208 // ctpscalers->printStream(std::cout);
209 ok = 1;
210 }
211 return *ctpscalers;
212}
definition of CTPConfiguration and related CTP structures
static BasicCCDBManager & instance()
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
uint32_t getRunNumber()
Definition Scalers.h:104
const std::string mCCDBPathOrbitReset
static std::string mCCDBHost
Database constants.
const std::string mQCDBPathCTPScalers
int saveRunScalersToQCDB(CTPRunScalers &scalers, long timeStart, long timeStop)
static std::string mQCDBHost
int saveSoxOrbit(uint32_t runNumber, uint32_t soxOrbit, long timeStart)
CTPRunScalers getScalersFromCCDB(long timestamp, std::string, bool &ok)
int saveRunScalersToCCDB(CTPRunScalers &scalers, long timeStart, long timeStop)
int saveRunConfigToCCDB(CTPConfiguration *cfg, long timeStart)
static CTPConfiguration getConfigFromCCDB(long timestamp, std::string run, bool &ok)
const std::string mCCDBPathCTPScalers
int saveOrbitReset(long timeStamp)
const std::string mCCDBPathSoxOrbit
const std::string CCDBPathCTPConfig
Database constants.
std::string to_string(gsl::span< T, Size > span)
Definition common.h:52
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"