Belle II Software  release-08-01-10
SoftGaussMassConstraint Class Reference

Implements constraint 0 = mass1 - mass2 - m. More...

#include <SoftGaussMassConstraint.h>

Inheritance diagram for SoftGaussMassConstraint:
Collaboration diagram for SoftGaussMassConstraint:

Public Member Functions

 SoftGaussMassConstraint (double sigma_, double mass_=0.)
 Constructor. More...
 
virtual ~SoftGaussMassConstraint ()
 Virtual destructor.
 
virtual double getValue () const override
 Returns the value of the constraint function.
 
virtual void getDerivatives (int idim, double der[]) const override
 Get first order derivatives. More...
 
virtual double getMass (int flag=1)
 Get the actual invariant mass of the fit objects with a given flag. More...
 
virtual void setMass (double mass_)
 Sets the target mass of the constraint. More...
 
virtual void setFOList (std::vector< ParticleFitObject * > *fitobjects_)
 Adds several ParticleFitObject objects to the list. More...
 
virtual void addToFOList (ParticleFitObject &fitobject, int flag=1)
 Adds one ParticleFitObject objects to the list.
 
virtual void resetFOList ()
 Resests ParticleFitObject list.
 
virtual double getChi2 () const override
 Returns the chi2.
 
virtual double getError () const override
 Returns the error on the value of the constraint.
 
virtual double getSigma () const
 Returns the sigma.
 
virtual double setSigma (double sigma_)
 Sets the sigma. More...
 
virtual void add2ndDerivativesToMatrix (double *M, int idim) const override
 Adds second order derivatives to global covariance matrix M. More...
 
virtual void addToGlobalChi2DerVector (double *y, int idim) const override
 Add derivatives of chi squared to global derivative matrix. More...
 
void invalidateCache () const
 Invalidates any cached values for the next event.
 
void test1stDerivatives ()
 
void test2ndDerivatives ()
 
double num1stDerivative (int ifo, int ilocal, double eps)
 Evaluates numerically the 1st derivative w.r.t. a parameter. More...
 
double num2ndDerivative (int ifo1, int ilocal1, double eps1, int ifo2, int ilocal2, double eps2)
 Evaluates numerically the 2nd derivative w.r.t. 2 parameters. More...
 
int getVarBasis () const
 
virtual const char * getName () const
 Returns the name of the constraint.
 
void setName (const char *name_)
 Set object's name.
 
virtual std::ostream & print (std::ostream &os) const
 print object to ostream More...
 

Protected Types

enum  { VAR_BASIS = BaseDefs::VARBASIS_EPXYZ }
 
typedef std::vector< ParticleFitObject * > FitObjectContainer
 Vector of pointers to ParticleFitObjects.
 
typedef FitObjectContainer::iterator FitObjectIterator
 Iterator through vector of pointers to ParticleFitObjects.
 
typedef FitObjectContainer::const_iterator ConstFitObjectIterator
 Constant iterator through vector of pointers to ParticleFitObjects.
 

Protected Member Functions

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 derivatives are zero. More...
 
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 zero. More...
 

Protected Attributes

double mass
 The mass difference between object sets 1 and 2.
 
FitObjectContainer fitobjects
 The FitObjectContainer.
 
std::vector< double > derivatives
 The derivatives.
 
std::vector< int > flags
 The flags can be used to divide the FitObjectContainer into several subsets used for example to implement an equal mass constraint (see MassConstraint).
 
double sigma
 The sigma of the Gaussian.
 
char * name
 

Related Functions

(Note that these are not member functions.)

std::ostream & operator<< (std::ostream &os, const BaseConstraint &bc)
 Prints out a BaseConstraint, using its print method. More...
 

Detailed Description

Implements constraint 0 = mass1 - mass2 - m.

This class implements different mass constraints:

  • the invariant mass of several objects should be m
  • the difference of the invariant masses between two sets of objects should be m (normally m=0 in this case).

Author: Jenny List, Benno List Last update:

Date
2008/02/12 16:43:26

by:

Author
blist

Definition at line 45 of file SoftGaussMassConstraint.h.

Constructor & Destructor Documentation

◆ SoftGaussMassConstraint()

SoftGaussMassConstraint ( double  sigma_,
double  mass_ = 0. 
)
explicit

Constructor.

Parameters
sigma_The sigma value
mass_The mass difference between object sets 1 and 2

Definition at line 35 of file SoftGaussMassConstraint.cc.

37  mass(mass_)
38  {}
double mass
The mass difference between object sets 1 and 2.
SoftGaussParticleConstraint(double sigma_)
Creates an empty SoftGaussParticleConstraint object.

