Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 //
0002 // This class stores the information about a FEC.
0003 // This include the number, crate, and base address
0004 //
0005 //
0006 
0007 #include "CalibFormats/SiPixelObjects/interface/PixelFECConfig.h"
0008 #include "CalibFormats/SiPixelObjects/interface/PixelTimeFormatter.h"
0009 #include <fstream>
0010 #include <sstream>
0011 #include <map>
0012 #include <cassert>
0013 #include <stdexcept>
0014 
0015 using namespace pos;
0016 using namespace std;
0017 
0018 PixelFECConfig::PixelFECConfig(std::vector<std::vector<std::string> > &tableMat) : PixelConfigBase(" ", " ", " ") {
0019   std::map<std::string, int> colM;
0020   std::vector<std::string> colNames;
0021   /**
0022  
0023    EXTENSION_TABLE_NAME:  (VIEW: )
0024    
0025    CONFIG_KEY                    NOT NULL VARCHAR2(80)
0026    KEY_TYPE                  NOT NULL VARCHAR2(80)
0027    KEY_ALIAS                     NOT NULL VARCHAR2(80)
0028    VERSION                        VARCHAR2(40)
0029    KIND_OF_COND                  NOT NULL VARCHAR2(40)
0030    PIXFEC_NAME                   NOT NULL VARCHAR2(200)
0031    CRATE_NUMBER                  NOT NULL NUMBER(38)
0032    SLOT_NUMBER                        NUMBER(38)
0033    VME_ADDR                  NOT NULL VARCHAR2(200)
0034  */
0035 
0036   colNames.push_back("CONFIG_KEY");
0037   colNames.push_back("KEY_TYPE");
0038   colNames.push_back("KEY_ALIAS");
0039   colNames.push_back("VERSION");
0040   colNames.push_back("KIND_OF_COND");
0041   colNames.push_back("PIXFEC_NAME");
0042   colNames.push_back("CRATE_NUMBER");
0043   colNames.push_back("SLOT_NUMBER");
0044   colNames.push_back("VME_ADDR");
0045 
0046   for (unsigned int c = 0; c < tableMat[0].size(); c++) {
0047     for (unsigned int n = 0; n < colNames.size(); n++) {
0048       if (tableMat[0][c] == colNames[n]) {
0049         colM[colNames[n]] = c;
0050         break;
0051       }
0052     }
0053   }  //end for
0054   /*
0055  for(unsigned int n=0; n<colNames.size(); n++)
0056    {
0057      if(colM.find(colNames[n]) == colM.end()){
0058        std::cerr << "[PixelFECConfig::PixelFECConfig()]\tCouldn't find in the database the column with name " << colNames[n] << std::endl;
0059        assert(0);
0060      }
0061    }
0062  */
0063 
0064   fecconfig_.clear();
0065   for (unsigned int r = 1; r < tableMat.size(); r++)  //Goes to every row of the Matrix
0066   {
0067     unsigned int fecnumber;
0068     unsigned int crate;
0069     unsigned int vme_base_address;
0070 
0071     //      01234567890123
0072     //      BPix_Pxl_FEC_1
0073     //     string fullFECName = tableMat[r][colM["PIXEL_FEC"]] ;
0074     //     fullFECName.replace(0,13,"") ;
0075     fecnumber = atoi(tableMat[r][colM["PIXFEC_NAME"]].c_str());
0076     crate = atoi(tableMat[r][colM["CRATE_NUMBER"]].c_str());
0077     string hexVMEAddr = tableMat[r][colM["VME_ADDR"]];
0078     sscanf(hexVMEAddr.c_str(), "%x", &vme_base_address);
0079     PixelFECParameters tmp;
0080 
0081     tmp.setFECParameters(fecnumber, crate, vme_base_address);
0082 
0083     fecconfig_.push_back(tmp);
0084   }
0085 
0086 }  // end contructor
0087 
0088 //****************************************************************************************
0089 
0090 PixelFECConfig::PixelFECConfig(std::string filename) : PixelConfigBase(" ", " ", " ") {
0091   std::string mthn = "[[PixelFECConfig::PixelFECConfig()]\t\t\t   ";
0092 
0093   std::ifstream in(filename.c_str());
0094 
0095   if (!in.good()) {
0096     std::cout << __LINE__ << "]\t" << mthn << "Could not open: " << filename << std::endl;
0097     throw std::runtime_error("Failed to open file " + filename);
0098   } else {
0099     std::cout << __LINE__ << "]\t" << mthn << " Opened: " << filename << std::endl;
0100   }
0101 
0102   std::string dummy;
0103 
0104   in >> dummy;
0105   in >> dummy;
0106   in >> dummy;
0107   in >> dummy;
0108   in >> dummy;
0109   in >> dummy;
0110 
0111   do {
0112     unsigned int fecnumber;
0113     unsigned int crate;
0114     unsigned int vme_base_address;
0115 
0116     in >> fecnumber >> crate >> std::hex >> vme_base_address >> std::dec;
0117 
0118     if (!in.eof()) {
0119       //std::cout << __LINE__ << "]\t" << mthn << fecnumber <<" "<< crate << " "
0120       //      << std::hex << vme_base_address<<std::dec<<std::endl;
0121 
0122       PixelFECParameters tmp;
0123 
0124       tmp.setFECParameters(fecnumber, crate, vme_base_address);
0125 
0126       fecconfig_.push_back(tmp);
0127     }
0128 
0129   } while (!in.eof());
0130   in.close();
0131 }
0132 
0133 //std::ostream& operator<<(std::ostream& s, const PixelFECConfig& table){
0134 
0135 //for (unsigned int i=0;i<table.translationtable_.size();i++){
0136 //  s << table.translationtable_[i]<<std::endl;
0137 //   }
0138 // return s;
0139 
0140 //}
0141 
0142 unsigned int PixelFECConfig::getNFECBoards() const { return fecconfig_.size(); }
0143 
0144 unsigned int PixelFECConfig::getFECNumber(unsigned int i) const {
0145   assert(i < fecconfig_.size());
0146   return fecconfig_[i].getFECNumber();
0147 }
0148 
0149 unsigned int PixelFECConfig::getCrate(unsigned int i) const {
0150   assert(i < fecconfig_.size());
0151   return fecconfig_[i].getCrate();
0152 }
0153 
0154 unsigned int PixelFECConfig::getVMEBaseAddress(unsigned int i) const {
0155   assert(i < fecconfig_.size());
0156   return fecconfig_[i].getVMEBaseAddress();
0157 }
0158 
0159 unsigned int PixelFECConfig::crateFromFECNumber(unsigned int fecnumber) const {
0160   std::string mthn = "[PixelFECConfig::crateFromFECNumber()]\t\t\t    ";
0161   for (unsigned int i = 0; i < fecconfig_.size(); i++) {
0162     if (fecconfig_[i].getFECNumber() == fecnumber)
0163       return fecconfig_[i].getCrate();
0164   }
0165 
0166   std::cout << __LINE__ << "]\t" << mthn << "Could not find FEC number: " << fecnumber << std::endl;
0167 
0168   assert(0);
0169 
0170   return 0;
0171 }
0172 
0173 unsigned int PixelFECConfig::VMEBaseAddressFromFECNumber(unsigned int fecnumber) const {
0174   std::string mthn = "[PixelFECConfig::VMEBaseAddressFromFECNumber()]\t\t    ";
0175   for (unsigned int i = 0; i < fecconfig_.size(); i++) {
0176     if (fecconfig_[i].getFECNumber() == fecnumber)
0177       return fecconfig_[i].getVMEBaseAddress();
0178   }
0179 
0180   std::cout << __LINE__ << "]\t" << mthn << "Could not find FEC number: " << fecnumber << std::endl;
0181 
0182   assert(0);
0183 
0184   return 0;
0185 }
0186 
0187 //=============================================================================================
0188 void PixelFECConfig::writeASCII(std::string dir) const {
0189   if (!dir.empty())
0190     dir += "/";
0191   std::string filename = dir + "fecconfig.dat";
0192   std::ofstream out(filename.c_str());
0193 
0194   std::vector<PixelFECParameters>::const_iterator i = fecconfig_.begin();
0195 
0196   out << "#FEC number     crate     vme base address" << endl;
0197   for (; i != fecconfig_.end(); ++i) {
0198     out << i->getFECNumber() << "               " << i->getCrate() << "         "
0199         << "0x" << hex << i->getVMEBaseAddress() << dec << endl;
0200   }
0201 }
0202 
0203 //=============================================================================================
0204 void PixelFECConfig::writeXMLHeader(pos::PixelConfigKey key,
0205                                     int version,
0206                                     std::string path,
0207                                     std::ofstream *outstream,
0208                                     std::ofstream *out1stream,
0209                                     std::ofstream *out2stream) const {
0210   std::string mthn = "[PixelFECConfig::writeXMLHeader()]\t\t\t    ";
0211   std::stringstream fullPath;
0212   fullPath << path << "/Pixel_PixelFecParameters_" << PixelTimeFormatter::getmSecTime() << ".xml";
0213   cout << __LINE__ << "]\t" << mthn << "Writing to: " << fullPath.str() << endl;
0214 
0215   outstream->open(fullPath.str().c_str());
0216 
0217   *outstream << "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" << endl;
0218   *outstream << "<ROOT xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>" << endl;
0219   *outstream << " <HEADER>" << endl;
0220   *outstream << "  <TYPE>" << endl;
0221   *outstream << "   <EXTENSION_TABLE_NAME>PIXEL_FEC_PARAMETERS</EXTENSION_TABLE_NAME>" << endl;
0222   *outstream << "   <NAME>Pixel FEC Parameters</NAME>" << endl;
0223   *outstream << "  </TYPE>" << endl;
0224   *outstream << "  <RUN>" << endl;
0225   *outstream << "   <RUN_TYPE>Pixel FEC Parameters</RUN_TYPE>" << endl;
0226   *outstream << "   <RUN_NUMBER>1</RUN_NUMBER>" << endl;
0227   *outstream << "   <RUN_BEGIN_TIMESTAMP>" << pos::PixelTimeFormatter::getTime() << "</RUN_BEGIN_TIMESTAMP>" << endl;
0228   *outstream << "   <LOCATION>CERN P5</LOCATION>" << endl;
0229   *outstream << "  </RUN>" << endl;
0230   *outstream << " </HEADER>" << endl;
0231   *outstream << "" << endl;
0232   *outstream << " <DATA_SET>" << endl;
0233   *outstream << "  <PART>" << endl;
0234   *outstream << "   <NAME_LABEL>CMS-PIXEL-ROOT</NAME_LABEL>" << endl;
0235   *outstream << "   <KIND_OF_PART>Detector ROOT</KIND_OF_PART>" << endl;
0236   *outstream << "  </PART>" << endl;
0237   *outstream << "  <VERSION>" << version << "</VERSION>" << endl;
0238   *outstream << "  <COMMENT_DESCRIPTION>" << getComment() << "</COMMENT_DESCRIPTION>" << endl;
0239   *outstream << "  <CREATED_BY_USER>" << getAuthor() << "</CREATED_BY_USER>" << endl;
0240 }
0241 
0242 //=============================================================================================
0243 void PixelFECConfig::writeXML(std::ofstream *outstream, std::ofstream *out1stream, std::ofstream *out2stream) const {
0244   std::string mthn = "[PixelFECConfig::writeXML()]\t\t\t    ";
0245 
0246   std::vector<PixelFECParameters>::const_iterator i = fecconfig_.begin();
0247 
0248   for (; i != fecconfig_.end(); ++i) {
0249     *outstream << "" << endl;
0250     *outstream << "  <DATA>" << endl;
0251     *outstream << "   <PIXFEC_NAME>" << i->getFECNumber() << "</PIXFEC_NAME>" << endl;
0252     *outstream << "   <CRATE_NUMBER>" << i->getCrate() << "</CRATE_NUMBER>" << endl;
0253     *outstream << "   <VME_ADDR>0x" << hex << i->getVMEBaseAddress() << dec << "</VME_ADDR>" << endl;
0254     *outstream << "  </DATA>" << endl;
0255   }
0256 }
0257 
0258 //=============================================================================================
0259 void PixelFECConfig::writeXMLTrailer(std::ofstream *outstream,
0260                                      std::ofstream *out1stream,
0261                                      std::ofstream *out2stream) const {
0262   std::string mthn = "[PixelFECConfig::writeXMLTrailer()]\t\t\t    ";
0263 
0264   *outstream << "" << endl;
0265   *outstream << " </DATA_SET>" << endl;
0266   *outstream << "</ROOT> " << endl;
0267 
0268   outstream->close();
0269 }