#include #include #include #include "TRandom.h" #include "TH1F.h" #include "TFile.h" #include "TF1.h" #include "TMinuit.h" using namespace std; #include "fw_binned.cc" void fitfunction(Int_t &npar, Double_t *gin, Double_t &f,Double_t *par,Int_t iflag); int binned(TH1F *h1, TH1F *h2, TH1F *DATA, double &f1, double &ef1, double &f2, double &ef2); TH1F *hist1; TH1F *hist2; TH1F *hPD; //int fw_binned(int *a1, int *a2, int *ad, double &f1, double &ef1, double &f2, double &ef2){ int binned_fit(){ //------------- // Get Template //------------- TFile *fin = new TFile("template.root", "read"); TH1F *h1; TH1F *h2; h1 = dynamic_cast(fin->Get("hist1")); h2 = dynamic_cast(fin->Get("hist2")); //------------------------------------- // Make Pseudo Data (set Nev and Frac1) //------------------------------------- int Nev = 4000; float Frac1 = 0.2; int Nev1 = (int)(Nev*Frac1); int Nev2 = Nev - Nev1; TH1F *hist_d = new TH1F("hist_d", "hist_d", 100, 0, 100); hist_d->SetMarkerStyle(21); hist_d->SetLineWidth(2); for(int iev=0; ievFill( h1->GetRandom() ); for(int iev=0; ievFill( h2->GetRandom() ); double f1=0, ef1=0, f2=0, ef2=0; //HH binned minuit fit HH// binned(h1, h2, hist_d, f1, ef1, f2, ef2); cout<<" Minuit "<GetEntries()<<" "<GetEntries()<GetBinContent(i+1); a2[i]=h2->GetBinContent(i+1); ad[i]=hist_d->GetBinContent(i+1); } fw_binned(a1, a2, ad, f1, ef1, f2, ef2); cout<<" My FW fit "<Sumw2(); hist2->Sumw2(); hPD = DATA; //------ // Rebin //------ int Nrebin = 1; hist1->Rebin(Nrebin); hist2->Rebin(Nrebin); hPD ->Rebin(Nrebin); //------------- // Minuit setup //------------- //f = -2log const Int_t npar = 2; Int_t ier=0; TMinuit *gMinuit = new TMinuit(npar); gMinuit->SetPrintLevel(-1); gMinuit->SetFCN(fitfunction); Double_t vstart = 0.5; Double_t step = 0.1; Double_t min = 0.00; Double_t max = 1.00; Double_t fpar; Double_t ferr; // l minmum? // gMinuit->mnparm(0, "test1", vstart, step, min, max, ier); gMinuit->mnparm(1, "test2", vstart, step, min, max, ier); Double_t arg[] = {2000,1.0}; gMinuit->mnexcm("MIGRAD", arg, 2, ier); cout<< "Error flag = " << ier <mnpout(0,bb,cc,dd,ee,ff,gg); // cout << bb << " " << cc << " " << dd << " " << ee << " " << ff << " " << gg << endl; f1=cc; ef1=dd; gMinuit->mnpout(1,bb,cc,dd,ee,ff,gg); // cout << bb << " " << cc << " " << dd << " " << ee << " " << ff << " " << gg << endl; f2=cc; ef2=dd; return 0; } double BinFrac(int ih, int ibin){ double Frac = 0; if(ih==0){ // hist1 double TotNev = hist1->Integral(); double Content = hist1->GetBinContent(ibin); Frac = Content/TotNev; } if(ih==1){ // hist2 double TotNev = hist2->Integral(); double Content = hist2->GetBinContent(ibin); Frac = Content/TotNev; } return Frac; } void fitfunction(Int_t &npar, Double_t *gin, Double_t &f,Double_t *par,Int_t iflag){ Double_t f1=par[0]; Double_t f2=par[1]; Double_t logL=0; int Nbin = hPD->GetNbinsX(); for(Int_t ibin=1; ibin<=Nbin ; ibin++){ double mu = hPD->Integral() * ( f1*BinFrac(0,ibin) + f2*BinFrac(1,ibin) ); double N = hPD->GetBinContent(ibin); double logP = 0; if(mu>0){ logP = -mu + N*log(mu) - log(TMath::Factorial((int)N)); // cout<<"logP = "<