RDataFrame with TLegend problem


_ROOT Version:6-16-01
_Platform:MacOS
Compiler: Not Provided


Hi all:
I have a problem like below

RDataFrame aData("aa","aa.root");
TH1D * h1 = new TH1D("","",10,0,1);
auto hist0 = aData.Histo1D({*h1},"aa");
auto hist1 = aData.Histo1D({*h1},"bb");
hist0->SetLineColor(kRed);
hist1->SetLineColor(kBlack);
hist0->DrawCopy();
TLegend * l1 = new Legend(0.5,0.5,0.6,0.6);
l1->AddEntry(hist0.GetPtr(), "on", "lp");
l1->Draw("same");

hist Draw with red color
but the Legend in hist not Draw in Red?

how to solve?

Can you try to use the version of AddEntry “by name” ?

Hi,
the results of RDataFrame go out of scope (“die”) at the end of your function.
This is why you need hist0->DrawCopy() instead of just Draw(): you need to make a copy of that histogram that is registered with ROOT and draw that, because the copy does not die at the end of the function.

Now to your problem: your legend l1 will still need a valid histogram to check what color its line is, even after the function has exited. So you should not pass hist0 (which will die) but its copy:

auto newHistRegisteredWithROOT = hist0->DrawCopy();
...
l1->AddEntry(newHistRegisteredWithROOT, "on", "lp");

Hope this helps!
Enrico

OK thank you:grinning:

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