Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:08

0001 #include <stdexcept>
0002 #include "OnlineDB/Oracle/interface/Oracle.h"
0003 
0004 #include "OnlineDB/EcalCondDB/interface/MonRunIOV.h"
0005 #include "OnlineDB/EcalCondDB/interface/MonRunTag.h"
0006 #include "OnlineDB/EcalCondDB/interface/RunIOV.h"
0007 #include "OnlineDB/EcalCondDB/interface/Tm.h"
0008 #include "OnlineDB/EcalCondDB/interface/DateHandler.h"
0009 
0010 using namespace std;
0011 using namespace oracle::occi;
0012 
0013 MonRunIOV::MonRunIOV() {
0014   m_conn = nullptr;
0015   m_ID = 0;
0016   m_monRunTag = MonRunTag();
0017   m_runIOV = RunIOV();
0018   m_subRunNum = 0;
0019   m_subRunStart = Tm();
0020   m_subRunEnd = Tm();
0021 }
0022 
0023 MonRunIOV::~MonRunIOV() {}
0024 
0025 void MonRunIOV::setID(int id) { m_ID = id; }
0026 
0027 void MonRunIOV::setMonRunTag(const MonRunTag& tag) {
0028   if (tag != m_monRunTag) {
0029     m_ID = 0;
0030     m_monRunTag = tag;
0031   }
0032 }
0033 
0034 MonRunTag MonRunIOV::getMonRunTag() const { return m_monRunTag; }
0035 
0036 void MonRunIOV::setRunIOV(const RunIOV& iov) {
0037   if (iov != m_runIOV) {
0038     m_ID = 0;
0039     m_runIOV = iov;
0040   }
0041 }
0042 
0043 RunIOV MonRunIOV::getRunIOV() { return m_runIOV; }
0044 
0045 void MonRunIOV::setSubRunNumber(subrun_t subrun) {
0046   if (subrun != m_subRunNum) {
0047     m_ID = 0;
0048     m_subRunNum = subrun;
0049   }
0050 }
0051 
0052 run_t MonRunIOV::getSubRunNumber() const { return m_subRunNum; }
0053 
0054 void MonRunIOV::setSubRunStart(const Tm& start) {
0055   if (start != m_subRunStart) {
0056     m_ID = 0;
0057     m_subRunStart = start;
0058   }
0059 }
0060 
0061 Tm MonRunIOV::getSubRunStart() const { return m_subRunStart; }
0062 
0063 void MonRunIOV::setSubRunEnd(const Tm& end) {
0064   if (end != m_subRunEnd) {
0065     m_ID = 0;
0066     m_subRunEnd = end;
0067   }
0068 }
0069 
0070 Tm MonRunIOV::getSubRunEnd() const { return m_subRunEnd; }
0071 
0072 int MonRunIOV::fetchID() noexcept(false) {
0073   // Return from memory if available
0074   if (m_ID) {
0075     return m_ID;
0076   }
0077 
0078   this->checkConnection();
0079 
0080   // fetch the parent IDs
0081   int monRunTagID, runIOVID;
0082   this->fetchParentIDs(&monRunTagID, &runIOVID);
0083 
0084   if (!monRunTagID || !runIOVID) {
0085     return 0;
0086   }
0087 
0088   DateHandler dh(m_env, m_conn);
0089 
0090   if (m_subRunEnd.isNull()) {
0091     m_subRunEnd = dh.getPlusInfTm();
0092   }
0093 
0094   try {
0095     Statement* stmt = m_conn->createStatement();
0096     stmt->setSQL(
0097         "SELECT iov_id FROM mon_run_iov "
0098         "WHERE tag_id = :1 AND "
0099         "run_iov_id   = :2 AND "
0100         "subrun_num   = :3 AND "
0101         "subrun_start = :4 AND "
0102         "subrun_end   = :5");
0103     stmt->setInt(1, monRunTagID);
0104     stmt->setInt(2, runIOVID);
0105     stmt->setInt(3, m_subRunNum);
0106     stmt->setDate(4, dh.tmToDate(m_subRunStart));
0107     stmt->setDate(5, dh.tmToDate(m_subRunEnd));
0108 
0109     ResultSet* rset = stmt->executeQuery();
0110 
0111     if (rset->next()) {
0112       m_ID = rset->getInt(1);
0113     } else {
0114       m_ID = 0;
0115     }
0116     m_conn->terminateStatement(stmt);
0117   } catch (SQLException& e) {
0118     throw(std::runtime_error("MonRunIOV::fetchID:  " + e.getMessage()));
0119   }
0120 
0121   return m_ID;
0122 }
0123 
0124 void MonRunIOV::setByID(int id) noexcept(false) {
0125   this->checkConnection();
0126 
0127   DateHandler dh(m_env, m_conn);
0128 
0129   try {
0130     Statement* stmt = m_conn->createStatement();
0131 
0132     stmt->setSQL("SELECT tag_id, run_iov_id, subrun_num, subrun_start, subrun_end FROM mon_run_iov WHERE iov_id = :1");
0133     stmt->setInt(1, id);
0134 
0135     ResultSet* rset = stmt->executeQuery();
0136     if (rset->next()) {
0137       int monRunTagID = rset->getInt(1);
0138       int runIOVID = rset->getInt(2);
0139       m_subRunNum = rset->getInt(3);
0140       Date startDate = rset->getDate(4);
0141       Date endDate = rset->getDate(5);
0142 
0143       m_subRunStart = dh.dateToTm(startDate);
0144       m_subRunEnd = dh.dateToTm(endDate);
0145 
0146       m_monRunTag.setConnection(m_env, m_conn);
0147       m_monRunTag.setByID(monRunTagID);
0148 
0149       m_runIOV.setConnection(m_env, m_conn);
0150       m_runIOV.setByID(runIOVID);
0151 
0152       m_ID = id;
0153     } else {
0154       throw(std::runtime_error("MonRunIOV::setByID:  Given tag_id is not in the database"));
0155     }
0156 
0157     m_conn->terminateStatement(stmt);
0158   } catch (SQLException& e) {
0159     throw(std::runtime_error("MonRunIOV::setByID:  " + e.getMessage()));
0160   }
0161 }
0162 
0163 int MonRunIOV::writeDB() noexcept(false) {
0164   this->checkConnection();
0165 
0166   // Check if this IOV has already been written
0167   if (this->fetchID()) {
0168     return m_ID;
0169   }
0170 
0171   // fetch Parent IDs
0172   int monRunTagID, runIOVID;
0173   this->fetchParentIDs(&monRunTagID, &runIOVID);
0174 
0175   if (!monRunTagID) {
0176     monRunTagID = m_monRunTag.writeDB();
0177   }
0178 
0179   // Validate the data, use infinity-till convention
0180   DateHandler dh(m_env, m_conn);
0181 
0182   if (m_subRunStart.isNull()) {
0183     throw(std::runtime_error("MonRunIOV::writeDB:  Must setSubRunStart before writing"));
0184   }
0185 
0186   if (m_subRunEnd.isNull()) {
0187     m_subRunEnd = dh.getPlusInfTm();
0188   }
0189 
0190   try {
0191     Statement* stmt = m_conn->createStatement();
0192 
0193     stmt->setSQL(
0194         "INSERT INTO mon_run_iov (iov_id, tag_id, run_iov_id, subrun_num, subrun_start, subrun_end) "
0195         "VALUES (mon_run_iov_sq.NextVal, :1, :2, :3, :4, :5)");
0196     stmt->setInt(1, monRunTagID);
0197     stmt->setInt(2, runIOVID);
0198     stmt->setInt(3, m_subRunNum);
0199     stmt->setDate(4, dh.tmToDate(m_subRunStart));
0200     stmt->setDate(5, dh.tmToDate(m_subRunEnd));
0201 
0202     stmt->executeUpdate();
0203 
0204     m_conn->terminateStatement(stmt);
0205   } catch (SQLException& e) {
0206     throw(std::runtime_error("MonRunIOV::writeDB:  " + e.getMessage()));
0207   }
0208 
0209   // Now get the ID
0210   if (!this->fetchID()) {
0211     throw(std::runtime_error("MonRunIOV::writeDB:  Failed to write"));
0212   }
0213 
0214   return m_ID;
0215 }
0216 
0217 void MonRunIOV::fetchParentIDs(int* monRunTagID, int* runIOVID) noexcept(false) {
0218   // get the MonRunTag
0219   m_monRunTag.setConnection(m_env, m_conn);
0220   *monRunTagID = m_monRunTag.fetchID();
0221 
0222   // get the RunIOV
0223   m_runIOV.setConnection(m_env, m_conn);
0224   *runIOVID = m_runIOV.fetchID();
0225 
0226   if (!*runIOVID) {
0227     throw(std::runtime_error("MonRunIOV:  Given RunIOV does not exist in DB"));
0228   }
0229 }
0230 
0231 void MonRunIOV::setByRun(MonRunTag* montag, RunIOV* runiov, subrun_t subrun) noexcept(false) {
0232   this->checkConnection();
0233 
0234   runiov->setConnection(m_env, m_conn);
0235   int runIOVID = runiov->fetchID();
0236 
0237   if (!runIOVID) {
0238     throw(std::runtime_error("MonRunIOV::setByRun:  Given RunIOV does not exist in DB"));
0239   }
0240 
0241   montag->setConnection(m_env, m_conn);
0242   int monTagID = montag->fetchID();
0243 
0244   if (!monTagID) {
0245     throw(std::runtime_error("MonRunIOV::setByRun:  Given MonRunTag does not exist in the DB"));
0246   }
0247 
0248   DateHandler dh(m_env, m_conn);
0249 
0250   try {
0251     Statement* stmt = m_conn->createStatement();
0252 
0253     stmt->setSQL(
0254         "SELECT iov_id, subrun_start, subrun_end FROM mon_run_iov "
0255         "WHERE tag_id = :1 AND run_iov_id = :2 AND subrun_num = :3");
0256     stmt->setInt(1, monTagID);
0257     stmt->setInt(2, runIOVID);
0258     stmt->setInt(3, subrun);
0259 
0260     ResultSet* rset = stmt->executeQuery();
0261     if (rset->next()) {
0262       m_monRunTag = *montag;
0263       m_runIOV = *runiov;
0264       m_subRunNum = subrun;
0265 
0266       m_ID = rset->getInt(1);
0267       Date startDate = rset->getDate(2);
0268       Date endDate = rset->getDate(3);
0269 
0270       m_subRunStart = dh.dateToTm(startDate);
0271       m_subRunEnd = dh.dateToTm(endDate);
0272     } else {
0273       throw(std::runtime_error("MonRunIOV::setByRun:  Given subrun is not in the database"));
0274     }
0275 
0276     m_conn->terminateStatement(stmt);
0277   } catch (SQLException& e) {
0278     throw(std::runtime_error("MonRunIOV::setByRun:  " + e.getMessage()));
0279   }
0280 }