17 #include "analysis/OrcaKinFit/MassConstraint.h"
18 #include "analysis/OrcaKinFit/ParticleFitObject.h"
19 #include <framework/logging/Logger.h>
32 namespace OrcaKinFit {
46 double totE[2] = {0, 0};
47 double totpx[2] = {0, 0};
48 double totpy[2] = {0, 0};
49 double totpz[2] = {0, 0};
51 int index = (
flags[i] == 1) ? 0 : 1;
54 totE[index] += pfo->getE();
55 totpx[index] += pfo->getPx();
56 totpy[index] += pfo->getPy();
57 totpz[index] += pfo->getPz();
59 double result = -
mass;
60 result += std::sqrt(std::abs(totE[0] * totE[0] - totpx[0] * totpx[0] - totpy[0] * totpy[0] - totpz[0] * totpz[0]));
61 result -= std::sqrt(std::abs(totE[1] * totE[1] - totpx[1] * totpx[1] - totpy[1] * totpy[1] - totpz[1] * totpz[1]));
72 double totE[2] = {0, 0};
73 double totpx[2] = {0, 0};
74 double totpy[2] = {0, 0};
75 double totpz[2] = {0, 0};
76 bool valid[2] = {
false,
false};
77 for (
unsigned int i = 0; i <
fitobjects.size(); i++) {
78 int index = (
flags[i] == 1) ? 0 : 1;
80 auto* pfo =
dynamic_cast < ParticleFitObject*
>(
fitobjects[i]);
82 totE[index] += pfo->getE();
83 totpx[index] += pfo->getPx();
84 totpy[index] += pfo->getPy();
85 totpz[index] += pfo->getPz();
88 double m_inv[2] = {0, 0};
89 for (
int index = 0; index < 2; ++index) {
90 m2[index] = totE[index] * totE[index] - totpx[index] * totpx[index]
91 - totpy[index] * totpy[index] - totpz[index] * totpz[index];
92 if (m2[index] < 0 && m2[index] > -1E-9) m2[index] = 0;
93 if (m2[index] < 0 && valid[index]) {
94 B2ERROR(
"MassConstraint::getDerivatives: m2<0!");
95 for (
unsigned int j = 0; j <
fitobjects.size(); j++) {
96 int jndex = (
flags[j] == 1) ? 0 : 1;
99 auto* pfo =
dynamic_cast < ParticleFitObject*
>(
fitobjects[j]);
101 B2ERROR(pfo->getName() <<
102 ": E =" << pfo->getE() <<
103 ", px=" << pfo->getPx() <<
104 ", py=" << pfo->getPy() <<
105 ", pz=" << pfo->getPz());
108 B2ERROR(
"sum: E=" << totE[index] <<
", px=" << totpx[index]
109 <<
", py=" << totpy[index] <<
", pz=" << totpz[index] <<
", m2=" << m2[index]);
111 if (m2[index] != 0) m_inv[index] = 1 / std::sqrt(std::abs(m2[index]));
114 for (
unsigned int i = 0; i <
fitobjects.size(); i++) {
115 int index = (
flags[i] == 1) ? 0 : 1;
116 for (
int ilocal = 0; ilocal <
fitobjects[i]->getNPar(); ilocal++) {
118 int iglobal =
fitobjects[i]->getGlobalParNum(ilocal);
119 assert(iglobal >= 0 && iglobal < idim);
120 if (m2[index] != 0) {
122 auto* pfo =
dynamic_cast < ParticleFitObject*
>(
fitobjects[i]);
125 der[iglobal] = totE[index] * pfo->getDE(ilocal)
126 - totpx[index] * pfo->getDPx(ilocal)
127 - totpy[index] * pfo->getDPy(ilocal)
128 - totpz[index] * pfo->getDPz(ilocal);
129 der[iglobal] *= m_inv[index];
130 }
else der[iglobal] = 1;
131 if (index == 1) der[iglobal] *= -1.;
143 for (
unsigned int i = 0; i <
fitobjects.size(); i++) {
144 if (
flags[i] == flag) {
146 const ParticleFitObject* fok =
dynamic_cast < ParticleFitObject*
>(
fitobjects[i]);
149 totpx += fok->getPx();
150 totpy += fok->getPy();
151 totpz += fok->getPz();
154 return std::sqrt(std::abs(totE * totE - totpx * totpx - totpy * totpy - totpz * totpz));
164 B2DEBUG(14,
"MassConstraint::secondDerivatives: i=" << i <<
", j=" << j);
165 int index = (
flags[i] == 1) ? 0 : 1;
166 int jndex = (
flags[j] == 1) ? 0 : 1;
167 if (index != jndex)
return false;
172 for (
unsigned int k = 0; k <
fitobjects.size(); ++k) {
173 int kndex = (
flags[k] == 1) ? 0 : 1;
176 if (index == kndex) {
178 totpx += fok->
getPx();
179 totpy += fok->
getPy();
180 totpz += fok->
getPz();
185 B2ERROR(
"MassConstraint::secondDerivatives: totE = " << totE);
188 double m2 = std::abs(totE * totE - totpx * totpx - totpy * totpy - totpz * totpz);
189 double m = std::sqrt(m2);
191 double minv3 = 1 / (m * m * m);
193 assert(dderivatives);
194 for (
int k = 0; k < 16; ++k) dderivatives[k] = 0;
195 dderivatives[4 * 0 + 0] = (m2 - totE * totE) * minv3;
196 dderivatives[4 * 0 + 1] = dderivatives[4 * 1 + 0] = totE * totpx * minv3;
197 dderivatives[4 * 0 + 2] = dderivatives[4 * 2 + 0] = totE * totpy * minv3;
198 dderivatives[4 * 0 + 3] = dderivatives[4 * 3 + 0] = totE * totpz * minv3;
199 dderivatives[4 * 1 + 1] = -(m2 + totpx * totpx) * minv3;
200 dderivatives[4 * 1 + 2] = dderivatives[4 * 2 + 1] = -totpx * totpy * minv3;
201 dderivatives[4 * 1 + 3] = dderivatives[4 * 3 + 1] = -totpx * totpz * minv3;
202 dderivatives[4 * 2 + 2] = -(m2 + totpy * totpy) * minv3;
203 dderivatives[4 * 2 + 3] = dderivatives[4 * 3 + 2] = -totpy * totpz * minv3;
204 dderivatives[4 * 3 + 3] = -(m2 + totpz * totpz) * minv3;
214 int index = (
flags[i] == 1) ? 0 : 1;
215 for (
unsigned int j = 0; j <
fitobjects.size(); ++j) {
216 int jndex = (
flags[j] == 1) ? 0 : 1;
217 const ParticleFitObject* foj =
dynamic_cast < ParticleFitObject*
>(
fitobjects[j]);
219 if (index == jndex) {
221 totpx += foj->getPx();
222 totpy += foj->getPy();
223 totpz += foj->getPz();
228 B2ERROR(
"MassConstraint::firstDerivatives: totE = " << totE);
231 double m = std::sqrt(std::abs(totE * totE - totpx * totpx - totpy * totpy - totpz * totpz));
234 dderivatives[0] = totE / m;
235 dderivatives[1] = -totpx / m;
236 dderivatives[2] = -totpy / m;
237 dderivatives[3] = -totpz / m;
241 int MassConstraint::getVarBasis()
const