Miscalculation of errors when dividing a histogram retrieved from a root file

Root Version 6.10/04

Hello,

When you divide two histograms, ratio is calculated and error is also calculated as the ratio of errors of data and reference. Which is wrong as you also know because the calculation is not considering error propagation.

I realized that if the histograms are declared, filled or bin contents and error are set in the script, division works fine. If the bin content is given only but not bin error, then it works only if you call Sumw2 function before you divide the histogram.

Here comes the problem. When you retrieve histograms from a root file, clone data histogram into another histogram and divide, you realize that division is not working even if you call Sumw2 function before the division.
I was wondering if this is a known issue and/or has any fixes.

Here you can find four different methods to produce ratio and errors.

Plot at the top is the data and reference that I used to find the ratio.
At the bottom, there 4 ratio plots:

  1. Ratio plot: After getting histogram from root file, I have cloned it into another histogram by using Clone function and divided by reference.
    As you can see, errors of ratio histogram are being calculated automatically as the ratio of errors of data and reference which is wrong.
  2. Ratio plot: This time I have called Sumw2 function for the data, reference and cloned histogram before I divide.
    But it seems Sumw2 is not working as expected. Errors are being calculated in a wrong again.
    And I see this warning because of the Sumw2 function called by the clone histogram.
    “Warning in TProfile::Divide: Cannot preserve during the division of profiles the sum of bin weight square”
  3. Ratio Plot: Here, Instead of using Clone function, I have cloned the data with a loop. I have assigned bin contents and bin errors of data histogram to another histogram and divided by the reference.
    Resulting errors of the ratio histogram are as expected. This methods works fine.
  4. Ratio Plot: This plot was just to see the correct results of error propagation. I have set bin contents of this histogram by calculating the ratio of data and reference. Bin errors are calculated by using the error propagation formula.

Thanks a lot in advance.


ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Note that, with the TRatioPlot, you can use different “error options”, e.g. “pois” (the default), “divsym”, “diffsig” (with “errasym” or “errfunc”), “diff”, “cp”.

TRatioPlot did not solve this problem, errors on default option are nonsense. Also tried other options and calling Sumw2 on histograms but none of them showed the results same as the error propagation.

This is a bit odd. I have an example where I retrieve two histograms from root files, then do Sumw2() for each of them, clone the first one, and then Divide() this clone by the second one. The uncertainties of the ratio histogram are fine. At least in ROOT 5.30.06.

I can share this example if you are interested.

{
  TH1F *h1 = new TH1F("h1", "h1", 100, 0., 1.);
  h1->SetMarkerColor(kBlue); h1->SetLineColor(kBlue);
  h1->FillRandom("pol0");
  h1->Sumw2(1);
  TH1F *h2 = new TH1F("h2", "h2", 100, 0., 1.);
  h2->SetMarkerColor(kGreen); h2->SetLineColor(kGreen);
  h2->FillRandom("pol0");
  h2->Sumw2(1);
  TH1F *h3 = ((TH1F*)(h1->Clone("h3")));
  h3->SetMarkerColor(kRed); h3->SetLineColor(kRed);
  h3->Divide(h2);
  TCanvas *c = new TCanvas("c", "c");
  TRatioPlot *rp = new TRatioPlot(h1, h2, "pois");
  rp->Draw();
  rp->GetUpperPad()->cd();
  h1->Draw("SAME");
  rp->GetLowerPad()->cd();
  h3->Draw("SAME");
  c->cd(0);
}

Errors on division histogram are correct? I mean error propagation is considered? In my case errors on the ratio histogram are just calculated as the ratio of errors of data and reference.

If you fill or set bin contents of the histograms, Sumw2 works fine. In my case as I said, its not working if you retrieve the histogram from a file.

{
  TFile *f = TFile::Open("hsimple.root"); // ${ROOTSYS}/tutorials/hsimple.root
  TH1F *h1; f->GetObject("hpx", h1);
  h1->Sumw2(1);
  h1->SetMarkerColor(kBlue); h1->SetLineColor(kBlue);
  TH1F *h2 = ((TH1F*)(h1->Clone("h2")));
  h2->SetMarkerColor(kGreen); h2->SetLineColor(kGreen);
  TH1F *h3 = ((TH1F*)(h1->Clone("h3")));
  h3->SetMarkerColor(kRed); h3->SetLineColor(kRed);
  h3->Divide(h2);
  TCanvas *c = new TCanvas("c", "c");
  TRatioPlot *rp = new TRatioPlot(h1, h2, "divsym"); // "pois", "divsym", ...
  rp->Draw();
  rp->GetUpperPad()->cd();
  h1->Draw("SAME");
  rp->GetLowerPad()->cd();
  h3->Draw("SAME");
  c->cd(0);
}

Yes, they are correct.

Thanks for the answers guys. After your guidance, I have tested some more samples and saw that its working for TH1 files from the root file. Even if you do not call Sumw2. And it made me realize that the object I was retrieving at first was not TH1, it was TProfile. Hence it was needed to run ProjectionX() function before division. So thats why when I get it as TH1 it draws but errors were miscalculated. Thanks a lot for your helps to clear this out for me.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.