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