2D Histogram not drawing

Hi,
I’m new to root and need some help understanding why my code wont produce a histogram. I have a data set that I want to fill a 2D histogram with. Where invariant mass (Ws) is on the x-axis and total number of hadrons on the y-axis which are denoted as ‘AA’ and ‘total’ in my code.

void run(){
    TFile f("neutron.gst.root");   
    TH2F *h1 = new TH2F("h1","title",100,0,100,100,0,100);
    TTree* k;  
   
    f.GetObject("gst",k);

    Double_t AA; 
    Int_t BB, CC, DD, EE, FF;

k->SetBranchAddress("Ws",&AA);
k->SetBranchAddress("nfn",&BB);
k->SetBranchAddress("nfpip",&CC);
k->SetBranchAddress("nfpim",&DD);
k->SetBranchAddress("nfkp",&EE);
k->SetBranchAddress("nfkm",&FF);

Int_t NEntries = k->GetEntries();
cout << nm << endl;
 
 for(Int_t i=0; i<NEntries; ++i){
  
   k->GetEntry(i);
   Double_t total = BB+CC+DD+FF+EE;
   cout << AA <<"\t"<< BB << "\t" << CC << "\t" << DD << "\t" << EE << "\t"  << FF<<  "\t" << "total: " << total <<  endl;
   
   h1->Fill(AA,total);
   
 }
    TCanvas* canvas = new TCanvas("canvas", "canvas1", 600, 900);
    h1->Draw();
}

My issue is that everything compiles and the numbers the console reads out seem good, but the histogram isn’t showing up - its just the blank canvas.
Any suggestions?

Either create the “h1” histogram BEFORE you open the “f” file or add h1->SetDirectory(gROOT); or use h1->DrawCopy();

BTW. When you post “source code” or “output” here, do remember to enclose them into two lines which contain just three characters ``` (see how your post has been edited above).

1 Like

Thanks - It worked.
Okay, thanks for letting me know as I’m sure I’ll be using this forum more often. :slight_smile: