Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:31:49

0001 #ifndef ITIMINGDAT_H
0002 #define ITIMINGDAT_H
0003 
0004 #include <map>
0005 #include <stdexcept>
0006 #include <string>
0007 
0008 #include "OnlineDB/EcalCondDB/interface/IDataItem.h"
0009 #include "OnlineDB/EcalCondDB/interface/MonRunTag.h"
0010 #include "OnlineDB/EcalCondDB/interface/MonRunIOV.h"
0011 #include "OnlineDB/EcalCondDB/interface/EcalLogicID.h"
0012 #include "OnlineDB/EcalCondDB/interface/RunTag.h"
0013 #include "OnlineDB/EcalCondDB/interface/RunIOV.h"
0014 
0015 #include "OnlineDB/Oracle/interface/Oracle.h"
0016 
0017 class ITimingDat : public IDataItem {
0018 public:
0019   typedef oracle::occi::SQLException SQLException;
0020   typedef oracle::occi::ResultSet ResultSet;
0021   friend class EcalCondDBInterface;
0022 
0023   ITimingDat() {
0024     m_env = nullptr;
0025     m_conn = nullptr;
0026     m_writeStmt = nullptr;
0027     m_readStmt = nullptr;
0028 
0029     m_timingMean = 0;
0030     m_timingRMS = 0;
0031     m_taskStatus = false;
0032   };
0033 
0034   ~ITimingDat() override{};
0035 
0036   // User data methods
0037   inline std::string getTable() override { return m_table_name; }
0038   inline void setTable(std::string x) { m_table_name = x; }
0039 
0040   inline void setTimingMean(float mean) { m_timingMean = mean; }
0041   inline float getTimingMean() const { return m_timingMean; }
0042 
0043   inline void setTimingRMS(float rms) { m_timingRMS = rms; }
0044   inline float getTimingRMS() const { return m_timingRMS; }
0045 
0046   inline void setTaskStatus(bool status) { m_taskStatus = status; }
0047   inline bool getTaskStatus() const { return m_taskStatus; }
0048 
0049 private:
0050   void prepareWrite() noexcept(false) override {
0051     this->checkConnection();
0052 
0053     try {
0054       m_writeStmt = m_conn->createStatement();
0055       m_writeStmt->setSQL("INSERT INTO " + getTable() +
0056                           " (iov_id, logic_id, "
0057                           "timing_mean, timing_rms , task_status ) "
0058                           "VALUES (:iov_id, :logic_id, "
0059                           ":timing_mean, :timing_rms, :task_status )");
0060     } catch (SQLException& e) {
0061       throw(std::runtime_error("ITimingDat::prepareWrite():  " + e.getMessage()));
0062     }
0063   }
0064 
0065   template <class DATT, class IOVT>
0066   void writeDB(const EcalLogicID* ecid, const DATT* item, IOVT* iov) noexcept(false) {
0067     this->checkConnection();
0068     this->checkPrepare();
0069 
0070     int iovID = iov->fetchID();
0071     if (!iovID) {
0072       throw(std::runtime_error("ITimingDat::writeDB:  IOV not in DB"));
0073     }
0074 
0075     int logicID = ecid->getLogicID();
0076     if (!logicID) {
0077       throw(std::runtime_error("ITimingDat::writeDB:  Bad EcalLogicID"));
0078     }
0079 
0080     try {
0081       m_writeStmt->setInt(1, iovID);
0082       m_writeStmt->setInt(2, logicID);
0083 
0084       m_writeStmt->setFloat(3, item->getTimingMean());
0085       m_writeStmt->setFloat(4, item->getTimingRMS());
0086       m_writeStmt->setInt(5, item->getTaskStatus());
0087 
0088       m_writeStmt->executeUpdate();
0089     } catch (SQLException& e) {
0090       throw(std::runtime_error("ITimingDat::writeDB():  " + e.getMessage()));
0091     }
0092   }
0093 
0094   template <class DATT, class IOVT>
0095   void writeArrayDB(const std::map<EcalLogicID, DATT>* data, IOVT* iov) noexcept(false) {
0096     using oracle::occi::OCCIFLOAT;
0097     using oracle::occi::OCCIINT;
0098 
0099     this->checkConnection();
0100     this->checkPrepare();
0101 
0102     int iovID = iov->fetchID();
0103     if (!iovID) {
0104       throw(std::runtime_error("ITimingDat::writeArrayDB:  IOV not in DB"));
0105     }
0106 
0107     int nrows = data->size();
0108     int* ids = new int[nrows];
0109     int* iovid_vec = new int[nrows];
0110     float* xx = new float[nrows];
0111     float* yy = new float[nrows];
0112     int* st = new int[nrows];
0113 
0114     ub2* ids_len = new ub2[nrows];
0115     ub2* iov_len = new ub2[nrows];
0116     ub2* x_len = new ub2[nrows];
0117     ub2* y_len = new ub2[nrows];
0118     ub2* st_len = new ub2[nrows];
0119 
0120     const EcalLogicID* channel;
0121     const DATT* dataitem;
0122     int count = 0;
0123     //  typedef std::map< EcalLogicID, DATT >::const_iterator CI;
0124     typedef typename std::map<EcalLogicID, DATT>::const_iterator CI;
0125     for (CI p = data->begin(); p != data->end(); ++p) {
0126       channel = &(p->first);
0127       int logicID = channel->getLogicID();
0128       if (!logicID) {
0129         throw(std::runtime_error("ITimingDat::writeArrayDB:  Bad EcalLogicID"));
0130       }
0131       ids[count] = logicID;
0132       iovid_vec[count] = iovID;
0133 
0134       dataitem = &(p->second);
0135 
0136       float x = dataitem->getTimingMean();
0137       float y = dataitem->getTimingRMS();
0138       int statu = dataitem->getTaskStatus();
0139 
0140       xx[count] = x;
0141       yy[count] = y;
0142       st[count] = statu;
0143 
0144       ids_len[count] = sizeof(ids[count]);
0145       iov_len[count] = sizeof(iovid_vec[count]);
0146 
0147       x_len[count] = sizeof(xx[count]);
0148       y_len[count] = sizeof(yy[count]);
0149       st_len[count] = sizeof(st[count]);
0150 
0151       count++;
0152     }
0153 
0154     try {
0155       m_writeStmt->setDataBuffer(1, (dvoid*)iovid_vec, OCCIINT, sizeof(iovid_vec[0]), iov_len);
0156       m_writeStmt->setDataBuffer(2, (dvoid*)ids, OCCIINT, sizeof(ids[0]), ids_len);
0157       m_writeStmt->setDataBuffer(3, (dvoid*)xx, OCCIFLOAT, sizeof(xx[0]), x_len);
0158       m_writeStmt->setDataBuffer(4, (dvoid*)yy, OCCIFLOAT, sizeof(yy[0]), y_len);
0159       m_writeStmt->setDataBuffer(5, (dvoid*)st, OCCIINT, sizeof(st[0]), st_len);
0160 
0161       m_writeStmt->executeArrayUpdate(nrows);
0162 
0163       delete[] ids;
0164       delete[] iovid_vec;
0165       delete[] xx;
0166       delete[] yy;
0167       delete[] st;
0168 
0169       delete[] ids_len;
0170       delete[] iov_len;
0171       delete[] x_len;
0172       delete[] y_len;
0173       delete[] st_len;
0174 
0175     } catch (SQLException& e) {
0176       throw(std::runtime_error("ITimingDat::writeArrayDB():  " + e.getMessage()));
0177     }
0178   }
0179 
0180   template <class DATT, class IOVT>
0181   void fetchData(std::map<EcalLogicID, DATT>* fillMap, IOVT* iov) noexcept(false) {
0182     this->checkConnection();
0183     fillMap->clear();
0184 
0185     iov->setConnection(m_env, m_conn);
0186     int iovID = iov->fetchID();
0187     if (!iovID) {
0188       //  throw(std::runtime_error("ITimingDat::writeDB:  IOV not in DB"));
0189       return;
0190     }
0191 
0192     try {
0193       m_readStmt->setSQL(
0194           "SELECT cv.name, cv.logic_id, cv.id1, cv.id2, cv.id3, cv.maps_to, "
0195           "d.timing_mean, d.timing_rms, d.task_status "
0196           "FROM channelview cv JOIN " +
0197           getTable() +
0198           "  d "
0199           "ON cv.logic_id = d.logic_id AND cv.name = cv.maps_to "
0200           "WHERE d.iov_id = :iov_id");
0201       m_readStmt->setInt(1, iovID);
0202       ResultSet* rset = m_readStmt->executeQuery();
0203 
0204       std::pair<EcalLogicID, DATT> p;
0205       DATT dat;
0206       while (rset->next()) {
0207         p.first = EcalLogicID(rset->getString(1),   // name
0208                               rset->getInt(2),      // logic_id
0209                               rset->getInt(3),      // id1
0210                               rset->getInt(4),      // id2
0211                               rset->getInt(5),      // id3
0212                               rset->getString(6));  // maps_to
0213 
0214         dat.setTimingMean(rset->getFloat(7));
0215         dat.setTimingRMS(rset->getFloat(8));
0216         dat.setTaskStatus(rset->getInt(9));
0217 
0218         p.second = dat;
0219         fillMap->insert(p);
0220       }
0221 
0222     } catch (SQLException& e) {
0223       throw(std::runtime_error("ITimingDat::fetchData():  " + e.getMessage()));
0224     }
0225   }
0226 
0227   // User data
0228   float m_timingMean;
0229   float m_timingRMS;
0230   bool m_taskStatus;
0231   std::string m_table_name;
0232 };
0233 
0234 #endif