Draw fitted histogram without fit

I have one program which analyses some ROOT TTrees and makes an output .root file with histograms. Some of these histograms have been fitted with gaussians as part of the analysis. The fit functions are included in the output file. The specific commands that I use for fitting are these:

#hDMass is a dictionary of TH1Fs
for n,h in hDMass.iteritems():
    ftr = ROOT.TF1('ftr','gaus')
    ftr.SetParameter(1,1.85)
    ftr.SetParameter(2,0.005)
    h.Fit('ftr','S')
    #More code that does something with ftr

Later I have another shorter PyROOT script which actually draws the histograms for figures, but when the histograms which have been fitted are drawn, the fit function is automatically drawn as well. In my figures I don’t want to include the fitted function. Is there a way to draw only the histogram part, as if it had never been fitted? Or is there a way to delete the fitted function after the fact?

Thank you,
Jean-François

See the “0” fit option in: http://root.cern.ch/root/html532/TH1.html#TH1:Fit
See also the ‘Warning when using the option “0”’ in there.
This warning disappeared from the class description in the “trunk”. I don’t know why somebody decided to remove a lot of important informations from there: http://root.cern.ch/root/html/TH1.html#TH1:Fit@1

Thanks, I found the parts about the option “0” while fitting:

[quote]When selecting the option “0”, the fitted function is added to
the list of functions of the histogram, but it is not drawn.
You can undo what you disabled in the following way:
h.Fit(“myFunction”, “0”); // fit, store function but do not draw
h.Draw(); function is not drawn
const Int_t kNotDraw = 1<<9;
h.GetFunction(“myFunction”)->ResetBit(kNotDraw);
h.Draw(); // function is visible again
[/quote]
but I tried reversing the instructions to disable drawing if the fit was not done using the “0” option, but since I don’t understand how the ResetBit(kNotDraw) call works, it didn’t seem to do anything. What would be the inverse of the instructions shown in the quoted text? kNotDraw is just the number 512 (on my machine), so presumably I can call ResetBit with some other number to make the function invisible post facto, but I don’t know what this number is.

Jean-François

h.GetFunction(“myFunction”)->SetBit(TF1::kNotDraw);
For the “TF1::kNotDraw” see http://root.cern.ch/root/html/TF1.html

Thanks, I think my feeble attempts were working after all, but I was forgetting to do ROOT.gPad.Update() or something. It works now.

Also I wish to note that I can simply do h.GetFunction(‘ftr’).Delete() if I don’t need the fitted function at all, this works better with the y-scales, since in part of the drawing macro I also normalized the histograms but not the fitted function…

Jean-François

Hi, this is useful function, but I notice that:

  • if h is a TGraph rather than TH1 and it is stored in a root file together with it’s fit function
  • then, when I use the above function - it removes the fit curve but it does not remove the fit stat box.
  • the gStyle.SetOptFit(0) also does not help to remove it

Any ideas?

How about (before reading your object from the file): gROOT->ForceStyle(kTRUE); and/or (after drawing it): gPad->UseCurrentStyle();

None of that seems to work.
Below is a minimal example to reproduce the problem. The input root file is this: fApr.root (47.5 KB)

#!/usr/bin/env python
from ROOT import *
gROOT.SetBatch()
gStyle.SetOptFit(0)
gStyle.SetOptStat(0)

fN = TFile('fApr.root', 'read')
grName = 'SiPad2_GROUP_0_ELE_SENSOR_N300_irrHV_800'
gr = fN.Get(grName).Clone()

gr.Draw("AP")

#stat = gr.GetListOfFunctions().FindObject("stats")
#stat.SetOptFit(0)

fitFunc = 'f3'
gr.GetFunction(fitFunc).SetBit(TF1.kNotDraw)
#gr.GetFunction(fitFunc).Delete()

gPad.UseCurrentStyle()
gROOT.ForceStyle()
c1.Update()

c1.SaveAs("stat_fit_test.png")

And the Root version is: 6.06/01

A brutal fix: gr.GetFunction("stats").Delete();

1 Like