Can't fit a histogram loaded from file

In the below code, I read a histogram from a file and attach it to the variable c1. I used the exact same format as section 5.11 of this article. Then I attempt to fit and draw the histogram.

void rnf(){
	TFile hist("Gaussian.root");
	TH1F *c1=(TH1F*)hist.Get("c1;1");
	auto f=new TF1("f", "[0]*exp(-(x-[1])^2/2*[2]^2)",-15,20);
	c1->Fit("f");
	c1->Draw();
}

void Task4(){
	rnf();
}

The code works fine if I remove the Fit() part of the code, and it draws the histogram just fine. However, with the Fit() line added, the root session crashes without leaving an error message when I try to run it.
When I try to compile and run it, I get the error message “‘c1’ is not recognized as an internal or external command”.


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.20.06
Platform: Windows 10
Compiler: Not Provided


I do not have your ROOT file, therefore I tried to simulate the problem with the following macro:

void rnf(){
   auto c1 = new TH1F("c1", "c1", 100, -5., 5.) ;
   c1->FillRandom("gaus", 10000);
   auto f=new TF1("f", "[0]*exp(-(x-[1])^2/2*[2]^2)",-15,20);
   c1->Fit("f");
   c1->Draw();
}

void Task4(){
   rnf();
}

When I run this Task4.C it works just fine. I do not get a crash (I am on Mac).

Hello,

I had no problem fitting it to a histogram made in the same macro either. It’s only when I try to load the histogram from a root file that the problem occurs. Attached is the root file and a picture showing root quitting by itself when I try to run the macro. Note that the first function in my macro was used to generate the histogram, and I saved the histogram manually.

Gaussian.root (9.4 KB)

c1 is not an histogram but a canvas:

% rootls -l Gaussian.root
TCanvas  Jul 02 09:59 2020 c1  "c1"
1 Like