CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_2_9_HLT1_bphpatch4/src/FWCore/Python/src/PyModule.cc

Go to the documentation of this file.
00001 /*
00002  *  PyModule.cc
00003  *  CMSSW
00004  *
00005  *  Created by Chris Jones on 3/22/06.
00006  *
00007  */
00008 #include <boost/python/module.hpp>
00009 #include <boost/python/class.hpp>
00010 #include <boost/python/exception_translator.hpp>
00011 
00012 #include "FWCore/Framework/interface/GenericHandle.h"
00013 
00014 #include "FWCore/Python/src/EventWrapper.h"
00015 
00016 #include "FWCore/Utilities/interface/Exception.h"
00017 
00018 #include "Rtypes.h"
00019 #include "TClass.h"
00020 
00021 class TClass;
00022 namespace PyROOT {
00023   PyObject* BindRootObject( void* object, TClass* klass, Bool_t isRef = kFALSE );
00024 }
00025 
00026 namespace {
00027    void (edm::python::ConstEventWrapper::*getByLabel1)(std::string const& , edm::GenericHandle& ) const = &edm::python::ConstEventWrapper::getByLabel;
00028    void (edm::python::ConstEventWrapper::*getByLabel2)(std::string const& , std::string const&, edm::GenericHandle& ) const = &edm::python::ConstEventWrapper::getByLabel;
00029   
00030   PyObject* getFromHandle(const edm::GenericHandle& iHandle)
00031   {
00032      if(0 == iHandle.product() ) {
00033         boost::python::object null;
00034         return null.ptr();
00035      }
00036     return PyROOT::BindRootObject( iHandle.product()->Address(), TClass::GetClass(iHandle.type().TypeInfo()));
00037   }
00038 }
00039 
00040 //this function is used to translate C++ exceptions to python exceptions
00041 static 
00042 void
00043 exceptionTranslation(cms::Exception const& iException) {
00044    PyErr_SetString(PyExc_RuntimeError, iException.what());
00045 }
00046 
00047 
00048 using namespace boost::python;
00049 BOOST_PYTHON_MODULE(libFWCorePython)
00050 {
00051   register_exception_translator< cms::Exception >(exceptionTranslation);
00052 
00053   class_<edm::python::ConstEventWrapper>("ConstEvent",no_init)
00054   .def("getByLabel",getByLabel1)
00055   .def("getByLabel",getByLabel2);
00056   
00057   class_<edm::GenericHandle>("Handle",init<std::string>())
00058   .def("get",getFromHandle);
00059 }