CMS 3D CMS Logo

Public Member Functions | Static Public Member Functions | Private Attributes

PythonService Class Reference

#include <FWCore/Python/src/PythonService.h>

List of all members.

Public Member Functions

void postBeginJob ()
void postEndJob ()
void postProcessEvent (const edm::Event &, const edm::EventSetup &)
void preProcessEvent (const edm::EventID &, const edm::Timestamp &)
 PythonService (const edm::ParameterSet &, edm::ActivityRegistry &)
 ~PythonService ()

Static Public Member Functions

static void fillDescriptions (edm::ConfigurationDescriptions &descriptions)

Private Attributes

std::string command_
std::string fileName_
PythonManagerHandle handle_
boost::python::object service_

Detailed Description

Description: interface to write EDMService in Python

Usage: <usage>

Definition at line 32 of file PythonService.h.


Constructor & Destructor Documentation

PythonService::PythonService ( const edm::ParameterSet iConfig,
edm::ActivityRegistry iRegistry 
)

Definition at line 24 of file PythonService.cc.

References command_, gather_cfg::cout, Exception, convertXMLtoSQLite_cfg::fileName, edm::ParameterSet::getParameter(), postBeginJob(), postEndJob(), postProcessEvent(), query::result, service_, edm::ActivityRegistry::watchPostBeginJob(), edm::ActivityRegistry::watchPostEndJob(), and edm::ActivityRegistry::watchPostProcessEvent().

                                                                                          :
    handle_(PythonManager::handle())
{
    
    std::cout << "Start preparing PythonService" << std::endl;
    const std::string fileName = iConfig.getParameter<std::string>("fileName");
        
    using namespace boost::python;
        
    command_ = "from "+fileName + " import *\n";
    object main_module(( boost::python::handle<>(borrowed(PyImport_AddModule(const_cast<char *>("__main__"))))));
    object main_namespace = main_module.attr("__dict__");
    try {
      object result((boost::python::handle<>(PyRun_String(command_.c_str(),
                                           Py_file_input,
                                           main_namespace.ptr(),
                                           main_namespace.ptr()))));
      service_ = main_namespace["service"];
    } catch( ... ) {
      throw cms::Exception("Configuration") <<"No 'service' python variable defined in given fileName parameter.\n Please create an instance of the python class you want to use and pass that instance to the variable named 'service'.";
    }
        
    // connect methods and signals
    // later on here will be a check what python methods are present
    // for now we expect postBeginJob, postEndJob and postProcessEvent
        
    iRegistry.watchPostBeginJob(this,&PythonService::postBeginJob);
    iRegistry.watchPostEndJob(this,&PythonService::postEndJob);
    iRegistry.watchPostProcessEvent(this,&PythonService::postProcessEvent);
}
PythonService::~PythonService ( )

Definition at line 58 of file PythonService.cc.

{
}

Member Function Documentation

void PythonService::fillDescriptions ( edm::ConfigurationDescriptions descriptions) [static]

Definition at line 126 of file PythonService.cc.

References edm::ParameterSetDescription::add(), and edm::ConfigurationDescriptions::addDefault().

                                                                                {
    edm::ParameterSetDescription desc;
    desc.add<std::string>("fileName");
    descriptions.addDefault(desc);
}
void PythonService::postBeginJob ( )

Definition at line 66 of file PythonService.cc.

References pythonToCppException(), query::result, and service_.

Referenced by PythonService().

{
        using namespace boost::python;
    object main_module((boost::python::handle<>(borrowed(PyImport_AddModule(const_cast<char *>("__main__"))))));   
    object main_namespace = main_module.attr("__dict__");
    main_namespace["tempService"] = service_;

    try {
      object result((boost::python::handle<>(PyRun_String("tempService.postBeginJob()",
                                           Py_eval_input,
                                           main_namespace.ptr(),
                                           main_namespace.ptr()))));      
    }catch( error_already_set ) {
      pythonToCppException("RuntimeError");
    }
}
void PythonService::postEndJob ( )

Definition at line 84 of file PythonService.cc.

References pythonToCppException(), query::result, and service_.

Referenced by PythonService().

{
    using namespace boost::python;
    object main_module((boost::python::handle<>(borrowed(PyImport_AddModule(const_cast<char *>("__main__"))))));   
    object main_namespace = main_module.attr("__dict__");
    main_namespace["tempService"] = service_;

    try {
       object result((boost::python::handle<>(PyRun_String("tempService.postEndJob()",
                                           Py_eval_input,
                                           main_namespace.ptr(),
                                           main_namespace.ptr()))));      
    }catch( error_already_set ) {
       pythonToCppException("RuntimeError");
    }
}
void PythonService::postProcessEvent ( const edm::Event ,
const edm::EventSetup  
)

Definition at line 108 of file PythonService.cc.

References pythonToCppException(), query::result, and service_.

Referenced by PythonService().

{
    using namespace boost::python;
    object main_module((boost::python::handle<>(borrowed(PyImport_AddModule(const_cast<char *>("__main__"))))));   
    object main_namespace = main_module.attr("__dict__");
    main_namespace["tempService"] = service_;

    try {
       object result((boost::python::handle<>(PyRun_String("tempService.postProcessEvent()",
                                           Py_eval_input,
                                           main_namespace.ptr(),
                                           main_namespace.ptr()))));      
    }catch( error_already_set ) {
       pythonToCppException("RuntimeError");
    }

}
void PythonService::preProcessEvent ( const edm::EventID iID,
const edm::Timestamp iTime 
)

Definition at line 102 of file PythonService.cc.

{
   
}

Member Data Documentation

std::string PythonService::command_ [private]

Definition at line 47 of file PythonService.h.

Referenced by PythonService().

std::string PythonService::fileName_ [private]

Definition at line 46 of file PythonService.h.

Definition at line 44 of file PythonService.h.

boost::python::object PythonService::service_ [private]

Definition at line 45 of file PythonService.h.

Referenced by postBeginJob(), postEndJob(), postProcessEvent(), and PythonService().