Project
Loading...
Searching...
No Matches
ctp-bk-write.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// 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>
32#include "BookkeepingApi/BkpClientFactory.h"
33#include "BookkeepingApi/BkpClient.h"
34#include <iostream>
35#include <fstream>
36#include <vector>
37#include <string>
38namespace bpo = boost::program_options;
39//
40// Test in the lab
41// o2-ctp-bk-write -r 37 -s 1 -c 1 --ccdb='http://acsl-ccdb.cern.ch:8083' -b 'acsl-aliecs.cern.ch:4001' -t 1753185071753
42//
43int main(int argc, char** argv)
44{
45 const std::string testCCDB = "http://ccdb-test.cern.ch:8080";
46 // std::string prodCCDB = "http://o2-ccdb.internal";
47 const std::string aliceCCDB = "http://alice-ccdb.cern.ch";
48 bpo::variables_map vm;
49 bpo::options_description opt_general("Usage:\n " + std::string(argv[0]) +
50 " Write ctp config or scalers to BK\n");
51 bpo::options_description opt_hidden("");
52 bpo::options_description opt_all;
53 bpo::positional_options_description opt_pos;
54 try {
55 auto add_option = opt_general.add_options();
56 add_option("help,h", "Print this help message");
57 add_option("input-file,f", bpo::value<std::string>()->default_value("none"), "input file name, none - do not read file");
58 add_option("bkhost,b", bpo::value<std::string>()->default_value("none"), "bk web address");
59 add_option("ccdb", bpo::value<std::string>()->default_value("alice"), "choose databse: test- test ccdb; prod - production ccdb; alice - alice ccdb; else ccdb parameter");
60 add_option("run-number,r", bpo::value<uint32_t>()->default_value(0), "run number");
61 add_option("timestamp,t", bpo::value<uint64_t>()->default_value(0), "timestamp; if 0 timestamp is calulated inside this code");
62 add_option("cfg,c", bpo::value<bool>()->default_value(0), "Do cfg");
63 add_option("scalers,s", bpo::value<bool>()->default_value(0), "Do scalers");
64 //
65 opt_all.add(opt_general).add(opt_hidden);
66 bpo::store(bpo::command_line_parser(argc, argv).options(opt_all).positional(opt_pos).run(), vm);
67 if (vm.count("help")) {
68 std::cout << opt_general << std::endl;
69 exit(0);
70 }
71 bpo::notify(vm);
72 } catch (bpo::error& e) {
73 std::cerr << "ERROR: " << e.what() << std::endl
74 << std::endl;
75 std::cerr << opt_general << std::endl;
76 exit(1);
77 } catch (std::exception& e) {
78 std::cerr << e.what() << ", application will now exit" << std::endl;
79 exit(2);
80 }
81 uint64_t timestamp = vm["timestamp"].as<uint64_t>();
82 //
83 int ret = 0;
84 std::vector<std::string> runs;
85 int32_t run = vm["run-number"].as<uint32_t>();
86 std::cout << "run:" << run << std::endl;
87 if (run) {
88 std::cout << "pushing" << std::endl;
89 runs.push_back(std::to_string(run));
90 }
91 // read input file
92 std::string filename = vm["input-file"].as<std::string>();
93 if (filename != "none") {
94 std::ifstream file(filename);
95 if (!file.is_open()) {
96 LOG(fatal) << "Cannot open file:" << filename << std::endl;
97 } else {
98 std::string line;
99 while (std::getline(file, line)) {
100 std::cout << line << "\n";
101 std::vector<std::string> tokens = o2::utils::Str::tokenize(line, ' ');
102 // int run = std::stoi(tokens[0]);
103 runs.push_back(tokens[0]);
104 }
105 }
106 }
107 bool cfg = vm["cfg"].as<bool>();
108 bool scalers = vm["scalers"].as<bool>();
109 std::cout << "Doing: cfg:" << cfg << " scal:" << scalers << std::endl;
110 if (cfg || scalers) {
111 std::string bkhost = vm["bkhost"].as<std::string>();
112 std::unique_ptr<o2::bkp::api::BkpClient> mBKClient = o2::bkp::api::BkpClientFactory::create(bkhost);
113 // get from ccdb
114 std::string ccdbAddress;
115 if (vm["ccdb"].as<std::string>() == "prod") {
116 // ccdbAddress = prodCCDB;
117 } else if (vm["ccdb"].as<std::string>() == "test") {
118 ccdbAddress = testCCDB;
119 } else if (vm["ccdb"].as<std::string>() == "alice") {
120 ccdbAddress = aliceCCDB;
121 } else {
122 ccdbAddress = vm["ccdb"].as<std::string>();
123 }
125 std::cout << "CCDB: " << vm["ccdb"].as<std::string>() << " " << ccdbAddress << std::endl;
126 std::map<std::string, std::string> metadata;
127 for (auto const& run : runs) {
128 metadata["runNumber"] = run;
129 bool ok;
130 int runNumber = std::stoi(run);
131 auto ctpcfg = o2::ctp::ctpCCDBManager::getConfigFromCCDB(timestamp, run, ok);
132
133 if (cfg) {
134 std::string ctpcfgstr = ctpcfg.getConfigString();
135 try {
136 mBKClient->run()->setRawCtpTriggerConfiguration(runNumber, ctpcfgstr);
137 } catch (std::runtime_error& error) {
138 std::cerr << "An error occurred: " << error.what() << std::endl;
139 // return 1;
140 }
141 LOG(info) << "Run BK:" << run << " CFG:" << cfg;
142 }
143 if (scalers) {
144 auto ctpcnts = o2::ctp::ctpCCDBManager::getScalersFromCCDB(timestamp, run, "CTP/Calib/Scalers", ok);
145 ctpcnts.convertRawToO2();
146 std::vector<uint32_t> clsinds = ctpcnts.getClassIndexes();
147 long ts = ctpcnts.getTimeLimit().second;
148 int i = 0;
149 for (auto const& ind : clsinds) {
150 std::array<uint64_t, 7> cntsbk = ctpcnts.getIntegralForClass(i);
151 std::string clsname = ctpcfg.getClassNameFromHWIndex(cntsbk[0]);
152 try {
153 mBKClient->ctpTriggerCounters()->createOrUpdateForRun(runNumber, clsname, ts, cntsbk[1], cntsbk[2], cntsbk[3], cntsbk[4], cntsbk[5], cntsbk[6]);
154 std::cout << runNumber << " clsname: " << cntsbk[0] << " " << clsname << " t:" << ts << " cnts:" << cntsbk[1] << " " << cntsbk[2] << " " << cntsbk[3] << " " << cntsbk[4] << " " << cntsbk[5] << " " << cntsbk[6] << std::endl;
155 ;
156
157 } catch (std::runtime_error& error) {
158 std::cerr << "An error occurred: " << error.what() << std::endl;
159 // return 1;
160 }
161 LOG(debug) << "Run BK scalers ok";
162 i++;
163 }
164 }
165 }
166 // add to bk
167 }
168 std::cout << "o2-ctp-bk-write done" << std::endl;
169 return ret;
170}
std::ostringstream debug
int32_t i
CTPRunScalers getScalersFromCCDB(long timestamp, std::string run, bool &ok)
static void setCCDBHost(std::string host)
static CTPConfiguration getConfigFromCCDB(long timestamp, std::string run, bool &ok)
std::string to_string(gsl::span< T, Size > span)
Definition common.h:52
std::string filename()
static std::vector< std::string > tokenize(const std::string &src, char delim, bool trimToken=true, bool skipEmpty=true)
#define main
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"