CMS 3D CMS Logo

/data/git/CMSSW_5_3_11_patch5/src/CalibFormats/SiPixelObjects/src/PixelROCMaskBits.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/PixelROCMaskBits.h"
00010 #include "CalibFormats/SiPixelObjects/interface/PixelBase64.h"
00011 #include <iostream>
00012 #include <cassert>
00013 #include <typeinfo>
00014 
00015 using namespace pos;
00016 
00017 //=====================================================================/
00018 PixelROCMaskBits::PixelROCMaskBits(){
00019 }
00020 
00021 
00022 /**********************Start Modification******************************/
00023 void PixelROCMaskBits::setROCMaskBits(PixelROCName& rocid ,std::string bits)
00024 {
00025   std::string mthn = "[PixelROCMaskBits::setROCMaskBits()]\t\t\t    " ;
00026 try
00027   {
00028     rocid_=rocid;
00029     char cpt[520] ;
00030     bits.copy( cpt , 520);
00031     for(unsigned int i = 0 ; i < bits.size(); i++)
00032       {
00033         bits_[i] = (unsigned char)cpt[i];
00034         //      std::cout<< "bits_[" << i << "]\t" << bits_[i] <<std::endl;
00035         //      std::cout<<rocid_<<std::endl;
00036         //      std::cout.flags(std::ios::hex)
00037       }
00038   }
00039  catch(std::bad_cast)
00040    {
00041      std::cout << __LINE__ << "]\t" << mthn << "Error casting variable." << std::endl;
00042    }
00043 }
00044 /**********************End Modification******************************/
00045 
00046 // Added by Dario: handles the base_64-decoded strings from aDB read
00047 int PixelROCMaskBits::read(const PixelROCName& rocid, std::string in){
00048  rocid_=rocid;
00049  for( int i=0; i<(int)sizeof(bits_); i++)
00050  {
00051   bits_[i] = in.at(i) ;
00052  }
00053  return 1 ;
00054 }
00055 
00056 //=====================================================================/
00057 int PixelROCMaskBits::read(const PixelROCName& rocid, std::ifstream& in){
00058 
00059     rocid_=rocid;
00060 
00061     std::string tag;
00062 
00063     for (int i=0;i<52;i++){
00064     
00065       in >> tag;
00066       
00067       //std::cout << "Now reading col:"<<tag<<std::endl;
00068 
00069       std::string data;
00070 
00071       in >> data;
00072 
00073       //std::cout <<"data.size()" <<data.size()<<std::endl;
00074 
00075       unsigned char byte=0;
00076 
00077       for(int j=0;j<80;j++){
00078          
00079         if (data[j]=='1') byte+=128;
00080         
00081         if ((j+1)%8==0) {
00082             //std::cout << "Writing byte:"<<(int)byte<<std::endl;
00083           bits_[i*10+(j+1)/8-1]=byte;
00084           byte=0; 
00085         }
00086         else{
00087           byte/=2;
00088         }
00089         
00090 
00091       }
00092 
00093     }
00094 
00095     return 1;
00096 
00097 }
00098 
00099 //=====================================================================/
00100 // modified by MR on 23-06-2008 11:57:58
00101 int PixelROCMaskBits::read(const PixelROCName& rocid, std::istringstream& in)
00102 {
00103   rocid_=rocid;
00104   std::string tag;
00105   for (int i=0;i<52;i++)
00106     {
00107       in >> tag;
00108       //std::cout << "Now reading col:"<<tag<<std::endl;
00109       std::string data;
00110       in >> data;
00111       //std::cout <<"data.size()" <<data.size()<<std::endl;
00112       unsigned char byte=0;
00113       for(int j=0;j<80;j++)
00114         {
00115         if (data[j]=='1') byte+=128;
00116         if ((j+1)%8==0) 
00117           {
00118             //std::cout << "Writing byte:"<<(int)byte<<std::endl;
00119             bits_[i*10+(j+1)/8-1]=byte;
00120             byte=0; 
00121           }
00122         else
00123           {
00124             byte/=2;
00125           }
00126       }
00127     }
00128   return 1;
00129 }
00130 
00131 //=====================================================================/
00132 int PixelROCMaskBits::readBinary(const PixelROCName& rocid, std::ifstream& in){
00133 
00134     rocid_=rocid;
00135 
00136 
00137     in.read((char*)bits_,520);
00138 
00139 
00140     return 1;
00141 }
00142 
00143 //=====================================================================/
00144 void PixelROCMaskBits::writeBinary(std::ofstream& out) const{
00145 
00146     out << (char)rocid_.rocname().size();
00147     out.write(rocid_.rocname().c_str(),rocid_.rocname().size());
00148 
00149     for(unsigned int i=0;i<520;i++){
00150         out << bits_[i];
00151     }
00152 
00153 }
00154 
00155 //=====================================================================/
00156 void PixelROCMaskBits::writeASCII(std::ofstream& out) const{
00157 
00158     out << "ROC:    "<<rocid_.rocname()<<std::endl;
00159  
00160     for(unsigned int col=0;col<52;col++){
00161         out << "col";
00162         if (col<10) out << "0";
00163         out <<col<<":  ";
00164         for (int row=0;row<80;row++){
00165             out << mask(col,row);
00166         }
00167         out << std::endl;
00168     }
00169 
00170 }
00171 
00172 //=====================================================================/
00173 unsigned int PixelROCMaskBits::mask(unsigned int col, unsigned int row) const{
00174 
00175   unsigned int tmp=bits_[col*10+row/8];
00176 //   std::cout << "c =  " << col << "\tr = " << row << "\tbits_[" << (col*10+row/8) << "]=" << bits_[col*10+row/8] << std::endl ;
00177 //   std::cout << "[PixelROCMaskBits::mask()] tmp iniziale " << tmp      << std::endl ;
00178   tmp=tmp>>(row%8);                                             
00179 //   std::cout << "[PixelROCMaskBits::mask()] tmp finale   " << tmp      << std::endl ;
00180 //   unsigned int res = tmp&0x01 ;                                      
00181 //   std::cout << "[PixelROCMaskBits::mask()] return value " << res      << std::endl ;
00182   return tmp&0x01;
00183 
00184 }
00185 
00186 //=====================================================================/
00187 void PixelROCMaskBits::setMask(unsigned int col, unsigned int row, unsigned int mask){
00188 
00189   assert(mask==0||mask==1);  
00190 
00191   unsigned int bit=1<<(row%8);
00192   if (mask) bits_[col*10+row/8]=bits_[col*10+row/8]|bit;  
00193   if (!mask) bits_[col*10+row/8]=bits_[col*10+row/8]&(0xff^bit);
00194 
00195 }
00196 
00197 //=====================================================================/
00198 std::ostream& pos::operator<<(std::ostream& s, const PixelROCMaskBits& mask){
00199 
00200   s << "Dumping ROC masks" <<std::endl; 
00201 
00202   for(int i=0;i<52;i++){
00203     s<<"Col"<<i<<":";
00204     for(int j=0;j<10;j++){
00205       unsigned char bitmask=1;
00206       for(int k=0;k<8;k++){
00207         if(mask.bits_[i*10+j]&bitmask) {
00208           s << "1";
00209         }
00210         else{
00211           s << "0";
00212         }
00213         bitmask*=2;
00214       }
00215     }
00216     s<<std::endl;
00217   }
00218 
00219 
00220   return s;
00221   
00222 }
00223 
00224 //=============================================================================================
00225 void PixelROCMaskBits::writeXML(std::ofstream * out) const
00226 {
00227   std::string mthn = "[PixelROCMaskBits::writeXML()]\t\t\t\t" ;
00228 
00229   std::string encoded = base64_encode(bits_, sizeof(bits_));
00230 
00231   *out << "  <DATA>"                                             << std::endl ;
00232   *out << "   <ROC_NAME>"  << rocid_.rocname() << "</ROC_NAME>"  << std::endl ;
00233   *out << "   <KILL_MASK>" << encoded          << "</KILL_MASK>" << std::endl ;
00234   *out << "  </DATA>"                                            << std::endl ;
00235   *out << " "                                                    << std::endl ;
00236       
00237 }
00238 
00239