Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:14:49

0001 #include <memory>
0002 
0003 #include "CalibTracker/SiStripDCS/interface/SiStripCoralIface.h"
0004 #include "CondCore/CondDB/interface/ConnectionPool.h"
0005 #include "RelationalAccess/ISessionProxy.h"
0006 #include "RelationalAccess/ISchema.h"
0007 #include "RelationalAccess/ITable.h"
0008 #include "RelationalAccess/TableDescription.h"
0009 #include "RelationalAccess/ITablePrivilegeManager.h"
0010 #include "RelationalAccess/ICursor.h"
0011 #include "RelationalAccess/IQuery.h"
0012 #include "RelationalAccess/ITableDataEditor.h"
0013 #include "CoralBase/Exception.h"
0014 #include "CoralBase/AttributeList.h"
0015 #include "CoralBase/AttributeSpecification.h"
0016 #include "CoralBase/Attribute.h"
0017 
0018 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0019 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
0020 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0021 
0022 // constructor
0023 SiStripCoralIface::SiStripCoralIface(std::string connectionString, std::string authenticationPath, const bool debug)
0024     : m_connectionString(connectionString), m_authPath(authenticationPath), m_session(), debug_(debug) {
0025   std::cout << "Building coral interface" << std::endl;
0026   initialize();
0027 }
0028 
0029 // destructor
0030 SiStripCoralIface::~SiStripCoralIface() {
0031   LogTrace("SiStripCoralIface") << "[SiStripCoralIface::" << __func__ << "] Destructor called.";
0032   // m_connection.close();
0033 }
0034 
0035 // open DB connection
0036 void SiStripCoralIface::initialize() {
0037   cond::persistency::ConnectionPool connection;
0038   connection.setAuthenticationPath(m_authPath);
0039   connection.configure();
0040   m_session = connection.createSession(m_connectionString);
0041   try {
0042     m_transaction = std::make_unique<cond::persistency::TransactionScope>(m_session.transaction());
0043     m_transaction->start(true);
0044     LogTrace("SiStripCoralIface") << "[SiStripCoralIface::" << __func__ << "] Database connection opened";
0045   } catch (...) {
0046     edm::LogError("SiStripCoralIface") << "Database connection failed";
0047     exit(1);
0048   }
0049 }
0050 
0051 // access the status change or lastValue tables
0052 void SiStripCoralIface::doQuery(std::string queryType,
0053                                 const coral::TimeStamp& startTime,
0054                                 const coral::TimeStamp& endTime,
0055                                 std::vector<coral::TimeStamp>& vec_changedate,
0056                                 std::vector<float>& vec_actualValue,
0057                                 std::vector<std::string>& vec_dpname) {
0058   std::unique_ptr<coral::IQuery> query(
0059       m_session.coralSession().schema(std::string("CMS_TRK_DCS_PVSS_COND")).newQuery());
0060   std::string condition;
0061 
0062   LogTrace("SiStripCoralIface") << "[SiStripCoralIface::" << __func__ << "] table to be accessed: " << queryType;
0063 
0064   if (queryType == "STATUSCHANGE") {
0065     query->addToOutputList("FWCAENCHANNEL.CHANGE_DATE", "CHANGE_DATE");
0066     query->addToOutputList("FWCAENCHANNEL.ACTUAL_STATUS", "ACTUAL_STATUS");
0067     query->addToOutputList("DP_NAME2ID.DPNAME", "DPNAME");
0068     query->addToOrderList("FWCAENCHANNEL.CHANGE_DATE");
0069     query->addToTableList("FWCAENCHANNEL");
0070     query->addToTableList("DP_NAME2ID");
0071     condition =
0072         "FWCAENCHANNEL.DPID = DP_NAME2ID.ID AND FWCAENCHANNEL.CHANGE_DATE<=:tmax AND FWCAENCHANNEL.ACTUAL_STATUS IS "
0073         "NOT NULL AND FWCAENCHANNEL.CHANGE_DATE >=:tmin AND (DP_NAME2ID.DPNAME like '%easyBoard%')";
0074   } else if (queryType == "LASTVALUE") {
0075     query->addToOutputList("DCSLASTVALUE_VOLTAGE.CHANGE_DATE", "CHANGE_DATE");
0076     query->addToOutputList("DCSLASTVALUE_VOLTAGE.ACTUAL_VMON", "ACTUAL_VMON");
0077     query->addToOutputList("DP_NAME2ID.DPNAME", "DPNAME");
0078     query->addToOrderList("DCSLASTVALUE_VOLTAGE.CHANGE_DATE");
0079     query->addToTableList("DCSLASTVALUE_VOLTAGE");
0080     query->addToTableList("DP_NAME2ID");
0081     condition =
0082         "DCSLASTVALUE_VOLTAGE.DPID = DP_NAME2ID.ID AND DCSLASTVALUE_VOLTAGE.CHANGE_DATE<=:tmax AND "
0083         "DCSLASTVALUE_VOLTAGE.CHANGE_DATE>=:tmin AND DCSLASTVALUE_VOLTAGE.ACTUAL_VMON IS NOT NULL AND "
0084         "(DP_NAME2ID.DPNAME like '%easyBoard%')";
0085   }
0086 
0087   coral::AttributeList conditionData;
0088   conditionData.extend<coral::TimeStamp>("tmax");
0089   conditionData.extend<coral::TimeStamp>("tmin");
0090   query->setCondition(condition, conditionData);
0091   conditionData[0].data<coral::TimeStamp>() = endTime;
0092   conditionData[1].data<coral::TimeStamp>() = startTime;
0093 
0094   query->setMemoryCacheSize(100);
0095   std::cout << "Executing query" << std::endl;
0096   if (debug_)
0097     std::cout << "[SiStripCoralIface::" << __func__ << "] Dumping all query results:" << std::endl;
0098   coral::ICursor& cursor = query->execute();
0099   int numberRow = 0;
0100   while (cursor.next()) {
0101     const coral::AttributeList& row = cursor.currentRow();
0102     //DEBUG
0103     //Output the query results directly to cout,
0104     //Can be subsequently parsed by Python validation code
0105     if (debug_)
0106       row.toOutputStream(std::cout) << std::endl;
0107     numberRow++;
0108     if (queryType == "STATUSCHANGE") {
0109       coral::TimeStamp ts = row["CHANGE_DATE"].data<coral::TimeStamp>();
0110       vec_changedate.push_back(ts);
0111       float as = (float)row["ACTUAL_STATUS"].data<float>();
0112       vec_actualValue.push_back(as);
0113       std::string id_name = (std::string)row["DPNAME"].data<std::string>();
0114       vec_dpname.push_back(id_name);
0115     } else if (queryType == "LASTVALUE") {
0116       coral::TimeStamp ts = row["CHANGE_DATE"].data<coral::TimeStamp>();
0117       vec_changedate.push_back(ts);
0118       float av = (float)row["ACTUAL_VMON"].data<float>();
0119       vec_actualValue.push_back(av);
0120       std::string id_name = (std::string)row["DPNAME"].data<std::string>();
0121       vec_dpname.push_back(id_name);
0122     }
0123   }
0124   cursor.close();
0125   if (debug_)
0126     std::cout << "[SiStripCoralIface::" << __func__ << "] Finished dumping query results, " << numberRow
0127               << " rows were retrieved from PVSS Cond DB (both Pixel and Strip CAEN supplies)" << std::endl;
0128   edm::LogInfo("SiStripCoralIface") << "[SiStripCoralIface::" << __func__ << "] " << numberRow
0129                                     << " rows retrieved from PVSS Cond DB";
0130 }
0131 
0132 // access the channel settings in the status change table
0133 void SiStripCoralIface::doSettingsQuery(const coral::TimeStamp& startTime,
0134                                         const coral::TimeStamp& endTime,
0135                                         std::vector<coral::TimeStamp>& vec_changedate,
0136                                         std::vector<float>& vec_settings,
0137                                         std::vector<std::string>& vec_dpname,
0138                                         std::vector<uint32_t>& vec_dpid) {
0139   std::unique_ptr<coral::IQuery> query(
0140       m_session.coralSession().schema(std::string("CMS_TRK_DCS_PVSS_COND")).newQuery());
0141   query->addToOutputList("FWCAENCHANNEL.CHANGE_DATE", "CHANGE_DATE");
0142   query->addToOutputList("FWCAENCHANNEL.SETTINGS_V0", "VSET");
0143   query->addToOutputList("FWCAENCHANNEL.DPID", "DPID");
0144   query->addToOutputList("DP_NAME2ID.DPNAME", "DPNAME");
0145   query->addToOrderList("FWCAENCHANNEL.CHANGE_DATE");
0146   query->addToTableList("FWCAENCHANNEL");
0147   query->addToTableList("DP_NAME2ID");
0148   std::string condition =
0149       "FWCAENCHANNEL.DPID = DP_NAME2ID.ID AND FWCAENCHANNEL.CHANGE_DATE<=:tmax AND FWCAENCHANNEL.SETTINGS_V0 IS NOT "
0150       "NULL AND FWCAENCHANNEL.CHANGE_DATE >=:tmin AND (DP_NAME2ID.DPNAME like '%easyBoard%')";
0151 
0152   coral::AttributeList conditionData;
0153   conditionData.extend<coral::TimeStamp>("tmax");
0154   conditionData.extend<coral::TimeStamp>("tmin");
0155   query->setCondition(condition, conditionData);
0156   conditionData[0].data<coral::TimeStamp>() = endTime;
0157   conditionData[1].data<coral::TimeStamp>() = startTime;
0158 
0159   query->setMemoryCacheSize(100);
0160   coral::ICursor& cursor = query->execute();
0161   int numberRow = 0;
0162   while (cursor.next()) {
0163     const coral::AttributeList& row = cursor.currentRow();
0164     if (debug_)
0165       row.toOutputStream(std::cout) << std::endl;
0166     numberRow++;
0167     coral::TimeStamp ts = row["CHANGE_DATE"].data<coral::TimeStamp>();
0168     vec_changedate.push_back(ts);
0169     float vs = (float)row["VSET"].data<float>();
0170     vec_settings.push_back(vs);
0171     uint32_t id = (uint32_t)row["DPID"].data<float>();
0172     vec_dpid.push_back(id);
0173     std::string id_name = (std::string)row["DPNAME"].data<std::string>();
0174     vec_dpname.push_back(id_name);
0175   }
0176   cursor.close();
0177   edm::LogInfo("SiStripCoralIface") << "[SiStripCoralIface::" << __func__ << "] " << numberRow
0178                                     << " rows retrieved from PVSS Cond DB";
0179 }
0180 
0181 void SiStripCoralIface::doNameQuery(std::vector<std::string>& vec_dpname, std::vector<uint32_t>& vec_dpid) {
0182   std::unique_ptr<coral::IQuery> query(
0183       m_session.coralSession().schema(std::string("CMS_TRK_DCS_PVSS_COND")).newQuery());
0184   query->addToOutputList("DP_NAME2ID.DPNAME", "DPNAME");
0185   query->addToOutputList("DP_NAME2ID.ID", "DPID");
0186   query->addToTableList("DP_NAME2ID");
0187 
0188   std::string condition = "DP_NAME2ID.DPNAME like '%easyBoard%' ";
0189   query->setCondition(condition, coral::AttributeList());
0190 
0191   query->setMemoryCacheSize(100);
0192   coral::ICursor& cursor = query->execute();
0193   int numberRow = 0;
0194   while (cursor.next()) {
0195     const coral::AttributeList& row = cursor.currentRow();
0196     numberRow++;
0197     uint32_t id = (uint32_t)row["DPID"].data<float>();
0198     vec_dpid.push_back(id);
0199     std::string id_name = (std::string)row["DPNAME"].data<std::string>();
0200     vec_dpname.push_back(id_name);
0201   }
0202   cursor.close();
0203   edm::LogInfo("SiStripCoralIface") << "[SiStripCoralIface::" << __func__ << "] " << numberRow
0204                                     << " rows retrieved from PVSS Cond DB";
0205 }