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