Belle II Software light-2406-ragdoll
MomentumConstraint.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * Forked from https://github.com/iLCSoft/MarlinKinfit *
6 * *
7 * Further information about the fit engine and the user interface *
8 * provided in MarlinKinfit can be found at *
9 * https://www.desy.de/~blist/kinfit/doc/html/ *
10 * and in the LCNotes LC-TOOL-2009-001 and LC-TOOL-2009-004 available *
11 * from http://www-flc.desy.de/lcnotes/ *
12 * *
13 * See git log for contributors and copyright holders. *
14 * This file is licensed under LGPL-3.0, see LICENSE.md. *
15 **************************************************************************/
16
17#include "analysis/OrcaKinFit/MomentumConstraint.h"
18#include "analysis/OrcaKinFit/ParticleFitObject.h"
19#include <framework/logging/Logger.h>
20
21#include<iostream>
22
23#undef NDEBUG
24#include<cassert>
25
26
27namespace Belle2 {
32 namespace OrcaKinFit {
33
34 MomentumConstraint::MomentumConstraint(double efact_, double pxfact_, double pyfact_,
35 double pzfact_, double value_)
36 : efact(efact_),
37 pxfact(pxfact_),
38 pyfact(pyfact_),
39 pzfact(pzfact_),
40 value(value_),
41 cachevalid(false),
42 nparams(0)
43 {}
44
45// destructor
46 MomentumConstraint::~MomentumConstraint() = default;
47
48// calculate current value of constraint function
50 {
51 double totpx = 0;
52 double totpy = 0;
53 double totpz = 0;
54 double totE = 0;
55 for (auto fitobject : fitobjects) {
56
57 const ParticleFitObject* foi = dynamic_cast < ParticleFitObject* >(fitobject);
58 assert(foi);
59
60 if (pxfact != 0) totpx += foi->getPx();
61 if (pyfact != 0) totpy += foi->getPy();
62 if (pzfact != 0) totpz += foi->getPz();
63 if (efact != 0) totE += foi->getE();
64 }
65 return pxfact * totpx + pyfact * totpy + pzfact * totpz + efact * totE - value;
66 }
67
68// calculate vector/array of derivatives of this constraint
69// w.r.t. to ALL parameters of all fitobjects
70// here: d sum(px) /d par(i,j)
71// = d sum(px) /d px(i) * d px(i) /d par(i, j)
72// = 1 * d px(i) /d par(i, j)
73 void MomentumConstraint::getDerivatives(int idim, double der[]) const
74 {
75 for (auto fitobject : fitobjects) {
76 for (int ilocal = 0; ilocal < fitobject->getNPar(); ilocal++) {
77 if (!fitobject->isParamFixed(ilocal)) {
78 int iglobal = fitobject->getGlobalParNum(ilocal);
79 assert(iglobal >= 0 && iglobal < idim);
80 double d = 0;
81 const ParticleFitObject* foi = dynamic_cast < ParticleFitObject* >(fitobject);
82 assert(foi);
83 if (pxfact != 0) d += pxfact * foi->getDPx(ilocal);
84 if (pyfact != 0) d += pyfact * foi->getDPy(ilocal);
85 if (pzfact != 0) d += pzfact * foi->getDPz(ilocal);
86 if (efact != 0) d += efact * foi->getDE(ilocal);
87 der[iglobal] = d;
88 }
89 }
90 }
91 }
92
94 {
95 cachevalid = false;
96 }
97
98 void MomentumConstraint::updateCache() const
99 {
100 nparams = 0;
101 for (auto fitobject : fitobjects) {
102 for (int ilocal = 0; ilocal < fitobject->getNPar(); ilocal++) {
103 int iglobal = fitobject->getGlobalParNum(ilocal);
104 if (!fitobject->isParamFixed(ilocal)) {
105 assert(iglobal >= 0);
106 nparams++;
107 }
108 }
109 }
110 cachevalid = true;
111 }
112
113 bool MomentumConstraint::secondDerivatives(int i, int j, double* dderivatives) const
114 {
115 (void) i;
116 (void) j;
117 (void) dderivatives;
118 return false;
119 } //fix the warning
120
121 bool MomentumConstraint::firstDerivatives(int i, double* dderivatives) const
122 {
123 (void) i;
124 dderivatives[0] = efact;
125 dderivatives[1] = pxfact;
126 dderivatives[2] = pyfact;
127 dderivatives[3] = pzfact;
128 return true;
129 }
130
131 int MomentumConstraint::getVarBasis() const
132 {
133 return VAR_BASIS;
134 }
135
136 }// end OrcaKinFit namespace
137
139} // end Belle2 namespace
FitObjectContainer fitobjects
The FitObjectContainer.
virtual void getDerivatives(int idim, double der[]) const override
Get first order derivatives.
MomentumConstraint(double efact_=0, double pxfact_=0, double pyfact_=0, double pzfact_=0, double value_=0)
virtual double getValue() const override
Returns the value of the constraint.
virtual bool secondDerivatives(int i, int j, double *derivatives) const override
Second derivatives with respect to the 4-vectors of Fit objects i and j; result false if all derivati...
virtual bool firstDerivatives(int i, double *derivatives) const override
First derivatives with respect to the 4-vector of Fit objects i; result false if all derivatives are ...
virtual void invalidateCache() const override
Invalidates any cached values for the next event.
virtual double getPx() const
Return px.
virtual double getPz() const
Return pz.
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)
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 getE() const
Return E.
virtual double getDPy(int ilocal) const =0
Return d p_y / d par_ilocal (derivative of py w.r.t. local parameter ilocal)
Abstract base class for different kinds of events.
Definition: ClusterUtils.h:24