29 #ifndef SI_PIXEL_TEMPLATE_STANDALONE
39 #include "Math/DistFunc.h"
44 #ifndef SI_PIXEL_TEMPLATE_STANDALONE
48 #define LOGERROR(x) edm::LogError(x)
49 #define LOGDEBUG(x) LogDebug(x)
58 #define LOGERROR(x) std::cout << x << ": "
59 #define LOGDEBUG(x) std::cout << x << ": "
60 #define ENDL std::endl
101 std::vector<bool>& ydouble,
102 std::vector<bool>& xdouble,
123 int i,
j,
k, binq, midpix, fypix, nypix, lypix, logypx, lparm;
124 int fxpix, nxpix, lxpix, logxpx, shifty, shiftx;
126 int nybin, ycbin, nxbin, xcbin, minbinj, minbink;
127 int deltaj, jmin, jmax, kmin, kmax, km, fxbin, lxbin, fybin, lybin, djy, djx;
128 float sythr, sxthr,
delta, sigma, sigavg, pseudopix,
xsize,
ysize, qscale, lanpar[2][5];
129 float ss2, ssa, sa2, rat, fq, qtotal, qpixel, qavg;
130 float x1p, x2p, y1p, y2p, deltachi2;
131 float originx, originy, bias,
maxpix, minmax;
132 double chi2x, meanx, chi2y, meany, chi2ymin, chi2xmin, chi21max;
133 double hchi2, hndof, sigmal1, sigmal2, mpv1, mpv2, arg1, arg2, q05, like, loglike1, loglike2;
134 double prvav, mpv, sigmaQ,
kappa, xvav, beta2;
140 const float sqrt2x = {3.0000}, sqrt2y = {1.7000};
141 const float sqrt12 = {3.4641};
142 const float probmin = {1.110223e-16};
143 const float prob2Qmin = {1.e-5};
144 std::pair<int, int>
pixel;
151 const double mean1pix = {0.100}, chi21min = {0.160};
157 LOGDEBUG(
"SiPixelTemplateReco") <<
"input cluster direction cot(alpha) = " << cotalpha
158 <<
", cot(beta) = " << cotbeta
159 <<
" is not within the acceptance of template ID = " <<
id
160 <<
", no reconstruction performed" <<
ENDL;
171 pseudopix = templ.
s50();
173 q05 = 0.05f * pseudopix;
185 if (cluster.num_dimensions() != 2) {
186 LOGERROR(
"SiPixelTemplateReco") <<
"input cluster container (BOOST Multiarray) has wrong number of dimensions"
190 nclusx = (
int)cluster.shape()[0];
191 nclusy = (
int)cluster.shape()[1];
192 if (nclusx != (
int)xdouble.size()) {
193 LOGERROR(
"SiPixelTemplateReco") <<
"input cluster container x-size is not equal to double pixel flag container size"
197 if (nclusy != (
int)ydouble.size()) {
198 LOGERROR(
"SiPixelTemplateReco") <<
"input cluster container y-size is not equal to double pixel flag container size"
214 for (
i = 0;
i < nclusy; ++
i) {
215 for (
j = 0;
j < nclusx; ++
j) {
216 if (cluster[
j][
i] > 0) {
217 cluster[
j][
i] *= qscale;
225 minmax = 2.0f * templ.
pixmax();
226 for (
i = 0;
i < nclusy; ++
i) {
231 for (
j = 0;
j < nclusx; ++
j) {
232 qtotal += cluster[
j][
i];
244 for (
i = 0;
i < nclusy; ++
i) {
247 for (
j = 0;
j < nclusx; ++
j) {
248 ysum[
i] += cluster[
j][
i];
265 std::vector<std::pair<int, int> >::const_iterator zeroIter = zeropix.begin(), zeroEnd = zeropix.end();
266 for (; zeroIter != zeroEnd; ++zeroIter) {
267 i = zeroIter->second;
268 if (i < 0 || i >
TYSIZE - 1) {
269 LOGERROR(
"SiPixelTemplateReco") <<
"dead pixel column y-index " <<
i <<
", no reconstruction performed" <<
ENDL;
284 nypix = lypix - fypix + 1;
288 for (zeroIter = zeropix.begin(); zeroIter != zeroEnd; ++zeroIter) {
289 i = zeroIter->second;
291 if (j < 0 || j >
TXSIZE - 1) {
292 LOGERROR(
"SiPixelTemplateReco") <<
"dead pixel column x-index " <<
j <<
", no reconstruction performed" <<
ENDL;
295 if ((
i == fypix ||
i == lypix) && nypix > 1) {
303 if (nyzero[
i] > 0 && nyzero[
i] < 3) {
304 qpixel = (
maxpix - ysum[
i]) / (
float)nyzero[
i];
311 cluster[
j][
i] = qpixel;
326 for (
i = 0;
i < nclusy; ++
i) {
327 for (
j = 0;
j < nclusx; ++
j) {
328 ysum[
k] += cluster[
j][
i];
335 ysum[
k + 1] = ysum[
k];
357 for (
j = 0;
j < nclusx; ++
j) {
358 for (
i = 0;
i < nclusy; ++
i) {
359 xsum[
k] += cluster[
j][
i];
366 xsum[
k + 1] = xsum[
k];
392 ysort[logypx] = ysum[
i];
402 if ((lypix - fypix + 1) != nypix || nypix == 0) {
403 LOGDEBUG(
"SiPixelTemplateReco") <<
"y-length of pixel cluster doesn't agree with number of pixels above threshold"
406 LOGDEBUG(
"SiPixelTemplateReco") <<
"ysum[] = ";
408 LOGDEBUG(
"SiPixelTemplateReco") << ysum[
i] <<
", ";
419 LOGDEBUG(
"SiPixelTemplateReco") <<
"y-length of pixel cluster is larger than maximum template size" <<
ENDL;
421 LOGDEBUG(
"SiPixelTemplateReco") <<
"ysum[] = ";
423 LOGDEBUG(
"SiPixelTemplateReco") << ysum[
i] <<
", ";
433 midpix = (fypix + lypix) / 2;
435 shifty = templ.
cytemp() - midpix;
437 for (
i = lypix;
i >= fypix; --
i) {
438 ysum[
i + shifty] = ysum[
i];
440 yd[
i + shifty] = yd[
i];
443 }
else if (shifty < 0) {
444 for (
i = fypix;
i <= lypix; ++
i) {
445 ysum[
i + shifty] = ysum[
i];
447 yd[
i + shifty] = yd[
i];
456 if (fypix > 1 && fypix <
BYM2) {
457 ysum[fypix - 1] = pseudopix;
458 ysum[fypix - 2] = 0.2f * pseudopix;
462 if (lypix > 1 && lypix <
BYM2) {
463 ysum[lypix + 1] = pseudopix;
464 ysum[lypix + 2] = 0.2f * pseudopix;
489 xsort[logxpx] = xsum[
i];
499 if ((lxpix - fxpix + 1) != nxpix) {
500 LOGDEBUG(
"SiPixelTemplateReco") <<
"x-length of pixel cluster doesn't agree with number of pixels above threshold"
503 LOGDEBUG(
"SiPixelTemplateReco") <<
"xsum[] = ";
505 LOGDEBUG(
"SiPixelTemplateReco") << xsum[
i] <<
", ";
516 LOGDEBUG(
"SiPixelTemplateReco") <<
"x-length of pixel cluster is larger than maximum template size" <<
ENDL;
518 LOGDEBUG(
"SiPixelTemplateReco") <<
"xsum[] = ";
520 LOGDEBUG(
"SiPixelTemplateReco") << xsum[
i] <<
", ";
530 midpix = (fxpix + lxpix) / 2;
532 shiftx = templ.
cxtemp() - midpix;
534 for (
i = lxpix;
i >= fxpix; --
i) {
535 xsum[
i + shiftx] = xsum[
i];
537 xd[
i + shiftx] = xd[
i];
540 }
else if (shiftx < 0) {
541 for (
i = fxpix;
i <= lxpix; ++
i) {
542 xsum[
i + shiftx] = xsum[
i];
544 xd[
i + shiftx] = xd[
i];
553 if (fxpix > 1 && fxpix <
BXM2) {
554 xsum[fxpix - 1] = pseudopix;
555 xsum[fxpix - 2] = 0.2f * pseudopix;
559 if (lxpix > 1 && lxpix <
BXM2) {
560 xsum[lxpix + 1] = pseudopix;
561 xsum[lxpix + 2] = 0.2f * pseudopix;
596 #ifndef SI_PIXEL_TEMPLATE_STANDALONE
597 if ((sigmaQ <= 0.) || (mpv <= 0.) || (
kappa < 0.01) || (
kappa > 9.9)) {
598 throw cms::Exception(
"DataCorrupt") <<
"SiPixelTemplateSplit::Vavilov parameters mpv/sigmaQ/kappa = " << mpv
599 <<
"/" << sigmaQ <<
"/" <<
kappa << std::endl;
604 xvav = ((double)qtotal - mpv) / sigmaQ;
608 prvav = vvidist.
fcn(xvav);
610 if (prob2Q < prob2Qmin) {
620 if (!deadpix && qtotal < 1.9
f * templ.
qmin()) {
623 if (!deadpix && qtotal < 1.9
f * templ.
qmin(1)) {
629 LOGDEBUG(
"SiPixelTemplateSplit") <<
"ID = " <<
id <<
" cot(alpha) = " << cotalpha <<
" cot(beta) = " << cotbeta
630 <<
" nclusx = " << nclusx <<
" nclusy = " << nclusy <<
ENDL;
647 #ifndef SI_PIXEL_TEMPLATE_STANDALONE
648 if (speed < -1 || speed > 2) {
649 throw cms::Exception(
"DataCorrupt") <<
"SiPixelTemplateReco::PixelTempReco2D called with illegal speed = " <<
speed
675 LOGDEBUG(
"SiPixelTemplateReco") <<
"fypix " << fypix <<
" lypix = " << lypix <<
" fybin = " << fybin
676 <<
" lybin = " << lybin <<
" djy = " << djy <<
" logypx = " << logypx <<
ENDL;
677 LOGDEBUG(
"SiPixelTemplateReco") <<
"fxpix " << fxpix <<
" lxpix = " << lxpix <<
" fxbin = " << fxbin
678 <<
" lxbin = " << lxbin <<
" djx = " << djx <<
" logxpx = " << logxpx <<
ENDL;
685 sythr = 2.1f * (templ.
symax());
689 std::sort(&ysort[0], &ysort[logypx]);
691 sythr = 1.01f * ysort[0];
693 if (ysort[1] > sythr) {
694 sythr = 1.01f * ysort[1];
701 templ.
ysigma2(fypix, lypix, sythr, ysum, ysig2);
707 for (
i = fypix - 2;
i <= lypix + 2; ++
i) {
708 yw2[
i] = 1.f / ysig2[
i];
709 ysw[
i] = ysum[
i] * yw2[
i];
710 ss2 += ysum[
i] * ysw[
i];
719 std::vector<float> ytemp(
BYSIZE);
721 for (
j = jmin;
j < jmax;
j += deltaj) {
723 for (
k = kmin;
k <= km;
k += deltaj) {
730 if (nypix > logypx) {
733 if (yd[
i] && !yd[
i + 1]) {
736 sigavg = (ytemp[
i] + ytemp[
i + 1]) / 2.
f;
738 ytemp[
i + 1] = sigavg;
747 for (
i = fypix - 2;
i <= lypix + 2; ++
i) {
748 ssa += ysw[
i] * ytemp[
i];
749 sa2 += ytemp[
i] * ytemp[
i] * yw2[
i];
753 LOGERROR(
"SiPixelTemplateSplit") <<
"illegal chi2ymin normalization = " << rat <<
ENDL;
756 chi2y = ss2 - 2.f * ssa / rat + sa2 / (rat * rat);
757 if (chi2y < chi2ymin) {
765 if (minbinj > fybin) {
766 jmin = minbinj - deltaj;
770 if (minbinj < lybin) {
771 jmax = minbinj + deltaj;
775 if (minbink > fybin) {
776 kmin = minbink - deltaj;
780 if (minbink < lybin) {
781 kmax = minbink + deltaj;
788 LOGDEBUG(
"SiPixelTemplateReco") <<
"minbins " << minbinj <<
"," << minbink <<
" chi2ymin = " << chi2ymin <<
ENDL;
796 sigma = templ.
syone();
799 sigma = templ.
sytwo();
802 yrec1 = 0.5f * (fypix + lypix - 2 * shifty + 2.f * originy) *
ysize -
delta;
806 sigmay =
ysize / sqrt12;
813 chi21max = fmax(chi21min, (
double)templ.
chi2yminone());
814 chi2ymin -= chi21max;
819 meany = fmax(mean1pix, (
double)templ.
chi2yavgone());
820 hchi2 = chi2ymin / 2.;
822 prob2y = 1. - TMath::Gamma(hndof, hchi2);
829 if (logypx == 2 && fabsf(cotbeta) < 0.25
f) {
833 yrec1 = (fypix - shifty + originy) *
ysize;
834 yrec2 = (lypix - shifty + originy) *
ysize;
835 sigmay =
ysize / sqrt12;
840 yrec1 = (fypix + 0.5f - shifty + originy) *
ysize;
841 yrec2 = (lypix - shifty + originy) *
ysize;
842 sigmay =
ysize / sqrt12;
844 yrec1 = (fypix - shifty + originy) *
ysize;
845 yrec2 = (lypix - 0.5f - shifty + originy) *
ysize;
846 sigmay = 1.5f *
ysize / sqrt12;
851 yrec1 = (fypix + 0.5f - shifty + originy) *
ysize;
852 yrec2 = (lypix - 0.5f - shifty + originy) *
ysize;
853 sigmay = 2.f *
ysize / sqrt12;
858 <<
"weird problem: logical y-pixels = " << logypx <<
", total ysize in normal pixels = " << nypix <<
ENDL;
865 yrec1 = (0.125f * (minbink - ycbin) +
BHY - (
float)shifty + originy) *
ysize - bias;
866 yrec2 = (0.125f * (minbinj - ycbin) +
BHY - (
float)shifty + originy) *
ysize - bias;
867 sigmay = sqrt2y * templ.
yrmsc2m(binq);
872 if (chi2ymin < 0.0) {
882 hchi2 = chi2ymin / 2.;
884 prob2y = 1. - TMath::Gamma(hndof, hchi2);
891 sxthr = 2.1f * templ.
sxmax();
894 std::sort(&xsort[0], &xsort[logxpx]);
896 sxthr = 1.01f * xsort[0];
898 if (xsort[1] > sxthr) {
899 sxthr = 1.01f * xsort[1];
906 templ.
xsigma2(fxpix, lxpix, sxthr, xsum, xsig2);
912 for (
i = fxpix - 2;
i <= lxpix + 2; ++
i) {
913 xw2[
i] = 1.f / xsig2[
i];
914 xsw[
i] = xsum[
i] * xw2[
i];
915 ss2 += xsum[
i] * xsw[
i];
924 std::vector<float> xtemp(
BXSIZE);
926 for (
j = jmin;
j < jmax;
j += deltaj) {
928 for (
k = kmin;
k <= km;
k += deltaj) {
935 if (nxpix > logxpx) {
938 if (xd[
i] && !xd[
i + 1]) {
941 sigavg = (xtemp[
i] + xtemp[
i + 1]) / 2.
f;
943 xtemp[
i + 1] = sigavg;
952 for (
i = fxpix - 2;
i <= lxpix + 2; ++
i) {
953 ssa += xsw[
i] * xtemp[
i];
954 sa2 += xtemp[
i] * xtemp[
i] * xw2[
i];
958 LOGERROR(
"SiPixelTemplateSplit") <<
"illegal chi2xmin normalization = " << rat <<
ENDL;
961 chi2x = ss2 - 2.f * ssa / rat + sa2 / (rat * rat);
962 if (chi2x < chi2xmin) {
970 if (minbinj > fxbin) {
971 jmin = minbinj - deltaj;
975 if (minbinj < lxbin) {
976 jmax = minbinj + deltaj;
980 if (minbink > fxbin) {
981 kmin = minbink - deltaj;
985 if (minbink < lxbin) {
986 kmax = minbink + deltaj;
993 LOGDEBUG(
"SiPixelTemplateSplit") <<
"minbinj/minbink " << minbinj <<
"/" << minbink <<
" chi2xmin = " << chi2xmin
1002 sigma = templ.
sxone();
1005 sigma = templ.
sxtwo();
1007 xrec1 = 0.5f * (fxpix + lxpix - 2 * shiftx + 2.f * originx) *
xsize -
delta;
1010 sigmax =
xsize / sqrt12;
1017 chi21max = fmax(chi21min, (
double)templ.
chi2xminone());
1018 chi2xmin -= chi21max;
1019 if (chi2xmin < 0.) {
1022 meanx = fmax(mean1pix, (
double)templ.
chi2xavgone());
1023 hchi2 = chi2xmin / 2.;
1025 prob2x = 1. - TMath::Gamma(hndof, hchi2);
1033 xrec1 = (0.125f * (minbink - xcbin) +
BHX - (
float)shiftx + originx) *
xsize - bias;
1034 xrec2 = (0.125f * (minbinj - xcbin) +
BHX - (
float)shiftx + originx) *
xsize - bias;
1035 sigmax = sqrt2x * templ.
xrmsc2m(binq);
1039 if (chi2xmin < 0.0) {
1046 hchi2 = chi2xmin / 2.;
1048 prob2x = 1. - TMath::Gamma(hndof, hchi2);
1053 if (prob2y < probmin) {
1056 if (prob2x < probmin) {
1069 cluster2[
j][
i] = qscale * clust[
j - 1][
i - 1];
1071 cluster2[
j][
i] = 0.f;
1079 x1p = xrec1 +
xsize;
1080 x2p = xrec2 +
xsize;
1082 x1p = xrec1 +
xsize / 2.f;
1083 x2p = xrec2 +
xsize / 2.f;
1087 y1p = yrec1 +
ysize;
1088 y2p = yrec2 +
ysize;
1090 y1p = yrec1 +
ysize / 2.f;
1091 y2p = yrec2 +
ysize / 2.f;
1100 temp2d1[
j][
i] = 0.f;
1101 temp2d2[
j][
i] = 0.f;
1107 any2dfail = templ2D.
xytemp(
id, cotalpha, cotbeta, x1p, y1p, xdouble, ydouble, temp2d1) &&
1108 templ2D.
xytemp(
id, cotalpha, cotbeta, x2p, y2p, xdouble, ydouble, temp2d1);
1112 any2dfail = any2dfail && templ2D.
xytemp(
id, cotalpha, cotbeta, x1p, y2p, xdouble, ydouble, temp2d2);
1114 any2dfail = any2dfail && templ2D.
xytemp(
id, cotalpha, cotbeta, x2p, y1p, xdouble, ydouble, temp2d2);
1123 temp2d1[
j][
i] = 0.f;
1124 temp2d2[
j][
i] = 0.f;
1151 std::list<std::pair<int, int> > pixellst;
1157 if (cluster2[
j][
i] > 0.
f || temp2d1[
j][
i] > 0.
f || temp2d2[
j][
i] > 0.
f) {
1160 pixellst.push_back(
pixel);
1174 std::list<std::pair<int, int> >::const_iterator pixIter, pixEnd;
1175 pixIter = pixellst.begin();
1176 pixEnd = pixellst.end();
1177 for (; pixIter != pixEnd; ++pixIter) {
1179 i = pixIter->second;
1180 if ((i < BHY && cotbeta > 0.) || (
i >=
BHY && cotbeta < 0.)) {
1185 mpv1 = lanpar[lparm][0] + lanpar[lparm][1] * temp2d1[
j][
i];
1186 sigmal1 = lanpar[lparm][2] * mpv1;
1187 sigmal1 =
sqrt(sigmal1 * sigmal1 + lanpar[lparm][3] * lanpar[lparm][3]);
1190 arg1 = (cluster2[
j][
i] - mpv1) / sigmal1 - 0.22278;
1191 mpv2 = lanpar[lparm][0] + lanpar[lparm][1] * temp2d2[
j][
i];
1192 sigmal2 = lanpar[lparm][2] * mpv2;
1193 sigmal2 =
sqrt(sigmal2 * sigmal2 + lanpar[lparm][3] * lanpar[lparm][3]);
1196 arg2 = (cluster2[
j][
i] - mpv2) / sigmal2 - 0.22278;
1198 like = ROOT::Math::landau_pdf(arg1);
1201 loglike1 +=
log(like);
1203 like = ROOT::Math::landau_pdf(arg2);
1206 loglike2 +=
log(like);
1211 deltachi2 = loglike1 - loglike2;
1213 if (deltachi2 < 0.) {
1223 dchisq = fabs(deltachi2);
1258 std::vector<bool>& ydouble,
1259 std::vector<bool>& xdouble,
1276 const bool deadpix =
false;
1277 std::vector<std::pair<int, int> > zeropix;
1334 std::vector<bool>& ydouble,
1335 std::vector<bool>& xdouble,
1351 const bool deadpix =
false;
1352 std::vector<std::pair<int, int> > zeropix;
1353 const int speed = 1;
1408 std::vector<bool>& ydouble,
1409 std::vector<bool>& xdouble,
1423 const bool deadpix =
false;
1424 const bool resolve =
true;
1426 std::vector<std::pair<int, int> > zeropix;
1427 const int speed = 1;
1481 std::vector<bool>& ydouble,
1482 std::vector<bool>& xdouble,
1495 const bool deadpix =
false;
1496 const bool resolve =
true;
1497 float dchisq, prob2Q;
1498 std::vector<std::pair<int, int> > zeropix;
1499 const int speed = 1;