Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include <stdexcept>
0002 #include <string>
0003 #include "OnlineDB/Oracle/interface/Oracle.h"
0004 
0005 #include "OnlineDB/EcalCondDB/interface/DCUCapsuleTempRawDat.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 DCUCapsuleTempRawDat::DCUCapsuleTempRawDat() {
0013   m_env = nullptr;
0014   m_conn = nullptr;
0015   m_writeStmt = nullptr;
0016   m_readStmt = nullptr;
0017 
0018   m_capsuleTempADC = 0;
0019   m_capsuleTempRMS = 0;
0020 }
0021 
0022 DCUCapsuleTempRawDat::~DCUCapsuleTempRawDat() {}
0023 
0024 void DCUCapsuleTempRawDat::prepareWrite() noexcept(false) {
0025   this->checkConnection();
0026 
0027   try {
0028     m_writeStmt = m_conn->createStatement();
0029     m_writeStmt->setSQL(
0030         "INSERT INTO dcu_capsule_temp_raw_dat (iov_id, logic_id, "
0031         "capsule_temp_adc, capsule_temp_rms) "
0032         "VALUES (:iov_id, :logic_id, "
0033         ":3, :4)");
0034   } catch (SQLException& e) {
0035     throw(std::runtime_error("DCUCapsuleTempRawDat::prepareWrite():  " + e.getMessage()));
0036   }
0037 }
0038 
0039 void DCUCapsuleTempRawDat::writeDB(const EcalLogicID* ecid,
0040                                    const DCUCapsuleTempRawDat* item,
0041                                    DCUIOV* iov) noexcept(false) {
0042   this->checkConnection();
0043   this->checkPrepare();
0044 
0045   int iovID = iov->fetchID();
0046   if (!iovID) {
0047     throw(std::runtime_error("DCUCapsuleTempRawDat::writeDB:  IOV not in DB"));
0048   }
0049 
0050   int logicID = ecid->getLogicID();
0051   if (!logicID) {
0052     throw(std::runtime_error("DCUCapsuleTempRawDat::writeDB:  Bad EcalLogicID"));
0053   }
0054 
0055   try {
0056     m_writeStmt->setInt(1, iovID);
0057     m_writeStmt->setInt(2, logicID);
0058 
0059     m_writeStmt->setFloat(3, item->getCapsuleTempADC());
0060     m_writeStmt->setFloat(4, item->getCapsuleTempRMS());
0061 
0062     m_writeStmt->executeUpdate();
0063   } catch (SQLException& e) {
0064     throw(std::runtime_error("DCUCapsuleTempRawDat::writeDB():  " + e.getMessage()));
0065   }
0066 }
0067 
0068 void DCUCapsuleTempRawDat::fetchData(std::map<EcalLogicID, DCUCapsuleTempRawDat>* fillMap,
0069                                      DCUIOV* iov) noexcept(false) {
0070   this->checkConnection();
0071   fillMap->clear();
0072 
0073   iov->setConnection(m_env, m_conn);
0074   int iovID = iov->fetchID();
0075   if (!iovID) {
0076     //  throw(std::runtime_error("DCUCapsuleTempRawDat::writeDB:  IOV not in DB"));
0077     return;
0078   }
0079 
0080   try {
0081     m_readStmt->setSQL(
0082         "SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
0083         "d.capsule_temp_adc, d.capsule_temp_rms "
0084         "FROM channelview cv JOIN dcu_capsule_temp_raw_dat d "
0085         "ON cv.logic_id = d.logic_id AND cv.name = cv.maps_to "
0086         "WHERE d.iov_id = :iov_id");
0087     m_readStmt->setInt(1, iovID);
0088     ResultSet* rset = m_readStmt->executeQuery();
0089 
0090     std::pair<EcalLogicID, DCUCapsuleTempRawDat> p;
0091     DCUCapsuleTempRawDat dat;
0092     while (rset->next()) {
0093       p.first = EcalLogicID(rset->getString(1),   // name
0094                             rset->getInt(2),      // logic_id
0095                             rset->getInt(3),      // id1
0096                             rset->getInt(4),      // id2
0097                             rset->getInt(5),      // id3
0098                             rset->getString(6));  // maps_to
0099 
0100       dat.setCapsuleTempADC(rset->getFloat(7));
0101       dat.setCapsuleTempRMS(rset->getFloat(8));
0102 
0103       p.second = dat;
0104       fillMap->insert(p);
0105     }
0106   } catch (SQLException& e) {
0107     throw(std::runtime_error("DCUCapsuleTempRawDat::fetchData():  " + e.getMessage()));
0108   }
0109 }
0110 
0111 void DCUCapsuleTempRawDat::writeArrayDB(const std::map<EcalLogicID, DCUCapsuleTempRawDat>* data,
0112                                         DCUIOV* iov) noexcept(false) {
0113   this->checkConnection();
0114   this->checkPrepare();
0115 
0116   int iovID = iov->fetchID();
0117   if (!iovID) {
0118     throw(std::runtime_error("DCUCapsuleTempRawDat::writeArrayDB:  IOV not in DB"));
0119   }
0120 
0121   int nrows = data->size();
0122   int* ids = new int[nrows];
0123   int* iovid_vec = new int[nrows];
0124   float* xx = new float[nrows];
0125   float* yy = 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 
0132   const EcalLogicID* channel;
0133   const DCUCapsuleTempRawDat* dataitem;
0134   int count = 0;
0135   typedef map<EcalLogicID, DCUCapsuleTempRawDat>::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("DCUCapsuleTempRawDat::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->getCapsuleTempADC();
0148     float y = dataitem->getCapsuleTempRMS();
0149 
0150     xx[count] = x;
0151     yy[count] = y;
0152 
0153     ids_len[count] = sizeof(ids[count]);
0154     iov_len[count] = sizeof(iovid_vec[count]);
0155 
0156     x_len[count] = sizeof(xx[count]);
0157     y_len[count] = sizeof(yy[count]);
0158 
0159     count++;
0160   }
0161 
0162   try {
0163     m_writeStmt->setDataBuffer(1, (dvoid*)iovid_vec, OCCIINT, sizeof(iovid_vec[0]), iov_len);
0164     m_writeStmt->setDataBuffer(2, (dvoid*)ids, OCCIINT, sizeof(ids[0]), ids_len);
0165     m_writeStmt->setDataBuffer(3, (dvoid*)xx, OCCIFLOAT, sizeof(xx[0]), x_len);
0166     m_writeStmt->setDataBuffer(4, (dvoid*)yy, OCCIFLOAT, sizeof(yy[0]), y_len);
0167 
0168     m_writeStmt->executeArrayUpdate(nrows);
0169 
0170     delete[] ids;
0171     delete[] iovid_vec;
0172     delete[] xx;
0173     delete[] yy;
0174 
0175     delete[] ids_len;
0176     delete[] iov_len;
0177     delete[] x_len;
0178     delete[] y_len;
0179 
0180   } catch (SQLException& e) {
0181     throw(std::runtime_error("DCUCapsuleTempRawDat::writeArrayDB():  " + e.getMessage()));
0182   }
0183 }