00001 #include <stdexcept>
00002 #include <string>
00003 #include "OnlineDB/Oracle/interface/Oracle.h"
00004
00005 #include "OnlineDB/EcalCondDB/interface/LMFPNIRedDat.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 LMFPNIRedDat::LMFPNIRedDat()
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 LMFPNIRedDat::~LMFPNIRedDat()
00026 {
00027 }
00028
00029
00030
00031 void LMFPNIRedDat::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_ired_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("LMFPNIRedDat::prepareWrite(): "+e.getMessage()));
00044 }
00045 }
00046
00047
00048
00049 void LMFPNIRedDat::writeDB(const EcalLogicID* ecid, const LMFPNIRedDat* 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("LMFPNIRedDat::writeDB: IOV not in DB")); }
00057
00058 int logicID = ecid->getLogicID();
00059 if (!logicID) { throw(runtime_error("LMFPNIRedDat::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("LMFPNIRedDat::writeDB(): "+e.getMessage()));
00071 }
00072 }
00073
00074
00075
00076 void LMFPNIRedDat::fetchData(std::map< EcalLogicID, LMFPNIRedDat >* fillMap, LMFRunIOV* iov)
00077 throw(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.pn_peak, d.pn_err "
00093 "FROM channelview cv JOIN lmf_pn_ired_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, LMFPNIRedDat > p;
00100 LMFPNIRedDat 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.setPNPeak( rset->getFloat(7) );
00110 dat.setPNErr( rset->getFloat(8) );
00111
00112 p.second = dat;
00113 fillMap->insert(p);
00114 }
00115
00116 } catch (SQLException &e) {
00117 throw(runtime_error("LMFPNIRedDat::fetchData(): "+e.getMessage()));
00118 }
00119 }