File indexing completed on 2023-03-17 11:15:11
0001 #include <stdexcept>
0002 #include <string>
0003 #include "OnlineDB/Oracle/interface/Oracle.h"
0004
0005 #include "OnlineDB/EcalCondDB/interface/CaliTempDat.h"
0006 #include "OnlineDB/EcalCondDB/interface/CaliTag.h"
0007 #include "OnlineDB/EcalCondDB/interface/CaliIOV.h"
0008
0009 using namespace std;
0010 using namespace oracle::occi;
0011
0012 CaliTempDat::CaliTempDat() {
0013 m_env = nullptr;
0014 m_conn = nullptr;
0015 m_writeStmt = nullptr;
0016
0017 m_beta = 0;
0018 m_r25 = 0;
0019 m_offset = 0;
0020 m_taskStatus = false;
0021 }
0022
0023 CaliTempDat::~CaliTempDat() {}
0024
0025 void CaliTempDat::prepareWrite() noexcept(false) {
0026 this->checkConnection();
0027
0028 try {
0029 m_writeStmt = m_conn->createStatement();
0030 m_writeStmt->setSQL(
0031 "INSERT INTO cali_temp_dat (iov_id, logic_id, "
0032 " beta, r25, offset, task_status) "
0033 "VALUES (:iov_id, :logic_id, "
0034 ":3, :4, :5, :6 )");
0035 } catch (SQLException& e) {
0036 throw(std::runtime_error("CaliTempDat::prepareWrite(): " + e.getMessage()));
0037 }
0038 }
0039
0040 void CaliTempDat::writeDB(const EcalLogicID* ecid, const CaliTempDat* item, CaliIOV* iov) noexcept(false) {
0041 this->checkConnection();
0042 this->checkPrepare();
0043
0044 int iovID = iov->fetchID();
0045 if (!iovID) {
0046 throw(std::runtime_error("CaliTempDat::writeDB: IOV not in DB"));
0047 }
0048
0049 int logicID = ecid->getLogicID();
0050 if (!logicID) {
0051 throw(std::runtime_error("CaliTempDat::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->getBeta());
0059 m_writeStmt->setFloat(4, item->getR25());
0060 m_writeStmt->setFloat(5, item->getOffset());
0061 m_writeStmt->setInt(6, item->getTaskStatus());
0062
0063 m_writeStmt->executeUpdate();
0064 } catch (SQLException& e) {
0065 throw(std::runtime_error("CaliTempDat::writeDB(): " + e.getMessage()));
0066 }
0067 }
0068
0069 void CaliTempDat::fetchData(std::map<EcalLogicID, CaliTempDat>* fillMap, CaliIOV* 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
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.beta, d.r25, d.offset, d.task_status "
0084 "FROM channelview cv JOIN cali_temp_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, CaliTempDat> p;
0091 CaliTempDat dat;
0092 while (rset->next()) {
0093 p.first = EcalLogicID(rset->getString(1),
0094 rset->getInt(2),
0095 rset->getInt(3),
0096 rset->getInt(4),
0097 rset->getInt(5),
0098 rset->getString(6));
0099
0100 dat.setBeta(rset->getFloat(7));
0101 dat.setR25(rset->getFloat(8));
0102 dat.setOffset(rset->getFloat(9));
0103 dat.setTaskStatus(rset->getInt(10));
0104
0105 p.second = dat;
0106 fillMap->insert(p);
0107 }
0108 } catch (SQLException& e) {
0109 throw(std::runtime_error("CaliTempDat::fetchData(): " + e.getMessage()));
0110 }
0111 }
0112
0113 void CaliTempDat::writeArrayDB(const std::map<EcalLogicID, CaliTempDat>* data, CaliIOV* iov) noexcept(false) {
0114 this->checkConnection();
0115 this->checkPrepare();
0116
0117 int iovID = iov->fetchID();
0118 if (!iovID) {
0119 throw(std::runtime_error("CaliTempDat::writeArrayDB: IOV not in DB"));
0120 }
0121
0122 int nrows = data->size();
0123 int* ids = new int[nrows];
0124 int* iovid_vec = new int[nrows];
0125 float* xx = new float[nrows];
0126 float* yy = new float[nrows];
0127 float* zz = new float[nrows];
0128 int* st = new int[nrows];
0129
0130 ub2* ids_len = new ub2[nrows];
0131 ub2* iov_len = new ub2[nrows];
0132 ub2* x_len = new ub2[nrows];
0133 ub2* y_len = new ub2[nrows];
0134 ub2* z_len = new ub2[nrows];
0135 ub2* st_len = new ub2[nrows];
0136
0137 const EcalLogicID* channel;
0138 const CaliTempDat* dataitem;
0139 int count = 0;
0140 typedef map<EcalLogicID, CaliTempDat>::const_iterator CI;
0141 for (CI p = data->begin(); p != data->end(); ++p) {
0142 channel = &(p->first);
0143 int logicID = channel->getLogicID();
0144 if (!logicID) {
0145 throw(std::runtime_error("CaliTempDat::writeArrayDB: Bad EcalLogicID"));
0146 }
0147 ids[count] = logicID;
0148 iovid_vec[count] = iovID;
0149
0150 dataitem = &(p->second);
0151
0152 float x = dataitem->getBeta();
0153 float y = dataitem->getR25();
0154 float z = dataitem->getOffset();
0155 int statu = dataitem->getTaskStatus();
0156
0157 xx[count] = x;
0158 yy[count] = y;
0159 zz[count] = z;
0160 st[count] = statu;
0161
0162 ids_len[count] = sizeof(ids[count]);
0163 iov_len[count] = sizeof(iovid_vec[count]);
0164
0165 x_len[count] = sizeof(xx[count]);
0166 y_len[count] = sizeof(yy[count]);
0167 z_len[count] = sizeof(zz[count]);
0168 st_len[count] = sizeof(st[count]);
0169
0170 count++;
0171 }
0172
0173 try {
0174 m_writeStmt->setDataBuffer(1, (dvoid*)iovid_vec, OCCIINT, sizeof(iovid_vec[0]), iov_len);
0175 m_writeStmt->setDataBuffer(2, (dvoid*)ids, OCCIINT, sizeof(ids[0]), ids_len);
0176 m_writeStmt->setDataBuffer(3, (dvoid*)xx, OCCIFLOAT, sizeof(xx[0]), x_len);
0177 m_writeStmt->setDataBuffer(4, (dvoid*)yy, OCCIFLOAT, sizeof(yy[0]), y_len);
0178 m_writeStmt->setDataBuffer(5, (dvoid*)zz, OCCIFLOAT, sizeof(zz[0]), z_len);
0179 m_writeStmt->setDataBuffer(6, (dvoid*)st, OCCIINT, sizeof(st[0]), st_len);
0180
0181 m_writeStmt->executeArrayUpdate(nrows);
0182
0183 delete[] ids;
0184 delete[] iovid_vec;
0185 delete[] xx;
0186 delete[] yy;
0187 delete[] zz;
0188 delete[] st;
0189
0190 delete[] ids_len;
0191 delete[] iov_len;
0192 delete[] x_len;
0193 delete[] y_len;
0194 delete[] z_len;
0195
0196 delete[] st_len;
0197
0198 } catch (SQLException& e) {
0199 throw(std::runtime_error("MonPedestalsDat::writeArrayDB(): " + e.getMessage()));
0200 }
0201 }