28 bool py_to_cpp (
PyObject* candidates,
29 std::vector<McUtils::Strings>& parsed);
61 std::cout <<
"--- Parents ---" << std::endl;
64 std::cout <<
"--- Children ---" << std::endl;
72 std::stringstream( pdgIdString ) >> iPDG;
82 if ( inputCmd.empty() ) {
98 std::string
error =
"problem while parsing command [" + inputCmd +
"]";
99 throw std::runtime_error (
error);
102 if (!PyTuple_Check (
res)) {
104 std::string
error =
"expected a python tuple";
105 throw std::runtime_error (
error);
108 if (PyTuple_GET_SIZE (
res) != 3) {
110 std::string
error =
"expected a python tuple of size 3";
111 throw std::runtime_error (
error);
116 if (!
sc || !PyLong_Check (
sc)) {
119 std::string
error =
"corrupted return code";
120 throw std::runtime_error (
error);
123 Py_ssize_t status = PyLong_AsSsize_t (
sc);
127 std::string
error =
"failed to parse command ["+inputCmd+
"]";
128 throw std::runtime_error (
error);
133 Py_XINCREF (parents);
136 std::string
error =
"corrupted parents' list";
137 throw std::runtime_error (
error);
141 Py_XINCREF (children);
145 std::string
error =
"corrupted children' list";
146 throw std::runtime_error (
error);
150 if (parents==Py_None && children==Py_None) {
154 Py_DECREF (children);
160 Py_DECREF (children);
161 std::string
error =
"could not translate parents' list";
162 throw std::runtime_error (
error);
167 Py_DECREF (children);
168 std::string
error =
"could not translate children' list";
169 throw std::runtime_error (
error);
184 unsigned int iSlot = 0;
185 for( std::vector<McUtils::Strings>::const_iterator itr = list.begin();
188 std::stringstream iSlotStr;
190 const McUtils::Strings::const_iterator candEnd = itr->end();
191 std::cout <<
"slot #" << iSlotStr.str() <<
": candidates= [ ";
192 for( McUtils::Strings::const_iterator candidate = itr->begin();
193 candidate != candEnd;
195 std::cout << *candidate;
196 if ( candidate+1 != candEnd ) {
200 std::cout <<
" ]" << std::endl;
212 if (
this != &rhs ) {
225 if (!Py_IsInitialized()) {
229 const std::string
n =
"McParticleUtils.DecayParser";
230 PyObject *
module = PyImport_ImportModule (
const_cast<char*
>(
n.c_str()));
231 if (!module || !PyModule_Check (module)) {
233 std::string
error =
"could not import module ["+
n+
"]";
234 throw std::runtime_error (
error);
237 const std::string
fct_name =
"py_parse";
238 PyObject *
fct = PyDict_GetItemString (PyModule_GetDict (module),
239 const_cast<char*
>(
fct_name.c_str()));
245 if (!fct || !PyFunction_Check (fct)) {
246 std::string
error =
"could not get '"+
fct_name+
"' from module ["+
n+
"] or not a function";
247 throw std::runtime_error (
error);
255 std::vector<McUtils::Strings>& parsed)
257 bool all_good =
true;
258 if (candidates==Py_None) {
263 if (!PySequence_Check (candidates)) {
266 Py_ssize_t isz = PySequence_Size (candidates);
272 for (Py_ssize_t i = 0;
i!=isz; ++
i) {
273 PyObject *cand = PySequence_GetItem(candidates, i);
277 if (!PySequence_Check (cand)) {
281 Py_ssize_t jsz = PySequence_Size (cand);
289 for (Py_ssize_t j = 0; j!=jsz; ++j) {
290 PyObject *pdgid = PySequence_GetItem(cand, j);
std::pair< std::vector< unsigned int >, bool > res
Convert python string -> C++ string for py2 and py3.
DecayParser()
Default constructor:
void printMcUtilsStrings(const std::vector< McUtils::Strings > &list) const
Print the content of a vector of McUtils::Strings to std::cout.
void dump() const
Const methods:
virtual ~DecayParser()
Destructor:
DecayParser(const std::string &cmd)
Constructor with parameters:
std::vector< McUtils::Strings > m_parents
List of parents : each slot of the vector is a list of candidates So one could have something like : ...
void parse(const std::string &cmd)
Non-const methods:
int pdgId(const std::string &pdgIdString) const
DecayParser & operator=(const DecayParser &obj)
Assignment operator:
PyObject * m_parseFct
python function to parse the input string modeling the decay pattern to look for.
std::vector< McUtils::Strings > m_children
List of children : each slot of the vector is a list of candidates So one could have something like :...
std::pair< std::string, bool > PyGetString(PyObject *s)
Convert python string -> C++ string for py2 and py3.