Saving python dictionary in root file


_ROOT Version: 6.14
_PYTHON Version : 3.6.7
_Platform: Debian 9
_Compiler: GCC 7.3.0


Hello, I created a script that saves various fit data into a nested python dictionary, e.g.

d[1] = { "p1" : parameter1, "p2" : parameter2, "p3" : parameter3, "p4" : parameter4} d[2] = { "p1" : parameter1, "p2" : parameter2, "p3" : parameter3, "p5" : parameter5}
The identifier 1 belongs to a specific run_file which is part of a parameter scan, so does the identifier 2.

I would like to store the values. Preferably the way to store the values is as a table (something like an sql table) from which I can plot different parameters against each other in an easy way.

How can I best do this when storing the parameters with identifiers in a root file?
Note that sometimes, I do not have a parameter4 but a parameter 5 instead and vice versa.

Thanks for the help!

Hi @iMi

You need to create a ROOT TTree whose branches are “p1”, “p2”, etc, when you would store that TTree in a ROOT file. Simplifying a bit, you can think of a TTree as a table.

If your parameters are just floats, you can use a simplified version of a TTree that is called TNtuple. This is an example of how to fill a TNtuple and write it to a file in Python:

Alternatively, you can also use a more high-level interface that is called RDataFrame. See for instance here:

how a new RDataFrame is created with 10 entries and new columns are defined for it (your columns would be the parameters).

Hi, alternative you can use pickle module to save to a pkl file.

Thanks, the pickle module worked fine with me. It’s so easy.

Thanks for pointing these two methods out to me. I have tried the examples, but decided to use the python pickle module (see below) for now. Will be interesting for me to learn a bit more about the RDataFrame when time allows.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.