4 #include "canvas/Utilities/Exception.h"
8 #include "TDecompChol.h"
11 #include "CLHEP/Random/RandGaussQ.h"
14 std::vector<std::vector<double> >
WeightCalc::MultiGaussianSmearing(std::vector<double>
const& centralValue,
std::vector< std::vector<double> >
const& inputCovarianceMatrix,
int n_multisims,CLHEP::RandGaussQ& GaussRandom)
17 std::vector<std::vector<double> > setOfSmearedCentralValues;
20 unsigned int covarianceMatrix_dim = centralValue.size();
22 if (inputCovarianceMatrix.size() != covarianceMatrix_dim)
24 throw art::Exception(art::errors::Configuration)
25 <<
"inputCovarianceMatrix rows " << inputCovarianceMatrix.size()
26 <<
" not equal to entries of centralValue[]: " << covarianceMatrix_dim;
29 for (
auto const &
row : inputCovarianceMatrix )
31 if(
row.size() != covarianceMatrix_dim)
33 throw art::Exception(art::errors::Configuration)
34 <<
"inputCovarianceMatrix columns " <<
row.size()
35 <<
" not equal to entries of centralValue[]: " << covarianceMatrix_dim;
40 int dim = int(inputCovarianceMatrix.size());
41 TMatrixD covarianceMatrix(dim,dim);
43 for (
auto const &
row : inputCovarianceMatrix)
48 covarianceMatrix[i][j] =
element;
55 TDecompChol dc = TDecompChol(covarianceMatrix);
58 throw art::Exception(art::errors::StdException)
59 <<
"Cannot decompose covariance matrix to begin smearing.";
60 return setOfSmearedCentralValues;
65 TMatrixD U = dc.GetU();
68 for(
int n = 0;
n < n_multisims; ++
n)
72 int dim = centralValue.size();
74 std::vector<double> rands(dim);
75 GaussRandom.fireArray(dim, rands.data());
78 std::vector<double> smearedCentralValues;
79 for(
int col = 0; col < dim; ++col)
82 double weightFromU = 0.;
85 weightFromU += U(
row,col)*rands[
row];
90 smearedCentralValues.push_back(weightFromU + centralValue[col]);
94 setOfSmearedCentralValues.push_back(smearedCentralValues);
96 return setOfSmearedCentralValues;
106 std::vector<double> smearedCentralValues;
117 TDecompChol dc = TDecompChol(*(inputCovarianceMatrix));
120 throw art::Exception(art::errors::StdException)
121 <<
"Cannot decompose covariance matrix to begin smearing.";
122 return smearedCentralValues;
127 TMatrixD U = dc.GetU();
130 for(
unsigned int col = 0; col < centralValue.size(); ++col)
133 double weightFromU = 0.;
135 for(
unsigned int row = 0;
row < col+1; ++
row)
137 weightFromU += U(
row,col)*rand[
row];
143 smearedCentralValues.push_back(weightFromU + centralValue[col]);
145 return smearedCentralValues;
159 std::vector<double>
WeightCalc::MultiGaussianSmearing(std::vector<double>
const& centralValue, TMatrixD*
const& LowerTriangleCovarianceMatrix,
bool isDecomposed, std::vector< double > rand)
163 std::vector<double> smearedCentralValues;
167 throw art::Exception(art::errors::StdException)
168 <<
"Must supply the decomposed lower triangular covariance matrix.";
169 return smearedCentralValues;
173 for(
unsigned int col = 0; col < centralValue.size(); ++col)
176 double weightFromU = 0.;
178 for(
unsigned int row = 0;
row < col+1; ++
row)
180 weightFromU += LowerTriangleCovarianceMatrix[0][
row][col]*rand[
row];
186 smearedCentralValues.push_back(weightFromU + centralValue[col]);
188 return smearedCentralValues;
static std::vector< std::vector< double > > MultiGaussianSmearing(std::vector< double > const ¢ralValues, std::vector< std::vector< double >> const &inputCovarianceMatrix, int n_multisims, CLHEP::RandGaussQ &GaussRandom)
Applies Gaussian smearing to a set of data.
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.