root icon indicating copy to clipboard operation
root copied to clipboard

Loss of floating point precision when saving TCanvas as ROOT macro

Open guitargeek opened this issue 1 year ago • 0 comments

Check duplicate issues.

  • [ ] Checked for duplicates

Description

Floating point precision should be preserved when saving plots as ROOT macros with TCanvas::SaveAs(). This can probably be achieved with not too much effort using std::setprecision: https://en.cppreference.com/w/cpp/io/manip/setprecision

Reproducer

Run this code, which works fine:

TCanvas *c = new TCanvas("c", "c");
TH1D *histo = new TH1D("histo", "histo", 1, 595780500.110732, 595780531.878908);
histo->Draw();
c->SaveAs("plot.C");

The resulting macro will not preserve the precision on the floating point numbers:

   TH1D *histo__1 = new TH1D("histo__1","histo",1,5.957805e+08,5.957805e+08);

And you'll get an error when running it:

Processing plot.C...
Error in <TCanvas::Range>: illegal world coordinates range: x1=595780500.000000, y1=-0.131250, x2=595780500.000000, y2=1.181250
Error in <TCanvas::RangeAxis>: illegal axis coordinates range: xmin=595780500.000000, ymin=0.000000, xmax=595780500.000000, ymax=1.050000
root [1] Error in <TCanvas::Range>: illegal world coordinates range: x1=595780500.000000, y1=-0.131250, x2=595780500.000000, y2=1.181250
Error in <TCanvas::RangeAxis>: illegal axis coordinates range: xmin=595780500.000000, ymin=0.000000, xmax=595780500.000000, ymax=1.050000

ROOT version

   ------------------------------------------------------------------
  | Welcome to ROOT 6.32.06                        https://root.cern |
  | (c) 1995-2024, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for linuxx8664gcc on Sep 21 2024, 18:21:53                 |
  | From tags/6-32-06@6-32-06                                        |
  | With g++ (GCC) 13.3.0                                            |
  | Try '.help'/'.?', '.demo', '.license', '.credits', '.quit'/'.q'  |
   ------------------------------------------------------------------

Installation method

Nix

Operating system

NixOS (Linux)

Additional context

Can easily happen in the analysis of timeseries, where one often has large axis values because they represent time with respect to some reference point far back in time (e.g. astrophysics, finance, ...).

guitargeek avatar Oct 15 '24 11:10 guitargeek