How to exchange complex parameters between C++ and Python?

Here is my story. I need to loop over a tree and do some filter. The parameters are some kind complex. So I store them in a JSON file, something like this:

[
    {
	"h_idx": 12,
	"bin2e": [-7.70332, 7.78929e-02],
	"nBinsX": 4096,
	"hrange": [200,3700],
	"erange": [8.1, 280.5],
	"t_gp": [],
	"t_bkg": [],
       ...
    },
    {
	"h_idx": 13,
	"bin2e": [ -9.16058,  7.94685e-02],
	"nBinsX": 4096,
	"hrange": [200,3700],
	"erange": [],
	"t_gp": [],
	"t_bkg": [],
       ...
    },
 ...

In python, it is really charming to read the JSON:

import simplejson as sj
pars = sj.loads("".join(open("mypars.json").readlines()))

But the same goal is too sophisticated in C++. So, I want to use Python to read parameters, then get the parameters through TPython interface in C++.
First, I defined a struct in C++, and loaded it by “ROOT.gROOT.LoadMacro”. Then I read parameters and assigned them to C++ struct. But I failed in delivering them to C++ Class.
I tried an alternative way. I tried to invoke TPython::Eval in C++ to implement such a goal. Failed again.
:frowning:

Could anyone give me an example to do so?

1 Like

Hi,

I’ll assume that the problem of converting the python object into a C++ one is in the lists? If so, the easiest way to convert those is by declaring the equivalent std::vector and using +=; something equivalent to this:

from ROOT import std v = std.vector('double')() l = [ 1., 2., 3., 4. ] v += l
Cheers,
Wim