CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/OnlineDB/EcalCondDB/src/RunIOV.cc

Go to the documentation of this file.
00001 #include <stdexcept>
00002 #include "OnlineDB/Oracle/interface/Oracle.h"
00003 
00004 #include "OnlineDB/EcalCondDB/interface/RunIOV.h"
00005 #include "OnlineDB/EcalCondDB/interface/RunTag.h"
00006 #include "OnlineDB/EcalCondDB/interface/Tm.h"
00007 #include "OnlineDB/EcalCondDB/interface/DateHandler.h"
00008 
00009 using namespace std;
00010 using namespace oracle::occi;
00011 
00012 RunIOV::RunIOV()
00013 {
00014   m_conn = NULL;
00015   m_ID = 0;
00016   m_runNum = 0;
00017   m_runStart = Tm();
00018   m_runEnd = Tm();
00019 }
00020 
00021 
00022 
00023 RunIOV::~RunIOV()
00024 {
00025 }
00026 
00027 
00028 
00029 void RunIOV::setRunNumber(run_t run)
00030 {
00031   if ( run != m_runNum) {
00032     m_ID = 0;
00033     m_runNum = run;
00034   }
00035 }
00036 
00037 void RunIOV::setID(int id)
00038 {
00039      m_ID = id;
00040    }
00041 
00042 
00043 
00044 
00045 run_t RunIOV::getRunNumber() const
00046 {
00047   return m_runNum;
00048 }
00049 
00050 
00051 
00052 void RunIOV::setRunStart(Tm start)
00053 {
00054   if (start != m_runStart) {
00055     m_ID = 0;
00056     m_runStart = start;
00057   }
00058 }
00059 
00060 
00061 
00062 Tm RunIOV::getRunStart() const
00063 {
00064   return m_runStart;
00065 }
00066 
00067 
00068 
00069 void RunIOV::setRunEnd(Tm end)
00070 {
00071   if (end != m_runEnd) {
00072     m_ID = 0;
00073     m_runEnd = end;
00074   }
00075 }
00076 
00077 
00078 
00079 Tm RunIOV::getRunEnd() const
00080 {
00081   return m_runEnd;
00082 }
00083 
00084 
00085 
00086 void RunIOV::setRunTag(RunTag tag)
00087 {
00088   if (tag != m_runTag) {
00089     m_ID = 0;
00090     m_runTag = tag;
00091   }
00092 }
00093 
00094 
00095 
00096 RunTag RunIOV::getRunTag() const
00097 {
00098   return m_runTag;
00099 }
00100 
00101 
00102 
00103 int RunIOV::fetchID()
00104   throw(std::runtime_error)
00105 {
00106   // Return from memory if available
00107   if (m_ID) {
00108     return m_ID;
00109   }
00110 
00111   this->checkConnection();
00112 
00113   m_runTag.setConnection(m_env, m_conn);
00114   int tagID = m_runTag.fetchID();
00115   if (!tagID) { 
00116     return 0;
00117   }
00118 
00119   DateHandler dh(m_env, m_conn);
00120 
00121   if (m_runEnd.isNull()) {
00122     m_runEnd = dh.getPlusInfTm();
00123   }
00124 
00125   try {
00126     Statement* stmt = m_conn->createStatement();
00127     stmt->setSQL("SELECT iov_id FROM run_iov "
00128                  "WHERE tag_id = :tag_id AND "
00129                  "run_num = :run_num AND "
00130                  "run_start = :run_start  " );
00131     stmt->setInt(1, tagID);
00132     stmt->setInt(2, m_runNum);
00133     stmt->setDate(3, dh.tmToDate(m_runStart));
00134   
00135     ResultSet* rset = stmt->executeQuery();
00136 
00137     if (rset->next()) {
00138       m_ID = rset->getInt(1);
00139     } else {
00140       m_ID = 0;
00141     }
00142     m_conn->terminateStatement(stmt);
00143   } catch (SQLException &e) {
00144     throw(std::runtime_error("RunIOV::fetchID:  "+e.getMessage()));
00145   }
00146 
00147   return m_ID;
00148 }
00149 
00150 
00151 
00152 void RunIOV::setByID(int id) 
00153   throw(std::runtime_error)
00154 {
00155    this->checkConnection();
00156 
00157    DateHandler dh(m_env, m_conn);
00158 
00159    try {
00160      Statement* stmt = m_conn->createStatement();
00161 
00162      stmt->setSQL("SELECT tag_id, run_num, run_start, run_end FROM run_iov WHERE iov_id = :1");
00163      stmt->setInt(1, id);
00164      
00165      ResultSet* rset = stmt->executeQuery();
00166      if (rset->next()) {
00167        int tagID = rset->getInt(1);
00168        m_runNum = rset->getInt(2);
00169        Date startDate = rset->getDate(3);
00170        Date endDate = rset->getDate(4);
00171          
00172        m_runStart = dh.dateToTm( startDate );
00173        m_runEnd = dh.dateToTm( endDate );
00174 
00175        m_runTag.setConnection(m_env, m_conn);
00176        m_runTag.setByID(tagID);
00177        m_ID = id;
00178      } else {
00179        throw(std::runtime_error("RunIOV::setByID:  Given tag_id is not in the database"));
00180      }
00181      
00182      m_conn->terminateStatement(stmt);
00183    } catch (SQLException &e) {
00184      throw(std::runtime_error("RunIOV::setByID:  "+e.getMessage()));
00185    }
00186 }
00187 
00188 
00189 
00190 int RunIOV::writeDB()
00191   throw(std::runtime_error)
00192 {
00193   this->checkConnection();
00194 
00195   // Check if this IOV has already been written
00196   if (this->fetchID()) {
00197     return m_ID;
00198   }
00199   
00200   m_runTag.setConnection(m_env, m_conn);
00201   int tagID = m_runTag.writeDB();
00202   
00203   // Validate the data, use infinity-till convention
00204   DateHandler dh(m_env, m_conn);
00205 
00206   if (m_runStart.isNull()) {
00207     throw(std::runtime_error("RunIOV::writeDB:  Must setRunStart before writing"));
00208   }
00209   
00210   if (m_runEnd.isNull()) {
00211     m_runEnd = dh.getPlusInfTm();
00212   }
00213 
00214   try {
00215     Statement* stmt = m_conn->createStatement();
00216     
00217     stmt->setSQL("INSERT INTO run_iov (iov_id, tag_id, run_num, run_start, run_end) "
00218                  "VALUES (run_iov_sq.NextVal, :1, :2, :3, :4)");
00219     stmt->setInt(1, tagID);
00220     stmt->setInt(2, m_runNum);
00221     stmt->setDate(3, dh.tmToDate(m_runStart));
00222     stmt->setDate(4, dh.tmToDate(m_runEnd));
00223 
00224     stmt->executeUpdate();
00225 
00226     m_conn->terminateStatement(stmt);
00227   } catch (SQLException &e) {
00228     throw(std::runtime_error("RunIOV::writeDB:  "+e.getMessage()));
00229   }
00230 
00231   // Now get the ID
00232   if (!this->fetchID()) {
00233     throw(std::runtime_error("RunIOV::writeDB:  Failed to write"));
00234   }
00235   
00236   return m_ID;
00237 }
00238 
00239 
00240 int RunIOV::updateEndTimeDB()
00241   throw(std::runtime_error)
00242 {
00243   this->checkConnection();
00244 
00245   // Check if this IOV has already been written
00246   if(!this->fetchID()){
00247     this->writeDB();
00248   }
00249 
00250 
00251   m_runTag.setConnection(m_env, m_conn);
00252   //  int tagID = m_runTag.writeDB();
00253   
00254   // Validate the data, use infinity-till convention
00255   DateHandler dh(m_env, m_conn);
00256 
00257   // we only update the run end here   
00258   if (m_runEnd.isNull()) {
00259     m_runEnd = dh.getPlusInfTm();
00260   }
00261 
00262   try {
00263     Statement* stmt = m_conn->createStatement();
00264     
00265     stmt->setSQL("UPDATE run_iov set run_end=:1 where iov_id=:2 " );
00266     stmt->setDate(1, dh.tmToDate(m_runEnd));
00267     stmt->setInt(2, m_ID);
00268 
00269     stmt->executeUpdate();
00270 
00271     m_conn->terminateStatement(stmt);
00272   } catch (SQLException &e) {
00273     throw(std::runtime_error("RunIOV::writeDB:  "+e.getMessage()));
00274   }
00275 
00276   // Now get the ID
00277   if (!this->fetchID()) {
00278     throw(std::runtime_error("RunIOV::writeDB:  Failed to write"));
00279   }
00280   
00281   return m_ID;
00282 }
00283 
00284 int RunIOV::fetchIDByRunAndTag()
00285   throw(std::runtime_error)
00286 {
00287   // Return from memory if available
00288   if (m_ID) {
00289     return m_ID;
00290   }
00291 
00292   this->checkConnection();
00293 
00294   m_runTag.setConnection(m_env, m_conn);
00295   int tagID = m_runTag.fetchID();
00296   if (!tagID) { 
00297     return 0;
00298   }
00299 
00300   DateHandler dh(m_env, m_conn);
00301 
00302   if (m_runEnd.isNull()) {
00303     m_runEnd = dh.getPlusInfTm();
00304   }
00305 
00306   try {
00307     Statement* stmt = m_conn->createStatement();
00308     stmt->setSQL("SELECT iov_id FROM run_iov "
00309                  "WHERE tag_id = :tag_id AND "
00310                  "run_num = :run_num " );
00311     stmt->setInt(1, tagID);
00312     stmt->setInt(2, m_runNum);
00313   
00314     ResultSet* rset = stmt->executeQuery();
00315 
00316     if (rset->next()) {
00317       m_ID = rset->getInt(1);
00318     } else {
00319       m_ID = 0;
00320     }
00321     m_conn->terminateStatement(stmt);
00322   } catch (SQLException &e) {
00323     throw(std::runtime_error("RunIOV::fetchID:  "+e.getMessage()));
00324   }
00325 
00326   return m_ID;
00327 }
00328 
00329 
00330 int RunIOV::updateStartTimeDB()
00331   throw(std::runtime_error)
00332 {
00333   this->checkConnection();
00334 
00335   // Check if this IOV has already been written
00336   if(!this->fetchIDByRunAndTag()){
00337     this->writeDB();
00338   }
00339 
00340 
00341   //  m_runTag.setConnection(m_env, m_conn);
00342   // int tagID = m_runTag.writeDB();
00343   
00344   // Validate the data, use infinity-till convention
00345   DateHandler dh(m_env, m_conn);
00346 
00347   // we only update the run start here   
00348   if (m_runEnd.isNull()) {
00349     m_runEnd = dh.getPlusInfTm();
00350   }
00351 
00352   try {
00353     Statement* stmt = m_conn->createStatement();
00354     
00355     stmt->setSQL("UPDATE run_iov set run_start=:1 where iov_id=:2 " );
00356     stmt->setDate(1, dh.tmToDate(m_runStart));
00357     stmt->setInt(2, m_ID);
00358 
00359     stmt->executeUpdate();
00360 
00361     m_conn->terminateStatement(stmt);
00362   } catch (SQLException &e) {
00363     throw(std::runtime_error("RunIOV::writeDB:  "+e.getMessage()));
00364   }
00365 
00366   // Now get the ID
00367   if (!this->fetchID()) {
00368     throw(std::runtime_error("RunIOV::writeDB:  Failed to write"));
00369   }
00370   
00371   return m_ID;
00372 }
00373 
00374 
00375 
00376 void RunIOV::setByRun(RunTag* tag, run_t run) 
00377   throw(std::runtime_error)
00378 {
00379    this->checkConnection();
00380 
00381    tag->setConnection(m_env, m_conn);
00382    int tagID = tag->fetchID();
00383    if (!tagID) {
00384      throw(std::runtime_error("RunIOV::setByRun:  Given tag is not in the database"));
00385    }
00386    
00387    DateHandler dh(m_env, m_conn);
00388 
00389    try {
00390      Statement* stmt = m_conn->createStatement();
00391 
00392      stmt->setSQL("SELECT iov_id, run_start, run_end FROM run_iov WHERE tag_id = :1 AND run_num = :2");
00393      stmt->setInt(1, tagID);
00394      stmt->setInt(2, run);
00395      
00396      ResultSet* rset = stmt->executeQuery();
00397      if (rset->next()) {
00398        m_runTag = *tag;
00399        m_runNum = run;
00400 
00401        m_ID = rset->getInt(1);
00402        Date startDate = rset->getDate(2);
00403        Date endDate = rset->getDate(3);
00404          
00405        m_runStart = dh.dateToTm( startDate );
00406        m_runEnd = dh.dateToTm( endDate );
00407      } else {
00408        throw(std::runtime_error("RunIOV::setByRun:  Given run is not in the database"));
00409      }
00410      
00411      m_conn->terminateStatement(stmt);
00412    } catch (SQLException &e) {
00413      throw(std::runtime_error("RunIOV::setByRun:  "+e.getMessage()));
00414    }
00415 }
00416 
00417 
00418 
00419 void RunIOV::setByRun(std::string location, run_t run) 
00420   throw(std::runtime_error)
00421 {
00422   this->checkConnection();
00423    
00424   DateHandler dh(m_env, m_conn);
00425 
00426    try {
00427      Statement* stmt = m_conn->createStatement();
00428 
00429      stmt->setSQL("SELECT iov_id FROM run_iov riov "
00430                   "JOIN run_tag rtag ON riov.tag_id = rtag.tag_id "
00431                   "JOIN location_def loc ON rtag.location_id = loc.def_id "
00432                   "WHERE loc.location = :1 AND riov.run_num = :2 "
00433                   "AND rtag.gen_tag != 'INVALID'");
00434      stmt->setString(1, location);
00435      stmt->setInt(2, run);
00436      
00437      ResultSet* rset = stmt->executeQuery();
00438      if (rset->next()) {
00439        int id = rset->getInt(1);
00440        this->setByID(id);
00441      } else {
00442        throw(std::runtime_error("RunIOV::setByRun(loc, run):  Given run is not in the database"));
00443      }
00444      
00445      // Check for uniqueness of run
00446      if (rset->next()) {
00447        throw(std::runtime_error("RunIOV::setByRun(loc, run):  Run is nonunique for given location."));
00448      }
00449 
00450      m_conn->terminateStatement(stmt);
00451    } catch (SQLException &e) {
00452      throw(std::runtime_error("RunIOV::setByRun(loc, run):  "+e.getMessage()));
00453    }
00454 }
00455 
00456 
00457 void RunIOV::setByRecentData(std::string dataTable, RunTag* tag, run_t run) 
00458   throw(std::runtime_error)
00459 {
00460    this->checkConnection();
00461 
00462    tag->setConnection(m_env, m_conn);
00463    int tagID = tag->fetchID();
00464    if (!tagID) {
00465      throw(std::runtime_error("RunIOV::setByRecentData:  Given tag is not in the database"));
00466    }
00467    
00468    DateHandler dh(m_env, m_conn);
00469 
00470    try {
00471      Statement* stmt = m_conn->createStatement();
00472 
00473      stmt->setSQL("SELECT * FROM (SELECT riov.iov_id, riov.run_num, riov.run_start, riov.run_end "
00474                   "FROM run_iov riov "
00475                   "JOIN "+dataTable+" dat on dat.iov_id = riov.iov_id "
00476                   "WHERE tag_id = :1 AND riov.run_num <= :run ORDER BY riov.run_num DESC) WHERE rownum = 1");
00477 
00478      stmt->setInt(1, tagID);
00479      stmt->setInt(2, run);
00480      
00481      ResultSet* rset = stmt->executeQuery();
00482      if (rset->next()) {
00483        m_runTag = *tag;
00484 
00485        m_ID = rset->getInt(1);
00486        m_runNum = rset->getInt(2);
00487        Date startDate = rset->getDate(3);
00488        Date endDate = rset->getDate(4);
00489          
00490        m_runStart = dh.dateToTm( startDate );
00491        m_runEnd = dh.dateToTm( endDate );
00492      } else {
00493        throw(std::runtime_error("RunIOV::setByRecentData:  No data exists for given tag and run"));
00494      }
00495      
00496      m_conn->terminateStatement(stmt);
00497    } catch (SQLException &e) {
00498      throw(std::runtime_error("RunIOV::setByRecentData:  "+e.getMessage()));
00499    }
00500 }
00501 
00502 
00503 
00504 
00505 
00506 
00507 void RunIOV::setByRecentData(std::string dataTable, std::string location, run_t run) 
00508   throw(std::runtime_error)
00509 {
00510   this->checkConnection();
00511    
00512   DateHandler dh(m_env, m_conn);
00513 
00514    try {
00515      Statement* stmt = m_conn->createStatement();
00516 
00517      stmt->setSQL("SELECT * FROM (SELECT riov.iov_id, riov.run_num, riov.run_start, riov.run_end "
00518                   "FROM run_iov riov "
00519                   "JOIN "+dataTable+" dat on dat.iov_id = riov.iov_id "
00520                   "JOIN run_tag rtag ON riov.tag_id = rtag.tag_id "
00521                   "JOIN location_def loc ON rtag.location_id = loc.def_id "
00522                   "WHERE loc.location = :1 AND riov.run_num <= :2 ORDER BY riov.run_num DESC ) WHERE rownum = 1");
00523 
00524      stmt->setString(1, location);
00525      stmt->setInt(2, run);
00526      
00527      ResultSet* rset = stmt->executeQuery();
00528     
00529 
00530      if (rset->next()) {
00531        int id = rset->getInt(1);
00532        this->setByID(id);
00533      } else {
00534        throw(std::runtime_error("RunIOV::setByRecentData(datatable, loc, run):  Given run is not in the database"));
00535      }
00536 
00537      
00538      m_conn->terminateStatement(stmt);
00539    } catch (SQLException &e) {
00540      throw(std::runtime_error("RunIOV::setByRecentData:  "+e.getMessage()));
00541    }
00542 }
00543