Skip a few bins in the range while fitting

Hello Rooters,

This might sound a bit stupid, but is it possible to skip a few bins in the middle of your fit range when you fit a histogram?


I am trying to fit a 3rd order polynomial (in red) over the range (2180, 2280) but I want to skip a few bins around 2222 so that my final fit output parameters don’t take those bins into account. I also want the fit to appear smooth visually without any discontinuities.

In my attempt here, I tried fitting two different pol3 by skipping ‘that’ region in between but as expected, the fit looks discontinuous. I want it to look continuous and also skip that tiny region from my fit.
If there exists a way I can ‘stitch’ these two pol3 or fit a single pol3 but exclude that region, your suggestions or examples are welcome! :sweat_smile: Apologies if this sounds quite naive! :sweat_smile:

Thank you for your help!

Hi @tangophysics,

this is not so easy to implement in the standard ROOT fitting. You could have a look at RooFit, which is very widely used for more advanced fits.

Fitting a polynomial PDF to two ranges would look like this:

void script() {

    RooRealVar x{"x", "x", 0, 0, 10};

    x.setRange("r1", 2, 4);
    x.setRange("r2", 5, 7);

    RooRealVar p0{"p0", "p0", 20, -100, 100};
    RooRealVar p1{"p1", "p1", 0.6, -100, 100};
    RooRealVar p2{"p2", "p2", 1.2, -100, 100};
    RooRealVar p3{"p3", "p3", -0.12, -100, 100};

    RooArgList params{p0, p1, p2, p3};

    RooPolynomial poly{"poly", "poly", x, params, 0};

    // Generating toy data set to demonstrate, but you would have to create a
    // RooDataHist from your TH1.
    std::unique_ptr<RooDataSet> data{poly.generate(x, 10000)};

    using namespace RooFit;
    std::unique_ptr<RooFitResult> result{poly.fitTo(*data, Range("r1,r2"), PrintLevel(-1), Save())};

    auto c1 = new TCanvas("c1");

    auto frame = x.frame();





There is also a dedicated tutorial on fitting subranges:

You can easily construct a RooFit histogram to use as the data for the fit with the Import() command in the RooDataHist constructor.

Could this be a solution for you?


Hi @jonas, Thanks a lot for this detailed reply and suggestions, this might help me!!

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