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())};
result->Print();
auto c1 = new TCanvas("c1");
auto frame = x.frame();
frame->Draw();
data->plotOn(frame);
poly.plotOn(frame);
frame->Draw();
c1->SaveAs("plot.png");
}
There is also a dedicated tutorial on fitting subranges:
-
ROOT: tutorials/roofit/rf203_ranges.C File Reference
The problem was also recently discussed in the forum: - Fitting To Regions in RooFit
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?
Cheers,
Jonas