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 
8 
9 #include <cfloat>
10 
11 std::vector<double> PosteriorWeightsCalculator::weights(const TrackingRecHit& recHit) const {
12  switch (recHit.dimension()) {
13  case 1: return weights<1>(recHit);
14  case 2: return weights<2>(recHit);
15  case 3: return weights<3>(recHit);
16  case 4: return weights<4>(recHit);
17  case 5: return weights<5>(recHit);
18  }
19  throw cms::Exception("Error: rechit of size not 1,2,3,4,5");
20 }
21 
22 template<unsigned int D>
23 std::vector<double> PosteriorWeightsCalculator::weights(const TrackingRecHit& recHit) const {
24  typedef typename AlgebraicROOTObject<D,5>::Matrix MatD5;
25  typedef typename AlgebraicROOTObject<5,D>::Matrix Mat5D;
26  typedef typename AlgebraicROOTObject<D,D>::SymMatrix SMatDD;
27  typedef typename AlgebraicROOTObject<D>::Vector VecD;
28  using ROOT::Math::SMatrixNoInit;
29 
30  std::vector<double> weights;
31  if ( predictedComponents.empty() ) {
32  edm::LogError("EmptyPredictedComponents")<<"a multi state is empty. cannot compute any weight.";
33  return weights;
34  }
35  weights.reserve(predictedComponents.size());
36 
37  std::vector<double> detRs;
38  detRs.reserve(predictedComponents.size());
39  std::vector<double> chi2s;
40  chi2s.reserve(predictedComponents.size());
41 
42  VecD r, rMeas;
43  SMatDD V(SMatrixNoInit{}), R(SMatrixNoInit{});
45  //
46  // calculate chi2 and determinant / component and find
47  // minimum / maximum of chi2
48  //
49  double chi2Min(DBL_MAX);
50  for ( unsigned int i=0; i<predictedComponents.size(); i++ ) {
51 
52  KfComponentsHolder holder;
53  auto const & x = predictedComponents[i].localParameters().vector();
54  holder.template setup<D>(&r, &V, &p, &rMeas, &R,
55  x, predictedComponents[i].localError().matrix());
56  recHit.getKfComponents(holder);
57 
58  r -= rMeas;
59  R += V;
60 
61  double detR;
62  if (! R.Det2(detR) ) {
63  edm::LogError("PosteriorWeightsCalculator") << "PosteriorWeightsCalculator: determinant failed";
64  return std::vector<double>();
65  }
66  detRs.push_back(detR);
67 
68  bool ok = invertPosDefMatrix(R);
69  if ( !ok ) {
70  edm::LogError("PosteriorWeightsCalculator")
71  << "PosteriorWeightsCalculator: inversion failed";
72  return std::vector<double>();
73  }
74  double chi2 = ROOT::Math::Similarity(r,R);
75  chi2s.push_back(chi2);
76  if ( chi2<chi2Min ) chi2Min = chi2;
77  }
78 
79  if ( detRs.size()!=predictedComponents.size() ||
80  chi2s.size()!=predictedComponents.size() ) {
81  edm::LogError("PosteriorWeightsCalculator") << "Problem in vector sizes";
82  return std::vector<double>();
83  }
84 
85  //
86  // calculate weights (extracting a common factor
87  // exp(-0.5*chi2Min) to avoid numerical problems
88  // during exponentation
89  //
90  double sumWeights(0.);
91  for ( unsigned int i=0; i<predictedComponents.size(); i++ ) {
92  double priorWeight = predictedComponents[i].weight();
93 
94  double chi2 = chi2s[i] - chi2Min;
95 
96  double tempWeight(0.);
97  if ( detRs[i]>FLT_MIN ) {
98  //
99  // Calculation of (non-normalised) weight. Common factors exp(-chi2Norm/2.) and
100  // 1./sqrt(2*pi*recHit.dimension()) have been omitted
101  //
102  tempWeight = priorWeight * std::sqrt(1./detRs[i]) * std::exp(-0.5 * chi2);
103  }
104  else {
105  LogDebug("GsfTrackFitters") << "PosteriorWeightsCalculator: detR < FLT_MIN !!";
106  }
107  weights.push_back(tempWeight);
108  sumWeights += tempWeight;
109  }
110 
111  if ( sumWeights<DBL_MIN ) {
112  LogDebug("GsfTrackFitters") << "PosteriorWeightsCalculator: sumWeight < DBL_MIN";
113  edm::LogError("PosteriorWeightsCalculator") << "PosteriorWeightsCalculator: sumWeight < DBL_MIN";
114  return std::vector<double>();
115  }
116 
117  if ( weights.size()!=predictedComponents.size() ) {
118  edm::LogError("PosteriorWeightsCalculator") << "Problem in vector sizes (2)";
119  return std::vector<double>();
120  }
121  sumWeights = 1./sumWeights;
122  for (auto & w : weights) w *= sumWeights;
123  return weights;
124 }
#define LogDebug(id)
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
const double w
Definition: UKUtility.cc:23
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:18
bool invertPosDefMatrix(ROOT::Math::SMatrix< T, N, N, ROOT::Math::MatRepSym< T, N > > &m)
ROOT::Math::SVector< double, D1 > Vector