Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include <stdexcept>
0002 #include <string>
0003 #include "OnlineDB/Oracle/interface/Oracle.h"
0004 
0005 #include "OnlineDB/EcalCondDB/interface/MonOccupancyDat.h"
0006 
0007 using namespace std;
0008 using namespace oracle::occi;
0009 
0010 MonOccupancyDat::MonOccupancyDat() {
0011   m_env = nullptr;
0012   m_conn = nullptr;
0013   m_writeStmt = nullptr;
0014   m_readStmt = nullptr;
0015 
0016   m_eventsOverLowThreshold = 0;
0017   m_eventsOverHighThreshold = 0;
0018   m_avgEnergy = 0;
0019 }
0020 
0021 MonOccupancyDat::~MonOccupancyDat() {}
0022 
0023 void MonOccupancyDat::prepareWrite() noexcept(false) {
0024   this->checkConnection();
0025 
0026   try {
0027     m_writeStmt = m_conn->createStatement();
0028     m_writeStmt->setSQL(
0029         "INSERT INTO mon_occupancy_dat (iov_id, logic_id, "
0030         "events_over_low_threshold, events_over_high_threshold, avg_energy) "
0031         "VALUES (:iov_id, :logic_id, "
0032         ":3, :4, :5)");
0033   } catch (SQLException& e) {
0034     throw(std::runtime_error("MonOccupancyDat::prepareWrite():  " + e.getMessage()));
0035   }
0036 }
0037 
0038 void MonOccupancyDat::writeDB(const EcalLogicID* ecid, const MonOccupancyDat* item, MonRunIOV* iov) noexcept(false) {
0039   this->checkConnection();
0040   this->checkPrepare();
0041 
0042   int iovID = iov->fetchID();
0043   if (!iovID) {
0044     throw(std::runtime_error("MonOccupancyDat::writeDB:  IOV not in DB"));
0045   }
0046 
0047   int logicID = ecid->getLogicID();
0048   if (!logicID) {
0049     throw(std::runtime_error("MonOccupancyDat::writeDB:  Bad EcalLogicID"));
0050   }
0051 
0052   try {
0053     m_writeStmt->setInt(1, iovID);
0054     m_writeStmt->setInt(2, logicID);
0055 
0056     m_writeStmt->setInt(3, item->getEventsOverLowThreshold());
0057     m_writeStmt->setInt(4, item->getEventsOverHighThreshold());
0058     m_writeStmt->setFloat(5, item->getAvgEnergy());
0059 
0060     m_writeStmt->executeUpdate();
0061   } catch (SQLException& e) {
0062     throw(std::runtime_error("MonOccupancyDat::writeDB():  " + e.getMessage()));
0063   }
0064 }
0065 
0066 void MonOccupancyDat::fetchData(std::map<EcalLogicID, MonOccupancyDat>* 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     //  throw(std::runtime_error("MonOccupancyDat::writeDB:  IOV not in DB"));
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.events_over_low_threshold, d.events_over_high_threshold, d.avg_energy "
0081         "FROM channelview cv JOIN mon_occupancy_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, MonOccupancyDat> p;
0088     MonOccupancyDat dat;
0089     while (rset->next()) {
0090       p.first = EcalLogicID(rset->getString(1),   // name
0091                             rset->getInt(2),      // logic_id
0092                             rset->getInt(3),      // id1
0093                             rset->getInt(4),      // id2
0094                             rset->getInt(5),      // id3
0095                             rset->getString(6));  // maps_to
0096 
0097       dat.setEventsOverLowThreshold(rset->getInt(7));
0098       dat.setEventsOverHighThreshold(rset->getInt(8));
0099       dat.setAvgEnergy(rset->getFloat(9));
0100 
0101       p.second = dat;
0102       fillMap->insert(p);
0103     }
0104   } catch (SQLException& e) {
0105     throw(std::runtime_error("MonOccupancyDat::fetchData():  " + e.getMessage()));
0106   }
0107 }
0108 
0109 void MonOccupancyDat::writeArrayDB(const std::map<EcalLogicID, MonOccupancyDat>* data, MonRunIOV* iov) noexcept(false) {
0110   this->checkConnection();
0111   this->checkPrepare();
0112 
0113   int iovID = iov->fetchID();
0114   if (!iovID) {
0115     throw(std::runtime_error("MonOccupancyDat::writeArrayDB:  IOV not in DB"));
0116   }
0117 
0118   int nrows = data->size();
0119   int* ids = new int[nrows];
0120   int* iovid_vec = new int[nrows];
0121   int* xx = new int[nrows];
0122   int* yy = new int[nrows];
0123   float* zz = new float[nrows];
0124 
0125   ub2* ids_len = new ub2[nrows];
0126   ub2* iov_len = new ub2[nrows];
0127   ub2* x_len = new ub2[nrows];
0128   ub2* y_len = new ub2[nrows];
0129   ub2* z_len = new ub2[nrows];
0130 
0131   const EcalLogicID* channel;
0132   const MonOccupancyDat* dataitem;
0133   int count = 0;
0134   typedef map<EcalLogicID, MonOccupancyDat>::const_iterator CI;
0135   for (CI p = data->begin(); p != data->end(); ++p) {
0136     channel = &(p->first);
0137     int logicID = channel->getLogicID();
0138     if (!logicID) {
0139       throw(std::runtime_error("MonOccupancyDat::writeArrayDB:  Bad EcalLogicID"));
0140     }
0141     ids[count] = logicID;
0142     iovid_vec[count] = iovID;
0143 
0144     dataitem = &(p->second);
0145     // dataIface.writeDB( channel, dataitem, iov);
0146     int x = dataitem->getEventsOverLowThreshold();
0147     int y = dataitem->getEventsOverHighThreshold();
0148     float z = dataitem->getAvgEnergy();
0149 
0150     xx[count] = x;
0151     yy[count] = y;
0152     zz[count] = z;
0153 
0154     ids_len[count] = sizeof(ids[count]);
0155     iov_len[count] = sizeof(iovid_vec[count]);
0156 
0157     x_len[count] = sizeof(xx[count]);
0158     y_len[count] = sizeof(yy[count]);
0159     z_len[count] = sizeof(zz[count]);
0160 
0161     count++;
0162   }
0163 
0164   try {
0165     m_writeStmt->setDataBuffer(1, (dvoid*)iovid_vec, OCCIINT, sizeof(iovid_vec[0]), iov_len);
0166     m_writeStmt->setDataBuffer(2, (dvoid*)ids, OCCIINT, sizeof(ids[0]), ids_len);
0167     m_writeStmt->setDataBuffer(3, (dvoid*)xx, OCCIINT, sizeof(xx[0]), x_len);
0168     m_writeStmt->setDataBuffer(4, (dvoid*)yy, OCCIINT, sizeof(yy[0]), y_len);
0169     m_writeStmt->setDataBuffer(5, (dvoid*)zz, OCCIFLOAT, sizeof(zz[0]), z_len);
0170 
0171     m_writeStmt->executeArrayUpdate(nrows);
0172 
0173     delete[] ids;
0174     delete[] iovid_vec;
0175     delete[] xx;
0176     delete[] yy;
0177     delete[] zz;
0178 
0179     delete[] ids_len;
0180     delete[] iov_len;
0181     delete[] x_len;
0182     delete[] y_len;
0183     delete[] z_len;
0184 
0185   } catch (SQLException& e) {
0186     throw(std::runtime_error("MonOccupancyDat::writeArrayDB():  " + e.getMessage()));
0187   }
0188 }