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  if (deadFlag_ > 0xFF)
20  throw cms::Exception("GainCalibration Payload configuration error")
21  << "[SiPixelGainCalibrationHLT::SiPixelGainCalibrationHLT] Dead flag was set to " << deadFlag_ << ", and it must be set less than or equal to 255";
22 }
23 //
24 SiPixelGainCalibrationForHLT::SiPixelGainCalibrationForHLT(float minPed, float maxPed, float minGain, float maxGain) :
25  minPed_(minPed),
26  maxPed_(maxPed),
27  minGain_(minGain),
28  maxGain_(maxGain),
29  numberOfRowsToAverageOver_(80),
30  nBinsToUseForEncoding_(253),
31  deadFlag_(255),
32  noisyFlag_(254)
33 {
34  if (deadFlag_ > 0xFF)
35  throw cms::Exception("GainCalibration Payload configuration error")
36  << "[SiPixelGainCalibrationHLT::SiPixelGainCalibrationHLT] Dead flag was set to " << deadFlag_ << ", and it must be set less than or equal to 255";
37 }
38 
39 bool SiPixelGainCalibrationForHLT::put(const uint32_t& DetId, Range input, const int& nCols) {
40  // put in SiPixelGainCalibrationForHLT of DetId
41 
42  Registry::iterator p = std::lower_bound(indexes.begin(),indexes.end(),DetId,SiPixelGainCalibrationForHLT::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.ibegin=v_pedestals.size();
50  detregistry.iend=v_pedestals.size()+sd;
51  detregistry.ncols=nCols;
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 SiPixelGainCalibrationForHLT::getNCols(const uint32_t& DetId) const {
59  // get number of columns of DetId
60  RegistryIterator p = std::lower_bound(indexes.begin(),indexes.end(),DetId,SiPixelGainCalibrationForHLT::StrictWeakOrdering());
61  if (p==indexes.end()|| p->detid!=DetId)
62  return 0;
63  else
64  {
65  return p->ncols;
66  }
67 }
68 
70  // get SiPixelGainCalibrationForHLT Range of DetId
71 
72  RegistryIterator p = std::lower_bound(indexes.begin(),indexes.end(),DetId,SiPixelGainCalibrationForHLT::StrictWeakOrdering());
73  if (p==indexes.end()|| p->detid!=DetId)
75  else
76  return SiPixelGainCalibrationForHLT::Range(v_pedestals.begin()+p->ibegin,v_pedestals.begin()+p->iend);
77 }
78 
79 const std::pair<const SiPixelGainCalibrationForHLT::Range, const int>
81  RegistryIterator p = std::lower_bound(indexes.begin(),indexes.end(),DetId,SiPixelGainCalibrationForHLT::StrictWeakOrdering());
82  if (p==indexes.end()|| p->detid!=DetId)
83  return std::make_pair(SiPixelGainCalibrationForHLT::Range(v_pedestals.end(),v_pedestals.end()), 0);
84  else
85  return std::make_pair(SiPixelGainCalibrationForHLT::Range(v_pedestals.begin()+p->ibegin,v_pedestals.begin()+p->iend), p->ncols);
86 }
87 
88 void SiPixelGainCalibrationForHLT::getDetIds(std::vector<uint32_t>& DetIds_) const {
89  // returns vector of DetIds in map
93  DetIds_.push_back(p->detid);
94  }
95 }
96 
97 void SiPixelGainCalibrationForHLT::setData(float ped, float gain, std::vector<char>& vped, bool thisColumnIsDead, bool thisColumnIsNoisy){
98 
99  float theEncodedGain=0;
100  float theEncodedPed=0;
101  if(!thisColumnIsDead && !thisColumnIsNoisy){
102  theEncodedGain = encodeGain(gain);
103  theEncodedPed = encodePed (ped);
104  }
105 
106  unsigned int ped_ = (static_cast<unsigned int>(theEncodedPed)) & 0xFF;
107  unsigned int gain_ = (static_cast<unsigned int>(theEncodedGain)) & 0xFF;
108 
109  if (thisColumnIsDead)
110  {
111  ped_ = deadFlag_ & 0xFF;
112  gain_ = deadFlag_ & 0xFF;
113  }
114  else if (thisColumnIsNoisy)
115  {
116  ped_ = noisyFlag_ & 0xFF;
117  gain_ = noisyFlag_ & 0xFF;
118  }
119 
120  unsigned int data = (ped_ << 8) | gain_ ;
121  vped.resize(vped.size()+2);
122  // insert in vector of char
123  ::memcpy((void*)(&vped[vped.size()-2]),(void*)(&data),2);
124 }
125 
126 float SiPixelGainCalibrationForHLT::getPed(const int& col, const int& row, const Range& range, const int& nCols, bool& isDeadColumn, bool& isNoisyColumn) const {
127  // TODO MERGE THIS FUNCTION WITH GET GAIN, then provide wrappers
128 
129  // 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
130  unsigned int lengthOfColumnData = (range.second-range.first)/nCols;
131  unsigned int lengthOfAveragedDataInEachColumn = 2; // we always only have two values per column averaged block
132  unsigned int numberOfDataBlocksToSkip = row / numberOfRowsToAverageOver_;
133 
134  const DecodingStructure & s = (const DecodingStructure & ) *(range.first+col*lengthOfColumnData + lengthOfAveragedDataInEachColumn*numberOfDataBlocksToSkip);
135 
136  if ((s.ped & 0xFF) == deadFlag_)
137  isDeadColumn = true;
138  else if ((s.ped & 0xFF) == noisyFlag_)
139  isNoisyColumn = true;
140 
141  int maxRow = (lengthOfColumnData/lengthOfAveragedDataInEachColumn)*numberOfRowsToAverageOver_ - 1;
142  if (col >= nCols || row > maxRow){
143  throw cms::Exception("CorruptedData")
144  << "[SiPixelGainCalibrationForHLT::getPed] Pixel out of range: col " << col << " row: " << row;
145  }
146  return decodePed(s.ped & 0xFF);
147 }
148 
149 float SiPixelGainCalibrationForHLT::getGain(const int& col, const int& row, const Range& range, const int& nCols, bool& isDeadColumn, bool& isNoisyColumn) const {
150 
151  // 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
152  unsigned int lengthOfColumnData = (range.second-range.first)/nCols;
153  unsigned int lengthOfAveragedDataInEachColumn = 2; // we always only have two values per column averaged block
154  unsigned int numberOfDataBlocksToSkip = row / numberOfRowsToAverageOver_;
155 
156  const DecodingStructure & s = (const DecodingStructure & ) *(range.first+col*lengthOfColumnData + lengthOfAveragedDataInEachColumn*numberOfDataBlocksToSkip);
157 
158  if ((s.gain & 0xFF) == deadFlag_)
159  isDeadColumn = true;
160  else if ((s.gain & 0xFF) == noisyFlag_)
161  isNoisyColumn = true;
162 
163  int maxRow = (lengthOfColumnData/lengthOfAveragedDataInEachColumn)*numberOfRowsToAverageOver_ - 1;
164  if (col >= nCols || row > maxRow){
165  throw cms::Exception("CorruptedData")
166  << "[SiPixelGainCalibrationForHLT::getGain] Pixel out of range: col " << col << " row: " << row;
167  }
168  return decodeGain(s.gain & 0xFF);
169 
170 }
171 
172 float SiPixelGainCalibrationForHLT::encodeGain( const float& gain ) {
173 
174  if(gain < minGain_ || gain > maxGain_ ) {
175  throw cms::Exception("InsertFailure")
176  << "[SiPixelGainCalibrationForHLT::encodeGain] Trying to encode gain (" << gain << ") out of range [" << minGain_ << "," << maxGain_ << "]\n";
177  } else {
178  double precision = (maxGain_-minGain_)/static_cast<float>(nBinsToUseForEncoding_);
179  float encodedGain = (float)((gain-minGain_)/precision);
180  return encodedGain;
181  }
182 
183 }
184 
185 float SiPixelGainCalibrationForHLT::encodePed( const float& ped ) {
186 
187  if(ped < minPed_ || ped > maxPed_ ) {
188  throw cms::Exception("InsertFailure")
189  << "[SiPixelGainCalibrationForHLT::encodePed] Trying to encode pedestal (" << ped << ") out of range [" << minPed_ << "," << maxPed_ << "]\n";
190  } else {
191  double precision = (maxPed_-minPed_)/static_cast<float>(nBinsToUseForEncoding_);
192  float encodedPed = (float)((ped-minPed_)/precision);
193  return encodedPed;
194  }
195 
196 }
197 
198 float SiPixelGainCalibrationForHLT::decodePed( unsigned int ped ) const {
199 
200  double precision = (maxPed_-minPed_)/static_cast<float>(nBinsToUseForEncoding_);
201  float decodedPed = (float)(ped*precision + minPed_);
202  return decodedPed;
203 
204 }
205 
206 float SiPixelGainCalibrationForHLT::decodeGain( unsigned int gain ) const {
207 
208  double precision = (maxGain_-minGain_)/static_cast<float>(nBinsToUseForEncoding_);
209  float decodedGain = (float)(gain*precision + minGain_);
210  return decodedGain;
211 
212 }
213 
214 
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
float getGain(const int &col, const int &row, const Range &range, const int &nCols, bool &isDeadColumn, bool &isNoisyColumn) const
#define end
Definition: vmac.h:38
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:20
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:31
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
static const char gain_[]
const int getNCols(const uint32_t &detID) const