Subtracting multiple histogram with same TH2F

Hello,
I have a series of histograms that need to be added then subtracted in the final plot so I did following things

TCanvas* c6 = new TCanvas(“c6”, “”, 50, 50, 1000, 1000);
c6->Divide(2,2);
if(c6){

c6->cd(1);
TH2F *posXYdiff12 = new TH2F(“posXYdiff12”, “The distribution of neutrons escaped from the target geometry (XY), r1+2”, 401, -200.5, 200.5, 401, -200.5, 200.5);
EverythingN->Draw(“NPosY:NPosX>>posXYdiff12”, “reasonID==1”,“COLZ1”);
posXYdiff12->Add(posXYr2,1);

TH2F *posXYdiff23 = new TH2F(“posXYdiff23”, “The distribution of neutrons escaped from the target geometry (XY), r12+3”, 401, -200.5, 200.5, 401, -200.5, 200.5);
EverythingN->Draw(“NPosY:NPosX>>posXYdiff23”, “reasonID==3”,“COLZ1”);
posXYdiff23->Add(posXYdiff12,1);

c6->cd(2);
TH2F *posXYdiffr0 = new TH2F(“posXYdiffr0”, “The distribution of neutrons r-1 - (r1+r2+r3) (XY)”, 401, -200.5, 200.5, 401, -200.5, 200.5);
EverythingN->Draw(“NPosY:NPosX>>posXYdiffr0”, “reasonID==-1”,“COLZ1”);
posXYdiffr0->Add(posXYdiff23,-1);

Up to the point where cd(1), the plot is correct. However, when I did the subtraction, the number of entries are not matched up. If it is subtracting from posXYdiffr0, the number of entries showing in stat, should be 6050816.
I believe that the subtraction is not taking from the addition of reason id 1, and 3.

I really appreciate if you can help.

Thank you

I guess you could be interested in the TH1:Integral / TH2::Integral / TH3::Integral and / or the TH1::IntegralAndError / TH2::IntegralAndError / TH3::IntegralAndError methods.

When you “subtract” histograms, the “number of entries” is recalculated in a different way than when you “add” them so, you may get “unexpected” / “weird” values.

{
  TH1F *h1 = new TH1F("h1", "h1", 100, -5., 5.);
  h1->Sumw2(kTRUE);
#if 0 /* 0 or 1 */
  h1->FillRandom("gaus");
#else /* 0 or 1 */
  for (Int_t i = 0; i < 5000; i++) h1->Fill(gRandom->Gaus(), gRandom->Rndm());
#endif /* 0 or 1 */
  std::cout << h1->GetEntries() << std::endl;
  std::cout << h1->GetEffectiveEntries() << std::endl;
  TH1F *h2 = ((TH1F*)(h1->Clone("h2")));
  std::cout << h2->GetEntries() << std::endl;
  std::cout << h2->GetEffectiveEntries() << std::endl;
  h2->Add(h1, 1.0);
  std::cout << h2->GetEntries() << std::endl;
  std::cout << h2->GetEffectiveEntries() << std::endl;
  h2->Add(h1, -1.0);
  std::cout << h2->GetEntries() << std::endl;
  std::cout << h2->GetEffectiveEntries() << std::endl;
  std::cout << (h2->Integral() - h1->Integral()) << std::endl;
  gStyle->SetOptStat("nemruoi");
  TCanvas *c = new TCanvas("c", "c");
  h2->SetTitle("#color[2]{h2} = #color[3]{h1} + #color[3]{h1} - #color[3]{h1}");
  h2->SetLineColor(2); h1->SetLineColor(3);
  h2->Draw("E"); h1->Draw("SAME");
}

Thank you I ran the code you put it as an example.

I understand what you meant.

Hence, if I want to plot the subtracted histogram, am I suppose to set the entries?
like
posXYdiffr0->SetEntries(entries from h2 - entries from h1); ?

Hello I have changed the way to plot the subtraction.

  // Open file filled by Geant4 simulation
  TFile* f = new TFile("totalN.root","READ");
  TTree *EverythingN;
  f->GetObject("EverythingN", EverythingN);

c6->cd(1);
TH2F *posXYdiff12 = new TH2F("posXYdiff12", "The distribution of neutrons escaped from the target geometry (XY), r1+2", 401, -200.5, 200.5, 401, -200.5, 200.5);
EverythingN->Draw("NPosY:NPosX>>posXYdiff12", "reasonID==1","COLZ1");
posXYdiff12->Add(posXYr2,1);

TH2F *posXYdiff23 = new TH2F("posXYdiff23", "The distribution of neutrons escaped from the target geometry (XY), r12+3", 401, -200.5, 200.5, 401, -200.5, 200.5);
EverythingN->Draw("NPosY:NPosX>>posXYdiff23", "reasonID==3","COLZ1");
posXYdiff23->Add(posXYdiff12,1);
posXYdiff23->Draw("COLZ1");

c6->cd(2);
TH2F *posXYdiffr0 = new TH2F("posXYdiffr0", "The distribution of neutrons r-1 - (r1+r2+r3) (XY)", 401, -200.5, 200.5, 401, -200.5, 200.5);
EverythingN->Draw("NPosY:NPosX>>posXYdiffr0", "reasonID==-1","COLZ1");

TH2F *h_out = (TH2F*)posXYdiffr0->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 =posXYdiffr0->GetBinContent(i,j);
double c2 =posXYdiff23->GetBinContent(i,j);
h_out->SetBinContent(i,j,c1-c2);
h_out->Draw("COLZ1");
}
}
}

when I do this, I have this plot.


Basically I do not understand why the statistic is off. The number of entries on the graph right side should be 6050816 but it is no where close to that.

Please help me

Thank you

Can you provide an example we can run ? the file totalN.root is not available.

Yes sure the file is too large to post in here so I will share you the cernbox link:

https://cernbox.cern.ch/index.php/s/8eBgsIbkJ8Dfe1E

and here is the root script I was working on

plotNtuple_diffN.C (14.0 KB)

Thank you so much

I have also removed the Draw option “COLZ1”.

When I did that , I have everything blacked out like this

I ran your macro and do not get any black pad. What should I do to reproduce the problem ?

The problem is when you look at the c6 canvas.
If you look the plot like posXYdiffr0 , the problem is that the number of entries are not matching with what it suppose to be.

Secondarly, the blackout only creates when you remove COLZ1 draw option.