CMS 3D CMS Logo

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