11 #include <framework/database/TestingPayloadStorage.h>
12 #include <framework/dataobjects/EventMetaData.h>
13 #include <framework/logging/Logger.h>
14 #include <framework/utilities/FileSystem.h>
15 #include <framework/utilities/ScopeGuard.h>
17 #include <TDirectory.h>
21 #include <boost/filesystem.hpp>
22 #include <boost/algorithm/string.hpp>
24 namespace fs = boost::filesystem;
26 namespace Belle2::Conditions {
28 m_filename{
filename}, m_absoluteFilename{fs::absolute(m_filename).string()},
29 m_payloadDir{fs::path(m_absoluteFilename).parent_path().string()}
42 for (
const auto& [revision, iov] : it->second) {
43 if (iov.contains(event) and info.revision < revision) {
44 info.revision = revision;
51 B2FATAL(
"Could not find payload file specified in testing payload storage" <<
LogVar(
"storage filen",
m_filename)
52 <<
LogVar(
"name", info.name) <<
LogVar(
"local revision", info.revision)
53 <<
LogVar(
"payload filename", info.filename));
60 B2WARNING(
"Conditions: Temporary testing payload used for processing" <<
LogVar(
"storage file",
m_filename)
61 <<
LogVar(
"name", info.name) <<
LogVar(
"revision", info.revision) <<
LogVar(
"iov", info.iov));
71 B2WARNING(
"Given testing payload storage file doesn't exist or is not a regular file" <<
LogVar(
"storage file",
m_filename));
76 if (!file.is_open()) {
77 B2FATAL(
"Opening of testing payload storage file failed" <<
LogVar(
"storage file",
m_filename) <<
LogVar(
"error", strerror(errno)));
84 std::getline(file, line);
87 size_t commentChar = line.find(
'#');
88 if (commentChar != std::string::npos) {
89 line = line.substr(0, commentChar);
92 boost::algorithm::trim(line);
94 if (line.empty())
continue;
97 std::string revisionStr;
98 IntervalOfValidity iov;
100 std::stringstream(line) >> name >> revisionStr >> iov;
101 }
catch (std::runtime_error& e) {
102 throw std::runtime_error(
"line must be of the form 'dbstore/<payloadname> <revision> <firstExp>,<firstRun>,<finalExp>,<finalRun>'");
106 revision = stoi(revisionStr);
107 }
catch (std::invalid_argument& e) {
108 throw std::runtime_error(
"revision must be an integer");
111 size_t pos = name.find(
'/');
112 if (pos == std::string::npos) {
113 throw std::runtime_error(
"payload name must be of the form dbstore/<payloadname>");
115 std::string module = name.substr(pos + 1, name.length());
118 revision) <<
LogVar(
"iov", iov));
119 m_payloads[module].emplace_back(revision, iov);
121 }
catch (std::exception& e) {
122 B2FATAL(
"Problem reading testing payloads storage" <<
LogVar(
"storage file",
m_filename)
130 std::stringstream result;
131 if (!path.empty()) result << path <<
'/';
132 result <<
"dbstore_" << name <<
"_rev_" << revision <<
".root";
138 return store(name, iov, [
this, &
object, &name](
const std::string & filename) {
146 boost::filesystem::path resolved = boost::filesystem::canonical(fileName);
147 if (not boost::filesystem::is_regular_file(resolved)) {
148 B2ERROR(
"Problem creating testing payload: Given payload storage file doesn't exist" <<
LogVar(
"storage file", fileName));
151 return store(name, iov, [&resolved](
const std::string & destination) {
153 boost::filesystem::copy(resolved, destination);
159 const std::function<
bool(
const std::string&)>& writer)
162 B2ERROR(
"IoV is empty, refusing to store object in testing payload storage"
163 "Please provide a valid experiment/run range for the data, for example "
164 "using IntervalOfValidity::always() to store data which is always valid"
175 B2ERROR(
"Locking of testing payload storage file failed, cannot create payload"
180 if (!file.is_open()) {
181 B2ERROR(
"Could not open testing payload storage file for writing" <<
LogVar(
"storage file",
m_filename));
185 for (
int revision = 1; revision < INT_MAX; ++revision) {
199 if (!writer(filename))
return false;
201 file <<
"dbstore/" << name <<
" " << revision <<
" " << iov << std::endl;
203 <<
LogVar(
"local revision", revision) <<
LogVar(
"iov", iov));
208 B2ERROR(
"Could not find a suitable revision to create payload" <<
LogVar(
"storage file",
m_filename) <<
LogVar(
"name", name));
215 TDirectory::TContext saveDir;
217 std::unique_ptr<TFile> file{TFile::Open((fileName +
"?reproducible=PayloadFile").c_str(),
"RECREATE")};
218 if (!file || !file->IsOpen()) {
219 B2ERROR(
"Could not open payload file for writing." <<
LogVar(
"filename",
m_filename));
223 object->Write(name.c_str(), TObject::kSingleKey);