CMS 3D CMS Logo

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