This example implements a 3D dimensional Gaussian fit.
#ifndef MULTIDIMENSIONAL_FIT_INL_
#define MULTIDIMENSIONAL_FIT_INL_
#include <iostream>
#include <assert.h>
#include <time.h>
#include <chrono>
#include <random>
#include <algorithm>
#include <future>
#include <tclap/CmdLine.h>
#include "Minuit2/FunctionMinimum.h"
#include "Minuit2/MnUserParameterState.h"
#include "Minuit2/MnPrint.h"
#include "Minuit2/MnMigrad.h"
#include "Minuit2/MnMinimize.h"
#include "Minuit2/MnMinos.h"
#include "Minuit2/MnContours.h"
#include "Minuit2/CombinedMinimizer.h"
#include "Minuit2/MnPlot.h"
#include "Minuit2/MinosError.h"
#include "Minuit2/ContoursError.h"
#include "Minuit2/VariableMetricMinimizer.h"
#ifdef _ROOT_AVAILABLE_
#include <TROOT.h>
#include <TH1D.h>
#include <TH3D.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;
}
-1.0, 1.0);
double g = 1.0;
double X[3] = {x, y, z};
for(size_t i=0; i<3; i++) {
double m2 = (X[i] -
mean[i] )*(X[i] -
mean[i] );
g *=
exp(-m2/(2.0 * s2 ))/(
sqrt(2.0*s2*
PI));
}
return g;
#ifdef _ROOT_AVAILABLE_
TH3D hist_data_d("hist_data_d", "3D Gaussian - Data - Device",
TH3D hist_mc_d("hist_mc_d", "3D Gaussian - Fit - Device",
#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;
bool decision =
return decision;
});
std::cout << std::endl<< "Filtered data:" << std::endl;
for (size_t i = 0; i < 10; i++)
std::cout << "[" << i << "] :"
<< std::endl;
fcn.GetPDF().PrintRegisteredParameters();
ROOT::Minuit2::MnPrint::SetGlobalLevel(3);
std::cout <<
fcn.GetParameters().GetMnState() << std::endl;
auto start_d = std::chrono::high_resolution_clock::now();
FunctionMinimum minimum_d = FunctionMinimum(
migrad_d(5000, 5));
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 <<
"| GPU Time (ms) =" <<
elapsed_d.count() << std::endl;
std::cout << "-----------------------------------------" << std::endl;
{100, 100, 100},
#ifdef _ROOT_AVAILABLE_
for(size_t i=0; i<100; i++){
for(size_t j=0; j<100; j++){
for(size_t k=0; k<100; k++){
hist_data_d.SetBinContent(hist_data_d.GetBin(i+1, j+1, k+1),
}
}
}
for(size_t i=1; i< 101; i++ ) {
for(size_t j=1; j< 101; j++ ) {
for(size_t k=1; k< 101; k++ ) {
double x = hist_mc_d.GetXaxis()->GetBinCenter(i);
double y = hist_mc_d.GetYaxis()->GetBinCenter(j);
double z = hist_mc_d.GetZaxis()->GetBinCenter(k);
hist_mc_d.SetBinContent(hist_mc_d.GetBin(i,j,k),
fcn.GetPDF().GetFunctor()(value));
}
}
}
hist_mc_d.Scale(hist_data_d.Integral()/hist_mc_d.Integral() );
#endif //_ROOT_AVAILABLE_
}
#ifdef _ROOT_AVAILABLE_
TApplication *myapp=new TApplication("myapp",0,0);
TCanvas canvas_d("canvas_d" ,"Distributions - Device", 1500, 500);
canvas_d.Divide(3,1);
canvas_d.cd(1);
hist_data_d.Project3D("x")->Draw("hist");
hist_mc_d.Project3D("x")->Draw("chistsame");
hist_mc_d.Project3D("x")->SetLineColor(2);
canvas_d.cd(2);
hist_data_d.Project3D("y")->Draw("hist");
hist_mc_d.Project3D("y")->Draw("chistsame");
hist_mc_d.Project3D("y")->SetLineColor(2);
canvas_d.cd(3);
hist_data_d.Project3D("z")->Draw("hist");
hist_mc_d.Project3D("z")->Draw("chistsame");
hist_mc_d.Project3D("z")->SetLineColor(2);
TCanvas canvas2_d("canvas_d" ,"Distributions - Device", 1000, 500);
canvas2_d.Divide(2,1);
canvas2_d.cd(1);
hist_data_d.Draw("BOX1");
canvas2_d.cd(2);
hist_mc_d.Draw("iso");
myapp->Run();
#endif //_ROOT_AVAILABLE_
return 0;
}
#endif