Belle II Software  release-06-01-15
RecoilMassConstraint.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 #include "analysis/OrcaKinFit/RecoilMassConstraint.h"
10 #include "analysis/OrcaKinFit/ParticleFitObject.h"
11 
12 #include<iostream>
13 #include<cmath>
14 
15 #undef NDEBUG
16 #include<cassert>
17 
18 namespace Belle2 {
23  namespace OrcaKinFit {
24 
25 // constructor
26  RecoilMassConstraint::RecoilMassConstraint(double recoilmass, double beampx, double beampy, double beampz, double beampe)
27  : m_recoilMass(recoilmass), m_beamPx(beampx), m_beamPy(beampy), m_beamPz(beampz), m_beamE(beampe)
28  {}
29 
30 // destructor
32  {
33  ;
34  }
35 
36 // calculate current value of constraint function
38  {
39  double totE = 0.;
40  double totpx = 0.;
41  double totpy = 0.;
42  double totpz = 0.;
43 
44  for (auto fitobject : fitobjects) {
45  auto* pfo = dynamic_cast < ParticleFitObject* >(fitobject);
46  assert(pfo);
47  totE += pfo->getE();
48  totpx += pfo->getPx();
49  totpy += pfo->getPy();
50  totpz += pfo->getPz();
51  }
52 
53  const double recoilE = (m_beamE - totE);
54  const double recoilE2 = recoilE * recoilE;
55  const double recoilpx = (m_beamPx - totpx);
56  const double recoilpx2 = recoilpx * recoilpx;
57  const double recoilpy = (m_beamPy - totpy);
58  const double recoilpy2 = recoilpy * recoilpy;
59  const double recoilpz = (m_beamPz - totpz);
60  const double recoilpz2 = recoilpz * recoilpz;
61  const double recoil2 = recoilE2 - recoilpx2 - recoilpy2 - recoilpz2;
62  const double recoil = std::sqrt(std::fabs(recoil2));
63  const double result = recoil - m_recoilMass;
64  return result;
65  }
66 
67 // calculate vector/array of derivatives of this constraint
68 // w.r.t. to ALL parameters of all fitobjects
69 // here: d RM /d par(j)
70 // = d RM /d p(i) * d p(i) /d par(j)
71 // = +-1/RM * (p(i)-c(i)) * d p(i) /d par(j)
72 
73  void RecoilMassConstraint::getDerivatives(int idim, double der[]) const
74  {
75 
76  double totE = 0.;
77  double totpx = 0.;
78  double totpy = 0.;
79  double totpz = 0.;
80 
81  for (auto fitobject : fitobjects) {
82  auto* pfo = dynamic_cast < ParticleFitObject* >(fitobject);
83  assert(pfo);
84  totE += pfo->getE();
85  totpx += pfo->getPx();
86  totpy += pfo->getPy();
87  totpz += pfo->getPz();
88  }
89 
90  const double recoilE = (m_beamE - totE);
91  const double recoilE2 = recoilE * recoilE;
92  const double recoilpx = (m_beamPx - totpx);
93  const double recoilpx2 = recoilpx * recoilpx;
94  const double recoilpy = (m_beamPy - totpy);
95  const double recoilpy2 = recoilpy * recoilpy;
96  const double recoilpz = (m_beamPz - totpz);
97  const double recoilpz2 = recoilpz * recoilpz;
98  const double recoil2 = recoilE2 - recoilpx2 - recoilpy2 - recoilpz2;
99  const double recoil = std::sqrt(std::fabs(recoil2));
100 
101  double recoilmass_inv = 0.;
102  if (recoil > 1e-9) recoilmass_inv = 1. / recoil;
103 
104  for (auto fitobject : fitobjects) {
105  for (int ilocal = 0; ilocal < fitobject->getNPar(); ilocal++) {
106  if (!fitobject->isParamFixed(ilocal)) {
107  int iglobal = fitobject->getGlobalParNum(ilocal);
108  assert(iglobal >= 0 && iglobal < idim);
109 
110  auto* pfo = dynamic_cast < ParticleFitObject* >(fitobject);
111  assert(pfo);
112  der[iglobal] = - recoilE * pfo->getDE(ilocal)
113  + recoilpx * pfo->getDPx(ilocal)
114  + recoilpy * pfo->getDPy(ilocal)
115  + recoilpz * pfo->getDPz(ilocal);
116  der[iglobal] *= recoilmass_inv;
117  }
118  }
119  }
120  }
121 
122 
124  {
125  double totE = 0.;
126  double totpx = 0.;
127  double totpy = 0.;
128  double totpz = 0.;
129 
130  for (auto& fitobject : fitobjects) {
131  auto* pfo = dynamic_cast < ParticleFitObject* >(fitobject);
132  assert(pfo);
133  totE += pfo->getE();
134  totpx += pfo->getPx();
135  totpy += pfo->getPy();
136  totpz += pfo->getPz();
137  }
138 
139  const double recoilE = (m_beamE - totE);
140  const double recoilE2 = recoilE * recoilE;
141  const double recoilpx = (m_beamPx - totpx);
142  const double recoilpx2 = recoilpx * recoilpx;
143  const double recoilpy = (m_beamPy - totpy);
144  const double recoilpy2 = recoilpy * recoilpy;
145  const double recoilpz = (m_beamPz - totpz);
146  const double recoilpz2 = recoilpz * recoilpz;
147  const double recoil2 = recoilE2 - recoilpx2 - recoilpy2 - recoilpz2;
148  const double recoil = std::sqrt(std::fabs(recoil2));
149 
150  return recoil;
151  }
152 
153  void RecoilMassConstraint::setRecoilMass(double recoilmass_)
154  {
155  m_recoilMass = recoilmass_;
156  }
157 
158  bool RecoilMassConstraint::secondDerivatives(int i, int j, double* dderivatives) const
159  {
160  (void) i;
161  (void) j;
162  double totE = 0.;
163  double totpx = 0.;
164  double totpy = 0.;
165  double totpz = 0.;
166 
167  for (auto fitobject : fitobjects) {
168  auto* pfo = dynamic_cast < ParticleFitObject* >(fitobject);
169  assert(pfo);
170  totE += pfo->getE();
171  totpx += pfo->getPx();
172  totpy += pfo->getPy();
173  totpz += pfo->getPz();
174  }
175 
176  const double recoilE = (m_beamE - totE);
177  const double recoilE2 = recoilE * recoilE;
178  const double recoilpx = (m_beamPx - totpx);
179  const double recoilpx2 = recoilpx * recoilpx;
180  const double recoilpy = (m_beamPy - totpy);
181  const double recoilpy2 = recoilpy * recoilpy;
182  const double recoilpz = (m_beamPz - totpz);
183  const double recoilpz2 = recoilpz * recoilpz;
184  const double recoil2 = recoilE2 - recoilpx2 - recoilpy2 - recoilpz2;
185  const double recoil = sqrt(recoil2);
186 
187  assert(dderivatives);
188  for (int k = 0; k < 16; ++k) dderivatives[k] = 0;
189 
190  if (recoil > 1e-12) {
191  double recoilmass_inv3 = 1. / (recoil * recoil * recoil);
192 
193  dderivatives[4 * 0 + 0] = (recoil2 - recoilE2) * recoilmass_inv3; //dE^2
194  dderivatives[4 * 0 + 1] = dderivatives[4 * 1 + 0] = recoilE * recoilpx * recoilmass_inv3; //dEdpx
195  dderivatives[4 * 0 + 2] = dderivatives[4 * 2 + 0] = recoilE * recoilpy * recoilmass_inv3; //dEdpy
196  dderivatives[4 * 0 + 3] = dderivatives[4 * 3 + 0] = recoilE * recoilpz * recoilmass_inv3; //dEdpz
197  dderivatives[4 * 1 + 1] = -(recoil2 + recoilpx2) * recoilmass_inv3; //dpx^2
198  dderivatives[4 * 1 + 2] = dderivatives[4 * 2 + 1] = -recoilpx * recoilpy * recoilmass_inv3; //dpxdpy
199  dderivatives[4 * 1 + 3] = dderivatives[4 * 3 + 1] = -recoilpx * recoilpz * recoilmass_inv3; //dpxdpz
200  dderivatives[4 * 2 + 2] = -(recoil2 + recoilpy2) * recoilmass_inv3; //dpy^2
201  dderivatives[4 * 2 + 3] = dderivatives[4 * 3 + 2] = -recoilpy * recoilpz * recoilmass_inv3; //dpydpz
202  dderivatives[4 * 3 + 3] = -(recoil2 + recoilpz2) * recoilmass_inv3; //dpz^2
203  }
204 
205  return true;
206 
207  }
208 
209  bool RecoilMassConstraint::firstDerivatives(int i, double* dderivatives) const
210  {
211  (void) i;
212 
213  double totE = 0.;
214  double totpx = 0.;
215  double totpy = 0.;
216  double totpz = 0.;
217 
218  for (auto fitobject : fitobjects) {
219  auto* pfo = dynamic_cast < ParticleFitObject* >(fitobject);
220  assert(pfo);
221  totE += pfo->getE();
222  totpx += pfo->getPx();
223  totpy += pfo->getPy();
224  totpz += pfo->getPz();
225  }
226 
227  const double recoilE = (m_beamE - totE);
228  const double recoilE2 = recoilE * recoilE;
229  const double recoilpx = (m_beamPx - totpx);
230  const double recoilpx2 = recoilpx * recoilpx;
231  const double recoilpy = (m_beamPy - totpy);
232  const double recoilpy2 = recoilpy * recoilpy;
233  const double recoilpz = (m_beamPz - totpz);
234  const double recoilpz2 = recoilpz * recoilpz;
235  const double recoil2 = recoilE2 - recoilpx2 - recoilpy2 - recoilpz2;
236  const double recoil = sqrt(recoil2);
237 
238 
239  dderivatives[0] = -recoilE / recoil;
240  dderivatives[1] = recoilpx / recoil;
241  dderivatives[2] = recoilpy / recoil;
242  dderivatives[3] = recoilpz / recoil;
243 
244  return true;
245  }
246 
247  int RecoilMassConstraint::getVarBasis() const
248  {
249  return VAR_BASIS;
250  }
251  }// end OrcaKinFit namespace
253 } // end Belle2 namespace
254 
FitObjectContainer fitobjects
The FitObjectContainer.
virtual void setRecoilMass(double recoilmass)
Sets the target recoil mass of the constraint.
virtual void getDerivatives(int idim, double der[]) const override
Get first order derivatives.
virtual double getRecoilMass()
Get the actual recoil mass of the fit objects.
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...
RecoilMassConstraint(double recoilmass=0., double beampx=0., double beampy=0., double beampz=0, double beampe=0.)
Constructor.
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 ~RecoilMassConstraint()
Virtual destructor.
Abstract base class for different kinds of events.