Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:47:29

0001 //
0002 // This class provide the data structure for the
0003 // ROC DAC parameters
0004 //
0005 // At this point I do not see a reason to make an
0006 // abstract layer for this code.
0007 //
0008 
0009 #include "CalibFormats/SiPixelObjects/interface/PixelROCTrimBits.h"
0010 #include "CalibFormats/SiPixelObjects/interface/PixelBase64.h"
0011 #include <iostream>
0012 #include <sstream>
0013 #include <cassert>
0014 #include <typeinfo>
0015 
0016 using namespace pos;
0017 
0018 PixelROCTrimBits::PixelROCTrimBits() {}
0019 //This part has been modified from the orignal
0020 void PixelROCTrimBits::setROCTrimBits(PixelROCName rocid, std::string bits) {
0021   rocid_ = rocid;
0022   char cpt[2080];
0023   bits.copy(cpt, 2080);
0024   for (unsigned int i = 0; i < bits.size(); i++)
0025     bits_[i] = static_cast<unsigned char>(cpt[i]);
0026 }
0027 //End of part modified
0028 
0029 // Added by Dario: handles the base_64-decoded strings from aDB read
0030 int PixelROCTrimBits::read(const PixelROCName rocid, std::string in) {
0031   rocid_ = rocid;
0032   for (int i = 0; i < (int)sizeof(bits_); i++) {
0033     bits_[i] = in.at(i);
0034   }
0035   return 1;
0036 }
0037 
0038 int PixelROCTrimBits::read(PixelROCName rocid, std::ifstream& in) {
0039   std::string tag;
0040 
0041   //std::cout << "PixelROCTrimBits::read rocid:"<<rocid<<std::endl;
0042 
0043   rocid_ = rocid;
0044 
0045   //std::cout << "PixelROCTrimBits::read rocid_:"<<rocid_<<std::endl;
0046 
0047   for (int i = 0; i < 52; i++) {
0048     in >> tag;
0049 
0050     //std::cout << "Now reading col:"<<tag<<std::endl;
0051 
0052     std::string data;
0053 
0054     in >> data;
0055 
0056     //std::cout <<"data.size()" <<data.size()<<std::endl;
0057 
0058     unsigned char byte = 0;
0059 
0060     for (int j = 0; j < 80; j++) {
0061       unsigned char tmp = toupper(data[j]) - 48;
0062       if (tmp > 9)
0063         tmp -= 7;  //FIXME this is so ugly
0064 
0065       byte += tmp;
0066 
0067       if ((j + 1) % 2 == 0) {
0068         //std::cout << "Writing byte:"<<(int)byte<<std::endl;
0069         bits_[i * 40 + (j + 1) / 2 - 1] = byte;
0070         byte = 0;
0071       } else {
0072         byte *= 16;
0073       }
0074     }
0075   }
0076   return 1;
0077 }
0078 
0079 int PixelROCTrimBits::read(PixelROCName rocid, std::istringstream& in) {
0080   std::string tag;
0081   //std::cout << "PixelROCTrimBits::read rocid:"<<rocid<<std::endl;
0082   rocid_ = rocid;
0083   //std::cout << "PixelROCTrimBits::read rocid_:"<<rocid_<<std::endl;
0084   for (int i = 0; i < 52; i++) {
0085     in >> tag;
0086     //       std::cout << "Now reading col:"<<tag<<std::endl;
0087     std::string data;
0088     in >> data;
0089     //       std::cout <<" data: " <<data<<std::endl;
0090     unsigned char byte = 0;
0091     for (int j = 0; j < 80; j++) {
0092       unsigned char tmp = toupper(data[j]) - 48;
0093       if (tmp > 9)
0094         tmp -= 7;  //FIXME this is so ugly
0095       byte += tmp;
0096       if ((j + 1) % 2 == 0) {
0097         //std::cout << "Writing byte:"<<(int)byte<<std::endl;
0098         bits_[i * 40 + (j + 1) / 2 - 1] = byte;
0099         byte = 0;
0100       } else {
0101         byte *= 16;
0102       }
0103     }
0104   }
0105   return 1;
0106 }
0107 
0108 int PixelROCTrimBits::readBinary(PixelROCName rocid, std::ifstream& in) {
0109   rocid_ = rocid;
0110 
0111   in.read((char*)bits_, 2080);
0112 
0113   return 1;
0114 }
0115 
0116 void PixelROCTrimBits::writeBinary(std::ofstream& out) const {
0117   out << (char)rocid_.rocname().size();
0118   out.write(rocid_.rocname().c_str(), rocid_.rocname().size());
0119 
0120   //std::cout << "PixelROCTrimBits::writeBinary:"<<rocid_.rocname().size()
0121   // << " " <<rocid_.rocname()<<std::endl;
0122 
0123   for (unsigned int i = 0; i < 2080; i++) {
0124     out << bits_[i];
0125   }
0126 }
0127 
0128 void PixelROCTrimBits::writeASCII(std::ofstream& out) const {
0129   //std::cout << " PixelROCTrimBits::writeASCII rocid_.rocname():"<<rocid_.rocname()<<std::endl;
0130 
0131   out << "ROC:     " << rocid_.rocname() << std::endl;
0132 
0133   //std::cout << "PixelROCTrimBits::writeBinary:"<<rocid_.rocname().size()
0134   //     << " " <<rocid_.rocname()<<std::endl;
0135 
0136   for (unsigned int col = 0; col < 52; col++) {
0137     out << "col";
0138     if (col < 10)
0139       out << "0";
0140     out << col << ":   ";
0141     for (int row = 0; row < 80; row++) {
0142       out << std::hex << std::uppercase << trim(col, row) << std::dec;
0143     }
0144     out << std::endl;
0145   }
0146 }
0147 
0148 unsigned int PixelROCTrimBits::trim(unsigned int col, unsigned int row) const {
0149   unsigned int tmp = bits_[col * 40 + row / 2];
0150   if (row % 2 == 0)
0151     tmp /= 16;
0152   return tmp & 0x0F;
0153 }
0154 
0155 void PixelROCTrimBits::setTrim(unsigned int col, unsigned int row, unsigned int trim) {
0156   assert(trim < 16);
0157 
0158   unsigned int mask = 0xf0;
0159   if (row % 2 == 0) {
0160     trim <<= 4;
0161     mask >>= 4;
0162   }
0163   unsigned int tmp = bits_[col * 40 + row / 2];
0164   bits_[col * 40 + row / 2] = (tmp & mask) | trim;
0165 }
0166 
0167 std::ostream& pos::operator<<(std::ostream& s, const PixelROCTrimBits& mask) {
0168   s << "Dumping ROC masks" << std::endl;
0169 
0170   for (int i = 0; i < 52; i++) {
0171     s << "Col" << i << ": ";
0172     for (int j = 0; j < 40; j++) {
0173       unsigned char bitmask = 15 * 16;
0174       for (int k = 0; k < 2; k++) {
0175         unsigned int tmp = mask.bits_[i * 40 + j] & bitmask;
0176         if (tmp > 15)
0177           tmp /= 16;
0178         s << std::hex << tmp << std::dec;
0179         bitmask /= 16;
0180       }
0181     }
0182     s << std::endl;
0183   }
0184 
0185   return s;
0186 }
0187 
0188 //=============================================================================================
0189 void PixelROCTrimBits::writeXML(std::ofstream* out) const {
0190   std::string mthn = "[PixelROCTrimBits::writeXML()]\t\t\t\t";
0191 
0192   std::string encoded = base64_encode(bits_, sizeof(bits_));
0193   std::string decoded = base64_decode(encoded);
0194 
0195   *out << "  <DATA>" << std::endl;
0196   *out << "   <ROC_NAME>" << rocid_.rocname() << "</ROC_NAME>" << std::endl;
0197   *out << "   <TRIM_BITS>" << encoded << "</TRIM_BITS>" << std::endl;
0198   *out << "  </DATA>" << std::endl;
0199   *out << " " << std::endl;
0200 }