CMS 3D CMS Logo

PixelThresholdClusterizer.cc
Go to the documentation of this file.
1 //----------------------------------------------------------------------------
20 //----------------------------------------------------------------------------
21 
22 // Our own includes
24 #include "SiPixelArrayBuffer.h"
26 // Geometry
29 //#include "Geometry/CommonTopologies/RectangularPixelTopology.h"
30 
31 // STL
32 #include <stack>
33 #include <vector>
34 #include <iostream>
35 #include <atomic>
36 using namespace std;
37 
38 //----------------------------------------------------------------------------
42 //----------------------------------------------------------------------------
44  (edm::ParameterSet const& conf) :
45  bufferAlreadySet(false),
46  // Get thresholds in electrons
47  thePixelThreshold( conf.getParameter<int>("ChannelThreshold") ),
48  theSeedThreshold( conf.getParameter<int>("SeedThreshold") ),
49  theClusterThreshold( conf.getParameter<int>("ClusterThreshold") ),
50  theClusterThreshold_L1( conf.getParameter<int>("ClusterThreshold_L1") ),
51  theConversionFactor( conf.getParameter<int>("VCaltoElectronGain") ),
52  theConversionFactor_L1( conf.getParameter<int>("VCaltoElectronGain_L1") ),
53  theOffset( conf.getParameter<int>("VCaltoElectronOffset") ),
54  theOffset_L1( conf.getParameter<int>("VCaltoElectronOffset_L1") ),
55  theStackADC_( conf.exists("AdcFullScaleStack") ? conf.getParameter<int>("AdcFullScaleStack") : 255 ),
56  theFirstStack_( conf.exists("FirstStackLayer") ? conf.getParameter<int>("FirstStackLayer") : 5 ),
57  theElectronPerADCGain_( conf.exists("ElectronPerADCGain") ? conf.getParameter<double>("ElectronPerADCGain") : 135. ),
58  theNumOfRows(0), theNumOfCols(0), detid_(0),
59  // Get the constants for the miss-calibration studies
60  doMissCalibrate( conf.getUntrackedParameter<bool>("MissCalibrate",true) ),
61  doSplitClusters( conf.getParameter<bool>("SplitClusters") )
62 {
63  theBuffer.setSize( theNumOfRows, theNumOfCols );
64 }
67 
68 
69 // Configuration descriptions
70 void
72  // siPixelClusters
74  desc.add<edm::InputTag>("src", edm::InputTag("siPixelDigis"));
75  desc.add<int>("ChannelThreshold", 1000);
76  desc.addUntracked<bool>("MissCalibrate", true);
77  desc.add<bool>("SplitClusters", false);
78  desc.add<int>("VCaltoElectronGain", 65);
79  desc.add<int>("VCaltoElectronGain_L1", 65);
80  desc.add<int>("VCaltoElectronOffset", -414);
81  desc.add<int>("VCaltoElectronOffset_L1", -414);
82  desc.add<std::string>("payloadType", "Offline");
83  desc.add<int>("SeedThreshold", 1000);
84  desc.add<int>("ClusterThreshold_L1", 4000);
85  desc.add<int>("ClusterThreshold", 4000);
86  desc.add<int>("maxNumberOfClusters", -1);
87  descriptions.add("siPixelClusters", desc);
88 }
89 
90 //----------------------------------------------------------------------------
93 //----------------------------------------------------------------------------
95 {
96  // Cache the topology.
97  const PixelTopology & topol = pixDet->specificTopology();
98 
99  // Get the new sizes.
100  int nrows = topol.nrows(); // rows in x
101  int ncols = topol.ncolumns(); // cols in y
102 
103  theNumOfRows = nrows; // Set new sizes
104  theNumOfCols = ncols;
105 
106  if ( nrows > theBuffer.rows() ||
107  ncols > theBuffer.columns() )
108  { // change only when a larger is needed
109  //if( nrows != theNumOfRows || ncols != theNumOfCols ) {
110  //cout << " PixelThresholdClusterizer: pixel buffer redefined to "
111  // << nrows << " * " << ncols << endl;
112  //theNumOfRows = nrows; // Set new sizes
113  //theNumOfCols = ncols;
114  // Resize the buffer
115  theBuffer.setSize(nrows,ncols); // Modify
116  bufferAlreadySet = true;
117  }
118 
119  return true;
120 }
121 //----------------------------------------------------------------------------
127 //----------------------------------------------------------------------------
128 template<typename T>
130  const PixelGeomDetUnit * pixDet,
131  const TrackerTopology* tTopo,
132  const std::vector<short>& badChannels,
134 
135  typename T::const_iterator begin = input.begin();
136  typename T::const_iterator end = input.end();
137 
138  // Do not bother for empty detectors
139  //if (begin == end) cout << " PixelThresholdClusterizer::clusterizeDetUnit - No digis to clusterize";
140 
141  // Set up the clusterization on this DetId.
142  if ( !setup(pixDet) )
143  return;
144 
145  detid_ = input.detId();
146 
147  // Set separate cluster threshold for L1 (needed for phase1)
148  auto clusterThreshold = theClusterThreshold;
149  layer_ = (DetId(detid_).subdetId()==1) ? tTopo->pxbLayer(detid_) : 0;
150  if (layer_==1) clusterThreshold = theClusterThreshold_L1;
151 
152  // Copy PixelDigis to the buffer array; select the seed pixels
153  // on the way, and store them in theSeeds.
154  copy_to_buffer(begin, end);
155 
156  assert(output.empty());
157  // Loop over all seeds. TO DO: wouldn't using iterators be faster?
158  // edm::LogError("PixelThresholdClusterizer") << "Starting clusterizing" << endl;
159  for (unsigned int i = 0; i < theSeeds.size(); i++)
160  {
161 
162  // Gavril : The charge of seeds that were already inlcuded in clusters is set to 1 electron
163  // so we don't want to call "make_cluster" for these cases
164  if ( theBuffer(theSeeds[i]) >= theSeedThreshold )
165  { // Is this seed still valid?
166  // Make a cluster around this seed
167  SiPixelCluster && cluster = make_cluster( theSeeds[i] , output);
168 
169  // Check if the cluster is above threshold
170  // (TO DO: one is signed, other unsigned, gcc warns...)
171  if ( cluster.charge() >= clusterThreshold)
172  {
173  // std::cout << "putting in this cluster " << i << " " << cluster.charge() << " " << cluster.pixelADC().size() << endl;
174  // sort by row (x)
175  output.push_back( std::move(cluster) );
176  std::push_heap(output.begin(),output.end(),[](SiPixelCluster const & cl1,SiPixelCluster const & cl2) { return cl1.minPixelRow() < cl2.minPixelRow();});
177  }
178  }
179  }
180  // sort by row (x) maybe sorting the seed would suffice....
181  std::sort_heap(output.begin(),output.end(),[](SiPixelCluster const & cl1,SiPixelCluster const & cl2) { return cl1.minPixelRow() < cl2.minPixelRow();});
182 
183  // Erase the seeds.
184  theSeeds.clear();
185 
186  // Need to clean unused pixels from the buffer array.
187  clear_buffer(begin, end);
188 
189 }
190 
191 //----------------------------------------------------------------------------
199 //----------------------------------------------------------------------------
201 {
202  for(DigiIterator di = begin; di != end; ++di )
203  {
204  theBuffer.set_adc( di->row(), di->column(), 0 ); // reset pixel adc to 0
205  }
206 }
207 
209 {
210  for(ClusterIterator ci = begin; ci != end; ++ci )
211  {
212  for(int i = 0; i < ci->size(); ++i)
213  {
214  const SiPixelCluster::Pixel pixel = ci->pixel(i);
215 
216  theBuffer.set_adc( pixel.x, pixel.y, 0 ); // reset pixel adc to 0
217  }
218  }
219 }
220 
221 //----------------------------------------------------------------------------
223 //----------------------------------------------------------------------------
225 {
226 #ifdef PIXELREGRESSION
227  static std::atomic<int> s_ic=0;
228  in ic = ++s_ic;
229  if (ic==1) {
230  // std::cout << (doMissCalibrate ? "VI from db" : "VI linear") << std::endl;
231  }
232 #endif
233  int electron[end-begin]; // pixel charge in electrons
234  memset(electron, 0, sizeof(electron));
235  if ( doMissCalibrate ) {
236  if (layer_==1) {
237  (*theSiPixelGainCalibrationService_).calibrate(detid_,begin,end,theConversionFactor_L1, theOffset_L1,electron);
238  } else {
239  (*theSiPixelGainCalibrationService_).calibrate(detid_,begin,end,theConversionFactor, theOffset, electron);
240  }
241  } else {
242  int i=0;
243  for(DigiIterator di = begin; di != end; ++di) {
244  auto adc = di->adc();
245  const float gain = theElectronPerADCGain_; // default: 1 ADC = 135 electrons
246  const float pedestal = 0.; //
247  electron[i] = int(adc * gain + pedestal);
248  if (layer_>=theFirstStack_) {
249  if (theStackADC_==1&&adc==1) {
250  electron[i] = int(255*135); // Arbitrarily use overflow value.
251  }
252  if (theStackADC_>1&&theStackADC_!=255&&adc>=1){
253  const float gain = theElectronPerADCGain_; // default: 1 ADC = 135 electrons
254  electron[i] = int((adc-1) * gain * 255/float(theStackADC_-1));
255  }
256  }
257  ++i;
258  }
259  assert(i==(end-begin));
260  }
261 
262  int i=0;
263 #ifdef PIXELREGRESSION
264  static std::atomic<int> eqD=0;
265 #endif
266  for(DigiIterator di = begin; di != end; ++di) {
267  int row = di->row();
268  int col = di->column();
269  int adc = electron[i++]; // this is in electrons
270 
271 #ifdef PIXELREGRESSION
272  int adcOld = calibrate(di->adc(),col,row);
273  //assert(adc==adcOld);
274  if (adc!=adcOld) std::cout << "VI " << eqD <<' '<< ic <<' '<< end-begin <<' '<< i <<' '<< di->adc() <<' ' << adc <<' '<< adcOld << std::endl; else ++eqD;
275 #endif
276 
277  if(adc<100) adc=100; // put all negative pixel charges into the 100 elec bin
278 
279  if ( adc >= thePixelThreshold) {
280  theBuffer.set_adc( row, col, adc);
281  if ( adc >= theSeedThreshold) theSeeds.push_back( SiPixelCluster::PixelPos(row,col) );
282  }
283  }
284  assert(i==(end-begin));
285 
286 }
287 
289 {
290  // loop over clusters
291  for(ClusterIterator ci = begin; ci != end; ++ci) {
292  // loop over pixels
293  for(int i = 0; i < ci->size(); ++i) {
294  const SiPixelCluster::Pixel pixel = ci->pixel(i);
295 
296  int row = pixel.x;
297  int col = pixel.y;
298  int adc = pixel.adc;
299  if ( adc >= thePixelThreshold) {
300  theBuffer.add_adc( row, col, adc);
301  if ( adc >= theSeedThreshold) theSeeds.push_back( SiPixelCluster::PixelPos(row,col) );
302  }
303  }
304  }
305 }
306 
307 //----------------------------------------------------------------------------
308 // Calibrate adc counts to electrons
309 //-----------------------------------------------------------------
311 {
312  int electrons = 0;
313 
314  if ( doMissCalibrate )
315  {
316  // do not perform calibration if pixel is dead!
317 
318  if ( !theSiPixelGainCalibrationService_->isDead(detid_,col,row) &&
319  !theSiPixelGainCalibrationService_->isNoisy(detid_,col,row) )
320  {
321 
322  // Linear approximation of the TANH response
323  // Pixel(0,0,0)
324  //const float gain = 2.95; // 1 ADC = 2.95 VCALs (1/0.339)
325  //const float pedestal = -83.; // -28/0.339
326  // Roc-0 average
327  //const float gain = 1./0.357; // 1 ADC = 2.80 VCALs
328  //const float pedestal = -28.2 * gain; // -79.
329 
330  float DBgain = theSiPixelGainCalibrationService_->getGain(detid_, col, row);
331  float pedestal = theSiPixelGainCalibrationService_->getPedestal(detid_, col, row);
332  float DBpedestal = pedestal * DBgain;
333 
334  // Roc-6 average
335  //const float gain = 1./0.313; // 1 ADC = 3.19 VCALs
336  //const float pedestal = -6.2 * gain; // -19.8
337  //
338  float vcal = adc * DBgain - DBpedestal;
339 
340  // atanh calibration
341  // Roc-6 average
342  //const float p0 = 0.00492;
343  //const float p1 = 1.998;
344  //const float p2 = 90.6;
345  //const float p3 = 134.1;
346  // Roc-6 average
347  //const float p0 = 0.00382;
348  //const float p1 = 0.886;
349  //const float p2 = 112.7;
350  //const float p3 = 113.0;
351  //float vcal = ( atanh( (adc-p3)/p2) + p1)/p0;
352 
353  if (layer_==1) {
354  electrons = int( vcal * theConversionFactor_L1 + theOffset_L1);
355  } else {
356  electrons = int( vcal * theConversionFactor + theOffset);
357  }
358 
359  }
360  }
361  else
362  { // No misscalibration in the digitizer
363  // Simple (default) linear gain
364  const float gain = theElectronPerADCGain_; // default: 1 ADC = 135 electrons
365  const float pedestal = 0.; //
366  electrons = int(adc * gain + pedestal);
367  if (layer_>=theFirstStack_) {
368  if (theStackADC_==1&&adc==1)
369  {
370  electrons = int(255*135); // Arbitrarily use overflow value.
371  }
372  if (theStackADC_>1&&theStackADC_!=255&&adc>=1)
373  {
374  const float gain = theElectronPerADCGain_; // default: 1 ADC = 135 electrons
375  electrons = int((adc-1) * gain * 255/float(theStackADC_-1));
376  }
377  }
378  }
379 
380  return electrons;
381 }
382 
383 //----------------------------------------------------------------------------
385 //----------------------------------------------------------------------------
389 {
390 
391  //First we acquire the seeds for the clusters
392  int seed_adc;
393  stack<SiPixelCluster::PixelPos, vector<SiPixelCluster::PixelPos> > dead_pixel_stack;
394 
395  //The individual modules have been loaded into a buffer.
396  //After each pixel has been considered by the clusterizer, we set the adc count to 1
397  //to mark that we have already considered it.
398  //The only difference between dead/noisy pixels and standard ones is that for dead/noisy pixels,
399  //We consider the charge of the pixel to always be zero.
400 
401  /* this is not possible as dead and noisy pixel cannot make it into a seed...
402  if ( doMissCalibrate &&
403  (theSiPixelGainCalibrationService_->isDead(detid_,pix.col(),pix.row()) ||
404  theSiPixelGainCalibrationService_->isNoisy(detid_,pix.col(),pix.row())) )
405  {
406  std::cout << "IMPOSSIBLE" << std::endl;
407  seed_adc = 0;
408  theBuffer.set_adc(pix, 1);
409  }
410  else {
411  */
412  seed_adc = theBuffer(pix.row(), pix.col());
413  theBuffer.set_adc( pix, 1);
414  // }
415 
416  AccretionCluster acluster;
417  acluster.add(pix, seed_adc);
418 
419  //Here we search all pixels adjacent to all pixels in the cluster.
420  bool dead_flag = false;
421  while ( ! acluster.empty())
422  {
423  //This is the standard algorithm to find and add a pixel
424  auto curInd = acluster.top(); acluster.pop();
425  for ( auto c = std::max(0,int(acluster.y[curInd])-1); c < std::min(int(acluster.y[curInd])+2,theBuffer.columns()) ; ++c) {
426  for ( auto r = std::max(0,int(acluster.x[curInd])-1); r < std::min(int(acluster.x[curInd])+2,theBuffer.rows()); ++r) {
427  if ( theBuffer(r,c) >= thePixelThreshold) {
428  SiPixelCluster::PixelPos newpix(r,c);
429  if (!acluster.add( newpix, theBuffer(r,c))) goto endClus;
430  theBuffer.set_adc( newpix, 1);
431  }
432 
433 
434  /* //Commenting out the addition of dead pixels to the cluster until further testing -- dfehling 06/09
435  //Check on the bounds of the module; this is to keep the isDead and isNoisy modules from returning errors
436  else if(r>= 0 && c >= 0 && (r <= (theNumOfRows-1.)) && (c <= (theNumOfCols-1.))){
437  //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.
438  if((theSiPixelGainCalibrationService_->isDead(detid_,c,r) || theSiPixelGainCalibrationService_->isNoisy(detid_,c,r)) && theBuffer(r,c) != 1){
439 
440  //If a pixel is dead or noisy, check to see if we want to split the clusters or not.
441  //Push it into a dead pixel stack in case we want to split the clusters. Otherwise add it to the cluster.
442  //If we are splitting the clusters, we will iterate over the dead pixel stack later.
443 
444  SiPixelCluster::PixelPos newpix(r,c);
445  if(!doSplitClusters){
446 
447  cluster.add(newpix, theBuffer(r,c));}
448  else if(doSplitClusters){
449  dead_pixel_stack.push(newpix);
450  dead_flag = true;}
451 
452  theBuffer.set_adc(newpix, 1);
453  }
454 
455  }
456  */
457 
458 
459 
460  }
461  }
462 
463  } // while accretion
464  endClus:
465  SiPixelCluster cluster(acluster.isize,acluster.adc, acluster.x,acluster.y, acluster.xmin,acluster.ymin);
466  //Here we split the cluster, if the flag to do so is set and we have found a dead or noisy pixel.
467 
468  if (dead_flag && doSplitClusters)
469  {
470  // Set separate cluster threshold for L1 (needed for phase1)
471  auto clusterThreshold = theClusterThreshold;
472  if (layer_==1) clusterThreshold = theClusterThreshold_L1;
473 
474  //Set the first cluster equal to the existing cluster.
475  SiPixelCluster first_cluster = cluster;
476  bool have_second_cluster = false;
477  while ( !dead_pixel_stack.empty() )
478  {
479  //consider each found dead pixel
480  SiPixelCluster::PixelPos deadpix = dead_pixel_stack.top(); dead_pixel_stack.pop();
481  theBuffer.set_adc(deadpix, 1);
482 
483  //Clusterize the split cluster using the dead pixel as a seed
484  SiPixelCluster second_cluster = make_cluster(deadpix, output);
485 
486  //If both clusters would normally have been found by the clusterizer, put them into output
487  if ( second_cluster.charge() >= clusterThreshold &&
488  first_cluster.charge() >= clusterThreshold )
489  {
490  output.push_back( second_cluster );
491  have_second_cluster = true;
492  }
493 
494  //We also want to keep the merged cluster in data and let the RecHit algorithm decide which set to keep
495  //This loop adds the second cluster to the first.
496  const std::vector<SiPixelCluster::Pixel>& branch_pixels = second_cluster.pixels();
497  for ( unsigned int i = 0; i<branch_pixels.size(); i++)
498  {
499  int temp_x = branch_pixels[i].x;
500  int temp_y = branch_pixels[i].y;
501  int temp_adc = branch_pixels[i].adc;
502  SiPixelCluster::PixelPos newpix(temp_x, temp_y);
503  cluster.add(newpix, temp_adc);}
504  }
505 
506  //Remember to also add the first cluster if we added the second one.
507  if ( first_cluster.charge() >= clusterThreshold && have_second_cluster)
508  {
509  output.push_back( first_cluster );
510  std::push_heap(output.begin(),output.end(),[](SiPixelCluster const & cl1,SiPixelCluster const & cl2) { return cl1.minPixelRow() < cl2.minPixelRow();});
511  }
512  }
513 
514  return cluster;
515 }
516 
int adc(sample_type sample)
get the ADC sample (12 bits)
void clusterizeDetUnitT(const T &input, const PixelGeomDetUnit *pixDet, const TrackerTopology *tTopo, 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...
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
void push_back(data_type const &d)
virtual int nrows() const =0
static const char layer_[]
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
PixelThresholdClusterizer(edm::ParameterSet const &conf)
SiPixelCluster make_cluster(const SiPixelCluster::PixelPos &pix, edmNew::DetSetVector< SiPixelCluster >::FastFiller &output)
The actual clustering algorithm: group the neighboring pixels around the seed.
bool exists(std::string const &parameterName) const
checks if a parameter exists
def setup(process, global_tag, zero_tesla=False)
Definition: GeneralSetup.py:1
int charge() const
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
static std::string const input
Definition: EdmProvDump.cc:44
int minPixelRow() const
edm::DetSet< PixelDigi >::const_iterator DigiIterator
#define end
Definition: vmac.h:37
void add(const PixelPos &pix, int adc)
T min(T a, T b)
Definition: MathUtil.h:58
ParameterDescriptionBase * add(U const &iLabel, T const &value)
int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:37
bool add(SiPixelCluster::PixelPos const &p, UShort const iadc)
constexpr int col() const
unsigned int pxbLayer(const DetId &id) const
Definition: DetId.h:18
edmNew::DetSet< SiPixelCluster >::const_iterator ClusterIterator
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.
void add(std::string const &label, ParameterSetDescription const &psetDescription)
Pixel cluster – collection of neighboring pixels above threshold.
#define begin
Definition: vmac.h:30
virtual int ncolumns() const =0
col
Definition: cuy.py:1008
bool setup(const PixelGeomDetUnit *pixDet)
Private helper methods:
int calibrate(int adc, int col, int row)
long double T
constexpr int row() const
def move(src, dest)
Definition: eostools.py:510
const std::vector< Pixel > pixels() const
void copy_to_buffer(DigiIterator begin, DigiIterator end)
Copy adc counts from PixelDigis into the buffer, identify seeds.