Home | News | Documentation | Download

Setting Histo1D in pyROOT

Dear ROOT Community,
I have a simple question: I wish to fill histograms using RDataFrame, whilst setting their binning, title etc. The code below works well:

ROOT.ROOT.EnableImplicitMT()

h_tree = ROOT.RDataFrame("OutTree", "/home/rbrener/Analysis/Zprimebb/TRUTH3_Hists/bb_Zprime_mumu.root")

h_pt_mu = h_tree.Histo1D("mu_pt")
myfile = TFile('OutFile_OnlyHistos.root', 'RECREATE' )
h_pt_mu.Write()
myfile.Close()

However I want to set the binning of h_pt_mu. I’ve tried different combinations of arguments, e.g.
h_pt_mu = h_tree.Histo1D({"h_mu_pt", "h_mu_pt", 100, 0, 1e3}, "mu_pt")
but none work.
This should be possible I just can’t find the correct syntax.
Any help would be highly appreciated!


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.22/06
Platform: Linux
_Compiler:_Python3


Hi,

You can pass a tuple to Histo1D with the arguments to construct the model histogram:

 h_pt_mu = h_tree.Histo1D(("h_mu_pt", "h_mu_pt", 100, 0, 1e3), "mu_pt")

Thanks so much! This works. Whilst we’re there, may I ask: is there a way to pass the information from h_tree to a variable and only then save that variable into h_pt_mu (keeping the latter a Histo1D)? This is in order to apply some conditions on that variable and combine it with others, before saving to histograms, whilst maintaining the advantage of RDataFrame and ImplicitMT().

This is in order to apply some conditions on that variable and combine it with others, before saving to histograms, whilst maintaining the advantage of RDataFrame and ImplicitMT().

You mean applying Filter and Define transformations?

Yes, I think. A simple example would be e.g. storing in the Histo1D, h_tree, entries from mu_pt only if those entries are larger than 50.

Yes, please have a look at the Filter operation of RDataFrame:

https://root.cern/doc/master/classROOT_1_1RDF_1_1RInterface.html#a70284a3bedc72b19610aaa91b5007ebd

If you also need to define new columns (e.g. by combining existing columns), you can use Define:

https://root.cern/doc/master/classROOT_1_1RDF_1_1RInterface.html#a7d48eb23b4378e99ebccb35e94ad025a

Your example would be: df.Define("good_pts", "mu_pt[mu_pt > 50]").Histo1D("good_pts"). Also check ROOT: ROOT::RDataFrame Class Reference (the “Working with collections” section).

Cheers,
Enrico