Member Function Documentation

◆ add2ndDerivativesToMatrix()

void add2ndDerivativesToMatrix ( double *  M,
int  idim 
) const
overridevirtualinherited

Adds second order derivatives to global covariance matrix M.

Calculates the second derivative of the constraint g w.r.t.

the various parameters and adds it to the global covariance matrix

We denote with P_i the 4-vector of the i-th ParticleFitObject, then $$ \frac{\partial ^2 g}{\partial a_k \partial a_l} = \sum_i \sum_j \frac{\partial ^2 g}{\partial P_i \partial P_j} \cdot \frac{\partial P_i}{\partial a_k} \cdot \frac{\partial P_j}{\partial a_l}

  • \sum_i \frac{\partial g}{\partial P_i} \cdot \frac{\partial^2 P_i}{\partial a_k \partial a_l} $$ Here, $\frac{\partial P_i}{\partial a_k}$ is a $4 \times n_i$ Matrix, where $n_i$ is the number of parameters of FitObject i; Correspondingly, $\frac{\partial^2 P_i}{\partial a_k \partial a_l}$ is a $4 \times n_i \times n_i$ matrix. Also, $\frac{\partial ^2 g}{\partial P_i \partial P_j}$ is a $4\times 4$ matrix for a given i and j, and $\frac{\partial g}{\partial P_i}$ is a 4-vector (though not a Lorentz-vector!).

First, treat the part $$ \frac{\partial ^2 g}{\partial P_i \partial P_j} \cdot \frac{\partial P_i}{\partial a_k} \cdot \frac{\partial P_j}{\partial a_l} $$

Second, treat the parts $$ \sum_i \frac{\partial g}{\partial P_i} \cdot \frac{\partial^2 P_i}{\partial a_k \partial a_l} $$ and $$ \frac{\partial^2 h}{\partial g^2} \sum_i \frac{\partial g}{\partial P_i} \cdot \frac{\partial P_i}{\partial a_k} \sum_j \frac{\partial g}{\partial P_j} \cdot \frac{\partial P_j}{\partial a_l} $$

Here, $\frac{\partial g}{\partial P_i}$ is a 4-vector, which we pass on to the FitObject

Parameters
MCovariance matrix, at least idim x idim
idimFirst dimension of the array

Implements BaseSoftConstraint.

Definition at line 91 of file SoftGaussParticleConstraint.cc.

