CMS 3D CMS Logo

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