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