Adding weighted histograms now requires a call to ResetStats? Bug or feature?


ROOT Version: 6.18/02
Platform: linux
Compiler: gcc version 9.1.1 20190503


I’m transitioning from ROOT v5.34 to ROOT v6.18 and finding issues that break previously working analyses under the former version of ROOT.

In ROOT v5.34 if one takes histograms of, say, MC sources, scales them, then adds them, statistics such as mean and RMS reported by the summed histogram are the same as if one had generated a histogram in a single step using the resulting weights of this rescaled and summed histogram. In other words a change in normalization of the histogram does not change the mean and RMS.

Now in ROOT v6.18 if one performs the same exercise, the mean and RMS are very different. This effect appears as if scaling a histogram by some constant value will change the mean and RMS of the result.

Attached is an example

void test_hadd()
{

  // generate two MC source component distributions and sum
  TH1D *hSourceComp1 = new TH1D("hSourceComp1", "source 1", 20, -10, 10);
  TH1D *hSourceComp2 = new TH1D("hSourceComp2", "source 2", 20, -10, 10);
  TH1D *hSource = new TH1D("hSource", "comp1 + comp2", 20, -10, 10);

  for (size_t i = 0; i < 5000; ++i)
  {
    hSourceComp1->Fill(gRandom->Gaus(0., 1.));
    hSourceComp2->Fill(gRandom->Gaus(2., 1.));
  }

  // scale sources to (smaller) number of events reconstructed in the
  // ratio 1:3
#if ROOT_VERSION_CODE > ROOT_VERSION(5, 34, 36)
  hSourceComp1->Scale(0.25, "nosw2");
  hSourceComp2->Scale(0.75, "nosw2");
#else
  hSourceComp1->Scale(0.25);
  hSourceComp2->Scale(0.75);
#endif
  hSource->Add(hSourceComp1);
  hSource->Add(hSourceComp2);

  TH1D *hSourceWeightsOnly = new TH1D("hSourceWeightsOnly", "", 20, -10, 10);
  for (int j = 1; j < hSource->GetNbinsX() + 1; ++j)
    hSourceWeightsOnly->SetBinContent(j, hSource->GetBinContent(j));

}

In this case I generate two MC samples with different distributions, then mix them in different proportions into the resultant histogram named hSource. I also read out the hSource histo and generate one in which I fill a new one bin-by-bin by reading the weights. The mean in each histogram is 1.5. The following figure shows that mean and RMS approximately agree

Now, using ROOT v/6.18, if I run the same test macro, I get very different results. See the following figure. Here mean of hSource is 1.0 and the histo generated by copying the weights is 1.5. Now in ROOT 6.18 one has to use ResetStats() to get the two histograms to agree.

This change in behavior is unexpected. Is this a bug or an intended change to the way scaled histograms are added?

@moneta could you have a look here, please?

Hi,

This is a bug in the TH1::Scale function that does not update the global histogram statistics. This is not an issue if you are just scaling the histogram, but it will show up when adding two histograms.

There is an easy workaround for you: just call this TH1::Add function that adds two histograms and scales them:

hSource->Add(hSourceComp1,hSourceComp2,0.25,0.75);

Thank you for reporting this problem

Lorenzo

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