forked from uboone/xsec_analyzer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
NormShapeCovMatrix.hh
192 lines (147 loc) · 6.25 KB
/
NormShapeCovMatrix.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#pragma once
// Standard library includes
#include <stdexcept>
// ROOT includes
#include "TMatrixD.h"
// STV analysis includes
#include "UniverseMaker.hh"
// Splits the contributions from an ordinary covariance matrix into
// normalization, shape, and mixed pieces. See
// https://microboone-docdb.fnal.gov/cgi-bin/sso/RetrieveFile?docid=5926. Code
// taken from Afro with minor adjustments and sincere gratitude! ♥
class NormShapeCovMatrix {
public:
NormShapeCovMatrix() {}
NormShapeCovMatrix( const TMatrixD& col_matrix_pred, const TMatrixD& cov_matrix );
// Covariance matrix components
TMatrixD mixed_;
TMatrixD norm_;
TMatrixD shape_;
// For convenience, also store the mixed + shape piece in one matrix
TMatrixD mixed_plus_shape_;
};
NormShapeCovMatrix::NormShapeCovMatrix( const TMatrixD& col_matrix_pred,
const TMatrixD& cov_matrix )
{
int n_bins = col_matrix_pred.GetNrows();
int should_be_one = col_matrix_pred.GetNcols();
int n_rows = cov_matrix.GetNrows();
int n_cols = cov_matrix.GetNcols();
if ( n_bins != n_rows || n_bins != n_cols || should_be_one != 1 ) {
throw std::runtime_error( "Invalid matrix dimensions encountered in"
" constructor of NormShapeCovMatrix" );
}
shape_.ResizeTo( n_bins, n_bins );
mixed_.ResizeTo( n_bins, n_bins );
norm_.ResizeTo( n_bins, n_bins );
mixed_plus_shape_.ResizeTo( n_bins, n_bins );
double N_T = 0.;
for ( int idx = 0; idx < n_bins; ++idx ) {
N_T += col_matrix_pred( idx, 0 );
}
double M_kl = 0;
for ( int i = 0; i < n_bins; ++i ) {
for ( int j = 0; j < n_bins; ++j ) {
M_kl += cov_matrix( i, j );
}
}
for ( int i = 0; i < n_bins; ++i) {
for ( int j = 0; j < n_bins; ++j ) {
double N_i = col_matrix_pred( i, 0 );
double N_j = col_matrix_pred( j, 0 );
double M_ij = cov_matrix( i, j );
double M_ik = 0.;
for ( int k = 0; k < n_bins; ++k ) M_ik += cov_matrix( i, k );
double M_kj = 0.;
for( int k = 0; k < n_bins; ++k ) M_kj += cov_matrix( k, j );
shape_( i, j ) = M_ij - N_j*M_ik/N_T - N_i*M_kj/N_T + N_i*N_j*M_kl/N_T/N_T;
mixed_( i, j ) = N_j*M_ik/N_T + N_i*M_kj/N_T - 2.*N_i*N_j*M_kl/N_T/N_T;
norm_( i, j ) = N_i*N_j*M_kl/N_T/N_T;
}
}
mixed_plus_shape_ = shape_ + mixed_;
}
NormShapeCovMatrix make_block_diagonal_norm_shape_covmat(
const TMatrixD& unfolded_signal, const TMatrixD& unfolded_covmat,
const std::vector< TrueBin >& true_bins )
{
// Build a map of block indices to sets of signal true bin indices. This will
// be used below to extract each individual block.
std::map< int, std::vector<size_t> > block_map;
int num_true_signal_bins = 0;
for ( size_t tb = 0u; tb < true_bins.size(); ++tb ) {
const auto& tbin = true_bins.at( tb );
if ( tbin.type_ == TrueBinType::kSignalTrueBin ) {
++num_true_signal_bins;
int b_idx = tbin.block_index_;
auto& block_bins = block_map[ b_idx ];
block_bins.push_back( tb );
}
}
// TODO: add sanity checks of the block definitions
// Create a NormShapeCovMatrix object that we will fill manually with the full
// results for blockwise diagonal components of the decomposed covariance
// matrix
NormShapeCovMatrix bw_ns_cm;
bw_ns_cm.norm_.ResizeTo( num_true_signal_bins, num_true_signal_bins );
bw_ns_cm.shape_.ResizeTo( num_true_signal_bins, num_true_signal_bins );
bw_ns_cm.mixed_.ResizeTo( num_true_signal_bins, num_true_signal_bins );
bw_ns_cm.mixed_plus_shape_.ResizeTo( num_true_signal_bins,
num_true_signal_bins );
bw_ns_cm.norm_.Zero();
bw_ns_cm.shape_.Zero();
bw_ns_cm.mixed_.Zero();
bw_ns_cm.mixed_plus_shape_.Zero();
// Loop over the blocks. For each block, populate the input matrices and
// unfold.
for ( const auto& block_pair : block_map ) {
int b_idx = block_pair.first;
const auto& block_bins = block_pair.second;
std::cout << "Norm/shape decomposition for block " << b_idx << '\n';
// Get the dimensions of the current block
int num_block_true_bins = block_bins.size();
if ( num_block_true_bins < 1 ) throw std::runtime_error( "Block with zero"
" true bins encountered" );
// Prepare matrices to store the block contents
TMatrixD block_signal( num_block_true_bins, 1 );
TMatrixD block_cm( num_block_true_bins, num_block_true_bins );
// Populate the matrices for the current block
for ( int block_tb1 = 0; block_tb1 < num_block_true_bins; ++block_tb1 ) {
// Convert the current true bin index at the block level to the
// one at the global level
int tb1 = block_bins.at( block_tb1 );
// Copy the prior true bin contents into the block
block_signal( block_tb1, 0 ) = unfolded_signal( tb1, 0 );
for ( int block_tb2 = 0; block_tb2 < num_block_true_bins; ++block_tb2 ) {
// Convert the current true bin index at the block level to the one
// at the global level
int tb2 = block_bins.at( block_tb2 );
// Copy the unfolded covariance matrix element into the block
block_cm( block_tb1, block_tb2 ) = unfolded_covmat( tb1, tb2 );
}
}
// Decompose the covariance matrix for the current block into normalization,
// shape, and mixed pieces
NormShapeCovMatrix block_decomp( block_signal, block_cm );
// Store the partial results for this block in the appropriate parts of the
// matrices describing the full measurement
for ( int block_tb1 = 0; block_tb1 < num_block_true_bins; ++block_tb1 ) {
// Convert the current true bin index at the block level to the one
// at the global level
int tb1 = block_bins.at( block_tb1 );
for ( int block_tb2 = 0; block_tb2 < num_block_true_bins; ++block_tb2 ) {
// Convert the current true bin index at the block level to the one
// at the global level
int tb2 = block_bins.at( block_tb2 );
// Copy the decomposed covariance matrix elements from the current block
bw_ns_cm.norm_( tb1, tb2 ) = block_decomp.norm_( block_tb1, block_tb2 );
bw_ns_cm.shape_( tb1, tb2 )
= block_decomp.shape_( block_tb1, block_tb2 );
bw_ns_cm.mixed_( tb1, tb2 )
= block_decomp.mixed_( block_tb1, block_tb2 );
}
}
} // block loop
bw_ns_cm.mixed_plus_shape_ = bw_ns_cm.shape_ + bw_ns_cm.mixed_;
return bw_ns_cm;
}