00001 #include <stdexcept>
00002 #include <string>
00003 #include "OnlineDB/Oracle/interface/Oracle.h"
00004
00005 #include "OnlineDB/EcalCondDB/interface/MonOccupancyDat.h"
00006
00007 using namespace std;
00008 using namespace oracle::occi;
00009
00010 MonOccupancyDat::MonOccupancyDat()
00011 {
00012 m_env = NULL;
00013 m_conn = NULL;
00014 m_writeStmt = NULL;
00015 m_readStmt = NULL;
00016
00017 m_eventsOverLowThreshold = 0;
00018 m_eventsOverHighThreshold = 0;
00019 m_avgEnergy = 0;
00020 }
00021
00022
00023
00024 MonOccupancyDat::~MonOccupancyDat()
00025 {
00026 }
00027
00028
00029
00030 void MonOccupancyDat::prepareWrite()
00031 throw(std::runtime_error)
00032 {
00033 this->checkConnection();
00034
00035 try {
00036 m_writeStmt = m_conn->createStatement();
00037 m_writeStmt->setSQL("INSERT INTO mon_occupancy_dat (iov_id, logic_id, "
00038 "events_over_low_threshold, events_over_high_threshold, avg_energy) "
00039 "VALUES (:iov_id, :logic_id, "
00040 ":3, :4, :5)");
00041 } catch (SQLException &e) {
00042 throw(std::runtime_error("MonOccupancyDat::prepareWrite(): "+e.getMessage()));
00043 }
00044 }
00045
00046
00047
00048 void MonOccupancyDat::writeDB(const EcalLogicID* ecid, const MonOccupancyDat* item, MonRunIOV* iov)
00049 throw(std::runtime_error)
00050 {
00051 this->checkConnection();
00052 this->checkPrepare();
00053
00054 int iovID = iov->fetchID();
00055 if (!iovID) { throw(std::runtime_error("MonOccupancyDat::writeDB: IOV not in DB")); }
00056
00057 int logicID = ecid->getLogicID();
00058 if (!logicID) { throw(std::runtime_error("MonOccupancyDat::writeDB: Bad EcalLogicID")); }
00059
00060 try {
00061 m_writeStmt->setInt(1, iovID);
00062 m_writeStmt->setInt(2, logicID);
00063
00064 m_writeStmt->setInt(3, item->getEventsOverLowThreshold() );
00065 m_writeStmt->setInt(4, item->getEventsOverHighThreshold() );
00066 m_writeStmt->setFloat(5, item->getAvgEnergy() );
00067
00068 m_writeStmt->executeUpdate();
00069 } catch (SQLException &e) {
00070 throw(std::runtime_error("MonOccupancyDat::writeDB(): "+e.getMessage()));
00071 }
00072 }
00073
00074
00075
00076 void MonOccupancyDat::fetchData(std::map< EcalLogicID, MonOccupancyDat >* fillMap, MonRunIOV* 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.events_over_low_threshold, d.events_over_high_threshold, d.avg_energy "
00093 "FROM channelview cv JOIN mon_occupancy_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, MonOccupancyDat > p;
00100 MonOccupancyDat 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.setEventsOverLowThreshold( rset->getInt(7) );
00110 dat.setEventsOverHighThreshold( rset->getInt(8) );
00111 dat.setAvgEnergy( rset->getFloat(9) );
00112
00113 p.second = dat;
00114 fillMap->insert(p);
00115 }
00116 } catch (SQLException &e) {
00117 throw(std::runtime_error("MonOccupancyDat::fetchData(): "+e.getMessage()));
00118 }
00119 }
00120
00121 void MonOccupancyDat::writeArrayDB(const std::map< EcalLogicID, MonOccupancyDat >* data, MonRunIOV* iov)
00122 throw(std::runtime_error)
00123 {
00124 this->checkConnection();
00125 this->checkPrepare();
00126
00127 int iovID = iov->fetchID();
00128 if (!iovID) { throw(std::runtime_error("MonOccupancyDat::writeArrayDB: IOV not in DB")); }
00129
00130
00131 int nrows=data->size();
00132 int* ids= new int[nrows];
00133 int* iovid_vec= new int[nrows];
00134 int* xx= new int[nrows];
00135 int* yy= new int[nrows];
00136 float* zz= new float[nrows];
00137
00138 ub2* ids_len= new ub2[nrows];
00139 ub2* iov_len= new ub2[nrows];
00140 ub2* x_len= new ub2[nrows];
00141 ub2* y_len= new ub2[nrows];
00142 ub2* z_len= new ub2[nrows];
00143
00144
00145 const EcalLogicID* channel;
00146 const MonOccupancyDat* dataitem;
00147 int count=0;
00148 typedef map< EcalLogicID, MonOccupancyDat >::const_iterator CI;
00149 for (CI p = data->begin(); p != data->end(); ++p) {
00150 channel = &(p->first);
00151 int logicID = channel->getLogicID();
00152 if (!logicID) { throw(std::runtime_error("MonOccupancyDat::writeArrayDB: Bad EcalLogicID")); }
00153 ids[count]=logicID;
00154 iovid_vec[count]=iovID;
00155
00156 dataitem = &(p->second);
00157
00158 int x=dataitem->getEventsOverLowThreshold();
00159 int y=dataitem->getEventsOverHighThreshold();
00160 float z=dataitem->getAvgEnergy();
00161
00162
00163
00164 xx[count]=x;
00165 yy[count]=y;
00166 zz[count]=z;
00167
00168 ids_len[count]=sizeof(ids[count]);
00169 iov_len[count]=sizeof(iovid_vec[count]);
00170
00171 x_len[count]=sizeof(xx[count]);
00172 y_len[count]=sizeof(yy[count]);
00173 z_len[count]=sizeof(zz[count]);
00174
00175 count++;
00176 }
00177
00178
00179 try {
00180 m_writeStmt->setDataBuffer(1, (dvoid*)iovid_vec, OCCIINT, sizeof(iovid_vec[0]),iov_len);
00181 m_writeStmt->setDataBuffer(2, (dvoid*)ids, OCCIINT, sizeof(ids[0]), ids_len );
00182 m_writeStmt->setDataBuffer(3, (dvoid*)xx, OCCIINT , sizeof(xx[0]), x_len );
00183 m_writeStmt->setDataBuffer(4, (dvoid*)yy, OCCIINT , sizeof(yy[0]), y_len );
00184 m_writeStmt->setDataBuffer(5, (dvoid*)zz, OCCIFLOAT , sizeof(zz[0]), z_len );
00185
00186 m_writeStmt->executeArrayUpdate(nrows);
00187
00188 delete [] ids;
00189 delete [] iovid_vec;
00190 delete [] xx;
00191 delete [] yy;
00192 delete [] zz;
00193
00194 delete [] ids_len;
00195 delete [] iov_len;
00196 delete [] x_len;
00197 delete [] y_len;
00198 delete [] z_len;
00199
00200
00201 } catch (SQLException &e) {
00202 throw(std::runtime_error("MonOccupancyDat::writeArrayDB(): "+e.getMessage()));
00203 }
00204 }