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();
// }
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 We think it’s more in line with modern C++ best practices.
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.