test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SiPixelGainCalibration.cc
Go to the documentation of this file.
3 #include <algorithm>
4 #include <cstring>
5 
6 //
7 // Constructors
8 //
10  minPed_(0.),
11  maxPed_(255.),
12  minGain_(0.),
13  maxGain_(255.),
14  numberOfRowsToAverageOver_(1),
15  nBinsToUseForEncoding_(253),
16  deadFlag_(255),
17  noisyFlag_(254)
18 {
19  if (deadFlag_ > 0xFF)
20  throw cms::Exception("GainCalibration Payload configuration error")
21  << "[SiPixelGainCalibration::SiPixelGainCalibration] Dead flag was set to " << deadFlag_ << ", and it must be set less than or equal to 255";
22 }
23 //
24 SiPixelGainCalibration::SiPixelGainCalibration(float minPed, float maxPed, float minGain, float maxGain) :
25  minPed_(minPed),
26  maxPed_(maxPed),
27  minGain_(minGain),
28  maxGain_(maxGain),
29  numberOfRowsToAverageOver_(1),
30  nBinsToUseForEncoding_(253),
31  deadFlag_(255),
32  noisyFlag_(254)
33 {
34  if (deadFlag_ > 0xFF)
35  throw cms::Exception("GainCalibration Payload configuration error")
36  << "[SiPixelGainCalibration::SiPixelGainCalibration] Dead flag was set to " << deadFlag_ << ", and it must be set less than or equal to 255";
37 }
38 
39 bool SiPixelGainCalibration::put(const uint32_t& DetId, Range input, const int& nCols) {
40  // put in SiPixelGainCalibration of DetId
41 
42  Registry::iterator p = std::lower_bound(indexes.begin(),indexes.end(),DetId,SiPixelGainCalibration::StrictWeakOrdering());
43  if (p!=indexes.end() && p->detid==DetId)
44  return false;
45 
46  size_t sd= input.second-input.first;
47  DetRegistry detregistry;
48  detregistry.detid=DetId;
49  detregistry.ncols=nCols;
50  detregistry.ibegin=v_pedestals.size();
51  detregistry.iend=v_pedestals.size()+sd;
52  indexes.insert(p,detregistry);
53 
54  v_pedestals.insert(v_pedestals.end(),input.first,input.second);
55  return true;
56 }
57 
58 const int SiPixelGainCalibration::getNCols(const uint32_t& DetId) const {
59  // get number of columns of DetId
60  RegistryIterator p = std::lower_bound(indexes.begin(),indexes.end(),DetId,SiPixelGainCalibration::StrictWeakOrdering());
61  if (p==indexes.end()|| p->detid!=DetId)
62  return 0;
63  else
64  return p->ncols;
65 }
66 
68  // get SiPixelGainCalibration Range of DetId
69 
70  RegistryIterator p = std::lower_bound(indexes.begin(),indexes.end(),DetId,SiPixelGainCalibration::StrictWeakOrdering());
71  if (p==indexes.end()|| p->detid!=DetId)
73  else
74  return SiPixelGainCalibration::Range(v_pedestals.begin()+p->ibegin,v_pedestals.begin()+p->iend);
75 }
76 
77 const std::pair<const SiPixelGainCalibration::Range, const int>
79  RegistryIterator p = std::lower_bound(indexes.begin(),indexes.end(),DetId,SiPixelGainCalibration::StrictWeakOrdering());
80  if (p==indexes.end()|| p->detid!=DetId)
81  return std::make_pair(SiPixelGainCalibration::Range(v_pedestals.end(),v_pedestals.end()), 0);
82  else
83  return std::make_pair(SiPixelGainCalibration::Range(v_pedestals.begin()+p->ibegin,v_pedestals.begin()+p->iend), p->ncols);
84 }
85 
86 
87 void SiPixelGainCalibration::getDetIds(std::vector<uint32_t>& DetIds_) const {
88  // returns vector of DetIds in map
91  for (SiPixelGainCalibration::RegistryIterator p=begin; p != end; ++p) {
92  DetIds_.push_back(p->detid);
93  }
94 }
95 
96 void SiPixelGainCalibration::setData(float ped, float gain, std::vector<char>& vped, bool isDeadPixel, bool isNoisyPixel){
97 
98  float theEncodedGain=0;
99  float theEncodedPed=0;
100  if(!isDeadPixel && !isNoisyPixel){
101  theEncodedGain = encodeGain(gain);
102  theEncodedPed = encodePed (ped);
103  }
104 
105  unsigned int ped_ = (static_cast<unsigned int>(theEncodedPed)) & 0xFF;
106  unsigned int gain_ = (static_cast<unsigned int>(theEncodedGain)) & 0xFF;
107 
108  if (isDeadPixel)
109  {
110  ped_ = deadFlag_ & 0xFF;
111  gain_ = deadFlag_ & 0xFF;
112  }
113  if (isNoisyPixel)
114  {
115  ped_ = noisyFlag_ & 0xFF;
116  gain_ = noisyFlag_ & 0xFF;
117  }
118  unsigned int data = (ped_ << 8) | gain_ ;
119  vped.resize(vped.size()+2);
120  // insert in vector of char
121  ::memcpy((void*)(&vped[vped.size()-2]),(void*)(&data),2);
122 }
123 
124 float SiPixelGainCalibration::getPed(const int& col, const int& row, const Range& range, const int& nCols, bool& isDead, bool& isNoisy) const {
125 
126  int nRows = (range.second-range.first)/2 / nCols;
127  const DecodingStructure & s = (const DecodingStructure & ) *(range.first+(col*nRows + row)*2);
128  if (col >= nCols || row >= nRows){
129  throw cms::Exception("CorruptedData")
130  << "[SiPixelGainCalibration::getPed] Pixel out of range: col " << col << " row " << row;
131  }
132  if ((s.ped & 0xFF) == deadFlag_)
133  isDead = true;
134  if ((s.ped & 0xFF) == noisyFlag_)
135  isNoisy = true;
136  return decodePed(s.ped & 0xFF);
137 }
138 
139 float SiPixelGainCalibration::getGain(const int& col, const int& row, const Range& range, const int& nCols, bool& isDead, bool& isNoisy) const {
140 
141  int nRows = (range.second-range.first)/2 / nCols;
142  const DecodingStructure & s = (const DecodingStructure & ) *(range.first+(col*nRows + row)*2);
143  if (col >= nCols || row >= nRows){
144  throw cms::Exception("CorruptedData")
145  << "[SiPixelGainCalibration::getPed] Pixel out of range: col " << col << " row " << row;
146  }
147  if ((s.gain & 0xFF) == deadFlag_)
148  isDead = true;
149  if ((s.gain & 0xFF) == noisyFlag_)
150  isNoisy = true;
151  return decodeGain(s.gain & 0xFF);
152 }
153 
154 float SiPixelGainCalibration::encodeGain( const float& gain ) {
155 
156  if(gain < minGain_ || gain > maxGain_ ) {
157  throw cms::Exception("InsertFailure")
158  << "[SiPixelGainCalibration::encodeGain] Trying to encode gain (" << gain << ") out of range [" << minGain_ << "," << maxGain_ << "]\n";
159  } else {
160  double precision = (maxGain_-minGain_)/static_cast<float>(nBinsToUseForEncoding_);
161  float encodedGain = (float)((gain-minGain_)/precision);
162  return encodedGain;
163  }
164 
165 }
166 
167 float SiPixelGainCalibration::encodePed( const float& ped ) {
168 
169  if(ped < minPed_ || ped > maxPed_ ) {
170  throw cms::Exception("InsertFailure")
171  << "[SiPixelGainCalibration::encodePed] Trying to encode pedestal (" << ped << ") out of range [" << minPed_ << "," << maxPed_ << "]\n";
172  } else {
173  double precision = (maxPed_-minPed_)/static_cast<float>(nBinsToUseForEncoding_);
174  float encodedPed = (float)((ped-minPed_)/precision);
175  return encodedPed;
176  }
177 
178 }
179 
180 float SiPixelGainCalibration::decodePed( unsigned int ped ) const {
181 
182  double precision = (maxPed_-minPed_)/static_cast<float>(nBinsToUseForEncoding_);
183  float decodedPed = (float)(ped*precision + minPed_);
184  return decodedPed;
185 
186 }
187 
188 float SiPixelGainCalibration::decodeGain( unsigned int gain ) const {
189 
190  double precision = (maxGain_-minGain_)/static_cast<float>(nBinsToUseForEncoding_);
191  float decodedGain = (float)(gain*precision + minGain_);
192  return decodedGain;
193 
194 }
195 
float decodeGain(unsigned int gain) const
std::vector< char > v_pedestals
void getDetIds(std::vector< uint32_t > &DetIds_) const
std::vector< DetRegistry > indexes
const std::pair< const Range, const int > getRangeAndNCols(const uint32_t &detID) const
bool put(const uint32_t &detID, Range input, const int &nCols)
float getPed(const int &col, const int &row, const Range &range, const int &nCols, bool &isDead, bool &isNoisy) const
static std::string const input
Definition: EdmProvDump.cc:44
float encodePed(const float &ped)
#define end
Definition: vmac.h:37
Definition: DetId.h:18
float getGain(const int &col, const int &row, const Range &range, const int &nCols, bool &isDead, bool &isNoisy) const
const Range getRange(const uint32_t &detID) const
Registry::const_iterator RegistryIterator
double sd
std::pair< ContainerIterator, ContainerIterator > Range
#define begin
Definition: vmac.h:30
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
const int getNCols(const uint32_t &detID) const
static const char gain_[]
void setData(float ped, float gain, std::vector< char > &vped, bool thisPixelIsDead=false, bool thisPixelIsNoisy=false)
int col
Definition: cuy.py:1008
float encodeGain(const float &gain)
float decodePed(unsigned int ped) const