CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/CalibFormats/SiPixelObjects/src/PixelTBMSettings.cc

Go to the documentation of this file.
00001 //
00002 // This class provide a base class for the
00003 // pixel ROC DAC data for the pixel FEC configuration
00004 //
00005 //
00006 //
00007 //
00008 
00009 #include "CalibFormats/SiPixelObjects/interface/PixelTBMSettings.h"
00010 #include "CalibFormats/SiPixelObjects/interface/PixelTimeFormatter.h"
00011 #include <fstream>
00012 #include <sstream>
00013 #include <iostream>
00014 #include <ios>
00015 #include <assert.h>
00016 #include <stdexcept>
00017 
00018 
00019 using namespace pos;
00020 
00021 
00022 PixelTBMSettings::PixelTBMSettings(std::vector < std::vector< std::string> > &tableMat):PixelConfigBase("","",""){
00023   std::string mthn = "]\t[PixelTBMSettings::PixelTBMSettings()]\t\t\t    " ;
00024   std::vector< std::string > ins = tableMat[0];
00025   std::map<std::string , int > colM;
00026   std::vector<std::string > colNames;
00027 
00051   colNames.push_back("CONFIG_KEY"       );
00052   colNames.push_back("KEY_TYPE"         );
00053   colNames.push_back("KEY_ALIAS"        );
00054   colNames.push_back("VERSION"          );
00055   colNames.push_back("KIND_OF_COND"     );
00056   colNames.push_back("TBM_NAME"         );
00057   colNames.push_back("MODULE_NAME"      );
00058   colNames.push_back("HUB_ADDRS"        );
00059   colNames.push_back("TBM_MODE"         );
00060   colNames.push_back("ANLG_INBIAS_ADDR" );
00061   colNames.push_back("ANLG_INBIAS_VAL"  );
00062   colNames.push_back("ANLG_OUTBIAS_ADDR");
00063   colNames.push_back("ANLG_OUTBIAS_VAL" );
00064   colNames.push_back("ANLG_OUTGAIN_ADDR");
00065   colNames.push_back("ANLG_OUTGAIN_VAL" );
00066 
00067   for(unsigned int c = 0 ; c < ins.size() ; c++){
00068     for(unsigned int n=0; n<colNames.size(); n++){
00069       if(tableMat[0][c] == colNames[n]){
00070         colM[colNames[n]] = c;
00071         break;
00072       }
00073     }
00074   }//end for
00075   for(unsigned int n=0; n<colNames.size(); n++){
00076     if(colM.find(colNames[n]) == colM.end()){
00077       std::cerr << __LINE__ << mthn << "Couldn't find in the database the column with name " << colNames[n] << std::endl;
00078       assert(0);
00079     }
00080   }
00081  
00082   if(tableMat.size() >1)
00083     {
00084       //std::cout << __LINE__ << mthn << "Module from DB: " << tableMat[1][colM["MODULE_NAME"]]<< std::endl ;
00085       PixelROCName tmp(tableMat[1][colM["MODULE_NAME"]]);
00086       rocid_ = tmp ;
00087       //std::cout << __LINE__ << mthn << "Built ROCNAME: " << rocid_.rocname()<< std::endl ;
00088       
00089       analogInputBias_  = atoi(tableMat[1][colM["ANLG_INBIAS_VAL"]].c_str());
00090       analogOutputBias_ = atoi(tableMat[1][colM["ANLG_OUTBIAS_VAL"]].c_str());
00091       analogOutputGain_ = atoi(tableMat[1][colM["ANLG_OUTGAIN_VAL"]].c_str());
00092       
00093       if( tableMat[1][colM["TBM_MODE"]] == "SingleMode"){
00094         singlemode_=true;
00095       }
00096       else{
00097         singlemode_=false;
00098       }
00099     }
00100 }//end contructor
00101 
00102 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00103 
00104 PixelTBMSettings::PixelTBMSettings(std::string filename):
00105   PixelConfigBase("","",""){
00106 
00107     std::string mthn = "]\t[PixelTBMSettings::PixelTBMSettings()]\t\t\t    " ;
00108     if (filename[filename.size()-1]=='t'){
00109 
00110         std::ifstream in(filename.c_str());
00111 
00112         if (!in.good()){
00113             std::cout << __LINE__ << mthn << "Could not open:"<<filename<<std::endl;
00114             throw std::runtime_error("Failed to open file "+filename);
00115         }
00116         else {
00117           // std::cout << "Opened:"<<filename<<std::endl;
00118         }
00119         
00120         std::string tag;
00121 
00122         PixelROCName tmp(in);
00123 
00124         rocid_=tmp;
00125 
00126         unsigned int tmpint;    
00127 
00128         in >> tag;
00129         //std::cout << "Tag="<<tag<<std::endl;
00130         assert(tag=="AnalogInputBias:");
00131         in >> tmpint;
00132         analogInputBias_=tmpint;
00133 
00134         in >> tag;
00135         //std::cout << "Tag="<<tag<<std::endl;
00136         assert(tag=="AnalogOutputBias:");
00137         in >> tmpint;
00138         analogOutputBias_=tmpint;
00139 
00140         in >> tag;
00141         //std::cout << "Tag="<<tag<<std::endl;
00142         assert(tag=="AnalogOutputGain:");
00143         in >> tmpint;
00144         analogOutputGain_=tmpint;
00145 
00146         in >> tag;
00147         //std::cout << "Tag="<<tag<<std::endl;
00148         assert(tag=="Mode:");
00149         in >> tag;
00150         assert(tag=="SingleMode"||tag=="DualMode");
00151         
00152         singlemode_=true;
00153 
00154         if (tag=="DualMode") singlemode_=false;
00155 
00156         in.close();
00157 
00158     }
00159     else{
00160 
00161         std::ifstream in(filename.c_str(),std::ios::binary);
00162 
00163         if (!in.good()){
00164             std::cout << __LINE__ << mthn << "Could not open:"<<filename<<std::endl;
00165             assert(0);
00166         }
00167         else {
00168             std::cout << __LINE__ << mthn << "Opened:"<<filename<<std::endl;
00169         }
00170 
00171         char nchar;
00172         std::string s1;
00173 
00174         in.read(&nchar,1);
00175 
00176         s1.clear();
00177 
00178         //wrote these lines of code without ref. needs to be fixed
00179         for(int i=0;i< nchar; i++){
00180             char c;
00181             in >>c;
00182             s1.push_back(c);
00183         }
00184 
00185         PixelROCName tmp(s1);
00186 
00187         rocid_=tmp;
00188 
00189         in >> analogInputBias_;
00190         in >> analogOutputBias_;
00191         in >> analogOutputGain_;
00192         in >> singlemode_;
00193 
00194         in.close();
00195 
00196 
00197 
00198     }
00199 
00200 
00201 }
00202 
00203 void PixelTBMSettings::setTBMGenericValue(std::string what, int value) 
00204 {
00205  if(      what == "analogInputBias" )  {analogInputBias_  = (unsigned char)value;}
00206  else if( what == "analogOutputBias" ) {analogOutputBias_ = (unsigned char)value;}
00207  else if( what == "analogOutputGain" ) {analogOutputGain_ = (unsigned char)value;}
00208  else if( what == "Mode" )             {singlemode_       = (bool)value;         }
00209  else 
00210  {
00211    std::cout << __LINE__ << "]\t[PixelTBMSettings::setTBMGenericValue()]\t\tFATAL: invalid key/value pair: " << what << "/" << value << std::endl ; 
00212    assert(0);
00213  }
00214 }
00215  
00216 void PixelTBMSettings::writeBinary(std::string filename) const {
00217 
00218     std::ofstream out(filename.c_str(),std::ios::binary);
00219 
00220     out << (char)rocid_.rocname().size();
00221     out.write(rocid_.rocname().c_str(),rocid_.rocname().size());
00222 
00223     out <<analogInputBias_;
00224     out <<analogOutputBias_;
00225     out <<analogOutputGain_;
00226     out << singlemode_;
00227 
00228 
00229 }
00230 
00231 void PixelTBMSettings::writeASCII(std::string dir) const {
00232 
00233   PixelModuleName module(rocid_.rocname());
00234   
00235   if (dir!="") dir+="/";
00236   std::string filename=dir+"TBM_module_"+module.modulename()+".dat";
00237 
00238     std::ofstream out(filename.c_str());
00239 
00240     out << rocid_.rocname() << std::endl;
00241 
00242     out << "AnalogInputBias: "<<(int)analogInputBias_<<std::endl;
00243     out << "AnalogOutputBias: "<<(int)analogOutputBias_<<std::endl;
00244     out << "AnalogOutputGain: "<<(int)analogOutputGain_<<std::endl;
00245     out << "Mode: ";
00246     if (singlemode_) {
00247       out << "SingleMode" << std::endl;
00248     }
00249     else{
00250       out << "DualMode" << std::endl;
00251     }
00252 }
00253 
00254 void PixelTBMSettings::generateConfiguration(PixelFECConfigInterface* pixelFEC,
00255                                              PixelNameTranslation* trans,
00256                                              bool physics) const{
00257 
00258     PixelHdwAddress theROC=*(trans->getHdwAddress(rocid_));
00259 
00260 
00261     int mfec=theROC.mfec();
00262     int mfecchannel=theROC.mfecchannel();
00263     int tbmchannel=14; 
00264     int tbmchannelB=15; 
00265     int hubaddress=theROC.hubaddress();
00266 
00267     pixelFEC->injectrsttbm(mfec, 1);
00268     pixelFEC->injectrstroc(mfec,1);
00269     pixelFEC->enablecallatency(mfec,0);
00270     pixelFEC->disableexttrigger(mfec,0);
00271     pixelFEC->injecttrigger(mfec,0);
00272     pixelFEC->callatencycount(mfec,79);
00273 
00274     //pixelFEC->synccontrolregister(mfec);
00275 
00276     //Reset TBM and reset ROC
00277     pixelFEC->tbmcmd(mfec, mfecchannel, tbmchannel, hubaddress, 4, 2, 0x14, 0);
00278     //setting speed to 40MHz
00279     pixelFEC->tbmcmd(mfec, mfecchannel, tbmchannel, hubaddress, 4, 0, 1, 0);
00280     // setting the mode, we should always stay in the CAL mode
00281     // since the EventNumberClear Mode does not work correctly
00282     //if (physics) {  // comment out, stau always in the CAL mode,  d.k. 27/09/09
00283     //pixelFEC->tbmcmd(mfec, mfecchannel, tbmchannel, hubaddress, 4, 1, 0x80, 0);
00284     //} else {
00285     pixelFEC->tbmcmd(mfec, mfecchannel, tbmchannel, hubaddress, 4, 1, 0xc0, 0);
00286     //}
00287     //Enable token and analog output
00288     pixelFEC->tbmcmd(mfec, mfecchannel, tbmchannel, hubaddress, 4, 4, 0x0, 0);
00289 
00290     //Analog input bias
00291     pixelFEC->tbmcmd(mfec, mfecchannel, tbmchannel, hubaddress, 4, 5, 
00292                      analogInputBias_, 0);
00293     //Analog output bias
00294     pixelFEC->tbmcmd(mfec, mfecchannel, tbmchannel, hubaddress, 4, 6, 
00295                      analogOutputBias_, 0);
00296     //Analog output gain
00297     pixelFEC->tbmcmd(mfec, mfecchannel, tbmchannel, hubaddress, 4, 7, 
00298                      analogOutputGain_, 0);
00299 
00300 
00301     //setting speed to 40MHz
00302     pixelFEC->tbmcmd(mfec, mfecchannel, tbmchannelB, hubaddress, 4, 0, 1, 0);
00303     //pre-calibration, stay always in this mode
00304     pixelFEC->tbmcmd(mfec, mfecchannel, tbmchannelB, hubaddress, 4, 1, 0xc0, 0);
00305     //Reset TBM and reset ROC
00306     pixelFEC->tbmcmd(mfec, mfecchannel, tbmchannelB, hubaddress, 4, 2, 0x14, 0);
00307     //Enable token and analog output
00308     if (singlemode_){
00309       pixelFEC->tbmcmd(mfec, mfecchannel, tbmchannelB, hubaddress, 4, 4, 0x3, 0);
00310     }
00311     else{
00312       pixelFEC->tbmcmd(mfec, mfecchannel, tbmchannelB, hubaddress, 4, 4, 0x0, 0);
00313     }
00314 } 
00315 
00316 
00317 std::ostream& pos::operator<<(std::ostream& s, const PixelTBMSettings& tbm){
00318 
00319     s << "Module          :"<<tbm.rocid_.rocname() <<std::endl; 
00320     s << "analogInputBias :"<<tbm.analogInputBias_<<std::endl;
00321     s << "analogOutputBias:"<<tbm.analogOutputBias_<<std::endl;
00322     s << "analogOutputGain:"<<tbm.analogOutputGain_<<std::endl;
00323     if (tbm.singlemode_){
00324       s << "mode            :Singlemode"<<std::endl;
00325     }
00326     else{
00327       s << "mode            :Dualmode"<<std::endl;
00328     }
00329 
00330     return s;
00331 
00332 }
00333 //=============================================================================================
00334 void PixelTBMSettings::writeXMLHeader(pos::PixelConfigKey key, 
00335                                       int version, 
00336                                       std::string path, 
00337                                       std::ofstream *outstream,
00338                                       std::ofstream *out1stream,
00339                                       std::ofstream *out2stream) const
00340 {
00341   std::string mthn = "]\t[PixelTBMSettings::writeXMLHeader()]\t\t\t    " ;
00342   std::stringstream fullPath ;
00343   fullPath << path << "/Pixel_TbmParameters_" << PixelTimeFormatter::getmSecTime() << ".xml" ;
00344   std::cout << __LINE__ << mthn << "Writing to: " << fullPath.str() << std::endl ;
00345   
00346   outstream->open(fullPath.str().c_str()) ;
00347   
00348   *outstream << "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"                                    << std::endl ;
00349   *outstream << "<ROOT xmlns:xsi='https://www.w3.org/2001/XMLSchema-instance'>"                               << std::endl ;
00350   *outstream << " <HEADER>"                                                                                  << std::endl ;
00351   *outstream << "  <TYPE>"                                                                                   << std::endl ;
00352   *outstream << "   <EXTENSION_TABLE_NAME>PIXEL_TBM_PARAMETERS</EXTENSION_TABLE_NAME>"                       << std::endl ;
00353   *outstream << "   <NAME>Pixel TBM Parameters</NAME>"                                                       << std::endl ;
00354   *outstream << "  </TYPE>"                                                                                  << std::endl ;
00355   *outstream << "  <RUN>"                                                                                    << std::endl ;
00356   *outstream << "   <RUN_TYPE>Pixel TBM Parameters</RUN_TYPE>"                                               << std::endl ;
00357   *outstream << "   <RUN_NUMBER>1</RUN_NUMBER>"                                                              << std::endl ;
00358   *outstream << "   <RUN_BEGIN_TIMESTAMP>" << pos::PixelTimeFormatter::getTime() << "</RUN_BEGIN_TIMESTAMP>" << std::endl ;
00359   *outstream << "   <LOCATION>CERN P5</LOCATION>"                                                            << std::endl ; 
00360   *outstream << "  </RUN>"                                                                                   << std::endl ;
00361   *outstream << " </HEADER>"                                                                                 << std::endl ;
00362   *outstream << ""                                                                                           << std::endl ;
00363   *outstream << " <DATA_SET>"                                                                                << std::endl ;
00364   *outstream << "  <PART>"                                                                                   << std::endl ;
00365   *outstream << "   <NAME_LABEL>CMS-PIXEL-ROOT</NAME_LABEL>"                                                 << std::endl ;
00366   *outstream << "   <KIND_OF_PART>Detector ROOT</KIND_OF_PART>"                                              << std::endl ;
00367   *outstream << "  </PART>"                                                                                  << std::endl ;
00368   *outstream << "  <VERSION>"             << version      << "</VERSION>"                                    << std::endl ;
00369   *outstream << "  <COMMENT_DESCRIPTION>" << getComment() << "</COMMENT_DESCRIPTION>"                        << std::endl ;
00370   *outstream << "  <CREATED_BY_USER>"     << getAuthor()  << "</CREATED_BY_USER>"                            << std::endl ;
00371   *outstream << " "                                                                                          << std::endl ;
00372 }
00373 
00374 //=============================================================================================
00375 void PixelTBMSettings::writeXML(std::ofstream *outstream,
00376                                 std::ofstream *out1stream,
00377                                 std::ofstream *out2stream) const 
00378 {
00379   std::string mthn = "]\t[PixelTBMSettings::writeXML()]\t\t\t    " ;
00380 
00381   PixelModuleName module(rocid_.rocname());
00382                                                                                                              
00383   *outstream << "  <DATA>"                                                                                   << std::endl ;
00384   *outstream << "   <MODULE_NAME>"      << rocid_.rocname()      << "</MODULE_NAME>"                         << std::endl ;
00385   *outstream << "   <ANLG_INBIAS_VAL>"  <<(int)analogInputBias_  << "</ANLG_INBIAS_VAL>"                     << std::endl ;
00386   *outstream << "   <ANLG_OUTBIAS_VAL>" <<(int)analogOutputBias_ << "</ANLG_OUTBIAS_VAL>"                    << std::endl ;
00387   *outstream << "   <ANLG_OUTGAIN_VAL>" <<(int)analogOutputGain_ << "</ANLG_OUTGAIN_VAL>"                    << std::endl ;
00388   if (singlemode_) {                                                                                            
00389     *outstream << "  <TBM_MODE>SingleMode</TBM_MODE>"                                                        << std::endl ;
00390   }
00391   else{                                                                                                      
00392     *outstream << "  <TBM_MODE>DualMode</TBM_MODE>"                                                          << std::endl ;
00393   }                                                                                                          
00394   *outstream << "  </DATA>"                                                                                  << std::endl ;
00395 }
00396 
00397 //=============================================================================================
00398 void PixelTBMSettings::writeXMLTrailer(std::ofstream *outstream,
00399                                        std::ofstream *out1stream,
00400                                        std::ofstream *out2stream) const 
00401 {
00402   std::string mthn = "]\t[PixelTBMSettings::writeXMLTrailer()]\t\t\t    " ;
00403   
00404   *outstream << " "                                                                                          << std::endl ;
00405   *outstream << " </DATA_SET>"                                                                               << std::endl ;
00406   *outstream << "</ROOT> "                                                                                   << std::endl ;
00407 
00408   outstream->close() ;
00409 }