File indexing completed on 2024-04-06 12:03:20
0001 #include "CondTools/RunInfo/interface/RunInfoUpdate.h"
0002 #include "CondCore/CondDB/interface/Session.h"
0003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0004
0005 namespace {
0006 boost::posix_time::ptime parseTimeFromIsoString(const std::string& isoString) {
0007 boost::posix_time::time_input_facet* tif = new boost::posix_time::time_input_facet;
0008 tif->set_iso_extended_format();
0009 std::istringstream iss(isoString);
0010 iss.imbue(std::locale(std::locale::classic(), tif));
0011 boost::posix_time::ptime ret;
0012 iss >> ret;
0013 return ret;
0014 }
0015
0016 void getRunTimeParams(const RunInfo& runInfo, boost::posix_time::ptime& start, boost::posix_time::ptime& end) {
0017 std::string startStr = runInfo.m_start_time_str;
0018 if (startStr != "null") {
0019 start = parseTimeFromIsoString(startStr);
0020 }
0021 end = start;
0022 std::string stopStr = runInfo.m_stop_time_str;
0023 if (stopStr != "null") {
0024 end = parseTimeFromIsoString(stopStr);
0025 }
0026 }
0027 }
0028
0029 RunInfoUpdate::RunInfoUpdate(cond::persistency::Session& dbSession) : m_dbSession(dbSession) {}
0030
0031 RunInfoUpdate::~RunInfoUpdate() {}
0032
0033 void RunInfoUpdate::appendNewRun(const RunInfo& runInfo) {
0034 cond::persistency::RunInfoEditor runInfoWriter = m_dbSession.editRunInfo();
0035 boost::posix_time::ptime start;
0036 boost::posix_time::ptime end;
0037 getRunTimeParams(runInfo, start, end);
0038 edm::LogInfo("RunInfoUpdate") << "[RunInfoUpdate::" << __func__ << "]: Checking run " << runInfo.m_run
0039 << " for insertion in Condition DB" << std::endl;
0040 runInfoWriter.insertNew(runInfo.m_run, start, end);
0041 size_t newRuns = runInfoWriter.flush();
0042 edm::LogInfo("RunInfoUpdate") << "[RunInfoUpdate::" << __func__ << "]: " << newRuns << " new run(s) inserted."
0043 << std::endl;
0044 }
0045
0046
0047 size_t RunInfoUpdate::import(size_t maxEntries,
0048 const std::string& sourceTag,
0049 cond::persistency::Session& sourceSession) {
0050 cond::persistency::RunInfoEditor editor;
0051 std::cout << "# Loading tag " << sourceTag << "..." << std::endl;
0052 cond::persistency::IOVProxy runInfoTag = sourceSession.readIov(sourceTag);
0053 auto iovs = runInfoTag.selectAll();
0054 editor = m_dbSession.editRunInfo();
0055 cond::Time_t lastRun = editor.getLastInserted();
0056 std::cout << "# Last run found in RunInfo db : " << lastRun << std::endl;
0057 auto it = iovs.begin();
0058 if (lastRun > 0) {
0059 it = iovs.find(lastRun + 1);
0060 }
0061 if (it == iovs.end() || (*it).since == lastRun) {
0062 std::cout << "# No more run found to be imported." << std::endl;
0063 return 0;
0064 }
0065 size_t n_entries = 0;
0066 while (it != iovs.end() && n_entries <= maxEntries) {
0067 auto h = (*it).payloadId;
0068 std::shared_ptr<RunInfo> runInfo = sourceSession.fetchPayload<RunInfo>(h);
0069 if (runInfo->m_run != -1) {
0070 n_entries++;
0071 std::cout << "# Inserting run #" << runInfo->m_run << " (from since=" << (*it).since << ")" << std::endl;
0072 boost::posix_time::ptime start;
0073 boost::posix_time::ptime end;
0074 getRunTimeParams(*runInfo, start, end);
0075 editor.insert(runInfo->m_run, start, end);
0076 } else {
0077 std::cout << "# Skipping fake run #" << std::endl;
0078 }
0079 it++;
0080 }
0081 editor.flush();
0082 return n_entries;
0083 }