Skip to content

Histogram with fit function not correctly drawn to canvas in Jupyter notebook with JsROOT on #22769

Description

@vepadulano

Check duplicate issues.

  • Checked for duplicates

Description

This exercise we use in the student course https://github.com/root-project/student-course/blob/main/course/exercises/core/fitting-exercise.ipynb is currently not producing the correct plot in a Jupyter notebook with the default graphics mode (i.e. %jsroot on).

Image

If I explicitly turn off JsROOT via %jsroot off, then I can see the correct plot

Image

Reproducer

Paste the following code in a Jupyter notebook cell

import ROOT
print(f"{ROOT.__version__=}")
import numpy as np

# Turn this off to see the correct plot
%jsroot on

ROOT.gInterpreter.Declare("""
#ifndef MYFITFN
#define MYFITFN

double background(double *x, double *par) {
  return par[0] + par[1]*x[0] + par[2]*x[0]*x[0];
}

double lorentzianPeak(double *x, double *par) {
  return (0.5*par[0]*0.2/TMath::Pi())
         / TMath::Max(1.e-10, (x[0] - 1.)*(x[0] - 1.) + .25*0.04);
}


double fitFunction(double *x, double *par) {
  return background(x, par) + lorentzianPeak(x, &par[3]);
}

#endif
""")
 
data = np.loadtxt("fitting-exercise-data.txt")

hist = ROOT.TH1D(
   name="histo", title="Lorentzian Peak on Quadratic Background",
   nbinsx=60, xlow=0, xup=3)

hist.Fill(data, np.ones(data.size))

fitFunc = ROOT.TF1("Function", ROOT.fitFunction, xmin = 0, xmax = 3, npar = 4)
_ = hist.Fit(fitFunc, "Q")

canvas = ROOT.TCanvas()
hist.Draw()
canvas.Draw()

ROOT version

6.40.02

Installation method

LCG release

Operating system

Linux

Additional context

This was discovered during a ROOT Summer Student basic course session

Metadata

Metadata

Assignees

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions