CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_9_patch3/src/CalibFormats/SiPixelObjects/src/PixelPortcardMap.cc

Go to the documentation of this file.
00001 //
00002 // This class provides the mapping between
00003 // portcards and the modules controlled by
00004 // the card
00005 //
00006 //
00007 //
00008  
00009 #include "CalibFormats/SiPixelObjects/interface/PixelPortcardMap.h"
00010 #include "CalibFormats/SiPixelObjects/interface/PixelTimeFormatter.h"
00011 
00012 #include <sstream>
00013 #include <cassert>
00014 #include <stdexcept>
00015 
00016 using namespace pos;
00017 using namespace std;
00018 
00019 PixelPortcardMap::PixelPortcardMap(std::vector< std::vector < std::string> > &tableMat):PixelConfigBase(" "," "," "){
00020 
00021   std::string mthn = "[PixelPortcardMap::PixelPortcardMap()]\t\t\t    " ;
00022   std::vector< std::string > ins = tableMat[0];
00023   std::map<std::string , int > colM;
00024   std::vector<std::string > colNames;
00025 /*
00026   EXTENSION_TABLE_NAME: PIXEL_PORTCARD_MAP (VIEW: CONF_KEY_PORTCARD_MAP_V)
00027 
00028   CONFIG_KEY                                NOT NULL VARCHAR2(80)
00029   KEY_TYPE                                  NOT NULL VARCHAR2(80)
00030   KEY_ALIAS                                 NOT NULL VARCHAR2(80)
00031   VERSION                                            VARCHAR2(40)
00032   KIND_OF_COND                              NOT NULL VARCHAR2(40)
00033   PORT_CARD                                 NOT NULL VARCHAR2(200)
00034   PANEL_NAME                                NOT NULL VARCHAR2(200)
00035   TBM_MODE                                           VARCHAR2(200)
00036   AOH_CHAN                                  NOT NULL NUMBER(38)
00037 */
00038   colNames.push_back("CONFIG_KEY"  );
00039   colNames.push_back("KEY_TYPE"    );
00040   colNames.push_back("KEY_ALIAS"   );
00041   colNames.push_back("VERSION"     );
00042   colNames.push_back("KIND_OF_COND");
00043   colNames.push_back("PORT_CARD"   );
00044   colNames.push_back("PANEL_NAME"  );
00045   colNames.push_back("TBM_MODE"    );
00046   colNames.push_back("AOH_CHAN"    );
00047 /*
00048   colNames.push_back("CONFIG_KEY_ID" );
00049   colNames.push_back("CONFG_KEY"     );
00050   colNames.push_back("VERSION"       );
00051   colNames.push_back("KIND_OF_COND"  );
00052   colNames.push_back("SERIAL_NUMBER" );
00053   colNames.push_back("PORT_CARD"     );
00054   colNames.push_back("PANEL_NAME"    );
00055   colNames.push_back("TBM_MODE"      );
00056   colNames.push_back("AOH_CHAN"      );
00057  */ 
00058   for(unsigned int c = 0 ; c < ins.size() ; c++)
00059     {
00060       for(unsigned int n=0; n<colNames.size(); n++)
00061         {
00062           if(tableMat[0][c] == colNames[n])
00063             {
00064               colM[colNames[n]] = c;
00065               break;
00066             }
00067         }
00068     }//end for
00069   /*
00070   for(unsigned int n=0; n<colNames.size(); n++)
00071     {
00072       if(colM.find(colNames[n]) == colM.end())
00073         {
00074           std::cerr << __LINE__ << "]\t" << mthn 
00075                     << "Couldn't find in the database the column with name " << colNames[n] << std::endl;
00076           assert(0);
00077         }
00078     }
00079   */
00080         
00081         
00082   std::string portcardname;
00083   std::string modulename;
00084   unsigned int aoh;
00085   std::string aohstring;
00086   std::string tbmChannel;
00087   
00088   for(unsigned int r = 1 ; r < tableMat.size() ; r++){    //Goes to every row of the Matrix
00089     
00090     portcardname = tableMat[r][colM["PORT_CARD"]];
00091     modulename   = tableMat[r][colM["PANEL_NAME"]];
00092     aohstring    = tableMat[r][colM["AOH_CHAN"]];
00093     tbmChannel   = tableMat[r][colM["TBM_MODE"]] ;
00094 //     cout << "[PixelPortcardMap::PixelPortcardMap()]\t\t\t    "
00095 //       << "Portcardname: " << portcardname
00096 //       << "\tmodulename: "   << modulename
00097 //       << "\taohstring: "    << aohstring
00098 //       << "\ttbmChannel:"   << tbmChannel
00099 //       << endl ;
00100     //aohname.erase(0,20);  // Is going to be change when Umesh put a AOH Channel column in the view.
00101     aoh = (((unsigned int)atoi(aohstring.c_str())));
00102     //std::cout<<aoh<<std::endl;
00103     PixelModuleName module(modulename);
00104     if (module.modulename()!=modulename)
00105       {
00106         std::cout << __LINE__ << "]\t" << mthn << "Modulename: " << modulename          << std::endl;
00107         std::cout << __LINE__ << "]\t" << mthn << "Parsed to : " << module.modulename() << std::endl;
00108         assert(0);
00109       }
00110     if(tbmChannel == "")
00111       {
00112         tbmChannel = "A";// assert(0); // add TBMChannel to the input, then remove assert
00113       }
00114     PixelChannel channel(module, tbmChannel);
00115     std::pair<std::string, int> portcardAndAOH(portcardname, aoh);
00116     map_[channel] = portcardAndAOH;
00117   }//end for r
00118 
00119 
00120 }//end constructor
00121 //*****************************************************************************
00122 
00123 PixelPortcardMap::PixelPortcardMap(std::string filename):
00124   PixelConfigBase(" "," "," "){
00125 
00126   std::string mthn = "[PixelPortcardMap::PixelPortcardMap()]\t\t\t    " ;
00127   std::ifstream in(filename.c_str());
00128 
00129   if (!in.good()){
00130     std::cout << __LINE__ << "]\t" << mthn << "Could not open: " << filename <<std::endl;
00131     throw std::runtime_error("Failed to open file "+filename);
00132   }
00133   else {
00134     std::cout << __LINE__ << "]\t" << mthn << "Reading from: "   << filename <<std::endl;
00135   }
00136   
00137   std::string dummy;
00138 
00139   in >> dummy;
00140   in >> dummy;
00141   in >> dummy;
00142   in >> dummy;
00143   in >> dummy;
00144 
00145   do {
00146     
00147     std::string portcardname;
00148     std::string modulename;
00149     std::string TBMChannel = "A";
00150     std::string aoh_string;
00151     unsigned int aoh;
00152 
00153     in >> portcardname >> modulename >> aoh_string ;
00154     if (aoh_string == "A" || aoh_string == "B") // Optionally, the TBM channel may be specified after the module name.  Check for this.
00155       {
00156         TBMChannel = aoh_string;
00157         in >> aoh_string;
00158       }
00159     aoh = atoi(aoh_string.c_str());
00160     
00161     if (!in.eof() ){
00162       PixelModuleName module(modulename);
00163       if (module.modulename()!=modulename){
00164         std::cout << __LINE__ << "]\t" << mthn << "Modulename: " << modulename          << std::endl;
00165         std::cout << __LINE__ << "]\t" << mthn << "Parsed to : " << module.modulename() << std::endl;
00166         assert(0);
00167       }
00168 
00169       PixelChannel channel(module, TBMChannel);
00170       std::pair<std::string, int> portcardAndAOH(portcardname, aoh);
00171       map_[channel] = portcardAndAOH;
00172     }
00173             
00174 
00175   }while (!in.eof());
00176 }
00177 
00178     
00179 /*const std::map<PixelModuleName, int>& PixelPortcardMap::modules(std::string portcard) const{
00180 
00181   std::map<std::string,std::map<PixelModuleName, int> >::const_iterator theportcard=map_.find(portcard);
00182 
00183   if (theportcard==map_.end()) {
00184     std::cout << "Could not find portcard with name:"<<portcard<<std::endl;
00185   }
00186 
00187   return theportcard->second;
00188 
00189 }*/
00190 
00191 PixelPortcardMap::~PixelPortcardMap(){}
00192 
00193 
00194 void PixelPortcardMap::writeASCII(std::string dir) const {
00195 
00196   
00197   std::string mthn = "[PixelPortcardMap::writeASCII()]\t\t\t\t    " ;
00198   if (dir!="") dir+="/";
00199   string filename=dir+"portcardmap.dat";
00200   
00201   ofstream out(filename.c_str());
00202   if(!out.good()){
00203     cout << __LINE__ << "]\t" << mthn << "Could not open file: " << filename << endl;
00204     assert(0);
00205   }
00206 
00207   out <<"# Portcard          Module                     AOH channel" <<endl;
00208   std::map< PixelChannel, std::pair<std::string, int> >::const_iterator i=map_.begin();
00209  for(;i!=map_.end();++i){
00210     out << i->second.first<<"   "
00211         << i->first.module()<<"       "
00212         << i->first.TBMChannel()<<"       "
00213         << i->second.second<<endl;
00214   }
00215   out.close();
00216 
00217 
00218 }
00219 
00220 
00221 
00222 const std::set< std::pair< std::string, int > > PixelPortcardMap::PortCardAndAOHs(const PixelModuleName& aModule) const
00223 {
00224         std::set< std::pair< std::string, int > > returnThis;
00225         
00226         // Loop over the entire map, searching for elements matching PixelModuleName.  Add matching elements to returnThis.
00227         for( std::map< PixelChannel, std::pair<std::string, int> >::const_iterator map_itr = map_.begin(); map_itr != map_.end(); ++map_itr )
00228         {
00229                 if ( map_itr->first.modulename() == aModule.modulename() )
00230                 {
00231                         returnThis.insert(map_itr->second);
00232                 }
00233         }
00234         
00235         return returnThis;
00236 }
00237 
00238 // Added by Dario for Debbie (the PixelPortcardMap::portcards is way to slow for the interactive tool)
00239 bool PixelPortcardMap::getName(std::string moduleName, std::string &portcardName)
00240 {
00241         for( std::map< PixelChannel, std::pair<std::string, int> >::const_iterator map_itr = map_.begin(); map_itr != map_.end(); ++map_itr )
00242         {
00243                 if ( map_itr->first.modulename() == moduleName )
00244                 {
00245                         portcardName = map_itr->second.first;
00246                         return true ;
00247                 }
00248         }
00249         return false ;
00250 }
00251 
00252 const std::set< std::string > PixelPortcardMap::portcards(const PixelModuleName& aModule) const
00253 {
00254         std::set< std::string > returnThis;
00255         const std::set< std::pair< std::string, int > > portCardAndAOHs = PortCardAndAOHs(aModule);
00256         for ( std::set< std::pair< std::string, int > >::const_iterator portCardAndAOHs_itr = portCardAndAOHs.begin(); portCardAndAOHs_itr != portCardAndAOHs.end(); ++portCardAndAOHs_itr)
00257         {
00258                 returnThis.insert( (*portCardAndAOHs_itr).first );
00259         }
00260         return returnThis;
00261 }
00262 
00263 const std::pair< std::string, int > PixelPortcardMap::PortCardAndAOH(const PixelModuleName& aModule, const PixelTBMChannel& TBMChannel) const
00264 {
00265         return PortCardAndAOH(PixelChannel(aModule, TBMChannel));
00266 }
00267 
00268 const std::pair< std::string, int > PixelPortcardMap::PortCardAndAOH(const PixelModuleName& aModule, const std::string& TBMChannel) const
00269 {
00270         return PortCardAndAOH(PixelChannel(aModule, TBMChannel));
00271 }
00272 
00273 const std::pair< std::string, int > PixelPortcardMap::PortCardAndAOH(const PixelChannel& aChannel) const
00274 {
00275         std::map< PixelChannel, std::pair<std::string, int> >::const_iterator found = map_.find(aChannel);
00276         if ( found == map_.end() )
00277         {
00278                 std::pair< std::string, int > returnThis("none", 0);
00279                 return returnThis;
00280         }
00281         else
00282         {
00283                 return found->second;
00284         }
00285 }
00286 
00287 std::set< PixelModuleName > PixelPortcardMap::modules(std::string portCardName) const
00288 {
00289         std::set< PixelModuleName > returnThis;
00290         
00291         // Loop over the entire map, searching for elements matching portCardName.  Add matching elements to returnThis.
00292         for( std::map< PixelChannel, std::pair<std::string, int> >::const_iterator map_itr = map_.begin(); map_itr != map_.end(); ++map_itr )
00293         {
00294                 if ( map_itr->second.first == portCardName )
00295                 {
00296                         returnThis.insert(map_itr->first.module());
00297                 }
00298         }
00299         
00300         return returnThis;
00301 }
00302 
00303 std::set< std::string > PixelPortcardMap::portcards(const PixelDetectorConfig* detconfig)
00304 {
00305         std::set< std::string > returnThis;
00306 
00307         if(detconfig != 0){
00308         
00309           //still done done in an awkward way, but this avoids an
00310           //double nested loop that we had in the first implementation
00311           const std::vector <PixelModuleName> moduleList=detconfig->getModuleList();
00312           std::set< std::string > moduleNames;
00313           for(std::vector <PixelModuleName>::const_iterator it=moduleList.begin(), it_end=moduleList.end(); it!=it_end; ++it){
00314             moduleNames.insert(it->modulename());
00315           }
00316 
00317           for( std::map< PixelChannel, std::pair<std::string, int> >::const_iterator map_itr = map_.begin(); map_itr != map_.end(); ++map_itr )
00318             {
00319               if ( moduleNames.find(map_itr->first.modulename()) != moduleNames.end() ){
00320                   returnThis.insert(map_itr->second.first);
00321                 }
00322             }
00323         
00324          
00325           
00326          
00327         }
00328         else{
00329         
00330           for( std::map< PixelChannel, std::pair<std::string, int> >::const_iterator map_itr = map_.begin(); map_itr != map_.end(); ++map_itr )
00331             {
00332               
00333               returnThis.insert(map_itr->second.first);
00334             }
00335           
00336         }
00337         
00338         return returnThis;
00339 }
00340 //=============================================================================================
00341 void PixelPortcardMap::writeXMLHeader(pos::PixelConfigKey key, 
00342                                       int version, 
00343                                       std::string path, 
00344                                       std::ofstream *outstream,
00345                                       std::ofstream *out1stream,
00346                                       std::ofstream *out2stream) const
00347 {
00348   std::string mthn = "[PixelPortcardMap::writeXMLHeader()]\t\t\t    " ;
00349   std::stringstream fullPath ;
00350   fullPath << path << "/Pixel_PortCardMap_" << PixelTimeFormatter::getmSecTime() << ".xml" ;
00351   std::cout << __LINE__ << "]\t" << mthn << "Writing to: " << fullPath.str() << std::endl ;
00352   
00353   outstream->open(fullPath.str().c_str()) ;
00354   
00355   *outstream << "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"                                    << std::endl ;
00356   *outstream << "<ROOT xmlns:xsi='https://www.w3.org/2001/XMLSchema-instance'>"                               << std::endl ;
00357   *outstream << " <HEADER>"                                                                                  << std::endl ;
00358   *outstream << "  <TYPE>"                                                                                   << std::endl ;
00359   *outstream << "   <EXTENSION_TABLE_NAME>PIXEL_PORTCARD_MAP</EXTENSION_TABLE_NAME>"                         << std::endl ;
00360   *outstream << "   <NAME>Pixel Port Card Map</NAME>"                                                        << std::endl ;
00361   *outstream << "  </TYPE>"                                                                                  << std::endl ;
00362   *outstream << "  <RUN>"                                                                                    << std::endl ;
00363   *outstream << "   <RUN_TYPE>Pixel Port Card Map</RUN_TYPE>"                                                << std::endl ;
00364   *outstream << "   <RUN_NUMBER>1</RUN_NUMBER>"                                                              << std::endl ;
00365   *outstream << "   <RUN_BEGIN_TIMESTAMP>" << pos::PixelTimeFormatter::getTime() << "</RUN_BEGIN_TIMESTAMP>" << std::endl ;
00366   *outstream << "   <LOCATION>CERN P5</LOCATION>"                                                            << std::endl ; 
00367   *outstream << "  </RUN>"                                                                                   << std::endl ;
00368   *outstream << " </HEADER>"                                                                                 << std::endl ;
00369   *outstream << ""                                                                                           << std::endl ;
00370   *outstream << " <DATA_SET>"                                                                                << std::endl ;
00371   *outstream << "  <PART>"                                                                                   << std::endl ;
00372   *outstream << "   <NAME_LABEL>CMS-PIXEL-ROOT</NAME_LABEL>"                                                 << std::endl ;
00373   *outstream << "   <KIND_OF_PART>Detector ROOT</KIND_OF_PART>"                                              << std::endl ;
00374   *outstream << "  </PART>"                                                                                  << std::endl ;
00375   *outstream << "  <VERSION>"             << version      << "</VERSION>"                                    << std::endl ;
00376   *outstream << "  <COMMENT_DESCRIPTION>" << getComment() << "</COMMENT_DESCRIPTION>"                        << std::endl ;
00377   *outstream << "  <CREATED_BY_USER>"     << getAuthor()  << "</CREATED_BY_USER>"                            << std::endl ;
00378 }
00379 
00380 //=============================================================================================
00381 void PixelPortcardMap::writeXML(std::ofstream *outstream,
00382                                 std::ofstream *out1stream,
00383                                 std::ofstream *out2stream) const 
00384 {
00385   std::string mthn = "[PixelPortcardMap::writeXML()]\t\t\t    " ;
00386 
00387 
00388   std::map< PixelChannel, std::pair<std::string, int> >::const_iterator i=map_.begin();
00389   for(;i!=map_.end();++i){
00390      *outstream << "  <DATA>"                                                                                << std::endl ;
00391      *outstream << "   <PORT_CARD>"  << i->second.first       << "</PORT_CARD>"                              << std::endl ;
00392      *outstream << "   <PANEL_NAME>" << i->first.module()     << "</PANEL_NAME>"                             << std::endl ;
00393      *outstream << "   <TBM_MODE>"   << i->first.TBMChannel() << "</TBM_MODE>"                               << std::endl ;
00394      *outstream << "   <AOH_CHAN>"   << i->second.second      << "</AOH_CHAN>"                               << std::endl ;
00395      *outstream << "  </DATA>"                                                                               << std::endl ;
00396    }  
00397 }
00398 
00399 //=============================================================================================
00400 void PixelPortcardMap::writeXMLTrailer(std::ofstream *outstream,
00401                                        std::ofstream *out1stream,
00402                                        std::ofstream *out2stream) const
00403 {
00404   std::string mthn = "[PixelPortcardMap::writeXMLTrailer()]\t\t\t    " ;
00405   
00406   *outstream << " </DATA_SET>"                                                                               << std::endl ;
00407   *outstream << "</ROOT> "                                                                                   << std::endl ;
00408 
00409   outstream->close() ;
00410 }
00411