TH1D *hist = 0; double model(double x, double *par) { double mu = par[3]; double sigma = par[4]; double norm = 1./sqrt(2.*TMath::Pi())/sigma; double G = norm*exp(-0.5 *pow((x-mu)/sigma,2)); double BinWidth = hist->GetBinWidth(1); return par[0] + par[1]*x + par[2] * BinWidth * G; } void fcn(int &npar, double *gin, double &f, double *par, int iflag) { f = 0.; for(int i=1;i<=hist->GetNbinsX();i++) { double x = hist->GetBinCenter(i); double measure = hist->GetBinContent(i); double error = sqrt(measure); double func = model(x,par); double delta = (func - measure)/error; f += delta*delta; } } void example_05() { TFile *fin = new TFile("example_data.root"); hist = (TH1D *)fin->Get("hist"); hist->SetFillColor(kRed-9); hist->SetStats(false); hist->GetXaxis()->SetTitle("Mass"); TMinuit *gMinuit = new TMinuit(5); gMinuit->SetFCN(fcn); gMinuit->DefineParameter(0, "p0", 100., 1., 0., 200.); gMinuit->DefineParameter(1, "p1", -30., 1., -200., 200.); gMinuit->DefineParameter(2, "area", 2000., 1., 0.,20000.); gMinuit->DefineParameter(3, "mean", 1.00, 1., 0.5, 1.5); gMinuit->DefineParameter(4, "width", 0.05, 1., 0.001, 0.15); gMinuit->Command("MIGRAD"); gMinuit->Command("MIGRAD"); gMinuit->Command("MINOS"); double par[5],err[5]; for(int i=0;i<5;i++) gMinuit->GetParameter(i,par[i],err[i]); TH1F* curve = new TH1F("curve","curve",hist->GetNbinsX()*5, hist->GetXaxis()->GetXmin(),hist->GetXaxis()->GetXmax()); for(int i=1;i<=curve->GetNbinsX();i++) { double x = curve->GetBinCenter(i); double f = model(x,par); curve->SetBinContent(i,f); } curve->SetLineWidth(3); hist->Draw(); curve->Draw("csame"); }