Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:32:03

0001 #include <stdexcept>
0002 #include <string>
0003 #include "OnlineDB/Oracle/interface/Oracle.h"
0004 
0005 #include "OnlineDB/EcalCondDB/interface/MonLaserPulseDat.h"
0006 
0007 using namespace std;
0008 using namespace oracle::occi;
0009 
0010 MonLaserPulseDat::MonLaserPulseDat() {
0011   m_env = nullptr;
0012   m_conn = nullptr;
0013   m_writeStmt = nullptr;
0014   m_readStmt = nullptr;
0015 
0016   m_pulseHeightMean = 0;
0017   m_pulseHeightRMS = 0;
0018   m_pulseWidthMean = 0;
0019   m_pulseWidthRMS = 0;
0020 }
0021 
0022 MonLaserPulseDat::~MonLaserPulseDat() {}
0023 
0024 void MonLaserPulseDat::prepareWrite() noexcept(false) {
0025   this->checkConnection();
0026 
0027   try {
0028     m_writeStmt = m_conn->createStatement();
0029     m_writeStmt->setSQL(
0030         "INSERT INTO mon_laser_pulse_dat (iov_id, logic_id, "
0031         "pulse_height_mean, pulse_height_rms, pulse_width_mean, pulse_width_rms) "
0032         "VALUES (:iov_id, :logic_id, "
0033         ":3, :4, :5, :6)");
0034   } catch (SQLException& e) {
0035     throw(std::runtime_error("MonLaserPulseDat::prepareWrite():  " + e.getMessage()));
0036   }
0037 }
0038 
0039 void MonLaserPulseDat::writeDB(const EcalLogicID* ecid, const MonLaserPulseDat* item, MonRunIOV* iov) noexcept(false) {
0040   this->checkConnection();
0041   this->checkPrepare();
0042 
0043   int iovID = iov->fetchID();
0044   if (!iovID) {
0045     throw(std::runtime_error("MonLaserPulseDat::writeDB:  IOV not in DB"));
0046   }
0047 
0048   int logicID = ecid->getLogicID();
0049   if (!logicID) {
0050     throw(std::runtime_error("MonLaserPulseDat::writeDB:  Bad EcalLogicID"));
0051   }
0052 
0053   try {
0054     m_writeStmt->setInt(1, iovID);
0055     m_writeStmt->setInt(2, logicID);
0056 
0057     m_writeStmt->setFloat(3, item->getPulseHeightMean());
0058     m_writeStmt->setFloat(4, item->getPulseHeightRMS());
0059     m_writeStmt->setFloat(5, item->getPulseWidthMean());
0060     m_writeStmt->setFloat(6, item->getPulseWidthRMS());
0061 
0062     m_writeStmt->executeUpdate();
0063   } catch (SQLException& e) {
0064     throw(std::runtime_error("MonLaserPulseDat::writeDB():  " + e.getMessage()));
0065   }
0066 }
0067 
0068 void MonLaserPulseDat::fetchData(std::map<EcalLogicID, MonLaserPulseDat>* fillMap, MonRunIOV* iov) noexcept(false) {
0069   this->checkConnection();
0070   fillMap->clear();
0071 
0072   iov->setConnection(m_env, m_conn);
0073   int iovID = iov->fetchID();
0074   if (!iovID) {
0075     //  throw(std::runtime_error("MonLaserPulseDat::writeDB:  IOV not in DB"));
0076     return;
0077   }
0078 
0079   try {
0080     m_readStmt->setSQL(
0081         "SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
0082         "d.pulse_height_mean, d.pulse_height_rms, d.pulse_width_mean, d.pulse_width_rms "
0083         "FROM channelview cv JOIN mon_laser_pulse_dat d "
0084         "ON cv.logic_id = d.logic_id AND cv.name = cv.maps_to "
0085         "WHERE d.iov_id = :iov_id");
0086     m_readStmt->setInt(1, iovID);
0087     ResultSet* rset = m_readStmt->executeQuery();
0088 
0089     std::pair<EcalLogicID, MonLaserPulseDat> p;
0090     MonLaserPulseDat dat;
0091     while (rset->next()) {
0092       p.first = EcalLogicID(rset->getString(1),   // name
0093                             rset->getInt(2),      // logic_id
0094                             rset->getInt(3),      // id1
0095                             rset->getInt(4),      // id2
0096                             rset->getInt(5),      // id3
0097                             rset->getString(6));  // maps_to
0098 
0099       dat.setPulseHeightMean(rset->getFloat(7));
0100       dat.setPulseHeightRMS(rset->getFloat(8));
0101       dat.setPulseWidthMean(rset->getFloat(9));
0102       dat.setPulseWidthRMS(rset->getFloat(10));
0103 
0104       p.second = dat;
0105       fillMap->insert(p);
0106     }
0107   } catch (SQLException& e) {
0108     throw(std::runtime_error("MonLaserPulseDat::fetchData():  " + e.getMessage()));
0109   }
0110 }
0111 
0112 void MonLaserPulseDat::writeArrayDB(const std::map<EcalLogicID, MonLaserPulseDat>* data,
0113                                     MonRunIOV* iov) noexcept(false) {
0114   this->checkConnection();
0115   this->checkPrepare();
0116 
0117   int iovID = iov->fetchID();
0118   if (!iovID) {
0119     throw(std::runtime_error("MonLaserPulseDat::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   float* ww = new float[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* w_len = new ub2[nrows];
0136 
0137   const EcalLogicID* channel;
0138   const MonLaserPulseDat* dataitem;
0139   int count = 0;
0140   typedef map<EcalLogicID, MonLaserPulseDat>::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("MonLaserPulseDat::writeArrayDB:  Bad EcalLogicID"));
0146     }
0147     ids[count] = logicID;
0148     iovid_vec[count] = iovID;
0149 
0150     dataitem = &(p->second);
0151     // dataIface.writeDB( channel, dataitem, iov);
0152     float x = dataitem->getPulseHeightMean();
0153     float y = dataitem->getPulseHeightRMS();
0154     float z = dataitem->getPulseWidthMean();
0155     float w = dataitem->getPulseWidthRMS();
0156 
0157     xx[count] = x;
0158     yy[count] = y;
0159     zz[count] = z;
0160     ww[count] = w;
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     w_len[count] = sizeof(ww[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*)ww, OCCIFLOAT, sizeof(ww[0]), w_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[] ww;
0189 
0190     delete[] ids_len;
0191     delete[] iov_len;
0192     delete[] x_len;
0193     delete[] y_len;
0194     delete[] z_len;
0195     delete[] w_len;
0196 
0197   } catch (SQLException& e) {
0198     throw(std::runtime_error("MonLaserPulseDat::writeArrayDB():  " + e.getMessage()));
0199   }
0200 }