Trouble Creating a Ratioplot from RDataFrame Histograms in PyROOT


Please read tips for efficient and successful posting and posting code

_ROOT Version:_6.27/01
_Platform:_Ubuntu
Compiler: PyROOT (Python 3)


Hello,

I have created two histograms from RDataFrame and would like to create a ratioplot from them both. I am using the following code:

c1 = ROOT.TCanvas()

hist = df.Histo1D(("Ditau_m", "ditau_m", 100, 0, 1000),"ditau_m")

hist1 = df.Histo1D(("Ditau_m_mmc", "ditau_m_mmc", 100, 0, 1000),"ditau_m_mmc")

hist.Scale(hist.GetXaxis().GetBinWidth(1) / hist.Integral(), "width")

hist1.Scale(hist1.GetXaxis().GetBinWidth(1) / hist1.Integral(), "width")

hist.SetLineColor(ROOT.kRed)

hist1.SetLineColor(ROOT.kBlue)

hist1.Draw("HIST")

hist.Draw("SAME HIST")

rp = ROOT.TRatioPlot(hist1, hist)

rp.Draw()

c1.Update()

and getting the following error message

Traceback (most recent call last):
  File "/home/advait/Documents/internship_nn/data/ditauMassWeighted.py", line 80, in <module>
    rp = ROOT.TRatioPlot(hist1, hist)
TypeError: none of the 4 overloaded methods succeeded. Full details:
  TRatioPlot::TRatioPlot(TH1* h1, TH1* h2, const char* option = "pois") =>
    TypeError: could not convert argument 1
  TRatioPlot::TRatioPlot(THStack* st, TH1* h2, const char* option = "pois") =>
    TypeError: could not convert argument 1
  TRatioPlot::TRatioPlot(TH1* h1, const char* option = "", TFitResult* fitres = nullptr) =>
    TypeError: could not convert argument 1
  TRatioPlot::TRatioPlot() =>
    TypeError: takes at most 0 arguments (2 given)

I do not see what I am doing wrong. I see that the problem has something to do with me passing the wrong parameters, but I don’t see what I’m passing incorrectly. Any tips would be appreciated.
Kind Regards,
Advait

Hi @AdvaitDhingra ,

as per the error message, PyROOT could not convert hist1 and hist into TH1*. That’s because the return type of Histo1D etc. is not TH1D but RResultPtr<TH1D>. To get access to the underlying TH1D, in order to pass it to TRationPlot’s constructor, you have to call hist1.Get() and hist.Get():

ROOT.TRatioPlot(hist1.Get(), hist.Get())

Something like the code above should do the job :slight_smile:

Cheers,
Enrico

Hi Enrico,

thanks for your response. When I implement this, I get

Traceback (most recent call last):
  File "/home/advait/Documents/internship_nn/data/ditauMassWeighted.py", line 78, in <module>
    rp = ROOT.TRatioPlot(hist1.Get(), hist.Get())
AttributeError: 'TH1D' object has no attribute 'Get'. Did you mean: 'Set'?

Is there any other way to convert this result ptr to a normal th1d?

Hi,

my bad, the method is called GetPtr, not Get. You can trust the reference guide better than you can trust me :smiley: ROOT: ROOT::RDF::RResultPtr< T > Class Template Reference

Cheers,
Enrico

1 Like

Ah, now it works. Thank you :smiley:

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