CMS 3D CMS Logo

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

Go to the documentation of this file.
00001 #include "CalibFormats/SiPixelObjects/interface/PixelFEDTestDAC.h"
00002 #include "CalibFormats/SiPixelObjects/interface/PixelTimeFormatter.h"
00003 #include <string.h>
00004 #include <cassert>
00005 #include <map>
00006 #include <sstream>
00007 
00008 using namespace std;
00009 
00010 using namespace pos;
00011 
00012 PixelFEDTestDAC::PixelFEDTestDAC(std::vector<std::vector<std::string> > & tableMat )
00013 {
00014 
00015   std::string mthn = "[PixelFEDTestDAC::PixelFEDTestDAC()]\t\t\t    " ;
00016   const unsigned long int UB=200;
00017   const unsigned long int B=500;
00018   const unsigned long int offset=0;
00019   vector <unsigned int> pulseTrain(256), pixelDCol(1), pixelPxl(2), pixelTBMHeader(3), pixelTBMTrailer(3);
00020   unsigned int DCol, LorR, start=15;
00021   std::string line;
00022   std::string::size_type loc1, loc2, loc3, loc4;
00023   unsigned long int npos=std::string::npos;
00024   int i;
00025 
00026   std::map<std::string , int > colM;
00027   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("CALIB_TYPE"         );
00048   colNames.push_back("CALIB_OBJ_DATA_FILE");
00049   colNames.push_back("CALIB_OBJ_DATA_CLOB");
00050   
00051   for(unsigned int c = 0 ; c < tableMat[0].size() ; c++)
00052     {
00053       for(unsigned int n=0; n<colNames.size(); n++)
00054         {
00055           if(tableMat[0][c] == colNames[n])
00056             {
00057               colM[colNames[n]] = c;
00058               break;
00059             }
00060         }
00061     }//end for
00062   for(unsigned int n=0; n<colNames.size(); n++)
00063     {
00064       if(colM.find(colNames[n]) == colM.end())
00065         {
00066           std::cerr << __LINE__ << "]\t" << mthn << "Couldn't find in the database the column with name " << colNames[n] << std::endl;
00067           assert(0);
00068         }
00069     }
00070 
00071   
00072   std::istringstream fin ;
00073   fin.str(tableMat[1][colM["CALIB_OBJ_DATA_CLOB"]]) ;
00074   
00075   // Initialise the pulseTrain to offset+black
00076   for (unsigned int i=0;i<pulseTrain.size();++i)
00077     {
00078       pulseTrain[i]=offset+B;
00079     }
00080 
00081   i=start;
00082   
00083   getline(fin, line);
00084   mode_=line;
00085   assert(mode_=="EmulatedPhysics"||
00086          mode_=="FEDBaselineWithTestDACs"||
00087          mode_=="FEDAddressLevelWithTestDACs");
00088 
00089   while (!fin.eof())
00090     {
00091       getline(fin, line);
00092                 
00093       if (line.find("TBMHeader")!=npos)
00094         {
00095           loc1=line.find("(");         if (loc1==npos) {cout<< __LINE__ << "]\t" << mthn << "'(' not found after TBMHeader.\n"; break;}
00096           loc2=line.find(")", loc1+1); if (loc2==npos) {cout<< __LINE__ << "]\t" << mthn << "')' not found after TBMHeader.\n"; break;}
00097           int TBMHeader=atoi(line.substr(loc1+1,loc2-loc1-1).c_str());
00098           
00099           pulseTrain[i]=UB;++i;
00100           pulseTrain[i]=UB;++i;
00101           pulseTrain[i]=UB;++i;
00102           pulseTrain[i]=B;++i;
00103           
00104           pixelTBMHeader=decimalToBaseX(TBMHeader, 4, 4);
00105 
00106           pulseTrain[i]=levelEncoder(pixelTBMHeader[3]);++i;
00107           pulseTrain[i]=levelEncoder(pixelTBMHeader[2]);++i;
00108           pulseTrain[i]=levelEncoder(pixelTBMHeader[1]);++i;
00109           pulseTrain[i]=levelEncoder(pixelTBMHeader[0]);++i;
00110         }
00111       else if (line.find("ROCHeader")!=std::string::npos)
00112         {
00113           loc1=line.find("(");         if (loc1==npos) {cout<< __LINE__ << "]\t" << mthn << "'(' not found after ROCHeader.\n"; break;}
00114           loc2=line.find(")", loc1+1); if (loc2==npos) {cout<< __LINE__ << "]\t" << mthn << "')' not found after ROCHeader.\n"; break;}
00115           int LastDAC=atoi(line.substr(loc1+1,loc2-loc1-1).c_str());
00116 
00117           std::cout<<"--------------"<<std::endl;
00118           
00119           pulseTrain[i]=UB;++i;
00120           pulseTrain[i]=B;++i;
00121           pulseTrain[i]=levelEncoder(LastDAC); ++i;
00122         }
00123       else if (line.find("PixelHit")!=std::string::npos) {
00124 
00125         loc1=line.find("(");         if (loc1==npos) {cout<< __LINE__ << "]\t" << mthn << "'(' not found after PixelHit.\n";                        break;}
00126         loc2=line.find(",", loc1+1); if (loc2==npos) {cout<< __LINE__ << "]\t" << mthn << "',' not found after the first argument of PixelHit.\n";  break;}
00127         loc3=line.find(",", loc2+1); if (loc3==npos) {cout<< __LINE__ << "]\t" << mthn << "'.' not found after the second argument of PixelHit.\n"; break;}
00128         loc4=line.find(")", loc3+1); if (loc4==npos) {cout<< __LINE__ << "]\t" << mthn << "')' not found after the third argument of PixelHit.\n";  break;}
00129         int column=atoi(line.substr(loc1+1, loc2-loc1-1).c_str());
00130         int row=atoi(line.substr(loc2+1, loc3-loc2-1).c_str());
00131         int charge=atoi(line.substr(loc3+1, loc4-loc3-1).c_str());
00132         
00133         DCol=int(column/2);
00134         LorR=int(column-DCol*2);
00135         pixelDCol=decimalToBaseX(DCol, 6, 2);
00136         pixelPxl=decimalToBaseX((80-row)*2+LorR, 6, 3);
00137 
00138         std::cout<<"Pxl = "<<pixelPxl[2]<<pixelPxl[1]<<pixelPxl[0]<<", DCol= "<<pixelDCol[1]<<pixelDCol[0]<<std::endl;
00139         
00140         pulseTrain[i]=levelEncoder(pixelDCol[1]);++i;
00141         pulseTrain[i]=levelEncoder(pixelDCol[0]);++i;
00142         pulseTrain[i]=levelEncoder(pixelPxl[2]);++i;
00143         pulseTrain[i]=levelEncoder(pixelPxl[1]);++i;
00144         pulseTrain[i]=levelEncoder(pixelPxl[0]);++i;
00145         pulseTrain[i]=charge;++i;
00146                         
00147       }
00148       else if (line.find("TBMTrailer")!=std::string::npos)
00149         {
00150           loc1=line.find("(");         if (loc1==npos) {cout<< __LINE__ << "]\t" << mthn <<"'(' not found after TBMTrailer.\n"; break;}
00151           loc2=line.find(")", loc1+1); if (loc2==npos) {cout<< __LINE__ << "]\t" << mthn <<"')' not found after TBMTrailer.\n"; break;}
00152           int TBMTrailer=atoi(line.substr(loc1+1,loc2-loc1-1).c_str());
00153           
00154           pulseTrain[i]=UB;++i;
00155           pulseTrain[i]=UB;++i;
00156           pulseTrain[i]=B; ++i;
00157           pulseTrain[i]=B; ++i;
00158           
00159           pixelTBMTrailer=decimalToBaseX(TBMTrailer, 4, 4);
00160           pulseTrain[i]=levelEncoder(pixelTBMTrailer[3]);++i;
00161           pulseTrain[i]=levelEncoder(pixelTBMTrailer[2]);++i;
00162           pulseTrain[i]=levelEncoder(pixelTBMTrailer[1]);++i;
00163           pulseTrain[i]=levelEncoder(pixelTBMTrailer[0]);++i;
00164         }
00165     }
00166 //   fin.close();
00167   dacs_=pulseTrain;
00168 }
00169 
00170 
00171 PixelFEDTestDAC::PixelFEDTestDAC(std::string filename) 
00172 {
00173 
00174   std::string mthn = "[PixelFEDTestDAC::PixelFEDTestDAC()]\t\t\t\t    " ;
00175   const unsigned long int UB=200;
00176   const unsigned long int B=500;
00177   const unsigned long int offset=0;
00178   vector <unsigned int> pulseTrain(256), pixelDCol(1), pixelPxl(2), pixelTBMHeader(3), pixelTBMTrailer(3);
00179   unsigned int DCol, LorR, start=15;
00180   std::string line;
00181   std::string::size_type loc1, loc2, loc3, loc4;
00182   unsigned long int npos=std::string::npos;
00183   int i;
00184   
00185   // Initialise the pulseTrain to offset+black
00186   for (unsigned int i=0;i<pulseTrain.size();++i)
00187     {
00188       pulseTrain[i]=offset+B;
00189     }
00190   
00191   ifstream fin(filename.c_str());
00192 
00193   i=start;
00194 
00195   getline(fin, line);
00196   mode_=line;
00197   assert(mode_=="EmulatedPhysics"||
00198          mode_=="FEDBaselineWithTestDACs"||
00199          mode_=="FEDAddressLevelWithTestDACs");
00200 
00201   while (!fin.eof())
00202     {
00203       getline(fin, line);
00204                 
00205       if (line.find("TBMHeader")!=npos)
00206         {
00207           loc1=line.find("(");         if (loc1==npos) {cout<< __LINE__ << "]\t" << mthn << "'(' not found after TBMHeader.\n"; break;}
00208           loc2=line.find(")", loc1+1); if (loc2==npos) {cout<< __LINE__ << "]\t" << mthn << "')' not found after TBMHeader.\n"; break;}
00209           int TBMHeader=atoi(line.substr(loc1+1,loc2-loc1-1).c_str());
00210           
00211           pulseTrain[i]=UB;++i;
00212           pulseTrain[i]=UB;++i;
00213           pulseTrain[i]=UB;++i;
00214           pulseTrain[i]=B;++i;
00215           
00216           pixelTBMHeader=decimalToBaseX(TBMHeader, 4, 4);
00217 
00218           pulseTrain[i]=levelEncoder(pixelTBMHeader[3]);++i;
00219           pulseTrain[i]=levelEncoder(pixelTBMHeader[2]);++i;
00220           pulseTrain[i]=levelEncoder(pixelTBMHeader[1]);++i;
00221           pulseTrain[i]=levelEncoder(pixelTBMHeader[0]);++i;
00222         }
00223       else if (line.find("ROCHeader")!=std::string::npos)
00224         {
00225           loc1=line.find("(");         if (loc1==npos) {cout<< __LINE__ << "]\t" << mthn << "'(' not found after ROCHeader.\n"; break;}
00226           loc2=line.find(")", loc1+1); if (loc2==npos) {cout<< __LINE__ << "]\t" << mthn << "')' not found after ROCHeader.\n"; break;}
00227           int LastDAC=atoi(line.substr(loc1+1,loc2-loc1-1).c_str());
00228 
00229           std::cout<<"--------------"<<std::endl;
00230           
00231           pulseTrain[i]=UB;++i;
00232           pulseTrain[i]=B;++i;
00233           pulseTrain[i]=levelEncoder(LastDAC); ++i;
00234         }
00235       else if (line.find("PixelHit")!=std::string::npos) {
00236 
00237         loc1=line.find("(");         if (loc1==npos) {cout<< __LINE__ << "]\t" << mthn <<"'(' not found after PixelHit.\n";                        break;}
00238         loc2=line.find(",", loc1+1); if (loc2==npos) {cout<< __LINE__ << "]\t" << mthn <<"',' not found after the first argument of PixelHit.\n";  break;}
00239         loc3=line.find(",", loc2+1); if (loc3==npos) {cout<< __LINE__ << "]\t" << mthn <<"'.' not found after the second argument of PixelHit.\n"; break;}
00240         loc4=line.find(")", loc3+1); if (loc4==npos) {cout<< __LINE__ << "]\t" << mthn <<"')' not found after the third argument of PixelHit.\n";  break;}
00241         int column=atoi(line.substr(loc1+1, loc2-loc1-1).c_str());
00242         int row=atoi(line.substr(loc2+1, loc3-loc2-1).c_str());
00243         int charge=atoi(line.substr(loc3+1, loc4-loc3-1).c_str());
00244         
00245         DCol=int(column/2);
00246         LorR=int(column-DCol*2);
00247         pixelDCol=decimalToBaseX(DCol, 6, 2);
00248         pixelPxl=decimalToBaseX((80-row)*2+LorR, 6, 3);
00249 
00250         std::cout<<"Pxl = "<<pixelPxl[2]<<pixelPxl[1]<<pixelPxl[0]<<", DCol= "<<pixelDCol[1]<<pixelDCol[0]<<std::endl;
00251         
00252         pulseTrain[i]=levelEncoder(pixelDCol[1]);++i;
00253         pulseTrain[i]=levelEncoder(pixelDCol[0]);++i;
00254         pulseTrain[i]=levelEncoder(pixelPxl[2]);++i;
00255         pulseTrain[i]=levelEncoder(pixelPxl[1]);++i;
00256         pulseTrain[i]=levelEncoder(pixelPxl[0]);++i;
00257         pulseTrain[i]=charge;++i;
00258                         
00259       }
00260       else if (line.find("TBMTrailer")!=std::string::npos)
00261         {
00262           loc1=line.find("(");         if (loc1==npos) {cout<< __LINE__ << "]\t" << mthn <<"'(' not found after TBMTrailer.\n"; break;}
00263           loc2=line.find(")", loc1+1); if (loc2==npos) {cout<< __LINE__ << "]\t" << mthn <<"')' not found after TBMTrailer.\n"; break;}
00264           int TBMTrailer=atoi(line.substr(loc1+1,loc2-loc1-1).c_str());
00265           
00266           pulseTrain[i]=UB;++i;
00267           pulseTrain[i]=UB;++i;
00268           pulseTrain[i]=B; ++i;
00269           pulseTrain[i]=B; ++i;
00270           
00271           pixelTBMTrailer=decimalToBaseX(TBMTrailer, 4, 4);
00272           pulseTrain[i]=levelEncoder(pixelTBMTrailer[3]);++i;
00273           pulseTrain[i]=levelEncoder(pixelTBMTrailer[2]);++i;
00274           pulseTrain[i]=levelEncoder(pixelTBMTrailer[1]);++i;
00275           pulseTrain[i]=levelEncoder(pixelTBMTrailer[0]);++i;
00276         }
00277     }
00278   fin.close();
00279   dacs_=pulseTrain;
00280 }
00281 
00282 
00283 unsigned int PixelFEDTestDAC::levelEncoder(int level){
00284 
00285   unsigned int pulse;
00286   
00287   switch (level)
00288     {
00289     case 0: pulse=450; break;
00290     case 1: pulse=500; break;
00291     case 2: pulse=550; break;
00292     case 3: pulse=600; break;
00293     case 4: pulse=650; break;
00294     case 5: pulse=700; break;
00295     default: assert(0); break;
00296     }
00297   
00298   return pulse;
00299 
00300 }
00301 
00302 
00303 vector<unsigned int> PixelFEDTestDAC::decimalToBaseX (unsigned int a, unsigned int x, unsigned int length){
00304 
00305   vector<unsigned int> ans(100,0);
00306   int i=0;
00307   
00308   while (a>0)
00309     {
00310       ans[i]=a%x;
00311       //ans.push_back(a%x);
00312       a=a/x;
00313       i+=1;
00314     }
00315   
00316   if (length>0) ans.resize(length); else ans.resize(i);
00317   
00318   return ans;
00319 }
00320 
00321 //=============================================================================================
00322 void PixelFEDTestDAC::writeXMLHeader(pos::PixelConfigKey key, 
00323                                      int version, 
00324                                      std::string path, 
00325                                      std::ofstream *outstream,
00326                                      std::ofstream *out1stream,
00327                                      std::ofstream *out2stream) const
00328 {
00329   std::string mthn = "[PixelFEDTestDAC::writeXMLHeader()]\t\t\t    " ;
00330   std::stringstream maskFullPath ;
00331 
00332 //  writeASCII(path) ;
00333 
00334   maskFullPath << path << "/PixelCalib_Test_" << PixelTimeFormatter::getmSecTime() << ".xml";
00335   std::cout << __LINE__ << "]\t" << mthn << "Writing to: " << maskFullPath.str() << std::endl ;
00336 
00337   outstream->open(maskFullPath.str().c_str()) ;
00338   
00339   *outstream << "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"                                                          << std::endl ;
00340   *outstream << "<ROOT xmlns:xsi='https://www.w3.org/2001/XMLSchema-instance'>"                                                     << std::endl ;
00341   *outstream << ""                                                                                                                 << std::endl ;
00342   *outstream << " <HEADER>"                                                                                                        << std::endl ;
00343   *outstream << "  <TYPE>"                                                                                                         << std::endl ;
00344   *outstream << "   <EXTENSION_TABLE_NAME>PIXEL_CALIB_CLOB</EXTENSION_TABLE_NAME>"                                                 << std::endl ;
00345   *outstream << "   <NAME>Calibration Object Clob</NAME>"                                                                          << std::endl ;
00346   *outstream << "  </TYPE>"                                                                                                        << std::endl ;
00347   *outstream << "  <RUN>"                                                                                                          << std::endl ;
00348   *outstream << "   <RUN_TYPE>PixelFEDTestDAC</RUN_TYPE>"                                                                          << std::endl ;
00349   *outstream << "   <RUN_NUMBER>1</RUN_NUMBER>"                                                                                    << std::endl ;
00350   *outstream << "   <RUN_BEGIN_TIMESTAMP>" << PixelTimeFormatter::getTime() << "</RUN_BEGIN_TIMESTAMP>"                            << std::endl ;
00351   *outstream << "   <LOCATION>CERN P5</LOCATION>"                                                                                  << std::endl ; 
00352   *outstream << "  </RUN>"                                                                                                         << std::endl ;
00353   *outstream << " </HEADER>"                                                                                                       << std::endl ;
00354   *outstream << ""                                                                                                                 << std::endl ;
00355   *outstream << " <DATA_SET>"                                                                                                      << std::endl ;
00356   *outstream << ""                                                                                                                 << std::endl ;
00357   *outstream << "  <VERSION>" << version << "</VERSION>"                                                                           << std::endl ;
00358   *outstream << "  <COMMENT_DESCRIPTION>No comment defined: this class does NOT inherit from PixelCalibBase</COMMENT_DESCRIPTION>" << std::endl ;
00359   *outstream << "  <CREATED_BY_USER>Unknown user</CREATED_BY_USER>"                                                                << std::endl ;
00360   *outstream << ""                                                                                                                 << std::endl ;
00361   *outstream << "  <PART>"                                                                                                         << std::endl ;
00362   *outstream << "   <NAME_LABEL>CMS-PIXEL-ROOT</NAME_LABEL>"                                                                       << std::endl ;
00363   *outstream << "   <KIND_OF_PART>Detector ROOT</KIND_OF_PART>"                                                                    << std::endl ;
00364   *outstream << "  </PART>"                                                                                                        << std::endl ;
00365 
00366 }
00367 
00368 //=============================================================================================
00369 void PixelFEDTestDAC::writeXML( std::ofstream *outstream,
00370                                 std::ofstream *out1stream,
00371                                 std::ofstream *out2stream) const 
00372 {
00373   std::string mthn = "[PixelFEDTestDAC::writeXML()]\t\t\t    " ;
00374   
00375 
00376   *outstream << " "                                                                                       << std::endl ;
00377   *outstream << "  <DATA>"                                                                                << std::endl ;
00378   *outstream << "   <CALIB_OBJ_DATA_FILE>./fedtestdac.dat</CALIB_OBJ_DATA_FILE>"                          << std::endl ;
00379   *outstream << "   <CALIB_TYPE>fedtestdac</CALIB_TYPE>"                                                  << std::endl ;
00380   *outstream << "  </DATA>"                                                                               << std::endl ;
00381   *outstream << " "                                                                                       << std::endl ;
00382 }
00383 
00384 
00385 //=============================================================================================
00386 void PixelFEDTestDAC::writeXMLTrailer(std::ofstream *outstream,
00387                                       std::ofstream *out1stream,
00388                                       std::ofstream *out2stream ) const 
00389 {
00390   std::string mthn = "[PixelFEDTestDAC::writeXMLTrailer()]\t\t\t    " ;
00391   
00392   *outstream << " </DATA_SET>"                                                                            << std::endl ;
00393   *outstream << "</ROOT>"                                                                                 << std::endl ;
00394   
00395   outstream->close() ;
00396   std::cout << __LINE__ << "]\t" << mthn << "Data written "                                               << std::endl ;
00397 
00398 }