CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/OnlineDB/EcalCondDB/interface/EcalCondDBInterface.h

Go to the documentation of this file.
00001 /***********************************************/
00002 /* EcalCondDBInterface.h                       */
00003 /*                                             */
00004 /* $Id: EcalCondDBInterface.h,v 1.34 2011/04/06 11:59:08 organtin Exp $                                        */
00005 /*                                             */
00006 /* Interface to the Ecal Conditions DB.        */
00007 /***********************************************/
00008 
00009 #ifndef ECALCONDDBINTERFACE_HH
00010 #define ECALCONDDBINTERFACE_HH
00011 
00012 #include <iostream>
00013 #include <string>
00014 #include <vector>
00015 #include <map>
00016 #include <stdexcept>
00017 #include "OnlineDB/Oracle/interface/Oracle.h"
00018 
00019 
00020 #include "OnlineDB/EcalCondDB/interface/EcalDBConnection.h"
00021 #include "OnlineDB/EcalCondDB/interface/Tm.h"
00022 #include "OnlineDB/EcalCondDB/interface/EcalLogicID.h"
00023 #include "OnlineDB/EcalCondDB/interface/IIOV.h"
00024 #include "OnlineDB/EcalCondDB/interface/RunIOV.h"
00025 #include "OnlineDB/EcalCondDB/interface/MonRunIOV.h"
00026 #include "OnlineDB/EcalCondDB/interface/DCUIOV.h"
00027 #include "OnlineDB/EcalCondDB/interface/CaliIOV.h"
00028 #include "OnlineDB/EcalCondDB/interface/RunList.h"
00029 #include "OnlineDB/EcalCondDB/interface/MonRunList.h"
00030 #include "OnlineDB/EcalCondDB/interface/MonRunTag.h"
00031 #include "OnlineDB/EcalCondDB/interface/DCSPTMTempList.h"
00032 #include "OnlineDB/EcalCondDB/interface/all_fe_config_types.h"
00033 #include "OnlineDB/EcalCondDB/interface/all_lmf_types.h"
00034 #include "OnlineDB/EcalCondDB/interface/all_od_types.h"
00035 
00036 class EcalCondDBInterface : public EcalDBConnection {
00037  public:
00038 
00039   /******************\
00040   -  public methods  -
00041   \******************/
00042 
00052   EcalCondDBInterface( std::string host,
00053                        std::string sid,
00054                        std::string user,
00055                        std::string pass,
00056                        int port=1521 )
00057     : EcalDBConnection( host, sid, user, pass, port )
00058     {
00059       // call the parent constructor
00060 
00061       // create a DateHandler
00062       dh = new DateHandler(env, conn);
00063     }
00064 
00065 
00066 
00074   EcalCondDBInterface( std::string sid,
00075                        std::string user,
00076                        std::string pass )
00077     : EcalDBConnection( sid, user, pass )
00078     {
00079       // call the parent constructor
00080 
00081       // create a DateHandler
00082       dh = new DateHandler(env, conn);
00083     }
00084 
00085 
00089   virtual ~EcalCondDBInterface()
00090     throw(std::runtime_error)
00091     {
00092       // call the parent destructor
00093       
00094       // destroy the DateHandler
00095       delete(dh);
00096     }
00097 
00098 
00099 
00103   inline DateHandler* getDateHandler()
00104     {
00105       return dh;
00106     }
00107 
00108   
00117   EcalLogicID getEcalLogicID( std::string name,
00118                               int id1 = EcalLogicID::NULLID,
00119                               int id2 = EcalLogicID::NULLID,
00120                               int id3 = EcalLogicID::NULLID,
00121                               std::string mapsTo = "" )
00122     throw(std::runtime_error);
00123 
00130   EcalLogicID getEcalLogicID( int logicID )
00131     throw(std::runtime_error);
00132 
00142   std::vector<EcalLogicID> getEcalLogicIDSet( std::string name,
00143                                               int fromId1 = EcalLogicID::NULLID, int toId1 = EcalLogicID::NULLID,
00144                                               int fromId2 = EcalLogicID::NULLID, int toId2 = EcalLogicID::NULLID,
00145                                               int fromId3 = EcalLogicID::NULLID, int toId3 = EcalLogicID::NULLID,
00146                                               std::string mapsTo = "" )
00147     throw(std::runtime_error);
00148 
00149   std::map<int, int> getEcalLogicID2LmrMap();
00150   std::vector<EcalLogicID> getEcalLogicIDSetOrdered( std::string name,
00151                                                      int fromId1, int toId1,
00152                                                      int fromId2 = EcalLogicID::NULLID, int toId2 = EcalLogicID::NULLID,
00153                                                      int fromId3 = EcalLogicID::NULLID, int toId3 = EcalLogicID::NULLID,
00154                                                      std::string mapsTo = "", int orderedBy= EcalLogicID::NULLID ) throw(std::runtime_error);
00155 
00156 
00160   void insertRunIOV(RunIOV* iov)
00161     throw(std::runtime_error);
00162   // updates the end time of an iov
00163   void updateRunIOV(RunIOV* iov)
00164     throw(std::runtime_error);
00165   void updateRunIOVStartTime(RunIOV* iov)
00166     throw(std::runtime_error);
00167   void updateRunIOVEndTime(RunIOV* iov)
00168     throw(std::runtime_error);
00169 
00170   void updateRunConfig(ODRunConfigInfo* od) throw(std::runtime_error);
00171 
00172   void insertLmfIOV(LMFIOV* iov)
00173     throw(std::runtime_error);
00174   void insertLmfLmrSubIOV(LMFLmrSubIOV* iov)
00175     throw(std::runtime_error);
00176   void insertLmfSeq(LMFSeqDat* iov)
00177     throw(std::runtime_error);
00178   void insertLmfRunIOV(LMFRunIOV* iov)
00179     throw(std::runtime_error);
00180   void insertLmfDat(LMFDat* dat)
00181     throw(std::runtime_error);
00182   void insertLmfDat(std::list<LMFDat*> dat)
00183     throw(std::runtime_error);
00184 
00188   RunIOV fetchRunIOV(RunTag* tag, run_t run)
00189     throw(std::runtime_error);
00190 
00191 
00192 
00198   RunIOV fetchRunIOV(std::string location, run_t run)
00199     throw(std::runtime_error);
00200 
00201 
00202 
00206   void insertMonRunIOV(MonRunIOV* iov)
00207     throw(std::runtime_error);
00208 
00209 
00213   void insertDCUIOV(DCUIOV* iov)
00214     throw(std::runtime_error);
00215 
00216 
00220   MonRunIOV fetchMonRunIOV(RunTag* runtag, MonRunTag* montag, run_t run, subrun_t monrun)
00221     throw(std::runtime_error);
00222 
00223 
00227   DCUIOV fetchDCUIOV(DCUTag* tag, Tm evenTm)
00228     throw(std::runtime_error);
00229 
00230 
00231 
00235   LMFRunIOV fetchLMFRunIOV(RunTag* runtag, LMFRunTag* lmftag, run_t run, subrun_t lmfrun)
00236     throw(std::runtime_error);
00237   bool fetchLMFRunIOV(const LMFSeqDat&, LMFRunIOV&, int lmr, int type, 
00238                       int color) const;
00239   RunIOV fetchLMFLastRun() const; 
00240 
00244   CaliIOV fetchCaliIOV(CaliTag* tag, Tm evenTm)
00245     throw(std::runtime_error);
00246 
00247 
00251   RunList fetchRunList(RunTag tag) throw(std::runtime_error);
00252   RunList fetchRunList(RunTag tag, int min_run, int max_run) throw(std::runtime_error);
00253   RunList fetchNonEmptyRunList(RunTag tag, int min_run, int max_run) throw(std::runtime_error);
00254   RunList fetchNonEmptyGlobalRunList(RunTag tag, int min_run, int max_run) throw(std::runtime_error);
00255   RunList fetchRunListByLocation(RunTag tag, int min_run, int max_run , const LocationDef locDef) throw(std::runtime_error);
00256   RunList fetchGlobalRunListByLocation(RunTag tag, int min_run, int max_run , const LocationDef locDef) throw(std::runtime_error);
00257   RunList fetchRunListLastNRuns(RunTag tag, int max_run, int n_runs) throw(std::runtime_error);
00258 
00263   DCSPTMTempList fetchDCSPTMTempList(EcalLogicID ecid)  throw(std::runtime_error);
00264   DCSPTMTempList fetchDCSPTMTempList(EcalLogicID ecid, Tm start, Tm end) throw(std::runtime_error);
00265 
00266   MonRunList fetchMonRunList(RunTag tag, MonRunTag monruntag) throw(std::runtime_error);
00267   MonRunList fetchMonRunList(RunTag tag, MonRunTag monruntag,int min_run, int max_run) throw(std::runtime_error);
00268   MonRunList fetchMonRunListLastNRuns(RunTag tag, MonRunTag monruntag, int max_run, int n_runs) throw(std::runtime_error);
00269 
00270 
00271 
00272 
00273   /*
00274    *   Insert a set of data at the given iov.  If the iov does not yet
00275    *   exist in the database they will be written.  Nothing is committed in the the event of
00276    *   an exception
00277    */
00278   // XXX TODO:  Split declaration and definition using a macro
00279   // XXX        Naive method causes linker errors...
00280   // XXX        See example FWCore/Framework/interface/eventSetupGetImplementation.h
00281 
00282   template<class DATT, class IOVT>
00283   void insertDataSet(const std::map< EcalLogicID, DATT >* data, IOVT* iov)
00284     throw(std::runtime_error)
00285   {
00286     try {
00287       iov->setConnection(env, conn);
00288       // if it has not yet been written then write 
00289       if(iov->getID()==0){
00290         std::cout<<"IOV was not set we retrieve it from DB"<<std::endl;
00291         iov->fetchID();
00292       } 
00293       if(iov->getID()==0){
00294         std::cout<<"IOV was not written we write it"<<std::endl;
00295         iov->writeDB();
00296       } 
00297       
00298       DATT dataIface;
00299       dataIface.setConnection(env, conn);
00300       dataIface.prepareWrite();
00301       
00302       const EcalLogicID* channel;
00303       const DATT* dataitem;
00304       typedef typename std::map< EcalLogicID, DATT >::const_iterator CI;
00305       for (CI p = data->begin(); p != data->end(); ++p) {
00306         channel = &(p->first);
00307         dataitem = &(p->second);
00308         dataIface.writeDB( channel, dataitem, iov);
00309       }
00310       conn->commit();
00311       dataIface.terminateWriteStatement();
00312     } catch (std::runtime_error &e) {
00313       conn->rollback();
00314       throw(e);
00315     } catch (...) {
00316       conn->rollback();
00317       throw(std::runtime_error("EcalCondDBInterface::insertDataSet:  Unknown exception caught"));
00318     }
00319   }
00320   
00321   //test for DB Array insertion
00322   template<class DATT, class IOVT>
00323   void insertDataArraySet(const std::map< EcalLogicID, DATT >* data, IOVT* iov)
00324     throw(std::runtime_error)
00325   {
00326     try {
00327       iov->setConnection(env, conn);
00328       if(iov->getID()==0){
00329         std::cout<<"IOV was not set we retrieve it from DB"<<std::endl;
00330         iov->fetchID();
00331       } 
00332       if(iov->getID()==0){
00333         std::cout<<"IOV was not written we write it"<<std::endl;
00334         iov->writeDB();
00335       } 
00336 
00337       std::cout<<"id="<<iov->getID()<<std::endl;
00338 
00339       DATT dataIface;
00340       dataIface.setConnection(env, conn);
00341       dataIface.prepareWrite();
00342       
00343       dataIface.writeArrayDB(data, iov);
00344       conn->commit();
00345       
00346       dataIface.terminateWriteStatement();
00347    
00348     } catch (std::runtime_error &e) {
00349       conn->rollback();
00350       throw(e);
00351     } catch (...) {
00352       conn->rollback();
00353       throw(std::runtime_error("EcalCondDBInterface::insertDataSet:  Unknown exception caught"));
00354     }
00355   }
00356 
00357  template<class DATT, class IOVT>
00358   void insertDataSetVector( std::vector<EcalLogicID> ecid, std::vector<IOVT> run_iov, std::vector<DATT> data )
00359     throw(std::runtime_error)
00360   {
00361    
00362     int nruns= run_iov.size();
00363    
00364     if(run_iov.size()!=ecid.size() &&ecid.size()!=data.size()){
00365       throw(std::runtime_error("EcalCondDBInterface::insertDataSetVector:  vector sizes are different.."));
00366     } 
00367 
00368 
00369     try {
00370       
00371       DATT dataIface;
00372       dataIface.setConnection(env, conn);
00373       dataIface.prepareWrite();
00374 
00375       for (int i=0; i<nruns; i++){
00376 
00377         run_iov[i].setConnection(env, conn);
00378         run_iov[i].writeDB();
00379       
00380         dataIface.writeDB( &ecid[i], &data[i], &run_iov[i]);
00381         
00382         conn->commit();
00383       }
00384     } catch (std::runtime_error &e) {
00385       conn->rollback();
00386       throw(e);
00387     } catch (...) {
00388       conn->rollback();
00389       throw(std::runtime_error("EcalCondDBInterface::insertDataSet:  Unknown exception caught"));
00390     }
00391   }
00392 
00393 
00394 
00395 
00396 
00397  // CONFIG DB methods 
00398 
00399  template<class ICONF >
00400    void insertConfigSet( ICONF* iconf)
00401    throw(std::runtime_error)
00402    {
00403      try {
00404        iconf->setConnection(env, conn);
00405        iconf->prepareWrite();
00406        // if it has not yet been written then write 
00407        iconf->writeDB();
00408        std::cout<< "iconf inserted with ID="<<iconf->getId()<<std::endl;
00409        conn->commit();
00410        iconf->terminateWriteStatement();
00411      } catch (std::runtime_error &e) {
00412        conn->rollback();
00413        throw(e);
00414      } catch (...) {
00415        conn->rollback();
00416        throw(std::runtime_error("EcalCondDBInterface::insertDataSet:  Unknown exception caught"));
00417      }
00418    }
00419 
00420   /*
00421    *  Fetch a config set
00422    */
00423   template<class ICONF>
00424   void fetchConfigSet( ICONF* iconf)
00425     throw(std::runtime_error)
00426   {
00427 
00428     iconf->clear();
00429     iconf->setConnection(env, conn);
00430     iconf->createReadStatement();
00431     iconf->fetchData(iconf);
00432     iconf->terminateReadStatement();
00433 
00434   }
00435 
00436   /*
00437    *  Fetch a config set
00438    */
00439   template<class ICONF>
00440   void fetchLastConfigSet( ICONF* iconf)
00441     throw(std::runtime_error)
00442   {
00443 
00444     iconf->clear();
00445     iconf->setConnection(env, conn);
00446     iconf->createReadStatement();
00447     iconf->fetchLastData(iconf);
00448     iconf->terminateReadStatement();
00449 
00450   }
00451 
00452 
00453 
00454   /*
00455    *  Insert a config data set 
00456    */
00457   template<class DATT, class ICONF>
00458   void insertConfigDataSet(const std::vector< DATT > data, ICONF* iconf)
00459     throw(std::runtime_error)
00460   {
00461     try {
00462       iconf->setConnection(env, conn);
00463       // if it has not yet been written then write 
00464       if(iconf->getId()==0){
00465         std::cout<<"EcalCondDBInterface>> config_id was not set we retrieve it from DB"<<std::endl;
00466         iconf->fetchID();
00467       } 
00468       if(iconf->getId()==0){
00469         std::cout<<"EcalCondDBInterface>> configuration info was not written we write it"<<std::endl;
00470         iconf->writeDB();
00471       } 
00472       
00473       DATT dataIface;
00474       dataIface.setConnection(env, conn);
00475       dataIface.prepareWrite();
00476       
00477       const DATT* dataitem;
00478 
00479       for (int p = 0; p != data->size(); ++p) {
00480         dataitem = data[p];
00481         dataIface.writeDB( dataitem, iconf);
00482       }
00483       conn->commit();
00484       dataIface.terminateWriteStatement();
00485     } catch (std::runtime_error &e) {
00486       conn->rollback();
00487       throw(e);
00488     } catch (...) {
00489       conn->rollback();
00490       throw(std::runtime_error("EcalCondDBInterface::insertConfigDataSet:  Unknown exception caught"));
00491     }
00492   }
00493   /*
00494    *  insert a config data set array fast
00495    */
00496   template<class DATT, class ICONF>
00497   void insertConfigDataArraySet(const std::vector< DATT > data, ICONF* iconf)
00498     throw(std::runtime_error)
00499   {
00500     try {
00501       iconf->setConnection(env, conn);
00502       // if it has not yet been written then write 
00503       if(iconf->getId()==0){
00504         std::cout<<"EcalCondDBInterface>> config_id was not set we retrieve it from DB"<<std::endl;
00505         iconf->fetchID();
00506       } 
00507       if(iconf->getId()==0){
00508         std::cout<<"EcalCondDBInterface>> configuration info was not written we write it"<<std::endl;
00509         iconf->writeDB();
00510       } 
00511       
00512       DATT dataIface;
00513       dataIface.setConnection(env, conn);
00514       dataIface.prepareWrite();
00515       
00516       dataIface.writeArrayDB(data, iconf);
00517       conn->commit();
00518       
00519       dataIface.terminateWriteStatement();
00520    
00521     } catch (std::runtime_error &e) {
00522       conn->rollback();
00523       throw(e);
00524     } catch (...) {
00525       conn->rollback();
00526       throw(std::runtime_error("EcalCondDBInterface::insertConfigDataArraySet:  Unknown exception caught"));
00527     }
00528   }
00529   /*
00530    *  Fetch a set of data based on an EXACT match of an iov
00531    */
00532   template<class DATT, class ICONF>
00533   void fetchConfigDataSet(std::vector< DATT >* fillMap, ICONF* iconf)
00534     throw(std::runtime_error)
00535   {
00536 
00537     DATT datiface;
00538     datiface.setConnection(env, conn);
00539     datiface.createReadStatement();
00540     datiface.setPrefetchRowCount(1024);
00541     datiface.fetchData( fillMap, iconf );
00542     datiface.terminateReadStatement();
00543 
00544   }
00545 
00546 
00547 
00548 
00549 
00550 
00551 
00552   /*
00553    *  Fetch a set of data based on an EXACT match of an iov
00554    */
00555   template<class DATT, class IOVT>
00556   void fetchDataSet(std::map< EcalLogicID, DATT >* fillMap, IOVT* iov)
00557     throw(std::runtime_error)
00558   {
00559     fillMap->clear();
00560 
00561     DATT datiface;
00562     datiface.setConnection(env, conn);
00563     datiface.createReadStatement();
00564     datiface.setPrefetchRowCount(1024);
00565     datiface.fetchData( fillMap, iov );
00566     datiface.terminateReadStatement();
00567 
00568   }
00569 
00570 
00571 
00572 
00573   /*                               
00574    *  Fetch a set of DCS data based on time stamp
00575    */
00576   template<class DATT>
00577   void fetchDCSDataSet(std::list< std::pair< Tm, std::map<  EcalLogicID, DATT > > >* fillMap, Tm t)
00578     throw(std::runtime_error)
00579   {
00580     fillMap->clear();
00581     
00582     DATT datiface;
00583     datiface.setConnection(env, conn);
00584     datiface.createReadStatement();
00585     datiface.setPrefetchRowCount(1024);
00586     datiface.fetchHistoricalData( fillMap, t );
00587     datiface.terminateReadStatement();
00588     
00589   }
00590   
00591   
00592 
00593 
00594 
00595   /*
00596    *  Fetch a set of data based on an EXACT match of an iov
00597    * with a specific mapping name 
00598    */
00599   template<class DATT, class IOVT>
00600   void fetchDataSetWithMap(std::map< EcalLogicID, DATT >* fillMap, IOVT* iov, std::string mapping_name )
00601     throw(std::runtime_error)
00602   {
00603     fillMap->clear();
00604 
00605     DATT datiface;
00606     datiface.setConnection(env, conn);
00607     datiface.createReadStatement();
00608     datiface.setPrefetchRowCount(1024);
00609     datiface.fetchData( fillMap, iov , mapping_name);
00610     datiface.terminateReadStatement();
00611 
00612   }
00613 
00614   /*
00615    *  Fetch dataset that is valid for the given RunTag and run.
00616    *  Also fills the given IOV object with the IOV associated with the data
00617    *  run is optional, by default is set to infinity
00618    *  Note:  ONLY works for Run*Dat objects
00619    *  TODO:  Make this function (or similar) work for all *Dat objects
00620    */
00621   template<class DATT, class IOVT>
00622   void fetchValidDataSet(std::map< EcalLogicID, DATT >* fillMap, 
00623                          IOVT* fillIOV, 
00624                          RunTag* tag, run_t run = (unsigned int)-1)
00625   throw(std::runtime_error)
00626   {
00627     fillMap->clear();
00628     DATT datiface;
00629     fillIOV->setConnection(env, conn);
00630     fillIOV->setByRecentData(datiface.getTable(), tag, run);
00631     datiface.setConnection(env, conn);
00632     datiface.createReadStatement();
00633     datiface.setPrefetchRowCount(1024);
00634     datiface.fetchData( fillMap, fillIOV );
00635     datiface.terminateReadStatement();
00636   }
00637 
00638   /*
00639    *  Fetch dataset that is valid for the given location and run.
00640    *  Also fills the given IOV object with the IOV associated with the data
00641    *  run is optional, by default is set to infinity
00642    *  Note:  ONLY works for Run*Dat objects
00643    *  TODO:  Make this function (or similar) work for all *Dat objects
00644    */
00645   template<class DATT, class IOVT>
00646   void fetchValidDataSet(std::map< EcalLogicID, DATT >* fillMap,
00647                          IOVT* fillIOV,
00648                          std::string location, run_t run = (unsigned int)-1)
00649   throw(std::runtime_error)
00650   {
00651     fillMap->clear();
00652     DATT datiface;
00653     fillIOV->setConnection(env, conn);
00654     fillIOV->setByRecentData(datiface.getTable(), location, run);
00655     datiface.setConnection(env, conn);
00656     datiface.createReadStatement();
00657     datiface.setPrefetchRowCount(1024);
00658     datiface.fetchData( fillMap, fillIOV );
00659     datiface.terminateReadStatement();
00660   }
00661 
00662 
00663 
00664 
00665 
00666   void dummy();
00667 
00668  private:
00669 
00670   /*********************\
00671   -  private variables  -
00672   \*********************/
00673 
00674   DateHandler* dh;
00675 
00676   EcalCondDBInterface();
00677   EcalCondDBInterface(const EcalCondDBInterface& copy);
00678 
00679 
00680 };
00681 
00682 #endif