CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/CondCore/Utilities/src/CondPyInterface.cc

Go to the documentation of this file.
00001 #include "CondCore/Utilities/interface/CondPyInterface.h"
00002 
00003 #include <exception>
00004 #include <iostream>
00005 #include <iomanip>
00006 #include <fstream>
00007 #include <string>
00008 #include <vector>
00009 #include "FWCore/Utilities/interface/Exception.h"
00010 
00011 #include "FWCore/PluginManager/interface/ProblemTracker.h"
00012 #include "FWCore/Utilities/interface/Presence.h"
00013 #include "FWCore/PluginManager/interface/PresenceFactory.h"
00014 #include "FWCore/ServiceRegistry/interface/Service.h"
00015 
00016 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00017 
00018 
00019 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00020 #include "FWCore/PluginManager/interface/PluginManager.h"
00021 #include "FWCore/PluginManager/interface/standard.h"
00022 //#include "FWCore/PluginManager/interface/PluginCapabilities.h"
00023 #include "FWCore/PluginManager/interface/PluginFactory.h"
00024 
00025 #include "CondCore/DBCommon/interface/DbTransaction.h"
00026 #include "CondCore/DBCommon/interface/Exception.h"
00027 #include "CondCore/DBCommon/interface/FipProtocolParser.h"
00028 #include "CondCore/DBCommon/interface/ClassID.h"
00029 #include "CondCore/MetaDataService/interface/MetaData.h"
00030 #include "CondCore/DBCommon/interface/Logger.h"
00031 #include "CondCore/TagCollection/interface/TagCollectionRetriever.h"
00032 
00033 #include <iterator>
00034 #include <iostream>
00035 #include <sstream>
00036 
00037 
00038 namespace cond {
00039   
00040   static void topinit(){
00041     if(!edmplugin::PluginManager::isAvailable())
00042       edmplugin::PluginManager::configure(edmplugin::standard::config());
00043     return;
00044   }
00045 
00046   namespace impl {
00047     struct FWMagic {
00048       // A.  Instantiate a plug-in manager first.
00049       edm::AssertHandler ah;
00050       boost::shared_ptr<edm::ServiceRegistry::Operate> operate;
00051     };
00052   }
00053 
00054   FWIncantation::~FWIncantation(){}
00055   
00056 
00057   FWIncantation::FWIncantation() : magic(new impl::FWMagic) {
00058     topinit();
00059     // B.  Load the message service plug-in.  Forget this and bad things happen!
00060     //     In particular, the job hangs as soon as the output buffer fills up.
00061     //     That's because, without the message service, there is no mechanism for
00062     //     emptying the buffers.
00063     boost::shared_ptr<edm::Presence> theMessageServicePresence;
00064     theMessageServicePresence = boost::shared_ptr<edm::Presence>(edm::PresenceFactory::get()->
00065                                                                  makePresence("MessageServicePresence").release());
00066     
00067     // C.  Manufacture a configuration and establish it.
00068 
00069     /*
00070     std::string config =
00071       "process x = {"
00072       "service = MessageLogger {"
00073       "untracked vstring destinations = {'infos.mlog','warnings.mlog'}"
00074       "untracked PSet infos = {"
00075       "untracked string threshold = 'INFO'"
00076       "untracked PSet default = {untracked int32 limit = 1000000}"
00077       "untracked PSet FwkJob = {untracked int32 limit = 0}"
00078       "}"
00079       "untracked PSet warnings = {"
00080       "untracked string threshold = 'WARNING'"
00081       "untracked PSet default = {untracked int32 limit = 1000000}"
00082       "}"
00083       "untracked vstring fwkJobReports = {'FrameworkJobReport.xml'}"
00084       "untracked vstring categories = {'FwkJob'}"
00085       "untracked PSet FrameworkJobReport.xml = {"
00086       "untracked PSet default = {untracked int32 limit = 0}"
00087       "untracked PSet FwkJob = {untracked int32 limit = 10000000}"
00088       "}"
00089       "}"
00090       "service = JobReportService{}"
00091       "service = SiteLocalConfigService{}"
00092       "}";
00093     */
00094     /*
00095     std::string config =
00096       "import FWCore.ParameterSet.Config as cms\n"
00097       "process = cms.Process('x')\n"
00098       "JobReportService = cms.Service('JobReportService')\n"
00099       "SiteLocalConfigService = cms.Service('SiteLocalConfigService')\n"
00100       ;
00101     */
00102     
00103     boost::shared_ptr<std::vector<edm::ParameterSet> > psets(new std::vector<edm::ParameterSet>);
00104     edm::ParameterSet pSet;
00105     pSet.addParameter("@service_type",std::string("SiteLocalConfigService"));
00106     psets->push_back(pSet);
00107     
00108     // D.  Create the services.
00109     edm::ServiceToken tempToken(edm::ServiceRegistry::createSet(*psets.get()));
00110     
00111     // E.  Make the services available.
00112     magic->operate.reset(new edm::ServiceRegistry::Operate(tempToken));
00113     
00114   }
00115   
00116   //------------------------------------------------------------
00117 
00118 
00119   CondDB::CondDB() : me(){
00120     //topinit();    
00121   }
00122   CondDB::CondDB(cond::DbSession& session, boost::shared_ptr<cond::Logger> ilog) :
00123     me(session), logger(ilog) {
00124     //topinit();
00125   }
00126 
00127   // move ownership....
00128   CondDB::CondDB(const CondDB & other) : me(other.me), logger(other.logger) {
00129   }
00130 
00131   CondDB & CondDB::operator=(const CondDB & other) {
00132     if (this==&other) return *this; // unless =0 this is an error condition!
00133     me = other.me;
00134     logger = other.logger;
00135     return *this;
00136   }
00137 
00138   CondDB::~CondDB() {
00139   }
00140   
00141 
00142   std::string CondDB::allTags() const {
00143     std::ostringstream ss;
00144 
00145     cond::MetaData metadata_svc(me);
00146     std::vector<std::string> alltags;
00147     metadata_svc.listAllTags(alltags);
00148     
00149     std::copy (alltags.begin(),
00150                alltags.end(),
00151                std::ostream_iterator<std::string>(ss," ")
00152                );
00153     return ss.str();
00154   }
00155 
00156   std::string CondDB::iovToken(std::string const & tag) const {
00157     cond::MetaData metadata_svc(me);
00158     std::string token=metadata_svc.getToken(tag);
00159     return token;
00160   }
00161   
00162   // fix commit problem....
00163   IOVProxy CondDB::iov(std::string const & tag) const {
00164     return IOVProxy(me,iovToken(tag));
00165   }
00166   
00167   IOVProxy CondDB::iovWithLib(std::string const & tag) const {
00168     return IOVProxy(me,iovToken(tag));
00169   }
00170 
00171   IOVElementProxy CondDB::payLoad(std::string const & token) const {
00173     return IOVElementProxy(0,0,token);
00174   }
00175 
00176 
00177   cond::LogDBEntry CondDB::lastLogEntry(std::string const & tag) const {
00178     cond::LogDBEntry entry;
00179     if (logger)
00180       logger->LookupLastEntryByTag(tag,entry,false);
00181     return entry;
00182   }
00183 
00184   cond::LogDBEntry CondDB::lastLogEntryOK(std::string const & tag) const{
00185     cond::LogDBEntry entry;
00186     if (logger)
00187       logger->LookupLastEntryByTag(tag,entry,true);
00188     return entry;
00189   }
00190 
00191   void CondDB::startTransaction() const {
00192     me.transaction().start();
00193   }
00194   void CondDB::startReadOnlyTransaction() const {
00195     me.transaction().start(true);
00196   }
00197   void CondDB::commitTransaction() const {
00198     me.transaction().commit();
00199   }
00200 
00201   void CondDB::closeSession() const {
00202     //FIXME: does the session disconnection fix the socket failure in FroNTier (bug #84265)?
00203     me.close();
00204   }
00205   
00206   RDBMS::RDBMS() : connection(new DbConnection) {
00207     //topinit();
00208     connection->configure( cond::CmsDefaults );
00209   }
00210   RDBMS::~RDBMS() {}
00211 
00212   RDBMS::RDBMS(std::string const & authPath,  bool debug) : connection(new DbConnection) {
00213     //topinit();
00214     connection->configuration().setAuthenticationPath(authPath);
00215     if (debug) 
00216       connection->configuration().setMessageLevel( coral::Debug );
00217     else
00218       connection->configuration().setMessageLevel( coral::Error );
00219     connection->configuration().setPoolAutomaticCleanUp( false );
00220     connection->configuration().setConnectionTimeOut(0);
00221 
00222     connection->configure();
00223   }
00224   
00225   RDBMS::RDBMS(std::string const & user,std::string const & pass) : connection(new DbConnection) {
00226     //topinit();
00227     std::string userenv(std::string("CORAL_AUTH_USER=")+user);
00228     std::string passenv(std::string("CORAL_AUTH_PASSWORD=")+pass);
00229     ::putenv(const_cast<char*>(userenv.c_str()));
00230     ::putenv(const_cast<char*>(passenv.c_str()));
00231     connection->configuration().setMessageLevel( coral::Error );
00232     connection->configuration().setPoolAutomaticCleanUp( false );
00233     connection->configuration().setConnectionTimeOut(0);
00234     connection->configure();
00235   }
00236 
00237   void RDBMS::setLogger(std::string const & connstr) {
00238     DbSession loggerSession = connection->createSession();
00239     logger.reset(new cond::Logger(loggerSession));
00240     logger->connect( connstr, true );
00241   }
00242 
00243 
00244   
00245   CondDB RDBMS::getDB(std::string const & db) {
00246     DbSession dbSession = connection->createSession();
00247     dbSession.open( db );
00248     return CondDB(dbSession,logger);
00249   }
00250 
00251   CondDB RDBMS::getReadOnlyDB(std::string const & db) {
00252     DbSession dbSession = connection->createSession();
00253     dbSession.open( db, true );
00254     return CondDB(dbSession,logger);
00255   }
00256   
00257   GlobalTag const &  RDBMS::globalTag(std::string const & connstr, 
00258                                       std::string const & gname,
00259                                       std::string const & prefix, 
00260                                       std::string const & postfix) const {
00261     DbSession session = connection->createSession();
00262     session.open( connstr, true );
00263     session.transaction().start( true );
00264     TagCollectionRetriever gtr(session, prefix,postfix);
00265     const_cast<GlobalTag&>(m_globalTag).clear();
00266     gtr.getTagCollection(gname,const_cast<GlobalTag&>(m_globalTag));
00267     session.transaction().commit();
00268     return m_globalTag;  
00269   }
00270 
00271 
00272 }