CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/OnlineDB/EcalCondDB/src/MonRunIOV.cc

Go to the documentation of this file.
00001 #include <stdexcept>
00002 #include "OnlineDB/Oracle/interface/Oracle.h"
00003 
00004 #include "OnlineDB/EcalCondDB/interface/MonRunIOV.h"
00005 #include "OnlineDB/EcalCondDB/interface/MonRunTag.h"
00006 #include "OnlineDB/EcalCondDB/interface/RunIOV.h"
00007 #include "OnlineDB/EcalCondDB/interface/Tm.h"
00008 #include "OnlineDB/EcalCondDB/interface/DateHandler.h"
00009 
00010 using namespace std;
00011 using namespace oracle::occi;
00012 
00013 MonRunIOV::MonRunIOV()
00014 {
00015   m_conn = NULL;
00016   m_ID = 0;
00017   m_monRunTag = MonRunTag();
00018   m_runIOV = RunIOV();
00019   m_subRunNum = 0;
00020   m_subRunStart = Tm();
00021   m_subRunEnd = Tm();
00022 }
00023 
00024 
00025 
00026 MonRunIOV::~MonRunIOV()
00027 {
00028 }
00029 
00030 
00031 void MonRunIOV::setID(int id)
00032 {
00033     m_ID = id;
00034 }
00035 
00036 void MonRunIOV::setMonRunTag(MonRunTag tag)
00037 {
00038   if (tag != m_monRunTag) {
00039     m_ID = 0;
00040     m_monRunTag = tag;
00041   }
00042 }
00043 
00044 
00045 
00046 MonRunTag MonRunIOV::getMonRunTag() const
00047 {
00048   return m_monRunTag;
00049 }
00050 
00051 
00052 
00053 void MonRunIOV::setRunIOV(RunIOV iov)
00054 {
00055   if (iov != m_runIOV) {
00056     m_ID = 0;
00057     m_runIOV = iov;
00058   }
00059 }
00060 
00061 RunIOV MonRunIOV::getRunIOV() 
00062 { 
00063   return m_runIOV;
00064 }
00065 
00066 
00067 void MonRunIOV::setSubRunNumber(subrun_t subrun)
00068 {
00069   if (subrun != m_subRunNum) {
00070     m_ID = 0;
00071     m_subRunNum = subrun;
00072   }
00073 }
00074 
00075 
00076 
00077 run_t MonRunIOV::getSubRunNumber() const
00078 {
00079   return m_subRunNum;
00080 }
00081 
00082 
00083 
00084 void MonRunIOV::setSubRunStart(Tm start)
00085 {
00086   if (start != m_subRunStart) {
00087     m_ID = 0;
00088     m_subRunStart = start;
00089   }
00090 }
00091 
00092 
00093 
00094 Tm MonRunIOV::getSubRunStart() const
00095 {
00096   return m_subRunStart;
00097 }
00098 
00099 
00100 
00101 void MonRunIOV::setSubRunEnd(Tm end)
00102 {
00103   if (end != m_subRunEnd) {
00104     m_ID = 0;
00105     m_subRunEnd = end;
00106   }
00107 }
00108 
00109 
00110 
00111 Tm MonRunIOV::getSubRunEnd() const
00112 {
00113   return m_subRunEnd;
00114 }
00115 
00116 
00117 
00118 int MonRunIOV::fetchID()
00119   throw(std::runtime_error)
00120 {
00121   // Return from memory if available
00122   if (m_ID) {
00123     return m_ID;
00124   }
00125 
00126   this->checkConnection();
00127 
00128   // fetch the parent IDs
00129   int monRunTagID, runIOVID;
00130   this->fetchParentIDs(&monRunTagID, &runIOVID);
00131 
00132   if (!monRunTagID || !runIOVID) { 
00133     return 0;
00134   }
00135 
00136   DateHandler dh(m_env, m_conn);
00137 
00138   if (m_subRunEnd.isNull()) {
00139     m_subRunEnd = dh.getPlusInfTm();
00140   }
00141 
00142   try {
00143     Statement* stmt = m_conn->createStatement();
00144     stmt->setSQL("SELECT iov_id FROM mon_run_iov "
00145                  "WHERE tag_id = :1 AND "
00146                  "run_iov_id   = :2 AND "
00147                  "subrun_num   = :3 AND "
00148                  "subrun_start = :4 AND "
00149                  "subrun_end   = :5");
00150     stmt->setInt(1, monRunTagID);
00151     stmt->setInt(2, runIOVID);
00152     stmt->setInt(3, m_subRunNum);
00153     stmt->setDate(4, dh.tmToDate(m_subRunStart));
00154     stmt->setDate(5, dh.tmToDate(m_subRunEnd));
00155   
00156     ResultSet* rset = stmt->executeQuery();
00157 
00158     if (rset->next()) {
00159       m_ID = rset->getInt(1);
00160     } else {
00161       m_ID = 0;
00162     }
00163     m_conn->terminateStatement(stmt);
00164   } catch (SQLException &e) {
00165     throw(std::runtime_error("MonRunIOV::fetchID:  "+e.getMessage()));
00166   }
00167 
00168   return m_ID;
00169 }
00170 
00171 
00172 
00173 void MonRunIOV::setByID(int id) 
00174   throw(std::runtime_error)
00175 {
00176    this->checkConnection();
00177 
00178    DateHandler dh(m_env, m_conn);
00179 
00180    try {
00181      Statement* stmt = m_conn->createStatement();
00182 
00183      stmt->setSQL("SELECT tag_id, run_iov_id, subrun_num, subrun_start, subrun_end FROM mon_run_iov WHERE iov_id = :1");
00184      stmt->setInt(1, id);
00185      
00186      ResultSet* rset = stmt->executeQuery();
00187      if (rset->next()) {
00188        int monRunTagID = rset->getInt(1);
00189        int runIOVID = rset->getInt(2);
00190        m_subRunNum = rset->getInt(3);
00191        Date startDate = rset->getDate(4);
00192        Date endDate = rset->getDate(5);
00193          
00194        m_subRunStart = dh.dateToTm( startDate );
00195        m_subRunEnd = dh.dateToTm( endDate );
00196 
00197        m_monRunTag.setConnection(m_env, m_conn);
00198        m_monRunTag.setByID(monRunTagID);
00199 
00200        m_runIOV.setConnection(m_env, m_conn);
00201        m_runIOV.setByID(runIOVID);
00202 
00203        m_ID = id;
00204      } else {
00205        throw(std::runtime_error("MonRunIOV::setByID:  Given tag_id is not in the database"));
00206      }
00207      
00208      m_conn->terminateStatement(stmt);
00209    } catch (SQLException &e) {
00210      throw(std::runtime_error("MonRunIOV::setByID:  "+e.getMessage()));
00211    }
00212 }
00213 
00214 
00215 
00216 int MonRunIOV::writeDB()
00217   throw(std::runtime_error)
00218 {
00219   this->checkConnection();
00220 
00221   // Check if this IOV has already been written
00222   if (this->fetchID()) {
00223     return m_ID;
00224   }
00225 
00226   // fetch Parent IDs
00227   int monRunTagID, runIOVID;
00228   this->fetchParentIDs(&monRunTagID, &runIOVID);
00229                        
00230   if (!monRunTagID) {
00231     monRunTagID = m_monRunTag.writeDB();
00232   }
00233   
00234   // Validate the data, use infinity-till convention
00235   DateHandler dh(m_env, m_conn);
00236 
00237   if (m_subRunStart.isNull()) {
00238     throw(std::runtime_error("MonRunIOV::writeDB:  Must setSubRunStart before writing"));
00239   }
00240   
00241   if (m_subRunEnd.isNull()) {
00242     m_subRunEnd = dh.getPlusInfTm();
00243   }
00244 
00245   try {
00246     Statement* stmt = m_conn->createStatement();
00247     
00248     stmt->setSQL("INSERT INTO mon_run_iov (iov_id, tag_id, run_iov_id, subrun_num, subrun_start, subrun_end) "
00249                  "VALUES (mon_run_iov_sq.NextVal, :1, :2, :3, :4, :5)");
00250     stmt->setInt(1, monRunTagID);
00251     stmt->setInt(2, runIOVID);
00252     stmt->setInt(3, m_subRunNum);
00253     stmt->setDate(4, dh.tmToDate(m_subRunStart));
00254     stmt->setDate(5, dh.tmToDate(m_subRunEnd));
00255 
00256     stmt->executeUpdate();
00257 
00258     m_conn->terminateStatement(stmt);
00259   } catch (SQLException &e) {
00260     throw(std::runtime_error("MonRunIOV::writeDB:  "+e.getMessage()));
00261   }
00262 
00263   // Now get the ID
00264   if (!this->fetchID()) {
00265     throw(std::runtime_error("MonRunIOV::writeDB:  Failed to write"));
00266   }
00267   
00268   return m_ID;
00269 }
00270 
00271 
00272 
00273 void MonRunIOV::fetchParentIDs(int* monRunTagID, int* runIOVID)
00274   throw(std::runtime_error)
00275 {
00276   // get the MonRunTag
00277   m_monRunTag.setConnection(m_env, m_conn);
00278   *monRunTagID = m_monRunTag.fetchID();
00279 
00280   // get the RunIOV
00281   m_runIOV.setConnection(m_env, m_conn);
00282   *runIOVID = m_runIOV.fetchID();
00283 
00284   if (! *runIOVID) { 
00285     throw(std::runtime_error("MonRunIOV:  Given RunIOV does not exist in DB")); 
00286   }
00287 
00288 }
00289 
00290 
00291 
00292 void MonRunIOV::setByRun(MonRunTag* montag, RunIOV* runiov, subrun_t subrun)
00293   throw(std::runtime_error)
00294 {
00295   this->checkConnection();
00296   
00297   runiov->setConnection(m_env, m_conn);
00298   int runIOVID = runiov->fetchID();
00299 
00300   if (!runIOVID) {
00301     throw(std::runtime_error("MonRunIOV::setByRun:  Given RunIOV does not exist in DB"));
00302   }
00303 
00304   montag->setConnection(m_env, m_conn);
00305   int monTagID = montag->fetchID();
00306   
00307   if (!monTagID) {
00308     throw(std::runtime_error("MonRunIOV::setByRun:  Given MonRunTag does not exist in the DB"));
00309   }
00310 
00311   DateHandler dh(m_env, m_conn);
00312 
00313   try {
00314     Statement* stmt = m_conn->createStatement();
00315 
00316     stmt->setSQL("SELECT iov_id, subrun_start, subrun_end FROM mon_run_iov "
00317                  "WHERE tag_id = :1 AND run_iov_id = :2 AND subrun_num = :3");
00318     stmt->setInt(1, monTagID);
00319     stmt->setInt(2, runIOVID);
00320     stmt->setInt(3, subrun);
00321 
00322     ResultSet* rset = stmt->executeQuery();
00323     if (rset->next()) {
00324       m_monRunTag = *montag;
00325       m_runIOV = *runiov;
00326       m_subRunNum = subrun;
00327       
00328       m_ID = rset->getInt(1);
00329       Date startDate = rset->getDate(2);
00330       Date endDate = rset->getDate(3);
00331          
00332       m_subRunStart = dh.dateToTm( startDate );
00333       m_subRunEnd = dh.dateToTm( endDate );
00334     } else {
00335       throw(std::runtime_error("MonRunIOV::setByRun:  Given subrun is not in the database"));
00336     }
00337      
00338     m_conn->terminateStatement(stmt);
00339   } catch (SQLException &e) {
00340     throw(std::runtime_error("MonRunIOV::setByRun:  "+e.getMessage()));
00341   }
00342   
00343 }