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:

[code]{
TFile *f = new TFile(“myfile.root”);

f.ls();

TH1F * h1 = new TH1F(“h1”,“h1 title” , 100, 0, 4);
h1 = (TH1F*)f.Get(hist1);
h1->Draw();
}[/code]

with the comand f.ls 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/libRIO.so -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
Ciccio

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

Instead of

[code]{
TFile *f = new TFile(“myfile.root”);

f.ls();

TH1F * h1 = new TH1F(“h1”,“h1 title” , 100, 0, 4);
h1 = (TH1F*)f.Get(hist1);
h1->Draw();
}[/code]
do

[code]{
TFile *f = new TFile(“myfile.root”);

f.ls();

TH1F * h1 = (TH1F*)f.Get(“h1”);
h1->Draw();
}[/code]

Rene

2 Likes

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

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;
   }
 }
 fout.close();
}
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.)

e.g.:

$> 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)
BEGIN YODA_HISTO1D /h1
Path=/h1
Title=h1
Type=Histo1D
# 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
END YODA_HISTO1D

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

root-dump can be directly retrieved from:

or from sources:

$> go get go-hep.org/x/hep/groot/cmd/root-dump

hth,
-s