CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PythonService.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Python
4 // Class : PythonService
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author: Benedikt Hegner
10 // Created: Sun Jul 23 11:31:33 CEST 2006
11 //
12 
13 // system include files
14 
15 // user include files
17 
20 
21 //
22 // constructor
23 //
25  handle_(PythonManager::handle()) {
26 
27  std::cout << "Start preparing PythonService" << std::endl;
28  std::string const fileName = iConfig.getParameter<std::string>("fileName");
29 
30  using namespace boost::python;
31 
32  command_ = "from " + fileName + " import *\n";
33  object main_module((boost::python::handle<>(borrowed(PyImport_AddModule(const_cast<char*>("__main__"))))));
34  object main_namespace = main_module.attr("__dict__");
35  try {
36  object result((boost::python::handle<>(PyRun_String(command_.c_str(),
37  Py_file_input,
38  main_namespace.ptr(),
39  main_namespace.ptr()))));
40  service_ = main_namespace["service"];
41  } catch(...) {
42  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'.";
43  }
44 
45  // connect methods and signals
46  // later on here will be a check what python methods are present
47  // for now we expect postBeginJob, postEndJob and postProcessEvent
48 
52 }
53 
54 //
55 // destructor
56 //
58 }
59 
60 //
61 // member functions
62 //
64  using namespace boost::python;
65  object main_module((boost::python::handle<>(borrowed(PyImport_AddModule(const_cast<char *>("__main__"))))));
66  object main_namespace = main_module.attr("__dict__");
67  main_namespace["tempService"] = service_;
68 
69  try {
70  object result((boost::python::handle<>(PyRun_String("tempService.postBeginJob()",
71  Py_eval_input,
72  main_namespace.ptr(),
73  main_namespace.ptr()))));
74  } catch(error_already_set) {
75  pythonToCppException("RuntimeError");
76  }
77 }
78 
80  using namespace boost::python;
81  object main_module((boost::python::handle<>(borrowed(PyImport_AddModule(const_cast<char *>("__main__"))))));
82  object main_namespace = main_module.attr("__dict__");
83  main_namespace["tempService"] = service_;
84 
85  try {
86  object result((boost::python::handle<>(PyRun_String("tempService.postEndJob()",
87  Py_eval_input,
88  main_namespace.ptr(),
89  main_namespace.ptr()))));
90  } catch(error_already_set) {
91  pythonToCppException("RuntimeError");
92  }
93 }
94 
96 }
97 
99  using namespace boost::python;
100  object main_module((boost::python::handle<>(borrowed(PyImport_AddModule(const_cast<char *>("__main__"))))));
101  object main_namespace = main_module.attr("__dict__");
102  main_namespace["tempService"] = service_;
103 
104  try {
105  object result((boost::python::handle<>(PyRun_String("tempService.postProcessEvent()",
106  Py_eval_input,
107  main_namespace.ptr(),
108  main_namespace.ptr()))));
109  } catch(error_already_set) {
110  pythonToCppException("RuntimeError");
111  }
112 
113 }
114 
117  desc.add<std::string>("fileName");
118  descriptions.addDefault(desc);
119 }
void pythonToCppException(const std::string &iType)
Definition: PythonManager.cc:5
T getParameter(std::string const &) const
void preProcessEvent(const edm::EventID &, const edm::Timestamp &)
void watchPostEndJob(PostEndJob::slot_type const &iSlot)
void watchPostProcessEvent(PostProcessEvent::slot_type const &iSlot)
void addDefault(ParameterSetDescription const &psetDescription)
tuple result
Definition: query.py:137
tuple handle
Definition: patZpeak.py:22
boost::python::object service_
Definition: PythonService.h:45
ParameterDescriptionBase * add(U const &iLabel, T const &value)
void postBeginJob()
tuple cout
Definition: gather_cfg.py:121
PythonService(const edm::ParameterSet &, edm::ActivityRegistry &)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
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