CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/OnlineDB/EcalCondDB/src/ODWeightsDat.cc

Go to the documentation of this file.
00001 #include <stdexcept>
00002 #include <string>
00003 #include "OnlineDB/Oracle/interface/Oracle.h"
00004 
00005 #include "OnlineDB/EcalCondDB/interface/ODWeightsDat.h"
00006 
00007 using namespace std;
00008 using namespace oracle::occi;
00009 
00010 ODWeightsDat::ODWeightsDat()
00011 {
00012   m_env = NULL;
00013   m_conn = NULL;
00014   m_writeStmt = NULL;
00015   m_readStmt = NULL;
00016 
00017   m_sm = 0;
00018   m_fed = 0;
00019   m_tt = 0;
00020   m_xt = 0;
00021 
00022 }
00023 
00024 
00025 
00026 ODWeightsDat::~ODWeightsDat()
00027 {
00028 }
00029 
00030 void ODWeightsDat::clear() {
00031   m_sm = 0;
00032   m_fed = 0;
00033   m_tt = 0;
00034   m_xt = 0;
00035   m_wei0 = 0;
00036   m_wei1 = 0;
00037   m_wei2 = 0;
00038   m_wei3 = 0;
00039   m_wei4 = 0;
00040   m_wei5 = 0;
00041 }
00042 
00043 
00044 void ODWeightsDat::prepareWrite()
00045   throw(std::runtime_error)
00046 {
00047   this->checkConnection();
00048 
00049   try {
00050     m_writeStmt = m_conn->createStatement();
00051     m_writeStmt->setSQL("INSERT INTO "+getTable()+" (rec_id, sm_id, fed_id, tt_id, cry_id, wei0, wei1, wei2, wei3, wei4, wei5 ) "
00052                         "VALUES (:1, :2, :3, :4, :5, :6, :7, :8 , :9, :10, :11 )");
00053   } catch (SQLException &e) {
00054     throw(std::runtime_error("ODWeightsDat::prepareWrite():  "+e.getMessage()));
00055   }
00056 }
00057 
00058 
00059 
00060 void ODWeightsDat::writeDB(const ODWeightsDat* item, ODFEWeightsInfo* iov )
00061   throw(std::runtime_error)
00062 {
00063   this->checkConnection();
00064 
00065   try {
00066     m_writeStmt->setInt(1, item->getId());
00067     m_writeStmt->setInt(2, item->getSMId());
00068     m_writeStmt->setInt(3, item->getFedId() );
00069     m_writeStmt->setInt(4, item->getTTId() );
00070     m_writeStmt->setInt(5, item->getCrystalId() );
00071     
00072     m_writeStmt->setFloat(6, item->getWeight0() );
00073     m_writeStmt->setFloat(7, item->getWeight1() );
00074     m_writeStmt->setFloat(8, item->getWeight2() );
00075     m_writeStmt->setFloat(9, item->getWeight3() );
00076     m_writeStmt->setFloat(10, item->getWeight4() );
00077     m_writeStmt->setFloat(11, item->getWeight5() );
00078 
00079     m_writeStmt->executeUpdate();
00080   } catch (SQLException &e) {
00081     throw(std::runtime_error("ODWeightsDat::writeDB():  "+e.getMessage()));
00082   }
00083 }
00084 
00085 
00086 
00087 void ODWeightsDat::fetchData(std::vector< ODWeightsDat >* p, ODFEWeightsInfo* iov)
00088   throw(std::runtime_error)
00089 {
00090   this->checkConnection();
00091 
00092   iov->setConnection(m_env, m_conn);
00093   int iovID = iov->fetchID();
00094   if (!iovID) { 
00095     std::cout <<"ID not in the DB"<< endl; 
00096     return;
00097   }
00098 
00099   try {
00100     m_readStmt->setSQL("SELECT * FROM " + getTable() + " WHERE rec_id = :rec_id order by sm_id, fed_id, tt_id, cry_id");
00101     m_readStmt->setInt(1, iovID);
00102     ResultSet* rset = m_readStmt->executeQuery();
00103     
00104     //    std::vector< ODWeightsDat > p;
00105     ODWeightsDat dat;
00106     while(rset->next()) {
00107       // dat.setId( rset->getInt(1) );
00108       dat.setSMId( rset->getInt(2) );
00109       dat.setFedId( rset->getInt(3) );
00110       dat.setTTId( rset->getInt(4) );
00111       dat.setCrystalId( rset->getInt(5) );
00112       /*
00113       dat.setWeight0( rset->getFloat(6) );
00114       dat.setWeight1( rset->getFloat(7) );
00115       dat.setWeight2( rset->getFloat(8) );
00116       dat.setWeight3( rset->getFloat(9) );
00117       dat.setWeight4( rset->getFloat(10) );
00118       dat.setWeight5( rset->getFloat(11) );
00119       */
00120       p->push_back( dat);
00121       for(int iwei = 0; iwei < 6; iwei++) {
00122         dat.setWeight(iwei, rset->getFloat(6 + iwei) );
00123       }
00124       std::vector<float> dccwei( m_wei,  m_wei+6);
00125       dccw.push_back(dccwei);  // vector vector
00126 
00127     }
00128 
00129 
00130   } catch (SQLException &e) {
00131     throw(std::runtime_error("ODWeightsDat::fetchData():  "+e.getMessage()));
00132   }
00133 }
00134 
00135 void ODWeightsDat::fetchData(ODWeightsDat * p)
00136   throw(std::runtime_error)
00137 {
00138   this->checkConnection();
00139 
00140   try {
00141     m_readStmt->setSQL("SELECT * FROM " + getTable() + " WHERE rec_id = :1 order by sm_id, fed_id, tt_id, cry_id");
00142     m_readStmt->setInt(1, p->getId());
00143     ResultSet* rset = m_readStmt->executeQuery();
00144     
00145     int row = 0;
00146     while(rset->next()) {
00147       row++;
00148       p->setSMId(      rset->getInt(2) );
00149       p->setFedId(     rset->getInt(3) );
00150       p->setTTId(      rset->getInt(4) );
00151       p->setCrystalId( rset->getInt(5) );
00152       for(int iwei = 0; iwei < 6; iwei++) {
00153         p->setWeight(iwei, rset->getFloat(6 + iwei) );
00154       }
00155       std::vector<float> dccwei( m_wei,  m_wei+6);
00156       dccw.push_back(dccwei);  // vector vector
00157     }  // loop on all rows in the table
00158     std::cout << " table " << getTable() << " total nb of rows " << row << std::endl;
00159 
00160   } catch (SQLException &e) {
00161     throw(std::runtime_error("ODWeightsDat::fetchData():  "+e.getMessage()));
00162   }
00163 }
00164 
00165 //  ************************************************************************   // 
00166 
00167 void ODWeightsDat::writeArrayDB(const std::vector< ODWeightsDat > data, ODFEWeightsInfo* iov)
00168     throw(std::runtime_error)
00169 {
00170   this->checkConnection();
00171 
00172   int iovID = iov->fetchID();
00173   if (!iovID) { throw(std::runtime_error("ODDelays::writeArrayDB:  ODFEDelaysInfo not in DB")); }
00174 
00175 
00176   int nrows=data.size(); 
00177   int* ids= new int[nrows];
00178   int* xx= new int[nrows];
00179   int* yy= new int[nrows];
00180   int* zz= new int[nrows];
00181   int* st= new int[nrows];
00182   float* xx1= new float[nrows];
00183   float* yy1= new float[nrows];
00184   float* zz1= new float[nrows];
00185   float* xx2= new float[nrows];
00186   float* yy2= new float[nrows];
00187   float* zz2= new float[nrows];
00188 
00189 
00190   ub2* ids_len= new ub2[nrows];
00191   ub2* x_len= new ub2[nrows];
00192   ub2* y_len= new ub2[nrows];
00193   ub2* z_len= new ub2[nrows];
00194   ub2* st_len= new ub2[nrows];
00195   ub2* x1_len= new ub2[nrows];
00196   ub2* y1_len= new ub2[nrows];
00197   ub2* z1_len= new ub2[nrows];
00198   ub2* x2_len= new ub2[nrows];
00199   ub2* y2_len= new ub2[nrows];
00200   ub2* z2_len= new ub2[nrows];
00201 
00202   ODWeightsDat dataitem;
00203   
00204   int n_data= (int) data.size();
00205   for (int count = 0; count <n_data ; count++) {
00206     dataitem=data[count];
00207     ids[count]=iovID;
00208     xx[count]=dataitem.getSMId();
00209     yy[count]=dataitem.getFedId();
00210     zz[count]=dataitem.getTTId();
00211     st[count]=dataitem.getCrystalId();
00212     xx1[count]=dataitem.getWeight0();
00213     yy1[count]=dataitem.getWeight1();
00214     zz1[count]=dataitem.getWeight2();
00215     xx2[count]=dataitem.getWeight3();
00216     yy2[count]=dataitem.getWeight4();
00217     zz2[count]=dataitem.getWeight5();
00218 
00219 
00220         ids_len[count]=sizeof(ids[count]);
00221         x_len[count]=sizeof(xx[count]);
00222         y_len[count]=sizeof(yy[count]);
00223         z_len[count]=sizeof(zz[count]);
00224         st_len[count]=sizeof(st[count]);
00225         x1_len[count]=sizeof(xx1[count]);
00226         y1_len[count]=sizeof(yy1[count]);
00227         z1_len[count]=sizeof(zz1[count]);
00228         x2_len[count]=sizeof(xx2[count]);
00229         y2_len[count]=sizeof(yy2[count]);
00230         z2_len[count]=sizeof(zz2[count]);
00231 
00232      }
00233 
00234 
00235   try {
00236     m_writeStmt->setDataBuffer(1, (dvoid*)ids, OCCIINT, sizeof(ids[0]),ids_len);
00237     m_writeStmt->setDataBuffer(2, (dvoid*)xx, OCCIINT , sizeof(xx[0]), x_len );
00238     m_writeStmt->setDataBuffer(3, (dvoid*)yy, OCCIINT , sizeof(yy[0]), y_len );
00239     m_writeStmt->setDataBuffer(4, (dvoid*)zz, OCCIINT , sizeof(zz[0]), z_len );
00240     m_writeStmt->setDataBuffer(5, (dvoid*)st, OCCIINT , sizeof(st[0]), st_len );
00241     m_writeStmt->setDataBuffer(6, (dvoid*)xx1, OCCIFLOAT , sizeof(xx1[0]), x1_len );
00242     m_writeStmt->setDataBuffer(7, (dvoid*)yy1, OCCIFLOAT , sizeof(yy1[0]), y1_len );
00243     m_writeStmt->setDataBuffer(8, (dvoid*)zz1, OCCIFLOAT , sizeof(zz1[0]), z1_len );
00244     m_writeStmt->setDataBuffer(9, (dvoid*)xx2, OCCIFLOAT , sizeof(xx2[0]), x2_len );
00245     m_writeStmt->setDataBuffer(10, (dvoid*)yy2, OCCIFLOAT , sizeof(yy2[0]), y2_len );
00246     m_writeStmt->setDataBuffer(11, (dvoid*)zz2, OCCIFLOAT , sizeof(zz2[0]), z2_len );
00247    
00248 
00249     m_writeStmt->executeArrayUpdate(nrows);
00250 
00251     delete [] ids;
00252     delete [] xx;
00253     delete [] yy;
00254     delete [] zz;
00255     delete [] st;
00256     delete [] xx1;
00257     delete [] yy1;
00258     delete [] zz1;
00259     delete [] xx2;
00260     delete [] yy2;
00261     delete [] zz2;
00262 
00263     delete [] ids_len;
00264     delete [] x_len;
00265     delete [] y_len;
00266     delete [] z_len;
00267     delete [] st_len;
00268     delete [] x1_len;
00269     delete [] y1_len;
00270     delete [] z1_len;
00271     delete [] x2_len;
00272     delete [] y2_len;
00273     delete [] z2_len;
00274 
00275   } catch (SQLException &e) {
00276     throw(std::runtime_error("ODWeightsDat::writeArrayDB():  "+e.getMessage()));
00277   }
00278 }