Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
//
// This class provide the data structure for the
// ROC DAC parameters
//
// At this point I do not see a reason to make an
// abstract layer for this code.
//

#include "CalibFormats/SiPixelObjects/interface/PixelROCTrimBits.h"
#include "CalibFormats/SiPixelObjects/interface/PixelBase64.h"
#include <iostream>
#include <sstream>
#include <cassert>
#include <typeinfo>

using namespace pos;

PixelROCTrimBits::PixelROCTrimBits() {}
//This part has been modified from the orignal
void PixelROCTrimBits::setROCTrimBits(PixelROCName rocid, std::string bits) {
  rocid_ = rocid;
  char cpt[2080];
  bits.copy(cpt, 2080);
  for (unsigned int i = 0; i < bits.size(); i++)
    bits_[i] = static_cast<unsigned char>(cpt[i]);
}
//End of part modified

// Added by Dario: handles the base_64-decoded strings from aDB read
int PixelROCTrimBits::read(const PixelROCName rocid, std::string in) {
  rocid_ = rocid;
  for (int i = 0; i < (int)sizeof(bits_); i++) {
    bits_[i] = in.at(i);
  }
  return 1;
}

int PixelROCTrimBits::read(PixelROCName rocid, std::ifstream& in) {
  std::string tag;

  //std::cout << "PixelROCTrimBits::read rocid:"<<rocid<<std::endl;

  rocid_ = rocid;

  //std::cout << "PixelROCTrimBits::read rocid_:"<<rocid_<<std::endl;

  for (int i = 0; i < 52; i++) {
    in >> tag;

    //std::cout << "Now reading col:"<<tag<<std::endl;

    std::string data;

    in >> data;

    //std::cout <<"data.size()" <<data.size()<<std::endl;

    unsigned char byte = 0;

    for (int j = 0; j < 80; j++) {
      unsigned char tmp = toupper(data[j]) - 48;
      if (tmp > 9)
        tmp -= 7;  //FIXME this is so ugly

      byte += tmp;

      if ((j + 1) % 2 == 0) {
        //std::cout << "Writing byte:"<<(int)byte<<std::endl;
        bits_[i * 40 + (j + 1) / 2 - 1] = byte;
        byte = 0;
      } else {
        byte *= 16;
      }
    }
  }
  return 1;
}

int PixelROCTrimBits::read(PixelROCName rocid, std::istringstream& in) {
  std::string tag;
  //std::cout << "PixelROCTrimBits::read rocid:"<<rocid<<std::endl;
  rocid_ = rocid;
  //std::cout << "PixelROCTrimBits::read rocid_:"<<rocid_<<std::endl;
  for (int i = 0; i < 52; i++) {
    in >> tag;
    //       std::cout << "Now reading col:"<<tag<<std::endl;
    std::string data;
    in >> data;
    //       std::cout <<" data: " <<data<<std::endl;
    unsigned char byte = 0;
    for (int j = 0; j < 80; j++) {
      unsigned char tmp = toupper(data[j]) - 48;
      if (tmp > 9)
        tmp -= 7;  //FIXME this is so ugly
      byte += tmp;
      if ((j + 1) % 2 == 0) {
        //std::cout << "Writing byte:"<<(int)byte<<std::endl;
        bits_[i * 40 + (j + 1) / 2 - 1] = byte;
        byte = 0;
      } else {
        byte *= 16;
      }
    }
  }
  return 1;
}

int PixelROCTrimBits::readBinary(PixelROCName rocid, std::ifstream& in) {
  rocid_ = rocid;

  in.read((char*)bits_, 2080);

  return 1;
}

void PixelROCTrimBits::writeBinary(std::ofstream& out) const {
  out << (char)rocid_.rocname().size();
  out.write(rocid_.rocname().c_str(), rocid_.rocname().size());

  //std::cout << "PixelROCTrimBits::writeBinary:"<<rocid_.rocname().size()
  // << " " <<rocid_.rocname()<<std::endl;

  for (unsigned int i = 0; i < 2080; i++) {
    out << bits_[i];
  }
}

void PixelROCTrimBits::writeASCII(std::ofstream& out) const {
  //std::cout << " PixelROCTrimBits::writeASCII rocid_.rocname():"<<rocid_.rocname()<<std::endl;

  out << "ROC:     " << rocid_.rocname() << std::endl;

  //std::cout << "PixelROCTrimBits::writeBinary:"<<rocid_.rocname().size()
  //	 << " " <<rocid_.rocname()<<std::endl;

  for (unsigned int col = 0; col < 52; col++) {
    out << "col";
    if (col < 10)
      out << "0";
    out << col << ":   ";
    for (int row = 0; row < 80; row++) {
      out << std::hex << std::uppercase << trim(col, row) << std::dec;
    }
    out << std::endl;
  }
}

unsigned int PixelROCTrimBits::trim(unsigned int col, unsigned int row) const {
  unsigned int tmp = bits_[col * 40 + row / 2];
  if (row % 2 == 0)
    tmp /= 16;
  return tmp & 0x0F;
}

void PixelROCTrimBits::setTrim(unsigned int col, unsigned int row, unsigned int trim) {
  assert(trim < 16);

  unsigned int mask = 0xf0;
  if (row % 2 == 0) {
    trim <<= 4;
    mask >>= 4;
  }
  unsigned int tmp = bits_[col * 40 + row / 2];
  bits_[col * 40 + row / 2] = (tmp & mask) | trim;
}

std::ostream& pos::operator<<(std::ostream& s, const PixelROCTrimBits& mask) {
  s << "Dumping ROC masks" << std::endl;

  for (int i = 0; i < 52; i++) {
    s << "Col" << i << ": ";
    for (int j = 0; j < 40; j++) {
      unsigned char bitmask = 15 * 16;
      for (int k = 0; k < 2; k++) {
        unsigned int tmp = mask.bits_[i * 40 + j] & bitmask;
        if (tmp > 15)
          tmp /= 16;
        s << std::hex << tmp << std::dec;
        bitmask /= 16;
      }
    }
    s << std::endl;
  }

  return s;
}

//=============================================================================================
void PixelROCTrimBits::writeXML(std::ofstream* out) const {
  std::string mthn = "[PixelROCTrimBits::writeXML()]\t\t\t\t";

  std::string encoded = base64_encode(bits_, sizeof(bits_));
  std::string decoded = base64_decode(encoded);

  *out << "  <DATA>" << std::endl;
  *out << "   <ROC_NAME>" << rocid_.rocname() << "</ROC_NAME>" << std::endl;
  *out << "   <TRIM_BITS>" << encoded << "</TRIM_BITS>" << std::endl;
  *out << "  </DATA>" << std::endl;
  *out << " " << std::endl;
}