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