CMS 3D CMS Logo

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