Project
Loading...
Searching...
No Matches
UploadTool.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"
13#include "CCDB/CCDBQuery.h"
15#include "CCDB/CcdbObjectInfo.h"
16#include <map>
17#include "TFile.h"
18#include "TTree.h"
19#include "TClass.h"
20#include "TKey.h"
21#include <iostream>
22#include <boost/program_options.hpp>
23
24namespace bpo = boost::program_options;
25
26bool initOptionsAndParse(bpo::options_description& options, int argc, char* argv[], bpo::variables_map& vm)
27{
28 options.add_options()(
29 "host", bpo::value<std::string>()->default_value("ccdb-test.cern.ch:8080"), "CCDB server")(
30 "path,p", bpo::value<std::string>()->required(), "CCDB path")(
31 "file,f", bpo::value<std::string>()->required(), "ROOT file")(
32 "key,k", bpo::value<std::string>()->required(), "Key of object to upload")(
33 "meta,m", bpo::value<std::string>()->default_value(""), "List of key=value pairs for meta-information (k1=v1;k2=v2;k3=v3)")(
34 "starttimestamp,st", bpo::value<long>()->default_value(-1), "timestamp - default -1 = now")(
35 "endtimestamp,et", bpo::value<long>()->default_value(-1), "end of validity - default -1 = 1 day from now")(
36 "help,h", "Produce help message.");
37
38 try {
39 bpo::store(parse_command_line(argc, argv, options), vm);
40
41 // help
42 if (vm.count("help")) {
43 std::cout << options << std::endl;
44 return false;
45 }
46
47 bpo::notify(vm);
48 } catch (const bpo::error& e) {
49 std::cerr << e.what() << "\n\n";
50 std::cerr << "Error parsing command line arguments; Available options:\n";
51
52 std::cerr << options << std::endl;
53 return false;
54 }
55 return true;
56}
57
58// Remove leading whitespace
59std::string ltrimSpace(std::string src)
60{
61 return src.erase(0, src.find_first_not_of(' '));
62}
63
64// Remove trailing whitespace
65std::string rtrimSpace(std::string src)
66{
67 return src.erase(src.find_last_not_of(' ') + 1);
68}
69
70// Remove leading/trailing whitespace
71std::string trimSpace(std::string const& src)
72{
73 return ltrimSpace(rtrimSpace(src));
74}
75
76// Split a given string on a delim character, return vector of tokens
77// If trim is true, then also remove leading/trailing whitespace of each token.
78std::vector<std::string> splitString(const std::string& src, char delim, bool trim = false)
79{
80 std::stringstream ss(src);
81 std::string token;
82 std::vector<std::string> tokens;
83
84 while (std::getline(ss, token, delim)) {
85 token = (trim ? trimSpace(token) : token);
86 if (!token.empty()) {
87 tokens.push_back(std::move(token));
88 }
89 }
90
91 return tokens;
92}
93
94// a simple tool to take an abitrary object in a ROOT file and upload it to CCDB including some
95// meta information
96int main(int argc, char* argv[])
97{
98 bpo::options_description options("Allowed options");
99 bpo::variables_map vm;
100 if (!initOptionsAndParse(options, argc, argv, vm)) {
101 return 1;
102 }
103
105 auto host = vm["host"].as<std::string>();
106 api.init(host);
107
108 std::map<std::string, std::string> filter;
109 long starttimestamp = vm["starttimestamp"].as<long>();
110 if (starttimestamp == -1) {
111 starttimestamp = o2::ccdb::getCurrentTimestamp();
112 }
113 long endtimestamp = vm["endtimestamp"].as<long>();
114 if (endtimestamp == -1) {
115 constexpr long SECONDSPERDAY = 1 * 24 * 60 * 60;
116 endtimestamp = o2::ccdb::getFutureTimestamp(SECONDSPERDAY);
117 }
118
119 auto filename = vm["file"].as<std::string>();
120 auto path = vm["path"].as<std::string>();
121 auto keyname = vm["key"].as<std::string>();
122
123 // Take a vector of strings with elements of form a=b, and
124 // return a vector of pairs with each pair of form <a, b>
125 auto toKeyValPairs = [](std::vector<std::string> const& tokens) {
126 std::vector<std::pair<std::string, std::string>> pairs;
127
128 for (auto& token : tokens) {
129 auto keyval = splitString(token, '=');
130 if (keyval.size() != 2) {
131 // LOG(fatal) << "Illegal command-line key/value string: " << token;
132 continue;
133 }
134
135 std::pair<std::string, std::string> pair = std::make_pair(keyval[0], trimSpace(keyval[1]));
136 pairs.push_back(pair);
137 }
138
139 return pairs;
140 };
141 auto metastring = vm["meta"].as<std::string>();
142 auto keyvalues = toKeyValPairs(splitString(metastring, ';', true));
143
144 // fill meta map
145 std::map<std::string, std::string> meta;
146 for (auto& p : keyvalues) {
147 meta[p.first] = p.second;
148 }
149
150 if (filename == "headersOnly") {
151 auto ent = meta.find("Redirect");
152 std::cout << " Uploading a headers-only object to path " << path << " with timestamp validity from " << starttimestamp << " to " << endtimestamp
153 << " Redirection to: " << ((ent != meta.end()) ? ent->second : std::string{"none"}) << "\n";
154 api.storeAsBinaryFile(nullptr, 0, "ignored", "", path, meta, starttimestamp, endtimestamp);
155 if (!api.isSnapshotMode() && meta.find("adjustableEOV") != meta.end() && meta.find("default") == meta.end()) {
156 o2::ccdb::CcdbObjectInfo oi(path, "", "", meta, starttimestamp, endtimestamp);
158 }
159 } else {
160 TFile f(filename.c_str());
161 auto key = f.GetKey(keyname.c_str());
162 if (key) {
163 // get type of key
164 auto classname = key->GetClassName();
165 auto tcl = TClass::GetClass(classname);
166 auto object = f.Get<void>(keyname.c_str());
167 if (tcl->InheritsFrom("TTree")) {
168 auto tree = static_cast<TTree*>(object);
169 tree->LoadBaskets(0x1L << 32); // make tree memory based
170 tree->SetDirectory(nullptr);
171 }
172 // convert classname to typeinfo
173 // typeinfo
174 auto ti = tcl->GetTypeInfo();
175
176 std::cout << " Uploading an object of type " << key->GetClassName()
177 << " to path " << path << " with timestamp validity from " << starttimestamp
178 << " to " << endtimestamp << "\n";
179
180 api.storeAsTFile_impl(object, *ti, path, meta, starttimestamp, endtimestamp);
181 if (!api.isSnapshotMode() && meta.find("adjustableEOV") != meta.end() && meta.find("default") == meta.end()) {
182 o2::ccdb::CcdbObjectInfo oi(path, classname, filename, meta, starttimestamp, endtimestamp);
184 }
185 } else {
186 std::cerr << "Key " << keyname << " does not exist\n";
187 }
188 }
189
190 return 0;
191}
Timeframe settings Average distance in cm of events merged into Randomize distance around average distance of merged events Simulate correct bunch interactions instead of placing only the average number of events A value[n] sets TFlen for[n] collisions in Number of bunches per trainsort Number of bunch trains Instantaneous interaction rate Do not simulate border Total number of collisions to be placed in the interior of all time Do not select random but walk over array of events in stride steps h Event generator settings h configStandalone Use CPU for GPU Force usage of the specified GPU device no CPU fallback Continue processing after an error First event to Number of events to Number of iterations to Number of iterations to Number of initial iterations excluded from const char e
std::string ltrimSpace(std::string src)
std::string trimSpace(std::string const &src)
bool initOptionsAndParse(bpo::options_description &options, int argc, char *argv[], bpo::variables_map &vm)
std::string rtrimSpace(std::string src)
std::vector< std::string > splitString(const std::string &src, char delim, bool trim=false)
StringRef key
int storeAsTFile_impl(const void *obj1, std::type_info const &info, 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.cxx:413
void init(std::string const &hosts)
Definition CcdbApi.cxx:240
bool isSnapshotMode() const
Definition CcdbApi.h:99
int storeAsBinaryFile(const char *buffer, size_t size, const std::string &fileName, const std::string &objectType, const std::string &path, const std::map< std::string, std::string > &metadata, long startValidityTimestamp, long endValidityTimestamp, std::vector< char >::size_type maxSize=0) const
Definition CcdbApi.cxx:429
GLdouble f
Definition glcorearb.h:310
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
Definition glcorearb.h:1308
GLsizei const GLchar *const * path
Definition glcorearb.h:3591
GLuint object
Definition glcorearb.h:4041
long getCurrentTimestamp()
returns the timestamp in long corresponding to "now"
int adjustOverriddenEOV(CcdbApi &api, const CcdbObjectInfo &infoNew)
set EOV of overriden objects to SOV-1 of overriding one if it is allowed
long getFutureTimestamp(int secondsInFuture)
returns the timestamp in long corresponding to "now + secondsInFuture"
std::string filename()
#define main
std::unique_ptr< TTree > tree((TTree *) flIn.Get(std::string(o2::base::NameConf::CTFTREENAME).c_str()))