{
//Bevington Exercise 
//By Peter Malzacher

  const int nBins = 60;
  
  Stat_t data[nBins] = { 6, 1,10,12, 6,13,23,22,15,21,
                        23,26,36,25,27,35,40,44,66,81,
                        75,57,48,45,46,41,35,36,53,32,
                        40,37,38,31,36,44,42,37,32,32,
                        43,44,35,33,33,39,29,41,32,44,
                        26,39,29,35,32,21,21,15,25,15};
  TH1F histo("example_9_1",
  		"Lorentzian Peak on Quadratic Background",60,0,3);
  
  for(int i=0; i < nBins;  i++) {
    // we use these methods to explicitly set the content
	 // and error instead of using the fill method. 
    histo.SetBinContent(i+1,data[i]);
    histo.SetBinError(i+1,TMath::Sqrt(data[i]));
  }

  histo.Draw(); 

  gROOT->LoadMacro("fitf.C"); 
  
  // create a FT1 with the range from 0 to 3 and 6 parameters
  TF1 fitFcn("fitFcn",fitFunction,0,3,6);
  

  // first try without starting values for the parameters
  // This defaults to 1 for each param. 
	histo.Fit("fitFcn");
	// this results in an ok fit for the polynomial function
	// however the non-linear part (lorenzian) does not 
	// respond well.

  c1->Modified();
  c1->Update();
  
  // wait for user input to continue
  char in; 
  cout << "Hit return to continue: " ; 
  cin >> in ;
  
  // second try: set start values for some parameters
  fitFcn.SetParameter(4,0.2); // width
  fitFcn.SetParameter(5,1);	// peak

  histo.Fit("fitFcn","V");
  c1->Modified();
  c1->Update();

  // wait for user input to continue
  char in;  
  cout << "Hit return to continue: " ; 
  cin >> in ;
	
  // improve the picture:
  TF1 backFcn("backFcn",background,0,3,3);
  backFcn->SetLineColor(3);
  TF1 signalFcn("signalFcn",lorentzianPeak,0,3,3);
  signalFcn->SetLineColor(4);
  Double_t par[6];
 
  // writes the fit results into the par array
  fitFcn.GetParameters(par);
   
  backFcn.SetParameters(par);
  backFcn.Draw("same");

  signalFcn.SetParameters(&par[3]);
  signalFcn.Draw("same");

}

