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);
135 MatrixXd polSum2 = MatrixXd::Zero(
m_nodes.size(),
m_nodes.size());
136 VectorXd polSum = VectorXd::Zero(
m_nodes.size());
138 double xx = (x - a) / (b - a);
141 polSum2 += pol * pol.transpose();
146 MatrixXd coefs =
getCoefsCheb(polSum.size()).transpose();
147 VectorXd gridVals = coefs * polSum;
148 MatrixXd gridValsCov = coefs * polSum2 * coefs.transpose();
150 return std::make_pair(gridVals, gridValsCov);
159 ROOT::Math::Minimizer* minimum =
160 ROOT::Math::Factory::CreateMinimizer(
"Minuit2",
"");
163 minimum->SetMaxFunctionCalls(10000000);
164 minimum->SetMaxIterations(100000);
165 minimum->SetTolerance(10.0);
169 minimum->SetPrintLevel(0);
170 minimum->SetStrategy(2);
171 minimum->SetErrorDef(1);
177 for (
auto p : pars) {
178 std::string n = p.first;
179 double vCnt = p.second;
180 if (limits.count(n) == 1) {
181 double vMin = limits.at(n).first;
182 double vMax = limits.at(n).second;
183 double step = (vMax - vMin) / 100;
184 minimum->SetLimitedVariable(k, n, vCnt, step, vMin, vMax);
187 minimum->SetVariable(k, n, vCnt, step);
195 ROOT::Math::Functor f(*
this, pars.size());
196 minimum->SetFunction(f);
204 for (
unsigned i = 0; i <
m_parNames.size(); ++i)
207 MatrixXd covMat(parsF.size(), parsF.size());
208 for (
unsigned i = 0; i < parsF.size(); ++i)
209 for (
unsigned j = 0; j < parsF.size(); ++j)
210 covMat(i, j) = minimum->CovMatrix(i, j);
213 std::stringstream log;
214 log <<
"Minuit status : " << minimum->Status() <<
", ";
216 log <<
"\"" << p.first <<
"\" : " << p.second <<
", ";
222 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
std::pair< Pars, Eigen::MatrixXd > fitData(Pars pars, Limits limits, bool UseCheb=true)
Fit the data with specified initial values of parameters and limits on them.
double getFunctionFast(const Pars &pars, double x)
Evaluate the fitted function approximated with the Chebyshev polynomial, typically runs faster.
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...
std::pair< Eigen::VectorXd, Eigen::MatrixXd > getDataGridWithCov() const
Calculate Chebyshev coefficients with the covariance (useful for toy studies)
double getLogLikelihoodFast(const Pars &pars) const
Calculate log likelihood using approximation based on Chebyshev polynomials (typically faster)
Eigen::VectorXd interpol(const Eigen::VectorXd &xi, double x)
Get Interpolation vector k_i for point x from the function values at points xi (polynomial interpolat...
std::map< std::string, std::pair< double, double > > Limits
limits of parameters in ML fit
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.