Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:15:18

0001 #include <stdexcept>
0002 #include <string>
0003 #include "OnlineDB/Oracle/interface/Oracle.h"
0004 
0005 #include "OnlineDB/EcalCondDB/interface/MonH4TablePositionDat.h"
0006 
0007 using namespace std;
0008 using namespace oracle::occi;
0009 
0010 MonH4TablePositionDat::MonH4TablePositionDat() {
0011   m_env = nullptr;
0012   m_conn = nullptr;
0013   m_writeStmt = nullptr;
0014   m_readStmt = nullptr;
0015 
0016   m_tableX = 0;
0017   m_tableY = 0;
0018 }
0019 
0020 MonH4TablePositionDat::~MonH4TablePositionDat() {}
0021 
0022 void MonH4TablePositionDat::prepareWrite() noexcept(false) {
0023   this->checkConnection();
0024 
0025   try {
0026     m_writeStmt = m_conn->createStatement();
0027     m_writeStmt->setSQL(
0028         "INSERT INTO mon_h4_table_position_dat (iov_id, logic_id, "
0029         "table_x, table_y) "
0030         "VALUES (:iov_id, :logic_id, "
0031         ":3, :4)");
0032   } catch (SQLException& e) {
0033     throw(std::runtime_error("MonH4TablePositionDat::prepareWrite():  " + e.getMessage()));
0034   }
0035 }
0036 
0037 void MonH4TablePositionDat::writeDB(const EcalLogicID* ecid,
0038                                     const MonH4TablePositionDat* item,
0039                                     MonRunIOV* iov) noexcept(false) {
0040   this->checkConnection();
0041   this->checkPrepare();
0042 
0043   int iovID = iov->fetchID();
0044   if (!iovID) {
0045     throw(std::runtime_error("MonH4TablePositionDat::writeDB:  IOV not in DB"));
0046   }
0047 
0048   int logicID = ecid->getLogicID();
0049   if (!logicID) {
0050     throw(std::runtime_error("MonH4TablePositionDat::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->getTableX());
0058     m_writeStmt->setFloat(4, item->getTableY());
0059 
0060     m_writeStmt->executeUpdate();
0061   } catch (SQLException& e) {
0062     throw(std::runtime_error("MonH4TablePositionDat::writeDB():  " + e.getMessage()));
0063   }
0064 }
0065 
0066 void MonH4TablePositionDat::fetchData(std::map<EcalLogicID, MonH4TablePositionDat>* fillMap,
0067                                       MonRunIOV* 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("MonH4TablePositionDat::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.table_x, d.table_y "
0082         "FROM channelview cv JOIN mon_h4_table_position_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, MonH4TablePositionDat> p;
0089     MonH4TablePositionDat 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.setTableX(rset->getFloat(7));
0099       dat.setTableY(rset->getFloat(8));
0100 
0101       p.second = dat;
0102       fillMap->insert(p);
0103     }
0104   } catch (SQLException& e) {
0105     throw(std::runtime_error("MonH4TablePositionDat::fetchData():  " + e.getMessage()));
0106   }
0107 }
0108 
0109 void MonH4TablePositionDat::writeArrayDB(const std::map<EcalLogicID, MonH4TablePositionDat>* data,
0110                                          MonRunIOV* iov) noexcept(false) {
0111   this->checkConnection();
0112   this->checkPrepare();
0113 
0114   int iovID = iov->fetchID();
0115   if (!iovID) {
0116     throw(std::runtime_error("MonH4TablePositionDat::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 
0125   ub2* ids_len = new ub2[nrows];
0126   ub2* iov_len = new ub2[nrows];
0127   ub2* x_len = new ub2[nrows];
0128   ub2* y_len = new ub2[nrows];
0129 
0130   const EcalLogicID* channel;
0131   const MonH4TablePositionDat* dataitem;
0132   int count = 0;
0133   typedef map<EcalLogicID, MonH4TablePositionDat>::const_iterator CI;
0134   for (CI p = data->begin(); p != data->end(); ++p) {
0135     channel = &(p->first);
0136     int logicID = channel->getLogicID();
0137     if (!logicID) {
0138       throw(std::runtime_error("MonH4TablePositionDat::writeArrayDB:  Bad EcalLogicID"));
0139     }
0140     ids[count] = logicID;
0141     iovid_vec[count] = iovID;
0142 
0143     dataitem = &(p->second);
0144     // dataIface.writeDB( channel, dataitem, iov);
0145     float x = dataitem->getTableX();
0146     float y = dataitem->getTableY();
0147 
0148     xx[count] = x;
0149     yy[count] = y;
0150 
0151     ids_len[count] = sizeof(ids[count]);
0152     iov_len[count] = sizeof(iovid_vec[count]);
0153 
0154     x_len[count] = sizeof(xx[count]);
0155     y_len[count] = sizeof(yy[count]);
0156 
0157     count++;
0158   }
0159 
0160   try {
0161     m_writeStmt->setDataBuffer(1, (dvoid*)iovid_vec, OCCIINT, sizeof(iovid_vec[0]), iov_len);
0162     m_writeStmt->setDataBuffer(2, (dvoid*)ids, OCCIINT, sizeof(ids[0]), ids_len);
0163     m_writeStmt->setDataBuffer(3, (dvoid*)xx, OCCIFLOAT, sizeof(xx[0]), x_len);
0164     m_writeStmt->setDataBuffer(4, (dvoid*)yy, OCCIFLOAT, sizeof(yy[0]), y_len);
0165 
0166     m_writeStmt->executeArrayUpdate(nrows);
0167 
0168     delete[] ids;
0169     delete[] iovid_vec;
0170     delete[] xx;
0171     delete[] yy;
0172 
0173     delete[] ids_len;
0174     delete[] iov_len;
0175     delete[] x_len;
0176     delete[] y_len;
0177 
0178   } catch (SQLException& e) {
0179     throw(std::runtime_error("MonH4TablePositionDat::writeArrayDB():  " + e.getMessage()));
0180   }
0181 }