CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/CalibFormats/SiPixelObjects/src/PixelTrimAllPixels.cc

Go to the documentation of this file.
00001 //
00002 // This class provide a base class for the
00003 // pixel trim data for the pixel FEC configuration
00004 // This is a pure interface (abstract class) that
00005 // needs to have an implementation.
00006 //
00007 // Need to figure out what is 'VMEcommand' below! 
00008 //
00009 // All applications should just use this 
00010 // interface and not care about the specific
00011 // implementation.
00012 //
00013 
00014 #include <sstream>
00015 #include <iostream>
00016 #include <ios>
00017 #include <assert.h>
00018 #include <stdexcept>
00019 #include "CalibFormats/SiPixelObjects/interface/PixelTrimAllPixels.h"
00020 #include "CalibFormats/SiPixelObjects/interface/PixelTimeFormatter.h"
00021 #include "CalibFormats/SiPixelObjects/interface/PixelBase64.h"
00022 
00023 using namespace pos;
00024 
00025 PixelTrimAllPixels::PixelTrimAllPixels( std::vector <std::vector<std::string> >& tableMat):
00026   PixelTrimBase("","","")
00027 {
00028     std::string mthn = "]\t[PixelTrimAllPixels::PixelTrimAllPixels()]\t\t    " ; 
00029     std::stringstream currentRocName;
00030     std::map<std::string , int > colM;
00031     std::vector<std::string > colNames;
00044     colNames.push_back("CONFIG_KEY"  );
00045     colNames.push_back("KEY_TYPE"    );
00046     colNames.push_back("KEY_ALIAS"   );
00047     colNames.push_back("VERSION"     );
00048     colNames.push_back("KIND_OF_COND");
00049     colNames.push_back("ROC_NAME"    );
00050     colNames.push_back("TRIM_BITS"   );
00051  
00052 
00053     for(unsigned int c = 0 ; c < tableMat[0].size() ; c++)
00054       {
00055         for(unsigned int n=0; n<colNames.size(); n++)
00056           {
00057             if(tableMat[0][c] == colNames[n])
00058               {
00059                 colM[colNames[n]] = c;
00060                 break;
00061               }
00062           }
00063       }//end for
00064     for(unsigned int n=0; n<colNames.size(); n++)
00065       {
00066       if(colM.find(colNames[n]) == colM.end())
00067         {
00068           std::cerr << __LINE__ << mthn << "Couldn't find in the database the column with name " << colNames[n] << std::endl;
00069           assert(0);
00070         }
00071       }
00072     
00073     //unsigned char *bits ;        /// supose to be " unsigned  char bits[tableMat[1][colM["TRIM_BLOB"]].size()] ;  "
00074     //char c[2080];
00075     std::string bits;
00076         trimbits_.clear() ;
00077     for(unsigned int r = 1 ; r < tableMat.size() ; r++)    //Goes to every row of the Matrix
00078       {
00079         PixelROCName rocid( tableMat[r][colM["ROC_NAME"]] );
00080         PixelROCTrimBits tmp;     
00081         tmp.read(rocid, base64_decode(tableMat[r][colM["TRIM_BITS"]])) ;
00082         trimbits_.push_back(tmp);
00083       }//end for r 
00084     //std::cout<<trimbits_.size()<<std::endl;
00085 } //end contructor with databasa table
00086 
00087  
00088 PixelTrimAllPixels::PixelTrimAllPixels(std::string filename):
00089   PixelTrimBase("","",""){
00090 
00091     if (filename[filename.size()-1]=='t'){
00092 
00093       std::ifstream in(filename.c_str());
00094         
00095       if (!in.good())  throw std::runtime_error("Failed to open file "+filename);
00096       //        std::cout << "filename =" << filename << std::endl;
00097 
00098       std::string s1;
00099       in >> s1; 
00100 
00101       trimbits_.clear();
00102 
00103 
00104       while (in.good()){
00105           
00106         std::string s2;
00107         in>>s2;
00108 
00109         //          std::cout << "PixelTrimAllPixels::PixelTrimAllPixels read s1:"<<s1<< " s2:" << s2 << std::endl;
00110 
00111         assert( s1 == "ROC:" );
00112 
00113         PixelROCName rocid(s2);
00114 
00115         //std::cout << "PixelTrimAllPixels::PixelTrimAllPixels read rocid:"<<rocid<<std::endl;
00116             
00117         PixelROCTrimBits tmp;
00118       
00119         tmp.read(rocid, in);
00120 
00121         trimbits_.push_back(tmp);
00122 
00123         in >> s1;
00124 
00125       }
00126 
00127       in.close();
00128 
00129     }
00130     else{
00131 
00132       std::ifstream in(filename.c_str(),std::ios::binary);
00133       if (!in.good())  throw std::runtime_error("Failed to open file "+filename);
00134 
00135       char nchar;
00136 
00137       in.read(&nchar,1);
00138 
00139       std::string s1;
00140 
00141       //wrote these lines of code without ref. needs to be fixed
00142       for(int i=0;i< nchar; i++){
00143         char c;
00144         in >>c;
00145         s1.push_back(c);
00146       }
00147 
00148       //std::cout << "READ ROC name:"<<s1<<std::endl;
00149 
00150       trimbits_.clear();
00151 
00152 
00153       while (!in.eof()){
00154 
00155         //std::cout << "PixelTrimAllPixels::PixelTrimAllPixels read s1:"<<s1<<std::endl;
00156 
00157         PixelROCName rocid(s1);
00158 
00159         //std::cout << "PixelTrimAllPixels::PixelTrimAllPixels read rocid:"<<rocid<<std::endl;
00160             
00161         PixelROCTrimBits tmp;
00162       
00163         tmp.readBinary(rocid, in);
00164 
00165         trimbits_.push_back(tmp);
00166 
00167 
00168         in.read(&nchar,1);
00169 
00170         s1.clear();
00171 
00172         if (in.eof()) continue;
00173 
00174         //wrote these lines of code without ref. needs to be fixed
00175         for(int i=0;i< nchar; i++){
00176           char c;
00177           in >>c;
00178           s1.push_back(c);
00179         }
00180 
00181 
00182       }
00183 
00184       in.close();
00185 
00186 
00187 
00188     }
00189 
00190     //std::cout << "Read trimbits for "<<trimbits_.size()<<" ROCs"<<std::endl;
00191 
00192   }
00193 
00194 
00195 //std::string PixelTrimAllPixels::getConfigCommand(PixelMaskBase& pixelMask){
00196 //
00197 //  std::string s;
00198 //  return s;
00199 //
00200 //}
00201 
00202 PixelROCTrimBits PixelTrimAllPixels::getTrimBits(int ROCId) const {
00203 
00204   return trimbits_[ROCId];
00205 
00206 }
00207 
00208 PixelROCTrimBits* PixelTrimAllPixels::getTrimBits(PixelROCName name){
00209 
00210   for(unsigned int i=0;i<trimbits_.size();i++){
00211     if (trimbits_[i].name()==name) return &(trimbits_[i]);
00212   }
00213 
00214   return 0;
00215 
00216 }
00217 
00218 
00219 
00220 void PixelTrimAllPixels::generateConfiguration(PixelFECConfigInterface* pixelFEC,
00221                                                PixelNameTranslation* trans,
00222                                                const PixelMaskBase& pixelMask) const{
00223 
00224   for(unsigned int i=0;i<trimbits_.size();i++){
00225 
00226     std::vector<unsigned char> trimAndMasks(4160);
00227 
00228     const PixelROCMaskBits& maskbits=pixelMask.getMaskBits(i);
00229 
00230     for (unsigned int col=0;col<52;col++){
00231       for (unsigned int row=0;row<80;row++){
00232         unsigned char tmp=trimbits_[i].trim(col,row);
00233         if (maskbits.mask(col,row)!=0) tmp|=0x80;
00234         trimAndMasks[col*80+row]=tmp;
00235       }
00236     }
00237 
00238     // the slow way, one pixel at a time
00239     //pixelFEC->setMaskAndTrimAll(*(trans->getHdwAddress(trimbits_[i].name())),trimAndMasks);
00240     // the fast way, a full roc in column mode (& block xfer)
00241     const PixelHdwAddress* theROC = trans->getHdwAddress(trimbits_[i].name());
00242     pixelFEC->roctrimload(theROC->mfec(),
00243                           theROC->mfecchannel(),
00244                           theROC->hubaddress(),
00245                           theROC->portaddress(),
00246                           theROC->rocid(),
00247                           trimAndMasks);
00248   }
00249 }
00250 
00251 void PixelTrimAllPixels::writeBinary(std::string filename) const{
00252 
00253   
00254   std::ofstream out(filename.c_str(),std::ios::binary);
00255 
00256   for(unsigned int i=0;i<trimbits_.size();i++){
00257     trimbits_[i].writeBinary(out);
00258   }
00259 
00260 
00261 }
00262 
00263 
00264 void PixelTrimAllPixels::writeASCII(std::string dir) const{
00265 
00266   if (dir!="") dir+="/";
00267   PixelModuleName module(trimbits_[0].name().rocname());
00268   std::string filename=dir+"ROC_Trims_module_"+module.modulename()+".dat";
00269   
00270 
00271   std::ofstream out(filename.c_str());
00272 
00273   for(unsigned int i=0;i<trimbits_.size();i++){
00274     trimbits_[i].writeASCII(out);
00275   }
00276 
00277 
00278 }
00279 //=============================================================================================
00280 void PixelTrimAllPixels::writeXMLHeader(pos::PixelConfigKey key, 
00281                                         int version, 
00282                                         std::string path, 
00283                                         std::ofstream *outstream,
00284                                         std::ofstream *out1stream,
00285                                         std::ofstream *out2stream) const
00286 {
00287   std::string mthn = "[PixelTrimAllPixels::writeXMLHeader()]\t\t\t    " ;
00288   std::stringstream maskFullPath ;
00289 
00290   maskFullPath << path << "/Pixel_RocTrims_" << PixelTimeFormatter::getmSecTime() << ".xml";
00291   std::cout << mthn << "Writing to: " << maskFullPath.str() << std::endl ;
00292 
00293   outstream->open(maskFullPath.str().c_str()) ;
00294   
00295   *outstream << "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"                                 << std::endl ;
00296   *outstream << "<ROOT xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>"                            << std::endl ;
00297   *outstream << ""                                                                                        << std::endl ; 
00298   *outstream << " <HEADER>"                                                                               << std::endl ; 
00299   *outstream << "  <TYPE>"                                                                                << std::endl ; 
00300   *outstream << "   <EXTENSION_TABLE_NAME>ROC_TRIMS</EXTENSION_TABLE_NAME>"                               << std::endl ; 
00301   *outstream << "   <NAME>ROC Trim Bits</NAME>"                                                           << std::endl ; 
00302   *outstream << "  </TYPE>"                                                                               << std::endl ; 
00303   *outstream << "  <RUN>"                                                                                 << std::endl ; 
00304   *outstream << "   <RUN_TYPE>ROC Trim Bits</RUN_TYPE>"                                                   << std::endl ; 
00305   *outstream << "   <RUN_NUMBER>1</RUN_NUMBER>"                                                           << std::endl ; 
00306   *outstream << "   <RUN_BEGIN_TIMESTAMP>" << PixelTimeFormatter::getTime() << "</RUN_BEGIN_TIMESTAMP>"   << std::endl ; 
00307   *outstream << "   <LOCATION>CERN P5</LOCATION>"                                                         << std::endl ; 
00308   *outstream << "  </RUN>"                                                                                << std::endl ; 
00309   *outstream << " </HEADER>"                                                                              << std::endl ; 
00310   *outstream << ""                                                                                        << std::endl ; 
00311   *outstream << " <DATA_SET>"                                                                             << std::endl ;
00312   *outstream << ""                                                                                        << std::endl ;
00313   *outstream << "  <VERSION>"             << version      << "</VERSION>"                                 << std::endl ;
00314   *outstream << "  <COMMENT_DESCRIPTION>" << getComment() << "</COMMENT_DESCRIPTION>"                     << std::endl ;
00315   *outstream << "  <CREATED_BY_USER>"   << getAuthor()  << "</CREATED_BY_USER>"                   << std::endl ;
00316   *outstream << ""                                                                                        << std::endl ;
00317   *outstream << "  <PART>"                                                                                << std::endl ;
00318   *outstream << "   <NAME_LABEL>CMS-PIXEL-ROOT</NAME_LABEL>"                                              << std::endl ;      
00319   *outstream << "   <KIND_OF_PART>Detector ROOT</KIND_OF_PART>"                                           << std::endl ;         
00320   *outstream << "  </PART>"                                                                               << std::endl ;
00321   *outstream << ""                                                                                        << std::endl ;
00322 
00323 }
00324 
00325 //=============================================================================================
00326 void PixelTrimAllPixels::writeXML( std::ofstream *outstream,
00327                                    std::ofstream *out1stream,
00328                                    std::ofstream *out2stream) const 
00329 {
00330   std::string mthn = "[PixelTrimAllPixels::writeXML()]\t\t\t    " ;
00331 
00332   for(unsigned int i=0;i<trimbits_.size();i++){
00333       trimbits_[i].writeXML(outstream);
00334   }
00335 }
00336 
00337 //=============================================================================================
00338 void PixelTrimAllPixels::writeXMLTrailer(std::ofstream *outstream,
00339                                          std::ofstream *out1stream,
00340                                          std::ofstream *out2stream ) const 
00341 {
00342   std::string mthn = "[PixelTrimAllPixels::writeXMLTrailer()]\t\t\t    " ;
00343   
00344   *outstream << " </DATA_SET>"                                                                            << std::endl ;
00345   *outstream << "</ROOT>"                                                                                 << std::endl ;
00346   
00347   outstream->close() ;
00348   std::cout << mthn << "Data written "                                                                    << std::endl ;
00349 
00350 }