92  {
93 
100  double s = getSigma();
101  double fact = 2 * getValue() / (s * s);
102 
103  // Derivatives $\frac{\partial ^2 g}{\partial P_i \partial P_j}$ at fixed i, j
104  // d2GdPidPj[4*ii+jj] is derivative w.r.t. P_i,ii and P_j,jj, where ii=0,1,2,3 for E,px,py,pz
105  double d2GdPidPj[16];
106  // Derivatives $\frac {\partial P_i}{\partial a_k}$ for all i;
107  // k is local parameter number
108  // dPidAk[KMAX*4*i + 4*k + ii] is $\frac {\partial P_{i,ii}}{\partial a_k}$,
109  // with ii=0, 1, 2, 3 for E, px, py, pz
110  const int KMAX = 4;
111  const int n = fitobjects.size();
112  auto* dPidAk = new double[n * KMAX * 4];
113  bool* dPidAkval = new bool[n];
114 
115  for (int i = 0; i < n; ++i) dPidAkval[i] = false;
116 
117  // Derivatives $\frac{\partial ^2 g}{\partial P_i \partial a_l}$ at fixed i
118  // d2GdPdAl[4*l + ii] is $\frac{\partial ^2 g}{\partial P_{i,ii} \partial a_l}$
119  double d2GdPdAl[4 * KMAX];
120  // Derivatives $\frac{\partial ^2 g}{\partial a_k \partial a_l}$
121  double d2GdAkdAl[KMAX * KMAX] = {0};
122 
123  // Global parameter numbers: parglobal[KMAX*i+klocal]
124  // is global parameter number of local parameter klocal of i-th Fit object
125  int* parglobal = new int[KMAX * n];
126 
127  for (int i = 0; i < n; ++i) {
128  const ParticleFitObject* foi = fitobjects[i];
129  assert(foi);
130  for (int klocal = 0; klocal < foi->getNPar(); ++klocal) {
131  parglobal [KMAX * i + klocal] = foi->getGlobalParNum(klocal);
132  }
133  }
134 
135 
136  for (int i = 0; i < n; ++i) {
137  const ParticleFitObject* foi = fitobjects[i];
138  assert(foi);
139  for (int j = 0; j < n; ++j) {
140  const ParticleFitObject* foj = fitobjects[j];
141  assert(foj);
142  if (secondDerivatives(i, j, d2GdPidPj)) {
143  if (!dPidAkval[i]) {
144  foi->getDerivatives(dPidAk + i * (KMAX * 4), KMAX * 4);
145  dPidAkval[i] = true;
146  }
147  if (!dPidAkval[j]) {
148  foj->getDerivatives(dPidAk + j * (KMAX * 4), KMAX * 4);
149  dPidAkval[j] = true;
150  }
151  // Now sum over E/px/Py/Pz for object j:
152  // $$\frac{\partial ^2 g}{\partial P_{i,ii} \partial a_l}
153  // = (sum_{j}) sum_{jj} frac{\partial ^2 g}{\partial P_{i,ii} \partial P_{j,jj}}
154  // \cdot \frac{\partial P_{j,jj}}{\partial a_l}
155  // We're summing over jj here
156  for (int llocal = 0; llocal < foj->getNPar(); ++llocal) {
157  for (int ii = 0; ii < 4; ++ii) {
158  int ind1 = 4 * ii;
159  int ind2 = (KMAX * 4) * j + 4 * llocal;
160  double& r = d2GdPdAl[4 * llocal + ii];
161  r = d2GdPidPj[ ind1] * dPidAk[ ind2]; // E
162  r += d2GdPidPj[++ind1] * dPidAk[++ind2]; // px
163  r += d2GdPidPj[++ind1] * dPidAk[++ind2]; // py
164  r += d2GdPidPj[++ind1] * dPidAk[++ind2]; // pz
165  }
166  }
167  // Now sum over E/px/Py/Pz for object i, i.e. sum over ii:
168  // $$
169  // \frac{\partial ^2 g}{\partial a_k \partial a_l}
170  // = \sum_{ii} \frac{\partial ^2 g}{\partial P_{i,ii} \partial a_l} \cdot
171  // \frac{\partial P_{i,ii}}{\partial a_k}
172  // $$
173  for (int klocal = 0; klocal < foi->getNPar(); ++klocal) {
174  for (int llocal = 0; llocal < foj->getNPar(); ++llocal) {
175  int ind1 = 4 * llocal;
176  int ind2 = (KMAX * 4) * i + 4 * klocal;
177  double& r = d2GdAkdAl[KMAX * klocal + llocal];
178  r = d2GdPdAl[ ind1] * dPidAk[ ind2]; //E
179  r += d2GdPdAl[++ind1] * dPidAk[++ind2]; // px
180  r += d2GdPdAl[++ind1] * dPidAk[++ind2]; // py
181  r += d2GdPdAl[++ind1] * dPidAk[++ind2]; // pz
182  }
183  }
184  // Now expand the local parameter numbers to global ones
185  for (int klocal = 0; klocal < foi->getNPar(); ++klocal) {
186  int kglobal = parglobal [KMAX * i + klocal];
187  for (int llocal = 0; llocal < foj->getNPar(); ++llocal) {
188  int lglobal = parglobal [KMAX * j + llocal];
189  M [idim * kglobal + lglobal] += fact * d2GdAkdAl[KMAX * klocal + llocal];
190  }
191  }
192  }
193  }
194  }
213  auto* v = new double[idim];
214  for (int i = 0; i < idim; ++i) v[i] = 0;
215  double sqrtfact2 = sqrt(2.0) / s;
216 
217  double dgdpi[4];
218  for (int i = 0; i < n; ++i) {
219  const ParticleFitObject* foi = fitobjects[i];
220  assert(foi);
221  if (firstDerivatives(i, dgdpi)) {
222  foi->addTo2ndDerivatives(M, idim, fact, dgdpi, getVarBasis());
223  foi->addToGlobalChi2DerVector(v, idim, sqrtfact2, dgdpi, getVarBasis());
224  }
225  }
226 
227  for (int i = 0; i < idim; ++i) {
228  if (double vi = v[i]) {
229  int ioffs = i * idim;
230  for (double* pvj = v; pvj < v + idim; ++pvj) {
231  M[ioffs++] += vi * (*pvj);
232  }
233  }
234  }
235 
236 
237  delete[] dPidAk;
238  delete[] dPidAkval;
239  delete[] parglobal;
240  delete[] v;
241  }
virtual double getValue() const override=0
Returns the value of the constraint function.
FitObjectContainer fitobjects
The FitObjectContainer.
virtual bool secondDerivatives(int i, int j, double *derivatives) const =0
Second derivatives with respect to the 4-vectors of Fit objects i and j; result false if all derivati...
virtual bool firstDerivatives(int i, double *derivatives) const =0
First derivatives with respect to the 4-vector of Fit objects i; result false if all derivatives are ...
virtual double getSigma() const
Returns the sigma.
double sqrt(double a)
sqrt for double
Definition: beamHelpers.h:28

