ex-4: fit plot added

This commit is contained in:
Giù Marcer 2020-04-28 23:44:24 +02:00 committed by rnhmjoj
parent 2864d9e524
commit 9641c0a39b
4 changed files with 40 additions and 19 deletions

View File

@ -9,13 +9,14 @@
// Process CLI arguments.
//
int parser(size_t *N, size_t *n, double *p_max, char argc, char **argv)
int parser(size_t *N, size_t *n, double *p_max, char argc, char **argv, size_t *go)
{
for (size_t i = 1; i < argc; i++)
{
if (!strcmp(argv[i], "-n")) *N = atol(argv[++i]);
else if (!strcmp(argv[i], "-b")) *n = atol(argv[++i]);
else if (!strcmp(argv[i], "-p")) *p_max = atof(argv[++i]);
else if (!strcmp(argv[i], "-o")) *go = 1;
else
{
fprintf(stderr, "Usage: %s -[hnbp]\n", argv[0]);
@ -23,6 +24,7 @@ int parser(size_t *N, size_t *n, double *p_max, char argc, char **argv)
fprintf(stderr, "\t-n N\tThe number of events to generate. (default: 50000)\n");
fprintf(stderr, "\t-b N\tThe number of bins of the histogram. (default: 50)\n");
fprintf(stderr, "\t-p PMAX\tThe maximum value of momentum. (default: 10)\n");
fprintf(stderr, "\t-o \tPrint histogram to stdout.\n");
return 0;
}
}
@ -35,18 +37,19 @@ int main(int argc, char **argv)
// Set default options.
//
size_t N = 50000; // number of events.
size_t n = 50; // number of bins.
double p_max = 10; // maximum value of momentum module.
int res = parser(&N, &n, &p_max, argc, argv);
if (res == 1)
size_t N = 50000;
size_t n = 50;
double p_max = 10;
size_t go = 0;
int res = parser(&N, &n, &p_max, argc, argv, &go);
if (go == 0)
{
printf("\nGenerating histogram with:\n"
"%ld points\n"
"%ld bins\n"
"p_max = %.3f\n\n", N, n, p_max);
}
else return EXIT_FAILURE;
else if (res == 0) return EXIT_FAILURE;
// printf("step: \t%.5f\n", step);
@ -116,12 +119,15 @@ int main(int argc, char **argv)
// Compute the mean value of each bin and print it to stodut
// together with other useful things to make the histogram.
//
// printf("bins: \t%ld\n", n);
// printf("step: \t%.5f\n", step);
if (go == 1)
{
printf("bins: \t%ld\n", n);
printf("step: \t%.5f\n", step);
}
for (size_t i = 0; i < n; i++)
{
histo[i].sum = histo[i].sum / histo[i].amo; // Average P_v
//printf("\n%.5f", histo[i].sum);
if (go == 1) printf("\n%.5f", histo[i].sum);
};
// Compare the histigram with the expected function:
@ -165,9 +171,12 @@ int main(int argc, char **argv)
double result = x;
double res_chi = chi2(result, &params);
printf("Results:\n");
printf("χ² = %.3f\n", res_chi);
printf("p_max = %.3f\n", result);
if (go == 0)
{
printf("Results:\n");
printf("χ² = %.3f\n", res_chi);
printf("p_max = %.3f\n", result);
}
// Compute the second derivative of χ² in its minimum for the result error.
//
@ -195,9 +204,19 @@ int main(int argc, char **argv)
B = exp2d(x, result) * (1 - pow((histo[i].sum / expecto), 2));
error = error + A + B;
};
error = 1/error;
printf("ΔP_max = %.3f\n\n", error);
error = 1/error;
if (go == 0) printf("ΔP_max = %.3f\n", error);
// Check compatibility
//
double t = fabs(result - p_max)/error;
double alpha = 1 - erf(t/sqrt(2));
if (go == 0)
{
printf("\nCompatibility:\n");
printf("t = %.3f\n", t);
printf("α = %.3f\n\n", alpha);
}
// Free memory.
//

View File

@ -12,7 +12,6 @@ def main():
y = f(x)
plt.rcParams['font.size'] = 20
plt.figure()
plt.title('Expected distribution', loc='right')
plt.ylabel('$\\langle |P_v| \\rangle$')
plt.xlabel('$P_h$')

BIN
notes/images/fit.pdf Normal file

Binary file not shown.

View File

@ -247,8 +247,11 @@ where $\Delta p_{\text{max}}$ is the absolute error of $p_{\text{max}}$. At 95%
confidence level, the values are compatible if $p > 0.05$.
In this case:
- t = 0.278
- p = 0.781
- t = 0.295
- p = 0.768
which allows to assert that the sampled points actually follow the predicted
distribution.
distribution. In @fig:fit, the fit function superimposed on the histogram is
shown.
![Fitted data.](images/fit.pdf){#fig:fit}