CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
LagrangeParentParticleFitter.cc
Go to the documentation of this file.
5 
8 
9 /*
10 RefCountedKinematicTree LagrangeParentParticleFitter::fit(RefCountedKinematicTree tree, KinematicConstraint * cs) const
11 {
12 //fitting top paticle only
13  tree->movePointerToTheTop();
14  RefCountedKinematicParticle particle = tree->currentParticle();
15  RefCountedKinematicVertex inVertex = tree->currentDecayVertex();
16 
17 //parameters and covariance matrix of the particle to fit
18  AlgebraicVector par = particle->currentState().kinematicParameters().vector();
19  AlgebraicSymMatrix cov = particle->currentState().kinematicParametersError().matrix();
20 
21 //chi2 and ndf
22  float chi = 0.;
23  float ndf = 0.;
24 
25 //constraint
26 //here it is defined at 7-point
27 //taken just at current state parameters
28  AlgebraicVector vl;
29  AlgebraicMatrix dr;
30  AlgebraicVector dev;
31 
32 //initial expansion point
33  AlgebraicVector exPoint = particle->currentState().kinematicParameters().vector();
34 
35 //refitted parameters and covariance matrix:
36 //simple and symmetric
37  AlgebraicVector refPar;
38  AlgebraicSymMatrix refCovS;
39 
40  int nstep = 0;
41  double df = 0.;
42  do{
43  chi = particle->chiSquared();
44  ndf = particle->degreesOfFreedom();
45  df = 0.;
46 
47 //derivative and value at expansion point
48  vl = cs->value(exPoint).first;
49  dr = cs->derivative(exPoint).first;
50  dev = cs->deviations();
51 
52 //residual between expansion and current parameters
53 //0 at the first step
54  AlgebraicVector delta_alpha = par - exPoint;
55 
56 //parameters needed for refit
57 // v_d = (D * V_alpha & * D.T)^(-1)
58  AlgebraicMatrix drt = dr.T();
59  AlgebraicMatrix v_d = dr * cov * drt;
60  int ifail = 0;
61  v_d.invert(ifail);
62  if(ifail != 0) throw VertexException("ParentParticleFitter::error inverting covariance matrix");
63 
64 //lagrangian multipliers
65 //lambda = V_d * (D * delta_alpha + d)
66  AlgebraicVector lambda = v_d * (dr*delta_alpha + vl);
67 
68 //refitted parameters
69  refPar = par - (cov * drt * lambda);
70 
71 //refitted covariance: simple and SymMatrix
72  refCovS = cov;
73  AlgebraicMatrix sPart = drt * v_d * dr;
74  AlgebraicMatrix covF = cov * sPart * cov;
75  AlgebraicSymMatrix sCovF(7,0);
76 
77  for(int i = 1; i<8; i++)
78  {
79  for(int j = 1; j<8; j++)
80  {if(i<=j) sCovF(i,j) = covF(i,j);}
81  }
82  refCovS -= sCovF;
83 
84  for(int i = 1; i < 8; i++)
85  {refCovS(i,i) += dev(i);}
86 
87 //chiSquared
88  chi += (lambda.T() * (dr*delta_alpha + vl))(1);
89  ndf += cs->numberOfEquations();
90 
91 //new expansionPoint
92  exPoint = refPar;
93  AlgebraicVector vlp = cs->value(exPoint).first;
94  for(int i = 1; i< vl.num_row();i++)
95  {df += std::abs(vlp(i));}
96  nstep++;
97  }while(df>theMaxDiff && nstep<theMaxStep);
98 
100 //creating an output KinematicParticle
101 //and putting it on its place in the tree
102  KinematicParameters param(refPar);
103  KinematicParametersError er(refCovS);
104  KinematicState kState(param,er,particle->initialState().particleCharge());
105  RefCountedKinematicParticle refParticle = particle->refittedParticle(kState,chi,ndf,cs->clone());
106  tree->replaceCurrentParticle(refParticle);
107 
108 //replacing the vertex with its refitted version
109  GlobalPoint nvPos(param.vector()(1), param.vector()(2), param.vector()(3));
110  AlgebraicSymMatrix nvMatrix = er.matrix().sub(1,3);
111  GlobalError nvError(asSMatrix<3>(nvMatrix));
112  VertexState vState(nvPos, nvError, 1.0);
113  KinematicVertexFactory vFactory;
114  RefCountedKinematicVertex nVertex = vFactory.vertex(vState, inVertex,chi,ndf);
115  tree->replaceCurrentVertex(nVertex);
116 
117  return tree;
118 }
119 */
120 
121 std::vector<RefCountedKinematicTree> LagrangeParentParticleFitter::fit(std::vector<RefCountedKinematicTree> trees,
122  KinematicConstraint * cs)const
123 {
124 
125  InputSort iSort;
126  std::vector<RefCountedKinematicParticle> prt = iSort.sort(trees);
127  int nStates = prt.size();
128 
129 //making full initial parameters and covariance
130  AlgebraicVector part(7*nStates,0);
131  AlgebraicSymMatrix cov(7*nStates,0);
132 
133  AlgebraicVector chi_in(nStates,0);
134  AlgebraicVector ndf_in(nStates,0);
135  int l_c=0;
136  for(std::vector<RefCountedKinematicParticle>::const_iterator i = prt.begin(); i != prt.end(); i++)
137  {
138  AlgebraicVector7 lp = (*i)->currentState().kinematicParameters().vector();
139  for(int j = 1; j != 8; j++){part(7*l_c + j) = lp(j-1);}
140  AlgebraicSymMatrix lc= asHepMatrix<7>((*i)->currentState().kinematicParametersError().matrix());
141  cov.sub(7*l_c+1,lc);
142  chi_in(l_c+1) = (*i)->chiSquared();
143  ndf_in(l_c+1) = (*i)->degreesOfFreedom();
144  l_c++;
145  }
146 //refitted parameters and covariance matrix:
147 //simple and symmetric
148  AlgebraicVector refPar;
149  AlgebraicSymMatrix refCovS;
150 
151 //constraint values, derivatives and deviations:
152  AlgebraicVector vl;
153  AlgebraicMatrix dr;
154  AlgebraicVector dev;
155  int nstep = 0;
156  double df = 0.;
157  AlgebraicVector exPoint = part;
158 
159 //this piece of code should later be refactored:
160 // The algorithm is the same as above, but the
161 // number of refitted particles is >1. Smart way of
162 // refactoring should be chosen for it.
163  AlgebraicVector chi;
164  AlgebraicVector ndf;
165 // cout<<"Starting the main loop"<<endl;
166  do{
167  df = 0.;
168  chi = chi_in;
169  ndf = ndf_in;
170 // cout<<"Iterational linearization point: "<<exPoint<<endl;
171  vl = cs->value(exPoint).first;
172  dr = cs->derivative(exPoint).first;
173  dev = cs->deviations(nStates);
174 
175 // cout<<"The value : "<<vl<<endl;
176 // cout<<"The derivative: "<<dr<<endl;
177 // cout<<"deviations: "<<dev<<endl;
178 // cout<<"covariance "<<cov<<endl;
179 
180 //residual between expansion and current parameters
181 //0 at the first step
182  AlgebraicVector delta_alpha = part - exPoint;
183 
184 //parameters needed for refit
185 // v_d = (D * V_alpha & * D.T)^(-1)
186  AlgebraicMatrix drt = dr.T();
187  AlgebraicMatrix v_d = dr * cov * drt;
188  int ifail = 0;
189  v_d.invert(ifail);
190  if(ifail != 0) {
191  LogDebug("KinematicConstrainedVertexFitter")
192  << "Fit failed: unable to invert covariance matrix\n";
193  return std::vector<RefCountedKinematicTree>();
194  }
195 
196 //lagrangian multipliers
197 //lambda = V_d * (D * delta_alpha + d)
198  AlgebraicVector lambda = v_d * (dr*delta_alpha + vl);
199 
200 //refitted parameters
201  refPar = part - (cov * drt * lambda);
202 
203 //refitted covariance: simple and SymMatrix
204  refCovS = cov;
205  AlgebraicMatrix sPart = drt * v_d * dr;
206  AlgebraicMatrix covF = cov * sPart * cov;
207 
208 //total covariance symmatrix
209  AlgebraicSymMatrix sCovF(nStates*7,0);
210  for(int i = 1; i< nStates*7 +1; ++i)
211  {
212  for(int j = 1; j< nStates*7 +1; j++)
213  {if(i<=j) sCovF(i,j) = covF(i,j);}
214  }
215 
216  refCovS -= sCovF;
217 
218 // cout<<"Fitter: refitted covariance "<<refCovS<<endl;
219  for(int i = 1; i < nStates+1; i++)
220  {for(int j = 1; j<8; j++){refCovS((i-1)+j,(i-1)+j) += dev(j);}}
221 
222 //chiSquared
223  for(int k =1; k<nStates+1; k++)
224  {
225  chi(k) += (lambda.T() * (dr*delta_alpha + vl))(1);
226  ndf(k) += cs->numberOfEquations();
227  }
228 //new expansionPoint
229  exPoint = refPar;
230  AlgebraicVector vlp = cs->value(exPoint).first;
231  for(int i = 1; i< vl.num_row();i++)
232  {df += std::abs(vlp(i));}
233  nstep++;
234  }while(df>theMaxDiff && nstep<theMaxStep);
235 //here math and iterative part is finished, starting an output production
236 //creating an output KinematicParticle and putting it on its place in the tree
237 
238 //vector of refitted particles and trees
239  std::vector<RefCountedKinematicParticle> refPart;
240  std::vector<RefCountedKinematicTree> refTrees = trees;
241 
242  int j=1;
243  std::vector<RefCountedKinematicTree>::const_iterator tr = refTrees.begin();
244  for(std::vector<RefCountedKinematicParticle>::const_iterator i = prt.begin(); i!= prt.end(); i++)
245  {
246  AlgebraicVector7 lRefPar;
247  for(int k = 1; k<8 ; k++)
248  {lRefPar(k-1) = refPar((j-1)*7+k);}
249  AlgebraicSymMatrix77 lRefCovS = asSMatrix<7>(refCovS.sub((j-1)*7 +1,(j-1)*7+7));
250 
251 //new refitted parameters and covariance
252  KinematicParameters param(lRefPar);
253  KinematicParametersError er(lRefCovS);
254  KinematicState kState(param,er,(*i)->initialState().particleCharge(), (**i).magneticField());
255  RefCountedKinematicParticle refParticle = (*i)->refittedParticle(kState,chi(j),ndf(j),cs->clone());
256 
257 //replacing particle itself
258  (*tr)->findParticle(*i);
259  RefCountedKinematicVertex inVertex = (*tr)->currentDecayVertex();
260  (*tr)->replaceCurrentParticle(refParticle);
261 
262 //replacing the vertex with its refitted version
263  GlobalPoint nvPos(param.position());
264  AlgebraicSymMatrix nvMatrix = asHepMatrix<7>(er.matrix()).sub(1,3);
265  GlobalError nvError(asSMatrix<3>(nvMatrix));
266  VertexState vState(nvPos, nvError, 1.0);
267  KinematicVertexFactory vFactory;
268  RefCountedKinematicVertex nVertex = vFactory.vertex(vState,inVertex,chi(j),ndf(j));
269  (*tr)->replaceCurrentVertex(nVertex);
270  tr++;
271  j++;
272  }
273  return refTrees;
274 }
275 
277 {
278  theMaxDiff = pSet.getParameter<double>("maxDistance");
279  theMaxStep = pSet.getParameter<int>("maxNbrOfIterations");;
280 }
281 
283 {
284  theMaxDiff = 0.001;
285  theMaxStep = 100;
286 }
#define LogDebug(id)
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
auto_ptr< ClusterSequence > cs
void setParameters(const edm::ParameterSet &pSet)
#define abs(x)
Definition: mlp_lapack.h:159
virtual KinematicConstraint * clone() const =0
ROOT::Math::SMatrix< double, 7, 7, ROOT::Math::MatRepSym< double, 7 > > AlgebraicSymMatrix77
Definition: Matrices.h:8
ROOT::Math::SVector< double, 7 > AlgebraicVector7
Definition: Matrices.h:7
CLHEP::HepMatrix AlgebraicMatrix
int j
Definition: DBlmapReader.cc:9
virtual int numberOfEquations() const =0
int k[5][pyjets_maxn]
CLHEP::HepVector AlgebraicVector
virtual AlgebraicVector deviations(int nStates) const =0
part
Definition: HCALResponse.h:21
virtual std::pair< AlgebraicMatrix, AlgebraicVector > derivative(const AlgebraicVector &exPoint) const =0
CLHEP::HepSymMatrix AlgebraicSymMatrix
static RefCountedKinematicVertex vertex(const VertexState &state, float totalChiSq, float degreesOfFr)
std::vector< RefCountedKinematicTree > fit(std::vector< RefCountedKinematicTree > trees, KinematicConstraint *cs) const
AlgebraicSymMatrix77 matrix() const
virtual std::pair< AlgebraicVector, AlgebraicVector > value(const AlgebraicVector &exPoint) const =0