CMS 3D CMS Logo

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

Go to the documentation of this file.
00001 #include <stdexcept>
00002 #include "OnlineDB/Oracle/interface/Oracle.h"
00003 
00004 #include "OnlineDB/EcalCondDB/interface/RunList.h"
00005 #include "OnlineDB/EcalCondDB/interface/RunIOV.h"
00006 #include "OnlineDB/EcalCondDB/interface/RunTag.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 RunList::RunList()
00014 {
00015   m_conn = NULL;
00016 }
00017 
00018 RunList::~RunList()
00019 {
00020 }
00021 
00022 void RunList::setRunTag(RunTag tag)
00023 {
00024   if (tag != m_runTag) {
00025     m_runTag = tag;
00026   }
00027 }
00028 
00029 
00030 RunTag RunList::getRunTag() const
00031 {
00032   return m_runTag;
00033 }
00034 
00035  std::vector<RunIOV> RunList::getRuns() 
00036 {
00037   return m_vec_runiov;
00038 }
00039 
00040 void RunList::fetchRuns()
00041   throw(std::runtime_error)
00042 {
00043   fetchRuns(-1, -1);
00044 }
00045 
00046 void RunList::fetchRuns(int min_run, int max_run)
00047   throw(std::runtime_error)
00048 {
00049 
00050   this->checkConnection();
00051   int nruns=0;
00052 
00053   m_runTag.setConnection(m_env, m_conn);
00054   int tagID = m_runTag.fetchID();
00055   cout <<"tag id="<< tagID << endl;
00056   if (!tagID) { 
00057     return ;
00058   }
00059 
00060   int my_min_run=min_run-1;
00061   int my_max_run=max_run+1;
00062   try {
00063     Statement* stmt0 = m_conn->createStatement();
00064     string sql =  "SELECT count(iov_id) FROM run_iov "
00065       "WHERE tag_id = :tag_id ";
00066     if (min_run > 0) {
00067       sql += " and run_iov.run_num> :min_run and run_iov.run_num< :max_run ";
00068     }
00069     stmt0->setSQL(sql);
00070     stmt0->setInt(1, tagID);
00071     if (min_run > 0) {
00072       stmt0->setInt(2, my_min_run);
00073       stmt0->setInt(3, my_max_run);
00074     }
00075     
00076     ResultSet* rset0 = stmt0->executeQuery();
00077     if (rset0->next()) {
00078       nruns = rset0->getInt(1);
00079     }
00080     m_conn->terminateStatement(stmt0);
00081 
00082     cout <<"number of runs="<< nruns << endl;
00083     m_vec_runiov.reserve(nruns);
00084     
00085     Statement* stmt = m_conn->createStatement();
00086     sql = "SELECT iov_id, tag_id, run_num, run_start, run_end, " 
00087       "db_timestamp FROM run_iov "
00088       " WHERE tag_id = :tag_id ";
00089     if (min_run > 0) {
00090       sql += " and run_iov.run_num> :min_run and run_iov.run_num< :max_run ";
00091     }
00092     sql +=               " order by run_num ";
00093     stmt->setSQL(sql);
00094     stmt->setInt(1, tagID);
00095     if (min_run > 0) {
00096       stmt->setInt(2, my_min_run);
00097       stmt->setInt(3, my_max_run);
00098     }
00099 
00100     DateHandler dh(m_env, m_conn);
00101     Tm runStart;
00102     Tm runEnd;
00103     Tm dbtime;
00104   
00105     ResultSet* rset = stmt->executeQuery();
00106     int i=0;
00107     while (i<nruns) {
00108       rset->next();
00109       int iovID = rset->getInt(1);
00110       // int tagID = rset->getInt(2);
00111       int runNum = rset->getInt(3);
00112       Date startDate = rset->getDate(4);
00113       Date endDate = rset->getDate(5);
00114       Date dbDate = rset->getDate(6);
00115          
00116       runStart = dh.dateToTm( startDate );
00117       runEnd = dh.dateToTm( endDate );
00118       dbtime = dh.dateToTm( dbDate );
00119        
00120       RunIOV r ;
00121       r.setRunNumber(runNum);
00122       r.setRunStart(runStart);
00123       r.setRunEnd(runEnd);
00124       r.setDBInsertionTime(dbtime);
00125       r.setRunTag(m_runTag);
00126       r.setID(iovID);
00127       m_vec_runiov.push_back(r);
00128       
00129       i++;
00130     }
00131     m_conn->terminateStatement(stmt);
00132   } catch (SQLException &e) {
00133     throw(std::runtime_error("RunList::fetchRuns:  "+e.getMessage()));
00134   }
00135 }
00136 
00137 void RunList::fetchLastNRuns( int max_run, int n_runs  )
00138   throw(std::runtime_error)
00139 {
00140 
00141   // fetch the last n_runs that come just before max_run (including max_run)
00142 
00143   this->checkConnection();
00144 
00145 
00146   m_runTag.setConnection(m_env, m_conn);
00147   int tagID = m_runTag.fetchID();
00148   cout <<"tag id="<< tagID << endl;
00149   if (!tagID) { 
00150     return ;
00151   }
00152 
00153   int my_max_run=max_run+1;
00154   try {
00155 
00156     int nruns=n_runs+1;
00157     m_vec_runiov.reserve(nruns);
00158     
00159     Statement* stmt = m_conn->createStatement();
00160     stmt->setSQL("select iov_id, tag_id, run_num, run_start, run_end, DB_TIMESTAMP from "
00161                  " (SELECT * from RUN_IOV "
00162                  " WHERE tag_id = :tag_id "
00163                  " and run_num< :max_run "
00164                  " order by run_num DESC ) where rownum< :n_runs ORDER BY run_num ASC " );
00165     stmt->setInt(1, tagID);
00166     stmt->setInt(2, my_max_run);
00167     stmt->setInt(3, nruns);
00168 
00169     DateHandler dh(m_env, m_conn);
00170     Tm runStart;
00171     Tm runEnd;
00172     Tm dbtime;
00173   
00174     ResultSet* rset = stmt->executeQuery();
00175     int i=0;
00176     while (i<n_runs) {
00177       rset->next();
00178       int iovID = rset->getInt(1);
00179       // int tagID = rset->getInt(2);
00180        int runNum = rset->getInt(3);
00181        Date startDate = rset->getDate(4);
00182        Date endDate = rset->getDate(5);
00183        Date dbDate = rset->getDate(6);
00184          
00185        runStart = dh.dateToTm( startDate );
00186        runEnd = dh.dateToTm( endDate );
00187        dbtime = dh.dateToTm( dbDate );
00188        
00189        RunIOV r ;
00190        r.setRunNumber(runNum);
00191        r.setRunStart(runStart);
00192        r.setRunEnd(runEnd);
00193        r.setDBInsertionTime(dbtime);
00194        r.setRunTag(m_runTag);
00195        r.setID(iovID);
00196        m_vec_runiov.push_back(r);
00197       
00198       i++;
00199     }
00200 
00201     m_conn->terminateStatement(stmt);
00202   } catch (SQLException &e) {
00203     throw(std::runtime_error("RunList::fetchLastNRuns:  "+e.getMessage()));
00204   }
00205 
00206 
00207 }
00208 
00209 
00210 
00211 void RunList::fetchRunsByLocation (int min_run, int max_run, const LocationDef locDef )
00212   throw(std::runtime_error)
00213 {
00214 
00215   this->checkConnection();
00216   int nruns=0;
00217 
00218   int my_min_run=min_run-1;
00219   int my_max_run=max_run+1;
00220   try {
00221     Statement* stmt0 = m_conn->createStatement();
00222     stmt0->setSQL("SELECT count(iov_id) FROM run_iov r , run_tag t, location_def l "
00223                  " WHERE r.tag_id=t.tag_id and t.LOCATION_ID=l.def_id AND l.LOCATION= :1 " 
00224                   "  and r.run_num> :2 and r.run_num< :3 ");
00225     stmt0->setString(1,locDef.getLocation() );
00226     stmt0->setInt(2, my_min_run);
00227     stmt0->setInt(3, my_max_run);
00228   
00229     ResultSet* rset0 = stmt0->executeQuery();
00230     if (rset0->next()) {
00231       nruns = rset0->getInt(1);
00232     }
00233     m_conn->terminateStatement(stmt0);
00234 
00235     cout <<"number of runs="<< nruns << endl;
00236     
00237     m_vec_runiov.reserve(nruns);
00238     
00239     Statement* stmt = m_conn->createStatement();
00240     stmt->setSQL("SELECT  r.iov_id, r.tag_id, r.run_num, r.run_start, r.run_end, r.DB_TIMESTAMP , "
00241                  " t.gen_tag, rt.RUN_TYPE "
00242                  " FROM run_iov r , run_tag t, location_def l, run_type_def rt "
00243                  " WHERE r.tag_id=t.tag_id and t.LOCATION_ID=l.def_id and t.run_type_id=rt.DEF_ID "
00244                  " AND l.LOCATION= :1 "
00245                  " and r.run_num> :2 and r.run_num< :3 "
00246                  " order by run_num " );
00247     stmt->setString(1,locDef.getLocation() );
00248     stmt->setInt(2, my_min_run);
00249     stmt->setInt(3, my_max_run);
00250 
00251     DateHandler dh(m_env, m_conn);
00252     Tm runStart;
00253     Tm runEnd;
00254     Tm dbtime;
00255   
00256     ResultSet* rset = stmt->executeQuery();
00257     int i=0;
00258     while (i<nruns) {
00259       rset->next();
00260       int iovID = rset->getInt(1);
00261       //       int tagID = rset->getInt(2);
00262        int runNum = rset->getInt(3);
00263        Date startDate = rset->getDate(4);
00264        Date endDate = rset->getDate(5);
00265        Date dbDate = rset->getDate(6);
00266          
00267        runStart = dh.dateToTm( startDate );
00268        runEnd = dh.dateToTm( endDate );
00269        dbtime = dh.dateToTm( dbDate );
00270        
00271        RunTag atag;
00272        atag.setLocationDef(locDef);
00273        atag.setGeneralTag(rset->getString(7));
00274        RunTypeDef rundef;
00275        rundef.setRunType(rset->getString(8));
00276        atag.setRunTypeDef(rundef);
00277 
00278        RunIOV r ;
00279        r.setRunNumber(runNum);
00280        r.setRunStart(runStart);
00281        r.setRunEnd(runEnd);
00282        r.setDBInsertionTime(dbtime);
00283        r.setRunTag(atag);
00284        r.setID(iovID);
00285        m_vec_runiov.push_back(r);
00286       
00287       i++;
00288     }
00289    
00290 
00291     m_conn->terminateStatement(stmt);
00292 
00293   } catch (SQLException &e) {
00294     throw(std::runtime_error("RunList::fetchRunsByLocation:  "+e.getMessage()));
00295   }
00296 
00297 
00298 }
00299 
00300 void RunList::fetchGlobalRunsByLocation (int min_run, int max_run, const LocationDef locDef )
00301   throw(std::runtime_error)
00302 {
00303 
00304   this->checkConnection();
00305   int nruns=0;
00306 
00307   int my_min_run=min_run-1;
00308   int my_max_run=max_run+1;
00309   try {
00310     Statement* stmt0 = m_conn->createStatement();
00311     stmt0->setSQL("SELECT count(iov_id) FROM run_iov r , run_tag t, location_def l "
00312                  " WHERE r.tag_id=t.tag_id and t.LOCATION_ID=l.def_id AND l.LOCATION= :1 " 
00313                   "  and t.gen_tag='GLOBAL' "
00314                   "  and r.run_num> :2 and r.run_num< :3 ");
00315     stmt0->setString(1,locDef.getLocation() );
00316     stmt0->setInt(2, my_min_run);
00317     stmt0->setInt(3, my_max_run);
00318   
00319     ResultSet* rset0 = stmt0->executeQuery();
00320     if (rset0->next()) {
00321       nruns = rset0->getInt(1);
00322     }
00323     m_conn->terminateStatement(stmt0);
00324 
00325     cout <<"number of runs="<< nruns << endl;
00326     
00327     m_vec_runiov.reserve(nruns);
00328     
00329     Statement* stmt = m_conn->createStatement();
00330     stmt->setSQL("SELECT  r.iov_id, r.tag_id, r.run_num, r.run_start, r.run_end, r.DB_TIMESTAMP , "
00331                  " t.gen_tag, rt.RUN_TYPE "
00332                  " FROM run_iov r , run_tag t, location_def l, run_type_def rt "
00333                  " WHERE r.tag_id=t.tag_id and t.LOCATION_ID=l.def_id and t.run_type_id=rt.DEF_ID "
00334                  " AND l.LOCATION= :1 "
00335                  " and t.gen_tag='GLOBAL' "
00336                  " and r.run_num> :2 and r.run_num< :3 "
00337                  " order by run_num " );
00338     stmt->setString(1,locDef.getLocation() );
00339     stmt->setInt(2, my_min_run);
00340     stmt->setInt(3, my_max_run);
00341 
00342     DateHandler dh(m_env, m_conn);
00343     Tm runStart;
00344     Tm runEnd;
00345     Tm dbtime;
00346   
00347     ResultSet* rset = stmt->executeQuery();
00348     int i=0;
00349     while (i<nruns) {
00350       rset->next();
00351       int iovID = rset->getInt(1);
00352       //       int tagID = rset->getInt(2);
00353        int runNum = rset->getInt(3);
00354        Date startDate = rset->getDate(4);
00355        Date endDate = rset->getDate(5);
00356        Date dbDate = rset->getDate(6);
00357          
00358        runStart = dh.dateToTm( startDate );
00359        runEnd = dh.dateToTm( endDate );
00360        dbtime = dh.dateToTm( dbDate );
00361        
00362        RunTag atag;
00363        atag.setLocationDef(locDef);
00364        atag.setGeneralTag(rset->getString(7));
00365        RunTypeDef rundef;
00366        rundef.setRunType(rset->getString(8));
00367        atag.setRunTypeDef(rundef);
00368 
00369        RunIOV r ;
00370        r.setRunNumber(runNum);
00371        r.setRunStart(runStart);
00372        r.setRunEnd(runEnd);
00373        r.setDBInsertionTime(dbtime);
00374        r.setRunTag(atag);
00375        r.setID(iovID);
00376        m_vec_runiov.push_back(r);
00377       
00378       i++;
00379     }
00380    
00381 
00382     m_conn->terminateStatement(stmt);
00383 
00384   } catch (SQLException &e) {
00385     throw(std::runtime_error("RunList::fetchRunsByLocation:  "+e.getMessage()));
00386   }
00387 
00388 
00389 }