CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/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::fetchNonEmptyRuns() 
00041   throw(std::runtime_error)
00042 {
00043   fetchRuns(-1, -1, true, false); 
00044 }
00045 
00046 void RunList::fetchNonEmptyGlobalRuns() 
00047   throw(std::runtime_error)
00048 {
00049   fetchRuns(-1, -1, false, true); 
00050 }
00051 
00052 void RunList::fetchNonEmptyRuns(int min_run, int max_run) 
00053   throw(std::runtime_error)
00054 {
00055   fetchRuns(min_run, max_run, true, false); 
00056 }
00057 
00058 void RunList::fetchNonEmptyGlobalRuns(int min_run, int max_run) 
00059   throw(std::runtime_error)
00060 {
00061   fetchRuns(min_run, max_run, false, true); 
00062 }
00063 
00064 void RunList::fetchRuns()
00065   throw(std::runtime_error)
00066 {
00067   fetchRuns(-1, -1);
00068 }
00069 
00070 void RunList::fetchRuns(int min_run, int max_run)
00071   throw(std::runtime_error)
00072 {
00073   fetchRuns(min_run, max_run, false, false);
00074 }
00075 
00076 void RunList::fetchRuns(int min_run, int max_run, bool withTriggers,
00077                         bool withGlobalTriggers)
00078   throw(std::runtime_error)
00079 {
00080 
00081   /*
00082     withTriggers and withGlobalTriggers selects those non empty runs.
00083     Possible combinations are
00084 
00085     withTriggers withGlobalTriggers select
00086     ------------ ------------------ ------------------------------
00087     false        false              all
00088     false        true               only runs with global triggers
00089     true         false              only runs with any trigger
00090     true         true               only runs with global triggers
00091    */
00092   this->checkConnection();
00093   int nruns=0;
00094 
00095   m_runTag.setConnection(m_env, m_conn);
00096   int tagID = m_runTag.fetchID();
00097   cout <<"tag id="<< tagID << endl;
00098   if (!tagID) { 
00099     return ;
00100   }
00101 
00102   int my_min_run=min_run-1;
00103   int my_max_run=max_run+1;
00104   try {
00105     Statement* stmt0 = m_conn->createStatement();
00106     string sql =  "SELECT count(iov_id) FROM run_iov "
00107       "WHERE tag_id = :tag_id ";
00108     if (min_run > 0) {
00109       // don't need to specify empty/non empty here. This is needed
00110       // just to allocate the memory for the vector
00111       sql += " and run_iov.run_num> :min_run and run_iov.run_num< :max_run ";
00112     }
00113     stmt0->setSQL(sql);
00114     stmt0->setInt(1, tagID);
00115     if (min_run > 0) {
00116       stmt0->setInt(2, my_min_run);
00117       stmt0->setInt(3, my_max_run);
00118     }
00119     
00120     ResultSet* rset0 = stmt0->executeQuery();
00121     if (rset0->next()) {
00122       nruns = rset0->getInt(1);
00123     }
00124     m_conn->terminateStatement(stmt0);
00125 
00126     cout <<"number of runs="<< nruns << endl;
00127     m_vec_runiov.reserve(nruns);
00128     
00129     Statement* stmt = m_conn->createStatement();
00130     sql = "SELECT DISTINCT i.iov_id, tag_id, run_num, run_start, run_end, " 
00131       "db_timestamp FROM run_iov i ";
00132     if ((withTriggers) || (withGlobalTriggers)) {
00133       sql += "join cms_ecal_cond.run_dat d on d.iov_id = i.iov_id " 
00134         "left join CMS_RUNINFO.RUNSESSION_PARAMETER G on " 
00135         "(i.run_num = G.RUNNUMBER and G.NAME = 'CMS.TRG:NumTriggers') "; 
00136     }
00137     sql +=  "WHERE tag_id = :tag_id ";
00138     if (min_run > 0) {
00139       sql += "and i.run_num> :min_run and i.run_num< :max_run ";
00140     }
00141     if (withGlobalTriggers) {
00142       sql += "and G.STRING_VALUE != '0' "; 
00143     } else if (withTriggers) {
00144       sql += "and (G.STRING_VALUE != '0' or num_events > 0) ";
00145     }
00146     sql += " order by run_num ";
00147     stmt->setSQL(sql);
00148     stmt->setInt(1, tagID);
00149     if (min_run > 0) {
00150       stmt->setInt(2, my_min_run);
00151       stmt->setInt(3, my_max_run);
00152     }
00153 
00154     DateHandler dh(m_env, m_conn);
00155     Tm runStart;
00156     Tm runEnd;
00157     Tm dbtime;
00158   
00159     ResultSet* rset = stmt->executeQuery();
00160     int i=0;
00161     while ((i<nruns) && (rset->next()))  {
00162       int iovID = rset->getInt(1);
00163       // int tagID = rset->getInt(2);
00164       int runNum = rset->getInt(3);
00165       Date startDate = rset->getDate(4);
00166       Date endDate = rset->getDate(5);
00167       Date dbDate = rset->getDate(6);
00168          
00169       runStart = dh.dateToTm( startDate );
00170       runEnd = dh.dateToTm( endDate );
00171       dbtime = dh.dateToTm( dbDate );
00172        
00173       RunIOV r ;
00174       r.setRunNumber(runNum);
00175       r.setRunStart(runStart);
00176       r.setRunEnd(runEnd);
00177       r.setDBInsertionTime(dbtime);
00178       r.setRunTag(m_runTag);
00179       r.setID(iovID);
00180       m_vec_runiov.push_back(r);
00181       
00182       i++;
00183     }
00184     m_vec_runiov.resize(i);
00185     m_conn->terminateStatement(stmt);
00186   } catch (SQLException &e) {
00187     throw(std::runtime_error("RunList::fetchRuns:  "+e.getMessage()));
00188   }
00189 }
00190 
00191 void RunList::fetchLastNRuns( int max_run, int n_runs  )
00192   throw(std::runtime_error)
00193 {
00194 
00195   // fetch the last n_runs that come just before max_run (including max_run)
00196 
00197   this->checkConnection();
00198 
00199 
00200   m_runTag.setConnection(m_env, m_conn);
00201   int tagID = m_runTag.fetchID();
00202   cout <<"tag id="<< tagID << endl;
00203   if (!tagID) { 
00204     return ;
00205   }
00206 
00207   int my_max_run=max_run+1;
00208   try {
00209 
00210     int nruns=n_runs+1;
00211     m_vec_runiov.reserve(nruns);
00212     
00213     Statement* stmt = m_conn->createStatement();
00214     stmt->setSQL("select iov_id, tag_id, run_num, run_start, run_end, DB_TIMESTAMP from "
00215                  " (SELECT * from RUN_IOV "
00216                  " WHERE tag_id = :tag_id "
00217                  " and run_num< :max_run "
00218                  " order by run_num DESC ) where rownum< :n_runs ORDER BY run_num ASC " );
00219     stmt->setInt(1, tagID);
00220     stmt->setInt(2, my_max_run);
00221     stmt->setInt(3, nruns);
00222 
00223     DateHandler dh(m_env, m_conn);
00224     Tm runStart;
00225     Tm runEnd;
00226     Tm dbtime;
00227   
00228     ResultSet* rset = stmt->executeQuery();
00229     int i=0;
00230     while (i<n_runs) {
00231       rset->next();
00232       int iovID = rset->getInt(1);
00233       // int tagID = rset->getInt(2);
00234        int runNum = rset->getInt(3);
00235        Date startDate = rset->getDate(4);
00236        Date endDate = rset->getDate(5);
00237        Date dbDate = rset->getDate(6);
00238          
00239        runStart = dh.dateToTm( startDate );
00240        runEnd = dh.dateToTm( endDate );
00241        dbtime = dh.dateToTm( dbDate );
00242        
00243        RunIOV r ;
00244        r.setRunNumber(runNum);
00245        r.setRunStart(runStart);
00246        r.setRunEnd(runEnd);
00247        r.setDBInsertionTime(dbtime);
00248        r.setRunTag(m_runTag);
00249        r.setID(iovID);
00250        m_vec_runiov.push_back(r);
00251       
00252       i++;
00253     }
00254 
00255     m_conn->terminateStatement(stmt);
00256   } catch (SQLException &e) {
00257     throw(std::runtime_error("RunList::fetchLastNRuns:  "+e.getMessage()));
00258   }
00259 
00260 
00261 }
00262 
00263 
00264 
00265 void RunList::fetchRunsByLocation (int min_run, int max_run, const LocationDef locDef )
00266   throw(std::runtime_error)
00267 {
00268 
00269   this->checkConnection();
00270   int nruns=0;
00271 
00272   int my_min_run=min_run-1;
00273   int my_max_run=max_run+1;
00274   try {
00275     Statement* stmt0 = m_conn->createStatement();
00276     stmt0->setSQL("SELECT count(iov_id) FROM run_iov r , run_tag t, location_def l "
00277                  " WHERE r.tag_id=t.tag_id and t.LOCATION_ID=l.def_id AND l.LOCATION= :1 " 
00278                   "  and r.run_num> :2 and r.run_num< :3 ");
00279     stmt0->setString(1,locDef.getLocation() );
00280     stmt0->setInt(2, my_min_run);
00281     stmt0->setInt(3, my_max_run);
00282   
00283     ResultSet* rset0 = stmt0->executeQuery();
00284     if (rset0->next()) {
00285       nruns = rset0->getInt(1);
00286     }
00287     m_conn->terminateStatement(stmt0);
00288 
00289     cout <<"number of runs="<< nruns << endl;
00290     
00291     m_vec_runiov.reserve(nruns);
00292     
00293     Statement* stmt = m_conn->createStatement();
00294     stmt->setSQL("SELECT  r.iov_id, r.tag_id, r.run_num, r.run_start, r.run_end, r.DB_TIMESTAMP , "
00295                  " t.gen_tag, rt.RUN_TYPE "
00296                  " FROM run_iov r , run_tag t, location_def l, run_type_def rt "
00297                  " WHERE r.tag_id=t.tag_id and t.LOCATION_ID=l.def_id and t.run_type_id=rt.DEF_ID "
00298                  " AND l.LOCATION= :1 "
00299                  " and r.run_num> :2 and r.run_num< :3 "
00300                  " order by run_num " );
00301     stmt->setString(1,locDef.getLocation() );
00302     stmt->setInt(2, my_min_run);
00303     stmt->setInt(3, my_max_run);
00304 
00305     DateHandler dh(m_env, m_conn);
00306     Tm runStart;
00307     Tm runEnd;
00308     Tm dbtime;
00309   
00310     ResultSet* rset = stmt->executeQuery();
00311     int i=0;
00312     while (i<nruns) {
00313       rset->next();
00314       int iovID = rset->getInt(1);
00315       //       int tagID = rset->getInt(2);
00316        int runNum = rset->getInt(3);
00317        Date startDate = rset->getDate(4);
00318        Date endDate = rset->getDate(5);
00319        Date dbDate = rset->getDate(6);
00320          
00321        runStart = dh.dateToTm( startDate );
00322        runEnd = dh.dateToTm( endDate );
00323        dbtime = dh.dateToTm( dbDate );
00324        
00325        RunTag atag;
00326        atag.setLocationDef(locDef);
00327        atag.setGeneralTag(rset->getString(7));
00328        RunTypeDef rundef;
00329        rundef.setRunType(rset->getString(8));
00330        atag.setRunTypeDef(rundef);
00331 
00332        RunIOV r ;
00333        r.setRunNumber(runNum);
00334        r.setRunStart(runStart);
00335        r.setRunEnd(runEnd);
00336        r.setDBInsertionTime(dbtime);
00337        r.setRunTag(atag);
00338        r.setID(iovID);
00339        m_vec_runiov.push_back(r);
00340       
00341       i++;
00342     }
00343    
00344 
00345     m_conn->terminateStatement(stmt);
00346 
00347   } catch (SQLException &e) {
00348     throw(std::runtime_error("RunList::fetchRunsByLocation:  "+e.getMessage()));
00349   }
00350 
00351 
00352 }
00353 
00354 void RunList::fetchGlobalRunsByLocation (int min_run, int max_run, const LocationDef locDef )
00355   throw(std::runtime_error)
00356 {
00357 
00358   this->checkConnection();
00359   int nruns=0;
00360 
00361   int my_min_run=min_run-1;
00362   int my_max_run=max_run+1;
00363   try {
00364     Statement* stmt0 = m_conn->createStatement();
00365     stmt0->setSQL("SELECT count(iov_id) FROM run_iov r , run_tag t, location_def l "
00366                  " WHERE r.tag_id=t.tag_id and t.LOCATION_ID=l.def_id AND l.LOCATION= :1 " 
00367                   "  and t.gen_tag='GLOBAL' "
00368                   "  and r.run_num> :2 and r.run_num< :3 ");
00369     stmt0->setString(1,locDef.getLocation() );
00370     stmt0->setInt(2, my_min_run);
00371     stmt0->setInt(3, my_max_run);
00372   
00373     ResultSet* rset0 = stmt0->executeQuery();
00374     if (rset0->next()) {
00375       nruns = rset0->getInt(1);
00376     }
00377     m_conn->terminateStatement(stmt0);
00378 
00379     cout <<"number of runs="<< nruns << endl;
00380     
00381     m_vec_runiov.reserve(nruns);
00382     
00383     Statement* stmt = m_conn->createStatement();
00384     stmt->setSQL("SELECT  r.iov_id, r.tag_id, r.run_num, r.run_start, r.run_end, r.DB_TIMESTAMP , "
00385                  " t.gen_tag, rt.RUN_TYPE "
00386                  " FROM run_iov r , run_tag t, location_def l, run_type_def rt "
00387                  " WHERE r.tag_id=t.tag_id and t.LOCATION_ID=l.def_id and t.run_type_id=rt.DEF_ID "
00388                  " AND l.LOCATION= :1 "
00389                  " and t.gen_tag='GLOBAL' "
00390                  " and r.run_num> :2 and r.run_num< :3 "
00391                  " order by run_num " );
00392     stmt->setString(1,locDef.getLocation() );
00393     stmt->setInt(2, my_min_run);
00394     stmt->setInt(3, my_max_run);
00395 
00396     DateHandler dh(m_env, m_conn);
00397     Tm runStart;
00398     Tm runEnd;
00399     Tm dbtime;
00400   
00401     ResultSet* rset = stmt->executeQuery();
00402     int i=0;
00403     while (i<nruns) {
00404       rset->next();
00405       int iovID = rset->getInt(1);
00406       //       int tagID = rset->getInt(2);
00407        int runNum = rset->getInt(3);
00408        Date startDate = rset->getDate(4);
00409        Date endDate = rset->getDate(5);
00410        Date dbDate = rset->getDate(6);
00411          
00412        runStart = dh.dateToTm( startDate );
00413        runEnd = dh.dateToTm( endDate );
00414        dbtime = dh.dateToTm( dbDate );
00415        
00416        RunTag atag;
00417        atag.setLocationDef(locDef);
00418        atag.setGeneralTag(rset->getString(7));
00419        RunTypeDef rundef;
00420        rundef.setRunType(rset->getString(8));
00421        atag.setRunTypeDef(rundef);
00422 
00423        RunIOV r ;
00424        r.setRunNumber(runNum);
00425        r.setRunStart(runStart);
00426        r.setRunEnd(runEnd);
00427        r.setDBInsertionTime(dbtime);
00428        r.setRunTag(atag);
00429        r.setID(iovID);
00430        m_vec_runiov.push_back(r);
00431       
00432       i++;
00433     }
00434    
00435 
00436     m_conn->terminateStatement(stmt);
00437 
00438   } catch (SQLException &e) {
00439     throw(std::runtime_error("RunList::fetchRunsByLocation:  "+e.getMessage()));
00440   }
00441 
00442 
00443 }