Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include <stdexcept>
0002 #include <string>
0003 #include <cmath>
0004 #include <list>
0005 #include <string>
0006 #include <map>
0007 #include <ctime>
0008 
0009 #include "OnlineDB/EcalCondDB/interface/RunDCSHVDat.h"
0010 #include "OnlineDB/EcalCondDB/interface/RunIOV.h"
0011 #include "OnlineDB/EcalCondDB/interface/Tm.h"
0012 
0013 using namespace std;
0014 using namespace oracle::occi;
0015 
0016 RunDCSHVDat::RunDCSHVDat() {
0017   m_env = nullptr;
0018   m_conn = nullptr;
0019   m_writeStmt = nullptr;
0020   m_readStmt = nullptr;
0021 
0022   m_hv = 0;
0023   m_hvnom = 0;
0024   m_status = 0;
0025   m_tstatus = 0;
0026 }
0027 
0028 RunDCSHVDat::~RunDCSHVDat() {}
0029 
0030 void RunDCSHVDat::prepareWrite() noexcept(false) {}
0031 
0032 void RunDCSHVDat::writeDB(const EcalLogicID* ecid, const RunDCSHVDat* item, RunIOV* iov) noexcept(false) {}
0033 
0034 void RunDCSHVDat::fetchData(map<EcalLogicID, RunDCSHVDat>* fillMap, RunIOV* iov) noexcept(false) {
0035   fetchLastData(fillMap);
0036 }
0037 
0038 ResultSet* RunDCSHVDat::getBarrelRset(const Tm& timeStart) {
0039   DateHandler dh(m_env, m_conn);
0040 
0041   ResultSet* rset = nullptr;
0042   string query =
0043       "SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
0044       " d.actual_vmon, h.nominal_value ,  d.change_date "
0045       " FROM " +
0046       getEBAccount() +
0047       ".FWCAENCHANNEL d "
0048       " JOIN " +
0049       getEBAccount() +
0050       ".HV_MAPPING h on h.DPID = d.DPID "
0051       " join " +
0052       getEBAccount() +
0053       ".CHANNELVIEW cv on cv.logic_id=h.logic_id WHERE cv.maps_to = cv.name "
0054       " AND d.change_date> :1 AND d.actual_vmon is not null order by change_date ";
0055   try {
0056     m_readStmt->setSQL(query);
0057 
0058     m_readStmt->setDate(1, dh.tmToDate(timeStart));
0059 
0060     rset = m_readStmt->executeQuery();
0061   } catch (SQLException& e) {
0062 #if defined(_GLIBCXX_USE_CXX11_ABI) && (_GLIBCXX_USE_CXX11_ABI == 0)
0063     throw(std::runtime_error(std::string("RunDCSHVDat::getBarrelRset():  ") + e.getMessage() + " " + query));
0064 #else
0065     throw(std::runtime_error(std::string("RunDCSHVDat::getBarrelRset():  error code ") +
0066                              std::to_string(e.getErrorCode()) + " " + query));
0067 #endif
0068   }
0069   return rset;
0070 }
0071 
0072 ResultSet* RunDCSHVDat::getEndcapAnodeRset(const Tm& timeStart) {
0073   DateHandler dh(m_env, m_conn);
0074 
0075   ResultSet* rset = nullptr;
0076   string query =
0077       "SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
0078       " d.actual_vmon, '800' nominal_value ,  d.change_date "
0079       " FROM " +
0080       getEEAccount() +
0081       ".FWCAENCHANNEL d "
0082       " JOIN " +
0083       getEEAccount() +
0084       ".EE_HVA_MAPPING h on h.DPID = d.DPID "
0085       " join " +
0086       getEEAccount() +
0087       ".CHANNELVIEW cv on cv.logic_id=h.logic_id WHERE cv.maps_to = cv.name "
0088       " AND d.change_date> :1 AND d.actual_vmon is not null order by change_date ";
0089   try {
0090     m_readStmt->setSQL(query);
0091 
0092     m_readStmt->setDate(1, dh.tmToDate(timeStart));
0093 
0094     rset = m_readStmt->executeQuery();
0095   } catch (SQLException& e) {
0096 #if defined(_GLIBCXX_USE_CXX11_ABI) && (_GLIBCXX_USE_CXX11_ABI == 0)
0097     throw(std::runtime_error(std::string("RunDCSHVDat::getBarrelRset():  ") + e.getMessage() + " " + query));
0098 #else
0099     throw(std::runtime_error(std::string("RunDCSHVDat::getBarrelRset():  error code ") +
0100                              std::to_string(e.getErrorCode()) + " " + query));
0101 #endif
0102   }
0103   return rset;
0104 }
0105 
0106 ResultSet* RunDCSHVDat::getEndcapDynodeRset(const Tm& timeStart) {
0107   DateHandler dh(m_env, m_conn);
0108 
0109   ResultSet* rset = nullptr;
0110   string query =
0111       "SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
0112       " d.actual_vmon, '600' nominal_value ,  d.change_date "
0113       " FROM " +
0114       getEEAccount() +
0115       ".FWCAENCHANNEL d "
0116       " JOIN " +
0117       getEEAccount() +
0118       ".EE_HVD_MAPPING h on h.DPID = d.DPID "
0119       " join " +
0120       getEEAccount() +
0121       ".CHANNELVIEW cv on cv.logic_id=h.logic_id WHERE cv.maps_to = cv.name "
0122       " AND d.change_date> :1 AND d.actual_vmon is not null order by change_date ";
0123   try {
0124     m_readStmt->setSQL(query);
0125 
0126     m_readStmt->setDate(1, dh.tmToDate(timeStart));
0127 
0128     rset = m_readStmt->executeQuery();
0129   } catch (SQLException& e) {
0130 #if defined(_GLIBCXX_USE_CXX11_ABI) && (_GLIBCXX_USE_CXX11_ABI == 0)
0131     throw(std::runtime_error(std::string("RunDCSHVDat::getBarrelRset():  ") + e.getMessage() + " " + query));
0132 #else
0133     throw(std::runtime_error(std::string("RunDCSHVDat::getBarrelRset():  error code ") +
0134                              std::to_string(e.getErrorCode()) + " " + query));
0135 #endif
0136   }
0137   return rset;
0138 }
0139 
0140 /*
0141 ResultSet *RunDCSHVDat::getBarrelRset() {
0142   ResultSet* rset = NULL;
0143   string query = "SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
0144     " d.value, h.nominal_value , d.since "
0145     "FROM "+ getEBAccount()+".DCSLASTVALUE_VOLTAGE_VMON d "
0146     " JOIN "+ getEBAccount()+".HV_MAPPING h on "
0147     " h.DPID = d.DPID join channelview cv on cv.logic_id=h.logic_id WHERE cv.maps_to = cv.name"; 
0148   try {
0149     m_readStmt->setSQL(query);
0150     rset = m_readStmt->executeQuery();
0151   }
0152   catch (SQLException &e) {
0153     throw(std::runtime_error(std::string("RunDCSHVDat::getBarrelRset():  ") + e.getMessage() + " " + query));
0154   }
0155   return rset;
0156 }
0157 */
0158 
0159 ResultSet* RunDCSHVDat::getBarrelRset() {
0160   ResultSet* rset = nullptr;
0161   string query =
0162       "SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
0163       " d.VALUE_NUMBER, h.nominal_value , d.CHANGE_DATE "
0164       "FROM " +
0165       getEBAccount() +
0166       ".FWCAENCHANNEL_LV d "
0167       " JOIN " +
0168       getEBAccount() +
0169       ".HV_MAPPING h on "
0170       " h.DPID = d.DPID join channelview cv on cv.logic_id=h.logic_id WHERE cv.maps_to = cv.name and "
0171       "DPE_NAME='ACTUAL_VMON' ";
0172   try {
0173     m_readStmt->setSQL(query);
0174     rset = m_readStmt->executeQuery();
0175   } catch (SQLException& e) {
0176 #if defined(_GLIBCXX_USE_CXX11_ABI) && (_GLIBCXX_USE_CXX11_ABI == 0)
0177     throw(std::runtime_error(std::string("RunDCSHVDat::getBarrelRset():  ") + e.getMessage() + " " + query));
0178 #else
0179     throw(std::runtime_error(std::string("RunDCSHVDat::getBarrelRset():  error code ") +
0180                              std::to_string(e.getErrorCode()) + " " + query));
0181 #endif
0182   }
0183   return rset;
0184 }
0185 
0186 ResultSet* RunDCSHVDat::getEndcapAnodeRset() {
0187   ResultSet* rset = nullptr;
0188   string query =
0189       "SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
0190       " d.value_number, '800' NOMINAL_VALUE , d.CHANGE_DATE "
0191       "FROM " +
0192       getEEAccount() +
0193       ".FWCAENCHANNEL_LV d "
0194       " JOIN " +
0195       getEEAccount() +
0196       ".EE_HVA_MAPPING h on "
0197       " h.DPID = d.DPID join channelview cv on cv.logic_id=h.logic_id WHERE cv.maps_to = cv.name and "
0198       "dpe_name='ACTUAL_VMON' ";
0199   try {
0200     m_readStmt->setSQL(query);
0201     rset = m_readStmt->executeQuery();
0202   } catch (SQLException& e) {
0203 #if defined(_GLIBCXX_USE_CXX11_ABI) && (_GLIBCXX_USE_CXX11_ABI == 0)
0204     throw(std::runtime_error(std::string("RunDCSHVDat::getEndcapAnodeRset():  ") + e.getMessage() + " " + query));
0205 #else
0206     throw(std::runtime_error(std::string("RunDCSHVDat::getEndcapAnodeRset():  error code ") +
0207                              std::to_string(e.getErrorCode()) + " " + query));
0208 #endif
0209   }
0210   return rset;
0211 }
0212 
0213 ResultSet* RunDCSHVDat::getEndcapDynodeRset() {
0214   ResultSet* rset = nullptr;
0215   string query =
0216       "SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
0217       " d.value_number, '600' NOMINAL_VALUE , d.CHANGE_DATE "
0218       "FROM " +
0219       getEEAccount() +
0220       ".FWCAENCHANNEL_LV d "
0221       " JOIN " +
0222       getEEAccount() +
0223       ".EE_HVD_MAPPING h on "
0224       " h.DPID = d.DPID join channelview cv on cv.logic_id=h.logic_id WHERE cv.maps_to = cv.name and "
0225       "dpe_name='ACTUAL_VMON' ";
0226   try {
0227     m_readStmt->setSQL(query);
0228     rset = m_readStmt->executeQuery();
0229   } catch (SQLException& e) {
0230 #if defined(_GLIBCXX_USE_CXX11_ABI) && (_GLIBCXX_USE_CXX11_ABI == 0)
0231     throw(std::runtime_error(std::string("RunDCSHVDat::getEndcapDynodeRset():  ") + e.getMessage() + " " + query));
0232 #else
0233     throw(std::runtime_error(std::string("RunDCSHVDat::getEndcapDynodeRset():  error code ") +
0234                              std::to_string(e.getErrorCode()) + " " + query));
0235 #endif
0236   }
0237   return rset;
0238 }
0239 
0240 void RunDCSHVDat::fillTheMap(ResultSet* rset, map<EcalLogicID, RunDCSHVDat>* fillMap) {
0241   // method for last value queries
0242 
0243   std::pair<EcalLogicID, RunDCSHVDat> p;
0244   RunDCSHVDat dat;
0245   DateHandler dh(m_env, m_conn);
0246 
0247   try {
0248     while (rset->next()) {
0249       p.first = EcalLogicID(rset->getString(1),   // name
0250                             rset->getInt(2),      // logic_id
0251                             rset->getInt(3),      // id1
0252                             rset->getInt(4),      // id2
0253                             rset->getInt(5),      // id3
0254                             rset->getString(6));  // maps_to
0255 
0256       dat.setHV(rset->getFloat(7));
0257       dat.setHVNominal(rset->getFloat(8));
0258       Date sinceDate = rset->getDate(9);
0259       Tm sinceTm = dh.dateToTm(sinceDate);
0260       dat.setStatus(0);
0261       if (p.first.getName() == "EB_HV_channel") {
0262         setStatusForBarrel(dat, sinceTm);
0263       } else {
0264         setStatusForEndcaps(dat, sinceTm);
0265       }
0266       p.second = dat;
0267       fillMap->insert(p);
0268     }
0269   } catch (SQLException& e) {
0270 #if defined(_GLIBCXX_USE_CXX11_ABI) && (_GLIBCXX_USE_CXX11_ABI == 0)
0271     throw(std::runtime_error(std::string("RunDCSHVDat::fetchData():  ") + e.getMessage()));
0272 #else
0273     throw(std::runtime_error(std::string("RunDCSHVDat::fetchData():  error code ") + std::to_string(e.getErrorCode())));
0274 #endif
0275   }
0276 }
0277 
0278 void RunDCSHVDat::fillTheMapByTime(
0279     ResultSet* rset, std::list<DataReducer<RunDCSHVDat>::MyData<RunDCSHVDat> >* my_data_list) noexcept(false) {
0280   //                   std::list< std::pair< Tm, std::map< EcalLogicID, RunDCSHVDat > > >* fillMap) {
0281 
0282   // method for historic queries
0283 
0284   RunDCSHVDat dat;
0285   DateHandler dh(m_env, m_conn);
0286 
0287   try {
0288     int count = -1;
0289     while (rset->next()) {
0290       EcalLogicID ec = EcalLogicID(rset->getString(1),   // name
0291                                    rset->getInt(2),      // logic_id
0292                                    rset->getInt(3),      // id1
0293                                    rset->getInt(4),      // id2
0294                                    rset->getInt(5),      // id3
0295                                    rset->getString(6));  // maps_to
0296 
0297       dat.setHV(rset->getFloat(7));
0298       dat.setHVNominal(rset->getFloat(8));
0299       //      Date sinceDate = rset->getDate(9);
0300       //     Tm  sinceTm = dh.dateToTm( sinceDate );
0301       // Date sinceDate = rset->getDate(9);
0302       Timestamp ora_timestamp = rset->getTimestamp(9);
0303       Tm sinceTm;  // YYYY-MM-DD HH:MM:SS
0304 #if defined(_GLIBCXX_USE_CXX11_ABI) && (_GLIBCXX_USE_CXX11_ABI == 0)
0305       sinceTm.setToString(ora_timestamp.toText("yyyy-mm-dd hh24:mi:ss", 0));
0306 #else
0307       int year = 0;
0308       unsigned int month = 0;
0309       unsigned int day = 0;
0310       unsigned int hour = 0;
0311       unsigned int minute = 0;
0312       unsigned int second = 0;
0313       unsigned int fs = 0;
0314       ora_timestamp.getDate(year, month, day);
0315       ora_timestamp.getTime(hour, minute, second, fs);
0316       const std::tm tt = {.tm_sec = static_cast<int>(second),
0317                           .tm_min = static_cast<int>(minute),
0318                           .tm_hour = static_cast<int>(hour),
0319                           .tm_mday = static_cast<int>(day),
0320                           .tm_mon = static_cast<int>(month),
0321                           .tm_year = year - 1900,
0322                           .tm_wday = 0,
0323                           .tm_yday = 0,
0324                           .tm_isdst = 0,
0325                           .tm_gmtoff = 0,
0326                           .tm_zone = nullptr};
0327       char tt_str[30] = {0};
0328       if (std::strftime(tt_str, sizeof(tt_str), "%F %T", &tt)) {
0329         sinceTm.setToString(std::string(tt_str));
0330       } else {
0331         throw std::runtime_error("RunDCSHVDat::fillTheMapByTime: failed to generate the date string");
0332       }
0333 #endif
0334 
0335       dat.setStatus(0);
0336       if (ec.getName() == "EB_HV_channel") {
0337         setStatusForBarrel(dat, sinceTm);
0338       } else {
0339         setStatusForEndcaps(dat, sinceTm);
0340       }
0341 
0342       std::pair<EcalLogicID, RunDCSHVDat> d;
0343       d.first = ec;
0344       d.second = dat;
0345       std::pair<Tm, std::pair<EcalLogicID, RunDCSHVDat> > p;
0346       p.first = sinceTm;
0347       p.second = d;
0348 
0349       DataReducer<RunDCSHVDat>::MyData<RunDCSHVDat> pp;
0350       pp.m_iData = p;
0351 
0352       my_data_list->push_back(pp);
0353       count++;
0354       if (count < 100)
0355         std::cout << "DCS DB : size:" << my_data_list->size() << " Tm " << sinceTm.str() << " " << ec.getID1() << " "
0356                   << ec.getID2() << " " << dat.getHV() << std::endl;
0357     }
0358     std::cout << "DCS DB : size:" << my_data_list->size() << std::endl;
0359 
0360   } catch (SQLException& e) {
0361 #if defined(_GLIBCXX_USE_CXX11_ABI) && (_GLIBCXX_USE_CXX11_ABI == 0)
0362     throw(std::runtime_error(std::string("RunDCSHVDat::fetchData():  ") + e.getMessage()));
0363 #else
0364     throw(std::runtime_error(std::string("RunDCSHVDat::fetchData():  error code ") + std::to_string(e.getErrorCode())));
0365 #endif
0366   }
0367 }
0368 
0369 int RunDCSHVDat::nowMicroseconds() {
0370   Tm t_now_gmt;
0371 
0372   t_now_gmt.setToCurrentGMTime();
0373   int t_now_gmt_micros = t_now_gmt.microsTime();
0374   return t_now_gmt_micros;
0375 }
0376 
0377 void RunDCSHVDat::setStatusForBarrel(RunDCSHVDat& dat, const Tm& sinceTm) {
0378   int t_now_gmt_micros = nowMicroseconds();
0379 
0380   float hv_diff = dat.getHV() - dat.getHVNominal();
0381   if (hv_diff < 0)
0382     hv_diff = -hv_diff;
0383   if (hv_diff * 1000 > maxHVDifferenceEB) {
0384     dat.setStatus(HVNOTNOMINAL);
0385   }
0386   if (dat.getHV() * 1000 < minHV) {
0387     dat.setStatus(HVOFF);
0388   }
0389 
0390   int result = 0;
0391   long long d = (t_now_gmt_micros - sinceTm.microsTime());
0392   if (d > maxDifference) {
0393     result = -d / 1000000;
0394   }
0395   dat.setTimeStatus(result);
0396 }
0397 
0398 void RunDCSHVDat::setStatusForEndcaps(RunDCSHVDat& dat, const Tm& sinceTm) {
0399   int t_now_gmt_micros = nowMicroseconds();
0400 
0401   if (fabs(dat.getHV() - dat.getHVNominal()) * 1000 > maxHVDifferenceEE) {
0402     dat.setStatus(HVNOTNOMINAL);
0403   }
0404   if (dat.getHV() * 1000 < minHV) {
0405     dat.setStatus(HVOFF);
0406   }
0407 
0408   int result = 0;
0409   long long d = (t_now_gmt_micros - sinceTm.microsTime());
0410   if (d > maxDifference) {
0411     result = -d / 1000000;
0412   }
0413   dat.setTimeStatus(result);
0414 }
0415 
0416 void RunDCSHVDat::fetchLastData(map<EcalLogicID, RunDCSHVDat>* fillMap) noexcept(false) {
0417   this->checkConnection();
0418 
0419   fillMap->clear();
0420 
0421   try {
0422     std::pair<EcalLogicID, RunDCSHVDat> p;
0423     RunDCSHVDat dat;
0424 
0425     ResultSet* rset = getBarrelRset();
0426 
0427     fillTheMap(rset, fillMap);
0428     rset = getEndcapAnodeRset();
0429 
0430     fillTheMap(rset, fillMap);
0431     rset = getEndcapDynodeRset();
0432 
0433     fillTheMap(rset, fillMap);
0434   } catch (SQLException& e) {
0435 #if defined(_GLIBCXX_USE_CXX11_ABI) && (_GLIBCXX_USE_CXX11_ABI == 0)
0436     throw(std::runtime_error(std::string("RunDCSHVDat::fetchData():  ") + e.getMessage()));
0437 #else
0438     throw(std::runtime_error(std::string("RunDCSHVDat::fetchData():  error code ") + std::to_string(e.getErrorCode())));
0439 #endif
0440   }
0441 }
0442 
0443 void RunDCSHVDat::fetchHistoricalData(std::list<std::pair<Tm, std::map<EcalLogicID, RunDCSHVDat> > >* fillMap,
0444                                       const Tm& timeStart) noexcept(false) {
0445   this->checkConnection();
0446 
0447   fillMap->clear();
0448 
0449   std::list<DataReducer<RunDCSHVDat>::MyData<RunDCSHVDat> > my_data_list;
0450 
0451   try {
0452     std::pair<EcalLogicID, RunDCSHVDat> p;
0453     RunDCSHVDat dat;
0454 
0455     ResultSet* rset1 = getBarrelRset(timeStart);
0456     fillTheMapByTime(rset1, &my_data_list);
0457 
0458     ResultSet* rset2 = getEndcapAnodeRset(timeStart);
0459     fillTheMapByTime(rset2, &my_data_list);
0460 
0461     ResultSet* rset3 = getEndcapDynodeRset(timeStart);
0462     fillTheMapByTime(rset3, &my_data_list);
0463 
0464     DataReducer<RunDCSHVDat> my_dr;
0465     my_dr.setDataList(my_data_list);
0466     my_dr.getReducedDataList(fillMap);
0467 
0468   } catch (SQLException& e) {
0469 #if defined(_GLIBCXX_USE_CXX11_ABI) && (_GLIBCXX_USE_CXX11_ABI == 0)
0470     throw(std::runtime_error(std::string("RunDCSHVDat::fetchData():  ") + e.getMessage()));
0471 #else
0472     throw(std::runtime_error(std::string("RunDCSHVDat::fetchData():  error code ") + std::to_string(e.getErrorCode())));
0473 #endif
0474   }
0475 }