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


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:

#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:

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

Hi,

why not simply leave out this line:

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

? This way your function will not be drawn…

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.

All right, then keeping

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

but replacing

hist->Draw();

with

hist->Draw("HIST");

will do the trick as explained here.

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:

#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");

}

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:

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

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

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.

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.

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,

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!