How to save a user-defined function

I would like to save a canvas, that contains a user defined function, as a .C file.

I’ve tried creating the TF1 using a CLING function or a general C function.

When I try to open the canvas .C file, I get either the errors:

Error in TF1::TF1: No function found with the signature myTF1(Double_t,Double_t*)
Error in TClingCallFunc::IFacePtr(kind): Attempt to get interface while invalid.
Error in TClingCallFunc::ExecT: Called with no wrapper, not implemented!

or

Processing exampleCanvas.C…
input_line_45:2:66: error: use of undeclared identifier ‘customFunction’
Double_t TFormula____id16614074293475595597(Double_t x){ return customFunction(x[0]) ; }
^
Error in TFormula::Eval: Can’t find TFormula____id16614074293475595597 function prototype with arguments Double_t

Error in TFormula::ProcessFormula: Could not find customFunction function with 1 argument(s)
Error in TFormula::ProcessFormula: Formula “customFunction(x)” is invalid !
Error in TFormula::Eval: Formula is invalid and not ready to execute
customFunction is unknown.

The only work around I can find is using the GetHistogram method of TF1, using that to draw and save the function instead. However, this does not look nice as the histogram drops to 0 at the end of the functions range.example.C (473 Bytes)

The attached macro demonstrates what I am trying to do: run the macro, then try to open the canvas saved as a .C file.

Could someone please explain what I should do?

Thanks in advance


ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Function defined as a C function can be saved in a C file.
@moneta may explain.

I have the same issue. Calling TCanvas::SaveAs(“file.C”) when the plot contains a user defined function produces “file.C” that does not work in ROOT6. This process works in ROOT5 though. Is this a feature or a bug?

A workaround is to use TGraph::TGraph(TF1 *):
TGraph *g = new TGraph(myTF1);
g->Draw(“a,L”);
myCanvas->SaveAs(“exampleCanvas.C”);

Then
root exampleCanvas.C
works and reproduces the plot.

Can you post the reproducer file.C ?

@couet is there a way to delay this topic being closed after 14 days please? @moneta is yet to reply

Hi James, you just did! :smiley: Topics are only closed after 14 days of inactivity, pinging us does delay closing the topic

Ok, I have created a simplified version that illustrates what I was trying to do, it’s called example_modified.C (478 Bytes). Running
root -b -q example_modified.C produces two files:
exampleCanvas_does_not_work.C
exampleCanvas_works_fine.C

root exampleCanvas_works_fine.C produces a nice TGraph version of the function

root exampleCanvas_does_not_work.C , on the other hand, says
Error in TF1::TF1: No function found with the signature myTF1(Double_t,Double_t*)
and then it proceeds to say
Error in TClingCallFunc::IFacePtr(kind): Attempt to get interface while invalid.
Error in TClingCallFunc::ExecT: Called with no wrapper, not implemented!

~400 times

So my example is different from that of the OP in the sense that it’s implementing the custom function
as Double_t f(Double_t*, Double_t*), where the first argument is the coordinates and the second argument is the parameters. Then the canvas saves the plot using “TF1::SetSavedPoint” method. This used to work fine in ROOT5 but stopped working in ROOT6.

Hi,

Thank you for providing this example. It is true this feature does not work anymore. It would be great if you could open a JIRA open issue for this and we will try to restore it as soon as possible

Best regards

Lorenzo

This problem should be fixed by this PR

Thank you for reporting the issue. It was broken since 6.04

Thanks, I didn’t expect you would get to this so quickly.

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