# Saving canvas with fitted histogram to .gif file

**URL:** <https://root-forum.cern.ch/t/saving-canvas-with-fitted-histogram-to-gif-file/37094>\
**Category:** Newbie\
**Created:** [December 7, 2019, 11:31am UTC](https://root-forum.cern.ch/t/saving-canvas-with-fitted-histogram-to-gif-file/37094 "2019-12-07T11:31:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![fresh](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/2acd7d/32.png) [@fresh](https://root-forum.cern.ch/u/fresh)\
**Post date:** [December 7, 2019, 11:31am UTC](https://root-forum.cern.ch/t/saving-canvas-with-fitted-histogram-to-gif-file/37094/1 "2019-12-07T11:31:03Z")

</div>

Hi Rooters!

I have a canvas on which I have drawn a histogram. I have then fitted my histogram with a predefined function. I would now like to save the result to a .gif file. However, the fit is not shown.  
Here’s a snippet of the code I used:

```auto
auto controlCanvas(new TCanvas("ControlCanvas", "Distributions", 20, 10, 600, 800));
  controlCanvas->cd();
  hist->Draw();
  hist->Fit("expo");
  controlCanvas->Update();
  hist->SetLineWidth(5);
  hist->GetXaxis()->SetTitle("Momentum (GeV/c)");
  controlCanvas->Print("Hists/controlHist0.gif");`

```

Am I doing anything wrong?

P.S. I’m using ROOT in compiled mode.

---

<div class="post-metadata">

**Author:** ![Dilicus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dilicus/32/14338_2.png) [@Dilicus](https://root-forum.cern.ch/u/Dilicus)\
**Post date:** [December 7, 2019, 12:51pm UTC](https://root-forum.cern.ch/t/saving-canvas-with-fitted-histogram-to-gif-file/37094/2 "2019-12-07T12:51:11Z")

</div>

Hi,  
try to use [TCanvas::SaveAs()](https://root.cern.ch/doc/master/classTPad.html#ac53122e6b3fe76f52c379178482236dd) instead of print.

Cheers,  
Stefano

---

<div class="post-metadata">

**Author:** ![fresh](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/2acd7d/32.png) [@fresh](https://root-forum.cern.ch/u/fresh)\
**Post date:** [December 7, 2019, 4:33pm UTC](https://root-forum.cern.ch/t/saving-canvas-with-fitted-histogram-to-gif-file/37094/3 "2019-12-07T16:33:51Z")

</div>

I tried but no success.

---

<div class="post-metadata">

**Author:** ![Dilicus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dilicus/32/14338_2.png) [@Dilicus](https://root-forum.cern.ch/u/Dilicus)\
**Post date:** [December 7, 2019, 4:49pm UTC](https://root-forum.cern.ch/t/saving-canvas-with-fitted-histogram-to-gif-file/37094/4 "2019-12-07T16:49:00Z")

</div>

I tried this macro and for me SaveAs works fine.

```auto
void macro(){
    
    TCanvas * controlCanvas=new TCanvas("ControlCanvas", "Distributions", 20, 10, 600, 800);
    controlCanvas->cd();
    TH1D *hist = new TH1D("","",100,-1,1);
    hist->FillRandom("gaus",10000);
    hist->Draw();
    hist->Fit("gaus");
    controlCanvas->Update(); hist->SetLineWidth(5);
    hist->GetXaxis()->SetTitle("Momentum (GeV/c)");
    controlCanvas->SaveAs("controlHist0.gif");
}

```

Can be that your parentheses at the first line are not right?

S

---

<div class="post-metadata">

**Author:** ![fresh](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/2acd7d/32.png) [@fresh](https://root-forum.cern.ch/u/fresh)\
**Post date:** [December 7, 2019, 5:07pm UTC](https://root-forum.cern.ch/t/saving-canvas-with-fitted-histogram-to-gif-file/37094/5 "2019-12-07T17:07:07Z")

</div>

Everything seems right to me…  
You used a TH1D while I used a TH1F, maybe the problem’s there.  
But it would be quite strange if that were the case.  
Also, I’m wrapping my histograms in STL smart pointers. Don’t know whether that can be harmful, though.

---

<div class="post-metadata">

**Author:** ![Dilicus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dilicus/32/14338_2.png) [@Dilicus](https://root-forum.cern.ch/u/Dilicus)\
**Post date:** [December 7, 2019, 6:36pm UTC](https://root-forum.cern.ch/t/saving-canvas-with-fitted-histogram-to-gif-file/37094/6 "2019-12-07T18:36:41Z")

</div>

If you tried also with my macro and doesn’t work, you should wait for someone more expert than me.  
I don’t know if the STL smart pointer can be the problem.  
And I don’t think the TH1F is the problem I usually use TH1D just to avoid approximation problems.

---

<div class="post-metadata">

**Author:** ![fresh](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/2acd7d/32.png) [@fresh](https://root-forum.cern.ch/u/fresh)\
**Post date:** [December 7, 2019, 7:01pm UTC](https://root-forum.cern.ch/t/saving-canvas-with-fitted-histogram-to-gif-file/37094/7 "2019-12-07T19:01:17Z")

</div>

Your macro works fine on my machine. I really have no idea.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [December 9, 2019, 10:07am UTC](https://root-forum.cern.ch/t/saving-canvas-with-fitted-histogram-to-gif-file/37094/8 "2019-12-09T10:07:13Z")

</div>

Can you post a macro we can run reproducing the problem ?
