Import histogram from root file

Hi,

I have a root file, compiled by the simulation program Garfield++, which contains an TH1D histogram. However, when I view the content of the root file by File.Ls(), it returns:

TFile**		data/SingleGEM/time1.root	
 TFile*		data/SingleGEM/time1.root	
  KEY: TCanvas	c1;1	Signal

From the last line, it seems the histogram is saved as a Canvas object instead of an TH1D histogram.

Now I want to change this histogram (change title, axis, …), thus I load the histogram via:

TH1D *h1 = (TH1D*)File->Get("c1");

When I apply the histogram functions (h1->GetXaxis()->SetTitle(“test”), h1->GetEntries(), …), ROOT crashes (*** Break *** segmentation violation). It seems these functions does not apply on this type of histogram, loaded from the root file.

The problem is probably because the histogram is saved as a Canvas object. But is it possible to extract the histogram out of this canvas, and then apply the histogram functions on it?

Thanks,
Jan

1 Like

Hi Jan,

[quote=“Jan Eysermans”]TH1D h1 = (TH1D)File->Get(“c1”);[/quote]You cannot cast from a TCanvas class to a TH1D class (they are unrelated types). One way is to get the histogram from the canvas:

TCanvas *c1 = (TCanvas *)File->Get("c1"); TH1 *h1= (TH1*)c1->FindObject("histname");
Cheers, Bertrand.

Hi Bertrand,

Thanks a lot, this solution worked!