TH1::GetListOfFunctions problem

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)

I have found that I can get the desired behavior if I use the option “n” when I fit the TH1 (all other code identical). I still don’t understand why the example as written doesn’t have that behavior. Furthermore, I don’t understand why merely fitting with multiple functions doesn’t expand the list - in that case there is only ever one function in the list (the last one used to fit).

see doc of TH1::Fit and the description of option “+”

Rene

Hey Brent!

Add the “+” to your fit option and it works.

h1->Fit(gaus60, “q+”, “”, meanGuess-1.*sigmaGuess, meanGuess+3.*sigmaGuess);

and

h1->Fit(gaus26, “q+”, “”, meanGuess-1.*sigmaGuess, meanGuess+3.*sigmaGuess);

Adam

Thanks. Sorry for the stupid question. I had missed the “+” option in the doc and evidently misinterpreted the statement below it:

“When TH1::Fit is invoked, the fitted function is added to this list.”