File indexing completed on 2023-03-17 11:03:33
0001 #include "FWCore/Framework/interface/one/EDProducer.h"
0002 #include "FWCore/Framework/interface/Event.h"
0003 #include "FWCore/Framework/interface/MakerMacros.h"
0004 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0005 #include "FWCore/Utilities/interface/InputTag.h"
0006 #include "FWCore/Utilities/interface/EDGetToken.h"
0007 #include "FWCore/Utilities/interface/EDPutToken.h"
0008 #include "DataFormats/TestObjects/interface/ToyProducts.h"
0009 #include <pybind11/pybind11.h>
0010
0011 #include <iostream>
0012
0013 namespace edmtest {
0014 class PythonTestProducer : public edm::one::EDProducer<edm::one::SharedResources> {
0015 public:
0016 PythonTestProducer(edm::ParameterSet const&);
0017
0018 void produce(edm::Event& iEvent, edm::EventSetup const&) override;
0019
0020 private:
0021 edm::EDGetTokenT<IntProduct> get_;
0022 edm::EDPutTokenT<int> put_;
0023 int value_;
0024 pybind11::list outputList_;
0025 };
0026
0027 PythonTestProducer::PythonTestProducer(edm::ParameterSet const& iPS)
0028 : get_(consumes<IntProduct>(iPS.getParameter<edm::InputTag>("source"))) {
0029 pybind11::module main_module = pybind11::module::import("__main__");
0030 auto main_namespace = main_module.attr("__dict__");
0031
0032
0033 value_ = main_namespace[(iPS.getParameter<std::string>("inputVariable")).c_str()].cast<int>();
0034 outputList_ = main_namespace[(iPS.getParameter<std::string>("outputListVariable")).c_str()].cast<pybind11::list>();
0035 put_ = produces<int>();
0036
0037 usesResource("python");
0038 }
0039
0040 void PythonTestProducer::produce(edm::Event& iEvent, edm::EventSetup const&) {
0041 edm::Handle<IntProduct> h;
0042 iEvent.getByToken(get_, h);
0043 {
0044 pybind11::gil_scoped_acquire acquire;
0045 outputList_.append(h->value);
0046 }
0047 iEvent.emplace(put_, h->value + value_);
0048 }
0049 }
0050
0051
0052 DEFINE_FWK_MODULE(edmtest::PythonTestProducer);