File indexing completed on 2024-04-06 11:58:17
0001
0002
0003
0004
0005
0006
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
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
0029
0030
0031 }
0032 }
0033
0034
0035
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
0054
0055 std::string data;
0056
0057 in >> data;
0058
0059
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
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
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
0088 std::string data;
0089 in >> data;
0090
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
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
0146
0147 tmp = tmp >> (row % 8);
0148
0149
0150
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 }