CMS 3D CMS Logo

KinematicConstrainedVertexFitter.cc
Go to the documentation of this file.
6 
13  iterations = -1;
14  csum = -1000.0;
15 }
16 
18  finder = fnd.clone();
23  iterations = -1;
24  csum = -1000.0;
25 }
26 
28  delete finder;
29  delete vCons;
30  delete updator;
31  delete tBuilder;
32 }
33 
35  theMaxDelta = pSet.getParameter<double>("maxDelta");
36  theMaxStep = pSet.getParameter<int>("maxNbrOfIterations");
37  theMaxReducedChiSq = pSet.getParameter<double>("maxReducedChiSq");
38  theMinChiSqImprovement = pSet.getParameter<double>("minChiSqImprovement");
39 }
40 
42  theMaxDelta = 0.01;
43  theMaxStep = 1000;
44  theMaxReducedChiSq = 225.;
46 }
47 
48 RefCountedKinematicTree KinematicConstrainedVertexFitter::fit(const std::vector<RefCountedKinematicParticle> &part,
50  GlobalPoint *pt) {
51  if (part.size() < 2)
52  throw VertexException("KinematicConstrainedVertexFitter::input states are less than 2");
53 
54  //sorting out the input particles
55  InputSort iSort;
56  std::pair<std::vector<RefCountedKinematicParticle>, std::vector<FreeTrajectoryState> > input = iSort.sort(part);
57  const std::vector<RefCountedKinematicParticle> &particles = input.first;
58  const std::vector<FreeTrajectoryState> &fStates = input.second;
59 
60  // linearization point
61  // (only compute it using the linearization point finder if no point was passed to the fit function):
62  GlobalPoint linPoint;
63  if (pt != nullptr) {
64  linPoint = *pt;
65  } else {
66  linPoint = finder->getLinearizationPoint(fStates);
67  }
68 
69  //initial parameters:
70  int vSize = particles.size();
71  AlgebraicVector inPar(3 + 7 * vSize, 0);
72 
73  //final parameters
74  AlgebraicVector finPar(3 + 7 * vSize, 0);
75 
76  //initial covariance
77  AlgebraicMatrix inCov(3 + 7 * vSize, 3 + 7 * vSize, 0);
78 
79  //making initial vector of parameters and initial particle-related covariance
80  int nSt = 0;
81  std::vector<KinematicState> inStates;
82  for (std::vector<RefCountedKinematicParticle>::const_iterator i = particles.begin(); i != particles.end(); i++) {
83  KinematicState state = (*i)->stateAtPoint(linPoint);
84  if (!state.isValid()) {
85  LogDebug("KinematicConstrainedVertexFitter") << "State is invalid at point: " << linPoint << std::endl;
87  }
88  AlgebraicVector prPar = asHepVector<7>(state.kinematicParameters().vector());
89  for (int j = 1; j < 8; j++) {
90  inPar(3 + 7 * nSt + j) = prPar(j);
91  }
92  AlgebraicSymMatrix l_cov = asHepMatrix<7>(state.kinematicParametersError().matrix());
93  inCov.sub(4 + 7 * nSt, 4 + 7 * nSt, l_cov);
94  inStates.push_back(state);
95  ++nSt;
96  }
97 
98  //initial vertex error matrix components (huge error method)
99  //and vertex related initial vector components
100  double in_er = 100.;
101  inCov(1, 1) = in_er;
102  inCov(2, 2) = in_er;
103  inCov(3, 3) = in_er;
104 
105  inPar(1) = linPoint.x();
106  inPar(2) = linPoint.y();
107  inPar(3) = linPoint.z();
108 
109  //constraint equations value and number of iterations
110  double eq;
111  int nit = 0;
112  iterations = 0;
113  csum = 0.0;
114 
115  std::vector<KinematicState> lStates = inStates;
116  GlobalPoint lPoint = linPoint;
118  AlgebraicMatrix refCCov;
119 
120  double chisq = 1e6;
121  bool convergence = false;
122  //iterarions over the updator: each time updated parameters
123  //are taken as new linearization point
124  do {
125  eq = 0.;
126  std::pair<std::pair<std::vector<KinematicState>, AlgebraicMatrix>, RefCountedKinematicVertex> lRes =
127  updator->update(inPar, inCov, lStates, lPoint, cs);
128 
129  const std::vector<KinematicState> &newStates = lRes.first.first;
130 
131  if (particles.size() != newStates.size()) {
132  LogDebug("KinematicConstrainedVertexFitter") << "updator failure\n";
134  }
135 
136  rVtx = lRes.second;
137 
138  double newchisq = rVtx->chiSquared();
139  if (nit > 2 && newchisq > theMaxReducedChiSq * rVtx->degreesOfFreedom() &&
140  (newchisq - chisq) > (-theMinChiSqImprovement)) {
141  LogDebug("KinematicConstrainedVertexFitter") << "bad chisq and insufficient improvement, bailing\n";
143  }
144  chisq = newchisq;
145 
146  const GlobalPoint &newPoint = rVtx->position();
147 
148  double maxDelta = 0.0;
149 
150  double deltapos[3];
151  deltapos[0] = newPoint.x() - lPoint.x();
152  deltapos[1] = newPoint.y() - lPoint.y();
153  deltapos[2] = newPoint.z() - lPoint.z();
154  for (int i = 0; i < 3; ++i) {
155  double delta = deltapos[i] * deltapos[i] / rVtx->error().matrix()(i, i);
156  if (delta > maxDelta)
157  maxDelta = delta;
158  }
159 
160  for (std::vector<KinematicState>::const_iterator itold = lStates.begin(), itnew = newStates.begin();
161  itnew != newStates.end();
162  ++itold, ++itnew) {
163  for (int i = 0; i < 7; ++i) {
164  double deltapar = itnew->kinematicParameters()(i) - itold->kinematicParameters()(i);
165  double delta = deltapar * deltapar / itnew->kinematicParametersError().matrix()(i, i);
166  if (delta > maxDelta)
167  maxDelta = delta;
168  }
169  }
170 
171  lStates = newStates;
172  lPoint = newPoint;
173 
174  refCCov = lRes.first.second;
175  nit++;
176  convergence = maxDelta < theMaxDelta || (nit == theMaxStep && maxDelta < 4.0 * theMaxDelta);
177 
178  } while (nit < theMaxStep && !convergence);
179 
180  if (!convergence) {
182  }
183 
184  // std::cout << "old full cov matrix" << std::endl;
185  // std::cout << refCCov << std::endl;
186 
187  // cout<<"number of relinearizations "<<nit<<endl;
188  // cout<<"value obtained: "<<eq<<endl;
189  iterations = nit;
190  csum = eq;
191 
192  return tBuilder->buildTree(particles, lStates, rVtx, refCCov);
193 }
194 
196 
std::pair< std::vector< RefCountedKinematicParticle >, std::vector< FreeTrajectoryState > > sort(const std::vector< RefCountedKinematicParticle > &particles) const
Definition: InputSort.cc:5
RefCountedKinematicTree fit(const std::vector< RefCountedKinematicParticle > &part)
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
T z() const
Definition: PV3DBase.h:61
Common base class.
RefCountedKinematicTree buildTree(const std::vector< RefCountedKinematicParticle > &initialParticles, const std::vector< KinematicState > &finalStates, const RefCountedKinematicVertex vtx, const AlgebraicMatrix &fCov) const
static std::string const input
Definition: EdmProvDump.cc:50
T x() const
Definition: PV3DBase.h:59
T y() const
Definition: PV3DBase.h:60
CLHEP::HepMatrix AlgebraicMatrix
KinematicConstrainedVertexUpdator * updator
std::pair< std::pair< std::vector< KinematicState >, AlgebraicMatrix >, RefCountedKinematicVertex > update(const AlgebraicVector &inState, const AlgebraicMatrix &inCov, const std::vector< KinematicState > &lStates, const GlobalPoint &lPoint, MultiTrackKinematicConstraint *cs) const
void setParameters(const edm::ParameterSet &pSet)
CLHEP::HepVector AlgebraicVector
part
Definition: HCALResponse.h:20
virtual GlobalPoint getLinearizationPoint(const std::vector< reco::TransientTrack > &) const =0
virtual LinearizationPointFinder * clone() const =0
CLHEP::HepSymMatrix AlgebraicSymMatrix
#define LogDebug(id)