No values being passed by user defined fit function

Greetings all,

I am trying to perform a fit to a set of curves. I posted about this at the beginning of May and Lorenzo offered to help me with the fit, but the equation I was trying to fit to was incorrect and had to have a serious rethink of the physics of the situation before taking it forward.

The actual equation I need to fit is:

Sorry about the equation image and its size - I could get neither LaTex nor the BBcode size tags to work.

I have values for FW and Xk and I am trying to find the best global values for parameters G2 and G4. The various detector angles (theta) may give rise to somewhat different values for G2 and G4 but I am trying to get the single set of values that fits all four detector angles.

The various values for Xk are read from the ASCII file Xk.txt.

My many earlier attempts at this script had lots of badly formed C++ code, I think that I have tracked down most of the language errors.

I have tried to incorporate a simplified user-defined function “fTheta90” which contains just the numerator from the equation above (though I will eventually need to put the whole equation in). All that gets passed back by the user-defined function are values that approximate zero (not quite zero but infinitesimal).

I have tried making use of the various Xk values both in the form of arrays and as TGraphs but with no success.

Any guidance you can give on why only zero values that are being passed back by the user-defined function (and anything else I am doing wrong) would be greatly appreciated.

EDIT - I have tried the script in the following machine configs
ROOT 5.18/00b (branches/v5-18-00-patches@22563, Mar 20 2009, 00:45:00 on linuxx8664gcc)
CINT/ROOT C/C++ Interpreter version 5.16.29, Jan 08, 2008


ROOT 5.22/00 (trunk@26997, De 18 2008, 13:59:00 on win32)
CINT/ROOT C/C++ Interpreter version 5.16.29, Jan 08, 2008

Example_script.C (2.42 KB)
Example_script.C (2.42 KB)
PhiCanvas.root (16.6 KB)
Xk.txt (1.04 KB)

Could you post your Example_script.C file again. The one you posted seems to be deleted.


TGraph *fTheta90;
this has the same name as the function(which is also missing the return type Double_t). rename one of them
Double_t fTheta90(Double_t *x, Double_t *par)

in the function you need to use x[0] instead of x
return x0Theta90->Eval(x[0])+G2x2Theta90->Eval(x[0])+G4x4Theta90->Eval(x[0]);

set the numbers of parameters of FTheta90 to 2
FTheta90 = new TF1(“FTheta90”,fTheta90,0,180,2);
then set some parameters for FTheta90:

then it should draw

attached modified script
Example_script.C (2.63 KB)

Dear Rene,

Sorry for the missing file - I was trying to make the script clearer so I was trying to upload a newer version with a few more comments.

Dear Madcow,

Thank you for the tips - the script now returns non-zero values. I have a lot more work to do to get the values correct - but it is much easier when I have some value to work with.


Your script had many C++ problems. I strongly suggest to compile your scripts with ACLIC if you are new to C++. It is so simple. Instead of

root > .x Example_script.C do

root > .x Example_script.C+ Look at the attachment file, you reuse the same name for different objects and you also have many objects not defined. Compare every single line with your original.

Example_script.C (2.54 KB)

Dear Rene,

I apologise for the many coding errors. I will study your corrected version of the script closely and try to avoid such errors in future.


Dear Rene,

I have gone over your example script carefully and I have found it very instructive.

Your corrected version of the script does draw the FTheta90 curve but it does not appear to perform a fit to the xTheta90 curve on the PhiCanvas. The PhiCanvas.root file is opened in your script but the canvas is not drawn and does not appear to be used.

I am sorry that my original message was unclear. The intended purpose of the original example script was to perform fits to the curves on the PhiCanvas. Because of my coding errors I could only get zero values for the FTheta90 curve and so no fit was possible.

Any guidance you can give on how to perform the fit would be appreciated - my attempts building upon your example script have so far resulted in either the replacement of EDIT hTheta90 curve (mistyped as xTheta90 originally) with the FTheta90 or segfaults.


just get the Graph,Histogram or whatever holds the points and call the Fit function with your fitting function as argument

attached is a script which fits the Graph in the first Pad of the PhiCanvas (as it kind of looks like the function :slight_smile: )

First it gets the Canvas from the root file:
TCanvas * PhiCanvas = (TCanvas*)file->Get(“PhiCanvas”);
(note the type cast, Get() returns TObject from which TCanvas inherits)

then it gets the first pad:
TVirtualPad * pad1 = PhiCanvas->cd(1);
if (pad1)

and search for the Graph Object (which is named Graph in this case) and fits it.
TGraphErrors * gr = (TGraphErrors*)pad1->FindObject(“Graph”);

You can inspect what is in the canvas with
thats how I found the name of the Graph

of course you can also fit any of your created TGraphs in exact the same way.
Just set start parameters of the fit function call its TGraphs Fit() function.

the attached script also compiles so there are some changes.
Please check if it still does what you want.
Example_script.C (3.11 KB)

Dear Madcow,

Thank you for the corrected and updated example script.

My problem was not that I didn’t know to call Fit method - it was that I kept getting seg-faults when I tried to do the actual fit (or have my fit replace the pre-existing curve).

Whatever I was doing wrong is avoided in your script so I can use your script as an example and work forward from there.


Greetings all,

Not a question this time but an explanation of progress and what I have found about the causes of my various seg-faults. I am putting these up in case somebody experiences similar problems in future, in hopes that finding this short note might save them some time.

Firstly I had never realised how useful ACLIC was as a tool for revealing coding problems - thanks to Rene for re-emphasising its importance.

Secondly - I did not realise that I needed to use SetName to give my various TGraphs/TGraphErrors the names that I was using to try to call them. I was assuming that on every pad of my canvas there was a TGraph to fit with a name such as gTheta90, when all of the objects actually had the default name of “Graph”. Thanks to MadCow for pointing out that every TGraph on my canvas was uniformly named “Graph”. A considerable number of my problems can be traced to objects on my canvas having names that were different than what I thought they were. Making consistent use of commands such as PhiCanvas->GetListOfPrimitives()->ls() would have saved me a tremendous amount of lost time.

Thirdly - when I gave more meaningful names to my various TGraphs during their creation I seem to have picked some names that caused problems. One family of curves with the following names: “hThetaNeg115”, “hThetaNeg115x” and “hThetaNeg115y” caused problems when used as follows: gr3 = (TGraphErrors*)pad3->FindObject("hThetaNeg115"); For some reason use of these names resulted in each of the TGraphErrors on pad3 being deleted. While I don’t understand the problem I could work around it by replacing “hThetaNeg115” with “hThetaneg115” in the script which generates the canvas and working forward from there.

I can now generate the fits to my experimental data without generating any seg-faults. The fits produce parameters that are physically meaningful and consistent with expectations for the measurments we are performing.

Thanks to both Rene and MadCow for the invaluable assistance.


You are welcome. We are here to answer questions ::slight_smile: