CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
OldThreeThresholdAlgorithm.cc
Go to the documentation of this file.
2 #include <sstream>
4 
5 #define PATCH_FOR_DIGIS_DUPLICATION
6 
8  //Get ESObject
9  es.get<SiStripGainRcd>().get(gainHandle_);
12 }
13 
15  clusterizeDetUnit_(digis,output);
16 }
18  clusterizeDetUnit_(digis,output);
19 }
20 
21 template<typename InputDetSet>
24 
25 #ifdef PATCH_FOR_DIGIS_DUPLICATION
26  bool printPatchError=false;
27  int countErrors=0;
28 #endif
29  const uint32_t& detID = input.detId();
30 
31  if (!qualityHandle_->IsModuleUsable(detID)){
32 #ifdef DEBUG_SiStripClusterizer_
33  LogDebug("SiStripClusterizer") << "[OldThreeThresholdAlgorithm::clusterizeDetUnit] detid " << detID << " skipped because flagged NOT Usable in SiStripQuality " << std::endl;
34 #endif
35  return;
36  }
37 
38  typename InputDetSet::const_iterator begin=input.begin();
39  typename InputDetSet::const_iterator end =input.end();
40 
41  typename InputDetSet::const_iterator ibeg, iend, ihigh, itest;
42  ibeg = iend = begin;
43  cluster_digis_.clear();
44  cluster_digis_.reserve(10);
45 
46  //output.data.reserve( (end - begin)/3 + 1); // FIXME put back in if needed
47 
48  SiStripApvGain::Range detGainRange = gainHandle_->getRange(detID);
49  SiStripNoises::Range detNoiseRange = noiseHandle_->getRange(detID);
50  SiStripQuality::Range detQualityRange = qualityHandle_->getRange(detID);
51 
52  // AboveSeed predicate(seedThresholdInNoiseSigma(),SiStripNoiseService_,detID);
53  AboveSeed predicate(seedThresholdInNoiseSigma(), noiseHandle_, detNoiseRange, qualityHandle_, detQualityRange);
54 
55 #ifdef DEBUG_SiStripClusterizer_
56  std::stringstream ss;
57 #endif
58 
59  while ( ibeg != end &&
60  (ihigh = std::find_if( ibeg, end, predicate)) != end) {
61 
62 #ifdef DEBUG_SiStripClusterizer_
64  ss << "\nSeed Channel:\n\t\t detID "<< detID << " digis " << ihigh->strip()
65  << " adc " << ihigh->adc() << " is "
66  << " channelNoise " << noiseHandle_->getNoise(ihigh->strip(),detNoiseRange)
67  << " IsBadChannel from SiStripQuality " << qualityHandle_->IsStripBad(detQualityRange,ihigh->strip());
68 #endif
69 
70  // The seed strip is ihigh. Scan up and down from it, finding nearby strips above
71  // threshold, allowing for some holes. The accepted cluster runs from strip ibeg
72  // to iend, and itest is the strip under study, not yet accepted.
73  iend = ihigh;
74  itest = iend + 1;
75  while ( itest != end && (itest->strip() - iend->strip() <= max_holes_ + 1 )) {
76  float channelNoise = noiseHandle_->getNoise(itest->strip(),detNoiseRange);
77  bool IsBadChannel = qualityHandle_->IsStripBad(detQualityRange,itest->strip());
78 
79 #ifdef DEBUG_SiStripClusterizer_
81  ss << "\nStrips on the right:\n\t\t detID " << detID << " digis " << itest->strip()
82  << " adc " << itest->adc() << " " << " channelNoise " << channelNoise
83  << " IsBadChannel from SiStripQuality " << IsBadChannel;
84 #endif
85 
86  if (!IsBadChannel && itest->adc() >= static_cast<int>( channelThresholdInNoiseSigma() * channelNoise)) {
87  iend = itest;
88  }
89  ++itest;
90  }
91  //if the next digi after iend is an adiacent bad digi then insert into candidate cluster
92  itest=iend+1;
93  if ( itest != end && (itest->strip() - iend->strip() == 1) && qualityHandle_->IsStripBad(detQualityRange,itest->strip()) ){
94 #ifdef DEBUG_SiStripClusterizer_
96  ss << "\n\t\tInserted bad strip at the end edge iend->strip()= " << iend->strip() << " itest->strip() = " << itest->strip();
97 #endif
98 
99  iend++;
100  }
101 
102  ibeg = ihigh;
103  itest = ibeg - 1;
104  while ( itest >= begin && (ibeg->strip() - itest->strip() <= max_holes_ + 1 )) {
105  float channelNoise = noiseHandle_->getNoise(itest->strip(),detNoiseRange);
106  bool IsBadChannel = qualityHandle_->IsStripBad(detQualityRange,itest->strip());
107 
108 #ifdef DEBUG_SiStripClusterizer_
109  if(edm::isDebugEnabled())
110  ss << "\nStrips on the left:\n\t\t detID " << detID << " digis " << itest->strip()
111  << " adc " << itest->adc() << " " << " channelNoise " << channelNoise
112  << " IsBadChannel from SiStripQuality " << IsBadChannel;
113 #endif
114 
115 
116  if (!IsBadChannel && itest->adc() >= static_cast<int>( channelThresholdInNoiseSigma() * channelNoise)) {
117  ibeg = itest;
118  }
119  --itest;
120  }
121  //if the next digi after ibeg is an adiacent bad digi then insert into candidate cluster
122  itest=ibeg-1;
123  if ( itest >= begin && (ibeg->strip() - itest->strip() == 1) && qualityHandle_->IsStripBad(detQualityRange,itest->strip()) ) {
124 #ifdef DEBUG_SiStripClusterizer_
125  if(edm::isDebugEnabled())
126  ss << "\nInserted bad strip at the begin edge ibeg->strip()= " << ibeg->strip() << " itest->strip() = " << itest->strip();
127 #endif
128  ibeg--;
129  }
130 
131  float charge = 0;
132  float sigmaNoise2=0;
133  //int counts=0;
134  cluster_digis_.clear();
135 #ifdef PATCH_FOR_DIGIS_DUPLICATION
136  bool isDigiListBad=false;
137  int16_t oldStrip=-1;
138 #endif
139 
140  for (itest=ibeg; itest<=iend; itest++) {
141  float channelNoise = noiseHandle_->getNoise(itest->strip(),detNoiseRange);
142  bool IsBadChannel = qualityHandle_->IsStripBad(detQualityRange,itest->strip());
143 
144 #ifdef PATCH_FOR_DIGIS_DUPLICATION
145  if(itest->strip()==oldStrip){
146  isDigiListBad=true;
147  printPatchError=true;
148  countErrors++;
149  break;
150  }
151  oldStrip=itest->strip();
152 #endif
153 
154 
155 #ifdef DEBUG_SiStripClusterizer_
156  if(edm::isDebugEnabled())
157  ss << "\nLooking at cluster digis:\n\t\t detID " << detID << " digis " << itest->strip()
158  << " adc " << itest->adc() << " channelNoise " << channelNoise
159  << " IsBadChannel from SiStripQuality " << IsBadChannel;
160 #endif
161 
162 
163  //check for consecutive digis
164  if (itest!=ibeg && itest->strip()-(itest-1)->strip()!=1){
165  //digi *(itest-1) and *itest are not consecutive: create an equivalent number of Digis with zero amp
166  for (int j=(itest-1)->strip()+1;j<itest->strip();j++){
167  cluster_digis_.push_back(SiStripDigi(j,0)); //if strip bad or under threshold set StripDigi.adc_=0
168 #ifdef DEBUG_SiStripClusterizer_
169  ss << "\n\t\tHole added: detID " << detID << " digis " << j << " adc 0 ";
170 #endif
171  }
172  }
173  if (!IsBadChannel && itest->adc() >= static_cast<int>( channelThresholdInNoiseSigma()*channelNoise)) {
174 
175  float gainFactor = gainHandle_->getStripGain(itest->strip(), detGainRange);
176 
177  // Begin of Change done by Loic Quertenmont
178  // Protection if the Charge/Gain brings the charge to be bigger
179  // than 255 (largest integer containable in uint8)
180  // Also, Gain is applied only if the strip is not saturating.
181  // Same convention as in SimTracker/SiStripDigitizer/src/SiTrivialDigitalConverter.cc
182 
183  float stripCharge=(static_cast<float>(itest->adc()));
184 
185  // //dummy DEBUGG
186  // float stripCharge;
187  // for (uint16_t myadc=0; myadc<=255; myadc++){
188 
189  // stripCharge=(static_cast<float>(myadc));
190 
191  // gainFactor=0.73;
192  // //ENDDEBUGG
193 
194  //correct for gain only non-truncated channel charge. Throw exception if channel charge exceeding 255 ADC counts is found
195 
196  if(stripCharge<254){
197  stripCharge /= gainFactor;
198 
199  if(stripCharge>511.5){stripCharge=255;}
200  else if(stripCharge>253.5){stripCharge=254;}
201  }
202  else if(stripCharge>255){
203  throw cms::Exception("LogicError") << "Cluster charge (" << stripCharge << ") out of range. This clustering algorithm should only work with input charges lower than or equal to 255 ADC counts";
204 
205  }
206 
207  // //dummy DEBUGG
208  // std::vector<SiStripDigi> b; b.push_back(SiStripDigi(itest->strip(), static_cast<uint8_t>(stripCharge+0.5)));;
209  // SiStripCluster c( detID, SiStripCluster::SiStripDigiRange( b.begin(),b.end()));
210  // edm::LogInfo("MYTEST") << "myadc="<<myadc<<" stripCharge="<<stripCharge<<" stored charge="<< (unsigned int)(c.amplitudes()[0])<< std::endl;
211  // edm::LogInfo("MYTEST") << "myadc="<< (unsigned int)(static_cast<uint8_t>(255.5));
212  // }
213  // //ENDDEBUGG
214 
215  // End of Change done by Loic Quertenmont
216 
217 
218 
219  charge += stripCharge;
220  sigmaNoise2 += channelNoise*channelNoise/(gainFactor*gainFactor);
221 
222  cluster_digis_.push_back(SiStripDigi(itest->strip(), static_cast<uint8_t>(stripCharge+0.5)));
223  } else {
224  cluster_digis_.push_back(SiStripDigi(itest->strip(),0)); //if strip bad or under threshold set SiStripDigi.adc_=0
225 
226 #ifdef DEBUG_SiStripClusterizer_
227  if(edm::isDebugEnabled())
228  ss << "\n\t\tBad or under threshold digis: detID " << detID << " digis " << itest->strip()
229  << " adc " << itest->adc() << " channelNoise " << channelNoise
230  << " IsBadChannel from SiStripQuality " << IsBadChannel;
231 #endif
232  }
233  }
234  float sigmaNoise = sqrt(sigmaNoise2);
235 
236 #ifdef PATCH_FOR_DIGIS_DUPLICATION
237  if (charge >= clusterThresholdInNoiseSigma()*sigmaNoise && !isDigiListBad) {
238 #else
239  if (charge >= clusterThresholdInNoiseSigma()*sigmaNoise ) {
240 #endif
241 
242 #ifdef DEBUG_SiStripClusterizer_
243  if(edm::isDebugEnabled())
244  ss << "\n\t\tCluster accepted :)";
245 #endif
247  cluster_digis_.end())));
248 
249  } else {
250 #ifdef DEBUG_SiStripClusterizer_
251  if(edm::isDebugEnabled())
252  ss << "\n\t\tCluster rejected :(";
253 #endif
254  }
255  ibeg = iend+1;
256  }
257 
258 #ifdef PATCH_FOR_DIGIS_DUPLICATION
259  if(printPatchError)
260  edm::LogError("SiStripClusterizer") << "[OldThreeThresholdAlgorithm::clusterizeDetUnit] \n There are errors in " << countErrors << " clusters due to not unique digis ";
261 #endif
262 
263 #ifdef DEBUG_SiStripClusterizer_
264  LogDebug("SiStripClusterizer") << "[OldThreeThresholdAlgorithm::clusterizeDetUnit] \n" << ss.str();
265 #endif
266  }
267 
268 
#define LogDebug(id)
bool isDebugEnabled()
void push_back(data_type const &d)
std::pair< SiStripDigiIter, SiStripDigiIter > SiStripDigiRange
void initialize(const edm::EventSetup &)
void clusterizeDetUnit_(const InputDetSet &digis, edmNew::DetSetVector< SiStripCluster >::FastFiller &output)
edm::ESHandle< SiStripGain > gainHandle_
static std::string const input
Definition: EdmProvDump.cc:44
std::vector< SiStripDigi > cluster_digis_
edm::ESHandle< SiStripNoises > noiseHandle_
T sqrt(T t)
Definition: SSEVec.h:48
std::pair< ContainerIterator, ContainerIterator > Range
int j
Definition: DBlmapReader.cc:9
#define end
Definition: vmac.h:37
A Digi for the silicon strip detector, containing both strip and adc information, and suitable for st...
Definition: SiStripDigi.h:12
edm::ESHandle< SiStripQuality > qualityHandle_
const T & get() const
Definition: EventSetup.h:55
#define begin
Definition: vmac.h:30
void clusterizeDetUnit(const edm::DetSet< SiStripDigi > &digis, edmNew::DetSetVector< SiStripCluster >::FastFiller &output)
std::pair< ContainerIterator, ContainerIterator > Range
std::pair< ContainerIterator, ContainerIterator > Range
Definition: SiStripNoises.h:48