Skip to content

Commit 0beed6b

Browse files
chiarazampollishahor02
authored andcommitted
example macro for Milestone Week 2 - CCDB test
Ruben's macro Fixes clang remove initial macro include for usleep
1 parent 1ea125a commit 0beed6b

File tree

3 files changed

+183
-0
lines changed

3 files changed

+183
-0
lines changed

Detectors/Calibration/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,18 @@ o2_add_executable(ccdb-populator-workflow
4242
O2::DataFormatsTOF
4343
O2::CCDB)
4444

45+
install(FILES testMacros/populateCCDB.C
46+
DESTINATION share/macro/)
47+
48+
o2_add_test_root_macro(testMacros/populateCCDB.C
49+
PUBLIC_LINK_LIBRARIES O2::CCDB
50+
O2::Framework)
51+
52+
o2_add_executable(populate-ccdb
53+
COMPONENT_NAME calibration
54+
SOURCES testMacros/populateCCDB.cxx
55+
PUBLIC_LINK_LIBRARIES ROOT::MathCore
56+
O2::Framework
57+
O2::CCDB)
58+
4559
add_subdirectory(workflow)
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#if !defined(__CLING__) || defined(__ROOTCLING__)
2+
3+
#include <string>
4+
#include <chrono>
5+
#include <iostream>
6+
#include <string>
7+
#include <fstream>
8+
#include <regex>
9+
#include <unistd.h>
10+
#include <TRandom.h>
11+
#include "Framework/Logger.h"
12+
#include "CCDB/CcdbApi.h"
13+
14+
#endif
15+
16+
// macro to populate the CCDB emulating the rates that we expect for
17+
// Run 3, as read (in terms of size and rate) from an external file
18+
19+
using DurSec = std::chrono::duration<double, std::ratio<1, 1>>;
20+
21+
struct CCDBObj {
22+
std::string path;
23+
float validity;
24+
size_t sz;
25+
size_t count = 0;
26+
std::decay_t<decltype(std::chrono::high_resolution_clock::now())> lastUpdate{};
27+
CCDBObj(const std::string& _path, size_t _sz, float _val) : path(_path), validity(_val), sz(_sz) {}
28+
};
29+
30+
std::vector<CCDBObj> readObjectsList(const std::string& fname);
31+
void pushObject(o2::ccdb::CcdbApi& api, const CCDBObj& obj);
32+
33+
void populateCCDB(const std::string& fname = "cdbSizeV0.txt", const std::string& ccdbHost = "http://localhost:8080" /*"http://ccdb-test.cern.ch:8080"*/)
34+
{
35+
auto objs = readObjectsList(fname);
36+
if (objs.empty()) {
37+
return;
38+
}
39+
40+
o2::ccdb::CcdbApi api;
41+
api.init(ccdbHost.c_str()); // or http://localhost:8080 for a local installation
42+
43+
while (true) {
44+
auto timeLoopStart = std::chrono::high_resolution_clock::now();
45+
double minTLeft = 1e99;
46+
for (auto& o : objs) {
47+
DurSec elapsedSeconds = timeLoopStart - o.lastUpdate;
48+
if (elapsedSeconds.count() > o.validity || !o.count) {
49+
std::cout << "Storing entry: " << o.path << " copy " << o.count
50+
<< " after " << (o.count ? elapsedSeconds.count() : 0.) << "s\n";
51+
pushObject(api, o);
52+
o.count++;
53+
o.lastUpdate = timeLoopStart;
54+
if (minTLeft < 0.9 * o.validity) {
55+
minTLeft = o.validity;
56+
}
57+
}
58+
}
59+
usleep(minTLeft * 0.9 * 1e6);
60+
}
61+
}
62+
63+
std::vector<CCDBObj> readObjectsList(const std::string& fname)
64+
{
65+
std::vector<CCDBObj> objs;
66+
std::ifstream inFile(fname);
67+
if (!inFile.is_open()) {
68+
LOG(ERROR) << "Failed to open input file " << fname;
69+
return objs;
70+
}
71+
std::string str;
72+
while (std::getline(inFile, str)) {
73+
str = std::regex_replace(str, std::regex("^\\s+|\\s+$|\\s+\r\n$|\\s+\r$|\\s+\n$"), "$1");
74+
if (str[0] == '#' || str.empty()) {
75+
continue;
76+
}
77+
std::stringstream ss(str);
78+
std::string path;
79+
float sz = 0.f, sec = 0.f;
80+
ss >> path;
81+
ss >> sz;
82+
ss >> sec;
83+
if (sz == 0 || sec == 0) {
84+
LOG(ERROR) << "Invalid data for " << path;
85+
objs.clear();
86+
break;
87+
}
88+
LOG(INFO) << "Account " << path << " size= " << sz << " validity= " << sec;
89+
objs.emplace_back(path, sz, sec);
90+
}
91+
return objs;
92+
}
93+
94+
void pushObject(o2::ccdb::CcdbApi& api, const CCDBObj& obj)
95+
{
96+
std::vector<uint8_t> buff(obj.sz);
97+
for (auto& c : buff) {
98+
c = gRandom->Integer(0xff);
99+
}
100+
std::map<std::string, std::string> metadata; // can be empty
101+
metadata["responsible"] = "nobody";
102+
metadata["custom"] = "whatever";
103+
auto now = std::chrono::system_clock::now();
104+
auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
105+
auto timeStamp = now_ms.time_since_epoch();
106+
api.storeAsTFileAny(&buff, obj.path, metadata, timeStamp.count(), 1670700184549); // one year validity time
107+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright CERN and copyright holders of ALICE O2. This software is
2+
// distributed under the terms of the GNU General Public License v3 (GPL
3+
// Version 3), copied verbatim in the file "COPYING".
4+
//
5+
// See http://alice-o2.web.cern.ch/license for full licensing information.
6+
//
7+
// In applying this license CERN does not waive the privileges and immunities
8+
// granted to it by virtue of its status as an Intergovernmental Organization
9+
// or submit itself to any jurisdiction.
10+
11+
// executable to populate the CCDB emulating the rates that we expect for
12+
// Run 3, as read (in terms of size and rate) from an external file
13+
14+
#include "populateCCDB.C"
15+
#include <TRandom.h>
16+
#include <boost/program_options.hpp>
17+
#include <iostream>
18+
19+
namespace bpo = boost::program_options;
20+
21+
bool initOptionsAndParse(bpo::options_description& options, int argc, char* argv[], bpo::variables_map& vm)
22+
{
23+
options.add_options()(
24+
"host", bpo::value<std::string>()->default_value("ccdb-test.cern.ch:8080"), "CCDB server")(
25+
"in-file-name,n", bpo::value<std::string>()->default_value("cdbSizeV0.txt"), "File name with list of CCDB entries to upload")(
26+
"help,h", "Produce help message.");
27+
28+
try {
29+
bpo::store(parse_command_line(argc, argv, options), vm);
30+
31+
// help
32+
if (vm.count("help")) {
33+
std::cout << options << std::endl;
34+
return false;
35+
}
36+
37+
bpo::notify(vm);
38+
} catch (const bpo::error& e) {
39+
std::cerr << e.what() << "\n\n";
40+
std::cerr << "Error parsing command line arguments; Available options:\n";
41+
42+
std::cerr << options << std::endl;
43+
return false;
44+
}
45+
return true;
46+
}
47+
48+
int main(int argc, char* argv[])
49+
{
50+
bpo::options_description options("Allowed options");
51+
bpo::variables_map vm;
52+
if (!initOptionsAndParse(options, argc, argv, vm)) {
53+
return -1;
54+
}
55+
56+
// call populate "macro"
57+
auto& inputFile = vm["in-file-name"].as<std::string>();
58+
auto& ccdbHost = vm["host"].as<std::string>();
59+
populateCCDB(inputFile, ccdbHost);
60+
61+
return (0);
62+
}

0 commit comments

Comments
 (0)