CMS 3D CMS Logo

KinematicConstrainedVertexFitterT.h
Go to the documentation of this file.
1 #ifndef KinematicConstrainedVertexFitterT_H
2 #define KinematicConstrainedVertexFitterT_H
3 
9 
11 
14 
24 template <int nTrk, int nConstraint>
26 public:
30  explicit KinematicConstrainedVertexFitterT(const MagneticField* ifield);
31 
36 
38 
44  void setParameters(const edm::ParameterSet& pSet);
45 
50  RefCountedKinematicTree fit(const std::vector<RefCountedKinematicParticle>& part) { return fit(part, 0, 0); }
51 
55  RefCountedKinematicTree fit(const std::vector<RefCountedKinematicParticle>& part,
57  return fit(part, cs, nullptr);
58  };
59 
63  RefCountedKinematicTree fit(const std::vector<RefCountedKinematicParticle>& part,
65  GlobalPoint* pt);
66 
67  //return the number of iterations
68  int getNit() const;
69  //return the value of the constraint equation
70  float getCSum() const;
71 
72 private:
73  void defaultParameters();
74 
80 
81  float theMaxDelta; //maximum (delta parameter)^2/(sigma parameter)^2 per iteration for convergence
83  float theMaxReducedChiSq; //max of initial (after 2 iterations) chisq/dof value
84  float theMinChiSqImprovement; //minimum required improvement in chisq to avoid fit termination for cases exceeding theMaxReducedChiSq
85 
87  float csum;
88 };
89 
94 
95 template <int nTrk, int nConstraint>
97  : field(ifield) {
103  iterations = -1;
104  csum = -1000.0;
105 }
106 
107 template <int nTrk, int nConstraint>
109  const MagneticField* ifield, const LinearizationPointFinder& fnd)
110  : field(ifield) {
111  finder = fnd.clone();
116  iterations = -1;
117  csum = -1000.0;
118 }
119 
120 template <int nTrk, int nConstraint>
122  delete finder;
123  delete vCons;
124  delete updator;
125  delete tBuilder;
126 }
127 
128 template <int nTrk, int nConstraint>
130  theMaxDelta = pSet.getParameter<double>("maxDelta");
131  theMaxStep = pSet.getParameter<int>("maxNbrOfIterations");
132  theMaxReducedChiSq = pSet.getParameter<double>("maxReducedChiSq");
133  theMinChiSqImprovement = pSet.getParameter<double>("minChiSqImprovement");
134 }
135 
136 template <int nTrk, int nConstraint>
138  theMaxDelta = 0.01;
139  theMaxStep = 1000;
140  theMaxReducedChiSq = 225.;
141  theMinChiSqImprovement = 50.;
142 }
143 
144 template <int nTrk, int nConstraint>
146  const std::vector<RefCountedKinematicParticle>& part,
148  GlobalPoint* pt) {
149  assert(nConstraint == 0 || cs != nullptr);
150  if (part.size() != nTrk)
151  throw VertexException("KinematicConstrainedVertexFitterT::input states are not nTrk");
152 
153  //sorting out the input particles
154  InputSort iSort;
155  std::pair<std::vector<RefCountedKinematicParticle>, std::vector<FreeTrajectoryState> > input = iSort.sort(part);
156  const std::vector<RefCountedKinematicParticle>& particles = input.first;
157  const std::vector<FreeTrajectoryState>& fStates = input.second;
158 
159  // linearization point:
160  GlobalPoint linPoint = (pt != nullptr) ? *pt : finder->getLinearizationPoint(fStates);
161 
162  //initial parameters:
163  ROOT::Math::SVector<double, 3 + 7 * nTrk> inPar; //3+ 7*ntracks
164  ROOT::Math::SVector<double, 3 + 7 * nTrk> finPar; //3+ 7*ntracks
165 
166  ROOT::Math::SMatrix<double, 3 + 7 * nTrk, 3 + 7 * nTrk, ROOT::Math::MatRepSym<double, 3 + 7 * nTrk> > inCov;
167 
168  //making initial vector of parameters and initial particle-related covariance
169  int nSt = 0;
170  std::vector<KinematicState> lStates(nTrk);
171  for (std::vector<RefCountedKinematicParticle>::const_iterator i = particles.begin(); i != particles.end(); i++) {
172  lStates[nSt] = (*i)->stateAtPoint(linPoint);
173  KinematicState const& state = lStates[nSt];
174  if (!state.isValid()) {
175  LogDebug("KinematicConstrainedVertexFitter") << "State is invalid at point: " << linPoint << std::endl;
177  }
178  inPar.Place_at(state.kinematicParameters().vector(), 3 + 7 * nSt);
179  inCov.Place_at(state.kinematicParametersError().matrix(), 3 + 7 * nSt, 3 + 7 * nSt);
180  ++nSt;
181  }
182 
183  //initial vertex error matrix components (huge error method)
184  //and vertex related initial vector components
185  double in_er = 100.;
186  inCov(0, 0) = in_er;
187  inCov(1, 1) = in_er;
188  inCov(2, 2) = in_er;
189 
190  inPar(0) = linPoint.x();
191  inPar(1) = linPoint.y();
192  inPar(2) = linPoint.z();
193 
194  //constraint equations value and number of iterations
195  double eq;
196  int nit = 0;
197  iterations = 0;
198  csum = 0.0;
199 
200  GlobalPoint lPoint = linPoint;
202  ROOT::Math::SMatrix<double, 3 + 7 * nTrk, 3 + 7 * nTrk, ROOT::Math::MatRepSym<double, 3 + 7 * nTrk> > refCCov =
203  ROOT::Math::SMatrixNoInit();
204 
205  double chisq = 1e6;
206  bool convergence = false;
207 
208  //iterarions over the updator: each time updated parameters
209  //are taken as new linearization point
210  do {
211  eq = 0.;
212  refCCov = inCov;
213  std::vector<KinematicState> oldStates = lStates;
214  GlobalVector mf = field->inInverseGeV(lPoint);
215  rVtx = updator->update(inPar, refCCov, lStates, lPoint, mf, cs);
216  if (particles.size() != lStates.size() || rVtx == nullptr) {
217  LogDebug("KinematicConstrainedVertexFitter") << "updator failure\n";
219  }
220 
221  double newchisq = rVtx->chiSquared();
222  if (nit > 2 && newchisq > theMaxReducedChiSq * rVtx->degreesOfFreedom() &&
223  (newchisq - chisq) > (-theMinChiSqImprovement)) {
224  LogDebug("KinematicConstrainedVertexFitter") << "bad chisq and insufficient improvement, bailing\n";
226  }
227  chisq = newchisq;
228 
229  const GlobalPoint& newPoint = rVtx->position();
230 
231  double maxDelta = 0.0;
232 
233  double deltapos[3];
234  deltapos[0] = newPoint.x() - lPoint.x();
235  deltapos[1] = newPoint.y() - lPoint.y();
236  deltapos[2] = newPoint.z() - lPoint.z();
237  for (int i = 0; i < 3; ++i) {
238  double delta = deltapos[i] * deltapos[i] / rVtx->error().matrix()(i, i);
239  if (delta > maxDelta)
240  maxDelta = delta;
241  }
242 
243  for (std::vector<KinematicState>::const_iterator itold = oldStates.begin(), itnew = lStates.begin();
244  itnew != lStates.end();
245  ++itold, ++itnew) {
246  for (int i = 0; i < 7; ++i) {
247  double deltapar = itnew->kinematicParameters()(i) - itold->kinematicParameters()(i);
248  double delta = deltapar * deltapar / itnew->kinematicParametersError().matrix()(i, i);
249  if (delta > maxDelta)
250  maxDelta = delta;
251  }
252  }
253 
254  lPoint = newPoint;
255 
256  nit++;
257  convergence = maxDelta < theMaxDelta || (nit == theMaxStep && maxDelta < 4.0 * theMaxDelta);
258 
259  } while (nit < theMaxStep && !convergence);
260 
261  if (!convergence) {
263  }
264 
265  // std::cout << "new full cov matrix" << std::endl;
266  // std::cout << refCCov << std::endl;
267 
268  iterations = nit;
269  csum = eq;
270 
271  return tBuilder->buildTree<nTrk>(particles, lStates, rVtx, refCCov);
272 }
273 
274 template <int nTrk, int nConstraint>
276  return iterations;
277 }
278 
279 template <int nTrk, int nConstraint>
281  return csum;
282 }
283 
284 #endif
std::pair< std::vector< RefCountedKinematicParticle >, std::vector< FreeTrajectoryState > > sort(const std::vector< RefCountedKinematicParticle > &particles) const
Definition: InputSort.cc:5
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
T z() const
Definition: PV3DBase.h:61
Common base class.
assert(be >=bs)
static std::string const input
Definition: EdmProvDump.cc:50
T x() const
Definition: PV3DBase.h:59
T y() const
Definition: PV3DBase.h:60
void setParameters(const edm::ParameterSet &pSet)
KinematicConstrainedVertexUpdatorT< nTrk, nConstraint > * updator
KinematicConstrainedVertexFitterT(const MagneticField *ifield)
part
Definition: HCALResponse.h:20
RefCountedKinematicTree fit(const std::vector< RefCountedKinematicParticle > &part, MultiTrackKinematicConstraintT< nTrk, nConstraint > *cs)
virtual LinearizationPointFinder * clone() const =0
RefCountedKinematicTree fit(const std::vector< RefCountedKinematicParticle > &part)
#define LogDebug(id)