Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:31:56

0001 #include <stdexcept>
0002 #include <string>
0003 #include "OnlineDB/Oracle/interface/Oracle.h"
0004 
0005 #include "OnlineDB/EcalCondDB/interface/CaliCrystalIntercalDat.h"
0006 #include "OnlineDB/EcalCondDB/interface/CaliTag.h"
0007 #include "OnlineDB/EcalCondDB/interface/CaliIOV.h"
0008 
0009 using namespace std;
0010 using namespace oracle::occi;
0011 
0012 CaliCrystalIntercalDat::CaliCrystalIntercalDat() {
0013   m_env = nullptr;
0014   m_conn = nullptr;
0015   m_writeStmt = nullptr;
0016 
0017   m_cali = 0;
0018   m_caliRMS = 0;
0019   m_numEvents = 0;
0020   m_taskStatus = false;
0021 }
0022 
0023 CaliCrystalIntercalDat::~CaliCrystalIntercalDat() {}
0024 
0025 void CaliCrystalIntercalDat::prepareWrite() noexcept(false) {
0026   this->checkConnection();
0027 
0028   try {
0029     m_writeStmt = m_conn->createStatement();
0030     m_writeStmt->setSQL(
0031         "INSERT INTO cali_crystal_intercal_dat (iov_id, logic_id, "
0032         "cali, cali_rms, num_events, task_status) "
0033         "VALUES (:iov_id, :logic_id, "
0034         ":3, :4, :5, :6)");
0035   } catch (SQLException& e) {
0036     throw(std::runtime_error("CaliCrystalIntercalDat::prepareWrite():  " + e.getMessage()));
0037   }
0038 }
0039 
0040 void CaliCrystalIntercalDat::writeDB(const EcalLogicID* ecid,
0041                                      const CaliCrystalIntercalDat* item,
0042                                      CaliIOV* iov) noexcept(false) {
0043   this->checkConnection();
0044   this->checkPrepare();
0045 
0046   int iovID = iov->fetchID();
0047   if (!iovID) {
0048     throw(std::runtime_error("CaliCrystalIntercalDat::writeDB:  IOV not in DB"));
0049   }
0050 
0051   int logicID = ecid->getLogicID();
0052   if (!logicID) {
0053     throw(std::runtime_error("CaliCrystalIntercalDat::writeDB:  Bad EcalLogicID"));
0054   }
0055 
0056   try {
0057     m_writeStmt->setInt(1, iovID);
0058     m_writeStmt->setInt(2, logicID);
0059 
0060     m_writeStmt->setFloat(3, item->getCali());
0061     m_writeStmt->setFloat(4, item->getCaliRMS());
0062     m_writeStmt->setInt(5, item->getNumEvents());
0063     m_writeStmt->setInt(6, item->getTaskStatus());
0064 
0065     m_writeStmt->executeUpdate();
0066   } catch (SQLException& e) {
0067     throw(std::runtime_error("CaliCrystalIntercalDat::writeDB():  " + e.getMessage()));
0068   }
0069 }
0070 
0071 void CaliCrystalIntercalDat::fetchData(std::map<EcalLogicID, CaliCrystalIntercalDat>* fillMap,
0072                                        CaliIOV* iov) noexcept(false) {
0073   this->checkConnection();
0074   fillMap->clear();
0075 
0076   iov->setConnection(m_env, m_conn);
0077   int iovID = iov->fetchID();
0078   if (!iovID) {
0079     //  throw(std::runtime_error("CaliCrystalIntercalDat::writeDB:  IOV not in DB"));
0080     return;
0081   }
0082 
0083   try {
0084     m_readStmt->setSQL(
0085         "SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
0086         "d.cali, d.cali_rms, d.num_events, d.task_status "
0087         "FROM channelview cv JOIN cali_crystal_intercal_dat d "
0088         "ON cv.logic_id = d.logic_id AND cv.name = cv.maps_to "
0089         "WHERE d.iov_id = :iov_id");
0090     m_readStmt->setInt(1, iovID);
0091     ResultSet* rset = m_readStmt->executeQuery();
0092 
0093     std::pair<EcalLogicID, CaliCrystalIntercalDat> p;
0094     CaliCrystalIntercalDat dat;
0095     while (rset->next()) {
0096       p.first = EcalLogicID(rset->getString(1),   // name
0097                             rset->getInt(2),      // logic_id
0098                             rset->getInt(3),      // id1
0099                             rset->getInt(4),      // id2
0100                             rset->getInt(5),      // id3
0101                             rset->getString(6));  // maps_to
0102 
0103       dat.setCali(rset->getFloat(7));
0104       dat.setCaliRMS(rset->getFloat(8));
0105       dat.setNumEvents(rset->getInt(9));
0106       dat.setTaskStatus(rset->getInt(10));
0107 
0108       p.second = dat;
0109       fillMap->insert(p);
0110     }
0111   } catch (SQLException& e) {
0112     throw(std::runtime_error("CaliCrystalIntercalDat::fetchData():  " + e.getMessage()));
0113   }
0114 }
0115 
0116 void CaliCrystalIntercalDat::writeArrayDB(const std::map<EcalLogicID, CaliCrystalIntercalDat>* data,
0117                                           CaliIOV* iov) noexcept(false) {
0118   this->checkConnection();
0119   this->checkPrepare();
0120 
0121   int iovID = iov->fetchID();
0122   if (!iovID) {
0123     throw(std::runtime_error("CaliCrystalIntercalDat::writeArrayDB:  IOV not in DB"));
0124   }
0125 
0126   int nrows = data->size();
0127   int* ids = new int[nrows];
0128   int* iovid_vec = new int[nrows];
0129   float* xx = new float[nrows];
0130   float* yy = new float[nrows];
0131   int* tt = new int[nrows];
0132   int* st = new int[nrows];
0133 
0134   ub2* ids_len = new ub2[nrows];
0135   ub2* iov_len = new ub2[nrows];
0136   ub2* x_len = new ub2[nrows];
0137   ub2* y_len = new ub2[nrows];
0138   ub2* tt_len = new ub2[nrows];
0139   ub2* st_len = new ub2[nrows];
0140 
0141   const EcalLogicID* channel;
0142   const CaliCrystalIntercalDat* dataitem;
0143   int count = 0;
0144   typedef map<EcalLogicID, CaliCrystalIntercalDat>::const_iterator CI;
0145   for (CI p = data->begin(); p != data->end(); ++p) {
0146     channel = &(p->first);
0147     int logicID = channel->getLogicID();
0148     if (!logicID) {
0149       throw(std::runtime_error("CaliCrystalIntercalDat::writeArrayDB:  Bad EcalLogicID"));
0150     }
0151     ids[count] = logicID;
0152     iovid_vec[count] = iovID;
0153 
0154     dataitem = &(p->second);
0155     // dataIface.writeDB( channel, dataitem, iov);
0156     float x = dataitem->getCali();
0157     float y = dataitem->getCaliRMS();
0158     int xtt = dataitem->getNumEvents();
0159     int statu = dataitem->getTaskStatus();
0160 
0161     xx[count] = x;
0162     yy[count] = y;
0163     tt[count] = xtt;
0164     st[count] = statu;
0165 
0166     ids_len[count] = sizeof(ids[count]);
0167     iov_len[count] = sizeof(iovid_vec[count]);
0168 
0169     x_len[count] = sizeof(xx[count]);
0170     y_len[count] = sizeof(yy[count]);
0171     tt_len[count] = sizeof(tt[count]);
0172     st_len[count] = sizeof(st[count]);
0173 
0174     count++;
0175   }
0176 
0177   try {
0178     m_writeStmt->setDataBuffer(1, (dvoid*)iovid_vec, OCCIINT, sizeof(iovid_vec[0]), iov_len);
0179     m_writeStmt->setDataBuffer(2, (dvoid*)ids, OCCIINT, sizeof(ids[0]), ids_len);
0180     m_writeStmt->setDataBuffer(3, (dvoid*)xx, OCCIFLOAT, sizeof(xx[0]), x_len);
0181     m_writeStmt->setDataBuffer(4, (dvoid*)yy, OCCIFLOAT, sizeof(yy[0]), y_len);
0182     m_writeStmt->setDataBuffer(5, (dvoid*)tt, OCCIINT, sizeof(tt[0]), tt_len);
0183     m_writeStmt->setDataBuffer(6, (dvoid*)st, OCCIINT, sizeof(st[0]), st_len);
0184 
0185     m_writeStmt->executeArrayUpdate(nrows);
0186 
0187     delete[] ids;
0188     delete[] iovid_vec;
0189     delete[] xx;
0190     delete[] yy;
0191     delete[] st;
0192     delete[] tt;
0193 
0194     delete[] ids_len;
0195     delete[] iov_len;
0196     delete[] x_len;
0197     delete[] y_len;
0198     delete[] tt_len;
0199     delete[] st_len;
0200 
0201   } catch (SQLException& e) {
0202     throw(std::runtime_error("MonPedestalsDat::writeArrayDB():  " + e.getMessage()));
0203   }
0204 }