1 #include "CLHEP/Random/RandGaussQ.h"
3 #include "TDecompChol.h"
4 #include "canvas/Utilities/Exception.h"
11 std::vector<double>
const& centralValue,
12 std::vector< std::vector<double> >
const& inputCovarianceMatrix,
14 CLHEP::RandGaussQ& GaussRandom) {
15 std::vector<std::vector<double> > setOfSmearedCentralValues;
18 unsigned int covarianceMatrix_dim = centralValue.size();
20 if (inputCovarianceMatrix.size() != covarianceMatrix_dim) {
21 throw art::Exception(art::errors::Configuration)
22 <<
"inputCovarianceMatrix rows " << inputCovarianceMatrix.size()
23 <<
" not equal to entries of centralValue[]: " << covarianceMatrix_dim;
26 for (
auto const &
row : inputCovarianceMatrix) {
27 if (
row.size() != covarianceMatrix_dim) {
28 throw art::Exception(art::errors::Configuration)
29 <<
"inputCovarianceMatrix columns " <<
row.size()
30 <<
" not equal to entries of centralValue[]: " << covarianceMatrix_dim;
35 int dim = int(inputCovarianceMatrix.size());
36 TMatrixD covarianceMatrix(dim, dim);
37 for (
size_t i=0; i<inputCovarianceMatrix.size(); i++) {
38 for (
size_t j=0; j<inputCovarianceMatrix[i].size(); j++) {
39 covarianceMatrix[i][j] = inputCovarianceMatrix[i][j];
44 TDecompChol dc = TDecompChol(covarianceMatrix);
46 throw art::Exception(art::errors::StdException)
47 <<
"Cannot decompose covariance matrix to begin smearing.";
48 return setOfSmearedCentralValues;
53 TMatrixD U = dc.GetU();
56 for (
int n=0;
n<n_multisims;
n++) {
58 int dim = centralValue.size();
60 std::vector<double> rands(dim);
61 GaussRandom.fireArray(dim, rands.data());
64 std::vector<double> smearedCentralValues;
65 for(
int col=0; col<dim; col++) {
67 double weightFromU = 0.;
69 weightFromU += U(
row,col) * rands[
row];
75 smearedCentralValues.push_back(weightFromU + centralValue[col]);
79 setOfSmearedCentralValues.push_back(smearedCentralValues);
82 return setOfSmearedCentralValues;
87 std::vector<double>
const& centralValue,
88 TMatrixD*
const& inputCovarianceMatrix,
89 std::vector< float > rand) {
90 std::vector<double> smearedCentralValues;
94 TDecompChol dc = TDecompChol(*(inputCovarianceMatrix));
95 if (!dc.Decompose()) {
96 throw art::Exception(art::errors::StdException)
97 <<
"Cannot decompose covariance matrix to begin smearing.";
98 return smearedCentralValues;
103 TMatrixD U = dc.GetU();
105 for (
size_t col=0; col<centralValue.size(); col++) {
107 double weightFromU = 0;
108 for (
unsigned int row=0;
row<col+1;
row++) {
109 weightFromU += U(
row,col) * rand[
row];
115 smearedCentralValues.push_back(weightFromU + centralValue[col]);
118 return smearedCentralValues;
123 std::vector<double>
const& centralValue,
124 TMatrixD*
const& LowerTriangleCovarianceMatrix,
126 std::vector< float > rand) {
127 std::vector<double> smearedCentralValues;
130 throw art::Exception(art::errors::StdException)
131 <<
"Must supply the decomposed lower triangular covariance matrix.";
132 return smearedCentralValues;
135 for(
unsigned int col = 0; col < centralValue.size(); ++col) {
137 double weightFromU = 0;
139 weightFromU += LowerTriangleCovarianceMatrix[0][
row][col] * rand[
row];
145 smearedCentralValues.push_back(weightFromU + centralValue[col]);
148 return smearedCentralValues;
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
std::vector< std::vector< double > > MultiGaussianSmearing(std::vector< double > const ¢ralValue, std::vector< std::vector< double > > const &inputCovarianceMatrix, int n_multisims, CLHEP::RandGaussQ &GaussRandom)