CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/CalibFormats/SiPixelObjects/src/PixelROCTrimBits.cc

Go to the documentation of this file.
00001 //
00002 // This class provide the data structure for the
00003 // ROC DAC parameters
00004 //
00005 // At this point I do not see a reason to make an
00006 // abstract layer for this code.
00007 //
00008 
00009 #include "CalibFormats/SiPixelObjects/interface/PixelROCTrimBits.h"
00010 #include "CalibFormats/SiPixelObjects/interface/PixelBase64.h"
00011 #include <iostream>
00012 #include <sstream>
00013 #include <cassert>
00014 #include <typeinfo>
00015 
00016 using namespace pos;
00017 
00018 PixelROCTrimBits::PixelROCTrimBits()
00019 {
00020 }
00021 //This part has been modified from the orignal
00022 void PixelROCTrimBits::setROCTrimBits(PixelROCName rocid , std::string bits)
00023 {
00024 
00025 try{
00026   rocid_=rocid;
00027   char cpt[2080] ;
00028   bits.copy( cpt , 2080);
00029   for(unsigned int i = 0 ; i < bits.size(); i++)
00030         bits_[i] = (unsigned char)cpt[i];
00031 }catch(std::bad_cast){
00032 
00033 std::cout << "Error casting variable." << std::endl;
00034 
00035 }       
00036   
00037 }
00038 //End of part modified
00039 
00040 // Added by Dario: handles the base_64-decoded strings from aDB read
00041 int PixelROCTrimBits::read(const PixelROCName rocid, std::string in){
00042  rocid_=rocid;
00043  for( int i=0; i<(int)sizeof(bits_); i++)
00044  {
00045   bits_[i] = in.at(i) ;
00046  }
00047  return 1 ;
00048 }
00049 
00050 int PixelROCTrimBits::read(PixelROCName rocid,std::ifstream& in){
00051     
00052   std::string tag;
00053 
00054   //std::cout << "PixelROCTrimBits::read rocid:"<<rocid<<std::endl;
00055 
00056   rocid_=rocid;
00057   
00058   //std::cout << "PixelROCTrimBits::read rocid_:"<<rocid_<<std::endl;
00059 
00060   for (int i=0;i<52;i++){
00061     
00062     in >> tag;
00063     
00064     //std::cout << "Now reading col:"<<tag<<std::endl;
00065     
00066     std::string data;
00067     
00068     in >> data;
00069     
00070     //std::cout <<"data.size()" <<data.size()<<std::endl;
00071 
00072     unsigned char byte=0;
00073 
00074     for(int j=0;j<80;j++){
00075 
00076       unsigned char tmp=toupper(data[j])-48;
00077       if (tmp>9) tmp-=7;  //FIXME this is so ugly
00078 
00079       byte+=tmp;
00080       
00081       if ((j+1)%2==0) {
00082           //std::cout << "Writing byte:"<<(int)byte<<std::endl;
00083         bits_[i*40+(j+1)/2-1]=byte;
00084         byte=0; 
00085       }
00086       else{
00087         byte*=16;
00088       }
00089         
00090 
00091     }
00092 
00093   }
00094   return 1;
00095 
00096 }
00097 
00098 int PixelROCTrimBits::read(PixelROCName rocid, std::istringstream& in)
00099 {
00100   std::string tag;
00101   //std::cout << "PixelROCTrimBits::read rocid:"<<rocid<<std::endl;
00102   rocid_=rocid;
00103   //std::cout << "PixelROCTrimBits::read rocid_:"<<rocid_<<std::endl;
00104   for (int i=0;i<52;i++)
00105     {
00106       in >> tag;
00107 //       std::cout << "Now reading col:"<<tag<<std::endl;
00108       std::string data;
00109       in >> data;
00110 //       std::cout <<" data: " <<data<<std::endl;
00111       unsigned char byte=0;
00112       for(int j=0;j<80;j++)
00113         {
00114           unsigned char tmp=toupper(data[j])-48;
00115           if (tmp>9) tmp-=7;  //FIXME this is so ugly
00116           byte+=tmp;
00117           if ((j+1)%2==0) 
00118             {
00119               //std::cout << "Writing byte:"<<(int)byte<<std::endl;
00120               bits_[i*40+(j+1)/2-1]=byte;
00121               byte=0; 
00122             }
00123           else
00124             {
00125               byte*=16;
00126             }
00127         }
00128     }
00129   return 1;
00130 }
00131 
00132 
00133 
00134 int PixelROCTrimBits::readBinary(PixelROCName rocid,std::ifstream& in){
00135     
00136   rocid_=rocid;
00137 
00138   in.read((char*)bits_,2080);
00139 
00140   return 1;
00141 
00142 }
00143 
00144 
00145 void PixelROCTrimBits::writeBinary(std::ofstream& out) const{
00146 
00147     out << (char)rocid_.rocname().size();
00148     out.write(rocid_.rocname().c_str(),rocid_.rocname().size());
00149 
00150     //std::cout << "PixelROCTrimBits::writeBinary:"<<rocid_.rocname().size()
00151     // << " " <<rocid_.rocname()<<std::endl;
00152 
00153     for(unsigned int i=0;i<2080;i++){
00154         out << bits_[i];
00155     }
00156 
00157 }
00158 
00159 void PixelROCTrimBits::writeASCII(std::ofstream& out) const{
00160 
00161     //std::cout << " PixelROCTrimBits::writeASCII rocid_.rocname():"<<rocid_.rocname()<<std::endl;
00162 
00163 
00164     out << "ROC:     "<<rocid_.rocname()<<std::endl;
00165 
00166     //std::cout << "PixelROCTrimBits::writeBinary:"<<rocid_.rocname().size()
00167     //   << " " <<rocid_.rocname()<<std::endl;
00168 
00169     for(unsigned int col=0;col<52;col++){
00170         out << "col";
00171         if (col<10) out << "0";
00172         out <<col<<":   ";
00173         for (int row=0;row<80;row++){
00174             out << std::hex<<std::uppercase<<trim(col,row)<<std::dec;
00175         }
00176         out << std::endl;
00177     }
00178 
00179 }
00180 
00181 unsigned int PixelROCTrimBits::trim(unsigned int col, unsigned int row) const{
00182 
00183   unsigned int tmp=bits_[col*40+row/2];
00184   if (row%2==0) tmp/=16;
00185   return tmp&0x0F;
00186 
00187 }
00188 
00189 void PixelROCTrimBits::setTrim(unsigned int col, unsigned int row, unsigned int trim){
00190 
00191   assert(trim<16);
00192 
00193   unsigned int mask=0xf0;
00194   if (row%2==0) {
00195     trim<<=4;
00196     mask>>=4;
00197   }
00198   unsigned int tmp=bits_[col*40+row/2];
00199   bits_[col*40+row/2]=(tmp&mask)|trim;
00200 
00201 }
00202 
00203 
00204 
00205 
00206 std::ostream& pos::operator<<(std::ostream& s, const PixelROCTrimBits& mask){
00207 
00208   s << "Dumping ROC masks" <<std::endl; 
00209 
00210   for(int i=0;i<52;i++){
00211     s<<"Col"<<i<<": ";
00212     for(int j=0;j<40;j++){
00213       unsigned char bitmask=15*16;
00214       for(int k=0;k<2;k++){
00215         unsigned int tmp=mask.bits_[i*40+j]&bitmask;
00216         if (tmp>15) tmp/=16;
00217         s << std::hex << tmp << std::dec;
00218         bitmask/=16;
00219       }
00220     }
00221     s<<std::endl;
00222   }
00223 
00224 
00225   return s;
00226   
00227 }
00228 
00229 //=============================================================================================
00230 void PixelROCTrimBits::writeXML(std::ofstream * out) const
00231 {
00232   std::string mthn = "[PixelROCTrimBits::writeXML()]\t\t\t\t" ;
00233 
00234   std::string encoded = base64_encode(bits_, sizeof(bits_));
00235   std::string decoded = base64_decode(encoded);
00236 
00237   *out << "  <DATA>"                                             << std::endl ;
00238   *out << "   <ROC_NAME>"  << rocid_.rocname() << "</ROC_NAME>"  << std::endl ;
00239   *out << "   <TRIM_BITS>" << encoded          << "</TRIM_BITS>" << std::endl ;
00240   *out << "  </DATA>"                                            << std::endl ;
00241   *out << " "                                                    << std::endl ;
00242       
00243 }
00244