Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shift from Boost Python to Pybind11 #34692

Closed
wants to merge 12 commits into from
5 changes: 4 additions & 1 deletion CondCore/SiPixelPlugins/test/testSiPixelPayloadInspector.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <iostream>
#include <sstream>
#include <pybind11/pybind11.h>
#include "CondCore/Utilities/interface/PayloadInspector.h"
#include "CondCore/SiPixelPlugins/plugins/SiPixelLorentzAngle_PayloadInspector.cc"
#include "CondCore/SiPixelPlugins/plugins/SiPixelQuality_PayloadInspector.cc"
Expand All @@ -15,6 +16,8 @@
#include "FWCore/PluginManager/interface/SharedLibrary.h"
#include "FWCore/ServiceRegistry/interface/ServiceRegistry.h"

namespace py = pybind11;

int main(int argc, char** argv) {
Py_Initialize();

Expand Down Expand Up @@ -132,7 +135,7 @@ int main(int argc, char** argv) {
histo16.process(connectionString, PI::mk_input(tag, end, end));
edm::LogPrint("testSiPixelPayloadInspector") << histo16.data() << std::endl;

boost::python::dict inputs;
py::dict inputs;
inputs["SetLog"] = "True"; // sets to true, 1,True,Yes will work

SiPixelGainCalibrationOfflineGainsValuesBarrel histo17;
Expand Down
8 changes: 6 additions & 2 deletions CondCore/SiStripPlugins/test/testSiStripPayloadInspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#include "FWCore/PluginManager/interface/PluginManager.h"
#include "FWCore/PluginManager/interface/standard.h"
#include "FWCore/ServiceRegistry/interface/ServiceRegistry.h"
#include <pybind11/pybind11.h>

namespace py = pybind11;

int main(int argc, char** argv) {
Py_Initialize();
Expand All @@ -33,7 +36,7 @@ int main(int argc, char** argv) {
std::string tag = "SiStripApvGain_FromParticles_GR10_v11_offline";
cond::Time_t start = boost::lexical_cast<unsigned long long>(132440);
cond::Time_t end = boost::lexical_cast<unsigned long long>(285368);
boost::python::dict inputs;
py::dict inputs;

edm::LogPrint("testSiStripPayloadInspector") << "## Exercising Gains plots " << std::endl;

Expand Down Expand Up @@ -112,7 +115,8 @@ int main(int argc, char** argv) {
edm::LogPrint("testSiStripPayloadInspector") << histo11.data() << std::endl;

SiStripPedestalPerDetId histoPedestalForDetId;
inputs["DetIds"] += ",470065830,369121594,369124670,470177668"; // add a bunch of other DetIds
inputs["DetIds"] =
inputs["DetIds"].cast<std::string>() + ",470065830,369121594,369124670,470177668"; // add a bunch of other DetIds
histoPedestalForDetId.setInputParamValues(inputs);
histoPedestalForDetId.process(connectionString, PI::mk_input(tag, start, start));
edm::LogPrint("testSiStripPayloadInspector") << histoPedestalForDetId.data() << std::endl;
Expand Down
43 changes: 22 additions & 21 deletions CondCore/Utilities/interface/PayloadInspector.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#ifndef CondCore_Utilities_PayloadInspector_h
#define CondCore_Utilities_PayloadInspector_h

#include "CondCore/CondDB/interface/Utils.h"
#include "CondCore/CondDB/interface/Session.h"
#include "CondCore/CondDB/interface/Exception.h"
#include <iostream>

#include <string>
Expand All @@ -13,26 +10,29 @@

#include "FWCore/Utilities/interface/GlobalIdentifier.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "CondCore/CondDB/interface/Utils.h"
#include "CondCore/CondDB/interface/Session.h"
#include "CondCore/CondDB/interface/Exception.h"

#include <pybind11/pybind11.h>

#include <boost/python/list.hpp>
#include <boost/python/dict.hpp>
#include <boost/python/tuple.hpp>
namespace py = pybind11;

namespace PI {
inline boost::python::list mk_input(const std::string& tagName, cond::Time_t start, cond::Time_t end) {
boost::python::list ret;
ret.append(boost::python::make_tuple(tagName, std::to_string(start), std::to_string(end)));
inline py::list mk_input(const std::string& tagName, cond::Time_t start, cond::Time_t end) {
py::list ret;
ret.append(py::make_tuple(tagName, std::to_string(start), std::to_string(end)));
return ret;
}
inline boost::python::list mk_input(const std::string& tagName0,
cond::Time_t start0,
cond::Time_t end0,
const std::string& tagName1,
cond::Time_t start1,
cond::Time_t end1) {
boost::python::list ret;
ret.append(boost::python::make_tuple(tagName0, std::to_string(start0), std::to_string(end0)));
ret.append(boost::python::make_tuple(tagName1, std::to_string(start1), std::to_string(end1)));
inline py::list mk_input(const std::string& tagName0,
cond::Time_t start0,
cond::Time_t end0,
const std::string& tagName1,
cond::Time_t start1,
cond::Time_t end1) {
py::list ret;
ret.append(py::make_tuple(tagName0, std::to_string(start0), std::to_string(end0)));
ret.append(py::make_tuple(tagName1, std::to_string(start1), std::to_string(end1)));
return ret;
}
} // namespace PI
Expand Down Expand Up @@ -228,16 +228,17 @@ namespace cond {
bool isSingleIov() const;

// required in the browser
boost::python::list inputParams() const;
__attribute__((visibility("default"))) py::list inputParams() const;

// required in the browser
void setInputParamValues(const boost::python::dict& values);
__attribute__((visibility("default"))) void setInputParamValues(const py::dict& values);

// returns the json file with the plot data
std::string data() const;

// triggers the processing producing the plot
bool process(const std::string& connectionString, const boost::python::list& tagsWithTimeBoundaries);
__attribute__((visibility("default"))) bool process(const std::string& connectionString,
const py::list& tagsWithTimeBoundaries);

// called by the above method - to be used in C++ unit tests...
bool exec_process(const std::string& connectionString,
Expand Down
25 changes: 14 additions & 11 deletions CondCore/Utilities/interface/PayloadInspectorModule.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
#include <boost/python.hpp>
#include <pybind11/pybind11.h>

namespace py = pybind11;

#define PPCAT_NX(A, B) A##B
#define PPCAT(A, B) PPCAT_NX(A, B)
#define STRINGIZE_NX(A) #A
#define STRINGIZE(A) STRINGIZE_NX(A)

#define PAYLOAD_INSPECTOR_MODULE(PAYLOAD_TYPENAME) BOOST_PYTHON_MODULE(plugin##PAYLOAD_TYPENAME##_PayloadInspector)
#define PAYLOAD_INSPECTOR_MODULE(PAYLOAD_TYPENAME) PYBIND11_MODULE(plugin##PAYLOAD_TYPENAME##_PayloadInspector, m)

#define PAYLOAD_INSPECTOR_CLASS(CLASS_NAME) \
boost::python::class_<CLASS_NAME, boost::python::bases<cond::payloadInspector::PlotBase> >( \
STRINGIZE(PPCAT(plot_, CLASS_NAME)), boost::python::init<>()) \
.def("process", &cond::payloadInspector::PlotBase::process) \
.def("payloadType", &cond::payloadInspector::PlotBase::payloadType) \
.def("title", &cond::payloadInspector::PlotBase::title) \
.def("isSingleIov", &cond::payloadInspector::PlotBase::isSingleIov) \
.def("isTwoTags", &cond::payloadInspector::PlotBase::isTwoTags) \
.def("data", &cond::payloadInspector::PlotBase::data);
#define PAYLOAD_INSPECTOR_CLASS(CLASS_NAME) \
py::class_<CLASS_NAME, cond::payloadInspector::PlotBase>(m, \
STRINGIZE(PPCAT(plot_, CLASS_NAME))) \
.def(py::init<>()) \
.def("process", &CLASS_NAME::process) \
.def("payloadType", &CLASS_NAME::payloadType) \
.def("title", &CLASS_NAME::title) \
.def("isSingleIov", &CLASS_NAME::isSingleIov) \
.def("isTwoTags", &CLASS_NAME::isTwoTags) \
.def("data", &CLASS_NAME::data);
3 changes: 2 additions & 1 deletion CondCore/Utilities/plugins/BuildFile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
<use name="CondCore/Utilities"/>
<use name="CondCore/CondDB"/>
<use name="CondFormats/Common"/>
<use name="boost_python"/>
<use name="py3-pybind11"/>
<use name="python3"/>
<use name="rootgraphics"/>
</library>

Expand Down
18 changes: 12 additions & 6 deletions CondCore/Utilities/plugins/Module_PayloadInspector.cc
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
#include "CondCore/Utilities/interface/PayloadInspector.h"
#include <boost/python.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include <pybind11/pybind11.h>
#include <pybind11/stl_bind.h>
#include <pybind11/functional.h>
#include <pybind11/stl.h>

BOOST_PYTHON_MODULE(pluginModule_PayloadInspector) {
boost::python::class_<cond::payloadInspector::ModuleVersion>("ModuleVersion")
.def_readonly("label", &cond::payloadInspector::ModuleVersion::label);
boost::python::class_<cond::payloadInspector::PlotBase>("PlotBase")
namespace py = pybind11;

PYBIND11_MODULE(pluginModule_PayloadInspector, m) {
py::class_<cond::payloadInspector::ModuleVersion, std::shared_ptr<cond::payloadInspector::ModuleVersion>>(
m, "ModuleVersion")
.def("label", []() { return cond::payloadInspector::ModuleVersion::label; });

py::class_<cond::payloadInspector::PlotBase>(m, "PlotBase")
.def("process", &cond::payloadInspector::PlotBase::process)
.def("payloadType", &cond::payloadInspector::PlotBase::payloadType)
.def("type", &cond::payloadInspector::PlotBase::type)
Expand Down
4 changes: 2 additions & 2 deletions CondCore/Utilities/scripts/getPayloadData.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ def deserialize_iovs(db, plugin_name, plot_name, tags, time_type, input_params):
modv = getattr(plugin_base,'ModuleVersion')

success = False
if modv.label == '1.0':
if modv.label() == '1.0':
if len(tags)==1:
success = plot.process(db_name, tags[0][0], time_type, int(tags[0][1]), int(tags[0][2]) )
elif len(tags)==2:
success = plot.processTwoTags(db_name, tags[0][0], tags[1][0], int(tags[0][1]),int(tags[1][1]) )
elif modv.label == '2.0':
elif modv.label() == '2.0':
success = plot.process(db_name, tags)

output('plot processed data successfully: ', success)
Expand Down
35 changes: 19 additions & 16 deletions CondCore/Utilities/src/PayloadInspector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

#include <sstream>
#include <iostream>
#include <boost/python/extract.hpp>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/embed.h>

namespace py = pybind11;

namespace cond {

Expand All @@ -21,8 +25,6 @@ namespace cond {
return ret;
}

constexpr const char* const ModuleVersion::label;

PlotBase::PlotBase()
: m_plotAnnotations(),
m_inputParams(),
Expand All @@ -49,36 +51,37 @@ namespace cond {

bool PlotBase::isTwoTags() const { return m_plotAnnotations.ntags == 2; }

boost::python::list PlotBase::inputParams() const {
boost::python::list tmp;
py::list PlotBase::inputParams() const {
py::list tmp;
for (const auto& ip : m_inputParams) {
tmp.append(ip);
}
return tmp;
}

void PlotBase::setInputParamValues(const boost::python::dict& values) {
for (const auto& ip : m_inputParams) {
if (values.has_key(ip)) {
std::string val = boost::python::extract<std::string>(values.get(ip));
m_inputParamValues.insert(std::make_pair(ip, val));
void PlotBase::setInputParamValues(const py::dict& values) {
for (auto item : values) {
std::string key = item.first.cast<std::string>();
std::string val = item.second.cast<std::string>();
if (m_inputParams.find(key) != m_inputParams.end()) {
m_inputParamValues.insert(std::make_pair(key, val));
}
}
}

std::string PlotBase::data() const { return m_data; }

bool PlotBase::process(const std::string& connectionString, const boost::python::list& tagsWithTimeBoundaries) {
size_t nt = boost::python::len(tagsWithTimeBoundaries);
bool PlotBase::process(const std::string& connectionString, const py::list& tagsWithTimeBoundaries) {
size_t nt = tagsWithTimeBoundaries.size();
bool ret = false;
if (nt) {
std::vector<std::tuple<std::string, cond::Time_t, cond::Time_t> > tags;
tags.resize(nt);
for (size_t i = 0; i < nt; i++) {
boost::python::tuple entry = boost::python::extract<boost::python::tuple>(tagsWithTimeBoundaries[i]);
std::string tagName = boost::python::extract<std::string>(entry[0]);
std::string time0s = boost::python::extract<std::string>(entry[1]);
std::string time1s = boost::python::extract<std::string>(entry[2]);
py::tuple entry = tagsWithTimeBoundaries[i].cast<py::tuple>();
std::string tagName = entry[0].cast<std::string>();
std::string time0s = entry[1].cast<std::string>();
std::string time1s = entry[2].cast<std::string>();
cond::Time_t time0 = boost::lexical_cast<cond::Time_t>(time0s);
cond::Time_t time1 = boost::lexical_cast<cond::Time_t>(time1s);
tags[i] = std::make_tuple(tagName, time0, time1);
Expand Down