Skip to content

Commit 2bb8638

Browse files
committed
dev: code for repopulating BK with old configs/scalers
1 parent d6f6148 commit 2bb8638

File tree

4 files changed

+205
-2
lines changed

4 files changed

+205
-2
lines changed

Detectors/CTP/workflowScalers/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,11 @@ o2_add_executable(
3434
SOURCES src/ctp-ccdb-orbit.cxx
3535
PUBLIC_LINK_LIBRARIES O2::DataFormatsCTP
3636
Boost::program_options)
37+
o2_add_executable(
38+
bk-write
39+
COMPONENT_NAME ctp
40+
SOURCES src/ctp-bk-write.cxx
41+
PUBLIC_LINK_LIBRARIES O2::DataFormatsCTP
42+
O2::CTPWorkflowScalers
43+
AliceO2::BookkeepingApi
44+
Boost::program_options)

Detectors/CTP/workflowScalers/include/CTPWorkflowScalers/ctpCCDBManager.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ class ctpCCDBManager
3131
int saveOrbitReset(long timeStamp);
3232
int saveCtpCfg(uint32_t runNumber, long timeStamp);
3333
static CTPConfiguration getConfigFromCCDB(long timestamp, std::string run, bool& ok);
34-
static CTPConfiguration getConfigFromCCDB(long timestamp, std::string run);
35-
CTPRunScalers getScalersFromCCDB(long timestamp, std::string, bool& ok);
34+
CTPConfiguration getConfigFromCCDB(long timestamp, std::string run);
35+
CTPRunScalers getScalersFromCCDB(long timestamp, std::string run, bool& ok);
36+
static CTPRunScalers getScalersFromCCDB(long timestamp, std::string, std::string path, bool& ok);
3637
static void setCCDBHost(std::string host) { mCCDBHost = host; };
3738
static void setQCDBHost(std::string host) { mQCDBHost = host; };
3839
void setCtpCfgDir(std::string& ctpcfgdir) { mCtpCfgDir = ctpcfgdir; };
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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+
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
13+
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
14+
// All rights not expressly granted are reserved.
15+
//
16+
// This software is distributed under the terms of the GNU General Public
17+
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
18+
//
19+
// In applying this license CERN does not waive the privileges and immunities
20+
// granted to it by virtue of its status as an Intergovernmental Organization
21+
// or submit itself to any jurisdiction.
22+
23+
// example to run:
24+
//
25+
#include <boost/program_options.hpp>
26+
#include <filesystem>
27+
#include <TFile.h>
28+
#include <TStopwatch.h>
29+
#include "CommonUtils/StringUtils.h"
30+
#include <CCDB/BasicCCDBManager.h>
31+
#include "CTPWorkflowScalers/ctpCCDBManager.h"
32+
#include "BookkeepingApi/BkpClientFactory.h"
33+
#include "BookkeepingApi/BkpClient.h"
34+
#include <iostream>
35+
#include <fstream>
36+
#include <vector>
37+
#include <string>
38+
namespace bpo = boost::program_options;
39+
//
40+
41+
int main(int argc, char** argv)
42+
{
43+
const std::string testCCDB = "http://ccdb-test.cern.ch:8080";
44+
// std::string prodCCDB = "http://o2-ccdb.internal";
45+
const std::string aliceCCDB = "http://alice-ccdb.cern.ch";
46+
bpo::variables_map vm;
47+
bpo::options_description opt_general("Usage:\n " + std::string(argv[0]) +
48+
" Write ctp config or scalers to BK\n");
49+
bpo::options_description opt_hidden("");
50+
bpo::options_description opt_all;
51+
bpo::positional_options_description opt_pos;
52+
try {
53+
auto add_option = opt_general.add_options();
54+
add_option("help,h", "Print this help message");
55+
add_option("input-file,f", bpo::value<std::string>()->default_value("none"), "input file name, none - do not read file");
56+
add_option("bkhost,b", bpo::value<std::string>()->default_value("none"), "bk web address");
57+
add_option("ccdb", bpo::value<std::string>()->default_value("alice"), "choose databse: test- test ccdb; prod - production ccdb; alice - alice ccdb; else ccdb parameter");
58+
add_option("run-number,r", bpo::value<uint32_t>()->default_value(0), "run number");
59+
add_option("timestamp,t", bpo::value<uint64_t>()->default_value(0), "timestamp; if 0 timestamp is calulated inside this code");
60+
add_option("cfg,c",bpo::value<bool>()->default_value(0),"Do cfg");
61+
add_option("scalers,s",bpo::value<bool>()->default_value(0),"Do scalers");
62+
//
63+
opt_all.add(opt_general).add(opt_hidden);
64+
bpo::store(bpo::command_line_parser(argc, argv).options(opt_all).positional(opt_pos).run(), vm);
65+
if (vm.count("help")) {
66+
std::cout << opt_general << std::endl;
67+
exit(0);
68+
}
69+
bpo::notify(vm);
70+
} catch (bpo::error& e) {
71+
std::cerr << "ERROR: " << e.what() << std::endl
72+
<< std::endl;
73+
std::cerr << opt_general << std::endl;
74+
exit(1);
75+
} catch (std::exception& e) {
76+
std::cerr << e.what() << ", application will now exit" << std::endl;
77+
exit(2);
78+
}
79+
uint64_t timestamp = vm["timestamp"].as<uint64_t>();
80+
//
81+
int ret = 0;
82+
std::vector<std::string> runs;
83+
int32_t run = vm["run-number"].as<uint32_t>();
84+
std::cout << "run:" << run << std::endl;
85+
if(run) {
86+
std::cout << "pushing" << std::endl;
87+
runs.push_back(std::to_string(run));
88+
}
89+
// read input file
90+
std::string filename = vm["input-file"].as<std::string>();
91+
std::ifstream file(filename);
92+
if (!file.is_open()) {
93+
std::cout << "Cannot open file! Using only run:" << run << std::endl;
94+
} else {
95+
std::string line;
96+
while (std::getline(file, line)) {
97+
std::cout << line << "\n";
98+
std::vector<std::string> tokens = o2::utils::Str::tokenize(line, ' ');
99+
//int run = std::stoi(tokens[0]);
100+
runs.push_back(tokens[0]);
101+
}
102+
}
103+
bool cfg = vm["cfg"].as<bool>();
104+
bool scalers = vm["scalers"].as<bool>();
105+
std::cout << "Doing: cfg:" << cfg << " scal:" << scalers << std::endl;
106+
if(cfg || scalers){
107+
std::string bkhost = vm["bkhost"].as<std::string>();
108+
std::unique_ptr<o2::bkp::api::BkpClient> mBKClient = o2::bkp::api::BkpClientFactory::create(bkhost);
109+
//get from ccdb
110+
std::string ccdbAddress;
111+
if (vm["ccdb"].as<std::string>() == "prod") {
112+
//ccdbAddress = prodCCDB;
113+
} else if (vm["ccdb"].as<std::string>() == "test") {
114+
ccdbAddress = testCCDB;
115+
} else if (vm["ccdb"].as<std::string>() == "alice") {
116+
ccdbAddress = aliceCCDB;
117+
} else {
118+
ccdbAddress = vm["ccdb"].as<std::string>();
119+
}
120+
o2::ctp::ctpCCDBManager::setCCDBHost(ccdbAddress);
121+
std::cout << "CCDB: " << vm["ccdb"].as<std::string>() << " " << ccdbAddress << std::endl;
122+
//o2::ccdb::CcdbApi api;
123+
//api.init(ccdbAddress.c_str());
124+
std::map<std::string, std::string> metadata;
125+
for(auto const& run: runs) {
126+
metadata["runNumber"] = run;
127+
bool ok;
128+
int runNumber = std::stoi(run);
129+
auto ctpcfg = o2::ctp::ctpCCDBManager::getConfigFromCCDB(timestamp,run,ok);
130+
131+
if(cfg) {
132+
std::string ctpcfgstr = ctpcfg.getConfigString();
133+
try {
134+
mBKClient->run()->setRawCtpTriggerConfiguration(runNumber, ctpcfgstr);
135+
} catch (std::runtime_error& error) {
136+
std::cerr << "An error occurred: " << error.what() << std::endl;
137+
//return 1;
138+
}
139+
LOG(info) << "Run BK:" << run << " CFG:" << cfg;
140+
}
141+
if(scalers){
142+
auto ctpcnts = o2::ctp::ctpCCDBManager::getScalersFromCCDB(timestamp,run, "CTP/Calib/Scalers", ok);
143+
ctpcnts.convertRawToO2();
144+
std::vector<uint32_t> clsinds = ctpcnts.getClassIndexes();
145+
long ts = ctpcnts.getTimeLimit().second;
146+
int i = 0;
147+
for(auto const& ind: clsinds){
148+
std::array<uint64_t,7> cntsbk = ctpcnts.getIntegralForClass(i);
149+
std::string clsname = ctpcfg.getClassNameFromHWIndex(cntsbk[0]);
150+
try {
151+
mBKClient->ctpTriggerCounters()->createOrUpdateForRun(runNumber, clsname, ts, cntsbk[1], cntsbk[2], cntsbk[3], cntsbk[4], cntsbk[5], cntsbk[6]);
152+
std::cout << runNumber << " clsname: " << clsname << "t:" << ts << "cnts:" << cntsbk[1] << " " << cntsbk[2] << " " << cntsbk[3] << " " << cntsbk[4] << " " << cntsbk[5] << " " << cntsbk[6] << std::endl;;
153+
154+
} catch (std::runtime_error& error) {
155+
std::cerr << "An error occurred: " << error.what() << std::endl;
156+
//return 1;
157+
}
158+
LOG(info) << "Run BK scalers ok";
159+
i++;
160+
}
161+
}
162+
}
163+
// add to bk
164+
}
165+
std::cout << "o2-ctp-bk-write done" << std::endl;
166+
return ret;
167+
}

