CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SiPixelGainCalibrationOffline.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_(80),
15  nBinsToUseForEncoding_(253),
16  deadFlag_(255),
17  noisyFlag_(254) {
18  if (deadFlag_ > 0xFF)
19  throw cms::Exception("GainCalibration Payload configuration error")
20  << "[SiPixelGainCalibrationOffline::SiPixelGainCalibrationOffline] Dead flag was set to " << deadFlag_
21  << ", and it must be set less than or equal to 255";
22  if (noisyFlag_ > 0xFF)
23  throw cms::Exception("GainCalibration Payload configuration error")
24  << "[SiPixelGainCalibrationOffline::SiPixelGainCalibrationOffline] Noisy flag was set to " << noisyFlag_
25  << ", and it must be set less than or equal to 255";
26 }
27 //
28 SiPixelGainCalibrationOffline::SiPixelGainCalibrationOffline(float minPed, float maxPed, float minGain, float maxGain)
29  : minPed_(minPed),
30  maxPed_(maxPed),
31  minGain_(minGain),
32  maxGain_(maxGain),
33  numberOfRowsToAverageOver_(80),
34  nBinsToUseForEncoding_(253),
35  deadFlag_(255),
36  noisyFlag_(254) {
37  if (deadFlag_ > 0xFF)
38  throw cms::Exception("GainCalibration Payload configuration error")
39  << "[SiPixelGainCalibrationOffline::SiPixelGainCalibrationOffline] Dead flag was set to " << deadFlag_
40  << ", and it must be set less than or equal to 255";
41  if (noisyFlag_ > 0xFF)
42  throw cms::Exception("GainCalibration Payload configuration error")
43  << "[SiPixelGainCalibrationOffline::SiPixelGainCalibrationOffline] Noisy flag was set to " << noisyFlag_
44  << ", and it must be set less than or equal to 255";
45 }
46 
47 bool SiPixelGainCalibrationOffline::put(const uint32_t& DetId, Range input, const int& nCols) {
48  // put in SiPixelGainCalibrationOffline of DetId
49 
50  Registry::iterator p =
52  if (p != indexes.end() && p->detid == DetId)
53  return false;
54 
55  size_t sd = input.second - input.first;
56  DetRegistry detregistry;
57  detregistry.detid = DetId;
58  detregistry.ncols = nCols;
59  detregistry.ibegin = v_pedestals.size();
60  detregistry.iend = v_pedestals.size() + sd;
61  indexes.insert(p, detregistry);
62 
63  v_pedestals.insert(v_pedestals.end(), input.first, input.second);
64  return true;
65 }
66 
67 const int SiPixelGainCalibrationOffline::getNCols(const uint32_t& DetId) const {
68  // get number of columns of DetId
71  if (p == indexes.end() || p->detid != DetId)
72  return 0;
73  else
74  return p->ncols;
75 }
76 
78  // get SiPixelGainCalibrationOffline Range of DetId
79 
82  if (p == indexes.end() || p->detid != DetId)
84  else
85  return SiPixelGainCalibrationOffline::Range(v_pedestals.begin() + p->ibegin, v_pedestals.begin() + p->iend);
86 }
87 
88 const std::pair<const SiPixelGainCalibrationOffline::Range, const int> SiPixelGainCalibrationOffline::getRangeAndNCols(
89  const uint32_t& DetId) const {
92  if (p == indexes.end() || p->detid != DetId)
93  return std::make_pair(SiPixelGainCalibrationOffline::Range(v_pedestals.end(), v_pedestals.end()), 0);
94  else
95  return std::make_pair(
96  SiPixelGainCalibrationOffline::Range(v_pedestals.begin() + p->ibegin, v_pedestals.begin() + p->iend), p->ncols);
97 }
98 
99 void SiPixelGainCalibrationOffline::getDetIds(std::vector<uint32_t>& DetIds_) const {
100  // returns vector of DetIds in map
104  DetIds_.push_back(p->detid);
105  }
106 }
107 
109  float gain, const int& nRows, std::vector<char>& vped, bool thisColumnIsDead, bool thisColumnIsNoisy) {
110  float theEncodedGain = 0;
111  if (!thisColumnIsDead && !thisColumnIsNoisy)
112  theEncodedGain = encodeGain(gain);
113 
114  unsigned int gain_ = (static_cast<unsigned int>(theEncodedGain)) & 0xFF;
115 
116  // if this whole column is dead, set a char based dead flag in the blob.
117  if (thisColumnIsDead)
118  gain_ = deadFlag_ & 0xFF;
119  if (thisColumnIsNoisy)
120  gain_ = noisyFlag_ & 0xFF;
121 
122  vped.resize(vped.size() + 1);
123  //check to make sure the column is being placed in the right place in the blob
124  if (nRows != (int)numberOfRowsToAverageOver_) {
125  throw cms::Exception("GainCalibration Payload configuration error")
126  << "[SiPixelGainCalibrationOffline::setDataGain] You are setting a gain averaged over nRows = " << nRows
127  << " where this payload is set ONLY to average over " << numberOfRowsToAverageOver_ << " nRows";
128  }
129 
130  if (vped.size() % (nRows + 1) != 0) {
131  throw cms::Exception("FillError")
132  << "[SiPixelGainCalibrationOffline::setDataGain] Column gain average (OR SETTING AN ENTIRE COLUMN DEAD/NOISY) "
133  "must be filled after the pedestal for each row has been added. An additional source of this error would be "
134  "setting a pixel dead/noisy AND setting its pedestal";
135  }
136  // insert in vector of char
137  ::memcpy((void*)(&vped[vped.size() - 1]), (void*)(&gain_), 1);
138 }
139 
141  std::vector<char>& vped,
142  bool thisPixelIsDead,
143  bool thisPixelIsNoisy) {
144  float theEncodedPedestal = encodePed(pedestal);
145 
146  unsigned int ped_ = (static_cast<unsigned int>(theEncodedPedestal)) & 0xFF;
147 
148  if (thisPixelIsDead)
149  ped_ = deadFlag_ & 0xFF;
150  if (thisPixelIsNoisy)
151  ped_ = noisyFlag_ & 0xFF;
152 
153  vped.resize(vped.size() + 1);
154  // insert in vector of char
155  ::memcpy((void*)(&vped[vped.size() - 1]), (void*)(&ped_), 1);
156 }
157 
159  const int& col, const int& row, const Range& range, const int& nCols, bool& isDead, bool& isNoisy) const {
160  unsigned int lengthOfColumnData = (range.second - range.first) / nCols;
161  // determine what row averaged range we are in (i.e. ROC 1 or ROC 2)
162  unsigned int lengthOfAveragedDataInEachColumn = numberOfRowsToAverageOver_ + 1;
163  unsigned int numberOfAveragedDataBlocksToSkip = row / numberOfRowsToAverageOver_;
164  unsigned int offSetInCorrectDataBlock = row % numberOfRowsToAverageOver_;
165 
166  const unsigned int datum =
167  *(range.first + col * lengthOfColumnData + numberOfAveragedDataBlocksToSkip * lengthOfAveragedDataInEachColumn +
168  offSetInCorrectDataBlock) &
169  0xFF;
170 
171  int maxRow = lengthOfColumnData - (lengthOfColumnData % numberOfRowsToAverageOver_) - 1;
172  if (col >= nCols || row > maxRow) {
173  throw cms::Exception("CorruptedData")
174  << "[SiPixelGainCalibrationOffline::getPed] Pixel out of range: col " << col << " row " << row;
175  }
176 
177  if (datum == deadFlag_)
178  isDead = true;
179  if (datum == noisyFlag_)
180  isNoisy = true;
181 
182  return decodePed(datum);
183 }
184 
186  const int& row,
187  const Range& range,
188  const int& nCols,
189  bool& isDeadColumn,
190  bool& isNoisyColumn) const {
191  unsigned int lengthOfColumnData = (range.second - range.first) / nCols;
192  // determine what row averaged range we are in (i.e. ROC 1 or ROC 2)
193  unsigned int lengthOfAveragedDataInEachColumn = numberOfRowsToAverageOver_ + 1;
194  unsigned int numberOfAveragedDataBlocksToSkip = row / numberOfRowsToAverageOver_;
195 
196  // gain average is stored in the last location of current row averaged column data block
197  const unsigned int datum = *(range.first + col * lengthOfColumnData +
198  ((numberOfAveragedDataBlocksToSkip + 1) * lengthOfAveragedDataInEachColumn) - 1) &
199  0xFF;
200 
201  if (datum == deadFlag_)
202  isDeadColumn = true;
203  if (datum == noisyFlag_)
204  isNoisyColumn = true;
205 
206  int maxRow = lengthOfColumnData - (lengthOfColumnData % numberOfRowsToAverageOver_) - 1;
207  if (col >= nCols || row > maxRow) {
208  throw cms::Exception("CorruptedData")
209  << "[SiPixelGainCalibrationOffline::getPed] Pixel out of range: col " << col << " row " << row;
210  }
211 
212  return decodeGain(datum);
213 }
214 
216  if (gain < minGain_ || gain > maxGain_) {
217  throw cms::Exception("InsertFailure") << "[SiPixelGainCalibrationOffline::encodeGain] Trying to encode gain ("
218  << gain << ") out of range [" << minGain_ << "," << maxGain_ << "]\n";
219  } else {
220  double precision = (maxGain_ - minGain_) / static_cast<float>(nBinsToUseForEncoding_);
221  float encodedGain = (float)((gain - minGain_) / precision);
222  return encodedGain;
223  }
224 }
225 
227  if (ped < minPed_ || ped > maxPed_) {
228  throw cms::Exception("InsertFailure") << "[SiPixelGainCalibrationOffline::encodePed] Trying to encode pedestal ("
229  << ped << ") out of range [" << minPed_ << "," << maxPed_ << "]\n";
230  } else {
231  double precision = (maxPed_ - minPed_) / static_cast<float>(nBinsToUseForEncoding_);
232  float encodedPed = (float)((ped - minPed_) / precision);
233  return encodedPed;
234  }
235 }
236 
237 float SiPixelGainCalibrationOffline::decodePed(unsigned int ped) const {
238  double precision = (maxPed_ - minPed_) / static_cast<float>(nBinsToUseForEncoding_);
239  float decodedPed = (float)(ped * precision + minPed_);
240  return decodedPed;
241 }
242 
243 float SiPixelGainCalibrationOffline::decodeGain(unsigned int gain) const {
244  double precision = (maxGain_ - minGain_) / static_cast<float>(nBinsToUseForEncoding_);
245  float decodedGain = (float)(gain * precision + minGain_);
246  return decodedGain;
247 }
float getGain(const int &col, const int &row, const Range &range, const int &nCols, bool &isDeadColumn, bool &isNoisyColumn) const
void setDataPedestal(float pedestal, std::vector< char > &vped, bool thisPixelIsDead=false, bool thisPixelIsNoisy=false)
float getPed(const int &col, const int &row, const Range &range, const int &nCols, bool &isDead, bool &isNoisy) const
std::pair< ContainerIterator, ContainerIterator > Range
const int getNCols(const uint32_t &detID) const
void setDataGain(float gain, const int &nRows, std::vector< char > &vped, bool thisColumnIsDead=false, bool thisColumnIsNoisy=false)
static std::string const input
Definition: EdmProvDump.cc:47
const uint16_t range(const Frame &aFrame)
Definition: DetId.h:17
const Range getRange(const uint32_t &detID) const
double sd
float decodeGain(unsigned int gain) const
const std::pair< const Range, const int > getRangeAndNCols(const uint32_t &detID) const
__host__ __device__ constexpr RandomIt lower_bound(RandomIt first, RandomIt last, const T &value, Compare comp={})
static const char gain_[]
string end
Definition: dataset.py:937
void getDetIds(std::vector< uint32_t > &DetIds_) const
int col
Definition: cuy.py:1009
bool put(const uint32_t &detID, Range input, const int &nCols)
float decodePed(unsigned int ped) const