How to Correctly Find the Error of a Component of an Integral

Hi,

I am working on a fitting macro where I use a combined function (Gaussian + Linear) to fit a dataset and subsequently calculate the integral and integral errors of the total function as well as its individual components. I’m looking to verify the correctness of my approach, particularly in how I calculate and interpret the integral errors for the separate components based on their covariance.

void TestIntegralError() {
    TH1F *h = new TH1F("h", "Gaussian + Linear Fit Example", 100, -4, 4);
    for (int i = 0; i < 10000; i++) {
        h->Fill(gRandom->Gaus(0, 1)); // Gaussian distribution
    }
    for (int i = 0; i < 5000; i++) {
        h->Fill(gRandom->Uniform(-4, 4)); // Linear background
    }

    TCanvas *c1 = new TCanvas("c1", "Canvas", 800, 600);
    h->Draw();

    // Define the composite fit function (Gaussian + linear)
    TF1 *fitFunc = new TF1("fitFunc", "gaus(0) + pol1(3)", -4, 4);
    fitFunc->SetParameters(500, 0, 1, 100, 10);

    TFitResultPtr r = h->Fit(fitFunc, "S");
    TMatrixDSym cov = r->GetCovarianceMatrix();
    fitFunc->Draw("same");
    c1->Update();

    // Calculate the integral and its error of the full function
    Double_t integralFull, errorFull;
    integralFull = fitFunc->Integral(-4,4);
    errorFull = fitFunc->IntegralError(-4, 4, r->GetParams(), cov.GetMatrixArray());

    // Retrieve Gaussian parameters and covariance matrix
    Double_t params[3] = {fitFunc->GetParameter(0), fitFunc->GetParameter(1), fitFunc->GetParameter(2)};
    Double_t errors[3] = {fitFunc->GetParError(0), fitFunc->GetParError(1), fitFunc->GetParError(2)};

    // Define a new Gaussian function
    TF1 *gaussian = new TF1("gaussian", "gaus", -4, 4);
    gaussian->SetParameters(params);
    gaussian->SetParErrors(errors);

    // Extract submatrix for Gaussian parameters (0, 1, 2)
    TMatrixD cov_gaussian = cov.GetSub(0, 2, 0, 2);

    // Calculate the integral and its error
    Double_t integral, error;
    integral = gaussian->Integral(-4, 4);
    error = gaussian->IntegralError(-4, 4, params, cov_gaussian.GetMatrixArray());

    // Retrieve Linear Bg parameters and covariance matrix
    Double_t paramsBg[2] = {fitFunc->GetParameter(3), fitFunc->GetParameter(4)};
    Double_t errorsBg[2] = {fitFunc->GetParError(3), fitFunc->GetParError(4)};

    // Define a new Bg function
    TF1 *linear = new TF1("linear", "pol1", -4, 4);
    linear->SetParameters(paramsBg);
    linear->SetParErrors(errorsBg);

    // Extract submatrix for Bg parameters (3, 4)
    TMatrixD cov_bg = cov.GetSub(3, 4, 3, 4);

    // Calculate the integral and its error
    Double_t integralBg, errorBg;
    integralBg = linear->Integral(-4, 4);
    errorBg = linear->IntegralError(-4, 4, paramsBg, cov_bg.GetMatrixArray());
}

My concern is mainly about whether I am correctly accounting for the correlations between different parameters in my error calculations. The way I’m calculating the integral errors now seems like I’m ignoring the correlations between the Gaussian parameters and the Linear parameters. Any feedback on whether I should approach this differently is greatly appreciated. Thank you!

Dear Pete,

I am adding in the loop @moneta and @jonas that can comment about properly taking into account correlations in fits.

Cheers,
Danilo

1 Like