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