00001 #include <stdexcept>
00002 #include <string>
00003 #include "OnlineDB/Oracle/interface/Oracle.h"
00004
00005 #include "OnlineDB/EcalCondDB/interface/MonLaserPulseDat.h"
00006
00007 using namespace std;
00008 using namespace oracle::occi;
00009
00010 MonLaserPulseDat::MonLaserPulseDat()
00011 {
00012 m_env = NULL;
00013 m_conn = NULL;
00014 m_writeStmt = NULL;
00015 m_readStmt = NULL;
00016
00017 m_pulseHeightMean = 0;
00018 m_pulseHeightRMS = 0;
00019 m_pulseWidthMean = 0;
00020 m_pulseWidthRMS = 0;
00021 }
00022
00023
00024
00025 MonLaserPulseDat::~MonLaserPulseDat()
00026 {
00027 }
00028
00029
00030
00031 void MonLaserPulseDat::prepareWrite()
00032 throw(std::runtime_error)
00033 {
00034 this->checkConnection();
00035
00036 try {
00037 m_writeStmt = m_conn->createStatement();
00038 m_writeStmt->setSQL("INSERT INTO mon_laser_pulse_dat (iov_id, logic_id, "
00039 "pulse_height_mean, pulse_height_rms, pulse_width_mean, pulse_width_rms) "
00040 "VALUES (:iov_id, :logic_id, "
00041 ":3, :4, :5, :6)");
00042 } catch (SQLException &e) {
00043 throw(std::runtime_error("MonLaserPulseDat::prepareWrite(): "+e.getMessage()));
00044 }
00045 }
00046
00047
00048
00049 void MonLaserPulseDat::writeDB(const EcalLogicID* ecid, const MonLaserPulseDat* item, MonRunIOV* iov)
00050 throw(std::runtime_error)
00051 {
00052 this->checkConnection();
00053 this->checkPrepare();
00054
00055 int iovID = iov->fetchID();
00056 if (!iovID) { throw(std::runtime_error("MonLaserPulseDat::writeDB: IOV not in DB")); }
00057
00058 int logicID = ecid->getLogicID();
00059 if (!logicID) { throw(std::runtime_error("MonLaserPulseDat::writeDB: Bad EcalLogicID")); }
00060
00061 try {
00062 m_writeStmt->setInt(1, iovID);
00063 m_writeStmt->setInt(2, logicID);
00064
00065 m_writeStmt->setFloat(3, item->getPulseHeightMean() );
00066 m_writeStmt->setFloat(4, item->getPulseHeightRMS() );
00067 m_writeStmt->setFloat(5, item->getPulseWidthMean() );
00068 m_writeStmt->setFloat(6, item->getPulseWidthRMS() );
00069
00070 m_writeStmt->executeUpdate();
00071 } catch (SQLException &e) {
00072 throw(std::runtime_error("MonLaserPulseDat::writeDB(): "+e.getMessage()));
00073 }
00074 }
00075
00076
00077
00078 void MonLaserPulseDat::fetchData(std::map< EcalLogicID, MonLaserPulseDat >* fillMap, MonRunIOV* iov)
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
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.pulse_height_mean, d.pulse_height_rms, d.pulse_width_mean, d.pulse_width_rms "
00095 "FROM channelview cv JOIN mon_laser_pulse_dat d "
00096 "ON cv.logic_id = d.logic_id AND cv.name = cv.maps_to "
00097 "WHERE d.iov_id = :iov_id");
00098 m_readStmt->setInt(1, iovID);
00099 ResultSet* rset = m_readStmt->executeQuery();
00100
00101 std::pair< EcalLogicID, MonLaserPulseDat > p;
00102 MonLaserPulseDat dat;
00103 while(rset->next()) {
00104 p.first = EcalLogicID( rset->getString(1),
00105 rset->getInt(2),
00106 rset->getInt(3),
00107 rset->getInt(4),
00108 rset->getInt(5),
00109 rset->getString(6));
00110
00111 dat.setPulseHeightMean( rset->getFloat(7) );
00112 dat.setPulseHeightRMS( rset->getFloat(8) );
00113 dat.setPulseWidthMean( rset->getFloat(9) );
00114 dat.setPulseWidthRMS( rset->getFloat(10) );
00115
00116 p.second = dat;
00117 fillMap->insert(p);
00118 }
00119 } catch (SQLException &e) {
00120 throw(std::runtime_error("MonLaserPulseDat::fetchData(): "+e.getMessage()));
00121 }
00122 }
00123
00124 void MonLaserPulseDat::writeArrayDB(const std::map< EcalLogicID, MonLaserPulseDat >* 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("MonLaserPulseDat::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 float* ww= new float[nrows];
00141
00142
00143 ub2* ids_len= new ub2[nrows];
00144 ub2* iov_len= new ub2[nrows];
00145 ub2* x_len= new ub2[nrows];
00146 ub2* y_len= new ub2[nrows];
00147 ub2* z_len= new ub2[nrows];
00148 ub2* w_len= new ub2[nrows];
00149
00150
00151 const EcalLogicID* channel;
00152 const MonLaserPulseDat* dataitem;
00153 int count=0;
00154 typedef map< EcalLogicID, MonLaserPulseDat >::const_iterator CI;
00155 for (CI p = data->begin(); p != data->end(); ++p) {
00156 channel = &(p->first);
00157 int logicID = channel->getLogicID();
00158 if (!logicID) { throw(std::runtime_error("MonLaserPulseDat::writeArrayDB: Bad EcalLogicID")); }
00159 ids[count]=logicID;
00160 iovid_vec[count]=iovID;
00161
00162 dataitem = &(p->second);
00163
00164 float x=dataitem->getPulseHeightMean();
00165 float y=dataitem->getPulseHeightRMS();
00166 float z=dataitem->getPulseWidthMean();
00167 float w=dataitem->getPulseWidthRMS();
00168
00169
00170 xx[count]=x;
00171 yy[count]=y;
00172 zz[count]=z;
00173 ww[count]=w;
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 w_len[count]=sizeof(ww[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*)ww, OCCIFLOAT , sizeof(ww[0]), w_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 [] ww;
00204
00205 delete [] ids_len;
00206 delete [] iov_len;
00207 delete [] x_len;
00208 delete [] y_len;
00209 delete [] z_len;
00210 delete [] w_len;
00211
00212
00213
00214 } catch (SQLException &e) {
00215 throw(std::runtime_error("MonLaserPulseDat::writeArrayDB(): "+e.getMessage()));
00216 }
00217 }