Can't see ProjectionYs of TH2F after Drawn on TCanvas. Bug or Feature?

Dear experts,
I’m having a problem with projections of a TH2. On the TCanvas shown by root I only see blank spaces (see attachments). In the .root file in a TBrowser they are there as they should be. Is this a bug or am I doing sth wrong?
Thanks a lot in advance!

#include "TH1F.h"
#include "TH2F.h"
#include "TCanvas.h"
#include "TFile.h"

void testDraw(){
    
    TH2* h2 = new TH2F("t","t",2, 0., 1., 2, 0., 2);
    h2->Fill(.1, .1);
    
    TFile outfile("resolutions.root", "recreate");
    h2->Write();
    
    TCanvas *cR = new TCanvas("cR", "cR", 2000, 1000);
    cR->Divide(2,1);
    for (int i=1; i<=2; ++i){
        TH1* h1 = h2->ProjectionY(Form("h1_%d", i), i, i);
        h1->SetTitle(h1->GetName());
        h1->Write();
        cR->cd(i);
        h1->Draw();
    }
    TCanvas *c2 = new TCanvas("c2", "c2", 2000, 1000);
    h2->Draw("colz");
}
outfile.Close();

ROOT Version: 6.28/04
Platform: macosx64 (13.6.4)
Compiler: Homebrew clang version 15.0.7



Hi,

Thanks for your post.
It’s an ownership matter. By closing the file, you are removing the objects which are drawn.

I hope this helps.

Cheers,
Danilo

Hi Danilo,
thanks, it helped. Not closing alone doesnt help (because outfile lives on the stack) but creating outfile on the heap and not closing it indeed solved the problem. Thanks!

Glad to hear you are unblocked!

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