CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PixelThresholdClusterizer.cc
Go to the documentation of this file.
1 //----------------------------------------------------------------------------
20 //----------------------------------------------------------------------------
21 
22 // Our own includes
26 // Geometry
29 //#include "Geometry/CommonTopologies/RectangularPixelTopology.h"
31 
32 // STL
33 #include <stack>
34 #include <vector>
35 #include <iostream>
36 using namespace std;
37 
38 //----------------------------------------------------------------------------
42 //----------------------------------------------------------------------------
45  conf_(conf), bufferAlreadySet(false), theNumOfRows(0), theNumOfCols(0), detid_(0)
46 {
47  // Get thresholds in electrons
48  thePixelThreshold =
49  conf_.getParameter<int>("ChannelThreshold");
50  theSeedThreshold =
51  conf_.getParameter<int>("SeedThreshold");
52  theClusterThreshold =
53  conf_.getParameter<double>("ClusterThreshold");
54  theConversionFactor =
55  conf_.getParameter<int>("VCaltoElectronGain");
56  theOffset =
57  conf_.getParameter<int>("VCaltoElectronOffset");
58  if ( conf_.exists("AdcFullScaleStack") ) theStackADC_=conf_.getParameter<int>("AdcFullScaleStack");
59  else
60  theStackADC_=255;
61  if ( conf_.exists("FirstStackLayer") ) theFirstStack_=conf_.getParameter<int>("FirstStackLayer");
62  else
63  theFirstStack_=5;
64 
65  // Get the constants for the miss-calibration studies
66  doMissCalibrate=conf_.getUntrackedParameter<bool>("MissCalibrate",true);
67  doSplitClusters = conf.getParameter<bool>("SplitClusters");
68  theBuffer.setSize( theNumOfRows, theNumOfCols );
69 }
72 
73 //----------------------------------------------------------------------------
76 //----------------------------------------------------------------------------
78 {
79  // Cache the topology.
80  const PixelTopology & topol = pixDet->specificTopology();
81 
82  // Get the new sizes.
83  int nrows = topol.nrows(); // rows in x
84  int ncols = topol.ncolumns(); // cols in y
85 
86  theNumOfRows = nrows; // Set new sizes
87  theNumOfCols = ncols;
88 
89  if ( nrows > theBuffer.rows() ||
90  ncols > theBuffer.columns() )
91  { // change only when a larger is needed
92  //if( nrows != theNumOfRows || ncols != theNumOfCols ) {
93  //cout << " PixelThresholdClusterizer: pixel buffer redefined to "
94  // << nrows << " * " << ncols << endl;
95  //theNumOfRows = nrows; // Set new sizes
96  //theNumOfCols = ncols;
97  // Resize the buffer
98  theBuffer.setSize(nrows,ncols); // Modify
99  bufferAlreadySet = true;
100  }
101 
102  return true;
103 }
104 //----------------------------------------------------------------------------
110 //----------------------------------------------------------------------------
112  const PixelGeomDetUnit * pixDet,
113  const std::vector<short>& badChannels,
115 
116  DigiIterator begin = input.begin();
117  DigiIterator end = input.end();
118 
119  // Do not bother for empty detectors
120  //if (begin == end) cout << " PixelThresholdClusterizer::clusterizeDetUnit - No digis to clusterize";
121 
122  // Set up the clusterization on this DetId.
123  if ( !setup(pixDet) )
124  return;
125 
126  detid_ = input.detId();
127 
128  // Copy PixelDigis to the buffer array; select the seed pixels
129  // on the way, and store them in theSeeds.
130  copy_to_buffer(begin, end);
131 
132  // At this point we know the number of seeds on this DetUnit, and thus
133  // also the maximal number of possible clusters, so resize theClusters
134  // in order to make vector<>::push_back() efficient.
135  // output.reserve ( theSeeds.size() ); //GPetruc: It is better *not* to reserve, with the new DetSetVector!
136 
137 
138  // Loop over all seeds. TO DO: wouldn't using iterators be faster?
139  // edm::LogError("PixelThresholdClusterizer") << "Starting clusterizing" << endl;
140  for (unsigned int i = 0; i < theSeeds.size(); i++)
141  {
142 
143  // Gavril : The charge of seeds that were already inlcuded in clusters is set to 1 electron
144  // so we don't want to call "make_cluster" for these cases
145  if ( theBuffer(theSeeds[i]) >= theSeedThreshold )
146  { // Is this seed still valid?
147  // Make a cluster around this seed
148  SiPixelCluster && cluster = make_cluster( theSeeds[i] , output);
149 
150  // Check if the cluster is above threshold
151  // (TO DO: one is signed, other unsigned, gcc warns...)
152  if ( cluster.charge() >= theClusterThreshold)
153  {
154  // std::cout << "putting in this cluster " << i << " " << cluster.charge() << " " << cluster.pixelADC().size() << endl;
155  output.push_back( std::move(cluster) );
156  }
157  }
158  }
159 
160  // Erase the seeds.
161  theSeeds.clear();
162 
163  // Need to clean unused pixels from the buffer array.
164  clear_buffer(begin, end);
165 
166 }
167 
168 //----------------------------------------------------------------------------
176 //----------------------------------------------------------------------------
178 {
179  for(DigiIterator di = begin; di != end; ++di )
180  {
181  theBuffer.set_adc( di->row(), di->column(), 0 ); // reset pixel adc to 0
182  }
183 }
184 
185 //----------------------------------------------------------------------------
187 //----------------------------------------------------------------------------
189 {
190  static int ic=0;
191  if (ic==0) {
192  // std::cout << (doMissCalibrate ? "VI from db" : "VI linear") << std::endl;
193  }
194  ic++;
195  int electron[end-begin];
196  if ( doMissCalibrate ) {
197  (*theSiPixelGainCalibrationService_).calibrate(detid_,begin,end,theConversionFactor, theOffset,electron);
198  } else {
199  int layer = (DetId(detid_).subdetId()==1) ? PXBDetId(detid_).layer() : 0;
200  int i=0;
201  for(DigiIterator di = begin; di != end; ++di) {
202  auto adc = di->adc();
203  const float gain = 135.; // 1 ADC = 135 electrons
204  const float pedestal = 0.; //
205  electron[i] = int(adc * gain + pedestal);
206  if (layer>=theFirstStack_) {
207  if (theStackADC_==1&&adc==1) {
208  electron[i] = int(255*135); // Arbitrarily use overflow value.
209  }
210  if (theStackADC_>1&&theStackADC_!=255&&adc>=1){
211  const float gain = 135.; // 1 ADC = 135 electrons
212  electron[i] = int((adc-1) * gain * 255/float(theStackADC_-1));
213  }
214  }
215  ++i;
216  }
217  assert(i==(end-begin));
218  }
219 
220  int i=0;
221 #ifdef PIXELREGRESSION
222  static int eqD=0;
223 #endif
224  for(DigiIterator di = begin; di != end; ++di) {
225  int row = di->row();
226  int col = di->column();
227  int adc = electron[i++];
228 #ifdef PIXELREGRESSION
229  int adcOld = calibrate(di->adc(),col,row);
230  //assert(adc==adcOld);
231  if (adc!=adcOld) std::cout << "VI " << eqD <<' '<< ic <<' '<< end-begin <<' '<< i <<' '<< di->adc() <<' ' << adc <<' '<< adcOld << std::endl; else ++eqD;
232 #endif
233  if ( adc >= thePixelThreshold) {
234  theBuffer.set_adc( row, col, adc);
235  if ( adc >= theSeedThreshold) theSeeds.push_back( SiPixelCluster::PixelPos(row,col) );
236  }
237  }
238  assert(i==(end-begin));
239 
240 }
241 
242 //----------------------------------------------------------------------------
243 // Calibrate adc counts to electrons
244 //-----------------------------------------------------------------
246 {
247  int electrons = 0;
248  int layer= 0;
249  if (DetId(detid_).subdetId()==1){ layer = PXBDetId(detid_).layer();}
250 
251  if ( doMissCalibrate )
252  {
253  // do not perform calibration if pixel is dead!
254 
255  if ( !theSiPixelGainCalibrationService_->isDead(detid_,col,row) &&
256  !theSiPixelGainCalibrationService_->isNoisy(detid_,col,row) )
257  {
258 
259  // Linear approximation of the TANH response
260  // Pixel(0,0,0)
261  //const float gain = 2.95; // 1 ADC = 2.95 VCALs (1/0.339)
262  //const float pedestal = -83.; // -28/0.339
263  // Roc-0 average
264  //const float gain = 1./0.357; // 1 ADC = 2.80 VCALs
265  //const float pedestal = -28.2 * gain; // -79.
266 
267  float DBgain = theSiPixelGainCalibrationService_->getGain(detid_, col, row);
268  float DBpedestal = theSiPixelGainCalibrationService_->getPedestal(detid_, col, row) * DBgain;
269 
270 
271  // Roc-6 average
272  //const float gain = 1./0.313; // 1 ADC = 3.19 VCALs
273  //const float pedestal = -6.2 * gain; // -19.8
274  //
275  float vcal = adc * DBgain - DBpedestal;
276 
277  // atanh calibration
278  // Roc-6 average
279  //const float p0 = 0.00492;
280  //const float p1 = 1.998;
281  //const float p2 = 90.6;
282  //const float p3 = 134.1;
283  // Roc-6 average
284  //const float p0 = 0.00382;
285  //const float p1 = 0.886;
286  //const float p2 = 112.7;
287  //const float p3 = 113.0;
288  //float vcal = ( atanh( (adc-p3)/p2) + p1)/p0;
289 
290  electrons = int( vcal * theConversionFactor + theOffset);
291  }
292  }
293  else
294  { // No misscalibration in the digitizer
295  // Simple (default) linear gain
296  const float gain = 135.; // 1 ADC = 135 electrons
297  const float pedestal = 0.; //
298  electrons = int(adc * gain + pedestal);
299  if (layer>=theFirstStack_) {
300  if (theStackADC_==1&&adc==1)
301  {
302  electrons = int(255*135); // Arbitrarily use overflow value.
303  }
304  if (theStackADC_>1&&theStackADC_!=255&&adc>=1)
305  {
306  const float gain = 135.; // 1 ADC = 135 electrons
307  electrons = int((adc-1) * gain * 255/float(theStackADC_-1));
308  }
309  }
310  }
311 
312  return electrons;
313 }
314 
315 
316 namespace {
317 
318  struct AccretionCluster {
319  typedef unsigned short UShort;
320  static constexpr UShort MAXSIZE = 256;
321  UShort adc[256];
322  UShort x[256];
323  UShort y[256];
324  UShort xmin=16000;
325  UShort ymin=16000;
326  unsigned int isize=0;
327  unsigned int curr=0;
328 
329  // stack interface (unsafe ok for use below)
330  UShort top() const { return curr;}
331  void pop() { ++curr;}
332  bool empty() { return curr==isize;}
333 
334  bool add(SiPixelCluster::PixelPos const & p, UShort const iadc) {
335  if (isize==MAXSIZE) return false;
336  xmin=std::min(xmin,(unsigned short)(p.row()));
337  ymin=std::min(ymin,(unsigned short)(p.col()));
338  adc[isize]=iadc;
339  x[isize]=p.row();
340  y[isize++]=p.col();
341  return true;
342  }
343  };
344 
345 }
346 
347 //----------------------------------------------------------------------------
349 //----------------------------------------------------------------------------
353 {
354 
355  //First we acquire the seeds for the clusters
356  int seed_adc;
357  stack<SiPixelCluster::PixelPos, vector<SiPixelCluster::PixelPos> > dead_pixel_stack;
358 
359  //The individual modules have been loaded into a buffer.
360  //After each pixel has been considered by the clusterizer, we set the adc count to 1
361  //to mark that we have already considered it.
362  //The only difference between dead/noisy pixels and standard ones is that for dead/noisy pixels,
363  //We consider the charge of the pixel to always be zero.
364 
365  /* this is not possible as dead and noisy pixel cannot make it into a seed...
366  if ( doMissCalibrate &&
367  (theSiPixelGainCalibrationService_->isDead(detid_,pix.col(),pix.row()) ||
368  theSiPixelGainCalibrationService_->isNoisy(detid_,pix.col(),pix.row())) )
369  {
370  std::cout << "IMPOSSIBLE" << std::endl;
371  seed_adc = 0;
372  theBuffer.set_adc(pix, 1);
373  }
374  else {
375  */
376  seed_adc = theBuffer(pix.row(), pix.col());
377  theBuffer.set_adc( pix, 1);
378  // }
379 
380  AccretionCluster acluster;
381  acluster.add(pix, seed_adc);
382 
383  //Here we search all pixels adjacent to all pixels in the cluster.
384  bool dead_flag = false;
385  while ( ! acluster.empty())
386  {
387  //This is the standard algorithm to find and add a pixel
388  auto curInd = acluster.top(); acluster.pop();
389  for ( auto c = std::max(0,int(acluster.y[curInd])-1); c < std::min(int(acluster.y[curInd])+2,theBuffer.columns()) ; ++c) {
390  for ( auto r = std::max(0,int(acluster.x[curInd])-1); r < std::min(int(acluster.x[curInd])+2,theBuffer.rows()); ++r) {
391  if ( theBuffer(r,c) >= thePixelThreshold) {
392  SiPixelCluster::PixelPos newpix(r,c);
393  if (!acluster.add( newpix, theBuffer(r,c))) goto endClus;
394  theBuffer.set_adc( newpix, 1);
395  }
396 
397 
398  /* //Commenting out the addition of dead pixels to the cluster until further testing -- dfehling 06/09
399  //Check on the bounds of the module; this is to keep the isDead and isNoisy modules from returning errors
400  else if(r>= 0 && c >= 0 && (r <= (theNumOfRows-1.)) && (c <= (theNumOfCols-1.))){
401  //Check for dead/noisy pixels check that the buffer is not -1 (already considered). Check whether we want to split clusters separated by dead pixels or not.
402  if((theSiPixelGainCalibrationService_->isDead(detid_,c,r) || theSiPixelGainCalibrationService_->isNoisy(detid_,c,r)) && theBuffer(r,c) != 1){
403 
404  //If a pixel is dead or noisy, check to see if we want to split the clusters or not.
405  //Push it into a dead pixel stack in case we want to split the clusters. Otherwise add it to the cluster.
406  //If we are splitting the clusters, we will iterate over the dead pixel stack later.
407 
408  SiPixelCluster::PixelPos newpix(r,c);
409  if(!doSplitClusters){
410 
411  cluster.add(newpix, theBuffer(r,c));}
412  else if(doSplitClusters){
413  dead_pixel_stack.push(newpix);
414  dead_flag = true;}
415 
416  theBuffer.set_adc(newpix, 1);
417  }
418 
419  }
420  */
421 
422 
423 
424  }
425  }
426 
427  } // while accretion
428  endClus:
429  SiPixelCluster cluster(acluster.isize,acluster.adc, acluster.x,acluster.y, acluster.xmin,acluster.ymin);
430  //Here we split the cluster, if the flag to do so is set and we have found a dead or noisy pixel.
431 
432  if (dead_flag && doSplitClusters)
433  {
434  //Set the first cluster equal to the existing cluster.
435  SiPixelCluster first_cluster = cluster;
436  bool have_second_cluster = false;
437  while ( !dead_pixel_stack.empty() )
438  {
439  //consider each found dead pixel
440  SiPixelCluster::PixelPos deadpix = dead_pixel_stack.top(); dead_pixel_stack.pop();
441  theBuffer.set_adc(deadpix, 1);
442 
443  //Clusterize the split cluster using the dead pixel as a seed
444  SiPixelCluster second_cluster = make_cluster(deadpix, output);
445 
446  //If both clusters would normally have been found by the clusterizer, put them into output
447  if ( second_cluster.charge() >= theClusterThreshold &&
448  first_cluster.charge() >= theClusterThreshold )
449  {
450  output.push_back( second_cluster );
451  have_second_cluster = true;
452  }
453 
454  //We also want to keep the merged cluster in data and let the RecHit algorithm decide which set to keep
455  //This loop adds the second cluster to the first.
456  const std::vector<SiPixelCluster::Pixel>& branch_pixels = second_cluster.pixels();
457  for ( unsigned int i = 0; i<branch_pixels.size(); i++)
458  {
459  int temp_x = branch_pixels[i].x;
460  int temp_y = branch_pixels[i].y;
461  int temp_adc = branch_pixels[i].adc;
462  SiPixelCluster::PixelPos newpix(temp_x, temp_y);
463  cluster.add(newpix, temp_adc);}
464  }
465 
466  //Remember to also add the first cluster if we added the second one.
467  if ( first_cluster.charge() >= theClusterThreshold && have_second_cluster)
468  {
469  output.push_back( first_cluster );
470  }
471  }
472 
473  return cluster;
474 }
475 
int adc(sample_type sample)
get the ADC sample (12 bits)
iterator end()
Definition: DetSet.h:60
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
float charge() const
void push_back(data_type const &d)
void clusterizeDetUnit(const edm::DetSet< PixelDigi > &input, const PixelGeomDetUnit *pixDet, const std::vector< short > &badChannels, edmNew::DetSetVector< SiPixelCluster >::FastFiller &output)
Cluster pixels. This method operates on a matrix of pixels and finds the largest contiguous cluster a...
PixelThresholdClusterizer(edm::ParameterSet const &conf)
det_id_type detId() const
Definition: DetSet.h:72
SiPixelCluster make_cluster(const SiPixelCluster::PixelPos &pix, edmNew::DetSetVector< SiPixelCluster >::FastFiller &output)
The actual clustering algorithm: group the neighboring pixels around the seed.
virtual int ncolumns() const =0
virtual int nrows() const =0
void add(const std::vector< const T * > &source, std::vector< const T * > &dest)
unsigned int layer() const
layer id
Definition: PXBDetId.h:35
static std::string const input
Definition: EdmProvDump.cc:44
const T & max(const T &a, const T &b)
edm::DetSet< PixelDigi >::const_iterator DigiIterator
#define end
Definition: vmac.h:37
void add(const PixelPos &pix, int adc)
int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:37
tuple conf
Definition: dbtoconf.py:185
constexpr int col() const
iterator begin()
Definition: DetSet.h:59
Definition: DetId.h:18
void clear_buffer(DigiIterator begin, DigiIterator end)
Clear the internal buffer array.
virtual const PixelTopology & specificTopology() const
Returns a reference to the pixel proxy topology.
Pixel cluster – collection of neighboring pixels above threshold.
#define begin
Definition: vmac.h:30
bool setup(const PixelGeomDetUnit *pixDet)
Private helper methods:
tuple cout
Definition: gather_cfg.py:121
int calibrate(int adc, int col, int row)
Definition: DDAxes.h:10
int col
Definition: cuy.py:1008
void setup(std::vector< TH2F > &depth, std::string name, std::string units="")
constexpr int row() const
const std::vector< Pixel > pixels() const
#define constexpr
void copy_to_buffer(DigiIterator begin, DigiIterator end)
Copy adc counts from PixelDigis into the buffer, identify seeds.