CMS 3D CMS Logo

LagrangeParentParticleFitter.cc

Go to the documentation of this file.
00001 #include "RecoVertex/KinematicFit/interface/LagrangeParentParticleFitter.h"
00002 #include "RecoVertex/KinematicFitPrimitives/interface/KinematicVertexFactory.h"
00003 // #include "Utilities/UI/interface/SimpleConfigurable.h"
00004 #include "RecoVertex/KinematicFit/interface/InputSort.h"
00005 #include "RecoVertex/VertexPrimitives/interface/VertexException.h"
00006 
00007 LagrangeParentParticleFitter::LagrangeParentParticleFitter()
00008 {readParameters();}
00009 
00010 /*
00011 RefCountedKinematicTree LagrangeParentParticleFitter::fit(RefCountedKinematicTree tree, KinematicConstraint * cs) const
00012 {
00013 //fitting top paticle only
00014  tree->movePointerToTheTop();
00015  RefCountedKinematicParticle particle = tree->currentParticle(); 
00016  RefCountedKinematicVertex inVertex = tree->currentDecayVertex();
00017 
00018 //parameters and covariance matrix of the particle to fit 
00019  AlgebraicVector par = particle->currentState().kinematicParameters().vector();
00020  AlgebraicSymMatrix cov = particle->currentState().kinematicParametersError().matrix();
00021 
00022 //chi2 and ndf
00023  float chi = 0.;
00024  float ndf = 0.;
00025  
00026 //constraint
00027 //here it is defined at 7-point
00028 //taken just at current state parameters
00029  AlgebraicVector vl;
00030  AlgebraicMatrix dr;
00031  AlgebraicVector dev;
00032  
00033 //initial expansion point 
00034  AlgebraicVector exPoint = particle->currentState().kinematicParameters().vector();
00035  
00036 //refitted parameters and covariance matrix:
00037 //simple and symmetric 
00038  AlgebraicVector refPar;
00039  AlgebraicSymMatrix refCovS; 
00040  
00041  int nstep = 0;
00042  double df = 0.;
00043  do{ 
00044    chi = particle->chiSquared();
00045    ndf = particle->degreesOfFreedom();
00046    df =  0.;
00047    
00048 //derivative and value at expansion point  
00049    vl = cs->value(exPoint).first;
00050    dr = cs->derivative(exPoint).first;
00051    dev = cs->deviations();  
00052    
00053 //residual between expansion and current parameters
00054 //0 at the first step   
00055    AlgebraicVector delta_alpha = par - exPoint;  
00056   
00057 //parameters needed for refit
00058 // v_d = (D * V_alpha & * D.T)^(-1)   
00059    AlgebraicMatrix drt = dr.T();
00060    AlgebraicMatrix v_d = dr * cov * drt;
00061    int ifail = 0;
00062    v_d.invert(ifail);
00063    if(ifail != 0) throw VertexException("ParentParticleFitter::error inverting covariance matrix");
00064   
00065 //lagrangian multipliers  
00066 //lambda = V_d * (D * delta_alpha + d)
00067    AlgebraicVector lambda = v_d * (dr*delta_alpha + vl);
00068   
00069 //refitted parameters
00070    refPar = par - (cov * drt * lambda);  
00071   
00072 //refitted covariance: simple and SymMatrix  
00073    refCovS = cov;
00074    AlgebraicMatrix sPart = drt * v_d * dr;
00075    AlgebraicMatrix covF = cov * sPart * cov; 
00076    AlgebraicSymMatrix sCovF(7,0);
00077   
00078    for(int i = 1; i<8; i++)
00079    {
00080      for(int j = 1; j<8; j++)
00081      {if(i<=j) sCovF(i,j) = covF(i,j);}
00082    }
00083    refCovS -= sCovF; 
00084   
00085    for(int i = 1; i < 8; i++)
00086    {refCovS(i,i) += dev(i);}
00087   
00088 //chiSquared  
00089    chi +=  (lambda.T() * (dr*delta_alpha + vl))(1);
00090    ndf +=  cs->numberOfEquations();
00091    
00092 //new expansionPoint
00093    exPoint = refPar;
00094    AlgebraicVector vlp = cs->value(exPoint).first;
00095    for(int i = 1; i< vl.num_row();i++)
00096    {df += abs(vlp(i));}
00097    nstep++; 
00098   }while(df>theMaxDiff && nstep<theMaxStep);
00099   
00101 //creating an output KinematicParticle 
00102 //and putting it on its place in the tree
00103   KinematicParameters param(refPar);
00104   KinematicParametersError er(refCovS);
00105   KinematicState kState(param,er,particle->initialState().particleCharge());
00106   RefCountedKinematicParticle refParticle  = particle->refittedParticle(kState,chi,ndf,cs->clone());                                                     
00107   tree->replaceCurrentParticle(refParticle);
00108   
00109 //replacing the  vertex with its refitted version
00110   GlobalPoint nvPos(param.vector()(1), param.vector()(2), param.vector()(3)); 
00111   AlgebraicSymMatrix nvMatrix = er.matrix().sub(1,3);
00112   GlobalError nvError(nvMatrix);
00113   VertexState vState(nvPos, nvError, 1.0);
00114   KinematicVertexFactory vFactory;
00115   RefCountedKinematicVertex nVertex = vFactory.vertex(vState, inVertex,chi,ndf);
00116   tree->replaceCurrentVertex(nVertex);
00117    
00118   return tree;
00119 }
00120 */
00121 
00122 vector<RefCountedKinematicTree>  LagrangeParentParticleFitter::fit(vector<RefCountedKinematicTree> trees, 
00123                                                                            KinematicConstraint * cs)const                                         
00124 {
00125  
00126  InputSort iSort;
00127  vector<RefCountedKinematicParticle> prt = iSort.sort(trees);
00128  int nStates = prt.size(); 
00129 
00130 //making full initial parameters and covariance
00131  AlgebraicVector part(7*nStates,0);
00132  AlgebraicSymMatrix cov(7*nStates,0);
00133  
00134  AlgebraicVector chi_in(nStates,0);
00135  AlgebraicVector ndf_in(nStates,0);
00136  int l_c=0;
00137  for(vector<RefCountedKinematicParticle>::const_iterator i = prt.begin(); i != prt.end(); i++)
00138  {
00139   AlgebraicVector7 lp = (*i)->currentState().kinematicParameters().vector();
00140   for(int j = 1; j != 8; j++){part(7*l_c + j) = lp(j-1);}
00141   AlgebraicSymMatrix lc= asHepMatrix<7>((*i)->currentState().kinematicParametersError().matrix());
00142   cov.sub(7*l_c+1,lc);
00143   chi_in(l_c+1) = (*i)->chiSquared();
00144   ndf_in(l_c+1) = (*i)->degreesOfFreedom();
00145   l_c++;
00146  }
00147 //refitted parameters and covariance matrix:
00148 //simple and symmetric 
00149  AlgebraicVector refPar;
00150  AlgebraicSymMatrix refCovS; 
00151  
00152 //constraint values, derivatives and deviations:  
00153  AlgebraicVector vl;
00154  AlgebraicMatrix dr;
00155  AlgebraicVector dev;
00156  int nstep = 0;
00157  double df = 0.; 
00158  AlgebraicVector exPoint = part;
00159  
00160 //this piece of code should later be refactored:
00161 // The algorithm is the same as above, but the
00162 // number of refitted particles is >1. Smart way of
00163 // refactoring should be chosen for it.
00164  AlgebraicVector chi;
00165  AlgebraicVector ndf;
00166 // cout<<"Starting the main loop"<<endl;
00167  do{
00168   df = 0.;
00169   chi = chi_in;
00170   ndf = ndf_in;
00171 //  cout<<"Iterational linearization point: "<<exPoint<<endl;
00172   vl = cs->value(exPoint).first;
00173   dr = cs->derivative(exPoint).first;
00174   dev = cs->deviations(nStates);
00175 
00176 //  cout<<"The value : "<<vl<<endl;
00177 //  cout<<"The derivative: "<<dr<<endl;
00178 //  cout<<"deviations: "<<dev<<endl;
00179 //  cout<<"covariance "<<cov<<endl;
00180 
00181 //residual between expansion and current parameters
00182 //0 at the first step   
00183   AlgebraicVector delta_alpha = part - exPoint;  
00184 
00185 //parameters needed for refit
00186 // v_d = (D * V_alpha & * D.T)^(-1)   
00187    AlgebraicMatrix drt = dr.T();
00188    AlgebraicMatrix v_d = dr * cov * drt;
00189    int ifail = 0;
00190    v_d.invert(ifail);
00191    if(ifail != 0) throw VertexException("ParentParticleFitter::error inverting covariance matrix");
00192   
00193 //lagrangian multipliers  
00194 //lambda = V_d * (D * delta_alpha + d)
00195    AlgebraicVector lambda = v_d * (dr*delta_alpha + vl);
00196   
00197 //refitted parameters
00198    refPar = part - (cov * drt * lambda);  
00199   
00200 //refitted covariance: simple and SymMatrix  
00201    refCovS = cov;
00202    AlgebraicMatrix sPart = drt * v_d * dr;
00203    AlgebraicMatrix covF = cov * sPart * cov; 
00204 
00205 //total covariance symmatrix    
00206   AlgebraicSymMatrix sCovF(nStates*7,0);
00207   for(int i = 1; i< nStates*7 +1; ++i)
00208   {
00209    for(int j = 1; j< nStates*7 +1; j++)
00210    {if(i<=j) sCovF(i,j) = covF(i,j);}
00211   }
00212   
00213   refCovS -= sCovF; 
00214   
00215 //  cout<<"Fitter: refitted covariance "<<refCovS<<endl;
00216   for(int i = 1; i < nStates+1; i++)
00217   {for(int j = 1; j<8; j++){refCovS((i-1)+j,(i-1)+j) += dev(j);}} 
00218   
00219 //chiSquared  
00220   for(int k =1; k<nStates+1; k++)
00221   {
00222    chi(k) +=  (lambda.T() * (dr*delta_alpha + vl))(1);
00223    ndf(k) +=  cs->numberOfEquations();
00224   }  
00225 //new expansionPoint
00226   exPoint = refPar;
00227   AlgebraicVector vlp = cs->value(exPoint).first;
00228   for(int i = 1; i< vl.num_row();i++)
00229   {df += abs(vlp(i));}
00230   nstep++; 
00231  }while(df>theMaxDiff && nstep<theMaxStep);
00232 //here math and iterative part is finished, starting an output production
00233 //creating an output KinematicParticle and putting it on its place in the tree
00234  
00235 //vector of refitted particles and trees 
00236  vector<RefCountedKinematicParticle> refPart;
00237  vector<RefCountedKinematicTree> refTrees = trees;
00238  
00239  int j=1;
00240  vector<RefCountedKinematicTree>::const_iterator tr = refTrees.begin();
00241  for(vector<RefCountedKinematicParticle>::const_iterator i = prt.begin(); i!= prt.end(); i++)
00242  {
00243   AlgebraicVector7 lRefPar;
00244   for(int k = 1; k<8 ; k++)
00245   {lRefPar(k-1) = refPar((j-1)*7+k);}
00246   AlgebraicSymMatrix77 lRefCovS = asSMatrix<7>(refCovS.sub((j-1)*7 +1,(j-1)*7+7));
00247   
00248 //new refitted parameters and covariance  
00249   KinematicParameters param(lRefPar);
00250   KinematicParametersError er(lRefCovS); 
00251   KinematicState kState(param,er,(*i)->initialState().particleCharge(), (**i).magneticField());
00252   RefCountedKinematicParticle refParticle  = (*i)->refittedParticle(kState,chi(j),ndf(j),cs->clone());
00253   
00254 //replacing particle itself  
00255   (*tr)->findParticle(*i);
00256   RefCountedKinematicVertex inVertex =  (*tr)->currentDecayVertex();
00257   (*tr)->replaceCurrentParticle(refParticle);
00258   
00259 //replacing the  vertex with its refitted version
00260   GlobalPoint nvPos(param.vector()(1), param.vector()(2), param.vector()(3)); 
00261   AlgebraicSymMatrix nvMatrix = asHepMatrix<7>(er.matrix()).sub(1,3);
00262   GlobalError nvError(nvMatrix);
00263   VertexState vState(nvPos, nvError, 1.0);
00264   KinematicVertexFactory vFactory;
00265   RefCountedKinematicVertex nVertex = vFactory.vertex(vState,inVertex,chi(j),ndf(j));
00266   (*tr)->replaceCurrentVertex(nVertex);  
00267   tr++;
00268   j++;
00269  }
00270  return refTrees; 
00271 }     
00272 
00273 void LagrangeParentParticleFitter::readParameters()
00274 {
00275 //FIXME
00276 //  static SimpleConfigurable<double>
00277 //    maxEquationValueConfigurable(0.01,"LagrangeParentParticleFitter:maximumValue");
00278 //  theMaxDiff = maxEquationValueConfigurable.value();
00279 // 
00280 //  static SimpleConfigurable<int>
00281 //    maxStepConfigurable(10,"LagrangeParentParticleFitter:maximumNumberOfIterations");
00282 //  theMaxStep = maxStepConfigurable.value();
00283 
00284   theMaxDiff = 0.01;
00285   theMaxStep = 10;
00286 }

Generated on Tue Jun 9 17:46:09 2009 for CMSSW by  doxygen 1.5.4