TH1D *hist = 0; TNtupleD *nt = 0; double model(double x, double *par) { double bprime = par[0]; double fs = par[1]/nt->GetEntries(); double mu = par[2]; double sigma = par[3]; double norm = 1./sqrt(2.*TMath::Pi())/sigma; double G = norm*exp(-0.5 * pow((x-mu)/sigma,2)); return fs * G + (1.-fs) * (1 + bprime*x)/(2. + 2.*bprime); } void fcn(int &npar, double *gin, double &f, double *par, int iflag) { f = 0.; 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_06() { TFile *fin = new TFile("example_data.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(4); gMinuit->SetFCN(fcn); gMinuit->DefineParameter(0, "bprime",-0.3, 1., -10., 10.); gMinuit->DefineParameter(1, "area", 2000., 1., 0.,20000.); gMinuit->DefineParameter(2, "mean", 1.00, 1., 0.5, 1.5); gMinuit->DefineParameter(3, "width", 0.05, 1., 0.001, 0.15); gMinuit->Command("MIGRAD"); gMinuit->Command("MIGRAD"); gMinuit->Command("MINOS"); double par[4],err[4]; for(int i=0;i<4;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*nt->GetEntries()*BinWidth); } curve->SetLineWidth(3); hist->Draw(); curve->Draw("csame"); }