Read a histogram from a ROOT file

Hi all,
I have a root file with inside histograms, now I would like to open this file and to see an histogram, that I know the name of this histogram in the file.
I red the “HowTo’s”, but I alway obtain an error.
My code is:

TFile *f = new TFile(“myfile.root”);;

TH1F * h1 = new TH1F(“h1”,“h1 title” , 100, 0, 4);
h1 = (TH1F*)f.Get(hist1);

with the comand I obtain this:

KEY: TH1F     hist1;1   Hist1
KEY: TH1F     hist2;1   Hist2
KEY: TH1F     hist3;1   Hist3
KEY: TH1F     hist4;1   Hist4

and I would like to see the histogram hist1.
And I have the error:

Error: Can't call TFile::Get(hist1) in current scope test.c:7: Possible candidates are... (in TFile) (in TDirectoryFile) /usr/local/lib/root/ -1:-1 0 public: virtual TObject* TDirectoryFile::Get(const char* namecycle); public: virtual TObject* TDirectory::Get(const char* namecycle); *** Interpreter error recovered ***

where is my error?
thank you

your missing the " " around the name to indicate that its a string:
h1 = (TH1F*)f.Get(“hist1”);

Instead of

TFile *f = new TFile(“myfile.root”);;

TH1F * h1 = new TH1F(“h1”,“h1 title” , 100, 0, 4);
h1 = (TH1F*)f.Get(hist1);

TFile *f = new TFile(“myfile.root”);;

TH1F * h1 = (TH1F*)f.Get(“h1”);



thank you at all, it’s work.
Thank you

I’ve a histogram and I want to extract this information: values of x-axis and their corresponding values or y-axis then I write them as two columns in text file; how can I do that?

1D or 2D ?

I have this problem too, 1D. Could you help, please?

You can use:

virtual Double_t GetBinCenter (Int_t bin) const

And see also all the possible “getters” here.

// save the following code to out.C and run .x out.C in ROOT environment
void out()
  TFile *f=new TFile("myROOT.root");//includes TH2F *myHist2;
  TH2F *h=(TH2F*)f->Get("myHist2");
  ofstream fout("output.txt");
  for(int i=1;i<=h->GetNbinsX();i++) { 
   for(int j=1;j<=h->GetNbinsY();j++) {
     fout<<h->GetXaxis()->GetBinCenter(i)<<" "<<h->GetYaxis()->GetBinCenter(j)<<" "<<h->GetBinContent(i,j)<<endl;
1 Like

if none of the already provided answers work for you, you could use root-dump (a Go-based program that dumps on screen the content of ROOT files.)


$> root-ls ./histos.root
=== [./histos.root] ===
version: 60806
TH1D    h1      h1      (cycle=1)
TH2D    h2      h2      (cycle=1)

$> root-dump -name=h1 ./histos.root
>>> file[../../testdata/histos.root]
key[000]: h1;1 "h1" (TH1D)
# Mean: 8.632380e-03
# Area: 1.000000e+04
# ID	 ID	 sumw	 sumw2	 sumwx	 sumwx2	 numEntries
Total   	Total   	1.000000e+04	1.000000e+04	8.632380e+01	1.115446e+04	10002
Underflow	Underflow	4.500000e+01	2.025000e+03	0.000000e+00	0.000000e+00	1
Overflow	Overflow	1.000000e+00	1.000000e+00	0.000000e+00	0.000000e+00	1
# xlow	 xhigh	 sumw	 sumw2	 sumwx	 sumwx2	 numEntries
-4.000000e+00	-3.200000e+00	5.000000e+00	5.000000e+00	0.000000e+00	0.000000e+00	5
-3.200000e+00	-2.400000e+00	7.500000e+01	7.500000e+01	0.000000e+00	0.000000e+00	75
-2.400000e+00	-1.600000e+00	4.560000e+02	4.560000e+02	0.000000e+00	0.000000e+00	456
-1.600000e+00	-8.000000e-01	1.563000e+03	1.563000e+03	0.000000e+00	0.000000e+00	1563
-8.000000e-01	2.220446e-16	2.885000e+03	2.885000e+03	0.000000e+00	0.000000e+00	2885
0.000000e+00	8.000000e-01	2.890000e+03	2.890000e+03	0.000000e+00	0.000000e+00	2890
8.000000e-01	1.600000e+00	1.550000e+03	1.550000e+03	0.000000e+00	0.000000e+00	1550
1.600000e+00	2.400000e+00	4.850000e+02	4.850000e+02	0.000000e+00	0.000000e+00	485
2.400000e+00	3.200000e+00	8.100000e+01	8.100000e+01	0.000000e+00	0.000000e+00	81
3.200000e+00	4.000000e+00	1.000000e+01	1.000000e+01	0.000000e+00	0.000000e+00	10

key[001]: h2;1 "h2" (TH2D)

root-dump can be directly retrieved from:

or from sources:

$> go get