Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 10:42:02

0001 #include "CalibFormats/SiPixelObjects/interface/PixelFEDTestDAC.h"
0002 #include "CalibFormats/SiPixelObjects/interface/PixelTimeFormatter.h"
0003 #include <cstring>
0004 #include <cassert>
0005 #include <map>
0006 #include <sstream>
0007 
0008 using namespace std;
0009 
0010 using namespace pos;
0011 
0012 PixelFEDTestDAC::PixelFEDTestDAC(std::vector<std::vector<std::string> > &tableMat) {
0013   std::string mthn = "[PixelFEDTestDAC::PixelFEDTestDAC()]\t\t\t    ";
0014   const unsigned long int UB = 200;
0015   const unsigned long int B = 500;
0016   const unsigned long int offset = 0;
0017   vector<unsigned int> pulseTrain(256), pixelDCol(1), pixelPxl(2), pixelTBMHeader(3), pixelTBMTrailer(3);
0018   unsigned int DCol, LorR, start = 15;
0019   std::string line;
0020   std::string::size_type loc1, loc2, loc3, loc4;
0021   unsigned long int npos = std::string::npos;
0022   int i;
0023 
0024   std::map<std::string, int> colM;
0025   std::vector<std::string> colNames;
0026   /**
0027 
0028   EXTENSION_TABLE_NAME: PIXEL_CALIB_CLOB (VIEW: CONF_KEY_PIXEL_CALIB_V)
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   CALIB_TYPE                         VARCHAR2(200)
0036   CALIB_OBJ_DATA_FILE               NOT NULL VARCHAR2(200)
0037   CALIB_OBJ_DATA_CLOB               NOT NULL CLOB
0038   */
0039 
0040   colNames.push_back("CONFIG_KEY");
0041   colNames.push_back("KEY_TYPE");
0042   colNames.push_back("KEY_ALIAS");
0043   colNames.push_back("VERSION");
0044   colNames.push_back("KIND_OF_COND");
0045   colNames.push_back("CALIB_TYPE");
0046   colNames.push_back("CALIB_OBJ_DATA_FILE");
0047   colNames.push_back("CALIB_OBJ_DATA_CLOB");
0048 
0049   for (unsigned int c = 0; c < tableMat[0].size(); c++) {
0050     for (unsigned int n = 0; n < colNames.size(); n++) {
0051       if (tableMat[0][c] == colNames[n]) {
0052         colM[colNames[n]] = c;
0053         break;
0054       }
0055     }
0056   }  //end for
0057   for (unsigned int n = 0; n < colNames.size(); n++) {
0058     if (colM.find(colNames[n]) == colM.end()) {
0059       std::cerr << __LINE__ << "]\t" << mthn << "Couldn't find in the database the column with name " << colNames[n]
0060                 << std::endl;
0061       assert(0);
0062     }
0063   }
0064 
0065   std::istringstream fin;
0066   fin.str(tableMat[1][colM["CALIB_OBJ_DATA_CLOB"]]);
0067 
0068   // Initialise the pulseTrain to offset+black
0069   for (unsigned int i = 0; i < pulseTrain.size(); ++i) {
0070     pulseTrain[i] = offset + B;
0071   }
0072 
0073   i = start;
0074 
0075   getline(fin, line);
0076   mode_ = line;
0077   assert(mode_ == "EmulatedPhysics" || mode_ == "FEDBaselineWithTestDACs" || mode_ == "FEDAddressLevelWithTestDACs");
0078 
0079   while (!fin.eof()) {
0080     getline(fin, line);
0081 
0082     if (line.find("TBMHeader") != npos) {
0083       loc1 = line.find('(');
0084       if (loc1 == npos) {
0085         cout << __LINE__ << "]\t" << mthn << "'(' not found after TBMHeader.\n";
0086         break;
0087       }
0088       loc2 = line.find(')', loc1 + 1);
0089       if (loc2 == npos) {
0090         cout << __LINE__ << "]\t" << mthn << "')' not found after TBMHeader.\n";
0091         break;
0092       }
0093       int TBMHeader = atoi(line.substr(loc1 + 1, loc2 - loc1 - 1).c_str());
0094 
0095       pulseTrain[i] = UB;
0096       ++i;
0097       pulseTrain[i] = UB;
0098       ++i;
0099       pulseTrain[i] = UB;
0100       ++i;
0101       pulseTrain[i] = B;
0102       ++i;
0103 
0104       pixelTBMHeader = decimalToBaseX(TBMHeader, 4, 4);
0105 
0106       pulseTrain[i] = levelEncoder(pixelTBMHeader[3]);
0107       ++i;
0108       pulseTrain[i] = levelEncoder(pixelTBMHeader[2]);
0109       ++i;
0110       pulseTrain[i] = levelEncoder(pixelTBMHeader[1]);
0111       ++i;
0112       pulseTrain[i] = levelEncoder(pixelTBMHeader[0]);
0113       ++i;
0114     } else if (line.find("ROCHeader") != std::string::npos) {
0115       loc1 = line.find('(');
0116       if (loc1 == npos) {
0117         cout << __LINE__ << "]\t" << mthn << "'(' not found after ROCHeader.\n";
0118         break;
0119       }
0120       loc2 = line.find(')', loc1 + 1);
0121       if (loc2 == npos) {
0122         cout << __LINE__ << "]\t" << mthn << "')' not found after ROCHeader.\n";
0123         break;
0124       }
0125       int LastDAC = atoi(line.substr(loc1 + 1, loc2 - loc1 - 1).c_str());
0126 
0127       std::cout << "--------------" << std::endl;
0128 
0129       pulseTrain[i] = UB;
0130       ++i;
0131       pulseTrain[i] = B;
0132       ++i;
0133       pulseTrain[i] = levelEncoder(LastDAC);
0134       ++i;
0135     } else if (line.find("PixelHit") != std::string::npos) {
0136       loc1 = line.find('(');
0137       if (loc1 == npos) {
0138         cout << __LINE__ << "]\t" << mthn << "'(' not found after PixelHit.\n";
0139         break;
0140       }
0141       loc2 = line.find(',', loc1 + 1);
0142       if (loc2 == npos) {
0143         cout << __LINE__ << "]\t" << mthn << "',' not found after the first argument of PixelHit.\n";
0144         break;
0145       }
0146       loc3 = line.find(',', loc2 + 1);
0147       if (loc3 == npos) {
0148         cout << __LINE__ << "]\t" << mthn << "'.' not found after the second argument of PixelHit.\n";
0149         break;
0150       }
0151       loc4 = line.find(')', loc3 + 1);
0152       if (loc4 == npos) {
0153         cout << __LINE__ << "]\t" << mthn << "')' not found after the third argument of PixelHit.\n";
0154         break;
0155       }
0156       int column = atoi(line.substr(loc1 + 1, loc2 - loc1 - 1).c_str());
0157       int row = atoi(line.substr(loc2 + 1, loc3 - loc2 - 1).c_str());
0158       int charge = atoi(line.substr(loc3 + 1, loc4 - loc3 - 1).c_str());
0159 
0160       DCol = int(column / 2);
0161       LorR = int(column - DCol * 2);
0162       pixelDCol = decimalToBaseX(DCol, 6, 2);
0163       pixelPxl = decimalToBaseX((80 - row) * 2 + LorR, 6, 3);
0164 
0165       std::cout << "Pxl = " << pixelPxl[2] << pixelPxl[1] << pixelPxl[0] << ", DCol= " << pixelDCol[1] << pixelDCol[0]
0166                 << std::endl;
0167 
0168       pulseTrain[i] = levelEncoder(pixelDCol[1]);
0169       ++i;
0170       pulseTrain[i] = levelEncoder(pixelDCol[0]);
0171       ++i;
0172       pulseTrain[i] = levelEncoder(pixelPxl[2]);
0173       ++i;
0174       pulseTrain[i] = levelEncoder(pixelPxl[1]);
0175       ++i;
0176       pulseTrain[i] = levelEncoder(pixelPxl[0]);
0177       ++i;
0178       pulseTrain[i] = charge;
0179       ++i;
0180 
0181     } else if (line.find("TBMTrailer") != std::string::npos) {
0182       loc1 = line.find('(');
0183       if (loc1 == npos) {
0184         cout << __LINE__ << "]\t" << mthn << "'(' not found after TBMTrailer.\n";
0185         break;
0186       }
0187       loc2 = line.find(')', loc1 + 1);
0188       if (loc2 == npos) {
0189         cout << __LINE__ << "]\t" << mthn << "')' not found after TBMTrailer.\n";
0190         break;
0191       }
0192       int TBMTrailer = atoi(line.substr(loc1 + 1, loc2 - loc1 - 1).c_str());
0193 
0194       pulseTrain[i] = UB;
0195       ++i;
0196       pulseTrain[i] = UB;
0197       ++i;
0198       pulseTrain[i] = B;
0199       ++i;
0200       pulseTrain[i] = B;
0201       ++i;
0202 
0203       pixelTBMTrailer = decimalToBaseX(TBMTrailer, 4, 4);
0204       pulseTrain[i] = levelEncoder(pixelTBMTrailer[3]);
0205       ++i;
0206       pulseTrain[i] = levelEncoder(pixelTBMTrailer[2]);
0207       ++i;
0208       pulseTrain[i] = levelEncoder(pixelTBMTrailer[1]);
0209       ++i;
0210       pulseTrain[i] = levelEncoder(pixelTBMTrailer[0]);
0211       ++i;
0212     }
0213   }
0214   //   fin.close();
0215   dacs_ = pulseTrain;
0216 }
0217 
0218 PixelFEDTestDAC::PixelFEDTestDAC(std::string filename) {
0219   std::string mthn = "[PixelFEDTestDAC::PixelFEDTestDAC()]\t\t\t\t    ";
0220   const unsigned long int UB = 200;
0221   const unsigned long int B = 500;
0222   const unsigned long int offset = 0;
0223   vector<unsigned int> pulseTrain(256), pixelDCol(1), pixelPxl(2), pixelTBMHeader(3), pixelTBMTrailer(3);
0224   unsigned int DCol, LorR, start = 15;
0225   std::string line;
0226   std::string::size_type loc1, loc2, loc3, loc4;
0227   unsigned long int npos = std::string::npos;
0228   int i;
0229 
0230   // Initialise the pulseTrain to offset+black
0231   for (unsigned int i = 0; i < pulseTrain.size(); ++i) {
0232     pulseTrain[i] = offset + B;
0233   }
0234 
0235   ifstream fin(filename.c_str());
0236 
0237   i = start;
0238 
0239   getline(fin, line);
0240   mode_ = line;
0241   assert(mode_ == "EmulatedPhysics" || mode_ == "FEDBaselineWithTestDACs" || mode_ == "FEDAddressLevelWithTestDACs");
0242 
0243   while (!fin.eof()) {
0244     getline(fin, line);
0245 
0246     if (line.find("TBMHeader") != npos) {
0247       loc1 = line.find('(');
0248       if (loc1 == npos) {
0249         cout << __LINE__ << "]\t" << mthn << "'(' not found after TBMHeader.\n";
0250         break;
0251       }
0252       loc2 = line.find(')', loc1 + 1);
0253       if (loc2 == npos) {
0254         cout << __LINE__ << "]\t" << mthn << "')' not found after TBMHeader.\n";
0255         break;
0256       }
0257       int TBMHeader = atoi(line.substr(loc1 + 1, loc2 - loc1 - 1).c_str());
0258 
0259       pulseTrain[i] = UB;
0260       ++i;
0261       pulseTrain[i] = UB;
0262       ++i;
0263       pulseTrain[i] = UB;
0264       ++i;
0265       pulseTrain[i] = B;
0266       ++i;
0267 
0268       pixelTBMHeader = decimalToBaseX(TBMHeader, 4, 4);
0269 
0270       pulseTrain[i] = levelEncoder(pixelTBMHeader[3]);
0271       ++i;
0272       pulseTrain[i] = levelEncoder(pixelTBMHeader[2]);
0273       ++i;
0274       pulseTrain[i] = levelEncoder(pixelTBMHeader[1]);
0275       ++i;
0276       pulseTrain[i] = levelEncoder(pixelTBMHeader[0]);
0277       ++i;
0278     } else if (line.find("ROCHeader") != std::string::npos) {
0279       loc1 = line.find('(');
0280       if (loc1 == npos) {
0281         cout << __LINE__ << "]\t" << mthn << "'(' not found after ROCHeader.\n";
0282         break;
0283       }
0284       loc2 = line.find(')', loc1 + 1);
0285       if (loc2 == npos) {
0286         cout << __LINE__ << "]\t" << mthn << "')' not found after ROCHeader.\n";
0287         break;
0288       }
0289       int LastDAC = atoi(line.substr(loc1 + 1, loc2 - loc1 - 1).c_str());
0290 
0291       std::cout << "--------------" << std::endl;
0292 
0293       pulseTrain[i] = UB;
0294       ++i;
0295       pulseTrain[i] = B;
0296       ++i;
0297       pulseTrain[i] = levelEncoder(LastDAC);
0298       ++i;
0299     } else if (line.find("PixelHit") != std::string::npos) {
0300       loc1 = line.find('(');
0301       if (loc1 == npos) {
0302         cout << __LINE__ << "]\t" << mthn << "'(' not found after PixelHit.\n";
0303         break;
0304       }
0305       loc2 = line.find(',', loc1 + 1);
0306       if (loc2 == npos) {
0307         cout << __LINE__ << "]\t" << mthn << "',' not found after the first argument of PixelHit.\n";
0308         break;
0309       }
0310       loc3 = line.find(',', loc2 + 1);
0311       if (loc3 == npos) {
0312         cout << __LINE__ << "]\t" << mthn << "'.' not found after the second argument of PixelHit.\n";
0313         break;
0314       }
0315       loc4 = line.find(')', loc3 + 1);
0316       if (loc4 == npos) {
0317         cout << __LINE__ << "]\t" << mthn << "')' not found after the third argument of PixelHit.\n";
0318         break;
0319       }
0320       int column = atoi(line.substr(loc1 + 1, loc2 - loc1 - 1).c_str());
0321       int row = atoi(line.substr(loc2 + 1, loc3 - loc2 - 1).c_str());
0322       int charge = atoi(line.substr(loc3 + 1, loc4 - loc3 - 1).c_str());
0323 
0324       DCol = int(column / 2);
0325       LorR = int(column - DCol * 2);
0326       pixelDCol = decimalToBaseX(DCol, 6, 2);
0327       pixelPxl = decimalToBaseX((80 - row) * 2 + LorR, 6, 3);
0328 
0329       std::cout << "Pxl = " << pixelPxl[2] << pixelPxl[1] << pixelPxl[0] << ", DCol= " << pixelDCol[1] << pixelDCol[0]
0330                 << std::endl;
0331 
0332       pulseTrain[i] = levelEncoder(pixelDCol[1]);
0333       ++i;
0334       pulseTrain[i] = levelEncoder(pixelDCol[0]);
0335       ++i;
0336       pulseTrain[i] = levelEncoder(pixelPxl[2]);
0337       ++i;
0338       pulseTrain[i] = levelEncoder(pixelPxl[1]);
0339       ++i;
0340       pulseTrain[i] = levelEncoder(pixelPxl[0]);
0341       ++i;
0342       pulseTrain[i] = charge;
0343       ++i;
0344 
0345     } else if (line.find("TBMTrailer") != std::string::npos) {
0346       loc1 = line.find('(');
0347       if (loc1 == npos) {
0348         cout << __LINE__ << "]\t" << mthn << "'(' not found after TBMTrailer.\n";
0349         break;
0350       }
0351       loc2 = line.find(')', loc1 + 1);
0352       if (loc2 == npos) {
0353         cout << __LINE__ << "]\t" << mthn << "')' not found after TBMTrailer.\n";
0354         break;
0355       }
0356       int TBMTrailer = atoi(line.substr(loc1 + 1, loc2 - loc1 - 1).c_str());
0357 
0358       pulseTrain[i] = UB;
0359       ++i;
0360       pulseTrain[i] = UB;
0361       ++i;
0362       pulseTrain[i] = B;
0363       ++i;
0364       pulseTrain[i] = B;
0365       ++i;
0366 
0367       pixelTBMTrailer = decimalToBaseX(TBMTrailer, 4, 4);
0368       pulseTrain[i] = levelEncoder(pixelTBMTrailer[3]);
0369       ++i;
0370       pulseTrain[i] = levelEncoder(pixelTBMTrailer[2]);
0371       ++i;
0372       pulseTrain[i] = levelEncoder(pixelTBMTrailer[1]);
0373       ++i;
0374       pulseTrain[i] = levelEncoder(pixelTBMTrailer[0]);
0375       ++i;
0376     }
0377   }
0378   fin.close();
0379   dacs_ = pulseTrain;
0380 }
0381 
0382 unsigned int PixelFEDTestDAC::levelEncoder(int level) {
0383   unsigned int pulse;
0384 
0385   switch (level) {
0386     case 0:
0387       pulse = 450;
0388       break;
0389     case 1:
0390       pulse = 500;
0391       break;
0392     case 2:
0393       pulse = 550;
0394       break;
0395     case 3:
0396       pulse = 600;
0397       break;
0398     case 4:
0399       pulse = 650;
0400       break;
0401     case 5:
0402       pulse = 700;
0403       break;
0404     default:
0405       assert(0);
0406       break;
0407   }
0408 
0409   return pulse;
0410 }
0411 
0412 vector<unsigned int> PixelFEDTestDAC::decimalToBaseX(unsigned int a, unsigned int x, unsigned int length) {
0413   vector<unsigned int> ans(100, 0);
0414   int i = 0;
0415 
0416   while (a > 0) {
0417     ans[i] = a % x;
0418     //ans.push_back(a%x);
0419     a = a / x;
0420     i += 1;
0421   }
0422 
0423   if (length > 0)
0424     ans.resize(length);
0425   else
0426     ans.resize(i);
0427 
0428   return ans;
0429 }
0430 
0431 //=============================================================================================
0432 void PixelFEDTestDAC::writeXMLHeader(pos::PixelConfigKey key,
0433                                      int version,
0434                                      std::string path,
0435                                      std::ofstream *outstream,
0436                                      std::ofstream *out1stream,
0437                                      std::ofstream *out2stream) const {
0438   std::string mthn = "[PixelFEDTestDAC::writeXMLHeader()]\t\t\t    ";
0439   std::stringstream maskFullPath;
0440 
0441   //  writeASCII(path) ;
0442 
0443   maskFullPath << path << "/PixelCalib_Test_" << PixelTimeFormatter::getmSecTime() << ".xml";
0444   std::cout << __LINE__ << "]\t" << mthn << "Writing to: " << maskFullPath.str() << std::endl;
0445 
0446   outstream->open(maskFullPath.str().c_str());
0447 
0448   *outstream << "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" << std::endl;
0449   *outstream << "<ROOT xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>" << std::endl;
0450   *outstream << "" << std::endl;
0451   *outstream << " <HEADER>" << std::endl;
0452   *outstream << "  <TYPE>" << std::endl;
0453   *outstream << "   <EXTENSION_TABLE_NAME>PIXEL_CALIB_CLOB</EXTENSION_TABLE_NAME>" << std::endl;
0454   *outstream << "   <NAME>Calibration Object Clob</NAME>" << std::endl;
0455   *outstream << "  </TYPE>" << std::endl;
0456   *outstream << "  <RUN>" << std::endl;
0457   *outstream << "   <RUN_TYPE>PixelFEDTestDAC</RUN_TYPE>" << std::endl;
0458   *outstream << "   <RUN_NUMBER>1</RUN_NUMBER>" << std::endl;
0459   *outstream << "   <RUN_BEGIN_TIMESTAMP>" << PixelTimeFormatter::getTime() << "</RUN_BEGIN_TIMESTAMP>" << std::endl;
0460   *outstream << "   <LOCATION>CERN P5</LOCATION>" << std::endl;
0461   *outstream << "  </RUN>" << std::endl;
0462   *outstream << " </HEADER>" << std::endl;
0463   *outstream << "" << std::endl;
0464   *outstream << " <DATA_SET>" << std::endl;
0465   *outstream << "" << std::endl;
0466   *outstream << "  <VERSION>" << version << "</VERSION>" << std::endl;
0467   *outstream << "  <COMMENT_DESCRIPTION>No comment defined: this class does NOT inherit from "
0468                 "PixelCalibBase</COMMENT_DESCRIPTION>"
0469              << std::endl;
0470   *outstream << "  <CREATED_BY_USER>Unknown user</CREATED_BY_USER>" << std::endl;
0471   *outstream << "" << std::endl;
0472   *outstream << "  <PART>" << std::endl;
0473   *outstream << "   <NAME_LABEL>CMS-PIXEL-ROOT</NAME_LABEL>" << std::endl;
0474   *outstream << "   <KIND_OF_PART>Detector ROOT</KIND_OF_PART>" << std::endl;
0475   *outstream << "  </PART>" << std::endl;
0476 }
0477 
0478 //=============================================================================================
0479 void PixelFEDTestDAC::writeXML(std::ofstream *outstream, std::ofstream *out1stream, std::ofstream *out2stream) const {
0480   std::string mthn = "[PixelFEDTestDAC::writeXML()]\t\t\t    ";
0481 
0482   *outstream << " " << std::endl;
0483   *outstream << "  <DATA>" << std::endl;
0484   *outstream << "   <CALIB_OBJ_DATA_FILE>./fedtestdac.dat</CALIB_OBJ_DATA_FILE>" << std::endl;
0485   *outstream << "   <CALIB_TYPE>fedtestdac</CALIB_TYPE>" << std::endl;
0486   *outstream << "  </DATA>" << std::endl;
0487   *outstream << " " << std::endl;
0488 }
0489 
0490 //=============================================================================================
0491 void PixelFEDTestDAC::writeXMLTrailer(std::ofstream *outstream,
0492                                       std::ofstream *out1stream,
0493                                       std::ofstream *out2stream) const {
0494   std::string mthn = "[PixelFEDTestDAC::writeXMLTrailer()]\t\t\t    ";
0495 
0496   *outstream << " </DATA_SET>" << std::endl;
0497   *outstream << "</ROOT>" << std::endl;
0498 
0499   outstream->close();
0500   std::cout << __LINE__ << "]\t" << mthn << "Data written " << std::endl;
0501 }