Problem with fitting / histos disappear from canvas after fitting

Hello

I have some data and bkg histograms, which I initially can plot alright…


However, when I do fit the data-bkg histogram ie like

hist_data.Draw()

# Plot bkg+bkg2 (green dashed)
hist_bkg_plus_bkg2 = hist_bkg1.Clone()
hist_bkg_plus_bkg2.Add(hist_bkg2)
hist_bkg_plus_bkg2.SetLineStyle(2)
hist_bkg_plus_bkg2.SetLineColor(ROOT.kGreen)
hist_bkg_plus_bkg2.Draw(" L same")  # Draw bkg+bkg2 (green dashed)

# Fit data-bkg distribution with a Gaussian (blue)
hist_data_minus_bkg = hist_data_minus_bkg.Clone()
fit_data_minus_bkg = hist_data_minus_bkg.Fit("gaus", "S", "", -50, 50)

#
hist_data_minus_bkg.SetLineColor(ROOT.kBlue)
hist_data_minus_bkg.SetLineWidth(2)
hist_data_minus_bkg.Draw("same")

then even if I do not plot the fitted function, it appears on canvas, whereas the data (red points in first plot) bit also bkg (green points in previous plot) disappear, ie only the data-bkg and the fit are drawn. Even if I try to redraw the other histograms, they still do not appear on the canvas

What am I missing?

Thanks
Alex

Hi Alex,

Thanks for posting. I think I miss some context to help out here: could you please post a minimal reproducer of the behaviour which I could also run? I am confident that once we have those few lines we can fix this.

Cheers,
Danilo

Hi Danilo

Thanks for your reply. I am attaching some small example code/files to reproduce the problem

Regards,
Alex

data.root (304.6 KB)
wjets.root (346.6 KB)
qcd.root (473.5 KB)
fitSimple.py (3.7 KB)

Once you have done a fit on a histogram, the fitting function becomes an item in the list of functions of this histogram. When the histogram is drawn the list of functions is also drawn. If you want to draw only the histogram you need the option “HIST”:

hist->Draw("hist");

I hope it helps.

Hello

Sorry, maybe it is trivial, but can you point me what commands I should use to get on the canvas

.- the data histo
.- the data-bkg histo
.- the fit curve as well

I ve been trying to use "hist" as you say, but I cannot make it to work and get all three curves on the canvas.

Thanks again
-a

{
   auto C = new TCanvas();
   auto h1 = new TH1D("h1","Background", 100,-1,1);
   auto h2 = new TH1D("h2","Data", 100,-1,1);
   h1->FillRandom("gaus",100000);
   h2->FillRandom("gaus",100000);
   h1->Draw("P");
   h2->Fit("gaus","","SAME");
}

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