CMS 3D CMS Logo

SiStripApvShotCleaner.cc
Go to the documentation of this file.
2 #include <algorithm>
3 
4 //Uncomment the following #define to have print debug
5 //#define DEBUGME
6 
9  maxNumOfApvs(6), //FED Default: 6 (i.e. max num apvs )
10  stripsPerApv(128),
11  stripsForMedian(64){}
12 
13 
14 
17  if(in.size()<64)
18  return false;
19 
20  if(loop(in)){
21  reset(scan,end);
22  return true;
23  }
24  return false;
25 }
26 
29 
30 #ifdef DEBUGME
31  std::stringstream ss;
32  ss << __func__ << " working on detid " << in.detId() << " for a digi.size=" << in.size();
33 #endif
34 
35  shots_=false;
36  for (auto& val :shotApv_) val=false;
37 
38  cacheDetId=in.detId();
39 
40  //Find the position in the DetSet where the first strip of an apv should be inserted
41  // needed to deduce if at least stripsForMedian strips per apv have been fired
42  for(size_t i=0;i<=maxNumOfApvs;++i){
43 
44  SiStripDigi d(i*stripsPerApv,0); //Fake digi, at the edge of the apv
45  pFirstDigiOfApv[i] = std::lower_bound(in.begin(),in.end(),d);
46 
47  //if satisfied it means that the number of digis in the apv i-1 is above stripsForMedia -> apvShot
49  shots_=true;
50  shotApv_[i-1]=true;
51 #ifdef DEBUGME
52  ss << " found an apv shot of " << pFirstDigiOfApv[i]-pFirstDigiOfApv[i-1] << " digis in detid " << in.detId() << " apv " << i << std::endl;
53 #endif
54  }
55 
56  //---------------------
57  //Just for debug REMOVE
58  /*
59  if(i>0){
60  ss << "detid " << in.detId() << " apv " << i-1 << " number digis " << pFirstDigiOfApv[i]-pFirstDigiOfApv[i-1] << " \t shot " << shotApv_[i-1] << std::endl;
61  if(pFirstDigiOfApv[i]-pFirstDigiOfApv[i-1]>stripsForMedian-2){
62  edm::DetSet<SiStripDigi>::const_iterator dig=pFirstDigiOfApv[i-1];
63  while(dig!=pFirstDigiOfApv[i]){
64  ss << "\t strip " << dig->strip() << " dig.adc " << dig->adc();
65  dig++;
66  }
67  ss << std::endl;
68  }
69  }
70  */
71  //-------------------------------
72  }
73 
74 #ifdef DEBUGME
75  edm::LogInfo("ApvShot") << ss.str();
76 #endif
77 
78  if(!shots_)
79  return false;
80 
82  return true;
83 }
84 
87  vdigis.clear();
88  //loop on Apvs and remove shots. if an apv doesn't have shots, copy it
89  for(size_t i=0;i<maxNumOfApvs;++i){
90  apvDigis.clear();
91 
92  if(shotApv_[i]){
93  apvDigis.insert(apvDigis.end(),pFirstDigiOfApv[i],pFirstDigiOfApv[i+1]);
94  subtractCM();
95  std::stable_sort(apvDigis.begin(),apvDigis.end());
96  vdigis.insert(vdigis.end(),apvDigis.begin(),apvDigis.end());
97  }else{
98  vdigis.insert(vdigis.end(),pFirstDigiOfApv[i],pFirstDigiOfApv[i+1]);
99  }
100  }
101 
102 #ifdef DEBUGME
103  std::stringstream ss;
104  ss <<"detid " << cacheDetId << " new digi.size " << vdigis.size() << "\n";
105  for(size_t i=0;i<vdigis.size();++i)
106  ss << "\t " << i << " strip " << vdigis[i].strip() << " adc " << vdigis[i].adc() ;
107  edm::LogInfo("ApvShot") << ss.str() << std::endl;
108 #endif
109 }
110 
112 
113  //order by charge
114  std::stable_sort(apvDigis.begin(),apvDigis.end(),
115  [](SiStripDigi const& a, SiStripDigi const& b) {return a.adc() > b.adc();});
116 
117  //ignore case where 64th strip is 0ADC
118  if(apvDigis[stripsForMedian].adc()==0){
119 #ifdef DEBUGME
120  std::stringstream ss;
121  ss << "case with strip64=0 --> detid= "<<cacheDetId<< "\n";
122  edm::LogInfo("ApvShot") << ss.str();
123 #endif
124  return;
125  }
126 
127  //Find the Median
128  float CM = 0.5f*(apvDigis[stripsForMedian].adc()+apvDigis[stripsForMedian-1].adc());
129 
130 
131  if(CM<=0)
132  return;
133 
134  //Subtract the median
135  const bool is10bit = apvDigis[0].adc() > 255; // approximation; definitely 10bit in this case
136  size_t i=0;
137  for(;i<stripsForMedian&&apvDigis[i].adc()>CM;++i){
138  const uint16_t adc = ( ( apvDigis[i].adc() > 253 ) && !is10bit ) ? apvDigis[i].adc() : (uint16_t)(apvDigis[i].adc()-CM);
139  apvDigis[i]=SiStripDigi(apvDigis[i].strip(),adc);
140  }
141  apvDigis.resize(i);
142 
143 #ifdef DEBUGME
144  std::stringstream ss;
145  ss << "[subtractCM] detid " << cacheDetId << " CM is " << CM << " the remaining strips after CM subtraction are " << i;
146  edm::LogInfo("ApvShot") << ss.str();
147 #endif
148 }
149 
150 
154  pDetSet->data.swap(vdigis);
155  a=pDetSet->begin();
156  b=pDetSet->end();
157 }
iterator end()
Definition: DetSet.h:60
edm::DetSet< SiStripDigi >::const_iterator pFirstDigiOfApv[7]
void dumpInVector(edm::DetSet< SiStripDigi >::const_iterator *, size_t)
det_id_type detId() const
Definition: DetSet.h:74
std::vector< SiStripDigi > vdigis
size_type size() const
Definition: DetSet.h:63
void reset(edm::DetSet< SiStripDigi >::const_iterator &a, edm::DetSet< SiStripDigi >::const_iterator &b)
#define end
Definition: vmac.h:39
constexpr int adc(sample_type sample)
get the ADC sample (12 bits)
A Digi for the silicon strip detector, containing both strip and adc information, and suitable for st...
Definition: SiStripDigi.h:12
std::unique_ptr< edm::DetSet< SiStripDigi > > pDetSet
bool loop(const edmNew::DetSet< SiStripDigi > &in)
iterator begin()
Definition: DetSet.h:59
double b
Definition: hdecay.h:120
std::vector< SiStripDigi > apvDigis
double a
Definition: hdecay.h:121
bool clean(const edmNew::DetSet< SiStripDigi > &in, edmNew::DetSet< SiStripDigi >::const_iterator &scan, edmNew::DetSet< SiStripDigi >::const_iterator &end)
collection_type::const_iterator const_iterator
Definition: DetSet.h:33
const uint16_t & adc() const
Definition: SiStripDigi.h:34