Project
Loading...
Searching...
No Matches
ctp-ccdb-orbit.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>
31#include <iostream>
32#include <vector>
33#include <string>
34namespace bpo = boost::program_options;
35//
36// get object from ccdb
37// auto & cc = o2::ccdb::BasicCCDNManager::instance();
38// auto pp = ccdbMgr.getSpecific<std::vector<long>>("CTP/Calib/OrbitResetTest")
39// std::cout << (*pp)[0] << std::endl;
40int main(int argc, char** argv)
41{
42 const std::string testCCDB = "http://ccdb-test.cern.ch:8080";
43 const std::string prodCCDB = "http://o2-ccdb.internal";
44 bpo::variables_map vm;
45 bpo::options_description opt_general("Usage:\n " + std::string(argv[0]) +
46 " Write orbit staff to ccdb\n");
47 bpo::options_description opt_hidden("");
48 bpo::options_description opt_all;
49 bpo::positional_options_description opt_pos;
50 try {
51 auto add_option = opt_general.add_options();
52 add_option("help,h", "Print this help message");
53 add_option("output-file,o", bpo::value<std::string>()->default_value("none"), "output file name, none - file not created");
54 add_option("output-dir,d", bpo::value<std::string>()->default_value("./"), "output dir");
55 add_option("ccdb", bpo::value<std::string>()->default_value("test"), "choose databse: test- test ccdb; prod - production ccdb; else ccdb parameter");
56 add_option("action,a", bpo::value<std::string>()->default_value(""), "sox - first orbit otherwise orbit reset");
57 add_option("run-number,r", bpo::value<int64_t>()->default_value(123), "run number");
58 add_option("testReset,t", bpo::value<bool>()->default_value(0), "0 = CTP/Calib/OrbitReset; 1 = CTP/Calib/OrbitResetTest");
59 add_option("sox-orbit,x", bpo::value<int64_t>()->default_value(0), "SOX orbit");
60 add_option("timestamp,s", bpo::value<uint64_t>()->default_value(0), "timestamp of SOX/orbit reading; if 0 timestamp is calulated inside this code");
61
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 int ret = 0;
80 std::string action = vm["action"].as<std::string>();
81 std::vector<int64_t> vect;
82 std::string ccdbPath;
83 long tt = vm["timestamp"].as<uint64_t>();
84 if (tt == 0) {
85 auto now = std::chrono::system_clock::now();
86 tt = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count();
87 }
88 vect.push_back(tt);
89 if (action == "sox") {
90 // write to CTP/Calib/FirstRunOrbit
91 std::cout << "===> FirsRunOrbit" << std::endl;
92 vect.push_back(vm["run-number"].as<int64_t>());
93 vect.push_back(vm["sox-orbit"].as<int64_t>());
94 ccdbPath = "CTP/Calib/FirstRunOrbit";
95 } else {
96 // write to CTP/Calib/OrbitReset
97 std::cout << "===> ResetOrbit" << std::endl;
98 ccdbPath = "CTP/Calib/OrbitReset";
99 if (vm["testReset"].as<bool>()) {
100 ccdbPath += "Test";
101 }
102 }
103 //
104 std::string ccdbAddress;
105 if (vm["ccdb"].as<std::string>() == "prod") {
106 ccdbAddress = prodCCDB;
107 } else if (vm["ccdb"].as<std::string>() == "test") {
108 ccdbAddress = testCCDB;
109 } else {
110 ccdbAddress = vm["ccdb"].as<std::string>();
111 }
112 std::cout << " Writing to db:" << ccdbAddress << std::endl;
113 if (ccdbAddress != "none") {
114 // auto& ccdbMgr = o2::ccdb::BasicCCDBManager::instance();
116 api.init(ccdbAddress.c_str());
117 std::map<std::string, std::string> metadata;
118 long tmin = tt / 1000;
119 long tmax = tmin + 381928219;
120 if (action == "sox") {
121 int64_t runnum = vm["run-number"].as<int64_t>();
122 metadata["runNumber"] = std::to_string(runnum);
123 std::cout << "Storing:" << ccdbPath << " " << metadata["runNumber"] << " tmin:" << tmin << " tmax:" << tmax << " ts:" << tt << std::endl;
124 ret = api.storeAsTFileAny(&(vect), ccdbPath, metadata, tmin, tmax);
125 } else {
126 std::cout << "Storing:" << ccdbPath << " tmin:" << tmin << " tmax:" << tmax << " ts:" << tt << std::endl;
127 ret = api.storeAsTFileAny(&(vect), ccdbPath, metadata, tmin, tmax);
128 }
129 }
130 //
131 if (vm["output-file"].as<std::string>() != "none") {
132 std::string file = vm["output-dir"].as<std::string>() + vm["output-file"].as<std::string>();
133 TFile* f = TFile::Open(file.c_str(), "RECREATE");
134 if (f == nullptr) {
135 std::cout << "Error: File" << file << " could not be open for writing !!!" << std::endl;
136 ret++;
137 } else {
138 std::cout << "File" << file << " being writen." << std::endl;
139 f->WriteObject(&vect, "ccdb_object");
140 f->Close();
141 }
142 } else {
143 std::cout << "No file created" << std::endl;
144 }
145 return ret;
146}
std::string ccdbPath(const std::string badChannelType)
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
GLdouble f
Definition glcorearb.h:310
std::string to_string(gsl::span< T, Size > span)
Definition common.h:52
#define main