forked from SVfit/ClassicSVfit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpybind_wrapper.cpp
233 lines (213 loc) · 13.6 KB
/
pybind_wrapper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
#include "../interface/MeasuredTauLepton.h"
#include "../interface/ClassicSVfit.h"
#include "../interface/FastMTT.h"
#include "TMatrixT.h" // Include the TMatrixT header
namespace py = pybind11;
PYBIND11_MODULE(pybind_wrapper, m) {
py::class_<ClassicSVfit>(m, "ClassicSVfit")
.def(py::init<int>())
.def("addLogM_fixed", &ClassicSVfit::addLogM_fixed)
.def("addLogM_dynamic", &ClassicSVfit::addLogM_dynamic)
.def("setDiTauMassConstraint", &ClassicSVfit::setDiTauMassConstraint)
.def("setVerbosity", &ClassicSVfit::setVerbosity)
.def("setMaxObjFunctionCalls", &ClassicSVfit::setMaxObjFunctionCalls)
.def("setLikelihoodFileName", &ClassicSVfit::setLikelihoodFileName)
.def("setTreeFileName", &ClassicSVfit::setTreeFileName)
.def("setHistogramAdapter",[](ClassicSVfit &self, classic_svFit::HistogramAdapter* adapter) {
self.setHistogramAdapter(adapter);
})
// .def("setHistogramAdapter",[](ClassicSVfit &self) {
// auto adapter = std::make_unique<classic_svFit::TauTauHistogramAdapter>();
// self.setHistogramAdapter(adapter.release());
// })
.def("getHistogramAdapter", &ClassicSVfit::getHistogramAdapter, py::return_value_policy::reference)
.def("prepareIntegrand", &ClassicSVfit::prepareIntegrand)
.def("prepareLeptonInput", &ClassicSVfit::prepareLeptonInput)
// .def("addMETEstimate", &ClassicSVfit::addMETEstimate)
.def("addMETEstimate", [](ClassicSVfit &self, double metx, double mety, const py::object &cppyy_matrix) {
// Check if the provided object is indeed a TMatrixT<double>
if (!cppyy_matrix || !PyObject_HasAttrString(cppyy_matrix.ptr(), "GetMatrixArray")) {
throw std::runtime_error("Invalid input: not a TMatrixT<double> object");
}
// Get the size of the matrix
const int nrows = py::getattr(cppyy_matrix, "GetNrows")().cast<int>();
const int ncols = py::getattr(cppyy_matrix, "GetNcols")().cast<int>();
// Create a TMatrixD with the same size
TMatrixD matrix(nrows, ncols);
// Extract data from the TMatrixT<double> object and fill the TMatrixD
for (int i = 0; i < nrows; ++i) {
for (int j = 0; j < ncols; ++j) {
// const double value = py::getattr(cppyy_matrix,"[][]")(i, j).cast<double>();
const double value = cppyy_matrix.attr("__getitem__")(i).attr("__getitem__")(j).cast<double>();
// const double value = cppyy_matrix[i][j].cast<double>();
matrix(i, j) = value;
}
}
// Call the addMETEstimate method with the TMatrixD
self.addMETEstimate(metx, mety, matrix);
}, py::arg("metx"), py::arg("mety"), py::arg("cppyy_matrix"))
.def("clearMET", &ClassicSVfit::clearMET)
// .def("integrate", &ClassicSVfit::integrate)
.def("integrate", [](ClassicSVfit &self,
const std::vector<classic_svFit::MeasuredTauLepton>& leptons,
const double& arg1, const double& arg2,
const py::object &cppyy_matrix){
// Check if the provided object is indeed a TMatrixT<double>
if (!cppyy_matrix || !PyObject_HasAttrString(cppyy_matrix.ptr(), "GetMatrixArray")) {
throw std::runtime_error("Invalid input: not a TMatrixT<double> object");
}
// Get the size of the matrix
const int nrows = py::getattr(cppyy_matrix, "GetNrows")().cast<int>();
const int ncols = py::getattr(cppyy_matrix, "GetNcols")().cast<int>();
// Create a TMatrixD with the same size
TMatrixD matrix(nrows, ncols);
// Extract data from the TMatrixT<double> object and fill the TMatrixD
for (int i = 0; i < nrows; ++i) {
for (int j = 0; j < ncols; ++j) {
// const double value = py::getattr(cppyy_matrix,"[][]")(i, j).cast<double>();
const double value = cppyy_matrix.attr("__getitem__")(i).attr("__getitem__")(j).cast<double>();
// const double value = cppyy_matrix[i][j].cast<double>();
matrix(i, j) = value;
}
}
// Call the addMETEstimate method with the TMatrixD
self.integrate(leptons, arg1, arg2, matrix);
}, py::arg("leptons"), py::arg("arg1"),py::arg("arg1"), py::arg("cppyy_matrix"))
.def("isValidSolution", &ClassicSVfit::isValidSolution)
.def("getComputingTime_cpu", &ClassicSVfit::getComputingTime_cpu)
.def("getComputingTime_real", &ClassicSVfit::getComputingTime_real);
py::class_<classic_svFit::MeasuredTauLepton>(m, "MeasuredTauLepton")
.def(py::init<>())
.def(py::init<int, double, double, double, double, int>())
.def(py::init<const classic_svFit::MeasuredTauLepton&>())
.def("type", &classic_svFit::MeasuredTauLepton::type)
.def("pt", &classic_svFit::MeasuredTauLepton::pt)
.def("eta", &classic_svFit::MeasuredTauLepton::eta)
.def("phi", &classic_svFit::MeasuredTauLepton::phi)
.def("mass", &classic_svFit::MeasuredTauLepton::mass)
.def("energy", &classic_svFit::MeasuredTauLepton::energy)
.def("px", &classic_svFit::MeasuredTauLepton::px)
.def("py", &classic_svFit::MeasuredTauLepton::py)
.def("pz", &classic_svFit::MeasuredTauLepton::pz)
.def("p", &classic_svFit::MeasuredTauLepton::p)
.def("decayMode", &classic_svFit::MeasuredTauLepton::decayMode)
.def("p4", &classic_svFit::MeasuredTauLepton::p4)
.def("p3", &classic_svFit::MeasuredTauLepton::p3)
.def("cosPhi_sinTheta", &classic_svFit::MeasuredTauLepton::cosPhi_sinTheta)
.def("sinPhi_sinTheta", &classic_svFit::MeasuredTauLepton::sinPhi_sinTheta)
.def("cosTheta", &classic_svFit::MeasuredTauLepton::cosTheta)
.def("roundToNdigits", &classic_svFit::MeasuredTauLepton::roundToNdigits);
py::class_<classic_svFit::HistogramAdapter>(m, "HistogramAdapter")
.def(py::init<std::vector<classic_svFit::SVfitQuantity*> const&>())
.def("setMeasurement", &classic_svFit::HistogramAdapter::setMeasurement)
.def("setTau1And2P4", &classic_svFit::HistogramAdapter::setTau1And2P4)
.def("registerQuantity", &classic_svFit::HistogramAdapter::registerQuantity)
.def("getQuantity", &classic_svFit::HistogramAdapter::getQuantity)
.def("getNQuantities", &classic_svFit::HistogramAdapter::getNQuantities)
.def("bookHistograms", &classic_svFit::HistogramAdapter::bookHistograms)
.def("writeHistograms", &classic_svFit::HistogramAdapter::writeHistograms)
.def("extractValue", &classic_svFit::HistogramAdapter::extractValue)
.def("extractUncertainty", &classic_svFit::HistogramAdapter::extractUncertainty)
.def("extractLmax", &classic_svFit::HistogramAdapter::extractLmax)
.def("extractValues", &classic_svFit::HistogramAdapter::extractValues)
.def("extractUncertainties", &classic_svFit::HistogramAdapter::extractUncertainties)
.def("extractLmaxima", &classic_svFit::HistogramAdapter::extractLmaxima)
.def("isValidSolution", &classic_svFit::HistogramAdapter::isValidSolution);
py::class_<classic_svFit::DiTauSystemHistogramAdapter, classic_svFit::HistogramAdapter>(m, "DiTauSystemHistogramAdapter")
.def(py::init<std::vector<classic_svFit::SVfitQuantity*> const&>())
.def("getPt", &classic_svFit::DiTauSystemHistogramAdapter::getPt)
.def("getPtErr", &classic_svFit::DiTauSystemHistogramAdapter::getPtErr)
.def("getPtLmax", &classic_svFit::DiTauSystemHistogramAdapter::getPtLmax)
.def("getEta", &classic_svFit::DiTauSystemHistogramAdapter::getEta)
.def("getEtaErr", &classic_svFit::DiTauSystemHistogramAdapter::getEtaErr)
.def("getEtaLmax", &classic_svFit::DiTauSystemHistogramAdapter::getEtaLmax)
.def("getPhi", &classic_svFit::DiTauSystemHistogramAdapter::getPhi)
.def("getPhiErr", &classic_svFit::DiTauSystemHistogramAdapter::getPhiErr)
.def("getPhiLmax", &classic_svFit::DiTauSystemHistogramAdapter::getPhiLmax)
.def("getMass", &classic_svFit::DiTauSystemHistogramAdapter::getMass)
.def("getMassErr", &classic_svFit::DiTauSystemHistogramAdapter::getMassErr)
.def("getMassLmax", &classic_svFit::DiTauSystemHistogramAdapter::getMassLmax)
.def("getTransverseMass", &classic_svFit::DiTauSystemHistogramAdapter::getTransverseMass)
.def("getTransverseMassErr", &classic_svFit::DiTauSystemHistogramAdapter::getTransverseMassErr)
.def("GetSVfitQuantities", [](classic_svFit::DiTauSystemHistogramAdapter& adapter) {
std::vector<const classic_svFit::SVfitQuantity*> svfitQuantities;
for (unsigned int i = 0; i < adapter.getNQuantities(); ++i) {
svfitQuantities.push_back(adapter.getQuantity(i));
}
return svfitQuantities;
})
.def("getTransverseMassLmax", &classic_svFit::DiTauSystemHistogramAdapter::getTransverseMassLmax);
py::class_<classic_svFit::TauTauHistogramAdapter, classic_svFit::DiTauSystemHistogramAdapter, std::unique_ptr<classic_svFit::TauTauHistogramAdapter, py::nodelete>>(m, "TauTauHistogramAdapter")
// py::class_<classic_svFit::TauTauHistogramAdapter, classic_svFit::DiTauSystemHistogramAdapter>(m, "TauTauHistogramAdapter")
.def(py::init<const std::vector<classic_svFit::SVfitQuantity*>&>(), py::arg("quantities") = std::vector<classic_svFit::SVfitQuantity*>())
.def("GetFittedHiggsLV", &classic_svFit::TauTauHistogramAdapter::GetFittedHiggsLV)
// .def("GetFittedTau1LV", &classic_svFit::TauTauHistogramAdapter::GetFittedTau1LV)
.def("GetFittedTau1LV", [](classic_svFit::TauTauHistogramAdapter &self) {
// Access GetFittedTau1LV()
classic_svFit::LorentzVector tau1P4 = self.GetFittedTau1LV();
// Convert to Python TLorentzVector object
py::object tau1P4_py = py::module::import("ROOT").attr("TLorentzVector")(tau1P4.Px(), tau1P4.Py(), tau1P4.Pz(), tau1P4.E());
return tau1P4_py;
})
.def("GetFittedTau2LV", [](classic_svFit::TauTauHistogramAdapter &self) {
// Access GetFittedTau2LV()
classic_svFit::LorentzVector tau2P4 = self.GetFittedTau2LV();
// Convert to Python TLorentzVector object
py::object tau2P4_py = py::module::import("ROOT").attr("TLorentzVector")(tau2P4.Px(), tau2P4.Py(), tau2P4.Pz(), tau2P4.E());
return tau2P4_py;
});
// .def("GetFittedTau2LV", &classic_svFit::TauTauHistogramAdapter::GetFittedTau2LV);
py::class_<classic_svFit::SVfitQuantity, std::unique_ptr<classic_svFit::SVfitQuantity, py::nodelete>>(m, "SVfitQuantity");
py::class_<FastMTT>(m, "FastMTT")
.def(py::init<>())
.def("initialize", &FastMTT::initialize)
.def("run", [](FastMTT &self,
const std::vector<classic_svFit::MeasuredTauLepton>& leptons,
const double& arg1, const double& arg2,
const py::object &cppyy_matrix){
// Check if the provided object is indeed a TMatrixD
if (!cppyy_matrix || !PyObject_HasAttrString(cppyy_matrix.ptr(), "GetNrows")) {
throw std::runtime_error("Invalid input: not a TMatrixD object");
}
// Get the size of the matrix
const int nrows = py::getattr(cppyy_matrix, "GetNrows")().cast<int>();
const int ncols = py::getattr(cppyy_matrix, "GetNcols")().cast<int>();
// Create a TMatrixD with the same size
TMatrixD matrix(nrows, ncols);
// Extract data from the TMatrixD object and fill the TMatrixD
for (int i = 0; i < nrows; ++i) {
for (int j = 0; j < ncols; ++j) {
const double value = cppyy_matrix.attr("__getitem__")(i).attr("__getitem__")(j).cast<double>();
matrix(i, j) = value;
}
}
// Call the run method with the TMatrixD
self.run(leptons, arg1, arg2, matrix);
}, py::arg("leptons"), py::arg("arg1"), py::arg("arg2"), py::arg("cppyy_matrix"))
.def("setLikelihoodParams", [](FastMTT &self, const std::vector<double> ¶ms) {
if (params.size() < 2) {
throw std::runtime_error("Invalid input: expected at least 2 parameters");
}
self.setLikelihoodParams(params);
}, py::arg("params"))
.def("enableComponent", &FastMTT::enableComponent)
.def("disableComponent", &FastMTT::disableComponent)
.def("getBestP4", &FastMTT::getBestP4)
// .def("getTau1P4", &FastMTT::getTau1P4)
.def("getTau1P4", [](FastMTT &self) {
py::object tau1P4_py = py::module::import("ROOT").attr("TLorentzVector")(self.getTau1P4().Px(), self.getTau1P4().Py(), self.getTau1P4().Pz(), self.getTau1P4().E());
return tau1P4_py;
})
// .def("getTau2P4", &FastMTT::getTau2P4)
.def("getTau2P4", [](FastMTT &self) {
py::object tau2P4_py = py::module::import("ROOT").attr("TLorentzVector")(self.getTau2P4().Px(), self.getTau2P4().Py(), self.getTau2P4().Pz(), self.getTau2P4().E());
return tau2P4_py;
})
.def("getBestX", &FastMTT::getBestX)
.def("getBestLikelihood", &FastMTT::getBestLikelihood)
.def("getCpuTime", &FastMTT::getCpuTime)
.def("getRealTime", &FastMTT::getRealTime);
}