CMS 3D CMS Logo

TAbsFitParticle.cc
Go to the documentation of this file.
1 // Classname: TAbsFitParticle
2 // Author: Jan E. Sundermann, Verena Klose (TU Dresden)
3 
4 //________________________________________________________________
5 //
6 // TAbsFitParticle::
7 // --------------------
8 //
9 // Abstract base class for particles to be used with kinematic fitter
10 //
11 
14 #include <iostream>
15 #include <iomanip>
16 #include "TClass.h"
17 
19  : TNamed("NoName", "NoTitle"),
20  _nPar(0),
21  _u1(),
22  _u2(),
23  _u3(),
24  _covMatrix(),
25  _covMatrixFit(),
26  _covMatrixDeltaY(),
27  _pull(),
28  _iniparameters(1, 1),
29  _parameters(1, 1),
30  _pini(),
31  _pcurr() {}
32 
33 TAbsFitParticle::TAbsFitParticle(const TString& name, const TString& title)
34  : TNamed(name, title),
35  _nPar(0),
36  _u1(),
37  _u2(),
38  _u3(),
39  _covMatrix(),
40  _covMatrixFit(),
41  _covMatrixDeltaY(),
42  _pull(),
43  _iniparameters(1, 1),
44  _parameters(1, 1),
45  _pini(),
46  _pcurr() {}
47 
49 
51  // Collect information to be used for printout
52 
53  std::stringstream info;
54  info << std::scientific << std::setprecision(6);
55 
56  info << "__________________________" << std::endl << std::endl;
57 
58  info << "OBJ: " << IsA()->GetName() << "\t" << GetName() << "\t" << GetTitle() << std::endl;
59 
60  info << std::setw(22) << "initial parameters:" << std::setw(5) << " " << std::setw(20)
61  << "current parameters:" << std::endl;
62  for (int i = 0; i < _nPar; i++) {
63  info << "par[" << i << "] = " << std::setw(18) << (*getParIni())(i, 0) << std::setw(20) << (*getParCurr())(i, 0)
64  << std::endl;
65  }
66 
67  info << std::setw(22) << "initial 4vector:" << std::setw(5) << " " << std::setw(20)
68  << "current 4vector:" << std::endl;
69  for (int i = 0; i < 4; i++) {
70  info << "p[" << i << "] = " << std::setw(20) << (*getIni4Vec())[i] << std::setw(20) << (*getCurr4Vec())[i]
71  << std::endl;
72  }
73  info << "mass = " << std::setw(20) << (*getIni4Vec()).M() << std::setw(20) << (*getCurr4Vec()).M() << std::endl;
74 
75  info << "u1 = " << _u1.X() << ", " << _u1.Y() << ", " << _u1.Z() << std::endl;
76  info << "u2 = " << _u2.X() << ", " << _u2.Y() << ", " << _u2.Z() << std::endl;
77  info << "u3 = " << _u3.X() << ", " << _u3.Y() << ", " << _u3.Z() << std::endl;
78 
79  return info.str();
80 }
81 
83  // Print particle contents
84 
85  edm::LogVerbatim("KinFitter") << this->getInfoString();
86 }
87 
89  // Reset particle to initial values
90 
92  _pcurr = _pini;
93  setCovMatrixFit(nullptr);
94  _pull.ResizeTo(_nPar, 1);
95  _pull.Zero();
96 }
97 
98 void TAbsFitParticle::setCovMatrix(const TMatrixD* theCovMatrix) {
99  // Set the measured covariance matrix
100 
101  _covMatrix.ResizeTo(_nPar, _nPar);
102  if (theCovMatrix == nullptr) {
103  _covMatrix.Zero();
104  } else if (theCovMatrix->GetNcols() == _nPar && theCovMatrix->GetNrows() == _nPar) {
105  _covMatrix = (*theCovMatrix);
106  } else {
107  edm::LogError("WrongMatrixSize") << GetName() << "::setCovMatrix - Covariance matrix needs to be a " << _nPar << "x"
108  << _nPar << " matrix.";
109  }
110 }
111 
112 void TAbsFitParticle::setCovMatrixFit(const TMatrixD* theCovMatrixFit) {
113  // Set the fitted covariance matrix
114 
115  _covMatrixFit.ResizeTo(_nPar, _nPar);
116  if (theCovMatrixFit == nullptr) {
117  _covMatrixFit.Zero();
118  } else if (theCovMatrixFit->GetNcols() == _nPar && theCovMatrixFit->GetNrows() == _nPar) {
119  _covMatrixFit = (*theCovMatrixFit);
120  } else {
121  edm::LogError("WrongMatrixSize") << GetName() << "::setCovMatrixFit - Fitted covariance matrix needs to be a "
122  << _nPar << "x" << _nPar << " matrix.";
123  }
124 }
125 
127  // Calculates V(deltaY) == V(y_meas) - V(y_fit)
128 
129  _covMatrixDeltaY.ResizeTo(_nPar, _nPar);
131  if (_covMatrixFit.GetNrows() == _nPar && _covMatrixFit.GetNcols() == _nPar)
133  else
134  edm::LogError("WrongMatrixSize") << GetName() << "::calcCovMatrixDeltaY - _covMatrixFit probably not set.";
135 }
136 
137 const TMatrixD* TAbsFitParticle::getPull() {
138  // Calculates the pull (y_fit - y_meas) / sigma
139  // with sigma = Sqrt( sigma[y_meas]^2 - V[y_fit]^2 )
140  // for all parameters
141 
142  _pull.ResizeTo(_nPar, 1);
143  _pull = _parameters;
146  for (int i = 0; i < _nPar; i++) {
147  Double_t sigmaDeltaY = _covMatrixDeltaY(i, i);
148  if (sigmaDeltaY < 0) {
149  edm::LogWarning("NegativeDiagonalElem") << "V[deltaY] has a negative diagonal element.";
150  _pull.Zero();
151  return &_pull;
152  } else {
153  _pull(i, 0) /= TMath::Sqrt(sigmaDeltaY);
154  }
155  }
156 
157  return &_pull;
158 }
159 
160 void TAbsFitParticle::applycorr(TMatrixD* corrMatrix) {
161  // Apply corrections to the parameters wrt. to the
162  // initial parameters y* = y + delta(y)
163  // This method will also calculate the fitted
164  // 4vector of the particle
165 
166  // update _parameters-Matrix
168  _parameters += (*corrMatrix);
169 
170  // calculates new 4vec
171  TLorentzVector* vec = calc4Vec(&_parameters);
172  _pcurr = (*vec);
173  delete vec;
174 }
175 
176 void TAbsFitParticle::setParIni(const TMatrixD* parini) {
177  if (parini == nullptr)
178  return;
179  else if (parini->GetNrows() == _iniparameters.GetNrows() && parini->GetNcols() == _iniparameters.GetNcols())
180  _iniparameters = (*parini);
181  else {
182  edm::LogError("WrongMatrixSize") << GetName() << "::setParIni - Matrices don't fit.";
183  return;
184  }
185 }
186 
188  //
190  return &_covMatrixDeltaY;
191 }
Log< level::Info, true > LogVerbatim
TMatrixD _covMatrixFit
static const TGPicture * info(bool iBackgroundIsBlack)
TString getInfoString()
const TLorentzVector * getIni4Vec()
virtual void reset()
virtual const TMatrixD * getCovMatrixDeltaY()
~TAbsFitParticle() override
Log< level::Error, false > LogError
virtual TLorentzVector * calc4Vec(const TMatrixD *params)=0
TLorentzVector _pini
TMatrixD _parameters
TLorentzVector _pcurr
virtual void setCovMatrix(const TMatrixD *theCovMatrix)
virtual void applycorr(TMatrixD *corrMatrix)
TMatrixD _covMatrixDeltaY
virtual void print()
const TMatrixD * getParIni()
const TLorentzVector * getCurr4Vec()
TMatrixD _iniparameters
void setParIni(const TMatrixD *parini)
Log< level::Warning, false > LogWarning
virtual void setCovMatrixFit(const TMatrixD *theCovMatrixFit)
virtual const TMatrixD * getPull()
const TMatrixD * getParCurr()