Need help with adding/subtracting two TH2 histograms

Hi all,

Is there a way to add/subtract two TH2 histograms (something like the Add() function for TH1)?

thanks,

Aram.

Hi Aram,

you can use TH1::Add also for TH2 histograms. TH2 derives from TH1, so the method is available for TH2 - and it’s designed to also work for TH2. Just do TH2D* h2=new TH2D(...); h2->Add(h2, -1.);
Axel.

Dear Axel,

Yes, I saw that. but when I try to use the Add function and then draw the result.
the canvas is entirely filled. But the integral number of events seems to be correct though.

here is the macro that I use maybe you could tell me what am I doing wrong?


void diff()
{
  TFile* if1;
  TFile* if2;
  TFile* of;
  
  TH2F *h21;
  TH2F *h22;

  if1= new TFile("delphi_5158.root", "READ");
  h21 = (TH2F*)if1->Get("comp_cluster");
  h21->SetDirectory(0);
  if1->Close();
  h21->SetName("emty_tgt_cluster");
  
  
  if2= new TFile("delphi_5251.root", "READ");
  h22 = (TH2F*)if2->Get("comp_cluster");
  h22->SetDirectory(0);
  if2->Close();
  h22->SetName("tgt_cluster");




  of = new TFile("diff.root", "RECREATE");

  h21->SetDirectory(of);
  h22->SetDirectory(of);
  
  h22->Add(h21, -0.5.);
  
  h22->Draw();

  of->Write();
  of->Close();
}

thanks,
Aram.

PS. Let me know if you need to see the root data files.

Could you send your data files?
It could simply be that the resulting histograms contains negative values.
In this case, ROOT will renormalize the contents to plot a maximum
of around 500 dots in each bin. Try the draw option “box” for example

Rene

Hi Rene,
Here are the data files.

Aram.
delphi_5251.root (97.8 KB)
delphi_5158.root (58.1 KB)

OK, my guess was correct.
Your TH2 histograms have 1200x1200 bins. You just need to have an entry per cell to get a black picture.
Try my suggestion with

h22->Draw("box");instead of

Rene