CMS 3D CMS Logo

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