Belle II Software  release-05-01-25
MomentumConstraint.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * See https://github.com/tferber/OrcaKinfit, forked from *
4  * https://github.com/iLCSoft/MarlinKinfit *
5  * *
6  * Further information about the fit engine and the user interface *
7  * provided in MarlinKinfit can be found at *
8  * https://www.desy.de/~blist/kinfit/doc/html/ *
9  * and in the LCNotes LC-TOOL-2009-001 and LC-TOOL-2009-004 available *
10  * from http://www-flc.desy.de/lcnotes/ *
11  * *
12  * Adopted by: Torben Ferber (torben.ferber@desy.de) (TF) *
13  * *
14  * This software is provided "as is" without any warranty. *
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 
27 namespace 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
49  double MomentumConstraint::getValue() const
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 contraint
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
Belle2::OrcaKinFit::MomentumConstraint::getDerivatives
virtual void getDerivatives(int idim, double der[]) const override
Get first order derivatives.
Definition: MomentumConstraint.cc:87
Belle2::OrcaKinFit::ParticleFitObject::getE
virtual double getE() const
Return E.
Definition: ParticleFitObject.cc:106
Belle2::OrcaKinFit::ParticleFitObject::getPx
virtual double getPx() const
Return px.
Definition: ParticleFitObject.cc:110
Belle2::OrcaKinFit::MomentumConstraint::invalidateCache
virtual void invalidateCache() const override
Invalidates any cached values for the next event.
Definition: MomentumConstraint.cc:107
Belle2::OrcaKinFit::BaseHardConstraint::fitobjects
FitObjectContainer fitobjects
The FitObjectContainer.
Definition: BaseHardConstraint.h:191
Belle2::OrcaKinFit::MomentumConstraint::getValue
virtual double getValue() const override
Returns the value of the constraint.
Definition: MomentumConstraint.cc:63
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::OrcaKinFit::MomentumConstraint::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: MomentumConstraint.cc:127
Belle2::OrcaKinFit::ParticleFitObject::getPy
virtual double getPy() const
Return py.
Definition: ParticleFitObject.cc:114
Belle2::OrcaKinFit::MomentumConstraint::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: MomentumConstraint.cc:135
Belle2::OrcaKinFit::ParticleFitObject
Definition: ParticleFitObject.h:93
Belle2::OrcaKinFit::ParticleFitObject::getPz
virtual double getPz() const
Return pz.
Definition: ParticleFitObject.cc:118
Belle2::OrcaKinFit::MomentumConstraint::MomentumConstraint
MomentumConstraint(double efact_=0, double pxfact_=0, double pyfact_=0, double pzfact_=0, double value_=0)
Definition: MomentumConstraint.cc:48