# RooFit when Histogram has negative values

**URL:** <https://root-forum.cern.ch/t/roofit-when-histogram-has-negative-values/64354>\
**Category:** Roofit and RooStats\
**Created:** [October 23, 2025, 8:32pm UTC](https://root-forum.cern.ch/t/roofit-when-histogram-has-negative-values/64354 "2025-10-23T20:32:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jasmine](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@Jasmine](https://root-forum.cern.ch/u/Jasmine)\
**Post date:** [October 23, 2025, 8:32pm UTC](https://root-forum.cern.ch/t/roofit-when-histogram-has-negative-values/64354/1 "2025-10-23T20:32:39Z")

</div>

Dear experts, I’m using a model A \* (s0 + a1\*d1 + a2\*d2) to fit the data energy spectrum. s0 is a nominal spectrum. And d1, d2 are difference spectra. I would like to how to construct this model. I used the following method. But the fitting result doesn’t seem right. Setting a1, a2 as constant just for test. The green line is A\*original normalize s0 hist. Could you help me?

> RooDataHist dh\_s0(“dh\_s0”, “nominal shape data”, RooArgSet(x), mc\_hists[0]);
> 
> ```
> RooHistPdf s0("s0_pdf", "nominal shape", RooArgSet(x), dh_s0); 
> 
> RooDataHist dh_d1("dh_d1", "delta1 shape data", RooArgSet(x), delta1);
> 
> RooHistFunc d1("d1", "delta1 shape", RooArgSet(x), dh_d1);
> 
> RooDataHist dh_d2("dh_d2", "delta2 shape data", RooArgSet(x), delta2);
> 
> RooHistFunc d2("d2", "delta2 shape", RooArgSet(x), dh_d2);
> 
> RooRealVar A("A", "whole factor", dataIntegral\*0.6, 1000, 2000);
> 
> RooRealVar a1("a1", "Δ1", 0, 0, 0.01);
> 
> RooRealVar a2("a2", "Δ2", 0, 0,0.01);
> 
> a1.setConstant(true);
> 
> a2.setConstant(true);
> 
> ```
> 
> RooFormulaVar model\_val(“model\_val”, “s0 + a1\*d1 + a2\*d2”, RooArgList(s0, a1, d1, a2, d2));
> 
> ```
> RooGenericPdf shape_pdf("shape_pdf", "@0", RooArgList(model_val, x));  
> 
> RooExtendPdf corrected_pdf("corrected_pdf", "extended model", shape_pdf, A);
> 
> ```
> 
> RooFitResult\* result\_chi2 = corrected\_pdf.chi2FitTo(data,
> 
> ```
> RooFit::Save(true),       
> 
> RooFit::PrintLevel(-1),   
> 
> RooFit::Strategy(1),       
> 
> RooFit::Minimizer("Minuit2", "Migrad"),
> 
> RooFit::Hesse(true),
> 
> RooFit::Extended(true)
> 
> );
> 
> ```

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/2/b/2be9bc1d3531bca84ae36ef59d68e72dc891f8ce.png)

---

<div class="post-metadata">

**Author:** ![mczurylo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mczurylo/32/33269_2.png) [@mczurylo](https://root-forum.cern.ch/u/mczurylo)\
**Post date:** [October 24, 2025, 7:45am UTC](https://root-forum.cern.ch/t/roofit-when-histogram-has-negative-values/64354/2 "2025-10-24T07:45:31Z")

</div>

Hi @Jasmine,

thanks for your question. I will add @jonas to give you some insights.

Cheers,

Marta

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [November 7, 2025, 7:46am UTC](https://root-forum.cern.ch/t/roofit-when-histogram-has-negative-values/64354/3 "2025-11-07T07:46:28Z")

</div>

Closed on November 7, 2025, 7:46am UTC

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