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);
hA->Add(hist2,1.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.0c2);
}
}
}
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);
h_out->Add(hist2,-1.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 ??
Please suggest.
cylab123
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.