RDataFrame Histo1D is not drawn

TestTree.root (7.1 KB)

Hello,

I have a strange problem in the RDataFrame Histo1D objects.
If I use the simple command as below in the ROOT interpreter, it works fine and I can see the well-drawn histogram.
However, the histogram is not drawn (only empty canvas appears) if I compile as a macro file and execute it (.x RDataTest.C)

// void RDataTest(){

ROOT::RDataFrame Data0("TreeMaster", "/home/cho/Desktop/TestTree.root");

  auto h = Data0.Histo1D<float>({"h", "h", 1000u, -500, 500}, "FPMW0_X");
  h->Draw();
// }

Can I know why such problem occurs?

Thanks,

Youngju

_ROOT Version: 6.24.06 / 6.16.00
_Platform: Centos7 / Ubuntu 18.04LTS
_Compiler: gcc 4.8.5 / gcc 7.5.0


Why did you comment the first and last line ?
Your macro should be:

void RDataTest(){
   ROOT::RDataFrame Data0("TreeMaster", "/home/cho/Desktop/TestTree.root");
   auto h = Data0.Histo1D<float>({"h", "h", 1000u, -500, 500}, "FPMW0_X");
   h->Draw();
}

Hello Couet,

I comment them to imply that I used the code in the interpreter directly.
Of course I used the uncomment macro as you wrote when I compile it.

Thanks,
Youngju

1 Like

Hi @Youngju_Cho ,
histograms returned by RDataFrame are owned by the user. In particular, the h object goes out of scope at the end of the function, and brings the histogram with it. This is different from the behavior of older ROOT interfaces that return TH1D* pointers where the histograms are owned by the ROOT memory management system (and they typically stay around until the end of the program).

A simple workaround is to call DrawClone instead of Draw. Alternatively you can restructure your code in a way such that the histogram does not get destroyed (e.g. you can return it from the function and assign it to a variable).

This is a common hurdle when first starting to use RDataFrame, there are a few similar posts on the forum. Hopefully things will actually be better than before when you get used to the new behavior :slight_smile: We think it’s more in line with modern C++ best practices.

Cheers,
Enrico

Hello eguiraud,

DrawClone() works well. Now I understand why the code works well when I use it directly in the ROOT interpreter.
Surely RDataFrame is somewhat unfamiliar, but I want to try it because of multi core processing.
Thank you very much for your kind explanation.

Youngju Cho

Great! I hope it will work well for you after a short adaptation phase :grinning_face_with_smiling_eyes: If not, we are here!

Cheers,
Enrico

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