15 :normaliseFlag(normalise)
25 std::vector<float>
GenericHouseholder::iterate(
const std::vector<std::vector<float> >& eventMatrix,
const std::vector<float>& energyVector,
const int nIter)
27 std::vector<float> solution;
28 std::vector<float> theCalibVector(energyVector.size(),1.);
29 std::vector<std::vector<float> > myEventMatrix(eventMatrix);
30 int Nevents = eventMatrix.size();
31 int Nchannels = eventMatrix[0].size();
34 for (
int iter=1;iter<=nIter;iter++)
37 solution =
iterate(myEventMatrix, energyVector);
39 if (solution.empty())
return solution;
43 for (
int i=0;
i<Nchannels;
i++)
45 for (
int ievent = 0; ievent<Nevents; ievent++)
47 myEventMatrix[ievent][
i] *= solution[
i];
50 theCalibVector[
i] *= solution[
i];
55 return theCalibVector;
68 std::vector<float> solution;
70 unsigned int m=eventMatrix.size();
71 unsigned int n=eventMatrix[0].size();
73 std::cout <<
"Householder::runIter(): starting calibration optimization:" << std::endl;
74 std::cout <<
" Events:" << m <<
", channels: " << n << std::endl;
77 if (m != energyVector.size())
79 std::cout <<
"Householder::runIter(): matrix dimensions non-conformant. " << std::endl;
80 std::cout <<
" energyVector.size()=" << energyVector.size() << std::endl;
81 std::cout <<
" eventMatrix[0].size()=" << eventMatrix[0].size() << std::endl;
82 std::cout <<
" ****************** ERROR *********************" << std::endl;
89 std::vector<std::vector<float> >
A(eventMatrix);
90 std::vector<float> energies(energyVector);
92 float normalisation = 0.;
97 std::cout <<
"Householder::iterate(): Normalising event data" << std::endl;
98 std::cout <<
" WARNING: assuming 5x5 filtering has already been done" << std::endl;
104 e25p += eventMatrix[
i][
j];
106 e25p /= energyVector[
i];
107 normalisation += e25p;
110 std::cout <<
" Normalisation = " << normalisation << std::endl;
112 for (i=0;i<energies.size();++
i)
113 energies[i]*=normalisation;
119 std::vector<std::vector<float> > Acopy(A);
120 std::vector<float>
alpha(n);
121 std::vector<int> pivot(n);
123 std::cout <<
"Householder::runIter(): Failed: Singular condition in decomposition."
125 std::cout <<
"***************** PROBLEM in DECOMPOSITION *************************"<<std::endl;
130 float etasqr = DBL_EPSILON*DBL_EPSILON;
131 std::cout<<
"LOOK at DBL_EPSILON :"<<DBL_EPSILON<<std::endl;
133 std::vector<float>
r(energies);
134 std::vector<float>
e(n);
137 solution.assign(n,0.);
138 solve(m,n,A,alpha,pivot,r,solution);
144 r[i]-=Acopy[i][j]*solution[j];
147 solve(m,n,A,alpha,pivot,r,e);
154 normy0+=solution[
i]*solution[
i];
158 std::cout <<
"Householder::runIter(): applying first correction" << std::endl;
159 std::cout <<
" normy0 = " << normy0 << std::endl;
160 std::cout <<
" norme1 = " << norme1 << std::endl;
164 if (norme1>(0.0625*normy0)) {
165 std::cout <<
"Householder::runIter(): first correction is too large. Failed."
173 std::cout <<
"Householder::runIter(): improving solution...." << std::endl;
176 while (norme1>(etasqr*normy0)) {
177 std::cout <<
"Householder::runIter(): norme1 = " << norme1 << std::endl;
182 r[i]-=Acopy[i][j]*solution[j];
186 solve(m,n,A,alpha,pivot,r,e);
195 if (norme1>(0.0625*norme0))
210 float beta,sigma,alphak,qrkk;
211 std::vector<float>
y(n);
212 std::vector<float> sum(n);
214 std::cout <<
"Householder::decompose() started" << std::endl;
222 sum[j]+=qr[i][j]*qr[i][j];
233 for (j=k+1;j<
n;j++) {
234 if (sigma < sum[j]) {
243 pivot[
k]=pivot[jbar];
250 qr[
i][
k]=qr[
i][jbar];
259 sigma+=qr[
i][
k]*qr[
i][
k];
264 std::cout <<
"Householder::decompose() failed" << std::endl;
271 alpha[
k] =
sqrt(sigma);
273 alpha[
k] =
sqrt(sigma) * (-1.);
276 beta = 1/(sigma-qrkk*alphak);
277 qr[
k][
k]=qrkk-alphak;
279 for (j=k+1;j<
n;j++) {
282 y[j]+=qr[i][k]*qr[i][j];
286 for (j=k+1;j<
n;j++) {
289 qr[
i][
j]-=qr[
i][
k]*y[
j];
290 sum[
j]-=qr[
k][
j]*qr[
k][
j];
295 std::cout <<
"Householder::decompose() finished" << std::endl;
302 std::vector<float> &
r, std::vector<float> &
y)
304 std::vector<float>
z(n,0.);
309 std::cout <<
"Householder::solve() begin" << std::endl;
315 gamma+=qr[i][j]*r[i];
316 gamma/=(alpha[
j]*qr[
j][
j]);
319 r[i]+=gamma*qr[i][j];
323 z[n-1]=r[n-1]/alpha[n-1];
326 for (i=n-2;i>=0;i--) {
337 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.