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