CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_8_patch3/src/FWCore/Python/src/PythonService.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     Python
00004 // Class  :     PythonService
00005 //
00006 // Implementation:
00007 //     <Notes on implementation>
00008 //
00009 // Original Author:  Benedikt Hegner
00010 //         Created:  Sun Jul 23 11:31:33 CEST 2006
00011 //
00012 
00013 // system include files
00014 
00015 // user include files
00016 #include "FWCore/Python/src/PythonService.h"
00017 
00018 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
00019 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
00020 
00021 //
00022 // constructor
00023 //
00024 PythonService::PythonService(edm::ParameterSet const& iConfig, edm::ActivityRegistry& iRegistry):
00025     handle_(PythonManager::handle()) {
00026 
00027     std::cout << "Start preparing PythonService" << std::endl;
00028     std::string const fileName = iConfig.getParameter<std::string>("fileName");
00029 
00030     using namespace boost::python;
00031 
00032     command_ = "from " + fileName + " import *\n";
00033     object main_module((boost::python::handle<>(borrowed(PyImport_AddModule(const_cast<char*>("__main__"))))));
00034     object main_namespace = main_module.attr("__dict__");
00035     try {
00036       object result((boost::python::handle<>(PyRun_String(command_.c_str(),
00037                                              Py_file_input,
00038                                              main_namespace.ptr(),
00039                                              main_namespace.ptr()))));
00040       service_ = main_namespace["service"];
00041     } catch(...) {
00042       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'.";
00043     }
00044 
00045     // connect methods and signals
00046     // later on here will be a check what python methods are present
00047     // for now we expect postBeginJob, postEndJob and postProcessEvent
00048 
00049     iRegistry.watchPostBeginJob(this,&PythonService::postBeginJob);
00050     iRegistry.watchPostEndJob(this,&PythonService::postEndJob);
00051     iRegistry.watchPostProcessEvent(this,&PythonService::postProcessEvent);
00052 }
00053 
00054 //
00055 // destructor
00056 //
00057 PythonService::~PythonService() {
00058 }
00059 
00060 //
00061 // member functions
00062 //
00063 void PythonService::postBeginJob() {
00064         using namespace boost::python;
00065     object main_module((boost::python::handle<>(borrowed(PyImport_AddModule(const_cast<char *>("__main__"))))));
00066     object main_namespace = main_module.attr("__dict__");
00067     main_namespace["tempService"] = service_;
00068 
00069     try {
00070       object result((boost::python::handle<>(PyRun_String("tempService.postBeginJob()",
00071                                              Py_eval_input,
00072                                              main_namespace.ptr(),
00073                                              main_namespace.ptr()))));
00074     } catch(error_already_set) {
00075       pythonToCppException("RuntimeError");
00076     }
00077 }
00078 
00079 void PythonService::postEndJob() {
00080     using namespace boost::python;
00081     object main_module((boost::python::handle<>(borrowed(PyImport_AddModule(const_cast<char *>("__main__"))))));
00082     object main_namespace = main_module.attr("__dict__");
00083     main_namespace["tempService"] = service_;
00084 
00085     try {
00086        object result((boost::python::handle<>(PyRun_String("tempService.postEndJob()",
00087                                                            Py_eval_input,
00088                                                            main_namespace.ptr(),
00089                                                            main_namespace.ptr()))));
00090     } catch(error_already_set) {
00091        pythonToCppException("RuntimeError");
00092     }
00093 }
00094 
00095 void PythonService::preProcessEvent(edm::EventID const&, edm::Timestamp const&) {
00096 }
00097 
00098 void PythonService::postProcessEvent(edm::Event const&, edm::EventSetup const&) {
00099     using namespace boost::python;
00100     object main_module((boost::python::handle<>(borrowed(PyImport_AddModule(const_cast<char *>("__main__"))))));
00101     object main_namespace = main_module.attr("__dict__");
00102     main_namespace["tempService"] = service_;
00103 
00104     try {
00105        object result((boost::python::handle<>(PyRun_String("tempService.postProcessEvent()",
00106                                                            Py_eval_input,
00107                                                            main_namespace.ptr(),
00108                                                            main_namespace.ptr()))));
00109     } catch(error_already_set) {
00110        pythonToCppException("RuntimeError");
00111     }
00112 
00113 }
00114 
00115 void PythonService::fillDescriptions(edm::ConfigurationDescriptions & descriptions) {
00116     edm::ParameterSetDescription desc;
00117     desc.add<std::string>("fileName");
00118     descriptions.addDefault(desc);
00119 }