General question about modeling

This is the same question I asked but in a wrong place. I think that’s why it was not responded. I have a list of 39 points in a table of data. I am supposed to find the best fit curve passing through all these points. I would like to see a sample example (Root has lots of histograms) that actually reads the file containing the table data and then doing a Xi-squared method to examine the “goodness” of the fit along with the best parameters found for the model. Is there such a thing for arbitrary models/theories? I would then need the best parametrization of the data folded within a 1D integral function to be plotted?

Thanks for all your help.

There are tons of examples available in the tutorials and in the ROOT documentation.
For example, you could start from:
root.cern.ch/root/htmldoc/guide … tml#graphs
and
root.cern.ch/root/htmldoc/guide … estimation

Thanks dplparo,
Here is my code:

[code]#include
#include <math.h>
#include “TFile.h”
#include “TF2.h”
#include “TH1.h”
#include “TH1F.h”
#include “TH2F.h”
#include “TCanvas.h”
#include “TFitResult.h”
#include “TMath.h”
#include “TGraphErrors.h”
#include “TGraph2DErrors.h”
#include “TLegend.h”
#include “TApplication.h”
#include “TGraphPolar.h”
#include “TLatex.h”
#include “TMinuit.h”
#include “TProfile.h”
#include “TRandom3.h”
#include “TStyle.h”

void format_line(TAttLine* line,int col,int sty){
line->SetLineWidth(5); line->SetLineColor(col);
line->SetLineStyle(sty);}

double the_model(double* vars, double* pars){
return pars[0]*std::pow(vars[0], pars[1])*std::pow((1-std::pow(vars[0], pars[2])), pars[3]);}

int myfit(){

TCanvas* c1=new TCanvas();
c1->SetGrid();
c1->SetLogy();

TGraphErrors graph_data("./ashur_input.txt", “%lg %lg %lg”);
graph_data.SetTitle(“Inclusive Differential Cross Section of Nucleon-Air Collisions; x_F; d(Sigma)/d(x_F)”);
graph_data.SetMarkerStyle(kCircle);
graph_data.SetFillColor(0);

graph_data.Print();

gStyle->SetOptTitle(0); gStyle->SetOptStat(0);
gStyle->SetOptFit(1111); gStyle->SetStatBorderSize(0);
gStyle->SetStatX(.89); gStyle->SetStatY(.89);

TF1 model(“model”, the_model,0,0.6);
double a=1; double b=1; double c=1; double d=1;
model.SetParameters(a,b,c,d);
model.SetParNames(“a”,“b”,“c”,“d”);
format_line(&model, kBlue,1);

model.SetParameter(0,1);
model.SetParameter(1,1);
model.SetParameter(2,1);
model.SetParameter(3,1);

TFitResultPtr frp = graph_data.Fit(&model, “S”);
frp->Print();
TMatrixDSym covMatrix (frp->GetCovarianceMatrix());
covMatrix.Print();

graph_data.GetYaxis()->SetRangeUser(10,1000000);
graph_data.DrawClone(“APE”);
}

#ifndef CINT
void StandaloneApplication(int argc, char** argv) {
// eventually, evaluate the application parameters argc, argv
// ==>> here the ROOT macro is called
myfit();
}
// This is the standard “main” of C++ starting
// a ROOT application
int main(int argc, char** argv) {
TApplication app(“ROOT Application”, &argc, argv);
StandaloneApplication(app.Argc(), app.Argv());
app.Run();
return 0;
}
#endif[/code]

I am not receiving any error message during compilation and linking. However, I am not getting the expected result. Here are the runtime error messages instead:

