"Warning: Fit Data is Empty" with non-empty histo

HI everyone,

I’m trying to fit a vector of histograms, but when I execute the macro, after a while root 6 write this warning:
"Warning: Fit Data is Empty"
And when I try to draw the fitted function, root crashes.

Now I’ll explain better: the code is very long but basically I create two vector of TH1F, one for electron and one for muon.
I fit all the vector of electron without problem, with the follower function:

void vechisto_gaus_fit(vector<TH1F*> fit_hist) { 		//fit_hist is the vector that I want to fit
    for(i=0;i<fit_hist.size();i++) {
        TF1 *f = new TF1("Gaussian","gaus",-fit_hist.at(i)->GetRMS(),fit_hist.at(i)->GetRMS());
        f->SetParameter(0, fit_hist.at(i)->Integral()/2.5);             
        f->SetParameter(1, fit_hist.at(i)->GetMean());
        f->SetParameter(2, fit_hist.at(i)->GetRMS()/2);
        fit_hist.at(i)->Fit(f, "QMR","",-fit_hist.at(i)->GetRMS(), fit_hist.at(i)->GetRMS());	//here's the warning
        TF1 *myfunc = fit_hist.at(i)->GetFunction("Gaussian");  
        myfunc->Draw("same");						//here's the crash

No problem, since here.
When I try to use the same function in order to fit the other vector of histograms, the muon one, at the first iteration, at the instruction Fit, root writes the warning above, while at the instruction Draw (see comment on the code), root crashes. The crash report is here:

I read that this problem occurs when I’m trying to fit an histo with no entries. So, before calling this fit function, I wrote all vector of histo in a TFile, and i checked all histograms of the vector of muon, one by one, and no one of them is empty.

Please, help me!

… Fit(f, “QMRW”, …

BTW. shouldn’t you have something like:TF1 *f = new TF1("Gaussian", "gaus", fit_hist.at(i)->GetMean()-fit_hist.at(i)->GetRMS(), fit_hist.at(i)->GetMean()+fit_hist.at(i)->GetRMS()); // ... fit_hist.at(i)->Fit(f, "QMR", "", fit_hist.at(i)->GetMean()-fit_hist.at(i)->GetRMS(), fit_hist.at(i)->GetMean()+fit_hist.at(i)->GetRMS());

I did what you said, using “QMRW” options, but nothing changes…

Yes, yuo’re right. I used the range [-punt_hist->GetRMS(), punt_hist->GetRMS()] because all histo are centred almost in zero, but you’re way is more precise.

Nevertheless, root still crashes…

Try to define the fit function only once, before the “for” loop: TF1 *f = gROOT->GetFunction("Gaussian"); if (!f) f = new TF1("Gaussian", "gaus", -1, 1); and then inside of the “for” loop: f->SetRange(fit_hist.at(i)->GetMean()-fit_hist.at(i)->GetRMS(), fit_hist.at(i)->GetMean()+fit_hist.at(i)->GetRMS()); and finally, add a “protection”: if (myfunc) myfunc->Draw((gPad ? "same" : ""));

Ok: now if I follow your advise, root doesn’t crash, but it doesn’n fit the first two histo of the vector…
If I don’t use your check:

it crash at the first iteration.

However, the first two histo don’t have nothing of particular…

Sorry, I cannot understand