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/PixelROCMaskBits.h"
0010 #include "CalibFormats/SiPixelObjects/interface/PixelBase64.h"
0011 #include <iostream>
0012 #include <cassert>
0013 #include <typeinfo>
0014 
0015 using namespace pos;
0016 
0017 //=====================================================================/
0018 PixelROCMaskBits::PixelROCMaskBits() {}
0019 
0020 /**********************Start Modification******************************/
0021 void PixelROCMaskBits::setROCMaskBits(PixelROCName& rocid, std::string bits) {
0022   std::string mthn = "[PixelROCMaskBits::setROCMaskBits()]\t\t\t    ";
0023   rocid_ = rocid;
0024   char cpt[520];
0025   bits.copy(cpt, 520);
0026   for (unsigned int i = 0; i < bits.size(); i++) {
0027     bits_[i] = static_cast<unsigned char>(cpt[i]);
0028     //      std::cout<< "bits_[" << i << "]\t" << bits_[i] <<std::endl;
0029     //      std::cout<<rocid_<<std::endl;
0030     //      std::cout.flags(std::ios::hex)
0031   }
0032 }
0033 /**********************End Modification******************************/
0034 
0035 // Added by Dario: handles the base_64-decoded strings from aDB read
0036 int PixelROCMaskBits::read(const PixelROCName& rocid, std::string in) {
0037   rocid_ = rocid;
0038   for (int i = 0; i < (int)sizeof(bits_); i++) {
0039     bits_[i] = in.at(i);
0040   }
0041   return 1;
0042 }
0043 
0044 //=====================================================================/
0045 int PixelROCMaskBits::read(const PixelROCName& rocid, std::ifstream& in) {
0046   rocid_ = rocid;
0047 
0048   std::string tag;
0049 
0050   for (int i = 0; i < 52; i++) {
0051     in >> tag;
0052 
0053     //std::cout << "Now reading col:"<<tag<<std::endl;
0054 
0055     std::string data;
0056 
0057     in >> data;
0058 
0059     //std::cout <<"data.size()" <<data.size()<<std::endl;
0060 
0061     unsigned char byte = 0;
0062 
0063     for (int j = 0; j < 80; j++) {
0064       if (data[j] == '1')
0065         byte += 128;
0066 
0067       if ((j + 1) % 8 == 0) {
0068         //std::cout << "Writing byte:"<<(int)byte<<std::endl;
0069         bits_[i * 10 + (j + 1) / 8 - 1] = byte;
0070         byte = 0;
0071       } else {
0072         byte /= 2;
0073       }
0074     }
0075   }
0076 
0077   return 1;
0078 }
0079 
0080 //=====================================================================/
0081 // modified by MR on 23-06-2008 11:57:58
0082 int PixelROCMaskBits::read(const PixelROCName& rocid, std::istringstream& in) {
0083   rocid_ = rocid;
0084   std::string tag;
0085   for (int i = 0; i < 52; i++) {
0086     in >> tag;
0087     //std::cout << "Now reading col:"<<tag<<std::endl;
0088     std::string data;
0089     in >> data;
0090     //std::cout <<"data.size()" <<data.size()<<std::endl;
0091     unsigned char byte = 0;
0092     for (int j = 0; j < 80; j++) {
0093       if (data[j] == '1')
0094         byte += 128;
0095       if ((j + 1) % 8 == 0) {
0096         //std::cout << "Writing byte:"<<(int)byte<<std::endl;
0097         bits_[i * 10 + (j + 1) / 8 - 1] = byte;
0098         byte = 0;
0099       } else {
0100         byte /= 2;
0101       }
0102     }
0103   }
0104   return 1;
0105 }
0106 
0107 //=====================================================================/
0108 int PixelROCMaskBits::readBinary(const PixelROCName& rocid, std::ifstream& in) {
0109   rocid_ = rocid;
0110 
0111   in.read((char*)bits_, 520);
0112 
0113   return 1;
0114 }
0115 
0116 //=====================================================================/
0117 void PixelROCMaskBits::writeBinary(std::ofstream& out) const {
0118   out << (char)rocid_.rocname().size();
0119   out.write(rocid_.rocname().c_str(), rocid_.rocname().size());
0120 
0121   for (unsigned int i = 0; i < 520; i++) {
0122     out << bits_[i];
0123   }
0124 }
0125 
0126 //=====================================================================/
0127 void PixelROCMaskBits::writeASCII(std::ofstream& out) const {
0128   out << "ROC:    " << rocid_.rocname() << std::endl;
0129 
0130   for (unsigned int col = 0; col < 52; col++) {
0131     out << "col";
0132     if (col < 10)
0133       out << "0";
0134     out << col << ":  ";
0135     for (int row = 0; row < 80; row++) {
0136       out << mask(col, row);
0137     }
0138     out << std::endl;
0139   }
0140 }
0141 
0142 //=====================================================================/
0143 unsigned int PixelROCMaskBits::mask(unsigned int col, unsigned int row) const {
0144   unsigned int tmp = bits_[col * 10 + row / 8];
0145   //   std::cout << "c =  " << col << "\tr = " << row << "\tbits_[" << (col*10+row/8) << "]=" << bits_[col*10+row/8] << std::endl ;
0146   //   std::cout << "[PixelROCMaskBits::mask()] tmp iniziale " << tmp      << std::endl ;
0147   tmp = tmp >> (row % 8);
0148   //   std::cout << "[PixelROCMaskBits::mask()] tmp finale   " << tmp      << std::endl ;
0149   //   unsigned int res = tmp&0x01 ;
0150   //   std::cout << "[PixelROCMaskBits::mask()] return value " << res      << std::endl ;
0151   return tmp & 0x01;
0152 }
0153 
0154 //=====================================================================/
0155 void PixelROCMaskBits::setMask(unsigned int col, unsigned int row, unsigned int mask) {
0156   assert(mask == 0 || mask == 1);
0157 
0158   unsigned int bit = 1 << (row % 8);
0159   if (mask)
0160     bits_[col * 10 + row / 8] = bits_[col * 10 + row / 8] | bit;
0161   if (!mask)
0162     bits_[col * 10 + row / 8] = bits_[col * 10 + row / 8] & (0xff ^ bit);
0163 }
0164 
0165 //=====================================================================/
0166 std::ostream& pos::operator<<(std::ostream& s, const PixelROCMaskBits& mask) {
0167   s << "Dumping ROC masks" << std::endl;
0168 
0169   for (int i = 0; i < 52; i++) {
0170     s << "Col" << i << ":";
0171     for (int j = 0; j < 10; j++) {
0172       unsigned char bitmask = 1;
0173       for (int k = 0; k < 8; k++) {
0174         if (mask.bits_[i * 10 + j] & bitmask) {
0175           s << "1";
0176         } else {
0177           s << "0";
0178         }
0179         bitmask *= 2;
0180       }
0181     }
0182     s << std::endl;
0183   }
0184 
0185   return s;
0186 }
0187 
0188 //=============================================================================================
0189 void PixelROCMaskBits::writeXML(std::ofstream* out) const {
0190   std::string mthn = "[PixelROCMaskBits::writeXML()]\t\t\t\t";
0191 
0192   std::string encoded = base64_encode(bits_, sizeof(bits_));
0193 
0194   *out << "  <DATA>" << std::endl;
0195   *out << "   <ROC_NAME>" << rocid_.rocname() << "</ROC_NAME>" << std::endl;
0196   *out << "   <KILL_MASK>" << encoded << "</KILL_MASK>" << std::endl;
0197   *out << "  </DATA>" << std::endl;
0198   *out << " " << std::endl;
0199 }