Reading Histogram from root file


Hello,
I am trying to read a histogram from a root file. Using lines

TFile *f = new TFile("ht.root");
gDirectory->ls();
TH1F *clust_chip_0 = (TH1F*) f->Get("clust_Chip_0");
 clust_chip_0->Draw();

The list shows that the histogram is there

 KEY: TH1F	 clust_Chip_0;1	 clust_Chip_0

But I do not get anything instead an error and a warning

Error in <HandleInterpreterException>: Trying to dereference null pointer or trying to call routine taking non-null arguments.
Execution of your code was aborted.
In file included from input_line_10:1:
warning: null passed to a callee that requires a non-null argument [-Wnonnull]
                 clust_chip_0->Draw();
                 ^~~~~~~~~~~~

Please read tips for efficient and successful posting and posting code

ROOT Version: 6.13/01
Platform: Ubuntu 16.04
Compiler: Not Provided


Qasim

Hi,
since your histogram seem does not exist, can you try in the interactive session to check what is the output
of f->Get("clust_Chip_0") .
Because if the output is nullptr the next error is perfectly fine and the problem could be in how the histogram has been saved in the file.

Cheers,
S

Try:

TH1F *clust_chip_0; gDirectory->GetObject("clust_Chip_0", clust_chip_0);

and if it doesn’t help, attach your “ht.root” file for inspection.

[quote=“Wile_E_Coyote, post:3, topic:38006”]
TH1F *clust_chip_0; gDirectory->GetObject(“clust_Chip_0”, clust_chip_0);
[/quote]Thanks for the suggestion
I have tried it and I got same response.
ht.root file is attached for inspectionht.root (37.4 KB)

You have an extra space character in the beginning of all histrogram names, e.g. " clust_Chip_0" (you’d better fix the procedure which creates all these histograms).

Hi,
Thanks for pointing out the mistake and I have fixed it.
Now I can read the histogram and its values but still not able to draw.
the " ```
clust_chip_0->Draw();
gives an empty canvas

It works as expected for me:

$ root.exe -l 
root [0] TFile *f = TFile::Open("/Users/pcanal/Downloads/ht.root")
(TFile *) 0x7ff71bc2a670
root [1] TH1F *clust_chip_0 = (TH1F*) f->Get(" clust_Chip_0")
(TH1F *) 0x7ff71bd79d40
root [2] clust_chip_0->GetEntries()
(double) 305.00000
root [3] clust_chip_0->Draw()
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1

and the canvas has some non-empty bins.

Hi pcanal,
thank you for your response.
I was actually using f->close() which was clearing the canvas.

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