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