CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC2_patch1/src/CalibFormats/SiPixelObjects/src/PixelNameTranslation.cc

Go to the documentation of this file.
00001 //
00002 // This class stores the name and related
00003 // hardware mapings for a ROC 
00004 //
00005 //
00006 
00007 #include "CalibFormats/SiPixelObjects/interface/PixelNameTranslation.h"
00008 #include "CalibFormats/SiPixelObjects/interface/PixelDetectorConfig.h"
00009 #include "CalibFormats/SiPixelObjects/interface/PixelTimeFormatter.h"
00010 //#include "PixelUtilities/PixelTestStandUtilities/include/PixelTimer.h"
00011 #include <fstream>
00012 #include <sstream>
00013 #include <map>
00014 #include <string>
00015 #include <vector>
00016 #include <assert.h>
00017 #include <stdexcept>
00018 
00019 using namespace pos;
00020 using namespace std;
00021 
00022 
00023 PixelNameTranslation::PixelNameTranslation(std::vector< std::vector<std::string> > &tableMat):PixelConfigBase(" "," "," "){
00024   std::string mthn = "[PixelNameTranslation::PixelNameTranslation()]\t\t    " ;
00025   std::map<std::string , int > colM;
00026   std::vector<std::string > colNames;
00027 
00028 /*
00029   EXTENSION_TABLE_NAME: PIXEL_NAME_TRANS (VIEW: CONF_KEY_NAME_TRANS_V)
00030 
00031   CONFIG_KEY                                NOT NULL VARCHAR2(80)
00032   KEY_TYPE                                  NOT NULL VARCHAR2(80)
00033   KEY_ALIAS                                 NOT NULL VARCHAR2(80)
00034   VERSION                                            VARCHAR2(40)
00035   KIND_OF_COND                              NOT NULL VARCHAR2(40)
00036   ROC_NAME                                  NOT NULL VARCHAR2(200)
00037   PXLFEC_NAME                               NOT NULL NUMBER(38)
00038   MFEC_POSN                                 NOT NULL NUMBER(38)
00039   MFEC_CHAN                                 NOT NULL NUMBER(38)
00040   HUB_ADDRS                                          NUMBER(38)
00041   PORT_NUM                                  NOT NULL NUMBER(38)
00042   ROC_I2C_ADDR                              NOT NULL NUMBER(38)
00043   PXLFED_NAME                               NOT NULL NUMBER(38)
00044   FED_CHAN                                  NOT NULL NUMBER(38)
00045   FED_ROC_NUM                               NOT NULL NUMBER(38)
00046   TBM_MODE                                           VARCHAR2(200)
00047 */
00048 
00049   colNames.push_back("CONFIG_KEY"  ); //0
00050   colNames.push_back("KEY_TYPE"    ); //1
00051   colNames.push_back("KEY_ALIAS"   ); //2
00052   colNames.push_back("VERSION"     ); //3
00053   colNames.push_back("KIND_OF_COND"); //4
00054   colNames.push_back("ROC_NAME"    ); //5
00055   colNames.push_back("PXLFEC_NAME" ); //6 
00056   colNames.push_back("MFEC_POSN"   ); //7 
00057   colNames.push_back("MFEC_CHAN"   ); //8 
00058   colNames.push_back("HUB_ADDRS"   ); //9 
00059   colNames.push_back("PORT_NUM"    ); //10
00060   colNames.push_back("ROC_I2C_ADDR"); //11
00061   colNames.push_back("PXLFED_NAME" ); //12
00062   colNames.push_back("FED_CHAN"    ); //13
00063   colNames.push_back("FED_ROC_NUM" ); //14
00064   colNames.push_back("TBM_MODE"    ); //15
00065 
00066   for(unsigned int c = 0 ; c < tableMat[0].size() ; c++){
00067     for(unsigned int n=0; n<colNames.size(); n++){
00068       if(tableMat[0][c] == colNames[n]){
00069         colM[colNames[n]] = c;
00070         break;
00071       }
00072     }
00073   }//end for
00074   /*
00075   for(unsigned int n=0; n<colNames.size(); n++){
00076     if(colM.find(colNames[n]) == colM.end()){
00077       std::cerr << __LINE__ << "]\t" << mthn << "Couldn't find in the database the column with name " << colNames[n] << std::endl;
00078       assert(0);
00079     }
00080   }
00081   */
00082 
00083  for(unsigned int r = 1 ; r < tableMat.size() ; r++){    //Goes to every row of the Matrix
00084    std::string rocname       = tableMat[r][colM["ROC_NAME"]] ;
00085    std::string TBMChannel    = tableMat[r][colM["TBM_MODE"]] ; // assert(0); // need to add this to the input table
00086    if(TBMChannel == "")
00087      {
00088        TBMChannel = "A" ;
00089      }
00090    /* // modified by MR on 13-07-2008 11:32:50
00091       Umesh changed the content of the column and 
00092       stripped out the FPix_Pxl_FEC_ part of the "number"
00093    tableMat[r][colM["PXLFEC_NAME"]].erase(0 , 13);//PIXFEC
00094    unsigned int fecnumber    = (unsigned int)atoi(tableMat[r][colM["PXLFEC_NAME"]].c_str());
00095    */
00096    unsigned int fecnumber    = (unsigned int)atoi(tableMat[r][colM["PXLFEC_NAME"]].c_str());
00097    unsigned int mfec         = (unsigned int)atoi(tableMat[r][colM["MFEC_POSN"]].c_str());
00098    unsigned int mfecchannel  = (unsigned int)atoi(tableMat[r][colM["MFEC_CHAN"]].c_str());
00099    unsigned int hubaddress   = (unsigned int)atoi(tableMat[r][colM["HUB_ADDRS"]].c_str());
00100    unsigned int portaddress  = (unsigned int)atoi(tableMat[r][colM["PORT_NUM"]].c_str());
00101    unsigned int rocid        = (unsigned int)atoi(tableMat[r][colM["ROC_I2C_ADDR"]].c_str());
00102    // modified by MR on 13-07-2008 11:47:32
00103    /* Umesh changed the content of the column and 
00104       stripped out the PxlFED_ part of the "number"
00105      
00106    tableMat[r][colM["PXLFED_NAME"]].erase(0,7);//FED
00107    */
00108    unsigned int fednumber    = (unsigned int)atoi(tableMat[r][colM["PXLFED_NAME"]].c_str());
00109    unsigned int fedchannel   = (unsigned int)atoi(tableMat[r][colM["FED_CHAN"]].c_str());
00110    unsigned int fedrocnumber = (unsigned int)(atoi(tableMat[r][colM["FED_ROC_NUM"]].c_str()));
00111         
00112         
00113    PixelROCName aROC(rocname);
00114    if (aROC.rocname()!=rocname){
00115      std::cout << __LINE__ << "]\t" << mthn << "Rocname  : " << rocname        << std::endl;
00116      std::cout << __LINE__ << "]\t" << mthn << "Parsed to: " << aROC.rocname() << std::endl;
00117      assert(0);
00118    }
00119 
00120    if (ROCNameFromFEDChannelROCExists(fednumber,fedchannel,
00121                                       fedrocnumber)){
00122      std::cout << __LINE__ << "]\t" << mthn 
00123                << "ROC with fednumber=" << fednumber
00124                << " fedchannel="        << fedchannel
00125                << " roc number="        << fedrocnumber
00126                << " already exists"     << std::endl;
00127      std::cout << __LINE__ << "]\t" << mthn << "Fix this inconsistency in the name translation"
00128                << std::endl;
00129      assert(0);
00130    }
00131    
00132    PixelHdwAddress hdwAdd(fecnumber,mfec,mfecchannel,
00133                           hubaddress,portaddress,
00134                           rocid,
00135                           fednumber,fedchannel,fedrocnumber);
00136 //    std::cout << "[PixelNameTranslation::PixelNameTranslation()] aROC: " << aROC << std::endl;
00137    translationtable_[aROC]=hdwAdd;
00138    fedlookup_[hdwAdd]=aROC;
00139 
00140    PixelModuleName aModule(rocname);
00141    PixelChannel aChannel(aModule, TBMChannel);
00142 
00143    hdwTranslationTable_[hdwAdd] = aChannel;
00144    
00145 
00146    // 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.
00147    bool foundChannel = false;
00148 
00149    std::map<PixelChannel, PixelHdwAddress >::const_iterator channelTranslationTable_itr = channelTranslationTable_.find(aChannel);
00150 
00151    if ( channelTranslationTable_itr != channelTranslationTable_.end()) {
00152 
00153      if (!(channelTranslationTable_itr->second |= hdwAdd))
00154            {
00155              cout << "Found two ROCs on the same channe, but not same hdw"<<endl;
00156              cout << "Hdw1:"<<endl<<channelTranslationTable_itr->second<<endl;
00157              cout << "Hdw2:"<<endl<<hdwAdd<<endl;
00158            }
00159      assert( channelTranslationTable_itr->second |= hdwAdd );
00160      foundChannel = true;
00161    }
00162    else if ( channelTranslationTable_itr->first.module() == aModule ) 
00163      {
00164        assert( channelTranslationTable_itr->second.fecnumber() == hdwAdd.fecnumber() );
00165        assert( channelTranslationTable_itr->second.mfec() == hdwAdd.mfec() );
00166        assert( channelTranslationTable_itr->second.mfecchannel() == hdwAdd.mfecchannel() );
00167        //assert( channelTranslationTable_itr->second.portaddress() == hdwAdd.portaddress() );
00168        assert( channelTranslationTable_itr->second.hubaddress() == hdwAdd.hubaddress() );
00169        assert( channelTranslationTable_itr->second.fednumber() != hdwAdd.fednumber() || channelTranslationTable_itr->second.fedchannel() != hdwAdd.fedchannel() );
00170      }
00171    
00172    if ( foundChannel == false ) {
00173      channelTranslationTable_[aChannel] = hdwAdd;
00174    }     
00175 
00176  }//end for r
00177 
00178   const std::map<unsigned int, std::set<unsigned int> > fedsAndChannels=getFEDsAndChannels();
00179 
00180 
00181   std::vector<PixelROCName> tmp(24);
00182 
00183   std::map<unsigned int, std::map<unsigned int, int > > counter;
00184     //       FED id                  FED channel
00185 
00186   std::map<unsigned int, std::map<unsigned int, int > > maxindex;
00187 
00188   std::map<PixelROCName,PixelHdwAddress>::const_iterator it=translationtable_.begin();
00189     
00190   for(;it!=translationtable_.end();it++){
00191 
00192     int index=it->second.fedrocnumber();
00193 
00194     unsigned int fednumber=it->second.fednumber();
00195     unsigned int fedchannel=it->second.fedchannel();
00196 
00197     std::vector<PixelROCName>& tmp= rocsFromFEDidAndChannel_[fednumber][fedchannel];
00198     
00199     if (tmp.size()==0){
00200       tmp.resize(24);
00201       counter[fednumber][fedchannel]=0;
00202       maxindex[fednumber][fedchannel]=0;
00203     }
00204 
00205     if (index>maxindex[fednumber][fedchannel]) maxindex[fednumber][fedchannel]=index;
00206     tmp[index]=it->first;
00207     counter[fednumber][fedchannel]++;
00208 
00209   }
00210 
00211 
00212   it=translationtable_.begin();
00213     
00214   for(;it!=translationtable_.end();it++){
00215 
00216     unsigned int fednumber=it->second.fednumber();
00217     unsigned int fedchannel=it->second.fedchannel();
00218     
00219     std::vector<PixelROCName>& tmp= rocsFromFEDidAndChannel_[fednumber][fedchannel];
00220     
00221     assert(counter[fednumber][fedchannel]==maxindex[fednumber][fedchannel]+1);
00222 
00223     tmp.resize(counter[fednumber][fedchannel]);
00224 
00225   }
00226 
00227 }//end contructor
00228 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00229 
00230 PixelNameTranslation::PixelNameTranslation(std::string filename):
00231   PixelConfigBase(" "," "," "){
00232 
00233   static std::string mthn = "[PixelNameTranslation::PixelNameTranslation()]\t\t    " ;
00234   
00235   std::ifstream in(filename.c_str());
00236   
00237   if (!in.good()){
00238     std::cout << __LINE__ << "]\t" << mthn << "Could not open: " << filename << std::endl;
00239     throw std::runtime_error("Failed to open file "+filename);
00240   }
00241   else {
00242     std::cout << __LINE__ << "]\t" << mthn << "Reading from: "   << filename << std::endl;
00243   }
00244   
00245   std::string dummy;
00246 
00247   getline(in, dummy); // skip the column headings
00248 
00249 
00250   do {
00251 
00252     std::string rocname;
00253     std::string TBMChannel;
00254     unsigned int fecnumber;
00255     unsigned int mfec;
00256     unsigned int mfecchannel;
00257     unsigned int hubaddress;
00258     unsigned int portaddress;
00259     unsigned int rocid;
00260     unsigned int fednumber;
00261     unsigned int fedchannel;
00262     unsigned int fedrocnumber;
00263 
00264 
00265     in >> rocname;
00266     in >> TBMChannel;
00267     if ( TBMChannel != "A" && TBMChannel != "B" ) // no TBM channel was specified, so default to A and set fecnumber to the value of this string
00268       {
00269         fecnumber = atoi(TBMChannel.c_str());
00270         TBMChannel = "A";
00271       }
00272     else // TBM channel was specified, now read fecnumber
00273       {
00274         in >> fecnumber;
00275       }
00276     in >> mfec >> mfecchannel 
00277        >> hubaddress >> portaddress >> rocid >> fednumber 
00278        >> fedchannel >> fedrocnumber;
00279 
00280     if (!in.eof() ){
00281 
00282       PixelROCName aROC(rocname);
00283 
00284       //debug
00285       //if (aROC.rocname()!=rocname){
00286       //std::cout << __LINE__ << "]\t" << mthn << "Rocname  : " << rocname        << std::endl;
00287       //std::cout << __LINE__ << "]\t" << mthn << "Parsed to: " << aROC.rocname() << std::endl;
00288       //assert(0);
00289       //}
00290 
00291       if (ROCNameFromFEDChannelROCExists(fednumber,fedchannel,
00292                                          fedrocnumber)){
00293         std::cout << __LINE__ << "]\t"     << mthn 
00294                   << "ROC with fednumber=" << fednumber
00295                   << " fedchannel="        << fedchannel
00296                   << " roc number="        << fedrocnumber
00297                   << " already exists"     << std::endl;
00298         std::cout << __LINE__ << "]\t"     << mthn 
00299                   << "Fix this inconsistency in the name translation"
00300                   << std::endl;
00301         assert(0);
00302         
00303       }
00304 
00305       PixelHdwAddress hdwAdd(fecnumber,mfec,mfecchannel,
00306                              hubaddress,portaddress,
00307                              rocid,
00308                              fednumber,fedchannel,fedrocnumber);
00309       //std::cout << aROC << std::endl;
00310       // modified by MR on 18-01-2008 11:18:53
00311       //       std::cout << hdwAdd << std::endl ;
00312 //cout << "[PixelNameTranslation::PixelNameTranslation()]\t\t-----------------------------"  << endl ;
00313 // A fecnumber    
00314 // B mfec         
00315 // C mfecchannel  
00316 // D hubaddress   
00317 // E portaddress  
00318 // F rocid        
00319 // G fednumber    
00320 // H fedchannel   
00321 // I fedrocnumber 
00322 //cout << "[PixelNameTranslation::PixelNameTranslation()]\t\t"
00323 //     << " A " << fecnumber   
00324 //     << " B " << mfec       
00325 //     << " C " << mfecchannel  
00326 //     << " D " << hubaddress   
00327 //     << " E " << portaddress  
00328 //     << " F " << rocid        
00329 //     << " G " << fednumber    
00330 //     << " H " << fedchannel   
00331 //     << " I " << fedrocnumber << endl ;    
00332 
00333 
00334       translationtable_[aROC]=hdwAdd;
00335       fedlookup_[hdwAdd]=aROC;
00336             
00337       PixelModuleName aModule(rocname);
00338       PixelChannel aChannel(aModule, TBMChannel);
00339 
00340       hdwTranslationTable_[hdwAdd] = aChannel;
00341 
00342       // Look for this channel in channelTransaltionTable.  If it is found, 
00343       // check that the hardware address agrees.  If not, add it to the table.
00344       // Also, if another channel on that module is found, check that the FEC 
00345       // part agrees, and the FED part doesn't.
00346       bool foundChannel = false;
00347 
00348 
00349       std::map<PixelChannel, PixelHdwAddress >::const_iterator channelTranslationTable_itr = channelTranslationTable_.find(aChannel);
00350 
00351       if ( channelTranslationTable_itr != channelTranslationTable_.end()) {
00352         if (!(channelTranslationTable_itr->second |= hdwAdd)){
00353                 
00354           cout << __LINE__ << "]\t" << mthn << "Found two ROCs on the same channe, but not same hdw" << endl;
00355           cout << __LINE__ << "]\t" << mthn << "Hdw1: " << endl << channelTranslationTable_itr->second << endl;
00356           cout << __LINE__ << "]\t" << mthn << "Hdw2: " << endl << hdwAdd << endl;
00357         }
00358         assert( channelTranslationTable_itr->second |= hdwAdd );
00359         foundChannel = true;
00360       }
00361       else if ( channelTranslationTable_itr->first.module() == aModule ) {
00362         assert( channelTranslationTable_itr->second.fecnumber() == hdwAdd.fecnumber() );
00363         assert( channelTranslationTable_itr->second.mfec() == hdwAdd.mfec() );
00364         assert( channelTranslationTable_itr->second.mfecchannel() == hdwAdd.mfecchannel() );
00365         //assert( channelTranslationTable_itr->second.portaddress() == hdwAdd.portaddress() );
00366         assert( channelTranslationTable_itr->second.hubaddress() == hdwAdd.hubaddress() );
00367         assert( channelTranslationTable_itr->second.fednumber() != hdwAdd.fednumber() || channelTranslationTable_itr->second.fedchannel() != hdwAdd.fedchannel() );
00368       }
00369     
00370     
00371      
00372       if ( foundChannel == false ){
00373         channelTranslationTable_[aChannel] = hdwAdd;
00374       }
00375 
00376     }
00377   }
00378   while (!in.eof());
00379   in.close();
00380 
00381 
00382   const std::map<unsigned int, std::set<unsigned int> > fedsAndChannels=getFEDsAndChannels();
00383 
00384 
00385   std::vector<PixelROCName> tmp(24);
00386 
00387   std::map<unsigned int, std::map<unsigned int, int > > counter;
00388     //       FED id                  FED channel
00389 
00390   std::map<unsigned int, std::map<unsigned int, int > > maxindex;
00391 
00392   std::map<PixelROCName,PixelHdwAddress>::const_iterator it=translationtable_.begin();
00393     
00394   for(;it!=translationtable_.end();it++){
00395 
00396     int index=it->second.fedrocnumber();
00397 
00398     unsigned int fednumber=it->second.fednumber();
00399     unsigned int fedchannel=it->second.fedchannel();
00400 
00401     std::vector<PixelROCName>& tmp= rocsFromFEDidAndChannel_[fednumber][fedchannel];
00402     
00403     if (tmp.size()==0){
00404       tmp.resize(24);
00405       counter[fednumber][fedchannel]=0;
00406       maxindex[fednumber][fedchannel]=0;
00407     }
00408 
00409     if (index>maxindex[fednumber][fedchannel]) maxindex[fednumber][fedchannel]=index;
00410     tmp[index]=it->first;
00411     counter[fednumber][fedchannel]++;
00412 
00413   }
00414 
00415 
00416   it=translationtable_.begin();
00417     
00418   for(;it!=translationtable_.end();it++){
00419 
00420     unsigned int fednumber=it->second.fednumber();
00421     unsigned int fedchannel=it->second.fedchannel();
00422     
00423     std::vector<PixelROCName>& tmp= rocsFromFEDidAndChannel_[fednumber][fedchannel];
00424     
00425     assert(counter[fednumber][fedchannel]==maxindex[fednumber][fedchannel]+1);
00426 
00427     tmp.resize(counter[fednumber][fedchannel]);
00428 
00429   }
00430 
00431 
00432 }
00433 
00434 std::ostream& operator<<(std::ostream& s, const PixelNameTranslation& table){
00435 
00436   //for (unsigned int i=0;i<table.translationtable_.size();i++){
00437   //    s << table.translationtable_[i]<<std::endl;
00438   //   }
00439   return s;
00440 
00441 }
00442 
00443 std::list<const PixelROCName*> PixelNameTranslation::getROCs() const
00444 {
00445   std::list<const PixelROCName*> listOfROCs;
00446   for ( std::map<PixelROCName, PixelHdwAddress>::const_iterator translationTableEntry = translationtable_.begin();
00447         translationTableEntry != translationtable_.end(); ++translationTableEntry ) {
00448     listOfROCs.push_back(&(translationTableEntry->first));
00449   }
00450 
00451   return listOfROCs;
00452 }
00453 
00454 std::list<const PixelModuleName*> PixelNameTranslation::getModules() const
00455 {
00456   std::list<const PixelModuleName*> listOfModules;
00457   for ( std::map<PixelChannel, PixelHdwAddress >::const_iterator channelTranslationTable_itr = channelTranslationTable_.begin(); channelTranslationTable_itr != channelTranslationTable_.end(); channelTranslationTable_itr++ )
00458     {
00459       bool foundOne = false;
00460       for ( std::list<const PixelModuleName*>::const_iterator listOfModules_itr = listOfModules.begin(); listOfModules_itr != listOfModules.end(); listOfModules_itr++ )
00461         {
00462           if ( *(*listOfModules_itr) == channelTranslationTable_itr->first.module() )
00463             {
00464               foundOne = true;
00465               break;
00466             }
00467         }
00468       if (!foundOne) listOfModules.push_back( &(channelTranslationTable_itr->first.module()) );
00469     }
00470 
00471   return listOfModules;
00472 }
00473 
00474 std::set<PixelChannel> PixelNameTranslation::getChannels() const
00475 {
00476   std::set<PixelChannel> channelSet;
00477   for ( std::map<PixelChannel, PixelHdwAddress >::const_iterator channelTranslationTable_itr = channelTranslationTable_.begin(); channelTranslationTable_itr != channelTranslationTable_.end(); channelTranslationTable_itr++ )
00478     {
00479       channelSet.insert(channelTranslationTable_itr->first);
00480     }
00481   return channelSet;
00482 }
00483 
00484 std::set<PixelChannel> PixelNameTranslation::getChannels(const PixelDetectorConfig& aDetectorConfig) const
00485 {
00486   std::set<PixelChannel> channelSet;
00487   for ( std::map<PixelChannel, PixelHdwAddress >::const_iterator channelTranslationTable_itr = channelTranslationTable_.begin(); channelTranslationTable_itr != channelTranslationTable_.end(); channelTranslationTable_itr++ )
00488     {
00489       if ( aDetectorConfig.containsModule(channelTranslationTable_itr->first.module()) ) channelSet.insert(channelTranslationTable_itr->first);
00490     }
00491   return channelSet;
00492 }
00493 
00494 const PixelHdwAddress* PixelNameTranslation::getHdwAddress(const PixelROCName& aROC) const{
00495 
00496   static std::string mthn = "[PixelNameTranslation::getHdwAddress()]\t\t    " ;
00497   std::map<PixelROCName,PixelHdwAddress>::const_iterator it=
00498     translationtable_.find(aROC);
00499 
00500   if (it==translationtable_.end()){
00501     std::cout<< __LINE__ << "]\t" << mthn << "Could not look up ROC: " << aROC << std::endl;
00502     assert(0);
00503   }
00504     
00505   return &(it->second);
00506 
00507 }
00508 
00509 // Added for Debbie (used there only) to allow integrity checks (Dario)
00510 bool PixelNameTranslation::checkROCExistence(const PixelROCName& aROC) const{
00511 
00512   std::string mthn = "[PixelNameTranslation::checkROCExistence()]\t\t    " ;
00513   if (translationtable_.find(aROC)==translationtable_.end()) return false ;
00514   return true ;
00515 }
00516 
00517 const bool PixelNameTranslation::checkFor(const PixelROCName& aROC) const{ 
00518   if (translationtable_.find(aROC)==translationtable_.end())
00519     {
00520       return false ;
00521     }
00522   else 
00523     {
00524       return true ;
00525     }
00526   }
00527 
00528 const PixelHdwAddress& PixelNameTranslation::getHdwAddress(const PixelChannel& aChannel) const
00529 {
00530   std::map<PixelChannel, PixelHdwAddress >::const_iterator channelHdwAddress_itr = channelTranslationTable_.find(aChannel);
00531   assert( channelHdwAddress_itr != channelTranslationTable_.end() );
00532   return channelHdwAddress_itr->second;
00533 }
00534 
00535 const PixelHdwAddress& PixelNameTranslation::firstHdwAddress(const PixelModuleName& aModule) const
00536 {
00537         std::string mthn = "[PixelNameTranslation::firstHdwAddress()]\t\t    " ;
00538         std::set<PixelChannel> channelsOnModule = getChannelsOnModule(aModule);
00539         if (channelsOnModule.size() == 0 ){
00540           cout << __LINE__ << "]\t" << mthn << "module=" << aModule << " has zero channels!" << endl;
00541           cout << __LINE__ << "]\t" << mthn << "Will terminate" << endl;
00542           ::abort();
00543         }
00544         std::set<PixelChannel>::const_iterator firstChannel = channelsOnModule.begin();
00545         assert( firstChannel != channelsOnModule.end() );
00546         return getHdwAddress( *firstChannel );
00547 }
00548 
00549 const PixelChannel& PixelNameTranslation::getChannelForROC(const PixelROCName& aROC) const
00550 {
00551   std::map<PixelROCName,PixelHdwAddress>::const_iterator foundEntry = translationtable_.find(aROC);
00552   assert( foundEntry != translationtable_.end() );
00553   return getChannelFromHdwAddress( foundEntry->second );
00554 }
00555 
00556 std::set< PixelChannel > PixelNameTranslation::getChannelsOnModule(const PixelModuleName& aModule) const
00557 {
00558   std::set< PixelChannel > returnThis;
00559   for ( std::map<PixelChannel, PixelHdwAddress >::const_iterator channelTranslationTable_itr = channelTranslationTable_.begin(); channelTranslationTable_itr != channelTranslationTable_.end(); channelTranslationTable_itr++ )
00560     {
00561       if ( channelTranslationTable_itr->first.module() == aModule ) returnThis.insert(channelTranslationTable_itr->first);
00562     }
00563   assert( returnThis.size() <= 2 );
00564   return returnThis;
00565 }
00566 
00567 
00568 const std::vector<PixelROCName>& PixelNameTranslation::getROCsFromFEDChannel(unsigned int fednumber, 
00569                                                                       unsigned int fedchannel) const{
00570 
00571   std::map<unsigned int, std::map<unsigned int, std::vector<PixelROCName> > >::const_iterator it=rocsFromFEDidAndChannel_.find(fednumber);
00572 
00573   assert(it!=rocsFromFEDidAndChannel_.end());
00574 
00575   std::map<unsigned int, std::vector<PixelROCName> >::const_iterator it2=it->second.find(fedchannel);
00576 
00577   assert(it2!=it->second.end());
00578 
00579   return it2->second;
00580 
00581 }
00582 
00583 
00584 //Will return ROC names sorted by FED readout order.
00585 //This (private) method will be called once to build this list
00586 //when the data is read in.
00587 
00588 std::vector<PixelROCName> PixelNameTranslation::buildROCsFromFEDChannel(unsigned int fednumber, 
00589                                                                         unsigned int fedchannel) const{
00590 
00591   std::vector<PixelROCName> tmp(24);
00592 
00593   int counter=0;        
00594 
00595   int maxindex=0;
00596 
00597   std::map<PixelROCName,PixelHdwAddress>::const_iterator it=translationtable_.begin();
00598     
00599   for(;it!=translationtable_.end();it++){
00600 
00601     if (it->second.fednumber()==fednumber&&
00602         it->second.fedchannel()==fedchannel){
00603       int index=it->second.fedrocnumber();
00604       if (index>maxindex) maxindex=index;
00605       //std::cout << "Found one:"<<index<<" "<<it->first<<std::endl;
00606       tmp[index]=it->first;
00607       counter++;
00608     }
00609 
00610   }
00611 
00612   assert(counter==maxindex+1);
00613 
00614   tmp.resize(counter);
00615 
00616   return tmp;
00617 
00618 }
00619 
00620 
00621 bool PixelNameTranslation::ROCNameFromFEDChannelROCExists(unsigned int fednumber, 
00622                                                           unsigned int channel, 
00623                                                           unsigned int roc) const {
00624 
00625 
00626   PixelHdwAddress tmp(0,0,0,0,0,0,fednumber,channel,roc);
00627 
00628   return (fedlookup_.find(tmp)!=fedlookup_.end());
00629 
00630 }
00631 
00632 
00633 PixelROCName PixelNameTranslation::ROCNameFromFEDChannelROC(unsigned int fednumber, 
00634                                                             unsigned int channel, 
00635                                                             unsigned int roc) const {
00636 
00637 
00638   std::string mthn = "[PixelNameTranslation::ROCNameFromFEDChannelROC()]\t\t    " ;
00639   PixelHdwAddress tmp(0,0,0,0,0,0,fednumber,channel,roc);
00640 
00641   std::map<PixelHdwAddress,PixelROCName,PixelHdwAddress>::const_iterator it1=fedlookup_.find(tmp);
00642 
00643   if (it1!=fedlookup_.end()){
00644     return it1->second;
00645   }
00646 
00647   std::cout << __LINE__ << "]\t" << mthn << "could not find ROCName "
00648             << " for FED#" << fednumber << " chan=" << channel << " roc#=" << roc << std::endl;
00649 
00650   assert(0);
00651 
00652   PixelROCName tmp1;
00653 
00654   return tmp1;
00655 
00656 }
00657 
00658 PixelChannel PixelNameTranslation::ChannelFromFEDChannel(unsigned int fednumber, unsigned int fedchannel) const
00659 {
00660         std::string mthn = "[PixelNameTranslation::ChannelFromFEDChannel()]\t\t    " ;
00661         std::map<PixelChannel,PixelHdwAddress>::const_iterator toReturn;
00662         bool foundOne = false;
00663         for(std::map<PixelChannel,PixelHdwAddress>::const_iterator it=channelTranslationTable_.begin(); it!=channelTranslationTable_.end();it++)
00664         {
00665                 if (it->second.fednumber()==fednumber && it->second.fedchannel()==fedchannel)
00666                 {
00667                         if ( foundOne )
00668                         {
00669                                 std::cout << __LINE__ << "]\t" << mthn 
00670                                           << "ERROR: multiple channels on FED#" << fednumber << ", chan=" << fedchannel << std::endl;
00671                                 assert(0);
00672                         }
00673                         else
00674                         {
00675                                 toReturn = it;
00676                                 foundOne = true;
00677                         }
00678                 }
00679         }
00680         
00681         if ( !foundOne )
00682         {
00683                 std::cout << __LINE__ << "]\t" << mthn 
00684                           << "ERROR: no channel found for FED#" << fednumber << ", chan=" << fedchannel << std::endl;
00685                 assert(0);
00686         }
00687         
00688         return toReturn->first;
00689 }
00690 
00691 bool PixelNameTranslation::FEDChannelExist(unsigned int fednumber, unsigned int fedchannel) const
00692 {
00693         std::string mthn = "[PixelNameTranslation::FEDChannelExist()]\t\t    " ;
00694         std::map<PixelChannel,PixelHdwAddress>::const_iterator toReturn;
00695         bool foundOne = false;
00696         for(std::map<PixelChannel,PixelHdwAddress>::const_iterator it=channelTranslationTable_.begin(); it!=channelTranslationTable_.end();it++)
00697         {
00698                 if (it->second.fednumber()==fednumber && it->second.fedchannel()==fedchannel)
00699                 {
00700                         if ( foundOne )
00701                         {
00702                                 std::cout << __LINE__ << "]\t" << mthn 
00703                                           << "ERROR: multiple channels on FED#" << fednumber << ", chan=" << fedchannel << std::endl;
00704                                 assert(0);
00705                         }
00706                         else
00707                         {
00708                                 toReturn = it;
00709                                 foundOne = true;
00710                         }
00711                 }
00712         }
00713         return foundOne;
00714 }
00715 
00716 const PixelChannel& PixelNameTranslation::getChannelFromHdwAddress(const PixelHdwAddress& aHdwAddress) const
00717 {
00718 // modified by MR on 30-01-2008 10:38:22
00719   std::string mthn = "[PixelNameTranslation::getChannelFromHdwAddress()]\t\t    " ;
00720 
00721   std::map<PixelHdwAddress, PixelChannel >::const_iterator it=
00722     hdwTranslationTable_.find(aHdwAddress);
00723 
00724   if (it==hdwTranslationTable_.end()){
00725     std::cout << __LINE__ << "]\t" << mthn 
00726               << "ERROR: no channel found for hardware address " << aHdwAddress << std::endl;
00727     assert(0);
00728   }
00729 
00730   return it->second;
00731 
00732   /*
00733     for ( std::map<PixelChannel, PixelHdwAddress >::const_iterator channelTranslationTable_itr = channelTranslationTable_.begin(); 
00734         channelTranslationTable_itr != channelTranslationTable_.end(); channelTranslationTable_itr++ )
00735      {
00736       if ( aHdwAddress |= channelTranslationTable_itr->second )
00737         {
00738           return channelTranslationTable_itr->first;
00739         }
00740      }
00741 // modified by MR on 30-01-2008 13:56:34
00742 // if you get here then there was NO match on the previous loop!!
00743   std::cout << __LINE__ << "]\t" << mthn 
00744             << "ERROR: no channel found for hardware address " << aHdwAddress << std::endl;
00745   assert(0);
00746   */
00747 }
00748 
00749 void PixelNameTranslation::writeASCII(std::string dir) const {
00750 
00751   std::string mthn = "[PixelNameTranslation::writeASCII()]\t\t\t    " ;
00752   if (dir!="") dir+="/";
00753   std::string filename=dir+"translation.dat";
00754 
00755   std::ofstream out(filename.c_str());
00756   
00757   //std::cout << "[PixelNameTranslation::writeASCII()]\t\tfilename: " 
00758   //        << filename 
00759   //        << " status: " 
00760   //        << out 
00761   //        << "   " 
00762   //        << out.is_open() 
00763   //        <<endl ;
00764 
00765   out << "# name                          TBMchannel  FEC      mfec  mfecchannel hubaddress portadd rocid     FED     channel     roc#"<<endl;
00766 
00767   std::map<PixelROCName,PixelHdwAddress>::const_iterator iroc=translationtable_.begin();
00768 
00769   for (;iroc!=translationtable_.end();++iroc) {
00770   
00771     // Find the PixelChannel for this ROC, in order to get the TBM channel.
00772     std::string TBMChannel = getChannelFromHdwAddress(iroc->second).TBMChannelString();
00773   
00774     out << iroc->first.rocname()<<"       "
00775         << TBMChannel<<"       "
00776         << iroc->second.fecnumber()<<"       "
00777         << iroc->second.mfec()<<"       "
00778         << iroc->second.mfecchannel()<<"       "
00779         << iroc->second.hubaddress()<<"       "
00780         << iroc->second.portaddress()<<"       "
00781         << iroc->second.rocid()<<"         "
00782         << iroc->second.fednumber()<<"       "
00783         << iroc->second.fedchannel()<<"       "
00784         << iroc->second.fedrocnumber()
00785         << endl;
00786   }
00787 
00788 
00789 
00790   out.close();
00791 
00792 }
00793 
00794 //=============================================================================================
00795 void PixelNameTranslation::writeXMLHeader(pos::PixelConfigKey key, 
00796                                           int version, 
00797                                           std::string path, 
00798                                           std::ofstream *outstream,
00799                                           std::ofstream *out1stream,
00800                                           std::ofstream *out2stream) const 
00801 {
00802   std::string mthn = "[PixelNameTranslation:::writeXMLHeader()]\t\t\t    " ;
00803   std::stringstream fullPath ;
00804   fullPath << path << "/Pixel_NameTranslation_" << PixelTimeFormatter::getmSecTime() << ".xml" ;
00805   cout << __LINE__ << "]\t" << mthn << "Writing to: " << fullPath.str() << endl ;
00806   
00807   outstream->open(fullPath.str().c_str()) ;
00808   *outstream << "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"                                    << endl ;
00809   *outstream << "<ROOT xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>"                               << endl ;
00810   *outstream << " <HEADER>"                                                                                  << endl ;
00811   *outstream << "  <TYPE>"                                                                                   << endl ;
00812   *outstream << "   <EXTENSION_TABLE_NAME>PIXEL_NAME_TRANSLATION</EXTENSION_TABLE_NAME>"                     << endl ;
00813   *outstream << "   <NAME>Pixel Name Translation</NAME>"                                                     << endl ;
00814   *outstream << "  </TYPE>"                                                                                  << endl ;
00815   *outstream << "  <RUN>"                                                                                    << endl ;
00816   *outstream << "   <RUN_TYPE>Pixel Name Translation</RUN_TYPE>"                                             << endl ;
00817   *outstream << "   <RUN_NUMBER>1</RUN_NUMBER>"                                                              << endl ;
00818   *outstream << "   <RUN_BEGIN_TIMESTAMP>" << pos::PixelTimeFormatter::getTime() << "</RUN_BEGIN_TIMESTAMP>" << endl ;
00819   *outstream << "   <LOCATION>CERN P5</LOCATION>"                                                            << endl ; 
00820   *outstream << "  </RUN>"                                                                                   << endl ;
00821   *outstream << " </HEADER>"                                                                                 << endl ;
00822   *outstream << "  "                                                                                         << endl ;
00823   *outstream << " <DATA_SET>"                                                                                << endl ;
00824   *outstream << "  <PART>"                                                                                   << endl ;
00825   *outstream << "   <NAME_LABEL>CMS-PIXEL-ROOT</NAME_LABEL>"                                                 << endl ;
00826   *outstream << "   <KIND_OF_PART>Detector ROOT</KIND_OF_PART>"                                              << endl ;
00827   *outstream << "  </PART>"                                                                                  << endl ;
00828   *outstream << "  <VERSION>"             << version      << "</VERSION>"                                    << endl ;
00829   *outstream << "  <COMMENT_DESCRIPTION>" << getComment() << "</COMMENT_DESCRIPTION>"                        << endl ;
00830   *outstream << "  <CREATED_BY_USER>"     << getAuthor()  << "</CREATED_BY_USER>"                            << endl ;
00831   *outstream << "  "                                                                                         << endl ;
00832 
00833 }  
00834 
00835 //=============================================================================================
00836 void PixelNameTranslation::writeXML(std::ofstream *outstream,
00837                                     std::ofstream *out1stream,
00838                                     std::ofstream *out2stream) const 
00839 {
00840   std::string mthn = "[PixelNameTranslation::writeXML()]\t\t\t    " ;
00841   
00842   std::map<PixelROCName,PixelHdwAddress>::const_iterator iroc=translationtable_.begin();
00843 
00844   for (;iroc!=translationtable_.end();++iroc) 
00845     {
00846       // Find the PixelChannel for this ROC, in order to get the TBM channel.
00847       std::string TBMChannel = getChannelFromHdwAddress(iroc->second).TBMChannelString();
00848 
00849       *outstream << "  <DATA>"                                                                               << endl ;
00850       *outstream << "   <ROC_NAME>"     << iroc->first.rocname()        << "</ROC_NAME>"                     << endl ;
00851       *outstream << "   <TBM_MODE>"     << TBMChannel                   << "</TBM_MODE>"                     << endl ;
00852       *outstream << "   <PXLFEC_NAME>"  << iroc->second.fecnumber()     << "</PXLFEC_NAME>"                  << endl ;
00853       *outstream << "   <MFEC_POSN>"    << iroc->second.mfec()  << "</MFEC_POSN>"                            << endl ;
00854       *outstream << "   <MFEC_CHAN>"    << iroc->second.mfecchannel()  << "</MFEC_CHAN>"                     << endl ;
00855       *outstream << "   <HUB_ADDRS>"    << iroc->second.hubaddress()    << "</HUB_ADDRS>"                    << endl ;
00856       *outstream << "   <PORT_NUM>"     << iroc->second.portaddress()  << "</PORT_NUM>"                      << endl ;
00857       *outstream << "   <ROC_I2C_ADDR>" << iroc->second.rocid() << "</ROC_I2C_ADDR>"                         << endl ;
00858       *outstream << "   <PXLFED_NAME>"  << iroc->second.fednumber()     << "</PXLFED_NAME>"                  << endl ;
00859       *outstream << "   <FED_CHAN>"     << iroc->second.fedchannel()    << "</FED_CHAN>"                     << endl ;
00860       *outstream << "   <FED_ROC_NUM>"  << iroc->second.fedrocnumber() << "</FED_ROC_NUM>"                   << endl ;
00861       *outstream << "  </DATA>"                                                                              << endl ;
00862       *outstream << ""                                                                                       << endl ;
00863     }
00864 }
00865 
00866 //=============================================================================================
00867 void PixelNameTranslation::writeXMLTrailer(std::ofstream *outstream,
00868                                            std::ofstream *out1stream,
00869                                            std::ofstream *out2stream) const 
00870 {
00871   std::string mthn = "[PixelNameTranslation::writeXMLTrailer()]\t\t\t    " ;
00872   
00873   *outstream << " </DATA_SET>"                                                                               << endl ;
00874   *outstream << "</ROOT> "                                                                                   << endl ;
00875 
00876   outstream->close() ;
00877 }
00878 
00879 //=============================================================================================
00880 void PixelNameTranslation::writeXML(pos::PixelConfigKey key, int version, std::string path) const {
00881   std::string mthn = "[PixelNameTranslation::writeXML]\t\t\t    " ;
00882   std::stringstream fullPath ;
00883 
00884   fullPath << path << "/Pixel_NameTranslation.xml" ;
00885   cout << __LINE__ << "]\t" << mthn << "Writing to: " << fullPath.str()  << endl ;
00886   
00887   std::ofstream out(fullPath.str().c_str()) ;
00888 
00889   out << "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"                                    << endl ;
00890   out << "<ROOT xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>"                               << endl ;
00891   out << ""                                                                                           << endl ;
00892   out << " <HEADER>"                                                                                  << endl ;
00893   out << "  <HINTS mode='only-det-root' />"                                                           << endl ; 
00894   out << "  <TYPE>"                                                                                   << endl ;
00895   out << "   <EXTENSION_TABLE_NAME>PIXEL_NAME_TRANSLATION</EXTENSION_TABLE_NAME>"                     << endl ;
00896   out << "   <NAME>Pixel Name Translation</NAME>"                                                     << endl ;
00897   out << "  </TYPE>"                                                                                  << endl ;
00898   out << "  <RUN>"                                                                                    << endl ;
00899   out << "   <RUN_TYPE>Pixel Name Translation</RUN_TYPE>"                                             << endl ;
00900   out << "   <RUN_NUMBER>1</RUN_NUMBER>"                                                              << endl ;
00901   out << "   <RUN_BEGIN_TIMESTAMP>" << pos::PixelTimeFormatter::getTime() << "</RUN_BEGIN_TIMESTAMP>" << endl ;
00902   out << "   <COMMENT_DESCRIPTION>Test of Name Translation xml</COMMENT_DESCRIPTION>"                 << endl ;
00903   out << "   <LOCATION>CERN TAC</LOCATION>"                                                           << endl ;
00904   out << "   <CREATED_BY_USER>Dario Menasce</CREATED_BY_USER>"                                        << endl ;
00905   out << "  </RUN>"                                                                                   << endl ;
00906   out << " </HEADER>"                                                                                 << endl ;
00907   out << ""                                                                                           << endl ;
00908   out << " <DATA_SET>"                                                                                << endl ;
00909   out << "  <VERSION>" << version << "</VERSION>"                                                     << endl ;
00910   out << "  <PART>"                                                                                   << endl ;
00911   out << "   <NAME_LABEL>CMS-PIXEL-ROOT</NAME_LABEL>"                                                 << endl ;
00912   out << "   <KIND_OF_PART>Detector ROOT</KIND_OF_PART>"                                              << endl ;
00913   out << "  </PART>"                                                                                  << endl ;
00914   out << ""                                                                                           << endl ;
00915 
00916   std::map<PixelROCName,PixelHdwAddress>::const_iterator iroc=translationtable_.begin();
00917 
00918   for (;iroc!=translationtable_.end();++iroc) 
00919     {
00920       // Find the PixelChannel for this ROC, in order to get the TBM channel.
00921       std::string TBMChannel = getChannelFromHdwAddress(iroc->second).TBMChannelString();
00922 
00923       out << "  <DATA>"                                                                               << endl ;
00924       out << "   <PXLFEC_NAME>"  << iroc->second.fecnumber()    << "</PXLFEC_NAME>"                   << endl ;
00925       out << "   <MFEC_POSN>"    << iroc->second.mfec()         << "</MFEC_POSN>"                     << endl ;
00926       out << "   <MFEC_CHAN>"    << iroc->second.mfecchannel()  << "</MFEC_CHAN>"                     << endl ;
00927       out << "   <HUB_ADDRS>"    << iroc->second.hubaddress()   << "</HUB_ADDRS>"                     << endl ;
00928       out << "   <PORT_NUM>"     << iroc->second.portaddress()  << "</PORT_NUM>"                      << endl ;
00929       out << "   <ROC_I2C_ADDR>" << iroc->second.rocid()        << "</ROC_I2C_ADDR>"                  << endl ;
00930       out << "   <PXLFED_NAME>"  << iroc->second.fednumber()    << "</PXLFED_NAME>"                   << endl ;
00931       out << "   <FED_CHAN>"     << iroc->second.fedchannel()   << "</FED_CHAN>"                      << endl ;
00932       out << "   <FED_RO_NUM>"  << iroc->second.fedrocnumber() << "</FED_ROC_NUM>"                    << endl ;
00933       out << "  </DATA>"                                                                              << endl ;
00934       out << ""                                                                                       << endl ;
00935     }
00936   out << " </DATA_SET> "                                                                              << endl ;
00937   out << "</ROOT> "                                                                                   << endl ;
00938   out.close() ;
00939   assert(0) ;
00940 }
00941 
00942 const std::vector<PixelROCName>& 
00943 PixelNameTranslation::getROCsFromChannel(const PixelChannel& aChannel) const {
00944 
00945   const PixelHdwAddress& channelHdwAddress = getHdwAddress(aChannel);
00946   return getROCsFromFEDChannel( channelHdwAddress.fednumber(), 
00947                                 channelHdwAddress.fedchannel() );
00948 
00949 }
00950 
00951 std::vector<PixelROCName> PixelNameTranslation::getROCsFromModule(const PixelModuleName& aModule) const
00952 {
00953   std::vector<PixelROCName> returnThis;
00954         
00955   std::set<PixelChannel> channelsOnThisModule = getChannelsOnModule(aModule);
00956   for ( std::set<PixelChannel>::const_iterator channelsOnThisModule_itr = channelsOnThisModule.begin(); channelsOnThisModule_itr != channelsOnThisModule.end(); channelsOnThisModule_itr++ )
00957     {
00958       std::vector<PixelROCName> ROCsOnThisChannel = getROCsFromChannel( *channelsOnThisModule_itr );
00959       for ( std::vector<PixelROCName>::const_iterator ROCsOnThisChannel_itr = ROCsOnThisChannel.begin(); ROCsOnThisChannel_itr != ROCsOnThisChannel.end(); ROCsOnThisChannel_itr++ )
00960         {
00961           returnThis.push_back(*ROCsOnThisChannel_itr);
00962         }
00963     }
00964 
00965   return returnThis;
00966 }
00967 
00968 //====================================================================================
00969 // Added by Dario
00970 bool PixelNameTranslation::ROCexists(PixelROCName theROC)
00971 {
00972   if (translationtable_.find(theROC)==translationtable_.end()) {return false ;}
00973   return true ;
00974 }
00975 
00976 
00977 std::map <unsigned int, std::set<unsigned int> > PixelNameTranslation::getFEDsAndChannels() const {
00978 
00979   std::map <unsigned int, std::set<unsigned int> > tmp;
00980 
00981 std::map<PixelChannel, PixelHdwAddress >::const_iterator 
00982   channelTranslationTable_itr = channelTranslationTable_.begin();
00983 
00984   for ( ; channelTranslationTable_itr != channelTranslationTable_.end(); 
00985           channelTranslationTable_itr++ ) {
00986 
00987     unsigned int fednumber=channelTranslationTable_itr->second.fednumber();
00988     unsigned int fedchannel=channelTranslationTable_itr->second.fedchannel();
00989 
00990     tmp[fednumber].insert(fedchannel);
00991 
00992   }
00993 
00994   return tmp;
00995   
00996 }