[quote]x[0]=0.00288, y[0]=581709, ex[0]=0, ey[0]=0
x[1]=0.01152, y[1]=491107, ex[1]=0, ey[1]=0
x[2]=0.02112, y[2]=400812, ex[2]=0, ey[2]=0
x[3]=0.0336, y[3]=327119, ex[3]=0, ey[3]=0
x[4]=0.04896, y[4]=249493, ex[4]=0, ey[4]=0
x[5]=0.05952, y[5]=210634, ex[5]=0, ey[5]=0
x[6]=0.07584, y[6]=166184, ex[6]=0, ey[6]=0
x[7]=0.09024, y[7]=145133, ex[7]=0, ey[7]=0
x[8]=0.10656, y[8]=114505, ex[8]=0, ey[8]=0
x[9]=0.11904, y[9]=96670.5, ex[9]=0, ey[9]=0
x[10]=0.1392, y[10]=76269.9, ex[10]=0, ey[10]=0
x[11]=0.15648, y[11]=60174.4, ex[11]=0, ey[11]=0
x[12]=0.17472, y[12]=49110.7, ex[12]=0, ey[12]=0
x[13]=0.1968, y[13]=37456.7, ex[13]=0, ey[13]=0
x[14]=0.2112, y[14]=32711.9, ex[14]=0, ey[14]=0
x[15]=0.22752, y[15]=27617, ex[15]=0, ey[15]=0
x[16]=0.24384, y[16]=23315.6, ex[16]=0, ey[16]=0
x[17]=0.26208, y[17]=18395.3, ex[17]=0, ey[17]=0
x[18]=0.28032, y[18]=15013.1, ex[18]=0, ey[18]=0
x[19]=0.29952, y[19]=12252.8, ex[19]=0, ey[19]=0
x[20]=0.31488, y[20]=10344.4, ex[20]=0, ey[20]=0
x[21]=0.33216, y[21]=8442.49, ex[21]=0, ey[21]=0
x[22]=0.34848, y[22]=7127.56, ex[22]=0, ey[22]=0
x[23]=0.36192, y[23]=6224.69, ex[23]=0, ey[23]=0
x[24]=0.37824, y[24]=5080.22, ex[24]=0, ey[24]=0
x[25]=0.39264, y[25]=4288.97, ex[25]=0, ey[25]=0
x[26]=0.41184, y[26]=3383.86, ex[26]=0, ey[26]=0
x[27]=0.42912, y[27]=2761.7, ex[27]=0, ey[27]=0
x[28]=0.4464, y[28]=2178.89, ex[28]=0, ey[28]=0
x[29]=0.45696, y[29]=1902.88, ex[29]=0, ey[29]=0
x[30]=0.47424, y[30]=1553.02, ex[30]=0, ey[30]=0
x[31]=0.49152, y[31]=1225.28, ex[31]=0, ey[31]=0
x[32]=0.5136, y[32]=873.326, ex[32]=0, ey[32]=0
x[33]=0.52992, y[33]=712.756, ex[33]=0, ey[33]=0
x[34]=0.54048, y[34]=622.469, ex[34]=0, ey[34]=0
x[35]=0.55296, y[35]=543.618, ex[35]=0, ey[35]=0
x[36]=0.56832, y[36]=428.897, ex[36]=0, ey[36]=0
x[37]=0.57888, y[37]=362.096, ex[37]=0, ey[37]=0
x[38]=0.59328, y[38]=285.682, ex[38]=0, ey[38]=0
Error in : function model has illegal number of parameters = 0
Error in : TFitResult is empty - use the fit option S
FitResult::Print - Error: Empty FitResult !
Error in : TFitResult is empty - use the fit option S
Warning in TFitResult::GetCovarianceMatrix: covariance matrix is not available
0x0 matrix is as follows
[/quote]

I am getting the data point read from the file but nothing about the fit. Can you see my mistakes?

Thanks

Error in <Fit>: function model has illegal number of parameters = 0

you are trying to fit a model w/o parameters.

I had actually tried

instead of

But it is not working.
I am also not sure whether the following line is correct.

When I change “the_model” to “model” in two instances, the program crashes with the following error:

[quote]The lines below might hint at the cause of the crash.
If they do not help you then please submit a bug report at
root.cern.ch/bugs. Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.

#9 0x2024448b in ?? ()
#10 0xb6fb7933 in TF1::TF1(TF1 const&) () from /home/…/pythia_root/root-6.02.08/lib/libHist.so.6.02
#11 0x0804d43e in myfit() ()
#12 0x0804d7e9 in StandaloneApplication(int, char**) ()
#13 0x0804d845 in main ()[/quote]

Oops! Got it! the syntax was in correct in the line

It should have been

Thanks again,

It seems I have a similar problem now with this fitting. Is anyone online to let me know what is wrong with my code?

[code]#include
#include <math.h>
#include “TFile.h”
#include “TF2.h”
#include “TH1.h”
#include “TH1F.h”
#include “TH2F.h”
#include “TCanvas.h”
#include “TFitResult.h”
#include “TMath.h”
#include “TGraphErrors.h”
#include “TGraph2DErrors.h”
#include “TLegend.h”
#include “TApplication.h”
#include “TGraphPolar.h”
#include “TLatex.h”
#include “TMinuit.h”
#include “TProfile.h”
#include “TRandom3.h”
#include “TStyle.h”

void format_line(TAttLine* line,int col,int sty){
line->SetLineWidth(3); line->SetLineColor(col);
line->SetLineStyle(sty);}

double the_model(double* vars, double* pars){
return pars[0] + pars[1] * std::pow(vars[0], pars[2]) ;}

