Belle II Software  release-05-01-25
PxPyPzMFitObject.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2017 - Belle II Collaboration *
4  * *
5  * PxPyPzE fit object *
6  * *
7  * Author: The Belle II Collaboration *
8  * Contributors: Torben Ferber (torben.ferber@desy.de) (TF) *
9  * *
10  * This software is provided "as is" without any warranty. *
11  **************************************************************************/
12 
13 #include "analysis/OrcaKinFit/PxPyPzMFitObject.h"
14 #include <framework/logging/Logger.h>
15 #include <cmath>
16 #include <cassert>
17 #include <iostream>
18 
19 using std::sqrt;
20 using std::sin;
21 using std::cos;
22 
23 namespace Belle2 {
28  namespace OrcaKinFit {
29 
30 // constructor
31  PxPyPzMFitObject::PxPyPzMFitObject(CLHEP::HepLorentzVector& particle, const CLHEP::HepSymMatrix& covmatrix)
32  : cachevalid(false), chi2(0), dEdpx(0), dEdpy(0), dEdpz(0),
33  dE2dpxdpx(0), dE2dpxdpy(0), dE2dpxdpz(0), dE2dpydpy(0), dE2dpydpz(0), dE2dpzdpz(0)
34  {
35 
36  assert(int(NPAR) <= int(BaseDefs::MAXPAR));
37 
38  initCov();
39  setMass(particle.m());
40 
41  setParam(0, particle.px(), true);
42  setParam(1, particle.py(), true);
43  setParam(2, particle.pz(), true);
44 
45  setMParam(0, particle.px());
46  setMParam(1, particle.py());
47  setMParam(2, particle.pz());
48 
49  //set covariance matrix
50  for (int i = 0; i < int(NPAR); i++) {
51  for (int j = 0; j < int(NPAR); j++) {
52  setCov(i, j, covmatrix[i][j]);
53  }
54  }
55 
56  invalidateCache();
57  }
58 
59 // destructor
60  PxPyPzMFitObject::~PxPyPzMFitObject() = default;
61 
62  PxPyPzMFitObject::PxPyPzMFitObject(const PxPyPzMFitObject& rhs)
63  : ParticleFitObject(rhs), cachevalid(false), chi2(0), dEdpx(0), dEdpy(0), dEdpz(0),
64  dE2dpxdpx(0), dE2dpxdpy(0), dE2dpxdpz(0), dE2dpydpy(0), dE2dpydpz(0), dE2dpzdpz(0)
65  {
66 
68  }
69 
71  {
72  if (this != &rhs) {
73  assign(rhs); // calls virtual function assign of derived class
74  }
75  return *this;
76  }
77 
78  PxPyPzMFitObject* PxPyPzMFitObject::copy() const
79  {
80  return new PxPyPzMFitObject(*this);
81  }
82 
83  PxPyPzMFitObject& PxPyPzMFitObject::assign(const BaseFitObject& source)
84  {
85  if (const auto* psource = dynamic_cast<const PxPyPzMFitObject*>(&source)) {
86  if (psource != this) {
88  // only mutable data members, need not to be copied, if cache is invalid
89  }
90  } else {
91  assert(0);
92  }
93  return *this;
94  }
95 
96  const char* PxPyPzMFitObject::getParamName(int ilocal) const
97  {
98  switch (ilocal) {
99  case 0: return "px";
100  case 1: return "py";
101  case 2: return "pz";
102  }
103  return "undefined";
104  }
105 
106 
107  bool PxPyPzMFitObject::updateParams(double p[], int idim)
108  {
109  invalidateCache();
110 
111  int ipx = getGlobalParNum(0);
112  int ipy = getGlobalParNum(1);
113  int ipz = getGlobalParNum(2);
114  assert(ipx >= 0 && ipx < idim);
115  assert(ipy >= 0 && ipy < idim);
116  assert(ipz >= 0 && ipz < idim);
117 
118  double px = p[ipx];
119  double py = p[ipy];
120  double pz = p[ipz];
121 
122  bool result = ((px - par[0]) * (px - par[0]) > eps2 * cov[0][0]) ||
123  ((py - par[1]) * (py - par[1]) > eps2 * cov[1][1]) ||
124  ((pz - par[2]) * (pz - par[2]) > eps2 * cov[2][2]);
125 
126  par[0] = px;
127  par[1] = py;
128  par[2] = pz;
129  p[ipx] = par[0];
130  p[ipy] = par[1];
131  p[ipz] = par[2];
132  return result;
133  }
134 
135 // these depend on actual parametrisation!
136 
137  double PxPyPzMFitObject::getDPx(int ilocal) const
138  {
139  assert(ilocal >= 0 && ilocal < NPAR);
140  if (!cachevalid) updateCache();
141  switch (ilocal) {
142  case 0: return 1.0;
143  case 1: return 0.0;
144  case 2: return 0.0;
145  }
146  return 0;
147  }
148 
149  double PxPyPzMFitObject::getDPy(int ilocal) const
150  {
151  assert(ilocal >= 0 && ilocal < NPAR);
152  if (!cachevalid) updateCache();
153  switch (ilocal) {
154  case 0: return 0.0;
155  // case 1: return dpydtheta;
156  case 1: return 1.0;
157  case 2: return 0.0;
158  }
159  return 0;
160  }
161 
162  double PxPyPzMFitObject::getDPz(int ilocal) const
163  {
164  assert(ilocal >= 0 && ilocal < NPAR);
165  if (!cachevalid) updateCache();
166  switch (ilocal) {
167  case 0: return 0.0;
168  case 1: return 0.0;
169  case 2: return 1.0;
170  }
171  return 0;
172  }
173 
174  double PxPyPzMFitObject::getDE(int ilocal) const
175  {
176  assert(ilocal >= 0 && ilocal < NPAR);
177  if (!cachevalid) updateCache();
178  switch (ilocal) {
179  case 0: return dEdpx;
180  case 1: return dEdpy;
181  case 2: return dEdpz;
182  }
183  return 0;
184  }
185 
186  double PxPyPzMFitObject::getFirstDerivative_Meta_Local(int iMeta, int ilocal , int metaSet) const
187  {
188  // iMeta = intermediate variable (i.e. E,px,py,pz)
189  // ilocal = local variable (ptinv, theta, phi)
190  // metaSet = which set of intermediate varlables
191 
192  assert(metaSet == 0); // only defined for E,px,py,pz
193 
194  switch (iMeta) {
195  case 0: // E
196  return getDE(ilocal);
197  break;
198  case 1: // Px
199  return getDPx(ilocal);
200  break;
201  case 2: // Py
202  return getDPy(ilocal);
203  break;
204  case 3: // Pz
205  return getDPz(ilocal);
206  break;
207  default:
208  assert(0);
209  }
210  }
211 
212  double PxPyPzMFitObject::getSecondDerivative_Meta_Local(int iMeta, int ilocal , int jlocal , int metaSet) const
213  {
214  assert(metaSet == 0);
215  if (!cachevalid) updateCache();
216  if (jlocal < ilocal) {
217  int temp = jlocal;
218  jlocal = ilocal;
219  ilocal = temp;
220  }
221 
222  switch (iMeta) {
223  case 0: // E
224  if (ilocal == 0 && jlocal == 0) return dE2dpxdpx;
225  else if (ilocal == 0 && jlocal == 1) return dE2dpxdpy;
226  else if (ilocal == 0 && jlocal == 2) return dE2dpxdpz;
227  else if (ilocal == 1 && jlocal == 1) return dE2dpydpy;
228  else if (ilocal == 1 && jlocal == 2) return dE2dpydpz;
229  else if (ilocal == 2 && jlocal == 2) return dE2dpzdpz;
230  else return 0;
231  break;
232  case 1: // px
233  return 0;
234  break;
235  case 2: // py
236  return 0;
237  break;
238  case 3: // pz
239  return 0;
240  break;
241  default:
242  assert(0);
243  }
244  }
245 
246  void PxPyPzMFitObject::updateCache() const
247  {
248 
249  // get the basic quantities
250  double px = par[0];
251  double py = par[1];
252  double pz = par[2];
253  double px2 = px * px;
254  double py2 = py * py;
255  double pz2 = pz * pz;
256  double p = std::sqrt(px2 + py2 + pz2);
257  assert(p != 0);
258 
259  double mass2 = mass * mass;
260  double e2 = px2 + py2 + pz2 + mass2;
261  double e = std::sqrt(e2);
262 
263  // get the first derivatives
264  dEdpx = px / e;
265  dEdpy = py / e;
266  dEdpz = pz / e;
267 
268  // get second derivatives
269  double e32 = std::pow(e, 1.5);
270  dE2dpxdpx = (py2 + pz2 + mass2) / (e32);
271  dE2dpxdpy = -(px * py) / (e32);
272  dE2dpxdpz = -(px * pz) / (e32);
273  dE2dpydpy = (px2 + pz2 + mass2) / (e32);
274  dE2dpydpz = -(py * pz) / (e32);
275  dE2dpzdpz = (px2 + py2 + mass2) / (e32);
276 
277  fourMomentum.setValues(e, px, py, pz);
278 
279  cachevalid = true;
280  }
281  }// end OrcaKinFit namespace
283 } // end Belle2 namespace
284 
Belle2::OrcaKinFit::PxPyPzMFitObject::getDPz
virtual double getDPz(int ilocal) const override
Return d p_z / d par_ilocal (derivative of pz w.r.t. local parameter ilocal)
Definition: PxPyPzMFitObject.cc:162
prepareAsicCrosstalkSimDB.e
e
aux.
Definition: prepareAsicCrosstalkSimDB.py:53
Belle2::OrcaKinFit::PxPyPzMFitObject::getDPx
virtual double getDPx(int ilocal) const override
Return d p_x / d par_ilocal (derivative of px w.r.t. local parameter ilocal)
Definition: PxPyPzMFitObject.cc:137
Belle2::OrcaKinFit::ParticleFitObject::mass
double mass
mass of particle
Definition: ParticleFitObject.h:193
Belle2::OrcaKinFit::PxPyPzMFitObject::getParamName
virtual const char * getParamName(int ilocal) const override
Definition: PxPyPzMFitObject.cc:96
Belle2::OrcaKinFit::ParticleFitObject::setMass
virtual bool setMass(double mass_)
Set mass of particle; return=success.
Definition: ParticleFitObject.cc:63
Belle2::OrcaKinFit::PxPyPzMFitObject::getDPy
virtual double getDPy(int ilocal) const override
Return d p_y / d par_ilocal (derivative of py w.r.t. local parameter ilocal)
Definition: PxPyPzMFitObject.cc:149
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::OrcaKinFit::PxPyPzMFitObject::assign
virtual PxPyPzMFitObject & assign(const BaseFitObject &source) override
Assign from anther object, if of same type.
Definition: PxPyPzMFitObject.cc:83
Belle2::OrcaKinFit::PxPyPzMFitObject::operator=
PxPyPzMFitObject & operator=(const PxPyPzMFitObject &rhs)
Definition: PxPyPzMFitObject.cc:70
Belle2::OrcaKinFit::PxPyPzMFitObject::updateParams
virtual bool updateParams(double p[], int idim) override
Definition: PxPyPzMFitObject.cc:107
Belle2::OrcaKinFit::PxPyPzMFitObject
Definition: PxPyPzMFitObject.h:40
Belle2::OrcaKinFit::PxPyPzMFitObject::getDE
virtual double getDE(int ilocal) const override
Return d E / d par_ilocal (derivative of E w.r.t. local parameter ilocal)
Definition: PxPyPzMFitObject.cc:174
Belle2::OrcaKinFit::ParticleFitObject
Definition: ParticleFitObject.h:93
Belle2::OrcaKinFit::ParticleFitObject::assign
virtual ParticleFitObject & assign(const BaseFitObject &source) override
Assign from anther object, if of same type.
Definition: ParticleFitObject.cc:73