# TH2 GetCovariance function explanation

Hi,

So I have a doubt about how covariance is exactly calculated for 2D histograms. I found th code in the documentation of TH2 but I don’t know what each variable stands for:

Double_t TH2::GetCovariance(Int_t axis1, Int_t axis2) const
{
//-------Return covariance between axis1 and axis2----*
//- ====================================================

if (axis1 < 1 || axis2 < 1 || axis1 > 2 || axis2 > 2) {
Error(“GetCovariance”,“Wrong parameters”);
return 0;
}
Double_t stats[7];
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];

if (sumw == 0) return 0;
if (axis1 == 1 && axis2 == 1) {
return TMath::Abs(sumwx2/sumw - sumwx/sumwsumwx/sumw);
}
if (axis1 == 2 && axis2 == 2) {
return TMath::Abs(sumwy2/sumw - sumwy/sumw
sumwy/sumw);
}
return sumwxy/sumw - sumwx/sumw*sumwy/sumw;
}

Could any one explain what each term, stats[0] to stats[7], stands for?

Ricardo

where sumw = Sum of the weights
sumwx = Sum of weight * x
sumw2 = Sum of weight ^2
sumwxy = Sum of weight * x * y
etc…

where the sum are meant for all the histogram entries

Lorenzo

1 Like

Thank you a lot Lorenzo!

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