Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-10-08 23:09:51

0001 #include <stdexcept>
0002 #include <string>
0003 #include "OnlineDB/Oracle/interface/Oracle.h"
0004 
0005 #include "OnlineDB/EcalCondDB/interface/FEConfigLUTGroupDat.h"
0006 #include "OnlineDB/EcalCondDB/interface/FEConfigLUTInfo.h"
0007 
0008 using namespace std;
0009 using namespace oracle::occi;
0010 
0011 FEConfigLUTGroupDat::FEConfigLUTGroupDat() {
0012   m_env = nullptr;
0013   m_conn = nullptr;
0014   m_writeStmt = nullptr;
0015   m_readStmt = nullptr;
0016 
0017   m_group_id = 0;
0018   for (int i = 0; i < 1024; i++) {
0019     m_lut[i] = 0;
0020   }
0021 }
0022 
0023 FEConfigLUTGroupDat::~FEConfigLUTGroupDat() {}
0024 
0025 void FEConfigLUTGroupDat::prepareWrite() noexcept(false) {
0026   this->checkConnection();
0027 
0028   try {
0029     m_writeStmt = m_conn->createStatement();
0030     m_writeStmt->setSQL(
0031         "INSERT INTO fe_lut_per_group_dat (lut_conf_id, group_id, "
0032         " lut_id, lut_value ) "
0033         "VALUES (:lut_conf_id, :group_id, "
0034         ":lut_id, :lut_value )");
0035   } catch (SQLException& e) {
0036     throw(std::runtime_error(std::string("FEConfigLUTGroupDat::prepareWrite():  ") + e.getMessage()));
0037   }
0038 }
0039 
0040 void FEConfigLUTGroupDat::writeDB(const EcalLogicID* ecid,
0041                                   const FEConfigLUTGroupDat* item,
0042                                   FEConfigLUTInfo* iconf) noexcept(false) {
0043   this->checkConnection();
0044   this->checkPrepare();
0045 
0046   int iconfID = iconf->fetchID();
0047 
0048   cout << "iconf=" << iconfID << endl;
0049 
0050   if (!iconfID) {
0051     throw(std::runtime_error("FEConfigLUTGroupDat::writeArrayDB:  ICONF not in DB"));
0052   }
0053 
0054   int nrows = 1024;
0055   int* iconfid_vec = new int[nrows];
0056   int* xx = new int[nrows];
0057   int* yy = new int[nrows];
0058   int* zz = new int[nrows];
0059 
0060   ub2* iconf_len = new ub2[nrows];
0061   ub2* x_len = new ub2[nrows];
0062   ub2* y_len = new ub2[nrows];
0063   ub2* z_len = new ub2[nrows];
0064 
0065   for (int count = 0; count < nrows; count++) {
0066     iconfid_vec[count] = iconfID;
0067     int x = item->getLUTGroupId();
0068     int y = count;
0069     int z = m_lut[count];
0070 
0071     xx[count] = x;
0072     yy[count] = y;
0073     zz[count] = z;
0074 
0075     iconf_len[count] = sizeof(iconfid_vec[count]);
0076 
0077     x_len[count] = sizeof(xx[count]);
0078     y_len[count] = sizeof(yy[count]);
0079     z_len[count] = sizeof(zz[count]);
0080   }
0081 
0082   try {
0083     m_writeStmt->setDataBuffer(1, (dvoid*)iconfid_vec, OCCIINT, sizeof(iconfid_vec[0]), iconf_len);
0084     m_writeStmt->setDataBuffer(2, (dvoid*)xx, OCCIINT, sizeof(xx[0]), x_len);
0085     m_writeStmt->setDataBuffer(3, (dvoid*)yy, OCCIINT, sizeof(yy[0]), y_len);
0086     m_writeStmt->setDataBuffer(4, (dvoid*)zz, OCCIINT, sizeof(zz[0]), z_len);
0087 
0088     m_writeStmt->executeArrayUpdate(nrows);
0089 
0090     delete[] iconfid_vec;
0091     delete[] xx;
0092     delete[] yy;
0093     delete[] zz;
0094 
0095     delete[] iconf_len;
0096     delete[] x_len;
0097     delete[] y_len;
0098     delete[] z_len;
0099 
0100   } catch (SQLException& e) {
0101     throw(std::runtime_error(std::string("FEConfigLUTGroupDat::writeArrayDB():  ") + e.getMessage()));
0102   }
0103 }
0104 
0105 void FEConfigLUTGroupDat::fetchData(map<EcalLogicID, FEConfigLUTGroupDat>* fillMap,
0106                                     FEConfigLUTInfo* iconf) noexcept(false) {
0107   this->checkConnection();
0108   fillMap->clear();
0109 
0110   iconf->setConnection(m_env, m_conn);
0111   int iconfID = iconf->fetchID();
0112   if (!iconfID) {
0113     throw(std::runtime_error("FEConfigLUTGroupDat::fetchData:  ICONF not in DB"));
0114     return;
0115   }
0116 
0117   try {
0118     m_readStmt->setSQL(
0119         "SELECT d.group_id, d.lut_id, d.lut_value "
0120         "FROM fe_lut_per_group_dat d "
0121         "WHERE lut_conf_id = :lut_conf_id order by d.group_id, d.lut_id ");
0122     m_readStmt->setInt(1, iconfID);
0123     ResultSet* rset = m_readStmt->executeQuery();
0124 
0125     FEConfigLUTGroupDat dat;
0126     std::pair<EcalLogicID, FEConfigLUTGroupDat> p;
0127 
0128     int nrows = 1024;
0129 
0130     int ig = -1;
0131 
0132     while (rset->next()) {
0133       ig = rset->getInt(1);
0134       int il = rset->getInt(2);
0135       int ival = rset->getInt(3);
0136       if (il == 0) {
0137         p.first = EcalLogicID("Group_id", ig);  // a dummy logic_id
0138         dat = FEConfigLUTGroupDat();
0139         dat.setLUTGroupId(ig);
0140         dat.setLUTValue(il, ival);
0141       } else {
0142         dat.setLUTValue(il, ival);
0143       }
0144 
0145       if (il == (nrows - 1)) {
0146         p.second = dat;
0147         fillMap->insert(p);
0148       }
0149     }
0150   } catch (SQLException& e) {
0151     throw(std::runtime_error(std::string("FEConfigLUTGroupDat::fetchData:  ") + e.getMessage()));
0152   }
0153 }
0154 
0155 void FEConfigLUTGroupDat::writeArrayDB(const std::map<EcalLogicID, FEConfigLUTGroupDat>* data,
0156                                        FEConfigLUTInfo* iconf) noexcept(false) {
0157   this->checkConnection();
0158   this->checkPrepare();
0159 
0160   int iconfID = iconf->fetchID();
0161   if (!iconfID) {
0162     throw(std::runtime_error("FEConfigLUTGroupDat::writeArrayDB:  ICONF not in DB"));
0163   }
0164 
0165   int nrows = data->size() * 1024;
0166 
0167   int* iconfid_vec = new int[nrows];
0168   int* xx = new int[nrows];
0169   int* yy = new int[nrows];
0170   int* zz = new int[nrows];
0171 
0172   ub2* iconf_len = new ub2[nrows];
0173   ub2* x_len = new ub2[nrows];
0174   ub2* y_len = new ub2[nrows];
0175   ub2* z_len = new ub2[nrows];
0176 
0177   const FEConfigLUTGroupDat* dataitem;
0178   int count = 0;
0179   typedef map<EcalLogicID, FEConfigLUTGroupDat>::const_iterator CI;
0180   for (CI p = data->begin(); p != data->end(); ++p) {
0181     dataitem = &(p->second);
0182     int x = dataitem->getLUTGroupId();
0183 
0184     for (int i = 0; i < 1024; i++) {
0185       iconfid_vec[count] = iconfID;
0186       int y = i;
0187       int z = dataitem->getLUTValue(i);
0188 
0189       xx[count] = x;
0190       yy[count] = y;
0191       zz[count] = z;
0192 
0193       iconf_len[count] = sizeof(iconfid_vec[count]);
0194 
0195       x_len[count] = sizeof(xx[count]);
0196       y_len[count] = sizeof(yy[count]);
0197       z_len[count] = sizeof(zz[count]);
0198 
0199       count++;
0200     }
0201   }
0202 
0203   try {
0204     //    for (int i=0; i<nrows; i++){
0205 
0206     int i = 0;
0207     cout << "about to insert " << iconfid_vec[i] << " " << xx[i] << " " << yy[i] << " " << zz[i] << endl;
0208     i = nrows - 1;
0209     cout << "about to insert " << iconfid_vec[i] << " " << xx[i] << " " << yy[i] << " " << zz[i] << endl;
0210     // }
0211     m_writeStmt->setDataBuffer(1, (dvoid*)iconfid_vec, OCCIINT, sizeof(iconfid_vec[0]), iconf_len);
0212     m_writeStmt->setDataBuffer(2, (dvoid*)xx, OCCIINT, sizeof(xx[0]), x_len);
0213     m_writeStmt->setDataBuffer(3, (dvoid*)yy, OCCIINT, sizeof(yy[0]), y_len);
0214     m_writeStmt->setDataBuffer(4, (dvoid*)zz, OCCIINT, sizeof(zz[0]), z_len);
0215 
0216     m_writeStmt->executeArrayUpdate(nrows);
0217 
0218     delete[] iconfid_vec;
0219     delete[] xx;
0220     delete[] yy;
0221     delete[] zz;
0222 
0223     delete[] iconf_len;
0224     delete[] x_len;
0225     delete[] y_len;
0226     delete[] z_len;
0227 
0228   } catch (SQLException& e) {
0229     throw(std::runtime_error(std::string("FEConfigLUTGroupDat::writeArrayDB():  ") + e.getMessage()));
0230   }
0231 }