CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/OnlineDB/EcalCondDB/src/MonPedestalOffsetsDat.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/MonPedestalOffsetsDat.h"
00006 #include "OnlineDB/EcalCondDB/interface/MonRunTag.h"
00007 #include "OnlineDB/EcalCondDB/interface/MonRunIOV.h"
00008 
00009 using namespace std;
00010 using namespace oracle::occi;
00011 
00012 MonPedestalOffsetsDat::MonPedestalOffsetsDat()
00013 {
00014   m_env = NULL;
00015   m_conn = NULL;
00016   m_writeStmt = NULL;
00017   m_readStmt = NULL;
00018 
00019   m_dacG1 = 0;
00020   m_dacG6 = 0;
00021   m_dacG12 = 0;
00022   m_taskStatus = 0;
00023 }
00024 
00025 
00026 
00027 MonPedestalOffsetsDat::~MonPedestalOffsetsDat()
00028 {
00029 }
00030 
00031 
00032 
00033 void MonPedestalOffsetsDat::prepareWrite()
00034   throw(std::runtime_error)
00035 {
00036   this->checkConnection();
00037 
00038   try {
00039     m_writeStmt = m_conn->createStatement();
00040     m_writeStmt->setSQL("INSERT INTO mon_pedestal_offsets_dat (iov_id, logic_id, "
00041                         "dac_g1, dac_g6, dac_g12, task_status) "
00042                         "VALUES (:iov_id, :logic_id, "
00043                         ":dac_g1, :dac_g6, :dac_g12, :task_status)");
00044   } catch (SQLException &e) {
00045     throw(std::runtime_error("MonPedestalOffsetsDat::prepareWrite():  "+e.getMessage()));
00046   }
00047 }
00048 
00049 
00050 
00051 void MonPedestalOffsetsDat::writeDB(const EcalLogicID* ecid, const MonPedestalOffsetsDat* item, MonRunIOV* iov )
00052   throw(std::runtime_error)
00053 {
00054   this->checkConnection();
00055   this->checkPrepare();
00056 
00057   int iovID = iov->fetchID();
00058   if (!iovID) { throw(std::runtime_error("MonPedestalOffsetsDat::writeDB:  IOV not in DB")); }
00059 
00060   int logicID = ecid->getLogicID();
00061   if (!logicID) { throw(std::runtime_error("MonPedestalOffsetsDat::writeDB:  Bad EcalLogicID")); }
00062   
00063   try {
00064     m_writeStmt->setInt(1, iovID);
00065     m_writeStmt->setInt(2, logicID);
00066     m_writeStmt->setInt(3, item->getDACG1() );
00067     m_writeStmt->setInt(4, item->getDACG6() );
00068     m_writeStmt->setInt(5, item->getDACG12() );
00069     m_writeStmt->setInt(6, item->getTaskStatus() );
00070 
00071     m_writeStmt->executeUpdate();
00072   } catch (SQLException &e) {
00073     throw(std::runtime_error("MonPedestalOffsetsDat::writeDB():  "+e.getMessage()));
00074   }
00075 }
00076 
00077 
00078 void MonPedestalOffsetsDat::fetchData(std::map< EcalLogicID, MonPedestalOffsetsDat >* fillMap, MonRunIOV* iov,  std::string mappa )
00079   throw(std::runtime_error)
00080 {
00081   this->checkConnection();
00082   fillMap->clear();
00083 
00084   iov->setConnection(m_env, m_conn);
00085   int iovID = iov->fetchID();
00086   if (!iovID) { 
00087     //  throw(std::runtime_error("MonPedestalOffsetsDat::writeDB:  IOV not in DB")); 
00088     return;
00089   }
00090 
00091   try {
00092 
00093     m_readStmt->setSQL("SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
00094                  "d.dac_g1, d.dac_g6, d.dac_g12, d.task_status "
00095                  "FROM channelview cv JOIN mon_pedestal_offsets_dat d "
00096                  "ON cv.logic_id = d.logic_id AND cv.name = " + mappa +
00097                  " WHERE d.iov_id = :iov_id");
00098     m_readStmt->setInt(1, iovID);
00099     ResultSet* rset = m_readStmt->executeQuery();
00100     
00101     std::pair< EcalLogicID, MonPedestalOffsetsDat > p;
00102     MonPedestalOffsetsDat dat;
00103     while(rset->next()) {
00104       p.first = EcalLogicID( rset->getString(1),     // name
00105                              rset->getInt(2),        // logic_id
00106                              rset->getInt(3),        // id1
00107                              rset->getInt(4),        // id2
00108                              rset->getInt(5),        // id3
00109                              rset->getString(6));    // maps_to
00110 
00111       dat.setDACG1( rset->getInt(7) );
00112       dat.setDACG6( rset->getInt(8) );
00113       dat.setDACG12( rset->getInt(9) );
00114       dat.setTaskStatus( rset->getInt(10) );
00115 
00116       p.second = dat;
00117       fillMap->insert(p);
00118     }
00119   } catch (SQLException &e) {
00120     throw(std::runtime_error("MonPedestalOffsetsDat::fetchData():  "+e.getMessage()));
00121   }
00122 }
00123 
00124 void MonPedestalOffsetsDat::writeArrayDB(const std::map< EcalLogicID, MonPedestalOffsetsDat >* data, MonRunIOV* iov)
00125   throw(std::runtime_error)
00126 {
00127   this->checkConnection();
00128   this->checkPrepare();
00129 
00130   int iovID = iov->fetchID();
00131   if (!iovID) { throw(std::runtime_error("MonPedestalOffsetsDat::writeArrayDB:  IOV not in DB")); }
00132 
00133 
00134   int nrows=data->size(); 
00135   int* ids= new int[nrows];
00136   int* iovid_vec= new int[nrows];
00137   float* xx= new float[nrows];
00138   float* yy= new float[nrows];
00139   float* zz= new float[nrows];
00140   int* st= new int[nrows];
00141 
00142   ub2* ids_len= new ub2[nrows];
00143   ub2* iov_len= new ub2[nrows];
00144   ub2* x_len= new ub2[nrows];
00145   ub2* y_len= new ub2[nrows];
00146   ub2* z_len= new ub2[nrows];
00147   ub2* st_len= new ub2[nrows];
00148 
00149   const EcalLogicID* channel;
00150   const MonPedestalOffsetsDat* dataitem;
00151   int count=0;
00152   typedef map< EcalLogicID, MonPedestalOffsetsDat >::const_iterator CI;
00153   for (CI p = data->begin(); p != data->end(); ++p) {
00154         channel = &(p->first);
00155         int logicID = channel->getLogicID();
00156         if (!logicID) { throw(std::runtime_error("MonPedestalOffsetsDat::writeArrayDB:  Bad EcalLogicID")); }
00157         ids[count]=logicID;
00158         iovid_vec[count]=iovID;
00159 
00160         dataitem = &(p->second);
00161         // dataIface.writeDB( channel, dataitem, iov);
00162         float x=dataitem->getDACG1();
00163         float y=dataitem->getDACG6();
00164         float z=dataitem->getDACG12();
00165         int statu=dataitem->getTaskStatus();
00166 
00167 
00168 
00169         xx[count]=x;
00170         yy[count]=y;
00171         zz[count]=z;
00172         st[count]=statu;
00173 
00174 
00175         ids_len[count]=sizeof(ids[count]);
00176         iov_len[count]=sizeof(iovid_vec[count]);
00177         
00178         x_len[count]=sizeof(xx[count]);
00179         y_len[count]=sizeof(yy[count]);
00180         z_len[count]=sizeof(zz[count]);
00181         st_len[count]=sizeof(st[count]);
00182 
00183         count++;
00184      }
00185 
00186 
00187   try {
00188     m_writeStmt->setDataBuffer(1, (dvoid*)iovid_vec, OCCIINT, sizeof(iovid_vec[0]),iov_len);
00189     m_writeStmt->setDataBuffer(2, (dvoid*)ids, OCCIINT, sizeof(ids[0]), ids_len );
00190     m_writeStmt->setDataBuffer(3, (dvoid*)xx, OCCIFLOAT , sizeof(xx[0]), x_len );
00191     m_writeStmt->setDataBuffer(4, (dvoid*)yy, OCCIFLOAT , sizeof(yy[0]), y_len );
00192     m_writeStmt->setDataBuffer(5, (dvoid*)zz, OCCIFLOAT , sizeof(zz[0]), z_len );
00193     m_writeStmt->setDataBuffer(6, (dvoid*)st, OCCIINT , sizeof(st[0]), st_len );
00194    
00195 
00196     m_writeStmt->executeArrayUpdate(nrows);
00197 
00198     delete [] ids;
00199     delete [] iovid_vec;
00200     delete [] xx;
00201     delete [] yy;
00202     delete [] zz;
00203     delete [] st;
00204 
00205     delete [] ids_len;
00206     delete [] iov_len;
00207     delete [] x_len;
00208     delete [] y_len;
00209     delete [] z_len;
00210     delete [] st_len;
00211 
00212 
00213   } catch (SQLException &e) {
00214     throw(std::runtime_error("MonPedestalOffsetsDat::writeArrayDB():  "+e.getMessage()));
00215   }
00216 }