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