13 :normaliseFlag(normalise)
23 std::vector<float>
GenericHouseholder::iterate(
const std::vector<std::vector<float> >& eventMatrix,
const std::vector<float>& energyVector,
const int nIter)
25 std::vector<float> solution;
26 std::vector<float> theCalibVector(energyVector.size(),1.);
27 std::vector<std::vector<float> > myEventMatrix(eventMatrix);
28 int Nevents = eventMatrix.size();
29 int Nchannels = eventMatrix[0].size();
35 solution =
iterate(myEventMatrix, energyVector);
37 if (solution.empty())
return solution;
41 for (
int i=0;
i<Nchannels;
i++)
43 for (
int ievent = 0; ievent<Nevents; ievent++)
45 myEventMatrix[ievent][
i] *= solution[
i];
48 theCalibVector[
i] *= solution[
i];
53 return theCalibVector;
66 std::vector<float> solution;
68 unsigned int m=eventMatrix.size();
69 unsigned int n=eventMatrix[0].size();
71 std::cout <<
"Householder::runIter(): starting calibration optimization:" << std::endl;
72 std::cout <<
" Events:" << m <<
", channels: " << n << std::endl;
75 if (m != energyVector.size())
77 std::cout <<
"Householder::runIter(): matrix dimensions non-conformant. " << std::endl;
78 std::cout <<
" energyVector.size()=" << energyVector.size() << std::endl;
79 std::cout <<
" eventMatrix[0].size()=" << eventMatrix[0].size() << std::endl;
80 std::cout <<
" ****************** ERROR *********************" << std::endl;
87 std::vector<std::vector<float> >
A(eventMatrix);
88 std::vector<float> energies(energyVector);
90 float normalisation = 0.;
95 std::cout <<
"Householder::iterate(): Normalising event data" << std::endl;
96 std::cout <<
" WARNING: assuming 5x5 filtering has already been done" << std::endl;
102 e25p += eventMatrix[
i][
j];
104 e25p /= energyVector[
i];
105 normalisation += e25p;
108 std::cout <<
" Normalisation = " << normalisation << std::endl;
110 for (i=0;i<energies.size();++
i)
111 energies[i]*=normalisation;
117 std::vector<std::vector<float> > Acopy(A);
118 std::vector<float>
alpha(n);
119 std::vector<int> pivot(n);
121 std::cout <<
"Householder::runIter(): Failed: Singular condition in decomposition."
123 std::cout <<
"***************** PROBLEM in DECOMPOSITION *************************"<<std::endl;
128 float etasqr = DBL_EPSILON*DBL_EPSILON;
129 std::cout<<
"LOOK at DBL_EPSILON :"<<DBL_EPSILON<<std::endl;
131 std::vector<float>
r(energies);
132 std::vector<float>
e(n);
135 solution.assign(n,0.);
136 solve(m,n,A,alpha,pivot,r,solution);
142 r[i]-=Acopy[i][j]*solution[j];
145 solve(m,n,A,alpha,pivot,r,e);
152 normy0+=solution[
i]*solution[
i];
156 std::cout <<
"Householder::runIter(): applying first correction" << std::endl;
157 std::cout <<
" normy0 = " << normy0 << std::endl;
158 std::cout <<
" norme1 = " << norme1 << std::endl;
162 if (norme1>(0.0625*normy0)) {
163 std::cout <<
"Householder::runIter(): first correction is too large. Failed."
171 std::cout <<
"Householder::runIter(): improving solution...." << std::endl;
174 while (norme1>(etasqr*normy0)) {
175 std::cout <<
"Householder::runIter(): norme1 = " << norme1 << std::endl;
180 r[i]-=Acopy[i][j]*solution[j];
184 solve(m,n,A,alpha,pivot,r,e);
193 if (norme1>(0.0625*norme0))
208 float beta,sigma,alphak,qrkk;
209 std::vector<float>
y(n);
210 std::vector<float> sum(n);
212 std::cout <<
"Householder::decompose() started" << std::endl;
220 sum[j]+=qr[i][j]*qr[i][j];
231 for (j=k+1;j<
n;j++) {
232 if (sigma < sum[j]) {
241 pivot[
k]=pivot[jbar];
248 qr[
i][
k]=qr[
i][jbar];
257 sigma+=qr[
i][
k]*qr[
i][
k];
262 std::cout <<
"Householder::decompose() failed" << std::endl;
269 alpha[
k] =
sqrt(sigma);
271 alpha[
k] =
sqrt(sigma) * (-1.);
274 beta = 1/(sigma-qrkk*alphak);
275 qr[
k][
k]=qrkk-alphak;
277 for (j=k+1;j<
n;j++) {
280 y[j]+=qr[i][k]*qr[i][j];
284 for (j=k+1;j<
n;j++) {
287 qr[
i][
j]-=qr[
i][
k]*y[
j];
288 sum[
j]-=qr[
k][
j]*qr[
k][
j];
293 std::cout <<
"Householder::decompose() finished" << std::endl;
300 std::vector<float> &
r, std::vector<float> &
y)
302 std::vector<float>
z(n,0.);
307 std::cout <<
"Householder::solve() begin" << std::endl;
313 gamma+=qr[i][j]*r[i];
314 gamma/=(alpha[
j]*qr[
j][
j]);
317 r[i]+=gamma*qr[i][j];
321 z[n-1]=r[n-1]/alpha[n-1];
324 for (i=n-2;i>=0;i--) {
335 std::cout <<
"Householder::solve() finished." << std::endl;
bool decompose(const int m, const int n, std::vector< std::vector< float > > &qr, std::vector< float > &alpha, std::vector< int > &pivot)
void solve(int m, int n, const std::vector< std::vector< float > > &qr, const std::vector< float > &alpha, const std::vector< int > &pivot, std::vector< float > &r, std::vector< float > &y)
GenericHouseholder(bool normalise=false)
std::vector< float > iterate(const std::vector< std::vector< float > > &eventMatrix, const std::vector< float > &energyVector, const int nIter)
run the Householder Algorithm several times (nIter). Returns the final vector of calibration coeffici...
~GenericHouseholder()
Destructor.