CMS 3D CMS Logo

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SiPixelTemplateReco.cc
Go to the documentation of this file.
1 //
2 // SiPixelTemplateReco.cc (Version 8.11)
3 //
4 // Add goodness-of-fit to algorithm, include single pixel clusters in chi2 calculation
5 // Try "decapitation" of large single pixels
6 // Add correction for (Q_F-Q_L)/(Q_F+Q_L) bias
7 // Add cot(beta) reflection to reduce y-entries and more sophisticated x-interpolation
8 // Fix small double pixel bug with decapitation (2.41 5-Mar-2007).
9 // Fix pseudopixel bug causing possible memory overwrite (2.42 12-Mar-2007)
10 // Adjust template binning to span 3 (or 4) central pixels and implement improved (faster) chi2min search
11 // Replace internal containers with static arrays
12 // Add external threshold to calls to ysigma2 and xsigma2, use sorted signal heights to guarrantee min clust size = 2
13 // Use denser search over larger bin range for clusters with big pixels.
14 // Use single calls to template object to load template arrays (had been many)
15 // Add speed switch to trade-off speed and robustness
16 // Add qmin and re-define qbin to flag low-q clusters
17 // Add qscale to match charge scales
18 // Return error if no pixels in cluster
19 // Replace 4 cout's with LogError's
20 // Add LogDebug I/O to report various common errors
21 // Incorporate "cluster repair" to handle dead pixels
22 // Take truncation size from new pixmax information
23 // Change to allow template sizes to be changed at compile time
24 // Move interpolation range error to LogDebug
25 // Add qbin = 5 and change 1-pixel probability to use new template info
26 // Add floor for probabilities (no exact zeros)
27 // Replace asserts with exceptions in CMSSW
28 // Change calling sequence to handle cot(beta)<0 for FPix cosmics
29 //
30 // V7.00 - Decouple BPix and FPix information into separate templates
31 // Pass all containers by alias to prevent excessive cpu-usage (V7.01)
32 // Slightly modify search bin range to avoid problem with single pixel clusters + large Lorentz drift (V7.02)
33 //
34 // V8.00 - Add 2D probabilities, take pixel sizes from the template
35 // V8.05 - Shift 2-D cluster to center on the buffer
36 // V8.06 - Add locBz to the 2-D template (causes failover to the simple template when the cotbeta-locBz correlation is incorrect ... ie for non-IP tracks)
37 // - include minimum value for prob2D (1.e-30)
38 // V8.07 - Tune 2-d probability: consider only pixels above threshold and use threshold value for zero signal pixels (non-zero template)
39 // V8.10 - Remove 2-d probability for ineffectiveness and replace with simple cluster charge probability
40 // V8.11 - Change probQ to upper tail probability always (rather than two-sided tail probability)
41 //
42 //
43 // Created by Morris Swartz on 10/27/06.
44 // Copyright 2006 __TheJohnsHopkinsUniversity__. All rights reserved.
45 //
46 //
47 
48 #include <math.h>
49 #include <algorithm>
50 #include <vector>
51 #include <utility>
52 #include <iostream>
53 // ROOT::Math has a c++ function that does the probability calc, but only in v5.12 and later
54 #include "TMath.h"
55 #include "Math/DistFunc.h"
56 // Use current version of gsl instead of ROOT::Math
57 //#include <gsl/gsl_cdf.h>
58 
59 #ifndef SI_PIXEL_TEMPLATE_STANDALONE
63 #define LOGERROR(x) edm::LogError(x)
64 #define LOGDEBUG(x) LogDebug(x)
65 static int theVerboseLevel = 2;
66 #define ENDL " "
68 #else
69 #include "SiPixelTemplateReco.h"
70 #include "VVIObj.h"
71 //static int theVerboseLevel = {2};
72 #define LOGERROR(x) std::cout << x << ": "
73 #define LOGDEBUG(x) std::cout << x << ": "
74 #define ENDL std::endl
75 #endif
76 
77 using namespace SiPixelTemplateReco;
78 
79 // *************************************************************************************************************************************
119 // *************************************************************************************************************************************
120 int SiPixelTemplateReco::PixelTempReco2D(int id, float cotalpha, float cotbeta, float locBz, array_2d& clust,
121  std::vector<bool>& ydouble, std::vector<bool>& xdouble,
122  SiPixelTemplate& templ,
123  float& yrec, float& sigmay, float& proby, float& xrec, float& sigmax, float& probx, int& qbin, int speed, bool deadpix, std::vector<std::pair<int, int> >& zeropix,
124  float& probQ)
125 
126 {
127  // Local variables
128  int i, j, k, minbin, binl, binh, binq, midpix, fypix, nypix, lypix, logypx;
129  int fxpix, nxpix, lxpix, logxpx, shifty, shiftx, nyzero[TYSIZE];
130  int nclusx, nclusy;
131  int deltaj, jmin, jmax, fxbin, lxbin, fybin, lybin, djy, djx;
132  int fypix2D, lypix2D, fxpix2D, lxpix2D;
133  float sythr, sxthr, rnorm, delta, sigma, sigavg, pseudopix, qscale, q50;
134  float ss2, ssa, sa2, ssba, saba, sba2, rat, fq, qtotal, qpixel;
135  float originx, originy, qfy, qly, qfx, qlx, bias, maxpix, minmax;
136  double chi2x, meanx, chi2y, meany, chi2ymin, chi2xmin, chi21max;
137  double hchi2, hndof, prvav, mpv, sigmaQ, kappa, xvav, beta2;
138  float ytemp[41][BYSIZE], xtemp[41][BXSIZE], ysum[BYSIZE], xsum[BXSIZE], ysort[BYSIZE], xsort[BXSIZE];
139  float chi2ybin[41], chi2xbin[41], ysig2[BYSIZE], xsig2[BXSIZE];
140  float yw2[BYSIZE], xw2[BXSIZE], ysw[BYSIZE], xsw[BXSIZE];
141  bool yd[BYSIZE], xd[BXSIZE], anyyd, anyxd, calc_probQ, use_VVIObj;
142  float ysize, xsize;
143  const float probmin={1.110223e-16};
144  const float probQmin={1.e-5};
145 
146 // The minimum chi2 for a valid one pixel cluster = pseudopixel contribution only
147 
148  const double mean1pix={0.100}, chi21min={0.160};
149 
150  // First, interpolate the template needed to analyze this cluster
151  // check to see of the track direction is in the physical range of the loaded template
152 
153  if(!templ.interpolate(id, cotalpha, cotbeta, locBz)) {
154  if (theVerboseLevel > 2) {LOGDEBUG("SiPixelTemplateReco") << "input cluster direction cot(alpha) = " << cotalpha << ", cot(beta) = " << cotbeta<< ", local B_z = " << locBz << ", template ID = " << id << ", no reconstruction performed" << ENDL;}
155  return 20;
156  }
157 
158  // Make a local copy of the cluster container so that we can muck with it
159 
160  array_2d cluster = clust;
161 
162  // Check to see if Q probability is selected
163 
164  calc_probQ = false;
165  use_VVIObj = false;
166  if(speed < 0) {
167  calc_probQ = true;
168  if(speed < -1) use_VVIObj = true;
169  speed = 0;
170  }
171 
172  if(speed > 3) {
173  calc_probQ = true;
174  if(speed < 5) use_VVIObj = true;
175  speed = 3;
176  }
177 
178  // Get pixel dimensions from the template (to allow multiple detectors in the future)
179 
180  xsize = templ.xsize();
181  ysize = templ.ysize();
182 
183  // Define size of pseudopixel
184 
185  q50 = templ.s50();
186  pseudopix = 0.2*q50;
187 
188  // Get charge scaling factor
189 
190  qscale = templ.qscale();
191 
192  // Check that the cluster container is (up to) a 7x21 matrix and matches the dimensions of the double pixel flags
193 
194  if(cluster.num_dimensions() != 2) {
195  LOGERROR("SiPixelTemplateReco") << "input cluster container (BOOST Multiarray) has wrong number of dimensions" << ENDL;
196  return 3;
197  }
198  nclusx = (int)cluster.shape()[0];
199  nclusy = (int)cluster.shape()[1];
200  if(nclusx != (int)xdouble.size()) {
201  LOGERROR("SiPixelTemplateReco") << "input cluster container x-size is not equal to double pixel flag container size" << ENDL;
202  return 4;
203  }
204  if(nclusy != (int)ydouble.size()) {
205  LOGERROR("SiPixelTemplateReco") << "input cluster container y-size is not equal to double pixel flag container size" << ENDL;
206  return 5;
207  }
208 
209  // enforce maximum size
210 
211  if(nclusx > TXSIZE) {nclusx = TXSIZE;}
212  if(nclusy > TYSIZE) {nclusy = TYSIZE;}
213 
214  // First, rescale all pixel charges
215 
216  for(j=0; j<nclusx; ++j)
217  for(i=0; i<nclusy; ++i)
218  if(cluster[j][i] > 0) {cluster[j][i] *= qscale;}
219 
220 
221  // Next, sum the total charge and "decapitate" big pixels
222 
223  qtotal = 0.;
224  minmax = templ.pixmax();
225  for(i=0; i<nclusy; ++i) {
226  maxpix = minmax;
227  if(ydouble[i]) {maxpix *=2.;}
228  for(j=0; j<nclusx; ++j) {
229  qtotal += cluster[j][i];
230  if(cluster[j][i] > maxpix) {cluster[j][i] = maxpix;}
231  }
232  }
233 
234  // Do the cluster repair here
235 
236  if(deadpix) {
237  fypix = BYM3; lypix = -1;
238  for(i=0; i<nclusy; ++i) {
239  ysum[i] = 0.; nyzero[i] = 0;
240  // Do preliminary cluster projection in y
241  for(j=0; j<nclusx; ++j) {
242  ysum[i] += cluster[j][i];
243  }
244  if(ysum[i] > 0.) {
245  // identify ends of cluster to determine what the missing charge should be
246  if(i < fypix) {fypix = i;}
247  if(i > lypix) {lypix = i;}
248  }
249  }
250 
251  // Now loop over dead pixel list and "fix" everything
252 
253  //First see if the cluster ends are redefined and that we have only one dead pixel per column
254 
255  std::vector<std::pair<int, int> >::const_iterator zeroIter = zeropix.begin(), zeroEnd = zeropix.end();
256  for ( ; zeroIter != zeroEnd; ++zeroIter ) {
257  i = zeroIter->second;
258  if(i<0 || i>TYSIZE-1) {LOGERROR("SiPixelTemplateReco") << "dead pixel column y-index " << i << ", no reconstruction performed" << ENDL;
259  return 11;}
260 
261  // count the number of dead pixels in each column
262  ++nyzero[i];
263  // allow them to redefine the cluster ends
264  if(i < fypix) {fypix = i;}
265  if(i > lypix) {lypix = i;}
266  }
267 
268  nypix = lypix-fypix+1;
269 
270  // Now adjust the charge in the dead pixels to sum to 0.5*truncation value in the end columns and the truncation value in the interior columns
271 
272  for (zeroIter = zeropix.begin(); zeroIter != zeroEnd; ++zeroIter ) {
273  i = zeroIter->second; j = zeroIter->first;
274  if(j<0 || j>TXSIZE-1) {LOGERROR("SiPixelTemplateReco") << "dead pixel column x-index " << j << ", no reconstruction performed" << ENDL;
275  return 12;}
276  if((i == fypix || i == lypix) && nypix > 1) {maxpix = templ.symax()/2.;} else {maxpix = templ.symax();}
277  if(ydouble[i]) {maxpix *=2.;}
278  if(nyzero[i] > 0 && nyzero[i] < 3) {qpixel = (maxpix - ysum[i])/(float)nyzero[i];} else {qpixel = 1.;}
279  if(qpixel < 1.) {qpixel = 1.;}
280  cluster[j][i] = qpixel;
281  // Adjust the total cluster charge to reflect the charge of the "repaired" cluster
282  qtotal += qpixel;
283  }
284  // End of cluster repair section
285  }
286 
287  // Next, make y-projection of the cluster and copy the double pixel flags into a 25 element container
288 
289  for(i=0; i<BYSIZE; ++i) { ysum[i] = 0.; yd[i] = false;}
290  k=0;
291  anyyd = false;
292  for(i=0; i<nclusy; ++i) {
293  for(j=0; j<nclusx; ++j) {
294  ysum[k] += cluster[j][i];
295  }
296 
297  // If this is a double pixel, put 1/2 of the charge in 2 consective single pixels
298 
299  if(ydouble[i]) {
300  ysum[k] /= 2.;
301  ysum[k+1] = ysum[k];
302  yd[k] = true;
303  yd[k+1] = false;
304  k=k+2;
305  anyyd = true;
306  } else {
307  yd[k] = false;
308  ++k;
309  }
310  if(k > BYM1) {break;}
311  }
312 
313  // Next, make x-projection of the cluster and copy the double pixel flags into an 11 element container
314 
315  for(i=0; i<BXSIZE; ++i) { xsum[i] = 0.; xd[i] = false;}
316  k=0;
317  anyxd = false;
318  for(j=0; j<nclusx; ++j) {
319  for(i=0; i<nclusy; ++i) {
320  xsum[k] += cluster[j][i];
321  }
322 
323  // If this is a double pixel, put 1/2 of the charge in 2 consective single pixels
324 
325  if(xdouble[j]) {
326  xsum[k] /= 2.;
327  xsum[k+1] = xsum[k];
328  xd[k]=true;
329  xd[k+1]=false;
330  k=k+2;
331  anyxd = true;
332  } else {
333  xd[k]=false;
334  ++k;
335  }
336  if(k > BXM1) {break;}
337  }
338 
339  // next, identify the y-cluster ends, count total pixels, nypix, and logical pixels, logypx
340 
341  fypix=-1;
342  nypix=0;
343  lypix=0;
344  logypx=0;
345  for(i=0; i<BYSIZE; ++i) {
346  if(ysum[i] > 0.) {
347  if(fypix == -1) {fypix = i;}
348  if(!yd[i]) {
349  ysort[logypx] = ysum[i];
350  ++logypx;
351  }
352  ++nypix;
353  lypix = i;
354  }
355  }
356 
357  // dlengthy = (float)nypix - templ.clsleny();
358 
359  // Make sure cluster is continuous
360 
361  if((lypix-fypix+1) != nypix || nypix == 0) {
362  LOGDEBUG("SiPixelTemplateReco") << "y-length of pixel cluster doesn't agree with number of pixels above threshold" << ENDL;
363  if (theVerboseLevel > 2) {
364  LOGDEBUG("SiPixelTemplateReco") << "ysum[] = ";
365  for(i=0; i<BYSIZE-1; ++i) {LOGDEBUG("SiPixelTemplateReco") << ysum[i] << ", ";}
366  LOGDEBUG("SiPixelTemplateReco") << ysum[BYSIZE-1] << ENDL;
367  }
368 
369  return 1;
370  }
371 
372  // If cluster is longer than max template size, technique fails
373 
374  if(nypix > TYSIZE) {
375  LOGDEBUG("SiPixelTemplateReco") << "y-length of pixel cluster is larger than maximum template size" << ENDL;
376  if (theVerboseLevel > 2) {
377  LOGDEBUG("SiPixelTemplateReco") << "ysum[] = ";
378  for(i=0; i<BYSIZE-1; ++i) {LOGDEBUG("SiPixelTemplateReco") << ysum[i] << ", ";}
379  LOGDEBUG("SiPixelTemplateReco") << ysum[BYSIZE-1] << ENDL;
380  }
381 
382  return 6;
383  }
384 
385  // Remember these numbers for later
386 
387  fypix2D = fypix;
388  lypix2D = lypix;
389 
390  // next, center the cluster on pixel 12 if necessary
391 
392  midpix = (fypix+lypix)/2;
393  shifty = BHY - midpix;
394  if(shifty > 0) {
395  for(i=lypix; i>=fypix; --i) {
396  ysum[i+shifty] = ysum[i];
397  ysum[i] = 0.;
398  yd[i+shifty] = yd[i];
399  yd[i] = false;
400  }
401  } else if (shifty < 0) {
402  for(i=fypix; i<=lypix; ++i) {
403  ysum[i+shifty] = ysum[i];
404  ysum[i] = 0.;
405  yd[i+shifty] = yd[i];
406  yd[i] = false;
407  }
408  }
409  lypix +=shifty;
410  fypix +=shifty;
411 
412  // If the cluster boundaries are OK, add pesudopixels, otherwise quit
413 
414  if(fypix > 1 && fypix < BYM2) {
415  ysum[fypix-1] = pseudopix;
416  ysum[fypix-2] = pseudopix;
417  } else {return 8;}
418  if(lypix > 1 && lypix < BYM2) {
419  ysum[lypix+1] = pseudopix;
420  ysum[lypix+2] = pseudopix;
421  } else {return 8;}
422 
423  // finally, determine if pixel[0] is a double pixel and make an origin correction if it is
424 
425  if(ydouble[0]) {
426  originy = -0.5;
427  } else {
428  originy = 0.;
429  }
430 
431  // next, identify the x-cluster ends, count total pixels, nxpix, and logical pixels, logxpx
432 
433  fxpix=-1;
434  nxpix=0;
435  lxpix=0;
436  logxpx=0;
437  for(i=0; i<BXSIZE; ++i) {
438  if(xsum[i] > 0.) {
439  if(fxpix == -1) {fxpix = i;}
440  if(!xd[i]) {
441  xsort[logxpx] = xsum[i];
442  ++logxpx;
443  }
444  ++nxpix;
445  lxpix = i;
446  }
447  }
448 
449  // dlengthx = (float)nxpix - templ.clslenx();
450 
451  // Make sure cluster is continuous
452 
453  if((lxpix-fxpix+1) != nxpix) {
454 
455  LOGDEBUG("SiPixelTemplateReco") << "x-length of pixel cluster doesn't agree with number of pixels above threshold" << ENDL;
456  if (theVerboseLevel > 2) {
457  LOGDEBUG("SiPixelTemplateReco") << "xsum[] = ";
458  for(i=0; i<BXSIZE-1; ++i) {LOGDEBUG("SiPixelTemplateReco") << xsum[i] << ", ";}
459  LOGDEBUG("SiPixelTemplateReco") << ysum[BXSIZE-1] << ENDL;
460  }
461 
462  return 2;
463  }
464 
465  // If cluster is longer than max template size, technique fails
466 
467  if(nxpix > TXSIZE) {
468 
469  LOGDEBUG("SiPixelTemplateReco") << "x-length of pixel cluster is larger than maximum template size" << ENDL;
470  if (theVerboseLevel > 2) {
471  LOGDEBUG("SiPixelTemplateReco") << "xsum[] = ";
472  for(i=0; i<BXSIZE-1; ++i) {LOGDEBUG("SiPixelTemplateReco") << xsum[i] << ", ";}
473  LOGDEBUG("SiPixelTemplateReco") << ysum[BXSIZE-1] << ENDL;
474  }
475 
476  return 7;
477  }
478 
479  // Remember these numbers for later
480 
481  fxpix2D = fxpix;
482  lxpix2D = lxpix;
483 
484  // next, center the cluster on pixel 5 if necessary
485 
486  midpix = (fxpix+lxpix)/2;
487  shiftx = BHX - midpix;
488  if(shiftx > 0) {
489  for(i=lxpix; i>=fxpix; --i) {
490  xsum[i+shiftx] = xsum[i];
491  xsum[i] = 0.;
492  xd[i+shiftx] = xd[i];
493  xd[i] = false;
494  }
495  } else if (shiftx < 0) {
496  for(i=fxpix; i<=lxpix; ++i) {
497  xsum[i+shiftx] = xsum[i];
498  xsum[i] = 0.;
499  xd[i+shiftx] = xd[i];
500  xd[i] = false;
501  }
502  }
503  lxpix +=shiftx;
504  fxpix +=shiftx;
505 
506  // If the cluster boundaries are OK, add pesudopixels, otherwise quit
507 
508  if(fxpix > 1 && fxpix < BXM2) {
509  xsum[fxpix-1] = pseudopix;
510  xsum[fxpix-2] = pseudopix;
511  } else {return 9;}
512  if(lxpix > 1 && lxpix < BXM2) {
513  xsum[lxpix+1] = pseudopix;
514  xsum[lxpix+2] = pseudopix;
515  } else {return 9;}
516 
517  // finally, determine if pixel[0] is a double pixel and make an origin correction if it is
518 
519  if(xdouble[0]) {
520  originx = -0.5;
521  } else {
522  originx = 0.;
523  }
524 
525  // uncertainty and final corrections depend upon total charge bin
526 
527  fq = qtotal/templ.qavg();
528  if(fq > 1.5) {
529  binq=0;
530  } else {
531  if(fq > 1.0) {
532  binq=1;
533  } else {
534  if(fq > 0.85) {
535  binq=2;
536  } else {
537  binq=3;
538  }
539  }
540  }
541 
542  // Return the charge bin via the parameter list unless the charge is too small (then flag it)
543 
544  qbin = binq;
545  if(!deadpix && qtotal < 0.95*templ.qmin()) {qbin = 5;} else {
546  if(!deadpix && qtotal < 0.95*templ.qmin(1)) {qbin = 4;}
547  }
548  if (theVerboseLevel > 9) {
549  LOGDEBUG("SiPixelTemplateReco") <<
550  "ID = " << id <<
551  " cot(alpha) = " << cotalpha << " cot(beta) = " << cotbeta <<
552  " nclusx = " << nclusx << " nclusy = " << nclusy << ENDL;
553  }
554 
555 
556  // Next, copy the y- and x-templates to local arrays
557 
558  // First, decide on chi^2 min search parameters
559 
560 #ifndef SI_PIXEL_TEMPLATE_STANDALONE
561  if(speed < 0 || speed > 3) {
562  throw cms::Exception("DataCorrupt") << "SiPixelTemplateReco::PixelTempReco2D called with illegal speed = " << speed << std::endl;
563  }
564 #else
565  assert(speed >= 0 && speed < 4);
566 #endif
567  fybin = 2; lybin = 38; fxbin = 2; lxbin = 38; djy = 1; djx = 1;
568  if(speed > 0) {
569  fybin = 8; lybin = 32;
570  if(yd[fypix]) {fybin = 4; lybin = 36;}
571  if(lypix > fypix) {
572  if(yd[lypix-1]) {fybin = 4; lybin = 36;}
573  }
574  fxbin = 8; lxbin = 32;
575  if(xd[fxpix]) {fxbin = 4; lxbin = 36;}
576  if(lxpix > fxpix) {
577  if(xd[lxpix-1]) {fxbin = 4; lxbin = 36;}
578  }
579  }
580 
581  if(speed > 1) {
582  djy = 2; djx = 2;
583  if(speed > 2) {
584  if(!anyyd) {djy = 4;}
585  if(!anyxd) {djx = 4;}
586  }
587  }
588 
589  if (theVerboseLevel > 9) {
590  LOGDEBUG("SiPixelTemplateReco") <<
591  "fypix " << fypix << " lypix = " << lypix <<
592  " fybin = " << fybin << " lybin = " << lybin <<
593  " djy = " << djy << " logypx = " << logypx << ENDL;
594  LOGDEBUG("SiPixelTemplateReco") <<
595  "fxpix " << fxpix << " lxpix = " << lxpix <<
596  " fxbin = " << fxbin << " lxbin = " << lxbin <<
597  " djx = " << djx << " logxpx = " << logxpx << ENDL;
598  }
599 
600  // Now do the copies
601 
602  templ.ytemp(fybin, lybin, ytemp);
603 
604  templ.xtemp(fxbin, lxbin, xtemp);
605 
606  // Do the y-reconstruction first
607 
608  // Apply the first-pass template algorithm to all clusters
609 
610  // Modify the template if double pixels are present
611 
612  if(nypix > logypx) {
613  i=fypix;
614  while(i < lypix) {
615  if(yd[i] && !yd[i+1]) {
616  for(j=fybin; j<=lybin; ++j) {
617 
618  // Sum the adjacent cells and put the average signal in both
619 
620  sigavg = (ytemp[j][i] + ytemp[j][i+1])/2.;
621  ytemp[j][i] = sigavg;
622  ytemp[j][i+1] = sigavg;
623  }
624  i += 2;
625  } else {
626  ++i;
627  }
628  }
629  }
630 
631  // Define the maximum signal to allow before de-weighting a pixel
632 
633  sythr = 1.1*(templ.symax());
634 
635  // Make sure that there will be at least two pixels that are not de-weighted
636 
637  std::sort(&ysort[0], &ysort[logypx]);
638  if(logypx == 1) {sythr = 1.01f*ysort[0];} else {
639  if (ysort[1] > sythr) { sythr = 1.01f*ysort[1]; }
640  }
641 
642  // Evaluate pixel-by-pixel uncertainties (weights) for the templ analysis
643 
644  // for(i=0; i<BYSIZE; ++i) { ysig2[i] = 0.;}
645  templ.ysigma2(fypix, lypix, sythr, ysum, ysig2);
646 
647  // Find the template bin that minimizes the Chi^2
648 
649  chi2ymin = 1.e15;
650  for(i=fybin; i<=lybin; ++i) {
651  chi2ybin[i] = -1.e15f;
652  }
653  ss2 = 0.f;
654  for(i=fypix-2; i<=lypix+2; ++i) {
655  yw2[i] = 1.f/ysig2[i];
656  ysw[i] = ysum[i]*yw2[i];
657  ss2 += ysum[i]*ysw[i];
658  }
659  minbin = -1;
660  deltaj = djy;
661  jmin = fybin;
662  jmax = lybin;
663  while(deltaj > 0) {
664  for(j=jmin; j<=jmax; j+=deltaj) {
665  if(chi2ybin[j] < -100.f) {
666  ssa = 0.f;
667  sa2 = 0.f;
668  for(i=fypix-2; i<=lypix+2; ++i) {
669  ssa += ysw[i]*ytemp[j][i];
670  sa2 += ytemp[j][i]*ytemp[j][i]*yw2[i];
671  }
672  rat=ssa/ss2;
673  if(rat <= 0.f) {LOGERROR("SiPixelTemplateReco") << "illegal chi2ymin normalization (1) = " << rat << ENDL; rat = 1.;}
674  chi2ybin[j]=ss2-2.f*ssa/rat+sa2/(rat*rat);
675  }
676  if(chi2ybin[j] < chi2ymin) {
677  chi2ymin = chi2ybin[j];
678  minbin = j;
679  }
680  }
681  deltaj /= 2;
682  if(minbin > fybin) {jmin = minbin - deltaj;} else {jmin = fybin;}
683  if(minbin < lybin) {jmax = minbin + deltaj;} else {jmax = lybin;}
684  }
685 
686  if (theVerboseLevel > 9) {
687  LOGDEBUG("SiPixelTemplateReco") <<
688  "minbin " << minbin << " chi2ymin = " << chi2ymin << ENDL;
689  }
690 
691  // Do not apply final template pass to 1-pixel clusters (use calibrated offset)
692 
693  if(logypx == 1) {
694 
695  if(nypix ==1) {
696  delta = templ.dyone();
697  sigma = templ.syone();
698  } else {
699  delta = templ.dytwo();
700  sigma = templ.sytwo();
701  }
702 
703  yrec = 0.5f*(fypix+lypix-2*shifty+2.f*originy)*ysize-delta;
704  if(sigma <= 0.) {
705  sigmay = 43.3f;
706  } else {
707  sigmay = sigma;
708  }
709 
710  // Do probability calculation for one-pixel clusters
711 
712  chi21max = fmax(chi21min, (double)templ.chi2yminone());
713  chi2ymin -=chi21max;
714  if(chi2ymin < 0.) {chi2ymin = 0.;}
715  // proby = gsl_cdf_chisq_Q(chi2ymin, mean1pix);
716  meany = fmax(mean1pix, (double)templ.chi2yavgone());
717  hchi2 = chi2ymin/2.; hndof = meany/2.;
718  proby = 1. - TMath::Gamma(hndof, hchi2);
719 
720  } else {
721 
722  // For cluster > 1 pix, make the second, interpolating pass with the templates
723 
724  binl = minbin - 1;
725  binh = binl + 2;
726  if(binl < fybin) { binl = fybin;}
727  if(binh > lybin) { binh = lybin;}
728  ssa = 0.;
729  sa2 = 0.;
730  ssba = 0.;
731  saba = 0.;
732  sba2 = 0.;
733  for(i=fypix-2; i<=lypix+2; ++i) {
734  ssa += ysw[i]*ytemp[binl][i];
735  sa2 += ytemp[binl][i]*ytemp[binl][i]*yw2[i];
736  ssba += ysw[i]*(ytemp[binh][i] - ytemp[binl][i]);
737  saba += ytemp[binl][i]*(ytemp[binh][i] - ytemp[binl][i])*yw2[i];
738  sba2 += (ytemp[binh][i] - ytemp[binl][i])*(ytemp[binh][i] - ytemp[binl][i])*yw2[i];
739  }
740 
741  // rat is the fraction of the "distance" from template a to template b
742 
743  rat=(ssba*ssa-ss2*saba)/(ss2*sba2-ssba*ssba);
744  if(rat < 0.) {rat=0.;}
745  if(rat > 1.) {rat=1.0;}
746  rnorm = (ssa+rat*ssba)/ss2;
747 
748  // Calculate the charges in the first and last pixels
749 
750  qfy = ysum[fypix];
751  if(yd[fypix]) {qfy+=ysum[fypix+1];}
752  if(logypx > 1) {
753  qly=ysum[lypix];
754  if(yd[lypix-1]) {qly+=ysum[lypix-1];}
755  } else {
756  qly = qfy;
757  }
758 
759  // Now calculate the mean bias correction and uncertainties
760 
761  float qyfrac = (qfy-qly)/(qfy+qly);
762  bias = templ.yflcorr(binq,qyfrac)+templ.yavg(binq);
763 
764  // uncertainty and final correction depend upon charge bin
765 
766  yrec = (0.125f*binl+BHY-2.5f+rat*(binh-binl)*0.125f-(float)shifty+originy)*ysize - bias;
767  sigmay = templ.yrms(binq);
768 
769  // Do goodness of fit test in y
770 
771  if(rnorm <= 0.) {LOGERROR("SiPixelTemplateReco") << "illegal chi2y normalization (2) = " << rnorm << ENDL; rnorm = 1.;}
772  chi2y=ss2-2./rnorm*ssa-2./rnorm*rat*ssba+(sa2+2.*rat*saba+rat*rat*sba2)/(rnorm*rnorm)-templ.chi2ymin(binq);
773  if(chi2y < 0.0) {chi2y = 0.0;}
774  meany = templ.chi2yavg(binq);
775  if(meany < 0.01) {meany = 0.01;}
776  // gsl function that calculates the chi^2 tail prob for non-integral dof
777  // proby = gsl_cdf_chisq_Q(chi2y, meany);
778  // proby = ROOT::Math::chisquared_cdf_c(chi2y, meany);
779  hchi2 = chi2y/2.; hndof = meany/2.;
780  proby = 1. - TMath::Gamma(hndof, hchi2);
781  }
782 
783  // Do the x-reconstruction next
784 
785  // Apply the first-pass template algorithm to all clusters
786 
787  // Modify the template if double pixels are present
788 
789  if(nxpix > logxpx) {
790  i=fxpix;
791  while(i < lxpix) {
792  if(xd[i] && !xd[i+1]) {
793  for(j=fxbin; j<=lxbin; ++j) {
794 
795  // Sum the adjacent cells and put the average signal in both
796 
797  sigavg = (xtemp[j][i] + xtemp[j][i+1])/2.;
798  xtemp[j][i] = sigavg;
799  xtemp[j][i+1] = sigavg;
800  }
801  i += 2;
802  } else {
803  ++i;
804  }
805  }
806  }
807 
808  // Define the maximum signal to allow before de-weighting a pixel
809 
810  sxthr = 1.1f*templ.sxmax();
811 
812  // Make sure that there will be at least two pixels that are not de-weighted
813  std::sort(&xsort[0], &xsort[logxpx]);
814  if(logxpx == 1) {sxthr = 1.01f*xsort[0];} else {
815  if (xsort[1] > sxthr) { sxthr = 1.01f*xsort[1]; }
816  }
817 
818  // Evaluate pixel-by-pixel uncertainties (weights) for the templ analysis
819 
820  // for(i=0; i<BXSIZE; ++i) { xsig2[i] = 0.; }
821  templ.xsigma2(fxpix, lxpix, sxthr, xsum, xsig2);
822 
823  // Find the template bin that minimizes the Chi^2
824 
825  chi2xmin = 1.e15;
826  for(i=fxbin; i<=lxbin; ++i) { chi2xbin[i] = -1.e15f;}
827  ss2 = 0.f;
828  for(i=fxpix-2; i<=lxpix+2; ++i) {
829  xw2[i] = 1.f/xsig2[i];
830  xsw[i] = xsum[i]*xw2[i];
831  ss2 += xsum[i]*xsw[i];
832  }
833 
834  minbin = -1;
835  deltaj = djx;
836  jmin = fxbin;
837  jmax = lxbin;
838  while(deltaj > 0) {
839  for(j=jmin; j<=jmax; j+=deltaj) {
840  if(chi2xbin[j] < -100.f) {
841  ssa = 0.f;
842  sa2 = 0.f;
843  for(i=fxpix-2; i<=lxpix+2; ++i) {
844  ssa += xsw[i]*xtemp[j][i];
845  sa2 += xtemp[j][i]*xtemp[j][i]*xw2[i];
846  }
847  rat=ssa/ss2;
848  if(rat <= 0.f) {LOGERROR("SiPixelTemplateReco") << "illegal chi2xmin normalization (1) = " << rat << ENDL; rat = 1.;}
849  chi2xbin[j]=ss2-2.f*ssa/rat+sa2/(rat*rat);
850  }
851  if(chi2xbin[j] < chi2xmin) {
852  chi2xmin = chi2xbin[j];
853  minbin = j;
854  }
855  }
856  deltaj /= 2;
857  if(minbin > fxbin) {jmin = minbin - deltaj;} else {jmin = fxbin;}
858  if(minbin < lxbin) {jmax = minbin + deltaj;} else {jmax = lxbin;}
859  }
860 
861  if (theVerboseLevel > 9) {
862  LOGDEBUG("SiPixelTemplateReco") <<
863  "minbin " << minbin << " chi2xmin = " << chi2xmin << ENDL;
864  }
865 
866  // Do not apply final template pass to 1-pixel clusters (use calibrated offset)
867 
868  if(logxpx == 1) {
869 
870  if(nxpix ==1) {
871  delta = templ.dxone();
872  sigma = templ.sxone();
873  } else {
874  delta = templ.dxtwo();
875  sigma = templ.sxtwo();
876  }
877  xrec = 0.5*(fxpix+lxpix-2*shiftx+2.*originx)*xsize-delta;
878  if(sigma <= 0.) {
879  sigmax = 28.9;
880  } else {
881  sigmax = sigma;
882  }
883 
884  // Do probability calculation for one-pixel clusters
885 
886  chi21max = fmax(chi21min, (double)templ.chi2xminone());
887  chi2xmin -=chi21max;
888  if(chi2xmin < 0.) {chi2xmin = 0.;}
889  meanx = fmax(mean1pix, (double)templ.chi2xavgone());
890  hchi2 = chi2xmin/2.; hndof = meanx/2.;
891  probx = 1. - TMath::Gamma(hndof, hchi2);
892 
893  } else {
894 
895  // Now make the second, interpolating pass with the templates
896 
897  binl = minbin - 1;
898  binh = binl + 2;
899  if(binl < fxbin) { binl = fxbin;}
900  if(binh > lxbin) { binh = lxbin;}
901  ssa = 0.;
902  sa2 = 0.;
903  ssba = 0.;
904  saba = 0.;
905  sba2 = 0.;
906  for(i=fxpix-2; i<=lxpix+2; ++i) {
907  ssa += xsw[i]*xtemp[binl][i];
908  sa2 += xtemp[binl][i]*xtemp[binl][i]*xw2[i];
909  ssba += xsw[i]*(xtemp[binh][i] - xtemp[binl][i]);
910  saba += xtemp[binl][i]*(xtemp[binh][i] - xtemp[binl][i])*xw2[i];
911  sba2 += (xtemp[binh][i] - xtemp[binl][i])*(xtemp[binh][i] - xtemp[binl][i])*xw2[i];
912  }
913 
914  // rat is the fraction of the "distance" from template a to template b
915 
916  rat=(ssba*ssa-ss2*saba)/(ss2*sba2-ssba*ssba);
917  if(rat < 0.f) {rat=0.f;}
918  if(rat > 1.f) {rat=1.0f;}
919  rnorm = (ssa+rat*ssba)/ss2;
920 
921  // Calculate the charges in the first and last pixels
922 
923  qfx = xsum[fxpix];
924  if(xd[fxpix]) {qfx+=xsum[fxpix+1];}
925  if(logxpx > 1) {
926  qlx=xsum[lxpix];
927  if(xd[lxpix-1]) {qlx+=xsum[lxpix-1];}
928  } else {
929  qlx = qfx;
930  }
931 
932  // Now calculate the mean bias correction and uncertainties
933 
934  float qxfrac = (qfx-qlx)/(qfx+qlx);
935  bias = templ.xflcorr(binq,qxfrac)+templ.xavg(binq);
936 
937  // uncertainty and final correction depend upon charge bin
938 
939  xrec = (0.125f*binl+BHX-2.5f+rat*(binh-binl)*0.125f-(float)shiftx+originx)*xsize - bias;
940  sigmax = templ.xrms(binq);
941 
942  // Do goodness of fit test in x
943 
944  if(rnorm <= 0.) {LOGERROR("SiPixelTemplateReco") << "illegal chi2x normalization (2) = " << rnorm << ENDL; rnorm = 1.;}
945  chi2x=ss2-2./rnorm*ssa-2./rnorm*rat*ssba+(sa2+2.*rat*saba+rat*rat*sba2)/(rnorm*rnorm)-templ.chi2xmin(binq);
946  if(chi2x < 0.0) {chi2x = 0.0;}
947  meanx = templ.chi2xavg(binq);
948  if(meanx < 0.01) {meanx = 0.01;}
949  // gsl function that calculates the chi^2 tail prob for non-integral dof
950  // probx = gsl_cdf_chisq_Q(chi2x, meanx);
951  // probx = ROOT::Math::chisquared_cdf_c(chi2x, meanx, trx0);
952  hchi2 = chi2x/2.; hndof = meanx/2.;
953  probx = 1. - TMath::Gamma(hndof, hchi2);
954  }
955 
956  // Don't return exact zeros for the probability
957 
958  if(proby < probmin) {proby = probmin;}
959  if(probx < probmin) {probx = probmin;}
960 
961  // Decide whether to generate a cluster charge probability
962 
963  if(calc_probQ) {
964 
965  // Calculate the Vavilov probability that the cluster charge is OK
966 
967  templ.vavilov_pars(mpv, sigmaQ, kappa);
968 #ifndef SI_PIXEL_TEMPLATE_STANDALONE
969  if((sigmaQ <=0.) || (mpv <= 0.) || (kappa < 0.01) || (kappa > 9.9)) {
970  throw cms::Exception("DataCorrupt") << "SiPixelTemplateReco::Vavilov parameters mpv/sigmaQ/kappa = " << mpv << "/" << sigmaQ << "/" << kappa << std::endl;
971  }
972 #else
973  assert((sigmaQ > 0.) && (mpv > 0.) && (kappa > 0.01) && (kappa < 10.));
974 #endif
975  xvav = ((double)qtotal-mpv)/sigmaQ;
976  beta2 = 1.;
977  if(use_VVIObj) {
978  // VVIObj is a private port of CERNLIB VVIDIS
979  VVIObj vvidist(kappa, beta2, 1);
980  prvav = vvidist.fcn(xvav);
981 
982  // std::cout << "vav: " << kappa << " " << xvav << " " << prvav
983  // << " " << TMath::VavilovI(xvav, kappa, beta2) << std::endl;
984 
985  } else {
986  // Use faster but less accurate TMath Vavilov distribution function
987  prvav = TMath::VavilovI(xvav, kappa, beta2);
988  }
989  // Change to upper tail probability
990  // if(prvav > 0.5) prvav = 1. - prvav;
991  // probQ = (float)(2.*prvav);
992  probQ = 1. - prvav;
993  if(probQ < probQmin) {probQ = probQmin;}
994  } else {
995  probQ = -1;
996  }
997 
998  return 0;
999 } // PixelTempReco2D
1000 
1001 // *************************************************************************************************************************************
1002 // Overload parameter list for compatibility with older versions
1029 // *************************************************************************************************************************************
1030 int SiPixelTemplateReco::PixelTempReco2D(int id, float cotalpha, float cotbeta, float locBz, array_2d& cluster,
1031  std::vector<bool>& ydouble, std::vector<bool>& xdouble,
1032  SiPixelTemplate& templ,
1033  float& yrec, float& sigmay, float& proby, float& xrec, float& sigmax, float& probx, int& qbin, int speed,
1034  float& probQ)
1035 
1036 {
1037  // Local variables
1038  const bool deadpix = false;
1039  std::vector<std::pair<int, int> > zeropix;
1040 
1041  return SiPixelTemplateReco::PixelTempReco2D(id, cotalpha, cotbeta, locBz, cluster, ydouble, xdouble, templ,
1042  yrec, sigmay, proby, xrec, sigmax, probx, qbin, speed, deadpix, zeropix, probQ);
1043 
1044 } // PixelTempReco2D
1045 
1046 // *************************************************************************************************************************************
1047 // Overload parameter list for compatibility with older versions
1073 // *************************************************************************************************************************************
1074 int SiPixelTemplateReco::PixelTempReco2D(int id, float cotalpha, float cotbeta, array_2d& cluster,
1075  std::vector<bool>& ydouble, std::vector<bool>& xdouble,
1076  SiPixelTemplate& templ,
1077  float& yrec, float& sigmay, float& proby, float& xrec, float& sigmax, float& probx, int& qbin, int speed,
1078  float& probQ)
1079 
1080 {
1081  // Local variables
1082  const bool deadpix = false;
1083  std::vector<std::pair<int, int> > zeropix;
1084  float locBz = -1.;
1085  if(cotbeta < 0.) {locBz = -locBz;}
1086 
1087  return SiPixelTemplateReco::PixelTempReco2D(id, cotalpha, cotbeta, locBz, cluster, ydouble, xdouble, templ,
1088  yrec, sigmay, proby, xrec, sigmax, probx, qbin, speed, deadpix, zeropix, probQ);
1089 
1090 } // PixelTempReco2D
1091 
1092 
1093 // *************************************************************************************************************************************
1094 // Overload parameter list for compatibility with older versions
1117 // *************************************************************************************************************************************
1118 int SiPixelTemplateReco::PixelTempReco2D(int id, float cotalpha, float cotbeta, array_2d& cluster,
1119  std::vector<bool>& ydouble, std::vector<bool>& xdouble,
1120  SiPixelTemplate& templ,
1121  float& yrec, float& sigmay, float& proby, float& xrec, float& sigmax, float& probx, int& qbin, int speed)
1122 
1123 {
1124  // Local variables
1125  const bool deadpix = false;
1126  std::vector<std::pair<int, int> > zeropix;
1127  float locBz = -1.;
1128  if(cotbeta < 0.) {locBz = -locBz;}
1129  float probQ;
1130  if(speed < 0) speed = 0;
1131  if(speed > 3) speed = 3;
1132 
1133  return SiPixelTemplateReco::PixelTempReco2D(id, cotalpha, cotbeta, locBz, cluster, ydouble, xdouble, templ,
1134  yrec, sigmay, proby, xrec, sigmax, probx, qbin, speed, deadpix, zeropix, probQ);
1135 
1136 } // PixelTempReco2D
dbl * delta
Definition: mlp_gen.cc:36
int i
Definition: DBlmapReader.cc:9
#define BXSIZE
float chi2xminone()
//!&lt; minimum of x chi^2 for 1 pixel clusters
float symax()
average pixel signal for y-projection of cluster
float yavg(int i)
average y-bias of reconstruction binned in 4 charge bins
#define BYSIZE
#define TXSIZE
float chi2xmin(int i)
minimum y chi^2 in 4 charge bins
#define BXM1
float chi2ymin(int i)
minimum y chi^2 in 4 charge bins
float xrms(int i)
average x-rms of reconstruction binned in 4 charge bins
float qmin()
minimum cluster charge for valid hit (keeps 99.9% of simulated hits)
float xflcorr(int binq, float qflx)
float sytwo()
rms for one double-pixel y-clusters
float chi2yminone()
//!&lt; minimum of y chi^2 for 1 pixel clusters
bool interpolate(int id, float cotalpha, float cotbeta, float locBz)
float sxone()
rms for one pixel x-clusters
#define BYM1
#define BXM2
boost::multi_array< float, 2 > array_2d
float qscale()
charge scaling factor
float dxone()
mean offset/correction for one pixel x-clusters
static int theVerboseLevel
float chi2yavg(int i)
average y chi^2 in 4 charge bins
void vavilov_pars(double &mpv, double &sigma, double &kappa)
void xtemp(int fxbin, int lxbin, float xtemplate[41][BXSIZE])
float yrms(int i)
average y-rms of reconstruction binned in 4 charge bins
#define BHX
float yflcorr(int binq, float qfly)
int j
Definition: DBlmapReader.cc:9
float xsize()
pixel x-size (microns)
#define BYM2
double f[11][100]
void ytemp(int fybin, int lybin, float ytemplate[41][BYSIZE])
float sxtwo()
rms for one double-pixel x-clusters
int PixelTempReco2D(int id, float cotalpha, float cotbeta, float locBz, array_2d &cluster, std::vector< bool > &ydouble, std::vector< bool > &xdouble, SiPixelTemplate &templ, float &yrec, float &sigmay, float &proby, float &xrec, float &sigmax, float &probx, int &qbin, int speed, bool deadpix, std::vector< std::pair< int, int > > &zeropix, float &probQ)
#define BYM3
float dytwo()
mean offset/correction for one double-pixel y-clusters
double fcn(double x) const
Definition: VVIObj.cc:136
#define LOGDEBUG(x)
float s50()
1/2 of the pixel threshold signal in electrons
int k[5][pyjets_maxn]
#define TYSIZE
static const int maxpix
#define LOGERROR(x)
float syone()
rms for one pixel y-clusters
float chi2yavgone()
//!&lt; average y chi^2 for 1 pixel clusters
float qavg()
average cluster charge for this set of track angles
#define BHY
float sxmax()
average pixel signal for x-projection of cluster
void xsigma2(int fxpix, int lxpix, float sxthr, float xsum[BXSIZE], float xsig2[BXSIZE])
Definition: VVIObj.h:23
float chi2xavgone()
//!&lt; average x chi^2 for 1 pixel clusters
float pixmax()
maximum pixel charge
void ysigma2(int fypix, int lypix, float sythr, float ysum[BYSIZE], float ysig2[BYSIZE])
dbl * Gamma
Definition: mlp_gen.cc:38
#define ENDL
float chi2xavg(int i)
averaage x chi^2 in 4 charge bins
float dyone()
mean offset/correction for one pixel y-clusters
float xavg(int i)
average x-bias of reconstruction binned in 4 charge bins
float dxtwo()
mean offset/correction for one double-pixel x-clusters
float ysize()
pixel y-size (microns)