Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:15:20

0001 #include <stdexcept>
0002 #include <string>
0003 #include "OnlineDB/Oracle/interface/Oracle.h"
0004 
0005 #include "OnlineDB/EcalCondDB/interface/MonPedestalsOnlineDat.h"
0006 #include "OnlineDB/EcalCondDB/interface/MonRunTag.h"
0007 #include "OnlineDB/EcalCondDB/interface/MonRunIOV.h"
0008 
0009 using namespace std;
0010 using namespace oracle::occi;
0011 
0012 MonPedestalsOnlineDat::MonPedestalsOnlineDat() {
0013   m_env = nullptr;
0014   m_conn = nullptr;
0015   m_writeStmt = nullptr;
0016   m_readStmt = nullptr;
0017 
0018   m_adcMeanG12 = 0;
0019   m_adcRMSG12 = 0;
0020   m_taskStatus = false;
0021 }
0022 
0023 MonPedestalsOnlineDat::~MonPedestalsOnlineDat() {}
0024 
0025 void MonPedestalsOnlineDat::prepareWrite() noexcept(false) {
0026   this->checkConnection();
0027 
0028   try {
0029     m_writeStmt = m_conn->createStatement();
0030     m_writeStmt->setSQL(
0031         "INSERT INTO mon_pedestals_online_dat (iov_id, logic_id, "
0032         "adc_mean_g12, adc_rms_g12, task_status) "
0033         "VALUES (:iov_id, :logic_id, "
0034         ":adc_mean_g12, :adc_rms_g12, :task_status)");
0035   } catch (SQLException& e) {
0036     throw(std::runtime_error("MonPedestalsOnlineDat::prepareWrite():  " + e.getMessage()));
0037   }
0038 }
0039 
0040 void MonPedestalsOnlineDat::writeDB(const EcalLogicID* ecid,
0041                                     const MonPedestalsOnlineDat* item,
0042                                     MonRunIOV* iov) noexcept(false) {
0043   this->checkConnection();
0044   this->checkPrepare();
0045 
0046   int iovID = iov->fetchID();
0047   if (!iovID) {
0048     throw(std::runtime_error("MonPedestalsOnlineDat::writeDB:  IOV not in DB"));
0049   }
0050 
0051   int logicID = ecid->getLogicID();
0052   if (!logicID) {
0053     throw(std::runtime_error("MonPedestalsOnlineDat::writeDB:  Bad EcalLogicID"));
0054   }
0055 
0056   try {
0057     m_writeStmt->setInt(1, iovID);
0058     m_writeStmt->setInt(2, logicID);
0059     m_writeStmt->setFloat(3, item->getADCMeanG12());
0060     m_writeStmt->setFloat(4, item->getADCRMSG12());
0061     m_writeStmt->setInt(5, item->getTaskStatus());
0062 
0063     m_writeStmt->executeUpdate();
0064   } catch (SQLException& e) {
0065     throw(std::runtime_error("MonPedestalsOnlineDat::writeDB():  " + e.getMessage()));
0066   }
0067 }
0068 
0069 void MonPedestalsOnlineDat::fetchData(std::map<EcalLogicID, MonPedestalsOnlineDat>* fillMap,
0070                                       MonRunIOV* iov) noexcept(false) {
0071   this->checkConnection();
0072   fillMap->clear();
0073 
0074   iov->setConnection(m_env, m_conn);
0075   int iovID = iov->fetchID();
0076   if (!iovID) {
0077     //  throw(std::runtime_error("MonPedestalsOnlineDat::writeDB:  IOV not in DB"));
0078     return;
0079   }
0080 
0081   try {
0082     m_readStmt->setSQL(
0083         "SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
0084         "d.adc_mean_g12, d.adc_rms_g12, d.task_status "
0085         "FROM channelview cv JOIN mon_pedestals_online_dat d "
0086         "ON cv.logic_id = d.logic_id AND cv.name = cv.maps_to "
0087         "WHERE d.iov_id = :iov_id");
0088     m_readStmt->setInt(1, iovID);
0089     ResultSet* rset = m_readStmt->executeQuery();
0090 
0091     std::pair<EcalLogicID, MonPedestalsOnlineDat> p;
0092     MonPedestalsOnlineDat dat;
0093     while (rset->next()) {
0094       p.first = EcalLogicID(rset->getString(1),   // name
0095                             rset->getInt(2),      // logic_id
0096                             rset->getInt(3),      // id1
0097                             rset->getInt(4),      // id2
0098                             rset->getInt(5),      // id3
0099                             rset->getString(6));  // maps_to
0100 
0101       dat.setADCMeanG12(rset->getFloat(7));
0102       dat.setADCRMSG12(rset->getFloat(8));
0103       dat.setTaskStatus(rset->getInt(9));
0104 
0105       p.second = dat;
0106       fillMap->insert(p);
0107     }
0108   } catch (SQLException& e) {
0109     throw(std::runtime_error("MonPedestalsOnlineDat::fetchData():  " + e.getMessage()));
0110   }
0111 }
0112 
0113 void MonPedestalsOnlineDat::writeArrayDB(const std::map<EcalLogicID, MonPedestalsOnlineDat>* data,
0114                                          MonRunIOV* iov) noexcept(false) {
0115   this->checkConnection();
0116   this->checkPrepare();
0117 
0118   int iovID = iov->fetchID();
0119   if (!iovID) {
0120     throw(std::runtime_error("MonPedestalsOnlineDat::writeArrayDB:  IOV not in DB"));
0121   }
0122 
0123   int nrows = data->size();
0124   int* ids = new int[nrows];
0125   int* iovid_vec = new int[nrows];
0126   float* xx = new float[nrows];
0127   float* yy = 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* st_len = new ub2[nrows];
0135 
0136   const EcalLogicID* channel;
0137   const MonPedestalsOnlineDat* dataitem;
0138   int count = 0;
0139   typedef map<EcalLogicID, MonPedestalsOnlineDat>::const_iterator CI;
0140   for (CI p = data->begin(); p != data->end(); ++p) {
0141     channel = &(p->first);
0142     int logicID = channel->getLogicID();
0143     if (!logicID) {
0144       throw(std::runtime_error("MonPedestalsOnlineDat::writeArrayDB:  Bad EcalLogicID"));
0145     }
0146     ids[count] = logicID;
0147     iovid_vec[count] = iovID;
0148 
0149     dataitem = &(p->second);
0150     // dataIface.writeDB( channel, dataitem, iov);
0151     float x = dataitem->getADCMeanG12();
0152     float y = dataitem->getADCRMSG12();
0153     int statu = dataitem->getTaskStatus();
0154 
0155     xx[count] = x;
0156     yy[count] = y;
0157     st[count] = statu;
0158 
0159     ids_len[count] = sizeof(ids[count]);
0160     iov_len[count] = sizeof(iovid_vec[count]);
0161 
0162     x_len[count] = sizeof(xx[count]);
0163     y_len[count] = sizeof(yy[count]);
0164     st_len[count] = sizeof(st[count]);
0165 
0166     count++;
0167   }
0168 
0169   try {
0170     m_writeStmt->setDataBuffer(1, (dvoid*)iovid_vec, OCCIINT, sizeof(iovid_vec[0]), iov_len);
0171     m_writeStmt->setDataBuffer(2, (dvoid*)ids, OCCIINT, sizeof(ids[0]), ids_len);
0172     m_writeStmt->setDataBuffer(3, (dvoid*)xx, OCCIFLOAT, sizeof(xx[0]), x_len);
0173     m_writeStmt->setDataBuffer(4, (dvoid*)yy, OCCIFLOAT, sizeof(yy[0]), y_len);
0174     m_writeStmt->setDataBuffer(5, (dvoid*)st, OCCIINT, sizeof(st[0]), st_len);
0175 
0176     m_writeStmt->executeArrayUpdate(nrows);
0177 
0178     delete[] ids;
0179     delete[] iovid_vec;
0180     delete[] xx;
0181     delete[] yy;
0182     delete[] st;
0183 
0184     delete[] ids_len;
0185     delete[] iov_len;
0186     delete[] x_len;
0187     delete[] y_len;
0188     delete[] st_len;
0189 
0190   } catch (SQLException& e) {
0191     throw(std::runtime_error("MonPedestalsDat::writeArrayDB():  " + e.getMessage()));
0192   }
0193 }