Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-09-07 04:34:55

0001 //
0002 // This class stores the name and related
0003 // hardware mapings for a ROC
0004 //
0005 //
0006 
0007 #include "CalibFormats/SiPixelObjects/interface/PixelNameTranslation.h"
0008 #include "CalibFormats/SiPixelObjects/interface/PixelDetectorConfig.h"
0009 #include "CalibFormats/SiPixelObjects/interface/PixelTimeFormatter.h"
0010 //#include "PixelUtilities/PixelTestStandUtilities/include/PixelTimer.h"
0011 #include <fstream>
0012 #include <sstream>
0013 #include <map>
0014 #include <string>
0015 #include <vector>
0016 #include <cassert>
0017 #include <stdexcept>
0018 
0019 using namespace pos;
0020 using namespace std;
0021 
0022 PixelNameTranslation::PixelNameTranslation(std::vector<std::vector<std::string> >& tableMat)
0023     : PixelConfigBase(" ", " ", " ") {
0024   std::string mthn = "[PixelNameTranslation::PixelNameTranslation()]\t\t    ";
0025   std::map<std::string, int> colM;
0026   std::vector<std::string> colNames;
0027 
0028   /*
0029   EXTENSION_TABLE_NAME: PIXEL_NAME_TRANS (VIEW: CONF_KEY_NAME_TRANS_V)
0030 
0031   CONFIG_KEY                    NOT NULL VARCHAR2(80)
0032   KEY_TYPE                  NOT NULL VARCHAR2(80)
0033   KEY_ALIAS                 NOT NULL VARCHAR2(80)
0034   VERSION                        VARCHAR2(40)
0035   KIND_OF_COND                  NOT NULL VARCHAR2(40)
0036   ROC_NAME                  NOT NULL VARCHAR2(200)
0037   PXLFEC_NAME                   NOT NULL NUMBER(38)
0038   MFEC_POSN                 NOT NULL NUMBER(38)
0039   MFEC_CHAN                 NOT NULL NUMBER(38)
0040   HUB_ADDRS                      NUMBER(38)
0041   PORT_NUM                  NOT NULL NUMBER(38)
0042   ROC_I2C_ADDR                  NOT NULL NUMBER(38)
0043   PXLFED_NAME                   NOT NULL NUMBER(38)
0044   FED_CHAN                  NOT NULL NUMBER(38)
0045   FED_ROC_NUM                   NOT NULL NUMBER(38)
0046   TBM_MODE                       VARCHAR2(200)
0047 */
0048 
0049   colNames.push_back("CONFIG_KEY");    //0
0050   colNames.push_back("KEY_TYPE");      //1
0051   colNames.push_back("KEY_ALIAS");     //2
0052   colNames.push_back("VERSION");       //3
0053   colNames.push_back("KIND_OF_COND");  //4
0054   colNames.push_back("ROC_NAME");      //5
0055   colNames.push_back("PXLFEC_NAME");   //6
0056   colNames.push_back("MFEC_POSN");     //7
0057   colNames.push_back("MFEC_CHAN");     //8
0058   colNames.push_back("HUB_ADDRS");     //9
0059   colNames.push_back("PORT_NUM");      //10
0060   colNames.push_back("ROC_I2C_ADDR");  //11
0061   colNames.push_back("PXLFED_NAME");   //12
0062   colNames.push_back("FED_CHAN");      //13
0063   colNames.push_back("FED_ROC_NUM");   //14
0064   colNames.push_back("TBM_MODE");      //15
0065 
0066   for (unsigned int c = 0; c < tableMat[0].size(); c++) {
0067     for (unsigned int n = 0; n < colNames.size(); n++) {
0068       if (tableMat[0][c] == colNames[n]) {
0069         colM[colNames[n]] = c;
0070         break;
0071       }
0072     }
0073   }  //end for
0074   /*
0075   for(unsigned int n=0; n<colNames.size(); n++){
0076     if(colM.find(colNames[n]) == colM.end()){
0077       std::cerr << __LINE__ << "]\t" << mthn << "Couldn't find in the database the column with name " << colNames[n] << std::endl;
0078       assert(0);
0079     }
0080   }
0081   */
0082 
0083   for (unsigned int r = 1; r < tableMat.size(); r++) {  //Goes to every row of the Matrix
0084     std::string rocname = tableMat[r][colM["ROC_NAME"]];
0085     std::string TBMChannel = tableMat[r][colM["TBM_MODE"]];  // assert(0); // need to add this to the input table
0086     if (TBMChannel.empty()) {
0087       TBMChannel = "A";
0088     }
0089     /* // modified by MR on 13-07-2008 11:32:50
0090       Umesh changed the content of the column and 
0091       stripped out the FPix_Pxl_FEC_ part of the "number"
0092    tableMat[r][colM["PXLFEC_NAME"]].erase(0 , 13);//PIXFEC
0093    unsigned int fecnumber    = (unsigned int)atoi(tableMat[r][colM["PXLFEC_NAME"]].c_str());
0094    */
0095     unsigned int fecnumber = (unsigned int)atoi(tableMat[r][colM["PXLFEC_NAME"]].c_str());
0096     unsigned int mfec = (unsigned int)atoi(tableMat[r][colM["MFEC_POSN"]].c_str());
0097     unsigned int mfecchannel = (unsigned int)atoi(tableMat[r][colM["MFEC_CHAN"]].c_str());
0098     unsigned int hubaddress = (unsigned int)atoi(tableMat[r][colM["HUB_ADDRS"]].c_str());
0099     unsigned int portaddress = (unsigned int)atoi(tableMat[r][colM["PORT_NUM"]].c_str());
0100     unsigned int rocid = (unsigned int)atoi(tableMat[r][colM["ROC_I2C_ADDR"]].c_str());
0101     // modified by MR on 13-07-2008 11:47:32
0102     /* Umesh changed the content of the column and 
0103       stripped out the PxlFED_ part of the "number"
0104      
0105    tableMat[r][colM["PXLFED_NAME"]].erase(0,7);//FED
0106    */
0107     unsigned int fednumber = (unsigned int)atoi(tableMat[r][colM["PXLFED_NAME"]].c_str());
0108     unsigned int fedchannel = (unsigned int)atoi(tableMat[r][colM["FED_CHAN"]].c_str());
0109     unsigned int fedrocnumber = (unsigned int)(atoi(tableMat[r][colM["FED_ROC_NUM"]].c_str()));
0110 
0111     PixelROCName aROC(rocname);
0112     if (aROC.rocname() != rocname) {
0113       std::cout << __LINE__ << "]\t" << mthn << "Rocname  : " << rocname << std::endl;
0114       std::cout << __LINE__ << "]\t" << mthn << "Parsed to: " << aROC.rocname() << std::endl;
0115       assert(0);
0116     }
0117 
0118     if (ROCNameFromFEDChannelROCExists(fednumber, fedchannel, fedrocnumber)) {
0119       std::cout << __LINE__ << "]\t" << mthn << "ROC with fednumber=" << fednumber << " fedchannel=" << fedchannel
0120                 << " roc number=" << fedrocnumber << " already exists" << std::endl;
0121       std::cout << __LINE__ << "]\t" << mthn << "Fix this inconsistency in the name translation" << std::endl;
0122       assert(0);
0123     }
0124 
0125     PixelHdwAddress hdwAdd(
0126         fecnumber, mfec, mfecchannel, hubaddress, portaddress, rocid, fednumber, fedchannel, fedrocnumber);
0127     //    std::cout << "[PixelNameTranslation::PixelNameTranslation()] aROC: " << aROC << std::endl;
0128     translationtable_[aROC] = hdwAdd;
0129     fedlookup_[hdwAdd] = aROC;
0130 
0131     PixelModuleName aModule(rocname);
0132     PixelChannel aChannel(aModule, TBMChannel);
0133 
0134     hdwTranslationTable_[hdwAdd] = aChannel;
0135 
0136     // Look for this channel in channelTransaltionTable.  If it is found, check that the hardware address agrees.  If not, add it to the table.  Also, if another channel on that module is found, check that the FEC part agrees, and the FED part doesn't.
0137     bool foundChannel = false;
0138 
0139     std::map<PixelChannel, PixelHdwAddress>::const_iterator channelTranslationTable_itr =
0140         channelTranslationTable_.find(aChannel);
0141 
0142     if (channelTranslationTable_itr != channelTranslationTable_.end()) {
0143       if (!(channelTranslationTable_itr->second |= hdwAdd)) {
0144         cout << "Found two ROCs on the same channe, but not same hdw" << endl;
0145         cout << "Hdw1:" << endl << channelTranslationTable_itr->second << endl;
0146         cout << "Hdw2:" << endl << hdwAdd << endl;
0147       }
0148       assert(channelTranslationTable_itr->second |= hdwAdd);
0149       foundChannel = true;
0150     } else if (channelTranslationTable_itr->first.module() == aModule) {
0151       assert(channelTranslationTable_itr->second.fecnumber() == hdwAdd.fecnumber());
0152       assert(channelTranslationTable_itr->second.mfec() == hdwAdd.mfec());
0153       assert(channelTranslationTable_itr->second.mfecchannel() == hdwAdd.mfecchannel());
0154       //assert( channelTranslationTable_itr->second.portaddress() == hdwAdd.portaddress() );
0155       assert(channelTranslationTable_itr->second.hubaddress() == hdwAdd.hubaddress());
0156       assert(channelTranslationTable_itr->second.fednumber() != hdwAdd.fednumber() ||
0157              channelTranslationTable_itr->second.fedchannel() != hdwAdd.fedchannel());
0158     }
0159 
0160     if (foundChannel == false) {
0161       channelTranslationTable_[aChannel] = hdwAdd;
0162     }
0163 
0164   }  //end for r
0165 
0166   const std::map<unsigned int, std::set<unsigned int> > fedsAndChannels = getFEDsAndChannels();
0167 
0168   std::vector<PixelROCName> tmp(24);
0169 
0170   std::map<unsigned int, std::map<unsigned int, int> > counter;
0171   //       FED id                  FED channel
0172 
0173   std::map<unsigned int, std::map<unsigned int, int> > maxindex;
0174 
0175   std::map<PixelROCName, PixelHdwAddress>::const_iterator it = translationtable_.begin();
0176 
0177   for (; it != translationtable_.end(); ++it) {
0178     int index = it->second.fedrocnumber();
0179 
0180     unsigned int fednumber = it->second.fednumber();
0181     unsigned int fedchannel = it->second.fedchannel();
0182 
0183     std::vector<PixelROCName>& tmp = rocsFromFEDidAndChannel_[fednumber][fedchannel];
0184 
0185     if (tmp.empty()) {
0186       tmp.resize(24);
0187       counter[fednumber][fedchannel] = 0;
0188       maxindex[fednumber][fedchannel] = 0;
0189     }
0190 
0191     if (index > maxindex[fednumber][fedchannel])
0192       maxindex[fednumber][fedchannel] = index;
0193     tmp[index] = it->first;
0194     counter[fednumber][fedchannel]++;
0195   }
0196 
0197   it = translationtable_.begin();
0198 
0199   for (; it != translationtable_.end(); ++it) {
0200     unsigned int fednumber = it->second.fednumber();
0201     unsigned int fedchannel = it->second.fedchannel();
0202 
0203     std::vector<PixelROCName>& tmp = rocsFromFEDidAndChannel_[fednumber][fedchannel];
0204 
0205     assert(counter[fednumber][fedchannel] == maxindex[fednumber][fedchannel] + 1);
0206 
0207     tmp.resize(counter[fednumber][fedchannel]);
0208   }
0209 
0210 }  //end contructor
0211 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
0212 
0213 PixelNameTranslation::PixelNameTranslation(std::string filename) : PixelConfigBase(" ", " ", " ") {
0214   static std::string mthn = "[PixelNameTranslation::PixelNameTranslation()]\t\t    ";
0215 
0216   std::ifstream in(filename.c_str());
0217 
0218   if (!in.good()) {
0219     std::cout << __LINE__ << "]\t" << mthn << "Could not open: " << filename << std::endl;
0220     throw std::runtime_error("Failed to open file " + filename);
0221   } else {
0222     std::cout << __LINE__ << "]\t" << mthn << "Reading from: " << filename << std::endl;
0223   }
0224 
0225   std::string dummy;
0226 
0227   getline(in, dummy);  // skip the column headings
0228 
0229   do {
0230     std::string rocname;
0231     std::string TBMChannel;
0232     unsigned int fecnumber;
0233     unsigned int mfec;
0234     unsigned int mfecchannel;
0235     unsigned int hubaddress;
0236     unsigned int portaddress;
0237     unsigned int rocid;
0238     unsigned int fednumber;
0239     unsigned int fedchannel;
0240     unsigned int fedrocnumber;
0241 
0242     in >> rocname;
0243     in >> TBMChannel;
0244     if (TBMChannel != "A" &&
0245         TBMChannel !=
0246             "B")  // no TBM channel was specified, so default to A and set fecnumber to the value of this string
0247     {
0248       fecnumber = atoi(TBMChannel.c_str());
0249       TBMChannel = "A";
0250     } else  // TBM channel was specified, now read fecnumber
0251     {
0252       in >> fecnumber;
0253     }
0254     in >> mfec >> mfecchannel >> hubaddress >> portaddress >> rocid >> fednumber >> fedchannel >> fedrocnumber;
0255 
0256     if (!in.eof()) {
0257       PixelROCName aROC(rocname);
0258 
0259       //debug
0260       //if (aROC.rocname()!=rocname){
0261       //std::cout << __LINE__ << "]\t" << mthn << "Rocname  : " << rocname        << std::endl;
0262       //std::cout << __LINE__ << "]\t" << mthn << "Parsed to: " << aROC.rocname() << std::endl;
0263       //assert(0);
0264       //}
0265 
0266       if (ROCNameFromFEDChannelROCExists(fednumber, fedchannel, fedrocnumber)) {
0267         std::cout << __LINE__ << "]\t" << mthn << "ROC with fednumber=" << fednumber << " fedchannel=" << fedchannel
0268                   << " roc number=" << fedrocnumber << " already exists" << std::endl;
0269         std::cout << __LINE__ << "]\t" << mthn << "Fix this inconsistency in the name translation" << std::endl;
0270         assert(0);
0271       }
0272 
0273       PixelHdwAddress hdwAdd(
0274           fecnumber, mfec, mfecchannel, hubaddress, portaddress, rocid, fednumber, fedchannel, fedrocnumber);
0275       //std::cout << aROC << std::endl;
0276       // modified by MR on 18-01-2008 11:18:53
0277       //       std::cout << hdwAdd << std::endl ;
0278       //cout << "[PixelNameTranslation::PixelNameTranslation()]\t\t-----------------------------"  << endl ;
0279       // A fecnumber
0280       // B mfec
0281       // C mfecchannel
0282       // D hubaddress
0283       // E portaddress
0284       // F rocid
0285       // G fednumber
0286       // H fedchannel
0287       // I fedrocnumber
0288       //cout << "[PixelNameTranslation::PixelNameTranslation()]\t\t"
0289       //     << " A " << fecnumber
0290       //     << " B " << mfec
0291       //     << " C " << mfecchannel
0292       //     << " D " << hubaddress
0293       //     << " E " << portaddress
0294       //     << " F " << rocid
0295       //     << " G " << fednumber
0296       //     << " H " << fedchannel
0297       //     << " I " << fedrocnumber << endl ;
0298 
0299       translationtable_[aROC] = hdwAdd;
0300       fedlookup_[hdwAdd] = aROC;
0301 
0302       PixelModuleName aModule(rocname);
0303       PixelChannel aChannel(aModule, TBMChannel);
0304 
0305       hdwTranslationTable_[hdwAdd] = aChannel;
0306 
0307       // Look for this channel in channelTransaltionTable.  If it is found,
0308       // check that the hardware address agrees.  If not, add it to the table.
0309       // Also, if another channel on that module is found, check that the FEC
0310       // part agrees, and the FED part doesn't.
0311       bool foundChannel = false;
0312 
0313       std::map<PixelChannel, PixelHdwAddress>::const_iterator channelTranslationTable_itr =
0314           channelTranslationTable_.find(aChannel);
0315 
0316       if (channelTranslationTable_itr != channelTranslationTable_.end()) {
0317         if (!(channelTranslationTable_itr->second |= hdwAdd)) {
0318           cout << __LINE__ << "]\t" << mthn << "Found two ROCs on the same channe, but not same hdw" << endl;
0319           cout << __LINE__ << "]\t" << mthn << "Hdw1: " << endl << channelTranslationTable_itr->second << endl;
0320           cout << __LINE__ << "]\t" << mthn << "Hdw2: " << endl << hdwAdd << endl;
0321         }
0322         assert(channelTranslationTable_itr->second |= hdwAdd);
0323         foundChannel = true;
0324       } else if (channelTranslationTable_itr->first.module() == aModule) {
0325         assert(channelTranslationTable_itr->second.fecnumber() == hdwAdd.fecnumber());
0326         assert(channelTranslationTable_itr->second.mfec() == hdwAdd.mfec());
0327         assert(channelTranslationTable_itr->second.mfecchannel() == hdwAdd.mfecchannel());
0328         //assert( channelTranslationTable_itr->second.portaddress() == hdwAdd.portaddress() );
0329         assert(channelTranslationTable_itr->second.hubaddress() == hdwAdd.hubaddress());
0330         assert(channelTranslationTable_itr->second.fednumber() != hdwAdd.fednumber() ||
0331                channelTranslationTable_itr->second.fedchannel() != hdwAdd.fedchannel());
0332       }
0333 
0334       if (foundChannel == false) {
0335         channelTranslationTable_[aChannel] = hdwAdd;
0336       }
0337     }
0338   } while (!in.eof());
0339   in.close();
0340 
0341   const std::map<unsigned int, std::set<unsigned int> > fedsAndChannels = getFEDsAndChannels();
0342 
0343   std::vector<PixelROCName> tmp(24);
0344 
0345   std::map<unsigned int, std::map<unsigned int, int> > counter;
0346   //       FED id                  FED channel
0347 
0348   std::map<unsigned int, std::map<unsigned int, int> > maxindex;
0349 
0350   std::map<PixelROCName, PixelHdwAddress>::const_iterator it = translationtable_.begin();
0351 
0352   for (; it != translationtable_.end(); ++it) {
0353     int index = it->second.fedrocnumber();
0354 
0355     unsigned int fednumber = it->second.fednumber();
0356     unsigned int fedchannel = it->second.fedchannel();
0357 
0358     std::vector<PixelROCName>& tmp = rocsFromFEDidAndChannel_[fednumber][fedchannel];
0359 
0360     if (tmp.empty()) {
0361       tmp.resize(24);
0362       counter[fednumber][fedchannel] = 0;
0363       maxindex[fednumber][fedchannel] = 0;
0364     }
0365 
0366     if (index > maxindex[fednumber][fedchannel])
0367       maxindex[fednumber][fedchannel] = index;
0368     tmp[index] = it->first;
0369     counter[fednumber][fedchannel]++;
0370   }
0371 
0372   it = translationtable_.begin();
0373 
0374   for (; it != translationtable_.end(); ++it) {
0375     unsigned int fednumber = it->second.fednumber();
0376     unsigned int fedchannel = it->second.fedchannel();
0377 
0378     std::vector<PixelROCName>& tmp = rocsFromFEDidAndChannel_[fednumber][fedchannel];
0379 
0380     assert(counter[fednumber][fedchannel] == maxindex[fednumber][fedchannel] + 1);
0381 
0382     tmp.resize(counter[fednumber][fedchannel]);
0383   }
0384 }
0385 
0386 std::ostream& operator<<(std::ostream& s, const PixelNameTranslation& table) {
0387   //for (unsigned int i=0;i<table.translationtable_.size();i++){
0388   //    s << table.translationtable_[i]<<std::endl;
0389   //   }
0390   return s;
0391 }
0392 
0393 std::list<const PixelROCName*> PixelNameTranslation::getROCs() const {
0394   std::list<const PixelROCName*> listOfROCs;
0395   for (std::map<PixelROCName, PixelHdwAddress>::const_iterator translationTableEntry = translationtable_.begin();
0396        translationTableEntry != translationtable_.end();
0397        ++translationTableEntry) {
0398     listOfROCs.push_back(&(translationTableEntry->first));
0399   }
0400 
0401   return listOfROCs;
0402 }
0403 
0404 std::list<const PixelModuleName*> PixelNameTranslation::getModules() const {
0405   std::list<const PixelModuleName*> listOfModules;
0406   for (std::map<PixelChannel, PixelHdwAddress>::const_iterator channelTranslationTable_itr =
0407            channelTranslationTable_.begin();
0408        channelTranslationTable_itr != channelTranslationTable_.end();
0409        ++channelTranslationTable_itr) {
0410     bool foundOne = false;
0411     for (std::list<const PixelModuleName*>::const_iterator listOfModules_itr = listOfModules.begin();
0412          listOfModules_itr != listOfModules.end();
0413          ++listOfModules_itr) {
0414       if (*(*listOfModules_itr) == channelTranslationTable_itr->first.module()) {
0415         foundOne = true;
0416         break;
0417       }
0418     }
0419     if (!foundOne)
0420       listOfModules.push_back(&(channelTranslationTable_itr->first.module()));
0421   }
0422 
0423   return listOfModules;
0424 }
0425 
0426 std::set<PixelChannel> PixelNameTranslation::getChannels() const {
0427   std::set<PixelChannel> channelSet;
0428   for (std::map<PixelChannel, PixelHdwAddress>::const_iterator channelTranslationTable_itr =
0429            channelTranslationTable_.begin();
0430        channelTranslationTable_itr != channelTranslationTable_.end();
0431        ++channelTranslationTable_itr) {
0432     channelSet.insert(channelTranslationTable_itr->first);
0433   }
0434   return channelSet;
0435 }
0436 
0437 std::set<PixelChannel> PixelNameTranslation::getChannels(const PixelDetectorConfig& aDetectorConfig) const {
0438   std::set<PixelChannel> channelSet;
0439   for (std::map<PixelChannel, PixelHdwAddress>::const_iterator channelTranslationTable_itr =
0440            channelTranslationTable_.begin();
0441        channelTranslationTable_itr != channelTranslationTable_.end();
0442        ++channelTranslationTable_itr) {
0443     if (aDetectorConfig.containsModule(channelTranslationTable_itr->first.module()))
0444       channelSet.insert(channelTranslationTable_itr->first);
0445   }
0446   return channelSet;
0447 }
0448 
0449 const PixelHdwAddress* PixelNameTranslation::getHdwAddress(const PixelROCName& aROC) const {
0450   static std::string mthn = "[PixelNameTranslation::getHdwAddress()]\t\t    ";
0451   std::map<PixelROCName, PixelHdwAddress>::const_iterator it = translationtable_.find(aROC);
0452 
0453   if (it == translationtable_.end()) {
0454     std::cout << __LINE__ << "]\t" << mthn << "Could not look up ROC: " << aROC << std::endl;
0455     assert(0);
0456   }
0457 
0458   return &(it->second);
0459 }
0460 
0461 // Added for Debbie (used there only) to allow integrity checks (Dario)
0462 bool PixelNameTranslation::checkROCExistence(const PixelROCName& aROC) const {
0463   std::string mthn = "[PixelNameTranslation::checkROCExistence()]\t\t    ";
0464   if (translationtable_.find(aROC) == translationtable_.end())
0465     return false;
0466   return true;
0467 }
0468 
0469 const bool PixelNameTranslation::checkFor(const PixelROCName& aROC) const {
0470   if (translationtable_.find(aROC) == translationtable_.end()) {
0471     return false;
0472   } else {
0473     return true;
0474   }
0475 }
0476 
0477 const PixelHdwAddress& PixelNameTranslation::getHdwAddress(const PixelChannel& aChannel) const {
0478   std::map<PixelChannel, PixelHdwAddress>::const_iterator channelHdwAddress_itr =
0479       channelTranslationTable_.find(aChannel);
0480   assert(channelHdwAddress_itr != channelTranslationTable_.end());
0481   return channelHdwAddress_itr->second;
0482 }
0483 
0484 const PixelHdwAddress& PixelNameTranslation::firstHdwAddress(const PixelModuleName& aModule) const {
0485   std::string mthn = "[PixelNameTranslation::firstHdwAddress()]\t\t    ";
0486   std::set<PixelChannel> channelsOnModule = getChannelsOnModule(aModule);
0487   if (channelsOnModule.empty()) {
0488     cout << __LINE__ << "]\t" << mthn << "module=" << aModule << " has zero channels!" << endl;
0489     cout << __LINE__ << "]\t" << mthn << "Will terminate" << endl;
0490     ::abort();
0491   }
0492   std::set<PixelChannel>::const_iterator firstChannel = channelsOnModule.begin();
0493   assert(firstChannel != channelsOnModule.end());
0494   return getHdwAddress(*firstChannel);
0495 }
0496 
0497 const PixelChannel& PixelNameTranslation::getChannelForROC(const PixelROCName& aROC) const {
0498   std::map<PixelROCName, PixelHdwAddress>::const_iterator foundEntry = translationtable_.find(aROC);
0499   assert(foundEntry != translationtable_.end());
0500   return getChannelFromHdwAddress(foundEntry->second);
0501 }
0502 
0503 std::set<PixelChannel> PixelNameTranslation::getChannelsOnModule(const PixelModuleName& aModule) const {
0504   std::set<PixelChannel> returnThis;
0505   for (std::map<PixelChannel, PixelHdwAddress>::const_iterator channelTranslationTable_itr =
0506            channelTranslationTable_.begin();
0507        channelTranslationTable_itr != channelTranslationTable_.end();
0508        ++channelTranslationTable_itr) {
0509     if (channelTranslationTable_itr->first.module() == aModule)
0510       returnThis.insert(channelTranslationTable_itr->first);
0511   }
0512   assert(returnThis.size() <= 2);
0513   return returnThis;
0514 }
0515 
0516 const std::vector<PixelROCName>& PixelNameTranslation::getROCsFromFEDChannel(unsigned int fednumber,
0517                                                                              unsigned int fedchannel) const {
0518   std::map<unsigned int, std::map<unsigned int, std::vector<PixelROCName> > >::const_iterator it =
0519       rocsFromFEDidAndChannel_.find(fednumber);
0520 
0521   assert(it != rocsFromFEDidAndChannel_.end());
0522 
0523   std::map<unsigned int, std::vector<PixelROCName> >::const_iterator it2 = it->second.find(fedchannel);
0524 
0525   assert(it2 != it->second.end());
0526 
0527   return it2->second;
0528 }
0529 
0530 //Will return ROC names sorted by FED readout order.
0531 //This (private) method will be called once to build this list
0532 //when the data is read in.
0533 
0534 std::vector<PixelROCName> PixelNameTranslation::buildROCsFromFEDChannel(unsigned int fednumber,
0535                                                                         unsigned int fedchannel) const {
0536   std::vector<PixelROCName> tmp(24);
0537 
0538   int counter = 0;
0539 
0540   int maxindex = 0;
0541 
0542   std::map<PixelROCName, PixelHdwAddress>::const_iterator it = translationtable_.begin();
0543 
0544   for (; it != translationtable_.end(); ++it) {
0545     if (it->second.fednumber() == fednumber && it->second.fedchannel() == fedchannel) {
0546       int index = it->second.fedrocnumber();
0547       if (index > maxindex)
0548         maxindex = index;
0549       //std::cout << "Found one:"<<index<<" "<<it->first<<std::endl;
0550       tmp[index] = it->first;
0551       counter++;
0552     }
0553   }
0554 
0555   assert(counter == maxindex + 1);
0556 
0557   tmp.resize(counter);
0558 
0559   return tmp;
0560 }
0561 
0562 bool PixelNameTranslation::ROCNameFromFEDChannelROCExists(unsigned int fednumber,
0563                                                           unsigned int channel,
0564                                                           unsigned int roc) const {
0565   PixelHdwAddress tmp(0, 0, 0, 0, 0, 0, fednumber, channel, roc);
0566 
0567   return (fedlookup_.find(tmp) != fedlookup_.end());
0568 }
0569 
0570 PixelROCName PixelNameTranslation::ROCNameFromFEDChannelROC(unsigned int fednumber,
0571                                                             unsigned int channel,
0572                                                             unsigned int roc) const {
0573   std::string mthn = "[PixelNameTranslation::ROCNameFromFEDChannelROC()]\t\t    ";
0574   PixelHdwAddress tmp(0, 0, 0, 0, 0, 0, fednumber, channel, roc);
0575 
0576   std::map<PixelHdwAddress, PixelROCName, PixelHdwAddress>::const_iterator it1 = fedlookup_.find(tmp);
0577 
0578   if (it1 != fedlookup_.end()) {
0579     return it1->second;
0580   }
0581 
0582   std::cout << __LINE__ << "]\t" << mthn << "could not find ROCName "
0583             << " for FED#" << fednumber << " chan=" << channel << " roc#=" << roc << std::endl;
0584 
0585   assert(0);
0586 
0587   PixelROCName tmp1;
0588 
0589   return tmp1;
0590 }
0591 
0592 PixelChannel PixelNameTranslation::ChannelFromFEDChannel(unsigned int fednumber, unsigned int fedchannel) const {
0593   std::string mthn = "[PixelNameTranslation::ChannelFromFEDChannel()]\t\t    ";
0594   std::map<PixelChannel, PixelHdwAddress>::const_iterator toReturn;
0595   bool foundOne = false;
0596   for (std::map<PixelChannel, PixelHdwAddress>::const_iterator it = channelTranslationTable_.begin();
0597        it != channelTranslationTable_.end();
0598        ++it) {
0599     if (it->second.fednumber() == fednumber && it->second.fedchannel() == fedchannel) {
0600       if (foundOne) {
0601         std::cout << __LINE__ << "]\t" << mthn << "ERROR: multiple channels on FED#" << fednumber
0602                   << ", chan=" << fedchannel << std::endl;
0603         assert(0);
0604       } else {
0605         toReturn = it;
0606         foundOne = true;
0607       }
0608     }
0609   }
0610 
0611   if (!foundOne) {
0612     std::cout << __LINE__ << "]\t" << mthn << "ERROR: no channel found for FED#" << fednumber << ", chan=" << fedchannel
0613               << std::endl;
0614     assert(0);
0615   }
0616 
0617   return toReturn->first;
0618 }
0619 
0620 bool PixelNameTranslation::FEDChannelExist(unsigned int fednumber, unsigned int fedchannel) const {
0621   std::string mthn = "[PixelNameTranslation::FEDChannelExist()]\t\t    ";
0622   std::map<PixelChannel, PixelHdwAddress>::const_iterator toReturn;
0623   bool foundOne = false;
0624   for (std::map<PixelChannel, PixelHdwAddress>::const_iterator it = channelTranslationTable_.begin();
0625        it != channelTranslationTable_.end();
0626        ++it) {
0627     if (it->second.fednumber() == fednumber && it->second.fedchannel() == fedchannel) {
0628       if (foundOne) {
0629         std::cout << __LINE__ << "]\t" << mthn << "ERROR: multiple channels on FED#" << fednumber
0630                   << ", chan=" << fedchannel << std::endl;
0631         assert(0);
0632       } else {
0633         toReturn = it;
0634         foundOne = true;
0635       }
0636     }
0637   }
0638   return foundOne;
0639 }
0640 
0641 const PixelChannel& PixelNameTranslation::getChannelFromHdwAddress(const PixelHdwAddress& aHdwAddress) const {
0642   // modified by MR on 30-01-2008 10:38:22
0643   std::string mthn = "[PixelNameTranslation::getChannelFromHdwAddress()]\t\t    ";
0644 
0645   std::map<PixelHdwAddress, PixelChannel>::const_iterator it = hdwTranslationTable_.find(aHdwAddress);
0646 
0647   if (it == hdwTranslationTable_.end()) {
0648     std::cout << __LINE__ << "]\t" << mthn << "ERROR: no channel found for hardware address " << aHdwAddress
0649               << std::endl;
0650     assert(0);
0651   }
0652 
0653   return it->second;
0654 
0655   /*
0656     for ( std::map<PixelChannel, PixelHdwAddress >::const_iterator channelTranslationTable_itr = channelTranslationTable_.begin(); 
0657     channelTranslationTable_itr != channelTranslationTable_.end(); channelTranslationTable_itr++ )
0658      {
0659       if ( aHdwAddress |= channelTranslationTable_itr->second )
0660     {
0661       return channelTranslationTable_itr->first;
0662     }
0663      }
0664 // modified by MR on 30-01-2008 13:56:34
0665 // if you get here then there was NO match on the previous loop!!
0666   std::cout << __LINE__ << "]\t" << mthn 
0667             << "ERROR: no channel found for hardware address " << aHdwAddress << std::endl;
0668   assert(0);
0669   */
0670 }
0671 
0672 void PixelNameTranslation::writeASCII(std::string dir) const {
0673   std::string mthn = "[PixelNameTranslation::writeASCII()]\t\t\t    ";
0674   if (!dir.empty())
0675     dir += "/";
0676   std::string filename = dir + "translation.dat";
0677 
0678   std::ofstream out(filename.c_str());
0679 
0680   //std::cout << "[PixelNameTranslation::writeASCII()]\t\tfilename: "
0681   //        << filename
0682   //        << " status: "
0683   //        << out
0684   //        << "   "
0685   //        << out.is_open()
0686   //        <<endl ;
0687 
0688   out << "# name                          TBMchannel  FEC      mfec  mfecchannel hubaddress portadd rocid     FED     "
0689          "channel     roc#"
0690       << endl;
0691 
0692   std::map<PixelROCName, PixelHdwAddress>::const_iterator iroc = translationtable_.begin();
0693 
0694   for (; iroc != translationtable_.end(); ++iroc) {
0695     // Find the PixelChannel for this ROC, in order to get the TBM channel.
0696     std::string TBMChannel = getChannelFromHdwAddress(iroc->second).TBMChannelString();
0697 
0698     out << iroc->first.rocname() << "       " << TBMChannel << "       " << iroc->second.fecnumber() << "       "
0699         << iroc->second.mfec() << "       " << iroc->second.mfecchannel() << "       " << iroc->second.hubaddress()
0700         << "       " << iroc->second.portaddress() << "       " << iroc->second.rocid() << "         "
0701         << iroc->second.fednumber() << "       " << iroc->second.fedchannel() << "       "
0702         << iroc->second.fedrocnumber() << endl;
0703   }
0704 
0705   out.close();
0706 }
0707 
0708 //=============================================================================================
0709 void PixelNameTranslation::writeXMLHeader(pos::PixelConfigKey key,
0710                                           int version,
0711                                           std::string path,
0712                                           std::ofstream* outstream,
0713                                           std::ofstream* out1stream,
0714                                           std::ofstream* out2stream) const {
0715   std::string mthn = "[PixelNameTranslation:::writeXMLHeader()]\t\t\t    ";
0716   std::stringstream fullPath;
0717   fullPath << path << "/Pixel_NameTranslation_" << PixelTimeFormatter::getmSecTime() << ".xml";
0718   cout << __LINE__ << "]\t" << mthn << "Writing to: " << fullPath.str() << endl;
0719 
0720   outstream->open(fullPath.str().c_str());
0721   *outstream << "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" << endl;
0722   *outstream << "<ROOT xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>" << endl;
0723   *outstream << " <HEADER>" << endl;
0724   *outstream << "  <TYPE>" << endl;
0725   *outstream << "   <EXTENSION_TABLE_NAME>PIXEL_NAME_TRANSLATION</EXTENSION_TABLE_NAME>" << endl;
0726   *outstream << "   <NAME>Pixel Name Translation</NAME>" << endl;
0727   *outstream << "  </TYPE>" << endl;
0728   *outstream << "  <RUN>" << endl;
0729   *outstream << "   <RUN_TYPE>Pixel Name Translation</RUN_TYPE>" << endl;
0730   *outstream << "   <RUN_NUMBER>1</RUN_NUMBER>" << endl;
0731   *outstream << "   <RUN_BEGIN_TIMESTAMP>" << pos::PixelTimeFormatter::getTime() << "</RUN_BEGIN_TIMESTAMP>" << endl;
0732   *outstream << "   <LOCATION>CERN P5</LOCATION>" << endl;
0733   *outstream << "  </RUN>" << endl;
0734   *outstream << " </HEADER>" << endl;
0735   *outstream << "  " << endl;
0736   *outstream << " <DATA_SET>" << endl;
0737   *outstream << "  <PART>" << endl;
0738   *outstream << "   <NAME_LABEL>CMS-PIXEL-ROOT</NAME_LABEL>" << endl;
0739   *outstream << "   <KIND_OF_PART>Detector ROOT</KIND_OF_PART>" << endl;
0740   *outstream << "  </PART>" << endl;
0741   *outstream << "  <VERSION>" << version << "</VERSION>" << endl;
0742   *outstream << "  <COMMENT_DESCRIPTION>" << getComment() << "</COMMENT_DESCRIPTION>" << endl;
0743   *outstream << "  <CREATED_BY_USER>" << getAuthor() << "</CREATED_BY_USER>" << endl;
0744   *outstream << "  " << endl;
0745 }
0746 
0747 //=============================================================================================
0748 void PixelNameTranslation::writeXML(std::ofstream* outstream,
0749                                     std::ofstream* out1stream,
0750                                     std::ofstream* out2stream) const {
0751   std::string mthn = "[PixelNameTranslation::writeXML()]\t\t\t    ";
0752 
0753   std::map<PixelROCName, PixelHdwAddress>::const_iterator iroc = translationtable_.begin();
0754 
0755   for (; iroc != translationtable_.end(); ++iroc) {
0756     // Find the PixelChannel for this ROC, in order to get the TBM channel.
0757     std::string TBMChannel = getChannelFromHdwAddress(iroc->second).TBMChannelString();
0758 
0759     *outstream << "  <DATA>" << endl;
0760     *outstream << "   <ROC_NAME>" << iroc->first.rocname() << "</ROC_NAME>" << endl;
0761     *outstream << "   <TBM_MODE>" << TBMChannel << "</TBM_MODE>" << endl;
0762     *outstream << "   <PXLFEC_NAME>" << iroc->second.fecnumber() << "</PXLFEC_NAME>" << endl;
0763     *outstream << "   <MFEC_POSN>" << iroc->second.mfec() << "</MFEC_POSN>" << endl;
0764     *outstream << "   <MFEC_CHAN>" << iroc->second.mfecchannel() << "</MFEC_CHAN>" << endl;
0765     *outstream << "   <HUB_ADDRS>" << iroc->second.hubaddress() << "</HUB_ADDRS>" << endl;
0766     *outstream << "   <PORT_NUM>" << iroc->second.portaddress() << "</PORT_NUM>" << endl;
0767     *outstream << "   <ROC_I2C_ADDR>" << iroc->second.rocid() << "</ROC_I2C_ADDR>" << endl;
0768     *outstream << "   <PXLFED_NAME>" << iroc->second.fednumber() << "</PXLFED_NAME>" << endl;
0769     *outstream << "   <FED_CHAN>" << iroc->second.fedchannel() << "</FED_CHAN>" << endl;
0770     *outstream << "   <FED_ROC_NUM>" << iroc->second.fedrocnumber() << "</FED_ROC_NUM>" << endl;
0771     *outstream << "  </DATA>" << endl;
0772     *outstream << "" << endl;
0773   }
0774 }
0775 
0776 //=============================================================================================
0777 void PixelNameTranslation::writeXMLTrailer(std::ofstream* outstream,
0778                                            std::ofstream* out1stream,
0779                                            std::ofstream* out2stream) const {
0780   std::string mthn = "[PixelNameTranslation::writeXMLTrailer()]\t\t\t    ";
0781 
0782   *outstream << " </DATA_SET>" << endl;
0783   *outstream << "</ROOT> " << endl;
0784 
0785   outstream->close();
0786 }
0787 
0788 //=============================================================================================
0789 void PixelNameTranslation::writeXML(pos::PixelConfigKey key, int version, std::string path) const {
0790   std::string mthn = "[PixelNameTranslation::writeXML]\t\t\t    ";
0791   std::stringstream fullPath;
0792 
0793   fullPath << path << "/Pixel_NameTranslation.xml";
0794   cout << __LINE__ << "]\t" << mthn << "Writing to: " << fullPath.str() << endl;
0795 
0796   std::ofstream out(fullPath.str().c_str());
0797 
0798   out << "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" << endl;
0799   out << "<ROOT xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>" << endl;
0800   out << "" << endl;
0801   out << " <HEADER>" << endl;
0802   out << "  <HINTS mode='only-det-root' />" << endl;
0803   out << "  <TYPE>" << endl;
0804   out << "   <EXTENSION_TABLE_NAME>PIXEL_NAME_TRANSLATION</EXTENSION_TABLE_NAME>" << endl;
0805   out << "   <NAME>Pixel Name Translation</NAME>" << endl;
0806   out << "  </TYPE>" << endl;
0807   out << "  <RUN>" << endl;
0808   out << "   <RUN_TYPE>Pixel Name Translation</RUN_TYPE>" << endl;
0809   out << "   <RUN_NUMBER>1</RUN_NUMBER>" << endl;
0810   out << "   <RUN_BEGIN_TIMESTAMP>" << pos::PixelTimeFormatter::getTime() << "</RUN_BEGIN_TIMESTAMP>" << endl;
0811   out << "   <COMMENT_DESCRIPTION>Test of Name Translation xml</COMMENT_DESCRIPTION>" << endl;
0812   out << "   <LOCATION>CERN TAC</LOCATION>" << endl;
0813   out << "   <CREATED_BY_USER>Dario Menasce</CREATED_BY_USER>" << endl;
0814   out << "  </RUN>" << endl;
0815   out << " </HEADER>" << endl;
0816   out << "" << endl;
0817   out << " <DATA_SET>" << endl;
0818   out << "  <VERSION>" << version << "</VERSION>" << endl;
0819   out << "  <PART>" << endl;
0820   out << "   <NAME_LABEL>CMS-PIXEL-ROOT</NAME_LABEL>" << endl;
0821   out << "   <KIND_OF_PART>Detector ROOT</KIND_OF_PART>" << endl;
0822   out << "  </PART>" << endl;
0823   out << "" << endl;
0824 
0825   std::map<PixelROCName, PixelHdwAddress>::const_iterator iroc = translationtable_.begin();
0826 
0827   for (; iroc != translationtable_.end(); ++iroc) {
0828     // Find the PixelChannel for this ROC, in order to get the TBM channel.
0829     std::string TBMChannel = getChannelFromHdwAddress(iroc->second).TBMChannelString();
0830 
0831     out << "  <DATA>" << endl;
0832     out << "   <PXLFEC_NAME>" << iroc->second.fecnumber() << "</PXLFEC_NAME>" << endl;
0833     out << "   <MFEC_POSN>" << iroc->second.mfec() << "</MFEC_POSN>" << endl;
0834     out << "   <MFEC_CHAN>" << iroc->second.mfecchannel() << "</MFEC_CHAN>" << endl;
0835     out << "   <HUB_ADDRS>" << iroc->second.hubaddress() << "</HUB_ADDRS>" << endl;
0836     out << "   <PORT_NUM>" << iroc->second.portaddress() << "</PORT_NUM>" << endl;
0837     out << "   <ROC_I2C_ADDR>" << iroc->second.rocid() << "</ROC_I2C_ADDR>" << endl;
0838     out << "   <PXLFED_NAME>" << iroc->second.fednumber() << "</PXLFED_NAME>" << endl;
0839     out << "   <FED_CHAN>" << iroc->second.fedchannel() << "</FED_CHAN>" << endl;
0840     out << "   <FED_RO_NUM>" << iroc->second.fedrocnumber() << "</FED_ROC_NUM>" << endl;
0841     out << "  </DATA>" << endl;
0842     out << "" << endl;
0843   }
0844   out << " </DATA_SET> " << endl;
0845   out << "</ROOT> " << endl;
0846   out.close();
0847   assert(0);
0848 }
0849 
0850 const std::vector<PixelROCName>& PixelNameTranslation::getROCsFromChannel(const PixelChannel& aChannel) const {
0851   const PixelHdwAddress& channelHdwAddress = getHdwAddress(aChannel);
0852   return getROCsFromFEDChannel(channelHdwAddress.fednumber(), channelHdwAddress.fedchannel());
0853 }
0854 
0855 std::vector<PixelROCName> PixelNameTranslation::getROCsFromModule(const PixelModuleName& aModule) const {
0856   std::vector<PixelROCName> returnThis;
0857 
0858   std::set<PixelChannel> channelsOnThisModule = getChannelsOnModule(aModule);
0859   for (std::set<PixelChannel>::const_iterator channelsOnThisModule_itr = channelsOnThisModule.begin();
0860        channelsOnThisModule_itr != channelsOnThisModule.end();
0861        ++channelsOnThisModule_itr) {
0862     std::vector<PixelROCName> ROCsOnThisChannel = getROCsFromChannel(*channelsOnThisModule_itr);
0863     for (std::vector<PixelROCName>::const_iterator ROCsOnThisChannel_itr = ROCsOnThisChannel.begin();
0864          ROCsOnThisChannel_itr != ROCsOnThisChannel.end();
0865          ++ROCsOnThisChannel_itr) {
0866       returnThis.push_back(*ROCsOnThisChannel_itr);
0867     }
0868   }
0869 
0870   return returnThis;
0871 }
0872 
0873 //====================================================================================
0874 // Added by Dario
0875 bool PixelNameTranslation::ROCexists(PixelROCName theROC) {
0876   if (translationtable_.find(theROC) == translationtable_.end()) {
0877     return false;
0878   }
0879   return true;
0880 }
0881 
0882 std::map<unsigned int, std::set<unsigned int> > PixelNameTranslation::getFEDsAndChannels() const {
0883   std::map<unsigned int, std::set<unsigned int> > tmp;
0884 
0885   std::map<PixelChannel, PixelHdwAddress>::const_iterator channelTranslationTable_itr =
0886       channelTranslationTable_.begin();
0887 
0888   for (; channelTranslationTable_itr != channelTranslationTable_.end(); ++channelTranslationTable_itr) {
0889     unsigned int fednumber = channelTranslationTable_itr->second.fednumber();
0890     unsigned int fedchannel = channelTranslationTable_itr->second.fedchannel();
0891 
0892     tmp[fednumber].insert(fedchannel);
0893   }
0894 
0895   return tmp;
0896 }