Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 //
0002 // This class provide a base class for the
0003 // pixel mask data for the pixel FEC configuration
0004 // This is a pure interface (abstract class) that
0005 // needs to have an implementation.
0006 //
0007 // All applications should just use this
0008 // interface and not care about the specific
0009 // implementation
0010 //
0011 //
0012 #include <sstream>
0013 #include "CalibFormats/SiPixelObjects/interface/PixelModuleName.h"
0014 #include "CalibFormats/SiPixelObjects/interface/PixelMaskAllPixels.h"
0015 #include "CalibFormats/SiPixelObjects/interface/PixelTimeFormatter.h"
0016 #include "CalibFormats/SiPixelObjects/interface/PixelBase64.h"
0017 #include <fstream>
0018 #include <map>
0019 #include <iostream>
0020 #include <cassert>
0021 #include <stdexcept>
0022 
0023 using namespace pos;
0024 using namespace std;
0025 
0026 //================================================================================================================
0027 PixelMaskAllPixels::PixelMaskAllPixels(std::vector<std::vector<std::string> > &tableMat) : PixelMaskBase("", "", "") {
0028   std::string mthn = "[PixelMaskAllPixels::PixelMaskAllPixels()]\t\t    ";
0029   //std::cout << __LINE__ << "]\t" << mthn << "Table Size in const: " << tableMat.size() << std::endl;
0030 
0031   std::vector<std::string> ins = tableMat[0];
0032   std::map<std::string, int> colM;
0033   std::vector<std::string> colNames;
0034 
0035   /*
0036  EXTENSION_TABLE_NAME: ROC_MASKS (VIEW: CONF_KEY_ROC_MASKS_V)
0037 
0038  CONFIG_KEY                NOT NULL VARCHAR2(80)
0039  KEY_TYPE                  NOT NULL VARCHAR2(80)
0040  KEY_ALIAS                 NOT NULL VARCHAR2(80)
0041  VERSION                        VARCHAR2(40)
0042  KIND_OF_COND                  NOT NULL VARCHAR2(40)
0043  ROC_NAME                  NOT NULL VARCHAR2(200)
0044  KILL_MASK                 NOT NULL VARCHAR2(4000) colNames.push_back("CONFIG_KEY_ID" );
0045 */
0046   colNames.push_back("CONFIG_KEY");
0047   colNames.push_back("KEY_TYPE");
0048   colNames.push_back("KEY_ALIAS");
0049   colNames.push_back("VERSION");
0050   colNames.push_back("KIND_OF_COND");
0051   colNames.push_back("ROC_NAME");
0052   colNames.push_back("KILL_MASK");
0053 
0054   for (unsigned int c = 0; c < ins.size(); c++) {
0055     for (unsigned int n = 0; n < colNames.size(); n++) {
0056       if (tableMat[0][c] == colNames[n]) {
0057         colM[colNames[n]] = c;
0058         break;
0059       }
0060     }
0061   }  //end for
0062   for (unsigned int n = 0; n < colNames.size(); n++) {
0063     if (colM.find(colNames[n]) == colM.end()) {
0064       std::cerr << mthn << "Couldn't find in the database the column with name " << colNames[n] << std::endl;
0065       assert(0);
0066     }
0067   }
0068   maskbits_.clear();
0069   for (unsigned int r = 1; r < tableMat.size(); r++) {  //Goes to every row of the Matrix
0070     std::string currentRocName = tableMat[r][colM["ROC_NAME"]];
0071     PixelROCName rocid(currentRocName);
0072     PixelROCMaskBits tmp;
0073     tmp.read(rocid,
0074              base64_decode(tableMat[r][colM["KILL_MASK"]]));  // decode back from specially base64-encoded data for XML
0075     maskbits_.push_back(tmp);
0076   }  //end for r
0077 }
0078 
0079 //================================================================================================================
0080 // modified by MR on 18-04-2008 10:02:00
0081 PixelMaskAllPixels::PixelMaskAllPixels() : PixelMaskBase("", "", "") { ; }
0082 
0083 //================================================================================================================
0084 void PixelMaskAllPixels::addROCMaskBits(const PixelROCMaskBits &bits) { maskbits_.push_back(bits); }
0085 
0086 //================================================================================================================
0087 PixelMaskAllPixels::PixelMaskAllPixels(std::string filename) : PixelMaskBase("", "", "") {
0088   std::string mthn = "[PixelMaskAllPixels::PixelMaskAllPixels()]\t\t    ";
0089 
0090   if (filename[filename.size() - 1] == 't') {
0091     std::ifstream in(filename.c_str());
0092 
0093     if (!in.good()) {
0094       std::cout << __LINE__ << "]\t" << mthn << "Could not open: " << filename << std::endl;
0095       throw std::runtime_error("Failed to open file " + filename);
0096     }
0097 
0098     std::string tag;
0099     in >> tag;
0100 
0101     maskbits_.clear();
0102 
0103     while (!in.eof()) {
0104       PixelROCName rocid(in);
0105 
0106       PixelROCMaskBits tmp;
0107 
0108       tmp.read(rocid, in);
0109 
0110       maskbits_.push_back(tmp);
0111 
0112       in >> tag;
0113     }
0114 
0115     in.close();
0116 
0117   } else {
0118     std::ifstream in(filename.c_str(), std::ios::binary);
0119 
0120     char nchar;
0121 
0122     in.read(&nchar, 1);
0123 
0124     //in >> nchar;
0125 
0126     std::string s1;
0127 
0128     //wrote these lines of code without ref. needs to be fixed
0129     for (int i = 0; i < nchar; i++) {
0130       char c;
0131       in >> c;
0132       s1.push_back(c);
0133     }
0134 
0135     //std::cout << __LINE__ << "]\t" << mthn << "READ ROC name: "  << s1    << std::endl;
0136 
0137     maskbits_.clear();
0138 
0139     while (!in.eof()) {
0140       //std::cout << __LINE__ << "]\t" << mthn << "read s1: "    << s1    << std::endl;
0141 
0142       PixelROCName rocid(s1);
0143 
0144       //std::cout << __LINE__ << "]\t" << mthn << "read rocid: " << rocid << std::endl;
0145 
0146       PixelROCMaskBits tmp;
0147 
0148       tmp.readBinary(rocid, in);
0149 
0150       maskbits_.push_back(tmp);
0151 
0152       in.read(&nchar, 1);
0153 
0154       s1.clear();
0155 
0156       if (in.eof())
0157         continue;
0158 
0159       //std::cout << __LINE__ << "]\t" << mthn << "Will read: " << (int)nchar << " characters." <<std::endl;
0160 
0161       //wrote these lines of code without ref. needs to be fixed
0162       for (int i = 0; i < nchar; i++) {
0163         char c;
0164         in >> c;
0165         //std::cout << " " <<c;
0166         s1.push_back(c);
0167       }
0168       //std::cout << std::endl;
0169     }
0170 
0171     in.close();
0172   }
0173 
0174   //std::cout << __LINE__ << "]\t" << mthn << "Read maskbits for " << maskbits_.size() << " ROCs" << std::endl;
0175 }
0176 
0177 //================================================================================================================
0178 const PixelROCMaskBits &PixelMaskAllPixels::getMaskBits(int ROCId) const { return maskbits_[ROCId]; }
0179 
0180 //================================================================================================================
0181 PixelROCMaskBits *PixelMaskAllPixels::getMaskBits(PixelROCName name) {
0182   for (unsigned int i = 0; i < maskbits_.size(); i++) {
0183     if (maskbits_[i].name() == name)
0184       return &(maskbits_[i]);
0185   }
0186 
0187   return nullptr;
0188 }
0189 
0190 //================================================================================================================
0191 void PixelMaskAllPixels::writeBinary(std::string filename) const {
0192   std::ofstream out(filename.c_str(), std::ios::binary);
0193 
0194   for (unsigned int i = 0; i < maskbits_.size(); i++) {
0195     maskbits_[i].writeBinary(out);
0196   }
0197 }
0198 
0199 //================================================================================================================
0200 void PixelMaskAllPixels::writeASCII(std::string dir) const {
0201   if (!dir.empty())
0202     dir += "/";
0203   PixelModuleName module(maskbits_[0].name().rocname());
0204   std::string filename = dir + "ROC_Masks_module_" + module.modulename() + ".dat";
0205 
0206   std::ofstream out(filename.c_str());
0207 
0208   for (unsigned int i = 0; i < maskbits_.size(); i++) {
0209     maskbits_[i].writeASCII(out);
0210   }
0211 }
0212 
0213 //=============================================================================================
0214 void PixelMaskAllPixels::writeXMLHeader(pos::PixelConfigKey key,
0215                                         int version,
0216                                         std::string path,
0217                                         std::ofstream *outstream,
0218                                         std::ofstream *out1stream,
0219                                         std::ofstream *out2stream) const {
0220   std::string mthn = "[PixelMaskAllPixels::writeXMLHeader()]\t\t\t    ";
0221   std::stringstream maskFullPath;
0222 
0223   maskFullPath << path << "/Pixel_RocMasks_" << PixelTimeFormatter::getmSecTime() << ".xml";
0224   std::cout << __LINE__ << "]\t" << mthn << "Writing to: " << maskFullPath.str() << std::endl;
0225 
0226   outstream->open(maskFullPath.str().c_str());
0227 
0228   *outstream << "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" << std::endl;
0229   *outstream << "<ROOT xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>" << std::endl;
0230   *outstream << "" << std::endl;
0231   *outstream << " <HEADER>" << std::endl;
0232   *outstream << "  <TYPE>" << std::endl;
0233   *outstream << "   <EXTENSION_TABLE_NAME>ROC_MASKS</EXTENSION_TABLE_NAME>" << std::endl;
0234   *outstream << "   <NAME>ROC Mask Bits</NAME>" << std::endl;
0235   *outstream << "  </TYPE>" << std::endl;
0236   *outstream << "  <RUN>" << std::endl;
0237   *outstream << "   <RUN_TYPE>ROC Mask Bits</RUN_TYPE>" << std::endl;
0238   *outstream << "   <RUN_NUMBER>1</RUN_NUMBER>" << std::endl;
0239   *outstream << "   <RUN_BEGIN_TIMESTAMP>" << PixelTimeFormatter::getTime() << "</RUN_BEGIN_TIMESTAMP>" << std::endl;
0240   *outstream << "   <LOCATION>CERN P5</LOCATION>" << std::endl;
0241   *outstream << "  </RUN>" << std::endl;
0242   *outstream << " </HEADER>" << std::endl;
0243   *outstream << "" << std::endl;
0244   *outstream << " <DATA_SET>" << std::endl;
0245   *outstream << "" << std::endl;
0246   *outstream << "  <VERSION>" << version << "</VERSION>" << std::endl;
0247   *outstream << "  <COMMENT_DESCRIPTION>" << getComment() << "</COMMENT_DESCRIPTION>" << std::endl;
0248   *outstream << "  <CREATED_BY_USER>" << getAuthor() << "</CREATED_BY_USER>" << std::endl;
0249   *outstream << "" << std::endl;
0250   *outstream << "  <PART>" << std::endl;
0251   *outstream << "   <NAME_LABEL>CMS-PIXEL-ROOT</NAME_LABEL>" << std::endl;
0252   *outstream << "   <KIND_OF_PART>Detector ROOT</KIND_OF_PART>" << std::endl;
0253   *outstream << "  </PART>" << std::endl;
0254   *outstream << "  " << std::endl;
0255 }
0256 //=============================================================================================
0257 void PixelMaskAllPixels::writeXML(std::ofstream *outstream,
0258                                   std::ofstream *out1stream,
0259                                   std::ofstream *out2stream) const {
0260   std::string mthn = "[PixelMaskAllPixels::writeXML()]\t\t\t    ";
0261 
0262   for (unsigned int i = 0; i < maskbits_.size(); i++) {
0263     maskbits_[i].writeXML(outstream);
0264   }
0265 }
0266 //=============================================================================================
0267 void PixelMaskAllPixels::writeXMLTrailer(std::ofstream *outstream,
0268                                          std::ofstream *out1stream,
0269                                          std::ofstream *out2stream) const {
0270   std::string mthn = "[PixelMaskAllPixels::writeXMLTrailer()]\t\t\t    ";
0271 
0272   *outstream << "  " << std::endl;
0273   *outstream << " </DATA_SET>" << std::endl;
0274   *outstream << "</ROOT>" << std::endl;
0275 
0276   outstream->close();
0277   std::cout << __LINE__ << "]\t" << mthn << "Data written " << std::endl;
0278 }