test
CMS 3D CMS Logo

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