Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 //
0002 // This class stores the information about the global delay25 delay settings
0003 // For the time being, the affected delay25 channels are SDA, SCL, and TRG
0004 // (as implemented in PixelTKFECSupervisor)
0005 //
0006 
0007 #include "CalibFormats/SiPixelObjects/interface/PixelGlobalDelay25.h"
0008 #include "CalibFormats/SiPixelObjects/interface/PixelTimeFormatter.h"
0009 
0010 #include <sstream>
0011 #include <fstream>
0012 #include <map>
0013 #include <cassert>
0014 #include <cmath>
0015 #include <stdexcept>
0016 
0017 using namespace pos;
0018 using namespace std;
0019 
0020 PixelGlobalDelay25::PixelGlobalDelay25(vector<vector<string> > &tableMat) : PixelConfigBase(" ", " ", " ") {
0021   std::string mthn = "[PixelGlobalDelay25::PixelGlobalDelay25()]\t\t    ";
0022   vector<string> ins = tableMat[0];
0023   map<string, int> colM;
0024   vector<string> colNames;
0025 
0026   /**
0027     VIEW_NAME: CONF_KEY_GLOBAL_DELAY25_V
0028     
0029     Name                                        Null?    Type               POS variable
0030     ----------------------------------------- -------- ---------------------------------------------------------------
0031     CONFIG_KEY 
0032     KEY_TYPE
0033     KEY_ALIAS_ID
0034     KEY_ALIAS
0035     VERSION
0036     KIND_OF_COND
0037     GLOBALDELAY25                                       VARCHAR
0038 
0039   */
0040   colNames.push_back("CONFIG_KEY");
0041   colNames.push_back("KEY_TYPE");
0042   colNames.push_back("KEY_ALIAS_ID");
0043   colNames.push_back("KEY_ALIAS");
0044   colNames.push_back("VERSION");
0045   colNames.push_back("KIND_OF_COND");
0046   colNames.push_back("GLOBALDELAY25");
0047   for (unsigned int c = 0; c < ins.size(); c++) {
0048     for (unsigned int n = 0; n < colNames.size(); n++) {
0049       if (tableMat[0][c] == colNames[n]) {
0050         colM[colNames[n]] = c;
0051         break;
0052       }
0053     }
0054   }  //end for
0055   for (unsigned int n = 0; n < colNames.size(); n++) {
0056     if (colM.find(colNames[n]) == colM.end()) {
0057       std::cerr << "[PixelGlobalDelay25::PixelGlobalDelay25()]\tCouldn't find in the database the column with name "
0058                 << colNames[n] << std::endl;
0059       assert(0);
0060     }
0061   }
0062   sscanf(tableMat[1][colM["GLOBALDELAY25"]].c_str(), "%x", &delay_);
0063   std::cout << __LINE__ << "]\t" << mthn << "[DB] read global delay 0x" << std::hex << delay_ << std::dec << endl;
0064 
0065   if (delay_ >= 50) {
0066     std::cout << __LINE__ << "]\t" << mthn << "global delay is out of range (>= 1 Tclk)." << std::endl;
0067     std::cout << __LINE__ << "]\t" << mthn << "will not apply any global delays." << std::endl;
0068     std::cout << __LINE__ << "]\t" << mthn << "increase the delays in the TPLL if needed." << std::endl;
0069     delay_ = 0;
0070   }
0071 }
0072 
0073 PixelGlobalDelay25::PixelGlobalDelay25(std::string filename) : PixelConfigBase(" ", " ", " ") {
0074   std::string mthn = "[PixelGlobalDelay25::PixelGlobalDelay25()]\t\t\t    ";
0075   std::ifstream in(filename.c_str());
0076 
0077   if (!in.good()) {
0078     std::cout << __LINE__ << "]\t" << mthn << "Could not open: " << filename << std::endl;
0079     throw std::runtime_error("Failed to open file " + filename);
0080   } else {
0081     std::cout << __LINE__ << "]\t" << mthn << "Opened: " << filename << std::endl;
0082   }
0083 
0084   in >> std::hex >> delay_ >> std::dec;
0085   std::cout << __LINE__ << "]\t" << mthn << "read global delay 0x" << std::hex << delay_ << std::dec << endl;
0086 
0087   in.close();
0088 
0089   if (delay_ >= 50) {
0090     std::cout << __LINE__ << "]\t" << mthn << "global delay is out of range (>= 1 Tclk)." << std::endl;
0091     std::cout << __LINE__ << "]\t" << mthn << "will not apply any global delays." << std::endl;
0092     std::cout << __LINE__ << "]\t" << mthn << "increase the delays in the TPLL if needed." << std::endl;
0093     delay_ = 0;
0094   }
0095 }
0096 
0097 PixelGlobalDelay25::~PixelGlobalDelay25() {}
0098 
0099 unsigned int PixelGlobalDelay25::getDelay(unsigned int offset) const {
0100   std::string mthn = "[PixelGlobalDelay25::getDelay()]\t\t\t    ";
0101   unsigned int ret = offset + delay_;
0102   if (ret > 127) {
0103     std::cout << __LINE__ << "]\t" << mthn << "the required total delay " << ret << " is out of range." << endl;
0104     std::cout << __LINE__ << "]\t" << mthn << "original setting: " << offset << ", global delay: " << delay_ << endl;
0105     std::cout << __LINE__ << "]\t" << mthn << "we will keep the default delay setting..." << endl;
0106 
0107     ret = offset;
0108   }
0109 
0110   std::cout << __LINE__ << "]\t" << mthn << "getDelay(" << offset << ") returns " << ret << endl;
0111   return ret;
0112 }
0113 
0114 unsigned int PixelGlobalDelay25::getCyclicDelay(unsigned int offset) const {
0115   std::string mthn = "[PixelGlobalDelay25::getCyclicDelay()]\t\t\t    ";
0116   unsigned int ret = offset + delay_;
0117   if (ret > 120)
0118     ret -= 50;
0119   std::cout << __LINE__ << "]\t" << mthn << "getCyclicDelay(" << offset << ") returns " << ret << endl;
0120   return ret;
0121 }
0122 
0123 unsigned int PixelGlobalDelay25::getTTCrxDelay(unsigned int offset) const {
0124   // Computes the TTCrx delay settting required to compensate for the global Delay25 shift.
0125   //
0126   // 'offset' is the current register setting in the TTCrx register
0127   //
0128   // The unit of delay_ is 0.499 ns (Delay25 granularity) that needs to be converted
0129   // to the units of the TTCrx delay generator 103.96 ps
0130 
0131   std::string mthn = "[PixelGlobalDelay25::getTTCrxDelay()]\t\t\t    ";
0132   unsigned int K = (offset / 16 * 16 + offset % 16 * 15 + 30) % 240;
0133   K += (unsigned int)floor((delay_ * 0.499) / 0.1039583 + 0.5);  // add max 235
0134 
0135   unsigned int ret;
0136   if (K > 239) {
0137     std::cout << __LINE__ << "]\t" << mthn << "the required TTCrx fine delay " << K << " is out of range." << endl;
0138     std::cout << __LINE__ << "]\t" << mthn << "this can happen if the register was initialized to 0" << endl;
0139     std::cout << __LINE__ << "]\t" << mthn << "(i.e. delay of 3.1 ns) and the required delay is >21.7 ns." << endl;
0140     std::cout << __LINE__ << "]\t" << mthn << "we will keep the current delay setting..." << endl;
0141     ret = offset;
0142   } else {
0143     unsigned int n = K % 15;
0144     unsigned int m = ((K / 15) - n + 14) % 16;
0145     ret = 16 * n + m;
0146   }
0147 
0148   std::cout << __LINE__ << "]\t" << mthn << "getTTCrxDelay(" << offset << ") returns " << ret << endl;
0149   return ret;
0150   //return offset;
0151 }
0152 
0153 unsigned int PixelGlobalDelay25::getTTCrxDelay() const {
0154   // Computes the TTCrx delay settting required to compensate for the global Delay25 shift.
0155   //
0156   // Assumes that the current register setting in the TTCrx is 0 ns (14)
0157   //
0158   // The unit of delay_ is 0.499 ns (Delay25 granularity) that needs to be converted
0159   // to the units of the TTCrx delay generator 103.96 ps
0160 
0161   return getTTCrxDelay(14);
0162 }
0163 
0164 void PixelGlobalDelay25::writeASCII(std::string dir) const {
0165   std::string mthn = "[PixelGlobalDelay25::writeASCII()]\t\t\t    ";
0166   if (!dir.empty())
0167     dir += "/";
0168   string filename = dir + "globaldelay25.dat";
0169 
0170   ofstream out(filename.c_str());
0171   if (!out.good()) {
0172     cout << __LINE__ << "]\t" << mthn << "Could not open file:" << filename << endl;
0173     assert(0);
0174   }
0175 
0176   out << "0x" << hex << delay_ << dec << endl;
0177 
0178   out.close();
0179 }
0180 
0181 //=============================================================================================
0182 void PixelGlobalDelay25::writeXMLHeader(pos::PixelConfigKey key,
0183                                         int version,
0184                                         std::string path,
0185                                         std::ofstream *outstream,
0186                                         std::ofstream *out1stream,
0187                                         std::ofstream *out2stream) const {
0188   std::stringstream s;
0189   s << __LINE__ << "]\t[[PixelGlobalDelay25::writeXMLHeader()]\t\t\t    ";
0190   std::string mthn = s.str();
0191   std::stringstream fullPath;
0192   fullPath << path << "/Pixel_GlobalDelay25_" << PixelTimeFormatter::getmSecTime() << ".xml";
0193   cout << mthn << "Writing to: " << fullPath.str() << endl;
0194 
0195   outstream->open(fullPath.str().c_str());
0196 
0197   *outstream << "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" << std::endl;
0198   *outstream << "<ROOT xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>" << std::endl;
0199   *outstream << "" << std::endl;
0200   *outstream << " <!-- " << mthn << "-->" << std::endl;
0201   *outstream << "" << std::endl;
0202   *outstream << " <HEADER>" << std::endl;
0203   *outstream << "  <TYPE>" << std::endl;
0204   *outstream << "   <EXTENSION_TABLE_NAME>PIXEL_GLOBAL_DELAY25</EXTENSION_TABLE_NAME>" << std::endl;
0205   *outstream << "   <NAME>Pixel Global Delay25</NAME>" << std::endl;
0206   *outstream << "  </TYPE>" << std::endl;
0207   *outstream << "  <RUN>" << std::endl;
0208   *outstream << "   <RUN_TYPE>Pixel Global Delay25</RUN_TYPE>" << std::endl;
0209   *outstream << "   <RUN_NUMBER>1</RUN_NUMBER>" << std::endl;
0210   *outstream << "   <RUN_BEGIN_TIMESTAMP>" << pos::PixelTimeFormatter::getTime() << "</RUN_BEGIN_TIMESTAMP>"
0211              << std::endl;
0212   *outstream << "   <LOCATION>CERN P5</LOCATION>" << std::endl;
0213   *outstream << "  </RUN>" << std::endl;
0214   *outstream << " </HEADER>" << std::endl;
0215   *outstream << "" << std::endl;
0216   *outstream << "  <DATA_SET>" << std::endl;
0217   *outstream << " " << std::endl;
0218   *outstream << "   <VERSION>" << version << "</VERSION>" << std::endl;
0219   *outstream << "   <COMMENT_DESCRIPTION>" << getComment() << "</COMMENT_DESCRIPTION>" << std::endl;
0220   *outstream << "   <CREATED_BY_USER>" << getAuthor() << "</CREATED_BY_USER>" << std::endl;
0221   *outstream << " " << std::endl;
0222   *outstream << "   <PART>" << std::endl;
0223   *outstream << "    <NAME_LABEL>CMS-PIXEL-ROOT</NAME_LABEL>" << std::endl;
0224   *outstream << "    <KIND_OF_PART>Detector ROOT</KIND_OF_PART>" << std::endl;
0225   *outstream << "   </PART>" << std::endl;
0226 }
0227 
0228 //=============================================================================================
0229 void PixelGlobalDelay25::writeXML(std::ofstream *outstream,
0230                                   std::ofstream *out1stream,
0231                                   std::ofstream *out2stream) const {
0232   std::stringstream s;
0233   s << __LINE__ << "]\t[PixelGlobalDelay25::writeXML()]\t\t\t    ";
0234   std::string mthn = s.str();
0235 
0236   *outstream << "  <DATA>" << std::endl;
0237   *outstream << "   <GLOBALDELAY25>0x" << hex << delay_ << dec << "</GLOBALDELAY25>" << std::endl;
0238   *outstream << "  </DATA>" << std::endl;
0239   *outstream << " " << std::endl;
0240 }
0241 
0242 //=============================================================================================
0243 void PixelGlobalDelay25::writeXMLTrailer(std::ofstream *outstream,
0244                                          std::ofstream *out1stream,
0245                                          std::ofstream *out2stream) const {
0246   std::stringstream s;
0247   s << __LINE__ << "]\t[PixelGlobalDelay25::writeASCII()]\t\t\t    ";
0248   std::string mthn = s.str();
0249 
0250   *outstream << " " << std::endl;
0251   *outstream << " </DATA_SET>" << std::endl;
0252   *outstream << "</ROOT> " << std::endl;
0253 
0254   outstream->close();
0255 }