9#include "MdcAlignAlg/Millepede.h"
47 ,
int nloc,
double startfact,
int nstd
48 ,
double res_cut,
double res_cut_init)
52 cout <<
" * o o o " << endl;
53 cout <<
" o o o " << endl;
54 cout <<
" o ooooo o o o oo ooo oo ooo oo " << endl;
55 cout <<
" o o o o o o o o o o o o o o o o " << endl;
56 cout <<
" o o o o o o oooo o o oooo o o oooo " << endl;
57 cout <<
" o o o o o o o ooo o o o o " << endl;
58 cout <<
" o o o o o o oo o oo ooo oo ++ starts" << endl;
61 if (debug_mode) cout <<
"" << endl;
62 if (debug_mode) cout <<
"----------------------------------------------------" << endl;
63 if (debug_mode) cout <<
"" << endl;
64 if (debug_mode) cout <<
" Entering InitMille" << endl;
65 if (debug_mode) cout <<
"" << endl;
66 if (debug_mode) cout <<
"-----------------------------------------------------" << endl;
67 if (debug_mode) cout <<
"" << endl;
76 m_residual_cut = res_cut;
77 m_residual_cut_init = res_cut_init;
84 if (debug_mode) cout <<
"Number of global parameters : " << nagb << endl;
85 if (debug_mode) cout <<
"Number of local parameters : " << nalc << endl;
86 if (debug_mode) cout <<
"Number of standard deviations : " << nstdev << endl;
88 if (nagb>mglobl || nalc>mlocal)
90 if (debug_mode) cout <<
"Two many parameters !!!!!" << endl;
96 for (
int i=0; i<nagb; i++)
106 for (
int j=0; j<nagb;j++)
114 for (
int i=0; i<nalc; i++)
118 for (
int j=0; j<nalc;j++)
131 for (
int i=0; i<3; i++)
133 if (verbose_mode) cout <<
"GetDOF(" << i <<
")= " << DOF[i] << endl;
137 for (
int j=i*nglo; j<(i+1)*nglo; j++)
146 if (m_iteration) Millepede::InitUn(startfact);
157 if (debug_mode) cout <<
"" << endl;
158 if (debug_mode) cout <<
"----------------------------------------------------" << endl;
159 if (debug_mode) cout <<
"" << endl;
160 if (debug_mode) cout <<
" InitMille has been successfully called!" << endl;
161 if (debug_mode) cout <<
"" << endl;
162 if (debug_mode) cout <<
"-----------------------------------------------------" << endl;
163 if (debug_mode) cout <<
"" << endl;
184 if (index<0 || index>=nagb)
187 {pparm[index] = param;}
214 {psigm[index] = sigma;}
241bool Millepede::InitUn(
double cutfac)
243 cfactr = std::max(1.0, cutfac);
245 cout <<
"Initial cut factor is " << cfactr << endl;
267 cout <<
"Too many constraints !!!" << endl;
271 for (
int i=0; i<nagb; i++) {adercs[ncs][i] = dercs[i];}
275 cout <<
"Number of constraints increased to " << ncs << endl;
293bool Millepede::EquLoc(
double dergb[],
double derlc[],
double dernl[],
double rmeas,
double sigma)
298 for (
int i=0; i<nalc; i++)
302 for (
int i=0; i<nagb; i++)
311 double wght = 1.0/(sigma*sigma);
318 for (
int i=0; i<nalc; i++)
323 if (ialc == -1) ialc=i;
328 if (verbose_mode) cout << ialc <<
" / " << iblc << endl;
330 for (
int i=0; i<nagb; i++)
335 if (iagb == -1) iagb=i;
340 if (verbose_mode) cout << iagb <<
" / " << ibgb << endl;
343 arest.push_back(rmeas);
346 for (
int i=ialc; i<=iblc; i++)
351 arest.push_back(derlc[i]);
352 arenl.push_back(0.0);
358 arest.push_back(wght);
359 arenl.push_back(0.0);
361 for (
int i=iagb; i<=ibgb; i++)
366 arest.push_back(dergb[i]);
367 arenl.push_back(dernl[i]);
372 if (verbose_mode) cout <<
"Out Equloc -- NST = " << arest.size() << endl;
390 for(
int i=0; i<nalc; i++) {derlc[i] = 0.0;}
391 for(
int i=0; i<nagb; i++) {dergb[i] = 0.0;}
392 for(
int i=0; i<nagb; i++) {dernl[i] = 0.0;}
422 int i, j, k, ik, ij, ist, nderlc, ndergl, ndf;
427 double rmeas, wght, rms, cutval;
437 if (itert < 2 && single_fit != 1)
439 if (debug_mode) cout <<
"Store equation no: " <<
n << endl;
441 for (i=0; i<nst; i++)
443 storeind.push_back(indst[i]);
444 storeare.push_back(arest[i]);
445 storenl.push_back(arenl[i]);
447 if (arenl[i] != 0.) arest[i] = 0.0;
452 storeplace.push_back(storeind.size());
454 if (verbose_mode) cout <<
"StorePlace size = " << storeplace[
n] << endl;
455 if (verbose_mode) cout <<
"StoreInd size = " << storeind.size() << endl;
459 for (i=0; i<nalc; i++)
463 for (j=0; j<nalc; j++) {clmat[i][j] = 0.0;}
466 for (i=0; i<nagb; i++) {indnz[i] = -1;}
502 if (indst[ist] == -1)
504 if (ja == -1) {ja = ist;}
505 else if (jb == 0) {jb = ist;}
510 if (verbose_mode) cout <<
"rmeas = " << rmeas << endl ;
511 if (verbose_mode) cout <<
"wght = " << wght << endl ;
513 for (i=(jb+1); i<ist; i++)
517 if (verbose_mode) cout <<
"dparm[" << j <<
"] = " << dparm[j] << endl;
518 if (verbose_mode) cout <<
"Starting misalignment = " << pparm[j] << endl;
519 rmeas -= arest[i]*(pparm[j]+dparm[j]);
522 if (verbose_mode) cout <<
"rmeas after global stuff removal = " << rmeas << endl ;
524 for (i=(ja+1); i<jb; i++)
527 blvec[j] += wght*rmeas*arest[i];
529 if (verbose_mode) cout <<
"blvec[" << j <<
"] = " << blvec[j] << endl ;
531 for (k=(ja+1); k<=i ; k++)
534 clmat[j][ik] += wght*arest[i]*arest[k];
536 if (verbose_mode) cout <<
"clmat[" << j <<
"][" << ik <<
"] = " << clmat[j][ik] << endl;
553 nrank = Millepede::SpmInv(clmat, blvec, nalc, scdiag, scflag);
555 if (debug_mode) cout <<
"" << endl;
556 if (debug_mode) cout <<
" __________________________________________________" << endl;
557 if (debug_mode) cout <<
" Printout of local fit (FITLOC) with rank= "<< nrank << endl;
558 if (debug_mode) cout <<
" Result of local fit : (index/parameter/error)" << endl;
560 for (i=0; i<nalc; i++)
562 if (debug_mode) cout << std::setprecision(4) << std::fixed;
563 if (debug_mode) cout << std::setw(20) << i <<
" / " << std::setw(10)
564 << blvec[i] <<
" / " << sqrt(clmat[i][i]) << endl;
570 for (i=0; i<nalc; i++)
572 track_params[2*i] = blvec[i];
573 track_params[2*i+1] = sqrt(fabs(clmat[i][i]));
587 if (indst[ist] == -1)
589 if (ja == -1) {ja = ist;}
590 else if (jb == 0) {jb = ist;}
601 if (verbose_mode) cout <<
"" << endl;
602 if (verbose_mode) cout << std::setprecision(4) << std::fixed;
603 if (verbose_mode) cout <<
". equation: measured value " << std::setw(13)
604 << rmeas <<
" +/- " << std::setw(13) << 1.0/sqrt(wght) << endl;
605 if (verbose_mode) cout <<
"Number of derivatives (global, local): "
606 << ndergl <<
", " << nderlc << endl;
607 if (verbose_mode) cout <<
"Global derivatives are: (index/derivative/parvalue) " << endl;
609 for (i=(jb+1); i<ist; i++)
610 {
if (verbose_mode) cout << indst[i] <<
" / " << arest[i]
611 <<
" / " << pparm[indst[i]] << endl;}
613 if (verbose_mode) cout <<
"Local derivatives are: (index/derivative) " << endl;
615 for (i=(ja+1); i<jb; i++) {
if (verbose_mode) cout << indst[i] <<
" / " << arest[i] << endl;}
619 for (i=(ja+1); i<jb; i++)
622 rmeas -= arest[i]*blvec[j];
625 for (i=(jb+1); i<ist; i++)
628 rmeas -= arest[i]*(pparm[j]+dparm[j]);
633 if (verbose_reject) cout <<
"Residual value : "<< rmeas << endl;
636 if (fabs(rmeas) >= m_residual_cut_init && itert <= 1)
639 if (verbose_reject) cout <<
"Rejected track !!!!!" << endl;
646 if (fabs(rmeas) >= m_residual_cut && itert > 1)
649 if (verbose_reject) cout <<
"Rejected track !!!!!" << endl;
656 summ += wght*rmeas*rmeas ;
669 if (debug_mode) cout <<
"Final chi square / degrees of freedom "<< summ <<
" / " << ndf << endl;
671 if (ndf > 0) rms = summ/float(ndf);
673 if (single_fit == 0) loctot++;
675 if (nstdev != 0 && ndf > 0 && single_fit != 1)
677 cutval = Millepede::chindl(nstdev, ndf)*cfactr;
679 if (debug_mode) cout <<
"Reject if Chisq/Ndf = " << rms <<
" > " << cutval << endl;
683 if (verbose_mode) cout <<
"Rejected track !!!!!" << endl;
684 if (single_fit == 0) locrej++;
709 if (indst[ist] == -1)
711 if (ja == -1) {ja = ist;}
712 else if (jb == 0) {jb = ist;}
718 for (i=(jb+1); i<ist; i++)
721 rmeas -= arest[i]*(pparm[j]+dparm[j]);
726 for (i=(jb+1); i<ist; i++)
730 bgvec[j] += wght*rmeas*arest[i];
731 if (verbose_mode) cout <<
"bgvec[" << j <<
"] = " << bgvec[j] << endl ;
733 for (k=(jb+1); k<ist ; k++)
736 cgmat[j][ik] += wght*arest[i]*arest[k];
737 if (verbose_mode) cout <<
"cgmat[" << j <<
"][" << ik <<
"] = " << cgmat[j][ik] << endl;
743 for (i=(jb+1); i<ist; i++)
750 for (k=0; k<nalc; k++) {clcmat[nagbn][k] = 0.0;}
758 for (k=(ja+1); k<jb ; k++)
761 clcmat[ik][ij] += wght*arest[i]*arest[k];
762 if (verbose_mode) cout <<
"clcmat[" << ik <<
"][" << ij <<
"] = " << clcmat[ik][ij] << endl;
775 Millepede::SpAVAt(clmat, clcmat, corrm, nalc, nagbn);
776 Millepede::SpAX(clcmat, blvec, corrv, nalc, nagbn);
778 for (i=0; i<nagbn; i++)
781 bgvec[j] -= corrv[i];
783 for (k=0; k<nagbn; k++)
786 cgmat[j][ik] -= corrm[i][k];
825 double trackpars[2*mlocal];
827 int ntotal_start, ntotal;
829 cout <<
"..... Making global fit ....." << endl;
833 std::vector<double> track_slopes;
835 track_slopes.resize(2*ntotal_start);
837 for (i=0; i<2*ntotal_start; i++) track_slopes[i] = 0.;
839 if (itert <= 1) itelim=10;
841 while (itert < itelim)
843 if (debug_mode) cout <<
"ITERATION --> " << itert << endl;
846 cout <<
"...using " << ntotal <<
" tracks..." << endl;
850 for (i=0; i<nagb; i++) {diag[i] = cgmat[i][i];}
856 for (i=0; i<nagb; i++)
862 for (j=0; j<nagb; j++)
868 else if (psigm[i] > 0.0)
870 cgmat[i][i] += 1.0/(psigm[i]*psigm[i]);
875 if (debug_mode) cout <<
"Number of constraint equations : " << ncs << endl;
879 for (i=0; i<ncs; i++)
882 for (j=0; j<nagb; j++)
884 cgmat[nvar][j] = float(ntotal)*adercs[i][j];
885 cgmat[j][nvar] = float(ntotal)*adercs[i][j];
886 sum -= adercs[i][j]*(pparm[j]+dparm[j]);
889 cgmat[nvar][nvar] = 0.0;
890 bgvec[nvar] = float(ntotal)*sum;
898 double final_cor = 0.0;
902 for (j=0; j<nagb; j++)
904 for (i=0; i<nagb; i++)
908 final_cor += step[j]*cgmat[j][i]*step[i];
909 if (i == j) final_cor -= step[i]*step[i]/(psigm[i]*psigm[i]);
915 cout <<
" Final coeff is " << final_cor << endl;
916 cout <<
" Final NDOFs = " << nagb << endl;
920 nrank = Millepede::SpmInv(cgmat, bgvec, nvar, scdiag, scflag);
922 for (i=0; i<nagb; i++)
924 dparm[i] += bgvec[i];
925 if (verbose_mode) cout <<
"dparm[" << i <<
"] = " << dparm[i] << endl;
926 if (verbose_mode) cout <<
"cgmat[" << i <<
"][" << i <<
"] = " << cgmat[i][i] << endl;
927 if (verbose_mode) cout <<
"err = " << sqrt(fabs(cgmat[i][i])) << endl;
931 if (itert == 1) error[i] = cgmat[i][i];
935 cout <<
"The rank defect of the symmetric " << nvar <<
" by " << nvar
936 <<
" matrix is " << nvar-nf-nrank <<
" (bad if non 0)" << endl;
938 if (itert == 0)
break;
942 cout <<
"Total : " << loctot <<
" local fits, "
943 << locrej <<
" rejected." << endl;
950 if (cfactr != cfactref && sqrt(cfactr) > 1.2*cfactref)
952 cfactr = sqrt(cfactr);
960 if (itert == itelim)
break;
962 cout <<
"Iteration " << itert <<
" with cut factor " << cfactr << endl;
966 for (i=0; i<nvar; i++)
969 for (j=0; j<nvar; j++)
983 for (i=0; i<ntotal_start; i++)
988 (i>0) ? rank_i =
abs(storeplace[i-1]) : rank_i = 0;
989 rank_f = storeplace[i];
991 if (verbose_mode) cout <<
"Track " << i <<
" : " << endl;
992 if (verbose_mode) cout <<
"Starts at " << rank_i << endl;
993 if (verbose_mode) cout <<
"Ends at " << rank_f << endl;
1003 for (j=rank_i; j<rank_f; j++)
1005 indst.push_back(storeind[j]);
1007 if (storenl[j] == 0) arest.push_back(storeare[j]);
1008 if (storenl[j] == 1) arest.push_back(track_slopes[2*i]*storeare[j]);
1009 if (storenl[j] == 2) arest.push_back(track_slopes[2*i+1]*storeare[j]);
1012 for (j=0; j<2*nalc; j++) {trackpars[j] = 0.;}
1016 track_slopes[2*i] = trackpars[2];
1017 track_slopes[2*i+1] = trackpars[6];
1023 for (j=rank_i; j<rank_f; j++)
1025 indst.push_back(storeind[j]);
1027 if (storenl[j] == 0) arest.push_back(storeare[j]);
1028 if (storenl[j] == 1) arest.push_back(track_slopes[2*i]*storeare[j]);
1029 if (storenl[j] == 2) arest.push_back(track_slopes[2*i+1]*storeare[j]);
1032 for (j=0; j<2*nalc; j++) {trackpars[j] = 0.;}
1038 : storeplace[i] = -rank_f;
1046 Millepede::PrtGlo();
1048 for (j=0; j<nagb; j++)
1052 pull[j] = par[j]/sqrt(psigm[j]*psigm[j]-cgmat[j][j]);
1053 error[j] = sqrt(fabs(cgmat[j][j]));
1056 cout << std::setw(10) <<
" " << endl;
1057 cout << std::setw(10) <<
" * o o o " << endl;
1058 cout << std::setw(10) <<
" o o o " << endl;
1059 cout << std::setw(10) <<
" o ooooo o o o oo ooo oo ooo oo " << endl;
1060 cout << std::setw(10) <<
" o o o o o o o o o o o o o o o o " << endl;
1061 cout << std::setw(10) <<
" o o o o o o oooo o o oooo o o oooo " << endl;
1062 cout << std::setw(10) <<
" o o o o o o o ooo o o o o " << endl;
1063 cout << std::setw(10) <<
" o o o o o o oo o oo ooo oo ++ ends." << endl;
1064 cout << std::setw(10) <<
" o " << endl;
1084int Millepede::SpmInv(
double v[][mgl],
double b[],
int n,
double diag[],
bool flag[])
1089 double eps = 0.00000000000001;
1094 temp =
new double[
n];
1102 for (j=0; j<=i; j++) {
if (
v[j][i] == 0) {
v[j][i] =
v[i][j];}}
1111 if (fabs(
v[i][j]) >= r[i]) r[i] = fabs(
v[i][j]);
1112 if (fabs(
v[j][i]) >= c[i]) c[i] = fabs(
v[j][i]);
1118 if (0.0 != r[i]) r[i] = 1./r[i];
1119 if (0.0 != c[i]) c[i] = 1./c[i];
1127 for (j=0; j<
n; j++) {
v[i][j] = sqrt(r[i])*
v[i][j]*sqrt(c[j]);}
1133 for (i=0; i<
n; i++) {diag[i] = fabs(
v[i][i]);}
1142 if (flag[j] && (fabs(
v[j][j])>std::max(fabs(vkk),
eps*diag[j])))
1151 if (verbose_mode) cout <<
"Pivot value :" << vkk << endl;
1159 for (jj=0; jj<
n; jj++)
1161 if (j != k && jj != k)
1163 v[j][jj] =
v[j][jj] - vkk*
v[j][k]*
v[k][jj];
1172 v[j][k] = (
v[j][k])*vkk;
1173 v[k][j] = (
v[k][j])*vkk;
1199 for (j=0; j<
n; j++) {
v[i][j] = sqrt(c[i])*
v[i][j]*sqrt(r[j]);}
1206 for (jj=0; jj<
n; jj++)
1208 v[j][jj] = -
v[j][jj];
1209 temp[j] +=
v[j][jj]*b[jj];
1213 for (j=0; j<
n; j++) {b[j] = temp[j];}
1227int Millepede::SpmInv(
double v[][mlocal],
double b[],
int n,
double diag[],
bool flag[])
1231 double eps = 0.0000000000001;
1233 temp =
new double[
n];
1238 diag[i] = fabs(
v[i][i]);
1240 for (j=0; j<=i; j++)
1255 if (flag[j] && (fabs(
v[j][j])>std::max(fabs(vkk),
eps*diag[j])))
1271 for (jj=0; jj<
n; jj++)
1273 if (j != k && jj != k)
1275 v[j][jj] =
v[j][jj] - vkk*
v[j][k]*
v[k][jj];
1284 v[j][k] = (
v[j][k])*vkk;
1285 v[k][j] = (
v[k][j])*vkk;
1312 for (jj=0; jj<
n; jj++)
1314 v[j][jj] = -
v[j][jj];
1315 temp[j] +=
v[j][jj]*b[jj];
1350bool Millepede::SpAVAt(
double v[][mlocal],
double a[][mlocal],
double w[][mglobl],
int n,
int m)
1364 w[i][j] += a[i][k]*
v[k][l]*a[j][l];
1390bool Millepede::SpAX(
double a[][mlocal],
double x[],
double y[],
int n,
int m)
1400 y[i] += a[i][j]*
x[j];
1419bool Millepede::PrtGlo()
1424 cout <<
" Result of fit for global parameters" << endl;
1425 cout <<
" ===================================" << endl;
1426 cout <<
" I initial final differ "
1427 <<
" lastcor Error gcor" << endl;
1428 cout <<
"-----------------------------------------"
1429 <<
"------------------------------------------" << endl;
1431 for (
int i=0; i<nagb; i++)
1433 err = sqrt(fabs(cgmat[i][i]));
1434 if (cgmat[i][i] < 0.0) err = -err;
1437 if (i%(nagb/6) == 0)
1439 cout <<
"-----------------------------------------"
1440 <<
"------------------------------------------" << endl;
1445 if (fabs(cgmat[i][i]*diag[i]) > 0)
1447 cout << std::setprecision(4) << std::fixed;
1448 gcor = sqrt(fabs(1.0-1.0/(cgmat[i][i]*diag[i])));
1449 cout << std::setw(4) << i <<
" / " << std::setw(10) << pparm[i]
1450 <<
" / " << std::setw(10) << pparm[i]+ dparm[i]
1451 <<
" / " << std::setw(13) << dparm[i]
1452 <<
" / " << std::setw(13) << bgvec[i] <<
" / " << std::setw(10)
1453 << std::setprecision(5) << err <<
" / " << std::setw(10) << gcor << endl;
1457 cout << std::setw(4) << i <<
" / " << std::setw(10) <<
"OFF"
1458 <<
" / " << std::setw(10) <<
"OFF"
1459 <<
" / " << std::setw(13) <<
"OFF"
1460 <<
" / " << std::setw(13) <<
"OFF"
1461 <<
" / " << std::setw(10) <<
"OFF"
1462 <<
" / " << std::setw(10) <<
"OFF" << endl;
1478double Millepede::chindl(
int n,
int nd)
1481 double sn[3] = {0.47523, 1.690140, 2.782170};
1482 double table[3][30] = {{1.0000, 1.1479, 1.1753, 1.1798, 1.1775, 1.1730, 1.1680, 1.1630,
1483 1.1581, 1.1536, 1.1493, 1.1454, 1.1417, 1.1383, 1.1351, 1.1321,
1484 1.1293, 1.1266, 1.1242, 1.1218, 1.1196, 1.1175, 1.1155, 1.1136,
1485 1.1119, 1.1101, 1.1085, 1.1070, 1.1055, 1.1040},
1486 {4.0000, 3.0900, 2.6750, 2.4290, 2.2628, 2.1415, 2.0481, 1.9736,
1487 1.9124, 1.8610, 1.8171, 1.7791, 1.7457, 1.7161, 1.6897, 1.6658,
1488 1.6442, 1.6246, 1.6065, 1.5899, 1.5745, 1.5603, 1.5470, 1.5346,
1489 1.5230, 1.5120, 1.5017, 1.4920, 1.4829, 1.4742},
1490 {9.0000, 5.9146, 4.7184, 4.0628, 3.6410, 3.3436, 3.1209, 2.9468,
1491 2.8063, 2.6902, 2.5922, 2.5082, 2.4352, 2.3711, 2.3143, 2.2635,
1492 2.2178, 2.1764, 2.1386, 2.1040, 2.0722, 2.0428, 2.0155, 1.9901,
1493 1.9665, 1.9443, 1.9235, 1.9040, 1.8855, 1.8681}};
1501 m = std::max(1,std::min(
n,3));
1505 return table[m-1][nd-1];
1509 return ((sn[m-1]+sqrt(
float(2*nd-3)))*(sn[m-1]+sqrt(
float(2*nd-3))))/float(2*nd-2);
double abs(const EvtComplex &c)
EvtTensor3C eps(const EvtVector3R &v)
**********Class see also m_nmax DOUBLE PRECISION m_amel DOUBLE PRECISION m_x2 DOUBLE PRECISION m_alfinv DOUBLE PRECISION m_Xenph INTEGER m_KeyWtm INTEGER m_idyfs DOUBLE PRECISION m_zini DOUBLE PRECISION m_q2 DOUBLE PRECISION m_Wt_KF DOUBLE PRECISION m_WtCut INTEGER m_KFfin *COMMON c_KarLud $ !Input CMS energy[GeV] $ !CMS energy after beam spread beam strahlung[GeV] $ !Beam energy spread[GeV] $ !z boost due to beam spread $ !electron beam mass *ff pair spectrum $ !minimum v
bool InitMille(bool DOF[], double Sigm[], int nglo, int nloc, double startfact, int nstd, double res_cut, double res_cut_init)
void SetTrackNumber(int value)
bool ZerLoc(double dergb[], double derlc[], double dernl[])
bool EquLoc(double dergb[], double derlc[], double dernl[], double rmeas, double sigma)
bool MakeGlobalFit(double par[], double error[], double pull[])
bool ParGlo(int index, double param)
bool FitLoc(int n, double track_params[], int single_fit)
bool ParSig(int index, double sigma)
Millepede()
Standard constructor.
bool ConstF(double dercs[], double rhs)