# Fit personal function and chi square getting

Hi everyone, I’ve to fit this plot it’s a power law

dN/dE = E^(-2)

I want the fit and the chi square and I want to write the results of the fit and chi square on the plot (in the statistical box)

I wrote the macro fitspettro.c but as you can see it doesn’t work because:

1. The line of the fit isn’t right
2. I don’t have the fit and chi square values in the box on the plot
3. On the root panel the values of the fit aren’t right (as I wrote it’s a power law E^-2, so  must be 2);
4. The box where there is written “Fit” is so big and on the plot, how can I change the size and move it on the right? Thanks
fitspettro.c (1.51 KB)

Try: ``` TF1 *fitspettro = new TF1("fitspettro", " * TMath::Power(x, )", 1, 5e3); fitspettro->SetParameters(1.0e6, -2.0);``` or: ``` TF1 *fitspettro = new TF1("fitspettro", "TMath::Power( * x, )", 1, 5e3); fitspettro->SetParameters(1.0e-3, -2.0);```

Hi pepe thanks! I modified the macro using your code (See attachment), now how you can see the red line of the fit is perfect, but:

1. Why did you set 2 parameters` SetParameters(1.0e-3, -2.0); ` and not just `-2` if it is a power lin dN/dE=E^-2? I think the 2 parameters are the reason because I have 2 parameters on the result of the fit too 2. I still can’t have the chi square values
3. I still can’t have the fit and chi square values in the issue box of the plot

Thanks for helping me
fitspettro.c (1.53 KB)

Right in the beginning of your macro, add: `gStyle->SetOptFit();`
You can also try (note below “SetParameter”, not “SetParameters”): ``` TF1 *fitspettro = new TF1("fitspettro", " / x / x", 1, 5e3); fitspettro->SetParameter(0, 1.0e6);```

Hi pepe and thanks, I updated the code as you wrote, now I have the values in the box but they are so big, because the p0 value is 7,47E5 (and it must be 2) and the chi square/ndf is 2595/853… Someone to fix the problem please? In the attachment the actual macro
fitspettro.c (1.64 KB)

Hi,

I guess ROOT in this situation is just a tool, which is working quite well. You now have a way to carry out your fit, right (the minimisation converged)? The value of the reduced chi2 is a consequence of the error on the data and your fit model, ultimately on the physics problem you are trying to solve.

Looking at the graphical results of your fits, I would try two fit options (for both types of your “fitspettro” functions):
hene->Fit(“fitspettro”, “L”); // Use Loglikelihood method (default is chisquare method)
and:
hene->Fit(“fitspettro”, “W”); // Set all weights to 1 for non empty bins; ignore error bars
and then choose the one that goes better through the data points.

[quote=“dpiparo”]Hi,

I guess ROOT in this situation is just a tool, which is working quite well. You now have a way to carry out your fit, right (the minimisation converged)? The value of the reduced chi2 is a consequence of the error on the data and your fit model, ultimately on the physics problem you are trying to solve.[/quote]

Hi dpiparo, the set of data is simulated by CORSIKA. As sure you know, cosmic ray spectrum is a power law

dN/dE (proportional to) E^-gamma
where gamma depends by the range energy.
The author of simulations told me that simulations uses a power low E^-2, this is the reason because I’m fitting using a power low E^-2

[quote=“Pepe Le Pew”]Looking at the graphical results of your fits, I would try two fit options (for both types of your “fitspettro” functions):
hene->Fit(“fitspettro”, “L”); // Use Loglikelihood method (default is chisquare method)
and:
hene->Fit(“fitspettro”, “W”); // Set all weights to 1 for non empty bins; ignore error bars
and then choose the one that goes better through the data points.[/quote]

Hi pepe, for the case

``` TF1 *fitspettro = new TF1("fitspettro", " / x / x", 1, 5e3); fitspettro->SetParameter(0, 1.0e6);```

this is

``hene->Fit("fitspettro", "L"); `` and this is

``hene->Fit("fitspettro", "W"); `` for the case

```TF1 *fitspettro = new TF1("fitspettro", "TMath::Power( * x, )", 1, 5e3); fitspettro->SetParameters(1.0e-3, -2.0);```
this is

``hene->Fit("fitspettro", "L"); `` and this is

``hene->Fit("fitspettro", "W"); `` as you can see the `hene->Fit("fitspettro", "L"); ` doesn’t work fine. The case `hene->Fit("fitspettro", "W"); ` draws good lines, but there is still the problem of the parameter value…

Try to limit the fit range (e.g. from 1 to 300 GeV, so that the the average bin contents > 10):
hene->Fit(“fitspettro”, “”, “”, 1, 300);
hene->Fit(“fitspettro”, “W”, “”, 1, 300);
hene->Fit(“fitspettro”, “L”, “”, 1, 300);

Hi pepe, I tried to fit limiting the energy range, but unfortunatly I’ve the same problem…

I was thinking that using the codes

[quote=“Pepe Le Pew”]Try: ``` TF1 *fitspettro = new TF1("fitspettro", " * TMath::Power(x, )", 1, 5e3); fitspettro->SetParameters(1.0e6, -2.0);``` or: ``` TF1 *fitspettro = new TF1("fitspettro", "TMath::Power( * x, )", 1, 5e3); fitspettro->SetParameters(1.0e-3, -2.0);```[/quote]
the values of the fit are good, because the power law of comsic rays is

dN/dE= c E^-gamma

maybe the author of the simulations told me that the simulations are a power law E^-2 just meaning
dN/dE proportional to E^-2 so I understood dN/dE=E^-2…but looking the plot it must be c=1e6 (As correctly pepe wrote to set parameters)…I’m sorry!