Empty canvas after plotting


Hello all,

I have a code reading a ROOT file, and plotting a 2d histogram with hits. The code works well, and I can save the histograms. But I cannot seem to visualize the graphs. The canvas always pops up empty.

I’m sure I’m missing something basic, but cannot find out the issue. Any help would be more than welcome :slight_smile: Thank you in advance!

#include <TROOT.h>
#include <TH2D.h>
#include <TMath.h>
#include <TStyle.h>
#include <TCanvas.h>
#include <TSystem.h>
#include <ROOT/RDataFrame.hxx>

#include "tools.hxx"
#include "style_config.C"

TCanvas* doseCanvas = nullptr;
TCanvas* electronEkin = nullptr;
TCanvas* gammaEkin = nullptr;

void data_analysis()
{
    ROOT::EnableImplicitMT();

    Int_t Nx = 20;
    Int_t Ny = 20;
    Int_t NoEvents = 500000;

    TString file = "Data/Sr90_34cm.root";

    // Dose data
    ROOT::RDataFrame doseData("tiles", file);

    auto df2 = doseData.Define("ix", "(int)(copyNo/20)")
                       .Define("iy", "(int)(copyNo%20)");

    auto h2 = df2.Histo2D({"dose2d", "Total dose per tile;ix;iy",
                          Nx, -0.5, Nx-0.5, Ny, -0.5, Ny-0.5 },
                          "ix", "iy", "dose");

    gROOT->SetBatch(kFALSE);
    
    gStyle->SetOptStat(0);
    gStyle->SetNumberContours(255);

    doseCanvas = new TCanvas("doseCanvas", "Dose Map", 900, 720);
    doseCanvas->SetRightMargin(0.16);
    doseCanvas->cd();

    h2->GetZaxis()->SetTitle("Dose [krad]");
    h2->Draw("COLZ");

    doseCanvas->Modified();
    doseCanvas->Update();
}

ROOT Version: 6.34
Platform: Ubuntu 24.04, Conda environment
Compiler: GCC


We cannot run your example without the root file. But in any case, can you explain a bit more? Is this a full reproducer? You have other canvas pointers not used, so you may be doing other things we don’t see here. How do you run this? And how are you “saving the histograms”? Are you saying that when you run this code (how?) the canvas window is shown but is empty, and if you save that empty-looking canvas (how?) you do get what you expect?

1 Like

Try with h2->DrawCopy("COLZ");. I think that the histogram is drawn but immediately removed by going out of scope at the end of the function. With DrawCopy(), you send a copy to the canvas (which stays alive because it is a raw pointer).

1 Like