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