Problem storing a TF1 function

Hello everyone!
I have a very strange problem when I store a TF1 function in a ROOT file and then I plot it. If I define a function and I plot it, it looks great. If I save this function to a ROOT file, then open that ROOT file and plot again the previous function it looks different, very different! For example you can run these lines in the command line to see the error what I talking about:

root [0] TFile *rout = new TFile(“test.root”,“RECREATE”);
root [1] double sNeutronsParametrization(double *x, double *par) { return par[0] + par[1]/sqrt(x[0]);}
root [2] TF1 *fn = new TF1(“fn”,sNeutronsParametrization,5e-2,1e6,2);
root [3] fn->SetParameters(193.67300,422.51700)
root [4] fn->Draw(“APL”)
Info in TCanvas::MakeDefCanvas: created default TCanvas with name c1
root [5] fn->Write(“fn”)
(int) 944
root [6] .q

If you now open the “test.root” file and plot the previous saved function you will see the problem:

root [0] Attaching file test.root as _file0…
root [1] .ls
TFile** test.root
TFile* test.root
KEY: TF1 fn;1 fn
root [2] fn->Draw(“APL”)

I have found this problem in a compiled script so it has nothing to do with the command line. The ROOT version that I am running is the 6.22/02

Hi @vbabiano,
and welcome to the ROOT forum!

I can reproduce the issue with the following compilable snippet:

 #include <TApplication.h>
#include <TFile.h>
#include <TF1.h>
#include <TCanvas.h>

double sNeutronsParametrization(double *x, double *par)
   return par[0] + par[1] / sqrt(x[0]);

int main()
   TApplication app("app", nullptr, nullptr);

   TFile f1("test.root", "RECREATE");
   TF1 fn("fn", sNeutronsParametrization, 5e-2, 1e6, 2);
   fn.SetParameters(193.67300, 422.51700);

   TCanvas c;
   TFile f2("test.root");
   auto fn2 = f2.Get<TF1>("fn");

   return 0;

but I am not sure what is going on yet, I’ll get back to you as soon as I do (early next week).


Thanks you very much Enrico,
I will be waiting for your messages.


ROOT Forum -> Search -> “cannot be cloned”

1 Like

Thanks you very much Wile_E_Coyote,
Now I can see the problem. My apologies, I should have looked better before writing.

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