I wonder why the plot will not show unless I uncomment the line with TFile. The plot does not depend on the TFile anyway. I am using ROOT 6.20/04 installed using conda.
Thanks for the reply! Did you uncomment the line with TFile? I don’t understand why there is only an empty plot shown when I uncomment the TFile line. The file amps.root contains some histograms. canTest->Draw() does not help.
I am using ubuntu 18.04LTS.
Another strange point is that if I put everything into {...} rather than define a named function demo_plot, the plot will show.
Yes, I did. But what I forgot/oversaw is that it complains that the file doesn’t exist!
After creating one, I can reproduce. Will investigate!
Update: RooPlot will soon react to TH1::AddDirectory().
To explain why you don’t see the plot:
It gets attached to the file if a file is open. (That’s apparently happening so the plot automatically ends up in the file if its writable, ancient ROOT behaviour that we cannot change without breaking user code.)
When the function exits, the file gets closed, and the plot gets deleted. Therefore, it’s not visible, any more. If you make the file a pointer and let it leak, the plot also survives …
Actually, I am not really sure that RooPlot should react to TH1::AddDirectory at all.
It has a dedicated RooPlot::setAddDirectoryStatus method.
Maybe the right thing to do would be to make sure that, when the file is closed, the whole RooPlot is deleted, not just its contents (then it would be clear what happened).
Yes, you are right. I forgot about the created “canTest”. So, maybe there is no bug at all.
It’s just that the user must make sure that the created RooPlot is not “owned” by some opened file.
On the other hand, this behavior is counterintuitive. Standard ROOT canvases / pads / frames are not “owned” by files, so TH1::AddDirectory(kFALSE); should be sufficient to get their contents (with newly created histograms) intact.
Even more confusing is the fact that there is no “SetDirectory” method in RooPlot (so, e.g. one cannot change the “owner”).
Also the name “setAddDirectoryStatus” is much different from the default usual “AddDirectory”.