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