test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
IteratedMedianCMNSubtractor.cc
Go to the documentation of this file.
2 
7 #include <cmath>
8 
10  uint32_t n_cache_id = es.get<SiStripNoisesRcd>().cacheIdentifier();
11  uint32_t q_cache_id = es.get<SiStripQualityRcd>().cacheIdentifier();
12 
13  if(n_cache_id != noise_cache_id) {
14  es.get<SiStripNoisesRcd>().get( noiseHandle );
15  noise_cache_id = n_cache_id;
16  }
17  if(q_cache_id != quality_cache_id) {
18  es.get<SiStripQualityRcd>().get( qualityHandle );
19  quality_cache_id = q_cache_id;
20  }
21 }
22 
23 void IteratedMedianCMNSubtractor::subtract(const uint32_t& detId, const uint16_t& firstAPV, std::vector<int16_t>& digis){ subtract_(detId, firstAPV, digis);}
24 void IteratedMedianCMNSubtractor::subtract(const uint32_t& detId, const uint16_t& firstAPV, std::vector<float>& digis){ subtract_(detId,firstAPV, digis);}
25 
26 template<typename T>
27 inline
29 subtract_(const uint32_t& detId, const uint16_t& firstAPV, std::vector<T>& digis){
30 
31  SiStripNoises::Range detNoiseRange = noiseHandle->getRange(detId);
32  SiStripQuality::Range detQualityRange = qualityHandle->getRange(detId);
33 
34  typename std::vector<T>::iterator fs,ls;
35  float offset = 0;
36  std::vector< std::pair<float,float> > subset;
37  subset.reserve(128);
38 
39  _vmedians.clear();
40 
41  uint16_t APV=firstAPV;
42  for( ; APV< digis.size()/128+firstAPV; ++APV)
43  {
44  subset.clear();
45  // fill subset vector with all good strips and their noises
46  for (uint16_t istrip=APV*128; istrip<(APV+1)*128; ++istrip)
47  {
48  if ( !qualityHandle->IsStripBad(detQualityRange,istrip) )
49  {
50  std::pair<float,float> pin((float)digis[istrip-firstAPV*128], (float)noiseHandle->getNoiseFast(istrip,detNoiseRange));
51  subset.push_back( pin );
52  }
53  }
54 
55  // caluate offset for all good strips (first iteration)
56  if (subset.size() != 0)
57  offset = pairMedian(subset);
58 
59  // for second, third... iterations, remove strips over threshold
60  // and recalculate offset on remaining strips
61  for ( int ii = 0; ii<iterations_-1; ++ii )
62  {
63  std::vector< std::pair<float,float> >::iterator si = subset.begin();
64  while( si != subset.end() )
65  {
66  if( si->first-offset > cut_to_avoid_signal_*si->second )
67  si = subset.erase(si);
68  else
69  ++si;
70  }
71  if ( subset.size() == 0 ) break;
72  offset = pairMedian(subset);
73  }
74 
75  _vmedians.push_back(std::pair<short,float>(APV,offset));
76 
77  // remove offset
78  fs = digis.begin()+(APV-firstAPV)*128;
79  ls = digis.begin()+(APV-firstAPV+1)*128;
80  while (fs < ls) {
81  *fs = static_cast<T>(*fs-offset);
82  fs++;
83  }
84 
85  }
86 }
87 
88 
89 
90 inline float IteratedMedianCMNSubtractor::pairMedian( std::vector<std::pair<float,float> >& sample) {
91  std::vector<std::pair<float,float> >::iterator mid = sample.begin() + sample.size()/2;
92  std::nth_element(sample.begin(), mid, sample.end());
93  if( sample.size() & 1 ) //odd size
94  return (*mid).first;
95  return ( (*std::max_element(sample.begin(), mid)).first + (*mid).first ) / 2.;
96 }
97 
edm::ESHandle< SiStripNoises > noiseHandle
float pairMedian(std::vector< std::pair< float, float > > &sample)
int ii
Definition: cuy.py:588
return((rh^lh)&mask)
void subtract_(const uint32_t &, const uint16_t &, std::vector< T > &)
unsigned int offset(bool)
const T & get() const
Definition: EventSetup.h:55
void subtract(const uint32_t &, const uint16_t &, std::vector< int16_t > &)
std::vector< std::pair< short, float > > _vmedians
std::pair< ContainerIterator, ContainerIterator > Range
void init(const edm::EventSetup &es)
std::pair< ContainerIterator, ContainerIterator > Range
Definition: SiStripNoises.h:48
long double T
edm::ESHandle< SiStripQuality > qualityHandle