Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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