CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RandomMultiGauss.cc
Go to the documentation of this file.
1 //#include "CommonDet/DetUtilities/interface/DetExceptions.h"
3 #include "CLHEP/Random/RandGauss.h"
4 
5 #include <cfloat>
6 //
7 // constructor with means and covariance
8 //
10  theSize(aMatrix.num_row()),
11  theMeans(aVector),
12  theTriangle(theSize,theSize,0) {
13  //
14  // Check consistency
15  //
16  if ( theMeans.num_row() == theSize ) {
17  initialise(aMatrix);
18  }
19  else {
20 // throw DetLogicError("RandomMultiGauss: size of vector and matrix do not match");
22  }
23 }
24 //
25 // constructor with covariance (mean = 0)
26 //
28  theSize(aMatrix.num_row()),
29  theMeans(theSize,0),
30  theTriangle(theSize,theSize,0) {
31  //
32  initialise(aMatrix);
33 }
34 //
35 // construct triangular matrix (Cholesky decomposition)
36 //
38  //
39  // Cholesky decomposition with protection against empty rows/columns
40  //
41  for ( int i1=0; i1<theSize; i1++ ) {
42  if ( fabs(aMatrix[i1][i1])<FLT_MIN ) continue;
43 
44  for ( int i2=i1; i2<theSize; i2++ ) {
45  if ( fabs(aMatrix[i2][i2])<FLT_MIN ) continue;
46 
47  double sum = aMatrix[i2][i1];
48  for ( int i3=i1-1; i3>=0; i3-- ) {
49  if ( fabs(aMatrix[i3][i3])<FLT_MIN ) continue;
50  sum -= theTriangle[i1][i3]*theTriangle[i2][i3];
51  }
52 
53  if ( i1==i2 ) {
54  //
55  // check for positive definite input matrix, but allow for effects
56  // due to finite precision
57  //
58  if ( sum<=0 ) {
59 // if ( sum<-FLT_MIN ) throw DetLogicError("RandomMultiGauss: input matrix is not positive definite");
60  sum = FLT_MIN;
61  }
62  theTriangle[i1][i1] = sqrt(sum);
63  }
64  else {
65  theTriangle[i1][i2] = 0.;
66  theTriangle[i2][i1] = sum / theTriangle[i1][i1];
67  }
68  }
69  }
70 }
71 //
72 // generate vector of random numbers
73 //
75  AlgebraicVector vRandom(theSize,0);
76  for ( int i=0; i<theSize; i++ ) {
77  if ( theTriangle[i][i]!=0 )
78  vRandom[i] = CLHEP::RandGauss::shoot();
79  }
80  return theTriangle*vRandom+theMeans;
81 }
82 
AlgebraicMatrix theTriangle
int i
Definition: DBlmapReader.cc:9
void initialise(const AlgebraicSymMatrix &)
T sqrt(T t)
Definition: SSEVec.h:46
AlgebraicVector theMeans
CLHEP::HepVector AlgebraicVector
AlgebraicVector fire()
CLHEP::HepSymMatrix AlgebraicSymMatrix
RandomMultiGauss(const AlgebraicVector &aVector, const AlgebraicSymMatrix &aMatrix)