I am trying to build a pretty minimal pyhf example: two gaussians, one signal and one background, but I can't get it to work. My python code is:
import pyhf.readxml
import os
from ROOT import TH1F, TFile, TF1
mygaus = TF1("mygaus","TMath::Gaus(x,100,.5)",95, 115)
mygaus2 = TF1("mygaus2","TMath::Gaus(x,110,.2)",95, 115)
mygaus_data = TF1("mygaus_data","TMath::Gaus(x,110,.2)+TMath::Gaus(x,100,.5)",95, 115)
bkg_nominal = TH1F('bkg_nominal', '', 80, 95, 115)
bkg_nominal.FillRandom("mygaus", 10000)
sig_nominal = TH1F('sig_nominal', '', 80, 95, 115)
sig_nominal.FillRandom("mygaus2", 5000)
data_nominal = TH1F('data_nominal', '', 80, 95, 115)
data_nominal.FillRandom("mygaus_data", 10000)
meas = TFile('meas.root', 'RECREATE')
bkg_nominal.Write()
sig_nominal.Write()
data_nominal.Write()
meas.Close()
spec = pyhf.readxml.parse('meas.xml', os.getcwd())
workspace = pyhf.Workspace(spec)
pdf = workspace.model(measurement_name='meas')
data = workspace.data(pdf)
workspace.get_measurement(measurement_name='meas')
best_fit = pyhf.infer.mle.fit(data, pdf)
The XML file, which I basically copied from the example in the documentation, are written like this
meas.xml
<!DOCTYPE Combination SYSTEM 'HistFactorySchema.dtd'>
<Combination OutputFilePrefix="workspace" >
<Input>./meas_channel1.xml</Input>
<Measurement Name="meas" Lumi='1' LumiRelErr='0.1' ExportOnly="False" >
<POI>signorm</POI>
</Measurement>
</Combination>
meas_channel1.xml
<!DOCTYPE Channel SYSTEM 'HistFactorySchema.dtd'>
<Channel Name="channel1" InputFile="" >
<Data HistoName="data_nominal" InputFile="meas.root" />
<StatErrorConfig RelErrorThreshold="0.05" ConstraintType="Gaussian" />
<Sample Name="bkg" HistoName="bkg_nominal" InputFile="meas.root" NormalizeByTheory="True" >
<NormFactor Name="bkgnorm" Val="1" High="3" Low="0" Const="False" />
</Sample>
<Sample Name="sig" HistoName="sig_nominal" InputFile="meas.root" NormalizeByTheory="True" >
<NormFactor Name="signorm" Val="1" High="3" Low="0" Const="False" />
</Sample>
</Channel>
It looks all pretty simple and I am able to plot the histograms. However, when I get this error message:
ERROR:pyhf.optimize.opt_scipy: fun: nan
jac: array([nan, nan, nan])
message: 'Inequality constraints incompatible'
nfev: 5
nit: 1
njev: 1
status: 4
success: False
x: array([1., 1., 1.])
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-14-54e7c2f0a645> in <module>
2 data = workspace.data(pdf)
3 workspace.get_measurement(measurement_name='meas')
----> 4 best_fit = pyhf.infer.mle.fit(data, pdf)
/usr/local/lib/python3.7/site-packages/pyhf/infer/mle.py in fit(data, pdf, init_pars, par_bounds, **kwargs)
34 init_pars = init_pars or pdf.config.suggested_init()
35 par_bounds = par_bounds or pdf.config.suggested_bounds()
---> 36 return opt.minimize(twice_nll, data, pdf, init_pars, par_bounds, **kwargs)
37
38
/usr/local/lib/python3.7/site-packages/pyhf/optimize/opt_scipy.py in minimize(self, objective, data, pdf, init_pars, par_bounds, fixed_vals, return_fitted_val)
45 )
46 try:
---> 47 assert result.success
48 except AssertionError:
49 log.error(result)
AssertionError:
which is weird because I don't have any inequality constraint. I think I am doing something dumb, could you please help? Thank you!