Troubles to draw a TH1F with no vertical bar and fit on the same canvas


I’m new to Root and I’m having troubles with a plot.

I made an TH1F with a few points that I’d like to display and fit.
To display only the points I used:

h->Draw("HIST P9");

Without that option I get vertical bars that I don’t want on my points (from my understanding it’s due to the resolution point/px).

I then did the fit, choosing the option “SAME” for the draw:

h->Fit("pol1","V", "SAME",0, 1500);

And I updated the canvas.

Doing so, the plot contains only the points but not the fit curve.

If I put no option in the first TH1F draw:


I do get the fit function and the points on a same graph, but I get the annoying vertical bars I can’t seem to get rid of.

I can’t indetify what’s wrong in the code. Could you please help?


ROOT Version: latest
Platform: macOS


h->Fit("pol1","V", "SAME",0, 1500);
1 Like

Thanks for your reply.

Your solution worked!

Could you explain why it didn’t work previously? I really don’t get it.


Note that h->Sumw2(kFALSE); completely destroys the existing “weights”. The following “Fit” will usually be meaningless.

If you read the TH1 Class Reference, you will (in many places) find the opposite advice (i.e. you should execute h->Sumw2(kTRUE);)

IMPORTANT NOTE: If you intend to use the errors of this histogram later you should call Sumw2 before making this operation. This is particularly important if you fit the histogram after …

1 Like

I see the issue here. What should I do to solve the plot issue then?


Well, a 'brutal fix" would be to execute h->Sumw2(kFALSE); right AFTER “Fit”.

1 Like

The HIST option tells the histogram to be drawn only … not the function. That’s why the fit (which is a function) is not drawn. I would suggest to make a copy of the original histogram, on which you call Sumw2. Like that you do not destroy the original histogram, if you want to use the errors later on.

1 Like

Oh I hadn’t understood this! It’s clear now.

Thanks a lot.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.