Re: 2D histogram subtraction

Dear Rene,
Hi !
Thanks for the reply mail & the suggestions you gave. I tried to do exactly the same to subtract the 2 histogram in the following way -
############################################
TFile *f1 = new TFile(“file1.root”, “read”);
TH2F h1 = (TH2F)f1->Get(“hist1”);
//
TFile *f2 = new TFile(“file2.root”, “read”);
TH2F h2 = (TH2F)f2->Get(“hist2”);
TH2F h_out = (TH2F)h1->Clone("");
//
// Addition of 2 histograms (hist1 + hist 2)
TH2F hA = (TH2F)hist1->Clone(“hist1 + hist2”);
hA ->SetDirectory(0);
MyC1->cd(5);
hA->Draw();
//
// Difference of two histograms (hist1 - hist2)
//
MyC1->cd(6);
TFile *f1 = new TFile(“file1.root”, “read”);
TH2F h1 = (TH2F)f1->Get(“hist1”);
//
TFile f2 = new TFile(“file2.root”, “read”);
TH2F h2 = (TH2F)f2->Get(“hist2”);
TH2F h_out = (TH2F)h1->Clone("");
//
int nx = h_out->GetXaxis()->GetNbins();
int ny = h_out->GetYaxis()->GetNbins();
for (int i=1;i<=nx;i++) {
for (int j=1;j<=ny;j++) {
double c1 =h_out->GetBinContent(i,j);
if (c1 > 0) {
double c2 =h2->GetBinContent(i,j);
h_out->SetBinContent(i,j,c1-1.0
c2);
}
}
}

h_out->Draw();

###########################################################
By doing this, the result is as shown in Figure1. In the Figure1 it is clear that the sum is done correctly (as the number of entries in it is exactly equal to the sum of the entries in hist1 & hist2) . On the other hand it looks that the difference has gone crezy. The entries in this difference histogram (“hist1 - hist2”) make no sense at all. Also all the space is filled (black) compared to the case of addition. As it was a selective addition (i.e. only for c1>0), its appearence should be the same as of addition apart from the number of entries. Also I noticed that the difference histogram is taking very long time to generate (~ 15-20 min.) while the sum histogram takes comparatively very less time (~ 2 min.) The file size of - file1 = 2.7 MB & file2 = 1.27 MB.
The script itself takes some 20 min. to run & once the histograms are drawn, it takes ~ 5 minuits to save the canvas in jpg format.

Hoever, when I tried to do -
###########################################
TH2F h_out = (TH2F)hist1->Clone(“hist1 - hist2”);
h_out->SetDirectory(0);
MyC1->cd(6);
h_out->Draw();
//
#################################################
to subtract the 2 histograms, I got the correct number of entries (as shown in Figure2) in the difference histogram ('hist1 - hist2). But again it took exactly the same time (~ 20 minuits) to run the script, and the generated histogram is also completely filled (BLACK).
Do I need to add something more in the script ??
cylab123

Also, when posting code, please use the “code” button in the menu.

Rene

Hi !
Thanks for the reply. Here are the two data files attached along with the code.
cylab123

``````{
TCanvas *C1 = new TCanvas("MyC1","Fitting canvas",10,10,1400,1200);
C1->Divide(2,2);
//

C1->cd(1);
TFile *f1 = new TFile("file1.root", "read");
TH2F *h1 = (TH2F*)f1->Get("hist1");
h1->Draw();
//
C1->cd(2);
TFile *f2 = new TFile("file2.root", "read");
TH2F *h2 = (TH2F*)f2->Get("hist2");
h2->Draw();
//
// Addition of 2 histograms (hist1 + hist 2)
//
TH2F *hA = (TH2F*)h1->Clone("hist1 + hist2");
hA ->SetDirectory(0);
C1->cd(3);
hA->Draw();
//
// Difference of two histograms (hist1 - hist2)
//
C1->cd(4);
TH2F *h_out = (TH2F*)h1->Clone("hist1 - hist2");
//
int nx = h_out->GetXaxis()->GetNbins();
int ny = h_out->GetYaxis()->GetNbins();
//
for (int i=1;i<=nx;i++) {
for (int j=1;j<=ny;j++) {
double c1 =h_out->GetBinContent(i,j);
if (c1 > 0) {
double c2 =h2->GetBinContent(i,j);
h_out->SetBinContent(i,j,c1-1.0*c2);
}
}
}

h_out->Draw();

//
}``````

Hi !
Thanks for the reply. Here are the two data files attached along with the code.
cylab123

``````{
TCanvas *C1 = new TCanvas("MyC1","Fitting canvas",10,10,1400,1200);
C1->Divide(2,2);
//

C1->cd(1);
TFile *f1 = new TFile("file1.root", "read");
TH2F *h1 = (TH2F*)f1->Get("hist1");
h1->Draw();
//
C1->cd(2);
TFile *f2 = new TFile("file2.root", "read");
TH2F *h2 = (TH2F*)f2->Get("hist2");
h2->Draw();
//
// Addition of 2 histograms (hist1 + hist 2)
//
TH2F *hA = (TH2F*)h1->Clone("hist1 + hist2");
hA ->SetDirectory(0);
C1->cd(3);
hA->Draw();
//
// Difference of two histograms (hist1 - hist2)
//
C1->cd(4);
TH2F *h_out = (TH2F*)h1->Clone("hist1 - hist2");
//
int nx = h_out->GetXaxis()->GetNbins();
int ny = h_out->GetYaxis()->GetNbins();
//
for (int i=1;i<=nx;i++) {
for (int j=1;j<=ny;j++) {
double c1 =h_out->GetBinContent(i,j);
if (c1 > 0) {
double c2 =h2->GetBinContent(i,j);
h_out->SetBinContent(i,j,c1-1.0*c2);
}
}
}

h_out->Draw();

//
}``````

you posted twice file2.root, but not file1.root.
Take your time before posting, I will investigate tonight only.

Rene

Hi !
Sorry, for posting the same file twice. I just noticed that the size of ‘file1.root’ is (2.7MB) more than the permissible limit for attachment (2.0 MB) on Root Talk Forum and therefore could not be loded with my reply. To sortout this problem. I just send you all the three attachements (code.C, file1.root, file2.root) through my Yahoo account to you. Please try to reply soon.
cylab123

I am investigating this problem right now. With negative bins a scatter plot draws to many points. A workaround would be to use the option “scat” described here: root.cern.ch/root/html/THistPainter.html#HP11
But that’s only a workaround. I will fix it.