This example show how to perform an binned extended likelihood fit. The model has three components, two Gaussians and one Exponential, \( model(x) = N_1*Gaussian_1(x) + N_2*Gaussian_2(x) + N_3*Exponential()\) The example first generating a dataset sampling the model in parallel and then fit the parameters and yields.
#ifndef BINNED_EXTENDED_LOGLL_FIT_INL_
#define BINNED_EXTENDED_LOGLL_FIT_INL_
#include <iostream>
#include <assert.h>
#include <time.h>
#include <chrono>
#include <random>
#include <algorithm>
#include <tclap/CmdLine.h>
#include "Minuit2/FunctionMinimum.h"
#include "Minuit2/MnUserParameterState.h"
#include "Minuit2/MnPrint.h"
#include "Minuit2/MnMigrad.h"
#include "Minuit2/MnMinimize.h"
#ifdef _ROOT_AVAILABLE_
#include <TROOT.h>
#include <TH1D.h>
#include <TApplication.h>
#include <TCanvas.h>
#endif //_ROOT_AVAILABLE_
{
try {
TCLAP::CmdLine cmd("Command line arguments for ", '=');
TCLAP::ValueArg<size_t>
EArg(
"n",
"number-of-events",
"Number of events",
true, 10e6,
"size_t");
}
catch (TCLAP::ArgException &e) {
std::cerr << "error: " << e.error() << " for arg " << e.argId()
<< std::endl;
}
#ifdef _ROOT_AVAILABLE_
TH1D hist_gaussian_d(
"gaussian_d",
"Gaussian", 100,
min,
max);
TH1D hist_fitted_gaussian_d(
"fitted_gaussian_d",
"Gaussian", 100,
min,
max);
#endif //_ROOT_AVAILABLE_
{
std::cout<< std::endl<< "Generated data:"<< std::endl;
for(size_t i=0; i<10; i++)
std::cout << "[" << i << "] :" << data_d[i] << std::endl;
Hist_Data.Fill( data_d.begin(), data_d.end() );
ROOT::Minuit2::MnPrint::SetGlobalLevel(-1);
std::cout<<
fcn.GetParameters().GetMnState()<<std::endl;
auto start_d = std::chrono::high_resolution_clock::now();
auto end_d = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli>
elapsed_d = end_d - start_d;
std::cout<<"Minimum: "<< minimum_d << std::endl;
std::cout << "-----------------------------------------"<<std::endl;
std::cout <<
"| [Fit] GPU Time (ms) ="<<
elapsed_d.count() <<std::endl;
std::cout << "-----------------------------------------"<<std::endl;
#ifdef _ROOT_AVAILABLE_
for(size_t i=0; i<100; i++)
hist_gaussian_d.SetBinContent(i+1,
Hist_Data.GetBinContent(i));
hist_fitted_gaussian_d.Sumw2();
for (size_t i=0 ; i<=100 ; i++) {
double x = hist_fitted_gaussian_d.GetBinCenter(i);
hist_fitted_gaussian_d.SetBinContent(i,
fcn.GetPDF()(x) );
}
hist_fitted_gaussian_d.Scale(hist_gaussian_d.Integral()/hist_fitted_gaussian_d.Integral() );
#endif //_ROOT_AVAILABLE_
}
#ifdef _ROOT_AVAILABLE_
TApplication *myapp=new TApplication("myapp",0,0);
TCanvas canvas_d("canvas_d" ,"Distributions - Device", 500, 500);
hist_gaussian_d.Draw("hist");
hist_fitted_gaussian_d.Draw("histsameC");
hist_fitted_gaussian_d.SetLineColor(2);
myapp->Run();
#endif //_ROOT_AVAILABLE_
return 0;
}
#endif