# Saving/loading canvases in .C format after using roofit

**URL:** <https://root-forum.cern.ch/t/saving-loading-canvases-in-c-format-after-using-roofit/62463>\
**Category:** Roofit and RooStats\
**Tags:** graphics, python\
**Created:** [December 12, 2024, 7:19pm UTC](https://root-forum.cern.ch/t/saving-loading-canvases-in-c-format-after-using-roofit/62463 "2024-12-12T19:19:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![abrac](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/e47c2d/32.png) [@abrac](https://root-forum.cern.ch/u/abrac)\
**Post date:** [December 12, 2024, 7:19pm UTC](https://root-forum.cern.ch/t/saving-loading-canvases-in-c-format-after-using-roofit/62463/1 "2024-12-12T19:19:40Z")

</div>

Hello,

I am using roofit in pyROOT to fit to some data and I am interested in saving the resulting plot in .C format. When I plot just the roodatahist (prior to applying and plotting any fit), I am able to save the canvas using the File\>Save as option. When I try to reload this file in root, or using a TBrowser, the correct plot opens with no issues. However, once I apply the fit and plot it, this same exact procedure saves the .C file, but when trying to rerun it in root, it crashes. I’m not really sure what the issue is. I’ve attached a python file that reproduces this issue. When trying to open test\_nofit.C in root, there are no issues, but when trying to open test\_fit.C, it fails. Can somebody show me a better way to do this so that I can load these files with no issues?  
[saving\_test.py](https://root-forum.cern.ch/uploads/short-url/diCiHjNnuOia2CRKAaAHi5R1qGB.py) (1.3 KB)  
[test\_fit.C](https://root-forum.cern.ch/uploads/short-url/ayziJs0oSdn6WSYDKz0X2YxtoFw.C) (10.1 KB)  
[test\_nofit.C](https://root-forum.cern.ch/uploads/short-url/cQyJLRABNhLJWrc58kMNUJBUJqe.C) (6.4 KB)

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [December 12, 2024, 8:31pm UTC](https://root-forum.cern.ch/t/saving-loading-canvases-in-c-format-after-using-roofit/62463/2 "2024-12-12T20:31:32Z")

</div>

Hi,

Sorry to read ROOT did not work out of the box for you in this case.  
Thanks for the reproducer: I tried it with master and 6.34, however, I do not manage to reproduce the crash when running the macros representing the plot.

What ROOT version are you using?

Cheers,  
Danilo

---

<div class="post-metadata">

**Author:** ![abrac](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/e47c2d/32.png) [@abrac](https://root-forum.cern.ch/u/abrac)\
**Post date:** [December 12, 2024, 8:40pm UTC](https://root-forum.cern.ch/t/saving-loading-canvases-in-c-format-after-using-roofit/62463/3 "2024-12-12T20:40:51Z")

</div>

Hi,

Thanks for your response! I’m running root version 6.30/04, which I built from the conda package a couple of months ago.

How are you running the macros? I’m doing so from a root interactive session in terminal. Would it be helpful if I attached a picture of the errors I’m encountering?

Thanks again!

---

<div class="post-metadata">

**Author:** ![jonas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jonas/32/24756_2.png) [@jonas](https://root-forum.cern.ch/u/jonas)\
**Post date:** [December 13, 2024, 2:29am UTC](https://root-forum.cern.ch/t/saving-loading-canvases-in-c-format-after-using-roofit/62463/4 "2024-12-13T02:29:45Z")

</div>

Hello! I could reproduce the error with ROOT 6.30.04 (see bottom of this post).

The good news is that the problem is simple: invalid C++ variable names are used in the dumped `.C` code, and that this got fixed in ROOT 6.32.02 onward. Please use that ROOT version for a smoother experience.

In case you want to stick an older ROOT version, I’m afraid you have to work around this problem my manually fixing the generated `.C` code.

Cheers!  
Jonas

```txt
Processing test_fit.C...
In file included from input_line_8:1:
/afs/cern.ch/user/r/rembserj/test_fit.C:107:27: error: use of undeclared identifier 'x'
   Double_t gaussian_Norm[x]_fx1[134] = { -20.02, -20, 0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200, 220, 240, 260, 280,
                          ^
/afs/cern.ch/user/r/rembserj/test_fit.C:107:29: error: expected ';' at end of declaration
   Double_t gaussian_Norm[x]_fx1[134] = { -20.02, -20, 0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200, 220, 240, 260, 280,
                            ^
                            ;
/afs/cern.ch/user/r/rembserj/test_fit.C:116:27: error: use of undeclared identifier 'x'
   Double_t gaussian_Norm[x]_fy1[134] = { 0, 4.146547e-19, 4.146547e-19, 2.619263e-18, 1.594203e-17, 9.349329e-17, 5.283106e-16, 2.876539e-15, 1.509119e-14, 7.628666e-14, 3.715746e-13, 1.743875e-12, 7.886005e-12, 3.436139e-11, 1.442636e-10, 5.835991e-10, 2.274808e-09,
                          ^
/afs/cern.ch/user/r/rembserj/test_fit.C:116:29: error: expected ';' at end of declaration
   Double_t gaussian_Norm[x]_fy1[134] = { 0, 4.146547e-19, 4.146547e-19, 2.619263e-18, 1.594203e-17, 9.349329e-17, 5.283106e-16, 2.876539e-15, 1.509119e-14, 7.628666e-14, 3.715746e-13, 1.743875e-12, 7.886005e-12, 3.436139e-11, 1.442636e-10, 5.835991e-10, 2.274808e-09,
                            ^
                            ;
/afs/cern.ch/user/r/rembserj/test_fit.C:125:49: error: use of undeclared identifier 'x'
   TGraph *graph = new TGraph(134,gaussian_Norm[x]_fx1,gaussian_Norm[x]_fy1);
                                                ^
/afs/cern.ch/user/r/rembserj/test_fit.C:125:70: error: use of undeclared identifier 'x'
   TGraph *graph = new TGraph(134,gaussian_Norm[x]_fx1,gaussian_Norm[x]_fy1);

```

---

<div class="post-metadata">

**Author:** ![abrac](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/e47c2d/32.png) [@abrac](https://root-forum.cern.ch/u/abrac)\
**Post date:** [December 13, 2024, 2:02pm UTC](https://root-forum.cern.ch/t/saving-loading-canvases-in-c-format-after-using-roofit/62463/5 "2024-12-13T14:02:13Z")

</div>

Hi jonas! Yup, that’s exactly the error! I did have a friend try it on their machine (they homebrew installed root, so their version should be 6.32.08) and they also bumped into the issue. I’m updating root on my linux machine now to see if the error persists there. I’ll update here if anything!

Cheers,  
Aitor

---

<div class="post-metadata">

**Author:** ![abrac](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/e47c2d/32.png) [@abrac](https://root-forum.cern.ch/u/abrac)\
**Post date:** [December 13, 2024, 4:45pm UTC](https://root-forum.cern.ch/t/saving-loading-canvases-in-c-format-after-using-roofit/62463/6 "2024-12-13T16:45:04Z")

</div>

Alrighty, reinstalled conda with homebrew and it works now! The only caveat is that the file HAS to be generated with the new pyROOT. Trying to load the old test\_fit.C file (generated using root 6.30) with my updated root 6.32 still reproduces the error. This might be why the error still occurred on my friend’s machine. However, rerunning the reproducer with the new version of pyROOT and then opening the new test\_fit.C works!

Thanks so much for your help!

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [December 27, 2024, 4:46pm UTC](https://root-forum.cern.ch/t/saving-loading-canvases-in-c-format-after-using-roofit/62463/7 "2024-12-27T16:46:01Z")

</div>

Closed on December 27, 2024, 4:46pm UTC

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