Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:31:59

0001 #include <stdexcept>
0002 #include <string>
0003 #include "OnlineDB/Oracle/interface/Oracle.h"
0004 
0005 #include "OnlineDB/EcalCondDB/interface/FEConfigPedDat.h"
0006 #include "OnlineDB/EcalCondDB/interface/FEConfigPedInfo.h"
0007 
0008 using namespace std;
0009 using namespace oracle::occi;
0010 
0011 FEConfigPedDat::FEConfigPedDat() {
0012   m_env = nullptr;
0013   m_conn = nullptr;
0014   m_writeStmt = nullptr;
0015   m_readStmt = nullptr;
0016   m_ID = 0;
0017   m_pedMeanG1 = 0;
0018   m_pedMeanG6 = 0;
0019   m_pedMeanG12 = 0;
0020 }
0021 
0022 FEConfigPedDat::~FEConfigPedDat() {}
0023 
0024 void FEConfigPedDat::prepareWrite() noexcept(false) {
0025   this->checkConnection();
0026 
0027   try {
0028     m_writeStmt = m_conn->createStatement();
0029 
0030     m_writeStmt->setSQL("INSERT INTO " + getTable() +
0031                         " (ped_conf_id, logic_id, "
0032                         "mean_12, mean_6, mean_1 ) "
0033                         "VALUES (:ped_conf_id, :logic_id, "
0034                         ":ped_mean_g12, :ped_mean_g6, :ped_mean_g1 )");
0035   } catch (SQLException& e) {
0036     throw(std::runtime_error("FEConfigPedDat::prepareWrite():  " + e.getMessage()));
0037   }
0038 }
0039 
0040 void FEConfigPedDat::writeDB(const EcalLogicID* ecid,
0041                              const FEConfigPedDat* item,
0042                              FEConfigPedInfo* iconf) noexcept(false) {
0043   this->checkConnection();
0044 
0045   int iconfID = iconf->fetchID();
0046   if (!iconfID) {
0047     throw(std::runtime_error("FEConfigPedDat::writeDB:  ICONF not in DB"));
0048   }
0049 
0050   int logicID = ecid->getLogicID();
0051   if (!logicID) {
0052     throw(std::runtime_error("FEConfigPedDat::writeDB:  Bad EcalLogicID"));
0053   }
0054 
0055   try {
0056     m_writeStmt->setInt(1, iconfID);
0057     m_writeStmt->setInt(2, logicID);
0058     m_writeStmt->setFloat(3, item->getPedMeanG12());
0059     m_writeStmt->setFloat(4, item->getPedMeanG6());
0060     m_writeStmt->setFloat(5, item->getPedMeanG1());
0061 
0062     m_writeStmt->executeUpdate();
0063   } catch (SQLException& e) {
0064     throw(std::runtime_error("FEConfigPedDat::writeDB():  " + e.getMessage()));
0065   }
0066 }
0067 
0068 void FEConfigPedDat::fetchData(map<EcalLogicID, FEConfigPedDat>* fillMap, FEConfigPedInfo* iconf) noexcept(false) {
0069   this->checkConnection();
0070   fillMap->clear();
0071 
0072   iconf->setConnection(m_env, m_conn);
0073   int iconfID = iconf->fetchID();
0074   if (!iconfID) {
0075     //  throw(std::runtime_error("FEConfigPedDat::writeDB:  ICONF 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.mean_12, d.mean_6, d.mean_1 "
0083         "FROM channelview cv JOIN fe_config_ped_dat d "
0084         "ON cv.logic_id = d.logic_id AND cv.name = cv.maps_to "
0085         "WHERE ped_conf_id = :ped_conf_id");
0086     m_readStmt->setInt(1, iconfID);
0087     ResultSet* rset = m_readStmt->executeQuery();
0088 
0089     std::pair<EcalLogicID, FEConfigPedDat> p;
0090     FEConfigPedDat 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.setPedMeanG12(rset->getFloat(7));
0100       dat.setPedMeanG6(rset->getFloat(8));
0101       dat.setPedMeanG1(rset->getFloat(9));
0102 
0103       p.second = dat;
0104       fillMap->insert(p);
0105     }
0106   } catch (SQLException& e) {
0107     throw(std::runtime_error("FEConfigPedDat::fetchData:  " + e.getMessage()));
0108   }
0109 }
0110 
0111 void FEConfigPedDat::writeArrayDB(const std::map<EcalLogicID, FEConfigPedDat>* data,
0112                                   FEConfigPedInfo* iconf) noexcept(false) {
0113   this->checkConnection();
0114   this->checkPrepare();
0115 
0116   int iconfID = iconf->fetchID();
0117   if (!iconfID) {
0118     throw(std::runtime_error("FEConfigPedDat::writeArrayDB:  ICONF not in DB"));
0119   }
0120 
0121   int nrows = data->size();
0122   int* ids = new int[nrows];
0123   int* iconfid_vec = new int[nrows];
0124   float* xx = new float[nrows];
0125   float* yy = new float[nrows];
0126   float* zz = new float[nrows];
0127 
0128   ub2* ids_len = new ub2[nrows];
0129   ub2* iconf_len = new ub2[nrows];
0130   ub2* x_len = new ub2[nrows];
0131   ub2* y_len = new ub2[nrows];
0132   ub2* z_len = new ub2[nrows];
0133 
0134   const EcalLogicID* channel;
0135   const FEConfigPedDat* dataitem;
0136   int count = 0;
0137   typedef map<EcalLogicID, FEConfigPedDat>::const_iterator CI;
0138   for (CI p = data->begin(); p != data->end(); ++p) {
0139     channel = &(p->first);
0140     int logicID = channel->getLogicID();
0141     if (!logicID) {
0142       throw(std::runtime_error("FEConfigPedDat::writeArrayDB:  Bad EcalLogicID"));
0143     }
0144     ids[count] = logicID;
0145     iconfid_vec[count] = iconfID;
0146 
0147     dataitem = &(p->second);
0148     // dataIface.writeDB( channel, dataitem, iconf);
0149     float x = dataitem->getPedMeanG12();
0150     float y = dataitem->getPedMeanG6();
0151     float z = dataitem->getPedMeanG1();
0152 
0153     xx[count] = x;
0154     yy[count] = y;
0155     zz[count] = z;
0156 
0157     ids_len[count] = sizeof(ids[count]);
0158     iconf_len[count] = sizeof(iconfid_vec[count]);
0159 
0160     x_len[count] = sizeof(xx[count]);
0161     y_len[count] = sizeof(yy[count]);
0162     z_len[count] = sizeof(zz[count]);
0163 
0164     count++;
0165   }
0166 
0167   try {
0168     m_writeStmt->setDataBuffer(1, (dvoid*)iconfid_vec, OCCIINT, sizeof(iconfid_vec[0]), iconf_len);
0169     m_writeStmt->setDataBuffer(2, (dvoid*)ids, OCCIINT, sizeof(ids[0]), ids_len);
0170     m_writeStmt->setDataBuffer(3, (dvoid*)xx, OCCIFLOAT, sizeof(xx[0]), x_len);
0171     m_writeStmt->setDataBuffer(4, (dvoid*)yy, OCCIFLOAT, sizeof(yy[0]), y_len);
0172     m_writeStmt->setDataBuffer(5, (dvoid*)zz, OCCIFLOAT, sizeof(zz[0]), z_len);
0173 
0174     m_writeStmt->executeArrayUpdate(nrows);
0175 
0176     delete[] ids;
0177     delete[] iconfid_vec;
0178     delete[] xx;
0179     delete[] yy;
0180     delete[] zz;
0181 
0182     delete[] ids_len;
0183     delete[] iconf_len;
0184     delete[] x_len;
0185     delete[] y_len;
0186     delete[] z_len;
0187 
0188   } catch (SQLException& e) {
0189     throw(std::runtime_error("FEConfigPedDat::writeArrayDB():  " + e.getMessage()));
0190   }
0191 }