CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
MultiGaussianStateCombiner1D.cc
Go to the documentation of this file.
2 
4 
5 #include <cfloat>
6 
8  return combine(theState.components());
9 }
10 
12  if (theComponents.empty()) {
13  throw cms::Exception("LogicError") << "MultiGaussianStateCombiner1D:: state container to collapse is empty";
14  // return SingleState(SingleState::Vector(),SingleState::Matrix(),0.);
15  // return SingleState(SingleState::Vector(),
16  // SingleState::Matrix(),0.);
17  return SingleGaussianState1D();
18  }
19 
20  const SingleGaussianState1D firstState(theComponents.front());
21  if (theComponents.size() == 1)
22  return firstState;
23 
24  // int size = firstState.mean().num_row();
25  double meanMean(0.);
26  double weightSum(0.);
27  // double weight;
28  double measCovar1(0.);
29  double measCovar2(0.);
30  for (VSC::const_iterator mixtureIter1 = theComponents.begin(); mixtureIter1 != theComponents.end(); mixtureIter1++) {
31  double weight = mixtureIter1->weight();
32  weightSum += weight;
33 
34  double mean1 = mixtureIter1->mean();
35  meanMean += weight * mean1;
36  measCovar1 += weight * mixtureIter1->variance();
37 
38  for (VSC::const_iterator mixtureIter2 = mixtureIter1 + 1; mixtureIter2 != theComponents.end(); mixtureIter2++) {
39  double posDiff = mean1 - mixtureIter2->mean();
40  // SingleState::Matrix s(1,1); //stupid trick to make CLHEP work decently
41  // measCovar2 +=weight * (*mixtureIter2).weight() *
42  // s.similarity(posDiff.T().T());
43  // SingleState::Matrix mat;
44  // for ( unsigned int i1=0; i1<N; i1++ ) {
45  // for ( unsigned int i2=0; i2<=i1; i2++ ) mat(i1,i2) = posDiff(i1)*posDiff(i2);
46  // }
47  // measCovar2 += weight * (*mixtureIter2).weight() * mat;
48  //
49  // TensorProd yields a general matrix - need to convert to a symm. matrix
50  double covGen = posDiff * posDiff;
51  // double covSym(covGen.LowerBlock());
52  measCovar2 += weight * mixtureIter2->weight() * covGen;
53  }
54  }
55 
56  double measCovar;
57  if (weightSum < DBL_MIN) {
58  std::cout << "MultiGaussianStateCombiner1D:: New state has total weight of 0." << std::endl;
59  // meanMean = SingleState::Vector(size,0);
60  meanMean = 0.;
61  measCovar = 0.;
62  weightSum = 0.;
63  } else {
64  weightSum = 1. / weightSum;
65  meanMean *= weightSum;
66  measCovar1 *= weightSum;
67  measCovar2 *= weightSum * weightSum;
68  measCovar = measCovar1 + measCovar2;
69  // measCovar = measCovar1/weightSum + measCovar2/weightSum/weightSum;
70  }
71 
72  return SingleGaussianState1D(meanMean, measCovar, weightSum);
73 }
std::vector< SingleGaussianState1D > VSC
const SingleState1dContainer & components() const
access to components
tuple cout
Definition: gather_cfg.py:144
int weight
Definition: histoStyle.py:51
SingleGaussianState1D combine(const MultiGaussianState1D &theState) const