CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SiStripNoises.cc
Go to the documentation of this file.
3 #include <iostream>
4 #include <algorithm>
5 #include <math.h>
6 #include <iomanip>
8 
10  v_noises.clear();
11  indexes.clear();
12  v_noises.insert(v_noises.end(),input.v_noises.begin(),input.v_noises.end());
13  indexes.insert(indexes.end(),input.indexes.begin(),input.indexes.end());
14 }
15 
16 bool SiStripNoises::put(const uint32_t& DetId, const InputVector& input) {
17  std::vector<unsigned char> Vo_CHAR;
18  encode(input, Vo_CHAR);
19 
20  Registry::iterator p = std::lower_bound(indexes.begin(),indexes.end(),DetId,SiStripNoises::StrictWeakOrdering());
21  if (p!=indexes.end() && p->detid==DetId)
22  return false;
23 
24  size_t sd = Vo_CHAR.end() - Vo_CHAR.begin();
25  DetRegistry detregistry;
26  detregistry.detid = DetId;
27  detregistry.ibegin = v_noises.size();
28  detregistry.iend = v_noises.size()+sd;
29  indexes.insert(p,detregistry);
30  v_noises.insert(v_noises.end(),Vo_CHAR.begin(),Vo_CHAR.end());
31  return true;
32 }
33 
34 const SiStripNoises::Range SiStripNoises::getRange(const uint32_t DetId) const {
35  // get SiStripNoises Range of DetId
36 
37  RegistryIterator p = std::lower_bound(indexes.begin(),indexes.end(),DetId,SiStripNoises::StrictWeakOrdering());
38  if (p==indexes.end()|| p->detid!=DetId)
39  return SiStripNoises::Range(v_noises.end(),v_noises.end());
40  else {
41  __builtin_prefetch((&v_noises.front())+p->ibegin);
42  __builtin_prefetch((&v_noises.front())+p->ibegin+96);
43  __builtin_prefetch((&v_noises.front())+p->iend-96);
44  return SiStripNoises::Range(v_noises.begin()+p->ibegin,v_noises.begin()+p->iend);
45 
46  }
47 }
48 
50  if (pos>indexes.size()) return Range(v_noises.end(),v_noises.end());
51  auto p = indexes.begin()+pos;
52  __builtin_prefetch((&v_noises.front())+p->ibegin);
53  __builtin_prefetch((&v_noises.front())+p->ibegin+96);
54  __builtin_prefetch((&v_noises.front())+p->iend-96);
55  return Range(v_noises.begin()+p->ibegin,v_noises.begin()+p->iend);
56 }
57 
58 
59 void SiStripNoises::getDetIds(std::vector<uint32_t>& DetIds_) const {
60  // returns vector of DetIds in map
63  DetIds_.reserve(indexes.size());
64  for (SiStripNoises::RegistryIterator p=begin; p != end; ++p) {
65  DetIds_.push_back(p->detid);
66  }
67 }
68 
69 #ifdef EDM_ML_DEBUG
70 float SiStripNoises::getNoise(uint16_t strip, const Range& range) {
71  if (9*strip>=(range.second-range.first)*8){
72  throw cms::Exception("CorruptedData")
73  << "[SiStripNoises::getNoise] looking for SiStripNoises for a strip out of range: strip " << strip;
74  }
75  return getNoiseFast(strip,range);
76 }
77 
78 #endif
79 
81  v.push_back((static_cast<int16_t> (noise_*10.0 + 0.5) & 0x01FF)) ;
82 }
83 
84 
85 void SiStripNoises::encode(const InputVector& Vi, std::vector<unsigned char>& Vo){
86  static const uint16_t BITS_PER_STRIP = 9;
87  const size_t VoSize = (size_t)((Vi.size() * BITS_PER_STRIP)/8+.999);
88  Vo.resize(VoSize);
89  for(size_t i = 0; i<VoSize; ++i)
90  Vo[i] &= 0x00u;
91 
92  for(unsigned int stripIndex =0; stripIndex<Vi.size(); ++stripIndex){
93  unsigned char* data = &Vo[VoSize-1];
94  uint32_t lowBit = stripIndex * BITS_PER_STRIP;
95  uint8_t firstByteBit = (lowBit & 0x7);
96  uint8_t firstByteNBits = 8 - firstByteBit;
97  uint8_t firstByteMask = 0xffu << firstByteBit;
98  uint8_t secondByteNbits = (BITS_PER_STRIP - firstByteNBits);
99  uint8_t secondByteMask = ~(0xffu << secondByteNbits);
100 
101  *(data-lowBit/8) = (*(data-lowBit/8) & ~(firstByteMask)) | ((Vi[stripIndex] & 0xffu) <<firstByteBit);
102  *(data-lowBit/8-1) = (*(data-lowBit/8-1) & ~(secondByteMask)) | ((Vi[stripIndex] >> firstByteNBits) & secondByteMask);
103 
104  /*
105  if(stripIndex < 25 ){
106  std::cout << "***************ENCODE*********************"<<std::endl
107  << "\tdata-lowBit/8 :"<<print_as_binary((*(data-lowBit/8) & ~(firstByteMask)))
108  << "-"<<print_as_binary(((Vi[stripIndex] & 0xffu) <<firstByteBit))
109  << "\tdata-lowBit/8-1 :"<<print_as_binary((*(data-lowBit/8-1) & ~(secondByteMask)))
110  << "-"<<print_as_binary((((Vi[stripIndex]>> firstByteNBits) & secondByteMask)))
111  << std::endl;
112  std::cout << "strip "<<stripIndex<<"\tvi: " << Vi[stripIndex] <<"\t"
113  << print_short_as_binary(Vi[stripIndex])
114  << "\tvo1:"<< print_char_as_binary(*(data-lowBit/8))
115  << "\tvo2:"<< print_char_as_binary(*(data-lowBit/8-1))
116  << "\tlowBit:"<< lowBit
117  << "\tfirstByteMask :"<<print_as_binary(firstByteMask)
118  << "\tsecondByteMask:"<<print_as_binary(secondByteMask)
119  << "\tfirstByteBit:"<<print_as_binary(firstByteBit)
120  << std::endl;
121  }
122  */
123  }
124 }
125 
126 
127 //============ Methods for bulk-decoding all noises for a module ================
128 
129 
130 
131 void SiStripNoises::allNoises(std::vector<float> &noises, const Range& range) const {
132  size_t mysize = ((range.second-range.first) << 3) / 9;
133  size_t size = noises.size();
134  if (mysize < size) throw cms::Exception("CorruptedData")
135  << "[SiStripNoises::allNoises] Requested noise for " << noises.size() << " strips, I have it only for " << mysize << " strips\n";
136  size_t size8 = size & (~0x7), carry = size & 0x7; // we have an optimized way of unpacking 8 strips
137  const uint8_t *ptr = (&*range.second) - 1;
138  std::vector<float>::iterator out = noises.begin(), end8 = noises.begin() + size8;
139  // we do it this baroque way instead of just loopin on all the strips because it's faster
140  // as the value of 'skip' is a constant, so the compiler can compute the masks directly
141  while (out < end8) {
142  *out = static_cast<float> ( get9bits(ptr, 0) / 10.0f ); ++out;
143  *out = static_cast<float> ( get9bits(ptr, 1) / 10.0f ); ++out;
144  *out = static_cast<float> ( get9bits(ptr, 2) / 10.0f ); ++out;
145  *out = static_cast<float> ( get9bits(ptr, 3) / 10.0f ); ++out;
146  *out = static_cast<float> ( get9bits(ptr, 4) / 10.0f ); ++out;
147  *out = static_cast<float> ( get9bits(ptr, 5) / 10.0f ); ++out;
148  *out = static_cast<float> ( get9bits(ptr, 6) / 10.0f ); ++out;
149  *out = static_cast<float> ( get9bits(ptr, 7) / 10.0f ); ++out;
150  --ptr; // every 8 strips we have to skip one more bit
151  }
152  for (size_t rem = 0; rem < carry; ++rem ) {
153  *out = static_cast<float> ( get9bits(ptr, rem) / 10.0f ); ++out;
154  }
155 }
156 
157 
158 /*
159 const std::string SiStripNoises::print_as_binary(const uint8_t ch) const
160 {
161  std::string str;
162  int i = CHAR_BIT;
163  while (i > 0)
164  {
165  -- i;
166  str.push_back((ch&(1 << i) ? '1' : '0'));
167  }
168  return str;
169 }
170 
171 std::string SiStripNoises::print_char_as_binary(const unsigned char ch) const
172 {
173  std::string str;
174  int i = CHAR_BIT;
175  while (i > 0)
176  {
177  -- i;
178  str.push_back((ch&(1 << i) ? '1' : '0'));
179  }
180  return str;
181 }
182 
183 std::string SiStripNoises::print_short_as_binary(const short ch) const
184 {
185  std::string str;
186  int i = CHAR_BIT*2;
187  while (i > 0)
188  {
189  -- i;
190  str.push_back((ch&(1 << i) ? '1' : '0'));
191  }
192  return str;
193 }
194 */
195 
196 void SiStripNoises::printDebug(std::stringstream& ss) const{
198  uint16_t Nstrips;
199  std::vector<float> vstripnoise;
200 
201  ss << "detid" << std::setw(15) << "strip" << std::setw(10) << "noise" << std::endl;
202 
203  int detId = 0;
204  int oldDetId = 0;
205  for(;rit!=erit;++rit){
206  Nstrips = (rit->iend-rit->ibegin)*8/9; //number of strips = number of chars * char size / strip noise size
207  vstripnoise.resize(Nstrips);
208  allNoises(vstripnoise,make_pair(getDataVectorBegin()+rit->ibegin,getDataVectorBegin()+rit->iend));
209 
210  detId = rit->detid;
211  if( detId != oldDetId ) {
212  oldDetId = detId;
213  ss << detId;
214  }
215  else ss << " ";
216  for(size_t i=0;i<Nstrips;++i){
217  if( i != 0 ) ss << " ";
218  ss << std::setw(15) << i << std::setw(10) << vstripnoise[i] << std::endl;
219  }
220  }
221 }
222 
223 void SiStripNoises::printSummary(std::stringstream& ss) const{
224 
226 
227  std::stringstream tempss;
228 
230  uint16_t Nstrips;
231  std::vector<float> vstripnoise;
232  double mean,rms,min, max;
233  for(;rit!=erit;++rit){
234  Nstrips = (rit->iend-rit->ibegin)*8/9; //number of strips = number of chars * char size / strip noise size
235  vstripnoise.resize(Nstrips);
236  allNoises(vstripnoise,make_pair(getDataVectorBegin()+rit->ibegin,getDataVectorBegin()+rit->iend));
237  tempss << "\ndetid: " << rit->detid << " \t ";
238  mean=0; rms=0; min=10000; max=0;
239 
240  DetId detId(rit->detid);
241 
242  for(size_t i=0;i<Nstrips;++i){
243  mean+=vstripnoise[i];
244  rms+=vstripnoise[i]*vstripnoise[i];
245  if(vstripnoise[i]<min) min=vstripnoise[i];
246  if(vstripnoise[i]>max) max=vstripnoise[i];
247 
248  summary.add(detId, vstripnoise[i]);
249  }
250  mean/=Nstrips;
251  rms= sqrt(rms/Nstrips-mean*mean);
252 
253 
254  tempss << "Nstrips " << Nstrips << " \t; mean " << mean << " \t; rms " << rms << " \t; min " << min << " \t; max " << max << "\t " ;
255  }
256  ss << std::endl << "Summary:" << std::endl;
257  summary.print(ss);
258  ss << std::endl;
259  ss << tempss.str();
260 }
261 
262 std::vector<SiStripNoises::ratioData> SiStripNoises::operator / ( const SiStripNoises& d) {
263  std::vector<ratioData> result;
264  ratioData aData;
265 
268 
269  //Divide result by d
270  for(;iter!=iterE;++iter){
271  float value;
272  //get noise from d
273  aData.detid=iter->detid;
274  aData.values.clear();
275  Range d_range=d.getRange(iter->detid);
276  Range range=Range(v_noises.begin()+iter->ibegin,v_noises.begin()+iter->iend);
277 
278  //if denominator is missing, put the ratio value to 0xFFFF (=inf)
279  size_t strip=0, stripE= (range.second-range.first)*8/9;
280  for (;strip<stripE;++strip){
281  if(d_range.first==d_range.second){
282  value=0xFFFF;
283  }else{
284  value=getNoise(strip,range)/d.getNoise(strip,d_range);
285  }
286  aData.values.push_back(value);
287  }
288  result.push_back(aData);
289  }
290 
291  iter=d.getRegistryVectorBegin();
292  iterE=d.getRegistryVectorEnd();
293 
294  //Divide result by d
295  for(;iter!=iterE;++iter){
296  float value;
297  //get noise from d
298  Range range=this->getRange(iter->detid);
299  Range d_range=Range(d.v_noises.begin()+iter->ibegin,d.v_noises.begin()+iter->iend);
300  if(range.first==range.second){
301  aData.detid=iter->detid;
302  aData.values.clear();
303  size_t strip=0, stripE= (d_range.second-d_range.first)*8/9;
304  for (;strip<stripE;++strip){
305  value=0.;
306  aData.values.push_back(value);
307  }
308  result.push_back(aData);
309  }
310  }
311 
312  return result;
313 }
static const char noise_[]
int i
Definition: DBlmapReader.cc:9
Registry indexes
Definition: SiStripNoises.h:96
static uint16_t get9bits(const uint8_t *&ptr, int8_t skip)
std::vector< float > values
Definition: SiStripNoises.h:29
std::vector< uint16_t > InputVector
Definition: SiStripNoises.h:51
Container v_noises
Definition: SiStripNoises.h:95
std::vector< ratioData > operator/(const SiStripNoises &d)
static void encode(const InputVector &Vi, std::vector< unsigned char > &Vo_CHAR)
static std::string const input
Definition: EdmProvDump.cc:44
static float getNoiseFast(const uint16_t &strip, const Range &range)
Definition: SiStripNoises.h:67
static float getNoise(uint16_t strip, const Range &range)
Definition: SiStripNoises.h:74
T sqrt(T t)
Definition: SSEVec.h:48
tuple result
Definition: query.py:137
Registry::const_iterator RegistryIterator
Definition: SiStripNoises.h:50
#define end
Definition: vmac.h:37
T min(T a, T b)
Definition: MathUtil.h:58
void print(std::stringstream &ss, const bool mean=true) const
bool put(const uint32_t &detID, const InputVector &input)
tuple out
Definition: dbtoconf.py:99
Definition: DetId.h:18
PixelRecoRange< float > Range
RegistryIterator getRegistryVectorEnd() const
Definition: SiStripNoises.h:65
void getDetIds(std::vector< uint32_t > &DetIds_) const
RegistryIterator getRegistryVectorBegin() const
Definition: SiStripNoises.h:64
double sd
#define begin
Definition: vmac.h:30
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
Range getRangeByPos(unsigned short pos) const
const Range getRange(const uint32_t detID) const
void printDebug(std::stringstream &ss) const
void printSummary(std::stringstream &ss) const
std::pair< ContainerIterator, ContainerIterator > Range
Definition: SiStripNoises.h:48
void allNoises(std::vector< float > &noises, const Range &range) const
ContainerIterator getDataVectorBegin() const
Definition: SiStripNoises.h:62
void add(const DetId &detid, const float &value)
Used to compute the mean value of the value variable divided by subdetector, layer and mono/stereo...
tuple size
Write out results.
void setData(float noise_, InputVector &vped)