CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch13/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(const edm::ParameterSet& iConfig, edm::ActivityRegistry& iRegistry):
00025     handle_(PythonManager::handle())
00026 {
00027     
00028     std::cout << "Start preparing PythonService" << std::endl;
00029     const std::string fileName = iConfig.getParameter<std::string>("fileName");
00030         
00031     using namespace boost::python;
00032         
00033     command_ = "from "+fileName + " import *\n";
00034     object main_module(( boost::python::handle<>(borrowed(PyImport_AddModule(const_cast<char *>("__main__"))))));
00035     object main_namespace = main_module.attr("__dict__");
00036     try {
00037       object result((boost::python::handle<>(PyRun_String(command_.c_str(),
00038                                            Py_file_input,
00039                                            main_namespace.ptr(),
00040                                            main_namespace.ptr()))));
00041       service_ = main_namespace["service"];
00042     } catch( ... ) {
00043       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'.";
00044     }
00045         
00046     // connect methods and signals
00047     // later on here will be a check what python methods are present
00048     // for now we expect postBeginJob, postEndJob and postProcessEvent
00049         
00050     iRegistry.watchPostBeginJob(this,&PythonService::postBeginJob);
00051     iRegistry.watchPostEndJob(this,&PythonService::postEndJob);
00052     iRegistry.watchPostProcessEvent(this,&PythonService::postProcessEvent);
00053 }
00054 
00055 //
00056 // destructor
00057 //
00058 PythonService::~PythonService()
00059 {
00060 }
00061 
00062 
00063 //
00064 // member functions
00065 //
00066 void PythonService::postBeginJob()
00067 {
00068         using namespace boost::python;
00069     object main_module((boost::python::handle<>(borrowed(PyImport_AddModule(const_cast<char *>("__main__"))))));   
00070     object main_namespace = main_module.attr("__dict__");
00071     main_namespace["tempService"] = service_;
00072 
00073     try {
00074       object result((boost::python::handle<>(PyRun_String("tempService.postBeginJob()",
00075                                            Py_eval_input,
00076                                            main_namespace.ptr(),
00077                                            main_namespace.ptr()))));      
00078     }catch( error_already_set ) {
00079       pythonToCppException("RuntimeError");
00080     }
00081 }
00082 
00083 
00084 void PythonService::postEndJob()
00085 {
00086     using namespace boost::python;
00087     object main_module((boost::python::handle<>(borrowed(PyImport_AddModule(const_cast<char *>("__main__"))))));   
00088     object main_namespace = main_module.attr("__dict__");
00089     main_namespace["tempService"] = service_;
00090 
00091     try {
00092        object result((boost::python::handle<>(PyRun_String("tempService.postEndJob()",
00093                                            Py_eval_input,
00094                                            main_namespace.ptr(),
00095                                            main_namespace.ptr()))));      
00096     }catch( error_already_set ) {
00097        pythonToCppException("RuntimeError");
00098     }
00099 }
00100 
00101 
00102 void PythonService::preProcessEvent(const edm::EventID& iID, const edm::Timestamp& iTime)
00103 {
00104    
00105 }
00106 
00107 
00108 void PythonService::postProcessEvent(const edm::Event&, const edm::EventSetup&)
00109 {
00110     using namespace boost::python;
00111     object main_module((boost::python::handle<>(borrowed(PyImport_AddModule(const_cast<char *>("__main__"))))));   
00112     object main_namespace = main_module.attr("__dict__");
00113     main_namespace["tempService"] = service_;
00114 
00115     try {
00116        object result((boost::python::handle<>(PyRun_String("tempService.postProcessEvent()",
00117                                            Py_eval_input,
00118                                            main_namespace.ptr(),
00119                                            main_namespace.ptr()))));      
00120     }catch( error_already_set ) {
00121        pythonToCppException("RuntimeError");
00122     }
00123 
00124 }
00125 
00126 void PythonService::fillDescriptions(edm::ConfigurationDescriptions & descriptions) {
00127     edm::ParameterSetDescription desc;
00128     desc.add<std::string>("fileName");
00129     descriptions.addDefault(desc);
00130 }