Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:01:55

0001 #include "CondCore/Utilities/interface/PayloadInspector.h"
0002 #include "CondCore/CondDB/interface/ConnectionPool.h"
0003 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0004 #include "FWCore/ServiceRegistry/interface/ServiceRegistry.h"
0005 
0006 #include <sstream>
0007 #include <iostream>
0008 
0009 namespace cond {
0010 
0011   namespace payloadInspector {
0012 
0013     PlotAnnotations::PlotAnnotations() : m() {}
0014 
0015     std::string PlotAnnotations::get(const std::string& key) const {
0016       std::string ret("");
0017       auto im = m.find(key);
0018       if (im != m.end())
0019         ret = im->second;
0020       return ret;
0021     }
0022 
0023     constexpr const char* const ModuleVersion::label;
0024 
0025     PlotBase::PlotBase()
0026         : m_plotAnnotations(),
0027           m_inputParams(),
0028           m_tagNames(),
0029           m_tagBoundaries(),
0030           m_tagIovs(),
0031           m_inputParamValues(),
0032           m_data("") {}
0033 
0034     void PlotBase::addInputParam(const std::string& paramName) {
0035       // maybe add a check for existing params - returning an Exception when found...
0036       m_inputParams.insert(paramName);
0037     }
0038 
0039     std::string PlotBase::payloadType() const { return m_plotAnnotations.get(PlotAnnotations::PAYLOAD_TYPE_K); }
0040 
0041     std::string PlotBase::title() const { return m_plotAnnotations.get(PlotAnnotations::TITLE_K); }
0042 
0043     std::string PlotBase::type() const { return m_plotAnnotations.get(PlotAnnotations::PLOT_TYPE_K); }
0044 
0045     bool PlotBase::isSingleIov() const { return m_plotAnnotations.singleIov; }
0046 
0047     unsigned int PlotBase::ntags() const { return m_plotAnnotations.ntags; }
0048 
0049     bool PlotBase::isTwoTags() const { return m_plotAnnotations.ntags == 2; }
0050 
0051     py::list PlotBase::inputParams() const {
0052       py::list tmp;
0053       for (const auto& ip : m_inputParams) {
0054         tmp.append(ip);
0055       }
0056       return tmp;
0057     }
0058 
0059     void PlotBase::setInputParamValues(const py::dict& values) {
0060       for (auto item : values) {
0061         std::string k = item.first.cast<std::string>();
0062         std::string v = item.second.cast<std::string>();
0063         if (m_inputParams.find(k) != m_inputParams.end()) {
0064           m_inputParamValues.insert(std::make_pair(k, v));
0065         }
0066       }
0067     }
0068 
0069     std::string PlotBase::data() const { return m_data; }
0070 
0071     bool PlotBase::process(const std::string& connectionString, const py::list& tagsWithTimeBoundaries) {
0072       size_t nt = py::len(tagsWithTimeBoundaries);
0073       bool ret = false;
0074       if (nt) {
0075         std::vector<std::tuple<std::string, cond::Time_t, cond::Time_t> > tags;
0076         tags.resize(nt);
0077         for (size_t i = 0; i < nt; i++) {
0078           py::tuple entry = tagsWithTimeBoundaries[i].cast<py::tuple>();
0079           std::string tagName = entry[0].cast<std::string>();
0080           std::string time0s = entry[1].cast<std::string>();
0081           std::string time1s = entry[2].cast<std::string>();
0082           cond::Time_t time0 = std::stoull(time0s);
0083           cond::Time_t time1 = std::stoull(time1s);
0084           tags[i] = std::make_tuple(tagName, time0, time1);
0085         }
0086         ret = exec_process(connectionString, tags);
0087       }
0088       return ret;
0089     }
0090 
0091     bool PlotBase::exec_process(
0092         const std::string& connectionString,
0093         const std::vector<std::tuple<std::string, cond::Time_t, cond::Time_t> >& tagsWithTimeBoundaries) {
0094       m_tagNames.clear();
0095       m_tagBoundaries.clear();
0096       m_tagIovs.clear();
0097       init();
0098 
0099       std::vector<edm::ParameterSet> psets;
0100       edm::ParameterSet pSet;
0101       pSet.addParameter("@service_type", std::string("SiteLocalConfigService"));
0102       psets.push_back(pSet);
0103       static const edm::ServiceToken services(edm::ServiceRegistry::createSet(psets));
0104       const edm::ServiceRegistry::Operate operate(services);
0105       bool ret = false;
0106       size_t nt = tagsWithTimeBoundaries.size();
0107       if (nt) {
0108         cond::persistency::ConnectionPool connection;
0109         m_dbSession = connection.createSession(connectionString);
0110         m_dbSession.transaction().start();
0111         m_tagNames.resize(nt);
0112         m_tagBoundaries.resize(nt);
0113         m_tagIovs.resize(nt);
0114         for (size_t i = 0; i < nt; i++) {
0115           const std::string& tagName = std::get<0>(tagsWithTimeBoundaries[i]);
0116           cond::Time_t time0 = std::get<1>(tagsWithTimeBoundaries[i]);
0117           cond::Time_t time1 = std::get<2>(tagsWithTimeBoundaries[i]);
0118           m_tagNames[i] = tagName;
0119           m_tagBoundaries[i] = std::make_pair(time0, time1);
0120           auto proxy = m_dbSession.readIov(tagName);
0121           proxy.selectRange(time0, time1, m_tagIovs[i]);
0122         }
0123         m_data = processData();
0124         m_dbSession.transaction().commit();
0125         ret = true;
0126       }
0127       return ret;
0128     }
0129 
0130     void PlotBase::init() {}
0131 
0132     std::string PlotBase::processData() { return ""; }
0133 
0134     cond::Tag_t PlotBase::getTagInfo(const std::string& tag) {
0135       cond::Tag_t info = m_dbSession.readIov(tag).tagInfo();
0136       return info;
0137     }
0138 
0139     const std::map<std::string, std::string>& PlotBase::inputParamValues() const { return m_inputParamValues; }
0140 
0141     cond::persistency::Session PlotBase::dbSession() { return m_dbSession; }
0142 
0143   }  // namespace payloadInspector
0144 
0145 }  // namespace cond