Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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