Skip to content

THnBase::ProjectionAny does not calculate errors correctly #19241

Closed
@jgrosseo

Description

@jgrosseo

Check duplicate issues.

  • Checked for duplicates

Description

It seems to me that THnBase::ProjectionAny does not respect the user's choice of calculating the error correctly even when "E" is given as an option. Please find a minimal reproducer attached which compares the behaviour of TH2 -> TH1 (correct) to THn -> THn (not correct).

Please let me know if I am overlooking something.

Reproducer

void thn()
{
    auto hist = new TH2F("hist", "", 10, 0.5, 10.5, 10, 0.5, 10.5);
    hist->Sumw2();

    for (int i = 0; i < 9; i++)
        hist->Fill(1, 1, 0.1);

    float relError = hist->GetBinError(1, 1) / hist->GetBinContent(1, 1);
    printf("TH2:  %f +- %f --> %f\n", hist->GetBinContent(1, 1), hist->GetBinError(1, 1), relError);

    auto proj1 = hist->ProjectionX();
    relError = proj1->GetBinError(1, 1) / proj1->GetBinContent(1, 1);
    printf("Proj: %f +- %f --> %f\n", proj1->GetBinContent(1), proj1->GetBinError(1), relError);

    int bins[] = {10, 10, 10, 10};
    double xmin[] = {0, 0, 0, 0};
    double xmax[] = {10, 10, 10, 10};
    auto thn = new THnF("thn", "", 4, bins, xmin, xmax);
    thn->Sumw2();

    for (int i = 0; i < 9; i++)
        thn->Fill(0.5, 0.5, 0.5, 0.5, 0.1);

    double values[]{0.5, 0.5, 0.5, 0.5};
    auto binNo = thn->GetBin(values);
    relError = thn->GetBinError(binNo) / thn->GetBinContent(binNo);
    printf("\nTHn:  %f +- %f --> %f\n", thn->GetBinContent(binNo), thn->GetBinError(binNo), relError);

    Int_t dimensions[] = {0, 1};
    // Despite the option "E", the errors are resetted to sqrt(N) instead of keeping the original ones
    THnBase *proj = thn->ProjectionND(2, dimensions, "E");

    binNo = proj->GetBin(values);
    relError = proj->GetBinError(binNo) / proj->GetBinContent(binNo);
    printf("Proj: %f +- %f --> %f\n", proj->GetBinContent(binNo), proj->GetBinError(binNo), relError);
}

The output is

TH2:  0.900000 +- 0.300000 --> 0.333333
Proj: 0.900000 +- 0.300000 --> 0.333333

THn:  0.900000 +- 0.300000 --> 0.333333
Proj: 0.900000 +- 0.948683 --> 1.054093

while I believe all four lines should have identical numbers.

ROOT version

v6-32-06

Installation method

ALICE cvmfs installation

Operating system

Linux Ubuntu 24

Additional context

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

No status

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions