CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/CalibFormats/SiPixelObjects/src/PixelTKFECConfig.cc

Go to the documentation of this file.
00001 //
00002 // This class stores the information about a TKFEC.
00003 // This include the number, crate, and base address
00004 //
00005 //
00006 
00007 #include "CalibFormats/SiPixelObjects/interface/PixelTKFECConfig.h"
00008 #include "CalibFormats/SiPixelObjects/interface/PixelTimeFormatter.h"
00009 #include <fstream>
00010 #include <sstream>
00011 #include <map>
00012 #include <assert.h>
00013 #include <stdexcept>
00014 
00015 using namespace pos;
00016 using namespace std;
00017 
00018 
00019 PixelTKFECConfig::PixelTKFECConfig(std::vector<std::vector<std::string> >& tableMat ) : PixelConfigBase(" "," "," ")
00020 {
00021   std::map<std::string , int > colM;
00022   std::vector<std::string > colNames;
00042   colNames.push_back("CONFIG_KEY"  ); 
00043   colNames.push_back("KEY_TYPE"    ); 
00044   colNames.push_back("KEY_ALIAS"   ); 
00045   colNames.push_back("VERSION"     ); 
00046   colNames.push_back("KIND_OF_COND"); 
00047   colNames.push_back("TRKFEC_NAME" ); 
00048   colNames.push_back("CRATE_LABEL" ); 
00049   colNames.push_back("CRATE_NUMBER"); 
00050   colNames.push_back("TYPE"        );         
00051   colNames.push_back("SLOT_NUMBER" ); 
00052   colNames.push_back("VME_ADDR"    );
00053   colNames.push_back("I2CSPEED"    );
00054 
00055   for(unsigned int c = 0 ; c < tableMat[0].size() ; c++)
00056     {
00057       for(unsigned int n=0; n<colNames.size(); n++)
00058         {
00059           if(tableMat[0][c] == colNames[n])
00060             {
00061               colM[colNames[n]] = c;
00062               break;
00063             }
00064         }
00065     }//end for
00066   /*
00067   for(unsigned int n=0; n<colNames.size(); n++)
00068     {
00069       if(colM.find(colNames[n]) == colM.end())
00070         {
00071           std::cerr << "[PixelTKFECConfig::PixelTKFECConfig()]\tCouldn't find in the database the column with name " << colNames[n] << std::endl;
00072           assert(0);
00073         }
00074     }
00075   */
00076 
00077   for(unsigned int r = 1 ; r < tableMat.size() ; r++)    //Goes to every row of the Matrix
00078     {
00079       std::string TKFECID  = tableMat[r][colM["TRKFEC_NAME"]] ;
00080       unsigned int crate   = atoi(tableMat[r][colM["CRATE_NUMBER"]].c_str()) ;
00081       std::string type     = "VME" ;
00082       unsigned int address = strtoul(tableMat[r][colM["VME_ADDR"]].c_str() , 0, 16);
00083       PixelTKFECParameters tmp;
00084       tmp.setTKFECParameters(TKFECID , crate , type, address);
00085       TKFECconfig_.push_back(tmp);
00086       //      cout << "[PixelTKFECConfig::PixelTKFECConfig()]\tID: " << TKFECID << " crate: " << crate << " address: " << address << endl;
00087     }
00088 }// end contructor
00089 
00090 //****************************************************************************************
00091 
00092  
00093 PixelTKFECConfig::PixelTKFECConfig(std::string filename):
00094     PixelConfigBase(" "," "," "){
00095 
00096     std::string mthn ="]\t[PixelTKFECConfig::PixelTKFECConfig()]\t\t\t    " ;
00097     std::ifstream in(filename.c_str());
00098 
00099     if (!in.good()){
00100         std::cout << __LINE__ << mthn << "Could not open: " << filename << std::endl;
00101         throw std::runtime_error("Failed to open file "+filename);
00102     }
00103     else {
00104         std::cout << __LINE__ << mthn << "Opened: "         << filename << std::endl;
00105     }
00106 
00107     std::string dummy;
00108 
00109     getline(in, dummy); // skip the column headings
00110 
00111     do {
00112         
00113         std::string TKFECID;
00114         unsigned int crate;
00115         std::string type;
00116         unsigned int address;
00117 
00118         in >> TKFECID >> std::dec >> crate >> type;
00119         if (type=="VME" || type=="PCI")
00120         {
00121                 in >> std::hex>> address >>std::dec ;
00122         }
00123         else // type not specified, default to "VME"
00124         {
00125                 address = strtoul(type.c_str(), 0, 16); // convert string to integer using base 16
00126                 type = "VME";
00127         }
00128 
00129         if (!in.eof() ){
00130             //std::cout << TKFECID <<" "<< crate << " "  
00131             //      << std::hex << vme_base_address<<std::dec<<std::endl;
00132             
00133             PixelTKFECParameters tmp;
00134             
00135             tmp.setTKFECParameters(TKFECID , crate , type, address);
00136             
00137             TKFECconfig_.push_back(tmp);
00138         }
00139 
00140     }
00141     while (!in.eof());
00142     in.close();
00143 
00144 }
00145  
00146 PixelTKFECConfig::~PixelTKFECConfig() {}
00147 
00148 void PixelTKFECConfig::writeASCII(std::string dir) const {
00149 
00150   if (dir!="") dir+="/";
00151   string filename=dir+"tkfecconfig.dat";
00152 
00153   ofstream out(filename.c_str());
00154   if(!out.good()){
00155     cout << "Could not open file:"<<filename<<endl;
00156     assert(0);
00157   }
00158 
00159   out <<"#TKFEC ID     crate     VME/PCI    slot/address" <<endl;
00160   for(unsigned int i=0;i<TKFECconfig_.size();i++){
00161     out << TKFECconfig_[i].getTKFECID()<<"          "
00162         << TKFECconfig_[i].getCrate()<<"          ";
00163     if (TKFECconfig_[i].getType()=="PCI") {
00164       out << "PCI       ";
00165     } else {
00166       out << "          ";
00167     }
00168     out << "0x"<<hex<<TKFECconfig_[i].getAddress()<<dec<<endl;
00169   }
00170   out.close();
00171 }
00172 
00173 
00174 //std::ostream& operator<<(std::ostream& s, const PixelTKFECConfig& table){
00175 
00176     //for (unsigned int i=0;i<table.translationtable_.size();i++){
00177     //  s << table.translationtable_[i]<<std::endl;
00178     //   }
00179 // return s;
00180 
00181 //}
00182 
00183 
00184 unsigned int PixelTKFECConfig::getNTKFECBoards() const{
00185 
00186     return TKFECconfig_.size();
00187 
00188 }
00189 
00190 std::string PixelTKFECConfig::getTKFECID(unsigned int i) const{
00191 
00192     assert(i<TKFECconfig_.size());
00193     return TKFECconfig_[i].getTKFECID();
00194 
00195 }
00196 
00197 
00198 unsigned int PixelTKFECConfig::getCrate(unsigned int i) const{
00199 
00200     assert(i<TKFECconfig_.size());
00201     return TKFECconfig_[i].getCrate();
00202 
00203 }
00204 
00205 std::string PixelTKFECConfig::getType(unsigned int i) const{
00206 
00207     assert(i<TKFECconfig_.size());
00208     return TKFECconfig_[i].getType();
00209 
00210 }
00211 
00212 unsigned int PixelTKFECConfig::getAddress(unsigned int i) const{
00213 
00214     assert(i<TKFECconfig_.size());
00215     return TKFECconfig_[i].getAddress();
00216 
00217 }
00218 
00219 
00220 unsigned int PixelTKFECConfig::crateFromTKFECID(std::string TKFECID) const{
00221 
00222     for(unsigned int i=0;i<TKFECconfig_.size();i++){
00223         if (TKFECconfig_[i].getTKFECID()==TKFECID) return TKFECconfig_[i].getCrate();
00224     }
00225 
00226     std::cout << "Could not find TKFEC ID:"<<TKFECID<<std::endl;
00227 
00228     assert(0);
00229 
00230     return 0;
00231 
00232 }
00233 
00234 std::string PixelTKFECConfig::typeFromTKFECID(std::string TKFECID) const{
00235 
00236     for(unsigned int i=0;i<TKFECconfig_.size();i++){
00237         if (TKFECconfig_[i].getTKFECID()==TKFECID) return TKFECconfig_[i].getType();
00238     }
00239 
00240     std::cout << "Could not find TKFEC ID:"<<TKFECID<<std::endl;
00241 
00242     assert(0);
00243 
00244     return 0;
00245 
00246 }
00247 
00248 unsigned int PixelTKFECConfig::addressFromTKFECID(std::string TKFECID) const{
00249 
00250     for(unsigned int i=0;i<TKFECconfig_.size();i++){
00251         if (TKFECconfig_[i].getTKFECID()==TKFECID) return TKFECconfig_[i].getAddress();
00252     }
00253 
00254     std::cout << "Could not find TKFEC ID:"<<TKFECID<<std::endl;
00255 
00256     assert(0);
00257 
00258     return 0;
00259 
00260 }
00261 
00262 //=============================================================================================
00263 void PixelTKFECConfig::writeXMLHeader(pos::PixelConfigKey key, 
00264                                       int version, 
00265                                       std::string path, 
00266                                       std::ofstream *outstream,
00267                                       std::ofstream *out1stream,
00268                                       std::ofstream *out2stream) const
00269 {
00270   std::string mthn = "[PixelTKFECConfig::writeXMLHeader()]\t\t\t    " ;
00271   std::stringstream maskFullPath ;
00272 
00273   maskFullPath << path << "/Pixel_TrackerFecParameters_" << PixelTimeFormatter::getmSecTime() << ".xml";
00274   std::cout << mthn << "Writing to: " << maskFullPath.str() << std::endl ;
00275 
00276   outstream->open(maskFullPath.str().c_str()) ;
00277   
00278   *outstream << "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"                                 << std::endl ;
00279   *outstream << "<ROOT xmlns:xsi='https://www.w3.org/2001/XMLSchema-instance'>"                            << std::endl ;
00280   *outstream << ""                                                                                        << std::endl ; 
00281   *outstream << " <HEADER>"                                                                               << std::endl ; 
00282   *outstream << "  <TYPE>"                                                                                << std::endl ; 
00283   *outstream << "   <EXTENSION_TABLE_NAME>TRACKER_FEC_PARAMETERS</EXTENSION_TABLE_NAME>"                  << std::endl ; 
00284   *outstream << "   <NAME>Tracker FEC Parameters</NAME>"                                                  << std::endl ; 
00285   *outstream << "  </TYPE>"                                                                               << std::endl ; 
00286   *outstream << "  <RUN>"                                                                                 << std::endl ; 
00287   *outstream << "   <RUN_TYPE>Tracker FEC Parameters</RUN_TYPE>"                                          << std::endl ; 
00288   *outstream << "   <RUN_NUMBER>1</RUN_NUMBER>"                                                           << std::endl ; 
00289   *outstream << "   <RUN_BEGIN_TIMESTAMP>" << PixelTimeFormatter::getTime() << "</RUN_BEGIN_TIMESTAMP>"   << std::endl ; 
00290   *outstream << "   <LOCATION>CERN P5</LOCATION>"                                                         << std::endl ; 
00291   *outstream << "  </RUN>"                                                                                << std::endl ; 
00292   *outstream << " </HEADER>"                                                                              << std::endl ; 
00293   *outstream << ""                                                                                        << std::endl ; 
00294   *outstream << " <DATA_SET>"                                                                             << std::endl ;
00295   *outstream << ""                                                                                        << std::endl ;
00296   *outstream << "  <VERSION>"             << version      << "</VERSION>"                                 << std::endl ;
00297   *outstream << "  <COMMENT_DESCRIPTION>" << getComment() << "</COMMENT_DESCRIPTION>"                     << std::endl ;
00298   *outstream << "  <CREATED_BY_USER>"   << getAuthor()  << "</CREATED_BY_USER>"                           << std::endl ;
00299   *outstream << ""                                                                                        << std::endl ;
00300   *outstream << "  <PART>"                                                                                << std::endl ;
00301   *outstream << "   <NAME_LABEL>CMS-PIXEL-ROOT</NAME_LABEL>"                                              << std::endl ;      
00302   *outstream << "   <KIND_OF_PART>Detector ROOT</KIND_OF_PART>"                                           << std::endl ;         
00303   *outstream << "  </PART>"                                                                               << std::endl ;
00304 
00305 }
00306 
00307 //=============================================================================================
00308 void PixelTKFECConfig::writeXML( std::ofstream *outstream,
00309                                  std::ofstream *out1stream,
00310                                  std::ofstream *out2stream) const 
00311 {
00312   std::string mthn = "[PixelTKFECConfig::writeXML()]\t\t\t    " ;
00313 
00314   for(unsigned int i=0;i<TKFECconfig_.size();i++){
00315     *outstream << "  <DATA>"                                                                                     << std::endl ;
00316     *outstream << "   <TRKFEC_NAME>"                  << TKFECconfig_[i].getTKFECID()        << "</TRKFEC_NAME>" << std::endl ;
00317     *outstream << "   <CRATE_NUMBER>"                 << TKFECconfig_[i].getCrate()          << "</CRATE_NUMBER>"<< std::endl ;
00318     *outstream << "   <VME_ADDR>"      << "0x" << hex << TKFECconfig_[i].getAddress() << dec << "</VME_ADDR>"    << std::endl ;
00319     *outstream << "  </DATA>"                                                                                    << std::endl ;
00320   }
00321 }
00322 
00323 //=============================================================================================
00324 void PixelTKFECConfig::writeXMLTrailer(std::ofstream *outstream,
00325                                        std::ofstream *out1stream,
00326                                        std::ofstream *out2stream ) const 
00327 {
00328   std::string mthn = "[PixelTKFECConfig::writeXMLTrailer()]\t\t\t    " ;
00329   
00330   *outstream << " </DATA_SET>"                                                                            << std::endl ;
00331   *outstream << "</ROOT>"                                                                                 << std::endl ;
00332   
00333   outstream->close() ;
00334   std::cout << mthn << "Data written "                                                                    << std::endl ;
00335 
00336 }