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/DCUCapsuleTempDat.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 DCUCapsuleTempDat::DCUCapsuleTempDat() {
0013   m_env = nullptr;
0014   m_conn = nullptr;
0015   m_writeStmt = nullptr;
0016   m_readStmt = nullptr;
0017 
0018   m_capsuleTemp = 0;
0019 }
0020 
0021 DCUCapsuleTempDat::~DCUCapsuleTempDat() {}
0022 
0023 void DCUCapsuleTempDat::prepareWrite() noexcept(false) {
0024   this->checkConnection();
0025 
0026   try {
0027     m_writeStmt = m_conn->createStatement();
0028     m_writeStmt->setSQL(
0029         "INSERT INTO dcu_capsule_temp_dat (iov_id, logic_id, "
0030         "capsule_temp) "
0031         "VALUES (:iov_id, :logic_id, "
0032         ":capsule_temp)");
0033   } catch (SQLException& e) {
0034     throw(std::runtime_error("DCUCapsuleTempDat::prepareWrite():  " + e.getMessage()));
0035   }
0036 }
0037 
0038 void DCUCapsuleTempDat::writeDB(const EcalLogicID* ecid, const DCUCapsuleTempDat* 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("DCUCapsuleTempDat::writeDB:  IOV not in DB"));
0045   }
0046 
0047   int logicID = ecid->getLogicID();
0048   if (!logicID) {
0049     throw(std::runtime_error("DCUCapsuleTempDat::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->getCapsuleTemp());
0057 
0058     m_writeStmt->executeUpdate();
0059   } catch (SQLException& e) {
0060     throw(std::runtime_error("DCUCapsuleTempDat::writeDB():  " + e.getMessage()));
0061   }
0062 }
0063 
0064 void DCUCapsuleTempDat::fetchData(std::map<EcalLogicID, DCUCapsuleTempDat>* 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("DCUCapsuleTempDat::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.capsule_temp "
0079         "FROM channelview cv JOIN dcu_capsule_temp_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, DCUCapsuleTempDat> p;
0086     DCUCapsuleTempDat 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.setCapsuleTemp(rset->getFloat(7));
0096 
0097       p.second = dat;
0098       fillMap->insert(p);
0099     }
0100   } catch (SQLException& e) {
0101     throw(std::runtime_error("DCUCapsuleTempDat::fetchData():  " + e.getMessage()));
0102   }
0103 }
0104 void DCUCapsuleTempDat::writeArrayDB(const std::map<EcalLogicID, DCUCapsuleTempDat>* data,
0105                                      DCUIOV* iov) noexcept(false) {
0106   this->checkConnection();
0107   this->checkPrepare();
0108 
0109   int iovID = iov->fetchID();
0110   if (!iovID) {
0111     throw(std::runtime_error("DCUCapsuleTempDat::writeArrayDB:  IOV not in DB"));
0112   }
0113 
0114   int nrows = data->size();
0115   int* ids = new int[nrows];
0116   int* iovid_vec = new int[nrows];
0117   float* xx = new float[nrows];
0118 
0119   ub2* ids_len = new ub2[nrows];
0120   ub2* iov_len = new ub2[nrows];
0121   ub2* x_len = new ub2[nrows];
0122 
0123   const EcalLogicID* channel;
0124   const DCUCapsuleTempDat* dataitem;
0125   int count = 0;
0126   typedef map<EcalLogicID, DCUCapsuleTempDat>::const_iterator CI;
0127   for (CI p = data->begin(); p != data->end(); ++p) {
0128     channel = &(p->first);
0129     int logicID = channel->getLogicID();
0130     if (!logicID) {
0131       throw(std::runtime_error("DCUCapsuleTempDat::writeArrayDB:  Bad EcalLogicID"));
0132     }
0133     ids[count] = logicID;
0134     iovid_vec[count] = iovID;
0135 
0136     dataitem = &(p->second);
0137     // dataIface.writeDB( channel, dataitem, iov);
0138     float x = dataitem->getCapsuleTemp();
0139 
0140     xx[count] = x;
0141 
0142     ids_len[count] = sizeof(ids[count]);
0143     iov_len[count] = sizeof(iovid_vec[count]);
0144 
0145     x_len[count] = sizeof(xx[count]);
0146 
0147     count++;
0148   }
0149 
0150   try {
0151     m_writeStmt->setDataBuffer(1, (dvoid*)iovid_vec, OCCIINT, sizeof(iovid_vec[0]), iov_len);
0152     m_writeStmt->setDataBuffer(2, (dvoid*)ids, OCCIINT, sizeof(ids[0]), ids_len);
0153     m_writeStmt->setDataBuffer(3, (dvoid*)xx, OCCIFLOAT, sizeof(xx[0]), x_len);
0154 
0155     m_writeStmt->executeArrayUpdate(nrows);
0156 
0157     delete[] ids;
0158     delete[] iovid_vec;
0159     delete[] xx;
0160 
0161     delete[] ids_len;
0162     delete[] iov_len;
0163     delete[] x_len;
0164 
0165   } catch (SQLException& e) {
0166     throw(std::runtime_error("DCUCapsuleTempDat::writeArrayDB():  " + e.getMessage()));
0167   }
0168 }