CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PosteriorWeightsCalculator.cc
Go to the documentation of this file.
2 
6 
7 #include <cfloat>
8 
9 std::vector<double> PosteriorWeightsCalculator::weights(const TrackingRecHit& recHit) const {
10  switch (recHit.dimension()) {
11  case 1: return weights<1>(recHit);
12  case 2: return weights<2>(recHit);
13  case 3: return weights<3>(recHit);
14  case 4: return weights<4>(recHit);
15  case 5: return weights<5>(recHit);
16  }
17  throw cms::Exception("Error: rechit of size not 1,2,3,4,5");
18 }
19 
20 template<unsigned int D>
21 std::vector<double> PosteriorWeightsCalculator::weights(const TrackingRecHit& recHit) const {
22  typedef typename AlgebraicROOTObject<D,5>::Matrix MatD5;
23  typedef typename AlgebraicROOTObject<5,D>::Matrix Mat5D;
24  typedef typename AlgebraicROOTObject<D,D>::SymMatrix SMatDD;
25  typedef typename AlgebraicROOTObject<D>::Vector VecD;
26 
27  std::vector<double> weights;
28  if ( predictedComponents.empty() ) {
29  edm::LogError("EmptyPredictedComponents")<<"a multi state is empty. cannot compute any weight.";
30  return weights;
31  }
32  weights.reserve(predictedComponents.size());
33 
34  std::vector<double> detRs;
35  detRs.reserve(predictedComponents.size());
36  std::vector<double> chi2s;
37  chi2s.reserve(predictedComponents.size());
38 
39  MatD5 H;
40  VecD r, rMeas;
41  SMatDD V, R;
44  //
45  // calculate chi2 and determinant / component and find
46  // minimum / maximum of chi2
47  //
48  double chi2Min(DBL_MAX);
49  for ( unsigned int i=0; i<predictedComponents.size(); i++ ) {
50 // MeasurementExtractor me(predictedComponents[i]);
51 // // Residuals of aPredictedState w.r.t. aRecHit,
52 // //!!! AlgebraicVector r(recHit.parameters(predictedComponents[i]) - me.measuredParameters(recHit));
53 // VecD r = asSVector<D>(recHit.parameters()) - me.measuredParameters<D>(recHit);
54 // // and covariance matrix of residuals
55 // //!!! AlgebraicSymMatrix V(recHit.parametersError(predictedComponents[i]));
56 // SMatDD V = asSMatrix<D>(recHit.parametersError());
57 // SMatDD R = V + me.measuredError<D>(recHit);
58 
59  KfComponentsHolder holder;
60  x = predictedComponents[i].localParameters().vector();
61  holder.template setup<D>(&r, &V, &H, &p, &rMeas, &R,
62  x, predictedComponents[i].localError().matrix());
63  recHit.getKfComponents(holder);
64 
65  r -= rMeas;
66  R += V;
67 
68  double detR;
69  if (! R.Det2(detR) ) {
70  edm::LogError("PosteriorWeightsCalculator") << "PosteriorWeightsCalculator: determinant failed";
71  return std::vector<double>();
72  }
73  detRs.push_back(detR);
74 
75  int ierr = ! R.Invert(); // if (ierr != 0) throw exception;
76  if ( ierr!=0 ) {
77  edm::LogError("PosteriorWeightsCalculator")
78  << "PosteriorWeightsCalculator: inversion failed, ierr = " << ierr;
79  return std::vector<double>();
80  }
81  double chi2 = ROOT::Math::Similarity(r,R);
82  chi2s.push_back(chi2);
83  if ( chi2<chi2Min ) chi2Min = chi2;
84  }
85  if ( detRs.size()!=predictedComponents.size() ||
86  chi2s.size()!=predictedComponents.size() ) {
87  edm::LogError("PosteriorWeightsCalculator") << "Problem in vector sizes";
88  return std::vector<double>();
89  }
90  //
91  // calculate weights (extracting a common factor
92  // exp(-0.5*chi2Min) to avoid numerical problems
93  // during exponentation
94  //
95  double sumWeights(0.);
96  for ( unsigned int i=0; i<predictedComponents.size(); i++ ) {
97  double priorWeight = predictedComponents[i].weight();
98 
99  double chi2 = chi2s[i] - chi2Min;
100 
101  double tempWeight(0.);
102  if ( detRs[i]>FLT_MIN ) {
103  //
104  // Calculation of (non-normalised) weight. Common factors exp(-chi2Norm/2.) and
105  // 1./sqrt(2*pi*recHit.dimension()) have been omitted
106  //
107  tempWeight = priorWeight * sqrt(1./detRs[i]) * exp(-0.5 * chi2);
108  }
109  // else {
110  // edm::LogInfo("PosteriorWeightsCalculator") << "PosteriorWeightsCalculator: detR < FLT_MIN !!";
111  // }
112  weights.push_back(tempWeight);
113  sumWeights += tempWeight;
114  }
115  if ( sumWeights<DBL_MIN ) {
116  edm::LogInfo("PosteriorWeightsCalculator") << "PosteriorWeightsCalculator: sumWeight < DBL_MIN";
117  edm::LogError("PosteriorWeightsCalculator") << "PosteriorWeightsCalculator: sumWeight < DBL_MIN";
118  return std::vector<double>();
119  }
120 
121  if ( weights.size()!=predictedComponents.size() ) {
122  edm::LogError("PosteriorWeightsCalculator") << "Problem in vector sizes (2)";
123  return std::vector<double>();
124  }
125  for (std::vector<double>::iterator iter = weights.begin();
126  iter != weights.end(); iter++) {
127  (*iter) /= sumWeights;
128  }
129 
130  return weights;
131 }
int i
Definition: DBlmapReader.cc:9
virtual int dimension() const =0
virtual void getKfComponents(KfComponentsHolder &holder) const
ROOT::Math::SMatrix< double, D1, D1, ROOT::Math::MatRepSym< double, D1 > > SymMatrix
std::vector< double > weights(const TrackingRecHit &tsos) const
Create random state.
ROOT::Math::SMatrix< double, D1, D2, ROOT::Math::MatRepStd< double, D1, D2 > > Matrix
T sqrt(T t)
Definition: SSEVec.h:48
ROOT::Math::SVector< double, D1 > Vector
ROOT::Math::SVector< double, 5 > AlgebraicVector5
Definition: DDAxes.h:10