CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/OnlineDB/EcalCondDB/interface/EcalCondDBInterface.h

Go to the documentation of this file.
00001 /***********************************************/
00002 /* EcalCondDBInterface.h                       */
00003 /*                                             */
00004 /* $Id: EcalCondDBInterface.h,v 1.33 2011/02/07 10:23:33 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 fetchRunListByLocation(RunTag tag, int min_run, int max_run , const LocationDef locDef) throw(std::runtime_error);
00254   RunList fetchGlobalRunListByLocation(RunTag tag, int min_run, int max_run , const LocationDef locDef) throw(std::runtime_error);
00255   RunList fetchRunListLastNRuns(RunTag tag, int max_run, int n_runs) throw(std::runtime_error);
00256 
00261   DCSPTMTempList fetchDCSPTMTempList(EcalLogicID ecid)  throw(std::runtime_error);
00262   DCSPTMTempList fetchDCSPTMTempList(EcalLogicID ecid, Tm start, Tm end) throw(std::runtime_error);
00263 
00264   MonRunList fetchMonRunList(RunTag tag, MonRunTag monruntag) throw(std::runtime_error);
00265   MonRunList fetchMonRunList(RunTag tag, MonRunTag monruntag,int min_run, int max_run) throw(std::runtime_error);
00266   MonRunList fetchMonRunListLastNRuns(RunTag tag, MonRunTag monruntag, int max_run, int n_runs) throw(std::runtime_error);
00267 
00268 
00269 
00270 
00271   /*
00272    *   Insert a set of data at the given iov.  If the iov does not yet
00273    *   exist in the database they will be written.  Nothing is committed in the the event of
00274    *   an exception
00275    */
00276   // XXX TODO:  Split declaration and definition using a macro
00277   // XXX        Naive method causes linker errors...
00278   // XXX        See example FWCore/Framework/interface/eventSetupGetImplementation.h
00279 
00280   template<class DATT, class IOVT>
00281   void insertDataSet(const std::map< EcalLogicID, DATT >* data, IOVT* iov)
00282     throw(std::runtime_error)
00283   {
00284     try {
00285       iov->setConnection(env, conn);
00286       // if it has not yet been written then write 
00287       if(iov->getID()==0){
00288         std::cout<<"IOV was not set we retrieve it from DB"<<std::endl;
00289         iov->fetchID();
00290       } 
00291       if(iov->getID()==0){
00292         std::cout<<"IOV was not written we write it"<<std::endl;
00293         iov->writeDB();
00294       } 
00295       
00296       DATT dataIface;
00297       dataIface.setConnection(env, conn);
00298       dataIface.prepareWrite();
00299       
00300       const EcalLogicID* channel;
00301       const DATT* dataitem;
00302       typedef typename std::map< EcalLogicID, DATT >::const_iterator CI;
00303       for (CI p = data->begin(); p != data->end(); ++p) {
00304         channel = &(p->first);
00305         dataitem = &(p->second);
00306         dataIface.writeDB( channel, dataitem, iov);
00307       }
00308       conn->commit();
00309       dataIface.terminateWriteStatement();
00310     } catch (std::runtime_error &e) {
00311       conn->rollback();
00312       throw(e);
00313     } catch (...) {
00314       conn->rollback();
00315       throw(std::runtime_error("EcalCondDBInterface::insertDataSet:  Unknown exception caught"));
00316     }
00317   }
00318   
00319   //test for DB Array insertion
00320   template<class DATT, class IOVT>
00321   void insertDataArraySet(const std::map< EcalLogicID, DATT >* data, IOVT* iov)
00322     throw(std::runtime_error)
00323   {
00324     try {
00325       iov->setConnection(env, conn);
00326       if(iov->getID()==0){
00327         std::cout<<"IOV was not set we retrieve it from DB"<<std::endl;
00328         iov->fetchID();
00329       } 
00330       if(iov->getID()==0){
00331         std::cout<<"IOV was not written we write it"<<std::endl;
00332         iov->writeDB();
00333       } 
00334 
00335       std::cout<<"id="<<iov->getID()<<std::endl;
00336 
00337       DATT dataIface;
00338       dataIface.setConnection(env, conn);
00339       dataIface.prepareWrite();
00340       
00341       dataIface.writeArrayDB(data, iov);
00342       conn->commit();
00343       
00344       dataIface.terminateWriteStatement();
00345    
00346     } catch (std::runtime_error &e) {
00347       conn->rollback();
00348       throw(e);
00349     } catch (...) {
00350       conn->rollback();
00351       throw(std::runtime_error("EcalCondDBInterface::insertDataSet:  Unknown exception caught"));
00352     }
00353   }
00354 
00355  template<class DATT, class IOVT>
00356   void insertDataSetVector( std::vector<EcalLogicID> ecid, std::vector<IOVT> run_iov, std::vector<DATT> data )
00357     throw(std::runtime_error)
00358   {
00359    
00360     int nruns= run_iov.size();
00361    
00362     if(run_iov.size()!=ecid.size() &&ecid.size()!=data.size()){
00363       throw(std::runtime_error("EcalCondDBInterface::insertDataSetVector:  vector sizes are different.."));
00364     } 
00365 
00366 
00367     try {
00368       
00369       DATT dataIface;
00370       dataIface.setConnection(env, conn);
00371       dataIface.prepareWrite();
00372 
00373       for (int i=0; i<nruns; i++){
00374 
00375         run_iov[i].setConnection(env, conn);
00376         run_iov[i].writeDB();
00377       
00378         dataIface.writeDB( &ecid[i], &data[i], &run_iov[i]);
00379         
00380         conn->commit();
00381       }
00382     } catch (std::runtime_error &e) {
00383       conn->rollback();
00384       throw(e);
00385     } catch (...) {
00386       conn->rollback();
00387       throw(std::runtime_error("EcalCondDBInterface::insertDataSet:  Unknown exception caught"));
00388     }
00389   }
00390 
00391 
00392 
00393 
00394 
00395  // CONFIG DB methods 
00396 
00397  template<class ICONF >
00398    void insertConfigSet( ICONF* iconf)
00399    throw(std::runtime_error)
00400    {
00401      try {
00402        iconf->setConnection(env, conn);
00403        iconf->prepareWrite();
00404        // if it has not yet been written then write 
00405        iconf->writeDB();
00406        std::cout<< "iconf inserted with ID="<<iconf->getId()<<std::endl;
00407        conn->commit();
00408        iconf->terminateWriteStatement();
00409      } catch (std::runtime_error &e) {
00410        conn->rollback();
00411        throw(e);
00412      } catch (...) {
00413        conn->rollback();
00414        throw(std::runtime_error("EcalCondDBInterface::insertDataSet:  Unknown exception caught"));
00415      }
00416    }
00417 
00418   /*
00419    *  Fetch a config set
00420    */
00421   template<class ICONF>
00422   void fetchConfigSet( ICONF* iconf)
00423     throw(std::runtime_error)
00424   {
00425 
00426     iconf->clear();
00427     iconf->setConnection(env, conn);
00428     iconf->createReadStatement();
00429     iconf->fetchData(iconf);
00430     iconf->terminateReadStatement();
00431 
00432   }
00433 
00434   /*
00435    *  Fetch a config set
00436    */
00437   template<class ICONF>
00438   void fetchLastConfigSet( ICONF* iconf)
00439     throw(std::runtime_error)
00440   {
00441 
00442     iconf->clear();
00443     iconf->setConnection(env, conn);
00444     iconf->createReadStatement();
00445     iconf->fetchLastData(iconf);
00446     iconf->terminateReadStatement();
00447 
00448   }
00449 
00450 
00451 
00452   /*
00453    *  Insert a config data set 
00454    */
00455   template<class DATT, class ICONF>
00456   void insertConfigDataSet(const std::vector< DATT > data, ICONF* iconf)
00457     throw(std::runtime_error)
00458   {
00459     try {
00460       iconf->setConnection(env, conn);
00461       // if it has not yet been written then write 
00462       if(iconf->getId()==0){
00463         std::cout<<"EcalCondDBInterface>> config_id was not set we retrieve it from DB"<<std::endl;
00464         iconf->fetchID();
00465       } 
00466       if(iconf->getId()==0){
00467         std::cout<<"EcalCondDBInterface>> configuration info was not written we write it"<<std::endl;
00468         iconf->writeDB();
00469       } 
00470       
00471       DATT dataIface;
00472       dataIface.setConnection(env, conn);
00473       dataIface.prepareWrite();
00474       
00475       const DATT* dataitem;
00476 
00477       for (int p = 0; p != data->size(); ++p) {
00478         dataitem = data[p];
00479         dataIface.writeDB( dataitem, iconf);
00480       }
00481       conn->commit();
00482       dataIface.terminateWriteStatement();
00483     } catch (std::runtime_error &e) {
00484       conn->rollback();
00485       throw(e);
00486     } catch (...) {
00487       conn->rollback();
00488       throw(std::runtime_error("EcalCondDBInterface::insertConfigDataSet:  Unknown exception caught"));
00489     }
00490   }
00491   /*
00492    *  insert a config data set array fast
00493    */
00494   template<class DATT, class ICONF>
00495   void insertConfigDataArraySet(const std::vector< DATT > data, ICONF* iconf)
00496     throw(std::runtime_error)
00497   {
00498     try {
00499       iconf->setConnection(env, conn);
00500       // if it has not yet been written then write 
00501       if(iconf->getId()==0){
00502         std::cout<<"EcalCondDBInterface>> config_id was not set we retrieve it from DB"<<std::endl;
00503         iconf->fetchID();
00504       } 
00505       if(iconf->getId()==0){
00506         std::cout<<"EcalCondDBInterface>> configuration info was not written we write it"<<std::endl;
00507         iconf->writeDB();
00508       } 
00509       
00510       DATT dataIface;
00511       dataIface.setConnection(env, conn);
00512       dataIface.prepareWrite();
00513       
00514       dataIface.writeArrayDB(data, iconf);
00515       conn->commit();
00516       
00517       dataIface.terminateWriteStatement();
00518    
00519     } catch (std::runtime_error &e) {
00520       conn->rollback();
00521       throw(e);
00522     } catch (...) {
00523       conn->rollback();
00524       throw(std::runtime_error("EcalCondDBInterface::insertConfigDataArraySet:  Unknown exception caught"));
00525     }
00526   }
00527   /*
00528    *  Fetch a set of data based on an EXACT match of an iov
00529    */
00530   template<class DATT, class ICONF>
00531   void fetchConfigDataSet(std::vector< DATT >* fillMap, ICONF* iconf)
00532     throw(std::runtime_error)
00533   {
00534 
00535     DATT datiface;
00536     datiface.setConnection(env, conn);
00537     datiface.createReadStatement();
00538     datiface.setPrefetchRowCount(1024);
00539     datiface.fetchData( fillMap, iconf );
00540     datiface.terminateReadStatement();
00541 
00542   }
00543 
00544 
00545 
00546 
00547 
00548 
00549 
00550   /*
00551    *  Fetch a set of data based on an EXACT match of an iov
00552    */
00553   template<class DATT, class IOVT>
00554   void fetchDataSet(std::map< EcalLogicID, DATT >* fillMap, IOVT* iov)
00555     throw(std::runtime_error)
00556   {
00557     fillMap->clear();
00558 
00559     DATT datiface;
00560     datiface.setConnection(env, conn);
00561     datiface.createReadStatement();
00562     datiface.setPrefetchRowCount(1024);
00563     datiface.fetchData( fillMap, iov );
00564     datiface.terminateReadStatement();
00565 
00566   }
00567 
00568 
00569 
00570 
00571   /*                               
00572    *  Fetch a set of DCS data based on time stamp
00573    */
00574   template<class DATT>
00575   void fetchDCSDataSet(std::list< std::pair< Tm, std::map<  EcalLogicID, DATT > > >* fillMap, Tm t)
00576     throw(std::runtime_error)
00577   {
00578     fillMap->clear();
00579     
00580     DATT datiface;
00581     datiface.setConnection(env, conn);
00582     datiface.createReadStatement();
00583     datiface.setPrefetchRowCount(1024);
00584     datiface.fetchHistoricalData( fillMap, t );
00585     datiface.terminateReadStatement();
00586     
00587   }
00588   
00589   
00590 
00591 
00592 
00593   /*
00594    *  Fetch a set of data based on an EXACT match of an iov
00595    * with a specific mapping name 
00596    */
00597   template<class DATT, class IOVT>
00598   void fetchDataSetWithMap(std::map< EcalLogicID, DATT >* fillMap, IOVT* iov, std::string mapping_name )
00599     throw(std::runtime_error)
00600   {
00601     fillMap->clear();
00602 
00603     DATT datiface;
00604     datiface.setConnection(env, conn);
00605     datiface.createReadStatement();
00606     datiface.setPrefetchRowCount(1024);
00607     datiface.fetchData( fillMap, iov , mapping_name);
00608     datiface.terminateReadStatement();
00609 
00610   }
00611 
00612   /*
00613    *  Fetch dataset that is valid for the given RunTag and run.
00614    *  Also fills the given IOV object with the IOV associated with the data
00615    *  run is optional, by default is set to infinity
00616    *  Note:  ONLY works for Run*Dat objects
00617    *  TODO:  Make this function (or similar) work for all *Dat objects
00618    */
00619   template<class DATT, class IOVT>
00620   void fetchValidDataSet(std::map< EcalLogicID, DATT >* fillMap, 
00621                          IOVT* fillIOV, 
00622                          RunTag* tag, run_t run = (unsigned int)-1)
00623   throw(std::runtime_error)
00624   {
00625     fillMap->clear();
00626     DATT datiface;
00627     fillIOV->setConnection(env, conn);
00628     fillIOV->setByRecentData(datiface.getTable(), tag, run);
00629     datiface.setConnection(env, conn);
00630     datiface.createReadStatement();
00631     datiface.setPrefetchRowCount(1024);
00632     datiface.fetchData( fillMap, fillIOV );
00633     datiface.terminateReadStatement();
00634   }
00635 
00636   /*
00637    *  Fetch dataset that is valid for the given location and run.
00638    *  Also fills the given IOV object with the IOV associated with the data
00639    *  run is optional, by default is set to infinity
00640    *  Note:  ONLY works for Run*Dat objects
00641    *  TODO:  Make this function (or similar) work for all *Dat objects
00642    */
00643   template<class DATT, class IOVT>
00644   void fetchValidDataSet(std::map< EcalLogicID, DATT >* fillMap,
00645                          IOVT* fillIOV,
00646                          std::string location, run_t run = (unsigned int)-1)
00647   throw(std::runtime_error)
00648   {
00649     fillMap->clear();
00650     DATT datiface;
00651     fillIOV->setConnection(env, conn);
00652     fillIOV->setByRecentData(datiface.getTable(), location, run);
00653     datiface.setConnection(env, conn);
00654     datiface.createReadStatement();
00655     datiface.setPrefetchRowCount(1024);
00656     datiface.fetchData( fillMap, fillIOV );
00657     datiface.terminateReadStatement();
00658   }
00659 
00660 
00661 
00662 
00663 
00664   void dummy();
00665 
00666  private:
00667 
00668   /*********************\
00669   -  private variables  -
00670   \*********************/
00671 
00672   DateHandler* dh;
00673 
00674   EcalCondDBInterface();
00675   EcalCondDBInterface(const EcalCondDBInterface& copy);
00676 
00677 
00678 };
00679 
00680 #endif