# Error fitting Histo from a Canvas

**URL:** <https://root-forum.cern.ch/t/error-fitting-histo-from-a-canvas/15097>\
**Category:** ROOT\
**Created:** [October 17, 2012, 7:50am UTC](https://root-forum.cern.ch/t/error-fitting-histo-from-a-canvas/15097 "2012-10-17T07:50:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mr.M](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/7cd45c/32.png) [@Mr.M](https://root-forum.cern.ch/u/Mr.M)\
**Post date:** [October 17, 2012, 7:50am UTC](https://root-forum.cern.ch/t/error-fitting-histo-from-a-canvas/15097/1 "2012-10-17T07:50:02Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 17, 2012, 8:25am UTC](https://root-forum.cern.ch/t/error-fitting-histo-from-a-canvas/15097/2 "2012-10-17T08:25:28Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![Mr.M](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/7cd45c/32.png) [@Mr.M](https://root-forum.cern.ch/u/Mr.M)\
**Post date:** [October 17, 2012, 9:54am UTC](https://root-forum.cern.ch/t/error-fitting-histo-from-a-canvas/15097/3 "2012-10-17T09:54:52Z")

</div>

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](https://root-forum.cern.ch/uploads/default/original/2X/f/f244f5ca44e4e0e1763ed9267cb7b7a30ab3e29f.root) (15.5 KB)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 17, 2012, 10:51am UTC](https://root-forum.cern.ch/t/error-fitting-histo-from-a-canvas/15097/4 "2012-10-17T10:51:50Z")

</div>

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();

---

<div class="post-metadata">

**Author:** ![Mr.M](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/7cd45c/32.png) [@Mr.M](https://root-forum.cern.ch/u/Mr.M)\
**Post date:** [October 17, 2012, 12:13pm UTC](https://root-forum.cern.ch/t/error-fitting-histo-from-a-canvas/15097/5 "2012-10-17T12:13:33Z")

</div>

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