CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions | Static Public Member Functions | Private Attributes
PythonService Class Reference

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

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, edm::hlt::Exception, convertXMLtoSQLite_cfg::fileName, edm::ParameterSet::getParameter(), postBeginJob(), postEndJob(), postProcessEvent(), query::result, service_, edm::ActivityRegistry::watchPostBeginJob(), edm::ActivityRegistry::watchPostEndJob(), and edm::ActivityRegistry::watchPostProcessEvent().

24  :
26 {
27 
28  std::cout << "Start preparing PythonService" << std::endl;
29  const std::string fileName = iConfig.getParameter<std::string>("fileName");
30 
31  using namespace boost::python;
32 
33  command_ = "from "+fileName + " import *\n";
34  object main_module(( boost::python::handle<>(borrowed(PyImport_AddModule(const_cast<char *>("__main__"))))));
35  object main_namespace = main_module.attr("__dict__");
36  try {
37  object result((boost::python::handle<>(PyRun_String(command_.c_str(),
38  Py_file_input,
39  main_namespace.ptr(),
40  main_namespace.ptr()))));
41  service_ = main_namespace["service"];
42  } catch( ... ) {
43  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'.";
44  }
45 
46  // connect methods and signals
47  // later on here will be a check what python methods are present
48  // for now we expect postBeginJob, postEndJob and postProcessEvent
49 
53 }
T getParameter(std::string const &) const
void watchPostEndJob(PostEndJob::slot_type const &iSlot)
PythonManagerHandle handle_
Definition: PythonService.h:44
void watchPostProcessEvent(PostProcessEvent::slot_type const &iSlot)
tuple result
Definition: query.py:137
boost::python::object service_
Definition: PythonService.h:45
void postBeginJob()
static PythonManagerHandle handle()
tuple cout
Definition: gather_cfg.py:41
void postProcessEvent(const edm::Event &, const edm::EventSetup &)
void watchPostBeginJob(PostBeginJob::slot_type const &iSlot)
convenience function for attaching to signal
std::string command_
Definition: PythonService.h:47
PythonService::~PythonService ( )

Definition at line 58 of file PythonService.cc.

59 {
60 }

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().

126  {
128  desc.add<std::string>("fileName");
129  descriptions.addDefault(desc);
130 }
void addDefault(ParameterSetDescription const &psetDescription)
ParameterDescriptionBase * add(U const &iLabel, T const &value)
void PythonService::postBeginJob ( )

Definition at line 66 of file PythonService.cc.

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

Referenced by PythonService().

67 {
68  using namespace boost::python;
69  object main_module((boost::python::handle<>(borrowed(PyImport_AddModule(const_cast<char *>("__main__"))))));
70  object main_namespace = main_module.attr("__dict__");
71  main_namespace["tempService"] = service_;
72 
73  try {
74  object result((boost::python::handle<>(PyRun_String("tempService.postBeginJob()",
75  Py_eval_input,
76  main_namespace.ptr(),
77  main_namespace.ptr()))));
78  }catch( error_already_set ) {
79  pythonToCppException("RuntimeError");
80  }
81 }
void pythonToCppException(const std::string &iType)
Definition: PythonManager.cc:5
tuple result
Definition: query.py:137
boost::python::object service_
Definition: PythonService.h:45
void PythonService::postEndJob ( )

Definition at line 84 of file PythonService.cc.

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

Referenced by PythonService().

85 {
86  using namespace boost::python;
87  object main_module((boost::python::handle<>(borrowed(PyImport_AddModule(const_cast<char *>("__main__"))))));
88  object main_namespace = main_module.attr("__dict__");
89  main_namespace["tempService"] = service_;
90 
91  try {
92  object result((boost::python::handle<>(PyRun_String("tempService.postEndJob()",
93  Py_eval_input,
94  main_namespace.ptr(),
95  main_namespace.ptr()))));
96  }catch( error_already_set ) {
97  pythonToCppException("RuntimeError");
98  }
99 }
void pythonToCppException(const std::string &iType)
Definition: PythonManager.cc:5
tuple result
Definition: query.py:137
boost::python::object service_
Definition: PythonService.h:45
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().

109 {
110  using namespace boost::python;
111  object main_module((boost::python::handle<>(borrowed(PyImport_AddModule(const_cast<char *>("__main__"))))));
112  object main_namespace = main_module.attr("__dict__");
113  main_namespace["tempService"] = service_;
114 
115  try {
116  object result((boost::python::handle<>(PyRun_String("tempService.postProcessEvent()",
117  Py_eval_input,
118  main_namespace.ptr(),
119  main_namespace.ptr()))));
120  }catch( error_already_set ) {
121  pythonToCppException("RuntimeError");
122  }
123 
124 }
void pythonToCppException(const std::string &iType)
Definition: PythonManager.cc:5
tuple result
Definition: query.py:137
boost::python::object service_
Definition: PythonService.h:45
void PythonService::preProcessEvent ( const edm::EventID iID,
const edm::Timestamp iTime 
)

Definition at line 102 of file PythonService.cc.

103 {
104 
105 }

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.

PythonManagerHandle PythonService::handle_
private

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().