14#include <Math/Minimizer.h>
15#include <Math/Factory.h>
16#include <Math/Functor.h>
22#include <reconstruction/calibration/BeamSpotBoostInvMass/ChebFitter.h>
23#include <reconstruction/calibration/BeamSpotBoostInvMass/nodes.h>
24#include <framework/logging/Logger.h>
26#include <ChebFitter.h>
28#define B2INFO(arg) { std::cout << arg << std::endl;}
38 using Eigen::VectorXd;
39 using Eigen::MatrixXd;
48 fVals =
m_nodes.unaryExpr([&](
double x) {
return m_myFun(x, pars); });
53 fVals = -2 * log(fVals.array() / I);
100 for (
unsigned i = 0; i <
m_parNames.size(); ++i)
115 VectorXd polSum = VectorXd::Zero(
m_nodes.size());
117 double xx = (x - a) / (b - a);
134 ROOT::Math::Minimizer* minimum =
135 ROOT::Math::Factory::CreateMinimizer(
"Minuit2",
"");
138 minimum->SetMaxFunctionCalls(10000000);
139 minimum->SetMaxIterations(100000);
140 minimum->SetTolerance(10.0);
144 minimum->SetPrintLevel(0);
145 minimum->SetStrategy(2);
146 minimum->SetErrorDef(1);
152 for (
auto p : pars) {
153 std::string n = p.first;
154 double vCnt = p.second;
155 if (limits.count(n) == 1) {
156 double vMin = limits.at(n).first;
157 double vMax = limits.at(n).second;
158 double step = (vMax - vMin) / 100;
159 minimum->SetLimitedVariable(k, n, vCnt, step, vMin, vMax);
162 minimum->SetVariable(k, n, vCnt, step);
170 ROOT::Math::Functor f(*
this, pars.size());
171 minimum->SetFunction(f);
179 for (
unsigned i = 0; i <
m_parNames.size(); ++i)
182 MatrixXd covMat(parsF.size(), parsF.size());
183 for (
unsigned i = 0; i < parsF.size(); ++i)
184 for (
unsigned j = 0; j < parsF.size(); ++j)
185 covMat(i, j) = minimum->CovMatrix(i, j);
188 std::stringstream log;
189 log <<
"Minuit status : " << minimum->Status() <<
", ";
191 log <<
"\"" << p.first <<
"\" : " << p.second <<
", ";
197 return std::make_pair(parsF, covMat);
std::vector< double > m_data
vector with the data points to be fitted
bool m_useCheb
flag to use approximation based on Chebyshev polynomials
Eigen::VectorXd m_weights
vector with cheb weights for integration
std::vector< std::string > m_parNames
vector with names of the parameters
std::function< double(double, Pars)> m_myFun
function to fit
Eigen::VectorXd m_dataGrid
vector with the data points related to the cheb nodes (m_dataGrid.size = nodes.size)
Eigen::VectorXd m_nodes
vector with cheb nodes
Eigen::MatrixXd m_coefsMat
transformation matrix from chebPol to gridPoints
double operator()(const double *par) const
Evaluate the log likelihood.
double getLogLikelihoodSlow(const Pars &pars) const
Calculate log likelihood using exact formula.
Eigen::VectorXd getWeights(int Size)
Get the vector of weights to calculate the integral over the Chebyshev nodes The nodes are by definit...
void init(int Size, double xMin, double xMax)
Initialize the fitter (the Chebyshev coefficients)
Eigen::VectorXd getPols(int Size, double x)
Evaluate Chebyshev polynomials up to Size at point x It returns a vector of the P_i(x) for i=0....
std::map< std::string, double > Pars
values of parameters in ML fit
Eigen::VectorXd getDataGrid() const
Calculate Chebyshev coefficients for the data set.
Eigen::VectorXd getLogFunction(Pars pars) const
Get the -2*log(p(x)) on the Cheb nodes.
Eigen::MatrixXd getCoefsCheb(int oldSize)
Transformation matrix between Cheb nodes and Cheb coefficients with better normalization of the borde...
double getLogLikelihoodFast(const Pars &pars) const
Calculate log likelihood using approximation based on Chebyshev polynomials (typically faster)
std::map< std::string, std::pair< double, double > > Limits
limits of parameters in ML fit
std::pair< Pars, Eigen::MatrixXd > fitData(const Pars &pars, Limits limits, bool UseCheb=true)
Fit the data with specified initial values of parameters and limits on them.
Eigen::VectorXd getNodes(int Size)
Get the vector of positions of the Chebyshev nodes The nodes are by definition between 0 and 1,...
Abstract base class for different kinds of events.