Error fitting Histo from a Canvas

Hello everybody,
I have hopefully a very simple problem.

I’ve a simple .root file with only one Histo (inside a Canvas) inside.
If I try to fit the Histo I always get the error:
“illegal pointer to class object his 0x0 1397”

Here is my simple code:

TFile miofile(filename,“read”);
TCanvas can = (TCanvas)miofile.Get(“c1”);
TH1D his;
his = (TH1D
)can->GetPrimitive(“MCA”);
his->Draw();
his->Fit(“gaus”,"","",xmin,xmax);

If I am writing

can->Draw();
his->Fit(“gaus”,"","",xmin,xmax);
the compiler complains as well about “his”.

If I am writing only

can->Draw();

without his->fit() everything is fine.
How can I get access to the histo?

Try:
can->GetListOfPrimitives()->ls()
can->GetListOfPrimitives()->Print()
and see if your “c1” canvas really contains a histogram (TH1) with the name “MCA”.
(Note: if it exists, make sure it’s a TH1D, and not a TH1F, for example.)

This gives:

Collection name=‘TList’, class=‘TList’, size=2
Info in TPad::Print: ps file pad1.ps has been created
TPaveText X1=0.030000 Y1=0.965000 X2=0.980000 Y2=0.990000 FillColor=19 FillStyle=1001
Collection name=‘TList’, class=‘TList’, size=1
Text X=0.000000 Y=0.000000 Text= N957 MCA 15.10.2012 Font=0 Size=0.000000 Color=0 Align=0

By klicking on the kurve the TBrowser tells me, that there is a histo called MCA.
test.root (15.5 KB)

TFile miofile(“test.root”);
TCanvas can = (TCanvas)miofile.Get(“c1”);
TH1D his = ((TH1D)(can->FindObject(“MCA”)));
Double_t xmin = his->GetBinCenter(1);
Double_t xmax = his->GetBinCenter(his->GetNbinsX());
his->Fit(“gaus”, “”, “”, xmin, xmax);
can->Draw();

TFile miofile(“test.root”);
TCanvas can = (TCanvas)miofile.Get(“c1”);
((TPad *)can->FindObject(“pad1”))->GetListOfPrimitives()->ls();
((TPad *)can->FindObject(“pad1”))->GetListOfPrimitives()->Print();
TH1D his = ((TH1D)(((TPad *)can->FindObject(“pad1”))->GetPrimitive(“MCA”)));
Double_t xmin = his->GetBinCenter(1);
Double_t xmax = his->GetBinCenter(his->GetNbinsX());
his->Fit(“gaus”, “”, “”, xmin, xmax);
can->Draw();

Thank you very very much!
can->FindObject(“MCA”) was the trick. :smiley: