CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SiPixelGainCalibrationForHLT.cc
Go to the documentation of this file.
3 #include <cstring>
4 #include <algorithm>
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  initialize();
20  if (deadFlag_ > 0xFF)
21  throw cms::Exception("GainCalibration Payload configuration error")
22  << "[SiPixelGainCalibrationHLT::SiPixelGainCalibrationHLT] Dead flag was set to " << deadFlag_ << ", and it must be set less than or equal to 255";
23 }
24 //
25 SiPixelGainCalibrationForHLT::SiPixelGainCalibrationForHLT(float minPed, float maxPed, float minGain, float maxGain) :
26  minPed_(minPed),
27  maxPed_(maxPed),
28  minGain_(minGain),
29  maxGain_(maxGain),
30  numberOfRowsToAverageOver_(80),
31  nBinsToUseForEncoding_(253),
32  deadFlag_(255),
33  noisyFlag_(254)
34 {
35  initialize();
36  if (deadFlag_ > 0xFF)
37  throw cms::Exception("GainCalibration Payload configuration error")
38  << "[SiPixelGainCalibrationHLT::SiPixelGainCalibrationHLT] Dead flag was set to " << deadFlag_ << ", and it must be set less than or equal to 255";
39 }
40 
41 
43  pedPrecision = (maxPed_-minPed_)/static_cast<float>(nBinsToUseForEncoding_);
44  gainPrecision = (maxGain_-minGain_)/static_cast<float>(nBinsToUseForEncoding_);
45 
46 }
47 
48 bool SiPixelGainCalibrationForHLT::put(const uint32_t& DetId, Range input, const int& nCols) {
49  // put in SiPixelGainCalibrationForHLT of DetId
50 
51  Registry::iterator p = std::lower_bound(indexes.begin(),indexes.end(),DetId,SiPixelGainCalibrationForHLT::StrictWeakOrdering());
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.ibegin=v_pedestals.size();
59  detregistry.iend=v_pedestals.size()+sd;
60  detregistry.ncols=nCols;
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 SiPixelGainCalibrationForHLT::getNCols(const uint32_t& DetId) const {
68  // get number of columns of DetId
69  RegistryIterator p = std::lower_bound(indexes.begin(),indexes.end(),DetId,SiPixelGainCalibrationForHLT::StrictWeakOrdering());
70  if (p==indexes.end()|| p->detid!=DetId)
71  return 0;
72  else
73  {
74  return p->ncols;
75  }
76 }
77 
79  // get SiPixelGainCalibrationForHLT Range of DetId
80 
81  RegistryIterator p = std::lower_bound(indexes.begin(),indexes.end(),DetId,SiPixelGainCalibrationForHLT::StrictWeakOrdering());
82  if (p==indexes.end()|| p->detid!=DetId)
84  else
85  return SiPixelGainCalibrationForHLT::Range(v_pedestals.begin()+p->ibegin,v_pedestals.begin()+p->iend);
86 }
87 
88 const std::pair<const SiPixelGainCalibrationForHLT::Range, const int>
90  RegistryIterator p = std::lower_bound(indexes.begin(),indexes.end(),DetId,SiPixelGainCalibrationForHLT::StrictWeakOrdering());
91  if (p==indexes.end()|| p->detid!=DetId)
92  return std::make_pair(SiPixelGainCalibrationForHLT::Range(v_pedestals.end(),v_pedestals.end()), 0);
93  else
94  return std::make_pair(SiPixelGainCalibrationForHLT::Range(v_pedestals.begin()+p->ibegin,v_pedestals.begin()+p->iend), p->ncols);
95 }
96 
97 void SiPixelGainCalibrationForHLT::getDetIds(std::vector<uint32_t>& DetIds_) const {
98  // returns vector of DetIds in map
102  DetIds_.push_back(p->detid);
103  }
104 }
105 
106 void SiPixelGainCalibrationForHLT::setData(float ped, float gain, std::vector<char>& vped, bool thisColumnIsDead, bool thisColumnIsNoisy){
107 
108  float theEncodedGain=0;
109  float theEncodedPed=0;
110  if(!thisColumnIsDead && !thisColumnIsNoisy){
111  theEncodedGain = encodeGain(gain);
112  theEncodedPed = encodePed (ped);
113  }
114 
115  unsigned int ped_ = (static_cast<unsigned int>(theEncodedPed)) & 0xFF;
116  unsigned int gain_ = (static_cast<unsigned int>(theEncodedGain)) & 0xFF;
117 
118  if (thisColumnIsDead)
119  {
120  ped_ = deadFlag_ & 0xFF;
121  gain_ = deadFlag_ & 0xFF;
122  }
123  else if (thisColumnIsNoisy)
124  {
125  ped_ = noisyFlag_ & 0xFF;
126  gain_ = noisyFlag_ & 0xFF;
127  }
128 
129  unsigned int data = (ped_ << 8) | gain_ ;
130  vped.resize(vped.size()+2);
131  // insert in vector of char
132  ::memcpy((void*)(&vped[vped.size()-2]),(void*)(&data),2);
133 }
134 
135 
136 std::pair<float,float> SiPixelGainCalibrationForHLT::getPedAndGain(const int& col, const int& row, const Range& range, const int& nCols, bool& isDeadColumn, bool& isNoisyColumn ) const {
137  // determine what averaged data block we are in (there should be 1 or 2 of these depending on if plaquette is 1 by X or 2 by X
138  unsigned int lengthOfColumnData = (range.second-range.first)/nCols;
139  unsigned int lengthOfAveragedDataInEachColumn = 2; // we always only have two values per column averaged block
140  unsigned int numberOfDataBlocksToSkip = row / numberOfRowsToAverageOver_;
141 
142  const DecodingStructure & s = (const DecodingStructure & ) *(range.first + col*lengthOfColumnData + lengthOfAveragedDataInEachColumn*numberOfDataBlocksToSkip);
143 
144  isDeadColumn = (s.ped & 0xFF) == deadFlag_;
145  isNoisyColumn = (s.ped & 0xFF) == noisyFlag_;
146 
147  /*
148  int maxRow = (lengthOfColumnData/lengthOfAveragedDataInEachColumn)*numberOfRowsToAverageOver_ - 1;
149  if (col >= nCols || row > maxRow){
150  throw cms::Exception("CorruptedData")
151  << "[SiPixelGainCalibrationForHLT::getPed] Pixel out of range: col " << col << " row: " << row;
152  }
153  */
154 
155  return std::make_pair(decodePed(s.ped & 0xFF),decodeGain(s.gain & 0xFF));
156 
157 
158 }
159 
160 
161 float SiPixelGainCalibrationForHLT::getPed(const int& col, const int& row, const Range& range, const int& nCols, bool& isDeadColumn, bool& isNoisyColumn) const {
162  // TODO MERGE THIS FUNCTION WITH GET GAIN, then provide wrappers ( VI done, see above)
163 
164  // determine what averaged data block we are in (there should be 1 or 2 of these depending on if plaquette is 1 by X or 2 by X
165  unsigned int lengthOfColumnData = (range.second-range.first)/nCols;
166  unsigned int lengthOfAveragedDataInEachColumn = 2; // we always only have two values per column averaged block
167  unsigned int numberOfDataBlocksToSkip = row / numberOfRowsToAverageOver_;
168 
169  const DecodingStructure & s = (const DecodingStructure & ) *(range.first+col*lengthOfColumnData + lengthOfAveragedDataInEachColumn*numberOfDataBlocksToSkip);
170 
171  if ((s.ped & 0xFF) == deadFlag_)
172  isDeadColumn = true;
173  else if ((s.ped & 0xFF) == noisyFlag_)
174  isNoisyColumn = true;
175 
176  int maxRow = (lengthOfColumnData/lengthOfAveragedDataInEachColumn)*numberOfRowsToAverageOver_ - 1;
177  if (col >= nCols || row > maxRow){
178  throw cms::Exception("CorruptedData")
179  << "[SiPixelGainCalibrationForHLT::getPed] Pixel out of range: col " << col << " row: " << row;
180  }
181  return decodePed(s.ped & 0xFF);
182 }
183 
184 float SiPixelGainCalibrationForHLT::getGain(const int& col, const int& row, const Range& range, const int& nCols, bool& isDeadColumn, bool& isNoisyColumn) const {
185 
186  // determine what averaged data block we are in (there should be 1 or 2 of these depending on if plaquette is 1 by X or 2 by X
187  unsigned int lengthOfColumnData = (range.second-range.first)/nCols;
188  unsigned int lengthOfAveragedDataInEachColumn = 2; // we always only have two values per column averaged block
189  unsigned int numberOfDataBlocksToSkip = row / numberOfRowsToAverageOver_;
190 
191  const DecodingStructure & s = (const DecodingStructure & ) *(range.first+col*lengthOfColumnData + lengthOfAveragedDataInEachColumn*numberOfDataBlocksToSkip);
192 
193  if ((s.gain & 0xFF) == deadFlag_)
194  isDeadColumn = true;
195  else if ((s.gain & 0xFF) == noisyFlag_)
196  isNoisyColumn = true;
197 
198  int maxRow = (lengthOfColumnData/lengthOfAveragedDataInEachColumn)*numberOfRowsToAverageOver_ - 1;
199  if (col >= nCols || row > maxRow){
200  throw cms::Exception("CorruptedData")
201  << "[SiPixelGainCalibrationForHLT::getGain] Pixel out of range: col " << col << " row: " << row;
202  }
203  return decodeGain(s.gain & 0xFF);
204 
205 }
206 
207 float SiPixelGainCalibrationForHLT::encodeGain( const float& gain ) {
208 
209  if(gain < minGain_ || gain > maxGain_ ) {
210  throw cms::Exception("InsertFailure")
211  << "[SiPixelGainCalibrationForHLT::encodeGain] Trying to encode gain (" << gain << ") out of range [" << minGain_ << "," << maxGain_ << "]\n";
212  } else {
213  float precision = (maxGain_-minGain_)/static_cast<float>(nBinsToUseForEncoding_);
214  float encodedGain = (float)((gain-minGain_)/precision);
215  return encodedGain;
216  }
217 
218 }
219 
220 float SiPixelGainCalibrationForHLT::encodePed( const float& ped ) {
221 
222  if(ped < minPed_ || ped > maxPed_ ) {
223  throw cms::Exception("InsertFailure")
224  << "[SiPixelGainCalibrationForHLT::encodePed] Trying to encode pedestal (" << ped << ") out of range [" << minPed_ << "," << maxPed_ << "]\n";
225  } else {
226  float precision = (maxPed_-minPed_)/static_cast<float>(nBinsToUseForEncoding_);
227  float encodedPed = (float)((ped-minPed_)/precision);
228  return encodedPed;
229  }
230 
231 }
232 
std::pair< float, float > getPedAndGain(const int &col, const int &row, const Range &range, const int &nCols, bool &isDeadColumn, bool &isNoisyColumn) const
void setData(float ped, float gain, std::vector< char > &vped, bool thisColumnIsDead=false, bool thisColumnIsNoisy=false)
void getDetIds(std::vector< uint32_t > &DetIds_) const
const std::pair< const Range, const int > getRangeAndNCols(const uint32_t &detID) const
std::pair< ContainerIterator, ContainerIterator > Range
static std::string const input
Definition: EdmProvDump.cc:44
float getGain(const int &col, const int &row, const Range &range, const int &nCols, bool &isDeadColumn, bool &isNoisyColumn) const
#define end
Definition: vmac.h:37
float decodePed(unsigned int ped) const
float getPed(const int &col, const int &row, const Range &range, const int &nCols, bool &isDeadColumn, bool &isNoisyColumn) const
float decodeGain(unsigned int gain) const
Definition: DetId.h:18
bool put(const uint32_t &detID, Range input, const int &nCols)
Registry::const_iterator RegistryIterator
double sd
const Range getRange(const uint32_t &detID) const
#define begin
Definition: vmac.h:30
static const char gain_[]
const int getNCols(const uint32_t &detID) const
int col
Definition: cuy.py:1008