TH1D *hist = 0; TNtupleD *nt = 0; double model(double x, double *par) { double nb = par[0]; double bprime = par[1]; double ns = par[2]; 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)); return ns * G + nb * (1 + bprime*x)/(2. + 2.*bprime); } void fcn(int &npar, double *gin, double &f, double *par, int iflag) { double nb = par[0]; double ns = par[2]; f = 2.*(ns+nb); for (int i=0;iGetEntries();i++) { nt->GetEntry(i); double *mass = nt->GetArgs(); double L = model(*mass,par); if (L>0.) f -= 2.*log(L); else { f = HUGE; return; } } } void example_07() { TFile *fin = new TFile("example_data2.root"); hist = (TH1D *)fin->Get("hist"); nt = (TNtupleD *)fin->Get("nt"); hist->SetFillColor(kRed-9); hist->SetStats(false); hist->GetXaxis()->SetTitle("Mass"); TMinuit *gMinuit = new TMinuit(5); gMinuit->SetFCN(fcn); gMinuit->DefineParameter(0, "nbkg", 8000., 1., 0.,20000.); gMinuit->DefineParameter(1, "bprime",-0.3, 1., -10., 10.); 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); double BinWidth = hist->GetBinWidth(1); curve->SetBinContent(i,f*BinWidth); } curve->SetLineWidth(3); hist->Draw(); curve->Draw("csame"); }