Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:58:17

0001 //
0002 // This class provide a base class for the
0003 // pixel ROC DAC data for the pixel FEC configuration
0004 //
0005 //
0006 //
0007 //
0008 
0009 #include "CalibFormats/SiPixelObjects/interface/PixelTBMSettings.h"
0010 #include "CalibFormats/SiPixelObjects/interface/PixelTimeFormatter.h"
0011 #include <fstream>
0012 #include <sstream>
0013 #include <iostream>
0014 #include <ios>
0015 #include <cassert>
0016 #include <stdexcept>
0017 
0018 using namespace pos;
0019 
0020 PixelTBMSettings::PixelTBMSettings(std::vector<std::vector<std::string> > &tableMat) : PixelConfigBase("", "", "") {
0021   std::string mthn = "]\t[PixelTBMSettings::PixelTBMSettings()]\t\t\t    ";
0022   std::vector<std::string> ins = tableMat[0];
0023   std::map<std::string, int> colM;
0024   std::vector<std::string> colNames;
0025 
0026   /**
0027 
0028      EXTENSION_TABLE_NAME:     (VIEW:)
0029 
0030      CONFIG_KEY                    NOT NULL VARCHAR2(80)
0031      KEY_TYPE                      NOT NULL VARCHAR2(80)
0032      KEY_ALIAS                     NOT NULL VARCHAR2(80)
0033      VERSION                        VARCHAR2(40)
0034      KIND_OF_COND                  NOT NULL VARCHAR2(40)
0035      TBM_NAME                       VARCHAR2(200)
0036      MODULE_NAME                   NOT NULL VARCHAR2(200)
0037      HUB_ADDRS                      NUMBER(38)
0038      TBM_MODE                       VARCHAR2(200)
0039      ANLG_INBIAS_ADDR                   NUMBER(38)
0040      ANLG_INBIAS_VAL                   NOT NULL NUMBER(38)
0041      ANLG_OUTBIAS_ADDR                  NUMBER(38)
0042      ANLG_OUTBIAS_VAL                  NOT NULL NUMBER(38)
0043      ANLG_OUTGAIN_ADDR                  NUMBER(38)
0044      ANLG_OUTGAIN_VAL                  NOT NULL NUMBER(38)
0045 
0046      N.B.: Here we should (MUST) get a single row referring to a particula module for a particula version.
0047   */
0048 
0049   colNames.push_back("CONFIG_KEY");
0050   colNames.push_back("KEY_TYPE");
0051   colNames.push_back("KEY_ALIAS");
0052   colNames.push_back("VERSION");
0053   colNames.push_back("KIND_OF_COND");
0054   colNames.push_back("TBM_NAME");
0055   colNames.push_back("MODULE_NAME");
0056   colNames.push_back("HUB_ADDRS");
0057   colNames.push_back("TBM_MODE");
0058   colNames.push_back("ANLG_INBIAS_ADDR");
0059   colNames.push_back("ANLG_INBIAS_VAL");
0060   colNames.push_back("ANLG_OUTBIAS_ADDR");
0061   colNames.push_back("ANLG_OUTBIAS_VAL");
0062   colNames.push_back("ANLG_OUTGAIN_ADDR");
0063   colNames.push_back("ANLG_OUTGAIN_VAL");
0064 
0065   for (unsigned int c = 0; c < ins.size(); c++) {
0066     for (unsigned int n = 0; n < colNames.size(); n++) {
0067       if (tableMat[0][c] == colNames[n]) {
0068         colM[colNames[n]] = c;
0069         break;
0070       }
0071     }
0072   }  //end for
0073   for (unsigned int n = 0; n < colNames.size(); n++) {
0074     if (colM.find(colNames[n]) == colM.end()) {
0075       std::cerr << __LINE__ << mthn << "Couldn't find in the database the column with name " << colNames[n]
0076                 << std::endl;
0077       assert(0);
0078     }
0079   }
0080 
0081   if (tableMat.size() > 1) {
0082     //std::cout << __LINE__ << mthn << "Module from DB: " << tableMat[1][colM["MODULE_NAME"]]<< std::endl ;
0083     PixelROCName tmp(tableMat[1][colM["MODULE_NAME"]]);
0084     rocid_ = tmp;
0085     //std::cout << __LINE__ << mthn << "Built ROCNAME: " << rocid_.rocname()<< std::endl ;
0086 
0087     analogInputBias_ = atoi(tableMat[1][colM["ANLG_INBIAS_VAL"]].c_str());
0088     analogOutputBias_ = atoi(tableMat[1][colM["ANLG_OUTBIAS_VAL"]].c_str());
0089     analogOutputGain_ = atoi(tableMat[1][colM["ANLG_OUTGAIN_VAL"]].c_str());
0090 
0091     if (tableMat[1][colM["TBM_MODE"]] == "SingleMode") {
0092       singlemode_ = true;
0093     } else {
0094       singlemode_ = false;
0095     }
0096   }
0097 }  //end contructor
0098 
0099 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
0100 
0101 PixelTBMSettings::PixelTBMSettings(std::string filename) : PixelConfigBase("", "", "") {
0102   std::string mthn = "]\t[PixelTBMSettings::PixelTBMSettings()]\t\t\t    ";
0103   if (filename[filename.size() - 1] == 't') {
0104     std::ifstream in(filename.c_str());
0105 
0106     if (!in.good()) {
0107       std::cout << __LINE__ << mthn << "Could not open:" << filename << std::endl;
0108       throw std::runtime_error("Failed to open file " + filename);
0109     } else {
0110       // std::cout << "Opened:"<<filename<<std::endl;
0111     }
0112 
0113     std::string tag;
0114 
0115     PixelROCName tmp(in);
0116 
0117     rocid_ = tmp;
0118 
0119     unsigned int tmpint;
0120 
0121     in >> tag;
0122     //std::cout << "Tag="<<tag<<std::endl;
0123     assert(tag == "AnalogInputBias:");
0124     in >> tmpint;
0125     analogInputBias_ = tmpint;
0126 
0127     in >> tag;
0128     //std::cout << "Tag="<<tag<<std::endl;
0129     assert(tag == "AnalogOutputBias:");
0130     in >> tmpint;
0131     analogOutputBias_ = tmpint;
0132 
0133     in >> tag;
0134     //std::cout << "Tag="<<tag<<std::endl;
0135     assert(tag == "AnalogOutputGain:");
0136     in >> tmpint;
0137     analogOutputGain_ = tmpint;
0138 
0139     in >> tag;
0140     //std::cout << "Tag="<<tag<<std::endl;
0141     assert(tag == "Mode:");
0142     in >> tag;
0143     assert(tag == "SingleMode" || tag == "DualMode");
0144 
0145     singlemode_ = true;
0146 
0147     if (tag == "DualMode")
0148       singlemode_ = false;
0149 
0150     in.close();
0151 
0152   } else {
0153     std::ifstream in(filename.c_str(), std::ios::binary);
0154 
0155     if (!in.good()) {
0156       std::cout << __LINE__ << mthn << "Could not open:" << filename << std::endl;
0157       assert(0);
0158     } else {
0159       std::cout << __LINE__ << mthn << "Opened:" << filename << std::endl;
0160     }
0161 
0162     char nchar;
0163     std::string s1;
0164 
0165     in.read(&nchar, 1);
0166 
0167     s1.clear();
0168 
0169     //wrote these lines of code without ref. needs to be fixed
0170     for (int i = 0; i < nchar; i++) {
0171       char c;
0172       in >> c;
0173       s1.push_back(c);
0174     }
0175 
0176     PixelROCName tmp(s1);
0177 
0178     rocid_ = tmp;
0179 
0180     in >> analogInputBias_;
0181     in >> analogOutputBias_;
0182     in >> analogOutputGain_;
0183     in >> singlemode_;
0184 
0185     in.close();
0186   }
0187 }
0188 
0189 void PixelTBMSettings::setTBMGenericValue(std::string what, int value) {
0190   if (what == "analogInputBias") {
0191     analogInputBias_ = (unsigned char)value;
0192   } else if (what == "analogOutputBias") {
0193     analogOutputBias_ = (unsigned char)value;
0194   } else if (what == "analogOutputGain") {
0195     analogOutputGain_ = (unsigned char)value;
0196   } else if (what == "Mode") {
0197     singlemode_ = (bool)value;
0198   } else {
0199     std::cout << __LINE__ << "]\t[PixelTBMSettings::setTBMGenericValue()]\t\tFATAL: invalid key/value pair: " << what
0200               << "/" << value << std::endl;
0201     assert(0);
0202   }
0203 }
0204 
0205 void PixelTBMSettings::writeBinary(std::string filename) const {
0206   std::ofstream out(filename.c_str(), std::ios::binary);
0207 
0208   out << (char)rocid_.rocname().size();
0209   out.write(rocid_.rocname().c_str(), rocid_.rocname().size());
0210 
0211   out << analogInputBias_;
0212   out << analogOutputBias_;
0213   out << analogOutputGain_;
0214   out << singlemode_;
0215 }
0216 
0217 void PixelTBMSettings::writeASCII(std::string dir) const {
0218   PixelModuleName module(rocid_.rocname());
0219 
0220   if (!dir.empty())
0221     dir += "/";
0222   std::string filename = dir + "TBM_module_" + module.modulename() + ".dat";
0223 
0224   std::ofstream out(filename.c_str());
0225 
0226   out << rocid_.rocname() << std::endl;
0227 
0228   out << "AnalogInputBias: " << (int)analogInputBias_ << std::endl;
0229   out << "AnalogOutputBias: " << (int)analogOutputBias_ << std::endl;
0230   out << "AnalogOutputGain: " << (int)analogOutputGain_ << std::endl;
0231   out << "Mode: ";
0232   if (singlemode_) {
0233     out << "SingleMode" << std::endl;
0234   } else {
0235     out << "DualMode" << std::endl;
0236   }
0237 }
0238 
0239 void PixelTBMSettings::generateConfiguration(PixelFECConfigInterface *pixelFEC,
0240                                              PixelNameTranslation *trans,
0241                                              bool physics,
0242                                              bool doResets) const {
0243   PixelHdwAddress theROC = *(trans->getHdwAddress(rocid_));
0244 
0245   int mfec = theROC.mfec();
0246   int mfecchannel = theROC.mfecchannel();
0247   int tbmchannel = 14;
0248   int tbmchannelB = 15;
0249   int hubaddress = theROC.hubaddress();
0250 
0251   if (doResets) {
0252     pixelFEC->injectrsttbm(mfec, 1);
0253     pixelFEC->injectrstroc(mfec, 1);
0254   }
0255   pixelFEC->enablecallatency(mfec, 0);
0256   pixelFEC->disableexttrigger(mfec, 0);
0257   pixelFEC->injecttrigger(mfec, 0);
0258   pixelFEC->callatencycount(mfec, 79);
0259 
0260   //pixelFEC->synccontrolregister(mfec);
0261 
0262   //Reset TBM and reset ROC
0263   if (doResets)
0264     pixelFEC->tbmcmd(mfec, mfecchannel, tbmchannel, hubaddress, 4, 2, 0x14, 0);
0265   //setting speed to 40MHz
0266   pixelFEC->tbmcmd(mfec, mfecchannel, tbmchannel, hubaddress, 4, 0, 1, 0);
0267   // setting the mode, we should always stay in the CAL mode
0268   // since the EventNumberClear Mode does not work correctly
0269   //if (physics) {  // comment out, stau always in the CAL mode,  d.k. 27/09/09
0270   //pixelFEC->tbmcmd(mfec, mfecchannel, tbmchannel, hubaddress, 4, 1, 0x80, 0);
0271   //} else {
0272   pixelFEC->tbmcmd(mfec, mfecchannel, tbmchannel, hubaddress, 4, 1, 0xc0, 0);
0273   //}
0274   //Enable token and analog output
0275   pixelFEC->tbmcmd(mfec, mfecchannel, tbmchannel, hubaddress, 4, 4, 0x0, 0);
0276 
0277   //Analog input bias
0278   pixelFEC->tbmcmd(mfec, mfecchannel, tbmchannel, hubaddress, 4, 5, analogInputBias_, 0);
0279   //Analog output bias
0280   pixelFEC->tbmcmd(mfec, mfecchannel, tbmchannel, hubaddress, 4, 6, analogOutputBias_, 0);
0281   //Analog output gain
0282   pixelFEC->tbmcmd(mfec, mfecchannel, tbmchannel, hubaddress, 4, 7, analogOutputGain_, 0);
0283 
0284   //setting speed to 40MHz
0285   pixelFEC->tbmcmd(mfec, mfecchannel, tbmchannelB, hubaddress, 4, 0, 1, 0);
0286   //pre-calibration, stay always in this mode
0287   pixelFEC->tbmcmd(mfec, mfecchannel, tbmchannelB, hubaddress, 4, 1, 0xc0, 0);
0288   //Reset TBM and reset ROC
0289   if (doResets)
0290     pixelFEC->tbmcmd(mfec, mfecchannel, tbmchannelB, hubaddress, 4, 2, 0x14, 0);
0291   //Enable token and analog output
0292   if (singlemode_) {
0293     pixelFEC->tbmcmd(mfec, mfecchannel, tbmchannelB, hubaddress, 4, 4, 0x3, 0);
0294   } else {
0295     pixelFEC->tbmcmd(mfec, mfecchannel, tbmchannelB, hubaddress, 4, 4, 0x0, 0);
0296   }
0297 }
0298 
0299 std::ostream &pos::operator<<(std::ostream &s, const PixelTBMSettings &tbm) {
0300   s << "Module          :" << tbm.getROCName().rocname() << std::endl;
0301   s << "analogInputBias :" << tbm.getAnalogInputBias() << std::endl;
0302   s << "analogOutputBias:" << tbm.getAnalogOutputBias() << std::endl;
0303   s << "analogOutputGain:" << tbm.getAnalogOutputGain() << std::endl;
0304   if (tbm.getMode()) {
0305     s << "mode            :Singlemode" << std::endl;
0306   } else {
0307     s << "mode            :Dualmode" << std::endl;
0308   }
0309 
0310   return s;
0311 }
0312 //=============================================================================================
0313 void PixelTBMSettings::writeXMLHeader(pos::PixelConfigKey key,
0314                                       int version,
0315                                       std::string path,
0316                                       std::ofstream *outstream,
0317                                       std::ofstream *out1stream,
0318                                       std::ofstream *out2stream) const {
0319   std::string mthn = "]\t[PixelTBMSettings::writeXMLHeader()]\t\t\t    ";
0320   std::stringstream fullPath;
0321   fullPath << path << "/Pixel_TbmParameters_" << PixelTimeFormatter::getmSecTime() << ".xml";
0322   std::cout << __LINE__ << mthn << "Writing to: " << fullPath.str() << std::endl;
0323 
0324   outstream->open(fullPath.str().c_str());
0325 
0326   *outstream << "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" << std::endl;
0327   *outstream << "<ROOT xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>" << std::endl;
0328   *outstream << " <HEADER>" << std::endl;
0329   *outstream << "  <TYPE>" << std::endl;
0330   *outstream << "   <EXTENSION_TABLE_NAME>PIXEL_TBM_PARAMETERS</EXTENSION_TABLE_NAME>" << std::endl;
0331   *outstream << "   <NAME>Pixel TBM Parameters</NAME>" << std::endl;
0332   *outstream << "  </TYPE>" << std::endl;
0333   *outstream << "  <RUN>" << std::endl;
0334   *outstream << "   <RUN_TYPE>Pixel TBM Parameters</RUN_TYPE>" << std::endl;
0335   *outstream << "   <RUN_NUMBER>1</RUN_NUMBER>" << std::endl;
0336   *outstream << "   <RUN_BEGIN_TIMESTAMP>" << pos::PixelTimeFormatter::getTime() << "</RUN_BEGIN_TIMESTAMP>"
0337              << std::endl;
0338   *outstream << "   <LOCATION>CERN P5</LOCATION>" << std::endl;
0339   *outstream << "  </RUN>" << std::endl;
0340   *outstream << " </HEADER>" << std::endl;
0341   *outstream << "" << std::endl;
0342   *outstream << " <DATA_SET>" << std::endl;
0343   *outstream << "  <PART>" << std::endl;
0344   *outstream << "   <NAME_LABEL>CMS-PIXEL-ROOT</NAME_LABEL>" << std::endl;
0345   *outstream << "   <KIND_OF_PART>Detector ROOT</KIND_OF_PART>" << std::endl;
0346   *outstream << "  </PART>" << std::endl;
0347   *outstream << "  <VERSION>" << version << "</VERSION>" << std::endl;
0348   *outstream << "  <COMMENT_DESCRIPTION>" << getComment() << "</COMMENT_DESCRIPTION>" << std::endl;
0349   *outstream << "  <CREATED_BY_USER>" << getAuthor() << "</CREATED_BY_USER>" << std::endl;
0350   *outstream << " " << std::endl;
0351 }
0352 
0353 //=============================================================================================
0354 void PixelTBMSettings::writeXML(std::ofstream *outstream, std::ofstream *out1stream, std::ofstream *out2stream) const {
0355   std::string mthn = "]\t[PixelTBMSettings::writeXML()]\t\t\t    ";
0356 
0357   PixelModuleName module(rocid_.rocname());
0358 
0359   *outstream << "  <DATA>" << std::endl;
0360   *outstream << "   <MODULE_NAME>" << rocid_.rocname() << "</MODULE_NAME>" << std::endl;
0361   *outstream << "   <ANLG_INBIAS_VAL>" << (int)analogInputBias_ << "</ANLG_INBIAS_VAL>" << std::endl;
0362   *outstream << "   <ANLG_OUTBIAS_VAL>" << (int)analogOutputBias_ << "</ANLG_OUTBIAS_VAL>" << std::endl;
0363   *outstream << "   <ANLG_OUTGAIN_VAL>" << (int)analogOutputGain_ << "</ANLG_OUTGAIN_VAL>" << std::endl;
0364   if (singlemode_) {
0365     *outstream << "  <TBM_MODE>SingleMode</TBM_MODE>" << std::endl;
0366   } else {
0367     *outstream << "  <TBM_MODE>DualMode</TBM_MODE>" << std::endl;
0368   }
0369   *outstream << "  </DATA>" << std::endl;
0370 }
0371 
0372 //=============================================================================================
0373 void PixelTBMSettings::writeXMLTrailer(std::ofstream *outstream,
0374                                        std::ofstream *out1stream,
0375                                        std::ofstream *out2stream) const {
0376   std::string mthn = "]\t[PixelTBMSettings::writeXMLTrailer()]\t\t\t    ";
0377 
0378   *outstream << " " << std::endl;
0379   *outstream << " </DATA_SET>" << std::endl;
0380   *outstream << "</ROOT> " << std::endl;
0381 
0382   outstream->close();
0383 }