CMS 3D CMS Logo

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  *  Copyright 2006 __MyCompanyName__. All rights reserved.
00007  *
00008  */
00009 #include <boost/python/module.hpp>
00010 #include <boost/python/def.hpp>
00011 #include <boost/python/args.hpp>
00012 #include <boost/python/tuple.hpp>
00013 #include <boost/python/class.hpp>
00014 #include <boost/python/overloads.hpp>
00015 #include <boost/python/raw_function.hpp>
00016 #include <boost/python/return_internal_reference.hpp>
00017 #include <boost/python/exception_translator.hpp>
00018 
00019 #include "FWCore/Framework/interface/Event.h"
00020 #include "FWCore/Framework/interface/GenericHandle.h"
00021 
00022 #include "FWCore/Python/src/EventWrapper.h"
00023 
00024 #include "FWCore/Utilities/interface/Exception.h"
00025 
00026 #include "Rtypes.h"
00027 #include "TClass.h"
00028 
00029 class TClass;
00030 namespace PyROOT {
00031   PyObject* BindRootObject( void* object, TClass* klass, Bool_t isRef = kFALSE );
00032 }
00033 
00034 namespace {
00035    void (edm::python::ConstEventWrapper::*getByLabel1)(std::string const& , edm::GenericHandle& ) const = &edm::python::ConstEventWrapper::getByLabel;
00036    void (edm::python::ConstEventWrapper::*getByLabel2)(std::string const& , std::string const&, edm::GenericHandle& ) const = &edm::python::ConstEventWrapper::getByLabel;
00037   
00038   PyObject* getFromHandle(const edm::GenericHandle& iHandle)
00039   {
00040      if(0 == iHandle.product() ) {
00041         boost::python::object null;
00042         return null.ptr();
00043      }
00044     return PyROOT::BindRootObject( iHandle.product()->Address(), TClass::GetClass(iHandle.type().TypeInfo()));
00045   }
00046 }
00047 
00048 //this function is used to translate C++ exceptions to python exceptions
00049 static 
00050 void
00051 exceptionTranslation(cms::Exception const& iException) {
00052    PyErr_SetString(PyExc_RuntimeError, iException.what());
00053 }
00054 
00055 
00056 using namespace boost::python;
00057 BOOST_PYTHON_MODULE(libFWCorePython)
00058 {
00059   register_exception_translator< cms::Exception >(exceptionTranslation);
00060 
00061   class_<edm::python::ConstEventWrapper>("ConstEvent",no_init)
00062   .def("getByLabel",getByLabel1)
00063   .def("getByLabel",getByLabel2);
00064   
00065   class_<edm::GenericHandle>("Handle",init<std::string>())
00066   .def("get",getFromHandle);
00067 }

Generated on Tue Jun 9 17:36:32 2009 for CMSSW by  doxygen 1.5.4