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