Getting chi^2 for fitting of histogram in a loop

How would one go about getting chi^2 for fitting of a histogram in a for-loop? Consider the code in the attached file. In a different script, when running a for loop over the function defined in the attached code, the program stops and outputs “segmentation violation”. Is this due to the fact that, when calling the function, the histograms get drawn all over again? The aim is to get chi^2 for every fitting of the histogram in the script over a range of values for the parameter nRandom.

Fix a leak:
TF1 *fGaus = (TF1*)gROOT->GetFunction("gaus");

Your code should be fine if you keep nRandom < 1e6 or so. Otherwise, use:

  double *arr = new double[nRandom];
  // ...
  delete [] arr;

Thanks. How could one ignore the “Deleting TCanvas with same name…” warnings that arise when the function in the script is called in a for loop? Appending delete gROOT->FindObject(“canvas_name”); at the beginning of the script doesn’t do it.

Move the “Fit” statement to right after “canvas->cd(some_pad)”.

1 Like

Thanks. Why does this work?

Search for “draw” and “plot” in: TH1::Fit

1 Like

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