int myfit(){

TCanvas* c1=new TCanvas();
c1->SetGrid();
c1->SetLogy();

TGraphErrors graph_data("./Digitized_SPSMassDistributionFunction.txt", “%lg %lg”);
graph_data.SetTitle(“SPS Mass Digitized_SPSMassDistributionFunction for z_f=0.5; Log M/[(h^(-1) * Solar Mass]; Log V^(-1) dN/dLog M [h^3 * Mpc^(-3)]”);
graph_data.SetMarkerStyle(kMultiply);
graph_data.SetFillColor(kBlack);

graph_data.Print();

gStyle->SetOptTitle(1); gStyle->SetOptStat(0);
gStyle->SetOptFit(1111); gStyle->SetStatBorderSize(0);
gStyle->SetStatX(.89); gStyle->SetStatY(.89);

TF1 model(“model”, the_model,8,15,3);
double a=1; double b=1; double c=1;
model.SetParameters(a,b,c);
model.SetParNames(“a”,“b”,“c”);
format_line(&model, kBlue,1);

model.SetParameter(0,4445935);
model.SetParameter(1,0.2354424);
model.SetParameter(2,5.55373);

TFitResultPtr frp = graph_data.Fit(&model, “S”);
frp->Print();
TMatrixDSym covMatrix (frp->GetCovarianceMatrix());
covMatrix.Print();

graph_data.GetYaxis()->SetRangeUser(10,1000000);
graph_data.DrawClone(“APE”);
}

#ifndef CINT
void StandaloneApplication(int argc, char** argv) {
// eventually, evaluate the application parameters argc, argv
// ==>> here the ROOT macro is called
myfit();
}
// This is the standard “main” of C++ starting
// a ROOT application
int main(int argc, char** argv) {
TApplication app(“ROOT Application”, &argc, argv);
StandaloneApplication(app.Argc(), app.Argv());
app.Run();
return 0;
}
#endif
[/code]

It is linked and compiled correctly. But when applying

I am receiving the following error message:

[quote]Warning in : Fit data is empty
Error in : TFitResult is empty - use the fit option S
FitResult::Print - Error: Empty FitResult !
Error in : TFitResult is empty - use the fit option S
Warning in TFitResult::GetCovarianceMatrix: covariance matrix is not available

0x0 matrix is as follows[/quote]

And the graphing page comes out empty.

"Fit data is empty " suggests that your graph_data is empty (e.g. your “./Digitized_SPSMassDistributionFunction.txt” is empty or improperly formatted).

Thanks Wile,
There were commas between two columns x and y in the data file. I removed those. Now, it works.

Note that you can easily specify the “delimiter”, see the TGraphErrors constructor description.

Hi,
I just decided to continue this topic to push things further so that it may be helpful for future students. I have learned to model a set of experimental data according to some theory that is known in advance and then find the best parameters that will fit the data based on Xi squared method. Now, I would like to extend the code to a level that it can actually evaluate/predict the “model” at a particular value which is given in terms of (mean+/-stdev). Given the best-fit parameters, the code should be able to take a single value and then give the result in (mean+/-stdev). Your help is greatly appreciated. Attached in my code:

[code]#include
#include <math.h>
#include “TFile.h”
#include “TF2.h”
#include “TH1.h”
#include “TH1F.h”
#include “TH2F.h”
#include “TCanvas.h”
#include “TFitResult.h”
#include “TMath.h”
#include “TGraphErrors.h”
#include “TGraph2DErrors.h”
#include “TLegend.h”
#include “TApplication.h”
#include “TFormula.h”
#include “TGraphPolar.h”
#include “TLatex.h”
#include “TMinuit.h”
#include “TProfile.h”
#include “TRandom3.h”
#include “TStyle.h”

void format_line(TAttLine* line,int col,int sty){
line->SetLineWidth(3); line->SetLineColor(col);
line->SetLineStyle(sty);}

double the_model(double* vars, double* pars){
/return pars[0] + pars[1] * std::pow(1 + pars[2] * std::pow(vars[0], pars[3]), -1) ;}/
if (vars[0] < pars[2])
return pars[0] * std::pow((vars[0] / pars[2]), pars[1]);
else
return pars[0] * std::pow((vars[0] / pars[2]), pars[3]) ;}

int myfit(){

TCanvas* c1=new TCanvas();
c1->SetGrid();

TGraphErrors graph_data("./mydata=.txt", “%lg %lg”);
graph_data.SetTitle(“Plot; X; Y”);
graph_data.SetMarkerStyle(kMultiply);
graph_data.SetFillColor(kBlack);

graph_data.Print();

gStyle->SetOptTitle(1); gStyle->SetOptStat(0);
gStyle->SetOptFit(1111); gStyle->SetStatBorderSize(0);
gStyle->SetStatX(.89); gStyle->SetStatY(.89);

TF1 model(“model”, the_model,18.0,24.0,4);
double a=1; double b=1; double c=1; double d=1;
model.SetParameters(a,b,c,d);
model.SetParNames(“a”,“b”,“c”,“d”);
format_line(&model, kBlue,1);

model.SetParameter(0, -22.93);
model.SetParameter(1, 1.385);
model.SetParameter(2, 21.06);
model.SetParameter(3, 2.853);

TFitResultPtr frp = graph_data.Fit(&model, “S”);
frp->Print();
TMatrixDSym covMatrix (frp->GetCovarianceMatrix());
covMatrix.Print();

graph_data.GetYaxis()->SetRangeUser(-29,-20);
graph_data.DrawClone(“APE”);
}

#ifndef CINT
void StandaloneApplication(int argc, char** argv) {
// eventually, evaluate the application parameters argc, argv
// ==>> here the ROOT macro is called
myfit();
}
// This is the standard “main” of C++ starting
// a ROOT application
int main(int argc, char** argv) {
TApplication app(“ROOT Application”, &argc, argv);
StandaloneApplication(app.Argc(), app.Argv());
app.Run();
return 0;
}
#endif[/code]

Sincerely,