TH3 covariance computation issue

Hi ROOTers,
I noticed that when getting the diagonal elements of the covariance from a TH3 the returned values do not match with the variance of the projected 1D distributions. Indeed, looking at the source code one reads

Double_t TH3::GetCovariance(Int_t axis1, Int_t axis2) const
{
    if (axis1 < 1 || axis2 < 1 || axis1 > 3 || axis2 > 3) {
       Error("GetCovariance","Wrong parameters");
       return 0;
    }
    Double_t stats[kNstat];
    GetStats(stats);
    Double_t sumw   = stats[0];
    Double_t sumw2  = stats[1];
    Double_t sumwx  = stats[2];
    Double_t sumwx2 = stats[3];
    Double_t sumwy  = stats[4];
    Double_t sumwy2 = stats[5];
    Double_t sumwxy = stats[6];
    Double_t sumwz  = stats[7];
    Double_t sumwz2 = stats[8];
    Double_t sumwxz = stats[9];
    Double_t sumwyz = stats[10];
  
    if (sumw == 0) return 0;
    if (axis1 == 1 && axis2 == 1) {
       return TMath::Abs(sumwx2/sumw - sumwx*sumwx/sumw2);
    }
    if (axis1 == 2 && axis2 == 2) {
       return TMath::Abs(sumwy2/sumw - sumwy*sumwy/sumw2);
    }
    if (axis1 == 3 && axis2 == 3) {
       return TMath::Abs(sumwz2/sumw - sumwz*sumwz/sumw2);
    }
    ...
    return 0;
}

while in these cases I would expect the covariance being something like

if (axis1 == 1 && axis2 == 1) {
      return TMath::Abs( sumwx2 / sumw - sumwx*sumwx / (sumw*sumw) );
}

similarly to the implementation in TH2. Maybe I am missing something?

ROOT Version: 6.08/00
Platform: Ubuntu
Compiler: gcc-4.8.5


I’m sure @moneta will give some explanation about this

1 Like

Hi,
Yes, this is certainly a mistake in TH3::GetCovariance !
I will open a PR fixing this.
Thank you for posting it

Lorenzo

2 Likes

You’re welcome!
Best,

Daniele

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