CMS 3D CMS Logo

SiPixelGainCalibrationForHLT.cc

Go to the documentation of this file.
00001 #include "CondFormats/SiPixelObjects/interface/SiPixelGainCalibrationForHLT.h"
00002 #include "FWCore/Utilities/interface/Exception.h"
00003 
00004 //
00005 // Constructors
00006 //
00007 SiPixelGainCalibrationForHLT::SiPixelGainCalibrationForHLT() :
00008   minPed_(0.),
00009   maxPed_(255.),
00010   minGain_(0.),
00011   maxGain_(255.),
00012   numberOfRowsToAverageOver_(80),
00013   nBinsToUseForEncoding_(254),
00014   deadFlag_(255)
00015 {
00016    if (deadFlag_ > 0xFF)
00017       throw cms::Exception("GainCalibration Payload configuration error")
00018          << "[SiPixelGainCalibrationHLT::SiPixelGainCalibrationHLT] Dead flag was set to " << deadFlag_ << ", and it must be set less than or equal to 255";
00019 }
00020 //
00021 SiPixelGainCalibrationForHLT::SiPixelGainCalibrationForHLT(float minPed, float maxPed, float minGain, float maxGain) :
00022   minPed_(minPed),
00023   maxPed_(maxPed),
00024   minGain_(minGain),
00025   maxGain_(maxGain),
00026   numberOfRowsToAverageOver_(80),
00027   nBinsToUseForEncoding_(254),
00028   deadFlag_(255)
00029 {
00030    if (deadFlag_ > 0xFF)
00031       throw cms::Exception("GainCalibration Payload configuration error")
00032          << "[SiPixelGainCalibrationHLT::SiPixelGainCalibrationHLT] Dead flag was set to " << deadFlag_ << ", and it must be set less than or equal to 255";
00033 }
00034 
00035 bool SiPixelGainCalibrationForHLT::put(const uint32_t& DetId, Range input, const int& nCols) {
00036   // put in SiPixelGainCalibrationForHLT of DetId
00037 
00038   Registry::iterator p = std::lower_bound(indexes.begin(),indexes.end(),DetId,SiPixelGainCalibrationForHLT::StrictWeakOrdering());
00039   if (p!=indexes.end() && p->detid==DetId)
00040     return false;
00041   
00042   size_t sd= input.second-input.first;
00043   DetRegistry detregistry;
00044   detregistry.detid=DetId;
00045   detregistry.ibegin=v_pedestals.size();
00046   detregistry.iend=v_pedestals.size()+sd;
00047   detregistry.ncols=nCols;
00048   indexes.insert(p,detregistry);
00049 
00050   v_pedestals.insert(v_pedestals.end(),input.first,input.second);
00051   return true;
00052 }
00053 
00054 const int SiPixelGainCalibrationForHLT::getNCols(const uint32_t& DetId) const {
00055   // get number of columns of DetId
00056   RegistryIterator p = std::lower_bound(indexes.begin(),indexes.end(),DetId,SiPixelGainCalibrationForHLT::StrictWeakOrdering());
00057   if (p==indexes.end()|| p->detid!=DetId) 
00058     return 0;
00059   else
00060   {
00061     return p->ncols;
00062   }
00063 }
00064 
00065 const SiPixelGainCalibrationForHLT::Range SiPixelGainCalibrationForHLT::getRange(const uint32_t& DetId) const {
00066   // get SiPixelGainCalibrationForHLT Range of DetId
00067   
00068   RegistryIterator p = std::lower_bound(indexes.begin(),indexes.end(),DetId,SiPixelGainCalibrationForHLT::StrictWeakOrdering());
00069   if (p==indexes.end()|| p->detid!=DetId) 
00070     return SiPixelGainCalibrationForHLT::Range(v_pedestals.end(),v_pedestals.end()); 
00071   else 
00072     return SiPixelGainCalibrationForHLT::Range(v_pedestals.begin()+p->ibegin,v_pedestals.begin()+p->iend);
00073 }
00074 
00075 const std::pair<const SiPixelGainCalibrationForHLT::Range, const int>
00076 SiPixelGainCalibrationForHLT::getRangeAndNCols(const uint32_t& DetId) const {
00077   RegistryIterator p = std::lower_bound(indexes.begin(),indexes.end(),DetId,SiPixelGainCalibrationForHLT::StrictWeakOrdering());
00078   if (p==indexes.end()|| p->detid!=DetId) 
00079     return std::make_pair(SiPixelGainCalibrationForHLT::Range(v_pedestals.end(),v_pedestals.end()), 0); 
00080   else 
00081     return std::make_pair(SiPixelGainCalibrationForHLT::Range(v_pedestals.begin()+p->ibegin,v_pedestals.begin()+p->iend), p->ncols);
00082 }
00083 
00084 void SiPixelGainCalibrationForHLT::getDetIds(std::vector<uint32_t>& DetIds_) const {
00085   // returns vector of DetIds in map
00086   SiPixelGainCalibrationForHLT::RegistryIterator begin = indexes.begin();
00087   SiPixelGainCalibrationForHLT::RegistryIterator end   = indexes.end();
00088   for (SiPixelGainCalibrationForHLT::RegistryIterator p=begin; p != end; ++p) {
00089     DetIds_.push_back(p->detid);
00090   }
00091 }
00092 
00093 void SiPixelGainCalibrationForHLT::setData(float ped, float gain, std::vector<char>& vped, bool thisColumnIsDead){
00094   
00095   float theEncodedGain  = encodeGain(gain);
00096   float theEncodedPed   = encodePed (ped);
00097 
00098   unsigned int ped_   = (static_cast<unsigned int>(theEncodedPed))  & 0xFF; 
00099   unsigned int gain_  = (static_cast<unsigned int>(theEncodedGain)) & 0xFF;
00100 
00101   if (thisColumnIsDead)
00102   {
00103      ped_  = deadFlag_ & 0xFF;
00104      gain_ = deadFlag_ & 0xFF;
00105   }
00106 
00107   unsigned int data = (ped_ << 8) | gain_ ;
00108   vped.resize(vped.size()+2);
00109   // insert in vector of char
00110   ::memcpy((void*)(&vped[vped.size()-2]),(void*)(&data),2);
00111 }
00112 
00113 float SiPixelGainCalibrationForHLT::getPed(const int& col, const int& row, const Range& range, const int& nCols, bool& isDeadColumn) const {
00114    // TODO MERGE THIS FUNCTION WITH GET GAIN, then provide wrappers
00115 
00116   // 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
00117   unsigned int lengthOfColumnData  = (range.second-range.first)/nCols;
00118   unsigned int lengthOfAveragedDataInEachColumn = 2;  // we always only have two values per column averaged block 
00119   unsigned int numberOfDataBlocksToSkip = row / numberOfRowsToAverageOver_;
00120 
00121   const DecodingStructure & s = (const DecodingStructure & ) *(range.first+col*lengthOfColumnData + lengthOfAveragedDataInEachColumn*numberOfDataBlocksToSkip);
00122 
00123   if ((s.ped & 0xFF) == deadFlag_)
00124   {
00125      isDeadColumn = true;
00126   }
00127 
00128   int maxRow = (lengthOfColumnData/lengthOfAveragedDataInEachColumn)*numberOfRowsToAverageOver_ - 1;
00129   if (col >= nCols || row > maxRow){
00130     throw cms::Exception("CorruptedData")
00131       << "[SiPixelGainCalibrationForHLT::getPed] Pixel out of range: col " << col << " row: " << row;
00132   }  
00133   return decodePed(s.ped & 0xFF);  
00134 }
00135 
00136 float SiPixelGainCalibrationForHLT::getGain(const int& col, const int& row, const Range& range, const int& nCols, bool& isDeadColumn) const {
00137 
00138   // 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
00139   unsigned int lengthOfColumnData  = (range.second-range.first)/nCols;
00140   unsigned int lengthOfAveragedDataInEachColumn = 2;  // we always only have two values per column averaged block 
00141   unsigned int numberOfDataBlocksToSkip = row / numberOfRowsToAverageOver_;
00142 
00143   const DecodingStructure & s = (const DecodingStructure & ) *(range.first+col*lengthOfColumnData + lengthOfAveragedDataInEachColumn*numberOfDataBlocksToSkip);
00144 
00145   if ((s.gain & 0xFF) == deadFlag_)
00146   {
00147      isDeadColumn = true;
00148   }
00149 
00150   int maxRow = (lengthOfColumnData/lengthOfAveragedDataInEachColumn)*numberOfRowsToAverageOver_ - 1;
00151   if (col >= nCols || row > maxRow){
00152     throw cms::Exception("CorruptedData")
00153       << "[SiPixelGainCalibrationForHLT::getGain] Pixel out of range: col " << col << " row: " << row;
00154   }  
00155   return decodeGain(s.gain & 0xFF);  
00156 
00157 }
00158 
00159 float SiPixelGainCalibrationForHLT::encodeGain( const float& gain ) {
00160   
00161   if(gain < minGain_ || gain > maxGain_ ) {
00162     throw cms::Exception("InsertFailure")
00163       << "[SiPixelGainCalibrationForHLT::encodeGain] Trying to encode gain (" << gain << ") out of range [" << minGain_ << "," << maxGain_ << "]\n";
00164   } else {
00165     double precision   = (maxGain_-minGain_)/static_cast<float>(nBinsToUseForEncoding_);
00166     float  encodedGain = (float)((gain-minGain_)/precision);
00167     return encodedGain;
00168   }
00169 
00170 }
00171 
00172 float SiPixelGainCalibrationForHLT::encodePed( const float& ped ) {
00173 
00174   if(ped < minPed_ || ped > maxPed_ ) {
00175     throw cms::Exception("InsertFailure")
00176       << "[SiPixelGainCalibrationForHLT::encodePed] Trying to encode pedestal (" << ped << ") out of range [" << minPed_ << "," << maxPed_ << "]\n";
00177   } else {
00178     double precision   = (maxPed_-minPed_)/static_cast<float>(nBinsToUseForEncoding_);
00179     float  encodedPed = (float)((ped-minPed_)/precision);
00180     return encodedPed;
00181   }
00182 
00183 }
00184 
00185 float SiPixelGainCalibrationForHLT::decodePed( unsigned int ped ) const {
00186 
00187   double precision = (maxPed_-minPed_)/static_cast<float>(nBinsToUseForEncoding_);
00188   float decodedPed = (float)(ped*precision + minPed_);
00189   return decodedPed;
00190 
00191 }
00192 
00193 float SiPixelGainCalibrationForHLT::decodeGain( unsigned int gain ) const {
00194 
00195   double precision = (maxGain_-minGain_)/static_cast<float>(nBinsToUseForEncoding_);
00196   float decodedGain = (float)(gain*precision + minGain_);
00197   return decodedGain;
00198 
00199 }
00200 
00201 

Generated on Tue Jun 9 17:26:47 2009 for CMSSW by  doxygen 1.5.4