Empty canvas when creating TFile on stack

I think this question was already answered here: Empty canvas, but I am curious as to what is happening under the hood.

When creating a ROOT macro which opens a TFile on the stack, ROOT is unable to draw a histogram:

void foo(void)
{
    TFile f("some_file.root");
    TH1D *h = new TH1D("h","blah",100,0,100);
    h->Fill(1.0);
    h->Draw();
}

produces an empty canvas whereas

void foo(void)
{
    TFile *f = new TFile("some_file.root");
    TH1D *h = new TH1D("h","blah",100,0,100);
    h->Fill(1.0);
    h->Draw();
}

works.

Is the TH1D somehow being associated with the TFile and then being deleted when it goes out of scope?

Thanks,

Tony

https://root.cern/root/htmldoc/guides/users-guide/ObjectOwnership.html

Thanks for the link. For future readers, the documentation above doesn’t describe directly why a TFile affects the macro, but at ROOT files - ROOT it mentions that when you create a TFile it becomes the current directory, and then the rules stated in the above links show that a new histogram is now owned by this directory. Therefore, when the TFile object goes out of scope, it deletes the histogram.

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