Covariance of two histograms

Hello @moneta,
I am sorry to reply to you so late, due to something other occupied.
Here, I list my problem exactly.
I want to calculate a sum of mean a and mean b vs mass in all of event, namely

(<<a>> - <<b>>) vs mass

so I created two TH2D:

a->Fill(mass, <a>*weight1),
b->Fill(mass, <b>*weight2),   

Next part is the calculation process,

TH2D* a = (TH2D*)file1->Get(" ");
TH2D* b = (TH2D*)file1->Get(" ");
TH2D *acopy = (TH2D*) a->Clone();

TProfile*  tp_a = (TProfile*)acopy->ProfileX("tp_a");
TProfile*  tp_b = (TPrifile*)b->ProfileX("tp_b");

TH1D* th_a=(TH1D*) tp_a->ProjectionX("");
TH1D* th_b=(TH1D*) tp_b->ProjectionX("");

th_a->Add(th_b, -1); 

Actually, <<a>> and <<b>>, they are correlated, not independent.
So I have to consider the error of sum.
I know they have Reliability weights.
Refer to the below formula. <<a>>, <<b>> representing to <<4>>,<<2>>

Firstly, I have a problems.

  1. To get ∑(w^2), here, I consider w is the weight in each TProfile bin. So I want to use GetBinSumw2() , But the GetBinSumw2 return an array, I want to get a value in each bin.
  2. How to calcualte the Cov(<2>,<4>).

I am appreciate if you can give me a example.