File indexing completed on 2024-04-06 12:29:11
0001 #include "RecoVertex/KinematicFit/interface/LagrangeParentParticleFitter.h"
0002 #include "RecoVertex/KinematicFitPrimitives/interface/KinematicVertexFactory.h"
0003 #include "RecoVertex/KinematicFit/interface/InputSort.h"
0004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0005
0006 LagrangeParentParticleFitter::LagrangeParentParticleFitter() { defaultParameters(); }
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120 std::vector<RefCountedKinematicTree> LagrangeParentParticleFitter::fit(
0121 const std::vector<RefCountedKinematicTree>& trees, KinematicConstraint* cs) const {
0122 InputSort iSort;
0123 std::vector<RefCountedKinematicParticle> prt = iSort.sort(trees);
0124 int nStates = prt.size();
0125
0126
0127 AlgebraicVector part(7 * nStates, 0);
0128 AlgebraicSymMatrix cov(7 * nStates, 0);
0129
0130 AlgebraicVector chi_in(nStates, 0);
0131 AlgebraicVector ndf_in(nStates, 0);
0132 int l_c = 0;
0133 for (std::vector<RefCountedKinematicParticle>::const_iterator i = prt.begin(); i != prt.end(); i++) {
0134 AlgebraicVector7 lp = (*i)->currentState().kinematicParameters().vector();
0135 for (int j = 1; j != 8; j++) {
0136 part(7 * l_c + j) = lp(j - 1);
0137 }
0138 AlgebraicSymMatrix lc = asHepMatrix<7>((*i)->currentState().kinematicParametersError().matrix());
0139 cov.sub(7 * l_c + 1, lc);
0140 chi_in(l_c + 1) = (*i)->chiSquared();
0141 ndf_in(l_c + 1) = (*i)->degreesOfFreedom();
0142 l_c++;
0143 }
0144
0145
0146 AlgebraicVector refPar;
0147 AlgebraicSymMatrix refCovS;
0148
0149
0150 AlgebraicVector vl;
0151 AlgebraicMatrix dr;
0152 AlgebraicVector dev;
0153 int nstep = 0;
0154 double df = 0.;
0155 AlgebraicVector exPoint = part;
0156
0157
0158
0159
0160
0161 AlgebraicVector chi;
0162 AlgebraicVector ndf;
0163
0164 do {
0165 df = 0.;
0166 chi = chi_in;
0167 ndf = ndf_in;
0168
0169 vl = cs->value(exPoint).first;
0170 dr = cs->derivative(exPoint).first;
0171 dev = cs->deviations(nStates);
0172
0173
0174
0175
0176
0177
0178
0179
0180 AlgebraicVector delta_alpha = part - exPoint;
0181
0182
0183
0184 AlgebraicMatrix drt = dr.T();
0185 AlgebraicMatrix v_d = dr * cov * drt;
0186 int ifail = 0;
0187 v_d.invert(ifail);
0188 if (ifail != 0) {
0189 LogDebug("KinematicConstrainedVertexFitter") << "Fit failed: unable to invert covariance matrix\n";
0190 return std::vector<RefCountedKinematicTree>();
0191 }
0192
0193
0194
0195 AlgebraicVector lambda = v_d * (dr * delta_alpha + vl);
0196
0197
0198 refPar = part - (cov * drt * lambda);
0199
0200
0201 refCovS = cov;
0202 AlgebraicMatrix sPart = drt * v_d * dr;
0203 AlgebraicMatrix covF = cov * sPart * cov;
0204
0205
0206 AlgebraicSymMatrix sCovF(nStates * 7, 0);
0207 for (int i = 1; i < nStates * 7 + 1; ++i) {
0208 for (int j = 1; j < nStates * 7 + 1; j++) {
0209 if (i <= j)
0210 sCovF(i, j) = covF(i, j);
0211 }
0212 }
0213
0214 refCovS -= sCovF;
0215
0216
0217 for (int i = 1; i < nStates + 1; i++) {
0218 for (int j = 1; j < 8; j++) {
0219 refCovS((i - 1) + j, (i - 1) + j) += dev(j);
0220 }
0221 }
0222
0223
0224 for (int k = 1; k < nStates + 1; k++) {
0225 chi(k) += (lambda.T() * (dr * delta_alpha + vl))(1);
0226 ndf(k) += cs->numberOfEquations();
0227 }
0228
0229 exPoint = refPar;
0230 AlgebraicVector vlp = cs->value(exPoint).first;
0231 for (int i = 1; i < vl.num_row(); i++) {
0232 df += std::abs(vlp(i));
0233 }
0234 nstep++;
0235 } while (df > theMaxDiff && nstep < theMaxStep);
0236
0237
0238
0239
0240 std::vector<RefCountedKinematicParticle> refPart;
0241 std::vector<RefCountedKinematicTree> refTrees = trees;
0242
0243 int j = 1;
0244 std::vector<RefCountedKinematicTree>::const_iterator tr = refTrees.begin();
0245 for (std::vector<RefCountedKinematicParticle>::const_iterator i = prt.begin(); i != prt.end(); i++) {
0246 AlgebraicVector7 lRefPar;
0247 for (int k = 1; k < 8; k++) {
0248 lRefPar(k - 1) = refPar((j - 1) * 7 + k);
0249 }
0250 AlgebraicSymMatrix77 lRefCovS = asSMatrix<7>(refCovS.sub((j - 1) * 7 + 1, (j - 1) * 7 + 7));
0251
0252
0253 KinematicParameters param(lRefPar);
0254 KinematicParametersError er(lRefCovS);
0255 KinematicState kState(param, er, (*i)->initialState().particleCharge(), (**i).magneticField());
0256 RefCountedKinematicParticle refParticle = (*i)->refittedParticle(kState, chi(j), ndf(j), cs->clone());
0257
0258
0259 (*tr)->findParticle(*i);
0260 RefCountedKinematicVertex inVertex = (*tr)->currentDecayVertex();
0261 (*tr)->replaceCurrentParticle(refParticle);
0262
0263
0264 GlobalPoint nvPos(param.position());
0265 AlgebraicSymMatrix nvMatrix = asHepMatrix<7>(er.matrix()).sub(1, 3);
0266 GlobalError nvError(asSMatrix<3>(nvMatrix));
0267 VertexState vState(nvPos, nvError, 1.0);
0268 KinematicVertexFactory vFactory;
0269 RefCountedKinematicVertex nVertex = vFactory.vertex(vState, inVertex, chi(j), ndf(j));
0270 (*tr)->replaceCurrentVertex(nVertex);
0271 tr++;
0272 j++;
0273 }
0274 return refTrees;
0275 }
0276
0277 void LagrangeParentParticleFitter::setParameters(const edm::ParameterSet& pSet) {
0278 theMaxDiff = pSet.getParameter<double>("maxDistance");
0279 theMaxStep = pSet.getParameter<int>("maxNbrOfIterations");
0280 ;
0281 }
0282
0283 void LagrangeParentParticleFitter::defaultParameters() {
0284 theMaxDiff = 0.001;
0285 theMaxStep = 100;
0286 }