17 #include "analysis/OrcaKinFit/BaseHardConstraint.h"
18 #include <framework/logging/Logger.h>
32 namespace OrcaKinFit {
39 double dgdpi[BaseDefs::MAXINTERVARS];
40 for (
unsigned int i = 0; i <
fitobjects.size(); ++i) {
44 foi->addTo1stDerivatives(M, idim, dgdpi,
getGlobalNum(), getVarBasis());
86 double d2GdPidPj[BaseDefs::MAXINTERVARS * BaseDefs::MAXINTERVARS];
93 auto* dPidAk =
new double[n * BaseDefs::MAXPAR * BaseDefs::MAXINTERVARS];
94 bool* dPidAkval =
new bool[n];
96 for (
int i = 0; i < n; ++i) dPidAkval[i] =
false;
100 double d2GdPdAl[
static_cast<int>(BaseDefs::MAXINTERVARS) * BaseDefs::MAXPAR];
102 double d2GdAkdAl[BaseDefs::MAXPAR * BaseDefs::MAXPAR] = {0};
106 int* parglobal =
new int[BaseDefs::MAXPAR * n];
108 for (
int i = 0; i < n; ++i) {
111 for (
int klocal = 0; klocal < foi->
getNPar(); ++klocal) {
112 parglobal [BaseDefs::MAXPAR * i + klocal] = foi->
getGlobalParNum(klocal);
117 for (
int i = 0; i < n; ++i) {
120 for (
int j = 0; j < n; ++j) {
125 foi->getDerivatives(dPidAk + i * (
static_cast<int>(BaseDefs::MAXPAR) * BaseDefs::MAXINTERVARS),
126 static_cast<int>(BaseDefs::MAXPAR) * BaseDefs::MAXINTERVARS);
130 foj->getDerivatives(dPidAk + j * (
static_cast<int>(BaseDefs::MAXPAR) * BaseDefs::MAXINTERVARS),
131 static_cast<int>(BaseDefs::MAXPAR) * BaseDefs::MAXINTERVARS);
139 for (
int llocal = 0; llocal < foj->
getNPar(); ++llocal) {
140 for (
int ii = 0; ii < BaseDefs::MAXINTERVARS; ++ii) {
141 int ind1 = BaseDefs::MAXINTERVARS * ii;
142 int ind2 = (
static_cast<int>(BaseDefs::MAXPAR) * BaseDefs::MAXINTERVARS) * j + BaseDefs::MAXINTERVARS * llocal;
143 double& r = d2GdPdAl[BaseDefs::MAXINTERVARS * llocal + ii];
144 r = d2GdPidPj[ ind1] * dPidAk[ ind2];
145 r += d2GdPidPj[++ind1] * dPidAk[++ind2];
146 r += d2GdPidPj[++ind1] * dPidAk[++ind2];
147 r += d2GdPidPj[++ind1] * dPidAk[++ind2];
156 for (
int klocal = 0; klocal < foi->
getNPar(); ++klocal) {
157 for (
int llocal = 0; llocal < foj->
getNPar(); ++llocal) {
158 int ind1 = BaseDefs::MAXINTERVARS * llocal;
159 int ind2 = (
static_cast<int>(BaseDefs::MAXPAR) * BaseDefs::MAXINTERVARS) * i + BaseDefs::MAXINTERVARS * klocal;
160 double& r = d2GdAkdAl[BaseDefs::MAXPAR * klocal + llocal];
161 r = d2GdPdAl[ ind1] * dPidAk[ ind2];
162 r += d2GdPdAl[++ind1] * dPidAk[++ind2];
163 r += d2GdPdAl[++ind1] * dPidAk[++ind2];
164 r += d2GdPdAl[++ind1] * dPidAk[++ind2];
168 for (
int klocal = 0; klocal < foi->
getNPar(); ++klocal) {
169 int kglobal = parglobal [BaseDefs::MAXPAR * i + klocal];
170 for (
int llocal = 0; llocal < foj->
getNPar(); ++llocal) {
171 int lglobal = parglobal [BaseDefs::MAXPAR * j + llocal];
172 M [idim * kglobal + lglobal] += lambda * d2GdAkdAl[BaseDefs::MAXPAR * klocal + llocal];
187 double dgdpi[BaseDefs::MAXINTERVARS];
188 for (
int i = 0; i < n; ++i) {
192 foi->addTo2ndDerivatives(M, idim, lambda, dgdpi, getVarBasis());
203 double dgdpi[BaseDefs::MAXINTERVARS];
204 for (
unsigned int i = 0; i <
fitobjects.size(); ++i) {
216 double dgdpi[BaseDefs::MAXINTERVARS];
218 for (
unsigned int i = 0; i <
fitobjects.size(); ++i) {
222 error2 += foi->getError2(dgdpi, getVarBasis());
232 for (pw = w; pw < w + idim; * (pw++) = 0);
235 for (pw = w, pp = p; pw < w + idim; result += *(pp++) * *(pw++));
242 if (val == 0)
return mu * std::fabs(
dirDer(p, w, idim, 1));
243 else if (val > 0)
return mu *
dirDer(p, w, idim, 1);
244 else return -mu *
dirDer(p, w, idim, 1);
248 void BaseHardConstraint::test1stDerivatives()
250 B2INFO(
"BaseConstraint::test1stDerivatives for " <<
getName());
252 for (
double& i : y) i = 0;
254 double eps = 0.00001;
255 for (
unsigned int ifo = 0; ifo <
fitobjects.size(); ++ifo) {
258 for (
int ilocal = 0; ilocal < fo->
getNPar(); ++ilocal) {
260 double calc = y[iglobal];
262 B2INFO(
"fo: " << fo->
getName() <<
" par " << ilocal <<
"/"
264 <<
") calc: " << calc <<
" - num: " << num <<
" = " << calc - num);
269 void BaseHardConstraint::test2ndDerivatives()
271 B2INFO(
"BaseConstraint::test2ndDerivatives for " <<
getName());
272 const int idim = 100;
273 auto* M =
new double[idim * idim];
274 for (
int i = 0; i < idim * idim; ++i) M[i] = 0;
277 B2INFO(
"eps=" << eps);
279 for (
unsigned int ifo1 = 0; ifo1 <
fitobjects.size(); ++ifo1) {
282 for (
unsigned int ifo2 = ifo1; ifo2 <
fitobjects.size(); ++ifo2) {
285 for (
int ilocal1 = 0; ilocal1 < fo1->getNPar(); ++ilocal1) {
286 int iglobal1 = fo1->getGlobalParNum(ilocal1);
287 for (
int ilocal2 = (ifo1 == ifo2 ? ilocal1 : 0); ilocal2 < fo2->getNPar(); ++ilocal2) {
288 int iglobal2 = fo2->getGlobalParNum(ilocal2);
289 double calc = M[idim * iglobal1 + iglobal2];
291 B2INFO(
"fo1: " << fo1->getName() <<
" par " << ilocal1 <<
"/"
292 << iglobal1 <<
" (" << fo1->getParamName(ilocal1)
293 <<
"), fo2: " << fo2->getName() <<
" par " << ilocal2 <<
"/"
294 << iglobal2 <<
" (" << fo2->getParamName(ilocal2)
295 <<
") calc: " << calc <<
" - num: " << num <<
" = " << calc - num);
313 double result = (v1 - v2) / (2 * eps);
319 int ifo2,
int ilocal2,
double eps2)
323 if (ifo1 == ifo2 && ilocal1 == ilocal2) {
326 double save = fo->
getParam(ilocal1);
332 result = (v1 + v2 - 2 * v0) / (eps1 * eps1);
339 double save1 = fo1->
getParam(ilocal1);
340 double save2 = fo2->
getParam(ilocal2);
341 fo1->
setParam(ilocal1, save1 + eps1);
342 fo2->
setParam(ilocal2, save2 + eps2);
344 fo2->
setParam(ilocal2, save2 - eps2);
346 fo1->
setParam(ilocal1, save1 - eps1);
348 fo2->
setParam(ilocal2, save2 + eps2);
350 result = (v11 + v22 - v12 - v21) / (4 * eps1 * eps2);
357 void BaseHardConstraint::printFirstDerivatives()
const
360 B2INFO(
"BaseHardConstraint::printFirstDerivatives " <<
fitobjects.size());
362 double dgdpi[BaseDefs::MAXINTERVARS];
363 for (
unsigned int i = 0; i <
fitobjects.size(); ++i) {
367 B2INFO(
"first derivs for obj " << i <<
" : ");
368 for (
double j : dgdpi)
376 void BaseHardConstraint::printSecondDerivatives()
const
379 double d2GdPidPj[BaseDefs::MAXINTERVARS * BaseDefs::MAXINTERVARS];
383 for (
int i = 0; i < n; ++i) {
386 for (
int j = 0; j < n; ++j) {
391 B2INFO(
"second derivs for objs " << i <<
" " << j);
394 for (
int k1 = 0; k1 < BaseDefs::MAXINTERVARS; k1++) {
395 for (
int k2 = 0; k2 < BaseDefs::MAXINTERVARS; k2++) {
396 B2INFO(d2GdPidPj[k++] <<
" ");
virtual const char * getName() const
Returns the name of the constraint.
virtual int getNPar() const =0
Get total number of parameters of this FitObject.
virtual const char * getParamName(int ilocal) const =0
Get name of parameter ilocal.
virtual bool setParam(int ilocal, double par_, bool measured_, bool fixed_=false)
Set value and measured flag of parameter i; return: significant change.
virtual int getGlobalParNum(int ilocal) const
Get global parameter number of parameter ilocal.
virtual const char * getName() const
Get object's name.
virtual double getParam(int ilocal) const
Get current value of parameter ilocal.
virtual int addToGlobalChi2DerVector(double *y, int idim) const
Add derivatives of chi squared to global derivative vector.
virtual ~BaseHardConstraint()
Virtual destructor.
virtual int getGlobalNum() const
Accesses position of constraint in global constraint list.
virtual double getValue() const override=0
Returns the value of the constraint.
FitObjectContainer fitobjects
The FitObjectContainer.
virtual void add1stDerivativesToMatrix(double *M, int idim) const
Adds first order derivatives to global covariance matrix M.
virtual double getError() const override
Returns the error on the value of the constraint.
virtual void addToGlobalChi2DerVector(double *y, int idim, double lambda) const
Add lambda times derivatives of chi squared to global derivative vector.
virtual double dirDerAbs(double *p, double *w, int idim, double mu=1)
Calculate directional derivative for abs(c)
virtual double dirDer(double *p, double *w, int idim, double mu=1)
Calculate directional derivative.
virtual bool secondDerivatives(int i, int j, double *derivatives) const =0
Second derivatives with respect to the meta-variables of Fit objects i and j; result false if all der...
virtual bool firstDerivatives(int i, double *derivatives) const =0
First derivatives with respect to the meta-variables of Fit objects i; result false if all derivative...
virtual void add2ndDerivativesToMatrix(double *M, int idim, double lambda) const
Adds second order derivatives to global covariance matrix M.
virtual double num2ndDerivative(int ifo1, int ilocal1, double eps1, int ifo2, int ilocal2, double eps2)
Evaluates numerically the 2nd derivative w.r.t. 2 parameters.
virtual double num1stDerivative(int ifo, int ilocal, double eps)
Evaluates numerically the 1st derivative w.r.t. a parameter.
double sqrt(double a)
sqrt for double
Abstract base class for different kinds of events.