I am trying to do a series of gaussian fits on a histogram. I want to save these fits with the histogram so that other programs can have access to the fit results, without necessarily having to repeat the fits. I make two TF1*s called gaus60 and gaus26, initialize them with some estimated parameters and successfully fit them to the histogram. I then try to add each one to the list of functions associated with this histogram. The first Add(gaus60) works as expected. The second Add(gaus26) adds to the list of functions and expands its capacity to two, but the list then contains two identical copies of gaus26 and no vestige of gaus60. I don’t understand this behavior or I have misunderstood the purpose and use of the list of functions. Does anyone see the problem?
{
gROOT->Reset();
TFile f1("listOfFunctionsEx.root", "update");
TH1F* h1 = (TH1F*) f1.Get("adcSpec_0");
Double_t meanGuess = 1450.;
Double_t sigmaGuess = 15.;
TF1* gaus60 = new TF1("gaus60", "gaus", meanGuess-1.*sigmaGuess, meanGuess+3.*sigmaGuess);
gaus60->SetParameter(0, 1000);
gaus60->SetParameter(1, meanGuess);
gaus60->SetParameter(2, sigmaGuess);
h1->Fit(gaus60, "q", "", meanGuess-1.*sigmaGuess, meanGuess+3.*sigmaGuess);
gaus60->SetLineColor(kRed);
cout << " " << endl;
cout << "before adding any functions" << endl;
h1->GetListOfFunctions()->Clear();
h1->GetListOfFunctions()->Print();
h1->GetListOfFunctions()->Add(gaus60);
cout << " " << endl;
cout << "after adding gaus60" << endl;
h1->GetListOfFunctions()->Print();
Double_t mean = gaus60->GetParameter(1);
Double_t sigma = gaus60->GetParameter(2);
meanGuess = (26.34/59.54)*mean;
sigmaGuess = sigma;
TF1* gaus26 = new TF1("gaus26", "gaus", meanGuess-1.*sigmaGuess, meanGuess+3.*sigmaGuess);
gaus26->SetParameter(0, 500);
gaus26->SetParameter(1, meanGuess);
gaus26->SetParameter(2, sigmaGuess);
h1->Fit(gaus26, "q", "", meanGuess-1.*sigmaGuess, meanGuess+3.*sigmaGuess);
gaus26->SetLineColor(kRed);
h1->GetListOfFunctions()->Add(gaus26);
cout << " " << endl;
cout << "after adding gaus26" << endl;
h1->GetListOfFunctions()->Print();
h1->Draw();
}
listOfFunctionsEx.root (6.35 KB)