Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 //
0002 // This class provides the mapping between
0003 // portcards and the modules controlled by
0004 // the card
0005 //
0006 //
0007 //
0008 
0009 #include "CalibFormats/SiPixelObjects/interface/PixelPortcardMap.h"
0010 #include "CalibFormats/SiPixelObjects/interface/PixelTimeFormatter.h"
0011 
0012 #include <sstream>
0013 #include <cassert>
0014 #include <stdexcept>
0015 
0016 using namespace pos;
0017 using namespace std;
0018 
0019 PixelPortcardMap::PixelPortcardMap(std::vector<std::vector<std::string> > &tableMat) : PixelConfigBase(" ", " ", " ") {
0020   std::string mthn = "[PixelPortcardMap::PixelPortcardMap()]\t\t\t    ";
0021   std::vector<std::string> ins = tableMat[0];
0022   std::map<std::string, int> colM;
0023   std::vector<std::string> colNames;
0024   /*
0025   EXTENSION_TABLE_NAME: PIXEL_PORTCARD_MAP (VIEW: CONF_KEY_PORTCARD_MAP_V)
0026 
0027   CONFIG_KEY                    NOT NULL VARCHAR2(80)
0028   KEY_TYPE                  NOT NULL VARCHAR2(80)
0029   KEY_ALIAS                 NOT NULL VARCHAR2(80)
0030   VERSION                        VARCHAR2(40)
0031   KIND_OF_COND                  NOT NULL VARCHAR2(40)
0032   PORT_CARD                 NOT NULL VARCHAR2(200)
0033   PANEL_NAME                    NOT NULL VARCHAR2(200)
0034   TBM_MODE                       VARCHAR2(200)
0035   AOH_CHAN                  NOT NULL NUMBER(38)
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("PORT_CARD");
0043   colNames.push_back("PANEL_NAME");
0044   colNames.push_back("TBM_MODE");
0045   colNames.push_back("AOH_CHAN");
0046   /*
0047   colNames.push_back("CONFIG_KEY_ID" );
0048   colNames.push_back("CONFG_KEY"     );
0049   colNames.push_back("VERSION"       );
0050   colNames.push_back("KIND_OF_COND"  );
0051   colNames.push_back("SERIAL_NUMBER" );
0052   colNames.push_back("PORT_CARD"     );
0053   colNames.push_back("PANEL_NAME"    );
0054   colNames.push_back("TBM_MODE"      );
0055   colNames.push_back("AOH_CHAN"      );
0056  */
0057   for (unsigned int c = 0; c < ins.size(); c++) {
0058     for (unsigned int n = 0; n < colNames.size(); n++) {
0059       if (tableMat[0][c] == colNames[n]) {
0060         colM[colNames[n]] = c;
0061         break;
0062       }
0063     }
0064   }  //end for
0065   /*
0066   for(unsigned int n=0; n<colNames.size(); n++)
0067     {
0068       if(colM.find(colNames[n]) == colM.end())
0069     {
0070       std::cerr << __LINE__ << "]\t" << mthn 
0071                 << "Couldn't find in the database the column with name " << colNames[n] << std::endl;
0072       assert(0);
0073     }
0074     }
0075   */
0076 
0077   std::string portcardname;
0078   std::string modulename;
0079   unsigned int aoh;
0080   std::string aohstring;
0081   std::string tbmChannel;
0082 
0083   for (unsigned int r = 1; r < tableMat.size(); r++) {  //Goes to every row of the Matrix
0084 
0085     portcardname = tableMat[r][colM["PORT_CARD"]];
0086     modulename = tableMat[r][colM["PANEL_NAME"]];
0087     aohstring = tableMat[r][colM["AOH_CHAN"]];
0088     tbmChannel = tableMat[r][colM["TBM_MODE"]];
0089     //     cout << "[PixelPortcardMap::PixelPortcardMap()]\t\t\t    "
0090     //   << "Portcardname: " << portcardname
0091     //   << "\tmodulename: "   << modulename
0092     //   << "\taohstring: "    << aohstring
0093     //   << "\ttbmChannel:"   << tbmChannel
0094     //   << endl ;
0095     //aohname.erase(0,20);  // Is going to be change when Umesh put a AOH Channel column in the view.
0096     aoh = (((unsigned int)atoi(aohstring.c_str())));
0097     //std::cout<<aoh<<std::endl;
0098     PixelModuleName module(modulename);
0099     if (module.modulename() != modulename) {
0100       std::cout << __LINE__ << "]\t" << mthn << "Modulename: " << modulename << std::endl;
0101       std::cout << __LINE__ << "]\t" << mthn << "Parsed to : " << module.modulename() << std::endl;
0102       assert(0);
0103     }
0104     if (tbmChannel.empty()) {
0105       tbmChannel = "A";  // assert(0); // add TBMChannel to the input, then remove assert
0106     }
0107     PixelChannel channel(module, tbmChannel);
0108     std::pair<std::string, int> portcardAndAOH(portcardname, aoh);
0109     map_[channel] = portcardAndAOH;
0110   }  //end for r
0111 
0112 }  //end constructor
0113 //*****************************************************************************
0114 
0115 PixelPortcardMap::PixelPortcardMap(std::string filename) : PixelConfigBase(" ", " ", " ") {
0116   std::string mthn = "[PixelPortcardMap::PixelPortcardMap()]\t\t\t    ";
0117   std::ifstream in(filename.c_str());
0118 
0119   if (!in.good()) {
0120     std::cout << __LINE__ << "]\t" << mthn << "Could not open: " << filename << std::endl;
0121     throw std::runtime_error("Failed to open file " + filename);
0122   } else {
0123     std::cout << __LINE__ << "]\t" << mthn << "Reading from: " << filename << std::endl;
0124   }
0125 
0126   std::string dummy;
0127 
0128   in >> dummy;
0129   in >> dummy;
0130   in >> dummy;
0131   in >> dummy;
0132   in >> dummy;
0133 
0134   do {
0135     std::string portcardname;
0136     std::string modulename;
0137     std::string TBMChannel = "A";
0138     std::string aoh_string;
0139     unsigned int aoh;
0140 
0141     in >> portcardname >> modulename >> aoh_string;
0142     if (aoh_string == "A" ||
0143         aoh_string == "B")  // Optionally, the TBM channel may be specified after the module name.  Check for this.
0144     {
0145       TBMChannel = aoh_string;
0146       in >> aoh_string;
0147     }
0148     aoh = atoi(aoh_string.c_str());
0149 
0150     if (!in.eof()) {
0151       PixelModuleName module(modulename);
0152       if (module.modulename() != modulename) {
0153         std::cout << __LINE__ << "]\t" << mthn << "Modulename: " << modulename << std::endl;
0154         std::cout << __LINE__ << "]\t" << mthn << "Parsed to : " << module.modulename() << std::endl;
0155         assert(0);
0156       }
0157 
0158       PixelChannel channel(module, TBMChannel);
0159       std::pair<std::string, int> portcardAndAOH(portcardname, aoh);
0160       map_[channel] = portcardAndAOH;
0161     }
0162 
0163   } while (!in.eof());
0164 }
0165 
0166 /*const std::map<PixelModuleName, int>& PixelPortcardMap::modules(std::string portcard) const{
0167 
0168   std::map<std::string,std::map<PixelModuleName, int> >::const_iterator theportcard=map_.find(portcard);
0169 
0170   if (theportcard==map_.end()) {
0171     std::cout << "Could not find portcard with name:"<<portcard<<std::endl;
0172   }
0173 
0174   return theportcard->second;
0175 
0176 }*/
0177 
0178 PixelPortcardMap::~PixelPortcardMap() {}
0179 
0180 void PixelPortcardMap::writeASCII(std::string dir) const {
0181   std::string mthn = "[PixelPortcardMap::writeASCII()]\t\t\t\t    ";
0182   if (!dir.empty())
0183     dir += "/";
0184   string filename = dir + "portcardmap.dat";
0185 
0186   ofstream out(filename.c_str());
0187   if (!out.good()) {
0188     cout << __LINE__ << "]\t" << mthn << "Could not open file: " << filename << endl;
0189     assert(0);
0190   }
0191 
0192   out << "# Portcard          Module                     AOH channel" << endl;
0193   std::map<PixelChannel, std::pair<std::string, int> >::const_iterator i = map_.begin();
0194   for (; i != map_.end(); ++i) {
0195     out << i->second.first << "   " << i->first.module() << "       " << i->first.TBMChannel() << "       "
0196         << i->second.second << endl;
0197   }
0198   out.close();
0199 }
0200 
0201 const std::set<std::pair<std::string, int> > PixelPortcardMap::PortCardAndAOHs(const PixelModuleName &aModule) const {
0202   std::set<std::pair<std::string, int> > returnThis;
0203 
0204   // Loop over the entire map, searching for elements matching PixelModuleName.  Add matching elements to returnThis.
0205   for (std::map<PixelChannel, std::pair<std::string, int> >::const_iterator map_itr = map_.begin();
0206        map_itr != map_.end();
0207        ++map_itr) {
0208     if (map_itr->first.modulename() == aModule.modulename()) {
0209       returnThis.insert(map_itr->second);
0210     }
0211   }
0212 
0213   return returnThis;
0214 }
0215 
0216 // Added by Dario for Debbie (the PixelPortcardMap::portcards is way to slow for the interactive tool)
0217 bool PixelPortcardMap::getName(std::string moduleName, std::string &portcardName) {
0218   for (std::map<PixelChannel, std::pair<std::string, int> >::const_iterator map_itr = map_.begin();
0219        map_itr != map_.end();
0220        ++map_itr) {
0221     if (map_itr->first.modulename() == moduleName) {
0222       portcardName = map_itr->second.first;
0223       return true;
0224     }
0225   }
0226   return false;
0227 }
0228 
0229 const std::set<std::string> PixelPortcardMap::portcards(const PixelModuleName &aModule) const {
0230   std::set<std::string> returnThis;
0231   const std::set<std::pair<std::string, int> > portCardAndAOHs = PortCardAndAOHs(aModule);
0232   for (std::set<std::pair<std::string, int> >::const_iterator portCardAndAOHs_itr = portCardAndAOHs.begin();
0233        portCardAndAOHs_itr != portCardAndAOHs.end();
0234        ++portCardAndAOHs_itr) {
0235     returnThis.insert((*portCardAndAOHs_itr).first);
0236   }
0237   return returnThis;
0238 }
0239 
0240 const std::pair<std::string, int> PixelPortcardMap::PortCardAndAOH(const PixelModuleName &aModule,
0241                                                                    const PixelTBMChannel &TBMChannel) const {
0242   return PortCardAndAOH(PixelChannel(aModule, TBMChannel));
0243 }
0244 
0245 const std::pair<std::string, int> PixelPortcardMap::PortCardAndAOH(const PixelModuleName &aModule,
0246                                                                    const std::string &TBMChannel) const {
0247   return PortCardAndAOH(PixelChannel(aModule, TBMChannel));
0248 }
0249 
0250 const std::pair<std::string, int> PixelPortcardMap::PortCardAndAOH(const PixelChannel &aChannel) const {
0251   std::map<PixelChannel, std::pair<std::string, int> >::const_iterator found = map_.find(aChannel);
0252   if (found == map_.end()) {
0253     std::pair<std::string, int> returnThis("none", 0);
0254     return returnThis;
0255   } else {
0256     return found->second;
0257   }
0258 }
0259 
0260 std::set<PixelModuleName> PixelPortcardMap::modules(std::string portCardName) const {
0261   std::set<PixelModuleName> returnThis;
0262 
0263   // Loop over the entire map, searching for elements matching portCardName.  Add matching elements to returnThis.
0264   for (std::map<PixelChannel, std::pair<std::string, int> >::const_iterator map_itr = map_.begin();
0265        map_itr != map_.end();
0266        ++map_itr) {
0267     if (map_itr->second.first == portCardName) {
0268       returnThis.insert(map_itr->first.module());
0269     }
0270   }
0271 
0272   return returnThis;
0273 }
0274 
0275 std::set<std::string> PixelPortcardMap::portcards(const PixelDetectorConfig *detconfig) {
0276   std::set<std::string> returnThis;
0277 
0278   if (detconfig != nullptr) {
0279     //still done done in an awkward way, but this avoids an
0280     //double nested loop that we had in the first implementation
0281     const std::vector<PixelModuleName> &moduleList = detconfig->getModuleList();
0282     std::set<std::string> moduleNames;
0283     for (std::vector<PixelModuleName>::const_iterator it = moduleList.begin(), it_end = moduleList.end(); it != it_end;
0284          ++it) {
0285       moduleNames.insert(it->modulename());
0286     }
0287 
0288     for (std::map<PixelChannel, std::pair<std::string, int> >::const_iterator map_itr = map_.begin();
0289          map_itr != map_.end();
0290          ++map_itr) {
0291       if (moduleNames.find(map_itr->first.modulename()) != moduleNames.end()) {
0292         returnThis.insert(map_itr->second.first);
0293       }
0294     }
0295 
0296   } else {
0297     for (std::map<PixelChannel, std::pair<std::string, int> >::const_iterator map_itr = map_.begin();
0298          map_itr != map_.end();
0299          ++map_itr) {
0300       returnThis.insert(map_itr->second.first);
0301     }
0302   }
0303 
0304   return returnThis;
0305 }
0306 //=============================================================================================
0307 void PixelPortcardMap::writeXMLHeader(pos::PixelConfigKey key,
0308                                       int version,
0309                                       std::string path,
0310                                       std::ofstream *outstream,
0311                                       std::ofstream *out1stream,
0312                                       std::ofstream *out2stream) const {
0313   std::string mthn = "[PixelPortcardMap::writeXMLHeader()]\t\t\t    ";
0314   std::stringstream fullPath;
0315   fullPath << path << "/Pixel_PortCardMap_" << PixelTimeFormatter::getmSecTime() << ".xml";
0316   std::cout << __LINE__ << "]\t" << mthn << "Writing to: " << fullPath.str() << std::endl;
0317 
0318   outstream->open(fullPath.str().c_str());
0319 
0320   *outstream << "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" << std::endl;
0321   *outstream << "<ROOT xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>" << std::endl;
0322   *outstream << " <HEADER>" << std::endl;
0323   *outstream << "  <TYPE>" << std::endl;
0324   *outstream << "   <EXTENSION_TABLE_NAME>PIXEL_PORTCARD_MAP</EXTENSION_TABLE_NAME>" << std::endl;
0325   *outstream << "   <NAME>Pixel Port Card Map</NAME>" << std::endl;
0326   *outstream << "  </TYPE>" << std::endl;
0327   *outstream << "  <RUN>" << std::endl;
0328   *outstream << "   <RUN_TYPE>Pixel Port Card Map</RUN_TYPE>" << std::endl;
0329   *outstream << "   <RUN_NUMBER>1</RUN_NUMBER>" << std::endl;
0330   *outstream << "   <RUN_BEGIN_TIMESTAMP>" << pos::PixelTimeFormatter::getTime() << "</RUN_BEGIN_TIMESTAMP>"
0331              << std::endl;
0332   *outstream << "   <LOCATION>CERN P5</LOCATION>" << std::endl;
0333   *outstream << "  </RUN>" << std::endl;
0334   *outstream << " </HEADER>" << std::endl;
0335   *outstream << "" << std::endl;
0336   *outstream << " <DATA_SET>" << std::endl;
0337   *outstream << "  <PART>" << std::endl;
0338   *outstream << "   <NAME_LABEL>CMS-PIXEL-ROOT</NAME_LABEL>" << std::endl;
0339   *outstream << "   <KIND_OF_PART>Detector ROOT</KIND_OF_PART>" << std::endl;
0340   *outstream << "  </PART>" << std::endl;
0341   *outstream << "  <VERSION>" << version << "</VERSION>" << std::endl;
0342   *outstream << "  <COMMENT_DESCRIPTION>" << getComment() << "</COMMENT_DESCRIPTION>" << std::endl;
0343   *outstream << "  <CREATED_BY_USER>" << getAuthor() << "</CREATED_BY_USER>" << std::endl;
0344 }
0345 
0346 //=============================================================================================
0347 void PixelPortcardMap::writeXML(std::ofstream *outstream, std::ofstream *out1stream, std::ofstream *out2stream) const {
0348   std::string mthn = "[PixelPortcardMap::writeXML()]\t\t\t    ";
0349 
0350   std::map<PixelChannel, std::pair<std::string, int> >::const_iterator i = map_.begin();
0351   for (; i != map_.end(); ++i) {
0352     *outstream << "  <DATA>" << std::endl;
0353     *outstream << "   <PORT_CARD>" << i->second.first << "</PORT_CARD>" << std::endl;
0354     *outstream << "   <PANEL_NAME>" << i->first.module() << "</PANEL_NAME>" << std::endl;
0355     *outstream << "   <TBM_MODE>" << i->first.TBMChannel() << "</TBM_MODE>" << std::endl;
0356     *outstream << "   <AOH_CHAN>" << i->second.second << "</AOH_CHAN>" << std::endl;
0357     *outstream << "  </DATA>" << std::endl;
0358   }
0359 }
0360 
0361 //=============================================================================================
0362 void PixelPortcardMap::writeXMLTrailer(std::ofstream *outstream,
0363                                        std::ofstream *out1stream,
0364                                        std::ofstream *out2stream) const {
0365   std::string mthn = "[PixelPortcardMap::writeXMLTrailer()]\t\t\t    ";
0366 
0367   *outstream << " </DATA_SET>" << std::endl;
0368   *outstream << "</ROOT> " << std::endl;
0369 
0370   outstream->close();
0371 }