Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "OnlineDB/EcalCondDB/interface/LMFRunIOV.h"
0002 #include "OnlineDB/EcalCondDB/interface/LMFDefFabric.h"
0003 #include "OnlineDB/EcalCondDB/interface/DateHandler.h"
0004 
0005 #include <ctime>
0006 
0007 using namespace std;
0008 using namespace oracle::occi;
0009 
0010 void LMFRunIOV::initialize() {
0011   Tm tm;
0012   tm.setToCurrentGMTime();
0013 
0014   m_intFields["lmr"] = 0;
0015   m_intFields["tag_id"] = 0;
0016   m_intFields["seq_id"] = 0;
0017   m_intFields["color_id"] = 0;
0018   m_intFields["trigType_id"] = 0;
0019   m_stringFields["subrun_start"] = tm.str();
0020   m_stringFields["subrun_end"] = tm.str();
0021   m_stringFields["db_timestamp"] = tm.str();
0022   m_stringFields["subrun_type"] = "none";
0023   m_className = "LMFRunIOV";
0024 
0025   _fabric = nullptr;
0026 }
0027 
0028 LMFRunIOV::LMFRunIOV() : LMFUnique() { initialize(); }
0029 
0030 LMFRunIOV::LMFRunIOV(oracle::occi::Environment *env, oracle::occi::Connection *conn) : LMFUnique(env, conn) {
0031   initialize();
0032 }
0033 
0034 LMFRunIOV::LMFRunIOV(EcalDBConnection *c) : LMFUnique(c) { initialize(); }
0035 
0036 LMFRunIOV::LMFRunIOV(const LMFRunIOV &r) : LMFUnique::LMFUnique(r) {
0037   initialize();
0038   *this = r;
0039 }
0040 
0041 LMFRunIOV::~LMFRunIOV() {
0042   if (_fabric != nullptr) {
0043     delete _fabric;
0044   }
0045 }
0046 
0047 LMFRunIOV &LMFRunIOV::setLMFRunTag(const LMFRunTag &tag) {
0048   setInt("tag_id", tag.getID());
0049   return *this;
0050 }
0051 
0052 LMFRunIOV &LMFRunIOV::setLMFRunTag(int tag_id) {
0053   setInt("tag_id", tag_id);
0054   return *this;
0055 }
0056 
0057 LMFRunTag LMFRunIOV::getLMFRunTag() const {
0058   LMFRunTag rtag = LMFRunTag(m_env, m_conn);
0059   rtag.setByID(getInt("tag_id"));
0060   return rtag;
0061 }
0062 
0063 LMFRunIOV &LMFRunIOV::setColor(const LMFColor &color) {
0064   setInt("color_id", color.getID());
0065   return *this;
0066 }
0067 
0068 LMFRunIOV &LMFRunIOV::setColor(int color_id) {
0069   setInt("color_id", color_id);
0070   return *this;
0071 }
0072 
0073 void LMFRunIOV::checkFabric() {
0074   if (_fabric == nullptr) {
0075     _fabric = new LMFDefFabric(m_env, m_conn);
0076   }
0077 }
0078 
0079 LMFRunIOV &LMFRunIOV::setColorIndex(int color_index) {
0080   checkFabric();
0081   setInt("color_id", _fabric->getColorID(color_index));
0082   return *this;
0083 }
0084 
0085 LMFRunIOV &LMFRunIOV::setColor(std::string name) {
0086   checkFabric();
0087   setInt("color_id", _fabric->getColorID(name));
0088   return *this;
0089 }
0090 
0091 LMFColor LMFRunIOV::getLMFColor() const {
0092   LMFColor rcol = LMFColor(m_env, m_conn);
0093   rcol.setByID(getInt("color_id"));
0094   return rcol;
0095 }
0096 
0097 std::string LMFRunIOV::getColorShortName() const {
0098   LMFColor rcol = getLMFColor();
0099   return rcol.getShortName();
0100 }
0101 
0102 std::string LMFRunIOV::getColorLongName() const {
0103   LMFColor rcol = getLMFColor();
0104   return rcol.getLongName();
0105 }
0106 
0107 LMFRunIOV &LMFRunIOV::setTriggerType(LMFTrigType &trigType) {
0108   setInt("trigType_id", trigType.getID());
0109   return *this;
0110 }
0111 
0112 LMFRunIOV &LMFRunIOV::setTriggerType(std::string sname) {
0113   checkFabric();
0114   setInt("trigType_id", _fabric->getTrigTypeID(sname));
0115   return *this;
0116 }
0117 
0118 LMFRunIOV &LMFRunIOV::setTriggerType(int id) {
0119   setInt("trigType_id", id);
0120   return *this;
0121 }
0122 
0123 LMFTrigType LMFRunIOV::getTriggerType() const {
0124   LMFTrigType rt = LMFTrigType(m_env, m_conn);
0125   rt.setByID(getInt("trigType_id"));
0126   return rt;
0127 }
0128 
0129 LMFRunIOV &LMFRunIOV::setLmr(int n) {
0130   setInt("lmr", n);
0131   return *this;
0132 }
0133 
0134 int LMFRunIOV::getLmr() const { return getInt("lmr"); }
0135 
0136 LMFRunIOV &LMFRunIOV::setSubRunStart(const Tm &start) {
0137   setString("subrun_start", start.str());
0138   return *this;
0139 }
0140 
0141 Tm LMFRunIOV::getSubRunStart() const {
0142   Tm t;
0143   t.setToString(getString("subrun_start"));
0144   return t;
0145 }
0146 
0147 LMFRunIOV &LMFRunIOV::setSubRunEnd(const Tm &stop) {
0148   setString("subrun_end", stop.str());
0149   return *this;
0150 }
0151 
0152 Tm LMFRunIOV::getSubRunEnd() const {
0153   Tm t;
0154   t.setToString(getString("subrun_end"));
0155   return t;
0156 }
0157 
0158 // XXX: This method is not used within CMSSW
0159 Tm LMFRunIOV::getDBInsertionTime() const {
0160   Tm t;
0161   t.setToString(getString("db_timestamp"));
0162   return t;
0163 }
0164 
0165 LMFRunIOV &LMFRunIOV::setSubRunType(const std::string &s) {
0166   setString("subrun_type", s);
0167   return *this;
0168 }
0169 
0170 std::string LMFRunIOV::getSubRunType() const { return getString("subrun_type"); }
0171 
0172 LMFRunIOV &LMFRunIOV::setSequence(LMFSeqDat &seq) {
0173   LMFSeqDat *seqdat = new LMFSeqDat();
0174   *seqdat = seq;
0175   attach("sequence", seqdat);
0176   setInt("seq_id", seqdat->getID());
0177   return *this;
0178 }
0179 
0180 LMFSeqDat LMFRunIOV::getSequence() const {
0181   LMFSeqDat rs = LMFSeqDat(m_env, m_conn);
0182   rs.setByID(getInt("seq_id"));
0183   return rs;
0184 }
0185 
0186 void LMFRunIOV::dump() const {
0187   LMFUnique::dump();
0188   std::cout << "# Fabric Address: " << _fabric << std::endl;
0189   if (m_debug) {
0190     _fabric->dump();
0191   }
0192 }
0193 
0194 std::string LMFRunIOV::fetchIdSql(Statement *stmt) {
0195   std::string sql = "";
0196 
0197   sql =
0198       "SELECT LMF_IOV_ID FROM CMS_ECAL_LASER_COND.LMF_RUN_IOV WHERE "
0199       "SEQ_ID = :1 "
0200       "AND LMR = :2 ";
0201   if (m_intFields["tag_id"] > 0) {
0202     sql += "AND TAG_ID = :3";
0203   }
0204   stmt->setSQL(sql);
0205   stmt->setInt(1, m_intFields["seq_id"]);
0206   stmt->setInt(2, m_intFields["lmr"]);
0207   if (m_intFields["tag_id"] > 0) {
0208     stmt->setInt(3, m_intFields["tag_id"]);
0209   }
0210   return sql;
0211 }
0212 
0213 std::string LMFRunIOV::setByIDSql(Statement *stmt, int id) {
0214   DateHandler dh(m_env, m_conn);
0215   std::string sql =
0216       "SELECT TAG_ID, SEQ_ID, LMR, COLOR_ID, TRIG_TYPE, "
0217       "SUBRUN_START, SUBRUN_END, SUBRUN_TYPE, DB_TIMESTAMP FROM "
0218       "CMS_ECAL_LASER_COND.LMF_RUN_IOV "
0219       "WHERE LMF_IOV_ID = :1";
0220   stmt->setSQL(sql);
0221   stmt->setInt(1, id);
0222   return sql;
0223 }
0224 
0225 void LMFRunIOV::getParameters(ResultSet *rset) noexcept(false) {
0226   DateHandler dh(m_env, m_conn);
0227   setLMFRunTag(rset->getInt(1));
0228   LMFSeqDat *seq;
0229   if (m_foreignKeys.find("sequence") != m_foreignKeys.end()) {
0230     seq = (LMFSeqDat *)m_foreignKeys["sequence"];
0231     setInt("seq_id", seq->getID());
0232   } else {
0233     seq = new LMFSeqDat;
0234     seq->setConnection(m_env, m_conn);
0235     seq->setByID(rset->getInt(2));
0236     setInt("seq_id", seq->getID());
0237     delete seq;
0238   }
0239   setInt("lmr", rset->getInt(3));
0240   setColor(rset->getInt(4));
0241   setTriggerType(rset->getInt(5));
0242   Date start = rset->getDate(6);
0243   setString("subrun_start", dh.dateToTm(start).str());
0244   Date stop = rset->getDate(7);
0245   setString("subrun_end", dh.dateToTm(stop).str());
0246   setString("subrun_type", rset->getString(8));
0247 #if defined(_GLIBCXX_USE_CXX11_ABI) && (_GLIBCXX_USE_CXX11_ABI == 0)
0248   setString("db_timestamp", rset->getTimestamp(9).toText("YYYY-MM-DD HH24:MI:SS", 0));
0249 #else
0250   int year = 0;
0251   unsigned int month = 0;
0252   unsigned int day = 0;
0253   unsigned int hour = 0;
0254   unsigned int minute = 0;
0255   unsigned int second = 0;
0256   unsigned int fs = 0;
0257   rset->getTimestamp(9).getDate(year, month, day);
0258   rset->getTimestamp(9).getTime(hour, minute, second, fs);
0259   const std::tm tt = {// Different max(second) is defined by C99 and Oracle Timestamp.
0260                       .tm_sec = static_cast<int>(second),
0261                       .tm_min = static_cast<int>(minute),
0262                       .tm_hour = static_cast<int>(hour),
0263                       .tm_mday = static_cast<int>(day),
0264                       .tm_mon = static_cast<int>(month),
0265                       .tm_year = year - 1900,
0266                       .tm_wday = 0,
0267                       .tm_yday = 0,
0268                       .tm_isdst = 0,
0269                       .tm_gmtoff = 0,
0270                       .tm_zone = nullptr};
0271   char tt_str[30] = {0};
0272   if (std::strftime(tt_str, sizeof(tt_str), "%F %T", &tt)) {
0273     setString("db_timestamp", std::string(tt_str));
0274   } else {
0275     throw std::runtime_error(
0276         "LMFRunIOV::getParameters: failed to generate the date string for 'db_timestamp' parameter");
0277   }
0278 #endif
0279 }
0280 
0281 bool LMFRunIOV::isValid() {
0282   bool ret = true;
0283   if (!getLMFRunTag().isValid()) {
0284     ret = false;
0285   }
0286   if (!getSequence().isValid()) {
0287     ret = false;
0288   }
0289   if (!getTriggerType().isValid()) {
0290     ret = false;
0291   }
0292   if ((getLmr() < 0) || (getLmr() > 92)) {
0293     ret = false;
0294   }
0295   if (!getLMFColor().isValid()) {
0296     ret = false;
0297   }
0298   // subrun start and end are by definition valid
0299   return ret;
0300 }
0301 
0302 std::string LMFRunIOV::writeDBSql(Statement *stmt) {
0303   // check that everything has been setup
0304   int tag_id = getInt("tag_id");
0305   int seq_id = getInt("seq_id");
0306   int color_id = getInt("color_id");
0307   int tt = getInt("trigType_id");
0308   std::string sp = sequencePostfix(getSubRunStart());
0309   std::string sql =
0310       "INSERT INTO LMF_RUN_IOV (LMF_IOV_ID, TAG_ID, SEQ_ID, "
0311       "LMR, COLOR_ID, TRIG_TYPE, SUBRUN_START, SUBRUN_END, SUBRUN_TYPE) VALUES "
0312       "(lmf_run_iov_" +
0313       sp + "_sq.NextVal, :1, :2, :3, :4, :5, :6, :7, :8)";
0314   stmt->setSQL(sql);
0315   DateHandler dm(m_env, m_conn);
0316   stmt->setInt(1, tag_id);
0317   stmt->setInt(2, seq_id);
0318   stmt->setInt(3, getInt("lmr"));
0319   stmt->setInt(4, color_id);
0320   stmt->setInt(5, tt);
0321   stmt->setDate(6, dm.tmToDate(getSubRunStart()));
0322   stmt->setDate(7, dm.tmToDate(getSubRunEnd()));
0323   stmt->setString(8, getSubRunType());
0324   return sql;
0325 }
0326 
0327 std::list<LMFRunIOV> LMFRunIOV::fetchBySequence(const vector<int> &par,
0328                                                 const std::string &sql,
0329                                                 const std::string &method) noexcept(false) {
0330   std::list<LMFRunIOV> l;
0331   this->checkConnection();
0332   try {
0333     Statement *stmt = m_conn->createStatement();
0334     stmt->setSQL(sql);
0335     for (unsigned int i = 0; i < par.size(); i++) {
0336       stmt->setInt(i + 1, par[i]);
0337     }
0338     ResultSet *rset = stmt->executeQuery();
0339     while (rset->next() != 0) {
0340       int lmf_iov_id = rset->getInt(1);
0341       LMFRunIOV iov;
0342       iov.setConnection(m_env, m_conn);
0343       iov.setByID(lmf_iov_id);
0344       l.push_back(iov);
0345     }
0346     m_conn->terminateStatement(stmt);
0347   } catch (SQLException &e) {
0348     throw(std::runtime_error(m_className + "::" + method + ": " + e.getMessage()));
0349   }
0350   return l;
0351 }
0352 
0353 std::list<LMFRunIOV> LMFRunIOV::fetchBySequence(const LMFSeqDat &s) {
0354   int seq_id = s.getID();
0355   vector<int> parameters;
0356   parameters.push_back(seq_id);
0357   return fetchBySequence(parameters,
0358                          "SELECT LMF_IOV_ID FROM "
0359                          "CMS_ECAL_LASER_COND.LMF_RUN_IOV "
0360                          "WHERE SEQ_ID = :1",
0361                          "fetchBySequence");
0362 }
0363 
0364 std::list<LMFRunIOV> LMFRunIOV::fetchBySequence(const LMFSeqDat &s, int lmr) {
0365   int seq_id = s.getID();
0366   vector<int> parameters;
0367   parameters.push_back(seq_id);
0368   parameters.push_back(lmr);
0369   return fetchBySequence(parameters,
0370                          "SELECT LMF_IOV_ID FROM "
0371                          "CMS_ECAL_LASER_COND.LMF_RUN_IOV "
0372                          "WHERE SEQ_ID = :1 AND LMR = :2",
0373                          "fetchBySequence");
0374 }
0375 
0376 std::list<LMFRunIOV> LMFRunIOV::fetchBySequence(const LMFSeqDat &s, int lmr, int type, int color) {
0377   int seq_id = s.getID();
0378   vector<int> parameters;
0379   parameters.push_back(seq_id);
0380   parameters.push_back(lmr);
0381   parameters.push_back(color);
0382   parameters.push_back(type);
0383   return fetchBySequence(parameters,
0384                          "SELECT LMF_IOV_ID FROM "
0385                          "CMS_ECAL_LASER_COND.LMF_RUN_IOV "
0386                          "WHERE SEQ_ID = :1 AND LMR = :2 AND COLOR_ID = :3 "
0387                          "AND TRIG_TYPE = :4",
0388                          "fetchBySequence");
0389 }
0390 
0391 std::list<LMFRunIOV> LMFRunIOV::fetchLastBeforeSequence(const LMFSeqDat &s, int lmr, int type, int color) {
0392   int seq_id = s.getID();
0393   vector<int> parameters;
0394   parameters.push_back(seq_id);
0395   parameters.push_back(lmr);
0396   parameters.push_back(color);
0397   parameters.push_back(type);
0398   return fetchBySequence(parameters,
0399                          "SELECT LMF_IOV_ID FROM (SELECT "
0400                          "SEQ_ID, LMF_IOV_ID FROM "
0401                          "CMS_ECAL_LASER_COND.LMF_RUN_IOV "
0402                          "WHERE SEQ_ID < :1 AND LMR = :2 AND COLOR_ID = :3 "
0403                          "AND TRIG_TYPE = :4 ORDER BY SEQ_ID DESC) WHERE "
0404                          "ROWNUM <= 1",
0405                          "fetchBySequence");
0406 }
0407 
0408 LMFRunIOV &LMFRunIOV::operator=(const LMFRunIOV &r) {
0409   if (this != &r) {
0410     LMFUnique::operator=(r);
0411     if (r._fabric != nullptr) {
0412       checkFabric();  //      _fabric = new LMFDefFabric;
0413       if (m_debug) {
0414         _fabric->debug();
0415         std::cout << "COPYING INTO " << _fabric << std::endl;
0416       }
0417       *_fabric = *(r._fabric);
0418     }
0419   }
0420   return *this;
0421 }