Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:04

0001 #include <stdexcept>
0002 #include <string>
0003 #include "OnlineDB/Oracle/interface/Oracle.h"
0004 
0005 #include "OnlineDB/EcalCondDB/interface/FEConfigWeightGroupDat.h"
0006 #include "OnlineDB/EcalCondDB/interface/FEConfigWeightInfo.h"
0007 
0008 using namespace std;
0009 using namespace oracle::occi;
0010 
0011 FEConfigWeightGroupDat::FEConfigWeightGroupDat() {
0012   m_env = nullptr;
0013   m_conn = nullptr;
0014   m_writeStmt = nullptr;
0015   m_readStmt = nullptr;
0016 
0017   m_group_id = 0;
0018   m_w0 = 0;
0019   m_w1 = 0;
0020   m_w2 = 0;
0021   m_w3 = 0;
0022   m_w4 = 0;
0023 }
0024 
0025 FEConfigWeightGroupDat::~FEConfigWeightGroupDat() {}
0026 
0027 void FEConfigWeightGroupDat::prepareWrite() noexcept(false) {
0028   this->checkConnection();
0029 
0030   try {
0031     m_writeStmt = m_conn->createStatement();
0032     m_writeStmt->setSQL(
0033         "INSERT INTO fe_weight_per_group_dat (wei_conf_id, group_id, "
0034         " w0, w1, w2, w3, w4 ) "
0035         "VALUES (:wei_conf_id, :group_id, "
0036         ":w0, :w1, :w2, :w3, :w4 )");
0037   } catch (SQLException& e) {
0038     throw(std::runtime_error("FEConfigWeightGroupDat::prepareWrite():  " + e.getMessage()));
0039   }
0040 }
0041 
0042 void FEConfigWeightGroupDat::writeDB(const EcalLogicID* ecid,
0043                                      const FEConfigWeightGroupDat* item,
0044                                      FEConfigWeightInfo* iconf) noexcept(false) {
0045   this->checkConnection();
0046   this->checkPrepare();
0047 
0048   int iconfID = iconf->fetchID();
0049   if (!iconfID) {
0050     throw(std::runtime_error("FEConfigWeightGroupDat::writeDB:  ICONF not in DB"));
0051   }
0052   /* no need for the logic id in this table 
0053      int logicID = ecid->getLogicID();
0054      if (!logicID) { throw(std::runtime_error("FEConfigWeightGroupDat::writeDB:  Bad EcalLogicID")); }
0055   */
0056 
0057   try {
0058     m_writeStmt->setInt(1, iconfID);
0059 
0060     m_writeStmt->setInt(2, item->getWeightGroupId());
0061     m_writeStmt->setFloat(3, item->getWeight0());
0062     m_writeStmt->setFloat(4, item->getWeight1());
0063     m_writeStmt->setFloat(5, item->getWeight2());
0064     m_writeStmt->setFloat(6, item->getWeight3());
0065     m_writeStmt->setFloat(7, item->getWeight4());
0066 
0067     m_writeStmt->executeUpdate();
0068   } catch (SQLException& e) {
0069     throw(std::runtime_error("FEConfigWeightGroupDat::writeDB():  " + e.getMessage()));
0070   }
0071 }
0072 
0073 void FEConfigWeightGroupDat::fetchData(map<EcalLogicID, FEConfigWeightGroupDat>* fillMap,
0074                                        FEConfigWeightInfo* iconf) noexcept(false) {
0075   this->checkConnection();
0076   fillMap->clear();
0077 
0078   iconf->setConnection(m_env, m_conn);
0079   int iconfID = iconf->fetchID();
0080   if (!iconfID) {
0081     throw(std::runtime_error("FEConfigWeightGroupDat::fetchData:  ICONF not in DB"));
0082     return;
0083   }
0084 
0085   try {
0086     m_readStmt->setSQL(
0087         "SELECT d.group_id, d.w0, d.w1, d.w2, d.w3, d.w4 "
0088         "FROM fe_weight_per_group_dat d "
0089         "WHERE wei_conf_id = :wei_conf_id order by d.group_id ");
0090     m_readStmt->setInt(1, iconfID);
0091     ResultSet* rset = m_readStmt->executeQuery();
0092 
0093     std::pair<EcalLogicID, FEConfigWeightGroupDat> p;
0094     FEConfigWeightGroupDat dat;
0095     int ig = -1;
0096     while (rset->next()) {
0097       ig++;                              // we create a dummy logic_id
0098       p.first = EcalLogicID("Group_id",  // name
0099                             ig);         // logic_id
0100 
0101       dat.setWeightGroupId(rset->getInt(1));
0102       dat.setWeight0(rset->getFloat(2));
0103       dat.setWeight1(rset->getFloat(3));
0104       dat.setWeight2(rset->getFloat(4));
0105       dat.setWeight3(rset->getFloat(5));
0106       dat.setWeight4(rset->getFloat(6));
0107 
0108       p.second = dat;
0109       fillMap->insert(p);
0110     }
0111   } catch (SQLException& e) {
0112     throw(std::runtime_error("FEConfigWeightGroupDat::fetchData:  " + e.getMessage()));
0113   }
0114 }
0115 
0116 void FEConfigWeightGroupDat::writeArrayDB(const std::map<EcalLogicID, FEConfigWeightGroupDat>* data,
0117                                           FEConfigWeightInfo* iconf) noexcept(false) {
0118   this->checkConnection();
0119   this->checkPrepare();
0120 
0121   int iconfID = iconf->fetchID();
0122   if (!iconfID) {
0123     throw(std::runtime_error("FEConfigWeightGroupDat::writeArrayDB:  ICONF not in DB"));
0124   }
0125 
0126   int nrows = data->size();
0127   int* ids = new int[nrows];
0128   int* iconfid_vec = new int[nrows];
0129   int* xx = new int[nrows];
0130   float* yy = new float[nrows];
0131   float* zz = new float[nrows];
0132   float* rr = new float[nrows];
0133   float* ss = new float[nrows];
0134   float* tt = new float[nrows];
0135 
0136   ub2* ids_len = new ub2[nrows];
0137   ub2* iconf_len = new ub2[nrows];
0138   ub2* x_len = new ub2[nrows];
0139   ub2* y_len = new ub2[nrows];
0140   ub2* z_len = new ub2[nrows];
0141   ub2* r_len = new ub2[nrows];
0142   ub2* s_len = new ub2[nrows];
0143   ub2* t_len = new ub2[nrows];
0144 
0145   // const EcalLogicID* channel;
0146   const FEConfigWeightGroupDat* dataitem;
0147   int count = 0;
0148   typedef map<EcalLogicID, FEConfigWeightGroupDat>::const_iterator CI;
0149   for (CI p = data->begin(); p != data->end(); ++p) {
0150     // channel = &(p->first);
0151     //  int logicID = channel->getLogicID();
0152     //  if (!logicID) { throw(std::runtime_error("FEConfigWeightGroupDat::writeArrayDB:  Bad EcalLogicID")); }
0153     //  ids[count]=logicID;
0154     iconfid_vec[count] = iconfID;
0155 
0156     dataitem = &(p->second);
0157     // dataIface.writeDB( channel, dataitem, iconf);
0158     int x = dataitem->getWeightGroupId();
0159     float y = dataitem->getWeight0();
0160     float z = dataitem->getWeight1();
0161     float r = dataitem->getWeight2();
0162     float s = dataitem->getWeight3();
0163     float t = dataitem->getWeight4();
0164 
0165     xx[count] = x;
0166     yy[count] = y;
0167     zz[count] = z;
0168     rr[count] = r;
0169     ss[count] = s;
0170     tt[count] = t;
0171 
0172     //  ids_len[count]=sizeof(ids[count]);
0173     iconf_len[count] = sizeof(iconfid_vec[count]);
0174 
0175     x_len[count] = sizeof(xx[count]);
0176     y_len[count] = sizeof(yy[count]);
0177     z_len[count] = sizeof(zz[count]);
0178     r_len[count] = sizeof(rr[count]);
0179     s_len[count] = sizeof(ss[count]);
0180     t_len[count] = sizeof(tt[count]);
0181 
0182     count++;
0183   }
0184 
0185   try {
0186     m_writeStmt->setDataBuffer(1, (dvoid*)iconfid_vec, OCCIINT, sizeof(iconfid_vec[0]), iconf_len);
0187     m_writeStmt->setDataBuffer(2, (dvoid*)xx, OCCIINT, sizeof(xx[0]), x_len);
0188     m_writeStmt->setDataBuffer(3, (dvoid*)yy, OCCIFLOAT, sizeof(yy[0]), y_len);
0189     m_writeStmt->setDataBuffer(4, (dvoid*)zz, OCCIFLOAT, sizeof(zz[0]), z_len);
0190     m_writeStmt->setDataBuffer(5, (dvoid*)rr, OCCIFLOAT, sizeof(rr[0]), r_len);
0191     m_writeStmt->setDataBuffer(6, (dvoid*)ss, OCCIFLOAT, sizeof(ss[0]), s_len);
0192     m_writeStmt->setDataBuffer(7, (dvoid*)tt, OCCIFLOAT, sizeof(tt[0]), t_len);
0193 
0194     m_writeStmt->executeArrayUpdate(nrows);
0195 
0196     delete[] ids;
0197     delete[] iconfid_vec;
0198     delete[] xx;
0199     delete[] yy;
0200     delete[] zz;
0201     delete[] rr;
0202     delete[] ss;
0203     delete[] tt;
0204 
0205     delete[] ids_len;
0206     delete[] iconf_len;
0207     delete[] x_len;
0208     delete[] y_len;
0209     delete[] z_len;
0210     delete[] r_len;
0211     delete[] s_len;
0212     delete[] t_len;
0213 
0214   } catch (SQLException& e) {
0215     throw(std::runtime_error("FEConfigWeightGroupDat::writeArrayDB():  " + e.getMessage()));
0216   }
0217 }