Detectors/CTP/workflowScalers/src/ctpCCDBManager.cxx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,16 @@ int ctpCCDBManager::saveCtpCfg(uint32_t runNumber, long timeStart)
204204
}
205205
CTPConfiguration ctpCCDBManager::getConfigFromCCDB(long timestamp, std::string run, bool& ok)
206206
{
207+
207208
auto& mgr = o2::ccdb::BasicCCDBManager::instance();
208209
mgr.setURL(mCCDBHost);
209210
std::map<std::string, std::string> metadata; // can be empty
210211
metadata["runNumber"] = run;
212+
if(timestamp == 0) {
213+
// Timestamp
214+
auto soreor = mgr.getRunDuration(std::stoi(run));
215+
timestamp = (soreor.second - soreor.first) / 2 + soreor.first;
216+
}
211217
auto ctpconfigdb = mgr.getSpecific<CTPConfiguration>(CCDBPathCTPConfig, timestamp, metadata);
212218
if (ctpconfigdb == nullptr) {
213219
LOG(info) << "CTP config not in database, timestamp:" << timestamp;
@@ -245,3 +251,24 @@ CTPRunScalers ctpCCDBManager::getScalersFromCCDB(long timestamp, std::string run
245251
}
246252
return *ctpscalers;
247253
}
254+
CTPRunScalers ctpCCDBManager::getScalersFromCCDB(long timestamp, std::string run, std::string path, bool& ok)
255+
{
256+
auto& mgr = o2::ccdb::BasicCCDBManager::instance();
257+
mgr.setURL(mCCDBHost);
258+
std::map<std::string, std::string> metadata; // can be empty
259+
metadata["runNumber"] = run;
260+
if(timestamp == 0) {
261+
// Timestamp
262+
auto soreor = mgr.getRunDuration(std::stoi(run));
263+
timestamp = (soreor.second - soreor.first) / 2 + soreor.first;
264+
}
265+
auto ctpscalers = mgr.getSpecific<CTPRunScalers>(path, timestamp, metadata);
266+
if (ctpscalers == nullptr) {
267+
LOG(info) << "CTPRunScalers not in database, timestamp:" << timestamp;
268+
ok = 0;
269+
} else {
270+
// ctpscalers->printStream(std::cout);
271+
ok = 1;
272+
}
273+
return *ctpscalers;
274+
}

0 commit comments

Comments
 (0)