File indexing completed on 2023-03-17 10:42:02
0001
0002
0003
0004
0005
0006
0007 #include "CalibFormats/SiPixelObjects/interface/PixelFEDConfig.h"
0008 #include "CalibFormats/SiPixelObjects/interface/PixelTimeFormatter.h"
0009 #include <fstream>
0010 #include <iostream>
0011 #include <map>
0012 #include <cassert>
0013 #include <stdexcept>
0014
0015 using namespace pos;
0016 using namespace std;
0017
0018 PixelFEDConfig::PixelFEDConfig(std::vector<std::vector<std::string> > &tableMat) : PixelConfigBase(" ", " ", " ") {
0019 std::string mthn = "[PixelFEDConfig::PixelFEDConfig()]\t\t\t ";
0020
0021 std::vector<std::string> ins = tableMat[0];
0022 std::map<std::string, int> colM;
0023 std::vector<std::string> colNames;
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037 colNames.push_back("CONFIG_KEY");
0038 colNames.push_back("KEY_TYPE");
0039 colNames.push_back("KEY_ALIAS");
0040 colNames.push_back("VERSION");
0041 colNames.push_back("KIND_OF_COND");
0042 colNames.push_back("PIXEL_FED");
0043 colNames.push_back("CRATE_NUMBER");
0044 colNames.push_back("VME_ADDR");
0045
0046
0047
0048
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 }
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069 std::string fedname = "";
0070 unsigned int fednum = 0;
0071 fedconfig_.clear();
0072 bool flag = false;
0073 for (unsigned int r = 1; r < tableMat.size(); r++) {
0074
0075 fedname = tableMat
0076 [r]
0077 [colM["PIXEL_FED"]];
0078
0079
0080
0081
0082 fednum = (unsigned int)atoi(fedname.c_str());
0083
0084 if (fedconfig_.empty()) {
0085 PixelFEDParameters tmp;
0086 unsigned int vme_base_address = 0;
0087 vme_base_address = strtoul(tableMat[r][colM["VME_ADDR"]].c_str(), nullptr, 16);
0088
0089
0090 tmp.setFEDParameters(fednum, (unsigned int)atoi(tableMat[r][colM["CRATE_NUMBER"]].c_str()), vme_base_address);
0091 fedconfig_.push_back(tmp);
0092 } else {
0093 for (unsigned int y = 0; y < fedconfig_.size(); y++) {
0094 if (fedconfig_[y].getFEDNumber() ==
0095 fednum)
0096 {
0097 flag = true;
0098 break;
0099 } else
0100 flag = false;
0101 }
0102
0103 if (flag == false) {
0104 PixelFEDParameters tmp;
0105 tmp.setFEDParameters(fednum,
0106 (unsigned int)atoi(tableMat[r][colM["CRATE_NUMBER"]].c_str()),
0107 (unsigned int)strtoul(tableMat[r][colM["VME_ADDR"]].c_str(), nullptr, 16));
0108 fedconfig_.push_back(tmp);
0109 }
0110 }
0111 }
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122 }
0123
0124
0125
0126 PixelFEDConfig::PixelFEDConfig(std::string filename) : PixelConfigBase(" ", " ", " ") {
0127 std::string mthn = "[PixelFEDConfig::PixelFEDConfig()]\t\t\t ";
0128 std::ifstream in(filename.c_str());
0129
0130 if (!in.good()) {
0131 std::cout << __LINE__ << "]\t" << mthn << "Could not open: " << filename.c_str() << std::endl;
0132 throw std::runtime_error("Failed to open file " + filename);
0133 } else {
0134 std::cout << __LINE__ << "]\t" << mthn << "Opened: " << filename.c_str() << std::endl;
0135 }
0136
0137 std::string dummy;
0138
0139 in >> dummy;
0140 in >> dummy;
0141 in >> dummy;
0142 in >> dummy;
0143 in >> dummy;
0144 in >> dummy;
0145
0146 do {
0147 unsigned int fednumber;
0148 unsigned int crate;
0149 unsigned int vme_base_address;
0150
0151 in >> fednumber >> crate >> std::hex >> vme_base_address >> std::dec;
0152
0153 if (!in.eof()) {
0154
0155
0156 PixelFEDParameters tmp;
0157
0158 tmp.setFEDParameters(fednumber, crate, vme_base_address);
0159
0160 fedconfig_.push_back(tmp);
0161 }
0162
0163 } while (!in.eof());
0164 in.close();
0165 }
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176 PixelFEDConfig::~PixelFEDConfig() {}
0177
0178 void PixelFEDConfig::writeASCII(std::string dir) const {
0179 std::string mthn = "[PixelFEDConfig::writeASCII()]\t\t\t\t ";
0180 if (!dir.empty())
0181 dir += "/";
0182 string filename = dir + "fedconfig.dat";
0183
0184 ofstream out(filename.c_str());
0185 if (!out.good()) {
0186 cout << __LINE__ << "]\t" << mthn << "Could not open file: " << filename << endl;
0187 assert(0);
0188 }
0189
0190 out << " #FED number crate vme base address" << endl;
0191 for (unsigned int i = 0; i < fedconfig_.size(); i++) {
0192 out << fedconfig_[i].getFEDNumber() << " " << fedconfig_[i].getCrate() << " "
0193 << "0x" << hex << fedconfig_[i].getVMEBaseAddress() << dec << endl;
0194 }
0195 out.close();
0196 }
0197
0198 unsigned int PixelFEDConfig::getNFEDBoards() const { return fedconfig_.size(); }
0199
0200 unsigned int PixelFEDConfig::getFEDNumber(unsigned int i) const {
0201 assert(i < fedconfig_.size());
0202 return fedconfig_[i].getFEDNumber();
0203 }
0204
0205 unsigned int PixelFEDConfig::getCrate(unsigned int i) const {
0206 assert(i < fedconfig_.size());
0207 return fedconfig_[i].getCrate();
0208 }
0209
0210 unsigned int PixelFEDConfig::getVMEBaseAddress(unsigned int i) const {
0211 assert(i < fedconfig_.size());
0212 return fedconfig_[i].getVMEBaseAddress();
0213 }
0214
0215 unsigned int PixelFEDConfig::crateFromFEDNumber(unsigned int fednumber) const {
0216 std::string mthn = "[PixelFEDConfig::crateFromFEDNumber()]\t\t\t ";
0217 for (unsigned int i = 0; i < fedconfig_.size(); i++) {
0218 if (fedconfig_[i].getFEDNumber() == fednumber)
0219 return fedconfig_[i].getCrate();
0220 }
0221
0222 std::cout << __LINE__ << "]\t" << mthn << "Could not find FED number: " << fednumber << std::endl;
0223
0224 assert(0);
0225
0226 return 0;
0227 }
0228
0229 unsigned int PixelFEDConfig::VMEBaseAddressFromFEDNumber(unsigned int fednumber) const {
0230 std::string mthn = "[PixelFEDConfig::VMEBaseAddressFromFEDNumber()]\t\t ";
0231 for (unsigned int i = 0; i < fedconfig_.size(); i++) {
0232 if (fedconfig_[i].getFEDNumber() == fednumber)
0233 return fedconfig_[i].getVMEBaseAddress();
0234 }
0235
0236 std::cout << __LINE__ << "]\t" << mthn << "Could not find FED number: " << fednumber << std::endl;
0237
0238 assert(0);
0239
0240 return 0;
0241 }
0242
0243 unsigned int PixelFEDConfig::FEDNumberFromCrateAndVMEBaseAddress(unsigned int crate,
0244 unsigned int vmebaseaddress) const {
0245 std::string mthn = "[PixelFEDConfig::FEDNumberFromCrateAndVMEBaseAddress()]\t ";
0246 for (unsigned int i = 0; i < fedconfig_.size(); i++) {
0247 if (fedconfig_[i].getCrate() == crate && fedconfig_[i].getVMEBaseAddress() == vmebaseaddress)
0248 return fedconfig_[i].getFEDNumber();
0249 }
0250
0251 std::cout << __LINE__ << "]\t" << mthn << "Could not find FED crate and address: " << crate << ", " << vmebaseaddress
0252 << std::endl;
0253
0254 assert(0);
0255
0256 return 0;
0257 }
0258
0259
0260 void PixelFEDConfig::writeXMLHeader(pos::PixelConfigKey key,
0261 int version,
0262 std::string path,
0263 std::ofstream *outstream,
0264 std::ofstream *out1stream,
0265 std::ofstream *out2stream) const {
0266 std::string mthn = "[PixelFEDConfig::::writeXMLHeader()]\t\t\t ";
0267 std::stringstream fullPath;
0268 fullPath << path << "/Pixel_FedCrateConfig_" << PixelTimeFormatter::getmSecTime() << ".xml";
0269 cout << __LINE__ << "]\t" << mthn << "Writing to: " << fullPath.str() << endl;
0270
0271 outstream->open(fullPath.str().c_str());
0272 *outstream << "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" << endl;
0273 *outstream << "<ROOT xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>" << endl;
0274 *outstream << " <HEADER>" << endl;
0275 *outstream << " <TYPE>" << endl;
0276 *outstream << " <EXTENSION_TABLE_NAME>FED_CRATE_CONFIG</EXTENSION_TABLE_NAME>" << endl;
0277 *outstream << " <NAME>Pixel FED Crate Configuration</NAME>" << endl;
0278 *outstream << " </TYPE>" << endl;
0279 *outstream << " <RUN>" << endl;
0280 *outstream << " <RUN_NAME>Pixel FED Crate Configuration</RUN_NAME>" << endl;
0281 *outstream << " <RUN_BEGIN_TIMESTAMP>" << pos::PixelTimeFormatter::getTime() << "</RUN_BEGIN_TIMESTAMP>" << endl;
0282 *outstream << " <LOCATION>CERN P5</LOCATION>" << endl;
0283 *outstream << " </RUN>" << endl;
0284 *outstream << " </HEADER>" << endl;
0285 *outstream << " " << endl;
0286 *outstream << " <DATA_SET>" << endl;
0287 *outstream << " <PART>" << endl;
0288 *outstream << " <NAME_LABEL>CMS-PIXEL-ROOT</NAME_LABEL>" << endl;
0289 *outstream << " <KIND_OF_PART>Detector ROOT</KIND_OF_PART>" << endl;
0290 *outstream << " </PART>" << endl;
0291 *outstream << " <VERSION>" << version << "</VERSION>" << endl;
0292 *outstream << " <COMMENT_DESCRIPTION>" << getComment() << "</COMMENT_DESCRIPTION>" << endl;
0293 *outstream << " <CREATED_BY_USER>" << getAuthor() << "</CREATED_BY_USER>" << endl;
0294 *outstream << " " << endl;
0295 }
0296
0297
0298 void PixelFEDConfig::writeXML(std::ofstream *outstream, std::ofstream *out1stream, std::ofstream *out2stream) const {
0299 std::string mthn = "[PixelFEDConfig::writeXML()]\t\t\t ";
0300
0301 for (unsigned int i = 0; i < fedconfig_.size(); i++) {
0302 *outstream << " <DATA>" << endl;
0303 *outstream << " <PIXEL_FED>" << fedconfig_[i].getFEDNumber() << "</PIXEL_FED>" << endl;
0304 *outstream << " <CRATE_NUMBER>" << fedconfig_[i].getCrate() << "</CRATE_NUMBER>" << endl;
0305 *outstream << " <VME_ADDR>"
0306 << "0x" << hex << fedconfig_[i].getVMEBaseAddress() << dec << "</VME_ADDR>" << endl;
0307 *outstream << " </DATA>" << endl;
0308 *outstream << "" << endl;
0309 }
0310 }
0311
0312
0313 void PixelFEDConfig::writeXMLTrailer(std::ofstream *outstream,
0314 std::ofstream *out1stream,
0315 std::ofstream *out2stream) const {
0316 std::string mthn = "[PixelFEDConfig::writeXMLTrailer()]\t\t\t ";
0317
0318 *outstream << " </DATA_SET>" << endl;
0319 *outstream << "</ROOT> " << endl;
0320
0321 outstream->close();
0322 }