17#include "analysis/OrcaKinFit/ParticleFitObject.h"
18#include <framework/logging/Logger.h>
28#include <gsl/gsl_matrix.h>
29#include <gsl/gsl_linalg.h>
37 namespace OrcaKinFit {
40 : mass(0), fourMomentum(
FourVector(0, 0, 0, 0)), paramCycl{}
42 for (
double& i : paramCycl)
65 if (!isfinite(mass_))
return false;
66 if (
mass == mass_)
return true;
68 mass = std::abs(mass_);
76 if (psource !=
this) {
79 for (
int i = 0; i < BaseDefs::MAXPAR; ++i)
80 paramCycl[i] = psource->paramCycl[i];
96 os <<
"[" <<
getE() <<
", " <<
getPx() <<
", "
101 FourVector ParticleFitObject::getFourMomentum()
const
108 return getFourMomentum().
getE();
112 return getFourMomentum().
getPx();
116 return getFourMomentum().
getPy();
120 return getFourMomentum().
getPz();
124 return getFourMomentum().
getP();
128 return getFourMomentum().
getP2();
132 return getFourMomentum().
getPt();
136 return getFourMomentum().
getPt2();
154 for (
int ilocal = 0; ilocal <
getNPar(); ++ilocal) {
156 assert(iglobal >= 0 && iglobal < idim);
164 for (
int ilocal1 = 0; ilocal1 <
getNPar(); ++ilocal1) {
166 for (
int ilocal2 = ilocal1; ilocal2 <
getNPar(); ++ilocal2) {
168 M[idim * iglobal1 + iglobal2] +=
num2ndDerivative(ilocal1, eps, ilocal2, eps);
173 void ParticleFitObject::getDerivatives(
double der[],
int idim)
const
175 for (
int ilocal = 0; ilocal <
getNPar(); ++ilocal) {
176 assert(ilocal < idim);
177 der [4 * ilocal] =
getDE(ilocal);
178 der [4 * ilocal + 1] =
getDPx(ilocal);
179 der [4 * ilocal + 2] =
getDPy(ilocal);
180 der [4 * ilocal + 3] =
getDPz(ilocal);
184 void ParticleFitObject::test1stDerivatives()
186 B2INFO(
"ParticleFitObject::test1stDerivatives, object " <<
getName() <<
"\n");
187 double ycalc[100], ynum[100];
188 for (
int i = 0; i < 100; ++i) ycalc[i] = ynum[i] = 0;
190 double eps = 0.00001;
192 for (
int ilocal = 0; ilocal <
getNPar(); ++ilocal) {
194 double calc = ycalc[iglobal];
195 double num = ynum[iglobal];
196 B2INFO(
"fo: " <<
getName() <<
" par " << ilocal <<
"/"
198 <<
") calc: " << calc <<
" - num: " << num <<
" = " << calc - num);
202 void ParticleFitObject::test2ndDerivatives()
204 B2INFO(
"ParticleFitObject::test2ndDerivatives, object " <<
getName() <<
"\n");
205 const int idim = 100;
206 auto* Mnum =
new double[idim * idim];
207 auto* Mcalc =
new double[idim * idim];
208 for (
int i = 0; i < idim * idim; ++i) Mnum[i] = Mcalc[i] = 0;
211 B2INFO(
"eps=" << eps);
213 for (
int ilocal1 = 0; ilocal1 <
getNPar(); ++ilocal1) {
215 for (
int ilocal2 = ilocal1; ilocal2 <
getNPar(); ++ilocal2) {
217 double calc = Mcalc[idim * iglobal1 + iglobal2];
218 double num = Mnum[idim * iglobal1 + iglobal2];
219 B2INFO(
"fo: " <<
getName() <<
" par " << ilocal1 <<
"/"
221 <<
"), par " << ilocal2 <<
"/"
223 <<
") calc: " << calc <<
" - num: " << num <<
" = " << calc - num);
237 double result = (v1 - v2) / (2 * eps);
243 int ilocal2,
double eeps2)
247 if (ilocal1 == ilocal2) {
254 result = (v1 + v2 - 2 * v0) / (eeps1 * eeps1);
268 result = (v11 + v22 - v12 - v21) / (4 * eeps1 * eeps2);
282 double resid[BaseDefs::MAXPAR] = {0};
283 for (
int i = 0; i <
getNPar(); i++) {
286 if (paramCycl[i] > 0) {
287 resid[i] = fmod(resid[i], paramCycl[i]);
288 if (resid[i] > paramCycl[i] / 2) resid[i] -= paramCycl[i];
289 if (resid[i] < -paramCycl[i] / 2) resid[i] += paramCycl[i];
296 for (
int i = 0; i <
getNPar(); i++) {
298 for (
int j = 0; j <
getNPar(); j++) {
300 chi2 += resid[i] *
covinv[i][j] * resid[j];
bool covinvvalid
flag for valid inverse covariance matrix
virtual int getNPar() const =0
Get total number of parameters of this FitObject.
virtual void addToGlobalChi2DerMatrix(double *M, int idim) const
Add 2nd derivatives of chi squared to global derivative matrix.
virtual const char * getParamName(int ilocal) const =0
Get name of parameter ilocal.
virtual BaseFitObject & assign(const BaseFitObject &source)
Assign from anther object, if of same type.
virtual bool setParam(int ilocal, double par_, bool measured_, bool fixed_=false)
Set value and measured flag of parameter i; return: significant change.
double covinv[BaseDefs::MAXPAR][BaseDefs::MAXPAR]
inverse pf local covariance matrix
bool cachevalid
flag for valid cache
virtual int getGlobalParNum(int ilocal) const
Get global parameter number of parameter ilocal.
virtual const char * getName() const
Get object's name.
virtual bool isParamFixed(int ilocal) const
Returns whether parameter is fixed.
double par[BaseDefs::MAXPAR]
fit parameters
void invalidateCache() const
invalidate any cached quantities
double mpar[BaseDefs::MAXPAR]
measured parameters
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 std::ostream & printParams(std::ostream &os) const
print the parameters and errors
virtual bool calculateCovInv() const
Calculate the inverse of the covariance matrix.
virtual bool isParamMeasured(int ilocal) const
Get measured flag for parameter ilocal.
Yet another four vector class, with metric +—.
double getPx() const
Returns the x momentum / 1 component.
double getPz() const
Returns the z momentum / 3 component.
double getPt2() const
Returns the transverse momentum squared / magnitude of the 1 and 2 component vector squared.
double getPy() const
Returns the y momentum / 2 component.
double getP2() const
Returns the momentum squared / magnitude of the three vector squared.
double getPt() const
Returns the transverse momentum / magnitude of the 1 and 2 component vector.
double getP() const
Returns the momentum / magnitude of the three vector.
double getE() const
Returns the energy / 0 component.
virtual double getPx() const
Return px.
virtual ~ParticleFitObject()
Virtual destructor.
virtual void addToGlobalChi2DerMatrixNum(double *M, int idim, double eps)
Add numerically determined derivatives of chi squared to global covariance matrix.
double mass
mass of particle
ParticleFitObject & operator=(const ParticleFitObject &rhs)
Assignment.
virtual double getPz() const
Return pz.
virtual bool setMass(double mass_)
Set mass of particle; return=success.
double num2ndDerivative(int ilocal1, double eps1, int ilocal2, double eps2)
Evaluates numerically the 2nd derivative of chi2 w.r.t. 2 parameters.
virtual void addToGlobalChi2DerVectorNum(double *y, int idim, double eps)
Add numerically determined derivatives of chi squared to global derivative vector.
virtual std::ostream & print(std::ostream &os) const override
print object to ostream
virtual double getPt2() const
Return pt (transverse momentum) squared.
virtual double getDE(int ilocal) const =0
Return d E / d par_ilocal (derivative of E w.r.t. local parameter ilocal)
virtual double getPy() const
Return py.
virtual double getDPx(int ilocal) const =0
Return d p_x / d par_ilocal (derivative of px w.r.t. local parameter ilocal)
ParticleFitObject()
Default constructor.
virtual double getDPz(int ilocal) const =0
Return d p_z / d par_ilocal (derivative of pz w.r.t. local parameter ilocal)
virtual double getP2() const
Return p (momentum) squared.
virtual double getPt() const
Return pt (transverse momentum)
virtual double getP() const
Return p (momentum)
virtual ParticleFitObject & assign(const BaseFitObject &source) override
Assign from anther object, if of same type.
virtual double getE() const
Return E.
double num1stDerivative(int ilocal, double eps)
Evaluates numerically the 1st derivative of chi2 w.r.t. a parameter.
virtual double getChi2() const override
Get chi squared from measured and fitted parameters.
virtual double getDPy(int ilocal) const =0
Return d p_y / d par_ilocal (derivative of py w.r.t. local parameter ilocal)
virtual double getMass() const
Get mass of particle.
virtual std::ostream & print4Vector(std::ostream &os) const
print the four-momentum (E, px, py, pz)
Abstract base class for different kinds of events.