Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include <stdexcept>
0002 #include "OnlineDB/Oracle/interface/Oracle.h"
0003 
0004 #include "OnlineDB/EcalCondDB/interface/RunList.h"
0005 #include "OnlineDB/EcalCondDB/interface/RunIOV.h"
0006 #include "OnlineDB/EcalCondDB/interface/RunTag.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 RunList::RunList() { m_conn = nullptr; }
0014 
0015 RunList::~RunList() {}
0016 
0017 void RunList::setRunTag(const RunTag& tag) {
0018   if (tag != m_runTag) {
0019     m_runTag = tag;
0020   }
0021 }
0022 
0023 RunTag RunList::getRunTag() const { return m_runTag; }
0024 
0025 std::vector<RunIOV> RunList::getRuns() { return m_vec_runiov; }
0026 
0027 void RunList::fetchNonEmptyRuns() noexcept(false) { fetchRuns(-1, -1, true, false); }
0028 
0029 void RunList::fetchNonEmptyGlobalRuns() noexcept(false) { fetchRuns(-1, -1, false, true); }
0030 
0031 void RunList::fetchNonEmptyRuns(int min_run, int max_run) noexcept(false) { fetchRuns(min_run, max_run, true, false); }
0032 
0033 void RunList::fetchNonEmptyGlobalRuns(int min_run, int max_run) noexcept(false) {
0034   fetchRuns(min_run, max_run, false, true);
0035 }
0036 
0037 void RunList::fetchRuns() noexcept(false) { fetchRuns(-1, -1); }
0038 
0039 void RunList::fetchRuns(int min_run, int max_run) noexcept(false) { fetchRuns(min_run, max_run, false, false); }
0040 
0041 void RunList::fetchRuns(int min_run, int max_run, bool withTriggers, bool withGlobalTriggers) noexcept(false) {
0042   /*
0043     withTriggers and withGlobalTriggers selects those non empty runs.
0044     Possible combinations are
0045 
0046     withTriggers withGlobalTriggers select
0047     ------------ ------------------ ------------------------------
0048     false        false              all
0049     false        true               only runs with global triggers
0050     true         false              only runs with any trigger
0051     true         true               only runs with global triggers
0052    */
0053   this->checkConnection();
0054   int nruns = 0;
0055 
0056   m_runTag.setConnection(m_env, m_conn);
0057   int tagID = m_runTag.fetchID();
0058   cout << "tag id=" << tagID << endl;
0059   if (!tagID) {
0060     return;
0061   }
0062 
0063   int my_min_run = min_run - 1;
0064   int my_max_run = max_run + 1;
0065   try {
0066     Statement* stmt0 = m_conn->createStatement();
0067     string sql =
0068         "SELECT count(iov_id) FROM run_iov "
0069         "WHERE tag_id = :tag_id ";
0070     if (min_run > 0) {
0071       // don't need to specify empty/non empty here. This is needed
0072       // just to allocate the memory for the vector
0073       sql += " and run_iov.run_num> :min_run and run_iov.run_num< :max_run ";
0074     }
0075     stmt0->setSQL(sql);
0076     stmt0->setInt(1, tagID);
0077     if (min_run > 0) {
0078       stmt0->setInt(2, my_min_run);
0079       stmt0->setInt(3, my_max_run);
0080     }
0081 
0082     ResultSet* rset0 = stmt0->executeQuery();
0083     if (rset0->next()) {
0084       nruns = rset0->getInt(1);
0085     }
0086     m_conn->terminateStatement(stmt0);
0087 
0088     cout << "number of runs=" << nruns << endl;
0089     m_vec_runiov.reserve(nruns);
0090 
0091     Statement* stmt = m_conn->createStatement();
0092     sql =
0093         "SELECT DISTINCT i.iov_id, tag_id, run_num, run_start, run_end, "
0094         "db_timestamp FROM run_iov i ";
0095     if ((withTriggers) || (withGlobalTriggers)) {
0096       sql +=
0097           "join cms_ecal_cond.run_dat d on d.iov_id = i.iov_id "
0098           "left join CMS_WBM.RUNSUMMARY R on R.RUNNUMBER = i.RUN_NUM ";
0099     }
0100     sql += "WHERE tag_id = :tag_id ";
0101     if (min_run > 0) {
0102       sql += "and i.run_num> :min_run and i.run_num< :max_run ";
0103     }
0104     if (withGlobalTriggers) {
0105       sql += "and R.TRIGGERS > 0 ";
0106     } else if (withTriggers) {
0107       sql += "and ((R.TRIGGERS > 0) or (num_events > 0)) ";
0108     }
0109     sql += " order by run_num ";
0110     stmt->setSQL(sql);
0111     stmt->setInt(1, tagID);
0112     if (min_run > 0) {
0113       stmt->setInt(2, my_min_run);
0114       stmt->setInt(3, my_max_run);
0115     }
0116 
0117     DateHandler dh(m_env, m_conn);
0118     Tm runStart;
0119     Tm runEnd;
0120     Tm dbtime;
0121 
0122     ResultSet* rset = stmt->executeQuery();
0123     int i = 0;
0124     while ((i < nruns) && (rset->next())) {
0125       int iovID = rset->getInt(1);
0126       // int tagID = rset->getInt(2);
0127       int runNum = rset->getInt(3);
0128       Date startDate = rset->getDate(4);
0129       Date endDate = rset->getDate(5);
0130       Date dbDate = rset->getDate(6);
0131 
0132       runStart = dh.dateToTm(startDate);
0133       runEnd = dh.dateToTm(endDate);
0134       dbtime = dh.dateToTm(dbDate);
0135 
0136       RunIOV r;
0137       r.setRunNumber(runNum);
0138       r.setRunStart(runStart);
0139       r.setRunEnd(runEnd);
0140       r.setDBInsertionTime(dbtime);
0141       r.setRunTag(m_runTag);
0142       r.setID(iovID);
0143       m_vec_runiov.push_back(r);
0144 
0145       i++;
0146     }
0147     m_vec_runiov.resize(i);
0148     m_conn->terminateStatement(stmt);
0149   } catch (SQLException& e) {
0150     throw(std::runtime_error(std::string("RunList::fetchRuns:  ") + e.getMessage()));
0151   }
0152 }
0153 
0154 void RunList::fetchLastNRuns(int max_run, int n_runs) noexcept(false) {
0155   // fetch the last n_runs that come just before max_run (including max_run)
0156 
0157   this->checkConnection();
0158 
0159   m_runTag.setConnection(m_env, m_conn);
0160   int tagID = m_runTag.fetchID();
0161   cout << "tag id=" << tagID << endl;
0162   if (!tagID) {
0163     return;
0164   }
0165 
0166   int my_max_run = max_run + 1;
0167   try {
0168     int nruns = n_runs + 1;
0169     m_vec_runiov.reserve(nruns);
0170 
0171     Statement* stmt = m_conn->createStatement();
0172     stmt->setSQL(
0173         "select iov_id, tag_id, run_num, run_start, run_end, DB_TIMESTAMP from "
0174         " (SELECT * from RUN_IOV "
0175         " WHERE tag_id = :tag_id "
0176         " and run_num< :max_run "
0177         " order by run_num DESC ) where rownum< :n_runs ORDER BY run_num ASC ");
0178     stmt->setInt(1, tagID);
0179     stmt->setInt(2, my_max_run);
0180     stmt->setInt(3, nruns);
0181 
0182     DateHandler dh(m_env, m_conn);
0183     Tm runStart;
0184     Tm runEnd;
0185     Tm dbtime;
0186 
0187     ResultSet* rset = stmt->executeQuery();
0188     int i = 0;
0189     while (i < n_runs) {
0190       rset->next();
0191       int iovID = rset->getInt(1);
0192       // int tagID = rset->getInt(2);
0193       int runNum = rset->getInt(3);
0194       Date startDate = rset->getDate(4);
0195       Date endDate = rset->getDate(5);
0196       Date dbDate = rset->getDate(6);
0197 
0198       runStart = dh.dateToTm(startDate);
0199       runEnd = dh.dateToTm(endDate);
0200       dbtime = dh.dateToTm(dbDate);
0201 
0202       RunIOV r;
0203       r.setRunNumber(runNum);
0204       r.setRunStart(runStart);
0205       r.setRunEnd(runEnd);
0206       r.setDBInsertionTime(dbtime);
0207       r.setRunTag(m_runTag);
0208       r.setID(iovID);
0209       m_vec_runiov.push_back(r);
0210 
0211       i++;
0212     }
0213 
0214     m_conn->terminateStatement(stmt);
0215   } catch (SQLException& e) {
0216     throw(std::runtime_error(std::string("RunList::fetchLastNRuns:  ") + e.getMessage()));
0217   }
0218 }
0219 
0220 void RunList::fetchRunsByLocation(int min_run, int max_run, const LocationDef& locDef) noexcept(false) {
0221   this->checkConnection();
0222   int nruns = 0;
0223 
0224   int my_min_run = min_run - 1;
0225   int my_max_run = max_run + 1;
0226   try {
0227     Statement* stmt0 = m_conn->createStatement();
0228     stmt0->setSQL(
0229         "SELECT count(iov_id) FROM run_iov r , run_tag t, location_def l "
0230         " WHERE r.tag_id=t.tag_id and t.LOCATION_ID=l.def_id AND l.LOCATION= :1 "
0231         "  and r.run_num> :2 and r.run_num< :3 ");
0232     stmt0->setString(1, locDef.getLocation());
0233     stmt0->setInt(2, my_min_run);
0234     stmt0->setInt(3, my_max_run);
0235 
0236     ResultSet* rset0 = stmt0->executeQuery();
0237     if (rset0->next()) {
0238       nruns = rset0->getInt(1);
0239     }
0240     m_conn->terminateStatement(stmt0);
0241 
0242     cout << "number of runs=" << nruns << endl;
0243 
0244     m_vec_runiov.reserve(nruns);
0245 
0246     Statement* stmt = m_conn->createStatement();
0247     stmt->setSQL(
0248         "SELECT  r.iov_id, r.tag_id, r.run_num, r.run_start, r.run_end, r.DB_TIMESTAMP , "
0249         " t.gen_tag, rt.RUN_TYPE "
0250         " FROM run_iov r , run_tag t, location_def l, run_type_def rt "
0251         " WHERE r.tag_id=t.tag_id and t.LOCATION_ID=l.def_id and t.run_type_id=rt.DEF_ID "
0252         " AND l.LOCATION= :1 "
0253         " and r.run_num> :2 and r.run_num< :3 "
0254         " order by run_num ");
0255     stmt->setString(1, locDef.getLocation());
0256     stmt->setInt(2, my_min_run);
0257     stmt->setInt(3, my_max_run);
0258 
0259     DateHandler dh(m_env, m_conn);
0260     Tm runStart;
0261     Tm runEnd;
0262     Tm dbtime;
0263 
0264     ResultSet* rset = stmt->executeQuery();
0265     int i = 0;
0266     while (i < nruns) {
0267       rset->next();
0268       int iovID = rset->getInt(1);
0269       //       int tagID = rset->getInt(2);
0270       int runNum = rset->getInt(3);
0271       Date startDate = rset->getDate(4);
0272       Date endDate = rset->getDate(5);
0273       Date dbDate = rset->getDate(6);
0274 
0275       runStart = dh.dateToTm(startDate);
0276       runEnd = dh.dateToTm(endDate);
0277       dbtime = dh.dateToTm(dbDate);
0278 
0279       RunTag atag;
0280       atag.setLocationDef(locDef);
0281       atag.setGeneralTag(rset->getString(7));
0282       RunTypeDef rundef;
0283       rundef.setRunType(rset->getString(8));
0284       atag.setRunTypeDef(rundef);
0285 
0286       RunIOV r;
0287       r.setRunNumber(runNum);
0288       r.setRunStart(runStart);
0289       r.setRunEnd(runEnd);
0290       r.setDBInsertionTime(dbtime);
0291       r.setRunTag(atag);
0292       r.setID(iovID);
0293       m_vec_runiov.push_back(r);
0294 
0295       i++;
0296     }
0297 
0298     m_conn->terminateStatement(stmt);
0299 
0300   } catch (SQLException& e) {
0301     throw(std::runtime_error(std::string("RunList::fetchRunsByLocation:  ") + e.getMessage()));
0302   }
0303 }
0304 
0305 void RunList::fetchGlobalRunsByLocation(int min_run, int max_run, const LocationDef& locDef) noexcept(false) {
0306   this->checkConnection();
0307   int nruns = 0;
0308 
0309   int my_min_run = min_run - 1;
0310   int my_max_run = max_run + 1;
0311   try {
0312     Statement* stmt0 = m_conn->createStatement();
0313     stmt0->setSQL(
0314         "SELECT count(iov_id) FROM run_iov r , run_tag t, location_def l "
0315         " WHERE r.tag_id=t.tag_id and t.LOCATION_ID=l.def_id AND l.LOCATION= :1 "
0316         "  and t.gen_tag='GLOBAL' "
0317         "  and r.run_num> :2 and r.run_num< :3 ");
0318     stmt0->setString(1, locDef.getLocation());
0319     stmt0->setInt(2, my_min_run);
0320     stmt0->setInt(3, my_max_run);
0321 
0322     ResultSet* rset0 = stmt0->executeQuery();
0323     if (rset0->next()) {
0324       nruns = rset0->getInt(1);
0325     }
0326     m_conn->terminateStatement(stmt0);
0327 
0328     cout << "number of runs=" << nruns << endl;
0329 
0330     m_vec_runiov.reserve(nruns);
0331 
0332     Statement* stmt = m_conn->createStatement();
0333     stmt->setSQL(
0334         "SELECT  r.iov_id, r.tag_id, r.run_num, r.run_start, r.run_end, r.DB_TIMESTAMP , "
0335         " t.gen_tag, rt.RUN_TYPE "
0336         " FROM run_iov r , run_tag t, location_def l, run_type_def rt "
0337         " WHERE r.tag_id=t.tag_id and t.LOCATION_ID=l.def_id and t.run_type_id=rt.DEF_ID "
0338         " AND l.LOCATION= :1 "
0339         " and t.gen_tag='GLOBAL' "
0340         " and r.run_num> :2 and r.run_num< :3 "
0341         " order by run_num ");
0342     stmt->setString(1, locDef.getLocation());
0343     stmt->setInt(2, my_min_run);
0344     stmt->setInt(3, my_max_run);
0345 
0346     DateHandler dh(m_env, m_conn);
0347     Tm runStart;
0348     Tm runEnd;
0349     Tm dbtime;
0350 
0351     ResultSet* rset = stmt->executeQuery();
0352     int i = 0;
0353     while (i < nruns) {
0354       rset->next();
0355       int iovID = rset->getInt(1);
0356       //       int tagID = rset->getInt(2);
0357       int runNum = rset->getInt(3);
0358       Date startDate = rset->getDate(4);
0359       Date endDate = rset->getDate(5);
0360       Date dbDate = rset->getDate(6);
0361 
0362       runStart = dh.dateToTm(startDate);
0363       runEnd = dh.dateToTm(endDate);
0364       dbtime = dh.dateToTm(dbDate);
0365 
0366       RunTag atag;
0367       atag.setLocationDef(locDef);
0368       atag.setGeneralTag(rset->getString(7));
0369       RunTypeDef rundef;
0370       rundef.setRunType(rset->getString(8));
0371       atag.setRunTypeDef(rundef);
0372 
0373       RunIOV r;
0374       r.setRunNumber(runNum);
0375       r.setRunStart(runStart);
0376       r.setRunEnd(runEnd);
0377       r.setDBInsertionTime(dbtime);
0378       r.setRunTag(atag);
0379       r.setID(iovID);
0380       m_vec_runiov.push_back(r);
0381 
0382       i++;
0383     }
0384 
0385     m_conn->terminateStatement(stmt);
0386 
0387   } catch (SQLException& e) {
0388     throw(std::runtime_error(std::string("RunList::fetchRunsByLocation:  ") + e.getMessage()));
0389   }
0390 }