Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include <stdexcept>
0002 #include <sstream>
0003 #include <climits>
0004 #include "OnlineDB/EcalCondDB/interface/LMFSeqDat.h"
0005 #include "OnlineDB/EcalCondDB/interface/DateHandler.h"
0006 
0007 using namespace std;
0008 using namespace oracle::occi;
0009 
0010 LMFSeqDat::LMFSeqDat() : LMFUnique() {
0011   setClassName("LMFSeqDat");
0012 
0013   m_runIOV = RunIOV();
0014   m_intFields["seq_num"] = 0;
0015   Tm t;
0016   t = t.plusInfinity();
0017   m_stringFields["seq_start"] = t.str();
0018   m_stringFields["seq_stop"] = t.str();
0019   m_intFields["vmin"] = 1;
0020   m_intFields["vmax"] = 0;
0021 }
0022 
0023 LMFSeqDat::LMFSeqDat(EcalDBConnection *c) : LMFUnique(c) {
0024   setClassName("LMFSeqDat");
0025 
0026   m_runIOV = RunIOV();
0027   m_intFields["seq_num"] = 0;
0028   Tm t;
0029   t = t.plusInfinity();
0030   m_stringFields["seq_start"] = t.str();
0031   m_stringFields["seq_stop"] = t.str();
0032   m_intFields["vmin"] = 1;
0033   m_intFields["vmax"] = 0;
0034 }
0035 
0036 LMFSeqDat::LMFSeqDat(oracle::occi::Environment *env, oracle::occi::Connection *conn) : LMFUnique(env, conn) {
0037   setClassName("LMFSeqDat");
0038 
0039   m_runIOV = RunIOV();
0040   m_intFields["seq_num"] = 0;
0041   Tm t;
0042   t = t.plusInfinity();
0043   m_stringFields["seq_start"] = t.str();
0044   m_stringFields["seq_stop"] = t.str();
0045   m_intFields["vmin"] = 1;
0046   m_intFields["vmax"] = 0;
0047 }
0048 
0049 LMFSeqDat::~LMFSeqDat() {}
0050 
0051 Tm LMFSeqDat::getSequenceStop() const {
0052   Tm t;
0053   t.setToString(getString("seq_stop"));
0054   return t;
0055 }
0056 
0057 RunIOV LMFSeqDat::getRunIOV() const { return m_runIOV; }
0058 
0059 LMFSeqDat &LMFSeqDat::setRunIOV(const RunIOV &iov) {
0060   if (iov != m_runIOV) {
0061     m_ID = 0;
0062     m_runIOV = iov;
0063   }
0064   return *this;
0065 }
0066 
0067 std::string LMFSeqDat::fetchIdSql(Statement *stmt) {
0068   int runIOVID = m_runIOV.getID();
0069   if (!runIOVID) {
0070     if (m_debug) {
0071       std::cout << m_className << ": RunIOV not set" << endl;
0072     }
0073     return "";
0074   }
0075 
0076   if (m_debug) {
0077     std::cout << "Run IOV ID: " << runIOVID << std::endl;
0078     std::cout << "SEQ #     : " << getSequenceNumber() << std::endl;
0079     std::cout << "Versions  : " << getVmin() << " - " << getVmax() << endl;
0080   }
0081   std::string sql =
0082       "SELECT SEQ_ID FROM CMS_ECAL_LASER_COND.LMF_SEQ_DAT "
0083       "WHERE "
0084       "RUN_IOV_ID   = :1 AND "
0085       "SEQ_NUM      = :2 AND "
0086       "VMIN         = :3 ";
0087   if (getVmax() > 0) {
0088     sql += "AND VMAX = :4";
0089   } else {
0090     sql += "ORDER BY VMAX DESC";
0091   }
0092   stmt->setSQL(sql);
0093   stmt->setInt(1, runIOVID);
0094   stmt->setInt(2, getSequenceNumber());
0095   stmt->setInt(3, getVmin());
0096   if (getVmax() > 0) {
0097     stmt->setInt(4, getVmax());
0098   }
0099   return sql;
0100 }
0101 
0102 std::string LMFSeqDat::setByIDSql(Statement *stmt, int id) {
0103   std::string sql =
0104       "SELECT RUN_IOV_ID, SEQ_NUM, SEQ_START, SEQ_STOP, "
0105       "VMIN, VMAX FROM CMS_ECAL_LASER_COND.LMF_SEQ_DAT WHERE SEQ_ID = :1";
0106   stmt->setSQL(sql);
0107   stmt->setInt(1, id);
0108   return sql;
0109 }
0110 
0111 void LMFSeqDat::getParameters(ResultSet *rset) {
0112   DateHandler dh(m_env, m_conn);
0113   int runIOVID = rset->getInt(1);
0114   setInt("seq_num", rset->getInt(2));
0115   Date startDate = rset->getDate(3);
0116   Date endDate = rset->getDate(4);
0117   setInt("vmin", rset->getInt(5));
0118   setInt("vmax", rset->getInt(6));
0119 
0120   setString("seq_start", dh.dateToTm(startDate).str());
0121   setString("seq_stop", dh.dateToTm(endDate).str());
0122 
0123   m_runIOV.setConnection(m_env, m_conn);
0124   m_runIOV.setByID(runIOVID);
0125 }
0126 
0127 bool LMFSeqDat::isValid() const {
0128   bool ret = true;
0129   if (getSequenceStart().isNull()) {
0130     ret = false;
0131   }
0132   if ((getSequenceStop().str().length() > 0) && (getSequenceStop().microsTime() < getSequenceStart().microsTime())) {
0133     ret = false;
0134   }
0135   if (getSequenceStop() == Tm().plusInfinity()) {
0136     ret = false;
0137   }
0138   return ret;
0139 }
0140 
0141 std::string LMFSeqDat::writeDBSql(Statement *stmt) {
0142   // Validate the data, use infinity-till convention
0143   DateHandler dh(m_env, m_conn);
0144 
0145   if (!isValid()) {
0146     dump();
0147     throw(std::runtime_error("LMFSeqDat::writeDB: not valid"));
0148   }
0149 
0150   if (getSequenceStop().str().length() == 0) {
0151     setSequenceStop(dh.getPlusInfTm());
0152   }
0153   int runIOVID = m_runIOV.getID();
0154   if (runIOVID == 0) {
0155     throw(std::runtime_error("LMFSeqDat::writeDB: RunIOV not set"));
0156   }
0157   std::string sp = sequencePostfix(getSequenceStart());
0158   std::string sql =
0159       "INSERT INTO LMF_SEQ_DAT (SEQ_ID, RUN_IOV_ID, SEQ_NUM, "
0160       "SEQ_START, SEQ_STOP, VMIN, VMAX) "
0161       "VALUES (SEQ_ID_" +
0162       sp + "_SQ.NextVal, :1, :2, :3, :4, :5, :6)";
0163   cout << sql << endl;
0164   stmt->setSQL(sql);
0165   stmt->setInt(1, runIOVID);
0166   stmt->setInt(2, getSequenceNumber());
0167   stmt->setDate(3, dh.tmToDate(getSequenceStart()));
0168   stmt->setDate(4, dh.tmToDate(getSequenceStop()));
0169   stmt->setInt(5, getVmin());
0170   stmt->setInt(6, getVmax());
0171   return sql;
0172 }
0173 
0174 void LMFSeqDat::fetchParentIDs() noexcept(false) {
0175   // get the RunIOV
0176   m_runIOV.setConnection(m_env, m_conn);
0177   int runIOVID = m_runIOV.getID();
0178   m_runIOV.setByID(runIOVID);
0179 
0180   if (m_runIOV.getID() == 0) {
0181     throw(std::runtime_error("LMFSeqDat:  Given RunIOV does not exist in DB"));
0182   }
0183 }
0184 
0185 std::map<int, LMFSeqDat> LMFSeqDat::fetchByRunIOV(std::string sql, std::string method) noexcept(false) {
0186   std::vector<std::string> pars;
0187   return fetchByRunIOV(pars, sql, method);
0188 }
0189 
0190 std::map<int, LMFSeqDat> LMFSeqDat::fetchByRunIOV(int par, std::string sql, std::string method) noexcept(false) {
0191   std::vector<std::string> pars;
0192   std::stringstream ss;
0193   ss << "I" << par;
0194   pars.push_back(ss.str());
0195   return fetchByRunIOV(pars, sql, method);
0196 }
0197 
0198 std::map<int, LMFSeqDat> LMFSeqDat::fetchByRunIOV(const std::vector<std::string> &pars,
0199                                                   std::string sql,
0200                                                   std::string method) noexcept(false) {
0201   std::map<int, LMFSeqDat> l;
0202   this->checkConnection();
0203   try {
0204     Statement *stmt = m_conn->createStatement();
0205     stmt->setSQL(sql);
0206     for (unsigned int i = 0; i < pars.size(); i++) {
0207       if (pars[i][0] == 'I') {
0208         stmt->setInt(i + 1, atoi(pars[i].c_str() + 1));
0209       } else if (pars[i][0] == 'S') {
0210         stmt->setString(i + 1, pars[i].c_str() + 1);
0211       } else {
0212         throw(std::runtime_error(m_className + "::" + method + ": " + "Invalid type"));
0213       }
0214     }
0215     ResultSet *rset = stmt->executeQuery();
0216     while (rset->next() != 0) {
0217       int seq_id = rset->getInt(1);
0218       LMFSeqDat s;
0219       s.setConnection(m_env, m_conn);
0220       s.setByID(seq_id);
0221       l[s.getSequenceNumber()] = s;
0222     }
0223     m_conn->terminateStatement(stmt);
0224   } catch (SQLException &e) {
0225     throw(std::runtime_error(m_className + "::" + method + ": " + e.getMessage()));
0226   }
0227   return l;
0228 }
0229 
0230 LMFSeqDat LMFSeqDat::fetchLast() {
0231   LMFSeqDat ret;
0232   std::map<int, LMFSeqDat> m = fetchByRunIOV(
0233       "SELECT SEQ_ID FROM CMS_ECAL_LASER_COND.LMF_SEQ_DAT "
0234       "WHERE SEQ_ID = "
0235       "(SELECT MAX(SEQ_ID) FROM CMS_ECAL_LASER_COND.LMF_SEQ_DAT)",
0236       "fetchLast");
0237   if (!m.empty()) {
0238     ret = m.begin()->second;
0239   }
0240   return ret;
0241 }
0242 
0243 RunIOV LMFSeqDat::fetchLastRun() { return fetchLast().getRunIOV(); }
0244 
0245 std::map<int, LMFSeqDat> LMFSeqDat::fetchByRunIOV(RunIOV &iov) {
0246   int runIOVID = iov.getID();
0247   return fetchByRunIOV(runIOVID,
0248                        "SELECT SEQ_ID FROM CMS_ECAL_LASER_COND.LMF_SEQ_DAT "
0249                        "WHERE RUN_IOV_ID = :1",
0250                        "fetchByRunIOV");
0251 }
0252 
0253 std::map<int, LMFSeqDat> LMFSeqDat::fetchByRunIOV(RunIOV &iov, const LMFColor &col) {
0254   int runIOVID = iov.getID();
0255   int colorId = col.getID();
0256   std::vector<std::string> pars;
0257   std::stringstream ss;
0258   ss << "I" << runIOVID;
0259   pars.push_back(ss.str());
0260   ss.str(std::string());
0261   ss << "I" << colorId;
0262   pars.push_back(ss.str());
0263   return fetchByRunIOV(pars,
0264                        "SELECT S.SEQ_ID FROM CMS_ECAL_LASER_COND.LMF_SEQ_DAT "
0265                        "S JOIN CMS_ECAL_LASER_COND.LMF_RUN_IOV R"
0266                        " ON S.SEQ_ID = R.SEQ_ID WHERE RUN_IOV_ID = :1 AND "
0267                        " COLOR_ID = :2",
0268                        "fetchByRunIOVAndColor");
0269 }
0270 
0271 std::map<int, LMFSeqDat> LMFSeqDat::fetchByRunNumber(int runno) {
0272   return fetchByRunIOV(runno,
0273                        "SELECT SEQ_ID FROM CMS_ECAL_LASER_COND.LMF_SEQ_DAT D "
0274                        "JOIN CMS_ECAL_COND.RUN_IOV R ON "
0275                        "D.RUN_IOV_ID = R.IOV_ID WHERE RUN_NUM = :1",
0276                        "fetchByRunNumber");
0277 }
0278 
0279 LMFSeqDat LMFSeqDat::fetchByRunNumber(int runno, const Tm &taken_at) { return fetchByRunNumber(runno, taken_at.str()); }
0280 
0281 LMFSeqDat LMFSeqDat::fetchByRunNumber(int runno, std::string taken_at) {
0282   std::map<int, LMFSeqDat> l;
0283   std::vector<std::string> pars;
0284   std::stringstream ss;
0285   ss << "I" << runno;
0286   pars.push_back(ss.str());
0287   ss.str(std::string());
0288   ss << "S" << taken_at;
0289   pars.push_back(ss.str());
0290   std::string q =
0291       "SELECT SEQ_ID FROM CMS_ECAL_LASER_COND.LMF_SEQ_DAT D "
0292       "JOIN CMS_ECAL_COND.RUN_IOV R ON "
0293       "D.RUN_IOV_ID = R.IOV_ID WHERE RUN_NUM = :1 AND "
0294       "SEQ_START >= TO_DATE(:2, 'YYYY-MM-DD HH24:MI:SS') "
0295       "AND SEQ_STOP <= TO_DATE(:2, 'YYYY-MM-DD HH24:MI:SS')";
0296   l = fetchByRunIOV(pars, q, "fetchByRunNumberAt");
0297   LMFSeqDat ret;
0298   if (l.size() == 1) {
0299     std::map<int, LMFSeqDat>::const_iterator x = l.begin();
0300     ret = x->second;
0301   } else if (l.size() > 1) {
0302     std::cout << "WARNING: Your query returned more than one result. " << std::endl;
0303     std::cout << "         This was not expected. Please check the DB!!!" << std::endl;
0304     std::cout << "Your query: " << std::endl << q << std::endl;
0305     std::cout << "Your parameters: " << std::endl;
0306     for (unsigned int i = 0; i < pars.size(); i++) {
0307       std::cout << i << ": " << pars[i] << std::endl;
0308     }
0309   }
0310   return ret;
0311 }