◆ addToGlobalChi2DerVector()

void addToGlobalChi2DerVector ( double *  y,
int  idim 
) const
overridevirtualinherited

Add derivatives of chi squared to global derivative matrix.

Parameters
yVector of chi2 derivatives
idimVector size

Implements BaseSoftConstraint.

Definition at line 243 of file SoftGaussParticleConstraint.cc.

◆ firstDerivatives()

bool firstDerivatives ( int  i,
double *  derivatives 
) const
overrideprotectedvirtual

First derivatives with respect to the 4-vector of Fit objects i; result false if all derivatives are zero.

Parameters
inumber of 1st FitObject
derivativesThe result 4-vector

Implements SoftGaussParticleConstraint.

Definition at line 191 of file SoftGaussMassConstraint.cc.

◆ getDerivatives()

void getDerivatives ( int  idim,
double  der[] 
) const
overridevirtual

Get first order derivatives.

Call this with a predefined array "der" with the necessary number of entries!

Parameters
idimFirst dimension of the array
derArray of derivatives, at least idim x idim

Implements SoftGaussParticleConstraint.

Definition at line 68 of file SoftGaussMassConstraint.cc.

◆ getMass()

double getMass ( int  flag = 1)
virtual

Get the actual invariant mass of the fit objects with a given flag.

Parameters
flagThe flag

Definition at line 123 of file SoftGaussMassConstraint.cc.

◆ num1stDerivative()

double num1stDerivative ( int  ifo,
int  ilocal,
double  eps 
)
inherited

Evaluates numerically the 1st derivative w.r.t. a parameter.

Parameters
ifoNumber of FitObject
ilocalLocal parameter number
epsvariation of local parameter

Definition at line 312 of file SoftGaussParticleConstraint.cc.

◆ num2ndDerivative()

double num2ndDerivative ( int  ifo1,
int  ilocal1,
double  eps1,
int  ifo2,
int  ilocal2,
double  eps2 
)
inherited

Evaluates numerically the 2nd derivative w.r.t. 2 parameters.

Parameters
ifo1Number of 1st FitObject
ilocal11st local parameter number
eps1variation of 1st local parameter
ifo2Number of 1st FitObject
ilocal21st local parameter number
eps2variation of 2nd local parameter

Definition at line 326 of file SoftGaussParticleConstraint.cc.

◆ print()

std::ostream & print ( std::ostream &  os) const
virtualinherited

print object to ostream

Parameters
osThe output stream

Definition at line 76 of file BaseConstraint.cc.

◆ secondDerivatives()

bool secondDerivatives ( int  i,
int  j,
double *  derivatives 
) const
overrideprotectedvirtual

Second derivatives with respect to the 4-vectors of Fit objects i and j; result false if all derivatives are zero.

Parameters
inumber of 1st FitObject
jnumber of 2nd FitObject
derivativesThe result 4x4 matrix

Implements SoftGaussParticleConstraint.

Definition at line 145 of file SoftGaussMassConstraint.cc.

◆ setFOList()

virtual void setFOList ( std::vector< ParticleFitObject * > *  fitobjects_)
inlinevirtualinherited

Adds several ParticleFitObject objects to the list.

Parameters
fitobjects_A list of BaseFitObject objects

Definition at line 83 of file SoftGaussParticleConstraint.h.

85  {
86  for (int i = 0; i < (int) fitobjects_->size(); i++) {
87  fitobjects.push_back((*fitobjects_)[i]);
88  flags.push_back(1);
89  }
90  };
std::vector< int > flags
The flags can be used to divide the FitObjectContainer into several subsets used for example to imple...

◆ setMass()

void setMass ( double  mass_)
virtual

Sets the target mass of the constraint.

Parameters
mass_The new mass

Definition at line 140 of file SoftGaussMassConstraint.cc.

◆ setSigma()

double setSigma ( double  sigma_)
virtualinherited

Sets the sigma.

Parameters
sigma_The new sigma value

Definition at line 42 of file SoftGaussParticleConstraint.cc.

Friends And Related Function Documentation

◆ operator<<()

std::ostream & operator<< ( std::ostream &  os,
const BaseConstraint bc 
)
related

Prints out a BaseConstraint, using its print method.

Parameters
osThe output stream
bcThe object to print

Definition at line 114 of file BaseConstraint.h.


The documentation for this class was generated from the following files: