# Prevent TF1 from being automatically drawn on TH1 when added to list of functions

**URL:** <https://root-forum.cern.ch/t/prevent-tf1-from-being-automatically-drawn-on-th1-when-added-to-list-of-functions/41002>\
**Category:** Newbie\
**Created:** [August 14, 2020, 8:21pm UTC](https://root-forum.cern.ch/t/prevent-tf1-from-being-automatically-drawn-on-th1-when-added-to-list-of-functions/41002 "2020-08-14T20:21:52Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erdrick](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/7bcc69/32.png) [@Erdrick](https://root-forum.cern.ch/u/Erdrick)\
**Post date:** [August 14, 2020, 8:21pm UTC](https://root-forum.cern.ch/t/prevent-tf1-from-being-automatically-drawn-on-th1-when-added-to-list-of-functions/41002/1 "2020-08-14T20:21:52Z")

</div>

* * *

_ROOT Version:_ root/6.14.06  
_Platform:_ Red Hat Enterprise Linux Server release 7.8 (Maipo)  
_Compiler:_ gcc/8.3.0

* * *

Hello,

If I add a function to a histogram, how do I make the function _ **not** _ be drawn when I draw the histogram? Here is a minimal example showing my problem:

```auto
#include "TCanvas.h"
#include "TF1.h"
#include "TH1F.h"

void minimal() {

  TH1F* hist = new TH1F("hist", "", 20, 0, 10);

  TF1* function = new TF1("function", "gaus(0)", 0, 10);
  function->SetParameter(0, 10);
  function->SetParameter(1, 5);
  function->SetParameter(2, 0.25);

  for (int i=0; i<1e2; ++i) {
    hist->Fill(function->GetRandom());
  }

  hist->GetListOfFunctions()->Add(function);

  TCanvas* canvas = new TCanvas();                                                                                                                                                                                                                                                    
  hist->Draw();

}

```

The result is:

 ![Screen Shot 2020-08-14 at 4.13.49 PM](https://root-forum.cern.ch/uploads/default/original/3X/d/7/d77c9ebe38b1f49e572598df8ad5bf0a588e1c8e.png)

I did not expect the function to be drawn, since I did not tell it to draw itself. That is, there is no `function->Draw("same");`. In a case like this, how do I only draw the histogram?

Thanks

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [August 15, 2020, 11:21am UTC](https://root-forum.cern.ch/t/prevent-tf1-from-being-automatically-drawn-on-th1-when-added-to-list-of-functions/41002/2 "2020-08-15T11:21:50Z")

</div>

Hi,

why not simply leave out this line:

```auto
hist->GetListOfFunctions()->Add(function);

```

? This way your function will not be drawn…

---

<div class="post-metadata">

**Author:** ![Erdrick](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/7bcc69/32.png) [@Erdrick](https://root-forum.cern.ch/u/Erdrick)\
**Post date:** [August 15, 2020, 12:25pm UTC](https://root-forum.cern.ch/t/prevent-tf1-from-being-automatically-drawn-on-th1-when-added-to-list-of-functions/41002/3 "2020-08-15T12:25:27Z")

</div>

As stated in the original post, this is a **minimal** example to illustrate the problem. I have a reason for not removing that line in my (more complicated) data analysis.

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [August 15, 2020, 12:36pm UTC](https://root-forum.cern.ch/t/prevent-tf1-from-being-automatically-drawn-on-th1-when-added-to-list-of-functions/41002/4 "2020-08-15T12:36:29Z")

</div>

All right, then keeping

```auto
hist->GetListOfFunctions()->Add(function);

```

but replacing

```auto
hist->Draw();

```

with

```auto
hist->Draw("HIST");

```

will do the trick as explained [here](https://root.cern/doc/master/classTHistPainter.html#HP28).

---

<div class="post-metadata">

**Author:** ![Erdrick](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/7bcc69/32.png) [@Erdrick](https://root-forum.cern.ch/u/Erdrick)\
**Post date:** [August 15, 2020, 1:03pm UTC](https://root-forum.cern.ch/t/prevent-tf1-from-being-automatically-drawn-on-th1-when-added-to-list-of-functions/41002/5 "2020-08-15T13:03:39Z")

</div>

Hi,

Thank you for your help! But I guess I’m also looking for some deeper understanding. For example, I tweaked the minimal example so that the histogram has a function added to it via TH1::Fit (but with the “0” option so that the result is not automatically plotted). In this new example, there is a function in the list of functions, as proven in `hist->GetListOfFunctions()->Print();` but it is not drawn when I do a simple `hist->Draw();`. If I uncomment the last two lines, then the function is drawn. So why is this behavior different from the first minimal example?

In other words, in both minimal examples, a histogram has a function in its list of functions (in the first example, it was added manually; in the second example, it was added from a fit). Using `hist->Draw()`, the function is drawn in the first example, but not the second. Why is that?

Here is the second minimal example:

```auto
#include "TCanvas.h"
#include "TF1.h"
#include "TH1F.h"

void minimal() {

  TH1F* hist = new TH1F("hist", "", 20, 0, 10);

  TF1* function = new TF1("function", "gaus(0)", 0, 10);
  function->SetParameter(0, 10);
  function->SetParameter(1, 5);
  function->SetParameter(2, 0.25);

  for (int i=0; i<1e2; ++i) {
    hist->Fill(function->GetRandom());
  }

  hist->Fit(function, "Q 0 B", "", 0, 10);

  hist->GetListOfFunctions()->Print();

  TCanvas* canvas = new TCanvas();
  hist->Draw();

  //TF1* func = (TF1*) hist->GetFunction("function");
  //func->Draw("same");

}

```

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [August 15, 2020, 1:13pm UTC](https://root-forum.cern.ch/t/prevent-tf1-from-being-automatically-drawn-on-th1-when-added-to-list-of-functions/41002/6 "2020-08-15T13:13:06Z")

</div>

> [@Erdrick](#):
>
> So why is this behavior different from the first minimal example?

By default, the function is plotted (as we observed in the first example).But in the second example you chose not to draw the fitting function yourself:

```auto
hist->Fit(function, "Q 0 B", "", 0, 10);

```

This zero symbol in the second argument makes the fit result to be absent.

---

<div class="post-metadata">

**Author:** ![Erdrick](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/7bcc69/32.png) [@Erdrick](https://root-forum.cern.ch/u/Erdrick)\
**Post date:** [August 15, 2020, 1:23pm UTC](https://root-forum.cern.ch/t/prevent-tf1-from-being-automatically-drawn-on-th1-when-added-to-list-of-functions/41002/7 "2020-08-15T13:23:48Z")

</div>

Yes, I understand all of that (I wrote most of that in my second post). The point is that in the first example, I never told the function to draw itself either. It was simply added to the list of functions, as in the second example. So I think there is an inconsistency. I’ll wait a while longer to see if anyone can explain this on a deeper level. If no one does, I’ll accept the `hist->Draw("hist")` as a solution.

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [August 15, 2020, 1:36pm UTC](https://root-forum.cern.ch/t/prevent-tf1-from-being-automatically-drawn-on-th1-when-added-to-list-of-functions/41002/8 "2020-08-15T13:36:33Z")

</div>

Not sure I understand the question at this point.

In both cases, the function is added to the list of functions. In both cases, it is drawn by default. The drawing can be overruled by either `HIST` option of `TH1::Draw()` or `0` option of `TH1::Fit()`, neither of which are default options. So where is the inconsistency?

Yes, let’s wait for someone else to chime in.

---

<div class="post-metadata">

**Author:** ![Erdrick](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/7bcc69/32.png) [@Erdrick](https://root-forum.cern.ch/u/Erdrick)\
**Post date:** [August 15, 2020, 2:06pm UTC](https://root-forum.cern.ch/t/prevent-tf1-from-being-automatically-drawn-on-th1-when-added-to-list-of-functions/41002/9 "2020-08-15T14:06:12Z")

</div>

What you write makes sense and does indeed answer my question. I guess concerning my first minimal example, I was thinking there would be a way to add a function to the histogram via `hist->GetListOfFunctions()->Add(function);` and not draw it when I do `hist->Draw();` (like in the second minimal example). But now I understand that the “0” option in TH1::Fit means do not plot the fit result (do not automatically open a canvas and show the fit result) **AND** do not plot the fit result when the histogram is drawn by itself (`hist->Draw();`).

I was hoping in the first minimal example to do something like,

```auto
hist->GetListOfFunctions()->Add(function);
// then put some command here or maybe an option in the
// command above so that the function is not drawn with hist->Draw()
hist->Draw();
// the result would be only hist would be drawn until I explicitly wrote
// function->Draw("same");

```

Anyways, I am going to mark your response as the solution. Thank you very much for taking the time to help me on a weekend! I greatly appreciate it!
