00001 #include <stdexcept>
00002 #include <string>
00003 #include "OnlineDB/Oracle/interface/Oracle.h"
00004
00005 #include "OnlineDB/EcalCondDB/interface/LMFLaserConfigDat.h"
00006 #include "OnlineDB/EcalCondDB/interface/RunTag.h"
00007 #include "OnlineDB/EcalCondDB/interface/RunIOV.h"
00008 #include "OnlineDB/EcalCondDB/interface/IDBObject.h"
00009
00010 using namespace std;
00011 using namespace oracle::occi;
00012
00013 LMFLaserConfigDat::LMFLaserConfigDat()
00014 {
00015 m_env = NULL;
00016 m_conn = NULL;
00017 m_writeStmt = NULL;
00018 m_readStmt = NULL;
00019
00020
00021 m_wl=0;
00022 m_vfe_gain=0;
00023 m_pn_gain=0;
00024 m_power=0;
00025 m_attenuator=0;
00026 m_current=0;
00027 m_delay1=0;
00028 m_delay2=0;
00029 }
00030
00031
00032
00033 LMFLaserConfigDat::~LMFLaserConfigDat()
00034 {
00035 }
00036
00037
00038
00039 void LMFLaserConfigDat::prepareWrite()
00040 throw(runtime_error)
00041 {
00042 this->checkConnection();
00043
00044 try {
00045 m_writeStmt = m_conn->createStatement();
00046 m_writeStmt->setSQL("INSERT INTO lmf_laser_config_dat (lmf_iov_id, logic_id, "
00047 "wavelength, vfe_gain, pn_gain, lsr_power, lsr_attenuator, lsr_current, lsr_delay_1, lsr_delay_2) "
00048 "VALUES (:1, :2, "
00049 ":3, :4, :5, :6, :7, :8, :9, :10 )");
00050 } catch (SQLException &e) {
00051 throw(runtime_error("LMFLaserConfigDat::prepareWrite(): "+e.getMessage()));
00052 }
00053 }
00054
00055
00056
00057 void LMFLaserConfigDat::writeDB(const EcalLogicID* ecid, const LMFLaserConfigDat* item, LMFRunIOV* iov)
00058 throw(runtime_error)
00059 {
00060 this->checkConnection();
00061 this->checkPrepare();
00062
00063 int iovID = iov->fetchID();
00064 if (!iovID) { throw(runtime_error("LMFLaserConfigDat::writeDB: IOV not in DB")); }
00065
00066 int logicID = ecid->getLogicID();
00067 if (!logicID) { throw(runtime_error("LMFLaserConfigDat::writeDB: Bad EcalLogicID")); }
00068
00069 try {
00070 m_writeStmt->setInt(1, iovID);
00071 m_writeStmt->setInt(2, logicID);
00072
00073 m_writeStmt->setInt(3, item->getWavelength() );
00074 m_writeStmt->setInt(4, item->getVFEGain() );
00075 m_writeStmt->setInt(5, item->getPNGain() );
00076 m_writeStmt->setFloat(6, item->getPower() );
00077 m_writeStmt->setFloat(7, item->getAttenuator() );
00078 m_writeStmt->setFloat(8, item->getCurrent() );
00079 m_writeStmt->setFloat(9, item->getDelay1() );
00080 m_writeStmt->setFloat(10, item->getDelay2() );
00081
00082 m_writeStmt->executeUpdate();
00083 } catch (SQLException &e) {
00084 throw(runtime_error("LMFLaserConfigDat::writeDB(): "+e.getMessage()));
00085 }
00086 }
00087
00088
00089 void LMFLaserConfigDat::writeArrayDB(const std::map< EcalLogicID, LMFLaserConfigDat >* data, LMFRunIOV* iov)
00090 throw(runtime_error)
00091 {
00092 this->checkConnection();
00093 this->checkPrepare();
00094
00095 int iovID = iov->fetchID();
00096 if (!iovID) { throw(runtime_error("LMFLaserConfigDat::writeArrayDB: IOV not in DB")); }
00097
00098
00099 int nrows=data->size();
00100 int* ids= new int[nrows];
00101 int* iovid_vec= new int[nrows];
00102
00103 int* xx= new int[nrows];
00104 int* yy= new int[nrows];
00105 int* zz= new int[nrows];
00106 float* wwa= new float[nrows];
00107 float* uua= new float[nrows];
00108 float* tta= new float[nrows];
00109 float* wwb= new float[nrows];
00110 float* uub= new float[nrows];
00111
00112 ub2* ids_len= new ub2[nrows];
00113 ub2* iov_len= new ub2[nrows];
00114 ub2* x_len= new ub2[nrows];
00115 ub2* y_len= new ub2[nrows];
00116 ub2* z_len= new ub2[nrows];
00117 ub2* wa_len= new ub2[nrows];
00118 ub2* ua_len= new ub2[nrows];
00119 ub2* ta_len= new ub2[nrows];
00120 ub2* wb_len= new ub2[nrows];
00121 ub2* ub_len= new ub2[nrows];
00122
00123 const EcalLogicID* channel;
00124 const LMFLaserConfigDat* dataitem;
00125 int count=0;
00126 typedef map< EcalLogicID, LMFLaserConfigDat >::const_iterator CI;
00127 for (CI p = data->begin(); p != data->end(); ++p) {
00128 channel = &(p->first);
00129 int logicID = channel->getLogicID();
00130 if (!logicID) { throw(runtime_error("LMFLaserConfigDat::writeArrayDB: Bad EcalLogicID")); }
00131 ids[count]=logicID;
00132 iovid_vec[count]=iovID;
00133
00134 dataitem = &(p->second);
00135
00136
00137 int x=dataitem->getWavelength();
00138 int y=dataitem->getVFEGain();
00139 int z=dataitem->getPNGain();
00140 float wa=dataitem->getPower();
00141 float ua=dataitem->getAttenuator();
00142 float ta=dataitem->getCurrent();
00143 float wb=dataitem->getDelay1();
00144 float ub=dataitem->getDelay2();
00145
00146
00147 xx[count]=x;
00148 yy[count]=y;
00149 zz[count]=z;
00150 wwa[count]=wa;
00151 uua[count]=ua;
00152 tta[count]=ta;
00153 wwb[count]=wb;
00154 uub[count]=ub;
00155
00156 ids_len[count]=sizeof(ids[count]);
00157 iov_len[count]=sizeof(iovid_vec[count]);
00158
00159 x_len[count]=sizeof(xx[count]);
00160 y_len[count]=sizeof(yy[count]);
00161 z_len[count]=sizeof(zz[count]);
00162 wa_len[count]=sizeof(wwa[count]);
00163 ua_len[count]=sizeof(uua[count]);
00164 ta_len[count]=sizeof(tta[count]);
00165 wb_len[count]=sizeof(wwb[count]);
00166 ub_len[count]=sizeof(uub[count]);
00167
00168 count++;
00169 }
00170
00171
00172 try {
00173 m_writeStmt->setDataBuffer(1, (dvoid*)iovid_vec, OCCIINT, sizeof(iovid_vec[0]),iov_len);
00174 m_writeStmt->setDataBuffer(2, (dvoid*)ids, OCCIINT, sizeof(ids[0]), ids_len );
00175 m_writeStmt->setDataBuffer(3, (dvoid*)xx, OCCIINT , sizeof(xx[0]), x_len );
00176 m_writeStmt->setDataBuffer(4, (dvoid*)yy, OCCIINT , sizeof(yy[0]), y_len );
00177 m_writeStmt->setDataBuffer(5, (dvoid*)zz, OCCIINT , sizeof(zz[0]), z_len );
00178 m_writeStmt->setDataBuffer(6, (dvoid*)wwa, OCCIFLOAT , sizeof(wwa[0]), wa_len );
00179 m_writeStmt->setDataBuffer(7, (dvoid*)uua, OCCIFLOAT , sizeof(uua[0]), ua_len );
00180 m_writeStmt->setDataBuffer(8, (dvoid*)tta, OCCIFLOAT , sizeof(tta[0]), ta_len );
00181 m_writeStmt->setDataBuffer(9, (dvoid*)wwb, OCCIFLOAT , sizeof(wwb[0]), wb_len );
00182 m_writeStmt->setDataBuffer(10, (dvoid*)uub, OCCIFLOAT , sizeof(uub[0]), ub_len );
00183
00184 m_writeStmt->executeArrayUpdate(nrows);
00185
00186 delete [] ids;
00187 delete [] iovid_vec;
00188
00189 delete [] xx;
00190 delete [] yy;
00191 delete [] zz;
00192
00193 delete [] wwa;
00194 delete [] uua;
00195 delete [] tta;
00196 delete [] wwb;
00197 delete [] uub;
00198
00199
00200 delete [] ids_len;
00201 delete [] iov_len;
00202
00203 delete [] x_len;
00204 delete [] y_len;
00205 delete [] z_len;
00206
00207 delete [] wa_len;
00208 delete [] ua_len;
00209 delete [] ta_len;
00210 delete [] wb_len;
00211 delete [] ub_len;
00212
00213 } catch (SQLException &e) {
00214 throw(runtime_error("LMFLaserConfigDat::writeArrayDB(): "+e.getMessage()));
00215 }
00216 }
00217
00218 void LMFLaserConfigDat::fetchData(std::map< EcalLogicID, LMFLaserConfigDat >* fillMap, LMFRunIOV* iov)
00219 throw(runtime_error)
00220 {
00221 this->checkConnection();
00222 fillMap->clear();
00223
00224 iov->setConnection(m_env, m_conn);
00225 int iovID = iov->fetchID();
00226 if (!iovID) {
00227
00228 return;
00229 }
00230
00231 try {
00232
00233 m_readStmt->setSQL("SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
00234 "d.wavelength, d.vfe_gain, d.pn_gain, d.lsr_power, d.lsr_attenuator, d.lsr_current, d.lsr_delay_1, d.lsr_delay_2 "
00235 "FROM channelview cv JOIN lmf_laser_config_dat d "
00236 "ON cv.logic_id = d.logic_id AND cv.name = cv.maps_to "
00237 "WHERE d.lmf_iov_id = :iov_id");
00238
00239 m_readStmt->setInt(1, iovID);
00240
00241 ResultSet* rset = m_readStmt->executeQuery();
00242
00243 std::pair< EcalLogicID, LMFLaserConfigDat > p;
00244 LMFLaserConfigDat dat;
00245 while(rset->next()) {
00246 p.first = EcalLogicID( rset->getString(1),
00247 rset->getInt(2),
00248 rset->getInt(3),
00249 rset->getInt(4),
00250 rset->getInt(5),
00251 rset->getString(6));
00252
00253
00254 dat.setWavelength( rset->getInt(7) );
00255 dat.setVFEGain( rset->getInt(7) );
00256 dat.setPNGain( rset->getInt(7) );
00257 dat.setPower( rset->getFloat(8) );
00258 dat.setAttenuator( rset->getFloat(8) );
00259 dat.setCurrent( rset->getFloat(8) );
00260 dat.setDelay1( rset->getFloat(8) );
00261 dat.setDelay2( rset->getFloat(8) );
00262
00263 p.second = dat;
00264 fillMap->insert(p);
00265 }
00266
00267 } catch (SQLException &e) {
00268 throw(runtime_error("LMFLaserConfigDat::fetchData(): "+e.getMessage()));
00269 }
00270 }