Any help?

Dear Root experts,
Hi !
I have two 2d histograms of identical dimentsions (0,4000) & identical bin size (4000). I am adding them with the following script -
// Addition
TFile *fA = new TFile(“mat_G13_56CoFe0808_IMPAC_up_L.root”, “read”);
TH2F nh1 = (TH2F)fA->Get(“2hG13_up_L”);
TFile *fB = new TFile(“mat_G13_56CoFe0808_IMPAC_up_R.root”, “read”);
TH2F nh2 = (TH2F)fB->Get(“2hG13_up_R”);
TAxis *ax1 = nh1->GetXaxis();
TAxis *ay1 = nh1->GetYaxis();
TAxis *ax2 = nh2->GetXaxis();
TAxis *ay2 = nh2->GetYaxis();
TH2F *sum = new TH2F(“sum”,“nh1+nh2”,4000,0,4000,4000,0,4000);
Int_t i,j;
Float_t x,y,A,B,S;
for (i=1;i<=4000;i++) {
x = ax1->GetBinCenter(i);
for (j=1;j<=4000;j++) {
y = ay1->GetBinCenter(j);
A = nh1->GetCellContent(i,j);
B = nh2->GetCellContent(i,j);
S = A + B;

By this I am able to produce the added histogram. BUT the number of entries in the added histogram (“nh1+nh2” ) is not exactly equal to the sum of the entries in the two individual histogram - “2hG13_up_L” & “2hG13_up_R”. This you can see in the attched picture.

Please suggest, where I am wrong with the script ???


The way you proceed cannot give you the right number of entries. I suggest to short-circuit most of your code by simply calling TH1::Add doing what you want

TFile *fA = new TFile("mat_G13_56CoFe0808_IMPAC_up_L.root", "read"); TH2F *nh1 = (TH2F*)fA->Get("2hG13_up_L"); // TFile *fB = new TFile("mat_G13_56CoFe0808_IMPAC_up_R.root", "read"); TH2F *nh2 = (TH2F*)fB->Get("2hG13_up_R"); TH2F *sum = (TH2F*)nh1->Clone("sum"); sum->Add(nh2); sum->Draw();

Hi !
Thanks ! It works now.