Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include <stdexcept>
0002 #include <string>
0003 #include "OnlineDB/Oracle/interface/Oracle.h"
0004 
0005 #include "OnlineDB/EcalCondDB/interface/ODWeightsDat.h"
0006 
0007 using namespace std;
0008 using namespace oracle::occi;
0009 
0010 ODWeightsDat::ODWeightsDat() {
0011   m_env = nullptr;
0012   m_conn = nullptr;
0013   m_writeStmt = nullptr;
0014   m_readStmt = nullptr;
0015 
0016   m_sm = 0;
0017   m_fed = 0;
0018   m_tt = 0;
0019   m_xt = 0;
0020 }
0021 
0022 ODWeightsDat::~ODWeightsDat() {}
0023 
0024 void ODWeightsDat::clear() {
0025   m_sm = 0;
0026   m_fed = 0;
0027   m_tt = 0;
0028   m_xt = 0;
0029   m_wei0 = 0;
0030   m_wei1 = 0;
0031   m_wei2 = 0;
0032   m_wei3 = 0;
0033   m_wei4 = 0;
0034   m_wei5 = 0;
0035 }
0036 
0037 void ODWeightsDat::prepareWrite() noexcept(false) {
0038   this->checkConnection();
0039 
0040   try {
0041     m_writeStmt = m_conn->createStatement();
0042     m_writeStmt->setSQL("INSERT INTO " + getTable() +
0043                         " (rec_id, sm_id, fed_id, tt_id, cry_id, wei0, wei1, wei2, wei3, wei4, wei5 ) "
0044                         "VALUES (:1, :2, :3, :4, :5, :6, :7, :8 , :9, :10, :11 )");
0045   } catch (SQLException& e) {
0046     throw(std::runtime_error("ODWeightsDat::prepareWrite():  " + e.getMessage()));
0047   }
0048 }
0049 
0050 void ODWeightsDat::writeDB(const ODWeightsDat* item, ODFEWeightsInfo* iov) noexcept(false) {
0051   this->checkConnection();
0052 
0053   try {
0054     m_writeStmt->setInt(1, item->getId());
0055     m_writeStmt->setInt(2, item->getSMId());
0056     m_writeStmt->setInt(3, item->getFedId());
0057     m_writeStmt->setInt(4, item->getTTId());
0058     m_writeStmt->setInt(5, item->getCrystalId());
0059 
0060     m_writeStmt->setFloat(6, item->getWeight0());
0061     m_writeStmt->setFloat(7, item->getWeight1());
0062     m_writeStmt->setFloat(8, item->getWeight2());
0063     m_writeStmt->setFloat(9, item->getWeight3());
0064     m_writeStmt->setFloat(10, item->getWeight4());
0065     m_writeStmt->setFloat(11, item->getWeight5());
0066 
0067     m_writeStmt->executeUpdate();
0068   } catch (SQLException& e) {
0069     throw(std::runtime_error("ODWeightsDat::writeDB():  " + e.getMessage()));
0070   }
0071 }
0072 
0073 void ODWeightsDat::fetchData(std::vector<ODWeightsDat>* p, ODFEWeightsInfo* iov) noexcept(false) {
0074   this->checkConnection();
0075 
0076   iov->setConnection(m_env, m_conn);
0077   int iovID = iov->fetchID();
0078   if (!iovID) {
0079     std::cout << "ID not in the DB" << endl;
0080     return;
0081   }
0082 
0083   try {
0084     m_readStmt->setSQL("SELECT * FROM " + getTable() + " WHERE rec_id = :rec_id order by sm_id, fed_id, tt_id, cry_id");
0085     m_readStmt->setInt(1, iovID);
0086     ResultSet* rset = m_readStmt->executeQuery();
0087 
0088     //    std::vector< ODWeightsDat > p;
0089     ODWeightsDat dat;
0090     while (rset->next()) {
0091       // dat.setId( rset->getInt(1) );
0092       dat.setSMId(rset->getInt(2));
0093       dat.setFedId(rset->getInt(3));
0094       dat.setTTId(rset->getInt(4));
0095       dat.setCrystalId(rset->getInt(5));
0096       /*
0097       dat.setWeight0( rset->getFloat(6) );
0098       dat.setWeight1( rset->getFloat(7) );
0099       dat.setWeight2( rset->getFloat(8) );
0100       dat.setWeight3( rset->getFloat(9) );
0101       dat.setWeight4( rset->getFloat(10) );
0102       dat.setWeight5( rset->getFloat(11) );
0103       */
0104       p->push_back(dat);
0105       for (int iwei = 0; iwei < 6; iwei++) {
0106         dat.setWeight(iwei, rset->getFloat(6 + iwei));
0107       }
0108       std::vector<float> dccwei(m_wei, m_wei + 6);
0109       dccw.push_back(dccwei);  // vector vector
0110     }
0111 
0112   } catch (SQLException& e) {
0113     throw(std::runtime_error("ODWeightsDat::fetchData():  " + e.getMessage()));
0114   }
0115 }
0116 
0117 void ODWeightsDat::fetchData(ODWeightsDat* p) noexcept(false) {
0118   this->checkConnection();
0119 
0120   try {
0121     m_readStmt->setSQL("SELECT * FROM " + getTable() + " WHERE rec_id = :1 order by sm_id, fed_id, tt_id, cry_id");
0122     m_readStmt->setInt(1, p->getId());
0123     ResultSet* rset = m_readStmt->executeQuery();
0124 
0125     int row = 0;
0126     while (rset->next()) {
0127       row++;
0128       p->setSMId(rset->getInt(2));
0129       p->setFedId(rset->getInt(3));
0130       p->setTTId(rset->getInt(4));
0131       p->setCrystalId(rset->getInt(5));
0132       for (int iwei = 0; iwei < 6; iwei++) {
0133         p->setWeight(iwei, rset->getFloat(6 + iwei));
0134       }
0135       std::vector<float> dccwei(m_wei, m_wei + 6);
0136       dccw.push_back(dccwei);  // vector vector
0137     }                          // loop on all rows in the table
0138     std::cout << " table " << getTable() << " total nb of rows " << row << std::endl;
0139 
0140   } catch (SQLException& e) {
0141     throw(std::runtime_error("ODWeightsDat::fetchData():  " + e.getMessage()));
0142   }
0143 }
0144 
0145 //  ************************************************************************   //
0146 
0147 void ODWeightsDat::writeArrayDB(const std::vector<ODWeightsDat>& data, ODFEWeightsInfo* iov) noexcept(false) {
0148   this->checkConnection();
0149 
0150   int iovID = iov->fetchID();
0151   if (!iovID) {
0152     throw(std::runtime_error("ODDelays::writeArrayDB:  ODFEDelaysInfo not in DB"));
0153   }
0154 
0155   int nrows = data.size();
0156   int* ids = new int[nrows];
0157   int* xx = new int[nrows];
0158   int* yy = new int[nrows];
0159   int* zz = new int[nrows];
0160   int* st = new int[nrows];
0161   float* xx1 = new float[nrows];
0162   float* yy1 = new float[nrows];
0163   float* zz1 = new float[nrows];
0164   float* xx2 = new float[nrows];
0165   float* yy2 = new float[nrows];
0166   float* zz2 = new float[nrows];
0167 
0168   ub2* ids_len = new ub2[nrows];
0169   ub2* x_len = new ub2[nrows];
0170   ub2* y_len = new ub2[nrows];
0171   ub2* z_len = new ub2[nrows];
0172   ub2* st_len = new ub2[nrows];
0173   ub2* x1_len = new ub2[nrows];
0174   ub2* y1_len = new ub2[nrows];
0175   ub2* z1_len = new ub2[nrows];
0176   ub2* x2_len = new ub2[nrows];
0177   ub2* y2_len = new ub2[nrows];
0178   ub2* z2_len = new ub2[nrows];
0179 
0180   ODWeightsDat dataitem;
0181 
0182   int n_data = (int)data.size();
0183   for (int count = 0; count < n_data; count++) {
0184     dataitem = data[count];
0185     ids[count] = iovID;
0186     xx[count] = dataitem.getSMId();
0187     yy[count] = dataitem.getFedId();
0188     zz[count] = dataitem.getTTId();
0189     st[count] = dataitem.getCrystalId();
0190     xx1[count] = dataitem.getWeight0();
0191     yy1[count] = dataitem.getWeight1();
0192     zz1[count] = dataitem.getWeight2();
0193     xx2[count] = dataitem.getWeight3();
0194     yy2[count] = dataitem.getWeight4();
0195     zz2[count] = dataitem.getWeight5();
0196 
0197     ids_len[count] = sizeof(ids[count]);
0198     x_len[count] = sizeof(xx[count]);
0199     y_len[count] = sizeof(yy[count]);
0200     z_len[count] = sizeof(zz[count]);
0201     st_len[count] = sizeof(st[count]);
0202     x1_len[count] = sizeof(xx1[count]);
0203     y1_len[count] = sizeof(yy1[count]);
0204     z1_len[count] = sizeof(zz1[count]);
0205     x2_len[count] = sizeof(xx2[count]);
0206     y2_len[count] = sizeof(yy2[count]);
0207     z2_len[count] = sizeof(zz2[count]);
0208   }
0209 
0210   try {
0211     m_writeStmt->setDataBuffer(1, (dvoid*)ids, OCCIINT, sizeof(ids[0]), ids_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     m_writeStmt->setDataBuffer(5, (dvoid*)st, OCCIINT, sizeof(st[0]), st_len);
0216     m_writeStmt->setDataBuffer(6, (dvoid*)xx1, OCCIFLOAT, sizeof(xx1[0]), x1_len);
0217     m_writeStmt->setDataBuffer(7, (dvoid*)yy1, OCCIFLOAT, sizeof(yy1[0]), y1_len);
0218     m_writeStmt->setDataBuffer(8, (dvoid*)zz1, OCCIFLOAT, sizeof(zz1[0]), z1_len);
0219     m_writeStmt->setDataBuffer(9, (dvoid*)xx2, OCCIFLOAT, sizeof(xx2[0]), x2_len);
0220     m_writeStmt->setDataBuffer(10, (dvoid*)yy2, OCCIFLOAT, sizeof(yy2[0]), y2_len);
0221     m_writeStmt->setDataBuffer(11, (dvoid*)zz2, OCCIFLOAT, sizeof(zz2[0]), z2_len);
0222 
0223     m_writeStmt->executeArrayUpdate(nrows);
0224 
0225     delete[] ids;
0226     delete[] xx;
0227     delete[] yy;
0228     delete[] zz;
0229     delete[] st;
0230     delete[] xx1;
0231     delete[] yy1;
0232     delete[] zz1;
0233     delete[] xx2;
0234     delete[] yy2;
0235     delete[] zz2;
0236 
0237     delete[] ids_len;
0238     delete[] x_len;
0239     delete[] y_len;
0240     delete[] z_len;
0241     delete[] st_len;
0242     delete[] x1_len;
0243     delete[] y1_len;
0244     delete[] z1_len;
0245     delete[] x2_len;
0246     delete[] y2_len;
0247     delete[] z2_len;
0248 
0249   } catch (SQLException& e) {
0250     throw(std::runtime_error("ODWeightsDat::writeArrayDB():  " + e.getMessage()));
0251   }
0252 }