Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RF] Make plotting of histograms work for all orders of magnitude #13316

Merged
merged 2 commits into from
Aug 4, 2023

Conversation

guitargeek
Copy link
Contributor

@guitargeek guitargeek commented Jul 25, 2023

The RooFit plotting did not work well for variables of small orders of
magnitude, because there was a hardcoded check with an absolute value to
see if two points are considered the same: x2 - x1 < 1e-20. If this
condition is true, the point at x2 was not plotted. This meant that some
points that were actually different were skipped.

The fix is to change this condition to a relative one, considering xmin
and xmax. The code to generate sampling hints for histograms was also
moved inside RooCurve, such that it's easier to syncronize the relevant
magic constants.

Here is a demo of a plot that didn't work before but now works:

void repro()
{

   int nBins = 4;

   double scale = 1e-31;

   double xmin = 0. * scale;
   double xmax = 1. * scale;
   double binWidth = (xmax - xmin) / nBins;

   // Fill the bin boundaries
   std::vector<double> binBoundaries(nBins + 1);
   for (int i = 0; i <= nBins; ++i) {
      binBoundaries[i] = i * binWidth;
   }

   // The RooParametricStepFunction needs a TArrayD
   TArrayD step_edges{int(binBoundaries.size()), binBoundaries.data()};

   RooRealVar Gamma("Gamma", "Gamma", xmin, xmax);

   RooArgList step_values;
   step_values.addOwned(std::make_unique<RooRealVar>("coef1", "coef1", 0.0 / scale, 0.0, 1.0 / scale));
   step_values.addOwned(std::make_unique<RooRealVar>("coef2", "coef2", 0.1 / scale, 0.0, 1.0 / scale));
   step_values.addOwned(std::make_unique<RooRealVar>("coef3", "coef3", 0.1 / scale, 0.0, 1.0 / scale));

   RooParametricStepFunction prior("prior", "Prior for decay rate", Gamma, step_values, step_edges, nBins);

   RooPlot *xframe = Gamma.frame(xmin, xmax);

   prior.plotOn(xframe);

   auto c1 = new TCanvas();

   xframe->Draw();

   c1->SaveAs("plot.png");
}

This bugfix was inspired by the following forum post:
https://root-forum.cern.ch/t/rooparametricstepfunction/55820

@github-actions
Copy link

github-actions bot commented Jul 25, 2023

Test Results

         9 files           9 suites   1d 21h 58m 22s ⏱️
  2 476 tests   2 473 ✔️ 0 💤 3
21 183 runs  21 180 ✔️ 0 💤 3

For more details on these failures, see this check.

Results for commit 976a55d.

♻️ This comment has been updated with latest results.

@root-project root-project deleted a comment from phsft-bot Jul 25, 2023
@root-project root-project deleted a comment from phsft-bot Jul 25, 2023
@root-project root-project deleted a comment from phsft-bot Jul 25, 2023
@root-project root-project deleted a comment from phsft-bot Jul 25, 2023
@root-project root-project deleted a comment from phsft-bot Jul 25, 2023
@root-project root-project deleted a comment from sonatype-lift bot Aug 1, 2023
@root-project root-project deleted a comment from phsft-bot Aug 1, 2023
@root-project root-project deleted a comment from phsft-bot Aug 1, 2023
@root-project root-project deleted a comment from phsft-bot Aug 1, 2023
@phsft-bot
Copy link
Collaborator

Starting build on ROOT-debian10-i386/soversion, ROOT-performance-centos8-multicore/cxx17, ROOT-ubuntu2204/nortcxxmod, ROOT-ubuntu2004/python3, mac11/noimt, mac12arm/cxx20, windows10/cxx14
How to customize builds

@phsft-bot
Copy link
Collaborator

Build failed on ROOT-ubuntu2004/python3.
Running on root-ubuntu-2004-3.cern.ch:/home/sftnight/build/workspace/root-pullrequests-build
See console output.

Failing tests:

Copy link
Member

@lmoneta lmoneta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!
It makes sense to use a relative epsilon value instead of an absolute one

The RooFit plotting did not work well for variables of small orders of
magnitude, because there was a hardcoded check with an absolute value to
see if two points are considered the same: `x2 - x1 < 1e-20`. If this
condition is true, the point at x2 was not plotted. This meant that some
points that were actually different were skipped.

The fix is to change this condition to a relative one, considering xmin
and xmax. The code to generate sampling hints for histograms was also
moved inside RooCurve, such that it's easier to syncronize the relevant
magic constants.

Here is a demo of a plot that didn't work before but now works:

```
void repro()
{

   int nBins = 4;

   double scale = 1e-31;

   double xmin = 0. * scale;
   double xmax = 1. * scale;
   double binWidth = (xmax - xmin) / nBins;

   // Fill the bin boundaries
   std::vector<double> binBoundaries(nBins + 1);
   for (int i = 0; i <= nBins; ++i) {
      binBoundaries[i] = i * binWidth;
   }

   // The RooParametricStepFunction needs a TArrayD
   TArrayD step_edges{int(binBoundaries.size()), binBoundaries.data()};

   RooRealVar Gamma("Gamma", "Gamma", xmin, xmax);

   RooArgList step_values;
   step_values.addOwned(std::make_unique<RooRealVar>("coef1", "coef1", 0.0 / scale, 0.0, 1.0 / scale));
   step_values.addOwned(std::make_unique<RooRealVar>("coef2", "coef2", 0.1 / scale, 0.0, 1.0 / scale));
   step_values.addOwned(std::make_unique<RooRealVar>("coef3", "coef3", 0.1 / scale, 0.0, 1.0 / scale));

   RooParametricStepFunction prior("prior", "Prior for decay rate", Gamma, step_values, step_edges, nBins);

   RooPlot *xframe = Gamma.frame(xmin, xmax);

   prior.plotOn(xframe);

   auto c1 = new TCanvas();

   xframe->Draw();

   c1->SaveAs("plot.png");
}
```

This bugfix was inspired by the following forum post:
https://root-forum.cern.ch/t/rooparametricstepfunction/55820
@phsft-bot
Copy link
Collaborator

Starting build on ROOT-debian10-i386/soversion, ROOT-performance-centos8-multicore/cxx17, ROOT-ubuntu2204/nortcxxmod, ROOT-ubuntu2004/python3, mac11/noimt, mac12arm/cxx20, windows10/default
How to customize builds

@phsft-bot
Copy link
Collaborator

@guitargeek guitargeek merged commit e5b7ba0 into root-project:master Aug 4, 2023
7 of 13 checks passed
@guitargeek guitargeek deleted the step_function branch August 4, 2023 11:07
@phsft-bot
Copy link
Collaborator

Build failed on ROOT-debian10-i386/soversion.
Running on pcepsft11.dyndns.cern.ch:/home/sftnight/build/workspace/root-pullrequests-build
See console output.

Errors:

  • [2023-08-06T02:12:08.718Z] CMake Error at /home/sftnight/build/workspace/root-pullrequests-build/rootspi/jenkins/root-build.cmake:1144 (message):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants